Problem ID stringlengths 2 6 | Problem Description stringlengths 0 7.52k | Rating float64 800 3.5k | math bool 2 classes | greedy bool 2 classes | implementation bool 2 classes | dp bool 2 classes | data structures bool 2 classes | constructive algorithms bool 2 classes | brute force bool 2 classes | binary search bool 2 classes | sortings bool 2 classes | graphs bool 2 classes | __index_level_0__ int64 3 9.98k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1230A | Problem - 1230A - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags brute force implementation *800 No tag edit access β Contest materials β the numbers of candies in each bag. Output Output YES if it's possible to give the bags to Dawid's friends so that both friends receive the same amount of candies, or NO otherwise. Each character can be printed in any case (either uppercase or lowercase). Examples Input 1 7 11 5 Output YES Input 7 3 2 5 Output NO Note In the first sample test, Dawid can give the first and the third bag to the first friend, and the second and the fourth bag to the second friend. This way, each friend will receive $$$12$$$ candies. In the second sample test, it's impossible to distribute the bags. | 800 | false | false | true | false | false | false | true | false | false | false | 4,529 |
1452A | There is an infinite 2-dimensional grid. The robot stands in cell $$$(0, 0)$$$ and wants to reach cell $$$(x, y)$$$. Here is a list of possible commands the robot can execute: move north from cell $$$(i, j)$$$ to $$$(i, j + 1)$$$; move east from cell $$$(i, j)$$$ to $$$(i + 1, j)$$$; move south from cell $$$(i, j)$$$ to $$$(i, j - 1)$$$; move west from cell $$$(i, j)$$$ to $$$(i - 1, j)$$$; stay in cell $$$(i, j)$$$. The robot wants to reach cell $$$(x, y)$$$ in as few commands as possible. However, he can't execute the same command two or more times in a row. What is the minimum number of commands required to reach $$$(x, y)$$$ from $$$(0, 0)$$$? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0β the number of testcases. Each of the next $$$t$$$ lines contains two integers $$$x$$$ and $$$y$$$ ($$$0 le x, y le 10^4$$$)xa0β the destination coordinates of the robot. Output For each testcase print a single integerxa0β the minimum number of commands required for the robot to reach $$$(x, y)$$$ from $$$(0, 0)$$$ if no command is allowed to be executed two or more times in a row. Example Input 5 5 5 3 4 7 1 0 0 2 0 Note The explanations for the example test: We use characters N, E, S, W and 0 to denote going north, going east, going south, going west and staying in the current cell, respectively. In the first test case, the robot can use the following sequence: NENENENENE. In the second test case, the robot can use the following sequence: NENENEN. In the third test case, the robot can use the following sequence: ESENENE0ENESE. In the fourth test case, the robot doesn't need to go anywhere at all. In the fifth test case, the robot can use the following sequence: E0E. | 800 | true | false | false | false | false | false | false | false | false | false | 3,421 |
1799C | You are given a string $$$s$$$. You can reorder the characters to form a string $$$t$$$. Define $$$t_{mathrm{max}}$$$ to be the lexicographical maximum of $$$t$$$ and $$$t$$$ in reverse order. Given $$$s$$$ determine the lexicographically minimum value of $$$t_{mathrm{max}}$$$ over all reorderings $$$t$$$ of $$$s$$$. A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: $$$a$$$ is a prefix of $$$b$$$, but $$$a e b$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^5$$$) β the number of test cases. Descriptions of test cases follow. The first and only line of each test case contains a string $$$s$$$ ($$$1 leq s leq 10^5$$$). $$$s$$$ consists of only lowercase English letters. It is guaranteed that the sum of $$$s$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case print the lexicographically minimum value of $$$t_{mathrm{max}}$$$ over all reorderings $$$t$$$ of $$$s$$$. Example Input 12 a aab abb abc aabb aabbb aaabb abbb abbbb abbcc eaga ffcaba Output a aba bab bca abba abbba ababa bbab bbabb bbcca agea acffba Note For the first test case, there is only one reordering of $$$s$$$, namely "a". For the second test case, there are three reorderings of $$$s$$$. $$$t = mathtt{aab}$$$: $$$t_{mathrm{max}} = max(mathtt{aab}, mathtt{baa}) = mathtt{baa}$$$ $$$t = mathtt{aba}$$$: $$$t_{mathrm{max}} = max(mathtt{aba}, mathtt{aba}) = mathtt{aba}$$$ $$$t = mathtt{baa}$$$: $$$t_{mathrm{max}} = max(mathtt{baa}, mathtt{aab}) = mathtt{baa}$$$ The lexicographical minimum of $$$t_{mathrm{max}}$$$ over all cases is "aba". | 1,700 | false | true | false | false | false | false | false | false | false | false | 1,482 |
1286C1 | This problem is different with hard version only by constraints on total answers length It is an interactive problem Venya joined a tour to the madhouse, in which orderlies play with patients the following game. Orderlies pick a string $$$s$$$ of length $$$n$$$, consisting only of lowercase English letters. The player can ask two types of queries: ? l r β ask to list all substrings of $$$s[l..r]$$$. Substrings will be returned in random order, and in every substring, all characters will be randomly shuffled. ! s β guess the string picked by the orderlies. This query can be asked exactly once, after that the game will finish. If the string is guessed correctly, the player wins, otherwise he loses. The player can ask no more than $$$3$$$ queries of the first type. To make it easier for the orderlies, there is an additional limitation: the total number of returned substrings in all queries of the first type must not exceed $$$(n+1)^2$$$. Venya asked you to write a program, which will guess the string by interacting with the orderlies' program and acting by the game's rules. Your program should immediately terminate after guessing the string using a query of the second type. In case your program guessed the string incorrectly, or it violated the game rules, it will receive verdict Wrong answer. Note that in every test case the string is fixed beforehand and will not change during the game, which means that the interactor is not adaptive. Input First line contains number $$$n$$$ ($$$1 le n le 100$$$)xa0β the length of the picked string. Interaction You start the interaction by reading the number $$$n$$$. To ask a query about a substring from $$$l$$$ to $$$r$$$ inclusively ($$$1 le l le r le n$$$), you should output ? l r on a separate line. After this, all substrings of $$$s[l..r]$$$ will be returned in random order, each substring exactly once. In every returned substring all characters will be randomly shuffled. In the case, if you ask an incorrect query, ask more than $$$3$$$ queries of the first type or there will be more than $$$(n+1)^2$$$ substrings returned in total, you will receive verdict Wrong answer. To guess the string $$$s$$$, you should output ! s on a separate line. After printing each query, do not forget to flush the output. Otherwise, you will get Idleness limit exceeded. To flush the output, you can 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. If you received - (dash) as an answer to any query, you need to terminate your program with exit code 0 (for example, by calling exit(0)). This means that there was an error in the interaction protocol. If you don't terminate with exit code 0, you can receive any unsuccessful verdict. Hack format To hack a solution, use the following format: The first line should contain one integer $$$n$$$ ($$$1 le n le 100$$$)xa0β the length of the string, and the following line should contain the string $$$s$$$. Example Output ? 1 2 ? 3 4 ? 4 4 ! aabc | 2,400 | true | false | false | false | false | true | true | false | false | false | 4,259 |
1209B | It is a holiday season, and Koala is decorating his house with cool lights! He owns $$$n$$$ lights, all of which flash periodically. After taking a quick glance at them, Koala realizes that each of his lights can be described with two parameters $$$a_i$$$ and $$$b_i$$$. Light with parameters $$$a_i$$$ and $$$b_i$$$ will toggle (on to off, or off to on) every $$$a_i$$$ seconds starting from the $$$b_i$$$-th second. In other words, it will toggle at the moments $$$b_i$$$, $$$b_i + a_i$$$, $$$b_i + 2 cdot a_i$$$ and so on. You know for each light whether it's initially on or off and its corresponding parameters $$$a_i$$$ and $$$b_i$$$. Koala is wondering what is the maximum number of lights that will ever be on at the same time. So you need to find that out. Here is a graphic for the first example. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 100$$$), the number of lights. The next line contains a string $$$s$$$ of $$$n$$$ characters. The $$$i$$$-th character is "1", if the $$$i$$$-th lamp is initially on. Otherwise, $$$i$$$-th character is "0". The $$$i$$$-th of the following $$$n$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i, b_i le 5$$$) xa0β the parameters of the $$$i$$$-th light. Output Print a single integerxa0β the maximum number of lights that will ever be on at the same time. Examples Input 4 1111 3 4 5 2 3 1 3 2 Input 6 011100 5 3 5 5 2 4 3 5 4 2 1 5 Note For first example, the lamps' states are shown in the picture above. The largest number of simultaneously on lamps is $$$2$$$ (e.g. at the moment $$$2$$$). In the second example, all lights are initially on. So the answer is $$$4$$$. | 1,300 | true | false | true | false | false | false | false | false | false | false | 4,645 |
291C | The problem uses a simplified TCP/IP address model, please make sure you've read the statement attentively. Polycarpus has found a job, he is a system administrator. One day he came across _n_ IP addresses. Each IP address is a 32 bit number, represented as a group of four 8-bit numbers (without leading zeroes), separated by dots. For example, the record 0.255.1.123 shows a correct IP address and records 0.256.1.123 and 0.255.1.01 do not. In this problem an arbitrary group of four 8-bit numbers is a correct IP address. Having worked as an administrator for some time, Polycarpus learned that if you know the IP address, you can use the subnet mask to get the address of the network that has this IP addess. The subnet mask is an IP address that has the following property: if we write this IP address as a 32 bit string, that it is representable as "11...11000..000". In other words, the subnet mask first has one or more one bits, and then one or more zero bits (overall there are 32 bits). For example, the IP address 2.0.0.0 is not a correct subnet mask as its 32-bit record looks as 00000010000000000000000000000000. To get the network address of the IP address, you need to perform the operation of the bitwise "and" of the IP address and the subnet mask. For example, if the subnet mask is 255.192.0.0, and the IP address is 192.168.1.2, then the network address equals 192.128.0.0. In the bitwise "and" the result has a bit that equals 1 if and only if both operands have corresponding bits equal to one. Now Polycarpus wants to find all networks to which his IP addresses belong. Unfortunately, Polycarpus lost subnet mask. Fortunately, Polycarpus remembers that his IP addresses belonged to exactly _k_ distinct networks. Help Polycarpus find the subnet mask, such that his IP addresses will belong to exactly _k_ distinct networks. If there are several such subnet masks, find the one whose bit record contains the least number of ones. If such subnet mask do not exist, say so. | 1,600 | false | false | true | false | false | false | true | false | false | false | 8,669 |
811E | In his spare time Vladik estimates beauty of the flags. Every flag could be represented as the matrix _n_u2009Γu2009_m_ which consists of positive integers. Let's define the beauty of the flag as number of components in its matrix. We call component a set of cells with same numbers and between any pair of cells from that set there exists a path through adjacent cells from same component. Here is the example of the partitioning some flag matrix into components: But this time he decided to change something in the process. Now he wants to estimate not the entire flag, but some segment. Segment of flag can be described as a submatrix of the flag matrix with opposite corners at (1,u2009_l_) and (_n_,u2009_r_), where conditions 1u2009β€u2009_l_u2009β€u2009_r_u2009β€u2009_m_ are satisfied. Help Vladik to calculate the beauty for some segments of the given flag. Input First line contains three space-separated integers _n_, _m_, _q_ (1u2009β€u2009_n_u2009β€u200910, 1u2009β€u2009_m_,u2009_q_u2009β€u2009105)xa0β dimensions of flag matrix and number of segments respectively. Each of next _n_ lines contains _m_ space-separated integersxa0β description of flag matrix. All elements of flag matrix is positive integers not exceeding 106. Each of next _q_ lines contains two space-separated integers _l_, _r_ (1u2009β€u2009_l_u2009β€u2009_r_u2009β€u2009_m_)xa0β borders of segment which beauty Vladik wants to know. Output For each segment print the result on the corresponding line. Example Input 4 5 4 1 1 1 1 1 1 2 2 3 3 1 1 1 2 5 4 4 5 5 5 1 5 2 5 1 2 4 5 Note Partitioning on components for every segment from first test case: | 2,600 | false | false | false | false | true | false | false | false | false | true | 6,517 |
794D | Oleg the bank client lives in Bankopolia. There are _n_ cities in Bankopolia and some pair of cities are connected directly by bi-directional roads. The cities are numbered from 1 to _n_. There are a total of _m_ roads in Bankopolia, the _i_-th road connects cities _u__i_ and _v__i_. It is guaranteed that from each city it is possible to travel to any other city using some of the roads. Oleg wants to give a label to each city. Suppose the label of city _i_ is equal to _x__i_. Then, it must hold that for all pairs of cities (_u_,u2009_v_) the condition _x__u_u2009-u2009_x__v_u2009β€u20091 holds if and only if there is a road connecting _u_ and _v_. Oleg wonders if such a labeling is possible. Find an example of such labeling if the task is possible and state that it is impossible otherwise. Input The first line of input contains two space-separated integers _n_ and _m_ (2u2009β€u2009_n_u2009β€u20093Β·105, 1u2009β€u2009_m_u2009β€u20093Β·105)xa0β the number of cities and the number of roads. Next, _m_ lines follow. The _i_-th line contains two space-separated integers _u__i_ and _v__i_ (1u2009β€u2009_u__i_,u2009_v__i_u2009β€u2009_n_, _u__i_u2009β u2009_v__i_)xa0β the cities connected by the _i_-th road. It is guaranteed that there is at most one road between each pair of cities and it is possible to travel from any city to any other city using some roads. Output If the required labeling is not possible, output a single line containing the string "NO" (without quotes). Otherwise, output the string "YES" (without quotes) on the first line. On the next line, output _n_ space-separated integers, _x_1,u2009_x_2,u2009...,u2009_x__n_. The condition 1u2009β€u2009_x__i_u2009β€u2009109 must hold for all _i_, and for all pairs of cities (_u_,u2009_v_) the condition _x__u_u2009-u2009_x__v_u2009β€u20091 must hold if and only if there is a road connecting _u_ and _v_. Examples Input 5 10 1 2 1 3 1 4 1 5 2 3 2 4 2 5 3 4 3 5 5 4 Note For the first sample, _x_1u2009=u20092, _x_2u2009=u20093, _x_3u2009=u2009_x_4u2009=u20091 is a valid labeling. Indeed, (3,u20094), (1,u20092), (1,u20093), (1,u20094) are the only pairs of cities with difference of labels not greater than 1, and these are precisely the roads of Bankopolia. For the second sample, all pairs of cities have difference of labels not greater than 1 and all pairs of cities have a road connecting them. For the last sample, it is impossible to construct a labeling satisfying the given constraints. | 2,400 | false | false | false | false | false | false | false | false | false | true | 6,597 |
2030B | For an arbitrary binary string $$$t$$$$$$^{ ext{β}}$$$, let $$$f(t)$$$ be the number of non-empty subsequences$$$^{ ext{β }}$$$ of $$$t$$$ that contain only $$$mathtt{0}$$$, and let $$$g(t)$$$ be the number of non-empty subsequences of $$$t$$$ that contain at least one $$$mathtt{1}$$$. Note that for $$$f(t)$$$ and for $$$g(t)$$$, each subsequence is counted as many times as it appears in $$$t$$$. E.g., $$$f(mathtt{000}) = 7, g(mathtt{100}) = 4$$$. We define the oneness of the binary string $$$t$$$ to be $$$f(t)-g(t)$$$, where for an arbitrary integer $$$z$$$, $$$z$$$ represents the absolute value of $$$z$$$. You are given a positive integer $$$n$$$. Find a binary string $$$s$$$ of length $$$n$$$ such that its oneness is as small as possible. If there are multiple strings, you can print any of them. Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0β the number of test cases. The only line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 2cdot10^5$$$)xa0β the length of $$$s$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot10^5$$$. Output For each test case, output $$$s$$$ on a new line. If multiple answers exist, output any. Note In the first test case, for the example output, $$$f(t)=1$$$ because there is one subsequence that contains only $$$mathtt{0}$$$ ($$$mathtt{0}$$$), and $$$g(t)=0$$$ because there are no subsequences that contain at least one $$$1$$$. The oneness is $$$1-0=1$$$. The output $$$mathtt{1}$$$ is correct as well because its oneness is $$$0-1=1$$$. For the example output of the second test case, $$$f(t)=1$$$ because there is one non-empty subsequence that contains only $$$mathtt{0}$$$, and $$$g(t)=2$$$ because there are two non-empty subsequences that contain at least one $$$mathtt{1}$$$ ($$$mathtt{01}$$$ and $$$mathtt{1}$$$). The oneness is thus $$$1-2=1$$$. It can be shown that $$$1$$$ is the minimum possible value of its oneness over all possible binary strings of size $$$2$$$. | 800 | true | false | false | false | false | true | false | false | false | false | 87 |
1895A | Monocarp has found a treasure map. The map represents the treasure location as an OX axis. Monocarp is at $$$0$$$, the treasure chest is at $$$x$$$, the key to the chest is at $$$y$$$. Obviously, Monocarp wants to open the chest. He can perform the following actions: go $$$1$$$ to the left or $$$1$$$ to the right (spending $$$1$$$ second); pick the key or the chest up if he is in the same point as that object (spending $$$0$$$ seconds); put the chest down in his current point (spending $$$0$$$ seconds); open the chest if he's in the same point as the chest and has picked the key up (spending $$$0$$$ seconds). Monocarp can carry the chest, but the chest is pretty heavy. He knows that he can carry it for at most $$$k$$$ seconds in total (putting it down and picking it back up doesn't reset his stamina). What's the smallest time required for Monocarp to open the chest? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0β the number of testcases. The only line of each testcase contains three integers $$$x, y$$$ and $$$k$$$ ($$$1 le x, y le 100$$$; $$$x eq y$$$; $$$0 le k le 100$$$)xa0β the initial point of the chest, the point where the key is located, and the maximum time Monocarp can carry the chest for. Output For each testcase, print a single integerxa0β the smallest time required for Monocarp to open the chest. Note In the first testcase, Monocarp can open the chest in $$$7$$$ seconds with the following sequence of moves: go $$$5$$$ times to the right ($$$5$$$ seconds); pick up the chest ($$$0$$$ seconds); go $$$2$$$ times to the right ($$$2$$$ seconds); pick up the key ($$$0$$$ seconds); put the chest down ($$$0$$$ seconds); open the chest ($$$0$$$ seconds). He only carries the chest for $$$2$$$ seconds, which he has the stamina for. In the second testcase, Monocarp can pick up the key on his way to the chest. In the third testcase, Monocarp can't use the strategy from the first testcase because he would have to carry the chest for $$$3$$$ seconds, while he only has the stamina for $$$2$$$ seconds. Thus, he carries the chest to $$$7$$$, puts it down, moves $$$1$$$ to the right to pick up the key and returns $$$1$$$ left to open the chest. | 800 | true | false | false | false | false | false | false | false | false | false | 944 |
76E | Problem - 76E - Codeforces =============== xa0 ") β the number of points. Each of the following _N_ lines contain two integer numbers _X_ and _Y_ (u2009-u200910u2009000u2009β€u2009_X_,u2009_Y_u2009β€u200910u2009000) β the coordinates of points. Two or more points may coincide. Output The only line of output should contain the required sum of squares of distances between all pairs of points. Examples Input 4 1 1 -1 -1 1 -1 -1 1 Output 32 | 1,700 | true | false | true | false | false | false | false | false | false | false | 9,582 |
756B | A new innovative ticketing systems for public transport is introduced in Bytesburg. Now there is a single travel card for all transport. To make a trip a passenger scan his card and then he is charged according to the fare. The fare is constructed in the following manner. There are three types of tickets: 1. a ticket for one trip costs 20 byteland rubles, 2. a ticket for 90 minutes costs 50 byteland rubles, 3. a ticket for one day (1440 minutes) costs 120 byteland rubles. Note that a ticket for _x_ minutes activated at time _t_ can be used for trips started in time range from _t_ to _t_u2009+u2009_x_u2009-u20091, inclusive. Assume that all trips take exactly one minute. To simplify the choice for the passenger, the system automatically chooses the optimal tickets. After each trip starts, the system analyses all the previous trips and the current trip and chooses a set of tickets for these trips with a minimum total cost. Let the minimum total cost of tickets to cover all trips from the first to the current is _a_, and the total sum charged before is _b_. Then the system charges the passenger the sum _a_u2009-u2009_b_. You have to write a program that, for given trips made by a passenger, calculates the sum the passenger is charged after each trip. Input The first line of input contains integer number _n_ (1u2009β€u2009_n_u2009β€u2009105)xa0β the number of trips made by passenger. Each of the following _n_ lines contains the time of trip _t__i_ (0u2009β€u2009_t__i_u2009β€u2009109), measured in minutes from the time of starting the system. All _t__i_ are different, given in ascending order, i.xa0e. _t__i_u2009+u20091u2009>u2009_t__i_ holds for all 1u2009β€u2009_i_u2009<u2009_n_. Output Output _n_ integers. For each trip, print the sum the passenger is charged after it. Examples Input 10 13 45 46 60 103 115 126 150 256 516 Note In the first example, the system works as follows: for the first and second trips it is cheaper to pay for two one-trip tickets, so each time 20 rubles is charged, after the third trip the system understands that it would be cheaper to buy a ticket for 90 minutes. This ticket costs 50 rubles, and the passenger had already paid 40 rubles, so it is necessary to charge 10 rubles only. | 1,600 | false | false | false | true | false | false | false | true | false | false | 6,769 |
1158D | Vasya has $$$n$$$ different points $$$A_1, A_2, ldots A_n$$$ on the plane. No three of them lie on the same line He wants to place them in some order $$$A_{p_1}, A_{p_2}, ldots, A_{p_n}$$$, where $$$p_1, p_2, ldots, p_n$$$xa0β some permutation of integers from $$$1$$$ to $$$n$$$. After doing so, he will draw oriented polygonal line on these points, drawing oriented segments from each point to the next in the chosen order. So, for all $$$1 leq i leq n-1$$$ he will draw oriented segment from point $$$A_{p_i}$$$ to point $$$A_{p_{i+1}}$$$. He wants to make this polygonal line satisfying $$$2$$$ conditions: it will be non-self-intersecting, so any $$$2$$$ segments which are not neighbors don't have common points. it will be winding. Vasya has a string $$$s$$$, consisting of $$$(n-2)$$$ symbols "L" or "R". Let's call an oriented polygonal line winding, if its $$$i$$$-th turn left, if $$$s_i = $$$ "L" and right, if $$$s_i = $$$ "R". More formally: $$$i$$$-th turn will be in point $$$A_{p_{i+1}}$$$, where oriented segment from point $$$A_{p_i}$$$ to point $$$A_{p_{i+1}}$$$ changes to oriented segment from point $$$A_{p_{i+1}}$$$ to point $$$A_{p_{i+2}}$$$. Let's define vectors $$$overrightarrow{v_1} = overrightarrow{A_{p_i} A_{p_{i+1}}}$$$ and $$$overrightarrow{v_2} = overrightarrow{A_{p_{i+1}} A_{p_{i+2}}}$$$. Then if in order to rotate the vector $$$overrightarrow{v_1}$$$ by the smallest possible angle, so that its direction coincides with the direction of the vector $$$overrightarrow{v_2}$$$ we need to make a turn counterclockwise, then we say that $$$i$$$-th turn is to the left, and otherwise to the right. For better understanding look at this pictures with some examples of turns: There are left turns on this picture There are right turns on this picture You are given coordinates of the points $$$A_1, A_2, ldots A_n$$$ on the plane and string $$$s$$$. Find a permutation $$$p_1, p_2, ldots, p_n$$$ of the integers from $$$1$$$ to $$$n$$$, such that the polygonal line, drawn by Vasya satisfy two necessary conditions. Input The first line contains one integer $$$n$$$xa0β the number of points ($$$3 leq n leq 2000$$$). Next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$, divided by spacexa0β coordinates of the point $$$A_i$$$ on the plane ($$$-10^9 leq x_i, y_i leq 10^9$$$). The last line contains a string $$$s$$$ consisting of symbols "L" and "R" with length $$$(n-2)$$$. It is guaranteed that all points are different and no three points lie at the same line. Output If the satisfying permutation doesn't exists, print $$$-1$$$. In the other case, print $$$n$$$ numbers $$$p_1, p_2, ldots, p_n$$$xa0β the permutation which was found ($$$1 leq p_i leq n$$$ and all $$$p_1, p_2, ldots, p_n$$$ are different). If there exists more than one solution, you can find any. Note This is the picture with the polygonal line from the $$$1$$$ test: As we see, this polygonal line is non-self-intersecting and winding, because the turn in point $$$2$$$ is left. This is the picture with the polygonal line from the $$$2$$$ test: | 2,600 | true | true | false | false | false | true | false | false | false | false | 4,912 |
1175A | Problem - 1175A - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags implementation math *900 No tag edit access β Contest materials β the number of queries. The only line of each query contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 10^{18}$$$, $$$2 le k le 10^{18}$$$). Output For each query print the minimum number of steps to reach $$$0$$$ from $$$n$$$ in single line. Example Input 2 59 3 1000000000000000000 10 Output 8 19 Note Steps for the first test case are: $$$59 ightarrow 58 ightarrow 57 ightarrow 19 ightarrow 18 ightarrow 6 ightarrow 2 ightarrow 1 ightarrow 0$$$. In the second test case you have to divide $$$n$$$ by $$$k$$$ $$$18$$$ times and then decrease $$$n$$$ by $$$1$$$. | 900 | true | false | true | false | false | false | false | false | false | false | 4,845 |
1141F2 | This problem is given in two editions, which differ exclusively in the constraints on the number $$$n$$$. You are given an array of integers $$$a[1], a[2], dots, a[n].$$$ A block is a sequence of contiguous (consecutive) elements $$$a[l], a[l+1], dots, a[r]$$$ ($$$1 le l le r le n$$$). Thus, a block is defined by a pair of indices $$$(l, r)$$$. Find a set of blocks $$$(l_1, r_1), (l_2, r_2), dots, (l_k, r_k)$$$ such that: They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks $$$(l_i, r_i)$$$ and $$$(l_j, r_j$$$) where $$$i eq j$$$ either $$$r_i < l_j$$$ or $$$r_j < l_i$$$. For each block the sum of its elements is the same. Formally, $$$$$$a[l_1]+a[l_1+1]+dots+a[r_1]=a[l_2]+a[l_2+1]+dots+a[r_2]=$$$$$$ $$$$$$dots =$$$$$$ $$$$$$a[l_k]+a[l_k+1]+dots+a[r_k].$$$$$$ The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks $$$(l_1', r_1'), (l_2', r_2'), dots, (l_{k'}', r_{k'}')$$$ satisfying the above two requirements with $$$k' > k$$$. The picture corresponds to the first example. Blue boxes illustrate blocks. Write a program to find such a set of blocks. Input The first line contains integer $$$n$$$ ($$$1 le n le 1500$$$) β the length of the given array. The second line contains the sequence of elements $$$a[1], a[2], dots, a[n]$$$ ($$$-10^5 le a_i le 10^5$$$). Output In the first line print the integer $$$k$$$ ($$$1 le k le n$$$). The following $$$k$$$ lines should contain blocks, one per line. In each line print a pair of indices $$$l_i, r_i$$$ ($$$1 le l_i le r_i le n$$$) β the bounds of the $$$i$$$-th block. You can print blocks in any order. If there are multiple answers, print any of them. Examples Input 11 -5 -4 -3 -2 -1 0 1 2 3 4 5 | 1,900 | false | true | false | false | true | false | false | false | false | false | 5,015 |
1689D | Lena is a beautiful girl who likes logical puzzles. As a gift for her birthday, Lena got a matrix puzzle! The matrix consists of $$$n$$$ rows and $$$m$$$ columns, and each cell is either black or white. The coordinates $$$(i,j)$$$ denote the cell which belongs to the $$$i$$$-th row and $$$j$$$-th column for every $$$1leq i leq n$$$ and $$$1leq j leq m$$$. To solve the puzzle, Lena has to choose a cell that minimizes the Manhattan distance to the farthest black cell from the chosen cell. More formally, let there be $$$k ge 1$$$ black cells in the matrix with coordinates $$$(x_i,y_i)$$$ for every $$$1leq i leq k$$$. Lena should choose a cell $$$(a,b)$$$ that minimizes $$$$$$max_{i=1}^{k}(a-x_i+b-y_i).$$$$$$ As Lena has no skill, she asked you for help. Will you tell her the optimal cell to choose? Input There are several test cases in the input data. The first line contains a single integer $$$t$$$ ($$$1leq tleq 10,000$$$)xa0β the number of test cases. This is followed by the test cases description. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2leq n,m leq 1000$$$) xa0β the dimensions of the matrix. The following $$$n$$$ lines contain $$$m$$$ characters each, each character is either 'W' or 'B'. The $$$j$$$-th character in the $$$i$$$-th of these lines is 'W' if the cell $$$(i,j)$$$ is white, and 'B' if the cell $$$(i,j)$$$ is black. It is guaranteed that at least one black cell exists. It is guaranteed that the sum of $$$ncdot m$$$ does not exceed $$$10^6$$$. Output For each test case, output the optimal cell $$$(a,b)$$$ to choose. If multiple answers exist, output any. Example Input 5 3 2 BW WW WB 3 3 WWB WBW BWW 2 3 BBB BBB 5 5 BWBWB WBWBW BWBWB WBWBW BWBWB 9 9 WWWWWWWWW WWWWWWWWW BWWWWWWWW WWWWWWWWW WWWWBWWWW WWWWWWWWW WWWWWWWWW WWWWWWWWW WWWWWWWWB Output 2 1 2 2 1 2 3 3 6 5 Note In the first test case the two black cells have coordinates $$$(1,1)$$$ and $$$(3,2)$$$. The four optimal cells are $$$(1,2)$$$, $$$(2,1)$$$, $$$(2,2)$$$ and $$$(3,1)$$$. It can be shown that no other cell minimizes the maximum Manhattan distance to every black cell. In the second test case it is optimal to choose the black cell $$$(2,2)$$$ with maximum Manhattan distance being $$$2$$$. | 1,900 | false | false | false | true | true | false | false | false | false | false | 2,145 |
60A | Problem - 60A - Codeforces =============== xa0 , "Hidden to the right of the _i_-th box" ("To the right of _i_"). Such hints mean that there are no flakes in the _i_-th box as well. The Cereal Guy wants to know the minimal number of boxes he necessarily needs to check to find the flakes considering all the hints. Or he wants to find out that the hints are contradictory and the roommate lied to him, that is, no box has the flakes. Input The first line contains two integers _n_ and _m_ (1u2009β€u2009_n_u2009β€u20091000,u20090u2009β€u2009_m_u2009β€u20091000) which represent the number of boxes and the number of hints correspondingly. Next _m_ lines contain hints like "To the left of _i_" and "To the right of _i_", where _i_ is integer (1u2009β€u2009_i_u2009β€u2009_n_). The hints may coincide. Output The answer should contain exactly one integer β the number of boxes that should necessarily be checked or "-1" if the hints are contradictory. Examples Input 2 1 To the left of 2 Output 1 Input 3 2 To the right of 1 To the right of 2 Output 1 Input 3 1 To the left of 3 Output 2 Input 3 2 To the left of 2 To the right of 1 Output -1 | 1,300 | false | false | true | false | false | false | false | false | false | false | 9,675 |
123E | A maze is represented by a tree (an undirected graph, where exactly one way exists between each pair of vertices). In the maze the entrance vertex and the exit vertex are chosen with some probability. The exit from the maze is sought by Deep First Search. If there are several possible ways to move, the move is chosen equiprobably. Consider the following pseudo-code: DFS(x) if x == exit vertex then finish search flag[x] <- TRUE random shuffle the vertices' order in V(x) // here all permutations have equal probability to be chosen for i <- 1 to length[V] do if flag[V[i]] = FALSE then count++; DFS(y); count++; _V_(_x_) is the list vertices adjacent to _x_. The _flag_ array is initially filled as FALSE. _DFS_ initially starts with a parameter of an entrance vertex. When the search is finished, variable _count_ will contain the number of moves. Your task is to count the mathematical expectation of the number of moves one has to do to exit the maze. Input The first line determines the number of vertices in the graph _n_ (1u2009β€u2009_n_u2009β€u2009105). The next _n_u2009-u20091 lines contain pairs of integers _a__i_ and _b__i_, which show the existence of an edge between _a__i_ and _b__i_ vertices (1u2009β€u2009_a__i_,u2009_b__i_u2009β€u2009_n_). It is guaranteed that the given graph is a tree. Next _n_ lines contain pairs of non-negative numbers _x__i_ and _y__i_, which represent the probability of choosing the _i_-th vertex as an entrance and exit correspondingly. The probabilities to choose vertex _i_ as an entrance and an exit equal and correspondingly. The sum of all _x__i_ and the sum of all _y__i_ are positive and do not exceed 106. Output Print the expectation of the number of moves. The absolute or relative error should not exceed 10u2009-u20099. Examples Output 1.00000000000000000000 Output 2.00000000000000000000 Input 7 1 2 1 3 2 4 2 5 3 6 3 7 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Note In the first sample the entrance vertex is always 1 and the exit vertex is always 2. In the second sample the entrance vertex is always 1 and the exit vertex with the probability of 2/5 will be 2 of with the probability if 3/5 will be 3. The mathematical expectations for the exit vertices 2 and 3 will be equal (symmetrical cases). During the first move one can go to the exit vertex with the probability of 0.5 or to go to a vertex that's not the exit vertex with the probability of 0.5. In the first case the number of moves equals 1, in the second one it equals 3. The total mathematical expectation is counted as 2u2009/u20095u2009Γu2009(1u2009Γu20090.5u2009+u20093u2009Γu20090.5)u2009+u20093u2009/u20095u2009Γu2009(1u2009Γu20090.5u2009+u20093u2009Γu20090.5) | 2,500 | false | false | false | true | false | false | false | false | false | false | 9,385 |
1610A | You are playing a game on a $$$n imes m$$$ grid, in which the computer has selected some cell $$$(x, y)$$$ of the grid, and you have to determine which one. To do so, you will choose some $$$k$$$ and some $$$k$$$ cells $$$(x_1, y_1),, (x_2, y_2), ldots, (x_k, y_k)$$$, and give them to the computer. In response, you will get $$$k$$$ numbers $$$b_1,, b_2, ldots b_k$$$, where $$$b_i$$$ is the manhattan distance from $$$(x_i, y_i)$$$ to the hidden cell $$$(x, y)$$$ (so you know which distance corresponds to which of $$$k$$$ input cells). After receiving these $$$b_1,, b_2, ldots, b_k$$$, you have to be able to determine the hidden cell. What is the smallest $$$k$$$ for which is it possible to always guess the hidden cell correctly, no matter what cell computer chooses? As a reminder, the manhattan distance between cells $$$(a_1, b_1)$$$ and $$$(a_2, b_2)$$$ is equal to $$$a_1-a_2+b_1-b_2$$$. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. The description of test cases follows. The single line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 10^9$$$)xa0β the number of rows and the number of columns in the grid. Note In the first test case, the smallest such $$$k$$$ is $$$2$$$, for which you can choose, for example, cells $$$(1, 1)$$$ and $$$(2, 1)$$$. Note that you can't choose cells $$$(1, 1)$$$ and $$$(2, 3)$$$ for $$$k = 2$$$, as both cells $$$(1, 2)$$$ and $$$(2, 1)$$$ would give $$$b_1 = 1, b_2 = 2$$$, so we wouldn't be able to determine which cell is hidden if computer selects one of those. In the second test case, you should choose $$$k = 1$$$, for it you can choose cell $$$(3, 1)$$$ or $$$(1, 1)$$$. | 900 | true | false | false | false | false | false | false | false | false | false | 2,600 |
1291F | This is the easy version of the problem. You can find the hard version in the Div. 1 contest. Both versions only differ in the number of times you can ask your friend to taste coffee. This is an interactive problem. You're considering moving to another city, where one of your friends already lives. There are $$$n$$$ cafΓ©s in this city, where $$$n$$$ is a power of two. The $$$i$$$-th cafΓ© produces a single variety of coffee $$$a_i$$$. As you're a coffee-lover, before deciding to move or not, you want to know the number $$$d$$$ of distinct varieties of coffees produced in this city. You don't know the values $$$a_1, ldots, a_n$$$. Fortunately, your friend has a memory of size $$$k$$$, where $$$k$$$ is a power of two. Once per day, you can ask him to taste a cup of coffee produced by the cafΓ© $$$c$$$, and he will tell you if he tasted a similar coffee during the last $$$k$$$ days. You can also ask him to take a medication that will reset his memory. He will forget all previous cups of coffee tasted. You can reset his memory at most $$$30 000$$$ times. More formally, the memory of your friend is a queue $$$S$$$. Doing a query on cafΓ© $$$c$$$ will: Tell you if $$$a_c$$$ is in $$$S$$$; Add $$$a_c$$$ at the back of $$$S$$$; If $$$S > k$$$, pop the front element of $$$S$$$. Doing a reset request will pop all elements out of $$$S$$$. Your friend can taste at most $$$dfrac{2n^2}{k}$$$ cups of coffee in total. Find the diversity $$$d$$$ (number of distinct values in the array $$$a$$$). Note that asking your friend to reset his memory does not count towards the number of times you ask your friend to taste a cup of coffee. In some test cases the behavior of the interactor is adaptive. It means that the array $$$a$$$ may be not fixed before the start of the interaction and may depend on your queries. It is guaranteed that at any moment of the interaction, there is at least one array $$$a$$$ consistent with all the answers given so far. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 1024$$$, $$$k$$$ and $$$n$$$ are powers of two). It is guaranteed that $$$dfrac{2n^2}{k} le 20 000$$$. Interaction You begin the interaction by reading $$$n$$$ and $$$k$$$. To ask your friend to taste a cup of coffee produced by the cafΓ© $$$c$$$, in a separate line output? $$$c$$$ Where $$$c$$$ must satisfy $$$1 le c le n$$$. Don't forget to flush, to get the answer. In response, you will receive a single letter Y (yes) or N (no), telling you if variety $$$a_c$$$ is one of the last $$$k$$$ varieties of coffee in his memory. To reset the memory of your friend, in a separate line output the single letter R in upper case. You can do this operation at most $$$30 000$$$ times. When you determine the number $$$d$$$ of different coffee varieties, output! $$$d$$$ In case your query is invalid, you asked more than $$$frac{2n^2}{k}$$$ queries of type ? or you asked more than $$$30 000$$$ queries of type R, the program will print the letter E and will finish interaction. You will receive a Wrong Answer verdict. Make sure to exit immediately to avoid getting other verdicts. After printing a query do not forget to output 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. Hack format The first line should contain the word fixed The second line should contain two integers $$$n$$$ and $$$k$$$, separated by space ($$$1 le k le n le 1024$$$, $$$k$$$ and $$$n$$$ are powers of two). It must hold that $$$dfrac{2n^2}{k} le 20 000$$$. The third line should contain $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$, separated by spaces ($$$1 le a_i le n$$$). Examples Output ? 1 ? 2 ? 3 ? 4 R ? 4 ? 1 ? 2 ! 3 Output ? 2 ? 6 ? 4 ? 5 ? 2 ? 5 ! 6 Note In the first example, the array is $$$a = [1, 4, 1, 3]$$$. The city produces $$$3$$$ different varieties of coffee ($$$1$$$, $$$3$$$ and $$$4$$$). The successive varieties of coffee tasted by your friend are $$$1, 4, extbf{1}, 3, 3, 1, 4$$$ (bold answers correspond to Y answers). Note that between the two ? 4 asks, there is a reset memory request R, so the answer to the second ? 4 ask is N. Had there been no reset memory request, the answer to the second ? 4 ask is Y. In the second example, the array is $$$a = [1, 2, 3, 4, 5, 6, 6, 6]$$$. The city produces $$$6$$$ different varieties of coffee. The successive varieties of coffee tasted by your friend are $$$2, 6, 4, 5, extbf{2}, extbf{5}$$$. | 2,800 | false | false | false | false | false | false | false | false | false | true | 4,238 |
1627E | Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health. The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders. If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$j-k cdot x_i$$$ health points. Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav. Input The first line of input contains $$$t$$$ ($$$1 leq t leq 5 cdot 10^4$$$)xa0β the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 leq n, m leq 10^5$$$; $$$1 leq k leq 10^5$$$)xa0β the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, dots, x_n$$$ ($$$1 leq x_i leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 leq a_i < c_i leq n$$$; $$$1 leq b_i, d_i leq m$$$; $$$1 leq h_i leq 10^6$$$) xa0β the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$. Output Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes). Example Input 4 5 3 3 5 17 8 1 4 1 3 3 3 4 3 1 5 2 5 3 2 5 1 6 6 3 3 5 17 8 1 4 2 1 3 3 3 4 3 1 5 2 5 3 2 5 1 6 5 3 1 5 17 8 1 4 1 3 5 3 100 5 5 5 3 2 3 7 5 3 5 4 2 1 2 2 5 4 5 4 4 5 2 3 1 2 4 2 2 3 3 5 2 4 Output 16 NO ESCAPE -90 27 Note The figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ begin{align*} &mathrel{phantom{=}} x_1 cdot 1-3 - h_1 + x_3 cdot 3-2 - h_3 + x_5 cdot 1-3 &= 5 cdot 2 - 4 + 8 cdot 1 - 6 + 4 cdot 2 &= 16. end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ begin{align*} &mathrel{phantom{=}} x_1 cdot 1-3 - h_1 + x_3 cdot 3-1 - h_2 + a_5 cdot 2-3 &= 5 cdot 2 - 4 + 8 cdot 2 - 5 + 4 cdot 1 &= 21. end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$. In the second test case, there is no path to $$$(n, m)$$$. In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path). | 2,200 | false | false | true | true | true | false | false | false | false | false | 2,477 |
196A | Problem - 196A - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags greedy strings *1100 No tag edit access β Contest materials a subsequence of string _s_u2009=u2009_s_1_s_2... _s__s_. String _x_u2009=u2009_x_1_x_2... _x__x_ is lexicographically larger than string _y_u2009=u2009_y_1_y_2... _y__y_, if either _x_u2009>u2009_y_ and _x_1u2009=u2009_y_1,u2009_x_2u2009=u2009_y_2,u2009... ,u2009_x__y_u2009=u2009_y__y_, or exists such number _r_ (_r_u2009<u2009_x_,u2009_r_u2009<u2009_y_), that _x_1u2009=u2009_y_1,u2009_x_2u2009=u2009_y_2,u2009... ,u2009_x__r_u2009=u2009_y__r_ and _x__r_u2009+u20091u2009>u2009_y__r_u2009+u20091. Characters in lines are compared like their ASCII codes. Input The single line contains a non-empty string _s_, consisting only of lowercase English letters. The string's length doesn't exceed 105. Output Print the lexicographically maximum subsequence of string _s_. Examples Input ababba Output bbba Input abbcbccacbbcbaaba Output cccccbba Note Let's look at samples and see what the sought subsequences look like (they are marked with uppercase bold letters). The first sample: aBaBBA The second sample: abbCbCCaCbbCBaaBA | 1,100 | false | true | false | false | false | false | false | false | false | false | 9,058 |
148C | Β«Next pleaseΒ», β the princess called and cast an estimating glance at the next groom. The princess intends to choose the most worthy groom, this is, the richest one. Whenever she sees a groom who is more rich than each of the previous ones, she says a measured Β«Oh...Β». Whenever the groom is richer than all previous ones added together, she exclaims Β«Wow!Β» (no Β«Oh...Β» in this case). At the sight of the first groom the princess stays calm and says nothing. The fortune of each groom is described with an integer between 1 and 50000. You know that during the day the princess saw _n_ grooms, said Β«Oh...Β» exactly _a_ times and exclaimed Β«Wow!Β» exactly _b_ times. Your task is to output a sequence of _n_ integers _t_1,u2009_t_2,u2009...,u2009_t__n_, where _t__i_ describes the fortune of _i_-th groom. If several sequences are possible, output any of them. If no sequence exists that would satisfy all the requirements, output a single number -1. Input The only line of input data contains three integer numbers _n_,u2009_a_ and _b_ (1u2009β€u2009_n_u2009β€u2009100,u20090u2009β€u2009_a_,u2009_b_u2009β€u200915,u2009_n_u2009>u2009_a_u2009+u2009_b_), separated with single spaces. Output Output any sequence of integers _t_1,u2009_t_2,u2009...,u2009_t__n_, where _t__i_ (1u2009β€u2009_t__i_u2009β€u200950000) is the fortune of _i_-th groom, that satisfies the given constraints. If no sequence exists that would satisfy all the requirements, output a single number -1. Examples Output 5 1 3 6 16 35 46 4 200 99 Note Let's have a closer look at the answer for the first sample test. The princess said Β«Oh...Β» (highlighted in bold): 5 1 3 6 16 35 46 4 200 99. The princess exclaimed Β«Wow!Β» (highlighted in bold): 5 1 3 6 16 35 46 4 200 99. | 1,700 | false | true | false | false | false | true | false | false | false | false | 9,284 |
134C | There are _n_ players sitting at a round table. All of them have _s_ cards of _n_ colors in total. Besides, initially the first person had cards of only the first color, the second one had cards of only the second color and so on. They can swap the cards by the following rules: as the players swap, a player can give a card of his color only; a player can't accept a card of a color he already has (particularly, he can't take cards of his color, no matter whether he has given out all of them or not); during one swap a pair of people swaps cards (each person gives one card and takes one card). The aim of all _n_ people is as follows: each of them should give out all the cards he had initially (that is, all cards of his color). Your task is to denote whether such sequence of swaps is possible. If the answer is positive, you should list all the swaps. Input The first line contains integers _n_ (1u2009β€u2009_n_u2009β€u2009200000) and _s_ (1u2009β€u2009_s_u2009β€u2009200000). The second line contains _n_ numbers, the _i_-th number stands for how many cards the _i_-th player has by the moment the game starts. It is possible that a player has no cards initially. Output On the first line print "No" if such sequence of swaps is impossible. Otherwise, print "Yes". If the answer is positive, next print number _k_ β the number of the swaps. Then on _k_ lines describe the swaps by pairs of indices of the swapping players. Print the swaps and the numbers of the swaps in any order. Examples Output Yes 6 6 5 6 4 6 3 5 4 5 3 2 1 | 2,200 | false | true | false | false | false | true | false | false | false | true | 9,338 |
1114A | For simplicity, we'll assume that there are only three types of grapes: green grapes, purple grapes and black grapes. Andrew, Dmitry and Michal are all grapes' lovers, however their preferences of grapes are different. To make all of them happy, the following should happen: Andrew, Dmitry and Michal should eat at least $$$x$$$, $$$y$$$ and $$$z$$$ grapes, respectively. Andrew has an extreme affinity for green grapes, thus he will eat green grapes and green grapes only. On the other hand, Dmitry is not a fan of black grapesxa0β any types of grapes except black would do for him. In other words, Dmitry can eat green and purple grapes. Michal has a common tastexa0β he enjoys grapes in general and will be pleased with any types of grapes, as long as the quantity is sufficient. Knowing that his friends are so fond of grapes, Aki decided to host a grape party with them. He has prepared a box with $$$a$$$ green grapes, $$$b$$$ purple grapes and $$$c$$$ black grapes. However, Aki isn't sure if the box he prepared contains enough grapes to make everyone happy. Can you please find out whether it's possible to distribute grapes so that everyone is happy or Aki has to buy some more grapes? It is not required to distribute all the grapes, so it's possible that some of them will remain unused. Input The first line contains three integers $$$x$$$, $$$y$$$ and $$$z$$$ ($$$1 le x, y, z le 10^5$$$)xa0β the number of grapes Andrew, Dmitry and Michal want to eat. The second line contains three integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$1 le a, b, c le 10^5$$$)xa0β the number of green, purple and black grapes in the box. Output If there is a grape distribution that allows everyone to be happy, print "YES", otherwise print "NO". Note In the first example, there is only one possible distribution: Andrew should take $$$1$$$ green grape, Dmitry should take $$$3$$$ remaining green grapes and $$$3$$$ purple grapes, and Michal will take $$$2$$$ out of $$$3$$$ available black grapes. In the second test, there is no possible distribution, since Andrew is not be able to eat enough green grapes. :( | 800 | false | true | true | false | false | false | true | false | false | false | 5,134 |
133B | Unary is a minimalistic Brainfuck dialect in which programs are written using only one token. Brainfuck programs use 8 commands: "+", "-", "[", "]", "<", ">", "." and "," (their meaning is not important for the purposes of this problem). Unary programs are created from Brainfuck programs using the following algorithm. First, replace each command with a corresponding binary code, using the following conversion table: ">" u2009βu2009 1000, "<" u2009βu2009 1001, "+" u2009βu2009 1010, "-" u2009βu2009 1011, "." u2009βu2009 1100, "," u2009βu2009 1101, "[" u2009βu2009 1110, "]" u2009βu2009 1111. Next, concatenate the resulting binary codes into one binary number in the same order as in the program. Finally, write this number using unary numeral system β this is the Unary program equivalent to the original Brainfuck one. You are given a Brainfuck program. Your task is to calculate the size of the equivalent Unary program, and print it modulo 1000003 (106u2009+u20093). Input The input will consist of a single line _p_ which gives a Brainfuck program. String _p_ will contain between 1 and 100 characters, inclusive. Each character of _p_ will be "+", "-", "[", "]", "<", ">", "." or ",". Output Output the size of the equivalent Unary program modulo 1000003 (106u2009+u20093). Note To write a number _n_ in unary numeral system, one simply has to write 1 _n_ times. For example, 5 written in unary system will be 11111. In the first example replacing Brainfuck commands with binary code will give us 1101 1100. After we concatenate the codes, we'll get 11011100 in binary system, or 220 in decimal. That's exactly the number of tokens in the equivalent Unary program. | 1,200 | false | false | true | false | false | false | false | false | false | false | 9,341 |
988D | There are $$$n$$$ distinct points on a coordinate line, the coordinate of $$$i$$$-th point equals to $$$x_i$$$. Choose a subset of the given set of points such that the distance between each pair of points in a subset is an integral power of two. It is necessary to consider each pair of points, not only adjacent. Note that any subset containing one element satisfies the condition above. Among all these subsets, choose a subset with maximum possible size. In other words, you have to choose the maximum possible number of points $$$x_{i_1}, x_{i_2}, dots, x_{i_m}$$$ such that for each pair $$$x_{i_j}$$$, $$$x_{i_k}$$$ it is true that $$$x_{i_j} - x_{i_k} = 2^d$$$ where $$$d$$$ is some non-negative integer number (not necessarily the same for each pair of points). Input The first line contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) β the number of points. The second line contains $$$n$$$ pairwise distinct integers $$$x_1, x_2, dots, x_n$$$ ($$$-10^9 le x_i le 10^9$$$) β the coordinates of points. Output In the first line print $$$m$$$ β the maximum possible number of points in a subset that satisfies the conditions described above. In the second line print $$$m$$$ integers β the coordinates of points in the subset you have chosen. If there are multiple answers, print any of them. Note In the first example the answer is $$$[7, 3, 5]$$$. Note, that $$$7-3=4=2^2$$$, $$$7-5=2=2^1$$$ and $$$3-5=2=2^1$$$. You can't find a subset having more points satisfying the required property. | 1,800 | true | false | false | false | false | false | true | false | false | false | 5,760 |
955B | Problem - 955B - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags implementation *1400 No tag edit access β Contest materials ") . For example, ababa is adorable (you can transform it to aaabb, where the first three letters form a group of _a_-s and others β a group of _b_-s), but cccc is not since in each possible consequent partition letters in these two groups coincide. You're given a string _s_. Check whether it can be split into two non-empty subsequences such that the strings formed by these subsequences are adorable. Here a subsequence is an arbitrary set of indexes of the string. Input The only line contains _s_ (1u2009β€u2009_s_u2009β€u2009105) consisting of lowercase latin letters. Output Print Β«YesΒ» if the string can be split according to the criteria above or Β«NoΒ» otherwise. Each letter can be printed in arbitrary case. Examples Input ababa Output Yes Input zzcxx Output Yes Input yeee Output No Note In sample case two zzcxx can be split into subsequences zc and zxx each of which is adorable. There's no suitable partition in sample case three. | 1,400 | false | false | true | false | false | false | false | false | false | false | 5,899 |
1011A | Natasha is going to fly to Mars. She needs to build a rocket, which consists of several stages in some order. Each of the stages is defined by a lowercase Latin letter. This way, the rocket can be described by the stringxa0β concatenation of letters, which correspond to the stages. There are $$$n$$$ stages available. The rocket must contain exactly $$$k$$$ of them. Stages in the rocket should be ordered by their weight. So, after the stage with some letter can go only stage with a letter, which is at least two positions after in the alphabet (skipping one letter in between, or even more). For example, after letter 'c' can't go letters 'a', 'b', 'c' and 'd', but can go letters 'e', 'f', ..., 'z'. For the rocket to fly as far as possible, its weight should be minimal. The weight of the rocket is equal to the sum of the weights of its stages. The weight of the stage is the number of its letter in the alphabet. For example, the stage 'a 'weighs one ton,' b 'weighs two tons, and' z'xa0β $$$26$$$ tons. Build the rocket with the minimal weight or determine, that it is impossible to build a rocket at all. Each stage can be used at most once. Input The first line of input contains two integersxa0β $$$n$$$ and $$$k$$$ ($$$1 le k le n le 50$$$)xa0β the number of available stages and the number of stages to use in the rocket. The second line contains string $$$s$$$, which consists of exactly $$$n$$$ lowercase Latin letters. Each letter defines a new stage, which can be used to build the rocket. Each stage can be used at most once. Note In the first example, the following rockets satisfy the condition: "adx" (weight is $$$1+4+24=29$$$); "ady" (weight is $$$1+4+25=30$$$); "bdx" (weight is $$$2+4+24=30$$$); "bdy" (weight is $$$2+4+25=31$$$). Rocket "adx" has the minimal weight, so the answer is $$$29$$$. In the second example, target rocket is "belo". Its weight is $$$2+5+12+15=34$$$. In the third example, $$$n=k=2$$$, so the rocket must have both stages: 'a' and 'b'. This rocket doesn't satisfy the condition, because these letters are adjacent in the alphabet. Answer is -1. | 900 | false | true | true | false | false | false | false | false | true | false | 5,629 |
44H | Alas, finding one's true love is not easy. Masha has been unsuccessful in that yet. Her friend Dasha told Masha about a way to determine the phone number of one's Prince Charming through arithmancy. The phone number is divined like that. First one needs to write down one's own phone numbers. For example, let's suppose that Masha's phone number is 12345. After that one should write her favorite digit from 0 to 9 under the first digit of her number. That will be the first digit of the needed number. For example, Masha's favorite digit is 9. The second digit is determined as a half sum of the second digit of Masha's number and the already written down first digit from her beloved one's number. In this case the arithmetic average equals to (2u2009+u20099)u2009/u20092u2009=u20095.5. Masha can round the number up or down, depending on her wishes. For example, she chooses the digit 5. Having written down the resulting digit under the second digit of her number, Masha moves to finding the third digit in the same way, i.e. finding the half sum the the third digit of her number and the second digit of the new number. The result is (5u2009+u20093)u2009/u20092u2009=u20094. In this case the answer is unique. Thus, every _i_-th digit is determined as an arithmetic average of the _i_-th digit of Masha's number and the _i_u2009-u20091-th digit of her true love's number. If needed, the digit can be rounded up or down. For example, Masha can get: 12345 95444 Unfortunately, when Masha tried dialing the number, she got disappointed: as it turned out, the number was unavailable or outside the coverage area. But Masha won't give up. Perhaps, she rounded to a wrong digit or chose the first digit badly. That's why she keeps finding more and more new numbers and calling them. Count the number of numbers Masha calls. Masha calls all the possible numbers that can be found by the described means of arithmancy, except for, perhaps, her own one. | 1,700 | false | false | false | true | false | false | false | false | false | false | 9,762 |
1832D2 | The only difference between easy and hard versions is the maximum values of $$$n$$$ and $$$q$$$. You are given an array, consisting of $$$n$$$ integers. Initially, all elements are red. You can apply the following operation to the array multiple times. During the $$$i$$$-th operation, you select an element of the array; then: if the element is red, it increases by $$$i$$$ and becomes blue; if the element is blue, it decreases by $$$i$$$ and becomes red. The operations are numbered from $$$1$$$, i.u2009e. during the first operation some element is changed by $$$1$$$ and so on. You are asked $$$q$$$ queries of the following form: given an integer $$$k$$$, what can the largest minimum in the array be if you apply exactly $$$k$$$ operations to it? Note that the operations don't affect the array between queries, all queries are asked on the initial array $$$a$$$. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 2 cdot 10^5$$$)xa0β the number of elements in the array and the number of queries. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$). The third line contains $$$q$$$ integers $$$k_1, k_2, dots, k_q$$$ ($$$1 le k_j le 10^9$$$). Output For each query, print a single integerxa0β the largest minimum that the array can have after you apply exactly $$$k$$$ operations to it. Examples Input 4 10 5 2 8 4 1 2 3 4 5 6 7 8 9 10 Output 3 4 5 6 7 8 8 10 8 12 Input 5 10 5 2 8 4 4 1 2 3 4 5 6 7 8 9 10 Output 3 4 5 6 7 8 9 8 11 8 | 2,400 | true | true | true | false | false | true | false | true | false | false | 1,294 |
374D | Dima's spent much time thinking what present to give to Inna and gave her an empty sequence _w_. Now they want to fill sequence _w_ with numbers zero and one. For that, they decided to play an amusing game. Before the game begins, Dima chooses _m_ integers _a_1,u2009_a_2,u2009...,u2009_a__m_ (1u2009β€u2009_a_1u2009<u2009_a_2u2009<u2009...u2009<u2009_a__m_). Then Inna and Dima start playing, that is, adding numbers to sequence _w_. Each new number they choose is added to the end of the sequence. At some moments of time Dima feels that the game is going to end too soon (and he wants to play with Inna as long as possible), so he hits a table hard with his fist. At that the _a_1-th, _a_2-th, _a_3-th, ..., _a__k_-th numbers from the beginning simultaneously fall out of the sequence (the sequence gets _k_ numbers less). Here _k_ is such maximum number that value _a__k_ doesn't exceed the current length of the sequence. If number _a_1 is larger than the current length of _w_, then nothing falls out of the sequence. You are given the chronological sequence of events in the game. Each event is either adding a number to the end of sequence _w_ or Dima's hit on the table. Calculate the sequence _w_ after all these events happen. Input The first line of the input contains two integers _n_ and _m_ (1u2009β€u2009_n_,u2009_m_u2009β€u2009106) showing how many events took place and how many numbers Dima chose. The next line contains _m_ distinct integers _a__i_ (1u2009β€u2009_a__i_u2009β€u2009106) sorted in the increasing order. Next _n_ lines describe the events in the chronological order. Each line contains a single integer: -1, 0 or 1. Number -1 means that Dima hits the table. Number 0 means that Inna and Dima add number 0 to the end of the sequence. Number 1 means that Inna and Dima add number 1 to the end of the sequence. Output In a single line print a sequence of numbers 0 and 1 β the elements of the sequence after all events happen. Print the elements of the sequence in the order from the beginning to the end of the sequence. If after all events the sequence ends up empty, print "Poor stack!". Examples Input 10 3 1 3 6 -1 1 1 0 0 -1 0 1 -1 1 | 2,000 | false | false | false | true | true | false | false | true | false | false | 8,333 |
879A | Problem - 879A - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags implementation *900 No tag edit access β Contest materials . Borya will get the information about his health from the last doctor. Doctors have a strange working schedule. The doctor _i_ goes to work on the _s__i_-th day and works every _d__i_ day. So, he works on days _s__i_,u2009_s__i_u2009+u2009_d__i_,u2009_s__i_u2009+u20092_d__i_,u2009.... The doctor's appointment takes quite a long time, so Borya can not see more than one doctor per day. What is the minimum time he needs to visit all doctors? Input First line contains an integer _n_ β number of doctors (1u2009β€u2009_n_u2009β€u20091000). Next _n_ lines contain two numbers _s__i_ and _d__i_ (1u2009β€u2009_s__i_,u2009_d__i_u2009β€u20091000). Output Output a single integer β the minimum day at which Borya can visit the last doctor. Examples Input 3 2 2 1 2 2 2 Output 4 Input 2 10 1 6 5 Output 11 Note In the first sample case, Borya can visit all doctors on days 2, 3 and 4. In the second sample case, Borya can visit all doctors on days 10 and 11. | 900 | false | false | true | false | false | false | false | false | false | false | 6,229 |
1857F | You have an array $$$a$$$ of length $$$n$$$. Your task is to answer $$$q$$$ queries: given $$$x,y$$$, find the number of pairs $$$i$$$ and $$$j$$$ ($$$1 le i < j le n$$$) that both $$$a_i + a_j = x$$$ and $$$a_i cdot a_j = y$$$. That is, for the array $$$[1,3,2]$$$ and asking for $$$x=3,y=2$$$ the answer is $$$1$$$: $$$i=1$$$ and $$$j=2$$$ fail because $$$1 + 3 = 4$$$ and not $$$3,$$$ also $$$1 cdot 3=3$$$ and not $$$2$$$; $$$i=1$$$ and $$$j=3$$$ satisfies both conditions; $$$i=2$$$ and $$$j=3$$$ fail because $$$3 + 2 = 5$$$ and not $$$3,$$$ also $$$3 cdot 2=6$$$ and not $$$2$$$; Input The first line contains one integer $$$t$$$ ($$$1le tle 10^4$$$)xa0β the number of test cases. The second line of each test case contains one integer $$$n$$$ ($$$1 le n le 2cdot 10^5$$$)xa0β the length of the array $$$a$$$. The third line of each test case contains $$$n$$$ integers $$$a_1,a_2,dots,a_n$$$ ($$$1 le a_i le 10^9$$$)xa0β array $$$a$$$. The fourth line of each test case contains the integer $$$q$$$ ($$$1 le q le 2cdot 10^5$$$)xa0β the number of requests. The next $$$q$$$ lines contain two numbers each $$$x$$$ and $$$y$$$ ($$$1 le xle 2cdot 10^9,1le yle 10^{18}$$$)xa0β request. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. This is also guaranteed for the sum of $$$q$$$ values. Output For each test case print a line with $$$q$$$ numbers β the answers to the queries. Example Input 3 3 1 3 2 4 3 2 5 6 3 1 5 5 4 1 1 1 1 1 2 1 6 1 4 -2 3 3 3 3 2 -8 -1 -2 7 12 Note For the first test case, let's analyze each pair of numbers separately: pair $$$(a_1,a_2)$$$: $$$a_1 + a_2 = 4$$$, $$$a_1 cdot a_2 = 3$$$ pair $$$(a_1,a_3)$$$: $$$a_1 + a_3 = 3$$$, $$$a_1 cdot a_3 = 2$$$ pair $$$(a_2,a_3)$$$: $$$a_2 + a_3 = 5$$$, $$$a_2 cdot a_3 = 6$$$ From this, we can see that for the first query, the pair $$$(a_1,a_3)$$$ is suitable, for the second query, it is $$$(a_2,a_3)$$$, and there are no suitable pairs for the third and fourth queries. In the second test case, all combinations of pairs are suitable. | 1,600 | true | false | false | false | true | false | false | true | false | false | 1,139 |
1333E | Egor wants to achieve a rating of 1600 points on the well-known chess portal ChessForces and he needs your help! Before you start solving the problem, Egor wants to remind you how the chess pieces move. Chess rook moves along straight lines up and down, left and right, as many squares as it wants. And when it wants, it can stop. The queen walks in all directions vertically and diagonally at any distance. You can see the examples below. To reach the goal, Egor should research the next topic: There is an $$$N imes N$$$ board. Each cell of the board has a number from $$$1$$$ to $$$N ^ 2$$$ in it and numbers in all cells are distinct. In the beginning, some chess figure stands in the cell with the number $$$1$$$. Note that this cell is already considered as visited. After that every move is determined by the following rules: 1. Among all not visited yet cells to which the figure can get in one move, it goes to the cell that has minimal number. 2. If all accessible cells were already visited and some cells are not yet visited, then the figure is teleported to the not visited cell that has minimal number. If this step happens, the piece pays a fee of $$$1$$$ vun. 3. If all cells are already visited, the process is stopped. Egor should find an $$$N imes N$$$ board on which the rook pays strictly less vuns than the queen during the round with this numbering. Help him to find such $$$N imes N$$$ numbered board, or tell that it doesn't exist. Input The only line contains one integer $$$N$$$ xa0β the size of the board, $$$1le N le 500$$$. Output The output should contain $$$N$$$ lines. In $$$i$$$-th line output $$$N$$$ numbers xa0β numbers on the $$$i$$$-th row of the board. All numbers from $$$1$$$ to $$$N imes N$$$ must be used exactly once. On your board rook must pay strictly less vuns than the queen. If there are no solutions, print $$$-1$$$. If there are several solutions, you can output any of them. Examples Output 4 3 6 12 7 5 9 15 14 1 11 10 13 8 16 2 Note In case we have $$$1 imes 1$$$ board, both rook and queen do not have a chance to pay fees. In second sample rook goes through cells $$$1 o 3 o 4 o 6 o 9 o 5 o 7 o 13 o 2 o 8 o 16 o 11 o 10 o 12 o 15 o extbf{(1 vun)} o 14$$$. Queen goes through $$$1 o 3 o 4 o 2 o 5 o 6 o 9 o 7 o 13 o 8 o 11 o 10 o 12 o 15 o extbf{(1 vun)} o 14 o extbf{(1 vun)} o 16$$$. As a result rook pays 1 vun and queen pays 2 vuns. | 2,400 | false | false | false | false | false | true | true | false | false | false | 4,039 |
1190C | Duel!" Betting on the lovely princess Claris, the duel between Tokitsukaze and Quailty has started. There are $$$n$$$ cards in a row. Each card has two sides, one of which has color. At first, some of these cards are with color sides facing up and others are with color sides facing down. Then they take turns flipping cards, in which Tokitsukaze moves first. In each move, one should choose exactly $$$k$$$ consecutive cards and flip them to the same side, which means to make their color sides all face up or all face down. If all the color sides of these $$$n$$$ cards face the same direction after one's move, the one who takes this move will win. Princess Claris wants to know who will win the game if Tokitsukaze and Quailty are so clever that they won't make mistakes. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 10^5$$$). The second line contains a single string of length $$$n$$$ that only consists of $$$0$$$ and $$$1$$$, representing the situation of these $$$n$$$ cards, where the color side of the $$$i$$$-th card faces up if the $$$i$$$-th character is $$$1$$$, or otherwise, it faces down and the $$$i$$$-th character is $$$0$$$. Output Print "once again" (without quotes) if the total number of their moves can exceed $$$10^9$$$, which is considered a draw. In other cases, print "tokitsukaze" (without quotes) if Tokitsukaze will win, or "quailty" (without quotes) if Quailty will win. Note that the output characters are case-sensitive, and any wrong spelling would be rejected. Note In the first example, no matter how Tokitsukaze moves, there would be three cards with color sides facing the same direction after her move, and Quailty can flip the last card to this direction and win. In the second example, no matter how Tokitsukaze moves, Quailty can choose the same card and flip back to the initial situation, which can allow the game to end in a draw. In the third example, Tokitsukaze can win by flipping the leftmost five cards up or flipping the rightmost five cards down. The fourth example can be explained in the same way as the second example does. | 2,300 | false | true | false | false | false | false | true | false | false | false | 4,748 |
925A | In the year of $$$30XX$$$ participants of some world programming championship live in a single large hotel. The hotel has $$$n$$$ floors. Each floor has $$$m$$$ sections with a single corridor connecting all of them. The sections are enumerated from $$$1$$$ to $$$m$$$ along the corridor, and all sections with equal numbers on different floors are located exactly one above the other. Thus, the hotel can be represented as a rectangle of height $$$n$$$ and width $$$m$$$. We can denote sections with pairs of integers $$$(i, j)$$$, where $$$i$$$ is the floor, and $$$j$$$ is the section number on the floor. The guests can walk along the corridor on each floor, use stairs and elevators. Each stairs or elevator occupies all sections $$$(1, x)$$$, $$$(2, x)$$$, $$$ldots$$$, $$$(n, x)$$$ for some $$$x$$$ between $$$1$$$ and $$$m$$$. All sections not occupied with stairs or elevators contain guest rooms. It takes one time unit to move between neighboring sections on the same floor or to move one floor up or down using stairs. It takes one time unit to move up to $$$v$$$ floors in any direction using an elevator. You can assume you don't have to wait for an elevator, and the time needed to enter or exit an elevator is negligible. You are to process $$$q$$$ queries. Each query is a question "what is the minimum time needed to go from a room in section $$$(x_1, y_1)$$$ to a room in section $$$(x_2, y_2)$$$?" Input The first line contains five integers $$$n, m, c_l, c_e, v$$$ ($$$2 leq n, m leq 10^8$$$, $$$0 leq c_l, c_e leq 10^5$$$, $$$1 leq c_l + c_e leq m - 1$$$, $$$1 leq v leq n - 1$$$)xa0β the number of floors and section on each floor, the number of stairs, the number of elevators and the maximum speed of an elevator, respectively. The second line contains $$$c_l$$$ integers $$$l_1, ldots, l_{c_l}$$$ in increasing order ($$$1 leq l_i leq m$$$), denoting the positions of the stairs. If $$$c_l = 0$$$, the second line is empty. The third line contains $$$c_e$$$ integers $$$e_1, ldots, e_{c_e}$$$ in increasing order, denoting the elevators positions in the same format. It is guaranteed that all integers $$$l_i$$$ and $$$e_i$$$ are distinct. The fourth line contains a single integer $$$q$$$ ($$$1 leq q leq 10^5$$$)xa0β the number of queries. The next $$$q$$$ lines describe queries. Each of these lines contains four integers $$$x_1, y_1, x_2, y_2$$$ ($$$1 leq x_1, x_2 leq n$$$, $$$1 leq y_1, y_2 leq m$$$)xa0β the coordinates of starting and finishing sections for the query. It is guaranteed that the starting and finishing sections are distinct. It is also guaranteed that these sections contain guest rooms, i.xa0e. $$$y_1$$$ and $$$y_2$$$ are not among $$$l_i$$$ and $$$e_i$$$. Note In the first query the optimal way is to go to the elevator in the 5-th section in four time units, use it to go to the fifth floor in two time units and go to the destination in one more time unit. In the second query it is still optimal to use the elevator, but in the third query it is better to use the stairs in the section 2. | 1,600 | false | false | false | false | false | false | false | true | false | false | 6,011 |
316A2 | Special Agent Smart Beaver works in a secret research department of ABBYY. He's been working there for a long time and is satisfied with his job, as it allows him to eat out in the best restaurants and order the most expensive and exotic wood types there. The content special agent has got an important task: to get the latest research by British scientists on the English Language. These developments are encoded and stored in a large safe. The Beaver's teeth are strong enough, so the authorities assured that upon arriving at the place the beaver won't have any problems with opening the safe. And he finishes his aspen sprig and leaves for this important task. Of course, the Beaver arrived at the location without any problems, but alas. He can't open the safe with his strong and big teeth. At this point, the Smart Beaver get a call from the headquarters and learns that opening the safe with the teeth is not necessary, as a reliable source has sent the following information: the safe code consists of digits and has no leading zeroes. There also is a special hint, which can be used to open the safe. The hint is string _s_ with the following structure: if _s__i_ = "?", then the digit that goes _i_-th in the safe code can be anything (between 0 to 9, inclusively); if _s__i_ is a digit (between 0 to 9, inclusively), then it means that there is digit _s__i_ on position _i_ in code; if the string contains letters from "A" to "J", then all positions with the same letters must contain the same digits and the positions with distinct letters must contain distinct digits. The length of the safe code coincides with the length of the hint. For example, hint "?JGJ9" has such matching safe code variants: "51919", "55959", "12329", "93539" and so on, and has wrong variants such as: "56669", "00111", "03539" and "13666". After receiving such information, the authorities change the plan and ask the special agents to work quietly and gently and not to try to open the safe by mechanical means, and try to find the password using the given hint. At a special agent school the Smart Beaver was the fastest in his platoon finding codes for such safes, but now he is not in that shape: the years take their toll ... Help him to determine the number of possible variants of the code to the safe, matching the given hint. After receiving this information, and knowing his own speed of entering codes, the Smart Beaver will be able to determine whether he will have time for tonight's show "Beavers are on the trail" on his favorite TV channel, or he should work for a sleepless night... Input The first line contains string _s_ β the hint to the safe code. String _s_ consists of the following characters: ?, 0-9, A-J. It is guaranteed that the first character of string _s_ doesn't equal to character 0. The input limits for scoring 30 points are (subproblem A1): 1u2009β€u2009_s_u2009β€u20095. The input limits for scoring 100 points are (subproblems A1+A2): 1u2009β€u2009_s_u2009β€u2009105. Here _s_ means the length of string _s_. | 1,400 | true | false | false | false | false | false | false | false | false | false | 8,582 |
253D | Vasya has recently started to learn English. Now he needs to remember how to write English letters. He isn't sure about some of them, so he decided to train a little. He found a sheet of squared paper and began writing arbitrary English letters there. In the end Vasya wrote _n_ lines containing _m_ characters each. Thus, he got a rectangular _n_u2009Γu2009_m_ table, each cell of the table contained some English letter. Let's number the table rows from top to bottom with integers from 1 to _n_, and columns β from left to right with integers from 1 to _m_. After that Vasya looked at the resulting rectangular table and wondered, how many subtables are there, that matches both following conditions: the subtable contains at most _k_ cells with "a" letter; all letters, located in all four corner cells of the subtable, are equal. Formally, a subtable's definition is as follows. It is defined by four integers _x_1,u2009_y_1,u2009_x_2,u2009_y_2 such that 1u2009β€u2009_x_1u2009<u2009_x_2u2009β€u2009_n_, 1u2009β€u2009_y_1u2009<u2009_y_2u2009β€u2009_m_. Then the subtable contains all such cells (_x_,u2009_y_) (_x_ is the row number, _y_ is the column number), for which the following inequality holds _x_1u2009β€u2009_x_u2009β€u2009_x_2,u2009_y_1u2009β€u2009_y_u2009β€u2009_y_2. The corner cells of the table are cells (_x_1,u2009_y_1), (_x_1,u2009_y_2), (_x_2,u2009_y_1), (_x_2,u2009_y_2). Vasya is already too tired after he's been writing letters to a piece of paper. That's why he asks you to count the value he is interested in. Input The first line contains three integers _n_,u2009_m_,u2009_k_ (2u2009β€u2009_n_,u2009_m_u2009β€u2009400;xa00u2009β€u2009_k_u2009β€u2009_n_Β·_m_). Next _n_ lines contain _m_ characters each β the given table. Each character of the table is a lowercase English letter. Note There are two suitable subtables in the first sample: the first one's upper left corner is cell (2,u20092) and lower right corner is cell (3,u20093), the second one's upper left corner is cell (2,u20091) and lower right corner is cell (3,u20094). | 2,000 | false | false | false | false | false | false | true | false | false | false | 8,821 |
1493A | Problem - 1493A - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags constructive algorithms greedy *800 No tag edit access β Contest materials ") Editorial") elements of it. Input The first line contains the number of test cases $$$T$$$ ($$$1 le T le 100$$$). Each of the next $$$T$$$ lines contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 1000$$$) β the description of test cases. Output For each test case output two lines. In the first line output a single integer $$$m$$$ β the number of chosen integers. In the second line output $$$m$$$ distinct integers from $$$1$$$ to $$$n$$$ β the chosen numbers. If there are multiple answers, print any. You can print the numbers in any order. Example Input 3 3 2 5 3 1 1 Output 2 3 1 3 4 5 2 0 | 800 | false | true | false | false | false | true | false | false | false | false | 3,208 |
1850B | In 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? Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0β the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 50$$$)xa0β 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$$$)xa0β 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. Output For each test case, output a single line containing one integer $$$x$$$ ($$$1 leq x leq n$$$)xa0β 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. Example Input 3 5 7 2 12 5 9 3 9 4 10 1 3 1 2 3 4 5 6 1 1 43 Note In 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. | 800 | false | false | true | false | false | false | false | false | true | false | 1,181 |
950B | Hacker Zhorik wants to decipher two secret messages he intercepted yesterday. Yeah message is a sequence of encrypted blocks, each of them consists of several bytes of information. Zhorik knows that each of the messages is an archive containing one or more files. Zhorik knows how each of these archives was transferred through the network: if an archive consists of _k_ files of sizes _l_1,u2009_l_2,u2009...,u2009_l__k_ bytes, then the _i_-th file is split to one or more blocks _b__i_,u20091,u2009_b__i_,u20092,u2009...,u2009_b__i_,u2009_m__i_ (here the total length of the blocks _b__i_,u20091u2009+u2009_b__i_,u20092u2009+u2009...u2009+u2009_b__i_,u2009_m__i_ is equal to the length of the file _l__i_), and after that all blocks are transferred through the network, maintaining the order of files in the archive. Zhorik thinks that the two messages contain the same archive, because their total lengths are equal. However, each file can be split in blocks in different ways in the two messages. You are given the lengths of blocks in each of the two messages. Help Zhorik to determine what is the maximum number of files could be in the archive, if the Zhorik's assumption is correct. Input The first line contains two integers _n_, _m_ (1u2009β€u2009_n_,u2009_m_u2009β€u2009105) β the number of blocks in the first and in the second messages. The second line contains _n_ integers _x_1,u2009_x_2,u2009...,u2009_x__n_ (1u2009β€u2009_x__i_u2009β€u2009106) β the length of the blocks that form the first message. The third line contains _m_ integers _y_1,u2009_y_2,u2009...,u2009_y__m_ (1u2009β€u2009_y__i_u2009β€u2009106) β the length of the blocks that form the second message. It is guaranteed that _x_1u2009+u2009...u2009+u2009_x__n_u2009=u2009_y_1u2009+u2009...u2009+u2009_y__m_. Also, it is guaranteed that _x_1u2009+u2009...u2009+u2009_x__n_u2009β€u2009106. Output Print the maximum number of files the intercepted array could consist of. Examples Input 7 6 2 5 3 1 11 4 4 7 8 2 4 1 8 Note In the first example the maximum number of files in the archive is 3. For example, it is possible that in the archive are three files of sizes 2u2009+u20095u2009=u20097, 15u2009=u20093u2009+u20091u2009+u200911u2009=u20098u2009+u20092u2009+u20094u2009+u20091 and 4u2009+u20094u2009=u20098. In the second example it is possible that the archive contains two files of sizes 1 and 110u2009=u200910u2009+u2009100u2009=u2009100u2009+u200910. Note that the order of files is kept while transferring archives through the network, so we can't say that there are three files of sizes 1, 10 and 100. In the third example the only possibility is that the archive contains a single file of size 4. | 1,100 | false | true | true | false | false | false | false | false | false | false | 5,917 |
460B | Problem - 460B - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags brute force implementation math number theory *1500 No tag edit access β Contest materials of the equation: _x_u2009=u2009_b_Β·_s_(_x_)_a_u2009+u2009_c_,u2009 where _a_, _b_, _c_ are some predetermined constant values and function _s_(_x_) determines the sum of all digits in the decimal representation of number _x_. The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: _a_, _b_, _c_. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. Input The first line contains three space-separated integers: _a_,u2009_b_,u2009_c_ (1u2009β€u2009_a_u2009β€u20095;xa01u2009β€u2009_b_u2009β€u200910000;xa0u2009-u200910000u2009β€u2009_c_u2009β€u200910000). Output Print integer _n_ β the number of the solutions that you've found. Next print _n_ integers in the increasing order β the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 109. Examples Input 3 2 8 Output 3 10 2008 13726 Input 1 2 -18 Output 0 Input 2 2 -1 Output 4 1 31 337 967 | 1,500 | true | false | true | false | false | false | true | false | false | false | 8,000 |
778A | Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word _t_ and wants to get the word _p_ out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word _t_: _a_1... _a__t_. We denote the length of word _x_ as _x_. Note that after removing one letter, the indices of other letters don't change. For example, if _t_u2009=u2009"nastya" and _a_u2009=u2009[4,u20091,u20095,u20093,u20092,u20096] then removals make the following sequence of words "nastya" "nastya" "nastya" "nastya" "nastya" "nastya" "nastya". Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word _p_. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey. It is guaranteed that the word _p_ can be obtained by removing the letters from word _t_. Input The first and second lines of the input contain the words _t_ and _p_, respectively. Words are composed of lowercase letters of the Latin alphabet (1u2009β€u2009_p_u2009<u2009_t_u2009β€u2009200u2009000). It is guaranteed that the word _p_ can be obtained by removing the letters from word _t_. Next line contains a permutation _a_1,u2009_a_2,u2009...,u2009_a__t_ of letter indices that specifies the order in which Nastya removes letters of _t_ (1u2009β€u2009_a__i_u2009β€u2009_t_, all _a__i_ are distinct). Note In the first sample test sequence of removing made by Nastya looks like this: "ababcba" "ababcba" "ababcba" "ababcba" Nastya can not continue, because it is impossible to get word "abb" from word "ababcba". So, Nastya will remove only three letters. | 1,700 | false | true | false | false | false | false | false | true | false | false | 6,656 |
1070E | Polycarp has a lot of work to do. Recently he has learned a new time management rule: "if a task takes five minutes or less, do it immediately". Polycarp likes the new rule, however he is not sure that five minutes is the optimal value. He supposes that this value $$$d$$$ should be chosen based on existing task list. Polycarp has a list of $$$n$$$ tasks to complete. The $$$i$$$-th task has difficulty $$$p_i$$$, i.e. it requires exactly $$$p_i$$$ minutes to be done. Polycarp reads the tasks one by one from the first to the $$$n$$$-th. If a task difficulty is $$$d$$$ or less, Polycarp starts the work on the task immediately. If a task difficulty is strictly greater than $$$d$$$, he will not do the task at all. It is not allowed to rearrange tasks in the list. Polycarp doesn't spend any time for reading a task or skipping it. Polycarp has $$$t$$$ minutes in total to complete maximum number of tasks. But he does not want to work all the time. He decides to make a break after each group of $$$m$$$ consecutive tasks he was working on. The break should take the same amount of time as it was spent in total on completion of these $$$m$$$ tasks. For example, if $$$n=7$$$, $$$p=[3, 1, 4, 1, 5, 9, 2]$$$, $$$d=3$$$ and $$$m=2$$$ Polycarp works by the following schedule: Polycarp reads the first task, its difficulty is not greater than $$$d$$$ ($$$p_1=3 le d=3$$$) and works for $$$3$$$ minutes (i.e. the minutes $$$1$$$, $$$2$$$, $$$3$$$); Polycarp reads the second task, its difficulty is not greater than $$$d$$$ ($$$p_2=1 le d=3$$$) and works for $$$1$$$ minute (i.e. the minute $$$4$$$); Polycarp notices that he has finished $$$m=2$$$ tasks and takes a break for $$$3+1=4$$$ minutes (i.e. on the minutes $$$5, 6, 7, 8$$$); Polycarp reads the third task, its difficulty is greater than $$$d$$$ ($$$p_3=4 > d=3$$$) and skips it without spending any time; Polycarp reads the fourth task, its difficulty is not greater than $$$d$$$ ($$$p_4=1 le d=3$$$) and works for $$$1$$$ minute (i.e. the minute $$$9$$$); Polycarp reads the tasks $$$5$$$ and $$$6$$$, skips both of them ($$$p_5>d$$$ and $$$p_6>d$$$); Polycarp reads the $$$7$$$-th task, its difficulty is not greater than $$$d$$$ ($$$p_7=2 le d=3$$$) and works for $$$2$$$ minutes (i.e. the minutes $$$10$$$, $$$11$$$); Polycarp notices that he has finished $$$m=2$$$ tasks and takes a break for $$$1+2=3$$$ minutes (i.e. on the minutes $$$12, 13, 14$$$). Polycarp stops exactly after $$$t$$$ minutes. If Polycarp started a task but has not finished it by that time, the task is not considered as completed. It is allowed to complete less than $$$m$$$ tasks in the last group. Also Polycarp considers acceptable to have shorter break than needed after the last group of tasks or even not to have this break at allxa0β his working day is over and he will have enough time to rest anyway. Please help Polycarp to find such value $$$d$$$, which would allow him to complete maximum possible number of tasks in $$$t$$$ minutes. Input The first line of the input contains single integer $$$c$$$ ($$$1 le c le 5 cdot 10^4$$$)xa0β number of test cases. Then description of $$$c$$$ test cases follows. Solve test cases separately, test cases are completely independent and do not affect each other. Each test case is described by two lines. The first of these lines contains three space-separated integers $$$n$$$, $$$m$$$ and $$$t$$$ ($$$1 le n le 2 cdot 10^5, 1 le m le 2 cdot 10^5, 1 le t le 4 cdot 10^{10}$$$)xa0β the number of tasks in Polycarp's list, the number of tasks he can do without a break and the total amount of time Polycarp can work on tasks. The second line of the test case contains $$$n$$$ space separated integers $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le 2 cdot 10^5$$$)xa0β difficulties of the tasks. The sum of values $$$n$$$ for all test cases in the input does not exceed $$$2 cdot 10^5$$$. Output Print $$$c$$$ lines, each line should contain answer for the corresponding test casexa0β the maximum possible number of tasks Polycarp can complete and the integer value $$$d$$$ ($$$1 le d le t$$$) Polycarp should use in time management rule, separated by space. If there are several possible values $$$d$$$ for a test case, output any of them. Note In the first test case of the first example $$$n=5$$$, $$$m=2$$$ and $$$t=16$$$. The sequence of difficulties is $$$[5, 6, 1, 4, 7]$$$. If Polycarp chooses $$$d=5$$$ then he will complete $$$3$$$ tasks. Polycarp will work by the following schedule: Polycarp reads the first task, its difficulty is not greater than $$$d$$$ ($$$p_1=5 le d=5$$$) and works for $$$5$$$ minutes (i.e. the minutes $$$1, 2, dots, 5$$$); Polycarp reads the second task, its difficulty is greater than $$$d$$$ ($$$p_2=6 > d=5$$$) and skips it without spending any time; Polycarp reads the third task, its difficulty is not greater than $$$d$$$ ($$$p_3=1 le d=5$$$) and works for $$$1$$$ minute (i.e. the minute $$$6$$$); Polycarp notices that he has finished $$$m=2$$$ tasks and takes a break for $$$5+1=6$$$ minutes (i.e. on the minutes $$$7, 8, dots, 12$$$); Polycarp reads the fourth task, its difficulty is not greater than $$$d$$$ ($$$p_4=4 le d=5$$$) and works for $$$4$$$ minutes (i.e. the minutes $$$13, 14, 15, 16$$$); Polycarp stops work because of $$$t=16$$$. In total in the first test case Polycarp will complete $$$3$$$ tasks for $$$d=5$$$. He can't choose other value for $$$d$$$ to increase the number of completed tasks. | 2,100 | false | false | false | false | true | false | false | true | false | false | 5,363 |
961F | You are given a string _s_ consisting of _n_ lowercase Latin letters. Let's denote _k_-substring of _s_ as a string _subs__k_u2009=u2009_s__k__s__k_u2009+u20091.._s__n_u2009+u20091u2009-u2009_k_. Obviously, _subs_1u2009=u2009_s_, and there are exactly such substrings. Let's call some string _t_ an odd proper suprefix of a string _T_ iff the following conditions are met: _T_u2009>u2009_t_; _t_ is an odd number; _t_ is simultaneously a prefix and a suffix of _T_. For evey _k_-substring () of _s_ you have to calculate the maximum length of its odd proper suprefix. Input The first line contains one integer _n_ (2u2009β€u2009_n_u2009β€u2009106) β the length _s_. The second line contains the string _s_ consisting of _n_ lowercase Latin letters. Output Print integers. _i_-th of them should be equal to maximum length of an odd proper suprefix of _i_-substring of _s_ (or u2009-u20091, if there is no such string that is an odd proper suprefix of _i_-substring). Examples Input 24 abaaabaaaabaaabaaaabaaab Output 15 13 11 9 7 5 3 1 1 -1 -1 1 Input 19 cabcabbcabcabbcabca Output 5 3 1 -1 -1 1 1 -1 -1 -1 Note The answer for first sample test is folowing: 1-substring: bcabcabcabcabca 2-substring: cabcabcabcabc 3-substring: abcabcabcab 4-substring: bcabcabca 5-substring: cabcabc 6-substring: abcab 7-substring: bca 8-substring: c | 2,700 | false | false | false | false | false | false | false | true | false | false | 5,858 |
498B | It turns out that you are a great fan of rock band AC/PE. Peter learned that and started the following game: he plays the first song of the list of _n_ songs of the group, and you have to find out the name of the song. After you tell the song name, Peter immediately plays the following song in order, and so on. The _i_-th song of AC/PE has its recognizability _p__i_. This means that if the song has not yet been recognized by you, you listen to it for exactly one more second and with probability of _p__i_ percent you recognize it and tell it's name. Otherwise you continue listening it. Note that you can only try to guess it only when it is integer number of seconds after the moment the song starts playing. In all AC/PE songs the first words of chorus are the same as the title, so when you've heard the first _t__i_ seconds of _i_-th song and its chorus starts, you immediately guess its name for sure. For example, in the song Highway To Red the chorus sounds pretty late, but the song has high recognizability. In the song Back In Blue, on the other hand, the words from the title sound close to the beginning of the song, but it's hard to name it before hearing those words. You can name both of these songs during a few more first seconds. Determine the expected number songs of you will recognize if the game lasts for exactly _T_ seconds (i. e. you can make the last guess on the second _T_, after that the game stops). If all songs are recognized faster than in _T_ seconds, the game stops after the last song is recognized. Input The first line of the input contains numbers _n_ and _T_ (1u2009β€u2009_n_u2009β€u20095000, 1u2009β€u2009_T_u2009β€u20095000), separated by a space. Next _n_ lines contain pairs of numbers _p__i_ and _t__i_ (0u2009β€u2009_p__i_u2009β€u2009100, 1u2009β€u2009_t__i_u2009β€u2009_T_). The songs are given in the same order as in Petya's list. | 2,400 | false | false | false | true | false | false | false | false | false | false | 7,843 |
1200B | Gildong is playing a video game called Block Adventure. In Block Adventure, there are $$$n$$$ columns of blocks in a row, and the columns are numbered from $$$1$$$ to $$$n$$$. All blocks have equal heights. The height of the $$$i$$$-th column is represented as $$$h_i$$$, which is the number of blocks stacked in the $$$i$$$-th column. Gildong plays the game as a character that can stand only on the top of the columns. At the beginning, the character is standing on the top of the $$$1$$$-st column. The goal of the game is to move the character to the top of the $$$n$$$-th column. The character also has a bag that can hold infinitely many blocks. When the character is on the top of the $$$i$$$-th column, Gildong can take one of the following three actions as many times as he wants: if there is at least one block on the column, remove one block from the top of the $$$i$$$-th column and put it in the bag; if there is at least one block in the bag, take one block out of the bag and place it on the top of the $$$i$$$-th column; if $$$i < n$$$ and $$$h_i - h_{i+1} le k$$$, move the character to the top of the $$$i+1$$$-st column. $$$k$$$ is a non-negative integer given at the beginning of the game. Note that it is only possible to move to the next column. In actions of the first two types the character remains in the $$$i$$$-th column, and the value $$$h_i$$$ changes. The character initially has $$$m$$$ blocks in the bag. Gildong wants to know if it is possible to win the game. Help Gildong find the answer to his question. Input Each test contains one or more test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1000$$$). Description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 le n le 100$$$, $$$0 le m le 10^6$$$, $$$0 le k le 10^6$$$) β the number of columns in the game, the number of blocks in the character's bag at the beginning, and the non-negative integer $$$k$$$ described in the statement. The second line of each test case contains $$$n$$$ integers. The $$$i$$$-th integer is $$$h_i$$$ ($$$0 le h_i le 10^6$$$), the initial height of the $$$i$$$-th column. Output For each test case, print "YES" if it is possible to win the game. Otherwise, print "NO". You can print each letter in any case (upper or lower). Example Input 5 3 0 1 4 3 5 3 1 2 1 4 7 4 10 0 10 20 10 20 2 5 5 0 11 1 9 9 99 Note In the first case, Gildong can take one block from the $$$1$$$-st column, move to the $$$2$$$-nd column, put the block on the $$$2$$$-nd column, then move to the $$$3$$$-rd column. In the second case, Gildong has to put the block in his bag on the $$$1$$$-st column to get to the $$$2$$$-nd column. But it is impossible to get to the $$$3$$$-rd column because $$$h_2 - h_3 = 3 > k$$$ and there is no way to decrease the gap. In the fifth case, the character is already on the $$$n$$$-th column from the start so the game is won instantly. | 1,200 | false | true | false | true | false | false | false | false | false | false | 4,700 |
1542C | Let $$$f(i)$$$ denote the minimum positive integer $$$x$$$ such that $$$x$$$ is not a divisor of $$$i$$$. Compute $$$sum_{i=1}^n f(i)$$$ modulo $$$10^9+7$$$. In other words, compute $$$f(1)+f(2)+dots+f(n)$$$ modulo $$$10^9+7$$$. Input The first line contains a single integer $$$t$$$ ($$$1leq tleq 10^4$$$), the number of test cases. Then $$$t$$$ cases follow. The only line of each test case contains a single integer $$$n$$$ ($$$1leq nleq 10^{16}$$$). Output For each test case, output a single integer $$$ans$$$, where $$$ans=sum_{i=1}^n f(i)$$$ modulo $$$10^9+7$$$. Example Input 6 1 2 3 4 10 10000000000000000 Output 2 5 7 10 26 366580019 Note In the fourth test case $$$n=4$$$, so $$$ans=f(1)+f(2)+f(3)+f(4)$$$. $$$1$$$ is a divisor of $$$1$$$ but $$$2$$$ isn't, so $$$2$$$ is the minimum positive integer that isn't a divisor of $$$1$$$. Thus, $$$f(1)=2$$$. $$$1$$$ and $$$2$$$ are divisors of $$$2$$$ but $$$3$$$ isn't, so $$$3$$$ is the minimum positive integer that isn't a divisor of $$$2$$$. Thus, $$$f(2)=3$$$. $$$1$$$ is a divisor of $$$3$$$ but $$$2$$$ isn't, so $$$2$$$ is the minimum positive integer that isn't a divisor of $$$3$$$. Thus, $$$f(3)=2$$$. $$$1$$$ and $$$2$$$ are divisors of $$$4$$$ but $$$3$$$ isn't, so $$$3$$$ is the minimum positive integer that isn't a divisor of $$$4$$$. Thus, $$$f(4)=3$$$. Therefore, $$$ans=f(1)+f(2)+f(3)+f(4)=2+3+2+3=10$$$. | 1,600 | true | false | false | false | false | false | false | false | false | false | 2,932 |
1296E1 | This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different. You are given a string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $$$s$$$). After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times. The goal is to make the string sorted, i.e. all characters should be in alphabetical order. Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps. Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 200$$$) β the length of $$$s$$$. The second line of the input contains the string $$$s$$$ consisting of exactly $$$n$$$ lowercase Latin letters. Output If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line. Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of $$$n$$$ characters, the $$$i$$$-th character should be '0' if the $$$i$$$-th character is colored the first color and '1' otherwise). | 1,800 | false | true | false | true | false | true | false | false | true | true | 4,213 |
850F | You have a bag of balls of _n_ different colors. You have _a__i_ balls of the _i_-th color. While there are at least two different colored balls in the bag, perform the following steps: Take out two random balls without replacement one by one. These balls might be the same color. Color the second ball to the color of the first ball. You are not allowed to switch the order of the balls in this step. Place both balls back in the bag. All these actions take exactly one second. Let _M_u2009=u2009109u2009+u20097. It can be proven that the expected amount of time needed before you stop can be represented as a rational number , where _P_ and _Q_ are coprime integers and where _Q_ is not divisible by _M_. Return the value . Input The first line of input will contain a single integer _n_ (1u2009β€u2009_n_u2009β€u20092u2009500)xa0β the number of colors. The next line of input will contain _n_ space separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009β€u2009_a__i_u2009β€u2009105)xa0β the number of balls of each color. Output Print a single integer, the answer to the problem. Note In the first sample, no matter what happens, the balls will become the same color after one step. For the second sample, we have 6 balls. Letβs label the balls from 1 to 6, and without loss of generality, letβs say balls 1,2,3 are initially color 1, balls 4,5 are color 2, and ball 6 are color 3. Here is an example of how these steps can go: We choose ball 5 and ball 6. Ball 6 then becomes color 2. We choose ball 4 and ball 5. Ball 5 remains the same color (color 2). We choose ball 1 and ball 5. Ball 5 becomes color 1. We choose ball 6 and ball 5. Ball 5 becomes color 2. We choose ball 3 and ball 4. Ball 4 becomes color 1. We choose ball 4 and ball 6. Ball 6 becomes color 1. We choose ball 2 and ball 5. Ball 5 becomes color 1. At this point, the game ends since all the balls are the same color. This particular sequence took 7 seconds. It can be shown that the answer to this case is . | 2,800 | true | false | false | false | false | false | false | false | false | false | 6,346 |
356E | Xenia the coder went to The Olympiad of Informatics and got a string problem. Unfortunately, Xenia isn't fabulous in string algorithms. Help her solve the problem. String _s_ is a sequence of characters _s_1_s_2... _s__s_, where record _s_ shows the length of the string. Substring _s_[_i_... _j_] of string _s_ is string _s__i__s__i_u2009+u20091... _s__j_. String _s_ is a Gray string, if it meets the conditions: For example, strings "abacaba", "xzx", "g" are Gray strings and strings "aaa", "xz", "abaxcbc" are not. The beauty of string _p_ is the sum of the squares of the lengths of all substrings of string _p_ that are Gray strings. In other words, consider all pairs of values _i_,u2009_j_ (1u2009β€u2009_i_u2009β€u2009_j_u2009β€u2009_p_). If substring _p_[_i_... _j_] is a Gray string, you should add (_j_u2009-u2009_i_u2009+u20091)2 to the beauty. Xenia has got string _t_ consisting of lowercase English letters. She is allowed to replace at most one letter of the string by any other English letter. The task is to get a string of maximum beauty. Output Print the sought maximum beauty value Xenia can get. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Note In the first test sample the given string can be transformed into string _p_ = "zbz". Such string contains Gray strings as substrings _p_[1... 1], _p_[2... 2], _p_[3... 3] ΠΈ _p_[1... 3]. In total, the beauty of string _p_ gets equal to 12u2009+u200912u2009+u200912u2009+u200932u2009=u200912. You can't obtain a more beautiful string. In the second test case it is not necessary to perform any operation. The initial string has the maximum possible beauty. | 3,000 | false | false | true | true | false | false | false | false | false | false | 8,407 |
689A | While swimming at the beach, Mike has accidentally dropped his cellphone into the water. There was no worry as he bought a cheap replacement phone with an old-fashioned keyboard. The keyboard has only ten digital equal-sized keys, located in the following way: Together with his old phone, he lost all his contacts and now he can only remember the way his fingers moved when he put some number in. One can formally consider finger movements as a sequence of vectors connecting centers of keys pressed consecutively to put in a number. For example, the finger movements for number "586" are the same as finger movements for number "253": Mike has already put in a number by his "finger memory" and started calling it, so he is now worrying, can he be sure that he is calling the correct number? In other words, is there any other number, that has the same finger movements? Input The first line of the input contains the only integer _n_ (1u2009β€u2009_n_u2009β€u20099)xa0β the number of digits in the phone number that Mike put in. The second line contains the string consisting of _n_ digits (characters from '0' to '9') representing the number that Mike put in. Output If there is no other phone number with the same finger movements and Mike can be sure he is calling the correct number, print "YES" (without quotes) in the only line. Otherwise print "NO" (without quotes) in the first line. Note You can find the picture clarifying the first sample case in the statement above. | 1,400 | false | false | true | false | false | true | true | false | false | false | 7,069 |
1005B | You are given two strings $$$s$$$ and $$$t$$$. In a single move, you can choose any of two strings and delete the first (that is, the leftmost) character. After a move, the length of the string decreases by $$$1$$$. You can't choose a string if it is empty. For example: by applying a move to the string "where", the result is the string "here", by applying a move to the string "a", the result is an empty string "". You are required to make two given strings equal using the fewest number of moves. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the initial strings. Write a program that finds the minimum number of moves to make two given strings $$$s$$$ and $$$t$$$ equal. Input The first line of the input contains $$$s$$$. In the second line of the input contains $$$t$$$. Both strings consist only of lowercase Latin letters. The number of letters in each string is between 1 and $$$2cdot10^5$$$, inclusive. Output Output the fewest number of moves required. It is possible that, in the end, both strings will be equal to the empty string, and so, are equal to each other. In this case, the answer is obviously the sum of the lengths of the given strings. Note In the first example, you should apply the move once to the first string and apply the move once to the second string. As a result, both strings will be equal to "est". In the second example, the move should be applied to the string "codeforces" $$$8$$$ times. As a result, the string becomes "codeforces" $$$ o$$$ "es". The move should be applied to the string "yes" once. The result is the same string "yes" $$$ o$$$ "es". In the third example, you can make the strings equal only by completely deleting them. That is, in the end, both strings will be empty. In the fourth example, the first character of the second string should be deleted. | 900 | false | false | true | false | false | false | true | false | false | false | 5,661 |
334A | Problem - 334A - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags implementation *1000 No tag edit access β Contest materials β the number of Gerald's brothers. Output Let's assume that Gerald indexes his brothers with numbers from 1 to _n_. You need to print _n_ lines, on the _i_-th line print _n_ integers β the numbers of candies in the bags for the _i_-th brother. Naturally, all these numbers should be distinct and be within limits from 1 to _n_2. You can print the numbers in the lines in any order. It is guaranteed that the solution exists at the given limits. Examples Input 2 Output 1 4 2 3 Note The sample shows Gerald's actions if he has two brothers. In this case, his bags contain 1, 2, 3 and 4 candies. He can give the bags with 1 and 4 candies to one brother and the bags with 2 and 3 to the other brother. | 1,000 | false | false | true | false | false | false | false | false | false | false | 8,500 |
1609G | William has two arrays of numbers $$$a_1, a_2, dots, a_n$$$ and $$$b_1, b_2, dots, b_m$$$. The arrays satisfy the conditions of being convex. Formally an array $$$c$$$ of length $$$k$$$ is considered convex if $$$c_i - c_{i - 1} < c_{i + 1} - c_i$$$ for all $$$i$$$ from $$$2$$$ to $$$k - 1$$$ and $$$c_1 < c_2$$$. Throughout William's life he observed $$$q$$$ changes of two types happening to the arrays: 1. Add the arithmetic progression $$$d, d cdot 2, d cdot 3, dots, d cdot k$$$ to the suffix of the array $$$a$$$ of length $$$k$$$. The array after the change looks like this: $$$[a_1, a_2, dots, a_{n - k}, a_{n - k + 1} + d, a_{n - k + 2} + d cdot 2, dots, a_n + d cdot k]$$$. 2. The same operation, but for array $$$b$$$. After each change a matrix $$$d$$$ is created from arrays $$$a$$$ and $$$b$$$, of size $$$n imes m$$$, where $$$d_{i, j}=a_i + b_j$$$. William wants to get from cell ($$$1, 1$$$) to cell ($$$n, m$$$) of this matrix. From cell ($$$x, y$$$) he can only move to cells ($$$x + 1, y$$$) and ($$$x, y + 1$$$). The length of a path is calculated as the sum of numbers in cells visited by William, including the first and the last cells. After each change William wants you to help find out the minimal length of the path he could take. Input The first line contains three integers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$2 le n le 100, 2 le m le 10^5$$$, $$$1 le q le 10^5$$$), the sizes of the arrays and the number of changes. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^{12}$$$), the contents of array $$$a$$$. The third line contains $$$m$$$ integers $$$b_1, b_2, dots, b_m$$$ ($$$1 le b_i le 10^{12}$$$), the contents of array $$$b$$$. Each of the next $$$q$$$ lines contains three integers $$$type$$$, $$$k$$$ and $$$d$$$ ($$$1 le type le 2$$$, if $$$type = 1$$$, then $$$1 le k le n$$$ otherwise $$$1 le k le m$$$, $$$1 le d le 10^3$$$). Output After each change, output one integer, the minimum length of the path in the constructed matrix. Examples Input 5 3 4 1 2 4 7 11 5 7 10 1 3 2 2 2 5 1 5 4 2 1 7 Input 5 6 7 4 9 22 118 226 7 94 238 395 565 738 2 1 95 1 4 54 1 2 5 1 2 87 2 6 62 2 1 143 1 1 77 Output 3639 5122 5162 5617 7663 7806 7960 | 3,000 | true | true | false | false | true | false | false | false | false | false | 2,602 |
1036G | You are given an acyclic directed graph, consisting of $$$n$$$ vertices and $$$m$$$ edges. The graph contains no multiple edges and no self-loops. The vertex is called a source if it has no incoming edges. The vertex is called a sink if it has no outgoing edges. These definitions imply that some vertices can be both source and sink. The number of sources in the given graph is equal to the number of sinks in it, and each of these numbers doesn't exceed $$$20$$$. The following algorithm is applied to the graph: 1. if the graph has no sources and sinks then quit; 2. choose arbitrary source $$$s$$$, arbitrary sink $$$t$$$, add an edge from $$$t$$$ to $$$s$$$ to the graph and go to step $$$1$$$ (that operation pops $$$s$$$ out of sources and $$$t$$$ out of sinks). Note that $$$s$$$ and $$$t$$$ may be the same vertex, then a self-loop is added. At the end you check if the graph becomes strongly connected (that is, any vertex is reachable from any other vertex). Your task is to check that the graph becomes strongly connected no matter the choice of sources and sinks on the second step of the algorithm. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 10^6$$$) β the number of vertices and the number of edges in the graph, respectively. Each of the next $$$m$$$ lines contains two integers $$$v_i, u_i$$$ ($$$1 le v_i, u_i le n$$$, $$$v_i e u_i$$$) β the description of the $$$i$$$-th edge of the original graph. It is guaranteed that the number of sources and the number of sinks in the graph are the same and they don't exceed $$$20$$$. It is guaranteed that the given graph contains no multiple edges. It is guaranteed that the graph contains no cycles. Output Print "YES" if the graph becomes strongly connected no matter the choice of sources and sinks on the second step of the algorithm. Otherwise print "NO". | 2,700 | false | false | false | false | false | false | true | false | false | false | 5,517 |
965B | Arkady is playing Battleship. The rules of this game aren't really important. There is a field of $$$n imes n$$$ cells. There should be exactly one $$$k$$$-decker on the field, i.xa0e. a ship that is $$$k$$$ cells long oriented either horizontally or vertically. However, Arkady doesn't know where it is located. For each cell Arkady knows if it is definitely empty or can contain a part of the ship. Consider all possible locations of the ship. Find such a cell that belongs to the maximum possible number of different locations of the ship. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 100$$$)xa0β the size of the field and the size of the ship. The next $$$n$$$ lines contain the field. Each line contains $$$n$$$ characters, each of which is either '#' (denotes a definitely empty cell) or '.' (denotes a cell that can belong to the ship). Output Output two integersxa0β the row and the column of a cell that belongs to the maximum possible number of different locations of the ship. If there are multiple answers, output any of them. In particular, if no ship can be placed on the field, you can output any cell. Examples Input 10 4 #....##... .#...#.... ..#..#..#. ...#.#.... .#..##.#.. .....#...# ...#.##... .#...#.#.. .....#..#. ...#.#...# Input 19 6 ##..............### #......#####.....## .....#########..... ....###########.... ...#############... ..###############.. .#################. .#################. .#################. .#################. #####....##....#### ####............### ####............### #####...####...#### .#####..####..##### ...###........###.. ....###########.... .........##........ #.................# Note The picture below shows the three possible locations of the ship that contain the cell $$$(3, 2)$$$ in the first sample. | 1,300 | false | false | true | false | false | false | false | false | false | false | 5,841 |
1326B | Alicia has an array, $$$a_1, a_2, ldots, a_n$$$, of non-negative integers. For each $$$1 leq i leq n$$$, she has found a non-negative integer $$$x_i = max(0, a_1, ldots, a_{i-1})$$$. Note that for $$$i=1$$$, $$$x_i = 0$$$. For example, if Alicia had the array $$$a = {0, 1, 2, 0, 3}$$$, then $$$x = {0, 0, 1, 2, 2}$$$. Then, she calculated an array, $$$b_1, b_2, ldots, b_n$$$: $$$b_i = a_i - x_i$$$. For example, if Alicia had the array $$$a = {0, 1, 2, 0, 3}$$$, $$$b = {0-0, 1-0, 2-1, 0-2, 3-2} = {0, 1, 1, -2, 1}$$$. Alicia gives you the values $$$b_1, b_2, ldots, b_n$$$ and asks you to restore the values $$$a_1, a_2, ldots, a_n$$$. Can you help her solve the problem? Input The first line contains one integer $$$n$$$ ($$$3 leq n leq 200,000$$$)xa0β the number of elements in Alicia's array. The next line contains $$$n$$$ integers, $$$b_1, b_2, ldots, b_n$$$ ($$$-10^9 leq b_i leq 10^9$$$). It is guaranteed that for the given array $$$b$$$ there is a solution $$$a_1, a_2, ldots, a_n$$$, for all elements of which the following is true: $$$0 leq a_i leq 10^9$$$. Output Print $$$n$$$ integers, $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i leq 10^9$$$), such that if you calculate $$$x$$$ according to the statement, $$$b_1$$$ will be equal to $$$a_1 - x_1$$$, $$$b_2$$$ will be equal to $$$a_2 - x_2$$$, ..., and $$$b_n$$$ will be equal to $$$a_n - x_n$$$. It is guaranteed that there exists at least one solution for the given tests. It can be shown that the solution is unique. Note The first test was described in the problem statement. In the second test, if Alicia had an array $$$a = {1000, 1000000000, 0}$$$, then $$$x = {0, 1000, 1000000000}$$$ and $$$b = {1000-0, 1000000000-1000, 0-1000000000} = {1000, 999999000, -1000000000}$$$. | 900 | true | false | true | false | false | false | false | false | false | false | 4,086 |
245D | Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation. For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers _a_1,u2009_a_2,u2009...,u2009_a__n_. He also wrote a square matrix _b_ of size _n_u2009Γu2009_n_. The element of matrix _b_ that sits in the _i_-th row in the _j_-th column (we'll denote it as _b__ij_) equals: the "bitwise AND" of numbers _a__i_ and _a__j_ (that is, _b__ij_u2009=u2009_a__i_xa0&xa0_a__j_), if _i_u2009β u2009_j_; -1, if _i_u2009=u2009_j_. Having written out matrix _b_, Polycarpus got very happy and wiped _a_ off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly. Help Polycarpus, given matrix _b_, restore the sequence of numbers _a_1,u2009_a_2,u2009...,u2009_a__n_, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 109. Input The first line contains a single integer _n_ (1u2009β€u2009_n_u2009β€u2009100) β the size of square matrix _b_. Next _n_ lines contain matrix _b_. The _i_-th of these lines contains _n_ space-separated integers: the _j_-th number represents the element of matrix _b__ij_. It is guaranteed, that for all _i_ (1u2009β€u2009_i_u2009β€u2009_n_) the following condition fulfills: _b__ii_ = -1. It is guaranteed that for all _i_,u2009_j_ (1u2009β€u2009_i_,u2009_j_u2009β€u2009_n_;xa0_i_u2009β u2009_j_) the following condition fulfills: 0u2009β€u2009_b__ij_u2009β€u2009109, _b__ij_u2009=u2009_b__ji_. Output Print _n_ non-negative integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009β€u2009_a__i_u2009β€u2009109) β the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces. It is guaranteed that there is sequence _a_ that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them. | 1,500 | false | true | false | false | false | true | false | false | false | false | 8,853 |
1562C | Frodo was caught by Saruman. He tore a pouch from Frodo's neck, shook out its contentsxa0βthere was a pile of different rings: gold and silver..."How am I to tell which is the One?!" the mage howled. "Throw them one by one into the Cracks of Doom and watch when Mordor falls!" Somewhere in a parallel Middle-earth, when Saruman caught Frodo, he only found $$$n$$$ rings. And the $$$i$$$-th ring was either gold or silver. For convenience Saruman wrote down a binary string $$$s$$$ of $$$n$$$ characters, where the $$$i$$$-th character was 0 if the $$$i$$$-th ring was gold, and 1 if it was silver. Saruman has a magic function $$$f$$$, which takes a binary string and returns a number obtained by converting the string into a binary number and then converting the binary number into a decimal number. For example, $$$f(001010) = 10, f(111) = 7, f(11011101) = 221$$$. Saruman, however, thinks that the order of the rings plays some important role. He wants to find $$$2$$$ pairs of integers $$$(l_1, r_1), (l_2, r_2)$$$, such that: $$$1 le l_1 le n$$$, $$$1 le r_1 le n$$$, $$$r_1-l_1+1ge lfloor frac{n}{2} floor$$$ $$$1 le l_2 le n$$$, $$$1 le r_2 le n$$$, $$$r_2-l_2+1ge lfloor frac{n}{2} floor$$$ Pairs $$$(l_1, r_1)$$$ and $$$(l_2, r_2)$$$ are distinct. That is, at least one of $$$l_1 eq l_2$$$ and $$$r_1 eq r_2$$$ must hold. Let $$$t$$$ be the substring $$$s[l_1:r_1]$$$ of $$$s$$$, and $$$w$$$ be the substring $$$s[l_2:r_2]$$$ of $$$s$$$. Then there exists non-negative integer $$$k$$$, such that $$$f(t) = f(w) cdot k$$$. Here substring $$$s[l:r]$$$ denotes $$$s_ls_{l+1}ldots s_{r-1}s_r$$$, and $$$lfloor x floor$$$ denotes rounding the number down to the nearest integer. Help Saruman solve this problem! It is guaranteed that under the constraints of the problem at least one solution exists. Input Each test contains multiple test cases. The first line contains one positive integer $$$t$$$ ($$$1 le t le 10^3$$$), denoting the number of test cases. Description of the test cases follows. The first line of each test case contains one positive integer $$$n$$$ ($$$2 le n le 2 cdot 10^4$$$)xa0β length of the string. The second line of each test case contains a non-empty binary string of length $$$n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For every test case print four integers $$$l_1$$$, $$$r_1$$$, $$$l_2$$$, $$$r_2$$$, which denote the beginning of the first substring, the end of the first substring, the beginning of the second substring, and the end of the second substring, respectively. If there are multiple solutions, print any. Example Input 7 6 101111 9 111000111 8 10000000 5 11011 6 001111 3 101 30 100000000000000100000000000000 Output 3 6 1 3 1 9 4 9 5 8 1 4 1 5 3 5 1 6 2 4 1 2 2 3 1 15 16 30 Note In the first testcase $$$f(t) = f(1111) = 15$$$, $$$f(w) = f(101) = 5$$$. In the second testcase $$$f(t) = f(111000111) = 455$$$, $$$f(w) = f(000111) = 7$$$. In the third testcase $$$f(t) = f(0000) = 0$$$, $$$f(w) = f(1000) = 8$$$. In the fourth testcase $$$f(t) = f(11011) = 27$$$, $$$f(w) = f(011) = 3$$$. In the fifth testcase $$$f(t) = f(001111) = 15$$$, $$$f(w) = f(011) = 3$$$. | 1,500 | true | false | false | false | false | true | false | false | false | false | 2,818 |
200E | While most students still sit their exams, the tractor college has completed the summer exam session. In fact, students study only one subject at this college β the Art of Operating a Tractor. Therefore, at the end of a term a student gets only one mark, a three (satisfactory), a four (good) or a five (excellent). Those who score lower marks are unfortunately expelled. The college has _n_ students, and oddly enough, each of them can be on scholarship. The size of the scholarships varies each term. Since the end-of-the-term exam has just ended, it's time to determine the size of the scholarship to the end of next term. The monthly budget for the scholarships of the Tractor college is _s_ rubles. To distribute the budget optimally, you must follow these rules: The students who received the same mark for the exam, should receive the same scholarship; Let us denote the size of the scholarship (in roubles) for students who have received marks 3, 4 and 5 for the exam, as _k_3, _k_4 and _k_5, respectively. The values _k_3, _k_4 and _k_5 must be integers and satisfy the inequalities 0u2009β€u2009_k_3u2009β€u2009_k_4u2009β€u2009_k_5; Let's assume that _c_3, _c_4, _c_5 show how many students received marks 3, 4 and 5 for the exam, respectively. The budget of the scholarship should be fully spent on them, that is, _c_3Β·_k_3u2009+u2009_c_4Β·_k_4u2009+u2009_c_5Β·_k_5u2009=u2009_s_; Let's introduce function β the value that shows how well the scholarships are distributed between students. In the optimal distribution function _f_(_k_3,u2009_k_4,u2009_k_5) takes the minimum possible value. Given the results of the exam, and the budget size _s_, you have to find the optimal distribution of the scholarship. Input The first line has two integers _n_, _s_ (3u2009β€u2009_n_u2009β€u2009300,u20091u2009β€u2009_s_u2009β€u20093Β·105) β the number of students and the budget size for the scholarship, respectively. The second line contains _n_ integers, where the _i_-th number represents the mark that the _i_-th student got for the exam. It is guaranteed that at each mark was given to at least one student. Output On a single line print three integers _k_3, _k_4 and _k_5 β the sought values that represent the optimal distribution of the scholarships. If there are multiple optimal answers, print any of them. If there is no answer, print -1. | 2,400 | true | false | true | false | false | false | false | false | false | false | 9,040 |
1342D | So you decided to hold a contest on Codeforces. You prepared the problems: statements, solutions, checkers, validators, tests... Suddenly, your coordinator asks you to change all your tests to multiple testcases in the easiest problem! Initially, each test in that problem is just an array. The maximum size of an array is $$$k$$$. For simplicity, the contents of arrays don't matter. You have $$$n$$$ tests β the $$$i$$$-th test is an array of size $$$m_i$$$ ($$$1 le m_i le k$$$). Your coordinator asks you to distribute all of your arrays into multiple testcases. Each testcase can include multiple arrays. However, each testcase should include no more than $$$c_1$$$ arrays of size greater than or equal to $$$1$$$ ($$$ge 1$$$), no more than $$$c_2$$$ arrays of size greater than or equal to $$$2$$$, $$$dots$$$, no more than $$$c_k$$$ arrays of size greater than or equal to $$$k$$$. Also, $$$c_1 ge c_2 ge dots ge c_k$$$. So now your goal is to create the new testcases in such a way that: each of the initial arrays appears in exactly one testcase; for each testcase the given conditions hold; the number of testcases is minimum possible. Print the minimum possible number of testcases you can achieve and the sizes of arrays included in each testcase. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n, k le 2 cdot 10^5$$$)xa0β the number of initial tests and the limit for the size of each array. The second line contains $$$n$$$ integers $$$m_1, m_2, dots, m_n$$$ ($$$1 le m_i le k$$$)xa0β the sizes of the arrays in the original tests. The third line contains $$$k$$$ integers $$$c_1, c_2, dots, c_k$$$ ($$$n ge c_1 ge c_2 ge dots ge c_k ge 1$$$); $$$c_i$$$ is the maximum number of arrays of size greater than or equal to $$$i$$$ you can have in a single testcase. Output In the first line print a single integer $$$ans$$$ ($$$1 le ans le n$$$)xa0β the minimum number of testcases you can achieve. Each of the next $$$ans$$$ lines should contain the description of a testcase in the following format: $$$t$$$ $$$a_1$$$ $$$a_2$$$ $$$dots$$$ $$$a_{t}$$$ ($$$1 le tle n$$$)xa0β the testcase includes $$$t$$$ arrays, $$$a_i$$$ is the size of the $$$i$$$-th array in that testcase. Each of the initial arrays should appear in exactly one testcase. In particular, it implies that the sum of $$$t$$$ over all $$$ans$$$ testcases should be equal to $$$n$$$. Note that the answer always exists due to $$$c_k ge 1$$$ (and therefore $$$c_1 ge 1$$$). If there are multiple answers, you can output any one of them. Examples Input 6 10 5 8 1 10 8 7 6 6 4 4 3 2 2 2 1 1 Output 5 1 1 1 1 1 1 1 1 1 1 Note In the first example there is no way to distribute the tests into less than $$$3$$$ testcases. The given answer satisfies the conditions: each of the testcases includes no more than $$$4$$$ arrays of size greater than or equal to $$$1$$$ and no more than $$$1$$$ array of sizes greater than or equal to $$$2$$$ and $$$3$$$. Note that there are multiple valid answers for this test. For example, testcases with sizes $$$[[2], [1, 2], [3]]$$$ would also be correct. However, testcases with sizes $$$[[1, 2], [2, 3]]$$$ would be incorrect because there are $$$2$$$ arrays of size greater than or equal to $$$2$$$ in the second testcase. Note the difference between the third and the fourth examples. You can include up to $$$5$$$ arrays of size greater than or equal to $$$1$$$ in the third example, so you can put all arrays into a single testcase. And you can have only up to $$$1$$$ array in the fourth example. Thus, every array should be included in a separate testcase. | 1,900 | false | true | false | false | true | true | false | true | true | false | 3,996 |
1665D | This is an interactive problem. There is a positive integer $$$1 le x le 10^9$$$ that you have to guess. In one query you can choose two positive integers $$$a eq b$$$. As an answer to this query you will get $$$gcd(x + a, x + b)$$$, where $$$gcd(n, m)$$$ is the denoting the number of test cases. The integer $$$x$$$ that you have to guess satisfies the constraints: ($$$1 le x le 10^9$$$). Interaction The hidden number $$$x$$$ is fixed before the start of the interaction and does not depend on your queries. To guess each $$$x$$$ you can make no more than $$$30$$$ queries in the following way: "? a b" ($$$1 le a, b le 2 cdot 10^9$$$, $$$a eq b$$$). For this query you will get $$$gcd(x + a, x + b)$$$. When you know $$$x$$$, print a single line in the following format. "! x" ($$$1 le x le 10^9$$$). After that continue to solve the next test case. If you ask more than $$$30$$$ queries for one $$$x$$$ or make an invalid query, the interactor will terminate immediately and your program will receive verdict Wrong Answer. After printing each query do not forget to output end of line and flush the output buffer. Otherwise, you will get the Idleness limit exceeded verdict. To do flush use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; Read documentation for other languages. Hacks To use hacks, use the following format of tests: The first line should contain a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β the number of test cases. The first and only line of each test case should contain a single integer $$$x$$$ ($$$1 le x le 10^9$$$) denoting the integer $$$x$$$ that should be guessed. Example Output ? 1 2 ? 12 4 ! 4 ? 2000000000 1999999999 ! 1000000000 Note The first hidden number is $$$4$$$, that's why the answers for the queries are: "? 1 2"xa0β $$$gcd(4 + 1, 4 + 2) = gcd(5, 6) = 1$$$. "? 12 4"xa0β $$$gcd(4 + 12, 4 + 4) = gcd(16, 8) = 8$$$. The second hidden number is $$$10^9$$$, that's why the answer for the query is: "? 2000000000 1999999999"xa0β $$$gcd(3 cdot 10^9, 3 cdot 10^9 - 1) = 1$$$. These queries are made only for understanding the interaction and are not enough for finding the true $$$x$$$. | 2,000 | true | false | false | false | false | true | false | false | false | false | 2,287 |
160D | You are given a connected weighted undirected graph without any loops and multiple edges. Let us remind you that a graph's spanning tree is defined as an acyclic connected subgraph of the given graph that includes all of the graph's vertexes. The weight of a tree is defined as the sum of weights of the edges that the given tree contains. The minimum spanning tree (MST) of a graph is defined as the graph's spanning tree having the minimum possible weight. For any connected graph obviously exists the minimum spanning tree, but in the general case, a graph's minimum spanning tree is not unique. Your task is to determine the following for each edge of the given graph: whether it is either included in any MST, or included at least in one MST, or not included in any MST. Input The first line contains two integers _n_ and _m_ (2u2009β€u2009_n_u2009β€u2009105, ) β the number of the graph's vertexes and edges, correspondingly. Then follow _m_ lines, each of them contains three integers β the description of the graph's edges as "_a__i_ _b__i_ _w__i_" (1u2009β€u2009_a__i_,u2009_b__i_u2009β€u2009_n_,u20091u2009β€u2009_w__i_u2009β€u2009106,u2009_a__i_u2009β u2009_b__i_), where _a__i_ and _b__i_ are the numbers of vertexes connected by the _i_-th edge, _w__i_ is the edge's weight. It is guaranteed that the graph is connected and doesn't contain loops or multiple edges. Output Print _m_ lines β the answers for all edges. If the _i_-th edge is included in any MST, print "any"; if the _i_-th edge is included at least in one MST, print "at least one"; if the _i_-th edge isn't included in any MST, print "none". Print the answers for the edges in the order in which the edges are specified in the input. Examples Input 4 5 1 2 101 1 3 100 2 3 2 2 4 2 3 4 1 Output none any at least one at least one any Output at least one at least one at least one Note In the second sample the MST is unique for the given graph: it contains two first edges. In the third sample any two edges form the MST for the given graph. That means that each edge is included at least in one MST. | 2,300 | false | false | false | false | false | false | false | false | true | true | 9,232 |
1427G | You have to paint with shades of grey the tiles of an $$$n imes n$$$ wall. The wall has $$$n$$$ rows of tiles, each with $$$n$$$ tiles. The tiles on the boundary of the wall (i.e., on the first row, last row, first column and last column) are already painted and you shall not change their color. All the other tiles are not painted. Some of the tiles are broken, you shall not paint those tiles. It is guaranteed that the tiles on the boundary are not broken. You shall paint all the non-broken tiles that are not already painted. When you paint a tile you can choose from $$$10^9$$$ shades of grey, indexed from $$$1$$$ to $$$10^9$$$. You can paint multiple tiles with the same shade. Formally, painting the wall is equivalent to assigning a shade (an integer between $$$1$$$ and $$$10^9$$$) to each non-broken tile that is not already painted. The contrast between two tiles is the absolute value of the difference between the shades of the two tiles. The total contrast of the wall is the sum of the contrast of all the pairs of adjacent non-broken tiles (two tiles are adjacent if they share a side). Compute the minimum possible total contrast of the wall. Input The first line contains $$$n$$$ ($$$3le nle 200$$$) β the number of rows and columns. Then $$$n$$$ lines, each containing $$$n$$$ integers, follow. The $$$i$$$-th of these lines describe the $$$i$$$-th row of tiles. It contains the $$$n$$$ integers $$$a_{ij}$$$ ($$$-1le a_{ij} le 10^9)$$$. The value of $$$a_{ij}$$$ described the tile on the $$$i$$$-th row and $$$j$$$-th column: If $$$a_{ij}=0$$$, then the tile is not painted and shall be painted. If $$$a_{ij}=-1$$$, then the tile is broken and shall not be painted. If $$$1le a_{ij}le 10^9$$$, then the tile is already painted with the shade $$$a_{ij}$$$. It is guaranteed that the tiles on the boundary are already painted, the tiles not on the boundary are not already painted, and the tiles on the boundary are not broken. Output Print a single integer β the minimum possible total contrast of the wall. Examples Input 3 10 100 1 1 -1 100 10 10 10 Input 5 6 6 5 4 4 6 0 0 0 4 7 0 0 0 3 8 0 0 0 2 8 8 1 2 2 Input 7 315055237 841510063 581663979 148389224 405375301 243686840 882512379 683199716 -1 -1 0 0 0 346177625 496442279 0 0 0 0 0 815993623 223938231 0 0 -1 0 0 16170511 44132173 0 -1 0 0 0 130735659 212201259 0 0 -1 0 0 166102576 123213235 506794677 467013743 410119347 791447348 80193382 142887538 Note Explanation of the first testcase: The initial configuration of the tiles is (tiles to paint are denoted by ?): 1 7 6 4 ? 6 1 1 1 A possible way to paint the tile achieving the minimum possible contrast of $$$26$$$ is: 1 7 6 4 5 6 1 1 1 Explanation of the second testcase: Since all tiles are either painted or broken, there is nothing to do. The total contrast is $$$396$$$. Explanation of the third testcase: The initial configuration of the tiles is (tiles to paint are denoted by ?): 6 6 5 4 4 6 ? ? ? 4 7 ? ? ? 3 8 ? ? ? 2 8 8 1 2 2 A possible way to paint the tiles achieving the minimum possible contrast of $$$34$$$ is: 6 6 5 4 4 6 6 5 4 4 7 7 5 3 3 8 8 2 2 2 8 8 1 2 2 | 3,300 | false | false | false | false | false | false | false | false | false | true | 3,527 |
1015A | You are given a set of $$$n$$$ segments on the axis $$$Ox$$$, each segment has integer endpoints between $$$1$$$ and $$$m$$$ inclusive. Segments may intersect, overlap or even coincide with each other. Each segment is characterized by two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le m$$$) β coordinates of the left and of the right endpoints. Consider all integer points between $$$1$$$ and $$$m$$$ inclusive. Your task is to print all such points that don't belong to any segment. The point $$$x$$$ belongs to the segment $$$[l; r]$$$ if and only if $$$l le x le r$$$. Input The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 100$$$) β the number of segments and the upper bound for coordinates. The next $$$n$$$ lines contain two integers each $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le m$$$) β the endpoints of the $$$i$$$-th segment. Segments may intersect, overlap or even coincide with each other. Note, it is possible that $$$l_i=r_i$$$, i.e. a segment can degenerate to a point. Output In the first line print one integer $$$k$$$ β the number of points that don't belong to any segment. In the second line print exactly $$$k$$$ integers in any order β the points that don't belong to any segment. All points you print should be distinct. If there are no such points at all, print a single integer $$$0$$$ in the first line and either leave the second line empty or do not print it at all. Note In the first example the point $$$1$$$ belongs to the second segment, the point $$$2$$$ belongs to the first and the second segments and the point $$$5$$$ belongs to the third segment. The points $$$3$$$ and $$$4$$$ do not belong to any segment. In the second example all the points from $$$1$$$ to $$$7$$$ belong to the first segment. | 800 | false | false | true | false | false | false | false | false | false | false | 5,619 |
1538B | Polycarp has $$$n$$$ friends, the $$$i$$$-th of his friends has $$$a_i$$$ candies. Polycarp's friends do not like when they have different numbers of candies. In other words they want all $$$a_i$$$ to be the same. To solve this, Polycarp performs the following set of actions exactly once: Polycarp chooses $$$k$$$ ($$$0 le k le n$$$) arbitrary friends (let's say he chooses friends with indices $$$i_1, i_2, ldots, i_k$$$); Polycarp distributes their $$$a_{i_1} + a_{i_2} + ldots + a_{i_k}$$$ candies among all $$$n$$$ friends. During distribution for each of $$$a_{i_1} + a_{i_2} + ldots + a_{i_k}$$$ candies he chooses new owner. That can be any of $$$n$$$ friends. Note, that any candy can be given to the person, who has owned that candy before the distribution process. Note that the number $$$k$$$ is not fixed in advance and can be arbitrary. Your task is to find the minimum value of $$$k$$$. For example, if $$$n=4$$$ and $$$a=[4, 5, 2, 5]$$$, then Polycarp could make the following distribution of the candies: Polycarp chooses $$$k=2$$$ friends with indices $$$i=[2, 4]$$$ and distributes $$$a_2 + a_4 = 10$$$ candies to make $$$a=[4, 4, 4, 4]$$$ (two candies go to person $$$3$$$). Note that in this example Polycarp cannot choose $$$k=1$$$ friend so that he can redistribute candies so that in the end all $$$a_i$$$ are equal. For the data $$$n$$$ and $$$a$$$, determine the minimum value $$$k$$$. With this value $$$k$$$, Polycarp should be able to select $$$k$$$ friends and redistribute their candies so that everyone will end up with the same number of candies. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$). Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^4$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case output: the minimum value of $$$k$$$, such that Polycarp can choose exactly $$$k$$$ friends so that he can redistribute the candies in the desired way; "-1" if no such value $$$k$$$ exists. Example Input 5 4 4 5 2 5 2 0 4 5 10 8 5 1 4 1 10000 7 1 1 1 1 1 1 1 | 800 | true | true | false | false | false | false | false | false | false | false | 2,954 |
1256F | You are given two strings $$$s$$$ and $$$t$$$ both of length $$$n$$$ and both consisting of lowercase Latin letters. In one move, you can choose any length $$$len$$$ from $$$1$$$ to $$$n$$$ and perform the following operation: Choose any contiguous substring of the string $$$s$$$ of length $$$len$$$ and reverse it; at the same time choose any contiguous substring of the string $$$t$$$ of length $$$len$$$ and reverse it as well. Note that during one move you reverse exactly one substring of the string $$$s$$$ and exactly one substring of the string $$$t$$$. Also note that borders of substrings you reverse in $$$s$$$ and in $$$t$$$ can be different, the only restriction is that you reverse the substrings of equal length. For example, if $$$len=3$$$ and $$$n=5$$$, you can reverse $$$s[1 dots 3]$$$ and $$$t[3 dots 5]$$$, $$$s[2 dots 4]$$$ and $$$t[2 dots 4]$$$, but not $$$s[1 dots 3]$$$ and $$$t[1 dots 2]$$$. Your task is to say if it is possible to make strings $$$s$$$ and $$$t$$$ equal after some (possibly, empty) sequence of moves. You have to answer $$$q$$$ independent test cases. Input The first line of the input contains one integer $$$q$$$ ($$$1 le q le 10^4$$$) β the number of test cases. Then $$$q$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) β the length of $$$s$$$ and $$$t$$$. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. The third line of the test case contains one string $$$t$$$ consisting of $$$n$$$ lowercase Latin letters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$ ($$$sum n le 2 cdot 10^5$$$). Output For each test case, print the answer on it β "YES" (without quotes) if it is possible to make strings $$$s$$$ and $$$t$$$ equal after some (possibly, empty) sequence of moves and "NO" otherwise. Example Input 4 4 abcd abdc 5 ababa baaba 4 asdf asdg 4 abcd badc | 2,000 | false | false | false | false | false | true | false | false | true | false | 4,402 |
1010F | The Main Martian Tree grows on Mars. It is a binary tree (a rooted tree, with no more than two sons at each vertex) with $$$n$$$ vertices, where the root vertex has the number $$$1$$$. Its fruits are the Main Martian Fruits. It's summer now, so this tree does not have any fruit yet. Autumn is coming soon, and leaves and branches will begin to fall off the tree. It is clear, that if a vertex falls off the tree, then its entire subtree will fall off too. In addition, the root will remain on the tree. Formally: the tree will have some connected subset of vertices containing the root. After that, the fruits will grow on the tree (only at those vertices which remain). Exactly $$$x$$$ fruits will grow in the root. The number of fruits in each remaining vertex will be not less than the sum of the numbers of fruits in the remaining sons of this vertex. It is allowed, that some vertices will not have any fruits. Natasha wondered how many tree configurations can be after the described changes. Since this number can be very large, output it modulo $$$998244353$$$. Two configurations of the resulting tree are considered different if one of these two conditions is true: they have different subsets of remaining vertices; they have the same subset of remaining vertices, but there is a vertex in this subset where they have a different amount of fruits. Input The first line contains two integers: $$$n$$$ and $$$x$$$ ($$$1 le n le 10^5$$$, $$$0 le x le 10^{18}$$$)xa0β the size of the tree and the number of fruits in the root. The $$$i$$$-th of the following $$$(n-1)$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i, b_i le n$$$)xa0β vertices connected by the $$$i$$$-th edge of the tree. It is guaranteed that the input data describes a correct binary tree with the root at the vertex $$$1$$$. Note Consider the first example. There are $$$2$$$ fruits at the vertex $$$1$$$. The following $$$13$$$ options are possible: there is no vertex $$$2$$$, there is no vertex $$$3$$$; there is no vertex $$$2$$$, there are no fruits at the vertex $$$3$$$; there is no vertex $$$2$$$, there is $$$1$$$ fruit at the vertex $$$3$$$; there is no vertex $$$2$$$, there are $$$2$$$ fruits at the vertex $$$3$$$; there are no fruits at the vertex $$$2$$$, there is no vertex $$$3$$$; there are no fruits at the vertex $$$2$$$, there are no fruits at the vertex $$$3$$$; there are no fruits at the vertex $$$2$$$, there is $$$1$$$ fruit at the vertex $$$3$$$; there are no fruits at the vertex $$$2$$$, there are $$$2$$$ fruits at the vertex $$$3$$$; there is $$$1$$$ fruit at the vertex $$$2$$$, there is no vertex $$$3$$$; there is $$$1$$$ fruit at the vertex $$$2$$$, there are no fruits at the vertex $$$3$$$; there is $$$1$$$ fruit at the vertex $$$2$$$, there is $$$1$$$ fruit at the vertex $$$3$$$; there are $$$2$$$ fruits at the vertex $$$2$$$, there is no vertex $$$3$$$; there are $$$2$$$ fruits at the vertex $$$2$$$, there are no fruits at the vertex $$$3$$$. Consider the second example. There are $$$5$$$ fruits at the vertex $$$1$$$. The following $$$7$$$ options are possible: there is no vertex $$$2$$$; there are no fruits at the vertex $$$2$$$; there is $$$1$$$ fruit at the vertex $$$2$$$; there are $$$2$$$ fruits at the vertex $$$2$$$; there are $$$3$$$ fruits at the vertex $$$2$$$; there are $$$4$$$ fruits at the vertex $$$2$$$; there are $$$5$$$ fruits at the vertex $$$2$$$. | 3,400 | false | false | false | false | false | false | false | false | false | true | 5,630 |
1750H | You are given a binary string $$$s$$$ of length $$$n$$$. We define a maximal substring as a substring that cannot be extended while keeping all elements equal. For example, in the string $$$11000111$$$ there are three maximal substrings: $$$11$$$, $$$000$$$ and $$$111$$$. In one operation, you can select two maximal adjacent substrings. Since they are maximal and adjacent, it's easy to see their elements must have different values. Let $$$a$$$ be the length of the sequence of ones and $$$b$$$ be the length of the sequence of zeros. Then do the following: If $$$a ge b$$$, then replace $$$b$$$ selected zeros with $$$b$$$ ones. If $$$a < b$$$, then replace $$$a$$$ selected ones with $$$a$$$ zeros. As an example, for $$$1110000$$$ we make it $$$0000000$$$, for $$$0011$$$ we make it $$$1111$$$. We call a string being good if it can be turned into $$$1111...1111$$$ using the aforementioned operation any number of times (possibly, zero). Find the number of good substrings among all $$$frac{n(n+1)}{2}$$$ non-empty substrings of $$$s$$$. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^5$$$) β the number of test cases. The description of test cases follows. The first line of each test case contains $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) β the length of the string $$$s$$$. The second line of each test case contains the binary string $$$s$$$ of length $$$n$$$. It is guaranteed that sum of $$$n$$$ across all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print a single integer β the number of good substrings. Example Input 4 6 100011 3 101 5 11111 6 010101 Note Let's define a substring from index $$$l$$$ to index $$$r$$$ as $$$[l, r]$$$. For the first test case, the good substrings are: $$$[1,1]$$$, $$$[1,2]$$$, $$$[3,6]$$$, $$$[4,5]$$$, $$$[4,6]$$$, $$$[5,5]$$$, $$$[5,6]$$$, $$$[6,6]$$$. In the second test case, all substrings are good except $$$[2,2]$$$. In the third test case, all substrings are good. | 3,500 | false | false | false | true | true | true | false | false | false | false | 1,778 |
1293A | A.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them. It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal. ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there. CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant. Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way! Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 le n le 10^9$$$, $$$1 le s le n$$$, $$$1 le k le min(n-1, 1000)$$$)xa0β respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, ldots, a_k$$$ ($$$1 le a_i le n$$$)xa0β the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$. Output For each test case print a single integerxa0β the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant. Example Input 5 5 2 3 1 2 3 4 3 3 4 1 2 10 2 6 1 2 3 4 5 7 2 1 1 2 100 76 8 76 75 36 67 41 74 10 77 Note In the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$. In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere. In the third example test case, the closest open restaurant is on the $$$6$$$-th floor. | 1,100 | false | false | true | false | false | false | true | true | false | false | 4,231 |
57E | Brian the Rabbit adores chess. Not long ago he argued with Stewie the Rabbit that a knight is better than a king. To prove his point he tries to show that the knight is very fast but Stewie doesn't accept statements without evidence. He constructed an infinite chessboard for Brian, where he deleted several squares to add some more interest to the game. Brian only needs to count how many different board squares a knight standing on a square with coordinates of (0,u20090) can reach in no more than _k_ moves. Naturally, it is forbidden to move to the deleted squares. Brian doesn't very much like exact sciences himself and is not acquainted with programming, that's why he will hardly be able to get ahead of Stewie who has already started solving the problem. Help Brian to solve the problem faster than Stewie. Input The first line contains two integers _k_ and _n_ (0u2009β€u2009_k_u2009β€u20091018,u20090u2009β€u2009_n_u2009β€u2009440) which are correspondingly the maximal number of moves a knight can make and the number of deleted cells. Then follow _n_ lines, each giving the coordinates of a deleted square in the form (_x__i_,u2009_y__i_) (_x__i_u2009β€u200910,u2009_y__i_u2009β€u200910). All the numbers are integer, the deleted squares are different and it is guaranteed that the square (0,u20090) is not deleted. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d). Output You must print the answer on a single line. As it can be rather long, you should print it modulo 1000000007. Examples Input 2 7 -1 2 1 2 2 1 2 -1 1 -2 -1 -2 -2 -1 | 3,000 | true | false | false | false | false | false | false | false | false | false | 9,686 |
475D | Problem - 475D - Codeforces =============== xa0 ]( "Bayan Contest Warm Up round + 50 tshirts!") such that 1u2009β€u2009_l_u2009β€u2009_r_u2009β€u2009_n_ and _gcd_(_a__l_,u2009_a__l_u2009+u20091,u2009...,u2009_a__r_)u2009=u2009_x__i_. is a greatest common divisor of _v_1,u2009_v_2,u2009...,u2009_v__n_, that is equal to a largest positive integer that divides all _v__i_. Input The first line of the input contains integer _n_, (1u2009β€u2009_n_u2009β€u2009105), denoting the length of the sequence. The next line contains _n_ space separated integers _a_1,u2009...,u2009_a__n_, (1u2009β€u2009_a__i_u2009β€u2009109). The third line of the input contains integer _q_, (1u2009β€u2009_q_u2009β€u20093u2009Γu2009105), denoting the number of queries. Then follows _q_ lines, each contain an integer _x__i_, (1u2009β€u2009_x__i_u2009β€u2009109). Output For each query print the result in a separate line. Examples Input 3 2 6 3 5 1 2 3 4 6 Output 1 2 2 0 1 Input 7 10 20 3 15 1000 60 16 10 1 2 3 4 5 6 10 20 60 1000 Output 14 0 2 2 2 0 2 2 1 1 | 2,000 | true | false | false | false | true | false | true | false | false | false | 7,931 |
592B | Ari the monster always wakes up very early with the first ray of the sun and the first thing she does is feeding her squirrel. Ari draws a regular convex polygon on the floor and numbers it's vertices 1,u20092,u2009...,u2009_n_ in clockwise order. Then starting from the vertex 1 she draws a ray in the direction of each other vertex. The ray stops when it reaches a vertex or intersects with another ray drawn before. Ari repeats this process for vertex 2,u20093,u2009...,u2009_n_ (in this particular order). And then she puts a walnut in each region inside the polygon. Ada the squirrel wants to collect all the walnuts, but she is not allowed to step on the lines drawn by Ari. That means Ada have to perform a small jump if she wants to go from one region to another. Ada can jump from one region P to another region Q if and only if P and Q share a side or a corner. Assuming that Ada starts from outside of the picture, what is the minimum number of jumps she has to perform in order to collect all the walnuts? Input The first and only line of the input contains a single integer _n_ (3u2009β€u2009_n_u2009β€u200954321) - the number of vertices of the regular polygon drawn by Ari. Output Print the minimum number of jumps Ada should make to collect all the walnuts. Note, that she doesn't need to leave the polygon after. Note One of the possible solutions for the first sample is shown on the picture above. | 1,100 | true | false | false | false | false | false | false | false | false | false | 7,481 |
730I | There are _n_ students at Berland State University. Every student has two skills, each measured as a number: _a__i_ β the programming skill and _b__i_ β the sports skill. It is announced that an Olympiad in programming and sports will be held soon. That's why Berland State University should choose two teams: one to take part in the programming track and one to take part in the sports track. There should be exactly _p_ students in the programming team and exactly _s_ students in the sports team. A student can't be a member of both teams. The university management considers that the strength of the university on the Olympiad is equal to the sum of two values: the programming team strength and the sports team strength. The strength of a team is the sum of skills of its members in the corresponding area, so the strength of the programming team is the sum of all _a__i_ and the strength of the sports team is the sum of all _b__i_ over corresponding team members. Help Berland State University to compose two teams to maximize the total strength of the university on the Olympiad. Input The first line contains three positive integer numbers _n_, _p_ and _s_ (2u2009β€u2009_n_u2009β€u20093000, _p_u2009+u2009_s_u2009β€u2009_n_) β the number of students, the size of the programming team and the size of the sports team. The second line contains _n_ positive integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009β€u2009_a__i_u2009β€u20093000), where _a__i_ is the programming skill of the _i_-th student. The third line contains _n_ positive integers _b_1,u2009_b_2,u2009...,u2009_b__n_ (1u2009β€u2009_b__i_u2009β€u20093000), where _b__i_ is the sports skill of the _i_-th student. Output In the first line, print the the maximum strength of the university on the Olympiad. In the second line, print _p_ numbers β the members of the programming team. In the third line, print _s_ numbers β the members of the sports team. The students are numbered from 1 to _n_ as they are given in the input. All numbers printed in the second and in the third lines should be distinct and can be printed in arbitrary order. If there are multiple solutions, print any of them. Examples Input 5 2 2 1 3 4 5 2 5 3 2 1 4 Input 4 2 2 10 8 8 3 10 7 9 4 Input 5 3 1 5 2 5 1 7 6 3 1 6 3 | 2,000 | false | true | false | true | false | false | false | false | false | true | 6,880 |
796B | Zane the wizard is going to perform a magic show shuffling the cups. There are _n_ cups, numbered from 1 to _n_, placed along the _x_-axis on a table that has _m_ holes on it. More precisely, cup _i_ is on the table at the position _x_u2009=u2009_i_. The problematic bone is initially at the position _x_u2009=u20091. Zane will confuse the audience by swapping the cups _k_ times, the _i_-th time of which involves the cups at the positions _x_u2009=u2009_u__i_ and _x_u2009=u2009_v__i_. If the bone happens to be at the position where there is a hole at any time, it will fall into the hole onto the ground and will not be affected by future swapping operations. Do not forget that Zane is a wizard. When he swaps the cups, he does not move them ordinarily. Instead, he teleports the cups (along with the bone, if it is inside) to the intended positions. Therefore, for example, when he swaps the cup at _x_u2009=u20094 and the one at _x_u2009=u20096, they will not be at the position _x_u2009=u20095 at any moment during the operation. Zaneβs puppy, Inzane, is in trouble. Zane is away on his vacation, and Inzane cannot find his beloved bone, as it would be too exhausting to try opening all the cups. Inzane knows that the Codeforces community has successfully helped Zane, so he wants to see if it could help him solve his problem too. Help Inzane determine the final position of the bone. Input The first line contains three integers _n_, _m_, and _k_ (2u2009β€u2009_n_u2009β€u2009106, 1u2009β€u2009_m_u2009β€u2009_n_, 1u2009β€u2009_k_u2009β€u20093Β·105)xa0β the number of cups, the number of holes on the table, and the number of swapping operations, respectively. The second line contains _m_ distinct integers _h_1,u2009_h_2,u2009...,u2009_h__m_ (1u2009β€u2009_h__i_u2009β€u2009_n_)xa0β the positions along the _x_-axis where there is a hole on the table. Each of the next _k_ lines contains two integers _u__i_ and _v__i_ (1u2009β€u2009_u__i_,u2009_v__i_u2009β€u2009_n_, _u__i_u2009β u2009_v__i_)xa0β the positions of the cups to be swapped. Output Print one integerxa0β the final position along the _x_-axis of the bone. Examples Input 7 3 4 3 4 6 1 2 2 5 5 7 7 1 Note In the first sample, after the operations, the bone becomes at _x_u2009=u20092, _x_u2009=u20095, _x_u2009=u20097, and _x_u2009=u20091, respectively. In the second sample, after the first operation, the bone becomes at _x_u2009=u20092, and falls into the hole onto the ground. | 1,300 | false | false | true | false | false | false | false | false | false | false | 6,592 |
1599H | This is an interactive problem! As part of your contribution in the Great Bubble War, you have been tasked with finding the newly built enemy fortress. The world you live in is a giant $$$10^9 imes 10^9$$$ grid, with squares having both coordinates between $$$1$$$ and $$$10^9$$$. You know that the enemy base has the shape of a rectangle, with the sides parallel to the sides of the grid. The people of your world are extremely scared of being at the edge of the world, so you know that the base doesn't contain any of the squares on the edges of the grid (the $$$x$$$ or $$$y$$$ coordinate being $$$1$$$ or $$$10^9$$$). To help you locate the base, you have been given a device that you can place in any square of the grid, and it will tell you the manhattan distance to the closest square of the base. The manhattan distance from square $$$(a, b)$$$ to square $$$(p, q)$$$ is calculated as $$$aβp+bβq$$$. If you try to place the device inside the enemy base, you will be captured by the enemy. Because of this, you need to make sure to never place the device inside the enemy base. Unfortunately, the device is powered by a battery and you can't recharge it. This means that you can use the device at most $$$40$$$ times. Input The input contains the answers to your queries. Interaction Your code is allowed to place the device on any square in the grid by writing "? $$$i$$$ $$$j$$$" $$$(1 leq i,j leq 10^9)$$$. In return, it will recieve the manhattan distance to the closest square of the enemy base from square $$$(i,j)$$$ or $$$-1$$$ if the square you placed the device on is inside the enemy base or outside the grid. If you recieve $$$-1$$$ instead of a positive number, exit immidiately and you will see the wrong answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream. Your solution should use no more than $$$40$$$ queries. Once you are sure where the enemy base is located, you should print "! $$$x$$$ $$$y$$$ $$$p$$$ $$$q$$$" $$$(1 leq x leq pleq 10^9, 1 leq y leq qleq 10^9)$$$, where $$$(x, y)$$$ is the square inside the enemy base with the smallest $$$x$$$ and $$$y$$$ coordinates, and $$$(p, q)$$$ is the square inside the enemy base with the largest $$$x$$$ and $$$y$$$ coordinates. Note that answering doesn't count as one of the 40 queries. After printing a query or printing the answer, do not forget to output 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. Example Output ? 2 2 ? 5 5 ? 4 7 ? 1 5 ! 2 3 4 5 | 2,100 | true | false | false | false | false | false | false | false | false | false | 2,657 |
496E | You are an assistant director in a new musical play. The play consists of _n_ musical parts, each part must be performed by exactly one actor. After the casting the director chose _m_ actors who can take part in the play. Your task is to assign the parts to actors. However, there are several limitations. First, each actor has a certain voice range and there are some parts that he cannot sing. Formally, there are two integers for each actor, _c__i_ and _d__i_ (_c__i_u2009β€u2009_d__i_)xa0β the pitch of the lowest and the highest note that the actor can sing. There also are two integers for each partxa0β _a__j_ and _b__j_ (_a__j_u2009β€u2009_b__j_)xa0β the pitch of the lowest and the highest notes that are present in the part. The _i_-th actor can perform the _j_-th part if and only if _c__i_u2009β€u2009_a__j_u2009β€u2009_b__j_u2009β€u2009_d__i_, i.e. each note of the part is in the actor's voice range. According to the contract, the _i_-th actor can perform at most _k__i_ parts. Besides, you are allowed not to give any part to some actors (then they take part in crowd scenes). The rehearsal starts in two hours and you need to do the assignment quickly! Input The first line contains a single integer _n_xa0β the number of parts in the play (1u2009β€u2009_n_u2009β€u2009105). Next _n_ lines contain two space-separated integers each, _a__j_ and _b__j_xa0β the range of notes for the _j_-th part (1u2009β€u2009_a__j_u2009β€u2009_b__j_u2009β€u2009109). The next line contains a single integer _m_xa0β the number of actors (1u2009β€u2009_m_u2009β€u2009105). Next _m_ lines contain three space-separated integers each, _c__i_, _d__i_ and _k__i_xa0β the range of the _i_-th actor and the number of parts that he can perform (1u2009β€u2009_c__i_u2009β€u2009_d__i_u2009β€u2009109, 1u2009β€u2009_k__i_u2009β€u2009109). Output If there is an assignment that meets all the criteria aboce, print a single word "YES" (without the quotes) in the first line. In the next line print _n_ space-separated integers. The _i_-th integer should be the number of the actor who should perform the _i_-th part. If there are multiple correct assignments, print any of them. If there is no correct assignment, print a single word "NO" (without the quotes). Examples Input 3 1 3 2 4 3 5 2 1 4 2 2 5 1 Input 3 1 3 2 4 3 5 2 1 3 2 2 5 1 | 2,100 | false | true | false | false | false | false | false | false | true | false | 7,847 |
1621D | Circular land is an $$$2n imes 2n$$$ grid. Rows of this grid are numbered by integers from $$$1$$$ to $$$2n$$$ from top to bottom and columns of this grid are numbered by integers from $$$1$$$ to $$$2n$$$ from left to right. The cell $$$(x, y)$$$ is the cell on the intersection of row $$$x$$$ and column $$$y$$$ for $$$1 leq x leq 2n$$$ and $$$1 leq y leq 2n$$$. There are $$$n^2$$$ of your friends in the top left corner of the grid. That is, in each cell $$$(x, y)$$$ with $$$1 leq x, y leq n$$$ there is exactly one friend. Some of the other cells are covered with snow. Your friends want to get to the bottom right corner of the grid. For this in each cell $$$(x, y)$$$ with $$$n+1 leq x, y leq 2n$$$ there should be exactly one friend. It doesn't matter in what cell each of friends will be. You have decided to help your friends to get to the bottom right corner of the grid. For this, you can give instructions of the following types: You select a row $$$x$$$. All friends in this row should move to the next cell in this row. That is, friend from the cell $$$(x, y)$$$ with $$$1 leq y < 2n$$$ will move to the cell $$$(x, y + 1)$$$ and friend from the cell $$$(x, 2n)$$$ will move to the cell $$$(x, 1)$$$. You select a row $$$x$$$. All friends in this row should move to the previous cell in this row. That is, friend from the cell $$$(x, y)$$$ with $$$1 < y leq 2n$$$ will move to the cell $$$(x, y - 1)$$$ and friend from the cell $$$(x, 1)$$$ will move to the cell $$$(x, 2n)$$$. You select a column $$$y$$$. All friends in this column should move to the next cell in this column. That is, friend from the cell $$$(x, y)$$$ with $$$1 leq x < 2n$$$ will move to the cell $$$(x + 1, y)$$$ and friend from the cell $$$(2n, y)$$$ will move to the cell $$$(1, y)$$$. You select a column $$$y$$$. All friends in this column should move to the previous cell in this column. That is, friend from the cell $$$(x, y)$$$ with $$$1 < x leq 2n$$$ will move to the cell $$$(x - 1, y)$$$ and friend from the cell $$$(1, y)$$$ will move to the cell $$$(2n, y)$$$. Note how friends on the grid border behave in these instructions. Example of applying the third operation to the second column. Here, colorful circles denote your friends and blue cells are covered with snow. You can give such instructions any number of times. You can give instructions of different types. If after any instruction one of your friends is in the cell covered with snow he becomes ill. In order to save your friends you can remove snow from some cells before giving the first instruction: You can select the cell $$$(x, y)$$$ that is covered with snow now and remove snow from this cell for $$$c_{x, y}$$$ coins. You can do this operation any number of times. You want to spend the minimal number of coins and give some instructions to your friends. After this, all your friends should be in the bottom right corner of the grid and none of them should be ill. Please, find how many coins you will spend. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0β the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 leq n leq 250$$$). Each of the next $$$2n$$$ lines contains $$$2n$$$ integers $$$c_{i, 1}, c_{i, 2}, ldots, c_{i, 2n}$$$ ($$$0 leq c_{i, j} leq 10^9$$$)xa0β costs of removing snow from cells. If $$$c_{i, j} = 0$$$ for some $$$i, j$$$ than there is no snow in cell $$$(i, j)$$$. Otherwise, cell $$$(i, j)$$$ is covered with snow. It is guaranteed that $$$c_{i, j} = 0$$$ for $$$1 leq i, j leq n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$250$$$. Output For each test case output one integerxa0β the minimal number of coins you should spend. Example Input 4 1 0 8 1 99 2 0 0 0 0 0 0 0 0 9 9 2 2 9 9 9 9 2 0 0 4 2 0 0 2 4 4 2 4 2 2 4 2 4 4 0 0 0 0 0 0 0 2 0 0 0 0 0 0 2 0 0 0 0 0 0 2 0 0 0 0 0 0 2 0 0 0 0 0 0 2 2 0 2 2 0 0 2 0 1 6 2 1 0 2 0 0 2 4 7 4 2 0 0 0 2 0 1 6 Note In the first test case you can remove snow from the cells $$$(2, 1)$$$ and $$$(2, 2)$$$ for $$$100$$$ coins. Then you can give instructions All friends in the first collum should move to the previous cell. After this, your friend will be in the cell $$$(2, 1)$$$. All friends in the second row should move to the next cell. After this, your friend will be in the cell $$$(2, 2)$$$. In the second test case you can remove all snow from the columns $$$3$$$ and $$$4$$$ for $$$22$$$ coins. Then you can give instructions All friends in the first row should move to the next cell. All friends in the first row should move to the next cell. All friends in the second row should move to the next cell. All friends in the second row should move to the next cell. All friends in the third column should move to the next cell. All friends in the third column should move to the next cell. All friends in the fourth column should move to the next cell. All friends in the fourth column should move to the next cell. It can be shown that none of the friends will become ill and that it is impossible to spend less coins. | 2,100 | true | true | false | false | false | true | false | false | false | false | 2,517 |
1067B | Someone give a strange birthday present to Ivan. It is hedgehogxa0β connected undirected graph in which one vertex has degree at least $$$3$$$ (we will call it center) and all other vertices has degree 1. Ivan thought that hedgehog is too boring and decided to make himself $$$k$$$-multihedgehog. Let us define $$$k$$$-multihedgehog as follows: $$$1$$$-multihedgehog is hedgehog: it has one vertex of degree at least $$$3$$$ and some vertices of degree 1. For all $$$k ge 2$$$, $$$k$$$-multihedgehog is $$$(k-1)$$$-multihedgehog in which the following changes has been made for each vertex $$$v$$$ with degree 1: let $$$u$$$ be its only neighbor; remove vertex $$$v$$$, create a new hedgehog with center at vertex $$$w$$$ and connect vertices $$$u$$$ and $$$w$$$ with an edge. New hedgehogs can differ from each other and the initial gift. Thereby $$$k$$$-multihedgehog is a tree. Ivan made $$$k$$$-multihedgehog but he is not sure that he did not make any mistakes. That is why he asked you to check if his tree is indeed $$$k$$$-multihedgehog. Input First line of input contains $$$2$$$ integers $$$n$$$, $$$k$$$ ($$$1 le n le 10^{5}$$$, $$$1 le k le 10^{9}$$$)xa0β number of vertices and hedgehog parameter. Next $$$n-1$$$ lines contains two integers $$$u$$$ $$$v$$$ ($$$1 le u, ,, v le n; ,, u e v$$$)xa0β indices of vertices connected by edge. It is guaranteed that given graph is a tree. Output Print "Yes" (without quotes), if given graph is $$$k$$$-multihedgehog, and "No" (without quotes) otherwise. Examples Input 14 2 1 4 2 4 3 4 4 13 10 5 11 5 12 5 14 5 5 13 6 7 8 6 13 6 9 6 Note 2-multihedgehog from the first example looks like this: Its center is vertex $$$13$$$. Hedgehogs created on last step are: [4 (center), 1, 2, 3], [6 (center), 7, 8, 9], [5 (center), 10, 11, 12, 13]. Tree from second example is not a hedgehog because degree of center should be at least $$$3$$$. | 1,800 | false | false | false | false | false | false | false | false | false | true | 5,374 |
1656D | We say that a positive integer $$$n$$$ is $$$k$$$-good for some positive integer $$$k$$$ if $$$n$$$ can be expressed as a sum of $$$k$$$ positive integers which give $$$k$$$ distinct remainders when divided by $$$k$$$. Given a positive integer $$$n$$$, find some $$$k geq 2$$$ so that $$$n$$$ is $$$k$$$-good or tell that such a $$$k$$$ does not exist. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^5$$$) β the number of test cases. Each test case consists of one line with an integer $$$n$$$ ($$$2 leq n leq 10^{18}$$$). Output For each test case, print a line with a value of $$$k$$$ such that $$$n$$$ is $$$k$$$-good ($$$k geq 2$$$), or $$$-1$$$ if $$$n$$$ is not $$$k$$$-good for any $$$k$$$. If there are multiple valid values of $$$k$$$, you can print any of them. Note $$$6$$$ is a $$$3$$$-good number since it can be expressed as a sum of $$$3$$$ numbers which give different remainders when divided by $$$3$$$: $$$6 = 1 + 2 + 3$$$. $$$15$$$ is also a $$$3$$$-good number since $$$15 = 1 + 5 + 9$$$ and $$$1, 5, 9$$$ give different remainders when divided by $$$3$$$. $$$20$$$ is a $$$5$$$-good number since $$$20 = 2 + 3 + 4 + 5 + 6$$$ and $$$2,3,4,5,6$$$ give different remainders when divided by $$$5$$$. | 1,900 | true | false | false | false | false | true | false | false | false | false | 2,351 |
1656C | You are given an array of $$$n$$$ non-negative integers $$$a_1, a_2, ldots, a_n$$$. You can make the following operation: choose an integer $$$x geq 2$$$ and replace each number of the array by the remainder when dividing that number by $$$x$$$, that is, for all $$$1 leq i leq n$$$ set $$$a_i$$$ to $$$a_i bmod x$$$. Determine if it is possible to make all the elements of the array equal by applying the operation zero or more times. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$) β the number of test cases. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 10^5$$$) β the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i leq 10^9$$$) where $$$a_i$$$ is the $$$i$$$-th element of the array. The sum of $$$n$$$ for all test cases is at most $$$2 cdot 10^5$$$. Output For each test case, print a line with YES if you can make all elements of the list equal by applying the operation. Otherwise, print NO. You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as a positive answer). Example Input 4 4 2 5 6 8 3 1 1 1 5 4 1 7 0 8 4 5 9 17 5 Note In the first test case, one can apply the operation with $$$x = 3$$$ to obtain the array $$$[2, 2, 0, 2]$$$, and then apply the operation with $$$x = 2$$$ to obtain $$$[0, 0, 0, 0]$$$. In the second test case, all numbers are already equal. In the fourth test case, applying the operation with $$$x = 4$$$ results in the array $$$[1, 1, 1, 1]$$$. | 1,200 | true | false | false | false | false | true | false | false | true | false | 2,352 |
1426E | Alice and Bob have decided to play the game "Rock, Paper, Scissors". The game consists of several rounds, each round is independent of each other. In each round, both players show one of the following things at the same time: rock, paper or scissors. If both players showed the same things then the round outcome is a draw. Otherwise, the following rules applied: if one player showed rock and the other one showed scissors, then the player who showed rock is considered the winner and the other one is considered the loser; if one player showed scissors and the other one showed paper, then the player who showed scissors is considered the winner and the other one is considered the loser; if one player showed paper and the other one showed rock, then the player who showed paper is considered the winner and the other one is considered the loser. Alice and Bob decided to play exactly $$$n$$$ rounds of the game described above. Alice decided to show rock $$$a_1$$$ times, show scissors $$$a_2$$$ times and show paper $$$a_3$$$ times. Bob decided to show rock $$$b_1$$$ times, show scissors $$$b_2$$$ times and show paper $$$b_3$$$ times. Though, both Alice and Bob did not choose the sequence in which they show things. It is guaranteed that $$$a_1 + a_2 + a_3 = n$$$ and $$$b_1 + b_2 + b_3 = n$$$. Your task is to find two numbers: 1. the minimum number of round Alice can win; 2. the maximum number of rounds Alice can win. Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 10^{9}$$$) β the number of rounds. The second line of the input contains three integers $$$a_1, a_2, a_3$$$ ($$$0 le a_i le n$$$) β the number of times Alice will show rock, scissors and paper, respectively. It is guaranteed that $$$a_1 + a_2 + a_3 = n$$$. The third line of the input contains three integers $$$b_1, b_2, b_3$$$ ($$$0 le b_j le n$$$) β the number of times Bob will show rock, scissors and paper, respectively. It is guaranteed that $$$b_1 + b_2 + b_3 = n$$$. Output Print two integers: the minimum and the maximum number of rounds Alice can win. Examples Input 686 479 178 29 11 145 530 Input 319 10 53 256 182 103 34 Note In the first example, Alice will not win any rounds if she shows scissors and then paper and Bob shows rock and then scissors. In the best outcome, Alice will win one round if she shows paper and then scissors, and Bob shows rock and then scissors. In the second example, Alice will not win any rounds if Bob shows the same things as Alice each round. In the third example, Alice always shows paper and Bob always shows rock so Alice will win all three rounds anyway. | 1,800 | true | true | false | false | false | true | true | false | false | false | 3,535 |
931A | Two friends are on the coordinate axis _Ox_ in points with integer coordinates. One of them is in the point _x_1u2009=u2009_a_, another one is in the point _x_2u2009=u2009_b_. Each of the friends can move by one along the line in any direction unlimited number of times. When a friend moves, the tiredness of a friend changes according to the following rules: the first move increases the tiredness by 1, the second move increases the tiredness by 2, the thirdxa0β by 3 and so on. For example, if a friend moves first to the left, then to the right (returning to the same point), and then again to the left his tiredness becomes equal to 1u2009+u20092u2009+u20093u2009=u20096. The friends want to meet in a integer point. Determine the minimum total tiredness they should gain, if they meet in the same point. Input The first line contains a single integer _a_ (1u2009β€u2009_a_u2009β€u20091000) β the initial position of the first friend. The second line contains a single integer _b_ (1u2009β€u2009_b_u2009β€u20091000) β the initial position of the second friend. It is guaranteed that _a_u2009β u2009_b_. Output Print the minimum possible total tiredness if the friends meet in the same point. Note In the first example the first friend should move by one to the right (then the meeting happens at point 4), or the second friend should move by one to the left (then the meeting happens at point 3). In both cases, the total tiredness becomes 1. In the second example the first friend should move by one to the left, and the second friend should move by one to the right. Then they meet in the point 100, and the total tiredness becomes 1u2009+u20091u2009=u20092. In the third example one of the optimal ways is the following. The first friend should move three times to the right, and the second friend β two times to the left. Thus the friends meet in the point 8, and the total tiredness becomes 1u2009+u20092u2009+u20093u2009+u20091u2009+u20092u2009=u20099. | 800 | true | true | true | false | false | false | true | false | false | false | 5,981 |
1372C | # Omkar and Baseball Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 256 megabytes Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across n sessions follow the identity permutation (ie. in the first game he scores 1 point, in the second game he scores 2 points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up! Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on [1 , 2, 3] can yield [3 , 1, 2] but it cannot yield [3 , 2, 1] since the 2 is in the same position. Given a permutation of n integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesnβt exceed 10 18 .An array a is a subarray of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. # Input Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 100 ). Description of the test cases follows. The first line of each test case contains integer n (1 β€ n β€ 2 Β· 10 5) x16 the length of the given permutation. The second line of each test case contains n integers a1, a 2, ..., a n (1 β€ ai β€ n) x16 the initial permutation. It is guaranteed that the sum of n over all test cases does not exceed 2 Β· 10 5. # Output For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation. # Example standard input standard output 251 2 3 4 5 73 2 4 5 1 6 7 02 # Note In the first permutation, it is already sorted so no exchanges are needed. It can be shown that you need at least 2 exchanges to sort the second permutation. [3 , 2, 4, 5, 1, 6, 7] Perform special exchange on range ( 1, 5) [4 , 1, 2, 3, 5, 6, 7] Perform special exchange on range ( 1, 4) [1 , 2, 3, 4, 5, 6, 7] Page 1 of 1 | 1,500 | true | false | false | false | false | true | false | false | false | false | 3,805 |
321B | Fox Ciel is playing a card game with her friend Jiro. Jiro has _n_ cards, each one has two attributes: _position_ (Attack or Defense) and _strength_. Fox Ciel has _m_ cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack. Now is Ciel's battle phase, Ciel can do the following operation many times: 1. Choose one of her cards _X_. This card mustn't be chosen before. 2. If Jiro has no alive cards at that moment, he gets the damage equal to (_X_'s strength). Otherwise, Ciel needs to choose one Jiro's alive card _Y_, then: If _Y_'s position is Attack, then (_X_'s strength) u2009β₯u2009 (_Y_'s strength) must hold. After this attack, card _Y_ dies, and Jiro gets the damage equal to (_X_'s strength) - (_Y_'s strength). If _Y_'s position is Defense, then (_X_'s strength) u2009>u2009 (_Y_'s strength) must hold. After this attack, card _Y_ dies, but Jiro gets no damage. Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get. Input The first line contains two integers _n_ and _m_ (1u2009β€u2009_n_,u2009_m_u2009β€u2009100) β the number of cards Jiro and Ciel have. Each of the next _n_ lines contains a string _position_ and an integer _strength_ (0u2009β€u2009_strength_u2009β€u20098000) β the position and strength of Jiro's current card. Position is the string "ATK" for attack, and the string "DEF" for defense. Each of the next _m_ lines contains an integer _strength_ (0u2009β€u2009_strength_u2009β€u20098000) β the strength of Ciel's current card. Output Output an integer: the maximal damage Jiro can get. Examples Input 2 3 ATK 2000 DEF 1700 2500 2500 2500 Input 3 4 ATK 10 ATK 100 ATK 1000 1 11 101 1001 Note In the first test case, Ciel has 3 cards with same _strength_. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500u2009-u20092000u2009=u2009500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500u2009+u20092500u2009=u20093000. In the second test case, she should use the "1001" card to attack the "ATK 100" card, then use the "101" card to attack the "ATK 10" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001u2009-u2009100)u2009+u2009(101u2009-u200910)u2009=u2009992. In the third test case note that she can destroy the "ATK 0" card by a card with strength equal to 0, but she can't destroy a "DEF 0" card with that card. | 1,900 | false | true | false | true | false | false | false | false | false | false | 8,550 |
631D | Each employee of the "Blake Techologies" company uses a special messaging app "Blake Messenger". All the stuff likes this app and uses it constantly. However, some important futures are missing. For example, many users want to be able to search through the message history. It was already announced that the new feature will appear in the nearest update, when developers faced some troubles that only you may help them to solve. All the messages are represented as a strings consisting of only lowercase English letters. In order to reduce the network load strings are represented in the special compressed form. Compression algorithm works as follows: string is represented as a concatenation of _n_ blocks, each block containing only equal characters. One block may be described as a pair (_l__i_,u2009_c__i_), where _l__i_ is the length of the _i_-th block and _c__i_ is the corresponding letter. Thus, the string _s_ may be written as the sequence of pairs . Your task is to write the program, that given two compressed string _t_ and _s_ finds all occurrences of _s_ in _t_. Developers know that there may be many such occurrences, so they only ask you to find the number of them. Note that _p_ is the starting position of some occurrence of _s_ in _t_ if and only if _t__p__t__p_u2009+u20091..._t__p_u2009+u2009_s_u2009-u20091u2009=u2009_s_, where _t__i_ is the _i_-th character of string _t_. Note that the way to represent the string in compressed form may not be unique. For example string "aaaa" may be given as , , ... Input The first line of the input contains two integers _n_ and _m_ (1u2009β€u2009_n_,u2009_m_u2009β€u2009200u2009000)xa0β the number of blocks in the strings _t_ and _s_, respectively. The second line contains the descriptions of _n_ parts of string _t_ in the format "_l__i_-_c__i_" (1u2009β€u2009_l__i_u2009β€u20091u2009000u2009000)xa0β the length of the _i_-th part and the corresponding lowercase English letter. The second line contains the descriptions of _m_ parts of string _s_ in the format "_l__i_-_c__i_" (1u2009β€u2009_l__i_u2009β€u20091u2009000u2009000)xa0β the length of the _i_-th part and the corresponding lowercase English letter. Note In the first sample, _t_ = "aaabbccccaaacc", and string _s_ = "aabbc". The only occurrence of string _s_ in string _t_ starts at position _p_u2009=u20092. In the second sample, _t_ = "aaabbbbbbaaaaaaacccceeeeeeeeaa", and _s_ = "aaa". The occurrences of _s_ in _t_ start at positions _p_u2009=u20091, _p_u2009=u200910, _p_u2009=u200911, _p_u2009=u200912, _p_u2009=u200913 and _p_u2009=u200914. | 2,100 | false | false | true | false | true | false | false | false | false | false | 7,283 |
71D | Vasya has a pack of 54 cards (52 standard cards and 2 distinct jokers). That is all he has at the moment. Not to die from boredom, Vasya plays Solitaire with them. Vasya lays out _nm_ cards as a rectangle _n_u2009Γu2009_m_. If there are jokers among them, then Vasya should change them with some of the rest of 54u2009-u2009_nm_ cards (which are not layed out) so that there were no jokers left. Vasya can pick the cards to replace the jokers arbitrarily. Remember, that each card presents in pack exactly once (i. e. in a single copy). Vasya tries to perform the replacements so that the solitaire was solved. Vasya thinks that the solitaire is solved if after the jokers are replaced, there exist two non-overlapping squares 3u2009Γu20093, inside each of which all the cards either have the same suit, or pairwise different ranks. Determine by the initial position whether the solitaire can be solved or not. If it can be solved, show the way in which it is possible. Input The first line contains integers _n_ and _m_ (3u2009β€u2009_n_,u2009_m_u2009β€u200917, _n_u2009Γu2009_m_u2009β€u200952). Next _n_ lines contain _m_ words each. Each word consists of two letters. The jokers are defined as "J1" and "J2" correspondingly. For the rest of the cards, the first letter stands for the rank and the second one β for the suit. The possible ranks are: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K" and "A". The possible suits are: "C", "D", "H" and "S". All the cards are different. Output If the Solitaire can be solved, print on the first line "Solution exists." without the quotes. On the second line print in what way the jokers can be replaced. Three variants are possible: "There are no jokers.", if there are no jokers in the input data. "Replace J_x_ with _y_.", if there is one joker. _x_ is its number, and _y_ is the card it should be replaced with. "Replace J1 with _x_ and J2 with _y_.", if both jokers are present in the input data. _x_ and _y_ here represent distinct cards with which one should replace the first and the second jokers correspondingly. On the third line print the coordinates of the upper left corner of the first square 3u2009Γu20093 in the format "Put the first square to (_r_, _c_).", where _r_ and _c_ are the row and the column correspondingly. In the same manner print on the fourth line the coordinates of the second square 3u2009Γu20093 in the format "Put the second square to (_r_, _c_).". If there are several solutions to that problem, print any of them. If there are no solutions, print of the single line "No solution." without the quotes. See the samples to understand the output format better. Examples Input 4 6 2S 3S 4S 7S 8S AS 5H 6H 7H 5S TC AC 8H 9H TH 7C 8C 9C 2D 2C 3C 4C 5C 6C Input 4 6 2S 3S 4S 7S 8S AS 5H 6H 7H J1 TC AC 8H 9H TH 7C 8C 9C 2D 2C 3C 4C 5C 6C Output Solution exists. Replace J1 with 2H. Put the first square to (1, 1). Put the second square to (2, 4). Input 4 6 2S 3S 4S 7S 8S AS 5H 6H 7H QC TC AC 8H 9H TH 7C 8C 9C 2D 2C 3C 4C 5C 6C Output Solution exists. There are no jokers. Put the first square to (1, 1). Put the second square to (2, 4). Note The pretests cover all the possible output formats. | 2,200 | false | false | true | false | false | false | true | false | false | false | 9,613 |
195E | An oriented weighted forest is an acyclic weighted digraph in which from each vertex at most one edge goes. The root of vertex _v_ of an oriented weighted forest is a vertex from which no edge goes and which can be reached from vertex _v_ moving along the edges of the weighted oriented forest. We denote the root of vertex _v_ as _root_(_v_). The depth of vertex _v_ is the sum of weights of paths passing from the vertex _v_ to its root. Let's denote the depth of the vertex _v_ as _depth_(_v_). Let's consider the process of constructing a weighted directed forest. Initially, the forest does not contain vertices. Vertices are added sequentially one by one. Overall, there are _n_ performed operations of adding. The _i_-th (_i_u2009>u20090) adding operation is described by a set of numbers (_k_,u2009u2009_v_1,u2009u2009_x_1,u2009u2009_v_2,u2009u2009_x_2,u2009u2009... ,u2009u2009_v__k_,u2009u2009_x__k_) and means that we should add vertex number _i_ and _k_ edges to the graph: an edge from vertex _root_(_v_1) to vertex _i_ with weight _depth_(_v_1)u2009+u2009_x_1, an edge from vertex _root_(_v_2) to vertex _i_ with weight _depth_(_v_2)u2009+u2009_x_2 and so on. If _k_u2009=u20090, then only vertex _i_ is added to the graph, there are no added edges. Your task is like this: given the operations of adding vertices, calculate the sum of the weights of all edges of the forest, resulting after the application of all defined operations, modulo 1000000007 (109u2009+u20097). Input The first line contains a single integer _n_ (1u2009β€u2009_n_u2009β€u2009105) β the number of operations of adding a vertex. Next _n_ lines contain descriptions of the operations, the _i_-th line contains the description of the operation of adding the _i_-th vertex in the following format: the first number of a line is an integer _k_ (0u2009β€u2009_k_u2009β€u2009_i_u2009-u20091), then follow 2_k_ space-separated integers: _v_1,u2009_x_1,u2009_v_2,u2009_x_2,u2009... ,u2009_v__k_,u2009_x__k_ (1u2009β€u2009_v__j_u2009β€u2009_i_u2009-u20091,u2009_x__j_u2009β€u2009109). The operations are given in the order, in which they should be applied to the graph. It is guaranteed that sum _k_ of all operations does not exceed 105, also that applying operations of adding vertexes does not result in loops and multiple edges. | 2,000 | false | false | false | false | true | false | false | false | false | true | 9,059 |
321C | Now Fox Ciel becomes a commander of Tree Land. Tree Land, like its name said, has _n_ cities connected by _n_u2009-u20091 undirected roads, and for any two cities there always exists a path between them. Fox Ciel needs to assign an officer to each city. Each officer has a rank β a letter from 'A' to 'Z'. So there will be 26 different ranks, and 'A' is the topmost, so 'Z' is the bottommost. There are enough officers of each rank. But there is a special rule must obey: if _x_ and _y_ are two distinct cities and their officers have the same rank, then on the simple path between _x_ and _y_ there must be a city _z_ that has an officer with higher rank. The rule guarantee that a communications between same rank officers will be monitored by higher rank officer. Help Ciel to make a valid plan, and if it's impossible, output "Impossible!". Input The first line contains an integer _n_ (2u2009β€u2009_n_u2009β€u2009105) β the number of cities in Tree Land. Each of the following _n_u2009-u20091 lines contains two integers _a_ and _b_ (1u2009β€u2009_a_,u2009_b_u2009β€u2009_n_,u2009_a_u2009β u2009_b_) β they mean that there will be an undirected road between _a_ and _b_. Consider all the cities are numbered from 1 to _n_. It guaranteed that the given graph will be a tree. Output If there is a valid plane, output _n_ space-separated characters in a line β _i_-th character is the rank of officer in the city with number _i_. Otherwise output "Impossible!". Examples Input 10 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 Output D C B A D C B D C D Note In the first example, for any two officers of rank 'B', an officer with rank 'A' will be on the path between them. So it is a valid solution. | 2,100 | false | true | false | false | false | true | false | false | false | false | 8,549 |
2029I | Kevin has recently learned the definition of variance. For an array $$$a$$$ of length $$$n$$$, the variance of $$$a$$$ is defined as follows: Let $$$x=dfrac{1}{n}displaystylesum_{i=1}^n a_i$$$, i.e., $$$x$$$ is the mean of the array $$$a$$$; Then, the variance of $$$a$$$ is $$$$$$ V(a)=frac{1}{n}sum_{i=1}^n(a_i-x)^2. $$$$$$ Now, Kevin gives you an array $$$a$$$ consisting of $$$n$$$ integers, as well as an integer $$$k$$$. You can perform the following operation on $$$a$$$: Select an interval $$$[l,r]$$$ ($$$1le lle rle n$$$), then for each $$$lle ile r$$$, increase $$$a_i$$$ by $$$k$$$. For each $$$1le ple m$$$, you have to find the minimum possible variance of $$$a$$$ after exactly $$$p$$$ operations are performed, independently for each $$$p$$$. For simplicity, you only need to output the answers multiplied by $$$n^2$$$. It can be proven that the results are always integers. Input Each test contains multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1le tle 100$$$)xa0β the number of test cases. The description of test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1le n,mle 5000$$$, $$$color{red}{ncdot mle 2cdot 10^4}$$$, $$$1le kle 10^5$$$)xa0β the length of the array $$$a$$$, the maximum number of operations, and the number you add to $$$a_i$$$ each time, respectively. The second line contains $$$n$$$ integers $$$a_1,a_2,ldots, a_n$$$ ($$$1le a_ile 10^5$$$)xa0β the elements of the array $$$a$$$. It is guaranteed that the sum of $$$ncdot m$$$ over all tests does not exceed $$$2cdot 10^4$$$. Output For each test case, output $$$m$$$ integers in a single line, the $$$p$$$-th integer denoting the minimum possible variance of $$$a$$$ when exactly $$$p$$$ operations are performed, multiplied by $$$n^2$$$. Example Input 9 3 2 1 1 2 2 3 2 2 1 2 2 10 2 1 10 1 1 1 1 10 1 1 1 1 6 8 2 1 1 4 5 1 3 8 8 7 20 43 24 2 4 3 20 43 8 8 3 20 43 24 2 4 3 20 43 10 12 1 5 3 3 5 4 1 8 1 1 1 13 10 100000 1 2 3 4 5 6 7 8 9 10 11 5 4 10 5 10000 2308 9982 4435 3310 100000 9 7 8100 1919 100000 Output 0 0 2 2 1161 1024 53 21 21 5 5 5 5 5 10608 6912 4448 3104 1991 1312 535 304 13248 11184 9375 7815 6447 5319 4383 3687 385 316 269 224 181 156 124 101 80 56 41 29 1486 1486 1486 1486 1486 1486 1486 1486 1486 1486 134618047140 119919447140 107020847140 93922247140 82623647140 Note In the first test case: For $$$p = 1$$$, you can perform the operation on $$$[1, 1]$$$, changing $$$a$$$ from $$$[1, 2, 2]$$$ to $$$[2, 2, 2]$$$. Since all of the elements are equal, the variance is equal to $$$0$$$. For $$$p = 2$$$, you can perform the operation on $$$[1, 3]$$$ and then $$$[1, 1]$$$, changing $$$a$$$ from $$$[1, 2, 2]$$$ to $$$[2, 3, 3]$$$ to $$$[3, 3, 3]$$$. Since all of the elements are equal, the variance is equal to $$$0$$$. In the second test case, some possible optimal choices are: $$$p=1$$$: $$$[underline{1,},2,2] o [3,2,2]$$$; $$$p=2$$$: $$$[1,underline{2,2}] o [underline{1,},4,4] o [3,4,4]$$$. In the third test case, some possible optimal choices are: $$$p=1$$$: $$$[10,underline{1,1,1,1,10,1,1,1,1}] o[10,2,2,2,2,11,2,2,2,2]$$$; $$$p=2$$$: $$$[10,1,1,1,1,10,underline{1,1,1,1}] o [10,underline{1,1,1,1},10,2,2,2,2] o [10,2,2,2,2,10,2,2,2,2]$$$. In the eighth test case, the optimal choice for all $$$p$$$ is to perform the operation on the whole array $$$p$$$ times. | 3,400 | false | true | false | false | false | false | false | false | false | true | 89 |
1536A | Omkar has received a message from Anton saying "Your story for problem A is confusing. Just make a formal statement." Because of this, Omkar gives you an array $$$a = [a_1, a_2, ldots, a_n]$$$ of $$$n$$$ distinct integers. An array $$$b = [b_1, b_2, ldots, b_k]$$$ is called nice if for any two distinct elements $$$b_i, b_j$$$ of $$$b$$$, $$$b_i-b_j$$$ appears in $$$b$$$ at least once. In addition, all elements in $$$b$$$ must be distinct. Can you add several (maybe, $$$0$$$) integers to $$$a$$$ to create a nice array $$$b$$$ of size at most $$$300$$$? If $$$a$$$ is already nice, you don't have to add any elements. For example, array $$$[3, 6, 9]$$$ is nice, as $$$6-3=9-6 = 3$$$, which appears in the array, and $$$9-3 = 6$$$, which appears in the array, while array $$$[4, 2, 0, 6, 9]$$$ is not nice, as $$$9-4 = 5$$$ is not present in the array. For integers $$$x$$$ and $$$y$$$, $$$x-y = x-y$$$ if $$$x > y$$$ and $$$x-y = y-x$$$ otherwise. Input Each test contains multiple test cases. The first line contains $$$t$$$ ($$$1 leq t leq 50$$$), the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 leq n leq 100$$$) β the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ distinct integers $$$a_1, a_2, cdots, a_n$$$ ($$$-100 leq a_i leq 100$$$) β the elements of the array $$$a$$$. Output For each test case, output one line containing YES if Omkar can create a nice array $$$b$$$ by adding elements to $$$a$$$ and NO otherwise. The case of each letter does not matter, so yEs and nO will also be accepted. If the first line is YES, output a second line containing a single integer $$$k$$$ ($$$n leq k leq 300$$$). Then output one line containing $$$k$$$ distinct integers $$$b_1, b_2, cdots, b_k$$$ ($$$-10^9 leq b_i leq 10^9$$$), the elements of the nice array $$$b$$$. $$$b_1, b_2, cdots, b_k$$$ can be in any order. For each $$$a_i$$$ in $$$a$$$, $$$a_i$$$ must appear at least once in $$$b$$$. It can be proved that if Omkar can create such an array $$$b$$$, then he can also do so in a way that satisfies the above constraints. If multiple solutions exist, you can print any. Example Input 4 3 3 0 9 2 3 4 5 -7 3 13 -2 8 4 4 8 12 6 Output yes 4 6 0 3 9 yEs 5 5 3 1 2 4 NO Yes 6 8 12 6 2 4 10 Note For the first case, you can add integers to $$$a$$$ to receive the array $$$b = [6, 0, 3, 9]$$$. Note that $$$6-3 = 9-6 = 3-0 = 3$$$ and $$$3$$$ is in $$$b$$$, $$$6-0 = 9-3 = 6$$$ and $$$6$$$ is in $$$b$$$, and $$$9-0 = 9$$$ is in $$$b$$$, so $$$b$$$ is nice. For the second case, you can add integers to $$$a$$$ to receive the array $$$b = [5, 3, 1, 2, 4]$$$. We have that $$$2-1 = 3-2 = 4-3 = 5-4 = 1$$$ is in $$$b$$$, $$$3-1 = 4-2 = 5-3 = 2$$$ is in $$$b$$$, $$$4-1 = 5-2 = 3$$$ is in $$$b$$$, and $$$5-1 = 4$$$ is in $$$b$$$, so $$$b$$$ is nice. For the fourth case, you can add integers to $$$a$$$ to receive the array $$$b = [8, 12, 6, 2, 4, 10]$$$. We have that $$$4-2 = 6-4 = 8-6 = 10-8 = 12-10 = 2$$$ is in $$$b$$$, $$$6-2 = 8-4 = 10-6 = 12-8 = 4$$$ is in $$$b$$$, $$$8-2 = 10-4 = 12-6 = 6$$$ is in $$$b$$$, $$$10-2 = 12-4 = 8$$$ is in $$$b$$$, and $$$12-2 = 10$$$ is in $$$b$$$, so $$$b$$$ is nice. It can be proven that for all other test cases it is impossible to create a nice array $$$b$$$. | 800 | false | false | false | false | false | true | true | false | false | false | 2,968 |
1358C | During the quarantine, Sicromoft has more free time to create the new functions in "Celex-2021". The developers made a new function GAZ-GIZ, which infinitely fills an infinite table to the right and down from the upper left corner as follows: The cell with coordinates $$$(x, y)$$$ is at the intersection of $$$x$$$-th row and $$$y$$$-th column. Upper left cell $$$(1,1)$$$ contains an integer $$$1$$$. The developers of the SUM function don't sleep either. Because of the boredom, they teamed up with the developers of the RAND function, so they added the ability to calculate the sum on an arbitrary path from one cell to another, moving down or right. Formally, from the cell $$$(x,y)$$$ in one step you can move to the cell $$$(x+1, y)$$$ or $$$(x, y+1)$$$. After another Dinwows update, Levian started to study "Celex-2021" (because he wants to be an accountant!). After filling in the table with the GAZ-GIZ function, he asked you to calculate the quantity of possible different amounts on the path from a given cell $$$(x_1, y_1)$$$ to another given cell $$$(x_2, y_2$$$), if you can only move one cell down or right. Formally, consider all the paths from the cell $$$(x_1, y_1)$$$ to cell $$$(x_2, y_2)$$$ such that each next cell in the path is located either to the down or to the right of the previous one. Calculate the number of different sums of elements for all such paths. Input The first line contains one integer $$$t$$$ ($$$1 le t le 57179$$$) β the number of test cases. Each of the following $$$t$$$ lines contains four natural numbers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$, $$$y_2$$$ ($$$1 le x_1 le x_2 le 10^9$$$, $$$1 le y_1 le y_2 le 10^9$$$) β coordinates of the start and the end cells. Output For each test case, in a separate line, print the number of possible different sums on the way from the start cell to the end cell. | 1,600 | true | false | false | false | false | false | false | false | false | false | 3,895 |
1359A | The game of Berland poker is played with a deck of $$$n$$$ cards, $$$m$$$ of which are jokers. $$$k$$$ players play this game ($$$n$$$ is divisible by $$$k$$$). At the beginning of the game, each player takes $$$frac{n}{k}$$$ cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to $$$x - y$$$, where $$$x$$$ is the number of jokers in the winner's hand, and $$$y$$$ is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get $$$0$$$ points. Here are some examples: $$$n = 8$$$, $$$m = 3$$$, $$$k = 2$$$. If one player gets $$$3$$$ jokers and $$$1$$$ plain card, and another player gets $$$0$$$ jokers and $$$4$$$ plain cards, then the first player is the winner and gets $$$3 - 0 = 3$$$ points; $$$n = 4$$$, $$$m = 2$$$, $$$k = 4$$$. Two players get plain cards, and the other two players get jokers, so both of them are winners and get $$$0$$$ points; $$$n = 9$$$, $$$m = 6$$$, $$$k = 3$$$. If the first player gets $$$3$$$ jokers, the second player gets $$$1$$$ joker and $$$2$$$ plain cards, and the third player gets $$$2$$$ jokers and $$$1$$$ plain card, then the first player is the winner, and he gets $$$3 - 2 = 1$$$ point; $$$n = 42$$$, $$$m = 0$$$, $$$k = 7$$$. Since there are no jokers, everyone gets $$$0$$$ jokers, everyone is a winner, and everyone gets $$$0$$$ points. Given $$$n$$$, $$$m$$$ and $$$k$$$, calculate the maximum number of points a player can get for winning the game. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 500$$$) β the number of test cases. Then the test cases follow. Each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 le n le 50$$$, $$$0 le m le n$$$, $$$2 le k le n$$$, $$$k$$$ is a divisors of $$$n$$$). Output For each test case, print one integer β the maximum number of points a player can get for winning the game. Example Input 4 8 3 2 4 2 4 9 6 3 42 0 7 Note Test cases of the example are described in the statement. | 1,000 | true | true | false | false | false | false | true | false | false | false | 3,891 |
42B | Once Volodya was at the museum and saw a regular chessboard as a museum piece. And there were only four chess pieces on it: two white rooks, a white king and a black king. "Aha, blacks certainly didn't win!", β Volodya said and was right for sure. And your task is to say whether whites had won or not. Pieces on the chessboard are guaranteed to represent a correct position (every piece occupies one cell, no two pieces occupy the same cell and kings cannot take each other). Thus, your task is only to decide whether whites mate blacks. We would remind you that it means that the black king can be taken by one of the opponent's pieces at the moment and also it cannot move to an unbeaten position. A rook moves vertically or horizontally by any number of free cells (assuming there are no other pieces on its path), a king β to the adjacent cells (either by corner or by side). Certainly, pieces cannot leave the board. The black king might be able to take opponent's rooks at his turn (see sample 3). Input The input contains 4 space-separated piece positions: positions of the two rooks, the white king and the black king. Each position on 8u2009Γu20098 chessboard is denoted by two symbols β ('a' - 'h') and ('1' - '8') β which stand for horizontal and vertical coordinates of the cell occupied by the piece. It is guaranteed, that no two pieces occupy the same cell, and kings cannot take each other. Output Output should contain one word: "CHECKMATE" if whites mate blacks, and "OTHER" otherwise. | 1,700 | false | false | true | false | false | false | false | false | false | false | 9,778 |
1153D | Now Serval is a junior high school student in Japari Middle School, and he is still thrilled on math as before. As a talented boy in mathematics, he likes to play with numbers. This time, he wants to play with numbers on a rooted tree. A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a node $$$v$$$ is the last different from $$$v$$$ vertex on the path from the root to the vertex $$$v$$$. Children of vertex $$$v$$$ are all nodes for which $$$v$$$ is the parent. A vertex is a leaf if it has no children. The rooted tree Serval owns has $$$n$$$ nodes, node $$$1$$$ is the root. Serval will write some numbers into all nodes of the tree. However, there are some restrictions. Each of the nodes except leaves has an operation $$$max$$$ or $$$min$$$ written in it, indicating that the number in this node should be equal to the maximum or minimum of all the numbers in its sons, respectively. Assume that there are $$$k$$$ leaves in the tree. Serval wants to put integers $$$1, 2, ldots, k$$$ to the $$$k$$$ leaves (each number should be used exactly once). He loves large numbers, so he wants to maximize the number in the root. As his best friend, can you help him? Input The first line contains an integer $$$n$$$ ($$$2 leq n leq 3cdot 10^5$$$), the size of the tree. The second line contains $$$n$$$ integers, the $$$i$$$-th of them represents the operation in the node $$$i$$$. $$$0$$$ represents $$$min$$$ and $$$1$$$ represents $$$max$$$. If the node is a leaf, there is still a number of $$$0$$$ or $$$1$$$, but you can ignore it. The third line contains $$$n-1$$$ integers $$$f_2, f_3, ldots, f_n$$$ ($$$1 leq f_i leq i-1$$$), where $$$f_i$$$ represents the parent of the node $$$i$$$. Output Output one integerxa0β the maximum possible number in the root of the tree. Examples Input 6 1 0 1 1 0 1 1 2 2 2 2 Input 8 1 0 0 1 0 1 1 0 1 1 2 2 3 3 3 Input 9 1 1 0 0 1 0 1 0 1 1 1 2 2 3 3 4 4 Note Pictures below explain the examples. The numbers written in the middle of the nodes are their indices, and the numbers written on the top are the numbers written in the nodes. In the first example, no matter how you arrange the numbers, the answer is $$$1$$$. In the second example, no matter how you arrange the numbers, the answer is $$$4$$$. In the third example, one of the best solution to achieve $$$4$$$ is to arrange $$$4$$$ and $$$5$$$ to nodes $$$4$$$ and $$$5$$$. In the fourth example, the best solution is to arrange $$$5$$$ to node $$$5$$$. | 1,900 | false | true | false | true | false | false | false | true | false | false | 4,946 |
1692E | Slavic has an array of length $$$n$$$ consisting only of zeroes and ones. In one operation, he removes either the first or the last element of the array. What is the minimum number of operations Slavic has to perform such that the total sum of the array is equal to $$$s$$$ after performing all the operations? In case the sum $$$s$$$ can't be obtained after any amount of operations, you should output -1. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0β the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 leq n, s leq 2 cdot 10^5$$$)xa0β the length of the array and the needed sum of elements. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$0 leq a_i leq 1$$$)xa0β the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integerxa0β the minimum amount of operations required to have the total sum of the array equal to $$$s$$$, or -1 if obtaining an array with sum $$$s$$$ isn't possible. Example Input 7 3 1 1 0 0 3 1 1 1 0 9 3 0 1 0 1 1 1 0 0 1 6 4 1 1 1 1 1 1 5 1 0 0 1 1 0 16 2 1 1 0 0 1 0 0 1 1 0 0 0 0 0 1 1 6 3 1 0 1 0 0 0 Note In the first test case, the sum of the whole array is $$$1$$$ from the beginning, so we don't have to make any operations. In the second test case, the sum of the array is $$$2$$$ and we want it to be equal to $$$1$$$, so we should remove the first element. The array turns into $$$[1, 0]$$$, which has a sum equal to $$$1$$$. In the third test case, the sum of the array is $$$5$$$ and we need it to be $$$3$$$. We can obtain such a sum by removing the first two elements and the last element, doing a total of three operations. The array turns into $$$[0, 1, 1, 1, 0, 0]$$$, which has a sum equal to $$$3$$$. | 1,200 | false | false | true | false | false | false | false | true | false | false | 2,126 |
662A | As you know, the game of "Nim" is played with _n_ piles of stones, where the _i_-th pile initially contains _a__i_ stones. Two players alternate the turns. During a turn a player picks any non-empty pile and removes any positive number of stones from it. The one who is not able to make a move loses the game. Petya and Vasya are tired of playing Nim, so they invented their own version of the game and named it the "Gambling Nim". They have _n_ two-sided cards, one side of the _i_-th card has number _a__i_ written on it, while the other side has number _b__i_. At the beginning of the game the players put all the cards on the table, each card only one of its sides up, and this side is chosen independently and uniformly. Thus they obtain a sequence _c_1,u2009_c_2,u2009...,u2009_c__n_, where _c__i_ is equal to _a__i_ or _b__i_. Then they take _n_ piles of stones, with _i_-th pile containing exactly _c__i_ stones and play Nim. Petya takes the first turn. Given that both players play optimally, find the probability of Petya's victory. Output the answer as an irreducible fraction. Input The first line of the input contains a single integer _n_ (1u2009β€u2009_n_u2009β€u2009500u2009000)xa0β the number of cards in the deck. Each of the following _n_ lines contains the description of one card, consisting of two integers _a__i_ and _b__i_ (0u2009β€u2009_a__i_,u2009_b__i_u2009β€u20091018). Output Output the answer as an irreducible fraction _p_u2009/u2009_q_. If the probability of Petya's victory is 0, print 0/1. | 2,400 | true | false | false | false | false | false | false | false | false | false | 7,168 |
1593D2 | This problem is a complicated version of D1, but it has significant differences, so read the whole statement. Polycarp has an array of $$$n$$$ ($$$n$$$ is even) integers $$$a_1, a_2, dots, a_n$$$. Polycarp conceived of a positive integer $$$k$$$. After that, Polycarp began performing the following operations on the array: take an index $$$i$$$ ($$$1 le i le n$$$) and reduce the number $$$a_i$$$ by $$$k$$$. After Polycarp performed some (possibly zero) number of such operations, it turned out that at least half of the numbers in the array became the same. Find the maximum $$$k$$$ at which such a situation is possible, or print $$$-1$$$ if such a number can be arbitrarily large. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10$$$) β the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of two lines. The first line contains an even integer $$$n$$$ ($$$4 le n le 40$$$) ($$$n$$$ is even). The second line contains $$$n$$$ integers $$$a_1, a_2, dots a_n$$$ ($$$-10^6 le a_i le 10^6$$$). It is guaranteed that the sum of all $$$n$$$ specified in the given test cases does not exceed $$$100$$$. Output For each test case output on a separate line an integer $$$k$$$ ($$$k ge 1$$$) β the maximum possible number that Polycarp used in operations on the array, or $$$-1$$$, if such a number can be arbitrarily large. Example Input 4 6 48 13 22 -15 16 35 8 -1 0 1 -1 0 1 -1 0 4 100 -1000 -1000 -1000 4 1 1 1 1 | 1,900 | true | false | false | false | false | false | true | false | false | false | 2,682 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.