url
stringlengths
49
92
description
stringlengths
22
4.78k
cases
listlengths
0
6
https://atcoder.jp/contests/abc365/tasks/abc365_b
Problem Statement You are given an integer sequence A=(A_1,\ldots,A_N) of length N . Here, A_1, A_2, \ldots, A_N are all distinct. Which element in A is the second largest?
[ { "input": "4\n8 2 5 1\n", "output": "3\n" }, { "input": "8\n1 2 3 4 5 10 9 11\n", "output": "6\n" } ]
https://atcoder.jp/contests/abc365/tasks/abc365_c
Problem Statement There are N people participating in an event, and the transportation cost for the i -th person is A_i yen. Takahashi, the organizer of the event, decided to set a maximum limit x for the transportation subsidy. The subsidy for person i will be \min(x, A_i) yen. Here, x must be a non-negative integer. Given that Takahashi's budget is M yen, and he wants the total transportation subsidy for all N people to be at most M yen, what is the maximum possible value of the subsidy limit x ? If the subsidy limit can be made infinitely large, report that instead.
[ { "input": "4 8\n1 3 2 4\n", "output": "2\n" }, { "input": "3 20\n5 3 2\n", "output": "infinite\n" }, { "input": "10 23\n2 5 6 5 2 1 7 9 7 2\n", "output": "2\n" } ]
https://atcoder.jp/contests/abc365/tasks/abc365_d
Problem Statement Takahashi and Aoki played rock-paper-scissors N times. [Note: In this game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock.] Aoki's moves are represented by a string S of length N consisting of the characters R , P , and S . The i -th character of S indicates Aoki's move in the i -th game: R for Rock, P for Paper, and S for Scissors. Takahashi's moves satisfy the following conditions: Takahashi never lost to Aoki. For i=1,2,\ldots,N-1 , Takahashi's move in the i -th game is different from his move in the (i+1) -th game. Determine the maximum number of games Takahashi could have won. It is guaranteed that there exists a sequence of moves for Takahashi that satisfies these conditions.
[ { "input": "6\nPRSSRS\n", "output": "5\n" }, { "input": "10\nSSSSSSSSSS\n", "output": "5\n" }, { "input": "24\nSPRPSRRRRRPPRPRPSSRSPRSS\n", "output": "18\n" } ]
https://atcoder.jp/contests/abc365/tasks/abc365_e
Problem Statement You are given an integer sequence A=(A_1,\ldots,A_N) of length N . Find the value of the following expression: \displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^N (A_i \oplus A_{i+1}\oplus \ldots \oplus A_j) . Notes on bitwise XOR The bitwise XOR of non-negative integers A and B , denoted as A \oplus B , is defined as follows: In the binary representation of A \oplus B , the digit at the 2^k ( k \geq 0 ) position is 1 if and only if exactly one of the digits at the 2^k position in the binary representations of A and B is 1 ; otherwise, it is 0 . For example, 3 \oplus 5 = 6 (in binary: 011 \oplus 101 = 110 ). In general, the bitwise XOR of k integers p_1, \dots, p_k is defined as (\cdots ((p_1 \oplus p_2) \oplus p_3) \oplus \cdots \oplus p_k) . It can be proved that this is independent of the order of p_1, \dots, p_k .
[ { "input": "3\n1 3 2\n", "output": "3\n" }, { "input": "7\n2 5 6 5 2 1 7\n", "output": "83\n" } ]
https://atcoder.jp/contests/abc365/tasks/abc365_f
Problem Statement There are infinitely many cells on a plane. For every pair of integers (x,y) , there is a corresponding cell, which we will call cell (x,y) . Each cell is either an empty cell or a wall cell. You are given two sequences of positive integers of length N : L=(L _ 1,L _ 2,\dotsc,L _ N) and U=(U _ 1,U _ 2,\dotsc,U _ N) . Here, L _ i and U _ i satisfy 1\leq L _ i\leq U _ i\leq10 ^ 9 for i=1,2,\ldots,N . All cells (x,y)\ (1\leq x\leq N,L _ x\leq y\leq U _ x) are empty cells, and all other cells are wall cells. When Takahashi is at an empty cell (x,y) , he can perform one of the following actions. If cell (x+1,y) is an empty cell, move to cell (x+1,y) . If cell (x-1,y) is an empty cell, move to cell (x-1,y) . If cell (x,y+1) is an empty cell, move to cell (x,y+1) . If cell (x,y-1) is an empty cell, move to cell (x,y-1) . It is guaranteed that he can travel between any two empty cells by repeating his actions. Answer Q queries in the following format. For the i -th query (1\leq i\leq Q) , you are given a quadruple of integers (s _ {x,i},s _ {y,i},t _ {x,i},t _ {y,i}) . Find the minimum number of actions required for Takahashi to move from cell (s _ {x,i},s _ {y,i}) to cell (t _ {x,i},t _ {y,i}) . For each query, it is guaranteed that the given two cells are empty cells.
[ { "input": "7\n1 5\n3 3\n1 3\n1 1\n1 4\n2 4\n3 5\n3\n1 4 6 3\n1 4 1 1\n7 5 1 5\n", "output": "10\n3\n14\n" }, { "input": "12\n1 1000000000\n1000000000 1000000000\n1 1000000000\n1 1\n1 1000000000\n1000000000 1000000000\n1 1000000000\n1 1\n1 1000000000\n1000000000 1000000000\n1 1000000000\n1 1\n1\n1 1 12 1\n", "output": "6000000005\n" }, { "input": "10\n1694 7483\n3396 5566\n2567 6970\n1255 3799\n2657 3195\n3158 8007\n3368 8266\n1447 6359\n5365 8614\n3141 7245\n15\n3 3911 6 4694\n7 5850 10 4641\n1 5586 6 4808\n2 3401 8 2676\n3 3023 6 6923\n8 4082 3 6531\n6 3216 7 6282\n8 5121 8 3459\n8 4388 1 6339\n6 6001 3 6771\n10 5873 8 5780\n1 6512 6 6832\n8 5345 7 4975\n10 4010 8 2355\n7 5837 9 6279\n", "output": "2218\n1212\n4009\n1077\n3903\n4228\n3067\n1662\n4344\n6385\n95\n6959\n371\n4367\n444\n" } ]
https://atcoder.jp/contests/abc365/tasks/abc365_g
Problem Statement N people work at the AtCoder office. The office keeps records of entries and exits, and there have been M entries and exits since the records began. The i -th (1\leq i\leq M) record is represented by a pair of integers (T_i, P_i) , indicating that at time T_i , the P_i -th person either entered the office if they were outside, or exited the office if they were inside. It is known that all people were outside the office at the beginning of the records, and they are outside now. Answer Q queries in the following format. For the i -th (1\leq i\leq Q) query, you are given a pair of integers (A_i, B_i) . Find the total length of the periods during which both the A_i -th and B_i -th persons were inside the office since the records began.
[ { "input": "3 8\n10 1\n20 2\n30 1\n40 3\n60 3\n70 1\n80 2\n90 1\n3\n1 2\n1 3\n2 3\n", "output": "20\n0\n20\n" }, { "input": "10 20\n10257 9\n10490 4\n19335 1\n25893 5\n32538 9\n33433 3\n38522 9\n40629 9\n42896 5\n52106 1\n53024 3\n55610 5\n56721 9\n58286 9\n63128 3\n70513 3\n70977 4\n74936 5\n79883 9\n95116 9\n7\n1 3\n3 9\n1 9\n4 9\n1 5\n5 9\n3 5\n", "output": "18673\n2107\n15310\n25720\n17003\n10317\n16848\n" } ]
https://atcoder.jp/contests/codequeen2024-final-N9tn8QqD/tasks/codequeen2024_final_a
問題文 AtCoder Land は、 10 時ちょうど、 15 時ちょうど、 17 時ちょうどの 3 つのタイミングでしか入場できません。 高橋君は X 時ちょうどに AtCoder Land に到着しました。最短で何時ちょうどに入園できますか? 但し、高橋君の到着と同じタイミングに AtCoder Land に入場できる場合も、高橋君は AtCoder Land に入場できるものとします。
[ { "input": "14\n", "output": "15\n" }, { "input": "17\n", "output": "17\n" }, { "input": "6\n", "output": "10\n" } ]
https://atcoder.jp/contests/codequeen2024-final-N9tn8QqD/tasks/codequeen2024_final_b
問題文 AtCoder Land では時報がモールス信号で行なわれます。高橋君はこのモールス信号を解読したいです。 N 個の文字列 S_1, S_2, \ldots, S_N が与えられます。 S_i (1 \leq i \leq N) は . と - からなる文字列で、 0 以上 9 以下のある数字に対応するモールス符号です。 N 個の文字列を復号し、順に連結して 1 つの文字列として出力してください。 数字とモールス符号の対応は以下の通りです。 0 ----- 1 .---- 2 ..--- 3 ...-- 4 ....- 5 ..... 6 -.... 7 --... 8 ---.. 9 ----.
[ { "input": "3\n...--\n.....\n---..\n", "output": "358\n" }, { "input": "10\n-----\n.----\n..---\n...--\n....-\n.....\n-....\n--...\n---..\n----.\n", "output": "0123456789\n" } ]
https://atcoder.jp/contests/codequeen2024-final-N9tn8QqD/tasks/codequeen2024_final_c
問題文 高橋君は AtCoder Land にある屋外アトラクションに K 回乗ろうとしています。このアトラクションの一回の所要時間は T です。 現在の時刻は 0 です。時刻 0 から N までの降水量が与えられます。時刻 i から時刻 i+1 までの降水量は P_i です ( 0\leq i\lt N )。アトラクションには時刻 0,1,…,N−T に乗ることができます。時刻 t にアトラクションに乗ると時刻 t+T にアトラクションから降りることができ、アトラクションに乗っている間に P_t+P_{t+1}+\dots+P_{t+T-1} だけ濡れます。 高橋君はアトラクションに乗っている間にできるだけ雨に濡れないようにしたいです。時刻 N までに K 回アトラクションに乗るとき、高橋君が濡れる量の総和の最小値を求めてください。ただし、乗り換えにかかる時間や待ち時間は 0 とし、アトラクションから降りた時刻に新たにアトラクションに乗ることができるものとします。
[ { "input": "8 3 2\n3 5 10 4 1 7 3 9\n", "output": "23\n" }, { "input": "5 1 3\n1000 1 10000 100 10\n", "output": "10101\n" }, { "input": "15 5 2\n401054171 63773035 986525245 157986893 799814573 139201145 649233932 351289844 409065258 406122133 957285954 529460482 21179081 795984182 727882733\n", "output": "3522058414\n" } ]
https://atcoder.jp/contests/codequeen2024-final-N9tn8QqD/tasks/codequeen2024_final_d
問題文 AtCoder Land には N 個の地点と M 個の道があります。 i 個目の道は地点 a_i と b_i を双方向に結び、通過するのに d_i 分かかります。 これから、AtCoder Land で K 個のイベントが開催されます。 i 個目のイベントに参加するには、時刻 t_i 分に地点 c_i にいる必要があります。イベントにかかる時間は 0 分です。 あなたは、時刻 0 分に地点 1 にいます。適切に行動したとき、最大で何個のイベントに参加できるか求めてください。
[ { "input": "3 3 3\n1 2 1\n1 3 3\n2 3 7\n1 8\n2 2\n3 7\n", "output": "2\n" }, { "input": "1 0 2\n1 1\n1 100\n", "output": "2\n" }, { "input": "5 8 10\n1 2 149622151\n1 4 177783960\n4 5 118947237\n1 3 33222944\n1 5 295060863\n3 5 110881471\n2 3 34104208\n3 4 273071547\n2 650287940\n4 839296263\n3 462224593\n1 492601449\n4 384836991\n1 191890310\n5 576823355\n3 782177068\n3 404011431\n1 818008580\n", "output": "6\n" } ]
https://atcoder.jp/contests/codequeen2024-final-N9tn8QqD/tasks/codequeen2024_final_e
問題文 高橋くん一行は、AtCoder Land に行くためにランド内にあるホテルに泊まることにしました。 N 人の人と M 個のランクが付いた部屋があります。 i 番目の人はランクが A_i 以上の部屋に泊まりたいと考えています。 また、 i 番目の部屋のランクは R_i 、定員は B_i 人、客室料金は C_i 円です。 C_i 円払うことで、 B_i 人以下であれば何人でも i 番目の部屋に泊まることができます。 N 人全員が部屋に泊まることが可能か判定し、可能な場合必要な金額の総和の最小値を求めてください。
[ { "input": "4 3\n3 5 1 4\n5 3 3\n3 1 1\n2 3 2\n", "output": "4\n" }, { "input": "8 5\n2 5 1 5 2 1 2 4\n4 2 5\n7 2 4\n7 4 2\n1 4 7\n3 3 8\n", "output": "11\n" }, { "input": "10 1\n1 1 1 1 1 1 1 1 1 1\n10 4 1\n", "output": "-1\n" } ]
https://atcoder.jp/contests/codequeen2024-final-N9tn8QqD/tasks/codequeen2024_final_f
問題文 AtCoder Land で高橋君と青木君はイチゴの乗ったケーキを食べることになりました。 AtCoder Landのケーキは円形で、 N 個の半径方向の切れ込みによって N 個の扇形ピースに区切られています。 切れ込みを時計回りの順に切れ込み 1 , 切れ込み 2 , \ldots , 切れ込み N と番号をふったとき、時計回りに切れ込み i (1 \leq i \leq N-1) から切れ込み i+1 までの間の部分をピース i と呼びます。また、時計回りに切れ込み N から切れ込み 1 までの間の部分をピース N と呼びます。 ピース i (1 \leq i \leq N) の上には A_i 個のイチゴが乗っています。 高橋君と青木君はこれから以下のゲームをします。 まず、高橋君が N 個の切れ込みのなかから 1 つを選ぶ。選んだ切れ込みを切れ込み i とする。 次に、青木君が高橋君の選んだ切れ込み以外の N-1 個の切れ込みのなかから 1 つを選ぶ。選んだ切れ込みを切れ込み j とする。 高橋君は切れ込み i から時計回りに見ていって、切れ込み j までの範囲のピースをすべてもらう。 青木君は、残りのピースをすべてもらう。 高橋君がもらったピースの 1 ピースあたりのイチゴの個数の平均値が青木君がもらったピースの 1 ピースあたりのイチゴの個数の平均値以上であるとき、高橋君の勝ちです。そうでないとき、青木君の勝ちです。 青木君が勝つために最適な切れ込みを選ぶとき、高橋君は必ず勝つためにはどの切れ込みを選べばよいか求めてください。そのような切れ込みが、存在しないときは -1 を、複数存在する場合はそのうち番号が最小のものを答えてください。
[ { "input": "3\n3 8 5\n", "output": "2\n" }, { "input": "15\n4096 64 512 1 256 16384 8 1024 2048 2 128 32 4 16 8192\n", "output": "15\n" } ]
https://atcoder.jp/contests/codequeen2024-final-N9tn8QqD/tasks/codequeen2024_final_g
問題文 高橋くん一行は、AtCoder Land 内のアイス屋さんにやってきました。 アイス屋さんには N 種類のフレーバーのアイスがあり、アイスはそれぞれ十分な量の在庫があります。 このアイス屋さんでは、 5 段重ねのアイスを購入することができます。 使う 5 つのフレーバーは(重複も含めて)自由に選ぶことができますが、あるアイスの上に直接同じ味のアイスを重ねることはできません。 つまり、このアイス屋さんで購入できる 5 段重ねのアイスは N\times(N-1) ^ 4 通りあります。 高橋くんはこのアイス屋さんのクーポンを持っており、 i=1,2,\ldots,N すべてに対して i 番目のフレーバーのアイスを注文した数が C _ i 個以下なら、クーポンを使って購入することができます。 売られているアイスの定価は非常に高いため、高橋くんはクーポンを使って購入できる個数を超えてアイスを購入することはできません。 高橋くんは、なるべく多くの友達に 5 段重ねのアイスを買ってあげたいと思っています。 5 段重ねのアイスを最大でいくつ買うことができるか求め、それを実現するアイスの注文をひとつ構成してください。 厳密には、次のようになります。 次の条件をすべて満たす K\times5 項の列 (A _ {i,j}) _ {1\leq i\leq K,1\leq j\leq 5} が存在するような非負整数 K の最大値を求め、その K に対する具体的な (A _ {i,j}) をひとつ構成してください。 1\leq A _ {i,j}\leq N\ (1\leq i\leq K,1\leq j\leq 5) A _ {i,j}\neq A _ {i,j+1}\ (1\leq i\leq K,1\leq j\leq 4) x=1,2,\dotsc,N に対して、 A _ {i,j}=x となる組 (i,j) の個数は C _ x 個以下 ただし、条件を満たす (A _ {i,j}) が複数存在する場合、どれを出力しても構いません。
[ { "input": "5\n3 1 4 2 5\n", "output": "3\n1 2 1 3 1\n3 4 5 3 5\n5 3 5 4 5\n" }, { "input": "3\n1 2 1000\n", "output": "1\n3 1 3 2 3\n" }, { "input": "1\n3\n", "output": "0\n" }, { "input": "10\n2 1 8 6 1 2 1 6 9 1\n", "output": "7\n1 2 1 3 4\n3 4 3 4 3\n3 4 3 4 3\n3 4 5 6 7\n6 8 9 8 9\n9 8 9 8 9\n9 8 9 8 9\n" } ]
https://atcoder.jp/contests/codequeen2024-final-N9tn8QqD/tasks/codequeen2024_final_h
問題文 AtCoder Land には N 個のアトラクションがあります。 i 番目のアトラクションには、以下の 2 通りの搭乗方法があります。 通常通り搭乗する。 A_i 分後に搭乗が終了する。 ファストパスを使って搭乗する。 B_i 分後に搭乗が終了する。このとき、ファストパスが 1 枚消費される。 あなたはファストパスを K 枚持っています。 同じアトラクションに複数回搭乗しないものとすると、 T 分後までに最大でいくつのアトラクションに搭乗できますか? 但し、アトラクションを乗り換える時間は無視できること、最後に搭乗するアトラクションについて T 分後までに搭乗が終了している必要があることに注意してください。
[ { "input": "5 2 20\n7 4\n10 8\n3 3\n8 7\n9 5\n", "output": "4\n" }, { "input": "5 5 1\n1000000000000 1000000000000\n1000000000000 1000000000000\n1000000000000 1000000000000\n1000000000000 1000000000000\n1000000000000 1000000000000\n", "output": "0\n" }, { "input": "5 5 1000000000000000000\n1000000000000 1000000000000\n1000000000000 1000000000000\n1000000000000 1000000000000\n1000000000000 1000000000000\n1000000000000 1000000000000\n", "output": "5\n" }, { "input": "15 4 60\n9 4\n8 8\n10 10\n7 6\n10 3\n6 6\n8 7\n9 5\n6 5\n4 2\n1 1\n7 6\n8 6\n6 4\n4 3\n", "output": "12\n" } ]
https://atcoder.jp/contests/abc364/tasks/abc364_a
Problem Statement Takahashi is planning to eat N dishes. The i -th dish he plans to eat is sweet if S_i = sweet , and salty if S_i = salty . If he eats two sweet dishes consecutively, he will feel sick and be unable to eat any more dishes. Determine whether he can eat all the dishes.
[ { "input": "5\nsalty\nsweet\nsalty\nsalty\nsweet\n", "output": "Yes\n" }, { "input": "4\nsweet\nsalty\nsweet\nsweet\n", "output": "Yes\n" }, { "input": "6\nsalty\nsweet\nsweet\nsalty\nsweet\nsweet\n", "output": "No\n" } ]
https://atcoder.jp/contests/abc364/tasks/abc364_b
Problem Statement There is a grid with H rows and W columns. Let (i, j) denote the cell at the i -th row from the top and j -th column from the left. Cell (i, j) is empty if C_{i, j} is . , and not empty if C_{i, j} is # . Takahashi is currently at cell (S_i, S_j) , and he will act according to the following rules for i = 1, 2, \ldots, |X| in order. If the i -th character of X is L , and the cell to the left of his current cell exists and is empty, he moves to the cell to the left. Otherwise, he stays in the current cell. If the i -th character of X is R , and the cell to the right of his current cell exists and is empty, he moves to the cell to the right. Otherwise, he stays in the current cell. If the i -th character of X is U , and the cell above his current cell exists and is empty, he moves to the cell above. Otherwise, he stays in the current cell. If the i -th character of X is D , and the cell below his current cell exists and is empty, he moves to the cell below. Otherwise, he stays in the current cell. Print the cell where he is after completing the series of actions.
[ { "input": "2 3\n2 1\n.#.\n...\nULDRU\n", "output": "2 2\n" }, { "input": "4 4\n4 2\n....\n.#..\n...#\n....\nDUUUURULRD\n", "output": "2 4\n" }, { "input": "6 6\n1 1\n.#####\n######\n######\n######\n######\n######\nRURLDLULLRULRDL\n", "output": "1 1\n" } ]
https://atcoder.jp/contests/abc364/tasks/abc364_c
Problem Statement There are N dishes, and the i -th dish has a sweetness of A_i and a saltiness of B_i . Takahashi plans to arrange these N dishes in any order he likes and eat them in that order. He will eat the dishes in the arranged order, but he will stop eating as soon as the total sweetness of the dishes he has eaten exceeds X or the total saltiness exceeds Y . Find the minimum possible number of dishes that he will end up eating.
[ { "input": "4 7 18\n2 3 5 1\n8 8 1 4\n", "output": "2\n" }, { "input": "5 200000000000000 200000000000000\n1 1 1 1 1\n2 2 2 2 2\n", "output": "5\n" }, { "input": "8 30 30\n1 2 3 4 5 6 7 8\n8 7 6 5 4 3 2 1\n", "output": "6\n" } ]
https://atcoder.jp/contests/abc364/tasks/abc364_d
Problem Statement There are N+Q points A_1,\dots,A_N,B_1,\dots,B_Q on a number line, where point A_i has a coordinate a_i and point B_j has a coordinate b_j . For each j=1,2,\dots,Q , answer the following question: Let X be the point among A_1,A_2,\dots,A_N that is the k_j -th closest to point B_j . Find the distance between points X and B_j . More formally, let d_i be the distance between points A_i and B_j . Sort (d_1,d_2,\dots,d_N) in ascending order to get the sequence (d_1',d_2',\dots,d_N') . Find d_{k_j}' .
[ { "input": "4 3\n-3 -1 5 6\n-2 3\n2 1\n10 4\n", "output": "7\n3\n13\n" }, { "input": "2 2\n0 0\n0 1\n0 2\n", "output": "0\n0\n" }, { "input": "10 5\n-84 -60 -41 -100 8 -8 -52 -62 -61 -76\n-52 5\n14 4\n-2 6\n46 2\n26 7\n", "output": "11\n66\n59\n54\n88\n" } ]
https://atcoder.jp/contests/abc364/tasks/abc364_e
Problem Statement Takahashi has prepared N dishes for Snuke. The dishes are numbered from 1 to N , and dish i has a sweetness of A_i and a saltiness of B_i . Takahashi can arrange these dishes in any order he likes. Snuke will eat the dishes in the order they are arranged, but if at any point the total sweetness of the dishes he has eaten so far exceeds X or the total saltiness exceeds Y , he will not eat any further dishes. Takahashi wants Snuke to eat as many dishes as possible. Find the maximum number of dishes Snuke will eat if Takahashi arranges the dishes optimally.
[ { "input": "4 8 4\n1 5\n3 2\n4 1\n5 3\n", "output": "3\n" }, { "input": "2 1 1\n3 2\n3 2\n", "output": "1\n" }, { "input": "2 100 100\n3 2\n3 2\n", "output": "2\n" }, { "input": "6 364 463\n230 381\n154 200\n328 407\n339 94\n193 10\n115 309\n", "output": "3\n" } ]
https://atcoder.jp/contests/abc364/tasks/abc364_f
Problem Statement There is a graph with N + Q vertices, numbered 1, 2, \ldots, N + Q . Initially, the graph has no edges. For this graph, perform the following operation for i = 1, 2, \ldots, Q in order: For each integer j satisfying L_i \leq j \leq R_i , add an undirected edge with cost C_i between vertices N + i and j . Determine if the graph is connected after all operations are completed. If it is connected, find the cost of a minimum spanning tree of the graph. A minimum spanning tree is a spanning tree with the smallest possible cost, and the cost of a spanning tree is the sum of the costs of the edges used in the spanning tree.
[ { "input": "4 3\n1 2 2\n1 3 4\n2 4 5\n", "output": "22\n" }, { "input": "6 2\n1 2 10\n4 6 10\n", "output": "-1\n" }, { "input": "200000 4\n1 200000 1000000000\n1 200000 998244353\n1 200000 999999999\n1 200000 999999999\n", "output": "199651870599998\n" } ]
https://atcoder.jp/contests/abc364/tasks/abc364_g
Problem Statement The Country of AtCoder consists of N cities and M roads connecting them, and one can travel between any two cities by traversing some roads. The cities are numbered from 1 to N , and the roads are numbered from 1 to M . Road i connects cities A_i and B_i bidirectionally. Due to increasing traffic in the country, some roads are planned to be expanded. Currently, no roads have been expanded, and the cost to expand road i is C_i . Since it is difficult to expand all roads at once, the plan is to first designate K out of the N cities as major cities and perform the minimum necessary expansion work so that one can travel between any two major cities using only expanded roads. It has already been decided that cities 1, 2, \dots, K-1 will be major cities, but the last major city has not yet been decided. For each i=K, K+1, \dots, N , answer the following question: If city i is designated as the last major city, what is the minimum total cost of the expansion work required to ensure that one can travel between any two major cities using only expanded roads?
[ { "input": "4 5 3\n1 4 3\n3 4 4\n1 2 4\n2 3 2\n1 3 1\n", "output": "3\n6\n" }, { "input": "4 3 2\n2 4 28\n1 4 56\n1 3 82\n", "output": "84\n82\n56\n" }, { "input": "6 12 4\n2 6 68\n2 5 93\n4 6 28\n2 4 89\n3 6 31\n1 3 10\n1 2 53\n3 5 1\n3 5 74\n3 4 22\n4 5 80\n3 4 35\n", "output": "85\n64\n94\n" } ]
https://atcoder.jp/contests/ahc035/tasks/ahc035_a
Problem Statement There is a field represented by an N \times N grid. Let (0, 0) be the coordinates of the top-left square, and (i, j) be the coordinates of the square located i squares down and j squares to the right from there. You are given 2N(N-1) grain seeds. There are M evaluation criteria, and each seed k has a non-negative integer vector, called the evaluation vector, \boldsymbol{x_k}=(x_{k,0}, x_{k,1}, \cdots, x_{k,M-1}) , representing the goodness for each criterion. The value V_k of seed k is defined as V_k=\sum_{l=0}^{M-1}x_{k,l} . Repeat the following operation T times to maximize the maximum value \max (V_0, V_1, \cdots, V_{2N(N-1)-1}) of the seeds you have. Plant one seed in each of the N^2 squares of the field. The same seed cannot be planted in multiple squares, and the remaining 2N(N-1)-N^2 seeds that are not planted will be discarded. Then, for each pair of seeds (k, k') planted in adjacent squares (up, down, left, or right), a new seed with a new evaluation vector will be generated. The value of the l -th element of this new evaluation vector is chosen uniformly at random from the original values x_{k,l} and x_{k',l} . The 2N(N-1) seeds generated in this way will become the new set of seeds you have. Example of generating new seeds For example, when M=3 , consider planting seed 0 in square (0, 0) and seed 1 in square (0, 1) . If the evaluation vector of seed 0 is \boldsymbol{x_0}=(1, 2, 3) and the evaluation vector of seed 1 is \boldsymbol{x_1}=(4, 5, 6) , the possible evaluation vectors for the new seed are the following 8 possibilities: (1, 2, 3) (1, 2, 6) (1, 5, 3) (1, 5, 6) (4, 2, 3) (4, 2, 6) (4, 5, 3) (4, 5, 6) One of these 8 evaluation vectors is chosen uniformly at random and becomes the evaluation vector for the new seed.
[]
https://atcoder.jp/contests/abc363/tasks/abc363_a
Problem Statement In AtCoder, a user's rating is given as a positive integer, and based on this value, a certain number of ^ is displayed. Specifically, when the rating is between 1 and 399 , inclusive, the display rules are as follows: When the rating is between 1 and 99 , inclusive, ^ is displayed once. When the rating is between 100 and 199 , inclusive, ^ is displayed twice. When the rating is between 200 and 299 , inclusive, ^ is displayed three times. When the rating is between 300 and 399 , inclusive, ^ is displayed four times. Currently, Takahashi's rating is R . Here, it is guaranteed that R is an integer between 1 and 299 , inclusive. Find the minimum increase in rating required for him to increase the number of displayed ^ . It can be proved that under the constraints of this problem, he can increase the number of ^ without raising his rating to 400 or above.
[ { "input": "123\n", "output": "77\n" }, { "input": "250\n", "output": "50\n" } ]
https://atcoder.jp/contests/abc363/tasks/abc363_b
Problem Statement There are N people, and the current hair length of the i -th person (1 \leq i \leq N) is L_i . Each person's hair grows by 1 per day. Print the number of days after which the number of people whose hair length is at least T becomes P or more for the first time. If there are already P or more people whose hair length is at least T now, print 0 .
[ { "input": "5 10 3\n3 11 1 6 2\n", "output": "7\n" }, { "input": "2 5 2\n10 10\n", "output": "0\n" }, { "input": "3 10 1\n1 2 3\n", "output": "7\n" } ]
https://atcoder.jp/contests/abc363/tasks/abc363_c
Problem Statement You are given a string S of length N consisting only of lowercase English letters. Find the number of strings obtained by permuting the characters of S (including the string S itself) that do not contain a palindrome of length K as a substring. Here, a string T of length N is said to "contain a palindrome of length K as a substring" if and only if there exists a non-negative integer i not greater than (N-K) such that T_{i+j} = T_{i+K+1-j} for every integer j with 1 \leq j \leq K . Here, T_k denotes the k -th character of the string T .
[ { "input": "3 2\naab\n", "output": "1\n" }, { "input": "5 3\nzzyyx\n", "output": "16\n" }, { "input": "10 5\nabcwxyzyxw\n", "output": "440640\n" } ]
https://atcoder.jp/contests/abc363/tasks/abc363_d
Problem Statement A non-negative integer X is called a palindrome number if its decimal representation (without leading zeros) is a palindrome. For example, 363 , 12344321 , and 0 are all palindrome numbers. Find the N -th smallest palindrome number.
[ { "input": "46\n", "output": "363\n" }, { "input": "1\n", "output": "0\n" }, { "input": "1000000000000000000\n", "output": "90000000000000000000000000000000009\n" } ]
https://atcoder.jp/contests/abc363/tasks/abc363_e
Problem Statement There is an island of size H \times W , surrounded by the sea. The island is divided into H rows and W columns of 1 \times 1 sections, and the elevation of the section at the i -th row from the top and the j -th column from the left (relative to the current sea level) is A_{i,j} . Starting from now, the sea level rises by 1 each year. Here, a section that is vertically or horizontally adjacent to the sea or a section sunk into the sea and has an elevation not greater than the sea level will sink into the sea. Here, when a section newly sinks into the sea, any vertically or horizontally adjacent section with an elevation not greater than the sea level will also sink into the sea simultaneously, and this process repeats for the newly sunk sections. For each i=1,2,\ldots, Y , find the area of the island that remains above sea level i years from now.
[ { "input": "3 3 5\n10 2 10\n3 1 4\n10 5 10\n", "output": "9\n7\n6\n5\n4\n" }, { "input": "3 5 3\n2 2 3 3 3\n2 1 2 1 3\n2 2 3 3 3\n", "output": "15\n7\n0\n" } ]
https://atcoder.jp/contests/abc363/tasks/abc363_f
Problem Statement You are given an integer N . Print a string S that satisfies all of the following conditions. If no such string exists, print -1 . S is a string of length between 1 and 1000 , inclusive, consisting of the characters 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , and * (multiplication symbol). S is a palindrome. The first character of S is a digit. The value of S when evaluated as a formula equals N .
[ { "input": "363\n", "output": "11*3*11\n" }, { "input": "101\n", "output": "-1\n" }, { "input": "3154625100\n", "output": "2*57*184481*75*2\n" } ]
https://atcoder.jp/contests/abc363/tasks/abc363_g
Problem Statement You are given two sequences of length N : D=(D_1, D_2, \dots, D_N) and P=(P_1, P_2, \dots, P_N) . Process Q queries in the order they are given. Each query is given in the following format: c x y : Change D_c to x and P_c to y . Then, solve the following problem and print the answer. There are N jobs numbered 1 to N . Starting from today (consider this as day 1 ), you will choose and complete one job per day for N days. If you complete job i on or before day D_i , you will receive a reward of P_i . (If you do not complete it by day D_i , you get nothing.) Find the maximum total reward you can achieve by choosing the optimal order of completing the jobs.
[ { "input": "3 2\n1 2 3\n3 6 3\n3 1 4\n2 3 9\n", "output": "10\n13\n" }, { "input": "5 1\n1 2 3 4 5\n1000000000 1000000000 1000000000 1000000000 1000000000\n1 1 1000000000\n", "output": "5000000000\n" }, { "input": "10 10\n6 2 4 1 5 1 6 6 5 3\n45 65 71 52 86 52 48 60 40 98\n5 6 5\n8 4 34\n6 7 83\n1 3 21\n7 5 85\n7 4 51\n8 2 81\n2 7 54\n6 1 5\n8 6 30\n", "output": "394\n379\n462\n457\n459\n414\n443\n479\n401\n396\n" } ]
https://atcoder.jp/contests/abc362/tasks/abc362_a
Problem Statement Takahashi came to a store to buy a pen. Here, a red pen costs R yen, a green pen costs G yen, and a blue pen costs B yen. Takahashi dislikes the color C . If C is Red , he cannot buy a red pen; if C is Green , he cannot buy a green pen; and if C is Blue , he cannot buy a blue pen. Determine the minimum amount of money he needs to buy one pen.
[ { "input": "20 30 10\nBlue\n", "output": "20\n" }, { "input": "100 100 100\nRed\n", "output": "100\n" }, { "input": "37 39 93\nBlue\n", "output": "37\n" } ]
https://atcoder.jp/contests/abc362/tasks/abc362_b
Problem Statement In the xy -plane, there are three points A(x_A, y_A) , B(x_B, y_B) , and C(x_C, y_C) that are not collinear. Determine whether the triangle ABC is a right triangle.
[ { "input": "0 0\n4 0\n0 3\n", "output": "Yes\n" }, { "input": "-4 3\n2 1\n3 4\n", "output": "Yes\n" }, { "input": "2 4\n-3 2\n1 -2\n", "output": "No\n" } ]
https://atcoder.jp/contests/abc362/tasks/abc362_c
Problem Statement You are given N pairs of integers (L_1, R_1), (L_2, R_2), \ldots, (L_N, R_N) . Determine whether there exists a sequence of N integers X = (X_1, X_2, \ldots, X_N) that satisfies the following conditions, and print one such sequence if it exists. L_i \leq X_i \leq R_i for each i = 1, 2, \ldots, N . \displaystyle \sum_{i=1}^N X_i = 0 .
[ { "input": "3\n3 5\n-4 1\n-2 3\n", "output": "Yes\n4 -3 -1\n" }, { "input": "3\n1 2\n1 2\n1 2\n", "output": "No\n" }, { "input": "6\n-87 12\n-60 -54\n2 38\n-76 6\n87 96\n-17 38\n", "output": "Yes\n-66 -57 31 -6 89 9\n" } ]
https://atcoder.jp/contests/abc362/tasks/abc362_d
Problem Statement You are given a simple connected undirected graph with N vertices and M edges. Each vertex i\,(1\leq i \leq N) has a weight A_i . Each edge j\,(1\leq j \leq M) connects vertices U_j and V_j bidirectionally and has a weight B_j . The weight of a path in this graph is defined as the sum of the weights of the vertices and edges that appear on the path. For each i=2,3,\dots,N , solve the following problem: Find the minimum weight of a path from vertex 1 to vertex i .
[ { "input": "3 3\n1 2 3\n1 2 1\n1 3 6\n2 3 2\n", "output": "4 9\n" }, { "input": "2 1\n0 1\n1 2 3\n", "output": "4\n" }, { "input": "5 8\n928448202 994752369 906965437 942744902 907560126\n2 5 975090662\n1 2 908843627\n1 5 969061140\n3 4 964249326\n2 3 957690728\n2 4 942986477\n4 5 948404113\n1 3 988716403\n", "output": "2832044198 2824130042 4696218483 2805069468\n" } ]
https://atcoder.jp/contests/abc362/tasks/abc362_e
Problem Statement You are given a sequence A = (A_1, A_2, \dots, A_N) of length N . For each k = 1, 2, \dots, N , find the number, modulo 998244353 , of (not necessarily contiguous) subsequences of A of length k that are arithmetic sequences. Two subsequences are distinguished if they are taken from different positions, even if they are equal as sequences. What is a subsequence? A subsequence of a sequence A is a sequence obtained by deleting zero or more elements from A and arranging the remaining elements without changing the order.
[ { "input": "5\n1 2 3 2 3\n", "output": "5 10 3 0 0\n" }, { "input": "4\n1 2 3 4\n", "output": "4 6 2 1\n" }, { "input": "1\n100\n", "output": "1\n" } ]
https://atcoder.jp/contests/abc362/tasks/abc362_f
Problem Statement You are given a tree T with N vertices. The vertices are numbered 1 to N , and the i -th edge (1 \leq i \leq N-1) connects vertices u_i and v_i bidirectionally. Using T , define a complete graph G with N vertices as follows: The weight w(x,y) of the edge between vertices x and y in G is the shortest distance between vertices x and y in T . Find one maximum weight maximum matching in G . That is, find a set of \lfloor N/2 \rfloor pairs of vertices M=\{(x_1,y_1),(x_2,y_2),\dots,(x_{\lfloor N/2 \rfloor},y_{\lfloor N/2 \rfloor})\} such that each vertex 1,2,\dots, N appears in M at most once, and \displaystyle \sum_{i=1}^{\lfloor N/2 \rfloor} w(x_i,y_i) is maximized.
[ { "input": "4\n1 2\n2 3\n3 4\n", "output": "2 4\n1 3\n" }, { "input": "3\n1 2\n2 3\n", "output": "1 3\n" } ]
https://atcoder.jp/contests/abc362/tasks/abc362_g
Problem Statement You are given a string S consisting of lowercase English letters. You are also given Q queries to process sequentially. The i -th query is described as follows: A string T_i consisting of lowercase English letters is given. Print the number of substrings of S that equal T_i . Two substrings are distinguished if they are taken from different positions, even if they are equal as strings.
[ { "input": "missisippi\n5\ni\ns\na\nis\nmissisippi\n", "output": "4\n3\n0\n2\n1\n" }, { "input": "aaaaaa\n6\na\naa\naaa\naaaa\naaaaa\naaaaaa\n", "output": "6\n5\n4\n3\n2\n1\n" } ]
https://atcoder.jp/contests/awtf2024-open/tasks/awtf2024_a
Problem Statement There are N slimes on a number line, and the i -th slime is at coordinate A_i . All these coordinates are distinct. Each slime has a weight of 1 . Also, you are given an integer K . First, you will choose K slimes and remove the others from the number line. Then, from time 0 , the chosen slimes will move as follows: Movement of each slime: Let R be the total weight of slimes at coordinates greater than its own, and L be the total weight of slimes at coordinates less than its own. Then, it moves with a velocity of R-L . Note that the velocity can be negative. That is, it moves in the negative direction if R-L<0 . When two or more slimes reach the same coordinate at the same time, they merge. The weight of the merged slime is the sum of the weights of the slimes before merging. The merged slime will move according to the same rules as above. The K slimes will continue to merge until there is only one slime left. Let t be the moment when this single slime is born. Your goal is to choose the K slimes so that t is maximized. Find the maximum value of t .
[ { "input": "3 2\n0 1 2\n", "output": "1.00000000000000000000\n" }, { "input": "4 3\n0 1 4 9\n", "output": "2.83333333333333333333\n" }, { "input": "4 4\n0 1 2 3\n", "output": "0.50000000000000000000\n" }, { "input": "20 6\n0 3441380 120768398 229897071 231209282 232046760 254924545 325399248 385631087 400098966 480503302 501372095 502644652 524585010 541761042 691400171 725009462 767549897 837806226 927396743\n", "output": "135453315.33333333333333333333\n" } ]
https://atcoder.jp/contests/awtf2024-open/tasks/awtf2024_b
Problem Statement You are given a string S of length N consisting of 0 and 1 . We define an inversion pair as a pair of integers (i, j) ( 1 \leq i < j \leq N ) such that the i -th character of S is 1 and the j -th character of S is 0 . As long as there is an inversion pair in S , perform the following operation: Randomly choose an inversion pair (i, j) . The choice is independent of previous choices and is uniformly random. Then, swap the i -th and j -th characters of S . Find the expected number of operations, modulo 998244353 . What is the expected value modulo 998244353 ? It can be proved that the sought expected value is always rational. Moreover, under the constraints of this problem, it can be proved that if the expected value is expressed as an irreducible fraction \frac{P}{Q} , then Q \neq 0 \pmod{998244353} . Thus, there exists a unique integer R such that R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353 . Report this R .
[ { "input": "2\n10\n", "output": "1\n" }, { "input": "3\n110\n", "output": "499122178\n" }, { "input": "1\n0\n", "output": "0\n" }, { "input": "10\n1011000010\n", "output": "133099253\n" }, { "input": "100\n0101110010001000111000111001001101001100000111110001010010001010101100011001011011101101100001100111\n", "output": "407907276\n" } ]
https://atcoder.jp/contests/awtf2024-open/tasks/awtf2024_c
Problem Statement Solve the following problem for T test cases. You are currently at coordinate 0 on a number line, and you want to reach coordinate L . You will use a car for the movement. This car runs on two types of fuel: types 1 and 2 . For each type, the car has a fuel tank of that type with a capacity of C liters. Currently, both fuel tanks are full. The car can consume x liters of either type of fuel (where x is any positive integer not exceeding the remaining fuel) to move x units in any direction on the number line. You can choose which type of fuel to use for each movement. There are N fuel stations on the number line. The i -th fuel station is located at coordinate X_i . When the car is exactly at coordinate X_i , you can buy type- K_i fuel at a cost of 1 per liter. Of course, you cannot buy more fuel than allowed by the tank capacity. Determine whether it is possible to reach coordinate L . If it is possible, find the minimum total cost of purchasing fuel.
[ { "input": "5\n1 10 4\n7\n1\n1 10 6\n7\n1\n2 12 3\n5 7\n1 1\n2 12 3\n5 7\n1 2\n20 749013197 23809523\n46981984 70791437 118235723 132421762 180040807 203849360 251468335 275277857 322889975 346699150 394318091 418113855 465732891 489532137 537144103 558852533 606466719 630275002 677584754 701394209\n1 2 2 1 1 2 1 2 1 2 2 1 2 1 2 1 1 2 1 2\n", "output": "2\n0\n-1\n6\n585545066743659\n" } ]
https://atcoder.jp/contests/awtf2024-open/tasks/awtf2024_d
Problem Statement You are given a permutation P=(P_1,P_2,\cdots,P_N) of (1,2,\cdots,N) . You want to perform zero or more swaps of adjacent elements so that P satisfies the following condition: The number of indices i such that P_i > P_{i+1} is at most 1 . Find the minimum number of swaps required.
[ { "input": "3\n3 2 1\n", "output": "1\n" }, { "input": "4\n2 4 1 3\n", "output": "0\n" }, { "input": "6\n2 3 1 6 4 5\n", "output": "1\n" }, { "input": "20\n8 13 6 11 20 3 12 18 17 4 10 1 7 16 19 5 2 15 14 9\n", "output": "36\n" } ]
https://atcoder.jp/contests/awtf2024-open/tasks/awtf2024_e
Problem Statement Solve the following problem for T test cases. There is a board with N rows and N columns. Let (i,j) denote the cell at the i -th row from the top and j -th column from the left. Initially, all cells are colorless. You have N^2 stamps. For each pair of integers (h,w) ( 1 \leq h,w \leq N ), there is one stamp of size h \times w , which is called stamp (h,w) . All stamps have different colors. "Pressing a stamp (h,w) " means the following operation: Choose a position (a,b) ( 1 \leq a \leq N-h+1,\ 1 \leq b \leq N-w+1 ). Then, color all cells (i,j) ( a \leq i \leq a+h-1 , b \leq j \leq b+w-1 ) with the color of stamp (h,w) . If a cell is already colored, the color will be overwritten. Your goal is to press each stamp exactly once so that all cells on the final board have different colors. You have already pressed K stamps. The i -th pressed stamp was (H_i,W_i) , and its position was (A_i,B_i) . Show one way to press the remaining N^2-K stamps to achieve the goal. In this problem, you are only provided with inputs where you can achieve the goal by pressing the remaining stamps in an appropriate order and positions.
[ { "input": "4\n2 2\n2 2 1 1\n1 2 2 1\n2 0\n3 3\n3 3 1 1\n3 2 1 2\n2 3 1 1\n5 15\n5 5 1 1\n4 5 2 1\n5 4 1 1\n4 4 2 1\n3 5 3 1\n2 5 4 1\n1 5 4 1\n5 3 1 1\n3 4 3 1\n2 4 3 1\n4 3 2 1\n1 4 4 1\n3 3 3 1\n2 3 4 1\n1 3 5 1\n", "output": "2 1 1 1\n1 1 2 1\n\n2 2 1 1\n2 1 1 1\n1 2 1 1\n1 1 1 1\n\n2 2 1 2\n3 1 1 2\n1 3 2 1\n1 2 2 1\n2 1 1 2\n1 1 1 2\n\n5 2 1 1\n4 2 1 1\n3 2 1 1\n5 1 1 1\n2 2 1 1\n4 1 1 1\n1 2 1 1\n3 1 1 1\n2 1 1 1\n1 1 1 1\n" } ]
https://atcoder.jp/contests/awtf2024/tasks/awtf2024_a
Problem Statement There are N slimes on a number line, and the i -th slime is at coordinate A_i . All these coordinates are distinct. Each slime has a weight of 1 . Also, you are given an integer K . First, you will choose K slimes and remove the others from the number line. Then, from time 0 , the chosen slimes will move as follows: Movement of each slime: Let R be the total weight of slimes at coordinates greater than its own, and L be the total weight of slimes at coordinates less than its own. Then, it moves with a velocity of R-L . Note that the velocity can be negative. That is, it moves in the negative direction if R-L<0 . When two or more slimes reach the same coordinate at the same time, they merge. The weight of the merged slime is the sum of the weights of the slimes before merging. The merged slime will move according to the same rules as above. The K slimes will continue to merge until there is only one slime left. Let t be the moment when this single slime is born. Your goal is to choose the K slimes so that t is maximized. Find the maximum value of t .
[ { "input": "3 2\n0 1 2\n", "output": "1.00000000000000000000\n" }, { "input": "4 3\n0 1 4 9\n", "output": "2.83333333333333333333\n" }, { "input": "4 4\n0 1 2 3\n", "output": "0.50000000000000000000\n" }, { "input": "20 6\n0 3441380 120768398 229897071 231209282 232046760 254924545 325399248 385631087 400098966 480503302 501372095 502644652 524585010 541761042 691400171 725009462 767549897 837806226 927396743\n", "output": "135453315.33333333333333333333\n" } ]
https://atcoder.jp/contests/awtf2024/tasks/awtf2024_b
Problem Statement You are given a string S of length N consisting of 0 and 1 . We define an inversion pair as a pair of integers (i, j) ( 1 \leq i < j \leq N ) such that the i -th character of S is 1 and the j -th character of S is 0 . As long as there is an inversion pair in S , perform the following operation: Randomly choose an inversion pair (i, j) . The choice is independent of previous choices and is uniformly random. Then, swap the i -th and j -th characters of S . Find the expected number of operations, modulo 998244353 . What is the expected value modulo 998244353 ? It can be proved that the sought expected value is always rational. Moreover, under the constraints of this problem, it can be proved that if the expected value is expressed as an irreducible fraction \frac{P}{Q} , then Q \neq 0 \pmod{998244353} . Thus, there exists a unique integer R such that R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353 . Report this R .
[ { "input": "2\n10\n", "output": "1\n" }, { "input": "3\n110\n", "output": "499122178\n" }, { "input": "1\n0\n", "output": "0\n" }, { "input": "10\n1011000010\n", "output": "133099253\n" }, { "input": "100\n0101110010001000111000111001001101001100000111110001010010001010101100011001011011101101100001100111\n", "output": "407907276\n" } ]
https://atcoder.jp/contests/awtf2024/tasks/awtf2024_c
Problem Statement Solve the following problem for T test cases. You are currently at coordinate 0 on a number line, and you want to reach coordinate L . You will use a car for the movement. This car runs on two types of fuel: types 1 and 2 . For each type, the car has a fuel tank of that type with a capacity of C liters. Currently, both fuel tanks are full. The car can consume x liters of either type of fuel (where x is any positive integer not exceeding the remaining fuel) to move x units in any direction on the number line. You can choose which type of fuel to use for each movement. There are N fuel stations on the number line. The i -th fuel station is located at coordinate X_i . When the car is exactly at coordinate X_i , you can buy type- K_i fuel at a cost of 1 per liter. Of course, you cannot buy more fuel than allowed by the tank capacity. Determine whether it is possible to reach coordinate L . If it is possible, find the minimum total cost of purchasing fuel.
[ { "input": "5\n1 10 4\n7\n1\n1 10 6\n7\n1\n2 12 3\n5 7\n1 1\n2 12 3\n5 7\n1 2\n20 749013197 23809523\n46981984 70791437 118235723 132421762 180040807 203849360 251468335 275277857 322889975 346699150 394318091 418113855 465732891 489532137 537144103 558852533 606466719 630275002 677584754 701394209\n1 2 2 1 1 2 1 2 1 2 2 1 2 1 2 1 1 2 1 2\n", "output": "2\n0\n-1\n6\n585545066743659\n" } ]
https://atcoder.jp/contests/awtf2024/tasks/awtf2024_d
Problem Statement You are given a permutation P=(P_1,P_2,\cdots,P_N) of (1,2,\cdots,N) . You want to perform zero or more swaps of adjacent elements so that P satisfies the following condition: The number of indices i such that P_i > P_{i+1} is at most 1 . Find the minimum number of swaps required.
[ { "input": "3\n3 2 1\n", "output": "1\n" }, { "input": "4\n2 4 1 3\n", "output": "0\n" }, { "input": "6\n2 3 1 6 4 5\n", "output": "1\n" }, { "input": "20\n8 13 6 11 20 3 12 18 17 4 10 1 7 16 19 5 2 15 14 9\n", "output": "36\n" } ]
https://atcoder.jp/contests/awtf2024/tasks/awtf2024_e
Problem Statement Solve the following problem for T test cases. There is a board with N rows and N columns. Let (i,j) denote the cell at the i -th row from the top and j -th column from the left. Initially, all cells are colorless. You have N^2 stamps. For each pair of integers (h,w) ( 1 \leq h,w \leq N ), there is one stamp of size h \times w , which is called stamp (h,w) . All stamps have different colors. "Pressing a stamp (h,w) " means the following operation: Choose a position (a,b) ( 1 \leq a \leq N-h+1,\ 1 \leq b \leq N-w+1 ). Then, color all cells (i,j) ( a \leq i \leq a+h-1 , b \leq j \leq b+w-1 ) with the color of stamp (h,w) . If a cell is already colored, the color will be overwritten. Your goal is to press each stamp exactly once so that all cells on the final board have different colors. You have already pressed K stamps. The i -th pressed stamp was (H_i,W_i) , and its position was (A_i,B_i) . Show one way to press the remaining N^2-K stamps to achieve the goal. In this problem, you are only provided with inputs where you can achieve the goal by pressing the remaining stamps in an appropriate order and positions.
[ { "input": "4\n2 2\n2 2 1 1\n1 2 2 1\n2 0\n3 3\n3 3 1 1\n3 2 1 2\n2 3 1 1\n5 15\n5 5 1 1\n4 5 2 1\n5 4 1 1\n4 4 2 1\n3 5 3 1\n2 5 4 1\n1 5 4 1\n5 3 1 1\n3 4 3 1\n2 4 3 1\n4 3 2 1\n1 4 4 1\n3 3 3 1\n2 3 4 1\n1 3 5 1\n", "output": "2 1 1 1\n1 1 2 1\n\n2 2 1 1\n2 1 1 1\n1 2 1 1\n1 1 1 1\n\n2 2 1 2\n3 1 1 2\n1 3 2 1\n1 2 2 1\n2 1 1 2\n1 1 1 2\n\n5 2 1 1\n4 2 1 1\n3 2 1 1\n5 1 1 1\n2 2 1 1\n4 1 1 1\n1 2 1 1\n3 1 1 1\n2 1 1 1\n1 1 1 1\n" } ]
https://atcoder.jp/contests/abc361/tasks/abc361_a
Problem Statement You are given an integer sequence A of length N and integers K and X . Print the integer sequence B obtained by inserting the integer X immediately after the K -th element of the sequence A .
[ { "input": "4 3 7\n2 3 5 11\n", "output": "2 3 5 7 11\n" }, { "input": "1 1 100\n100\n", "output": "100 100\n" }, { "input": "8 8 3\n9 9 8 2 4 4 3 5\n", "output": "9 9 8 2 4 4 3 5 3\n" } ]
https://atcoder.jp/contests/abc361/tasks/abc361_b
Problem Statement You are trying to implement collision detection in a 3D game. In a 3 -dimensional space, let C(a,b,c,d,e,f) denote the cuboid with a diagonal connecting (a,b,c) and (d,e,f) , and with all faces parallel to the xy -plane, yz -plane, or zx -plane. (This definition uniquely determines C(a,b,c,d,e,f) .) Given two cuboids C(a,b,c,d,e,f) and C(g,h,i,j,k,l) , determine whether their intersection has a positive volume.
[ { "input": "0 0 0 4 5 6\n2 3 4 5 6 7\n", "output": "Yes\n" }, { "input": "0 0 0 2 2 2\n0 0 2 2 2 4\n", "output": "No\n" }, { "input": "0 0 0 1000 1000 1000\n10 10 10 100 100 100\n", "output": "Yes\n" } ]
https://atcoder.jp/contests/abc361/tasks/abc361_c
Problem Statement You are given a sequence A of length N . Freely choose exactly K elements from A and remove them, then concatenate the remaining elements in their original order to form a new sequence B . Find the minimum possible value of this: the maximum value of B minus the minimum value of B .
[ { "input": "5 2\n3 1 5 4 9\n", "output": "2\n" }, { "input": "6 5\n1 1 1 1 1 1\n", "output": "0\n" }, { "input": "8 3\n31 43 26 6 18 36 22 13\n", "output": "18\n" } ]
https://atcoder.jp/contests/abc361/tasks/abc361_d
Problem Statement There are N+2 cells arranged in a row. Let cell i denote the i -th cell from the left. There is one stone placed in each of the cells from cell 1 to cell N . For each 1 \leq i \leq N , the stone in cell i is white if S_i is W , and black if S_i is B . Cells N+1 and N+2 are empty. You can perform the following operation any number of times (possibly zero): Choose a pair of adjacent cells that both contain stones, and move these two stones to the empty two cells while preserving their order. More precisely, choose an integer x such that 1 \leq x \leq N+1 and both cells x and x+1 contain stones. Let k and k+1 be the empty two cells. Move the stones from cells x and x+1 to cells k and k+1 , respectively. Determine if it is possible to achieve the following state, and if so, find the minimum number of operations required: Each of the cells from cell 1 to cell N contains one stone, and for each 1 \leq i \leq N , the stone in cell i is white if T_i is W , and black if T_i is B .
[ { "input": "6\nBWBWBW\nWWWBBB\n", "output": "4\n" }, { "input": "6\nBBBBBB\nWWWWWW\n", "output": "-1\n" }, { "input": "14\nBBBWBWWWBBWWBW\nWBWWBBWWWBWBBB\n", "output": "7\n" } ]
https://atcoder.jp/contests/abc361/tasks/abc361_e
Problem Statement In the nation of AtCoder, there are N cities numbered 1 to N and N-1 roads numbered 1 to N-1 . Road i connects cities A_i and B_i bidirectionally, and its length is C_i . Any pair of cities can be reached from each other by traveling through some roads. Find the minimum travel distance required to start from a city and visit all cities at least once using the roads.
[ { "input": "4\n1 2 2\n1 3 3\n1 4 4\n", "output": "11\n" }, { "input": "10\n10 9 1000000000\n9 8 1000000000\n8 7 1000000000\n7 6 1000000000\n6 5 1000000000\n5 4 1000000000\n4 3 1000000000\n3 2 1000000000\n2 1 1000000000\n", "output": "9000000000\n" } ]
https://atcoder.jp/contests/abc361/tasks/abc361_f
Problem Statement How many integers x between 1 and N , inclusive, can be expressed as x = a^b using some positive integer a and a positive integer b not less than 2 ?
[ { "input": "99\n", "output": "12\n" }, { "input": "1000000000000000000\n", "output": "1001003332\n" } ]
https://atcoder.jp/contests/abc361/tasks/abc361_g
Problem Statement There are N stones placed on a 2 -dimensional plane. The i -th stone is located at coordinates (X_i, Y_i) . All stones are located at lattice points in the first quadrant (including the axes). Count the number of lattice points (x, y) where no stone is placed and it is impossible to reach (-1, -1) from (x, y) by repeatedly moving up, down, left, or right by 1 without passing through coordinates where a stone is placed. More precisely, count the number of lattice points (x, y) where no stone is placed, and there does not exist a finite sequence of integer pairs (x_0, y_0), \ldots, (x_k, y_k) that satisfies all of the following four conditions: (x_0, y_0) = (x, y) . (x_k, y_k) = (-1, -1) . |x_i - x_{i+1}| + |y_i - y_{i+1}| = 1 for all 0 \leq i < k . There is no stone at (x_i, y_i) for all 0 \leq i \leq k .
[ { "input": "5\n1 0\n0 1\n2 3\n1 2\n2 1\n", "output": "1\n" }, { "input": "0\n", "output": "0\n" }, { "input": "22\n0 1\n0 2\n0 3\n1 0\n1 4\n2 0\n2 2\n2 4\n3 0\n3 1\n3 2\n3 4\n5 1\n5 2\n5 3\n6 0\n6 4\n7 0\n7 4\n8 1\n8 2\n8 3\n", "output": "6\n" } ]
https://atcoder.jp/contests/abc360/tasks/abc360_a
Problem Statement Takahashi eats three plates for breakfast: rice, miso soup, and salad. His table is long and narrow, so he arranged the three plates in a row. The arrangement is given by a string S , where the i -th plate from the left is rice if S_i is R , miso soup if S_i is M , and salad if S_i is S . Determine whether the plate of rice is to the left of the plate of miso soup.
[ { "input": "RSM\n", "output": "Yes\n" }, { "input": "SMR\n", "output": "No\n" } ]
https://atcoder.jp/contests/abc360/tasks/abc360_b
Problem Statement You are given two strings S and T consisting of lowercase English letters. Determine if there exists a pair of integers c and w such that 1 \leq c \leq w < |S| and the following condition is satisfied. Here, |S| denotes the length of the string S . Note that w must be less than |S| . If S is split at every w characters from the beginning, the concatenation of the c -th characters of the substrings of length at least c in order equals T .
[ { "input": "atcoder toe\n", "output": "Yes\n" }, { "input": "beginner r\n", "output": "No\n" }, { "input": "verticalreading agh\n", "output": "No\n" } ]
https://atcoder.jp/contests/abc360/tasks/abc360_c
Problem Statement There are N boxes numbered 1 to N and N items numbered 1 to N . Item i (1 \leq i \leq N) is in box A_i and has a weight of W_i . You can repeatedly perform the operation of choosing an item and moving it to another box zero or more times. If the weight of the item being moved is w , the cost of the operation is w . Find the minimum total cost required to make each box contain exactly one item.
[ { "input": "5\n2 2 3 3 5\n33 40 2 12 16\n", "output": "35\n" }, { "input": "12\n3 6 7 4 12 4 8 11 11 1 8 11\n3925 9785 9752 3587 4013 1117 3937 7045 6437 6208 3391 6309\n", "output": "17254\n" } ]
https://atcoder.jp/contests/abc360/tasks/abc360_d
Problem Statement There are N ants on a number line, labeled 1 to N . Ant i (1 \leq i \leq N) starts at coordinate X_i and faces either a positive or negative direction. Initially, all ants are at distinct coordinates. The direction each ant is facing is represented by a binary string S of length N , where ant i is facing the negative direction if S_i is 0 and the positive direction if S_i is 1 . Let the current time be 0 , and the ants move in their respective directions at a speed of 1 unit per unit time for (T+0.1) units of time until time (T+0.1) . If multiple ants reach the same coordinate, they pass through each other without changing direction or speed. After (T+0.1) units of time, all ants stop. Find the number of pairs (i, j) such that 1 \leq i < j \leq N and ants i and j pass each other from now before time (T+0.1) .
[ { "input": "6 3\n101010\n-5 -1 0 1 2 4\n", "output": "5\n" }, { "input": "13 656320850\n0100110011101\n-900549713 -713494784 -713078652 -687818593 -517374932 -498415009 -472742091 -390030458 -379340552 -237481538 -44636942 352721061 695864366\n", "output": "14\n" } ]
https://atcoder.jp/contests/abc360/tasks/abc360_e
Problem Statement There are N - 1 white balls and one black ball. These N balls are arranged in a row, with the black ball initially at the leftmost position. Takahashi will perform the following operation exactly K times. Choose an integer uniformly at random between 1 and N , inclusive, twice. Let a and b the chosen integers. If a \neq b , swap the a -th and b -th balls from the left. After K operations, let the black ball be at the x -th position from the left. Find the expected value of x , modulo 998244353 . What is expected value modulo 998244353 ? It can be proved that the sought expected value will always be rational. Additionally, under the constraints of this problem, it can be proved that if this value is expressed as an irreducible fraction \frac{P}{Q} , then Q \not \equiv 0 \pmod{998244353} . Therefore, there exists a unique integer R such that R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353 . Report this R .
[ { "input": "2 1\n", "output": "499122178\n" }, { "input": "3 2\n", "output": "554580198\n" }, { "input": "4 4\n", "output": "592707587\n" } ]
https://atcoder.jp/contests/abc360/tasks/abc360_f
Problem Statement You are given N intervals numbered 1 to N . Interval i is [L_i, R_i] . Two intervals [l_a, r_a] and [l_b, r_b] are said to intersect if and only if they satisfy either (l_a < l_b < r_a < r_b) or (l_b < l_a < r_b < r_a) . Define f(l, r) as the number of intervals i ( 1 \leq i \leq N ) that intersect with the interval [l, r] . Among all pairs of integers (l, r) satisfying 0 \leq l < r \leq 10^{9} , find the pair (l, r) that maximizes f(l, r) . If there are multiple such pairs, choose the one with the smallest l . If there are still multiple pairs, choose the one with the smallest r among them. (Since 0 \leq l < r , the pair (l, r) to be answered is uniquely determined.)
[ { "input": "5\n1 7\n3 9\n7 18\n10 14\n15 20\n", "output": "4 11\n" }, { "input": "11\n856977192 996441446\n298251737 935869360\n396653206 658841528\n710569907 929136831\n325371222 425309117\n379628374 697340458\n835681913 939343451\n140179224 887672320\n375607390 611397526\n93530028 581033295\n249611310 775998537\n", "output": "396653207 887672321\n" } ]
https://atcoder.jp/contests/abc360/tasks/abc360_g
Problem Statement You are given an integer sequence A of length N . Takahashi will perform the following operation exactly once: Choose an integer x between 1 and N , inclusive, and an arbitrary integer y . Replace A_x with y . Find the maximum possible length of a longest increasing subsequence (LIS) of A after performing the operation. What is longest increasing subsequence? A subsequence of a sequence A is a sequence that can be derived from A by extracting some elements without changing the order. A longest increasing subsequence of a sequence A is a longest subsequence of A that is strictly increasing.
[ { "input": "4\n3 2 2 4\n", "output": "3\n" }, { "input": "5\n4 5 3 6 7\n", "output": "4\n" } ]
https://atcoder.jp/contests/arc180/tasks/arc180_a
Problem Statement You are given a string S of length N consisting of characters A and B . You can perform the following two types of operations zero or more times in any order: Choose a (contiguous) substring ABA in S and replace it with A . Choose a (contiguous) substring BAB in S and replace it with B . Find, modulo 10^9+7 , the number of possible strings S after performing the operations.
[ { "input": "4\nABAB\n", "output": "2\n" }, { "input": "1\nA\n", "output": "1\n" }, { "input": "17\nBBABABAABABAAAABA\n", "output": "18\n" }, { "input": "100\nABAABAABABBABAABAABAABABBABBABBABBABBABBABBABBABBABBABBABBABBABBABAABABAABABBABBABABBABAABAABAABAABA\n", "output": "415919090\n" } ]
https://atcoder.jp/contests/arc180/tasks/arc180_b
Problem Statement You are given a permutation P=(P_1,P_2,\cdots,P_N) of (1,2,\cdots,N) . Additionally, you are given an integer K . You can perform the following operation zero or more times: Choose integers l and r ( 1 \leq l < r \leq N ). Here, the pair (l,r) must satisfy all of the following conditions: K \leq r-l . P_l > P_r at the time of the operation. The pair (l,r) has never been chosen before. Then, swap the values of P_l and P_r . You want to maximize the number of operations. Find one way to achieve this.
[ { "input": "3 1\n3 2 1\n", "output": "3\n2 3\n1 3\n1 2\n" }, { "input": "5 4\n1 4 3 2 5\n", "output": "0\n" }, { "input": "4 2\n4 1 2 3\n", "output": "2\n1 4\n1 3\n" }, { "input": "10 5\n8 7 6 10 9 3 1 5 2 4\n", "output": "15\n3 8\n2 8\n3 10\n3 9\n1 8\n2 10\n2 9\n2 7\n1 10\n5 10\n1 9\n4 10\n4 9\n1 7\n1 6\n" } ]
https://atcoder.jp/contests/arc180/tasks/arc180_c
Problem Statement You are given an integer sequence A=(A_1,A_2,\cdots,A_N) of length N . You will perform the following operation exactly once: Choose a non-empty subsequence of A (not necessarily contiguous) and replace it with its cumulative sums. More precisely, first choose a sequence of indices (i_1,i_2,\cdots,i_k) such that 1 \leq i_1 < i_2 < \cdots < i_k \leq N . The length of the sequence k ( 1 \leq k \leq N ) can be chosen freely. Then, for each j ( 1 \leq j \leq k ), replace the value of A_{i_j} with \sum_{1 \leq x \leq j} A_{i_x} . This replacement is done simultaneously for all chosen indices. Find, modulo 10^9+7 , the number of possible sequences A after the operation.
[ { "input": "3\n1 1 2\n", "output": "4\n" }, { "input": "4\n1 -1 1 -1\n", "output": "8\n" }, { "input": "5\n0 0 0 0 0\n", "output": "1\n" }, { "input": "40\n2 -2 1 3 -3 -1 -2 -3 0 -1 -2 0 -3 0 0 2 0 -1 2 -2 -2 -1 3 -2 -2 -2 2 3 2 -3 0 -2 2 1 3 0 -1 0 -2 -3\n", "output": "420429545\n" } ]
https://atcoder.jp/contests/arc180/tasks/arc180_d
Problem Statement You are given an integer sequence A=(A_1,A_2,\cdots,A_N) of length N . Answer the following Q queries. The i -th query: You are given integers L_i and R_i . Solve the following problem for B=(A_{L_i},A_{L_i+1},\cdots,A_{R_i}) . Divide B into three non-empty contiguous subsequences. For each contiguous subsequence, let us find the maximum value of its elements. Find the minimum possible sum of these maximum values. Here, the constraints of the problem force the length of B to be at least 3 , so there is always at least one way to divide it into three non-empty contiguous subsequences.
[ { "input": "7 5\n4 3 1 1 4 5 2\n1 7\n2 4\n3 5\n1 5\n4 7\n", "output": "10\n5\n6\n9\n8\n" }, { "input": "10 15\n8 3 8 10 1 5 3 1 6 4\n4 6\n2 5\n6 9\n8 10\n2 9\n4 10\n1 5\n1 8\n1 3\n4 8\n1 10\n2 10\n6 10\n2 6\n2 6\n", "output": "16\n14\n12\n11\n17\n17\n19\n14\n19\n14\n17\n17\n12\n16\n16\n" } ]
https://atcoder.jp/contests/arc180/tasks/arc180_e
Problem Statement You are given an integer sequence A=(A_1,A_2,\cdots,A_N) of length N . Here, it is guaranteed that 0 \leq A_i < i for each i . The score and cost of a permutation P=(P_1,P_2,\cdots,P_N) of (1,2,\cdots,N) are defined as follows: The score of P is the length of a longest increasing subsequence of P . The cost of P is the number of integers i ( 1 \leq i \leq N ) that satisfy the following condition: There are fewer than A_i integers j such that j < i and P_j > P_i . For each k=1,2,\cdots,N , solve the following problem: Find the minimum cost of a permutation P whose score is at least k .
[ { "input": "4\n0 1 2 1\n", "output": "0 0 1 3\n" }, { "input": "3\n0 0 0\n", "output": "0 0 0\n" }, { "input": "5\n0 1 2 3 4\n", "output": "0 1 2 3 4\n" }, { "input": "11\n0 0 2 3 4 5 3 7 8 2 10\n", "output": "0 0 0 1 2 3 4 5 7 8 9\n" } ]
https://atcoder.jp/contests/arc180/tasks/arc180_f
Problem Statement You are given integers N and A . You will perform the following operations: Generate N real numbers uniformly at random between 0 and 1 , inclusive. All generations are independent, and the random numbers are uniformly distributed. Call the generated N real numbers x_1, x_2, \cdots, x_N in ascending order. That is, 0 \leq x_1 \leq x_2 \leq \cdots \leq x_N \leq 1 . Your score is given by the following formula: \displaystyle \prod_{i=1}^{N} \left(1+\sum_{j=i+1}^N x_j^A \right) Calculate the expected value, modulo 10^9+7 , of the score. Definition of expected value modulo 10^9+7 It can be proved that the sought expected value is always rational. Furthermore, under the constraints of this problem, it can be proved that if the expected value is expressed as an irreducible fraction \frac{P}{Q} , then Q \neq 0 \pmod{10^9+7} . Therefore, there exists a unique integer R such that R \times Q \equiv P \pmod{10^9+7}, 0 \leq R < 10^9+7 . Report this R .
[ { "input": "2 1\n", "output": "666666673\n" }, { "input": "1 1\n", "output": "1\n" }, { "input": "2 2\n", "output": "500000005\n" }, { "input": "3 2\n", "output": "142857147\n" }, { "input": "5 3\n", "output": "758371066\n" }, { "input": "10000 12345\n", "output": "32201773\n" } ]
https://atcoder.jp/contests/abc359/tasks/abc359_a
Problem Statement You are given N strings. The i -th string S_i (1 \leq i \leq N) is either Takahashi or Aoki . How many i are there such that S_i is equal to Takahashi ?
[ { "input": "3\nAoki\nTakahashi\nTakahashi\n", "output": "2\n" }, { "input": "2\nAoki\nAoki\n", "output": "0\n" }, { "input": "20\nAoki\nTakahashi\nTakahashi\nAoki\nAoki\nAoki\nAoki\nTakahashi\nAoki\nAoki\nAoki\nTakahashi\nTakahashi\nAoki\nTakahashi\nAoki\nAoki\nAoki\nAoki\nTakahashi\n", "output": "7\n" } ]
https://atcoder.jp/contests/abc359/tasks/abc359_b
Problem Statement There are 2N people standing in a row, and the person at the i -th position from the left is wearing clothes of color A_i . Here, the clothes have N colors from 1 to N , and exactly two people are wearing clothes of each color. Find how many of the integers i=1,2,\ldots,N satisfy the following condition: There is exactly one person between the two people wearing clothes of color i .
[ { "input": "3\n1 2 1 3 2 3\n", "output": "2\n" }, { "input": "2\n1 1 2 2\n", "output": "0\n" }, { "input": "4\n4 3 2 3 2 1 4 1\n", "output": "3\n" } ]
https://atcoder.jp/contests/abc359/tasks/abc359_c
Problem Statement The coordinate plane is covered with 2\times1 tiles. The tiles are laid out according to the following rules: For an integer pair (i,j) , the square A _ {i,j}=\lbrace(x,y)\mid i\leq x\leq i+1\wedge j\leq y\leq j+1\rbrace is contained in one tile. When i+j is even, A _ {i,j} and A _ {i + 1,j} are contained in the same tile. Tiles include their boundaries, and no two different tiles share a positive area. Near the origin, the tiles are laid out as follows: Takahashi starts at the point (S _ x+0.5,S _ y+0.5) on the coordinate plane. He can repeat the following move as many times as he likes: Choose a direction (up, down, left, or right) and a positive integer n . Move n units in that direction. Each time he enters a tile, he pays a toll of 1 . Find the minimum toll he must pay to reach the point (T _ x+0.5,T _ y+0.5) .
[ { "input": "5 0\n2 5\n", "output": "5\n" }, { "input": "3 1\n4 1\n", "output": "0\n" }, { "input": "2552608206527595 5411232866732612\n771856005518028 7206210729152763\n", "output": "1794977862420151\n" } ]
https://atcoder.jp/contests/abc359/tasks/abc359_d
Problem Statement You are given a string S of length N consisting of characters A , B , and ? . You are also given a positive integer K . A string T consisting of A and B is considered a good string if it satisfies the following condition: No contiguous substring of length K in T is a palindrome. Let q be the number of ? characters in S . There are 2^q strings that can be obtained by replacing each ? in S with either A or B . Find how many of these strings are good strings. The count can be very large, so find it modulo 998244353 .
[ { "input": "7 4\nAB?A?BA\n", "output": "1\n" }, { "input": "40 7\n????????????????????????????????????????\n", "output": "116295436\n" }, { "input": "15 5\nABABA??????????\n", "output": "0\n" }, { "input": "40 8\n?A?B??B?B?AA?A?B??B?A???B?BB?B???BA??BAA\n", "output": "259240\n" } ]
https://atcoder.jp/contests/abc359/tasks/abc359_e
Problem Statement You are given a sequence of positive integers of length N : H=(H _ 1,H _ 2,\dotsc,H _ N) . There is a sequence of non-negative integers of length N+1 : A=(A _ 0,A _ 1,\dotsc,A _ N) . Initially, A _ 0=A _ 1=\dotsb=A _ N=0 . Perform the following operations repeatedly on A : Increase the value of A _ 0 by 1 . For i=1,2,\ldots,N in this order, perform the following operation: If A _ {i-1}\gt A _ i and A _ {i-1}\gt H _ i , decrease the value of A _ {i-1} by 1 and increase the value of A _ i by 1 . For each i=1,2,\ldots,N , find the number of operations before A _ i>0 holds for the first time.
[ { "input": "5\n3 1 4 1 5\n", "output": "4 5 13 14 26\n" }, { "input": "6\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n", "output": "1000000001 2000000001 3000000001 4000000001 5000000001 6000000001\n" }, { "input": "15\n748 169 586 329 972 529 432 519 408 587 138 249 656 114 632\n", "output": "749 918 1921 2250 4861 5390 5822 6428 6836 7796 7934 8294 10109 10223 11373\n" } ]
https://atcoder.jp/contests/abc359/tasks/abc359_f
Problem Statement You are given a sequence of integers A=(A_1,\ldots,A_N) . For a tree T with N vertices, define f(T) as follows: Let d_i be the degree of vertex i in T . Then, f(T)=\sum_{i=1}^N {d_i}^2 A_i . Find the minimum possible value of f(T) . The constraints guarantee the answer to be less than 2^{63} .
[ { "input": "4\n3 2 5 2\n", "output": "24\n" }, { "input": "3\n4 3 2\n", "output": "15\n" }, { "input": "7\n10 5 10 2 10 13 15\n", "output": "128\n" } ]
https://atcoder.jp/contests/abc359/tasks/abc359_g
Problem Statement You are given a tree with N vertices. The i -th edge connects vertices u_i and v_i bidirectionally. Additionally, you are given an integer sequence A=(A_1,\ldots,A_N) . Here, define f(i,j) as follows: If A_i = A_j , then f(i,j) is the minimum number of edges you need to traverse to move from vertex i to vertex j . If A_i \neq A_j , then f(i,j) = 0 . Calculate the value of the following expression: \displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^N f(i,j) .
[ { "input": "4\n3 4\n4 2\n1 2\n2 1 1 2\n", "output": "4\n" }, { "input": "8\n8 6\n3 8\n1 4\n7 8\n4 5\n3 4\n8 2\n1 2 2 2 3 1 1 3\n", "output": "19\n" } ]
https://atcoder.jp/contests/npcapc_2024/tasks/npcapc_2024_a
問題文 英大文字と英小文字からなる長さ N の文字列のうち、 NPCAPC と npcapc の両方を部分文字列(連続でなくてもよい)として含むものの個数を 998244353 で割ったあまりを求めてください。 T 個のテストケースが与えられるので、それぞれについて解いてください。
[ { "input": "4\n12\n6\n5839\n123456\n", "output": "924\n0\n966252995\n432934749\n" }, { "input": "3\n123456789\n987654321\n999999999\n", "output": "333574957\n124462731\n163251704\n" } ]
https://atcoder.jp/contests/npcapc_2024/tasks/npcapc_2024_b
問題文 長さ N の正整数列 A=(A_1,A_2,\dots,A_N) が与えられます。 k=0,1,\dots,N について以下の問題を解いてください。 \lbrace 1,2,\dots,N\rbrace の部分集合 S であって以下の条件を満たすものの個数を 998244353 で割ったあまりを求めてください。 \left\vert T\right\vert=\left\vert S\right\vert-k を満たす S の部分集合 T であって \sum_{i \in T}A_{i}\ge M を満たすものが存在する。
[ { "input": "4 7\n3 1 5 2\n", "output": "6\n4\n1\n0\n0\n" }, { "input": "1 5\n7\n", "output": "1\n0\n" }, { "input": "9 18\n1 9 5 6 2 7 1 4 8\n", "output": "346\n309\n230\n126\n46\n10\n1\n0\n0\n0\n" } ]
https://atcoder.jp/contests/npcapc_2024/tasks/npcapc_2024_c
問題文 なむか君は友達のなぷか君と協力して問題 1 , 問題 2 , \dots , 問題 N の N 個の問題を全て解くことになりました。 最初二人とも疲れは 0 ですが、問題を 1 問解くとその問題を解いた人の疲れが 1 増えます。問題 i を疲れが j のときに解くと、なむか君は A_i+C_j 分かかり、なぷか君は B_i+D_j 分かかります。ただし、 2 人が同時に別の問題を解くことは出来ません。 なむか君となぷか君が N 個の問題を解く時間の総和の最小値を求めてください。
[ { "input": "3\n1 3 5\n6 4 2\n1 2 3\n1 2 3\n", "output": "10\n" }, { "input": "5\n2 4 3 1 2\n9 2 5 3 8\n1 2 8 3 2\n5 4 3 2 1\n", "output": "28\n" }, { "input": "8\n21 85 72 22 81 20 88 28\n75 22 78 92 55 56 73 44\n39 14 64 27 73 42 16 84\n27 7 91 85 69 95 70 27\n", "output": "621\n" } ]
https://atcoder.jp/contests/npcapc_2024/tasks/npcapc_2024_d
問題文 長さ N の非負整数列 A=(A_1,A_2,\dots,A_N) と Q 個のクエリが与えられます。 i 個目のクエリは以下のような内容です。 A_{x_i} を y_i に変更し、その後の A について以下の問題の答えを求める。 白い箱と黒い箱と 1 から M の番号が付いた M 個のボールがあります。始め、全てのボールは白い箱に入っています。 ここで、あなたは以下の操作を N 回繰り返します。 1 \le x \le M を満たす整数 x を選ぶ。ボール x を今入っている箱からもう一方の箱に移動させる。 i 回目の操作終了後、黒い箱に入っているボールの番号は全て A_i 以下である必要があります。操作列としてあり得るものの個数を 998244353 で割ったあまりを求めてください。 クエリを順に処理してください。
[ { "input": "3 3 2\n1 3 1\n3 2\n1 3\n", "output": "5\n14\n" }, { "input": "6 8 1\n3 8 7 7 1 6\n1 4\n", "output": "2100\n" }, { "input": "12 10 8\n1 3 2 6 3 6 7 7 5 5 4 7\n12 4\n7 10\n4 2\n9 8\n9 9\n8 3\n4 9\n10 2\n", "output": "2741280\n3007680\n1503840\n1916160\n1972800\n728640\n1821600\n621440\n" } ]
https://atcoder.jp/contests/npcapc_2024/tasks/npcapc_2024_e
問題文 あなたは 2 次元平面を用いてゲームをします。始め、 -100 \le x \le 100,-100 \le y \le 0 を満たす格子点 (x,y) に駒が 1 個ずつ置かれています。 あなたは以下の操作を 0 回以上行うことが出来ます。 2 個の点の組 (a,b),(c,d) であって、 |a-c|+|b-d| = 1 かつ (a,b),(c,d) 両方に駒が 1 個以上あるものを選ぶ。 (a,b) にある駒 1 個を (c,d) を中心として時計回りか反時計周りに 90 度回転させた点に置き、 (c,d) にある駒を 1 個消す。(既に移動先の点に駒が置かれていても良い。) あなたの目標は操作が全て終わったときに y 座標が N 以上であるいずれかの点に駒が置かれている状態にすることです。目標が達成可能か判定し、可能ならば操作列を 1 個構築してください。 T 個のテストケースが与えられるので、それぞれについて解いてください。
[ { "input": "1\n1\n", "output": "1\n1 0 0 0 0 1\n" } ]
https://atcoder.jp/contests/npcapc_2024/tasks/npcapc_2024_f
問題文 1 から N の番号がついた N 人の人が一列に並べられた M 個の椅子に座ります。左から i 番目の椅子を椅子 i と呼びます。人 i は椅子 A_i に座ります。 ある人が座った時に、その人の左右それぞれで最も近い座っている人のいる椅子の番号(それぞれない場合は 0,M+1 )を L,R としたとき、座った人のスコアを R - L とします。 N 人が順番に座る方法は N! 通りありますが、 N 人のスコアの総和としてあり得る最大値を求めてください。
[ { "input": "3 10\n3 7 10\n", "output": "28\n" }, { "input": "5 20\n3 10 11 14 17\n", "output": "73\n" }, { "input": "10 1000000000\n136909656 243332691 643531997 505919307 43553384 657638276 57213246 179732866 357373203 182482400\n", "output": "7649951260\n" } ]
https://atcoder.jp/contests/npcapc_2024/tasks/npcapc_2024_g
問題文 PCT 君は以下のような問題を作りました。 Common Segment N 個の区間 [L_1,R_1],[L_2,R_2],\dots,[L_N,R_N] が与えられます。ここで [L, R] は L 以上 R 以下の全ての整数からなる区間を意味します。 ここから 1 個以上の区間を選ぶ方法は 2^N-1 通りありますが、そのうち選んだ全ての区間の共通部分が空でないものの個数を 998244353 で割ったあまりを求めてください。 PCT 君は間違えてテストケースの L_i,R_i のうち一部の値をなくしてしまいました。困った彼のために以下の問題を解いてください。 Many Common Segment Testcases Common Segment のテストケースが与えられます。ただし、なくしてしまった L_i,R_i の値は代わりに -1 が与えられます。 このとき、元のテストケースは 1 \le L_i \le R_i \le M\ (1 \le i \le N) を満たしていたとのことです。元のテストケースとしてあり得るもの全てに対して Common Segment を解き、答えの総和を 998244353 で割ったあまりを求めてください。
[ { "input": "3 3\n1 -1\n2 2\n2 3\n", "output": "18\n" }, { "input": "5 8\n1 7\n2 3\n4 8\n6 8\n1 5\n", "output": "15\n" }, { "input": "10 13\n4 -1\n-1 -1\n7 11\n-1 -1\n-1 -1\n-1 -1\n11 -1\n3 8\n-1 9\n-1 -1\n", "output": "841024210\n" } ]
https://atcoder.jp/contests/npcapc_2024/tasks/npcapc_2024_h
問題文 1 から N の番号がついた N 個のスイッチがあります。今全てのスイッチはオフになっています。 これからスイッチを自由な順番で 1 個ずつ押していきますが、それぞれのスイッチは壊れています。具体的には、スイッチ i は押すのに T_i 秒かかり、押すと以下のような挙動を示します。 確率 \frac{A_i}{B_i} でオンになる。 確率 1 - \frac{A_i}{B_i} で N 個のスイッチが全てオフになる。 スイッチがオンになるかどうかはスイッチを押す毎に独立に選択されます。また、あるスイッチを押している間他のスイッチを押すことは出来ません。 あなたの目標は出来るだけ早く全てのスイッチをオンにすることです。あなたが適切にスイッチを押したときに、全てのスイッチをオンにするのにかかる秒数の期待値 \bmod\ 998244353 を求めてください。 期待値 \bmod\ 998244353 の定義 求める期待値は必ず有理数になることが証明できます。また、この問題の制約のもとでは、その値を既約分数 \frac{P}{Q} で表した時、 Q \neq 0 \pmod{998244353} となることも証明できます。よって、 R \times Q = P \pmod{998244353},0 \le R < 998244353 を満たす整数 R が一意に定まります。この R を答えてください。
[ { "input": "2\n3 3 5\n2 4 7\n", "output": "831870305\n" }, { "input": "5\n2 5 9\n6 4 7\n1 9 14\n17 8 13\n10 4 11\n", "output": "914017655\n" }, { "input": "8\n6 2 8\n3 1 8\n5 30 71\n7 9 58\n6 4 7\n6 9 25\n2 8 67\n6 6 55\n", "output": "923892723\n" } ]
https://atcoder.jp/contests/npcapc_2024/tasks/npcapc_2024_i
問題文 (1,\dots,N) の順列 (P_1,\dots,P_N) であって、以下の条件を満たすものの個数を 998244353 で割ったあまりを求めてください。 ある整数 i(1\le i\lt N) が存在して A_{P_1}+\dots+A_{P_i}=A_{P_{i+1}}+\dots+A_{P_N} が成立する。
[ { "input": "3\n4 9 5\n", "output": "4\n" }, { "input": "2\n100 100\n", "output": "2\n" }, { "input": "8\n3 2 6 3 1 2 4 5\n", "output": "11520\n" } ]
https://atcoder.jp/contests/npcapc_2024/tasks/npcapc_2024_j
問題文 (1,2,\dots,N) の順列が M 個与えられます。 i 個目の順列は P_i = (P_{i,1},P_{i,2},\dots,P_{i,N}) です。 あなたは数列 Q=(1,2,\dots,N) を持っています。そして、以下の操作を 0 回以上行うことが出来ます。 1 \le i \le M を満たす整数 i を選び、 Q を (Q_{P_{i,1}},Q_{P_{i,2}},\dots,Q_{P_{i,N}}) に更新する。 操作後の Q としてあり得る数列全てに対する転倒数の総和を 998244353 で割ったあまりを求めてください。
[ { "input": "3 2\n1 2 3\n2 3 1\n", "output": "4\n" }, { "input": "5 2\n3 4 5 1 2\n1 5 4 3 2\n", "output": "50\n" }, { "input": "30 12\n1 2 9 4 5 6 7 8 3 10 11 12 19 14 15 25 17 18 20 26 21 22 23 24 16 29 27 28 13 30\n9 2 27 4 5 10 7 8 1 25 11 12 24 14 15 16 17 18 19 20 21 22 23 28 6 26 3 13 29 30\n1 5 3 29 2 6 7 8 9 10 11 12 13 16 15 18 17 14 19 20 21 22 28 27 25 26 24 23 4 30\n7 2 3 25 5 6 1 28 21 15 11 12 13 14 10 17 16 18 19 20 9 22 23 24 4 26 27 8 29 30\n1 2 5 4 16 6 7 8 9 11 10 3 13 14 15 12 17 23 19 20 21 29 18 24 25 26 27 28 22 30\n19 24 3 4 1 6 7 8 9 10 11 12 13 21 15 16 17 18 5 22 20 14 23 2 25 26 27 28 29 30\n1 2 3 4 5 6 27 8 9 10 29 12 24 14 15 16 17 18 30 20 7 22 13 23 25 26 21 28 11 19\n1 2 25 4 5 6 7 8 9 20 23 12 13 14 15 16 17 18 19 10 29 22 3 24 11 26 27 28 30 21\n1 2 16 4 5 6 3 8 9 10 11 12 22 29 13 7 17 18 19 20 21 15 23 24 14 26 27 28 25 30\n1 13 3 4 5 6 21 8 24 10 7 12 20 14 15 16 17 2 19 18 11 22 23 9 25 26 27 28 29 30\n1 2 3 4 5 6 25 8 9 19 11 12 13 7 10 16 21 18 15 20 17 22 23 24 28 26 27 14 29 30\n1 2 27 21 5 6 7 8 9 10 18 24 13 14 15 16 17 12 19 11 4 22 23 20 25 26 3 28 29 30\n", "output": "701414999\n" } ]
https://atcoder.jp/contests/npcapc_2024/tasks/npcapc_2024_k
問題文 NPCA 国は横一列に並んだ N 個のマスからなり、左から順に 1 から N の番号がつけられています。ここで、マス i の高さを H_i とします。始め、 H_1=H_2=\dots=H_N=0 となっています。 各 1\le i\le N-1 について、 H_i と H_{i+1} の差の絶対値が D_i 未満であるとき、マス i とマス i+1 の間で争いが起こってしまいます。さて、平和を愛する NPCA 国の王のなぷか君の目標はどの隣り合うマスの間でも争いが起きなくなることです。そのため、なぷか君は以下の魔法を 0 回以上かけることが出来ます。 H_i=H_{i+1}=\dots=H_{j} を満たす整数 i,j(1\le i\le j\le N) を選んで H_i,H_{i+1},\dots,H_{j} に 1 を加算する。 なぷか君が目標を達成するのに必要な魔法をかける回数としてあり得る最小値を求めてください。
[ { "input": "4\n2 3 1\n", "output": "4\n" }, { "input": "3\n0 0\n", "output": "0\n" }, { "input": "10\n1 9 5 6 2 7 1 4 8\n", "output": "22\n" } ]
https://atcoder.jp/contests/npcapc_2024/tasks/npcapc_2024_l
問題文 長さ N-1 の広義単調増加である正整数列 X=(X_1,X_2,\dots,X_{N-1}) が与えられます。 N 頂点 M 辺単純連結無向グラフ G のコストを \sum_{i=1}^{N} \sum_{j=i+1}^{N} X_{d(i,j)} と定義します。ただし、 d(i,j) は G 上で頂点 i から頂点 j へ移動するまでに通る辺の本数としてあり得る最小値として定義します。 コストが最小になる N 頂点 M 辺単純連結無向グラフ G を 1 個構築してください。
[ { "input": "3 2\n4 5\n", "output": "1 2\n1 3\n" }, { "input": "4 6\n12 34 56\n", "output": "1 2\n1 3\n1 4\n2 3\n2 4\n3 4\n" } ]
https://atcoder.jp/contests/npcapc_2024/tasks/npcapc_2024_m
問題文 なむか君は長さ N の数列 A=(A_1,A_2,\dots, A_N) を、なむか君のあこがれの人は長さ M の数列 B=(B_1,B_2,\dots,B_M) を持っています。 なむか君はあこがれの人に近づくために A から異なる M 個の要素を選び、好きな順番で並べて長さ M の数列 C=(C_1, C_2,\dots,C_M) を作ります。 このとき、 \sum_{i=1}^M \left\vert B_i-C_i\right\vert としてあり得る最小値を求めてください。
[ { "input": "5 3\n2 6 5 1 1\n6 3 8\n", "output": "4\n" }, { "input": "3 2\n1 1 9\n1 1\n", "output": "0\n" }, { "input": "11 7\n13 21 9 5 16 32 15 29 20 40 4\n24 34 43 39 18 30 11\n", "output": "32\n" } ]
https://atcoder.jp/contests/npcapc_2024/tasks/npcapc_2024_n
問題文 各要素が 1 次の多項式である N 次正方行列 P(x) が与えられます。 P(x) の (i,j) 成分は a_{i,j} x + b_{i,j} です。 \prod_{i=0}^{M-1} P(2^ix) = P(x) P(2x) \dots P(2^{M-1}x) の (1,1) 成分 f(x) = \sum_{i=0}^{M} c_ix^i の各係数 c_0,c_1,\dots,c_M を \bmod\ 10^9 + 7 で求めてください。
[ { "input": "2 2\n1 2\n3 4\n2 0\n1 2\n", "output": "4\n8\n14\n" }, { "input": "4 10\n520471651 866160932 848899741 650346545\n756377215 402412491 920748640 255249004\n371442152 93295238 350011159 679848583\n528399020 957465601 22001245 407745834\n363922864 418156995 324388560 611306817\n671914474 323815171 638034305 796072406\n765823638 254662378 175686978 123364350\n786531344 396717902 774813609 895184869\n", "output": "890467395\n623743197\n432365684\n555543126\n145580696\n550546744\n959254454\n836036617\n945090197\n106843161\n866547396\n" }, { "input": "6 23\n101670804 457970042 521121852 851881468 298366530 271962368\n881900040 161849089 409608300 527884448 898980182 538728808\n624037110 955334452 644656371 684645088 612630196 365375437\n135489465 838789241 374389562 238291227 977400760 496900790\n921432977 606711088 740916866 405856539 822796504 19906274\n631056816 823924205 719574385 332547421 298321832 594473309\n145245098 519214077 131200957 702537460 682007417 304700059\n419152148 943797679 19640790 464034522 441355576 454665900\n822982080 991406863 249537771 45708817 502700323 683669766\n326512291 871373558 410675396 208947517 645163777 622674739\n606534945 349193474 895660493 579299011 661372605 314032169\n497618687 102217570 426720147 363888279 910628723 894753922\n", "output": "18827363\n93291123\n549166310\n727345493\n212460686\n835043567\n382235992\n234331494\n126083178\n340949995\n361327462\n549134620\n481914498\n34075195\n89718312\n945811332\n898724999\n944812555\n123616792\n779724718\n995912550\n188150326\n361531843\n801483934\n" } ]
https://atcoder.jp/contests/npcapc_2024/tasks/npcapc_2024_o
問題文 NPCA 校には 2N 人の生徒がおり、各生徒には 1 から 2N の順番が付けられています。なぷか君は NPCA 校の教師で、生徒を N 人ずつの 2 つのクラスに分けることになりました。 ここで、クラス分けの不満度を以下のように定義します。 整数 i(1 \le i \le M) のうち、生徒 A_i と生徒 B_i が同じクラスにいるもの全てに対する 2^i の総和 なぷか君のために、不満度が最小となるクラス分けの方法を 1 つ構築してください。
[ { "input": "2 4\n1 3\n2 4\n1 4\n1 2\n", "output": "0101\n" }, { "input": "1 0\n", "output": "01\n" }, { "input": "3 7\n2 5\n1 3\n4 6\n2 6\n4 5\n2 4\n5 6\n", "output": "011010\n" } ]
https://atcoder.jp/contests/npcapc_2024/tasks/npcapc_2024_p
問題文 PCT 君は次のような問題を作りました。 Shuffle and Increasing 長さ N の正整数列 A=(A_1,A_2,\dots,A_N) が与えられます。 A を並び替えて得られる列のうち、 A_i < A_{i+1} を満たす i がちょうど M 個あるものの個数を 998244353 で割ったあまりを求めてください。 制約 1 \le N \le 30 0 \le M \le N-1 1 \le A_i \le N PCT 君は上記の問題で答えが X になる入力を作りたいです。そのような入力が存在するか判定し、存在するならば 1 個構築してください。 T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
[ { "input": "4\n4\n131\n333854876\n620963539\n", "output": "4 2\n1 3 3 2\n6 3\n6 2 6 4 5 1 \n17 10\n8 7 1 14 8 5 12 5 2 17 7 16 15 17 14 15 6 \n24 14\n16 8 14 18 15 16 21 4 10 14 3 4 22 8 16 11 22 17 11 5 12 2 13 18 \n" } ]
https://atcoder.jp/contests/ahc034/tasks/ahc034_a
Problem Statement There is an N \times N grid of land. Let (0,0) be the coordinates of the top-left square and (i,j) be the coordinates of the square located i squares down and j squares to the right from there. The height h_{i,j} of each square (i,j) is given as input. The total height of all squares is exactly 0 . Initially, the dump truck is at the square (0,0) with an empty load. In each turn, you can perform one of the following three operations: Specify an integer d satisfying 0<d\leq 10^6 to load d units of soil from the current square onto the dump truck. This operation decreases the height of the current square by d and increases the amount of soil loaded onto the dump truck by d . The height can become negative. This operation incurs a cost of d . Specify an integer d satisfying 0<d\leq 10^6 to unload d units of soil from the dump truck to the current square. This operation increases the height of the current square by d and decreases the amount of soil loaded onto the dump truck by d . The value of d must not exceed the amount of soil currently loaded onto the dump truck. This operation incurs a cost of d . Move the dump truck to an adjacent square (up, down, left, or right). The dump truck cannot move outside the N \times N grid. If the dump truck has d units of soil loaded, this operation incurs a cost of 100+d . You can perform a maximum of 100000 turns of operation. Your task is to determine the sequence of operations that minimizes the cost to make the height of every square 0 .
[ { "input": "20\n10 4 0 0 4 10 16 20 21 16 11 2 -7 -16 -23 -25 -23 -16 -7 2\n16 10 6 5 7 10 13 15 14 10 4 -4 -13 -21 -29 -32 -29 -22 -13 -3\n20 14 10 8 9 10 11 12 10 6 0 -7 -15 -24 -31 -33 -31 -25 -16 -8\n20 15 12 10 10 10 10 11 8 5 0 -5 -12 -20 -26 -29 -28 -23 -15 -7\n16 13 12 10 10 10 10 10 9 7 4 1 -4 -10 -16 -20 -20 -17 -10 -3\n10 10 10 10 11 10 10 10 10 10 10 9 7 2 -3 -8 -9 -8 -3 3\n4 7 9 10 10 10 10 10 11 14 16 18 17 14 9 5 1 1 4 9\n0 5 8 10 10 10 10 10 12 15 20 24 25 24 19 14 9 7 9 13\n0 6 10 12 11 11 9 9 10 14 20 26 29 28 24 18 13 10 10 14\n4 10 14 16 13 10 7 5 6 10 16 23 26 26 23 16 10 8 6 10\n11 16 20 21 16 10 4 0 0 4 10 16 21 20 16 10 4 0 1 4\n16 23 26 24 18 9 1 -4 -7 -4 2 9 12 12 8 3 -3 -6 -7 -3\n20 26 27 24 17 7 -3 -11 -14 -12 -7 -1 2 2 -1 -5 -10 -12 -11 -7\n20 24 24 20 13 3 -8 -17 -21 -20 -16 -12 -9 -9 -11 -14 -15 -15 -12 -6\n16 18 17 14 6 -3 -13 -20 -24 -25 -23 -21 -20 -19 -21 -21 -18 -15 -10 -3\n10 10 8 4 -1 -8 -14 -20 -23 -25 -25 -26 -27 -28 -28 -25 -21 -14 -5 3\n4 2 -1 -4 -7 -9 -12 -15 -18 -19 -23 -26 -29 -31 -31 -28 -21 -11 -1 8\n0 -4 -7 -9 -9 -8 -7 -7 -8 -11 -16 -20 -26 -30 -31 -28 -20 -8 2 12\n0 -6 -9 -10 -7 -3 1 3 3 -1 -7 -13 -20 -26 -29 -27 -20 -9 2 13\n4 -3 -6 -6 -3 3 9 12 12 9 3 -5 -12 -20 -25 -26 -21 -12 -1 9\n", "output": "+10\nR\n+4\nR\nR\nR\nR\nR\nR\nR\n+50\nR\nR\nR\nR\n-7\nD\n-13\nL\n-44\n" } ]
https://atcoder.jp/contests/abc358/tasks/abc358_a
Problem Statement Takahashi is heading to AtCoder Land. There is a signboard in front of him, and he wants to determine whether it says AtCoder Land. You are given two strings S and T separated by a space. Determine whether S= AtCoder and T= Land .
[ { "input": "AtCoder Land\n", "output": "Yes\n" }, { "input": "CodeQUEEN Land\n", "output": "No\n" }, { "input": "aTcodeR lANd\n", "output": "No\n" } ]
https://atcoder.jp/contests/abc358/tasks/abc358_b
Problem Statement At the entrance of AtCoder Land, there is a single ticket booth where visitors line up to purchase tickets one by one. The purchasing process takes A seconds per person. Once the person at the front of the line finishes purchasing their ticket, the next person (if any) immediately starts their purchasing process. Currently, there is no one in line at the ticket booth, and N people will come to buy tickets one after another. Specifically, the i -th person will arrive at the ticket booth T_i seconds from now. If there is already a line, they will join the end of it; if not, they will start the purchasing process immediately. Here, T_1 < T_2 < \dots < T_N . For each i\ (1 \leq i \leq N) , determine how many seconds from now the i -th person will finish purchasing their ticket.
[ { "input": "3 4\n0 2 10\n", "output": "4\n8\n14\n" }, { "input": "3 3\n1 4 7\n", "output": "4\n7\n10\n" }, { "input": "10 50000\n120190 165111 196897 456895 540000 552614 561627 743796 757613 991216\n", "output": "170190\n220190\n270190\n506895\n590000\n640000\n690000\n793796\n843796\n1041216\n" } ]
https://atcoder.jp/contests/abc358/tasks/abc358_c
Problem Statement In AtCoder Land, there are N popcorn stands numbered 1 to N . They have M different flavors of popcorn, labeled 1, 2, \dots, M , but not every stand sells all flavors of popcorn. Takahashi has obtained information about which flavors of popcorn are sold at each stand. This information is represented by N strings S_1, S_2, \dots, S_N of length M . If the j -th character of S_i is o , it means that stand i sells flavor j of popcorn. If it is x , it means that stand i does not sell flavor j . Each stand sells at least one flavor of popcorn, and each flavor of popcorn is sold at least at one stand. Takahashi wants to try all the flavors of popcorn but does not want to move around too much. Determine the minimum number of stands Takahashi needs to visit to buy all the flavors of popcorn.
[ { "input": "3 5\noooxx\nxooox\nxxooo\n", "output": "2\n" }, { "input": "3 2\noo\nox\nxo\n", "output": "1\n" }, { "input": "8 6\nxxoxxo\nxxoxxx\nxoxxxx\nxxxoxx\nxxoooo\nxxxxox\nxoxxox\noxoxxo\n", "output": "3\n" } ]
https://atcoder.jp/contests/abc358/tasks/abc358_d
Problem Statement A souvenir shop at AtCoder Land sells N boxes. The boxes are numbered 1 to N , and box i has a price of A_i yen and contains A_i pieces of candy. Takahashi wants to buy M out of the N boxes and give one box each to M people named 1, 2, \ldots, M . Here, he wants to buy boxes that can satisfy the following condition: For each i = 1, 2, \ldots, M , person i is given a box containing at least B_i pieces of candy. Note that it is not allowed to give more than one box to a single person or to give the same box to multiple people. Determine whether it is possible to buy M boxes that can satisfy the condition, and if it is possible, find the minimum total amount of money Takahashi needs to pay.
[ { "input": "4 2\n3 4 5 4\n1 4\n", "output": "7\n" }, { "input": "3 3\n1 1 1\n1000000000 1000000000 1000000000\n", "output": "-1\n" }, { "input": "7 3\n2 6 8 9 5 1 11\n3 5 7\n", "output": "19\n" } ]
https://atcoder.jp/contests/abc358/tasks/abc358_e
Problem Statement AtCoder Land sells tiles with English letters written on them. Takahashi is thinking of making a nameplate by arranging these tiles in a row. Find the number, modulo 998244353 , of strings consisting of uppercase English letters with a length between 1 and K , inclusive, that satisfy the following conditions: For every integer i satisfying 1 \leq i \leq 26 , the following holds: Let a_i be the i -th uppercase English letter in lexicographical order. For example, a_1 = A , a_5 = E , a_{26} = Z . The number of occurrences of a_i in the string is between 0 and C_i , inclusive.
[ { "input": "2\n2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", "output": "10\n" }, { "input": "358\n1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", "output": "64\n" }, { "input": "1000\n1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000\n", "output": "270274035\n" } ]
https://atcoder.jp/contests/abc358/tasks/abc358_f
Problem Statement Snuke is planning to build a maze as a new attraction in AtCoder Land. The maze is represented as a grid with N rows and M columns, with the top edge of the top-right cell being the entrance and the bottom edge of the bottom-right cell being the exit. He will create the maze by appropriately placing walls between adjacent cells. He loves simple mazes, so he wants the path from the entrance to the exit to pass through exactly K cells without any branches. Determine if it is possible to create such a maze, and if possible, construct one. For example, in the following figure, N=3 and M=3 , and walls are placed at the solid lines (walls are always placed around the outer perimeter except for the entrance and exit). In this case, the path from the entrance to the exit passes through exactly 7 cells without any branches. Below is a formal statement. There is a grid with N rows and M columns. Let (i, j) denote the cell at the i -th row from the top and j -th column from the left. For each pair of side-adjacent cells, you can decide whether to place a wall between them. Determine whether it is possible to place walls to satisfy the following condition, and if it is possible, construct one such placement. Consider an undirected graph G with NM vertices. Each vertex of G is uniquely labeled by a pair of integers (i,j)\ (1\leq i\leq N, 1\leq j\leq M) . Two distinct vertices (i_1,j_1) and (i_2,j_2) are connected by an edge if and only if |i_1-i_2|+|j_1-j_2|=1 and there is no wall between the corresponding cells (i_1,j_1) and (i_2,j_2) on the grid. Condition: there exists a simple path with K vertices that connects the two vertices (1,M) and (N,M) , and the connected component containing the vertices (1,M) and (N,M) consists only of this path.
[ { "input": "3 3 7\n", "output": "Yes\n+++++S+\n+o.o.o+\n+.+-+-+\n+o.o.o+\n+-+-+.+\n+o.o|o+\n+++++G+\n" }, { "input": "3 3 2\n", "output": "No\n" }, { "input": "4 1 4\n", "output": "Yes\n+S+\n+o+\n+.+\n+o+\n+.+\n+o+\n+.+\n+o+\n+G+\n" } ]
https://atcoder.jp/contests/abc358/tasks/abc358_g
Problem Statement AtCoder Land is represented by a grid with H rows and W columns. Let (i, j) denote the cell at the i -th row and j -th column. Takahashi starts at cell (S_i, S_j) and repeats the following action K times: He either stays in the current cell or moves to an adjacent cell. After this action, if he is in cell (i, j) , he gains a fun value of A_{i, j} . Find the maximum total fun value he can gain. Here, a cell (x', y') is considered adjacent to cell (x, y) if and only if |x - x'| + |y - y'| = 1 .
[ { "input": "2 3 3\n1 2\n2 1 2\n3 4 5\n", "output": "14\n" }, { "input": "2 2 1000000000\n2 1\n100 100\n100 99\n", "output": "100000000000\n" } ]
https://atcoder.jp/contests/atcoder12live/tasks/atcoder12live_a
問題文 演奏会では N 人の演奏者が横一列に並び T 秒の曲を演奏している。 左から i (0\leq i\leq N-1) 番目の演奏者を演奏者 i と呼ぶことにする。 各演奏者について、デフォルトの音量を基準値 0 とする。指揮者は毎秒、 0\leq L\leq R\leq N-1 を満たす整数の組 (L,R) と整数 dV を選択し、 L\leq i\leq R を満たす全ての演奏者 i に、 dV だけ音量を変更するよう指示を出すことができる。 時刻 t に指示を出された演奏者は時刻 t 以降( t を含む)の音量が dV だけ変化する。 複数回音量変更の指示をした場合、変化量は累積される。例えば、時刻 0 に演奏者 i の音量を 1 変化させ、時刻 1 に演奏者 i の音量を -2 変化させた場合、時刻 1 における音量は 1-2=-1 となる。 各時刻 t 、各演奏者 i について、理想的な音量 V_{t,i} が入力として与えられるので、 実際の音量と理想的な音量との差の絶対値の総和が出来るだけ小さくなるように指揮をして欲しい。
[ { "input": "5 3\n0 0 0 0 0\n0 -1 -1 -1 0\n1 2 2 2 2\n", "output": "0 0 0\n1 3 1\n1 4 -3\n" } ]
https://atcoder.jp/contests/abc357/tasks/abc357_a
Problem Statement There is a bottle of disinfectant that can disinfect exactly M hands. N aliens come one by one to disinfect their hands. The i -th alien ( 1 \leq i \leq N ) has H_i hands and wants to disinfect all of their hands once. Determine how many aliens can disinfect all of their hands. Here, even if there is not enough disinfectant left for an alien to disinfect all of their hands when they start, they will use up the remaining disinfectant.
[ { "input": "5 10\n2 3 2 5 3\n", "output": "3\n" }, { "input": "5 10\n2 3 2 3 5\n", "output": "4\n" }, { "input": "1 5\n1\n", "output": "1\n" } ]
https://atcoder.jp/contests/abc357/tasks/abc357_b
Problem Statement You are given a string S consisting of lowercase and uppercase English letters. The length of S is odd. If the number of uppercase letters in S is greater than the number of lowercase letters, convert all lowercase letters in S to uppercase. Otherwise, convert all uppercase letters in S to lowercase.
[ { "input": "AtCoder\n", "output": "atcoder\n" }, { "input": "SunTORY\n", "output": "SUNTORY\n" }, { "input": "a\n", "output": "a\n" } ]