problem_statement
stringlengths
147
8.53k
input
stringlengths
1
771
output
stringlengths
1
592
time_limit
stringclasses
32 values
memory_limit
stringclasses
21 values
tags
stringlengths
6
168
F. Panda Meetupstime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe red pandas are in town to meet their relatives, the blue pandas! The town is modeled by a number line.The pandas have already planned their meetup, but the schedule keeps changing. You are given q updates of the form x t c. If c < 0, it means |c| more red pandas enter the number line at position x and time t. Then, each unit of time, they can each independently move one unit in either direction across the number line, or not move at all. If c > 0, it means that c more blue pandas check position x for red pandas at time t. If a blue panda does not meet a red panda at that specific location and time, they dejectedly leave the number line right away. If there is a red panda at a position at the same time a blue panda checks it, they form a friendship and leave the number line. Each red panda can form a friendship with at most one blue panda and vice versa. The updates will be given in order of non-decreasing x values. After each update, please print the maximum number of friendships if the red pandas move in an optimal order based on all the updates given in the input above (and including) this update. The order in which a red panda moves can change between updates.InputThe first line contains q (1 \leq q \leq 2 \cdot 10^5)  – the number of updates.The i-th line of the next q lines consists of 3 integers x_i, t_i and c_i (0 \leq x_i \leq 10^9, 0 \leq t_i \leq 10^9, 0 < |c_i| \leq 1000) – the description of the i-th update.It is guaranteed that the x_i will be given in non-decreasing order.OutputAfter each update, print the maximum number of friendships that can be formed. ExamplesInput 5 0 6 3 4 2 -5 7 4 -6 10 5 100 10 8 7 Output 0 3 3 3 10 Input 5 0 6 3 4 2 -5 7 4 -6 10 5 100 11 8 7 Output 0 3 3 3 9 Input 7 0 8 6 2 7 -2 3 1 -6 5 3 -8 7 3 -3 8 0 -2 8 2 1 Output 0 0 6 6 6 6 7 Input 4 0 0 -3 0 0 2 0 0 4 0 0 -10 Output 0 2 3 6 NoteIn the first example, the number of friendships after each update can be optimized as follows: 3 blue pandas now check for red pandas at position 0 at time 6. There are no red pandas anywhere, so there are no friendships. 5 red pandas now appear at position 4 and time 2. 4 of the red pandas can travel to position 0 at time 6, where 3 of them can make friendships with the 3 existing blue pandas. 6 red pandas now appear at position 7 and time 4. No new blue pandas are added, so the maximum number of friendships is still 3. 100 blue pandas now appear at position 10 and time 5. No red pandas can reach them at a time of 5, so no new friendships are created. 7 blue pandas now appear at position 10 and time 8. 6 of the red pandas at position 7 and time 4, along with 1 red panda at position 4 and time 2, can reach 7 of the blue pandas at position 10 at time 8, adding 7 new friendships. This brings the total to 10 friendships.
5 0 6 3 4 2 -5 7 4 -6 10 5 100 10 8 7
0 3 3 3 10
4 seconds
256 megabytes
['data structures', 'dp', 'flows', '*3500']
E. Rivalriestime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputNtarsis has an array a of length n.The power of a subarray a_l \dots a_r (1 \leq l \leq r \leq n) is defined as: The largest value x such that a_l \dots a_r contains x and neither a_1 \dots a_{l-1} nor a_{r+1} \dots a_n contains x. If no such x exists, the power is 0. Call an array b a rival to a if the following holds: The length of both a and b are equal to some n. Over all l, r where 1 \leq l \leq r \leq n, the power of a_l \dots a_r equals the power of b_l \dots b_r. The elements of b are positive. Ntarsis wants you to find a rival b to a such that the sum of b_i over 1 \leq i \leq n is maximized. Help him with this task!InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^5). The description of the test cases follows.The first line of each test case has a single integer n (1 \leq n \leq 10^5).The next line contains n integers a_1, a_2, \ldots, a_n (1 \leq a_i \leq 10^9).It is guaranteed that the sum of n across all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output n integers b_1, b_2, \ldots, b_n — a valid rival to a such that b_1 + b_2 + \cdots + b_n is maximal. If there exist multiple rivals with the maximum sum, output any of them.ExampleInput 751 4 1 3 351 4 1 8 852 1 1 1 283 2 3 5 2 2 5 381 1 1 1 4 3 3 3101 9 5 9 8 1 5 8 9 1161 1 1 1 5 5 5 5 9 9 9 9 7 7 7 7Output 2 4 2 3 3 3 4 3 8 8 2 1 2 1 2 4 2 4 5 5 2 5 4 1 2 2 1 4 3 2 3 7 9 5 9 8 9 5 8 9 7 1 8 8 1 5 8 8 5 9 9 9 9 7 8 8 7 NoteFor the first test case, one rival with the maximal sum is [2, 4, 2, 3, 3].[2, 4, 2, 3, 3] can be shown to be a rival to [1, 4, 1, 3, 3].All possible subarrays of a and b and their corresponding powers are listed below: The power of a[1:1] = [1] = 0, the power of b[1:1] = [2] = 0. The power of a[1:2] = [1, 4] = 4, the power of b[1:2] = [2, 4] = 4. The power of a[1:3] = [1, 4, 1] = 4, the power of b[1:3] = [2, 4, 2] = 4. The power of a[1:4] = [1, 4, 1, 3] = 4, the power of b[1:4] = [2, 4, 2, 3] = 4. The power of a[1:5] = [1, 4, 1, 3, 3] = 4, the power of b[1:5] = [2, 4, 2, 3, 3] = 4. The power of a[2:2] = [4] = 4, the power of b[2:2] = [4] = 4. The power of a[2:3] = [4, 1] = 4, the power of b[2:3] = [4, 2] = 4. The power of a[2:4] = [4, 1, 3] = 4, the power of b[2:4] = [4, 2, 3] = 4. The power of a[2:5] = [4, 1, 3, 3] = 4, the power of b[2:5] = [4, 2, 3, 3] = 4. The power of a[3:3] = [1] = 0, the power of b[3:3] = [2] = 0. The power of a[3:4] = [1, 3] = 0, the power of b[3:4] = [2, 3] = 0. The power of a[3:5] = [1, 3, 3] = 3, the power of b[3:5] = [2, 3, 3] = 3. The power of a[4:4] = [3] = 0, the power of b[4:4] = [3] = 0. The power of a[4:5] = [3, 3] = 3, the power of b[4:5] = [3, 3] = 3. The power of a[5:5] = [3] = 0, the power of b[5:5] = [3] = 0. It can be shown there exists no rival with a greater sum than 2 + 4 + 2 + 3 + 3 = 14.
751 4 1 3 351 4 1 8 852 1 1 1 283 2 3 5 2 2 5 381 1 1 1 4 3 3 3101 9 5 9 8 1 5 8 9 1161 1 1 1 5 5 5 5 9 9 9 9 7 7 7 7
2 4 2 3 3 3 4 3 8 8 2 1 2 1 2 4 2 4 5 5 2 5 4 1 2 2 1 4 3 2 3 7 9 5 9 8 9 5 8 9 7 1 8 8 1 5 8 8 5 9 9 9 9 7 8 8 7
2 seconds
512 megabytes
['constructive algorithms', 'data structures', 'greedy', '*3400']
D. Miriany and Matchsticktime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMiriany's matchstick is a 2 \times n grid that needs to be filled with characters A or B. He has already filled in the first row of the grid and would like you to fill in the second row. You must do so in a way such that the number of adjacent pairs of cells with different characters^\dagger is equal to k. If it is impossible, report so.^\dagger An adjacent pair of cells with different characters is a pair of cells (r_1, c_1) and (r_2, c_2) (1 \le r_1, r_2 \le 2, 1 \le c_1, c_2 \le n) such that |r_1 - r_2| + |c_1 - c_2| = 1 and the characters in (r_1, c_1) and (r_2, c_2) are different.InputThe first line consists of an integer t, the number of test cases (1 \leq t \leq 1000). The description of the test cases follows.The first line of each test case has two integers, n and k (1 \leq n \leq 2 \cdot 10^5, 0 \leq k \leq 3 \cdot n) – the number of columns of the matchstick, and the number of adjacent pairs of cells with different characters required.The following line contains string s of n characters (s_i is either A or B) – Miriany's top row of the matchstick.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, if there is no way to fill the second row with the number of adjacent pairs of cells with different characters equals k, output "NO". Otherwise, output "YES". Then, print n characters that a valid bottom row for Miriany's matchstick consists of. If there are several answers, output any of them.ExampleInput 410 1ABBAAABBAA4 5AAAA9 17BAAABBAAB4 9ABABOutput NO YES BABB YES ABABAABAB NONoteIn the first test case, it can be proved that there exists no possible way to fill in row 2 of the grid such that k = 1. For the second test case, BABB is one possible answer.The grid below is the result of filling in BABB as the second row. \begin{array}{|c|c|} \hline A & A & A & A \cr \hline B & A & B & B \cr \hline \end{array} The pairs of different characters are shown below in red: \begin{array}{|c|c|} \hline \color{red}{A} & A & A & A \cr \hline \color{red}{B} & A & B & B \cr \hline \end{array}—————————————————\begin{array}{|c|c|} \hline A & A & \color{red}{A} & A \cr \hline B & A & \color{red}{B} & B \cr \hline \end{array}—————————————————\begin{array}{|c|c|} \hline A & A & A & \color{red}{A} \cr \hline B & A & B & \color{red}{B} \cr \hline \end{array}—————————————————\begin{array}{|c|c|} \hline A & A & A & A \cr \hline \color{red}{B} & \color{red}{A} & B & B \cr \hline \end{array}—————————————————\begin{array}{|c|c|} \hline A & A & A & A \cr \hline B & \color{red}{A} & \color{red}{B} & B \cr \hline \end{array}There are a total of 5 pairs, which satisfies k.
410 1ABBAAABBAA4 5AAAA9 17BAAABBAAB4 9ABAB
NO YES BABB YES ABABAABAB NO
2 seconds
256 megabytes
['constructive algorithms', 'dp', 'greedy', '*2800']
C. Ina of the Mountaintime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputTo prepare her "Takodachi" dumbo octopuses for world domination, Ninomae Ina'nis, a.k.a. Ina of the Mountain, orders Hoshimachi Suisei to throw boulders at them. Ina asks you, Kiryu Coco, to help choose where the boulders are thrown.There are n octopuses on a single-file trail on Ina's mountain, numbered 1, 2, \ldots, n. The i-th octopus has a certain initial health value a_i, where 1 \leq a_i \leq k.Each boulder crushes consecutive octopuses with indexes l, l+1, \ldots, r, where 1 \leq l \leq r \leq n. You can choose the numbers l and r arbitrarily for each boulder.For each boulder, the health value of each octopus the boulder crushes is reduced by 1. However, as octopuses are immortal, once they reach a health value of 0, they will immediately regenerate to a health value of k. Given the octopuses' initial health values, find the minimum number of boulders that need to be thrown to make the health of all octopuses equal to k.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^5). The description of the test cases follows.The first line of each test case contains two integers n and k (1 \le n \le 2 \cdot 10^5, 1 \le k \le 10^9) – the number of octopuses, and the upper bound of a octopus's health value.The second line contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le k) – the initial health values of the octopuses.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output the minimum number of boulders that need to be thrown to make the health values of all octopuses equal to k.ExampleInput 24 31 2 1 37 31 2 3 1 3 2 1Output 2 4 NoteIn the first test case, the minimum number of boulders thrown is 2: Throw the first boulder between [l,r] = [1,3]. Then, the octopuses' health values become [3, 1, 3, 3]. Throw the second boulder between [l,r] = [2,2]. Then, the octopuses' health values become [3, 3, 3, 3]. In the second test case, the minimum number of boulders thrown is 4. The [l,r] ranges are [1,7], [2, 6], [3, 5], [4, 4].
24 31 2 1 37 31 2 3 1 3 2 1
2 4
2 seconds
256 megabytes
['data structures', 'dp', 'greedy', 'math', '*2400']
B. Imbalanced Arraystime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputNtarsis has come up with an array a of n non-negative integers.Call an array b of n integers imbalanced if it satisfies the following: -n\le b_i\le n, b_i \ne 0, there are no two indices (i, j) (1 \le i, j \le n) such that b_i + b_j = 0, for each 1 \leq i \leq n, there are exactly a_i indices j (1 \le j \le n) such that b_i+b_j>0, where i and j are not necessarily distinct. Given the array a, Ntarsis wants you to construct some imbalanced array. Help him solve this task, or determine it is impossible.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^5). The description of the test cases follows.The first line of each test case has a single integer n (1 \leq n \leq 10^5).The next line contains n integers a_1, a_2, \ldots, a_n (0 \leq a_i \leq n).It is guaranteed that the sum of n across all test cases does not exceed 10^5.OutputFor each test case, output "NO" if there exists no imbalanced array. Otherwise, output "YES". Then, on the next line, output n integers b_1, b_2, \ldots, b_n where b_i \neq 0 for all 1 \leq i \leq n — an imbalanced array.ExampleInput 51141 4 3 430 1 044 3 2 131 3 1Output YES 1 NO YES -3 1 -2 YES 4 2 -1 -3 YES -1 3 -1NoteFor the first test case, b = [1] is an imbalanced array. This is because for i = 1, there is exactly one j (j = 1) where b_1 + b_j > 0.For the second test case, it can be shown that there exists no imbalanced array.For the third test case, a = [0, 1, 0]. The array b = [-3, 1, -2] is an imbalanced array. For i = 1 and i = 3, there exists no index j such that b_i + b_j > 0. For i = 2, there is only one index j = 2 such that b_i + b_j > 0 (b_2 + b_2 = 1 + 1 = 2). Another possible output for the third test case could be b = [-2, 1, -3].
51141 4 3 430 1 044 3 2 131 3 1
YES 1 NO YES -3 1 -2 YES 4 2 -1 -3 YES -1 3 -1
2 seconds
256 megabytes
['constructive algorithms', 'graphs', 'greedy', 'math', 'sortings', 'two pointers', '*1800']
A. Ntarsis' Settime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputNtarsis has been given a set S, initially containing integers 1, 2, 3, \ldots, 10^{1000} in sorted order. Every day, he will remove the a_1-th, a_2-th, \ldots, a_n-th smallest numbers in S simultaneously.What is the smallest element in S after k days?InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^5). The description of the test cases follows.The first line of each test case consists of two integers n and k (1 \leq n,k \leq 2 \cdot 10^5) — the length of a and the number of days.The following line of each test case consists of n integers a_1, a_2, \ldots, a_n (1 \leq a_i \leq 10^9) — the elements of array a.It is guaranteed that: The sum of n over all test cases won't exceed 2 \cdot 10^5; The sum of k over all test cases won't exceed 2 \cdot 10^5; a_1 < a_2 < \cdots < a_n for all test cases. OutputFor each test case, print an integer that is the smallest element in S after k days.ExampleInput 75 11 2 4 5 65 31 3 5 6 74 10002 3 4 59 14341 4 7 9 12 15 17 18 2010 41 3 5 7 9 11 13 15 17 1910 61 4 7 10 13 16 19 22 25 2810 1500001 3 4 5 10 11 12 13 14 15Output 3 9 1 12874 16 18 1499986 NoteFor the first test case, each day the 1-st, 2-nd, 4-th, 5-th, and 6-th smallest elements need to be removed from S. So after the first day, S will become \require{cancel} \{\cancel 1, \cancel 2, 3, \cancel 4, \cancel 5, \cancel 6, 7, 8, 9, \ldots\} = \{3, 7, 8, 9, \ldots\}. The smallest element is 3.For the second case, each day the 1-st, 3-rd, 5-th, 6-th and 7-th smallest elements need to be removed from S. S will be changed as follows: DayS beforeS after1\{\cancel 1, 2, \cancel 3, 4, \cancel 5, \cancel 6, \cancel 7, 8, 9, 10, \ldots \}\to\{2, 4, 8, 9, 10, \ldots\}2\{\cancel 2, 4, \cancel 8, 9, \cancel{10}, \cancel{11}, \cancel{12}, 13, 14, 15, \ldots\}\to\{4, 9, 13, 14, 15, \ldots\}3\{\cancel 4, 9, \cancel{13}, 14, \cancel{15}, \cancel{16}, \cancel{17}, 18, 19, 20, \ldots\}\to\{9, 14, 18, 19, 20, \ldots\} The smallest element left after k = 3 days is 9.
75 11 2 4 5 65 31 3 5 6 74 10002 3 4 59 14341 4 7 9 12 15 17 18 2010 41 3 5 7 9 11 13 15 17 1910 61 4 7 10 13 16 19 22 25 2810 1500001 3 4 5 10 11 12 13 14 15
3 9 1 12874 16 18 1499986
2 seconds
256 megabytes
['binary search', 'math', 'number theory', '*1800']
G. Vlad and the Mountainstime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputVlad decided to go on a trip to the mountains. He plans to move between n mountains, some of which are connected by roads. The i-th mountain has a height of h_i.If there is a road between mountains i and j, Vlad can move from mountain i to mountain j by spending h_j - h_i units of energy. If his energy drops below zero during the transition, he will not be able to move from mountain i to mountain j. Note that h_j - h_i can be negative and then the energy will be restored.Vlad wants to consider different route options, so he asks you to answer the following queries: is it possible to construct some route starting at mountain a and ending at mountain b, given that he initially has e units of energy?InputThe first line of the input contains an integer t (1 \le t \le 10^4) — the number of test cases.The descriptions of the test cases follow.The first line of each test case contains two numbers n and m (2 \le n \le 2 \cdot 10^5, 1 \le m \le \min(\frac{n\cdot(n - 1)}{2}, 2 \cdot 10^5)) — the number of mountains and the number of roads between them, respectively.The second line contains n integers h_1, h_2, h_3, \dots, h_n (1 \le h_i \le 10^9) — the heights of the mountains.The next m lines contain two integers u and v (1 \le u, v \le n, u \ne v) — the numbers of the mountains connected by a road. It is guaranteed that no road appears twice.The next line contains an integer q (1 \le q \le 2 \cdot 10^5) — the number of queries.The following q lines contain three numbers a, b, and e (1 \le a, b \le n, 0 \le e \le 10^9) — the initial and final mountains of the route, and the amount of energy, respectively.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5. The same guarantee applies to m and q.OutputFor each query, output "YES" if Vlad can construct a route from mountain a to mountain b, and "NO" otherwise.You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).In the examples below, the answers for different test cases are separated by an empty line, which you do not need to output.ExamplesInput 27 71 5 3 4 2 4 11 44 33 63 22 55 65 751 1 36 2 04 7 01 7 41 7 26 54 7 6 2 5 11 35 31 52 46 251 5 11 3 11 2 10006 2 66 2 5Output YES NO YES YES NO YES NO NO YES NO Input 23 21 3 91 22 351 1 13 2 21 1 23 3 01 2 13 31 4 11 22 31 353 3 91 3 61 1 23 3 63 3 4Output YES YES YES YES NO YES YES YES YES YES Input 16 107 9 2 10 8 64 26 14 53 56 41 32 66 51 23 654 4 83 3 15 5 92 1 76 6 10Output YES YES YES YES YES
27 71 5 3 4 2 4 11 44 33 63 22 55 65 751 1 36 2 04 7 01 7 41 7 26 54 7 6 2 5 11 35 31 52 46 251 5 11 3 11 2 10006 2 66 2 5
YES NO YES YES NO YES NO NO YES NO
5 seconds
256 megabytes
['binary search', 'data structures', 'dsu', 'graphs', 'implementation', 'sortings', 'trees', 'two pointers', '*2000']
F. Lisa and the Martianstime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputLisa was kidnapped by martians! It okay, because she has watched a lot of TV shows about aliens, so she knows what awaits her. Let's call integer martian if it is a non-negative integer and strictly less than 2^k, for example, when k = 12, the numbers 51, 1960, 0 are martian, and the numbers \pi, -1, \frac{21}{8}, 4096 are not.The aliens will give Lisa n martian numbers a_1, a_2, \ldots, a_n. Then they will ask her to name any martian number x. After that, Lisa will select a pair of numbers a_i, a_j (i \neq j) in the given sequence and count (a_i \oplus x) \& (a_j \oplus x). The operation \oplus means Bitwise exclusive OR, the operation \& means Bitwise And. For example, (5 \oplus 17) \& (23 \oplus 17) = (00101_2 \oplus 10001_2) \& (10111_2 \oplus 10001_2) = 10100_2 \& 00110_2 = 00100_2 = 4.Lisa is sure that the higher the calculated value, the higher her chances of returning home. Help the girl choose such i, j, x that maximize the calculated value.InputThe first line contains an integer t (1 \le t \le 10^4) — number of testcases.Each testcase is described by two lines.The first line contains integers n, k (2 \le n \le 2 \cdot 10^5, 1 \le k \le 30) — the length of the sequence of martian numbers and the value of k.The second line contains n integers a_1, a_2, \ldots, a_n (0 \le a_i < 2^k) — a sequence of martian numbers.It is guaranteed that the sum of n over all testcases does not exceed 2 \cdot 10^5.OutputFor each testcase, print three integers i, j, x (1 \le i, j \le n, i \neq j, 0 \le x < 2^k). The value of (a_i \oplus x) \& (a_j \oplus x) should be the maximum possible.If there are several solutions, you can print any one.ExampleInput 105 43 9 1 4 133 11 0 16 12144 1580 1024 100 9 134 37 3 0 43 20 0 12 412 29 46 14 9 4 4 4 5 10 22 11 02 411 49 42 11 10 1 6 9 11 0 5Output 1 3 14 1 3 0 5 6 4082 2 3 7 1 2 3 1 2 15 4 5 11 1 2 0 1 2 0 2 7 4 NoteFirst testcase: (3 \oplus 14) \& (1 \oplus 14) = (0011_2 \oplus 1110_2) \& (0001_2 \oplus 1110_2) = 1101_2 = 1101_2 \& 1111_2 = 1101_2 = 13.Second testcase: (1 \oplus 0) \& (1 \oplus 0) = 1.Third testcase: (9 \oplus 4082) \& (13 \oplus 4082) = 4091.Fourth testcase: (3 \oplus 7) \& (0 \oplus 7) = 4.
105 43 9 1 4 133 11 0 16 12144 1580 1024 100 9 134 37 3 0 43 20 0 12 412 29 46 14 9 4 4 4 5 10 22 11 02 411 49 42 11 10 1 6 9 11 0 5
1 3 14 1 3 0 5 6 4082 2 3 7 1 2 3 1 2 15 4 5 11 1 2 0 1 2 0 2 7 4
3 seconds
512 megabytes
['bitmasks', 'greedy', 'math', 'strings', 'trees', '*1800']
E. Nastya and Potionstime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAlchemist Nastya loves mixing potions. There are a total of n types of potions, and one potion of type i can be bought for c_i coins.Any kind of potions can be obtained in no more than one way, by mixing from several others. The potions used in the mixing process will be consumed. Moreover, no potion can be obtained from itself through one or more mixing processes.As an experienced alchemist, Nastya has an unlimited supply of k types of potions p_1, p_2, \dots, p_k, but she doesn't know which one she wants to obtain next. To decide, she asks you to find, for each 1 \le i \le n, the minimum number of coins she needs to spend to obtain a potion of type i next.InputThe first line of each test contains an integer t (1 \le t \le 10^4) — the number of test cases.Each test case is described as follows:The first line contains two integers n and k (1 \le k < n \le 2 \cdot 10^5) — the total number of potion types and the number of potion types Nastya already has.The second line contains n integers c_1, c_2, \dots, c_n (1 \le c_i \le 10^9) — the costs of buying the potions.The third line contains k distinct integers p_1, p_2, \dots, p_k (1 \le p_i \le n) — the indices of potions Nastya already has an unlimited supply of.This is followed by n lines describing ways to obtain potions by mixing.Each line starts with the integer m_i (0 \le m_i < n) — the number of potions required to mix a potion of the type i (1 \le i \le n).Then line contains m_i distinct integers e_1, e_2, \dots, e_{m_i} (1 \le e_j \le n, e_j \ne i) — the indices of potions needed to mix a potion of the type i. If this list is empty, then a potion of the type i can only be bought.It is guaranteed that no potion can be obtained from itself through one or more mixing processes.It is guaranteed that the sum of all n values across all test cases does not exceed 2 \cdot 10^5. Similarly, it is guaranteed that the sum of all m_i values across all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output n integers — the minimum number of coins Nastya needs to spend to obtain a potion of each type.ExamplesInput 45 130 8 3 5 1033 2 4 50 0 2 3 50 3 25 143 31 31 20 2 1 25 15 4 1 3 422 4 53 3 5 42 1 41 50 4 21 1 5 42 43 2 4 30 2 2 41 2Output 23 8 0 5 10 0 143 0 5 0 1 3 4 0 0 0 0 Input 36 35 5 4 5 2 23 4 52 2 51 53 4 1 64 2 6 1 50 0 6 21 4 4 1 5 23 64 6 3 4 54 6 5 3 40 1 51 60 2 14 310 1 1Output 0 0 0 0 0 2 0 0 0 0 0 0 0 0 NoteIn the first test case of the first sample, it is optimal: Get a potion of the first type by buying and mixing 2, 4 and 5; a potion of the second type can only be obtained by purchasing it; Nastya already has an unlimited number of potions of the third type; a potion of the fourth type is more profitable to buy than to buy and mix other potions; a potion of the fifth type can only be obtained by purchasing it.
45 130 8 3 5 1033 2 4 50 0 2 3 50 3 25 143 31 31 20 2 1 25 15 4 1 3 422 4 53 3 5 42 1 41 50 4 21 1 5 42 43 2 4 30 2 2 41 2
23 8 0 5 10 0 143 0 5 0 1 3 4 0 0 0 0
3 seconds
256 megabytes
['dfs and similar', 'dp', 'graphs', 'sortings', '*1500']
D. Prefix Permutation Sumstime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYour friends have an array of n elements, calculated its array of prefix sums and passed it to you, accidentally losing one element during the transfer. Your task is to find out if the given array can matches permutation.A permutation of n elements is an array of n numbers from 1 to n such that each number occurs exactly one times in it.The array of prefix sums of the array a — is such an array b that b_i = \sum_{j=1}^i a_j, 1 \le i \le n.For example, the original permutation was [1, 5, 2, 4, 3]. Its array of prefix sums — [1, 6, 8, 12, 15]. Having lost one element, you can get, for example, arrays [6, 8, 12, 15] or [1, 6, 8, 15].It can also be shown that the array [1, 2, 100] does not correspond to any permutation.InputThe first line contains a positive number t (1 \le t \le 10^4) — the number of test cases. The description of the test cases follows.The first line of the description of each test case contains a positive number n (2 \le n \le 2 \cdot 10^5) — the size of the initial array.The second line of the description of each test case contains n - 1 positive number a_i (1 \le a_i \le 10^{18}), a_{i-1} < a_i — elements of the array of prefix sums.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output "YES" if such a permutation exists, and "NO" otherwise.You can output "YES" and "NO" in any case (for example, the strings "yEs", "yes" and "Yes" will be recognized as a positive response).ExampleInput 1256 8 12 1551 6 8 1541 2 10041 3 62231 243 7 1055 44 46 5041 9 10513 21 36 4251 2 3 100000000000000000099 11 12 20 25 28 30 33Output YES YES NO YES YES NO YES NO NO NO NO NO NoteIn the fourth example, for example, the permutation [1, 2, 3, 4] is suitable. In the fifth example, for example, the permutation [2, 1] is suitable. In the seventh example, for example, the permutation [1, 2, 4, 3] is suitable.
1256 8 12 1551 6 8 1541 2 10041 3 62231 243 7 1055 44 46 5041 9 10513 21 36 4251 2 3 100000000000000000099 11 12 20 25 28 30 33
YES YES NO YES YES NO YES NO NO NO NO NO
3 seconds
256 megabytes
['implementation', 'math', '*1300']
C. Tiles Comebacktime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputVlad remembered that he had a series of n tiles and a number k. The tiles were numbered from left to right, and the i-th tile had colour c_i.If you stand on the first tile and start jumping any number of tiles right, you can get a path of length p. The length of the path is the number of tiles you stood on.Vlad wants to see if it is possible to get a path of length p such that: it ends at tile with index n; p is divisible by k the path is divided into blocks of length exactly k each; tiles in each block have the same colour, the colors in adjacent blocks are not necessarily different. For example, let n = 14, k = 3.The colours of the tiles are contained in the array c = [\color{red}{1}, \color{violet}{2}, \color{red}{1}, \color{red}{1}, \color{gray}{7}, \color{orange}{5}, \color{green}{3}, \color{green}{3}, \color{red}{1}, \color{green}{3}, \color{blue}{4}, \color{blue}{4}, \color{violet}{2}, \color{blue}{4}]. Then we can construct a path of length 6 consisting of 2 blocks:\color{red}{c_1} \rightarrow \color{red}{c_3} \rightarrow \color{red}{c_4} \rightarrow \color{blue}{c_{11}} \rightarrow \color{blue}{c_{12}} \rightarrow \color{blue}{c_{14}}All tiles from the 1-st block will have colour \color{red}{\textbf{1}}, from the 2-nd block will have colour \color{blue}{\textbf{4}}.It is also possible to construct a path of length 9 in this example, in which all tiles from the 1-st block will have colour \color{red}{\textbf{1}}, from the 2-nd block will have colour \color{green}{\textbf{3}}, and from the 3-rd block will have colour \color{blue}{\textbf{4}}.InputThe first line of input data contains a single integer t (1 \le t \le 10^4) — the number of test cases.The description of the test cases follows.The first line of each test case contains two integers n and k (1 \le k \le n \le 2 \cdot 10^5)—the number of tiles in the series and the length of the block.The second line of each test case contains n integers c_1, c_2, c_3, \dots, c_n (1 \le c_i \le n) — the colours of the tiles.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output on a separate line: YES if you can get a path that satisfies these conditions; NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive response).ExampleInput 104 21 1 1 114 31 2 1 1 7 5 3 3 1 3 4 4 2 43 33 1 310 41 2 1 2 1 2 1 2 1 26 21 3 4 1 6 62 21 14 22 1 1 12 11 23 22 2 24 11 1 2 2Output YES YES NO NO YES YES NO YES YES YES NoteIn the first test case, you can jump from the first tile to the last tile;The second test case is explained in the problem statement.
104 21 1 1 114 31 2 1 1 7 5 3 3 1 3 4 4 2 43 33 1 310 41 2 1 2 1 2 1 2 1 26 21 3 4 1 6 62 21 14 22 1 1 12 11 23 22 2 24 11 1 2 2
YES YES NO NO YES YES NO YES YES YES
2 seconds
256 megabytes
['greedy', '*1000']
B. Parity Sorttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have an array of integers a of length n. You can apply the following operation to the given array: Swap two elements a_i and a_j such that i \neq j, a_i and a_j are either both even or both odd. Determine whether it is possible to sort the array in non-decreasing order by performing the operation any number of times (possibly zero).For example, let a = [7, 10, 1, 3, 2]. Then we can perform 3 operations to sort the array: Swap a_3 = 1 and a_1 = 7, since 1 and 7 are odd. We get a = [1, 10, 7, 3, 2]; Swap a_2 = 10 and a_5 = 2, since 10 and 2 are even. We get a = [1, 2, 7, 3, 10]; Swap a_4 = 3 and a_3 = 7, since 3 and 7 are odd. We get a = [1, 2, 3, 7, 10]. InputThe first line of input data contains a single integer t (1 \le t \le 10^4) — the number of test cases.The description of the test cases follows.The first line of each test case contains one integer n (1 \le n \le 2 \cdot 10^5) — the length of array a.The second line of each test case contains exactly n positive integers a_1, a_2, \dots, a_n (1 \le a_i \le 10^9) — the elements of array a.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output on a separate line: YES if the array can be sorted by applying the operation to it some number of times; NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive response).ExampleInput 657 10 1 3 2411 9 3 5511 3 15 3 2610 7 8 1 2 311056 6 4 1 6Output YES YES NO NO YES NONoteThe first test case is explained in the problem statement.
657 10 1 3 2411 9 3 5511 3 15 3 2610 7 8 1 2 311056 6 4 1 6
YES YES NO NO YES NO
2 seconds
256 megabytes
['greedy', 'sortings', 'two pointers', '*800']
A. Escalator Conversationstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputOne day, Vlad became curious about who he can have a conversation with on the escalator in the subway. There are a total of n passengers. The escalator has a total of m steps, all steps indexed from 1 to m and i-th step has height i \cdot k.Vlad's height is H centimeters. Two people with heights a and b can have a conversation on the escalator if they are standing on different steps and the height difference between them is equal to the height difference between the steps.For example, if two people have heights 170 and 180 centimeters, and m = 10, k = 5, then they can stand on steps numbered 7 and 5, where the height difference between the steps is equal to the height difference between the two people: k \cdot 2 = 5 \cdot 2 = 10 = 180 - 170. There are other possible ways.Given an array h of size n, where h_i represents the height of the i-th person. Vlad is interested in how many people he can have a conversation with on the escalator individually.For example, if n = 5, m = 3, k = 3, H = 11, and h = [5, 4, 14, 18, 2], Vlad can have a conversation with the person with height 5 (Vlad will stand on step 1, and the other person will stand on step 3) and with the person with height 14 (for example, Vlad can stand on step 3, and the other person will stand on step 2). Vlad cannot have a conversation with the person with height 2 because even if they stand on the extreme steps of the escalator, the height difference between them will be 6, while their height difference is 9. Vlad cannot have a conversation with the rest of the people on the escalator, so the answer for this example is 2.InputThe first line contains a single integer t (1 \le t \le 1000) — the number of test cases.Then the descriptions of the test cases follow.The first line of each test case contains integers: n, m, k, H (1 \le n,m \le 50, 1 \le k,H \le 10^6). Here, n is the number of people, m is the number of steps, k is the height difference between neighboring steps, and H is Vlad's height.The second line contains n integers: h_1, h_2, \ldots, h_n (1 \le h_i \le 10^6). Here, h_i represents the height of the i-th person.OutputFor each test case, output a single integer — the number of people Vlad can have a conversation with on the escalator individually.ExampleInput 75 3 3 115 4 14 18 22 9 5 611 910 50 3 1143 44 74 98 62 60 99 4 11 734 8 8 4968 58 82 737 1 4 6618 66 39 83 48 99 799 1 1 1326 23 84 6 60 87 40 41 256 13 3 2830 70 85 13 1 55Output 2 1 4 1 0 0 3 NoteThe first example is explained in the problem statement.In the second example, Vlad can have a conversation with the person with height 11.In the third example, Vlad can have a conversation with people with heights: 44, 74, 98, 62. Therefore, the answer is 4.In the fourth example, Vlad can have a conversation with the person with height 73.
75 3 3 115 4 14 18 22 9 5 611 910 50 3 1143 44 74 98 62 60 99 4 11 734 8 8 4968 58 82 737 1 4 6618 66 39 83 48 99 799 1 1 1326 23 84 6 60 87 40 41 256 13 3 2830 70 85 13 1 55
2 1 4 1 0 0 3
2 seconds
256 megabytes
['brute force', 'constructive algorithms', 'math', '*800']
H. The Third Lettertime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn order to win his toughest battle, Mircea came up with a great strategy for his army. He has n soldiers and decided to arrange them in a certain way in camps. Each soldier has to belong to exactly one camp, and there is one camp at each integer point on the x-axis (at points \cdots, -2, -1, 0, 1, 2, \cdots).The strategy consists of m conditions. Condition i tells that soldier a_i should belong to a camp that is situated d_i meters in front of the camp that person b_i belongs to. (If d_i < 0, then a_i's camp should be -d_i meters behind b_i's camp.)Now, Mircea wonders if there exists a partition of soldiers that respects the condition and he asks for your help! Answer "YES" if there is a partition of the n soldiers that satisfies all of the m conditions and "NO" otherwise.Note that two different soldiers may be placed in the same camp.InputThe first line contains a single integer t (1 \leq t \leq 100) — the number of test cases.The first line of each test case contains two positive integers n and m (2 \leq n \leq 2 \cdot 10^5; 1 \leq m \leq n) — the number of soldiers, and the number of conditions respectively.Then m lines follow, each of them containing 3 integers: a_i, b_i, d_i (a_i \neq b_i; 1 \leq a_i, b_i \leq n; -10^9 \leq d_i \leq 10^9) — denoting the conditions explained in the statement. Note that if d_i is positive, a_i should be d_i meters in front of b_i and if it is negative, a_i should be -d_i meters behind b_i.Note that the sum of n over all test cases doesn't exceed 2 \cdot 10^5.OutputFor each test case, output "YES" if there is an arrangement of the n soldiers that satisfies all of the m conditions and "NO" otherwise.ExampleInput 45 31 2 22 3 44 2 -66 51 2 22 3 44 2 -65 4 43 5 1002 21 2 51 2 44 11 2 3Output YES NO NO YES NoteFor the first test case, we can partition the soldiers into camps in the following way: soldier: Soldier 1 in the camp with the coordinate x = 3. Soldier 2 in the camp with the coordinate x = 5. Soldier 3 in the camp with the coordinate x = 9. Soldier 4 in the camp with the coordinate x = 11. For the second test case, there is no partition that can satisfy all the constraints at the same time.For the third test case, there is no partition that satisfies all the constraints since we get contradictory information about the same pair.For the fourth test case, in order to satisfy the only condition, a possible partition is: Soldier 1 in the camp with the coordinate x = 10. Soldier 2 in the camp with the coordinate x = 13. Soldier 3 in the camp with the coordinate x = -2023. Soldier 4 in the camp with the coordinate x = -2023.
45 31 2 22 3 44 2 -66 51 2 22 3 44 2 -65 4 43 5 1002 21 2 51 2 44 11 2 3
YES NO NO YES
2 seconds
256 megabytes
['dfs and similar', 'dsu', 'graphs', 'greedy', 'implementation', '*1700']
G. The Morning Startime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputA compass points directly toward the morning star. It can only point in one of eight directions: the four cardinal directions (N, S, E, W) or some combination (NW, NE, SW, SE). Otherwise, it will break. The directions the compass can point. There are n distinct points with integer coordinates on a plane. How many ways can you put a compass at one point and the morning star at another so that the compass does not break?InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^4). The description of the test cases follows.The first line of each test case contains a single integer n (2 \leq n \leq 2 \cdot 10^5) — the number of points.Then n lines follow, each line containing two integers x_i, y_i (-10^9 \leq x_i, y_i \leq 10^9) — the coordinates of each point, all points have distinct coordinates.It is guaranteed that the sum of n over all test cases doesn't exceed 2 \cdot 10^5.OutputFor each test case, output a single integer — the number of pairs of points that don't break the compass.ExampleInput 530 0-1 -11 144 55 76 910 133-1000000000 10000000000 01000000000 -100000000050 02 2-1 5-1 102 1130 0-1 21 -2Output 6 2 6 8 0 NoteIn the first test case, any pair of points won't break the compass: The compass is at (0,0), the morning star is at (-1,-1): the compass will point \text{SW}. The compass is at (0,0), the morning star is at (1,1): the compass will point \text{NE}. The compass is at (-1,-1), the morning star is at (0,0): the compass will point \text{NE}. The compass is at (-1,-1), the morning star is at (1,1): the compass will point \text{NE}. The compass is at (1,1), the morning star is at (0,0): the compass will point \text{SW}. The compass is at (1,1), the morning star is at (-1,-1): the compass will point \text{SW}. In the second test case, only two pairs of points won't break the compass: The compass is at (6,9), the morning star is at (10,13): the compass will point \text{NE}. The compass is at (10,13), the morning star is at (6,9): the compass will point \text{SW}.
530 0-1 -11 144 55 76 910 133-1000000000 10000000000 01000000000 -100000000050 02 2-1 5-1 102 1130 0-1 21 -2
6 2 6 8 0
2 seconds
256 megabytes
['combinatorics', 'data structures', 'geometry', 'implementation', 'math', 'sortings', '*1500']
F. We Were Both Childrentime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMihai and Slavic were looking at a group of n frogs, numbered from 1 to n, all initially located at point 0. Frog i has a hop length of a_i. Each second, frog i hops a_i units forward. Before any frogs start hopping, Slavic and Mihai can place exactly one trap in a coordinate in order to catch all frogs that will ever pass through the corresponding coordinate.However, the children can't go far away from their home so they can only place a trap in the first n points (that is, in a point with a coordinate between 1 and n) and the children can't place a trap in point 0 since they are scared of frogs.Can you help Slavic and Mihai find out what is the maximum number of frogs they can catch using a trap?InputThe first line of the input contains a single integer t (1 \le t \le 100) — the number of test cases. The description of test cases follows.The first line of each test case contains a single integer n (1 \leq n \leq 2 \cdot 10^5) — the number of frogs, which equals the distance Slavic and Mihai can travel to place a trap.The second line of each test case contains n integers a_1, \ldots, a_n (1 \leq a_i \leq 10^9) — the lengths of the hops of the corresponding frogs.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case output a single integer — the maximum number of frogs Slavic and Mihai can catch using a trap.ExampleInput 751 2 3 4 532 2 263 1 3 4 9 1091 3 2 4 2 3 7 8 511087 11 6 8 12 4 4 8109 11 9 12 1 7 2 5 8 10Output 3 3 3 5 0 4 4 NoteIn the first test case, the frogs will hop as follows: Frog 1: 0 \to 1 \to 2 \to 3 \to \mathbf{\color{red}{4}} \to \cdots Frog 2: 0 \to 2 \to \mathbf{\color{red}{4}} \to 6 \to 8 \to \cdots Frog 3: 0 \to 3 \to 6 \to 9 \to 12 \to \cdots Frog 4: 0 \to \mathbf{\color{red}{4}} \to 8 \to 12 \to 16 \to \cdots Frog 5: 0 \to 5 \to 10 \to 15 \to 20 \to \cdots Therefore, if Slavic and Mihai put a trap at coordinate 4, they can catch three frogs: frogs 1, 2, and 4. It can be proven that they can't catch any more frogs.In the second test case, Slavic and Mihai can put a trap at coordinate 2 and catch all three frogs instantly.
751 2 3 4 532 2 263 1 3 4 9 1091 3 2 4 2 3 7 8 511087 11 6 8 12 4 4 8109 11 9 12 1 7 2 5 8 10
3 3 3 5 0 4 4
3 seconds
256 megabytes
['brute force', 'implementation', 'math', 'number theory', '*1300']
E. Cardboard for Picturestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMircea has n pictures. The i-th picture is a square with a side length of s_i centimeters. He mounted each picture on a square piece of cardboard so that each picture has a border of w centimeters of cardboard on all sides. In total, he used c square centimeters of cardboard. Given the picture sizes and the value c, can you find the value of w? A picture of the first test case. Here c = 50 = 5^2 + 4^2 + 3^2, so w=1 is the answer. Please note that the piece of cardboard goes behind each picture, not just the border.InputThe first line contains a single integer t (1 \leq t \leq 1000) — the number of test cases.The first line of each test case contains two positive integers n (1 \leq n \leq 2 \cdot 10^5) and c (1 \leq c \leq 10^{18}) — the number of paintings, and the amount of used square centimeters of cardboard.The second line of each test case contains n space-separated integers s_i (1 \leq s_i \leq 10^4) — the sizes of the paintings.The sum of n over all test cases doesn't exceed 2 \cdot 10^5.Additional constraint on the input: Such an integer w exists for each test case.Please note, that some of the input for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).OutputFor each test case, output a single integer — the value of w (w \geq 1) which was used to use exactly c squared centimeters of cardboard.ExampleInput 10 3 50 3 2 1 1 100 6 5 500 2 2 2 2 2 2 365 3 4 2 469077255466389 10000 2023 10 635472106413848880 9181 4243 7777 1859 2017 4397 14 9390 2245 7225 7 176345687772781240 9202 9407 9229 6257 7743 5738 7966 14 865563946464579627 3654 5483 1657 7571 1639 9815 122 9468 3079 2666 5498 4540 7861 5384 19 977162053008871403 9169 9520 9209 9013 9300 9843 9933 9454 9960 9167 9964 9701 9251 9404 9462 9277 9661 9164 9161 18 886531871815571953 2609 10 5098 9591 949 8485 6385 4586 1064 5412 6564 8460 2245 6552 5089 8353 3803 3764 Output 1 2 4 5 7654321 126040443 79356352 124321725 113385729 110961227 NoteThe first test case is explained in the statement.For the second test case, the chosen w was 2, thus the only cardboard covers an area of c = (2 \cdot 2 + 6)^2 = 10^2 = 100 squared centimeters.For the third test case, the chosen w was 4, which obtains the covered area c = (2 \cdot 4 + 2)^2 \times 5 = 10^2 \times 5 = 100 \times 5 = 500 squared centimeters.
10 3 50 3 2 1 1 100 6 5 500 2 2 2 2 2 2 365 3 4 2 469077255466389 10000 2023 10 635472106413848880 9181 4243 7777 1859 2017 4397 14 9390 2245 7225 7 176345687772781240 9202 9407 9229 6257 7743 5738 7966 14 865563946464579627 3654 5483 1657 7571 1639 9815 122 9468 3079 2666 5498 4540 7861 5384 19 977162053008871403 9169 9520 9209 9013 9300 9843 9933 9454 9960 9167 9964 9701 9251 9404 9462 9277 9661 9164 9161 18 886531871815571953 2609 10 5098 9591 949 8485 6385 4586 1064 5412 6564 8460 2245 6552 5089 8353 3803 3764
1 2 4 5 7654321 126040443 79356352 124321725 113385729 110961227
2 seconds
256 megabytes
['binary search', 'geometry', 'implementation', 'math', '*1100']
D. Balanced Roundtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are the author of a Codeforces round and have prepared n problems you are going to set, problem i having difficulty a_i. You will do the following process: remove some (possibly zero) problems from the list; rearrange the remaining problems in any order you wish. A round is considered balanced if and only if the absolute difference between the difficulty of any two consecutive problems is at most k (less or equal than k).What is the minimum number of problems you have to remove so that an arrangement of problems is balanced?InputThe first line contains a single integer t (1 \leq t \leq 1000) — the number of test cases.The first line of each test case contains two positive integers n (1 \leq n \leq 2 \cdot 10^5) and k (1 \leq k \leq 10^9) — the number of problems, and the maximum allowed absolute difference between consecutive problems.The second line of each test case contains n space-separated integers a_i (1 \leq a_i \leq 10^9) — the difficulty of each problem.Note that the sum of n over all test cases doesn't exceed 2 \cdot 10^5.OutputFor each test case, output a single integer — the minimum number of problems you have to remove so that an arrangement of problems is balanced.ExampleInput 75 11 2 4 5 61 2108 317 3 1 20 12 5 17 124 22 4 6 85 32 3 19 10 83 41 10 58 18 3 1 4 5 10 7 3Output 2 0 5 0 3 1 4 NoteFor the first test case, we can remove the first 2 problems and construct a set using problems with the difficulties [4, 5, 6], with difficulties between adjacent problems equal to |5 - 4| = 1 \leq 1 and |6 - 5| = 1 \leq 1.For the second test case, we can take the single problem and compose a round using the problem with difficulty 10.
75 11 2 4 5 61 2108 317 3 1 20 12 5 17 124 22 4 6 85 32 3 19 10 83 41 10 58 18 3 1 4 5 10 7 3
2 0 5 0 3 1 4
2 seconds
256 megabytes
['brute force', 'greedy', 'implementation', 'sortings', '*900']
C. Word on the Papertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputOn an 8 \times 8 grid of dots, a word consisting of lowercase Latin letters is written vertically in one column, from top to bottom. What is it?InputThe input consists of multiple test cases. The first line of the input contains a single integer t (1 \leq t \leq 1000) — the number of test cases.Each test case consists of 8 lines, each containing 8 characters. Each character in the grid is either \texttt{.} (representing a dot) or a lowercase Latin letter (\texttt{a}–\texttt{z}). The word lies entirely in a single column and is continuous from the beginning to the ending (without gaps). See the sample input for better understanding.OutputFor each test case, output a single line containing the word made up of lowercase Latin letters (\texttt{a}–\texttt{z}) that is written vertically in one column from top to bottom.ExampleInput 5...................................i.....................................l.......o.......s.......t....................................................................t.......h.......e................................................g.......a.......m.......ea.......a.......a.......a.......a.......a.......a.......a.......Output i lost the game aaaaaaaa
5...................................i.....................................l.......o.......s.......t....................................................................t.......h.......e................................................g.......a.......m.......ea.......a.......a.......a.......a.......a.......a.......a.......
i lost the game aaaaaaaa
1 second
256 megabytes
['implementation', 'strings', '*800']
B. Ten Words of Wisdomtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn the game show "Ten Words of Wisdom", there are n participants numbered from 1 to n, each of whom submits one response. The i-th response is a_i words long and has quality b_i. No two responses have the same quality, and at least one response has length at most 10.The winner of the show is the response which has the highest quality out of all responses that are not longer than 10 words. Which response is the winner?InputThe first line contains a single integer t (1 \leq t \leq 100) — the number of test cases.The first line of each test case contains a single integer n (1 \leq n \leq 50) — the number of responses.Then n lines follow, the i-th of which contains two integers a_i and b_i (1 \leq a_i, b_i \leq 50) — the number of words and the quality of the i-th response, respectively. Additional constraints on the input: in each test case, at least one value of i satisfies a_i \leq 10, and all values of b_i are distinct.OutputFor each test case, output a single line containing one integer x (1 \leq x \leq n) — the winner of the show, according to the rules given in the statement.It can be shown that, according to the constraints in the statement, exactly one winner exists for each test case.ExampleInput 357 212 59 39 410 131 23 45 611 43Output 4 3 1 NoteIn the first test case, the responses provided are as follows: Response 1: 7 words, quality 2 Response 2: 12 words, quality 5 Response 3: 9 words, quality 3 Response 4: 9 words, quality 4 Response 5: 10 words, quality 1 We can see that the responses with indices 1, 3, 4, and 5 have lengths not exceeding 10 words. Out of these responses, the winner is the one with the highest quality.Comparing the qualities, we find that: Response 1 has quality 2. Response 3 has quality 3. Response 4 has quality 4. Response 5 has quality 1. Among these responses, Response 4 has the highest quality.
357 212 59 39 410 131 23 45 611 43
4 3 1
1 second
256 megabytes
['implementation', 'sortings', '*800']
A. To My Criticstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputSuneet has three digits a, b, and c. Since math isn't his strongest point, he asks you to determine if you can choose any two digits to make a sum greater or equal to 10.Output "YES" if there is such a pair, and "NO" otherwise.InputThe first line contains a single integer t (1 \leq t \leq 1000) — the number of test cases.The only line of each test case contains three digits a, b, c (0 \leq a, b, c \leq 9).OutputFor each test case, output "YES" if such a pair exists, and "NO" otherwise.You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).ExampleInput 58 1 24 4 59 9 90 0 08 5 3Output YES NO YES NO YES NoteFor the first test case, by choosing the digits 8 and 2 we can obtain a sum of 8 + 2 = 10 which satisfies the condition, thus the output should be "YES".For the second test case, any combination of chosen digits won't be at least 10, thus the output should be "NO" (note that we can not choose the digit on the same position twice).For the third test case, any combination of chosen digits will have a sum equal to 18, thus the output should be "YES".
58 1 24 4 59 9 90 0 08 5 3
YES NO YES NO YES
1 second
256 megabytes
['implementation', 'sortings', '*800']
F. XOR Partitiontime limit per test7 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputFor a set of integers S, let's define its cost as the minimum value of x \oplus y among all pairs of different integers from the set (here, \oplus denotes bitwise XOR). If there are less than two elements in the set, its cost is equal to 2^{30}.You are given a set of integers \{a_1, a_2, \dots, a_n\}. You have to partition it into two sets S_1 and S_2 in such a way that every element of the given set belongs to exactly one of these two sets. The value of the partition is the minimum among the costs of S_1 and S_2.Find the partition with the maximum possible value.InputThe first line contains n (2 \le n \le 200000) — the number of elements in the set.The second line contains n distinct integers a_1, a_2, ..., a_n (0 \le a_i < 2^{30}) — the elements of the set.OutputPrint a string n characters 0 and/or 1 describing the partition as follows: the i-th character should be 0 if the element a_i belongs to S_1, otherwise, that character should be 1.If there are multiple optimal answers, print any of them.ExamplesInput542 13 1337 37 152Output10001Input41 2 3 4Output1100Input21 2Output10Input87 6 5 4 3 2 1 0Output10010110
Input542 13 1337 37 152
Output10001
7 seconds
512 megabytes
['binary search', 'bitmasks', 'data structures', 'divide and conquer', 'greedy', 'trees', '*2700']
E. Max to the Right of Mintime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a permutation p of length n — an array, consisting of integers from 1 to n, all distinct.Let p_{l,r} denote a subarray — an array formed by writing down elements from index l to index r, inclusive.Let \mathit{maxpos}_{l,r} denote the index of the maximum element on p_{l,r}. Similarly, let \mathit{minpos}_{l,r} denote the index of the minimum element on it.Calculate the number of subarrays p_{l,r} such that \mathit{maxpos}_{l,r} > \mathit{minpos}_{l,r}.InputThe first line contains a single integer n (1 \le n \le 10^6) — the number of elements in the permutation.The second line contains n integers p_1, p_2, \dots, p_n (1 \le p_i \le n). All p_i are distinct.OutputPrint a single integer — the number of subarrays p_{l,r} such that \mathit{maxpos}_{l,r} > \mathit{minpos}_{l,r}.ExamplesInput 3 1 2 3 Output 3 Input 6 5 3 6 1 4 2 Output 4 Input 10 5 1 6 2 8 3 4 10 9 7 Output 38
3 1 2 3
3
3 seconds
256 megabytes
['binary search', 'data structures', 'divide and conquer', 'dp', 'dsu', 'two pointers', '*2300']
D. Array Paintingtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array of n integers, where each integer is either 0, 1, or 2. Initially, each element of the array is blue.Your goal is to paint each element of the array red. In order to do so, you can perform operations of two types: pay one coin to choose a blue element and paint it red; choose a red element which is not equal to 0 and a blue element adjacent to it, decrease the chosen red element by 1, and paint the chosen blue element red. What is the minimum number of coins you have to spend to achieve your goal?InputThe first line contains one integer n (1 \le n \le 2 \cdot 10^5).The second line contains n integers a_1, a_2, \dots, a_n (0 \le a_i \le 2).OutputPrint one integer — the minimum number of coins you have to spend in order to paint all elements red.ExamplesInput 3 0 2 0 Output 1 Input 4 0 0 1 1 Output 2 Input 7 0 1 0 0 1 0 2 Output 4 NoteIn the first example, you can paint all elements red with having to spend only one coin as follows: paint the 2-nd element red by spending one coin; decrease the 2-nd element by 1 and paint the 1-st element red; decrease the 2-nd element by 1 and paint the 3-rd element red. In the second example, you can paint all elements red spending only two coins as follows: paint the 4-th element red by spending one coin; decrease the 4-th element by 1 and paint the 3-rd element red; paint the 1-st element red by spending one coin; decrease the 3-rd element by 1 and paint the 2-nd element red.
3 0 2 0
1
2 seconds
256 megabytes
['constructive algorithms', 'greedy', 'two pointers', '*1700']
C. Binary String Copyingtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a string s consisting of n characters 0 and/or 1.You make m copies of this string, let the i-th copy be the string t_i. Then you perform exactly one operation on each of the copies: for the i-th copy, you sort its substring [l_i; r_i] (the substring from the l_i-th character to the r_i-th character, both endpoints inclusive). Note that each operation affects only one copy, and each copy is affected by only one operation.Your task is to calculate the number of different strings among t_1, t_2, \ldots, t_m. Note that the initial string s should be counted only if at least one of the copies stays the same after the operation.InputThe first line contains a single integer t (1 \le t \le 10^4) — the number of test cases.The first line of each test case contains two integers n and m (1 \le n, m \le 2 \cdot 10^5) — the length of s and the number of copies, respectively.The second line contains n characters 0 and/or 1 — the string s.Then m lines follow. The i-th of them contains two integers l_i and r_i (1 \le l_i \le r_i \le n) — the description of the operation applied to the i-th copy.The sum of n over all test cases doesn't exceed 2 \cdot 10^5. The sum of m over all test cases doesn't exceed 2 \cdot 10^5.OutputPrint one integer — the number of different strings among t_1, t_2, \ldots, t_m.ExampleInput 36 51011001 21 32 45 51 66 41001112 21 41 31 21 101 1Output 3 3 1 NoteConsider the first example. Copies below are given in order of the input operations. Underlined substrings are substrings that are sorted: 101100 \rightarrow 011100; 101100 \rightarrow 011100; 101100 \rightarrow 101100; 101100 \rightarrow 101100; 101100 \rightarrow 000111. There are three different strings among t_1, t_2, t_3, t_4, t_5: 000111, 011100 and 101100.Consider the second example: 100111 \rightarrow 100111; 100111 \rightarrow 001111; 100111 \rightarrow 001111; 100111 \rightarrow 010111. There are three different strings among t_1, t_2, t_3, t_4: 001111, 010111 and 100111.
36 51011001 21 32 45 51 66 41001112 21 41 31 21 101 1
3 3 1
2 seconds
256 megabytes
['binary search', 'brute force', 'data structures', 'hashing', 'strings', '*1600']
B. Monsterstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMonocarp is playing yet another computer game. And yet again, his character is killing some monsters. There are n monsters, numbered from 1 to n, and the i-th of them has a_i health points initially.Monocarp's character has an ability that deals k damage to the monster with the highest current health. If there are several of them, the one with the smaller index is chosen. If a monster's health becomes less than or equal to 0 after Monocarp uses his ability, then it dies.Monocarp uses his ability until all monsters die. Your task is to determine the order in which monsters will die.InputThe first line contains a single integer t (1 \le t \le 10^4) — the number of test cases.The first line of each test case contains two integers n and k (1 \le n \le 3 \cdot 10^5; 1 \le k \le 10^9) — the number of monsters and the damage which Monocarp's ability deals.The second line contains n integers a_1, a_2, \dots, a_n (1 \le a_i \le 10^9) — the initial health points of monsters.The sum of n over all test cases doesn't exceed 3 \cdot 10^5.OutputFor each test case, print n integers — the indices of monsters in the order they die. ExampleInput 33 21 2 32 31 14 32 8 3 5Output 2 1 3 1 2 3 1 2 4 NoteIn the first example, the health points change as follows: [1, 2, \underline{3}] \rightarrow [1, \underline{2}, 1] \rightarrow [\underline{1}, 0, 1] \rightarrow [-1, 0, \underline{1}] \rightarrow [-1, 0, -1]. The monster that is going to take damage the next time Monocarp uses his ability is underlined.In the second example, the health points change as follows: [\underline{1}, 1] \rightarrow [-2, \underline{1}] \rightarrow [-2, -2].In the third example, the health points change as follows: [2, \underline{8}, 3, 5] \rightarrow [2, \underline{5}, 3, 5] \rightarrow [2, 2, 3, \underline{5}] \rightarrow [2, 2, \underline{3}, 2] \rightarrow [\underline{2}, 2, 0, 2] \rightarrow [-1, \underline{2}, 0, 2] \rightarrow [-1, -1, 0, \underline{2}] \rightarrow [-1, -1, 0, -1].
33 21 2 32 31 14 32 8 3 5
2 1 3 1 2 3 1 2 4
2 seconds
256 megabytes
['greedy', 'math', 'sortings', '*1000']
A. Morning Sandwichtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMonocarp always starts his morning with a good ol' sandwich. Sandwiches Monocarp makes always consist of bread, cheese and/or ham.A sandwich always follows the formula: a piece of bread a slice of cheese or ham a piece of bread \dots a slice of cheese or ham a piece of bread So it always has bread on top and at the bottom, and it alternates between bread and filling, where filling is a slice of either cheese or ham. Each piece of bread and each slice of cheese or ham is called a layer.Today Monocarp woke up and discovered that he has b pieces of bread, c slices of cheese and h slices of ham. What is the maximum number of layers his morning sandwich can have?InputThe first line contains a single integer t (1 \le t \le 1000) — the number of testcases.Each testcase consists of three integers b, c and h (2 \le b \le 100; 1 \le c, h \le 100) — the number of pieces of bread, slices of cheese and slices of ham, respectively.OutputFor each testcase, print a single integer — the maximum number of layers Monocarp's morning sandwich can have.ExampleInput 32 1 110 1 23 7 8Output 3 7 5 NoteIn the first testcase, Monocarp can arrange a sandwich with three layers: either a piece of bread, a slice of cheese and another piece of bread, or a piece of bread, a slice of ham and another piece of bread.In the second testcase, Monocarp has a lot of bread, but not too much filling. He can arrange a sandwich with four pieces of bread, one slice of cheese and two slices of ham.In the third testcase, it's the opposite — Monocarp has a lot of filling, but not too much bread. He can arrange a sandwich with three pieces of bread and two slices of cheese, for example.
32 1 110 1 23 7 8
3 7 5
2 seconds
256 megabytes
['implementation', 'math', '*800']
F. Vika and Wikitime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputRecently, Vika was studying her favorite internet resource - Wikipedia.On the expanses of Wikipedia, she read about an interesting mathematical operation bitwise XOR, denoted by \oplus. Vika began to study the properties of this mysterious operation. To do this, she took an array a consisting of n non-negative integers and applied the following operation to all its elements at the same time: a_i = a_i \oplus a_{(i+1) \bmod n}. Here x \bmod y denotes the remainder of dividing x by y. The elements of the array are numbered starting from 0.Since it is not enough to perform the above actions once for a complete study, Vika repeats them until the array a becomes all zeros.Determine how many of the above actions it will take to make all elements of the array a zero. If this moment never comes, output -1.InputThe first line contains a single integer n (1 \le n \le 2^{20}) - the length of the array a.It is guaranteed that n can be represented as 2^k for some integer k (0 \le k \le 20).The second line contains n integers a_0, a_1, a_2, \dots, a_{n-1} (0 \le a_i \le 10^9) - the elements of the array a.OutputOutput a single number - the minimum number of actions required to make all elements of the array a zero, or -1 if the array a will never become zero.ExamplesInput 4 1 2 1 2 Output 2 Input 2 0 0 Output 0 Input 1 14 Output 1 Input 8 0 1 2 3 4 5 6 7 Output 5 NoteIn the first example, after one operation, the array a will become equal to [3, 3, 3, 3]. After one more operation, it will become equal to [0, 0, 0, 0].In the second example, the array a initially consists only of zeros.In the third example, after one operation, the array a will become equal to [0].
4 1 2 1 2
2
2 seconds
256 megabytes
['binary search', 'bitmasks', 'combinatorics', 'divide and conquer', 'dp', 'math', '*2400']
E. Vika and Stone Skippingtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn Vika's hometown, Vladivostok, there is a beautiful sea.Often you can see kids skimming stones. This is the process of throwing a stone into the sea at a small angle, causing it to fly far and bounce several times off the water surface.Vika has skimmed stones many times and knows that if you throw a stone from the shore perpendicular to the coastline with a force of f, it will first touch the water at a distance of f from the shore, then bounce off and touch the water again at a distance of f - 1 from the previous point of contact. The stone will continue to fly in a straight line, reducing the distances between the points where it touches the water, until it falls into the sea.Formally, the points at which the stone touches the water surface will have the following coordinates: f, f + (f - 1), f + (f - 1) + (f - 2), ... , f + (f - 1) + (f - 2) + \ldots + 1 (assuming that 0 is the coordinate of the shoreline).Once, while walking along the embankment of Vladivostok in the evening, Vika saw a group of guys skipping stones across the sea, launching them from the same point with different forces.She became interested in what is the maximum number of guys who can launch a stone with their force f_i, so that all f_i are different positive integers, and all n stones touched the water at the point with the coordinate x (assuming that 0 is the coordinate of the shoreline).After thinking a little, Vika answered her question. After that, she began to analyze how the answer to her question would change if she multiplied the coordinate x by some positive integers x_1, x_2, ... , x_q, which she picked for analysis.Vika finds it difficult to cope with such analysis on her own, so she turned to you for help.Formally, Vika is interested in the answer to her question for the coordinates X_1 = x \cdot x_1, X_2 = X_1 \cdot x_2, ... , X_q = X_{q-1} \cdot x_q. Since the answer for such coordinates can be quite large, find it modulo M. It is guaranteed that M is prime.InputThe first line of the input contains three integers x (1 \le x \le 10^9), q (1 \le q \le 10^5) and M (100 \le M \le 2 \cdot 10^9) — the initial coordinate for which Vika answered the question on her own, the number of integers x_i by which Vika will multiply the initial coordinate and prime module M.The second line of the input contains q integers x_1, x_2, x_3, \ldots, x_q (1 \le x_i \le 10^6) — the integers described in the statement.OutputOutput q integers, where the i-th number corresponds to the answer to Vika's question for the coordinate X_i. Output all the answers modulo M.ExamplesInput 1 2 179 2 3 Output 1 2 Input 7 5 998244353 2 13 1 44 179 Output 2 4 4 8 16 Input 1000000000 10 179 58989 49494 8799 9794 97414 141241 552545 145555 548959 774175 Output 120 4 16 64 111 43 150 85 161 95 NoteIn the first sample, to make the stone touch the water at a point with coordinate 2, it needs to be thrown with a force of 2. To make the stone touch the water at a point with coordinate 2 \cdot 3 = 6, it needs to be thrown with a force of 3 or 6.In the second sample, you can skim a stone with a force of 5 or 14 to make it touch the water at a point with coordinate 7 \cdot 2 = 14. For the coordinate 14 \cdot 13 = 182, there are 4 possible forces: 20, 29, 47, 182.
1 2 179 2 3
1 2
3 seconds
256 megabytes
['brute force', 'implementation', 'math', 'number theory', '*2600']
D. Vika and Bonusestime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputA new bonus system has been introduced at Vika's favorite cosmetics store, "Golden Pear"!The system works as follows: suppose a customer has b bonuses. Before paying for the purchase, the customer can choose one of two options: Get a discount equal to the current number of bonuses, while the bonuses are not deducted. Accumulate an additional x bonuses, where x is the last digit of the number b. As a result, the customer's account will have b+x bonuses. For example, if a customer had 24 bonuses, he can either get a discount of 24 or accumulate an additional 4 bonuses, after which his account will have 28 bonuses.At the moment, Vika has already accumulated s bonuses.The girl knows that during the remaining time of the bonus system, she will make k more purchases at the "Golden Pear" store network.After familiarizing herself with the rules of the bonus system, Vika became interested in the maximum total discount she can get.Help the girl answer this question.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \le t \le 10^5) — the number of test cases. The description of the test cases follows.The test case consists of a single line containing two integers s and k (0 \le s \le 10^9, 1 \le k \le 10^9) — the current number of bonuses in Vika's account and how many more purchases the girl will make.OutputFor each test case, output a single integer — the maximum total discount that can be obtained through the bonus system.ExampleInput 61 311 30 1795 1000000000723252212 856168102728598293 145725253Output 4 33 0 9999999990 1252047198518668448 106175170582793129 NoteIn the first test case, Vika can accumulate bonuses after the first and second purchases, after which she can get a discount of 4.In the second test case, Vika can get a discount of 11 three times, and the total discount will be 33.In the third example, regardless of Vika's actions, she will always get a total discount of 0.
61 311 30 1795 1000000000723252212 856168102728598293 145725253
4 33 0 9999999990 1252047198518668448 106175170582793129
3 seconds
256 megabytes
['binary search', 'brute force', 'math', 'ternary search', '*2200']
C. Vika and Price Tagstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputVika came to her favorite cosmetics store "Golden Pear". She noticed that the prices of n items have changed since her last visit.She decided to analyze how much the prices have changed and calculated the difference between the old and new prices for each of the n items.Vika enjoyed calculating the price differences and decided to continue this process.Let the old prices be represented as an array of non-negative integers a, and the new prices as an array of non-negative integers b. Both arrays have the same length n.In one operation, Vika constructs a new array c according to the following principle: c_i = |a_i - b_i|. Then, array c renamed into array b, and array b renamed into array a at the same time, after which Vika repeats the operation with them.For example, if a = [1, 2, 3, 4, 5, 6, 7]; b = [7, 6, 5, 4, 3, 2, 1], then c = [6, 4, 2, 0, 2, 4, 6]. Then, a = [7, 6, 5, 4, 3, 2, 1]; b = [6, 4, 2, 0, 2, 4, 6].Vika decided to call a pair of arrays a, b dull if after some number of such operations all elements of array a become zeros.Output "YES" if the original pair of arrays is dull, and "NO" otherwise.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \le t \le 10^4) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer n (1 \le n \le 10^5) — the number of items whose prices have changed.The second line contains n integers a_1, a_2, \ldots, a_n (0 \le a_i \le 10^9) — the old prices of the items.The third line contains n integers b_1, b_2, \ldots, b_n (0 \le b_i \le 10^9) — the new prices of the items.It is guaranteed that the sum of n over all test cases does not exceed 10^5.OutputFor each test case, output "YES" if the pair of price arrays is dull, and "NO" otherwise.You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.ExampleInput 940 0 0 01 2 3 431 2 31 2 321 22 16100 23 53 11 56 321245 31 12 6 6 671 2 3 4 5 6 77 6 5 4 3 2 134 0 24 0 232 5 21 3 426 14 220 00 3Output YES YES NO NO YES YES NO YES YES NoteIn the first test case, the array a is initially zero.In the second test case, after the first operation a = [1, 2, 3], b = [0, 0, 0]. After the second operation a = [0, 0, 0], b = [1, 2, 3].In the third test case, it can be shown that the array a will never become zero.
940 0 0 01 2 3 431 2 31 2 321 22 16100 23 53 11 56 321245 31 12 6 6 671 2 3 4 5 6 77 6 5 4 3 2 134 0 24 0 232 5 21 3 426 14 220 00 3
YES YES NO NO YES YES NO YES YES
1 second
256 megabytes
['math', 'number theory', '*1800']
B. Vika and the Bridgetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn the summer, Vika likes to visit her country house. There is everything for relaxation: comfortable swings, bicycles, and a river.There is a wooden bridge over the river, consisting of n planks. It is quite old and unattractive, so Vika decided to paint it. And in the shed, they just found cans of paint of k colors.After painting each plank in one of k colors, Vika was about to go swinging to take a break from work. However, she realized that the house was on the other side of the river, and the paint had not yet completely dried, so she could not walk on the bridge yet.In order not to spoil the appearance of the bridge, Vika decided that she would still walk on it, but only stepping on planks of the same color. Otherwise, a small layer of paint on her sole will spoil the plank of another color. Vika also has a little paint left, but it will only be enough to repaint one plank of the bridge.Now Vika is standing on the ground in front of the first plank. To walk across the bridge, she will choose some planks of the same color (after repainting), which have numbers 1 \le i_1 < i_2 < \ldots < i_m \le n (planks are numbered from 1 from left to right). Then Vika will have to cross i_1 - 1, i_2 - i_1 - 1, i_3 - i_2 - 1, \ldots, i_m - i_{m-1} - 1, n - i_m planks as a result of each of m + 1 steps.Since Vika is afraid of falling, she does not want to take too long steps. Help her and tell her the minimum possible maximum number of planks she will have to cross in one step, if she can repaint one (or zero) plank a different color while crossing the bridge.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \le t \le 10^4) — the number of test cases. The description of the test cases follows.The first line of each test case contains two integers n and k (1 \le k \le n \le 2 \cdot 10^5) — the number of planks in the bridge and the number of different colors of paint.The second line of each test case contains n integers c_1, c_2, c_3, \dots, c_n (1 \le c_i \le k) — the colors in which Vika painted the planks of the bridge.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output a single integer — the minimum possible maximum number of planks that Vika will have to step over in one step.ExampleInput 55 21 1 2 1 17 31 2 3 3 3 2 16 61 2 3 4 5 68 41 2 3 4 2 3 1 43 11 1 1Output 0 1 2 2 0 NoteIn the first test case, Vika can repaint the plank in the middle in color 1 and walk across the bridge without stepping over any planks.In the second test case, Vika can repaint the plank in the middle in color 2 and walk across the bridge, stepping over only one plank each time.In the third test case, Vika can repaint the penultimate plank in color 2 and walk across the bridge, stepping only on planks with numbers 2 and 5. Then Vika will have to step over 1, 2 and 1 plank each time she steps, so the answer is 2.In the fourth test case, Vika can simply walk across the bridge without repainting it, stepping over two planks each time, walking on planks of color 3.In the fifth test case, Vika can simply walk across the bridge without repainting it, without stepping over any planks.
55 21 1 2 1 17 31 2 3 3 3 2 16 61 2 3 4 5 68 41 2 3 4 2 3 1 43 11 1 1
0 1 2 2 0
1 second
256 megabytes
['binary search', 'data structures', 'greedy', 'implementation', 'math', 'sortings', '*1200']
A. Vika and Her Friendstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputVika and her friends went shopping in a mall, which can be represented as a rectangular grid of rooms with sides of length n and m. Each room has coordinates (a, b), where 1 \le a \le n, 1 \le b \le m. Thus we call a hall with coordinates (c, d) a neighbouring for it if |a - c| + |b - d| = 1.Tired of empty fashion talks, Vika decided to sneak away unnoticed. But since she hasn't had a chance to visit one of the shops yet, she doesn't want to leave the mall. After a while, her friends noticed Vika's disappearance and started looking for her.Currently, Vika is in a room with coordinates (x, y), and her k friends are in rooms with coordinates (x_1, y_1), (x_2, y_2), ... , (x_k, y_k), respectively. The coordinates can coincide. Note that all the girls must move to the neighbouring rooms.Every minute, first Vika moves to one of the adjacent to the side rooms of her choice, and then each friend (seeing Vika's choice) also chooses one of the adjacent rooms to move to.If at the end of the minute (that is, after all the girls have moved on to the neighbouring rooms) at least one friend is in the same room as Vika, she is caught and all the other friends are called.Tell us, can Vika run away from her annoying friends forever, or will she have to continue listening to empty fashion talks after some time?InputEach test consists of multiple test cases. The first line contains a single integer t (1 \le t \le 100) — the number of test cases. The description of the test cases follows.The first line of each test case contains three integers n, m, k (1 \le n, m, k \le 100) — the sizes of the mall and the number of Vika's friends.The second line of each test case contains a pair of integers x and y (1 \le x \le n, 1 \le y \le m) — the coordinates of the room where Vika is.Each of the next k lines of each test case contains a pair of integers x_i and y_i (1 \le x_i \le n, 1 \le y_i \le m) — the coordinates of the room where the i-th friend is.OutputFor each test case, output "YES" if Vika can run away from her friends forever, otherwise output "NO".You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.ExampleInput 62 2 11 11 22 2 21 12 22 21 2 11 11 25 5 43 31 11 55 15 52 2 21 12 11 23 4 11 23 3Output YES NO YES NO YES YES NoteIn the first test case, the friend will never catch up with Vika, because Vika can always move to the room diagonally opposite to the one where the friend is.In the second test case, no matter where Vika goes, each of her friends can catch her after the first move.In the third test case, Vika and her friend will always be in different halls.
62 2 11 11 22 2 21 12 22 21 2 11 11 25 5 43 31 11 55 15 52 2 21 12 11 23 4 11 23 3
YES NO YES NO YES YES
1 second
256 megabytes
['games', 'math', '*900']
F. The Boss's Identitytime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputWhile tracking Diavolo's origins, Giorno receives a secret code from Polnareff. The code can be represented as an infinite sequence of positive integers: a_1, a_2, \dots . Giorno immediately sees the pattern behind the code. The first n numbers a_1, a_2, \dots, a_n are given. For i > n the value of a_i is (a_{i-n}\ |\ a_{i-n+1}), where | denotes the bitwise OR operator.Pieces of information about Diavolo are hidden in q questions. Each question has a positive integer v associated with it and its answer is the smallest index i such that a_i > v. If no such i exists, the answer is -1. Help Giorno in answering the questions!InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10\,000). The description of the test cases follows.The first line of each test case contains two integers n and q (2 \leq n \leq 2 \cdot 10^5 , 1 \leq q \leq 2 \cdot 10^5).The second line of each test case contains n integers a_1,a_2,\ldots,a_n (0 \leq a_i \leq 10^9) — the parts of the code which define the pattern.The i-th line of the next q lines contain a single integer v_i (0 \leq v_i \leq 10^9) — the question Giorno asks you.The sum of n and q over all test cases does not exceed 2 \cdot 10^5. OutputPrint q numbers. The i-th number is the answer to the i-th question asked by Giorno.ExampleInput 32 32 11234 50 2 1 3012345 51 2 3 4 572604Output 1 3 -1 2 2 4 -1 -1 -1 3 8 1 5 NoteIn the first test case, a = [2,1,3,3,\ldots]. For the first question, a_1=2 is the element with the smallest index greater than 1. For the second question, a_3=3 is the element with the smallest index greater than 2. For the third question, there is no index i such that a_i > 3.
32 32 11234 50 2 1 3012345 51 2 3 4 572604
1 3 -1 2 2 4 -1 -1 -1 3 8 1 5
3 seconds
512 megabytes
['binary search', 'bitmasks', 'data structures', 'dfs and similar', 'greedy', 'math', 'sortings', '*2500']
E. Triangle Platinum?time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is an interactive problem.Made in Heaven is a rather curious Stand. Of course, it is (arguably) the strongest Stand in existence, but it is also an ardent puzzle enjoyer. For example, it gave Qtaro the following problem recently:Made in Heaven has n hidden integers a_1, a_2, \dots, a_n (3 \le n \le 5000, 1 \le a_i \le 4). Qtaro must determine all the a_i by asking Made in Heaven some queries of the following form: In one query Qtaro is allowed to give Made in Heaven three distinct indexes i, j and k (1 \leq i, j, k \leq n). If a_i, a_j, a_k form the sides of a non-degenerate triangle^\dagger, Made in Heaven will respond with the area of this triangle. Otherwise, Made in Heaven will respond with 0. By asking at most 5500 such questions, Qtaro must either tell Made in Heaven all the values of the a_i, or report that it is not possible to uniquely determine them.Unfortunately due to the universe reboot, Qtaro is not as smart as Jotaro. Please help Qtaro solve Made In Heaven's problem. —————————————————————— ^\dagger Three positive integers a, b, c are said to form the sides of a non-degenerate triangle if and only if all of the following three inequalities hold: a+b > c, b+c > a, c+a > b. InteractionThe interaction begins with reading n (3 \le n \le 5000), the number of hidden integers. To ask a question corresponding to the triple (i, j, k) (1 \leq i < j < k \leq n), output "? i j k" without quotes. Afterward, you should read a single integer s. If s = 0, then a_i, a_j, and a_k are not the sides of a non-degenerate triangle. Otherwise, s = 16 \Delta^2, where \Delta is the area of the triangle. The area is provided in this format for your convenience so that you need only take integer input. If the numbers a_i cannot be uniquely determined print "! -1" without quotes. On the other hand, if you have determined all the values of a_i print "! a_1 a_2 \dots a_n" on a single line.The interactor is non-adaptive. The hidden array a_1, a_2, \dots, a_n is fixed beforehand and is not changed during the interaction process.After printing a query do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see the documentation for other languages. HacksYou can hack a solution with the following input format.The first line contains a single integer n (3 \le n \le 5000) — the number of hidden integers.The second line contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le 4) — the hidden array.ExamplesInput 3 63 Output ? 1 2 3 ! -1Input 6 0 0 0 63 15 135 Output ? 1 2 3 ? 2 3 4 ? 4 5 6 ? 1 5 6 ? 3 5 6 ? 1 2 4 ! 3 2 1 4 2 2Input 15 3 3 3 3 3 0 Output ? 1 2 3 ? 4 6 7 ? 8 9 10 ? 11 12 13 ? 13 14 15 ? 4 5 6 ! -1 Input 15 3 15 0 3 3 3 Output ? 1 2 3 ? 3 4 5 ? 4 5 6 ? 7 8 9 ? 10 11 12 ? 13 14 15 ! 1 1 1 2 2 4 1 1 1 1 1 1 1 1 1 1 Input 10 3 48 3 48 63 0 Output ? 1 3 5 ? 4 6 8 ? 1 5 9 ? 6 8 10 ? 4 2 6 ? 7 10 8 ! 1 3 1 2 1 2 4 2 1 2 NoteIn the first example, the interaction process happens as follows: StdinStdoutExplanation3Read n = 3. There are 3 hidden integers? 1 2 3Ask for the area formed by a_1, a_2 and a_363Received 16\Delta^2 = 63. So the area \Delta = \sqrt{\frac{63}{16}} \approx 1.984313! -1Answer that there is no unique array satisfying the queries. From the area received, we can deduce that the numbers that forms the triangle are either (4, 4, 1) or (3, 2, 2) (in some order). As there are multiple arrays of numbers that satisfy the queries, a unique answer cannot be found.In the second example, the interaction process happens as follows: StepStdinStdoutExplanation16Read n = 6. There are 6 hidden integers2? 1 2 3Ask for the area formed by a_1, a_2 and a_330Does not form a non-degenerate triangle4? 2 3 4Ask for the area formed by a_2, a_3 and a_450Does not form a non-degenerate triangle6? 4 5 6Ask for the area formed by a_4, a_5 and a_670Does not form a non-degenerate triangle8? 1 5 6Ask for the area formed by a_1, a_5 and a_6963Received 16\Delta^2 = 63. So the area \Delta = \sqrt{\frac{63}{16}} \approx 1.98431310? 3 5 6Ask for the area formed by a_3, a_5 and a_61115Received 16\Delta^2 = 15. So the area \Delta = \sqrt{\frac{15}{16}} \approx 0.96824512? 1 2 4Ask for the area formed by a_3, a_5 and a_613135Received 16\Delta^2 = 135. So the area \Delta = \sqrt{\frac{135}{16}} \approx 2.90473814! 3 2 1 4 2 2A unique answer is found, which is a = [3, 2, 1, 4, 2, 2]. From steps 10 and 11, we can deduce that the the multiset \left\{a_3, a_5, a_6\right\} must be \left\{2, 2, 1\right\}.From steps 8 and 9, the multiset \left\{a_1, a_5, a_6\right\} must be either \left\{4, 4, 1\right\} or \left\{3, 2, 2\right\}.As \left\{a_3, a_5, a_6\right\} and \left\{a_1, a_5, a_6\right\} share a_5 and a_6, we conclude that a_5 = a_6 = 2, as well as a_1 = 3, a_3 = 1.From steps 6 and 7, we know that a_5 = a_6 = 2, and a_4, a_5 and a_6 cannot form a non-degenerate triangle, hence a_4 = 4.With all the known information, only a_2 = 2 satisfies the queries made in steps 2, 3, 4, 5, 12 and 13.In the third example, one array that satisfies the queries is [1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1].
3 63
? 1 2 3 ! -1
2 seconds
256 megabytes
['brute force', 'combinatorics', 'implementation', 'interactive', 'math', 'probabilities', '*2900']
D. Professor Higashikatatime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputJosuke is tired of his peaceful life in Morioh. Following in his nephew Jotaro's footsteps, he decides to study hard and become a professor of computer science. While looking up competitive programming problems online, he comes across the following one: Let s be a binary string of length n. An operation on s is defined as choosing two distinct integers i and j (1 \leq i < j \leq n), and swapping the characters s_i, s_j.Consider the m strings t_1, t_2, \ldots, t_m, where t_i is the substring ^\dagger of s from l_i to r_i. Define t(s) = t_1+t_2+\ldots+t_m as the concatenation of the strings t_i in that order.There are q updates to the string. In the i-th update s_{x_i} gets flipped. That is if s_{x_i}=1, then s_{x_i} becomes 0 and vice versa. After each update, find the minimum number of operations one must perform on s to make t(s) lexicographically as large^\ddagger as possible. Note that no operation is actually performed. We are only interested in the number of operations.Help Josuke in his dream by solving the problem for him. —————————————————————— \dagger A string a is a substring of a string b if a can be obtained from b by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.\ddagger A string a is lexicographically larger than a string b of the same length if and only if the following holds: in the first position where a and b differ, the string a has a 1, and the string b has a 0. InputThe first line contains three integers n, m, q (1 \leq n,m,q \leq 2 \cdot 10^5). The next line contains a binary string s of length n, consisting only of digits 0 and 1.The i-th line of the next m lines contains two integers l_i and r_i (1 \leq l_i \leq r_i \leq n).The i-th line of the next q lines contains a single integer x_i (1 \leq x_i \leq n).OutputPrint q integers. The i-th integer is the minimum number of operations that need to be performed on s to get the lexicographically largest possible string t(s) in the i-th round.ExamplesInput 2 2 4 01 1 2 1 2 1 1 2 2 Output 0 1 0 1 Input 8 6 10 10011010 5 6 2 3 6 8 5 7 5 8 6 8 3 5 6 2 5 2 5 8 4 1 Output 2 3 2 2 1 2 2 2 2 2 NoteIn the first test case,Originally, t(s) = s(1,2) + s(1,2) = 0101.After the 1-st query, s becomes 11 and consequently t becomes 1111. You don't need to perform any operation as t(s) is already the lexicographically largest string possible.After the 2-nd query, s becomes 01 and consequently t becomes 0101. You need to perform 1 operation by swapping s_1 and s_2. Consequently, t(s) becomes 1010 which is the lexicographically largest string you can achieve.
2 2 4 01 1 2 1 2 1 1 2 2
0 1 0 1
2 seconds
256 megabytes
['data structures', 'dsu', 'greedy', 'implementation', 'strings', '*1900']
C. Vampiric Powers, anyone?time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputDIO knows that the Stardust Crusaders have determined his location and will be coming to fight him. To foil their plans he decides to send out some Stand users to fight them. Initially, he summoned n Stand users with him, the i-th one having a strength of a_i. Using his vampiric powers, he can do the following as many times as he wishes: Let the current number of Stand users be m. DIO chooses an index i (1 \le i \le m). Then he summons a new Stand user, with index m+1 and strength given by: a_{m+1} = a_i \oplus a_{i+1} \oplus \ldots \oplus a_m,where the operator \oplus denotes the bitwise XOR operation. Now, the number of Stand users becomes m+1. Unfortunately for DIO, by using Hermit Purple's divination powers, the Crusaders know that he is plotting this, and they also know the strengths of the original Stand users. Help the Crusaders find the maximum possible strength of a Stand user among all possible ways that DIO can summon.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10\,000). The description of the test cases follows.The first line of each test case contains a single integer n (1 \le n \le 10^5)  – the number of Stand users initially summoned.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (0 \le a_i < 2^8)  – the strength of each Stand user.It is guaranteed that the sum of n over all test cases does not exceed 10^5.OutputFor each test case, output a single integer, maximum strength of a Stand user among all possible ways that DIO can summon.ExampleInput 340 2 5 131 2 358 2 4 12 1Output 7 3 14 NoteIn the first test case, one of the ways to add new Stand users is as follows: Choose i=n. Now, a becomes [0,2,5,1,1]. Choose i=1. Now, a becomes [0,2,5,1,1,7]. 7 is the maximum strength of a Stand user DIO can summon.In the second test case, DIO does not need to add more Stand users because 3 is the maximum strength of a Stand user DIO can summon.
340 2 5 131 2 358 2 4 12 1
7 3 14
1 second
256 megabytes
['bitmasks', 'brute force', 'dp', 'greedy', '*1400']
B. Hamon Odysseytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputJonathan is fighting against DIO's Vampire minions. There are n of them with strengths a_1, a_2, \dots, a_n. \def\and {{\,\texttt{&}\,}}Denote (l, r) as the group consisting of the vampires with indices from l to r. Jonathan realizes that the strength of any such group is in its weakest link, that is, the bitwise AND. More formally, the strength level of the group (l, r) is defined as f(l,r) = a_l \and a_{l+1} \and a_{l+2} \and \ldots \and a_r. Here, \and denotes the bitwise AND operation. Because Jonathan would like to defeat the vampire minions fast, he will divide the vampires into contiguous groups, such that each vampire is in exactly one group, and the sum of strengths of the groups is minimized. Among all ways to divide the vampires, he would like to find the way with the maximum number of groups.Given the strengths of each of the n vampires, find the maximum number of groups among all possible ways to divide the vampires with the smallest sum of strengths.InputThe first line contains a single integer t (1 \leq t \leq 10^4) — the number of test cases. The description of test cases follows.The first line of each test case contains a single integer n (1 \leq n \leq 2 \cdot 10^5) — the number of vampires.The second line of each test case contains n integers a_1,a_2,\ldots,a_n (0 \leq a_i \leq 10^9) — the individual strength of each vampire.The sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output a single integer — the maximum number of groups among all possible ways to divide the vampires with the smallest sum of strengths.ExampleInput 331 2 352 3 1 5 245 7 12 6Output 1 2 1 NoteIn the first test case, the optimal way is to take all the n vampires as a group. So, f(1,3) = 1 \and 2 \and 3 = 0.In the second test case, the optimal way is to make 2 groups, (2,3,1) and (5,2). So, f(1,3) + f(4,5) = (2 \and 3 \and 1) + (5 \and 2) = 0 + 0 = 0.
331 2 352 3 1 5 245 7 12 6
1 2 1
1 second
256 megabytes
['bitmasks', 'greedy', 'two pointers', '*1000']
A. The Man who became a God time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputKars is tired and resentful of the narrow mindset of his village since they are content with staying where they are and are not trying to become the perfect life form. Being a top-notch inventor, Kars wishes to enhance his body and become the perfect life form. Unfortunately, n of the villagers have become suspicious of his ideas. The i-th villager has a suspicion of a_i on him. Individually each villager is scared of Kars, so they form into groups to be more powerful.The power of the group of villagers from l to r be defined as f(l,r) where f(l,r) = |a_l - a_{l+1}| + |a_{l + 1} - a_{l + 2}| + \ldots + |a_{r-1} - a_r|.Here |x-y| is the absolute value of x-y. A group with only one villager has a power of 0.Kars wants to break the villagers into exactly k contiguous subgroups so that the sum of their power is minimized. Formally, he must find k - 1 positive integers 1 \le r_1 < r_2 < \ldots < r_{k - 1} < n such that f(1, r_1) + f(r_1 + 1, r_2) + \ldots + f(r_{k-1} + 1, n) is minimised. Help Kars in finding the minimum value of f(1, r_1) + f(r_1 + 1, r_2) + \ldots + f(r_{k-1} + 1, n).InputThe first line contains a single integer t (1 \leq t \leq 100) — the number of test cases. The description of test cases follows.The first line of each test case contains two integers n,k (1 \leq k \leq n \leq 100) — the number of villagers and the number of groups they must be split into.The second line of each test case contains n integers a_1,a_2, \ldots, a_n (1 \leq a_i \leq 500) — the suspicion of each of the villagers.OutputFor each test case, output a single integer — the minimum possible value of sum of power of all the groups i. e. the minimum possible value of f(1,r_1) + f(r_1 + 1, r_2) + \ldots + f(r_{k-1} + 1, n).ExampleInput 34 21 3 5 26 31 9 12 4 7 212 81 9 8 2 3 3 1 8 7 7 9 2Output 4 11 2 NoteIn the first test case, we will group the villagers with suspicion (1,3,5,2) into (1,3,5) and (2). So, f(1,3) + f(4,4) = (|1 - 3| + |3 - 5|) + 0 = 4 + 0 = 4.In the second test case, we will group the villagers with suspicion (1,9,12,4,7,2) into (1),(9,12),(4,7,2). So, f(1,1) + f(2,3) + f(4,6) = 0 + 3 + 8 = 11.
34 21 3 5 26 31 9 12 4 7 212 81 9 8 2 3 3 1 8 7 7 9 2
4 11 2
1 second
256 megabytes
['greedy', 'sortings', '*800']
G. Rudolf and CodeVid-23time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputA new virus called "CodeVid-23" has spread among programmers. Rudolf, being a programmer, was not able to avoid it.There are n symptoms numbered from 1 to n that can appear when infected. Initially, Rudolf has some of them. He went to the pharmacy and bought m medicines.For each medicine, the number of days it needs to be taken is known, and the set of symptoms it removes. Unfortunately, medicines often have side effects. Therefore, for each medicine, the set of symptoms that appear when taking it is also known.After reading the instructions, Rudolf realized that taking more than one medicine at a time is very unhealthy.Rudolph wants to be healed as soon as possible. Therefore, he asks you to calculate the minimum number of days to remove all symptoms, or to say that it is impossible.InputThe first line contains a single integer t (1 \le t \le 100) — the number of test cases.Then follow the descriptions of the test cases.The first line of each test case contains two integers n, m (1 \le n \le 10, 1 \le m \le 10^3) — the number of symptoms and medicines, respectively.The second line of each test case contains a string of length n consisting of the characters 0 and 1 — the description of Rudolf's symptoms. If the i-th character of the string is 1, Rudolf has the i-th symptom, otherwise he does not.Then follow 3 \cdot m lines — the description of the medicines.The first line of each medicine description contains an integer d (1 \le d \le 10^3) — the number of days the medicine needs to be taken.The next two lines of the medicine description contain two strings of length n, consisting of the characters 0 and 1 — the description of the symptoms it removes and the description of the side effects.In the first of the two lines, 1 at position i means that the medicine removes the i-th symptom, and 0 otherwise.In the second of the two lines, 1 at position i means that the i-th symptom appears after taking the medicine, and 0 otherwise.Different medicines can have the same sets of symptoms and side effects. If a medicine relieves a certain symptom, it will not be among the side effects.The sum of m over all test cases does not exceed 10^3.OutputFor each test case, output a single integer on a separate line — the minimum number of days it will take Rudolf to remove all symptoms. If this never happens, output -1.ExampleInput 45 410011310000001103001010000030101000100511010001004 1000010101101002 21121001301102 311301103100041001Output 8 0 -1 6 NoteIn the first example, we can first apply medicine number 4, after which the symptoms will look like "00101". After that, medicine number 2, then all symptoms will disappear, and the number of days will be 5 + 3 = 8. Another option is to apply the medicines in the order 1, 3, 2. In this case, all symptoms will also disappear, but the number of days will be 3 + 3 + 3 = 9.In the second example, there are no symptoms initially, so the treatment will take 0 days.In the third example, there are no options to remove all symptoms.
45 410011310000001103001010000030101000100511010001004 1000010101101002 21121001301102 311301103100041001
8 0 -1 6
1 second
256 megabytes
['bitmasks', 'dp', 'graphs', 'greedy', 'shortest paths', '*1900']
F. Rudolph and Mimictime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is an interactive task.Rudolph is a scientist who studies alien life forms. There is a room in front of Rudolph with n different objects scattered around. Among the objects there is exactly one amazing creature — a mimic that can turn into any object. He has already disguised himself in this room and Rudolph needs to find him by experiment.The experiment takes place in several stages. At each stage, the following happens: Rudolf looks at all the objects in the room and writes down their types. The type of each object is indicated by a number; there can be several objects of the same type. After inspecting, Rudolph can point to an object that he thinks is a mimic. After that, the experiment ends. Rudolph only has one try, so if he is unsure of the mimic's position, he does the next step instead. Rudolf can remove any number of objects from the room (possibly zero). Then Rudolf leaves the room and at this time all objects, including the mimic, are mixed with each other, their order is changed, and the mimic can transform into any other object (even one that is not in the room). After this, Rudolf returns to the room and repeats the stage. The mimic may not change appearance, but it can not remain a same object for more than two stages in a row.Rudolf's task is to detect mimic in no more than five stages.InputThe first line contains one integer t (1 \le t \le 1000) — the number of test cases.The first line of each test case contains one integer n (2 \le n \le 200) — the number of objects in the room.The second line of each test case contains n integers a_1,a_2,...,a_n (1 \le a_i \le 9) — object types.InteractionAfter you have read the description of the input data set, you must make no more than 5 queries. Reading the input data is considered the beginning of the first stage, and the mimic may already begin to change.The request is a line. The first character of the line indicates the request type. To remove objects, print "-". After that print the number k — how many objects you want to remove. Then there are k numbers — indexes of objects in their current location. Indexing starts from one. You can remove the mimic, but in this case you will not be able to point to it and will get "Wrong answer" verdict.In response to the request you will receive a line containing integers — the objects remaining in the room after removal and mixing.To indicate the position of a mimic, print "!", then print the index of the object that is the mimic.The task will be considered solved if the position of the mimic is specified correctly.If you make more than five requests, or make an invalid request, the solution will get "Wrong answer" verdict.After outputting a query or the answer do not forget to output the end of line and flush the output. Otherwise, you will get "Idleness limit exceeded". To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. HacksYou can hack a solution with the following input format.The first line contains one integer t (1 \le t \le 1000) — the number of test cases.The first line of each test case contains two integers n, k (2 \le n \le 200, 1 \le k \le n) — the number of objects and the position of the mimic.The second line contains of each test case n integers a_1, a_2,...,a_n (1 \le a_i \le 9) – initial array of objects.ExampleInput 3 5 1 1 2 2 3 2 1 1 2 2 2 2 2 8 1 2 3 4 3 4 2 1 4 3 4 3 2 2 1 3 2 3 3 2 5 3 2 2 5 15 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 7 9 5 4 3 2 1 Output - 1 5 - 1 3 - 2 1 2 ! 1 - 0 - 4 1 3 7 8 - 1 4 - 1 2 ! 2 - 0 ! 10NoteExplanation for the first test: initial array is x_1, x_2, x_3, x_4, x_5. Mimic is in first position. Delete the fifth object. After that, the positions are shuffled, and the mimic chose not to change his appearance. Object positions become x_4, x_1, x_2, x_3. Delete the third objects. The mimic is forced to turn into another object, because it has already been in the form 1 for two stages. The mimic chose to transform into 2, the objects are shuffled and become x_3, x_4, x_1. Delete the first and second objects. The objects positions become x_1. Only the mimic remains, and it remains an object 2. Point to the first element.
3 5 1 1 2 2 3 2 1 1 2 2 2 2 2 8 1 2 3 4 3 4 2 1 4 3 4 3 2 2 1 3 2 3 3 2 5 3 2 2 5 15 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 7 9 5 4 3 2 1
- 1 5 - 1 3 - 2 1 2 ! 1 - 0 - 4 1 3 7 8 - 1 4 - 1 2 ! 2 - 0 ! 10
1 second
256 megabytes
['constructive algorithms', 'implementation', 'interactive', '*1800']
E2. Rudolf and Snowflakes (hard version)time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is the hard version of the problem. The only difference is that in this version n \le 10^{18}.One winter morning, Rudolf was looking thoughtfully out the window, watching the falling snowflakes. He quickly noticed a certain symmetry in the configuration of the snowflakes. And like a true mathematician, Rudolf came up with a mathematical model of a snowflake.He defined a snowflake as an undirected graph constructed according to the following rules: Initially, the graph has only one vertex. Then, more vertices are added to the graph. The initial vertex is connected by edges to k new vertices (k > 1). Each vertex that is connected to only one other vertex is connected by edges to k more new vertices. This step should be done at least once. The smallest possible snowflake for k = 4 is shown in the figure. After some mathematical research, Rudolf realized that such snowflakes may not have any number of vertices. Help Rudolf check whether a snowflake with n vertices can exist.InputThe first line of the input contains an integer t (1 \le t \le 10^4) — the number of test cases.Then follow the descriptions of the test cases.The first line of each test case contains an integer n (1 \le n \le 10^{18}) — the number of vertices for which it is necessary to check the existence of a snowflake.OutputOutput t lines, each of which is the answer to the corresponding test case — "YES" if there exists such k > 1 that a snowflake with the given number of vertices can be constructed; "NO" otherwise.ExampleInput 912361315255101011000000000000000000Output NO NO NO NO YES YES YES YES NO
912361315255101011000000000000000000
NO NO NO NO YES YES YES YES NO
2 seconds
256 megabytes
['binary search', 'brute force', 'implementation', 'math', '*1800']
E1. Rudolf and Snowflakes (simple version)time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is a simple version of the problem. The only difference is that in this version n \le 10^6.One winter morning, Rudolf was looking thoughtfully out the window, watching the falling snowflakes. He quickly noticed a certain symmetry in the configuration of the snowflakes. And like a true mathematician, Rudolf came up with a mathematical model of a snowflake.He defined a snowflake as an undirected graph constructed according to the following rules: Initially, the graph has only one vertex. Then, more vertices are added to the graph. The initial vertex is connected by edges to k new vertices (k > 1). Each vertex that is connected to only one other vertex is connected by edges to k more new vertices. This step should be done at least once. The smallest possible snowflake for k = 4 is shown in the figure. After some mathematical research, Rudolf realized that such snowflakes may not have any number of vertices. Help Rudolf check if a snowflake with n vertices can exist.InputThe first line of the input contains an integer t (1 \le t \le 10^4) — the number of test cases.Then follow the descriptions of the test cases.The first line of each test case contains an integer n (1 \le n \le 10^6) — the number of vertices for which it is necessary to check the existence of a snowflake.OutputOutput t lines, each of which is the answer to the corresponding test case — "YES" if there exists such k > 1 for which a snowflake with the given number of vertices can be constructed; "NO" otherwise.ExampleInput 912361315255101011000000Output NO NO NO NO YES YES YES YES NO
912361315255101011000000
NO NO NO NO YES YES YES YES NO
2 seconds
256 megabytes
['brute force', 'implementation', 'math', '*1300']
D. Rudolph and Christmas Treetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputRudolph drew a beautiful Christmas tree and decided to print the picture. However, the ink in the cartridge often runs out at the most inconvenient moment. Therefore, Rudolph wants to calculate in advance how much green ink he will need.The tree is a vertical trunk with identical triangular branches at different heights. The thickness of the trunk is negligible.Each branch is an isosceles triangle with base d and height h, whose base is perpendicular to the trunk. The triangles are arranged upward at an angle, and the trunk passes exactly in the middle. The base of the i-th triangle is located at a height of y_i.The figure below shows an example of a tree with d = 4, h = 2 and three branches with bases at heights [1, 4, 5]. Help Rudolph calculate the total area of the tree branches.InputThe first line contains a single integer t (1 \le t \le 10^4) — the number of test cases.Then follow the descriptions of the test cases.The first line of each test case contains three integers n, d, h (1 \le n, d, h \le 2 \cdot 10^5) — the number of branches, the length of the base, and the height of the branches, respectively.The second line of each test case contains n integers y_i (1 \le y_i \le 10^9, y_1 < y_2 < ... < y_n) — the heights of the bases of the branches.The sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output a single real number on a separate line — the total area of the tree branches. The answer will be considered correct if its absolute or relative error does not exceed 10^{-6}.ExampleInput 53 4 21 4 51 5 134 6 61 2 3 42 1 2000001 2000002 4 39 11Output 11 2.5 34.5 199999.9999975 11.333333
53 4 21 4 51 5 134 6 61 2 3 42 1 2000001 2000002 4 39 11
11 2.5 34.5 199999.9999975 11.333333
2 seconds
256 megabytes
['constructive algorithms', 'geometry', 'math', '*1200']
C. Rudolf and the Another Competitiontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputRudolf has registered for a programming competition that will follow the rules of ICPC. The rules imply that for each solved problem, a participant gets 1 point, and also incurs a penalty equal to the number of minutes passed from the beginning of the competition to the moment of solving the problem. In the final table, the participant with the most points is ranked higher, and in case of a tie in points, the participant with the lower penalty is ranked higher.In total, n participants have registered for the competition. Rudolf is a participant with index 1. It is known that m problems will be proposed. And the competition will last h minutes.A powerful artificial intelligence has predicted the values t_{i, j}, which represent the number of minutes it will take for the i-th participant to solve the j-th problem.Rudolf realized that the order of solving problems will affect the final result. For example, if h = 120, and the times to solve problems are [20, 15, 110], then if Rudolf solves the problems in the order: {3, 1, 2}, then he will only solve the third problem and get 1 point and 110 penalty. {1, 2, 3}, then he will solve the first problem after 20 minutes from the start, the second one after 20+15=35 minutes, and he will not have time to solve the third one. Thus, he will get 2 points and 20+35=55 penalty. {2, 1, 3}, then he will solve the second problem after 15 minutes from the start, the first one after 15+20=35 minutes, and he will not have time to solve the third one. Thus, he will get 2 points and 15+35=50 penalty. Rudolf became interested in what place he will take in the competition if each participant solves problems in the optimal order based on the predictions of the artificial intelligence. It will be assumed that in case of a tie in points and penalty, Rudolf will take the best place.InputThe first line contains an integer t (1 \le t \le 10^3) — the number of test cases.Then follow the descriptions of the test cases.The first line of each test case contains three integers n, m, h (1 \le n \cdot m \le 2 \cdot 10^5, 1 \le h \le 10^6) — the number of participants, the number of problems, and the duration of the competition, respectively.Then there are n lines, each containing m integers t_{i, j} (1 \le t_{i, j} \le 10^6) — the number of minutes it will take for the i-th participant to solve the j-th problem.The sum of n \cdot m over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output an integer — Rudolf's place in the final table if all participants solve problems in the optimal order.ExampleInput 53 3 12020 15 11090 90 10040 40 402 1 12030301 3 12010 20 303 2 278 910 710 83 3 157 2 67 5 41 9 8Output 2 1 1 2 1 NoteIn the first example, Rudolf will get 2 points and 50 penalty minutes. The second participant will solve only one problem and get 1 point and 90 penalty minutes. And the third participant will solve all 3 problems and get 3 points and 240 penalty minutes. Thus, Rudolf will take the second place.In the second example, both participants will get 1 point and 30 penalty minutes. In case of a tie in points, Rudolf gets the better position, so he will take the first place.In the third example, Rudolf is the only participant, so he will take the first place.In the fourth example, all participants can solve two problems with penalty of 25 = 8 + (8 + 9), 24 = 7 + (7 + 10) and 26 = 8 + (8 + 10), respectively, thanks to the penalty, the second participant gets the first place, and Rudolf gets the second.
53 3 12020 15 11090 90 10040 40 402 1 12030301 3 12010 20 303 2 278 910 710 83 3 157 2 67 5 41 9 8
2 1 1 2 1
1 second
256 megabytes
['constructive algorithms', 'data structures', 'dp', 'greedy', 'sortings', '*1200']
B. Rudolph and Tic-Tac-Toetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputRudolph invented the game of tic-tac-toe for three players. It has classic rules, except for the third player who plays with pluses. Rudolf has a 3 \times 3 field  — the result of the completed game. Each field cell contains either a cross, or a nought, or a plus sign, or nothing. The game is won by the player who makes a horizontal, vertical or diagonal row of 3's of their symbols.Rudolph wants to find the result of the game. Either exactly one of the three players won or it ended in a draw. It is guaranteed that multiple players cannot win at the same time.InputThe first line contains one integer t (1 \le t \le 10^4) — the number of test cases.Each test case consists of three lines, each of which consists of three characters. The symbol can be one of four: "X" means a cross, "O" means a nought, "+" means a plus, "." means an empty cell.OutputFor each test case, print the string "X" if the crosses won, "O" if the noughts won, "+" if the pluses won, "DRAW" if there was a draw.ExampleInput 5+X+OXOOX.O+.+OXX+O.XOOX.+++O.+X.O+...++X.O+..Output X O + DRAW DRAW
5+X+OXOOX.O+.+OXX+O.XOOX.+++O.+X.O+...++X.O+..
X O + DRAW DRAW
1 second
256 megabytes
['brute force', 'implementation', 'strings', '*800']
A. Rudolph and Cut the Rope time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n nails driven into the wall, the i-th nail is driven a_i meters above the ground, one end of the b_i meters long rope is tied to it. All nails hang at different heights one above the other. One candy is tied to all ropes at once. Candy is tied to end of a rope that is not tied to a nail.To take the candy, you need to lower it to the ground. To do this, Rudolph can cut some ropes, one at a time. Help Rudolph find the minimum number of ropes that must be cut to get the candy.The figure shows an example of the first test: InputThe first line contains one integer t (1 \le t \le 10^4) — the number of test cases.The first line of each test case contains one integer n (1 \le n \le 50) — the number of nails.The i-th of the next n lines contains two integers a_i and b_i (1 \le a_i, b_i \le 200) — the height of the i-th nail and the length of the rope tied to it, all a_i are different.It is guaranteed that the data is not contradictory, it is possible to build a configuration described in the statement.OutputFor each test case print one integer — the minimum number of ropes that need to be cut to make the candy fall to the ground.ExampleInput 434 33 11 249 25 27 73 4511 75 1012 93 21 535 64 57 7Output 2 2 3 0
434 33 11 249 25 27 73 4511 75 1012 93 21 535 64 57 7
2 2 3 0
2 seconds
256 megabytes
['implementation', 'math', '*800']
F. Swimmers in the Pooltime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThere is a pool of length l where n swimmers plan to swim. People start swimming at the same time (at the time moment 0), but you can assume that they take different lanes, so they don't interfere with each other.Each person swims along the following route: they start at point 0 and swim to point l with constant speed (which is equal to v_i units per second for the i-th swimmer). After reaching the point l, the swimmer instantly (in negligible time) turns back and starts swimming to the point 0 with the same constant speed. After returning to the point 0, the swimmer starts swimming to the point l, and so on.Let's say that some real moment of time is a meeting moment if there are at least two swimmers that are in the same point of the pool at that moment of time (that point may be 0 or l as well as any other real point inside the pool).The pool will be open for t seconds. You have to calculate the number of meeting moments while the pool is open. Since the answer may be very large, print it modulo 10^9 + 7.InputThe first line contains two integers l and t (1 \le l, t \le 10^9) — the length of the pool and the duration of the process (in seconds).The second line contains the single integer n (2 \le n \le 2 \cdot 10^5) — the number of swimmers.The third line contains n integers v_1, v_2, \dots, v_n (1 \le v_i \le 2 \cdot 10^5), where v_i is the speed of the i-th swimmer. All v_i are pairwise distinct.OutputPrint one integer — the number of meeting moments (including moment t if needed and excluding moment 0), taken modulo 10^9 + 7.ExamplesInput 9 18 2 1 2 Output 3 Input 12 13 3 4 2 6 Output 10 Input 1 1000000000 3 100000 150000 200000 Output 997200007 NoteIn the first example, there are three meeting moments: moment 6, during which both swimmers are in the point 6; moment 12, during which both swimmers are in the point 6; and moment 18, during which both swimmers are in the point 0.
9 18 2 1 2
3
3 seconds
512 megabytes
['dp', 'fft', 'math', 'number theory', '*2800']
E. Boxes and Ballstime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n boxes placed in a line. The boxes are numbered from 1 to n. Some boxes contain one ball inside of them, the rest are empty. At least one box contains a ball and at least one box is empty.In one move, you have to choose a box with a ball inside and an adjacent empty box and move the ball from one box into another. Boxes i and i+1 for all i from 1 to n-1 are considered adjacent to each other. Boxes 1 and n are not adjacent.How many different arrangements of balls exist after exactly k moves are performed? Two arrangements are considered different if there is at least one such box that it contains a ball in one of them and doesn't contain a ball in the other one.Since the answer might be pretty large, print its remainder modulo 10^9+7.InputThe first line contains two integers n and k (2 \le n \le 1500; 1 \le k \le 1500) — the number of boxes and the number of moves.The second line contains n integers a_1, a_2, \dots, a_n (a_i \in \{0, 1\}) — 0 denotes an empty box and 1 denotes a box with a ball inside. There is at least one 0 and at least one 1.OutputPrint a single integer — the number of different arrangements of balls that can exist after exactly k moves are performed, modulo 10^9+7.ExamplesInput 4 1 1 0 1 0 Output 3 Input 4 2 1 0 1 0 Output 2 Input 10 6 1 0 0 1 0 0 0 1 1 1 Output 69 NoteIn the first example, there are the following possible arrangements: 0 1 1 0 — obtained after moving the ball from box 1 to box 2; 1 0 0 1 — obtained after moving the ball from box 3 to box 4; 1 1 0 0 — obtained after moving the ball from box 3 to box 2. In the second example, there are the following possible arrangements: 1 0 1 0 — three ways to obtain that: just reverse the operation performed during the first move; 0 1 0 1 — obtained from either of the first two arrangements after the first move.
4 1 1 0 1 0
3
5 seconds
256 megabytes
['dp', 'implementation', 'math', '*2500']
D. Rating Systemtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are developing a rating system for an online game. Every time a player participates in a match, the player's rating changes depending on the results.Initially, the player's rating is 0. There are n matches; after the i-th match, the rating change is equal to a_i (the rating increases by a_i if a_i is positive, or decreases by |a_i| if it's negative. There are no zeros in the sequence a).The system has an additional rule: for a fixed integer k, if a player's rating has reached the value k, it will never fall below it. Formally, if a player's rating at least k, and a rating change would make it less than k, then the rating will decrease to exactly k.Your task is to determine the value k in such a way that the player's rating after all n matches is the maximum possible (among all integer values of k). If there are multiple possible answers, you can print any of them.InputThe first line contains a single integer t (1 \le t \le 10^4) — the number of test cases.The first line of each test case contains a single integer n (1 \le n \le 3 \cdot 10^5) — the number of matches.The second line contains n integer a_1, a_2, \dots, a_n (-10^9 \le a_i \le 10^9; a_i \ne 0) — the rating change after the i-th match.The sum of n over all test cases doesn't exceed 3 \cdot 10^5.OutputFor each test case, print one integer m (-10^{18} \le m \le 10^{18}) — the value of k such that the rating of the player will be the maximum possible when using this value. It can be shown that at least one of the optimal answers meets the constraint -10^{18} \le m \le 10^{18}.ExampleInput 443 -2 1 23-1 -2 -124 275 1 -3 2 -1 -2 2Output 3 0 25 6 NoteIn the first example, if k=3, then the rating changes as follows: 0 \rightarrow 3 \rightarrow 3 \rightarrow 4 \rightarrow 6.In the second example, if k=0, then the rating changes as follows: 0 \rightarrow 0 \rightarrow 0 \rightarrow 0.In the third example, if k=25, then the rating changes as follows: 0 \rightarrow 4 \rightarrow 6.In the fourth example, if k=6, then the rating changes as follows: 0 \rightarrow 5 \rightarrow 6 \rightarrow 6 \rightarrow 8 \rightarrow 7 \rightarrow 6 \rightarrow 8.
443 -2 1 23-1 -2 -124 275 1 -3 2 -1 -2 2
3 0 25 6
2 seconds
256 megabytes
['binary search', 'brute force', 'data structures', 'dp', 'dsu', 'greedy', 'math', 'two pointers', '*1800']
C. Strong Passwordtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMonocarp finally got the courage to register on ForceCoders. He came up with a handle but is still thinking about the password.He wants his password to be as strong as possible, so he came up with the following criteria: the length of the password should be exactly m; the password should only consist of digits from 0 to 9; the password should not appear in the password database (given as a string s) as a subsequence (not necessarily contiguous). Monocarp also came up with two strings of length m: l and r, both consisting only of digits from 0 to 9. He wants the i-th digit of his password to be between l_i and r_i, inclusive.Does there exist a password that fits all criteria?InputThe first line contains a single integer t (1 \le t \le 10^4) — the number of testcases.The first line of each testcase contains a string s (1 \le |s| \le 3 \cdot 10^5), consisting only of digits from 0 to 9 — the password database.The second line contains a single integer m (1 \le m \le 10) — the required length of the password.The third line contains a string l (|l| = m), consisting only of digits from 0 to 9 — the lower restriction on each digit.The fourth line contains a string r (|r| = m), consisting only of digits from 0 to 9 — the upper restriction on each digit. l_i \le r_i for all i from 1 to m.The sum of lengths of s over all testcases doesn't exceed 3 \cdot 10^5.OutputFor each testcase, print "YES" if there exists a password that fits all criteria. Print "NO" otherwise.ExampleInput 5880055535351234562505612341234123431114441234443214321459249590001021011Output YES NO YES NO YES NoteIn the first testcase, Monocarp can choose password "50". It doesn't appear in s as a subsequence.In the second testcase, all combinations of three digits, each of them being from 1 to 4, fit the criteria on l and r. However, all of them appear in s as subsequences. For example, "314" appears at positions [3, 5, 12] and "222" appears at positions [2, 6, 10].In the third testcase, Monocarp can choose password "4321". Actually, that is the only password that fits the criteria on l and r. Luckily, it doesn't appear in s as a subsequence.In the fourth testcase, only "49" and "59" fit the criteria on l and r. Both of them appear in s as subsequences.In the fifth testcase, Monocarp can choose password "11".
5880055535351234562505612341234123431114441234443214321459249590001021011
YES NO YES NO YES
2 seconds
256 megabytes
['binary search', 'dp', 'greedy', 'strings', '*1400']
B. Come Togethertime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputBob and Carol hanged out with Alice the whole day, but now it's time to go home. Alice, Bob and Carol live on an infinite 2D grid in cells A, B, and C respectively. Right now, all of them are in cell A.If Bob (or Carol) is in some cell, he (she) can move to one of the neighboring cells. Two cells are called neighboring if they share a side. For example, the cell (3, 5) has four neighboring cells: (2, 5), (4, 5), (3, 6) and (3, 4).Bob wants to return to the cell B, Carol — to the cell C. Both of them want to go along the shortest path, i. e. along the path that consists of the minimum possible number of cells. But they would like to walk together as well. What is the maximum possible number of cells that Bob and Carol can walk together if each of them walks home using one of the shortest paths?InputThe first line contains the single integer t (1 \le t \le 10^4) — the number of test cases.The first line of each test case contains two integers x_A and y_A (1 \le x_A, y_A \le 10^8) — the position of cell A, where both Bob and Carol are right now.The second line contains two integers x_B and y_B (1 \le x_B, y_B \le 10^8) — the position of cell B (Bob's house).The third line contains two integers x_C and y_C (1 \le x_C, y_C \le 10^8) — the position of cell C (Carol's house).Additional constraint on the input: the cells A, B, and C are pairwise distinct in each test case.OutputFor each test case, print the single integer — the maximum number of cells Bob and Carol can walk together if each of them goes home along one of the shortest paths.ExampleInput 33 11 36 45 22 27 21 14 35 5Output 3 1 6 NoteIn all pictures, red color denotes cells belonging only to Bob's path, light blue color — cells belonging only to Carol's path, and dark blue color — cells belonging to both paths.One of the optimal routes for the first test case is shown below: Bob's route contains 5 cells, Carol's route — 7 cells, and they will visit 3 cells together.The optimal answer for the second test case is shown below: Bob's route contains 4 cells, Carol's route — 3 cells, and they will visit only 1 cell together.One of the optimal answers for the third test case is shown below: Bob's route contains 6 cells, Carol's route — 9 cells, and they will visit 6 cells together.
33 11 36 45 22 27 21 14 35 5
3 1 6
2 seconds
256 megabytes
['geometry', 'implementation', 'math', '*900']
A. Forbidden Integertime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an integer n, which you want to obtain. You have an unlimited supply of every integer from 1 to k, except integer x (there are no integer x at all).You are allowed to take an arbitrary amount of each of these integers (possibly, zero). Can you make the sum of taken integers equal to n?If there are multiple answers, print any of them.InputThe first line contains a single integer t (1 \le t \le 100) — the number of testcases.The only line of each testcase contains three integers n, k and x (1 \le x \le k \le n \le 100).OutputFor each test case, in the first line, print "YES" or "NO" — whether you can take an arbitrary amount of each integer from 1 to k, except integer x, so that their sum is equal to n.If you can, the second line should contain a single integer m — the total amount of taken integers. The third line should contain m integers — each of them from 1 to k, not equal to x, and their sum is n.If there are multiple answers, print any of them.ExampleInput 510 3 25 2 14 2 17 7 36 1 1Output YES 6 3 1 1 1 1 3 NO YES 2 2 2 YES 1 7 NO NoteAnother possible answer for the first testcase is [3, 3, 3, 1]. Note that you don't have to minimize the amount of taken integers. There also exist other answers.In the second testcase, you only have an unlimited supply of integer 2. There is no way to get sum 5 using only them.In the fifth testcase, there are no integers available at all, so you can't get any positive sum.
510 3 25 2 14 2 17 7 36 1 1
YES 6 3 1 1 1 1 3 NO YES 2 2 2 YES 1 7 NO
2 seconds
256 megabytes
['constructive algorithms', 'implementation', 'math', 'number theory', '*800']
H. Multiple of Three Cyclestime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array a_1,\dots,a_n of length n is initially all blank. There are n updates where one entry of a is updated to some number, such that a becomes a permutation of 1,2,\dots,n after all the updates.After each update, find the number of ways (modulo 998\,244\,353) to fill in the remaining blank entries of a so that a becomes a permutation of 1,2,\dots,n and all cycle lengths in a are multiples of 3.A permutation of 1,2,\dots,n is an array of length n consisting of n distinct integers from 1 to n in arbitrary order. A cycle in a permutation a is a sequence of pairwise distinct integers (i_1,\dots,i_k) such that i_2 = a_{i_1},i_3 = a_{i_2},\dots,i_k = a_{i_{k-1}},i_1 = a_{i_k}. The length of this cycle is the number k, which is a multiple of 3 if and only if k \equiv 0 \pmod 3.InputThe first line contains a single integer n (3 \le n \le 3 \cdot 10^5, n \equiv 0 \pmod 3).The i-th of the next n lines contains two integers x_i and y_i, representing that the i-th update changes a_{x_i} to y_i.It is guaranteed that x_1,\dots,x_n and y_1,\dots,y_n are permutations of 1,2,\dots,n, i.e. a becomes a permutation of 1,2,\dots,n after all the updates.OutputOutput n lines: the number of ways (modulo 998\,244\,353) after the first 1,2,\dots,n updates.ExamplesInput 6 3 2 1 4 4 5 2 6 5 1 6 3 Output 32 8 3 2 1 1 Input 3 1 1 2 3 3 2 Output 0 0 0 Input 18 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 1 Output 671571067 353924552 521242461 678960117 896896000 68992000 6272000 627200 62720 7840 1120 160 32 8 2 1 1 1 NoteIn the first sample, for example, after the 3rd update the 3 ways to complete the permutation a = [4,\_,2,5,\_,\_] are as follows: [4,1,2,5,6,3]: The only cycle is (1\,4\,5\,6\,3\,2), with length 6. [4,6,2,5,1,3]: The cycles are (1\,4\,5) and (2\,6\,3), with lengths 3 and 3. [4,6,2,5,3,1]: The only cycle is (1\,4\,5\,3\,2\,6), with length 6. In the second sample, the first update creates a cycle of length 1, so there are no ways to make all cycle lengths a multiple of 3.
6 3 2 1 4 4 5 2 6 5 1 6 3
32 8 3 2 1 1
3 seconds
256 megabytes
['combinatorics', 'data structures', 'dp', 'dsu', 'math', '*3400']
G. Tree Weightstime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a tree with n nodes labelled 1,2,\dots,n. The i-th edge connects nodes u_i and v_i and has an unknown positive integer weight w_i. To help you figure out these weights, you are also given the distance d_i between the nodes i and i+1 for all 1 \le i \le n-1 (the sum of the weights of the edges on the simple path between the nodes i and i+1 in the tree).Find the weight of each edge. If there are multiple solutions, print any of them. If there are no weights w_i consistent with the information, print a single integer -1.InputThe first line contains a single integer n (2 \le n \le 10^5).The i-th of the next n-1 lines contains two integers u_i and v_i (1 \le u_i,v_i \le n, u_i \ne v_i).The last line contains n-1 integers d_1,\dots,d_{n-1} (1 \le d_i \le 10^{12}).It is guaranteed that the given edges form a tree.OutputIf there is no solution, print a single integer -1. Otherwise, output n-1 lines containing the weights w_1,\dots,w_{n-1}.If there are multiple solutions, print any of them.ExamplesInput 5 1 2 1 3 2 4 2 5 31 41 59 26 Output 31 10 18 8 Input 3 1 2 1 3 18 18 Output -1 Input 9 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 2 236 205 72 125 178 216 214 117 Output 31 41 59 26 53 58 97 93 NoteIn the first sample, the tree is as follows: In the second sample, note that w_2 is not allowed to be 0 because it must be a positive integer, so there is no solution.In the third sample, the tree is as follows:
5 1 2 1 3 2 4 2 5 31 41 59 26
31 10 18 8
5 seconds
256 megabytes
['bitmasks', 'constructive algorithms', 'data structures', 'dfs and similar', 'implementation', 'math', 'matrices', 'number theory', 'trees', '*3000']
F2. Min Cost Permutation (Hard Version)time limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe only difference between this problem and the easy version is the constraints on t and n.You are given an array of n positive integers a_1,\dots,a_n, and a (possibly negative) integer c.Across all permutations b_1,\dots,b_n of the array a_1,\dots,a_n, consider the minimum possible value of \sum_{i=1}^{n-1} |b_{i+1}-b_i-c|. Find the lexicographically smallest permutation b of the array a that achieves this minimum.A sequence x is lexicographically smaller than a sequence y if and only if one of the following holds: x is a prefix of y, but x \ne y; in the first position where x and y differ, the sequence x has a smaller element than the corresponding element in y.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^4). The description of the test cases follows.The first line of each test case contains two integers n and c (1 \le n \le 2 \cdot 10^5, -10^9 \le c \le 10^9).The second line of each test case contains n integers a_1,\dots,a_n (1 \le a_i \le 10^9).It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output n integers b_1,\dots,b_n, the lexicographically smallest permutation of a that achieves the minimum \sum\limits_{i=1}^{n-1} |b_{i+1}-b_i-c|.ExampleInput 36 -73 1 4 1 5 93 21 3 51 27182818Output 9 3 1 4 5 1 1 3 5 2818 NoteIn the first test case, it can be proven that the minimum possible value of \sum\limits_{i=1}^{n-1} |b_{i+1}-b_i-c| is 27, and the permutation b = [9,3,1,4,5,1] is the lexicographically smallest permutation of a that achieves this minimum: |3-9-(-7)|+|1-3-(-7)|+|4-1-(-7)|+|5-4-(-7)|+|1-5-(-7)| = 1+5+10+8+3 = 27.In the second test case, the minimum possible value of \sum\limits_{i=1}^{n-1} |b_{i+1}-b_i-c| is 0, and b = [1,3,5] is the lexicographically smallest permutation of a that achieves this.In the third test case, there is only one permutation b.
36 -73 1 4 1 5 93 21 3 51 27182818
9 3 1 4 5 1 1 3 5 2818
3 seconds
256 megabytes
['binary search', 'constructive algorithms', 'data structures', 'greedy', 'math', 'sortings', '*2800']
F1. Min Cost Permutation (Easy Version)time limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe only difference between this problem and the hard version is the constraints on t and n.You are given an array of n positive integers a_1,\dots,a_n, and a (possibly negative) integer c.Across all permutations b_1,\dots,b_n of the array a_1,\dots,a_n, consider the minimum possible value of \sum_{i=1}^{n-1} |b_{i+1}-b_i-c|. Find the lexicographically smallest permutation b of the array a that achieves this minimum.A sequence x is lexicographically smaller than a sequence y if and only if one of the following holds: x is a prefix of y, but x \ne y; in the first position where x and y differ, the sequence x has a smaller element than the corresponding element in y.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^3). The description of the test cases follows.The first line of each test case contains two integers n and c (1 \le n \le 5 \cdot 10^3, -10^9 \le c \le 10^9).The second line of each test case contains n integers a_1,\dots,a_n (1 \le a_i \le 10^9).It is guaranteed that the sum of n over all test cases does not exceed 5 \cdot 10^3.OutputFor each test case, output n integers b_1,\dots,b_n, the lexicographically smallest permutation of a that achieves the minimum \sum\limits_{i=1}^{n-1} |b_{i+1}-b_i-c|.ExampleInput 36 -73 1 4 1 5 93 21 3 51 27182818Output 9 3 1 4 5 1 1 3 5 2818 NoteIn the first test case, it can be proven that the minimum possible value of \sum\limits_{i=1}^{n-1} |b_{i+1}-b_i-c| is 27, and the permutation b = [9,3,1,4,5,1] is the lexicographically smallest permutation of a that achieves this minimum: |3-9-(-7)|+|1-3-(-7)|+|4-1-(-7)|+|5-4-(-7)|+|1-5-(-7)| = 1+5+10+8+3 = 27.In the second test case, the minimum possible value of \sum\limits_{i=1}^{n-1} |b_{i+1}-b_i-c| is 0, and b = [1,3,5] is the lexicographically smallest permutation of a that achieves this.In the third test case, there is only one permutation b.
36 -73 1 4 1 5 93 21 3 51 27182818
9 3 1 4 5 1 1 3 5 2818
3 seconds
256 megabytes
['brute force', 'constructive algorithms', 'greedy', 'math', '*2600']
E. Great Gridstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn n \times m grid of characters is called great if it satisfies these three conditions: Each character is either 'A', 'B', or 'C'. Every 2 \times 2 contiguous subgrid contains all three different letters. Any two cells that share a common edge contain different letters. Let (x,y) denote the cell in the x-th row from the top and y-th column from the left.You want to construct a great grid that satisfies k constraints. Each constraint consists of two cells, (x_{i,1},y_{i,1}) and (x_{i,2},y_{i,2}), that share exactly one corner. You want your great grid to have the same letter in cells (x_{i,1},y_{i,1}) and (x_{i,2},y_{i,2}).Determine whether there exists a great grid satisfying all the constraints. InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^3). The description of the test cases follows.The first line of each test case contains three integers, n, m, and k (2 \le n,m \le 2 \cdot 10^3, 1 \le k \le 4 \cdot 10^3).Each of the next k lines contains four integers, x_{i,1}, y_{i,1}, x_{i,2}, and y_{i,2} (1 \le x_{i,1} < x_{i,2} \le n, 1 \le y_{i,1},y_{i,2} \le m). It is guaranteed that either (x_{i,2},y_{i,2}) = (x_{i,1}+1,y_{i,1}+1) or (x_{i,2},y_{i,2}) = (x_{i,1}+1,y_{i,1}-1).The pairs of cells are pairwise distinct, i.e. for all 1 \le i < j \le k, it is not true that x_{i,1} = x_{j,1}, y_{i,1} = y_{j,1}, x_{i,2} = x_{j,2}, and y_{i,2} = y_{j,2}.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^3.It is guaranteed that the sum of m over all test cases does not exceed 2 \cdot 10^3.It is guaranteed that the sum of k over all test cases does not exceed 4 \cdot 10^3.OutputFor each test case, output "YES" if a great grid satisfying all the constraints exists and "NO" otherwise.You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.ExampleInput 43 4 41 1 2 22 1 3 21 4 2 32 3 3 22 7 21 1 2 21 2 2 18 5 41 2 2 11 5 2 47 1 8 27 4 8 58 5 41 2 2 11 5 2 47 1 8 27 5 8 4Output YES NO YES NO NoteIn the first test case, the following great grid satisfies all the constraints: BABCCBCAACABIn the second test case, the two constraints imply that cells (1,1) and (2,2) have the same letter and cells (1,2) and (2,1) have the same letter, which makes it impossible for the only 2 \times 2 subgrid to contain all three different letters.
43 4 41 1 2 22 1 3 21 4 2 32 3 3 22 7 21 1 2 21 2 2 18 5 41 2 2 11 5 2 47 1 8 27 4 8 58 5 41 2 2 11 5 2 47 1 8 27 5 8 4
YES NO YES NO
1 second
256 megabytes
['2-sat', 'constructive algorithms', 'dfs and similar', 'dsu', 'graphs', '*2400']
D. Row Majortime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe row-major order of an r \times c grid of characters A is the string obtained by concatenating all the rows, i.e. A_{11}A_{12} \dots A_{1c}A_{21}A_{22} \dots A_{2c} \dots A_{r1}A_{r2} \dots A_{rc}. A grid of characters A is bad if there are some two adjacent cells (cells sharing an edge) with the same character.You are given a positive integer n. Consider all strings s consisting of only lowercase Latin letters such that they are not the row-major order of any bad grid. Find any string with the minimum number of distinct characters among all such strings of length n.It can be proven that at least one such string exists under the constraints of the problem.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^4). The description of the test cases follows.The only line of each test case contains a single integer n (1 \le n \le 10^6).It is guaranteed that the sum of n over all test cases does not exceed 10^6.OutputFor each test case, output a string with the minimum number of distinct characters among all suitable strings of length n.If there are multiple solutions, print any of them.ExampleInput 44216Output that is a tomato NoteIn the first test case, there are 3 ways s can be the row-major order of a grid, and they are all not bad: tththathatat It can be proven that 3 distinct characters is the minimum possible.In the second test case, there are 2 ways s can be the row-major order of a grid, and they are both not bad: iiss It can be proven that 2 distinct characters is the minimum possible.In the third test case, there is only 1 way s can be the row-major order of a grid, and it is not bad.In the fourth test case, there are 4 ways s can be the row-major order of a grid, and they are all not bad: ttotomtomatoomaatomtoato It can be proven that 4 distinct characters is the minimum possible. Note that, for example, the string "orange" is not an acceptable output because it has 6 > 4 distinct characters, and the string "banana" is not an acceptable output because it is the row-major order of the following bad grid: banana
44216
that is a tomato
2 seconds
256 megabytes
['constructive algorithms', 'greedy', 'math', 'number theory', 'strings', '*1400']
C. Particlestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have discovered n mysterious particles on a line with integer charges of c_1,\dots,c_n. You have a device that allows you to perform the following operation: Choose a particle and remove it from the line. The remaining particles will shift to fill in the gap that is created. If there were particles with charges x and y directly to the left and right of the removed particle, they combine into a single particle of charge x+y. For example, if the line of particles had charges of [-3,1,4,-1,5,-9], performing the operation on the 4th particle will transform the line into [-3,1,9,-9]. If we then use the device on the 1st particle in this new line, the line will turn into [1,9,-9]. You will perform operations until there is only one particle left. What is the maximum charge of this remaining particle that you can obtain?InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^4). The description of the test cases follows.The first line of each test case contains a single integer n (1 \le n \le 2 \cdot 10^5).The second line of each test case contains n integers c_1,\dots,c_n (-10^9 \le c_i \le 10^9).It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output one integer, the maximum charge of the remaining particle.ExampleInput 36-3 1 4 -1 5 -95998244353 998244353 998244353 998244353 9982443531-2718Output 9 2994733059 -2718 NoteIn the first test case, the best strategy is to use the device on the 4th particle, then on the 1st particle (as described in the statement), and finally use the device on the new 3rd particle followed by the 1st particle.In the second test case, the best strategy is to use the device on the 4th particle to transform the line into [998244353,998244353,1996488706], then on the 2nd particle to transform the line into [2994733059]. Be wary of integer overflow.In the third test case, there is only one particle, so no operations can be performed.
36-3 1 4 -1 5 -95998244353 998244353 998244353 998244353 9982443531-2718
9 2994733059 -2718
1 second
256 megabytes
['dp', 'greedy', 'implementation', 'math', '*1300']
B. Permutations & Primestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a positive integer n.In this problem, the \operatorname{MEX} of a collection of integers c_1,c_2,\dots,c_k is defined as the smallest positive integer x which does not occur in the collection c. The primality of an array a_1,\dots,a_n is defined as the number of pairs (l,r) such that 1 \le l \le r \le n and \operatorname{MEX}(a_l,\dots,a_r) is a prime number. Find any permutation of 1,2,\dots,n with the maximum possible primality among all permutations of 1,2,\dots,n. Note: A prime number is a number greater than or equal to 2 that is not divisible by any positive integer except 1 and itself. For example, 2,5,13 are prime numbers, but 1 and 6 are not prime numbers. A permutation of 1,2,\dots,n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array), and [1,3,4] is also not a permutation (n=3 but there is 4 in the array). InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^4). The description of the test cases follows.The only line of each test case contains a single integer n (1 \le n \le 2 \cdot 10^5).It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output n integers: a permutation of 1,2,\dots,n that achieves the maximum possible primality.If there are multiple solutions, print any of them.ExampleInput 3215Output 2 1 1 5 2 1 4 3 NoteIn the first test case, there are 3 pairs (l,r) with 1 \le l \le r \le 2, out of which 2 have a prime \operatorname{MEX}(a_l,\dots,a_r): (l,r) = (1,1): \operatorname{MEX}(2) = 1, which is not prime. (l,r) = (1,2): \operatorname{MEX}(2,1) = 3, which is prime. (l,r) = (2,2): \operatorname{MEX}(1) = 2, which is prime. Therefore, the primality is 2.In the second test case, \operatorname{MEX}(1) = 2 is prime, so the primality is 1.In the third test case, the maximum possible primality is 8.
3215
2 1 1 5 2 1 4 3
1 second
256 megabytes
['constructive algorithms', 'math', '*1000']
A. Subtraction Gametime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two positive integers, a and b (a < b).For some positive integer n, two players will play a game starting with a pile of n stones. They take turns removing exactly a or exactly b stones from the pile. The player who is unable to make a move loses.Find a positive integer n such that the second player to move in this game has a winning strategy. This means that no matter what moves the first player makes, the second player can carefully choose their moves (possibly depending on the first player's moves) to ensure they win.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 100). The description of the test cases follows.The only line of each test case contains two integers, a and b (1 \le a < b \le 100).OutputFor each test case, output any positive integer n (1 \le n \le 10^6) such that the second player to move wins.It can be proven that such an n always exists under the constraints of the problem.ExampleInput 31 41 59 26Output 2 6 3 NoteIn the first test case, when n = 2, the first player must remove a = 1 stone. Then, the second player can respond by removing a = 1 stone. The first player can no longer make a move, so the second player wins.In the second test case, when n = 6, the first player has two options: If they remove b = 5 stones, then the second player can respond by removing a = 1 stone. The first player can no longer make a move, so the second player wins. If they remove a = 1 stone, then the second player can respond by removing a = 1 stone. Afterwards, the players can only alternate removing exactly a = 1 stone. The second player will take the last stone and win. Since the second player has a winning strategy no matter what the first player does, this is an acceptable output.In the third test case, the first player cannot make any moves when n = 3, so the second player immediately wins.
31 41 59 26
2 6 3
1 second
256 megabytes
['constructive algorithms', 'games', '*800']
F2. Omsk Metro (hard version)time limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThis is the hard version of the problem. The only difference between the simple and hard versions is that in this version u can take any possible value.As is known, Omsk is the capital of Berland. Like any capital, Omsk has a well-developed metro system. The Omsk metro consists of a certain number of stations connected by tunnels, and between any two stations there is exactly one path that passes through each of the tunnels no more than once. In other words, the metro is a tree.To develop the metro and attract residents, the following system is used in Omsk. Each station has its own weight x \in \{-1, 1\}. If the station has a weight of -1, then when the station is visited by an Omsk resident, a fee of 1 burle is charged. If the weight of the station is 1, then the Omsk resident is rewarded with 1 burle.Omsk Metro currently has only one station with number 1 and weight x = 1. Every day, one of the following events occurs: A new station with weight x is added to the station with number v_i, and it is assigned a number that is one greater than the number of existing stations. Alex, who lives in Omsk, wonders: is there a subsegment\dagger (possibly empty) of the path between vertices u and v such that, by traveling along it, exactly k burles can be earned (if k < 0, this means that k burles will have to be spent on travel). In other words, Alex is interested in whether there is such a subsegment of the path that the sum of the weights of the vertices in it is equal to k. Note that the subsegment can be empty, and then the sum is equal to 0. You are a friend of Alex, so your task is to answer Alex's questions.\daggerSubsegment — continuous sequence of elements.InputThe first line contains a single number t (1 \leq t \leq 10^4) — the number of test cases.The first line of each test case contains the number n (1 \leq n \leq 2 \cdot 10^5) — the number of events.Then there are n lines describing the events. In the i-th line, one of the following options is possible: First comes the symbol "+" (without quotes), then two numbers v_i and x_i (x_i \in \{-1, 1\}, it is also guaranteed that the vertex with number v_i exists). In this case, a new station with weight x_i is added to the station with number v_i. First comes the symbol "?" (without quotes), and then three numbers u_i, v_i, and k_i (-n \le k_i \le n). It is guaranteed that the vertices with numbers u_i and v_i exist. In this case, it is necessary to determine whether there is a subsegment (possibly empty) of the path between stations u_i and v_i with a sum of weights exactly equal to k_i. It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each of Alex's questions, output "Yes" (without quotes) if the subsegment described in the condition exists, otherwise output "No" (without quotes).You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).ExamplesInput 18+ 1 -1? 1 1 2? 1 2 1+ 1 1? 1 3 -1? 1 1 1? 1 3 2? 1 1 0Output NO YES NO YES YES YES Input 17+ 1 -1+ 2 -1+ 2 1+ 3 -1? 5 2 2? 3 1 -1? 5 4 -3Output NO YES YES NoteExplanation of the first sample.The answer to the second question is "Yes", because there is a path 1.In the fourth question, we can choose the 1 path again.In the fifth query, the answer is "Yes", since there is a path 1-3.In the sixth query, we can choose an empty path because the sum of the weights on it is 0.It is not difficult to show that there are no paths satisfying the first and third queries.
18+ 1 -1? 1 1 2? 1 2 1+ 1 1? 1 3 -1? 1 1 1? 1 3 2? 1 1 0
NO YES NO YES YES YES
2 seconds
512 megabytes
['data structures', 'dfs and similar', 'divide and conquer', 'dp', 'math', 'trees', '*2300']
F1. Omsk Metro (simple version)time limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThis is the simple version of the problem. The only difference between the simple and hard versions is that in this version u = 1.As is known, Omsk is the capital of Berland. Like any capital, Omsk has a well-developed metro system. The Omsk metro consists of a certain number of stations connected by tunnels, and between any two stations there is exactly one path that passes through each of the tunnels no more than once. In other words, the metro is a tree.To develop the metro and attract residents, the following system is used in Omsk. Each station has its own weight x \in \{-1, 1\}. If the station has a weight of -1, then when the station is visited by an Omsk resident, a fee of 1 burle is charged. If the weight of the station is 1, then the Omsk resident is rewarded with 1 burle.Omsk Metro currently has only one station with number 1 and weight x = 1. Every day, one of the following events occurs: A new station with weight x is added to the station with number v_i, and it is assigned a number that is one greater than the number of existing stations. Alex, who lives in Omsk, wonders: is there a subsegment\dagger (possibly empty) of the path between vertices u and v such that, by traveling along it, exactly k burles can be earned (if k < 0, this means that k burles will have to be spent on travel). In other words, Alex is interested in whether there is such a subsegment of the path that the sum of the weights of the vertices in it is equal to k. Note that the subsegment can be empty, and then the sum is equal to 0. You are a friend of Alex, so your task is to answer Alex's questions.\daggerSubsegment — continuous sequence of elements.InputThe first line contains a single number t (1 \leq t \leq 10^4) — the number of test cases.The first line of each test case contains the number n (1 \leq n \leq 2 \cdot 10^5) — the number of events.Then there are n lines describing the events. In the i-th line, one of the following options is possible: First comes the symbol "+" (without quotes), then two numbers v_i and x_i (x_i \in \{-1, 1\}, it is also guaranteed that the vertex with number v_i exists). In this case, a new station with weight x_i is added to the station with number v_i. First comes the symbol "?" (without quotes), and then three numbers u_i, v_i, and k_i (-n \le k_i \le n). It is guaranteed that the vertices with numbers u_i and v_i exist. In this case, it is necessary to determine whether there is a subsegment (possibly empty) of the path between stations u_i and v_i with a sum of weights exactly equal to k_i. In this version of the task, it is guaranteed that u_i = 1. It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each of Alex's questions, output "Yes" (without quotes) if the subsegment described in the condition exists, otherwise output "No" (without quotes).You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).ExamplesInput 18+ 1 -1? 1 1 2? 1 2 1+ 1 1? 1 3 -1? 1 1 1? 1 3 2? 1 1 0Output NO YES NO YES YES YES Input 110+ 1 -1+ 1 -1+ 3 1+ 3 -1+ 3 1? 1 6 -1? 1 6 2? 1 2 0? 1 5 -2? 1 4 3Output YES NO YES YES NO NoteExplanation of the first sample.The answer to the second question is "Yes", because there is a path 1.In the fourth question, we can choose the 1 path again.In the fifth query, the answer is "Yes", since there is a path 1-3.In the sixth query, we can choose an empty path because the sum of the weights on it is 0.It is not difficult to show that there are no paths satisfying the first and third queries.
18+ 1 -1? 1 1 2? 1 2 1+ 1 1? 1 3 -1? 1 1 1? 1 3 2? 1 1 0
NO YES NO YES YES YES
2 seconds
512 megabytes
['data structures', 'dfs and similar', 'dp', 'graphs', 'greedy', 'math', 'trees', '*1800']
E. Tracking Segmentstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a consisting of n zeros. You are also given a set of m not necessarily different segments. Each segment is defined by two numbers l_i and r_i (1 \le l_i \le r_i \le n) and represents a subarray a_{l_i}, a_{l_i+1}, \dots, a_{r_i} of the array a.Let's call the segment l_i, r_i beautiful if the number of ones on this segment is strictly greater than the number of zeros. For example, if a = [1, 0, 1, 0, 1], then the segment [1, 5] is beautiful (the number of ones is 3, the number of zeros is 2), but the segment [3, 4] is not is beautiful (the number of ones is 1, the number of zeros is 1).You also have q changes. For each change you are given the number 1 \le x \le n, which means that you must assign an element a_x the value 1.You have to find the first change after which at least one of m given segments becomes beautiful, or report that none of them is beautiful after processing all q changes.InputThe first line contains a single integer t (1 \le t \le 10^4) — the number of test cases.The first line of each test case contains two integers n and m (1 \le m \le n \le 10^5) — the size of the array a and the number of segments, respectively.Then there are m lines consisting of two numbers l_i and r_i (1 \le l_i \le r_i \le n) —the boundaries of the segments.The next line contains an integer q (1 \le q \le n) — the number of changes.The following q lines each contain a single integer x (1 \le x \le n) — the index of the array element that needs to be set to 1. It is guaranteed that indexes in queries are distinct.It is guaranteed that the sum of n for all test cases does not exceed 10^5.OutputFor each test case, output one integer  — the minimum change number after which at least one of the segments will be beautiful, or -1 if none of the segments will be beautiful.ExampleInput 65 51 24 51 51 32 45531244 21 14 42235 21 51 5421345 21 51 35412355 51 51 51 51 51 431433 22 21 33231Output 3 -1 3 3 3 1 NoteIn the first case, after first 2 changes we won't have any beautiful segments, but after the third one on a segment [1; 5] there will be 3 ones and only 2 zeros, so the answer is 3.In the second case, there won't be any beautiful segments.
65 51 24 51 51 32 45531244 21 14 42235 21 51 5421345 21 51 35412355 51 51 51 51 51 431433 22 21 33231
3 -1 3 3 3 1
2 seconds
256 megabytes
['binary search', 'brute force', 'data structures', 'two pointers', '*1600']
D. Apple Treetime limit per test4 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputTimofey has an apple tree growing in his garden; it is a rooted tree of n vertices with the root in vertex 1 (the vertices are numbered from 1 to n). A tree is a connected graph without loops and multiple edges.This tree is very unusual — it grows with its root upwards. However, it's quite normal for programmer's trees.The apple tree is quite young, so only two apples will grow on it. Apples will grow in certain vertices (these vertices may be the same). After the apples grow, Timofey starts shaking the apple tree until the apples fall. Each time Timofey shakes the apple tree, the following happens to each of the apples:Let the apple now be at vertex u. If a vertex u has a child, the apple moves to it (if there are several such vertices, the apple can move to any of them). Otherwise, the apple falls from the tree. It can be shown that after a finite time, both apples will fall from the tree.Timofey has q assumptions in which vertices apples can grow. He assumes that apples can grow in vertices x and y, and wants to know the number of pairs of vertices (a, b) from which apples can fall from the tree, where a — the vertex from which an apple from vertex x will fall, b — the vertex from which an apple from vertex y will fall. Help him do this.InputThe first line contains integer t (1 \leq t \leq 10^4) — the number of test cases.The first line of each test case contains integer n (2 \leq n \leq 2 \cdot 10^5) — the number of vertices in the tree.Then there are n - 1 lines describing the tree. In line i there are two integers u_i and v_i (1 \leq u_i, v_i \leq n, u_i \ne v_i) — edge in tree.The next line contains a single integer q (1 \leq q \leq 2 \cdot 10^5) — the number of Timofey's assumptions.Each of the next q lines contains two integers x_i and y_i (1 \leq x_i, y_i \leq n) — the supposed vertices on which the apples will grow for the assumption i.It is guaranteed that the sum of n does not exceed 2 \cdot 10^5. Similarly, It is guaranteed that the sum of q does not exceed 2 \cdot 10^5.OutputFor each Timofey's assumption output the number of ordered pairs of vertices from which apples can fall from the tree if the assumption is true on a separate line.ExamplesInput 251 23 45 33 243 45 14 41 331 21 331 12 33 1Output 2 2 1 4 4 1 2 Input 255 11 22 34 325 55 153 25 32 14 234 32 14 2Output 1 2 1 4 2 NoteIn the first example: For the first assumption, there are two possible pairs of vertices from which apples can fall from the tree: (4, 4), (5, 4). For the second assumption there are also two pairs: (5, 4), (5, 5). For the third assumption there is only one pair: (4, 4). For the fourth assumption, there are 4 pairs: (4, 4), (4, 5), (5, 4), (5, 5). Tree from the first example. For the second example, there are 4 of possible pairs of vertices from which apples can fall: (2, 3), (2, 2), (3, 2), (3, 3). For the second assumption, there is only one possible pair: (2, 3). For the third assumption, there are two pairs: (3, 2), (3, 3).
251 23 45 33 243 45 14 41 331 21 331 12 33 1
2 2 1 4 4 1 2
4 seconds
512 megabytes
['combinatorics', 'dfs and similar', 'dp', 'math', 'trees', '*1200']
C. Sum in Binary Treetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputVanya really likes math. One day when he was solving another math problem, he came up with an interesting tree. This tree is built as follows.Initially, the tree has only one vertex with the number 1 — the root of the tree. Then, Vanya adds two children to it, assigning them consecutive numbers — 2 and 3, respectively. After that, he will add children to the vertices in increasing order of their numbers, starting from 2, assigning their children the minimum unused indices. As a result, Vanya will have an infinite tree with the root in the vertex 1, where each vertex will have exactly two children, and the vertex numbers will be arranged sequentially by layers. Part of Vanya's tree. Vanya wondered what the sum of the vertex numbers on the path from the vertex with number 1 to the vertex with number n in such a tree is equal to. Since Vanya doesn't like counting, he asked you to help him find this sum.InputThe first line contains a single integer t (1 \le t \le 10^4) — the number of test cases.This is followed by t lines — the description of the test cases. Each line contains one integer n (1 \le n \le 10^{16}) — the number of vertex for which Vanya wants to count the sum of vertex numbers on the path from the root to that vertex.OutputFor each test case, print one integer — the desired sum.ExampleInput 63103711000000000000000015Output 4 18 71 1 19999999999999980 26 NoteIn the first test case of example on the path from the root to the vertex 3 there are two vertices 1 and 3, their sum equals 4.In the second test case of example on the path from the root to the vertex with number 10 there are vertices 1, 2, 5, 10, sum of their numbers equals 1+2+5+10 = 18.
63103711000000000000000015
4 18 71 1 19999999999999980 26
1 second
256 megabytes
['bitmasks', 'combinatorics', 'math', 'trees', '*800']
B. Long Longtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputToday Alex was brought array a_1, a_2, \dots, a_n of length n. He can apply as many operations as he wants (including zero operations) to change the array elements.In 1 operation Alex can choose any l and r such that 1 \leq l \leq r \leq n, and multiply all elements of the array from l to r inclusive by -1. In other words, Alex can replace the subarray [a_l, a_{l + 1}, \dots, a_r] by [-a_l, -a_{l + 1}, \dots, -a_r] in 1 operation.For example, let n = 5, the array is [1, -2, 0, 3, -1], l = 2 and r = 4, then after the operation the array will be [1, 2, 0, -3, -1].Alex is late for school, so you should help him find the maximum possible sum of numbers in the array, which can be obtained by making any number of operations, as well as the minimum number of operations that must be done for this.InputThe first line contains a single integer t (1 \leq t \leq 10^4) — number of test cases. Then the descriptions of the test cases follow.The first line of each test case contains one integer n (1 \leq n \leq 2 \cdot 10^5) — length of the array.The second line contains n integers a_1, a_2, \dots, a_n (-10^9 \leq a_i \leq 10^9) — elements of the array.It is guaranteed that the sum of n for all test cases does not exceed 2 \cdot 10^5.OutputFor each test case output two space-separated numbers: the maximum possible sum of numbers in the array and the minimum number of operations to get this sum.Pay attention that an answer may not fit in a standard integer type, so do not forget to use 64-bit integer type.ExampleInput 56-1 7 -4 -2 5 -88-1 0 0 -2 1 0 -3 052 -1 0 -3 -750 -17 0 1 04-1 0 -2 -1Output 27 3 7 2 13 1 18 1 4 1 NoteBelow, for each test case, only one of the possible shortest sequences of operations is provided among many. There are others that have the same length and lead to the maximum sum of elements.In the first test case, Alex can make operations: (1, 4), (2, 2), (6, 6).In the second test case, to get the largest sum you need to make operations: (1, 8), (5, 6).In the fourth test case, it is necessary to make only one operation: (2, 3).
56-1 7 -4 -2 5 -88-1 0 0 -2 1 0 -3 052 -1 0 -3 -750 -17 0 1 04-1 0 -2 -1
27 3 7 2 13 1 18 1 4 1
2 seconds
256 megabytes
['greedy', 'math', 'two pointers', '*800']
A. Sasha and Array Coloringtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputSasha found an array a consisting of n integers and asked you to paint elements.You have to paint each element of the array. You can use as many colors as you want, but each element should be painted into exactly one color, and for each color, there should be at least one element of that color.The cost of one color is the value of \max(S) - \min(S), where S is the sequence of elements of that color. The cost of the whole coloring is the sum of costs over all colors.For example, suppose you have an array a = [\color{red}{1}, \color{red}{5}, \color{blue}{6}, \color{blue}{3}, \color{red}{4}], and you painted its elements into two colors as follows: elements on positions 1, 2 and 5 have color 1; elements on positions 3 and 4 have color 2. Then: the cost of the color 1 is \max([1, 5, 4]) - \min([1, 5, 4]) = 5 - 1 = 4; the cost of the color 2 is \max([6, 3]) - \min([6, 3]) = 6 - 3 = 3; the total cost of the coloring is 7. For the given array a, you have to calculate the maximum possible cost of the coloring.InputThe first line contains one integer t (1 \leq t \leq 1000) — the number of test cases.The first line of each test case contains a single integer n (1 \le n \le 50) — length of a.The second line contains n integers a_1, a_2, \dots, a_n (1 \leq a_i \leq 50) — array a.OutputFor each test case output the maximum possible cost of the coloring.ExampleInput 651 5 6 3 41541 6 3 961 13 9 3 7 242 2 2 254 5 2 2 3Output 7 0 11 23 0 5 NoteIn the first example one of the optimal coloring is [\color{red}{1}, \color{red}{5}, \color{blue}{6}, \color{blue}{3}, \color{red}{4}]. The answer is (5 - 1) + (6 - 3) = 7.In the second example, the only possible coloring is [\color{blue}{5}], for which the answer is 5 - 5 = 0.In the third example, the optimal coloring is [\color{blue}{1}, \color{red}{6}, \color{red}{3}, \color{blue}{9}], the answer is (9 - 1) + (6 - 3) = 11.
651 5 6 3 41541 6 3 961 13 9 3 7 242 2 2 254 5 2 2 3
7 0 11 23 0 5
1 second
256 megabytes
['greedy', 'sortings', 'two pointers', '*800']
I. Tenzing and Necklacetime limit per test2 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputbright, sunny and innocent......Tenzing has a beautiful necklace. The necklace consists of n pearls numbered from 1 to n with a string connecting pearls i and (i \text{ mod } n)+1 for all 1 \leq i \leq n.One day Tenzing wants to cut the necklace into several parts by cutting some strings. But for each connected part of the necklace, there should not be more than k pearls. The time needed to cut each string may not be the same. Tenzing needs to spend a_i minutes cutting the string between pearls i and (i \text{ mod } n)+1.Tenzing wants to know the minimum time in minutes to cut the necklace such that each connected part will not have more than k pearls.InputEach test contains multiple test cases. The first line of input contains a single integer t (1 \le t \le 10^5) — the number of test cases. The description of test cases follows.The first line of each test case contains two integers n and k (2\leq n\leq 5\cdot 10^5, 1\leq k <n).The second line of each test case contains n integers a_1,a_2,\ldots,a_n (1\leq a_i\leq 10^9).It is guaranteed that the sum of n of all test cases does not exceed 5 \cdot 10^5.OutputFor each test case, output the minimum total time in minutes required.ExampleInput 45 21 1 1 1 15 21 2 3 4 56 34 2 5 1 3 310 32 5 6 5 2 1 7 9 7 2Output 3 7 5 15 NoteIn the first test case, the necklace will be cut into 3 parts: [1,2][3,4][5], so the total time is 3.In the second test case, the necklace will be cut into 3 parts: [5,1][2][3,4], Tenzing will cut the strings connecting (1,2), (2,3) and (4,5), so the total time is a_1+a_2+a_4=7.
45 21 1 1 1 15 21 2 3 4 56 34 2 5 1 3 310 32 5 6 5 2 1 7 9 7 2
3 7 5 15
2 seconds
1024 megabytes
['divide and conquer', 'dp', 'greedy', '*3500']
H. Tenzing and Random Real Numberstime limit per test1 secondmemory limit per test1024 megabytesinputstandard inputoutputstandard outputThere are n uniform random real variables between 0 and 1, inclusive, which are denoted as x_1, x_2, \ldots, x_n.Tenzing has m conditions. Each condition has the form of x_i+x_j\le 1 or x_i+x_j\ge 1.Tenzing wants to know the probability that all the conditions are satisfied, modulo 998~244~353.Formally, let M = 998~244~353. It can be shown that the answer can be expressed as an irreducible fraction \frac{p}{q}, where p and q are integers and q \not \equiv 0 \pmod{M}. Output the integer equal to p \cdot q^{-1} \bmod M. In other words, output the integer x that 0 \le x < M and x \cdot q \equiv p \pmod{M}.InputThe first line contains two integers n and m (1\le n\le 20, 0\le m\le n^2+n).Then following m lines of input contains three integers t, i and j (t \in \{0,1\}, 1\le i\le j\le n). If t=0, the condition is x_i+x_j\le 1. If t=1, the condition is x_i+x_j\ge 1. It is guaranteed that all conditions are pairwise distinct.OutputOutput the probability that all the conditions are satisfied, modulo M = 998~244~353.ExamplesInput 3 2 0 1 2 1 3 3 Output 748683265 Input 3 3 0 1 2 0 1 3 0 2 3 Output 748683265 Input 3 4 0 1 2 0 1 3 1 2 3 1 2 2 Output 935854081 Input 4 4 0 1 2 0 3 4 1 1 3 1 2 4 Output 0 Input 8 12 0 1 2 0 2 3 1 3 4 0 1 4 0 5 6 0 6 7 1 7 8 0 5 8 1 3 7 1 3 8 1 4 7 1 4 8 Output 997687297 NoteIn the first test case, the conditions are x_1+x_2 \le 1 and x_3+x_3\ge 1, and the probability that each condition is satisfied is \frac 12, so the probability that they are both satisfied is \frac 12\cdot \frac 12=\frac 14, modulo 998~244~353 is equal to 748683265.In the second test case, the answer is \frac 14.In the third test case, the answer is \frac 1{16}.In the fourth test case, the sum of all variables must equal 2, so the probability is 0.
3 2 0 1 2 1 3 3
748683265
1 second
1024 megabytes
['bitmasks', 'dp', 'graphs', 'math', 'probabilities', '*3000']
G. Tenzing and Random Operationstime limit per test2 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputYet another random problem.Tenzing has an array a of length n and an integer v.Tenzing will perform the following operation m times: Choose an integer i such that 1 \leq i \leq n uniformly at random. For all j such that i \leq j \leq n, set a_j := a_j + v. Tenzing wants to know the expected value of \prod_{i=1}^n a_i after performing the m operations, modulo 10^9+7.Formally, let M = 10^9+7. It can be shown that the answer can be expressed as an irreducible fraction \frac{p}{q}, where p and q are integers and q \not \equiv 0 \pmod{M}. Output the integer equal to p \cdot q^{-1} \bmod M. In other words, output the integer x that 0 \le x < M and x \cdot q \equiv p \pmod{M}.InputThe first line of input contains three integers n, m and v (1\leq n\leq 5000, 1\leq m,v\leq 10^9).The second line of input contains n integers a_1,a_2,\ldots,a_n (1\leq a_i\leq 10^9).OutputOutput the expected value of \prod_{i=1}^n a_i modulo 10^9+7.ExamplesInput 2 2 5 2 2 Output 84 Input 5 7 9 9 9 8 2 4 Output 975544726 NoteThere are three types of a after performing all the m operations : 1. a_1=2,a_2=12 with \frac{1}{4} probability.2. a_1=a_2=12 with \frac{1}{4} probability.3. a_1=7,a_2=12 with \frac{1}{2} probability.So the expected value of a_1\cdot a_2 is \frac{1}{4}\cdot (24+144) + \frac{1}{2}\cdot 84=84.
2 2 5 2 2
84
2 seconds
1024 megabytes
['combinatorics', 'dp', 'math', 'probabilities', '*2800']
F. Tenzing and Treetime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputTenzing has an undirected tree of n vertices.Define the value of a tree with black and white vertices in the following way. The value of an edge is the absolute difference between the number of black nodes in the two components of the tree after deleting the edge. The value of the tree is the sum of values over all edges.For all k such that 0 \leq k \leq n, Tenzing wants to know the maximum value of the tree when k vertices are painted black and n-k vertices are painted white.InputThe first line of the input contains a single integer n (1\leq n\leq 5000) — the number of vertices.The following n-1 lines of the input contains 2 integers u_i and v_i (1 \leq u_i, v_i \leq n, u_i \neq v_i) — indicating an edge between vertices u_i and v_i. It is guaranteed that the given edges form a tree.OutputOutput n+1 numbers. The i-th number is the answer of k=i-1.ExamplesInput 4 1 2 3 2 2 4 Output 0 3 4 5 6 Input 1 Output 0 0 NoteConsider the first example. When k=2, Tenzing can paint vertices 1 and 2 black then the value of edge (1,2) is 0, and the values of other edges are all equal to 2. So the value of that tree is 4.
4 1 2 3 2 2 4
0 3 4 5 6
2 seconds
512 megabytes
['dfs and similar', 'greedy', 'shortest paths', 'sortings', 'trees', '*2500']
E. Tenzing and Triangletime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n pairwise-distinct points and a line x+y=k on a two-dimensional plane. The i-th point is at (x_i,y_i). All points have non-negative coordinates and are strictly below the line. Alternatively, 0 \leq x_i,y_i, x_i+y_i < k.Tenzing wants to erase all the points. He can perform the following two operations: Draw triangle: Tenzing will choose two non-negative integers a, b that satisfy a+b<k, then all points inside the triangle formed by lines x=a, y=b and x+y=k will be erased. It can be shown that this triangle is an isosceles right triangle. Let the side lengths of the triangle be l, l and \sqrt 2 l respectively. Then, the cost of this operation is l \cdot A.The blue area of the following picture describes the triangle with a=1,b=1 with cost =1\cdot A. Erase a specific point: Tenzing will choose an integer i that satisfies 1 \leq i \leq n and erase the point i. The cost of this operation is c_i.Help Tenzing find the minimum cost to erase all of the points.InputThe first line of the input contains three integers n, k and A (1\leq n,k\leq 2\cdot 10^5, 1\leq A\leq 10^4) — the number of points, the coefficient describing the hypotenuse of the triangle and the coefficient describing the cost of drawing a triangle.The following n lines of the input the i-th line contains three integers x_i,y_i,c_i (0\leq x_i,y_i,x_i+y_i< k, 1\leq c_i\leq 10^4) — the coordinate of the i-th points and the cost of erasing it using the second operation. It is guaranteed that the coordinates are pairwise distinct.OutputOutput a single integer —the minimum cost needed to erase all of the points.ExamplesInput 4 6 1 1 2 1 2 1 1 1 1 1 3 2 6 Output 4 Input 6 7 1 4 2 1 3 3 1 5 1 4 3 2 5 4 1 1 0 6 4 Output 4 Input 10 4 100 0 0 1 0 1 1 0 2 50 0 3 200 1 0 1 1 1 1 1 2 1 2 0 200 2 1 200 3 0 200 Output 355 NoteThe picture of the first example:Tenzing do the following operations: draw a triangle with a=3,b=2, the cost =1\cdot A=1. erase the first point, the cost =1. erase the second point, the cost =1. erase the third point, the cost =1. The picture of the second example:
4 6 1 1 2 1 2 1 1 1 1 1 3 2 6
4
2 seconds
256 megabytes
['data structures', 'dp', 'geometry', 'greedy', 'math', '*2300']
D. Tenzing and His Animal Friends time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputTell a story about me and my animal friends.Tenzing has n animal friends. He numbers them from 1 to n.One day Tenzing wants to play with his animal friends. To do so, Tenzing will host several games.In one game, he will choose a set S which is a subset of \{1,2,3,...,n\} and choose an integer t. Then, he will play the game with the animals in S for t minutes.But there are some restrictions: Tenzing loves friend 1 very much, so 1 must be an element of S. Tenzing doesn't like friend n, so n must not be an element of S. There are m additional restrictions. The i-th special restriction is described by integers u_i, v_i and y_i, suppose x is the total time that exactly one of u_i and v_i is playing with Tenzing. Tenzing must ensure that x is less or equal to y_i. Otherwise, there will be unhappiness. Tenzing wants to know the maximum total time that he can play with his animal friends. Please find out the maximum total time that Tenzing can play with his animal friends and a way to organize the games that achieves this maximum total time, or report that he can play with his animal friends for an infinite amount of time. Also, Tenzing does not want to host so many games, so he will host at most n^2 games.InputThe first line of input contains two integers n and m (2 \leq n \leq 100, 0 \leq m \leq \frac{n(n-1)}{2}) — the number of animal friends and the number of special restrictions.The i-th of the following m lines of input contains three integers u_i, v_i and y_i (1\leq u_i<v_i\leq n, 0\leq y_i\leq 10^9) — describing the i-th special restriction. It is guaranteed that for 1 \leq i < j \leq m, (u_i,v_i) \neq (u_j,v_j).OutputIf Tenzing can play with his animal friends for an infinite amount of time, output "inf". (Output without quotes.)Otherwise, in the first line, output the total time T (0 \leq t \leq 10^{18}) and the number of games k (0 \leq k \leq n^2).In the following k lines of output, output a binary string s of length n and an integer t (0 \leq t \leq 10^{18}) — representing the set S and the number of minutes this game will be played. If s_i=\texttt{1}, then i \in S, otherwise if s_i=\texttt{0}, then i \notin S.Under the constraints of this problem, it can be proven that if Tenzing can only play with his friends for a finite amount of time, then he can only play with them for at most 10^{18} minutes.ExamplesInput 5 4 1 3 2 1 4 2 2 3 1 2 5 1 Output 4 4 10000 1 10010 1 10100 1 11110 1 Input 3 0 Output inf NoteIn the first test case: Tenzing will host a game with friend \{1\} for 1 minute. Tenzing will host a game with friends \{1,4\} for 1 minute. Tenzing will host a game with friends \{1,3\} for 1 minute. Tenzing will host a game with friends \{1,2,3,4\} for 1 minute. If after that, Tenzing host another game with friends \{1,2\} for 1 minute. Then the time of exactly one of friends 2 or 3 with Tenzing will becomes 2 minutes which will not satisfy the 3-rd special restriction.In the second test case, there is no special restrictions. So Tenzing can host a game with friend \{1\} for an infinite amount of time.
5 4 1 3 2 1 4 2 2 3 1 2 5 1
4 4 10000 1 10010 1 10100 1 11110 1
1 second
256 megabytes
['constructive algorithms', 'graphs', 'greedy', '*1900']
C. Tenzing and Ballstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputEnjoy erasing Tenzing, identified as Accepted!Tenzing has n balls arranged in a line. The color of the i-th ball from the left is a_i.Tenzing can do the following operation any number of times: select i and j such that 1\leq i < j \leq |a| and a_i=a_j, remove a_i,a_{i+1},\ldots,a_j from the array (and decrease the indices of all elements to the right of a_j by j-i+1). Tenzing wants to know the maximum number of balls he can remove.InputEach test contains multiple test cases. The first line of input contains a single integer t (1\leq t\leq 10^3) — the number of test cases. The description of test cases follows.The first line contains a single integer n (1\leq n\leq 2\cdot 10^5) — the number of balls.The second line contains n integers a_1,a_2,\ldots,a_n (1\leq a_i \leq n) — the color of the balls.It is guaranteed that sum of n of all test cases will not exceed 2\cdot 10^5.OutputFor each test case, output the maximum number of balls Tenzing can remove.ExampleInput 251 2 2 3 341 2 1 2Output 4 3 NoteIn the first example, Tenzing will choose i=2 and j=3 in the first operation so that a=[1,3,3]. Then Tenzing will choose i=2 and j=3 again in the second operation so that a=[1]. So Tenzing can remove 4 balls in total.In the second example, Tenzing will choose i=1 and j=3 in the first and only operation so that a=[2]. So Tenzing can remove 3 balls in total.
251 2 2 3 341 2 1 2
4 3
1 second
256 megabytes
['dp', '*1500']
B. Tenzing and Bookstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputTenzing received 3n books from his fans. The books are arranged in 3 stacks with n books in each stack. Each book has a non-negative integer difficulty rating.Tenzing wants to read some (possibly zero) books. At first, his knowledge is 0.To read the books, Tenzing will choose a non-empty stack, read the book on the top of the stack, and then discard the book. If Tenzing's knowledge is currently u, then his knowledge will become u|v after reading a book with difficulty rating v. Here | denotes the bitwise OR operation. Note that Tenzing can stop reading books whenever he wants.Tenzing's favourite number is x. Can you help Tenzing check if it is possible for his knowledge to become x?InputEach test contains multiple test cases. The first line of input contains a single integer t (1 \le t \le 10^4) — the number of test cases. The description of test cases follows.The first line of each test case contains two integers n and x (1 \leq n \leq 10^5, 0 \leq x \leq 10^9) — the number of books in each stack and Tenzing's favourite number.The second line of each test case contains n integers a_1,a_2,\ldots,a_n (0 \leq a_i \leq 10^9)  — the difficulty rating of the books in the first stack, from top to bottom.The third line of each test case contains n integers b_1,b_2,\ldots,b_n (0 \leq b_i \leq 10^9)  — the difficulty rating of the books in the second stack, from top to bottom.The fourth line of each test case contains n integers c_1,c_2,\ldots,c_n (0 \leq c_i \leq 10^9)  — the difficulty rating of the books in the third stack, from top to bottom.It is guaranteed that the sum of n does not exceed 10^5.OutputFor each test case, output "Yes" (without quotes) if Tenzing can make his knowledge equal to x, and "No" (without quotes) otherwise.You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.ExampleInput 35 71 2 3 4 55 4 3 2 11 3 5 7 95 23 2 3 4 55 4 3 2 13 3 5 7 93 01 2 33 2 12 2 2Output Yes No Yes NoteFor the first test case, Tenzing can read the following 4 books: read the book with difficulty rating 1 on the top of the first stack. Tenzing's knowledge changes to 0|1=1. read the book with difficulty rating 1 on the top of the third stack. Tenzing's knowledge changes to 1|1=1. read the book with difficulty rating 2 on the top of the first stack. Tenzing's knowledge changes to 1|2=3. read the book with difficulty rating 5 on the top of the second stack. Tenzing's knowledge changes to 3|5=7. After reading all books, Tenzing's knowledge is 7.For the third test case, Tenzing can read 0 books to make his final knowledge equals to 0.
35 71 2 3 4 55 4 3 2 11 3 5 7 95 23 2 3 4 55 4 3 2 13 3 5 7 93 01 2 33 2 12 2 2
Yes No Yes
1 second
256 megabytes
['bitmasks', 'greedy', 'math', '*1100']
A. Tenzing and Tsondutime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputTsondu always runs first! ! !Tsondu and Tenzing are playing a card game. Tsondu has n monsters with ability values a_1, a_2, \ldots, a_n while Tenzing has m monsters with ability values b_1, b_2, \ldots, b_m.Tsondu and Tenzing take turns making moves, with Tsondu going first. In each move, the current player chooses two monsters: one on their side and one on the other side. Then, these monsters will fight each other. Suppose the ability values for the chosen monsters are x and y respectively, then the ability values of the monsters will become x-y and y-x respectively. If the ability value of any monster is smaller than or equal to 0, the monster dies.The game ends when at least one player has no monsters left alive. The winner is the player with at least one monster left alive. If both players have no monsters left alive, the game ends in a draw.Find the result of the game when both players play optimally.InputEach test contains multiple test cases. The first line of input contains a single integer t (1 \le t \le 2 \cdot 10^3) — the number of test cases. The description of test cases follows.The first line of each test case contains two integers n and m (1 \leq n,m \leq 50) — the number of monsters Tsondu and Tenzing have respectively.The second line of each test case contains n integers a_1,a_2,\ldots,a_n (1 \leq a_i \leq 10^9) — the ability values of Tsondu's monsters. The third line of each test case contains m integers b_1,b_2,\ldots,b_m (1 \leq b_i \leq 10^9) — the ability values of Tenzing's monsters. OutputFor each test case, output "Tsondu" if Tsondu wins, "Tenzing" if Tenzing wins, and "Draw" if the game ends in a draw. (Output without quotes.)Note that the output is case-sensitive. For example, if the answer is "Tsondu", the outputs "tsondu", "TSONDU", and "tSonDu" will all be recognized as incorrect outputs.ExampleInput 61 391 2 32 31 21 1 13 21 2 31 13 31 1 12 2 210 101 2 3 3 2 2 1 1 2 23 3 3 3 2 1 1 1 1 110 101 2 3 4 5 6 7 8 9 106 7 8 9 10 11 1 1 1 1Output Tsondu Draw Tsondu Tenzing Draw Draw NoteConsider the first test case. It can be shown that Tsondu has a winning strategy. The following is a possible way that Tsondu can win (note that the players may not be playing optimally in this example): In the first move, Tsondu chooses a monster with ability value 9 on his side to fight against a monster with ability value 1 on Tenzing's side, the ability value of both monsters become 8 and -8 respectively. The monster with ability value -8 on Tenzing's side dies. In the second move, Tenzing chooses a monster with ability value 2 on his side to fight against a monster with ability value 8 on Tsondu's side, the ability value of both monsters become -6 and 6 respectively. The monster with ability value -6 on Tenzing's side dies. In the third move, Tsondu chooses a monster with ability value 6 on his side to fight against a monster with ability value 3 onTenzing's side, the ability value of both monsters become 3 and -3 respectively. The monster with ability value -3 on Tenzing's side dies. Now, Tenzing has no monsters left alive. Since Tsondu still has monsters left alive, Tsondu wins.
61 391 2 32 31 21 1 13 21 2 31 13 31 1 12 2 210 101 2 3 3 2 2 1 1 2 23 3 3 3 2 1 1 1 1 110 101 2 3 4 5 6 7 8 9 106 7 8 9 10 11 1 1 1 1
Tsondu Draw Tsondu Tenzing Draw Draw
1 second
256 megabytes
['games', 'math', '*800']
F. Monocarp and a Strategic Gametime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputMonocarp plays a strategic computer game in which he develops a city. The city is inhabited by creatures of four different races — humans, elves, orcs, and dwarves.Each inhabitant of the city has a happiness value, which is an integer. It depends on how many creatures of different races inhabit the city. Specifically, the happiness of each inhabitant is 0 by default; it increases by 1 for each other creature of the same race and decreases by 1 for each creature of a hostile race. Humans are hostile to orcs (and vice versa), and elves are hostile to dwarves (and vice versa).At the beginning of the game, Monocarp's city is not inhabited by anyone. During the game, n groups of creatures will come to his city, wishing to settle there. The i-th group consists of a_i humans, b_i orcs, c_i elves, and d_i dwarves. Each time, Monocarp can either accept the entire group of creatures into the city, or reject the entire group.The game calculates Monocarp's score according to the following formula: m + k, where m is the number of inhabitants in the city, and k is the sum of the happiness values of all creatures in the city.Help Monocarp earn the maximum possible number of points by the end of the game!InputThe first line contains an integer n (1 \leq n \leq 3 \cdot 10^{5}) — the number of groups of creatures that come to Monocarp's city.Then n lines follow. The i-th of them contains four integers a_i, b_i, c_i, and d_i (0 \leq a_i, b_i, c_i, d_i \leq 10^{9}) — the number of humans, orcs, elves and dwarves (respectively) in the i-th group.OutputOutput a single number — the maximum score Monocarp can have by the end of the game. Your answer will be considered correct if its absolute or relative error does not exceed 10^{-9}. That is, if your answer is a, and the jury's answer is b, then the solution will be accepted if \frac{|a-b|}{\max(1,|b|)} \le 10^{-9}.Note that the correct answer is always an integer, but sometimes it doesn't fit in 64-bit integer types, so you are allowed to print it as a non-integer number.ExamplesInput 5 0 0 1 0 1 3 4 2 2 5 1 2 4 5 4 3 1 4 4 5 Output 85 Input 4 3 3 1 5 5 1 5 3 4 4 4 1 1 3 4 4 Output 41 NoteIn the first example, the best course of action is to accept all of the groups.In the second example, the best course of action is to accept the groups 2 and 3, and decline the groups 1 and 4.
5 0 0 1 0 1 3 4 2 2 5 1 2 4 5 4 3 1 4 4 5
85
1 second
256 megabytes
['geometry', 'sortings', 'two pointers', '*2700']
E. Fill the Matrixtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a square matrix, consisting of n rows and n columns of cells, both numbered from 1 to n. The cells are colored white or black. Cells from 1 to a_i are black, and cells from a_i+1 to n are white, in the i-th column.You want to place m integers in the matrix, from 1 to m. There are two rules: each cell should contain at most one integer; black cells should not contain integers. The beauty of the matrix is the number of such j that j+1 is written in the same row, in the next column as j (in the neighbouring cell to the right).What's the maximum possible beauty of the matrix?InputThe first line contains a single integer t (1 \le t \le 10^4) — the number of testcases.The first line of each testcase contains a single integer n (1 \le n \le 2 \cdot 10^5) — the size of the matrix.The second line contains n integers a_1, a_2, \dots, a_n (0 \le a_i \le n) — the number of black cells in each column.The third line contains a single integer m (0 \le m \le \sum \limits_{i=1}^n n - a_i) — the number of integers you have to write in the matrix. Note that this number might not fit into a 32-bit integer data type.The sum of n over all testcases doesn't exceed 2 \cdot 10^5.OutputFor each testcase, print a single integer — the maximum beauty of the matrix after you write all m integers in it. Note that there are no more integers than the white cells, so the answer always exists.ExampleInput 630 0 0942 0 3 1542 0 3 1642 0 3 110100 2 2 1 5 10 3 4 1 120110Output 6 3 4 4 16 0
630 0 0942 0 3 1542 0 3 1642 0 3 110100 2 2 1 5 10 3 4 1 120110
6 3 4 4 16 0
2 seconds
256 megabytes
['data structures', 'greedy', 'math', '*2200']
D. Pairs of Segmentstime limit per test4 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputTwo segments [l_1, r_1] and [l_2, r_2] intersect if there exists at least one x such that l_1 \le x \le r_1 and l_2 \le x \le r_2.An array of segments [[l_1, r_1], [l_2, r_2], \dots, [l_k, r_k]] is called beautiful if k is even, and is possible to split the elements of this array into \frac{k}{2} pairs in such a way that: every element of the array belongs to exactly one of the pairs; segments in each pair intersect with each other; segments in different pairs do not intersect. For example, the array [[2, 4], [9, 12], [2, 4], [7, 7], [10, 13], [6, 8]] is beautiful, since it is possible to form 3 pairs as follows: the first element of the array (segment [2, 4]) and the third element of the array (segment [2, 4]); the second element of the array (segment [9, 12]) and the fifth element of the array (segment [10, 13]); the fourth element of the array (segment [7, 7]) and the sixth element of the array (segment [6, 8]). As you can see, the segments in each pair intersect, and no segments from different pairs intersect.You are given an array of n segments [[l_1, r_1], [l_2, r_2], \dots, [l_n, r_n]]. You have to remove the minimum possible number of elements from this array so that the resulting array is beautiful.InputThe first line contains one integer t (1 \le t \le 1000) — the number of test cases.The first line of each test case contains one integer n (2 \le n \le 2000) — the number of segments in the array. Then, n lines follow, the i-th of them contains two integers l_i and r_i (0 \le l_i \le r_i \le 10^9) denoting the i-th segment.Additional constraint on the input: the sum of n over all test cases does not exceed 2000.OutputFor each test case, print one integer — the minimum number of elements you have to remove so that the resulting array is beautiful.ExampleInput 372 49 122 47 74 810 136 852 22 80 101 25 641 12 23 34 4Output 1 3 4 NoteIn the first test case of the example, it is enough to delete the 5-th element of the array of segments. Then you get the array [[2, 4], [9, 12], [2, 4], [7, 7], [10, 13], [6, 8]], which is beautiful.In the second test case of the example, you can delete the 1-st, 3-rd and 4-th element of the array. Then you get the array [[2, 8], [5, 6]], which is beautiful.In the third test case of the example, you have to delete the whole array.
372 49 122 47 74 810 136 852 22 80 101 25 641 12 23 34 4
1 3 4
4 seconds
512 megabytes
['data structures', 'greedy', 'sortings', 'two pointers', '*2000']
C. Ranom Numberstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputNo, not "random" numbers.Ranom digits are denoted by uppercase Latin letters from A to E. Moreover, the value of the letter A is 1, B is 10, C is 100, D is 1000, E is 10000.A Ranom number is a sequence of Ranom digits. The value of the Ranom number is calculated as follows: the values of all digits are summed up, but some digits are taken with negative signs: a digit is taken with negative sign if there is a digit with a strictly greater value to the right of it (not necessarily immediately after it); otherwise, that digit is taken with a positive sign.For example, the value of the Ranom number DAAABDCA is 1000 - 1 - 1 - 1 - 10 + 1000 + 100 + 1 = 2088.You are given a Ranom number. You can change no more than one digit in it. Calculate the maximum possible value of the resulting number.InputThe first line contains a single integer t (1 \le t \le 10^4) — the number of test cases.The only line of each test case contains a string s (1 \le |s| \le 2 \cdot 10^5) consisting of uppercase Latin letters from A to E — the Ranom number you are given.The sum of the string lengths over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, print a single integer — the maximum possible value of the number, if you can change no more than one digit in it.ExampleInput 4DAAABDCAABABCDEEDCBADDDDAAADDABECDOutput 11088 10010 31000 15886 NoteIn the first example, you can get EAAABDCA with the value 10000-1-1-1-10+1000+100+1=11088.In the second example, you can get EB with the value 10000+10=10010.
4DAAABDCAABABCDEEDCBADDDDAAADDABECD
11088 10010 31000 15886
2 seconds
256 megabytes
['brute force', 'dp', 'greedy', 'math', 'strings', '*1800']
B. Keep it Beautifultime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThe array [a_1, a_2, \dots, a_k] is called beautiful if it is possible to remove several (maybe zero) elements from the beginning of the array and insert all these elements to the back of the array in the same order in such a way that the resulting array is sorted in non-descending order.In other words, the array [a_1, a_2, \dots, a_k] is beautiful if there exists an integer i \in [0, k-1] such that the array [a_{i+1}, a_{i+2}, \dots, a_{k-1}, a_k, a_1, a_2, \dots, a_i] is sorted in non-descending order.For example: [3, 7, 7, 9, 2, 3] is beautiful: we can remove four first elements and insert them to the back in the same order, and we get the array [2, 3, 3, 7, 7, 9], which is sorted in non-descending order; [1, 2, 3, 4, 5] is beautiful: we can remove zero first elements and insert them to the back, and we get the array [1, 2, 3, 4, 5], which is sorted in non-descending order; [5, 2, 2, 1] is not beautiful. Note that any array consisting of zero elements or one element is beautiful.You are given an array a, which is initially empty. You have to process q queries to it. During the i-th query, you will be given one integer x_i, and you have to do the following: if you can append the integer x_i to the back of the array a so that the array a stays beautiful, you have to append it; otherwise, do nothing. After each query, report whether you appended the given integer x_i, or not.InputThe first line contains one integer t (1 \le t \le 10^4) — the number of test cases.Each test case consists of two lines. The first line contains one integer q (1 \le q \le 2 \cdot 10^5) — the number of queries. The second line contains q integers x_1, x_2, \dots, x_q (0 \le x_i \le 10^9).Additional constraint on the input: the sum of q over all test cases does not exceed 2 \cdot 10^5).OutputFor each test case, print one string consisting of exactly q characters. The i-th character of the string should be 1 if you appended the integer during the i-th query; otherwise, it should be 0.ExampleInput 393 7 7 9 2 4 6 3 451 1 1 1 153 2 1 2 3Output 111110010 11111 11011 NoteConsider the first test case of the example. Initially, the array is []. trying to append an integer 3. The array [3] is beautiful, so we append 3; trying to append an integer 7. The array [3, 7] is beautiful, so we append 7; trying to append an integer 7. The array [3, 7, 7] is beautiful, so we append 7; trying to append an integer 9. The array [3, 7, 7, 9] is beautiful, so we append 9; trying to append an integer 2. The array [3, 7, 7, 9, 2] is beautiful, so we append 2; trying to append an integer 4. The array [3, 7, 7, 9, 2, 4] is not beautiful, so we don't append 4; trying to append an integer 6. The array [3, 7, 7, 9, 2, 6] is not beautiful, so we don't append 6; trying to append an integer 3. The array [3, 7, 7, 9, 2, 3] is beautiful, so we append 3; trying to append an integer 4. The array [3, 7, 7, 9, 2, 3, 4] is not beautiful, so we don't append 4.
393 7 7 9 2 4 6 3 451 1 1 1 153 2 1 2 3
111110010 11111 11011
2 seconds
512 megabytes
['implementation', '*1000']
A. Game with Boardtime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputAlice and Bob play a game. They have a blackboard; initially, there are n integers written on it, and each integer is equal to 1.Alice and Bob take turns; Alice goes first. On their turn, the player has to choose several (at least two) equal integers on the board, wipe them and write a new integer which is equal to their sum.For example, if the board currently contains integers \{1, 1, 2, 2, 2, 3\}, then the following moves are possible: choose two integers equal to 1, wipe them and write an integer 2, then the board becomes \{2, 2, 2, 2, 3\}; choose two integers equal to 2, wipe them and write an integer 4, then the board becomes \{1, 1, 2, 3, 4\}; choose three integers equal to 2, wipe them and write an integer 6, then the board becomes \{1, 1, 3, 6\}. If a player cannot make a move (all integers on the board are different), that player wins the game.Determine who wins if both players play optimally.InputThe first line contains one integer t (1 \le t \le 99) — the number of test cases.Each test case consists of one line containing one integer n (2 \le n \le 100) — the number of integers equal to 1 on the board.OutputFor each test case, print Alice if Alice wins when both players play optimally. Otherwise, print Bob.ExampleInput 236Output Bob Alice NoteIn the first test case, n = 3, so the board initially contains integers \{1, 1, 1\}. We can show that Bob can always win as follows: there are two possible first moves for Alice. if Alice chooses two integers equal to 1, wipes them and writes 2, the board becomes \{1, 2\}. Bob cannot make a move, so he wins; if Alice chooses three integers equal to 1, wipes them and writes 3, the board becomes \{3\}. Bob cannot make a move, so he wins. In the second test case, n = 6, so the board initially contains integers \{1, 1, 1, 1, 1, 1\}. Alice can win by, for example, choosing two integers equal to 1, wiping them and writing 2 on the first turn. Then the board becomes \{1, 1, 1, 1, 2\}, and there are three possible responses for Bob: if Bob chooses four integers equal to 1, wipes them and writes 4, the board becomes \{2,4\}. Alice cannot make a move, so she wins; if Bob chooses three integers equal to 1, wipes them and writes 3, the board becomes \{1,2,3\}. Alice cannot make a move, so she wins; if Bob chooses two integers equal to 1, wipes them and writes 2, the board becomes \{1, 1, 2, 2\}. Alice can continue by, for example, choosing two integers equal to 2, wiping them and writing 4. Then the board becomes \{1,1,4\}. The only possible response for Bob is to choose two integers equal to 1 and write 2 instead of them; then the board becomes \{2,4\}, Alice cannot make a move, so she wins.
236
Bob Alice
2 seconds
512 megabytes
['constructive algorithms', 'games', '*800']
G2. In Search of Truth (Hard Version)time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe only difference between easy and hard versions is the maximum number of queries. In this version, you are allowed to ask at most 1000 queries.This is an interactive problem.You are playing a game. The circle is divided into n sectors, sectors are numbered from 1 to n in some order. You are in the adjacent room and do not know either the number of sectors or their numbers. There is also an arrow that initially points to some sector. Initially, the host tells you the number of the sector to which the arrow points. After that, you can ask the host to move the arrow k sectors counterclockwise or clockwise at most 1000 times. And each time you are told the number of the sector to which the arrow points.Your task is to determine the integer n — the number of sectors in at most 1000 queries.It is guaranteed that 1 \le n \le 10^6.InputThe input consists of a single integer x (1 \le x \le n) — the number of the initial sector.OutputAfter you determine the integer n — the number of sectors, you should output "! n" (1 \le n \le 10^6). After that the program should immediately terminate.Note that, printing the answer does not count as a query.It is guaranteed that the integer n and the numbers of the sectors are fixed initially and will not be changed by the jury program depending on the queries.InteractionAfter reading the description of the input, you may ask queries. Queries can be of two types: "+ k" (0 \le k \le 10^9) — ask to move the arrow k sectors clockwise. "- k" (0 \le k \le 10^9) — ask to move the arrow k sectors counterclockwise. After each query, you should read an integer x (1 \le x \le n) — the number of the current sector to which the arrow points.You are allowed to make at most 1000 queries in total.If you make too many queries, you will get Wrong answer.After printing a query or the answer, do not forget to output a the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see the documentation for other languages. ExampleInput 1 5 6 7 2 10 9 8 4 3 1Output + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 ! 10NoteHacksTo hack, use the following test format.In the first line, output a single integer n (1 \le n \le 10^6) — the number of sectors.In the second line, output n different integers 1 \le a_1, a_2, \dots, a_n \le n — the numbers of the sectors in clockwise order, the arrow initially points to the sector with the number a_1.
1 5 6 7 2 10 9 8 4 3 1
+ 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 ! 10
2 seconds
256 megabytes
['constructive algorithms', 'interactive', 'math', 'meet-in-the-middle', 'probabilities', '*2500']
G1. In Search of Truth (Easy Version)time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe only difference between easy and hard versions is the maximum number of queries. In this version, you are allowed to ask at most 2023 queries.This is an interactive problem.You are playing a game. The circle is divided into n sectors, sectors are numbered from 1 to n in some order. You are in the adjacent room and do not know either the number of sectors or their numbers. There is also an arrow that initially points to some sector. Initially, the host tells you the number of the sector to which the arrow points. After that, you can ask the host to move the arrow k sectors counterclockwise or clockwise at most 2023 times. And each time you are told the number of the sector to which the arrow points.Your task is to determine the integer n — the number of sectors in at most 2023 queries.It is guaranteed that 1 \le n \le 10^6.InputThe input consists of a single integer x (1 \le x \le n) — the number of the initial sector.OutputAfter you determine the integer n — the number of sectors, you should output "! n" (1 \le n \le 10^6). After that the program should immediately terminate.Note that, printing the answer does not count as a query.It is guaranteed that the integer n and the numbers of the sectors are fixed initially and will not be changed by the jury program depending on the queries.InteractionAfter reading the description of the input, you may ask queries. Queries can be of two types: "+ k" (0 \le k \le 10^9) — ask to move the arrow k sectors clockwise. "- k" (0 \le k \le 10^9) — ask to move the arrow k sectors counterclockwise. After each query, you should read an integer x (1 \le x \le n) — the number of the current sector to which the arrow points.You are allowed to make at most 2023 queries in total.If you make too many queries, you will get Wrong answer.After printing a query or the answer, do not forget to output a the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see the documentation for other languages. ExampleInput 1 5 6 7 2 10 9 8 4 3 1Output + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 ! 10NoteHacksTo hack, use the following test format.In the first line, output a single integer n (1 \le n \le 10^6) — the number of sectors.In the second line, output n different integers 1 \le a_1, a_2, \dots, a_n \le n — the numbers of the sectors in clockwise order, the arrow initially points to the sector with the number a_1.
1 5 6 7 2 10 9 8 4 3 1
+ 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 ! 10
2 seconds
256 megabytes
['constructive algorithms', 'interactive', 'math', 'meet-in-the-middle', 'probabilities', '*2200']
F. Railgunstime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputTema is playing a very interesting computer game.During the next mission, Tema's character found himself on an unfamiliar planet. Unlike Earth, this planet is flat and can be represented as an n \times m rectangle.Tema's character is located at the point with coordinates (0, 0). In order to successfully complete the mission, he needs to reach the point with coordinates (n, m) alive.Let the character of the computer game be located at the coordinate (i, j). Every second, starting from the first, Tema can: either use vertical hyperjump technology, after which his character will end up at coordinate (i + 1, j) at the end of the second; or use horizontal hyperjump technology, after which his character will end up at coordinate (i, j + 1) at the end of the second; or Tema can choose not to make a hyperjump, in which case his character will not move during this second; The aliens that inhabit this planet are very dangerous and hostile. Therefore, they will shoot from their railguns r times.Each shot completely penetrates one coordinate vertically or horizontally. If the character is in the line of its impact at the time of the shot (at the end of the second), he dies.Since Tema looked at the game's source code, he knows complete information about each shot — the time, the penetrated coordinate, and the direction of the shot.What is the minimum time for the character to reach the desired point? If he is doomed to die and cannot reach the point with coordinates (n, m), output -1.InputThe first line of the input contains a single integer T (1 \le T \le 10^4) — the number of test cases.Then follow the descriptions of the test cases.The first line of each test case contains two integers n and m (1 \le n \cdot m \le 10^4) — the size of the planet, its height and width.The second line of each test case contains a single integer r (1 \le r \le 100) — the number of shots.Then follow r lines, each describing one shot.A shot is described by three integers t, d, coord. Where t is the second at which the shot will be fired (1 \le t \le 10^9). d is the direction of the shot (d = 1 denotes a horizontal shot, d = 2 denotes a vertical shot). coord is the size of the penetrated coordinate (0 \le coord \le n for d = 1, 0 \le coord \le m for d = 2).The sum of the products n \cdot m over all test cases does not exceed 10^4.OutputFor each test case, output a single number — the minimum time for the character to reach the coordinate (n, m), or -1 if he is doomed to die.ExampleInput 51 341 2 02 2 13 2 24 1 13 362 1 02 1 12 1 22 2 02 2 12 2 22 137 1 22 1 17 2 12 259 1 23 2 05 1 24 2 27 1 04 676 1 212 1 34 1 017 2 31 2 616 2 63 2 4Output 5 -1 3 6 10 NoteIn the first test case, the character can move as follows: (0, 0) \rightarrow (0, 1) \rightarrow (0, 2) \rightarrow (0, 3) \rightarrow (0, 3) \rightarrow (1, 3).In the second test case, the character will not be able to leave the rectangle that will be completely penetrated by shots at the second 2.
51 341 2 02 2 13 2 24 1 13 362 1 02 1 12 1 22 2 02 2 12 2 22 137 1 22 1 17 2 12 259 1 23 2 05 1 24 2 27 1 04 676 1 212 1 34 1 017 2 31 2 616 2 63 2 4
5 -1 3 6 10
3 seconds
256 megabytes
['brute force', 'dfs and similar', 'dp', 'graphs', '*2200']
E. Character Blockingtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two strings of equal length s_1 and s_2, consisting of lowercase Latin letters, and an integer t.You need to answer q queries, numbered from 1 to q. The i-th query comes in the i-th second of time. Each query is one of three types: block the characters at position pos (indexed from 1) in both strings for t seconds; swap two unblocked characters; determine if the two strings are equal at the time of the query, ignoring blocked characters. Note that in queries of the second type, the characters being swapped can be from the same string or from s_1 and s_2.InputThe first line of the input contains a single integer T (1 \le T \le 10^4) — the number of test cases.Then follow the descriptions of the test cases.The first line of each test case contains a string s_1 consisting of lowercase Latin letters (length no more than 2 \cdot 10^5).The second line of each test case contains a string s_2 consisting of lowercase Latin letters (length no more than 2 \cdot 10^5).The strings have equal length.The third line of each test case contains two integers t and q (1 \le t, q \le 2 \cdot 10^5). The number t indicates the number of seconds for which a character is blocked. The number q corresponds to the number of queries.Each of the next q lines of each test case contains a single query. Each query is one of three types: "1\ \ \ pos" — block the characters at position pos in both strings for t seconds; "2\ \ \ 1/\;\!2\ \ \ pos_1\ \ \ 1/\;\!2\ \ \ pos_2" — swap two unblocked characters. The second number in the query indicates the number of the string from which the first character for the swap is taken. The third number in the query indicates the position in that string of that character. The fourth number in the query indicates the number of the string from which the second character for the swap is taken. The fifth number in the query indicates the position in that string of that character; "3" — determine if the two strings are equal at the time of the query, ignoring blocked characters. For queries of the first type, it is guaranteed that at the time of the query, the characters at position pos are not blocked.For queries of the second type, it is guaranteed that the characters being swapped are not blocked.All values of pos, pos_1, pos_2 are in the range from 1 to the length of the strings.The sum of the values of q over all test cases, as well as the total length of the strings s_1, does not exceed 2 \cdot 10^5.OutputFor each query of the third type, output "YES" if the two strings s_1 and s_2 are equal at the time of the query, ignoring blocked characters, and "NO" otherwise.You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes" and "YES" will be accepted as a positive answer.ExampleInput 2codeforcescodeblocks5 731 51 61 71 933coolclub2 52 1 2 2 32 2 2 2 41 233Output NO YES NO YES NO NoteLet's look at the strings s_1 and s_2 after each of the q queries. Blocked characters will be denoted in red.First example input:(codeforces, codeblocks) \rightarrow (codeforces, codeblocks) \rightarrow (code\color{red}{f}orces, code\color{red}{b}locks) \rightarrow (code\color{red}{fo}rces, code\color{red}{bl}ocks) \rightarrow (code\color{red}{for}ces, code\color{red}{blo}cks) \rightarrow (code\color{red}{for}c\color{red}{e}s, code\color{red}{blo}c\color{red}{k}s) \rightarrow (code\color{red}{for}c\color{red}{e}s, code\color{red}{blo}c\color{red}{k}s) \rightarrow (codef\color{red}{or}c\color{red}{e}s, codeb\color{red}{lo}c\color{red}{k}s)Second example input:(cool, club) \rightarrow (cuol, clob) \rightarrow (cuol, cbol) \rightarrow (c\color{red}{u}ol, c\color{red}{b}ol) \rightarrow (c\color{red}{u}ol, c\color{red}{b}ol) \rightarrow (cuol, cbol)
2codeforcescodeblocks5 731 51 61 71 933coolclub2 52 1 2 2 32 2 2 2 41 233
NO YES NO YES NO
2 seconds
256 megabytes
['data structures', 'hashing', 'implementation', '*1600']
D. Wooden Toy Festivaltime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn a small town, there is a workshop specializing in woodwork. Since the town is small, only three carvers work there.Soon, a wooden toy festival is planned in the town. The workshop employees want to prepare for it.They know that n people will come to the workshop with a request to make a wooden toy. People are different and may want different toys. For simplicity, let's denote the pattern of the toy that the i-th person wants as a_i (1 \le a_i \le 10^9).Each of the carvers can choose an integer pattern x (1 \le x \le 10^9) in advance, different carvers can choose different patterns. x is the integer. During the preparation for the festival, the carvers will perfectly work out the technique of making the toy of the chosen pattern, which will allow them to cut it out of wood instantly. To make a toy of pattern y for a carver who has chosen pattern x, it will take |x - y| time, because the more the toy resembles the one he can make instantly, the faster the carver will cope with the work.On the day of the festival, when the next person comes to the workshop with a request to make a wooden toy, the carvers can choose who will take on the job. At the same time, the carvers are very skilled people and can work on orders for different people simultaneously.Since people don't like to wait, the carvers want to choose patterns for preparation in such a way that the maximum waiting time over all people is as small as possible.Output the best maximum waiting time that the carvers can achieve.InputThe first line of the input contains an integer t (1 \le t \le 10^4) — the number of test cases.Then follow the descriptions of the test cases.The first line of a test case contains a single integer n (1 \le n \le 2 \cdot 10^5) — the number of people who will come to the workshop.The second line of a test case contains n integers a_1, a_2, a_3, \dots, a_n (1 \le a_i \le 10^9) — the patterns of toys.The sum of all n values over all test cases does not exceed 2 \cdot 10^5.OutputOutput t numbers, each of which is the answer to the corresponding test case — the best maximum waiting time that the carvers can achieve.ExampleInput 561 7 7 9 9 965 4 2 1 30 60914 19 37 59 1 4 4 98 731263 10 1 17 15 11Output 0 2 13 0 1 NoteIn the first example, the carvers can choose patterns 1, 7, 9 for preparation.In the second example, the carvers can choose patterns 3, 30, 60 for preparation.In the third example, the carvers can choose patterns 14, 50, 85 for preparation.
561 7 7 9 9 965 4 2 1 30 60914 19 37 59 1 4 4 98 731263 10 1 17 15 11
0 2 13 0 1
3 seconds
256 megabytes
['binary search', 'greedy', 'sortings', '*1400']
C. Ski Resorttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputDima Vatrushin is a math teacher at school. He was sent on vacation for n days for his good work. Dima has long dreamed of going to a ski resort, so he wants to allocate several consecutive days and go skiing. Since the vacation requires careful preparation, he will only go for at least k days.You are given an array a containing the weather forecast at the resort. That is, on the i-th day, the temperature will be a_i degrees.Dima was born in Siberia, so he can go on vacation only if the temperature does not rise above q degrees throughout the vacation.Unfortunately, Dima was so absorbed in abstract algebra that he forgot how to count. He asks you to help him and count the number of ways to choose vacation dates at the resort.InputThe first line of the input contains an integer t (1 \le t \le 10^4) — the number of test cases.Then follow the descriptions of the test cases.The first line of each test case contains three integers n, k, q (1 \le n \le 2 \cdot 10^5, 1 \le k \le n, -10^9 \le q \le 10^9) — the length of the array a, the minimum number of days at the resort, and the maximum comfortable temperature for Dima.The second line of each test case contains n integers a_1, a_2, a_3, \dots, a_n (-10^9 \le a_i \le 10^9) — the temperature at the ski resort.The sum of all n values over all test cases does not exceed 2 \cdot 10^5.OutputOutput t integers, each of which is the answer to the corresponding test case — the number of ways for Dima to choose vacation dates at the resort.ExampleInput 73 1 15-5 0 -105 3 -338 12 9 0 54 3 1212 12 10 154 1 -50 -1 2 55 5 03 -1 4 -5 -31 1 556 1 30 3 -2 5 -4 -4Output 6 0 1 0 0 1 9 NoteIn the first test case of the example, Dima can go on any day, so the suitable dates for him are [1], [2], [3], [1, 2], [2, 3], [1, 2, 3].In the second and fourth test cases of the example, Dima cannot go on any day due to the high temperature, so there are no suitable dates.In the third test case of the example, Dima can only go on the dates [1, 2, 3].
73 1 15-5 0 -105 3 -338 12 9 0 54 3 1212 12 10 154 1 -50 -1 2 55 5 03 -1 4 -5 -31 1 556 1 30 3 -2 5 -4 -4
6 0 1 0 0 1 9
1 second
256 megabytes
['combinatorics', 'math', 'two pointers', '*1000']
B. Binary Cafetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputOnce upon a time, Toma found himself in a binary cafe. It is a very popular and unusual place.The cafe offers visitors k different delicious desserts. The desserts are numbered from 0 to k-1. The cost of the i-th dessert is 2^i coins, because it is a binary cafe! Toma is willing to spend no more than n coins on tasting desserts. At the same time, he is not interested in buying any dessert more than once, because one is enough to evaluate the taste.In how many different ways can he buy several desserts (possibly zero) for tasting?InputThe first line of the input contains a single integer t (1 \le t \le 1000) — the number of test cases.Then follows t lines, each of which describes one test case.Each test case is given on a single line and consists of two integers n and k (1 \le n, k \le 10^9) — the number of coins Toma is willing to spend and the number of desserts in the binary cafe.OutputOutput t integers, the i-th of which should be equal to the answer for the i-th test case — the number of ways to buy desserts for tasting.ExampleInput 51 22 12 210 2179 100Output 2 2 3 4 180 NoteVariants for 1st sample: {}, {1}Variants for 2nd sample: {}, {1}Variants for 3rd sample: {}, {1}, {2}Variants for 4th sample: {}, {1}, {2}, {1, 2}
51 22 12 210 2179 100
2 2 3 4 180
1 second
256 megabytes
['bitmasks', 'combinatorics', 'math', '*1100']
A. Cipher Shifertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a string a (unknown to you), consisting of lowercase Latin letters, encrypted according to the following rule into string s: after each character of string a, an arbitrary (possibly zero) number of any lowercase Latin letters, different from the character itself, is added; after each such addition, the character that we supplemented is added. You are given string s, and you need to output the initial string a. In other words, you need to decrypt string s.Note that each string encrypted in this way is decrypted uniquely.InputThe first line of the input contains a single integer t (1 \le t \le 1000) — the number of test cases.The descriptions of the test cases follow.The first line of each test case contains a single integer n (2 \le n \le 100) — the length of the encrypted message.The second line of each test case contains a string s of length n — the encrypted message obtained from some string a.OutputFor each test case, output the decrypted message a on a separate line.ExampleInput 38abacabac5qzxcq20ccooddeeffoorrcceessOutput ac q codeforces NoteIn the first encrypted message, the letter a is encrypted as aba, and the letter c is encrypted as cabac.In the second encrypted message, only one letter q is encrypted as qzxcq.In the third encrypted message, zero characters are added to each letter.
38abacabac5qzxcq20ccooddeeffoorrcceess
ac q codeforces
1 second
256 megabytes
['implementation', 'strings', 'two pointers', '*800']
E. Decreasing Gametime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is an interactive problem.Consider the following game for two players: Initially, an array of integers a_1, a_2, \ldots, a_n of length n is written on blackboard. Game consists of rounds. On each round, the following happens: The first player selects any i such that a_i \gt 0. If there is no such i, the first player loses the game (the second player wins) and game ends. The second player selects any j \neq i such that a_j \gt 0. If there is no such j, the second player loses the game (the first player wins) and game ends. Let d = \min(a_i, a_j). The values of a_i and a_j are simultaneously decreased by d and the next round starts. It can be shown that game always ends after the finite number of rounds.You have to select which player you will play for (first or second) and win the game.InputThe first line contains a single integer n (1 \le n \le 300) — the length of array a.The second line contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le 300) — array a.InteractionInteraction begins after reading n and array a.You should start interaction by printing a single line, containing either "First" or "Second", representing the player you select.On each round, the following happens: If you are playing as the first player, you should print a single integer i (1 \le i \le n) on a separate line. After that, you should read a single integer j (-1 \le j \le n) written on a separate line.If j = -1, then you made an incorrect move. In this case your program should terminate immediately.If j = 0, then the second player can't make a correct move and you win the game. In this case your program should also terminate immediately.Otherwise j is equal to the index chosen by the second player, and you should proceed to the next round. If you are playing as the second player, you should read a single integer i (-1 \le i \le n) written on a separate line.If i = -1, then you made an incorrect move on the previous round (this cannot happen on the first round). In that case your program should terminate immediately.If i = 0, then the first player can't make a correct move and you win the game. In this case your program should also terminate immediately.Otherwise i is equal to the index chosen by first player. In this case you should write single integer j (1 \le j \le n) on a separate line and proceed to the next round. After printing i or j, do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see the documentation for other languages. HacksHacks are disabled in this problem.ExamplesInput 4 10 4 6 3 3 1 0Output First 1 2 4Input 6 4 5 5 11 3 2 2 5 4 6 1 0Output Second 4 4 3 1 3NoteIn the first example n = 4 and array a is [\, 10, 4, 6, 3 \,]. The game goes as follows: After reading array a contestant's program chooses to play as the first player and prints "First". First round: the first player chooses i = 1, the second player chooses j = 3. d = \min(a_1, a_3) = \min(10, 6) = 6 is calculated. Elements a_1 and a_3 are decreased by 6. Array a becomes equal to [\, 4, 4, 0, 3 \,]. Second round: the first player chooses i = 2, the second player chooses j = 1. d = \min(a_2, a_1) = \min(4, 4) = 4 is calculated. Elements a_2 and a_1 are decreased by 4. Array a becomes equal to [\, 0, 0, 0, 3 \,]. Third round: the first player chooses i = 4. There is no j \neq 4 such that a_j > 0, so the second player can't make a correct move and the first player wins. Jury's program prints j = 0. After reading it, contestant's program terminates. In the second example n = 6 and array a is [\, 4, 5, 5, 11, 3, 2 \,]. The game goes as follows: Contestant's program chooses to play as the second player and prints "Second". First round: i = 2, j = 4, a = [\, 4, 0, 5, 6, 3, 2 \,]. Second round: i = 5, j = 4, a = [\, 4, 0, 5, 3, 0, 2 \,]. Third round: i = 4, j = 3, a = [\, 4, 0, 2, 0, 0, 2 \,]. Fourth round: i = 6, j = 1, a = [\, 2, 0, 2, 0, 0, 0 \,]. Fifth round: i = 1, j = 3, a = [\, 0, 0, 0, 0, 0, 0 \,]. Sixth round: the first player can't make a correct move and the second player wins. Jury's program prints i = 0. After reading it, contestant's program terminates. Note that the example interaction contains extra empty lines so that it's easier to read. The real interaction doesn't contain any empty lines and you shouldn't print any extra empty lines as well.
4 10 4 6 3 3 1 0
First 1 2 4
2 seconds
256 megabytes
['constructive algorithms', 'dfs and similar', 'dp', 'greedy', 'interactive', '*2400']
D. Ball Sortingtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n colorful balls arranged in a row. The balls are painted in n distinct colors, denoted by numbers from 1 to n. The i-th ball from the left is painted in color c_i. You want to reorder the balls so that the i-th ball from the left has color i. Additionally, you have k \ge 1 balls of color 0 that you can use in the reordering process.Due to the strange properties of the balls, they can be reordered only by performing the following operations: Place a ball of color 0 anywhere in the sequence (between any two consecutive balls, before the leftmost ball or after the rightmost ball) while keeping the relative order of other balls. You can perform this operation no more than k times, because you have only k balls of color 0. Choose any ball of non-zero color such that at least one of the balls adjacent to him has color 0, and move that ball (of non-zero color) anywhere in the sequence (between any two consecutive balls, before the leftmost ball or after the rightmost ball) while keeping the relative order of other balls. You can perform this operation as many times as you want, but for each operation you should pay 1 coin. You can perform these operations in any order. After the last operation, all balls of color 0 magically disappear, leaving a sequence of n balls of non-zero colors.What is the minimum amount of coins you should spend on the operations of the second type, so that the i-th ball from the left has color i for all i from 1 to n after the disappearance of all balls of color zero? It can be shown that under the constraints of the problem, it is always possible to reorder the balls in the required way. Solve the problem for all k from 1 to n.InputThe first line contains integer t (1 \le t \le 500) — the number of test cases. The descriptions of the test cases follow.The first line contains one integer n (1 \le n \le 500) — the number of balls.The second line contains n distinct integers c_1, c_2, \ldots, c_n (1 \le c_i \le n) — the colors of balls from left to right.It is guaranteed that sum of n over all test cases doesn't exceed 500.OutputFor each test case, output n integers: the i-th (1 \le i \le n) of them should be equal to the minimum amount of coins you need to spend in order to reorder balls in the required way for k = i.ExampleInput 362 3 1 4 6 531 2 3117 3 4 6 8 9 10 2 5 11 1Output 3 2 2 2 2 2 0 0 0 10 5 4 4 4 4 4 4 4 4 4 NoteIn the first test case there are n = 6 balls. The colors of the balls from left to right are [\, 2, 3, 1, 4, 6, 5 \,]. Let's suppose k = 1. One of the ways to reorder the balls in the required way for 3 coins:[\, 2, 3, 1, 4, 6, 5 \,] \xrightarrow{\, 1 \,} [\, 2, 3, 1, 4, \color{red}{0}, 6, 5 \,] \xrightarrow{\, 2 \,} [\, 2, 3, \color{blue}{4}, 1, 0, 6, 5 \,] \xrightarrow{\, 2 \,} [\, \color{blue}{1}, 2, 3, 4, 0, 6, 5 \,] \xrightarrow{\, 2\,} [\, 1, 2, 3, 4, 0, 5, \color{blue}{6} \,]The number above the arrow is the operation type. Balls inserted on the operations of the first type are highlighted red; balls moved on the operations of second type are highlighted blue.It can be shown that for k = 1 it is impossible to rearrange balls in correct order for less than 3 coins. Let's suppose k = 2. One of the ways to reorder the balls in the required way for 2 coins:[\, 2, 3, 1, 4, 6, 5 \,] \xrightarrow{\, 1 \,} [\, 2, 3, 1, 4, 6, \color{red}{0}, 5\,] \xrightarrow{\, 2 \,} [\, 2, 3, 1, 4, 0, 5, \color{blue}{6}\,] \xrightarrow{\, 1 \,} [\, 2, 3, \color{red}{0}, 1, 4, 0, 5, 6 \,] \xrightarrow{\, 2 \,} [\, \color{blue}{1}, 2, 3, 0, 4, 0, 5, 6\,]Note that this sequence of operations is also correct for k greater than 2.It can be shown that for k from 2 to 6 it is impossible to rearrange balls in correct order for less than 2 coins.In the second test case the balls are already placed in the correct order, so answers for all k are equal to 0.
362 3 1 4 6 531 2 3117 3 4 6 8 9 10 2 5 11 1
3 2 2 2 2 2 0 0 0 10 5 4 4 4 4 4 4 4 4 4
2 seconds
256 megabytes
['data structures', 'dp', 'sortings', '*2100']
C. Insert Zero and Invert Prefixtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a sequence a_1, a_2, \ldots, a_n of length n, each element of which is either 0 or 1, and a sequence b, which is initially empty.You are going to perform n operations. On each of them you will increase the length of b by 1. On the i-th operation you choose an integer p between 0 and i-1. You insert 0 in the sequence b on position p+1 (after the first p elements), and then you invert the first p elements of b. More formally: let's denote the sequence b before the i-th (1 \le i \le n) operation as b_1, b_2, \ldots, b_{i-1}. On the i-th operation you choose an integer p between 0 and i-1 and replace b with \overline{b_1}, \overline{b_2}, \ldots, \overline{b_{p}}, 0, b_{p+1}, b_{p+2}, \ldots, b_{i-1}. Here, \overline{x} denotes the binary inversion. Hence, \overline{0} = 1 and \overline{1} = 0. You can find examples of operations in the Notes section.Determine if there exists a sequence of operations that makes b equal to a. If such sequence of operations exists, find it.InputEach test contains multiple test cases. The first line contains a single integer t (1 \le t \le 10^4) — the number of test cases.The first line of each test case contains one integer n (1 \le n \le 10^5) — the length of the sequence a.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (0 \le a_i \le 1) — the sequence a.It is guaranteed that the sum of n over all test cases does not exceed 10^5.OutputFor each test case: output "NO", if it is impossible to make b equal to a using the given operations; otherwise, output "YES" in the first line and n integers p_1, p_2, \ldots, p_n (0 \le p_i \le i-1) in the second line — the description of sequence of operations that makes b equal to a. Here, p_i should be the integer you choose on the i-th operation. If there are multiple solutions, you can output any of them. ExampleInput 451 1 0 0 01130 1 161 0 0 1 1 0Output YES 0 0 2 1 3 NO NO YES 0 1 0 2 4 2NoteIn the first test case, Before the first operation, b = [\,]. You choose p = 0 and replace b with [\, \underline{0} \,] On the second operation you choose p = 0 and replace b with [\, \underline{0}, 0 \,]. On the third operation you choose p = 2 and replace b with [\, 1, 1, \underline{0} \,]. On the fourth operation you choose p = 1 and replace b with [\, 0, \underline{0}, 1, 0 \,]. On the fifth operation you choose p = 3 and replace b with [\, 1, 1, 0, \underline{0}, 0 \,]. Hence, sequence b changes in the following way: [\,] \xrightarrow{p \, = \, 0} [\, \underline{0} \,] \xrightarrow{p \, = \, 0} [\, \underline{0}, 0 \,] \xrightarrow{p \, = \, 2} [\, 1, 1, \underline{0} \,] \xrightarrow{p \, = \, 1} [\, 0, \underline{0}, 1, 0 \,] \xrightarrow{p \, = \, 3} [\, 1, 1, 0, \underline{0}, 0 \,]. In the end the sequence b is equal to the sequence a, so this way to perform operations is one of the correct answers.In the second test case, n = 1 and the only achiveable sequence b is [\, 0 \, ].In the third test case, there are six possible sequences of operations: [\,] \xrightarrow{p \, = \, 0} [\, \underline{0} \,] \xrightarrow{p \, = \, 0} [\, \underline{0}, 0 \,] \xrightarrow{p \, = \, 0} [\, \underline{0}, 0, 0 \,]. [\,] \xrightarrow{p \, = \, 0} [\, \underline{0} \,] \xrightarrow{p \, = \, 0} [\, \underline{0}, 0 \,] \xrightarrow{p \, = \, 1} [\, 1, \underline{0}, 0 \,]. [\,] \xrightarrow{p \, = \, 0} [\, \underline{0} \,] \xrightarrow{p \, = \, 0} [\, \underline{0}, 0 \,] \xrightarrow{p \, = \, 2} [\, 1, 1, \underline{0} \,]. [\,] \xrightarrow{p \, = \, 0} [\, \underline{0} \,] \xrightarrow{p \, = \, 1} [\, 1, \underline{0} \,] \xrightarrow{p \, = \, 0} [\, \underline{0}, 1, 0 \,]. [\,] \xrightarrow{p \, = \, 0} [\, \underline{0} \,] \xrightarrow{p \, = \, 1} [\, 1, \underline{0} \,] \xrightarrow{p \, = \, 1} [\, 0, \underline{0}, 0 \,]. [\,] \xrightarrow{p \, = \, 0} [\, \underline{0} \,] \xrightarrow{p \, = \, 1} [\, 1, \underline{0} \,] \xrightarrow{p \, = \, 2} [\, 0, 1, \underline{0} \,]. None of them makes b equal to [\, 0, 1, 1 \,], so the answer is "NO".
451 1 0 0 01130 1 161 0 0 1 1 0
YES 0 0 2 1 3 NO NO YES 0 1 0 2 4 2
2 seconds
256 megabytes
['constructive algorithms', '*1300']
B. Lampstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have n lamps, numbered by integers from 1 to n. Each lamp i has two integer parameters a_i and b_i.At each moment each lamp is in one of three states: it may be turned on, turned off, or broken.Initially all lamps are turned off. In one operation you can select one lamp that is turned off and turn it on (you can't turn on broken lamps). You receive b_i points for turning lamp i on. The following happens after each performed operation: Let's denote the number of lamps that are turned on as x (broken lamps do not count). All lamps i such that a_i \le x simultaneously break, whether they were turned on or off. Please note that broken lamps never count as turned on and that after a turned on lamp breaks, you still keep points received for turning it on.You can perform an arbitrary number of operations.Find the maximum number of points you can get.InputThe first line contains a single integer t (1 \le t \le 10^4) — the number of test cases.The first line contains a single integer n (1 \le n \le 2 \cdot 10^5) — the number of lamps.Each of the next n lines contains two integers a_i and b_i (1 \le a_i \le n, 1 \le b_i \le 10^9) — parameters of the i-th lamp.It is guaranteed that sum of n over all test cases doesn't exceed 2 \cdot 10^5.OutputFor each test case, output one integer — the maximum number of points you can get.ExampleInput 442 21 61 101 1353 43 12 53 23 361 23 41 43 43 52 311 1Output 15 14 20 1 NoteIn first test case n = 4. One of ways to get the maximum number of points is as follows: You turn lamp 4 on and receive b_4 = 13 points. The number of lamps that are turned on is 1, so all lamps with a_i \le 1 (namely lamps 2, 3 and 4) break. Lamp 4 is no longer turned on, so the number of lamps that are turned becomes 0. The only lamp you can turn on is lamp 1, as all other lamps are broken. You receive b_1 = 2 points for turning it on. The number of lamps that are turned on is 1. As a_1 = 2, lamp 1 doesn't break. Your receive 13 + 2 = 15 points in total. It can be shown that this is the maximum number of points you can get, so the answer for the first test case is 15.In the second test case, one of the ways to get the maximum number of points is as follows: On the first operation you turn on lamp 4 and receive 2 points. No lamps break after the first operation. On the second operation you turn on lamp 3 and receive 5 points. After the second operation, there are 2 lamps turned on. As a_3 \le 2, lamp 3 breaks. On the third operation, you turn on lamp 1 and receive 4 points. On the fourth operation, you turn on lamp 5 and receive 3 points. After that there are 3 lamps turned on: lamps 1, 4 and 5. Lamps 1, 2, 4 and 5 simultaneously break, because for all of them a_i \le 3. You receive 2 + 5 + 4 + 3 = 14 points in total. It can be shown that this is the maximum number of points you can get.In the third test case, one of the ways to get the maximum number of points is as follows: Turn the lamp 3 on and receive 4 points. Lamps 1 and 3 break. Turn the lamp 2 on and receive 4 points. Turn the lamp 6 on and receive 3 points. Lamp 6 breaks. Turn the lamp 4 on and receive 4 points. Turn the lamp 5 on and receive 5 points. Lamps 2, 4 and 5 break. You receive 4 + 4 + 3 + 4 + 5 = 20 points in total. It can be shown that this is the maximum number of points you can get.
442 21 61 101 1353 43 12 53 23 361 23 41 43 43 52 311 1
15 14 20 1
1 second
256 megabytes
['greedy', 'sortings', '*1100']
A. The Good Arraytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers n and k.An array a_1, a_2, \ldots, a_n of length n, consisting of zeroes and ones is good if for all integers i from 1 to n both of the following conditions are satisfied: at least \lceil \frac{i}{k} \rceil of the first i elements of a are equal to 1, at least \lceil \frac{i}{k} \rceil of the last i elements of a are equal to 1. Here, \lceil \frac{i}{k} \rceil denotes the result of division of i by k, rounded up. For example, \lceil \frac{6}{3} \rceil = 2, \lceil \frac{11}{5} \rceil = \lceil 2.2 \rceil = 3 and \lceil \frac{7}{4} \rceil = \lceil 1.75 \rceil = 2.Find the minimum possible number of ones in a good array.InputEach test contains multiple test cases. The first line contains a single integer t (1 \le t \le 10^4) — the number of test cases.The only line of each test case contains two integers n, k (2 \le n \le 100, 1 \le k \le n) — the length of array and parameter k from the statement.OutputFor each test case output one integer — the minimum possible number of ones in a good array.It can be shown that under the given constraints at least one good array always exists.ExampleInput 73 25 29 37 110 49 58 8Output 2 3 4 7 4 3 2 NoteIn the first test case, n = 3 and k = 2: Array [ \, 1, 0, 1 \, ] is good and the number of ones in it is 2. Arrays [ \, 0, 0, 0 \, ], [ \, 0, 1, 0 \, ] and [ \, 0, 0, 1 \, ] are not good since for i=1 the first condition from the statement is not satisfied. Array [ \, 1, 0, 0 \, ] is not good since for i=1 the second condition from the statement is not satisfied. All other arrays of length 3 contain at least 2 ones. Thus, the answer is 2.In the second test case, n = 5 and k = 2: Array [ \, 1, 1, 0, 0, 1 \, ] is not good since for i=3 the second condition is not satisfied. Array [ \, 1, 0, 1, 0, 1 \, ] is good and the number of ones in it is 3. It can be shown that there is no good array with less than 3 ones, so the answer is 3. In the third test case, n = 9 and k = 3: Array [ \, 1, 0, 1, 0, 0, 0, 1, 0, 1 \, ] is good and the number of ones in it is 4. It can be shown that there is no good array with less than 4 ones, so the answer is 4. In the fourth test case, n = 7 and k = 1. The only good array is [ \, 1, 1, 1, 1, 1, 1, 1\, ], so the answer is 7.
73 25 29 37 110 49 58 8
2 3 4 7 4 3 2
1 second
256 megabytes
['greedy', 'implementation', 'math', '*800']
F. Stuck Conveyortime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is an interactive problem.There is an n by n grid of conveyor belts, in positions (1, 1) through (n, n) of a coordinate plane. Every other square in the plane is empty. Each conveyor belt can be configured to move boxes up ('^'), down ('v'), left ('<'), or right ('>'). If a box moves onto an empty square, it stops moving.However, one of the n^2 belts is stuck, and will always move boxes in the same direction, no matter how it is configured. Your goal is to perform a series of tests to determine which conveyor belt is stuck, and the direction in which it sends items.To achieve this, you can perform up to 25 tests. In each test, you assign a direction to all n^2 belts, place a box on top of one of them, and then turn all of the conveyors on. One possible result of a query with n=4. In this case, the box starts at (2, 2). If there were no stuck conveyor, it would end up at (5, 4), but because of the stuck '>' conveyor at (3, 3), it enters an infinite loop.The conveyors move the box around too quickly for you to see, so the only information you receive from a test is whether the box eventually stopped moving, and if so, the coordinates of its final position.InteractionYou begin the interaction by reading a single integer n (2 \le n\le 100) — the number of rows and columns in the grid.Then, you can make at most 25 queries.Each query should begin with a line of the form ? r c, where r and c are the initial row and column of the box, respectively.The next n lines of the query should contain n characters each. The jth character of the ith row should be one of '^', 'v', '<', or '>', indicating the direction of conveyor (i, j) for this query.After each query, you will receive two integers x and y. If x = y = -1, then the box entered an infinite loop. Otherwise, its final position was (x, y).If you make too many queries or make an invalid query, you will receive the Wrong Answer verdict.After you have found the stuck conveyor and its direction, print a single line ! r c dir, where r and c are the row and column of the stuck conveyor, respectively, and dir is one of '^', 'v', '<', or '>', indicating the direction of the stuck conveyor. Note that printing this answer does not count towards your total of 25 queries. After printing this line, your program should terminate.The interactor is non-adaptive. This means that the location and direction of the stuck belt is fixed at the start of the interaction, and does not change after the queries.After printing a query do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see the documentation for other languages. HacksTo make a hack, use the following format.The first line should contain a single integer n (1 \le n \le 100) — the number of rows and columns in the grid.The next line should contain two integers r and c (1 \le r, c \le n), as well as a character \mathrm{dir} (\mathrm{dir} is one of '^', 'v', '<', '>') — the position of the stuck conveyor and its fixed direction. These values should be separated by spaces.ExamplesInput 3 -1 -1 0 2Output ? 2 2 >>< >>v ^<< ? 1 1 >>< >>v ^<< ! 1 2 ^Input 4 -1 -1Output ? 2 2 v>v< ^v<v v>v^ >v>v ! 3 3 >NoteFor the first query of the first sample input, the box starts on (2, 2) and enters an infinite loop containing rows 2 and 3. Because the stuck conveyor is in row 1, it does not affect the outcome of the query.For the second query of the first sample input, the conveyors are configured in the same way, but the box starts on (1, 1). If there were no stuck conveyor, it would enter an infinite loop between (1, 2) and (1, 3). However, the stuck conveyor redirects it to (0, 2).After these two queries, the program is able to determine that the stuck conveyor is at (1, 2) and directs items upward.The query for the second sample input corresponds to the picture above. After asking the query, there are many possibilities for the stuck conveyor, but the program correctly guesses that it is at (3, 3) and directs items to the right.
3 -1 -1 0 2
? 2 2 >>< >>v ^<< ? 1 1 >>< >>v ^<< ! 1 2 ^
2 seconds
256 megabytes
['binary search', 'constructive algorithms', 'interactive', '*3000']
E. Count Supersequencestime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a of n integers, where all elements a_i lie in the range [1, k]. How many different arrays b of m integers, where all elements b_i lie in the range [1, k], contain a as a subsequence? Two arrays are considered different if they differ in at least one position.A sequence x is a subsequence of a sequence y if x can be obtained from y by the deletion of several (possibly, zero or all) elements.Since the answer may be large, print it modulo 10^9 + 7.InputThe first line of the input contains a single integer t (1 \le t \le 10^4) — the number of test cases. The description of the test cases follows.The first line of each test case contains three integers n, m, k (1 \le n \le 2\cdot 10^5, n \le m \le 10^9, 1 \le k \le 10^9) — the size of a, the size of b, and the maximum value allowed in the arrays, respectively.The next line of each test case contains n integers a_1, a_2, \ldots a_n (1\le a_i \le k) — the elements of the array a.It is guaranteed that the sum of n over all test cases does not exceed 2\cdot 10^5.OutputFor each test case, output a single integer — the number of suitable arrays b, modulo 10^9+7.ExampleInput 71 1000000 113 4 31 2 25 7 81 2 3 4 16 6 1818 2 2 5 2 161 10 218 10 12345671 1 2 1 2 2 2 15 1000000000 1000000000525785549 816356460 108064697 194447117 725595511Output 1 9 1079 1 1023 906241579 232432822 NoteFor the first example, since k=1, there is only one array of size m consisting of the integers [1, k]. This array ([1, 1, \ldots, 1]) contains the original array as a subsequence, so the answer is 1.For the second example, the 9 arrays are [1, 1, 2, 2], [1, 2, 1, 2], [1, 2, 2, 1], [1, 2, 2, 2], [1, 2, 2, 3], [1, 2, 3, 2], [1, 3, 2, 2], [2, 1, 2, 2], [3, 1, 2, 2].For the fourth example, since m=n, the only array of size m that contains a as a subsequence is a itself.
71 1000000 113 4 31 2 25 7 81 2 3 4 16 6 1818 2 2 5 2 161 10 218 10 12345671 1 2 1 2 2 2 15 1000000000 1000000000525785549 816356460 108064697 194447117 725595511
1 9 1079 1 1023 906241579 232432822
3 seconds
256 megabytes
['combinatorics', 'dp', 'math', '*2500']
D. Bracket Walktime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a string s of length n consisting of the characters '(' and ')'. You are walking on this string. You start by standing on top of the first character of s, and you want to make a sequence of moves such that you end on the n-th character. In one step, you can move one space to the left (if you are not standing on the first character), or one space to the right (if you are not standing on the last character). You may not stay in the same place, however you may visit any character, including the first and last character, any number of times.At each point in time, you write down the character you are currently standing on. We say the string is walkable if there exists some sequence of moves that take you from the first character to the last character, such that the string you write down is a regular bracket sequence.A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not.One possible valid walk on s=\mathtt{(())()))}. The red dot indicates your current position, and the red string indicates the string you have written down. Note that the red string is a regular bracket sequence at the end of the process.You are given q queries. Each query flips the value of a character from '(' to ')' or vice versa. After each query, determine whether the string is walkable.Queries are cumulative, so the effects of each query carry on to future queries.InputThe first line of the input contains two integers n and q (1 \le n, q \le 2\cdot 10^5) — the size of the string and the number of queries, respectively.The second line of the input contains a string s of size n, consisting of the characters '(' and ')' — the initial bracket string.Each of the next q lines contains a single integer i (1\le i \le n) — the index of the character to flip for that query.OutputFor each query, print "YES" if the string is walkable after that query, and "NO" otherwise.You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.ExamplesInput 10 9 (())()())) 9 7 2 6 3 6 7 4 8 Output YES YES NO NO YES NO YES NO NO Input 3 2 (() 2 3 Output NO NO NoteIn the first example: After the first query, the string is (())()()(). This string is a regular bracket sequence, so it is walkable by simply moving to the right. After the second query, the string is (())()))(). If you move right once, then left once, then walk right until you hit the end of the string, you produce the string (((())()))(), which is a regular bracket sequence. After the third query, the string is ()))()))(). We can show that this string is not walkable. In the second example, the strings after the queries are ()) and ()(, neither of which are walkable.
10 9 (())()())) 9 7 2 6 3 6 7 4 8
YES YES NO NO YES NO YES NO NO
3 seconds
256 megabytes
['data structures', 'greedy', 'strings', '*2100']