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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1762F | You are given an array $$$a$$$ consisting of $$$n$$$ integers and an integer $$$k$$$. A pair $$$(l,r)$$$ is good if there exists a sequence of indices $$$i_1, i_2, dots, i_m$$$ such that $$$i_1=l$$$ and $$$i_m=r$$$; $$$i_j < i_{j+1}$$$ for all $$$1 leq j < m$$$; and $$$a_{i_j}-a_{i_{j+1}} leq k$$$ for all $$$1 leq j < m$$$. Find the number of pairs $$$(l,r)$$$ ($$$1 leq l leq r leq n$$$) that are good. Input Each test contains 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 the test cases follows. The first line of each test case contains two space-separated integers $$$n$$$ and $$$k$$$ ($$$1 leq n leq 5 cdot 10^5$$$; $$$0 leq k leq 10^5$$$) — the length of the array $$$a$$$ and the integer $$$k$$$. The second line of each test case contains $$$n$$$ space-separated integers $$$a_1,a_2,ldots,a_n$$$ ($$$1 leq a_i leq 10^5$$$) — representing the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 cdot 10^5$$$. Note In the first test case, good pairs are $$$(1,1)$$$, $$$(1,2)$$$, $$$(1,3)$$$, $$$(2,2)$$$, $$$(2,3)$$$, and $$$(3,3)$$$. In the second test case, good pairs are $$$(1,1)$$$, $$$(1,3)$$$, $$$(1,4)$$$, $$$(2,2)$$$, $$$(2,3)$$$, $$$(2,4)$$$, $$$(3,3)$$$, $$$(3,4)$$$ and $$$(4,4)$$$. Pair $$$(1,4)$$$ is good because there exists a sequence of indices $$$1, 3, 4$$$ which satisfy the given conditions. | 2,600 | false | false | false | true | true | false | false | true | false | false | 1,735 |
166A | Another programming contest is over. You got hold of the contest's final results table. The table has the following data. For each team we are shown two numbers: the number of problems and the total penalty time. However, for no team we are shown its final place. You know the rules of comparing the results of two given teams very well. Let's say that team _a_ solved _p__a_ problems with total penalty time _t__a_ and team _b_ solved _p__b_ problems with total penalty time _t__b_. Team _a_ gets a higher place than team _b_ in the end, if it either solved more problems on the contest, or solved the same number of problems but in less total time. In other words, team _a_ gets a higher place than team _b_ in the final results' table if either _p__a_u2009>u2009_p__b_, or _p__a_u2009=u2009_p__b_ and _t__a_u2009<u2009_t__b_. It is considered that the teams that solve the same number of problems with the same penalty time share all corresponding places. More formally, let's say there is a group of _x_ teams that solved the same number of problems with the same penalty time. Let's also say that _y_ teams performed better than the teams from this group. In this case all teams from the group share places _y_u2009+u20091, _y_u2009+u20092, ..., _y_u2009+u2009_x_. The teams that performed worse than the teams from this group, get their places in the results table starting from the _y_u2009+u2009_x_u2009+u20091-th place. Your task is to count what number of teams from the given list shared the _k_-th place. Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u200950). Then _n_ lines contain the description of the teams: the _i_-th line contains two integers _p__i_ and _t__i_ (1u2009≤u2009_p__i_,u2009_t__i_u2009≤u200950) — the number of solved problems and the total penalty time of the _i_-th team, correspondingly. All numbers in the lines are separated by spaces. Output In the only line print the sought number of teams that got the _k_-th place in the final results' table. Examples Input 7 2 4 10 4 10 4 10 3 20 2 1 2 1 1 10 Note The final results' table for the first sample is: 1-3 places — 4 solved problems, the penalty time equals 10 4 place — 3 solved problems, the penalty time equals 20 5-6 places — 2 solved problems, the penalty time equals 1 7 place — 1 solved problem, the penalty time equals 10 The table shows that the second place is shared by the teams that solved 4 problems with penalty time 10. There are 3 such teams. The final table for the second sample is: 1 place — 5 solved problems, the penalty time equals 3 2-5 places — 3 solved problems, the penalty time equals 1 The table shows that the fourth place is shared by the teams that solved 3 problems with penalty time 1. There are 4 such teams. | 1,100 | false | false | true | false | false | false | false | true | true | false | 9,200 |
1263D | One unknown hacker wants to get the admin's password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator's office and stole a piece of paper with a list of $$$n$$$ passwords — strings, consists of small Latin letters. Hacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords $$$a$$$ and $$$b$$$ as follows: two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a letter, that exists in both $$$a$$$ and $$$b$$$; two passwords $$$a$$$ and $$$b$$$ are equivalent if there is a password $$$c$$$ from the list, which is equivalent to both $$$a$$$ and $$$b$$$. If a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system. For example, if the list contain passwords "a", "b", "ab", "d", then passwords "a", "b", "ab" are equivalent to each other, but the password "d" is not equivalent to any other password from list. In other words, if: admin's password is "b", then you can access to system by using any of this passwords: "a", "b", "ab"; admin's password is "d", then you can access to system by using only "d". Only one password from the list is the admin's password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system. Input The first line contain integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — number of passwords in the list. Next $$$n$$$ lines contains passwords from the list – non-empty strings $$$s_i$$$, with length at most $$$50$$$ letters. Some of the passwords may be equal. It is guaranteed that the total length of all passwords does not exceed $$$10^6$$$ letters. All of them consist only of lowercase Latin letters. | 1,500 | false | false | false | false | false | false | false | false | false | true | 4,384 |
1476C | You have $$$n$$$ chains, the $$$i$$$-th chain consists of $$$c_i$$$ vertices. Vertices in each chain are numbered independently from $$$1$$$ to $$$c_i$$$ along the chain. In other words, the $$$i$$$-th chain is the undirected graph with $$$c_i$$$ vertices and $$$(c_i - 1)$$$ edges connecting the $$$j$$$-th and the $$$(j + 1)$$$-th vertices for each $$$1 le j < c_i$$$. Now you decided to unite chains in one graph in the following way: 1. the first chain is skipped; 2. the $$$1$$$-st vertex of the $$$i$$$-th chain is connected by an edge with the $$$a_i$$$-th vertex of the $$$(i - 1)$$$-th chain; 3. the last ($$$c_i$$$-th) vertex of the $$$i$$$-th chain is connected by an edge with the $$$b_i$$$-th vertex of the $$$(i - 1)$$$-th chain. Picture of the first test case. Dotted lines are the edges added during uniting process Calculate the length of the longest simple cycle in the resulting graph. A simple cycle is a chain where the first and last vertices are connected as well. If you travel along the simple cycle, each vertex of this cycle will be visited exactly once. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$2 le n le 10^5$$$)xa0— the number of chains you have. The second line of each test case contains $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$ ($$$2 le c_i le 10^9$$$)xa0— the number of vertices in the corresponding chains. The third line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$a_1 = -1$$$; $$$1 le a_i le c_{i - 1}$$$). The fourth line of each test case contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$b_1 = -1$$$; $$$1 le b_i le c_{i - 1}$$$). Both $$$a_1$$$ and $$$b_1$$$ are equal to $$$-1$$$, they aren't used in graph building and given just for index consistency. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$. Output For each test case, print the length of the longest simple cycle. Example Input 3 4 3 4 3 3 -1 1 2 2 -1 2 2 3 2 5 6 -1 5 -1 1 3 3 5 2 -1 1 1 -1 3 5 Note In the first test case, the longest simple cycle is shown below: We can't increase it with the first chain, since in such case it won't be simplexa0— the vertex $$$2$$$ on the second chain will break simplicity. | 1,600 | false | true | false | true | false | false | false | false | false | true | 3,295 |
700D | Alice wants to send an important message to Bob. Message _a_u2009=u2009(_a_1,u2009...,u2009_a__n_) is a sequence of positive integers (characters). To compress the message Alice wants to use binary Huffman coding. We recall that binary Huffman code, or binary prefix code is a function _f_, that maps each letter that appears in the string to some binary string (that is, string consisting of characters '0' and '1' only) such that for each pair of different characters _a__i_ and _a__j_ string _f_(_a__i_) is not a prefix of _f_(_a__j_) (and vice versa). The result of the encoding of the message _a_1,u2009_a_2,u2009...,u2009_a__n_ is the concatenation of the encoding of each character, that is the string _f_(_a_1)_f_(_a_2)... _f_(_a__n_). Huffman codes are very useful, as the compressed message can be easily and uniquely decompressed, if the function _f_ is given. Code is usually chosen in order to minimize the total length of the compressed message, i.e. the length of the string _f_(_a_1)_f_(_a_2)... _f_(_a__n_). Because of security issues Alice doesn't want to send the whole message. Instead, she picks some substrings of the message and wants to send them separately. For each of the given substrings _a__l__i_... _a__r__i_ she wants to know the minimum possible length of the Huffman coding. Help her solve this problem. Input The first line of the input contains the single integer _n_ (1u2009≤u2009_n_u2009≤u2009100u2009000)xa0— the length of the initial message. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009100u2009000)xa0— characters of the message. Next line contains the single integer _q_ (1u2009≤u2009_q_u2009≤u2009100u2009000)xa0— the number of queries. Then follow _q_ lines with queries descriptions. The _i_-th of these lines contains two integers _l__i_ and _r__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_)xa0— the position of the left and right ends of the _i_-th substring respectively. Positions are numbered from 1. Substrings may overlap in any way. The same substring may appear in the input more than once. Output Print _q_ lines. Each line should contain a single integerxa0— the minimum possible length of the Huffman encoding of the substring _a__l__i_... _a__r__i_. Example Input 7 1 2 1 3 1 2 1 5 1 7 1 3 3 5 2 4 4 4 Note In the first query, one of the optimal ways to encode the substring is to map 1 to "0", 2 to "10" and 3 to "11". Note that it is correct to map the letter to the empty substring (as in the fifth query from the sample). | 3,100 | false | true | false | false | true | false | false | false | false | false | 7,022 |
1772B | You have a matrix $$$2 imes 2$$$ filled with distinct integers. You want your matrix to become beautiful. The matrix is beautiful if the following two conditions are satisfied: in each row, the first element is smaller than the second element; in each column, the first element is smaller than the second element. You can perform the following operation on the matrix any number of times: rotate it clockwise by $$$90$$$ degrees, so the top left element shifts to the top right cell, the top right element shifts to the bottom right cell, and so on: Determine if it is possible to make the matrix beautiful by applying zero or more operations. Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. Each test case consists of two lines. Each of those lines contains two integers — the elements of the corresponding row of the matrix. In each matrix, all four elements are distinct integers from $$$1$$$ to $$$100$$$. Output For each test case, print YES if the matrix can become beautiful, or NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). Example Input 6 1 3 5 7 8 10 3 4 8 10 4 3 6 1 9 2 7 5 4 2 1 2 4 3 Output YES YES NO YES YES NO | 800 | false | false | true | false | false | false | true | false | false | false | 1,662 |
404B | Valera takes part in the Berland Marathon. The marathon race starts at the stadium that can be represented on the plane as a square whose lower left corner is located at point with coordinates (0,u20090) and the length of the side equals _a_ meters. The sides of the square are parallel to coordinate axes. As the length of the marathon race is very long, Valera needs to have extra drink during the race. The coach gives Valera a bottle of drink each _d_ meters of the path. We know that Valera starts at the point with coordinates (0,u20090) and runs counter-clockwise. That is, when Valera covers _a_ meters, he reaches the point with coordinates (_a_,u20090). We also know that the length of the marathon race equals _nd_u2009+u20090.5 meters. Help Valera's coach determine where he should be located to help Valera. Specifically, determine the coordinates of Valera's positions when he covers _d_,u20092·_d_,u2009...,u2009_n_·_d_ meters. Input The first line contains two space-separated real numbers _a_ and _d_ (1u2009≤u2009_a_,u2009_d_u2009≤u2009105), given with precision till 4 decimal digits after the decimal point. Number _a_ denotes the length of the square's side that describes the stadium. Number _d_ shows that after each _d_ meters Valera gets an extra drink. The second line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105) showing that Valera needs an extra drink _n_ times. Output Print _n_ lines, each line should contain two real numbers _x__i_ and _y__i_, separated by a space. Numbers _x__i_ and _y__i_ in the _i_-th line mean that Valera is at point with coordinates (_x__i_,u2009_y__i_) after he covers _i_·_d_ meters. Your solution will be considered correct if the absolute or relative error doesn't exceed 10u2009-u20094. Note, that this problem have huge amount of output data. Please, do not use cout stream for output in this problem. Examples Output 1.0000000000 2.0000000000 2.0000000000 0.0000000000 Output 2.8819000000 0.0000000000 4.1470000000 1.6168000000 3.7953000000 4.1470000000 0.9134000000 4.1470000000 0.0000000000 2.1785000000 0.7034000000 0.0000000000 | 1,500 | true | false | true | false | false | false | false | false | false | false | 8,209 |
578E | There is a sand trail in front of Alice's home. In daytime, people walk over it and leave a footprint on the trail for their every single step. Alice cannot distinguish the order of the footprints, but she can tell whether each footprint is made by left foot or right foot. Also she's certain that all people are walking by alternating left foot and right foot. For example, suppose that one person walked through the trail and left some footprints. The footprints are RRLRL in order along the trail ('R' means right foot and 'L' means left foot). You might think the outcome of the footprints is strange. But in fact, some steps are resulting from walking backwards! There are some possible order of steps that produce these footprints such as 1u2009→u20093u2009→u20092u2009→u20095u2009→u20094 or 2u2009→u20093u2009→u20094u2009→u20095u2009→u20091 (we suppose that the distance between two consecutive steps can be arbitrarily long). The number of backward steps from above two examples are 2 and 1 separately. Alice is interested in these footprints. Whenever there is a person walking trough the trail, she takes a picture of all these footprints along the trail and erase all of them so that next person will leave a new set of footprints. We know that people walk by alternating right foot and left foot, but we don't know if the first step is made by left foot or right foot. Alice wants to know the minimum possible number of backward steps made by a person. But it's a little hard. Please help Alice to calculate it. You also need to construct one possible history of these footprints. Input Only one line containing the string _S_ (1u2009≤u2009_S_u2009≤u2009100u2009000) containing all footprints in order along the trail from entrance to exit. It is guaranteed that there is at least one possible footprint history. Output You should output 2 lines. The first line should contain a number denoting the minimum number of backward steps. The second line should contain a permutation of integers from 1 to _S_. This permutation should denote the order of footprints that may possible be used by person walked there. If there are several possible answers, you may output any of them. Note For the first sample, one possible order is 2u2009→u20095u2009→u20091u2009→u20093u2009→u20094, among them only the step 5u2009→u20091 is backward step so the answer is 1. For the second example one possible order is just to follow the order of input, thus there are no backward steps. For the third sample, there will be 4 backward steps because every step from L to R will be a backward step. | 2,700 | false | true | false | false | false | true | false | false | false | false | 7,532 |
1699C | You are given a permutation $$$a_1,a_2,ldots,a_n$$$ of integers from $$$0$$$ to $$$n - 1$$$. Your task is to find how many permutations $$$b_1,b_2,ldots,b_n$$$ are similar to permutation $$$a$$$. Two permutations $$$a$$$ and $$$b$$$ of size $$$n$$$ are considered similar if for all intervals $$$[l,r]$$$ ($$$1 le l le r le n$$$), the following condition is satisfied: $$$$$$operatorname{MEX}([a_l,a_{l+1},ldots,a_r])=operatorname{MEX}([b_l,b_{l+1},ldots,b_r]),$$$$$$ where the $$$operatorname{MEX}$$$ of a collection of integers $$$c_1,c_2,ldots,c_k$$$ is defined as the smallest non-negative integer $$$x$$$ which does not occur in collection $$$c$$$. For example, $$$operatorname{MEX}([1,2,3,4,5])=0$$$, and $$$operatorname{MEX}([0,1,2,4,5])=3$$$. Since the total number of such permutations can be very large, you will have to print its remainder modulo $$$10^9+7$$$. In this problem, a permutation of size $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$0$$$ to $$$n-1$$$ in arbitrary order. For example, $$$[1,0,2,4,3]$$$ is a permutation, while $$$[0,1,1]$$$ is not, since $$$1$$$ appears twice in the array. $$$[0,1,3]$$$ is also not a permutation, since $$$n=3$$$ and there is a $$$3$$$ in the array. Input Each test contains multiple test cases. The first line of input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The following lines contain the descriptions of the test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the size of permutation $$$a$$$. The second line of each test case contains $$$n$$$ distinct integers $$$a_1,a_2,ldots,a_n$$$ ($$$0 le a_i lt n$$$)xa0— the elements of permutation $$$a$$$. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$10^5$$$. Note For the first test case, the only permutations similar to $$$a=[4,0,3,2,1]$$$ are $$$[4,0,3,2,1]$$$ and $$$[4,0,2,3,1]$$$. For the second and third test cases, the given permutations are only similar to themselves. For the fourth test case, there are $$$4$$$ permutations similar to $$$a=[1,2,4,0,5,3]$$$: $$$[1,2,4,0,5,3]$$$; $$$[1,2,5,0,4,3]$$$; $$$[1,4,2,0,5,3]$$$; $$$[1,5,2,0,4,3]$$$. | 1,700 | true | false | false | false | false | true | false | false | false | false | 2,085 |
1715F | This is an interactive problem. Farmer Stanley grows corn on a rectangular field of size $$$ n imes m $$$ meters with corners in points $$$(0, 0)$$$, $$$(0, m)$$$, $$$(n, 0)$$$, $$$(n, m)$$$. This year the harvest was plentiful and corn covered the whole field. The night before harvest aliens arrived and poisoned the corn in a single $$$1 imes 1$$$ square with sides parallel to field borders. The corn inside the square must not be eaten, but you cannot distinguish it from ordinary corn by sight. Stanley can only collect a sample of corn from an arbitrary polygon and bring it to the laboratory, where it will be analyzed and Stanley will be told the amount of corn in the sample that was poisoned. Since the harvest will soon deteriorate, such a study can be carried out no more than $$$5$$$ times. More formally, it is allowed to make no more than $$$5$$$ queries, each of them calculates the area of intersection of a chosen polygon with a square of poisoned corn. It is necessary to find out the coordinates of the lower-left corner of the drawn square (the vertex of the square with the smallest $$$x$$$ and $$$y$$$ coordinates). Input First line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 100$$$)xa0— field sizes. Interaction In order to query the area of intersection of a polygon with $$$k$$$ ($$$3 le k le 1000$$$) vertices at points with coordinates $$$(x_1, y_1),; dots ,;(x_k, y_k)$$$ with a square of poisoned corn print $$$k+1$$$ lines. In the first of these lines print "? k". In the $$$i$$$-th of the next $$$k$$$ lines print two real numbers $$$x_i$$$ and $$$y_i$$$ ($$$x_i, y_i le 10^4$$$) with at most $$$15$$$ digits after decimal place. The polygon must have strictly positive area and contain no self-intersections. In response to this query you will receive a real number $$$s$$$ ($$$0 le s le 1$$$) with $$$15$$$ digits after decimal placexa0— the area of intersection of the square with the given polygon. If the polygon is invalid, there is no guarantee on the valid response. When you have identified the drawn square, print on a separate line "! x y", where $$$x$$$ and $$$y$$$ are real numbers with at most $$$15$$$ digits after decimal place representing the coordinates of its lower-left corner ($$$0 le x le n - 1$$$, $$$0 le y le m - 1$$$), and then you have to terminate your program. Your answer will be considered correct if its absolute or relative error on both coordinates does not exceed $$$10^{-6}$$$. Formally let your answer be $$$a$$$, jury answer be $$$b$$$. Your answer will be considered correct if $$$frac{a-b}{max(1,b)} le 10^{-6}$$$. 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. Hacks To make a hack, use the following test format. The first line of the input should contain two integers $$$n$$$ and $$$m$$$ ($$$1 le n,m le 100$$$)xa0— field sizes. The second line should contain two real numbers $$$x$$$ ($$$0 le x le n - 1$$$) and $$$y$$$ ($$$0 le y le m - 1$$$)xa0— coordinates of the lower-left corner of the square of poisoned corn. Example Output ? 4 0 0 2 0 2 3 0 3 ? 4 0 0 0 1 3 1 3 0 ! 1.5 0.5 Note In the first test from the statement, the aliens poisoned a square of corn with vertices at points with coordinates $$$(1.5, 0.5)$$$, $$$(1.5, 1.5)$$$, $$$(2.5, 1.5)$$$, $$$(2.5, 0.5)$$$. In the picture, it is red, the polygon selected in the query is blue, and their intersection is green. Picture for the first query: Picture for the second query: | 2,700 | true | false | false | false | false | true | false | false | false | false | 1,988 |
1973B | Today, Cat and Fox found an array $$$a$$$ consisting of $$$n$$$ non-negative integers. Define the loneliness of $$$a$$$ as the smallest positive integer $$$k$$$ ($$$1 le k le n$$$) such that for any two positive integers $$$i$$$ and $$$j$$$ ($$$1 leq i, j leq n - k +1$$$), the following holds: $$$$$$a_i a_{i+1} ldots a_{i+k-1} = a_j a_{j+1} ldots a_{j+k-1},$$$$$$ where $$$x y$$$ denotes the xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$1 leq n leq 10^5$$$)xa0— the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i < 2^{20}$$$)xa0— the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$. Output For each test case, print one integer xa0— the loneliness of the given array. Example Input 7 1 0 3 2 2 2 3 1 0 2 5 3 0 1 4 2 5 2 0 4 0 2 7 0 0 0 0 1 2 4 8 0 1 3 2 2 1 0 3 Note In the first example, the loneliness of an array with a single element is always $$$1$$$, so the answer is $$$1$$$. In the second example, the OR of each subarray of length $$$k = 1$$$ is $$$2$$$, so the loneliness of the whole array is $$$1$$$. In the seventh example, it's true that $$$(0 1 3) = (1 3 2) = (3 2 2) = (2 2 1) = (2 1 0) = (1 0 3) = 3$$$, so the condition is satisfied for $$$k = 3$$$. We can verify that the condition is not true for any smaller $$$k$$$, so the answer is indeed $$$3$$$. | 1,300 | true | true | false | false | true | false | false | true | false | false | 454 |
414D | Mashmokh is playing a new game. In the beginning he has _k_ liters of water and _p_ coins. Additionally he has a rooted tree (an undirected connected acyclic graph) that consists of _m_ vertices. Each vertex of the tree contains a water tank that is empty in the beginning. The game begins with the fact that Mashmokh chooses some (no more than _k_) of these tanks (except the root) and pours into each of them exactly 1 liter of water. Then the following process is performed until there is no water remained in tanks. The process consists of several steps. At the beginning of each step Mashmokh opens doors of all tanks. Then Mashmokh closes doors of some tanks (he is not allowed to close door of tank in the root) for the duration of this move. Let's denote the number of liters in some tank with closed door as _w_, Mashmokh pays _w_ coins for the closing of that tank during this move. Let's denote by _x_1,u2009_x_2,u2009...,u2009_x__m_ as the list of vertices of the tree sorted (nondecreasing) by their depth. The vertices from this list should be considered one by one in the order. Firstly vertex _x_1 (which is the root itself) is emptied. Then for each vertex _x__i_ (_i_u2009>u20091), if its door is closed then skip the vertex else move all the water from the tank of vertex _x__i_ to the tank of its father (even if the tank of the father is closed). Suppose _l_ moves were made until the tree became empty. Let's denote the amount of water inside the tank of the root after the _i_-th move by _w__i_ then Mashmokh will win _max_(_w_1,u2009_w_2,u2009...,u2009_w__l_) dollars. Mashmokh wanted to know what is the maximum amount of dollars he can win by playing the above game. He asked you to find this value for him. Input The first line of the input contains three space-separated integers _m_,u2009_k_,u2009_p_xa0(2u2009≤u2009_m_u2009≤u2009105;xa00u2009≤u2009_k_,u2009_p_u2009≤u2009109). Each of the following _m_u2009-u20091 lines contains two space-separated integers _a__i_,u2009_b__i_xa0(1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_m_;xa0_a__i_u2009≠u2009_b__i_) — the edges of the tree. Consider that the vertices of the tree are numbered from 1 to _m_. The root of the tree has number 1. Output Output a single integer, the number Mashmokh asked you to find. Examples Input 10 2 1 1 2 1 3 3 4 3 5 2 6 6 8 6 7 9 8 8 10 Input 5 1000 1000 1 2 1 3 3 4 3 5 Note The tree in the first sample is shown on the picture below. The black, red, blue colors correspond to vertices with 0, 1, 2 liters of water. One way to achieve the maximum amount of money is to put 1 liter of water in each of vertices 3 and 4. The beginning state is shown on the picture below. Then in the first move Mashmokh will pay one token to close the door of the third vertex tank. The tree after the first move is shown on the picture below. After the second move there are 2 liters of water in the root as shown on the picture below. | 2,300 | false | true | false | false | true | false | false | true | false | false | 8,166 |
1425H | Chaneka has a hobby of playing with animal toys. Every toy has a different fun value, a real number. Chaneka has four boxes to store the toys with specification: The first box stores toys with fun values in range of $$$(-infty,-1]$$$. The second box stores toys with fun values in range of $$$(-1, 0)$$$. The third box stores toys with fun values in range of $$$(0, 1)$$$. The fourth box stores toys with fun value in range of $$$[1, infty)$$$. Chaneka has $$$A$$$, $$$B$$$, $$$C$$$, $$$D$$$ toys in the first, second, third, and fourth box, respectively. One day she decides that she only wants one toy, a super toy. So she begins to create this super toy by sewing all the toys she has. While the number of toys Chaneka has is more than 1, she takes two different toys randomly and then sews them together, creating a new toy. The fun value of this new toy is equal to the multiplication of fun values of the sewn toys. She then puts this new toy in the appropriate box. She repeats this process until she only has one toy. This last toy is the super toy, and the box that stores this toy is the special box. As an observer, you only know the number of toys in each box initially but do not know their fun values. You also don't see the sequence of Chaneka's sewing. Determine which boxes can be the special box after Chaneka found her super toy. Input The first line has an integer $$$T$$$ $$$(1 le T le 5 cdot 10^4)$$$, the number of test cases. Every case contains a line with four space-separated integers $$$A$$$ $$$B$$$ $$$C$$$ $$$D$$$ $$$(0 le A, B, C, D le 10^6, A + B + C + D > 0)$$$, which denotes the number of toys in the first, second, third, and fourth box, respectively. Output For each case, print four space-separated strings. Each string represents the possibility that the first, second, third, and fourth box can be the special box from left to right. For each box, print "Ya" (Without quotes, Indonesian for yes) if that box can be the special box. Print "Tidak" (Without quotes, Indonesian for No) otherwise. Example Output Ya Ya Tidak Tidak Tidak Ya Tidak Tidak Note For the first case, here is a scenario where the first box is the special box: The first box had toys with fun values $$${-3}$$$. The second box had toys with fun values $$${ -0.5, -0.5 }$$$ The fourth box had toys with fun values $$${ 3 }$$$ The sewing sequence: 1. Chaneka sews the toy with fun $$$-0.5$$$ and $$$-0.5$$$ to a toy with fun $$$0.25$$$ and then put it in the third box. 2. Chaneka sews the toy with fun $$$-3$$$ and $$$0.25$$$ to a toy with fun $$$-0.75$$$ and then put it in the second box. 3. Chaneka sews the toy with fun $$$-0.75$$$ and $$$3$$$ to a toy with fun $$$-1.25$$$ and then put it in the first box, which then became the special box. Here is a scenario where the second box ends up being the special box: The first box had toys with fun values $$${-3}$$$ The second box had toys with fun values $$${ -0.33, -0.25 }$$$. The fourth box had toys with fun values $$${ 3 }$$$. The sewing sequence: 1. Chaneka sews the toy with fun $$$-3$$$ and $$$-0.33$$$ to a toy with fun $$$0.99$$$ and then put it in the third box. 2. Chaneka sews the toy with fun $$$0.99$$$ and $$$3$$$ to a toy with fun $$$2.97$$$ and then put in it the fourth box. 3. Chaneka sews the toy with fun $$$2.97$$$ and $$$-0.25$$$ to a toy with fun $$$-0.7425$$$ and then put it in the second box, which then became the special box. There is only one toy for the second case, so Chaneka does not have to sew anything because that toy, by definition, is the super toy. | 1,300 | false | false | false | false | false | true | false | false | false | false | 3,541 |
371D | There is a system of _n_ vessels arranged one above the other as shown in the figure below. Assume that the vessels are numbered from 1 to _n_, in the order from the highest to the lowest, the volume of the _i_-th vessel is _a__i_ liters. Initially, all the vessels are empty. In some vessels water is poured. All the water that overflows from the _i_-th vessel goes to the (_i_u2009+u20091)-th one. The liquid that overflows from the _n_-th vessel spills on the floor. Your task is to simulate pouring water into the vessels. To do this, you will need to handle two types of queries: 1. Add _x__i_ liters of water to the _p__i_-th vessel; 2. Print the number of liters of water in the _k__i_-th vessel. When you reply to the second request you can assume that all the water poured up to this point, has already overflown between the vessels. Input The first line contains integer _n_ — the number of vessels (1u2009≤u2009_n_u2009≤u20092·105). The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ — the vessels' capacities (1u2009≤u2009_a__i_u2009≤u2009109). The vessels' capacities do not necessarily increase from the top vessels to the bottom ones (see the second sample). The third line contains integer _m_ — the number of queries (1u2009≤u2009_m_u2009≤u20092·105). Each of the next _m_ lines contains the description of one query. The query of the first type is represented as "1xa0_p__i_xa0_x__i_", the query of the second type is represented as "2xa0_k__i_" (1u2009≤u2009_p__i_u2009≤u2009_n_, 1u2009≤u2009_x__i_u2009≤u2009109, 1u2009≤u2009_k__i_u2009≤u2009_n_). Output For each query, print on a single line the number of liters of water in the corresponding vessel. Examples Input 2 5 10 6 1 1 4 2 1 1 2 5 1 1 4 2 1 2 2 Input 3 5 10 8 6 1 1 12 2 2 1 1 6 1 3 2 2 2 2 3 | 1,800 | false | false | true | false | true | false | false | false | false | false | 8,345 |
360C | Levko loves strings of length _n_, consisting of lowercase English letters, very much. He has one such string _s_. For each string _t_ of length _n_, Levko defines its beauty relative to _s_ as the number of pairs of indexes _i_, _j_ (1u2009≤u2009_i_u2009≤u2009_j_u2009≤u2009_n_), such that substring _t_[_i_.._j_] is lexicographically larger than substring _s_[_i_.._j_]. The boy wondered how many strings _t_ are there, such that their beauty relative to _s_ equals exactly _k_. Help him, find the remainder after division this number by 1000000007 (109u2009+u20097). A substring _s_[_i_.._j_] of string _s_u2009=u2009_s_1_s_2... _s__n_ is string _s__i__s__i_u2009u2009+u2009u20091... _s__j_. String _x_u2009u2009=u2009u2009_x_1_x_2... _x__p_ is lexicographically larger than string _y_u2009u2009=u2009u2009_y_1_y_2... _y__p_, if there is such number _r_ (_r_u2009<u2009_p_), that _x_1u2009u2009=u2009u2009_y_1,u2009u2009_x_2u2009u2009=u2009u2009_y_2,u2009u2009... ,u2009u2009_x__r_u2009u2009=u2009u2009_y__r_ and _x__r_u2009u2009+u2009u20091u2009>u2009_y__r_u2009u2009+u2009u20091. The string characters are compared by their ASCII codes. Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u20092000, 0u2009≤u2009_k_u2009≤u20092000). The second line contains a non-empty string _s_ of length _n_. String _s_ consists only of lowercase English letters. Output Print a single number — the answer to the problem modulo 1000000007 (109u2009+u20097). | 2,500 | false | false | false | true | false | false | false | false | false | false | 8,392 |
1637G | Vitaly gave Maxim $$$n$$$ numbers $$$1, 2, ldots, n$$$ for his $$$16$$$-th birthday. Maxim was tired of playing board games during the celebration, so he decided to play with these numbers. In one step Maxim can choose two numbers $$$x$$$ and $$$y$$$ from the numbers he has, throw them away, and add two numbers $$$x + y$$$ and $$$x - y$$$ instead. He wants all his numbers to be equal after several steps and the sum of the numbers to be minimal. Help Maxim to find a solution. Maxim's friends don't want to wait long, so the number of steps in the solution should not exceed $$$20n$$$. It is guaranteed that under the given constraints, if a solution exists, then there exists a solution that makes all numbers equal, minimizes their sum, and spends no more than $$$20n$$$ moves. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 25,000$$$)xa0— the number of test cases. Each test case contains a single integer $$$n$$$ ($$$2 le n le 5 cdot 10^4$$$)xa0— the number of integers given to Maxim. It is guaranteed that the total sum of $$$n$$$ doesn't exceed $$$5 cdot 10^4$$$. Output For each test case print $$$-1$$$ if it's impossible to make all numbers equal. Otherwise print a single integer $$$s$$$ ($$$0 le s le 20n$$$)xa0— the number of steps. Then print $$$s$$$ lines. The $$$i$$$-th line must contain two integers $$$x_i$$$ and $$$y_i$$$xa0— numbers that Maxim chooses on the $$$i$$$-th step. The numbers must become equal after all operations. Don't forget that you not only need to make all numbers equal, but also minimize their sum. It is guaranteed that under the given constraints, if a solution exists, then there exists a solution that makes all numbers equal, minimizes their sum, and spends no more than $$$20n$$$ moves. | 3,000 | true | true | false | false | false | true | false | false | false | false | 2,428 |
1195D1 | This problem differs from the next one only in the presence of the constraint on the equal length of all numbers $$$a_1, a_2, dots, a_n$$$. Actually, this problem is a subtask of the problem D2 from the same contest and the solution of D2 solves this subtask too. A team of SIS students is going to make a trip on a submarine. Their target is an ancient treasure in a sunken ship lying on the bottom of the Great Rybinsk sea. Unfortunately, the students don't know the coordinates of the ship, so they asked Meshanya (who is a hereditary mage) to help them. He agreed to help them, but only if they solve his problem. Let's denote a function that alternates digits of two numbers $$$f(a_1 a_2 dots a_{p - 1} a_p, b_1 b_2 dots b_{q - 1} b_q)$$$, where $$$a_1 dots a_p$$$ and $$$b_1 dots b_q$$$ are digits of two integers written in the decimal notation without leading zeros. In other words, the function $$$f(x, y)$$$ alternately shuffles the digits of the numbers $$$x$$$ and $$$y$$$ by writing them from the lowest digits to the older ones, starting with the number $$$y$$$. The result of the function is also built from right to left (that is, from the lower digits to the older ones). If the digits of one of the arguments have ended, then the remaining digits of the other argument are written out. Familiarize with examples and formal definitions of the function below. For example: $$$$$$f(1111, 2222) = 12121212$$$$$$ $$$$$$f(7777, 888) = 7787878$$$$$$ $$$$$$f(33, 44444) = 4443434$$$$$$ $$$$$$f(555, 6) = 5556$$$$$$ $$$$$$f(111, 2222) = 2121212$$$$$$ Formally, if $$$p ge q$$$ then $$$f(a_1 dots a_p, b_1 dots b_q) = a_1 a_2 dots a_{p - q + 1} b_1 a_{p - q + 2} b_2 dots a_{p - 1} b_{q - 1} a_p b_q$$$; if $$$p < q$$$ then $$$f(a_1 dots a_p, b_1 dots b_q) = b_1 b_2 dots b_{q - p} a_1 b_{q - p + 1} a_2 dots a_{p - 1} b_{q - 1} a_p b_q$$$. Mishanya gives you an array consisting of $$$n$$$ integers $$$a_i$$$. All numbers in this array are of equal length (that is, they consist of the same number of digits). Your task is to help students to calculate $$$sum_{i = 1}^{n}sum_{j = 1}^{n} f(a_i, a_j)$$$ modulo $$$998,244,353$$$. Input The first line of the input contains a single integer $$$n$$$ ($$$1 le n le 100,000$$$) — the number of elements in the array. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$) — the elements of the array. All numbers $$$a_1, a_2, dots, a_n$$$ are of equal length (that is, they consist of the same number of digits). | 1,500 | true | false | false | false | false | false | false | false | false | false | 4,726 |
140F | As Gerald ..., in other words, on a New Year Eve Constantine prepared an unusual present for the Beautiful Lady. The present is the magic New Year snowflake that can make any dream come true. The New Year snowflake consists of tiny ice crystals, which can be approximately regarded as points on the plane. The beauty of the New Year snowflake is that it has a center of symmetry. This is a point such that for each crystal of the snowflake exists another crystal, symmetrical to it relative to that point. One of the crystals can be placed directly in the center of symmetry. While Constantine was choosing a snowflake among millions of other snowflakes, no less symmetrical and no less magical, then endured a difficult path through the drifts to the house of his mistress, while he was waiting with bated breath for a few long moments before the Beautiful Lady opens the door, some of the snowflake crystals melted and naturally disappeared. Constantine is sure that there were no more than _k_ of such crystals, because he handled the snowflake very carefully. Now he is ready to demonstrate to the Beautiful Lady all the power of nanotechnology and restore the symmetry of snowflakes. You are given the coordinates of the surviving snowflake crystals, given in nanometers. Your task is to identify all possible positions of the original center of symmetry. Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009200u2009000, 0u2009≤u2009_k_u2009≤u200910) — the number of the surviving snowflake crystals and the maximum number of melted crystals, correspondingly. Next _n_ lines contain the coordinates of the crystals that are left in the following form: "_x__i_ _y__i_". The coordinates are integers and do not exceed 5·108 in absolute value. All given points are different. Output The first line contains an integer _c_ — the number of possible symmetry centers. Next _c_ lines should contain the centers' descriptions. Each symmetry center is described by a couple of coordinates "_x_ _y_", separated by a space. Print the coordinates with absolute error not exceeding 10u2009-u20096. You are allowed to print the symmetry centers in any order. All printed points should be different. If there exist an infinite number of possible symmetry centers, print the single number "-1". Examples Output 5 0.0 0.5 0.5 0.0 0.5 0.5 0.5 1.0 1.0 0.5 | 2,600 | false | false | false | false | false | false | false | false | true | false | 9,313 |
995F | Allen, having graduated from the MOO Institute of Techcowlogy (MIT), has started a startup! Allen is the president of his startup. He also hires $$$n-1$$$ other employees, each of which is assigned a direct superior. If $$$u$$$ is a superior of $$$v$$$ and $$$v$$$ is a superior of $$$w$$$ then also $$$u$$$ is a superior of $$$w$$$. Additionally, there are no $$$u$$$ and $$$v$$$ such that $$$u$$$ is the superior of $$$v$$$ and $$$v$$$ is the superior of $$$u$$$. Allen himself has no superior. Allen is employee number $$$1$$$, and the others are employee numbers $$$2$$$ through $$$n$$$. Finally, Allen must assign salaries to each employee in the company including himself. Due to budget constraints, each employee's salary is an integer between $$$1$$$ and $$$D$$$. Additionally, no employee can make strictly more than his superior. Help Allen find the number of ways to assign salaries. As this number may be large, output it modulo $$$10^9 + 7$$$. Input The first line of the input contains two integers $$$n$$$ and $$$D$$$ ($$$1 le n le 3000$$$, $$$1 le D le 10^9$$$). The remaining $$$n-1$$$ lines each contain a single positive integer, where the $$$i$$$-th line contains the integer $$$p_i$$$ ($$$1 le p_i le i$$$). $$$p_i$$$ denotes the direct superior of employee $$$i+1$$$. Note In the first sample case, employee 2 and 3 report directly to Allen. The three salaries, in order, can be $$$(1,1,1)$$$, $$$(2,1,1)$$$, $$$(2,1,2)$$$, $$$(2,2,1)$$$ or $$$(2,2,2)$$$. In the second sample case, employee 2 reports to Allen and employee 3 reports to employee 2. In order, the possible salaries are $$$(1,1,1)$$$, $$$(2,1,1)$$$, $$$(2,2,1)$$$, $$$(2,2,2)$$$, $$$(3,1,1)$$$, $$$(3,2,1)$$$, $$$(3,2,2)$$$, $$$(3,3,1)$$$, $$$(3,3,2)$$$, $$$(3,3,3)$$$. | 2,700 | true | false | false | true | false | false | false | false | false | false | 5,721 |
1983B | You are given two grids of numbers $$$a$$$ and $$$b$$$, with $$$n$$$ rows and $$$m$$$ columns. All the values in the grid are $$$0$$$, $$$1$$$ or $$$2$$$. You can perform the following operation on $$$a$$$ any number of times: Pick any subrectangle in the grid with length and width $$$ge 2$$$. You are allowed to choose the entire grid as a subrectangle. The subrectangle has four corners. Take any pair of diagonally opposite corners of the chosen subrectangle and add $$$1$$$ to their values modulo $$$3$$$. For the pair of corners not picked, add $$$2$$$ to their values modulo $$$3$$$. Note that the operation only changes the values of the corners of the picked subrectangle. Is it possible to convert the grid $$$a$$$ into grid $$$b$$$ by applying the above operation any number of times (possibly zero)? Input The first line contains an integer $$$t$$$, the number of testcases ($$$1 le t le 250$$$). For each testcase: The first line contains two integers $$$n$$$ and $$$m$$$, the number of rows and columns in the grid ($$$2 le n,m le 500$$$). Each of the next n lines contain m characters — the $$$j$$$-th character of the $$$i$$$-th line represents $$$a_{i,j}$$$. Each of the next n lines contain m characters — the $$$j$$$-th character of the $$$i$$$-th line represents $$$b_{i,j}$$$ ($$$0 le a_{i,j}, b_{i,j} le 2$$$). It is guaranteed that the sum of $$$n$$$ over all test cases and the sum of $$$m$$$ over all test cases do not exceed $$$500$$$. Output For each test case print "YES" (without quotes) if it is possible to convert grid $$$a$$$ into grid $$$b$$$ and "NO" (without quotes) otherwise. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Example Input 7 3 3 000 000 000 111 111 111 4 4 0000 0000 0000 0000 2100 1200 0012 0021 4 4 1020 1200 1210 0000 0000 1200 2200 0000 3 3 012 012 012 010 111 011 8 8 00000000 00000000 00000000 00000000 00000000 00000000 00000000 10000000 00000000 01200000 02010000 00102000 00020100 00001020 00000210 10000000 2 7 0000000 0000000 2220111 0111222 2 7 0000000 0100010 2220111 1210202 Output YES YES YES NO YES NO YES Note In the first testcase, grid $$$a$$$ can be converted into $$$b$$$ in the following manner: $$$begin{matrix}fbox{0} & 0 & fbox{0} 0 & 0 & 0 fbox{0} & 0 & fbox{0}end{matrix} Rightarrow begin{matrix}1 & 0 & 2 0 & fbox{0} & fbox{0} 2 & fbox{0} & fbox{1}end{matrix} Rightarrow begin{matrix}1 & 0 & 2 fbox{0} & fbox{1} & 2 fbox{2} & fbox{2} & 2end{matrix} Rightarrow begin{matrix}1 & fbox{0} & fbox{2} 1 & 0 & 2 1 & fbox{0} & fbox{2}end{matrix} Rightarrow begin{matrix}1 & 1 & 1 1 & fbox{0} & fbox{2} 1 & fbox{2} & fbox{0}end{matrix} Rightarrow begin{matrix}1 & 1 & 1 1 & 1 & 1 1 & 1 & 1end{matrix}$$$ Here, in each operation, the top-right and bottom-left corners highlighted by a box are incremented by $$$2$$$ modulo $$$3$$$, while the top-left and bottom-right corners are incremented by $$$1$$$ modulo $$$3$$$. In the fourth testcase, it can be proven that it is not possible to convert grid $$$a$$$ into grid $$$b$$$ using the above-mentioned operations any number of times. | 1,200 | true | true | true | false | false | true | false | false | false | false | 389 |
576B | A tree of size _n_ is an undirected connected graph consisting of _n_ vertices without cycles. Consider some tree with _n_ vertices. We call a tree invariant relative to permutation _p_u2009=u2009_p_1_p_2... _p__n_, if for any two vertices of the tree _u_ and _v_ the condition holds: "vertices _u_ and _v_ are connected by an edge if and only if vertices _p__u_ and _p__v_ are connected by an edge". You are given permutation _p_ of size _n_. Find some tree size _n_, invariant relative to the given permutation. Input The first line contains number _n_ (1u2009≤u2009_n_u2009≤u2009105) — the size of the permutation (also equal to the size of the sought tree). The second line contains permutation _p__i_ (1u2009≤u2009_p__i_u2009≤u2009_n_). Output If the sought tree does not exist, print "NO" (without the quotes). Otherwise, print "YES", and then print _n_u2009-u20091 lines, each of which contains two integers — the numbers of vertices connected by an edge of the tree you found. The vertices are numbered from 1, the order of the edges and the order of the vertices within the edges does not matter. If there are multiple solutions, output any of them. Note In the first sample test a permutation transforms edge (4, 1) into edge (1, 4), edge (4, 2) into edge (1, 3) and edge (1, 3) into edge (4, 2). These edges all appear in the resulting tree. It can be shown that in the second sample test no tree satisfies the given condition. | 2,100 | false | true | false | false | false | true | false | false | false | false | 7,542 |
1578L | In a dream, Lucy found herself in a labyrinth. This labyrinth consists of $$$n$$$ rooms, connected by $$$m$$$ passages ($$$i$$$-th passage is $$$w_i$$$ cm wide). Each passage can be traversed in both directions. It is guaranteed that it is possible to get from any room to any other room. But this is not an ordinary labyrinthxa0— each room in this labyrinth contains a magic candy. When Lucy eats this magic candy, she is getting wider. Specifically, if she eats candy from room $$$i$$$ she becomes wider by $$$c_i$$$ cm. Note that she is not obliged to eat candy the first time she visits a particular room, but she can eat each candy only once. Unfortunately, passages in this labyrinth are pretty narrow, so after eating some candy, Lucy can get too wide and will not be able to traverse themxa0— her width should not be greater than the width of the corresponding passage. Lucy starts her journey in a room number $$$1$$$. She wants to eat all the candies. After that, she will just wake up, so she does not have to be able to return to the room $$$1$$$. She realizes that with her current width, she may not be able to do so, so she plans a workout before embarking on her journey. Lucy wants to know if it is possible to start with some positive width and still eat all the candies. If yes, then what is the maximal starting width with which it is possible. Input The first line contains two integers, $$$n$$$ and $$$m$$$ ($$$2 le n le 10^5; n - 1 le m le 10^5$$$)xa0— the number of rooms and the number of passages. The second line contains $$$n$$$ integersxa0— $$$c_i$$$ ($$$1 le c_i le 10^9$$$). Next $$$m$$$ lines contain three integers eachxa0— $$$a_i$$$, $$$b_i$$$ and $$$w_i$$$ ($$$1 le a_i, b_i le n; a_i e b_i; 1 le w_i le 10^9$$$) describing passage that connects rooms $$$a_i$$$ and $$$b_i$$$ and is $$$w_i$$$ cm wide. It is guaranteed that the resulting labyrinth is connected and there is at most one passage between any pair of rooms. Output If it is possible to eat all the candies, output the maximal possible starting width, otherwise output $$$-1$$$. Examples Input 3 3 1 2 3 1 2 4 1 3 4 2 3 6 | 2,400 | false | true | false | false | false | false | false | true | false | false | 2,743 |
231A | One day three best friends Petya, Vasya and Tonya decided to form a team and take part in programming contests. Participants are usually offered several problems during programming contests. Long before the start the friends decided that they will implement a problem if at least two of them are sure about the solution. Otherwise, the friends won't write the problem's solution. This contest offers _n_ problems to the participants. For each problem we know, which friend is sure about the solution. Help the friends find the number of problems for which they will write a solution. Input The first input line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20091000) — the number of problems in the contest. Then _n_ lines contain three integers each, each integer is either 0 or 1. If the first number in the line equals 1, then Petya is sure about the problem's solution, otherwise he isn't sure. The second number shows Vasya's view on the solution, the third number shows Tonya's view. The numbers on the lines are separated by spaces. Output Print a single integer — the number of problems the friends will implement on the contest. Note In the first sample Petya and Vasya are sure that they know how to solve the first problem and all three of them know how to solve the second problem. That means that they will write solutions for these problems. Only Petya is sure about the solution for the third problem, but that isn't enough, so the friends won't take it. In the second sample the friends will only implement the second problem, as Vasya and Tonya are sure about the solution. | 800 | false | true | false | false | false | false | true | false | false | false | 8,916 |
155B | Ilya plays a card game by the following rules. A player has several cards. Each card contains two non-negative integers inscribed, one at the top of the card and one at the bottom. At the beginning of the round the player chooses one of his cards to play it. If the top of the card contains number _a__i_, and the bottom contains number _b__i_, then when the player is playing the card, he gets _a__i_ points and also gets the opportunity to play additional _b__i_ cards. After the playing the card is discarded. More formally: let's say that there is a counter of the cards that can be played. At the beginning of the round the counter equals one. When a card is played, the counter decreases by one for the played card and increases by the number _b__i_, which is written at the bottom of the card. Then the played card is discarded. If after that the counter is not equal to zero, the player gets the opportunity to play another card from the remaining cards. The round ends when the counter reaches zero or the player runs out of cards. Of course, Ilya wants to get as many points as possible. Can you determine the maximum number of points he can score provided that you know his cards? Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20091000) — the number of cards Ilya has. Each of the next _n_ lines contains two non-negative space-separated integers — _a__i_ and _b__i_ (0u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009104) — the numbers, written at the top and the bottom of the _i_-th card correspondingly. Output Print the single number — the maximum number of points you can score in one round by the described rules. Note In the first sample none of two cards brings extra moves, so you should play the one that will bring more points. In the second sample you should first play the third card that doesn't bring any points but lets you play both remaining cards. | 1,100 | false | true | false | false | false | false | false | false | true | false | 9,253 |
1529B | A sequence $$$(b_1, b_2, ldots, b_k)$$$ is called strange, if the absolute difference between any pair of its elements is greater than or equal to the maximum element in the sequence. Formally speaking, it's strange if for every pair $$$(i, j)$$$ with $$$1 le i<j le k$$$, we have $$$a_i-a_jgeq MAX$$$, where $$$MAX$$$ is the largest element of the sequence. In particular, any sequence of length at most $$$1$$$ is strange. For example, the sequences $$$(-2021, -1, -1, -1)$$$ and $$$(-1, 0, 1)$$$ are strange, but $$$(3, 0, 1)$$$ is not, because $$$0 - 1 < 3$$$. Sifid has an array $$$a$$$ of $$$n$$$ integers. Sifid likes everything big, so among all the strange subsequences of $$$a$$$, he wants to find the length of the longest one. Can you help him? A sequence $$$c$$$ is a subsequence of an array $$$d$$$ if $$$c$$$ can be obtained from $$$d$$$ by deletion of several (possibly, zero or all) elements. Input The first line contains an integer $$$t$$$ $$$(1le tle 10^4)$$$ — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1le nle 10^5)$$$ — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ $$$(-10^9le a_i le 10^9)$$$ — the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$. Output For each test case output a single integer — the length of the longest strange subsequence of $$$a$$$. Example Input 6 4 -1 -2 0 0 7 -3 4 -2 0 -4 6 1 5 0 5 -3 2 -5 3 2 3 1 4 -3 0 2 0 6 -3 -2 -1 1 1 1 Note In the first test case, one of the longest strange subsequences is $$$(a_1, a_2, a_3, a_4)$$$ In the second test case, one of the longest strange subsequences is $$$(a_1, a_3, a_4, a_5, a_7)$$$. In the third test case, one of the longest strange subsequences is $$$(a_1, a_3, a_4, a_5)$$$. In the fourth test case, one of the longest strange subsequences is $$$(a_2)$$$. In the fifth test case, one of the longest strange subsequences is $$$(a_1, a_2, a_4)$$$. | 1,100 | true | true | false | false | false | false | false | false | true | false | 3,016 |
1428A | Wabbit is trying to move a box containing food for the rest of the zoo in the coordinate plane from the point $$$(x_1,y_1)$$$ to the point $$$(x_2,y_2)$$$. He has a rope, which he can use to pull the box. He can only pull the box if he stands exactly $$$1$$$ unit away from the box in the direction of one of two coordinate axes. He will pull the box to where he is standing before moving out of the way in the same direction by $$$1$$$ unit. For example, if the box is at the point $$$(1,2)$$$ and Wabbit is standing at the point $$$(2,2)$$$, he can pull the box right by $$$1$$$ unit, with the box ending up at the point $$$(2,2)$$$ and Wabbit ending at the point $$$(3,2)$$$. Also, Wabbit can move $$$1$$$ unit to the right, left, up, or down without pulling the box. In this case, it is not necessary for him to be in exactly $$$1$$$ unit away from the box. If he wants to pull the box again, he must return to a point next to the box. Also, Wabbit can't move to the point where the box is located. Wabbit can start at any point. It takes $$$1$$$ second to travel $$$1$$$ unit right, left, up, or down, regardless of whether he pulls the box while moving. Determine the minimum amount of time he needs to move the box from $$$(x_1,y_1)$$$ to $$$(x_2,y_2)$$$. Note that the point where Wabbit ends up at does not matter. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 leq t leq 1000)$$$: the number of test cases. The description of the test cases follows. Each of the next $$$t$$$ lines contains four space-separated integers $$$x_1, y_1, x_2, y_2$$$ $$$(1 leq x_1, y_1, x_2, y_2 leq 10^9)$$$, describing the next test case. Output For each test case, print a single integer: the minimum time in seconds Wabbit needs to bring the box from $$$(x_1,y_1)$$$ to $$$(x_2,y_2)$$$. Note In the first test case, the starting and the ending points of the box are $$$(1,2)$$$ and $$$(2,2)$$$ respectively. This is the same as the picture in the statement. Wabbit needs only $$$1$$$ second to move as shown in the picture in the statement. In the second test case, Wabbit can start at the point $$$(2,1)$$$. He pulls the box to $$$(2,1)$$$ while moving to $$$(3,1)$$$. He then moves to $$$(3,2)$$$ and then to $$$(2,2)$$$ without pulling the box. Then, he pulls the box to $$$(2,2)$$$ while moving to $$$(2,3)$$$. It takes $$$4$$$ seconds. | 800 | true | false | false | false | false | false | false | false | false | false | 3,525 |
1733E | There is a conveyor with $$$120$$$ rows and $$$120$$$ columns. Each row and column is numbered from $$$0$$$ to $$$119$$$, and the cell in $$$i$$$-th row and $$$j$$$-th column is denoted as $$$(i, j)$$$. The top leftmost cell is $$$(0, 0)$$$. Each cell has a belt, and all belts are initially facing to the right. Initially, a slime ball is on the belt of $$$(0, 0)$$$, and other belts are empty. Every second, the state of the conveyor changes as follows: All slime balls on the conveyor move one cell in the direction of the belt at the same time. If there is no cell in the moved position, the slime gets out of the conveyor, and if two slime balls move to the same cell, they merge into one. All belts with slime ball in the previous second change direction at the same time: belts facing to the right become facing to the down, and vice versa. A new slime ball is placed on cell $$$(0, 0)$$$. There are $$$q$$$ queries, each being three integers $$$t$$$, $$$x$$$, and $$$y$$$. You have to find out if there is a slime at the cell $$$(x, y)$$$ after $$$t$$$ seconds from the start. Can you do it? Input The first line contains one integer $$$q$$$ ($$$1 le q le 10^4$$$) — the number of queries. The only line of each query contains three integers $$$t$$$, $$$x$$$, and $$$y$$$ ($$$0 le t le 10^{18}$$$, $$$0 le x, y < 120$$$). Output Print the answer for each test case, one per line. If there is a slime ball in the cell $$$(x, y)$$$ after $$$t$$$ seconds from the initial state, print "YES". Otherwise, print "NO". Example Input 6 1 1 0 5 1 3 0 0 0 2 4 5 2 0 2 1547748756 100 111 Output NO YES YES NO YES YES Note The state of conveyor with $$$t = 0$$$. Red arrow represents the direction of each belt, and blue figure represents slime. The state of conveyor with $$$t = 1$$$. The state of conveyor with $$$t = 2$$$. | 2,700 | true | false | false | true | false | true | false | false | false | false | 1,887 |
718D | During the chemistry lesson Andrew learned that the saturated hydrocarbons (alkanes) enter into radical chlorination reaction. Andrew is a very curious boy, so he wondered how many different products of the reaction may be forms for a given alkane. He managed to solve the task for small molecules, but for large ones he faced some difficulties and asks you to help. Formally, you are given a tree consisting of _n_ vertices, such that the degree of each vertex doesn't exceed 4. You have to count the number of distinct non-isomorphic trees that can be obtained by adding to this tree one new vertex and one new edge, such that the graph is still the tree and the degree of each vertex doesn't exceed 4. Two trees are isomorphic if there exists a bijection _f_(_v_) such that vertices _u_ and _v_ are connected by an edge if and only if vertices _f_(_v_) and _f_(_u_) are connected by an edge. Input The first line of the input contains an integer _n_ (1u2009≤u2009_n_u2009≤u2009100u2009000)xa0— the number of vertices in the tree. Then follow _n_u2009-u20091 lines with edges descriptions. Each edge is given by two integers _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_)xa0— indices of vertices connected by an edge. It's guaranteed that the given graph is a tree and the degree of each vertex doesn't exceed 4. Output Print one integerxa0— the answer to the question. Note In the first sample, one can add new vertex to any existing vertex, but the trees we obtain by adding a new vertex to vertices 1, 3 and 4 are isomorphic, thus the answer is 2. In the second sample, one can't add new vertex to the first vertex, as its degree is already equal to four. Trees, obtained by adding a new vertex to vertices 2, 3, 4 and 5 are isomorphic, thus the answer is 1. | 2,900 | false | false | false | true | false | false | false | false | false | false | 6,941 |
1393E2 | This is a harder version of the problem E with larger constraints. Twilight Sparkle has received a new task from Princess Celestia. This time she asked to decipher the ancient scroll containing important knowledge of pony origin. To hide the crucial information from evil eyes, pony elders cast a spell on the scroll. That spell adds exactly one letter in any place to each word it is cast on. To make the path to the knowledge more tangled elders chose some of words in the scroll and cast a spell on them. Twilight Sparkle knows that the elders admired the order in all things so the scroll original scroll contained words in lexicographically non-decreasing order. She is asked to delete one letter from some of the words of the scroll (to undo the spell) to get some version of the original scroll. Unfortunately, there may be more than one way to recover the ancient scroll. To not let the important knowledge slip by Twilight has to look through all variants of the original scroll and find the required one. To estimate the maximum time Twilight may spend on the work she needs to know the number of variants she has to look through. She asks you to find that number! Since that number can be very big, Twilight asks you to find it modulo $$$10^9+7$$$. It may occur that princess Celestia has sent a wrong scroll so the answer may not exist. 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 $$$n$$$ ($$$1 le n le 10^5$$$): the number of words in the scroll. The $$$i$$$-th of the next $$$n$$$ lines contains a string consisting of lowercase English letters: the $$$i$$$-th word in the scroll. The length of each word is at least one. The sum of lengths of words does not exceed $$$10^6$$$. Output Print one integer: the number of ways to get a version of the original from the scroll modulo $$$10^9+7$$$. Examples Input 6 lapochka kartyshka bigbabytape morgenshtern ssshhhiiittt queen Note Notice that the elders could have written an empty word (but they surely cast a spell on it so it holds a length $$$1$$$ now). | 3,200 | false | false | true | true | false | false | false | false | false | false | 3,702 |
461D | Toastman came up with a very complicated task. He gives it to Appleman, but Appleman doesn't know how to solve it. Can you help him? Given a _n_u2009×u2009_n_ checkerboard. Each cell of the board has either character 'x', or character 'o', or nothing. How many ways to fill all the empty cells with 'x' or 'o' (each cell must contain only one character in the end) are there, such that for each cell the number of adjacent cells with 'o' will be even? Find the number of ways modulo 1000000007 (109u2009+u20097). Two cells of the board are adjacent if they share a side. Input The first line contains two integers _n_, _k_ (1u2009≤u2009_n_,u2009_k_u2009≤u2009105) — the size of the board, and the number of cells that has characters initially. Then _k_ lines follows. The _i_-th line contains two integers and a character: _a__i_, _b__i_, _c__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_; _c__i_ is either 'o' or 'x'). This line means: there is a character _c__i_ in the cell that is located on the intersection of the _a__i_-th row and _b__i_-th column. All the given cells are distinct. Consider that the rows are numbered from 1 to _n_ from top to bottom. Analogically, the columns are numbered from 1 to _n_ from left to right. Output Print a single integer — the answer to the problem. Note In the first example there are two ways: xxo xoo xox ooo oxx oox | 2,800 | true | false | false | false | false | false | false | false | false | false | 7,993 |
691B | Problem - 691B - Codeforces =============== xa0 which consists of only English letters. Output Print "TAK" if the string _s_ is "s-palindrome" and "NIE" otherwise. Examples Input oXoxoXo Output TAK Input bod Output TAK Input ER Output NIE | 1,600 | false | false | true | false | false | false | false | false | false | false | 7,046 |
1057B | We get more and more news about DDoS-attacks of popular websites. Arseny is an admin and he thinks that a website is under a DDoS-attack if the total number of requests for a some period of time exceeds $$$100 cdot t$$$, where $$$t$$$ — the number of seconds in this time segment. Arseny knows statistics on the number of requests per second since the server is booted. He knows the sequence $$$r_1, r_2, dots, r_n$$$, where $$$r_i$$$ — the number of requests in the $$$i$$$-th second after boot. Determine the length of the longest continuous period of time, which Arseny considers to be a DDoS-attack. A seeking time period should not go beyond the boundaries of the segment $$$[1, n]$$$. Input The first line contains $$$n$$$ ($$$1 le n le 5000$$$) — number of seconds since server has been booted. The second line contains sequence of integers $$$r_1, r_2, dots, r_n$$$ ($$$0 le r_i le 5000$$$), $$$r_i$$$ — number of requests in the $$$i$$$-th second. Output Print the only integer number — the length of the longest time period which is considered to be a DDoS-attack by Arseny. If it doesn't exist print 0. | 1,400 | false | false | false | false | false | false | true | false | false | false | 5,423 |
939B | Dima has a hamsters farm. Soon _N_ hamsters will grow up on it and Dima will sell them in a city nearby. Hamsters should be transported in boxes. If some box is not completely full, the hamsters in it are bored, that's why each box should be completely full with hamsters. Dima can buy boxes at a factory. The factory produces boxes of _K_ kinds, boxes of the _i_-th kind can contain in themselves _a__i_ hamsters. Dima can buy any amount of boxes, but he should buy boxes of only one kind to get a wholesale discount. Of course, Dima would buy boxes in such a way that each box can be completely filled with hamsters and transported to the city. If there is no place for some hamsters, Dima will leave them on the farm. Find out how many boxes and of which type should Dima buy to transport maximum number of hamsters. Input The first line contains two integers _N_ and _K_ (0u2009≤u2009_N_u2009≤u20091018, 1u2009≤u2009_K_u2009≤u2009105)xa0— the number of hamsters that will grow up on Dima's farm and the number of types of boxes that the factory produces. The second line contains _K_ integers _a_1, _a_2, ..., _a__K_ (1u2009≤u2009_a__i_u2009≤u20091018 for all _i_)xa0— the capacities of boxes. Output Output two integers: the type of boxes that Dima should buy and the number of boxes of that type Dima should buy. Types of boxes are numbered from 1 to _K_ in the order they are given in input. If there are many correct answers, output any of them. | 1,000 | false | false | true | false | false | false | false | false | false | false | 5,943 |
1511D | Problem - 1511D - 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 constructive algorithms graphs greedy strings *1600 No tag edit access → Contest materials such that $$$s_i = s_j$$$ and $$$s_{i+1} = s_{j+1}$$$. You are given two positive integers $$$n$$$ and $$$k$$$. Among all strings with length $$$n$$$ that contain only the first $$$k$$$ characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost — find any of them. Input The only line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 2 cdot 10^5; 1 le k le 26$$$). Output Print the string $$$s$$$ such that it consists of $$$n$$$ characters, each its character is one of the $$$k$$$ first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings — print any of them. Examples Input 9 4 Output aabacadbb Input 5 1 Output aaaaa Input 10 26 Output codeforces | 1,600 | false | true | false | false | false | true | true | false | false | true | 3,113 |
1312G | You are given a set of strings $$$S$$$. Each string consists of lowercase Latin letters. For each string in this set, you want to calculate the minimum number of seconds required to type this string. To type a string, you have to start with an empty string and transform it into the string you want to type using the following actions: if the current string is $$$t$$$, choose some lowercase Latin letter $$$c$$$ and append it to the back of $$$t$$$, so the current string becomes $$$t + c$$$. This action takes $$$1$$$ second; use autocompletion. When you try to autocomplete the current string $$$t$$$, a list of all strings $$$s in S$$$ such that $$$t$$$ is a prefix of $$$s$$$ is shown to you. This list includes $$$t$$$ itself, if $$$t$$$ is a string from $$$S$$$, and the strings are ordered lexicographically. You can transform $$$t$$$ into the $$$i$$$-th string from this list in $$$i$$$ seconds. Note that you may choose any string from this list you want, it is not necessarily the string you are trying to type. What is the minimum number of seconds that you have to spend to type each string from $$$S$$$? Note that the strings from $$$S$$$ are given in an unusual way. Input The first line contains one integer $$$n$$$ ($$$1 le n le 10^6$$$). Then $$$n$$$ lines follow, the $$$i$$$-th line contains one integer $$$p_i$$$ ($$$0 le p_i < i$$$) and one lowercase Latin character $$$c_i$$$. These lines form some set of strings such that $$$S$$$ is its subset as follows: there are $$$n + 1$$$ strings, numbered from $$$0$$$ to $$$n$$$; the $$$0$$$-th string is an empty string, and the $$$i$$$-th string ($$$i ge 1$$$) is the result of appending the character $$$c_i$$$ to the string $$$p_i$$$. It is guaranteed that all these strings are distinct. The next line contains one integer $$$k$$$ ($$$1 le k le n$$$) — the number of strings in $$$S$$$. The last line contains $$$k$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_k$$$ ($$$1 le a_i le n$$$, all $$$a_i$$$ are pairwise distinct) denoting the indices of the strings generated by above-mentioned process that form the set $$$S$$$ — formally, if we denote the $$$i$$$-th generated string as $$$s_i$$$, then $$$S = {s_{a_1}, s_{a_2}, dots, s_{a_k}}$$$. Output Print $$$k$$$ integers, the $$$i$$$-th of them should be equal to the minimum number of seconds required to type the string $$$s_{a_i}$$$. Examples Input 10 0 i 1 q 2 g 0 k 1 e 5 r 4 m 5 h 3 p 3 e 5 8 9 1 10 6 Input 8 0 a 1 b 2 a 2 b 4 a 4 b 5 c 6 d 5 2 3 4 7 8 Note In the first example, $$$S$$$ consists of the following strings: ieh, iqgp, i, iqge, ier. | 2,600 | false | false | false | true | true | false | false | false | false | false | 4,131 |
376A | You have a description of a lever as string _s_. We'll represent the string length as record _s_, then the lever looks as a horizontal bar with weights of length _s_u2009-u20091 with exactly one pivot. We will assume that the bar is a segment on the _Ox_ axis between points 0 and _s_u2009-u20091. The decoding of the lever description is given below. If the _i_-th character of the string equals "^", that means that at coordinate _i_ there is the pivot under the bar. If the _i_-th character of the string equals "=", that means that at coordinate _i_ there is nothing lying on the bar. If the _i_-th character of the string equals digit _c_ (1-9), that means that at coordinate _i_ there is a weight of mass _c_ on the bar. Your task is, given the lever description, print if it will be in balance or not. Assume that the bar doesn't weight anything. Assume that the bar initially is in balance then all weights are simultaneously put on it. After that the bar either tilts to the left, or tilts to the right, or is in balance. Input The first line contains the lever description as a non-empty string _s_ (3u2009≤u2009_s_u2009≤u2009106), consisting of digits (1-9) and characters "^" and "=". It is guaranteed that the line contains exactly one character "^". It is guaranteed that the pivot of the lever isn't located in any end of the lever bar. To solve the problem you may need 64-bit integer numbers. Please, do not forget to use them in your programs. Output Print "left" if the given lever tilts to the left, "right" if it tilts to the right and "balance", if it is in balance. Note As you solve the problem, you may find the following link useful to better understand how a lever functions: pictures to the examples: | 900 | true | false | true | false | false | false | false | false | false | false | 8,326 |
56E | Problem - 56E - 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 binary search data structures sortings *2200 No tag edit access → Contest materials ") which is the number of dominoes. Then follow _n_ lines containing two integers _x__i_ and _h__i_ (u2009-u2009108u2009≤u2009_x__i_u2009≤u2009108,u20092u2009≤u2009_h__i_u2009≤u2009108) each, which are the coordinate and height of every domino. No two dominoes stand on one point. Output Print _n_ space-separated numbers _z__i_ — the number of dominoes that will fall if Vasya pushes the _i_-th domino to the right (including the domino itself). Examples Input 4 16 5 20 5 10 10 18 2 Output 3 1 4 1 Input 4 0 10 1 5 9 10 15 10 Output 4 1 2 1 | 2,200 | false | false | false | false | true | false | false | true | true | false | 9,691 |
482B | Problem - 482B - 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 data structures trees *1800 No tag edit access → Contest materials meaning that value should be equal to _q__i_. Your task is to find any interesting array of _n_ elements or state that such array doesn't exist. Expression _x_&_y_ means the bitwise AND of numbers _x_ and _y_. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". Input The first line contains two integers _n_, _m_ (1u2009≤u2009_n_u2009≤u2009105, 1u2009≤u2009_m_u2009≤u2009105)xa0— the number of elements in the array and the number of limits. Each of the next _m_ lines contains three integers _l__i_, _r__i_, _q__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_, 0u2009≤u2009_q__i_u2009<u2009230) describing the _i_-th limit. Output If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print _n_ integers _a_ | 1,800 | false | false | false | false | true | true | false | false | false | false | 7,908 |
1494A | You are given a string $$$a$$$, consisting of $$$n$$$ characters, $$$n$$$ is even. For each $$$i$$$ from $$$1$$$ to $$$n$$$ $$$a_i$$$ is one of 'A', 'B' or 'C'. A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"), and ")(", "(" and ")" are not. You want to find a string $$$b$$$ that consists of $$$n$$$ characters such that: $$$b$$$ is a regular bracket sequence; if for some $$$i$$$ and $$$j$$$ ($$$1 le i, j le n$$$) $$$a_i=a_j$$$, then $$$b_i=b_j$$$. In other words, you want to replace all occurrences of 'A' with the same type of bracket, then all occurrences of 'B' with the same type of bracket and all occurrences of 'C' with the same type of bracket. Your task is to determine if such a string $$$b$$$ exists. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of testcases. Then the descriptions of $$$t$$$ testcases follow. The only line of each testcase contains a string $$$a$$$. $$$a$$$ consists only of uppercase letters 'A', 'B' and 'C'. Let $$$n$$$ be the length of $$$a$$$. It is guaranteed that $$$n$$$ is even and $$$2 le n le 50$$$. Output For each testcase print "YES" if there exists such a string $$$b$$$ that: $$$b$$$ is a regular bracket sequence; if for some $$$i$$$ and $$$j$$$ ($$$1 le i, j le n$$$) $$$a_i=a_j$$$, then $$$b_i=b_j$$$. Otherwise, print "NO". You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer). Example Input 4 AABBAC CACA BBBBAC ABCA Note In the first testcase one of the possible strings $$$b$$$ is "(())()". In the second testcase one of the possible strings $$$b$$$ is "()()". | 900 | false | false | true | false | false | false | true | false | false | false | 3,202 |
1280A | We start with a string $$$s$$$ consisting only of the digits $$$1$$$, $$$2$$$, or $$$3$$$. The length of $$$s$$$ is denoted by $$$s$$$. For each $$$i$$$ from $$$1$$$ to $$$s$$$, the $$$i$$$-th character of $$$s$$$ is denoted by $$$s_i$$$. There is one cursor. The cursor's location $$$ell$$$ is denoted by an integer in $$${0, ldots, s}$$$, with the following meaning: If $$$ell = 0$$$, then the cursor is located before the first character of $$$s$$$. If $$$ell = s$$$, then the cursor is located right after the last character of $$$s$$$. If $$$0 < ell < s$$$, then the cursor is located between $$$s_ell$$$ and $$$s_{ell+1}$$$. We denote by $$$s_ ext{left}$$$ the string to the left of the cursor and $$$s_ ext{right}$$$ the string to the right of the cursor. We also have a string $$$c$$$, which we call our clipboard, which starts out as empty. There are three types of actions: The Move action. Move the cursor one step to the right. This increments $$$ell$$$ once. The Cut action. Set $$$c leftarrow s_ ext{right}$$$, then set $$$s leftarrow s_ ext{left}$$$. The Paste action. Append the value of $$$c$$$ to the end of the string $$$s$$$. Note that this doesn't modify $$$c$$$. The cursor initially starts at $$$ell = 0$$$. Then, we perform the following procedure: 1. Perform the Move action once. 2. Perform the Cut action once. 3. Perform the Paste action $$$s_ell$$$ times. 4. If $$$ell = x$$$, stop. Otherwise, return to step 1. You're given the initial string $$$s$$$ and the integer $$$x$$$. What is the length of $$$s$$$ when the procedure stops? Since this value may be very large, only find it modulo $$$10^9 + 7$$$. It is guaranteed that $$$ell le s$$$ at any time. Input The first line of input contains a single integer $$$t$$$ ($$$1 le t le 1000$$$) denoting the number of test cases. The next lines contain descriptions of the test cases. The first line of each test case contains a single integer $$$x$$$ ($$$1 le x le 10^6$$$). The second line of each test case consists of the initial string $$$s$$$ ($$$1 le s le 500$$$). It is guaranteed, that $$$s$$$ consists of the characters "1", "2", "3". It is guaranteed that the sum of $$$x$$$ in a single file is at most $$$10^6$$$. It is guaranteed that in each test case before the procedure will stop it will be true that $$$ell le s$$$ at any time. Note Let's illustrate what happens with the first test case. Initially, we have $$$s = $$$ 231. Initially, $$$ell = 0$$$ and $$$c = varepsilon$$$ (the empty string). The following things happen if we follow the procedure above: Step 1, Move once: we get $$$ell = 1$$$. Step 2, Cut once: we get $$$s = $$$ 2 and $$$c = $$$ 31. Step 3, Paste $$$s_ell = $$$ 2 times: we get $$$s = $$$ 23131. Step 4: $$$ell = 1 ot= x = 5$$$, so we return to step 1. Step 1, Move once: we get $$$ell = 2$$$. Step 2, Cut once: we get $$$s = $$$ 23 and $$$c = $$$ 131. Step 3, Paste $$$s_ell = $$$ 3 times: we get $$$s = $$$ 23131131131. Step 4: $$$ell = 2 ot= x = 5$$$, so we return to step 1. Step 1, Move once: we get $$$ell = 3$$$. Step 2, Cut once: we get $$$s = $$$ 231 and $$$c = $$$ 31131131. Step 3, Paste $$$s_ell = $$$ 1 time: we get $$$s = $$$ 23131131131. Step 4: $$$ell = 3 ot= x = 5$$$, so we return to step 1. Step 1, Move once: we get $$$ell = 4$$$. Step 2, Cut once: we get $$$s = $$$ 2313 and $$$c = $$$ 1131131. Step 3, Paste $$$s_ell = $$$ 3 times: we get $$$s = $$$ 2313113113111311311131131. Step 4: $$$ell = 4 ot= x = 5$$$, so we return to step 1. Step 1, Move once: we get $$$ell = 5$$$. Step 2, Cut once: we get $$$s = $$$ 23131 and $$$c = $$$ 13113111311311131131. Step 3, Paste $$$s_ell = $$$ 1 times: we get $$$s = $$$ 2313113113111311311131131. Step 4: $$$ell = 5 = x$$$, so we stop. At the end of the procedure, $$$s$$$ has length $$$25$$$. | 1,700 | true | false | true | false | false | false | false | false | false | false | 4,294 |
1017F | Notice: unusual memory limit! After the war, destroyed cities in the neutral zone were restored. And children went back to school. The war changed the world, as well as education. In those hard days, a new math concept was created. As we all know, logarithm function can be described as: $$$$$$ log(p_1^{a_1}p_2^{a_2}...p_k^{a_2}) = a_1 log p_1 + a_2 log p_2 + ... + a_k log p_k $$$$$$ Where $$$p_1^{a_1}p_2^{a_2}...p_k^{a_2}$$$ is the prime factorization of a integer. A problem is that the function uses itself in the definition. That is why it is hard to calculate. So, the mathematicians from the neutral zone invented this: $$$$$$ ext{exlog}_f(p_1^{a_1}p_2^{a_2}...p_k^{a_2}) = a_1 f(p_1) + a_2 f(p_2) + ... + a_k f(p_k) $$$$$$ Notice that $$$ ext{exlog}_f(1)$$$ is always equal to $$$0$$$. This concept for any function $$$f$$$ was too hard for children. So teachers told them that $$$f$$$ can only be a polynomial of degree no more than $$$3$$$ in daily uses (i.e., $$$f(x) = Ax^3+Bx^2+Cx+D$$$). "Class is over! Don't forget to do your homework!" Here it is: $$$$$$ sum_{i=1}^n ext{exlog}_f(i) $$$$$$ Help children to do their homework. Since the value can be very big, you need to find the answer modulo $$$2^{32}$$$. Input The only line contains five integers $$$n$$$, $$$A$$$, $$$B$$$, $$$C$$$, and $$$D$$$ ($$$1 le n le 3 cdot 10^8$$$, $$$0 le A,B,C,D le 10^6$$$). Output Print the answer modulo $$$2^{32}$$$. Note In the first sample: $$$ ext{exlog}_f(1) = 0$$$ $$$ ext{exlog}_f(2) = 2$$$ $$$ ext{exlog}_f(3) = 3$$$ $$$ ext{exlog}_f(4) = 2 + 2 = 4$$$ $$$ ext{exlog}_f(5) = 5$$$ $$$ ext{exlog}_f(6) = 2 + 3 = 5$$$ $$$ ext{exlog}_f(7) = 7$$$ $$$ ext{exlog}_f(8) = 2 + 2 + 2 = 6$$$ $$$ ext{exlog}_f(9) = 3 + 3 = 6$$$ $$$ ext{exlog}_f(10) = 2 + 5 = 7$$$ $$$ ext{exlog}_f(11) = 11$$$ $$$ ext{exlog}_f(12) = 2 + 2 + 3 = 7$$$ $$$ sum_{i=1}^{12} ext{exlog}_f(i)=63 $$$ In the second sample: $$$ ext{exlog}_f(1) = 0$$$ $$$ ext{exlog}_f(2) = (1 imes 2^3 + 2 imes 2^2 + 3 imes 2 + 4) = 26$$$ $$$ ext{exlog}_f(3) = (1 imes 3^3 + 2 imes 3^2 + 3 imes 3 + 4) = 58$$$ $$$ ext{exlog}_f(4) = 2 imes ext{exlog}_f(2) = 52$$$ $$$ sum_{i=1}^4 ext{exlog}_f(i)=0+26+58+52=136 $$$ | 2,500 | true | false | false | false | false | false | true | false | false | false | 5,600 |
975A | In Aramic language words can only represent objects. Words in Aramic have special properties: A word is a root if it does not contain the same letter more than once. A root and all its permutations represent the same object. The root $$$x$$$ of a word $$$y$$$ is the word that contains all letters that appear in $$$y$$$ in a way that each letter appears once. For example, the root of "aaaa", "aa", "aaa" is "a", the root of "aabb", "bab", "baabb", "ab" is "ab". Any word in Aramic represents the same object as its root. You have an ancient script in Aramic. What is the number of different objects mentioned in the script? Input The first line contains one integer $$$n$$$ ($$$1 leq n leq 10^3$$$)xa0— the number of words in the script. The second line contains $$$n$$$ words $$$s_1, s_2, ldots, s_n$$$xa0— the script itself. The length of each string does not exceed $$$10^3$$$. It is guaranteed that all characters of the strings are small latin letters. Output Output one integerxa0— the number of different objects mentioned in the given ancient Aramic script. Note In the first test, there are two objects mentioned. The roots that represent them are "a","ab". In the second test, there is only one object, its root is "amer", the other strings are just permutations of "amer". | 900 | false | false | true | false | false | false | false | false | false | false | 5,835 |
1774G | ChthollyNotaSeniorious gives DataStructures a number axis with $$$m$$$ distinct segments on it. Let $$$f(l,r)$$$ be the number of ways to choose an even number of segments such that the union of them is exactly $$$[l,r]$$$, and $$$g(l,r)$$$ be the number of ways to choose an odd number of segments such that the union of them is exactly $$$[l,r]$$$. ChthollyNotaSeniorious asked DataStructures $$$q$$$ questions. In each query, ChthollyNotaSeniorious will give DataStructures two numbers $$$l, r$$$, and now he wishes that you can help him find the value $$$f(l,r)-g(l,r)$$$ modulo $$$998,244,353$$$ so that he wouldn't let her down. Input The first line of input contains two integers $$$m$$$ ($$$1 leq m leq 2 cdot 10^5$$$) and $$$q$$$ ($$$1 leq q leq 2 cdot 10^5$$$) xa0— the number of segments and queries, correspondingly. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 leq x_i < y_i leq 10^9$$$), denoting a segment $$$[x_i, y_i]$$$. It is guaranteed that all segments are distinct. More formally, there do not exist two numbers $$$i, j$$$ with $$$1 le i < j le m$$$ such that $$$x_i = x_j$$$ and $$$y_i = y_j$$$. The $$$i$$$-th of the next $$$q$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 leq l_i < r_i leq 10^9$$$), describing a query. Output For each query, output a single integer xa0— $$$f(l_i,r_i)-g(l_i,r_i)$$$ modulo $$$998,244,353$$$. Example Input 4 2 1 3 4 6 2 4 3 5 1 4 1 5 Note In the first query, we have to find $$$f(1, 4) - g(1, 4)$$$. The only subset of segments with union $$$[1, 4]$$$ is $$${[1, 3], [2, 4]}$$$, so $$$f(1, 4) = 1, g(1, 4) = 0$$$. In the second query, we have to find $$$f(1, 5) - g(1, 5)$$$. The only subsets of segments with union $$$[1, 5]$$$ are $$${[1, 3], [2, 4], [3, 5]}$$$ and $$${[1, 3], [3, 5]}$$$, so $$$f(1, 5) = 1, g(1, 5) = 1$$$. | 3,200 | false | false | false | true | true | true | true | false | false | false | 1,637 |
912D | While Grisha was celebrating New Year with Ded Moroz, Misha gifted Sasha a small rectangular pond of size _n_u2009×u2009_m_, divided into cells of size 1u2009×u20091, inhabited by tiny evil fishes (no more than one fish per cell, otherwise they'll strife!). The gift bundle also includes a square scoop of size _r_u2009×u2009_r_, designed for fishing. If the lower-left corner of the scoop-net is located at cell (_x_,u2009_y_), all fishes inside the square (_x_,u2009_y_)...(_x_u2009+u2009_r_u2009-u20091,u2009_y_u2009+u2009_r_u2009-u20091) get caught. Note that the scoop-net should lie completely inside the pond when used. Unfortunately, Sasha is not that skilled in fishing and hence throws the scoop randomly. In order to not frustrate Sasha, Misha decided to release _k_ fishes into the empty pond in such a way that the expected value of the number of caught fishes is as high as possible. Help Misha! In other words, put _k_ fishes in the pond into distinct cells in such a way that when the scoop-net is placed into a random position among (_n_u2009-u2009_r_u2009+u20091)·(_m_u2009-u2009_r_u2009+u20091) possible positions, the average number of caught fishes is as high as possible. Input The only line contains four integers _n_,u2009_m_,u2009_r_,u2009_k_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105, 1u2009≤u2009_r_u2009≤u2009_min_(_n_,u2009_m_), 1u2009≤u2009_k_u2009≤u2009_min_(_n_·_m_,u2009105)). Output Print a single numberxa0— the maximum possible expected number of caught fishes. You answer is considered correct, is its absolute or relative error does not exceed 10u2009-u20099. Namely, let your answer be _a_, and the jury's answer be _b_. Your answer is considered correct, if . Note In the first example you can put the fishes in cells (2,u20091), (2,u20092), (2,u20093). In this case, for any of four possible positions of the scoop-net (highlighted with light green), the number of fishes inside is equal to two, and so is the expected value. | 2,100 | false | true | false | false | true | false | false | false | false | true | 6,093 |
1989E | Problem - 1989E - 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 combinatorics dp math *2300 No tag edit access → Contest materials — Video Tutorial") $$$). Output Print one integer — the number of different arrays $$$b$$$ you can obtain, taken modulo $$$998244353$$$. Examples Input 2 2 Output 1 Input 4 3 Output 3 Input 6 2 Output 20 Input 6 5 Output 3 Input 133 7 Output 336975971 | 2,300 | true | false | false | true | false | false | false | false | false | false | 337 |
1690F | Polycarp found the string $$$s$$$ and the permutation $$$p$$$. Their lengths turned out to be the same and equal to $$$n$$$. A permutation of $$$n$$$ elementsxa0— is an array of length $$$n$$$, in which every integer from $$$1$$$ to $$$n$$$ occurs exactly once. For example, $$$[1, 2, 3]$$$ and $$$[4, 3, 5, 1, 2]$$$ are permutations, but $$$[1, 2, 4]$$$, $$$[4, 3, 2, 1, 2]$$$ and $$$[0, 1, 2]$$$ are not. In one operation he can multiply $$$s$$$ by $$$p$$$, so he replaces $$$s$$$ with string $$$new$$$, in which for any $$$i$$$ from $$$1$$$ to $$$n$$$ it is true that $$$new_i = s_{p_i}$$$. For example, with $$$s=wmbe$$$ and $$$p = [3, 1, 4, 2]$$$, after operation the string will turn to $$$s=s_3 s_1 s_4 s_2=bwem$$$. Polycarp wondered after how many operations the string would become equal to its initial value for the first time. Since it may take too long, he asks for your help in this matter. It can be proved that the required number of operations always exists. It can be very large, so use a 64-bit integer type. Input The first line of input contains one integer $$$t$$$ ($$$1 le t le 5000$$$) — the number of test cases in input. The first line of each case contains single integer $$$n$$$ ($$$1 le n le 200$$$) — the length of string and permutation. The second line of each case contains a string $$$s$$$ of length $$$n$$$, containing lowercase Latin letters. The third line of each case contains $$$n$$$ integersxa0— permutation $$$p$$$ ($$$1 le p_i le n$$$), all $$$p_i$$$ are different. Output Output $$$t$$$ lines, each of which contains the answer to the corresponding test case of input. As an answer output single integerxa0— the minimum number of operations, after which the string $$$s$$$ will become the same as it was before operations. Note In the first sample operation doesn't change the string, so it will become the same as it was after $$$1$$$ operations. In the second sample the string will change as follows: $$$s$$$ = babaa $$$s$$$ = abaab $$$s$$$ = baaba $$$s$$$ = abbaa $$$s$$$ = baaab $$$s$$$ = ababa | 1,700 | true | false | false | false | false | false | false | false | false | true | 2,138 |
1868B1 | This is the easy version of the problem. The only difference is that in this version everyone must give candies to exactly one person and receive candies from exactly one person. Note that a submission cannot pass both versions of the problem at the same time. You can make hacks only if both versions of the problem are solved. After Zhongkao examination, Daniel and his friends are going to have a party. Everyone will come with some candies. There will be $$$n$$$ people at the party. Initially, the $$$i$$$-th person has $$$a_i$$$ candies. During the party, they will swap their candies. To do this, they will line up in an arbitrary order and everyone will do the following exactly once: Choose an integer $$$p$$$ ($$$1 le p le n$$$) and a non-negative integer $$$x$$$, then give his $$$2^{x}$$$ candies to the $$$p$$$-th person. Note that one cannot give more candies than currently he has (he might receive candies from someone else before) and he cannot give candies to himself. Daniel likes fairness, so he will be happy if and only if everyone receives candies from exactly one person. Meanwhile, his friend Tom likes average, so he will be happy if and only if all the people have the same number of candies after all swaps. Determine whether there exists a way to swap candies, so that both Daniel and Tom will be happy after the swaps. Input The first line of input contains a single integer $$$t$$$ ($$$1le tle 1000$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2le nle 2cdot 10^5$$$) — the number of people at the party. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$1le a_ile 10^9$$$) — the number of candies each person has. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, print "Yes" (without quotes) if exists a way to swap candies to make both Daniel and Tom happy, and print "No" (without quotes) otherwise. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Example Input 6 3 2 4 3 5 1 2 3 4 5 6 1 4 7 1 5 4 2 20092043 20092043 12 9 9 8 2 4 4 3 5 1 1 1 1 6 2 12 7 16 11 12 Output Yes Yes No Yes No No Note In the first test case: The first person gives $$$1$$$ candy to the third person; The second person gives $$$2$$$ candies to the first person; The third person gives $$$1$$$ candy to the second person. Then all people have $$$3$$$ candies. In the second test case: The fifth person gives $$$4$$$ candies to the first person, from now on the first person has $$$5$$$ candies; The first person gives $$$2$$$ candies to the third person; The third person gives $$$2$$$ candies to the fifth person; The fourth person gives $$$2$$$ candies to the second person; The second person gives $$$1$$$ candy to the fourth person. Then all people have $$$3$$$ candies. Note that at first the first person cannot give $$$2$$$ candies to the third person, since he only has $$$a_1=1$$$ candy. But after the fifth person gives him $$$4$$$ candies, he can do this, because he currently has $$$1+4=5$$$ candies. In the third test case, it's impossible for all people to have the same number of candies. In the fourth test case, the first person gives $$$1024$$$ candies to the second person, and the second person gives $$$1024$$$ candies to the first person as well. | 1,700 | true | true | true | false | false | true | false | false | false | true | 1,067 |
803B | Problem - 803B - Codeforces =============== xa0 . There is at least one zero element in the given array. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u20092·105) — length of the array _a_. The second line contains integer elements of the array separated by single spaces (u2009-u2009109u2009≤u2009_a__i_u2009≤u2009109). Output Print the sequence _d_0,u2009_d_1,u2009...,u2009_d__n_u2009-u20091, where _d__i_ is the difference of indices between _i_ and nearest _j_ such that _a__j_u2009=u20090. It is possible that _i_u2009=u2009_j_. Examples Input 9 2 1 0 3 0 0 3 2 4 Output 2 1 0 1 0 0 1 2 3 Input 5 0 1 2 3 4 Output 0 1 2 3 4 Input 7 5 6 0 1 -2 3 4 Output 2 1 0 1 2 3 4 | 1,200 | false | false | false | false | false | true | false | false | false | false | 6,551 |
160A | Imagine that you have a twin brother or sister. Having another person that looks exactly like you seems very unusual. It's hard to say if having something of an alter ego is good or bad. And if you do have a twin, then you very well know what it's like. Now let's imagine a typical morning in your family. You haven't woken up yet, and Mom is already going to work. She has been so hasty that she has nearly forgotten to leave the two of her darling children some money to buy lunches in the school cafeteria. She fished in the purse and found some number of coins, or to be exact, _n_ coins of arbitrary values _a_1,u2009_a_2,u2009...,u2009_a__n_. But as Mom was running out of time, she didn't split the coins for you two. So she scribbled a note asking you to split the money equally. As you woke up, you found Mom's coins and read her note. "But why split the money equally?" — you thought. After all, your twin is sleeping and he won't know anything. So you decided to act like that: pick for yourself some subset of coins so that the sum of values of your coins is strictly larger than the sum of values of the remaining coins that your twin will have. However, you correctly thought that if you take too many coins, the twin will suspect the deception. So, you've decided to stick to the following strategy to avoid suspicions: you take the minimum number of coins, whose sum of values is strictly more than the sum of values of the remaining coins. On this basis, determine what minimum number of coins you need to take to divide them in the described manner. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of coins. The second line contains a sequence of _n_ integers _a_1, _a_2, ..., _a__n_ (1u2009≤u2009_a__i_u2009≤u2009100) — the coins' values. All numbers are separated with spaces. Output In the single line print the single number — the minimum needed number of coins. Note In the first sample you will have to take 2 coins (you and your twin have sums equal to 6,u20090 correspondingly). If you take 1 coin, you get sums 3,u20093. If you take 0 coins, you get sums 0,u20096. Those variants do not satisfy you as your sum should be strictly more that your twins' sum. In the second sample one coin isn't enough for us, too. You can pick coins with values 1,u20092 or 2,u20092. In any case, the minimum number of coins equals 2. | 900 | false | true | false | false | false | false | false | false | true | false | 9,235 |
768A | Night gathers, and now my watch begins. It shall not end until my death. I shall take no wife, hold no lands, father no children. I shall wear no crowns and win no glory. I shall live and die at my post. I am the sword in the darkness. I am the watcher on the walls. I am the shield that guards the realms of men. I pledge my life and honor to the Night's Watch, for this night and all the nights to come." — The Night's Watch oath. With that begins the watch of Jon Snow. He is assigned the task to support the stewards. This time he has _n_ stewards with him whom he has to provide support. Each steward has his own strength. Jon Snow likes to support a steward only if there exists at least one steward who has strength strictly less than him and at least one steward who has strength strictly greater than him. Can you find how many stewards will Jon support? Input First line consists of a single integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of stewards with Jon Snow. Second line consists of _n_ space separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009109) representing the values assigned to the stewards. Output Output a single integer representing the number of stewards which Jon will feed. Note In the first sample, Jon Snow cannot support steward with strength 1 because there is no steward with strength less than 1 and he cannot support steward with strength 5 because there is no steward with strength greater than 5. In the second sample, Jon Snow can support steward with strength 2 because there are stewards with strength less than 2 and greater than 2. | 900 | false | false | false | false | false | true | false | false | true | false | 6,713 |
367E | Sereja is interested in intervals of numbers, so he has prepared a problem about intervals for you. An interval of numbers is a pair of integers [_l_,u2009_r_] (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_m_). Interval [_l_1,u2009_r_1] belongs to interval [_l_2,u2009_r_2] if the following condition is met: _l_2u2009≤u2009_l_1u2009≤u2009_r_1u2009≤u2009_r_2. Sereja wants to write out a sequence of _n_ intervals [_l_1,u2009_r_1], [_l_2,u2009_r_2], ..., [_l__n_,u2009_r__n_] on a piece of paper. At that, no interval in the sequence can belong to some other interval of the sequence. Also, Sereja loves number _x_ very much and he wants some (at least one) interval in the sequence to have _l__i_u2009=u2009_x_. Sereja wonders, how many distinct ways to write such intervals are there? Help Sereja and find the required number of ways modulo 1000000007 (109u2009+u20097). Two ways are considered distinct if there is such _j_ (1u2009≤u2009_j_u2009≤u2009_n_), that the _j_-th intervals in two corresponding sequences are not equal. Input The first line contains integers _n_, _m_, _x_ (1u2009≤u2009_n_·_m_u2009≤u2009100000,u20091u2009≤u2009_x_u2009≤u2009_m_) — the number of segments in the sequence, the constraints on the numbers in segments and Sereja's favourite number. Output In a single line print the answer modulo 1000000007 (109u2009+u20097). Note In third example next sequences will be correct: {[1,u20091],u2009[3,u20093]}, {[1,u20092],u2009[3,u20093]}, {[2,u20092],u2009[3,u20093]}, {[3,u20093],u2009[1,u20091]}, {[3,u20093],u2009[2,u20092]}, {[3,u20093],u2009[1,u20092]}. | 2,700 | false | false | false | true | false | false | false | false | false | false | 8,361 |
16B | Problem - 16B - 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 implementation sortings *900 No tag edit access → Contest materials ") and integer _m_ (1u2009≤u2009_m_u2009≤u200920). The _i_u2009+u20091-th line contains a pair of numbers _a__i_ and _b__i_ (1u2009≤u2009_a__i_u2009≤u2009108,u20091u2009≤u2009_b__i_u2009≤u200910). All the input numbers are integer. Output Output the only number — answer to the problem. Examples Input 7 3 5 10 2 5 3 6 Output 62 Input 3 3 1 3 2 2 3 1 Output 7 | 900 | false | true | true | false | false | false | false | false | true | false | 9,914 |
1661B | Problem - 1661B - 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 bitmasks brute force dfs and similar dp graphs greedy shortest paths *1300 No tag edit access → Contest materials bmod 32768$$$ or set $$$v = (2 cdot v) bmod 32768$$$. You are given $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$. What is the minimum number of operations you need to make each $$$a_i$$$ equal to $$$0$$$? Input The first line contains the single integer $$$n$$$ ($$$1 le n le 32768$$$)xa0— the number of integers. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i < 32768$$$). Output Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the minimum number of operations required to make $$$a_i$$$ equal to $$$0$$$. Example Input 4 19 32764 10240 49 Output 14 4 4 15 Note Let's consider each $$$a_i$$$: $$$a_1 = 19$$$. You can, firstly, increase it by one to get $$$20$$$ and then multiply it by two $$$13$$$ times. You'll get $$$0$$$ in $$$1 + 13 = 14$$$ steps. $$$a_2 = 32764$$$. You can increase it by one $$$4$$$ times: $$$32764 ightarrow 32765 ightarrow 32766 ightarrow 32767 ightarrow 0$$$. $$$a_3 = 10240$$$. You can multiply it by two $$$4$$$ times: $$$10240 ightarrow 20480 ightarrow 8192 ightarrow 16384 ightarrow 0$$$. $$$a_4 = 49$$$. You can multiply it by two $$$15$$$ times. | 1,300 | false | true | false | true | false | false | true | false | false | true | 2,318 |
2026C | There is a shop that sells action figures near Monocarp's house. A new set of action figures will be released shortly; this set contains $$$n$$$ figures, the $$$i$$$-th figure costs $$$i$$$ coins and is available for purchase from day $$$i$$$ to day $$$n$$$. For each of the $$$n$$$ days, Monocarp knows whether he can visit the shop. Every time Monocarp visits the shop, he can buy any number of action figures which are sold in the shop (of course, he cannot buy an action figure that is not yet available for purchase). If Monocarp buys at least two figures during the same day, he gets a discount equal to the cost of the most expensive figure he buys (in other words, he gets the most expensive of the figures he buys for free). Monocarp wants to buy exactly one $$$1$$$-st figure, one $$$2$$$-nd figure, ..., one $$$n$$$-th figure from the set. He cannot buy the same figure twice. What is the minimum amount of money he has to spend? Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Each test case consists of two lines: the first line contains one integer $$$n$$$ ($$$1 le n le 4 cdot 10^5$$$) — the number of figures in the set (and the number of days); the second line contains a string $$$s$$$ ($$$s = n$$$, each $$$s_i$$$ is either 0 or 1). If Monocarp can visit the shop on the $$$i$$$-th day, then $$$s_i$$$ is 1; otherwise, $$$s_i$$$ is 0. Additional constraints on the input: in each test case, $$$s_n$$$ is 1, so Monocarp is always able to buy all figures during the $$$n$$$-th day; the sum of $$$n$$$ over all test cases does not exceed $$$4 cdot 10^5$$$. Output For each test case, print one integer — the minimum amount of money Monocarp has to spend. Example Input 4 1 1 6 101101 7 1110001 5 11111 Note In the first test case, Monocarp buys the $$$1$$$-st figure on the $$$1$$$-st day and spends $$$1$$$ coin. In the second test case, Monocarp can buy the $$$1$$$-st and the $$$3$$$-rd figure on the $$$3$$$-rd day, the $$$2$$$-nd and the $$$4$$$-th figure on the $$$4$$$-th day, and the $$$5$$$-th and the $$$6$$$-th figure on the $$$6$$$-th day. Then, he will spend $$$1+2+5=8$$$ coins. In the third test case, Monocarp can buy the $$$2$$$-nd and the $$$3$$$-rd figure on the $$$3$$$-rd day, and all other figures on the $$$7$$$-th day. Then, he will spend $$$1+2+4+5+6 = 18$$$ coins. | 1,500 | false | true | true | false | true | true | true | true | false | false | 114 |
493E | Problem - 493E - 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 math *2800 No tag edit access → Contest materials ") Editorial") u2009=u2009_a_0u2009+u2009_a_1_x_1u2009+u2009...u2009+u2009_a__n__x__n_. Numbers _a__i_ are called coefficients of a polynomial, non-negative integer _n_ is called a degree of a polynomial. Vasya has made a bet with his friends that he can solve any problem with polynomials. They suggested him the problem: "Determine how many polynomials _P_(_x_) exist with integer non-negative coefficients so that , and , where and _b_ are given positive integers"? Vasya does not like losing bets, but he has no idea how to solve this task, so please help him to solve the problem. Input The input contains three integer positive numbers no greater than 1018. Output If there is an infinite number of such polynomials, then print "inf" without quotes, otherwise print the reminder of an answer modulo 109u2009+u20097. Examples Input 2 2 2 Output 2 Input 2 3 3 Output 1 | 2,800 | true | false | false | false | false | false | false | false | false | false | 7,859 |
277B | Problem - 277B - 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 geometry *2300 No tag edit access → Contest materials . Output If there is no solution, print "-1". Otherwise, print _n_ pairs of integers — the coordinates of points of any set with the convexity of _m_. The coordinates shouldn't exceed 108 in their absolute value. Examples Input 4 3 Output 0 0 3 0 0 3 1 1 Input 6 3 Output -1 Input 6 6 Output 10 0 -10 0 10 1 9 1 9 -1 0 -2 Input 7 4 Output 176166 6377 709276 539564 654734 174109 910147 434207 790497 366519 606663 21061 859328 886001 | 2,300 | false | false | false | false | false | true | false | false | false | false | 8,726 |
1854D | Michael and Brian are stuck in a hotel with $$$n$$$ rooms, numbered from $$$1$$$ to $$$n$$$, and need to find each other. But this hotel's doors are all locked and the only way of getting around is by using the teleporters in each room. Room $$$i$$$ has a teleporter that will take you to room $$$a_i$$$ (it might be that $$$a_i = i$$$). But they don't know the values of $$$a_1,a_2, dots, a_n$$$. Instead, they can call up the front desk to ask queries. In one query, they give a room $$$u$$$, a positive integer $$$k$$$, and a set of rooms $$$S$$$. The hotel concierge answers whether a person starting in room $$$u$$$, and using the teleporters $$$k$$$ times, ends up in a room in $$$S$$$. Brian is in room $$$1$$$. Michael wants to know the set $$$A$$$ of rooms so that if he starts in one of those rooms they can use the teleporters to meet up. He can ask at most $$$2000$$$ queries. The values $$$a_1, a_2, dots, a_n$$$ are fixed before the start of the interaction and do not depend on your queries. In other words, the interactor is not adaptive. Input The input contains a single integer $$$n$$$ ($$$2 leq n leq 500$$$). Interaction To ask a query, print a line in the format "? u k S S_1 S_2 ... S_S" with $$$1 leq u, S_1, ldots, S_{S} leq n$$$, all the $$$S_i$$$ distinct, and $$$1 leq k leq 10^9$$$. As a response to the query you will get "1" if the answer is yes, and "0" if the answer is no. To output an answer, you should print "! A A_1 A_2 ... A_A" with $$$1 leq A_1, ldots, A_{A} leq n$$$ all distinct. Printing the answer doesn't count as a query. See the interaction example for more clarity. If you ask too many queries or you ask a malformed query, you will get Wrong answer. After printing a query do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see the documentation for other languages. Hack format For the hacks use the following format. The first line should contain $$$n$$$ — the number of rooms. The second line should contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ — the teleporter in room $$$i$$$ leads to room $$$a_i$$$. Example Output ? 3 5 2 2 3 ? 2 5 2 2 3 ! 3 1 3 4 Note In the sample test, there are $$$n=5$$$ rooms and the (hidden) array describing the behavior of the teleporters is $$$[1, 2, 1, 3, 2]$$$. The first query asks whether starting from room number $$$a=3$$$, and using the teleporters $$$5$$$ times, one ends up in one of the two rooms $$$S={2, 3}$$$. This action results in ending up in the room $$$1$$$, so the answer is $$$0$$$. The second query asks whether starting from room number $$$a=2$$$, and using the teleporters $$$5$$$ times, one ends up in one of the two rooms $$$S={2, 3}$$$. This action results in ending up in the room $$$2$$$, so the answer is $$$1$$$. | 3,000 | false | false | false | false | false | false | false | true | false | false | 1,155 |
1654C | Alice has a cake, and she is going to cut it. She will perform the following operation $$$n-1$$$ times: choose a piece of the cake (initially, the cake is all one piece) with weight $$$wge 2$$$ and cut it into two smaller pieces of weight $$$lfloorfrac{w}{2} floor$$$ and $$$lceilfrac{w}{2} ceil$$$ ($$$lfloor x floor$$$ and $$$lceil x ceil$$$ denote . After cutting the cake in $$$n$$$ pieces, she will line up these $$$n$$$ pieces on a table in an arbitrary order. Let $$$a_i$$$ be the weight of the $$$i$$$-th piece in the line. You are given the array $$$a$$$. Determine whether there exists an initial weight and sequence of operations which results in $$$a$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print a single line: print YES if the array $$$a$$$ could have resulted from Alice's operations, otherwise print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). Example Input 14 1 327 2 869 541 2 985214736 985214737 3 2 3 1 3 2 3 3 6 1 1 1 1 1 1 6 100 100 100 100 100 100 8 100 100 100 100 100 100 100 100 8 2 16 1 8 64 1 4 32 10 1 2 4 7 1 1 1 1 7 2 10 7 1 1 1 3 1 3 3 2 3 10 1 4 4 1 1 1 3 3 3 1 10 2 3 2 2 1 2 2 2 2 2 4 999999999 999999999 999999999 999999999 Output YES NO YES YES NO YES NO YES YES YES YES NO NO YES Note In the first test case, it's possible to get the array $$$a$$$ by performing $$$0$$$ operations on a cake with weight $$$327$$$. In the second test case, it's not possible to get the array $$$a$$$. In the third test case, it's possible to get the array $$$a$$$ by performing $$$1$$$ operation on a cake with weight $$$1,970,429,473$$$: Cut it in half, so that the weights are $$$[985,214,736, 985,214,737]$$$. Note that the starting weight can be greater than $$$10^9$$$. In the fourth test case, it's possible to get the array $$$a$$$ by performing $$$2$$$ operations on a cake with weight $$$6$$$: Cut it in half, so that the weights are $$$[3,3]$$$. Cut one of the two pieces with weight $$$3$$$, so that the new weights are $$$[1, 2, 3]$$$ which is equivalent to $$$[2, 3, 1]$$$ up to reordering. | 1,400 | false | true | true | false | true | false | false | false | true | false | 2,360 |
1316E | Alice, the president of club FCB, wants to build a team for the new volleyball tournament. The team should consist of $$$p$$$ players playing in $$$p$$$ different positions. She also recognizes the importance of audience support, so she wants to select $$$k$$$ people as part of the audience. There are $$$n$$$ people in Byteland. Alice needs to select exactly $$$p$$$ players, one for each position, and exactly $$$k$$$ members of the audience from this pool of $$$n$$$ people. Her ultimate goal is to maximize the total strength of the club. The $$$i$$$-th of the $$$n$$$ persons has an integer $$$a_{i}$$$ associated with himxa0— the strength he adds to the club if he is selected as a member of the audience. For each person $$$i$$$ and for each position $$$j$$$, Alice knows $$$s_{i, j}$$$ xa0— the strength added by the $$$i$$$-th person to the club if he is selected to play in the $$$j$$$-th position. Each person can be selected at most once as a player or a member of the audience. You have to choose exactly one player for each position. Since Alice is busy, she needs you to help her find the maximum possible strength of the club that can be achieved by an optimal choice of players and the audience. Input The first line contains $$$3$$$ integers $$$n,p,k$$$ ($$$2 leq n leq 10^{5}, 1 leq p leq 7, 1 le k, p+k le n$$$). The second line contains $$$n$$$ integers $$$a_{1},a_{2},ldots,a_{n}$$$. ($$$1 leq a_{i} leq 10^{9}$$$). The $$$i$$$-th of the next $$$n$$$ lines contains $$$p$$$ integers $$$s_{i, 1}, s_{i, 2}, dots, s_{i, p}$$$. ($$$1 leq s_{i,j} leq 10^{9}$$$) Output Print a single integer $$${res}$$$ xa0— the maximum possible strength of the club. Examples Input 4 1 2 1 16 10 3 18 19 13 15 Input 6 2 3 78 93 9 17 13 78 80 97 30 52 26 17 56 68 60 36 84 55 Input 3 2 1 500 498 564 100002 3 422332 2 232323 1 Note In the first sample, we can select person $$$1$$$ to play in the $$$1$$$-st position and persons $$$2$$$ and $$$3$$$ as audience members. Then the total strength of the club will be equal to $$$a_{2}+a_{3}+s_{1,1}$$$. | 2,300 | false | true | false | true | false | false | false | false | true | false | 4,117 |
193B | John Doe has four arrays: _a_, _b_, _k_, and _p_. Each array consists of _n_ integers. Elements of all arrays are indexed starting from 1. Array _p_ is a permutation of integers 1 to _n_. John invented a game for his friends and himself. Initially a player is given array _a_. The player must consecutively execute exactly _u_ operations on _a_. You are permitted to execute the following operations: After all _u_ operations are applied, the number of points the player gets is determined by the formula . John wants to find out what maximum number of points a player can win in his game. Help him. Input The first line contains space-separated integers _n_, _u_ and _r_ (1u2009≤u2009_n_,u2009_u_u2009≤u200930, 0u2009≤u2009_r_u2009≤u2009100) — the number of elements in each array, the number of operations and the number that describes one of the operations. Each of the next four lines contains _n_ space-separated integers — arrays _a_, _b_, _k_, _p_. The first line has array _a_, the second line has array _b_, the third line has array _k_ and the fourth one has array _p_. It is guaranteed that elements of arrays _a_ and _b_ are positive and do not exceed 104 (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009104), elements of array _k_ do not exceed 104 in the absolute value (_k_u2009≤u2009104) and _p_ is a permutation of numbers from 1 to _n_. Output On a single line print number _s_ — the maximum number of points that a player can win in John's game. 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. Examples Input 3 2 1 7 7 7 8 8 8 1 2 3 1 3 2 Note In the first sample you should first apply the operation of the first type, then the operation of the second type. | 2,000 | false | false | false | false | false | false | true | false | false | false | 9,069 |
1553D | You are given two strings $$$s$$$ and $$$t$$$, both consisting of lowercase English letters. You are going to type the string $$$s$$$ character by character, from the first character to the last one. When typing a character, instead of pressing the button corresponding to it, you can press the "Backspace" button. It deletes the last character you have typed among those that aren't deleted yet (or does nothing if there are no characters in the current string). For example, if $$$s$$$ is "abcbd" and you press Backspace instead of typing the first and the fourth characters, you will get the string "bd" (the first press of Backspace deletes no character, and the second press deletes the character 'c'). Another example, if $$$s$$$ is "abcaa" and you press Backspace instead of the last two letters, then the resulting text is "a". Your task is to determine whether you can obtain the string $$$t$$$, if you type the string $$$s$$$ and press "Backspace" instead of typing several (maybe zero) characters of $$$s$$$. Input The first line contains a single integer $$$q$$$ ($$$1 le q le 10^5$$$) — the number of test cases. The first line of each test case contains the string $$$s$$$ ($$$1 le s le 10^5$$$). Each character of $$$s$$$ is a lowercase English letter. The second line of each test case contains the string $$$t$$$ ($$$1 le t le 10^5$$$). Each character of $$$t$$$ is a lowercase English letter. It is guaranteed that the total number of characters in the strings over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print "YES" if you can obtain the string $$$t$$$ by typing the string $$$s$$$ and replacing some characters with presses of "Backspace" button, or "NO" if you cannot. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). Example Input 4 ababa ba ababa bb aaa aaaa aababa ababa Note Consider the example test from the statement. In order to obtain "ba" from "ababa", you may press Backspace instead of typing the first and the fourth characters. There's no way to obtain "bb" while typing "ababa". There's no way to obtain "aaaa" while typing "aaa". In order to obtain "ababa" while typing "aababa", you have to press Backspace instead of typing the first character, then type all the remaining characters. | 1,500 | false | true | false | true | false | false | false | false | false | false | 2,872 |
914D | Bash likes playing with arrays. He has an array _a_1,u2009_a_2,u2009... _a__n_ of _n_ integers. He likes to guess the greatest common divisor (gcd) of different segments of the array. Of course, sometimes the guess is not correct. However, Bash will be satisfied if his guess is almost correct. Suppose he guesses that the gcd of the elements in the range [_l_,u2009_r_] of _a_ is _x_. He considers the guess to be almost correct if he can change at most one element in the segment such that the gcd of the segment is _x_ after making the change. Note that when he guesses, he doesn't actually change the array — he just wonders if the gcd of the segment can be made _x_. Apart from this, he also sometimes makes changes to the array itself. Since he can't figure it out himself, Bash wants you to tell him which of his guesses are almost correct. Formally, you have to process _q_ queries of one of the following forms: 1u2009_l_u2009_r_u2009_x_ — Bash guesses that the gcd of the range [_l_,u2009_r_] is _x_. Report if this guess is almost correct. 2u2009_i_u2009_y_ — Bash sets _a__i_ to _y_. Note: The array is 1-indexed. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u20095·105) xa0— the size of the array. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109) xa0— the elements of the array. The third line contains an integer _q_ (1u2009≤u2009_q_u2009≤u20094·105) xa0— the number of queries. The next _q_ lines describe the queries and may have one of the following forms: 1u2009_l_u2009_r_u2009_x_ (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_,u20091u2009≤u2009_x_u2009≤u2009109). 2u2009_i_u2009_y_ (1u2009≤u2009_i_u2009≤u2009_n_,u20091u2009≤u2009_y_u2009≤u2009109). Guaranteed, that there is at least one query of first type. Output For each query of first type, output "YES" (without quotes) if Bash's guess is almost correct and "NO" (without quotes) otherwise. Examples Input 3 2 6 3 4 1 1 2 2 1 1 3 3 2 1 9 1 1 3 2 Input 5 1 2 3 4 5 6 1 1 4 2 2 3 6 1 1 4 2 1 1 5 2 2 5 10 1 1 5 2 Note In the first sample, the array initially is {2,u20096,u20093}. For query 1, the first two numbers already have their gcd as 2. For query 2, we can achieve a gcd of 3 by changing the first element of the array to 3. Note that the changes made during queries of type 1 are temporary and do not get reflected in the array. After query 3, the array is now {9,u20096,u20093}. For query 4, no matter which element you change, you cannot get the gcd of the range to be 2. | 1,900 | false | false | false | false | true | false | false | false | false | false | 6,080 |
220D | Problem - 220D - 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 geometry math *2500 No tag edit access → Contest materials , such that 0u2009≤u2009_x_u2009≤u2009_w_ and 0u2009≤u2009_y_u2009≤u2009_h_. Thus, the Little Elephant knows only (_w_u2009+u20091)·(_h_u2009+u20091) distinct integer points. The Little Elephant wants to paint a triangle with vertexes at integer points, the triangle's area must be a positive integer. For that, he needs to find the number of groups of three points that form such triangle. At that, the order of points in a group matters, that is, the group of three points (0;0), (0;2), (2;2) isn't equal to the group (0;2), (0;0), (2;2). Help the Little Elephant to find the number of groups of three integer points that form a nondegenerate triangle with integer area. Input A single line contains two integers _w_ and _h_ (1u2009≤u2009_w_,u2009_h_u2009≤u20094000). Output In a single output line print an integer — the remainder of dividing the answer to the problem by 1000000007 (109u2009+u20097). Examples Input 2 1 Output 36 Input 2 2 Output 240 | 2,500 | true | false | false | false | false | false | false | false | false | false | 8,956 |
1841E | There is a square matrix, consisting of $$$n$$$ rows and $$$n$$$ columns of cells, both numbered from $$$1$$$ to $$$n$$$. The cells are colored white or black. Cells from $$$1$$$ to $$$a_i$$$ are black, and cells from $$$a_i+1$$$ to $$$n$$$ are white, in the $$$i$$$-th column. You want to place $$$m$$$ integers in the matrix, from $$$1$$$ to $$$m$$$. There are two rules: each cell should contain at most one integer; black cells should not contain integers. The beauty of the matrix is the number of such $$$j$$$ that $$$j+1$$$ is written in the same row, in the next column as $$$j$$$ (in the neighbouring cell to the right). What's the maximum possible beauty of the matrix? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of testcases. The first line of each testcase contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the size of the matrix. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le n$$$)xa0— the number of black cells in each column. The third line contains a single integer $$$m$$$ ($$$0 le m le sum limits_{i=1}^n n - a_i$$$)xa0— the number of integers you have to write in the matrix. Note that this number might not fit into a 32-bit integer data type. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output For each testcase, print a single integerxa0— the maximum beauty of the matrix after you write all $$$m$$$ integers in it. Note that there are no more integers than the white cells, so the answer always exists. Example Input 6 3 0 0 0 9 4 2 0 3 1 5 4 2 0 3 1 6 4 2 0 3 1 10 10 0 2 2 1 5 10 3 4 1 1 20 1 1 0 | 2,200 | true | true | false | false | true | false | false | false | false | false | 1,241 |
767B | Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow. He knows that the receptionist starts working after _t__s_ minutes have passed after midnight and closes after _t__f_ minutes have passed after midnight (so that (_t__f_u2009-u20091) is the last minute when the receptionist is still working). The receptionist spends exactly _t_ minutes on each person in the queue. If the receptionist would stop working within _t_ minutes, he stops serving visitors (other than the one he already serves). Vasya also knows that exactly _n_ visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately. "Reception 1" For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them. Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! Input The first line contains three integers: the point of time when the receptionist begins to work _t__s_, the point of time when the receptionist stops working _t__f_ and the time the receptionist spends on each visitor _t_. The second line contains one integer _n_xa0— the amount of visitors (0u2009≤u2009_n_u2009≤u2009100u2009000). The third line contains positive integers in non-decreasing orderxa0— the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed 1012; it is guaranteed that _t__s_u2009<u2009_t__f_. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. Output Print single non-negative integerxa0— the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. Note In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served. | 2,100 | false | true | false | false | false | false | true | false | false | false | 6,717 |
1790A | On March 14, the day of the number $$$pi$$$ is celebrated all over the world. This is a very important mathematical constant equal to the ratio of the circumference of a circle to its diameter. Polycarp was told at school that the number $$$pi$$$ is irrational, therefore it has an infinite number of digits in decimal notation. He wanted to prepare for the Day of the number $$$pi$$$ by memorizing this number as accurately as possible. Polycarp wrote out all the digits that he managed to remember. For example, if Polycarp remembered $$$pi$$$ as $$$3.1415$$$, he wrote out 31415. Polycarp was in a hurry and could have made a mistake, so you decided to check how many first digits of the number $$$pi$$$ Polycarp actually remembers correctly. Input The first line of the input data contains the single integer $$$t$$$ ($$$1 le t le 10^3$$$)xa0— the number of test cases in the test. Each test case is described by a single string of digits $$$n$$$, which was written out by Polycarp. The string $$$n$$$ contains up to $$$30$$$ digits. Output Output $$$t$$$ integers, each of which is the answer to the corresponding test case, that is how many first digits of the number $$$pi$$$ Polycarp remembers correctly. Example Input 9 000 3 4141592653 141592653589793238462643383279 31420 31415 314159265358 27182 314159265358979323846264338327 Output 0 1 0 0 3 5 12 0 30 | 800 | true | false | true | false | false | false | false | false | false | false | 1,542 |
379C | Problem - 379C - Codeforces =============== xa0 has come up with a New Year adventure. Specifically, they decided to give ratings to all visitors. There are _n_ users on the site, for each user we know the rating value he wants to get as a New Year Present. We know that user _i_ wants to get at least _a__i_ rating units as a present. The X site is administered by very creative and thrifty people. On the one hand, they want to give distinct ratings and on the other hand, the total sum of the ratings in the present must be as small as possible. Help site X cope with the challenging task of rating distribution. Find the optimal distribution. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u20093·105) — the number of users on the site. The next line contains integer sequence _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109). Output Print a sequence of integers _b_1,u2009_b_2,u2009...,u2009_b__n_. Number _b__i_ means that user _i_ gets _b__i_ of rating as a present. The printed sequence must meet the problem conditions. If there are multiple optimal solutions, print any of them. Examples Input 3 5 1 1 Output 5 1 2 Input 1 1000000000 Output 1000000000 | 1,400 | false | true | false | false | false | false | false | false | true | false | 8,315 |
653G | Pussycat Sonya has an array consisting of _n_ positive integers. There are 2_n_ possible subsequences of the array. For each subsequence she counts the minimum number of operations to make all its elements equal. Each operation must be one of two: Choose some element of the subsequence and multiply it by some prime number. Choose some element of the subsequence and divide it by some prime number. The chosen element must be divisible by the chosen prime number. What is the sum of minimum number of operations for all 2_n_ possible subsequences? Find and print this sum modulo 109u2009+u20097. Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009300u2009000)xa0— the size of the array. The second line contains _n_ integers _t_1,u2009_t_2,u2009...,u2009_t__n_ (1u2009≤u2009_t__i_u2009≤u2009300u2009000)xa0— elements of the array. Note In the first sample, there are 8 possible subsequences: (60,u200960,u200940), (60,u200960), (60,u200940), (60,u200940), (60), (60), (40) and () (empty subsequence). For a subsequence (60,u200960,u200940) we can make all elements equal by two operationsxa0— divide 40 by 2 to get 20, and then multiply 20 by 3 to get 60. It's impossible to achieve the goal using less operations and thus we add 2 to the answer. There are two subsequences equal to (60,u200940) and for each of them the also need to make at least 2 operations. In each of other subsequences all numbers are already equal, so we need 0 operations for each of them. The sum is equal to 2u2009+u20092u2009+u20092u2009=u20096. | 3,100 | true | false | false | false | false | false | false | false | false | false | 7,198 |
660A | Problem - 660A - Codeforces =============== xa0 ]( "Educational Codeforces Round 11") — the number of elements in the given array. The second line contains _n_ integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009109) — the elements of the array _a_. Output Print integer _k_ on the first line — the least number of elements needed to add to the array _a_ to make it co-prime. The second line should contain _n_u2009+u2009_k_ integers _a__j_ — the elements of the array _a_ after adding _k_ elements to it. Note that the new array should be co-prime, so any two adjacent values should be co-prime. Also the new array should be got from the original array _a_ by adding _k_ elements to it. If there are multiple answers you can print any one of them. Example Input 3 2 7 28 Output 1 2 7 9 28 | 1,200 | true | true | true | false | false | false | false | false | false | false | 7,182 |
68A | Little Petya was given this problem for homework: You are given function (here represents the operation of taking the remainder). His task is to count the number of integers _x_ in range [_a_;_b_] with property _f_(_x_)u2009=u2009_x_. It is a pity that Petya forgot the order in which the remainders should be taken and wrote down only 4 numbers. Each of 24 possible orders of taking the remainder has equal probability of being chosen. For example, if Petya has numbers 1, 2, 3, 4 then he can take remainders in that order or first take remainder modulo 4, then modulo 2, 3, 1. There also are 22 other permutations of these numbers that represent orders in which remainder can be taken. In this problem 4 numbers wrote down by Petya will be pairwise distinct. Now it is impossible for Petya to complete the task given by teacher but just for fun he decided to find the number of integers with property that probability that _f_(_x_)u2009=u2009_x_ is not less than 31.4159265352718281828459045%. In other words, Petya will pick up the number _x_ if there exist at least 7 permutations of numbers _p_1,u2009_p_2,u2009_p_3,u2009_p_4, for which _f_(_x_)u2009=u2009_x_. Input First line of the input will contain 6 integers, separated by spaces: _p_1,u2009_p_2,u2009_p_3,u2009_p_4,u2009_a_,u2009_b_ (1u2009≤u2009_p_1,u2009_p_2,u2009_p_3,u2009_p_4u2009≤u20091000,u20090u2009≤u2009_a_u2009≤u2009_b_u2009≤u200931415). It is guaranteed that numbers _p_1,u2009_p_2,u2009_p_3,u2009_p_4 will be pairwise distinct. Output Output the number of integers in the given range that have the given property. | 1,100 | false | false | true | false | false | false | false | false | false | false | 9,631 |
1081A | Chouti was doing a competitive programming competition. However, after having all the problems accepted, he got bored and decided to invent some small games. He came up with the following game. The player has a positive integer $$$n$$$. Initially the value of $$$n$$$ equals to $$$v$$$ and the player is able to do the following operation as many times as the player want (possibly zero): choose a positive integer $$$x$$$ that $$$x<n$$$ and $$$x$$$ is not a divisor of $$$n$$$, then subtract $$$x$$$ from $$$n$$$. The goal of the player is to minimize the value of $$$n$$$ in the end. Soon, Chouti found the game trivial. Can you also beat the game? Input The input contains only one integer in the first line: $$$v$$$ ($$$1 le v le 10^9$$$), the initial value of $$$n$$$. Output Output a single integer, the minimum value of $$$n$$$ the player can get. Note In the first example, the player can choose $$$x=3$$$ in the first turn, then $$$n$$$ becomes $$$5$$$. He can then choose $$$x=4$$$ in the second turn to get $$$n=1$$$ as the result. There are other ways to get this minimum. However, for example, he cannot choose $$$x=2$$$ in the first turn because $$$2$$$ is a divisor of $$$8$$$. In the second example, since $$$n=1$$$ initially, the player can do nothing. | 800 | true | false | false | false | false | true | false | false | false | false | 5,323 |
323C | You are given two permutations _p_ and _q_, consisting of _n_ elements, and _m_ queries of the form: _l_1,u2009_r_1,u2009_l_2,u2009_r_2 (_l_1u2009≤u2009_r_1;xa0_l_2u2009≤u2009_r_2). The response for the query is the number of such integers from 1 to _n_, that their position in the first permutation is in segment [_l_1,u2009_r_1] (borders included), and position in the second permutation is in segment [_l_2,u2009_r_2] (borders included too). A permutation of _n_ elements is the sequence of _n_ distinct integers, each not less than 1 and not greater than _n_. Position of number _v_ (1u2009≤u2009_v_u2009≤u2009_n_) in permutation _g_1,u2009_g_2,u2009...,u2009_g__n_ is such number _i_, that _g__i_u2009=u2009_v_. Input The first line contains one integer _n_ (1u2009≤u2009_n_u2009≤u2009106), the number of elements in both permutations. The following line contains _n_ integers, separated with spaces: _p_1,u2009_p_2,u2009...,u2009_p__n_ (1u2009≤u2009_p__i_u2009≤u2009_n_). These are elements of the first permutation. The next line contains the second permutation _q_1,u2009_q_2,u2009...,u2009_q__n_ in same format. The following line contains an integer _m_ (1u2009≤u2009_m_u2009≤u20092·105), that is the number of queries. The following _m_ lines contain descriptions of queries one in a line. The description of the _i_-th query consists of four integers: _a_,u2009_b_,u2009_c_,u2009_d_ (1u2009≤u2009_a_,u2009_b_,u2009_c_,u2009_d_u2009≤u2009_n_). Query parameters _l_1,u2009_r_1,u2009_l_2,u2009_r_2 are obtained from the numbers _a_,u2009_b_,u2009_c_,u2009_d_ using the following algorithm: 1. Introduce variable _x_. If it is the first query, then the variable equals 0, else it equals the response for the previous query plus one. 2. Introduce function _f_(_z_)u2009=u2009((_z_u2009-u20091u2009+u2009_x_) _mod_ _n_)u2009+u20091. 3. Suppose _l_1u2009=u2009_min_(_f_(_a_),u2009_f_(_b_)),u2009_r_1u2009=u2009_max_(_f_(_a_),u2009_f_(_b_)),u2009_l_2u2009=u2009_min_(_f_(_c_),u2009_f_(_d_)),u2009_r_2u2009=u2009_max_(_f_(_c_),u2009_f_(_d_)). Output Print a response for each query in a separate line. Examples Input 4 4 3 2 1 2 3 4 1 3 1 2 3 4 1 3 2 1 1 4 2 3 | 2,400 | false | false | false | false | true | false | false | false | false | false | 8,542 |
649C | Statement is not available on English language На тренировку по подготовке к соревнованиям по программированию пришли _n_ команд. Тренер для каждой команды подобрал тренировку, комплект задач для _i_-й команды занимает _a__i_ страниц. В распоряжении тренера есть _x_ листов бумаги, у которых обе стороны чистые, и _y_ листов, у которых только одна сторона чистая. При печати условия на листе первого типа можно напечатать две страницы из условий задач, а при печати на листе второго типаxa0— только одну. Конечно, на листе нельзя печатать условия из двух разных комплектов задач. Обратите внимание, что при использовании листов, у которых обе стороны чистые, не обязательно печатать условие на обеих сторонах, одна из них может остаться чистой. Вам предстоит определить максимальное количество команд, которым тренер сможет напечатать комплекты задач целиком. Входные данные В первой строке входных данных следуют три целых числа _n_, _x_ и _y_ (1u2009≤u2009_n_u2009≤u2009200u2009000, 0u2009≤u2009_x_,u2009_y_u2009≤u2009109)xa0— количество команд, количество листов бумаги с двумя чистыми сторонами и количество листов бумаги с одной чистой стороной. Во второй строке входных данных следует последовательность из _n_ целых чисел _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u200910u2009000), где _i_-е число равно количеству страниц в комплекте задач для _i_-й команды. Выходные данные Выведите единственное целое числоxa0— максимальное количество команд, которым тренер сможет напечатать комплекты задач целиком. Примеры Входные данные 6 3 5 12 11 12 11 12 11 Примечание В первом тестовом примере можно напечатать оба комплекта задач. Один из возможных ответов — напечатать весь первый комплект задач на листах с одной чистой стороной (после этого останется 3 листа с двумя чистыми сторонами и 1 лист с одной чистой стороной), а второй комплект напечатать на трех листах с двумя чистыми сторонами. Во втором тестовом примере можно напечатать оба комплекта задач. Один из возможных ответов — напечатать первый комплект задач на двух листах с двумя чистыми сторонами (после этого останется 1 лист с двумя чистыми сторонами и 5 листов с одной чистой стороной), а второй комплект напечатать на одном листе с двумя чистыми сторонами и на пяти листах с одной чистой стороной. Таким образом, тренер использует все листы для печати. В третьем тестовом примере можно напечатать только один комплект задач (любой из трёх 11-страничных). Для печати 11-страничного комплекта задач будет израсходована вся бумага. | 1,500 | false | true | false | false | false | true | false | false | true | false | 7,220 |
1342B | Let's say string $$$s$$$ has period $$$k$$$ if $$$s_i = s_{i + k}$$$ for all $$$i$$$ from $$$1$$$ to $$$s - k$$$ ($$$s$$$ means length of string $$$s$$$) and $$$k$$$ is the minimum positive integer with this property. Some examples of a period: for $$$s$$$="0101" the period is $$$k=2$$$, for $$$s$$$="0000" the period is $$$k=1$$$, for $$$s$$$="010" the period is $$$k=2$$$, for $$$s$$$="0011" the period is $$$k=4$$$. You are given string $$$t$$$ consisting only of 0's and 1's and you need to find such string $$$s$$$ that: 1. String $$$s$$$ consists only of 0's and 1's; 2. The length of $$$s$$$ doesn't exceed $$$2 cdot t$$$; 3. String $$$t$$$ is a subsequence of string $$$s$$$; 4. String $$$s$$$ has smallest possible period among all strings that meet conditions 1—3. Let us recall that $$$t$$$ is a subsequence of $$$s$$$ if $$$t$$$ can be derived from $$$s$$$ by deleting zero or more elements (any) without changing the order of the remaining elements. For example, $$$t$$$="011" is a subsequence of $$$s$$$="10101". Input The first line contains single integer $$$T$$$ ($$$1 le T le 100$$$)xa0— the number of test cases. Next $$$T$$$ lines contain test cases — one per line. Each line contains string $$$t$$$ ($$$1 le t le 100$$$) consisting only of 0's and 1's. Output Print one string for each test case — string $$$s$$$ you needed to find. If there are multiple solutions print any one of them. Note In the first and second test cases, $$$s = t$$$ since it's already one of the optimal solutions. Answers have periods equal to $$$1$$$ and $$$2$$$, respectively. In the third test case, there are shorter optimal solutions, but it's okay since we don't need to minimize the string $$$s$$$. String $$$s$$$ has period equal to $$$1$$$. | 1,100 | false | false | false | false | false | true | false | false | false | false | 3,998 |
1025E | Vasya passes all exams! Despite expectations, Vasya is not tired, moreover, he is ready for new challenges. However, he does not want to work too hard on difficult problems. Vasya remembered that he has a not-so-hard puzzle: $$$m$$$ colored cubes are placed on a chessboard of size $$$n imes n$$$. The fact is that $$$m leq n$$$ and all cubes have distinct colors. Each cube occupies exactly one cell. Also, there is a designated cell for each cube on the board, the puzzle is to place each cube on its place. The cubes are fragile, so in one operation you only can move one cube onto one of four neighboring by side cells, if only it is empty. Vasya wants to be careful, so each operation takes exactly one second. Vasya used to train hard for VK Cup Final, so he can focus his attention on the puzzle for at most $$$3$$$ hours, that is $$$10800$$$ seconds. Help Vasya find such a sequence of operations that all cubes will be moved onto their designated places, and Vasya won't lose his attention. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq m leq n leq 50$$$). Each of the next $$$m$$$ lines contains two integers $$$x_i$$$, $$$y_i$$$ ($$$1 leq x_i, y_i leq n$$$), the initial positions of the cubes. The next $$$m$$$ lines describe the designated places for the cubes in the same format and order. It is guaranteed that all initial positions are distinct and all designated places are distinct, however, it is possible that some initial positions coincide with some final positions. Output In the first line print a single integer $$$k$$$ ($$$0 le k leq 10800$$$) — the number of operations Vasya should make. In each of the next $$$k$$$ lines you should describe one operation: print four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$, $$$y_2$$$, where $$$x_1, y_1$$$ is the position of the cube Vasya should move, and $$$x_2, y_2$$$ is the new position of the cube. The cells $$$x_1, y_1$$$ and $$$x_2, y_2$$$ should have a common side, the cell $$$x_2, y_2$$$ should be empty before the operation. We can show that there always exists at least one solution. If there are multiple solutions, print any of them. Examples Output 4 2 1 1 1 2 2 2 1 1 1 1 2 1 2 2 2 Input 4 3 2 2 2 3 3 3 3 2 2 2 2 3 Output 9 2 2 1 2 1 2 1 1 2 3 2 2 3 3 2 3 2 2 1 2 1 1 2 1 2 1 3 1 3 1 3 2 1 2 2 2 Note In the fourth example the printed sequence of movements (shown on the picture below) is valid, but not shortest. There is a solution in $$$3$$$ operations. | 2,700 | false | false | true | false | false | true | false | false | false | false | 5,579 |
234F | Vasya should paint a fence in front of his own cottage. The fence is a sequence of _n_ wooden boards arranged in a single row. Each board is a 1 centimeter wide rectangle. Let's number the board fence using numbers 1,u20092,u2009...,u2009_n_ from left to right. The height of the _i_-th board is _h__i_ centimeters. Vasya has a 1 centimeter wide brush and the paint of two colors, red and green. Of course, the amount of the paint is limited. Vasya counted the area he can paint each of the colors. It turned out that he can not paint over _a_ square centimeters of the fence red, and he can not paint over _b_ square centimeters green. Each board of the fence should be painted exactly one of the two colors. Perhaps Vasya won't need one of the colors. In addition, Vasya wants his fence to look smart. To do this, he should paint the fence so as to minimize the value that Vasya called the fence unattractiveness value. Vasya believes that two consecutive fence boards, painted different colors, look unattractive. The unattractiveness value of a fence is the total length of contact between the neighboring boards of various colors. To make the fence look nice, you need to minimize the value as low as possible. Your task is to find what is the minimum unattractiveness Vasya can get, if he paints his fence completely. The picture shows the fence, where the heights of boards (from left to right) are 2,3,2,4,3,1. The first and the fifth boards are painted red, the others are painted green. The first and the second boards have contact length 2, the fourth and fifth boards have contact length 3, the fifth and the sixth have contact length 1. Therefore, the unattractiveness of the given painted fence is 2+3+1=6. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009200) — the number of boards in Vasya's fence. The second line contains two integers _a_ and _b_ (0u2009≤u2009_a_,u2009_b_u2009≤u20094·104) — the area that can be painted red and the area that can be painted green, correspondingly. The third line contains a sequence of _n_ integers _h_1,u2009_h_2,u2009...,u2009_h__n_ (1u2009≤u2009_h__i_u2009≤u2009200) — the heights of the fence boards. All numbers in the lines are separated by single spaces. | 1,800 | false | false | false | true | false | false | false | false | false | false | 8,899 |
1296C | There is a robot on a coordinate plane. Initially, the robot is located at the point $$$(0, 0)$$$. Its path is described as a string $$$s$$$ of length $$$n$$$ consisting of characters 'L', 'R', 'U', 'D'. Each of these characters corresponds to some move: 'L' (left): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x - 1, y)$$$; 'R' (right): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x + 1, y)$$$; 'U' (up): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y + 1)$$$; 'D' (down): means that the robot moves from the point $$$(x, y)$$$ to the point $$$(x, y - 1)$$$. The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $$$(x_e, y_e)$$$, then after optimization (i.e. removing some single substring from $$$s$$$) the robot also ends its path at the point $$$(x_e, y_e)$$$. This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $$$s$$$). Recall that the substring of $$$s$$$ is such string that can be obtained from $$$s$$$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL". You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. The next $$$2t$$$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the length of the robot's path. The second line of the test case contains one string $$$s$$$ consisting of $$$n$$$ characters 'L', 'R', 'U', 'D' — the robot's path. 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. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $$$l$$$ and $$$r$$$ such that $$$1 le l le r le n$$$ — endpoints of the substring you remove. The value $$$r-l+1$$$ should be minimum possible. If there are several answers, print any of them. | 1,500 | false | false | true | false | true | false | false | false | false | false | 4,215 |
226D | Problem - 226D - 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 *2100 No tag edit access → Contest materials — the number of rows and the number of columns. Next _n_ lines follow, each contains _m_ integers: _j_-th integer in the _i_-th line is _a__i_,u2009_j_ (_a__i_,u2009_j_u2009≤u2009100), the number in the _i_-th row and _j_-th column of the table. The rows of the table numbered from 1 to _n_. The columns of the table numbered from 1 to _m_. Output In the first line print the number _a_ — the number of required applications of the first spell. Next print _a_ space-separated integers — the row numbers, you want to apply a spell. These row numbers must be distinct! In the second line print the number _b_ — the number of required applications of the second spell. Next print _b_ space-separated integers — the column numbers, you want to apply a spell. These column numbers must be distinct! If there are several solutions are allowed to print any of them. Examples Input 4 1 -1 -1 -1 -1 Output 4 1 2 3 4 0 Input 2 4 -1 -1 -1 2 1 1 1 1 Output 1 1 1 4 | 2,100 | false | true | false | false | false | true | false | false | false | false | 8,932 |
1007A | You are given an array of integers. Vasya can permute (change order) its integers. He wants to do it so that as many as possible integers will become on a place where a smaller integer used to stand. Help Vasya find the maximal number of such integers. For instance, if we are given an array $$$[10, 20, 30, 40]$$$, we can permute it so that it becomes $$$[20, 40, 10, 30]$$$. Then on the first and the second positions the integers became larger ($$$20>10$$$, $$$40>20$$$) and did not on the third and the fourth, so for this permutation, the number that Vasya wants to maximize equals $$$2$$$. Read the note for the first example, there is one more demonstrative test case. Help Vasya to permute integers in such way that the number of positions in a new array, where integers are greater than in the original one, is maximal. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 10^5$$$)xa0— the length of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$)xa0— the elements of the array. Output Print a single integerxa0— the maximal number of the array's elements which after a permutation will stand on the position where a smaller element stood in the initial array. Note In the first sample, one of the best permutations is $$$[1, 5, 5, 3, 10, 1, 1]$$$. On the positions from second to fifth the elements became larger, so the answer for this permutation is 4. In the second sample, there is no way to increase any element with a permutation, so the answer is 0. | 1,300 | true | false | false | false | true | false | false | false | true | false | 5,649 |
1777F | You are given an array $$$a$$$ consisting of $$$n$$$ non-negative integers. The numbness of a subarray $$$a_l, a_{l+1}, ldots, a_r$$$ (for arbitrary $$$l leq r$$$) is defined as $$$$$$max(a_l, a_{l+1}, ldots, a_r) oplus (a_l oplus a_{l+1} oplus ldots oplus a_r),$$$$$$ where $$$oplus$$$ denotes the . The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print one integer — the maximum numbness over all subarrays of the given array. Example Input 2 5 1 2 3 4 5 3 10 47 52 Note For the first test case, for the subarray $$$[3, 4, 5]$$$, its maximum value is $$$5$$$. Hence, its numbness is $$$3 oplus 4 oplus 5 oplus 5$$$ = $$$7$$$. This is the maximum possible numbness in this array. In the second test case the subarray $$$[47, 52]$$$ provides the maximum numbness. | 2,400 | false | false | false | false | true | false | false | false | false | false | 1,609 |
1630F | You are given an undirected graph of $$$n$$$ vertices indexed from $$$1$$$ to $$$n$$$, where vertex $$$i$$$ has a value $$$a_i$$$ assigned to it and all values $$$a_i$$$ are different. There is an edge between two vertices $$$u$$$ and $$$v$$$ if either $$$a_u$$$ divides $$$a_v$$$ or $$$a_v$$$ divides $$$a_u$$$. Find the minimum number of vertices to remove such that the remaining graph is bipartite, when you remove a vertex you remove all the edges incident to it. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 5cdot10^4$$$) — the number of vertices in the graph. The second line of each test case contains $$$n$$$ integers, the $$$i$$$-th of them is the value $$$a_i$$$ ($$$1 le a_i le 5cdot10^4$$$) assigned to the $$$i$$$-th vertex, all values $$$a_i$$$ are different. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5cdot10^4$$$. Output For each test case print a single integer — the minimum number of vertices to remove such that the remaining graph is bipartite. Example Input 4 4 8 4 2 1 4 30 2 3 5 5 12 4 6 2 3 10 85 195 5 39 3 13 266 154 14 2 Note In the first test case if we remove the vertices with values $$$1$$$ and $$$2$$$ we will obtain a bipartite graph, so the answer is $$$2$$$, it is impossible to remove less than $$$2$$$ vertices and still obtain a bipartite graph. test case #1 In the second test case we do not have to remove any vertex because the graph is already bipartite, so the answer is $$$0$$$. test case #2 In the third test case we only have to remove the vertex with value $$$12$$$, so the answer is $$$1$$$. test case #3 In the fourth test case we remove the vertices with values $$$2$$$ and $$$195$$$, so the answer is $$$2$$$. test case #4 | 3,400 | false | false | false | false | false | false | false | false | false | true | 2,461 |
188E | Problem - 188E - Codeforces =============== xa0 , "9" prints the lyrics of "99 Bottles of Beer" song, "+" increments the value stored in the internal accumulator. Instructions "H" and "Q" are case-sensitive and must be uppercase. The characters of the program which are not instructions are ignored. You are given a program written in HQ9+. You have to figure out whether executing this program will produce any output. Input The input will consist of a single line _p_ which will give a program in HQ9+. String _p_ will contain between 1 and 100 characters, inclusive. ASCII-code of each character of _p_ will be between 33 (exclamation mark) and 126 (tilde), inclusive. Output Output "YES", if executing the program will produce any output, and "NO" otherwise (quotes for clarity only). Examples Input Hello! Output YES Input Progopedia Output NO Note In the first case the program contains only one instruction — "H", which prints "Hello, World!". In the second case none of the program characters are language instructions. | 1,400 | false | false | true | false | false | false | false | false | false | false | 9,088 |
1789F | Serval loves Brain Power and his brain power problem. Serval defines that a string $$$T$$$ is powerful iff $$$T$$$ can be obtained by concatenating some string $$$T'$$$ multiple times. Formally speaking, $$$T$$$ is powerful iff there exist a string $$$T'$$$ and an integer $$$kgeq 2$$$ such that $$$$$$T=underbrace{T'+T'+dots+T'}_{k ext{ times}}$$$$$$ For example, gogogo is powerful because it can be obtained by concatenating go three times, but power is not powerful. Serval has a string $$$S$$$ consists of lowercase English letters. He is curious about the longest powerful subsequence of $$$S$$$, and he only needs you to find out the length of it. If all the non-empty subsequences of $$$S$$$ is not powerful, the answer is considered to be $$$0$$$. A string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by the deletion of several (possibly, zero or all) characters. Input The first line contains a single string $$$S$$$ ($$$Sleq 80$$$) consisting of lowercase English letters. Output Print a single integer — the length of the longest powerful subsequence of $$$S$$$. If all the non-empty subsequences of $$$S$$$ is not powerful, print $$$0$$$. Examples Input oooooooooooaaaaeaaiaujooooooooooooo Note For the first sample, all of the non-empty subsequences of buaa are listed below: b u a (occurred twice, buaa and buaa) bu ba (occurred twice, buaa and buaa) ua (occurred twice, buaa and buaa) aa bua (occurred twice, buaa and buaa ) baa uaa buaa Since $$$ exttt{aa} = exttt{a} + exttt{a}$$$, aa is a powerful subsequence. It can be shown that aa is the only powerful subsequence among them. So the answer is $$$2$$$. For the second sample, the longest powerful subsequence is codcod from codeforcesround. So the answer is $$$6$$$. | 2,700 | false | true | true | true | false | false | true | false | false | false | 1,543 |
1692D | Victor has a . He looks at the clock every $$$x$$$ minutes, and the clock is currently showing time $$$s$$$. How many different palindromes will Victor see in total after looking at the clock every $$$x$$$ minutes, the first time being at time $$$s$$$? For example, if the clock starts out as 03:12 and Victor looks at the clock every $$$360$$$ minutes (i.e. every $$$6$$$ hours), then he will see the times 03:12, 09:12, 15:12, 21:12, 03:12, and the times will continue to repeat. Here the time 21:12 is the only palindrome he will ever see, so the answer is $$$1$$$. A palindrome is a string that reads the same backward as forward. For example, the times 12:21, 05:50, 11:11 are palindromes but 13:13, 22:10, 02:22 are not. Input The first line of the input contains an integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0— the number of test cases. The description of each test case follows. The only line of each test case contains a string $$$s$$$ of length $$$5$$$ with the format "HH:MM" where "HH" is from "00" to "23" and "MM" is from "00" to "59" (both "HH" and "MM" have exactly two digits) and an integer $$$x$$$ ($$$1 leq x leq 1440$$$)xa0— the number of minutes Victor takes to look again at the clock. | 1,100 | false | false | true | false | false | false | true | false | false | false | 2,127 |
618B | Bob has a permutation of integers from 1 to _n_. Denote this permutation as _p_. The _i_-th element of _p_ will be denoted as _p__i_. For all pairs of distinct integers _i_,u2009_j_ between 1 and _n_, he wrote the number _a__i_,u2009_j_u2009=u2009_min_(_p__i_,u2009_p__j_). He writes _a__i_,u2009_i_u2009=u20090 for all integer _i_ from 1 to _n_. Bob gave you all the values of _a__i_,u2009_j_ that he wrote down. Your job is to reconstruct any permutation that could have generated these values. The input will be formed so that it is guaranteed that there is at least one solution that is consistent with the information given. Input The first line of the input will contain a single integer _n_ (2u2009≤u2009_n_u2009≤u200950). The next _n_ lines will contain the values of _a__i_,u2009_j_. The _j_-th number on the _i_-th line will represent _a__i_,u2009_j_. The _i_-th number on the _i_-th line will be 0. It's guaranteed that _a__i_,u2009_j_u2009=u2009_a__j_,u2009_i_ and there is at least one solution consistent with the information given. Output Print _n_ space separated integers, which represents a permutation that could have generated these values. If there are multiple possible solutions, print any of them. Examples Input 5 0 2 2 1 2 2 0 4 1 3 2 4 0 1 3 1 1 1 0 1 2 3 3 1 0 Note In the first case, the answer can be {1,u20092} or {2,u20091}. In the second case, another possible answer is {2,u20094,u20095,u20091,u20093}. | 1,100 | false | false | false | false | false | true | false | false | false | false | 7,363 |
1051B | Problem - 1051B - 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 math number theory *1000 No tag edit access → Contest materials le 3 cdot 10^5$$$ and $$$(r - l)$$$ is always odd. You want to split these numbers into exactly $$$frac{r - l + 1}{2}$$$ pairs in such a way that for each pair $$$(i, j)$$$ the greatest common divisor of $$$i$$$ and $$$j$$$ is equal to $$$1$$$. Each number should appear in exactly one of the pairs. Print the resulting pairs or output that no solution exists. If there are multiple solutions, print any of them. Input The only line contains two integers $$$l$$$ and $$$r$$$ ($$$1 le l < r le 10^{18}$$$, $$$r - l + 1 le 3 cdot 10^5$$$, $$$(r - l)$$$ is odd). Output If any solution exists, print "YES" in the first line. Each of the next $$$frac{r - l + 1}{2}$$$ lines should contain some pair of integers. GCD of numbers in each pair should be equal to $$$1$$$. All $$$(r - l + 1)$$$ numbers should be pairwise distinct and should have values from $$$l$$$ to $$$r$$$ inclusive. If there are multiple solutions, print any of them. If there exists no solution, print "NO". Example Input 1 8 Output YES 2 7 4 1 3 8 6 5 | 1,000 | true | true | false | false | false | false | false | false | false | false | 5,454 |
1237B | Consider a tunnel on a one-way road. During a particular day, $$$n$$$ cars numbered from $$$1$$$ to $$$n$$$ entered and exited the tunnel exactly once. All the cars passed through the tunnel at constant speeds. A traffic enforcement camera is mounted at the tunnel entrance. Another traffic enforcement camera is mounted at the tunnel exit. Perfectly balanced. Thanks to the cameras, the order in which the cars entered and exited the tunnel is known. No two cars entered or exited at the same time. Traffic regulations prohibit overtaking inside the tunnel. If car $$$i$$$ overtakes any other car $$$j$$$ inside the tunnel, car $$$i$$$ must be fined. However, each car can be fined at most once. Formally, let's say that car $$$i$$$ definitely overtook car $$$j$$$ if car $$$i$$$ entered the tunnel later than car $$$j$$$ and exited the tunnel earlier than car $$$j$$$. Then, car $$$i$$$ must be fined if and only if it definitely overtook at least one other car. Find the number of cars that must be fined. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 10^5$$$), denoting the number of cars. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$), denoting the ids of cars in order of entering the tunnel. All $$$a_i$$$ are pairwise distinct. The third line contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 le b_i le n$$$), denoting the ids of cars in order of exiting the tunnel. All $$$b_i$$$ are pairwise distinct. Output Output the number of cars to be fined. Examples Input 5 3 5 2 1 4 4 3 2 5 1 Input 7 5 2 3 6 7 1 4 2 3 6 7 1 4 5 Note The first example is depicted below: Car $$$2$$$ definitely overtook car $$$5$$$, while car $$$4$$$ definitely overtook cars $$$1$$$, $$$2$$$, $$$3$$$ and $$$5$$$. Cars $$$2$$$ and $$$4$$$ must be fined. In the second example car $$$5$$$ was definitely overtaken by all other cars. In the third example no car must be fined. | 1,300 | false | false | false | false | true | false | false | false | true | false | 4,511 |
1157A | Problem - 1157A - 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 *1100 No tag edit access → Contest materials ") $$$ in such a way: we add $$$1$$$ to $$$x$$$, then, while there is at least one trailing zero in the resulting number, we remove that zero. For example, $$$f(599) = 6$$$: $$$599 + 1 = 600 ightarrow 60 ightarrow 6$$$; $$$f(7) = 8$$$: $$$7 + 1 = 8$$$; $$$f(9) = 1$$$: $$$9 + 1 = 10 ightarrow 1$$$; $$$f(10099) = 101$$$: $$$10099 + 1 = 10100 ightarrow 1010 ightarrow 101$$$. We say that some number $$$y$$$ is reachable from $$$x$$$ if we can apply function $$$f$$$ to $$$x$$$ some (possibly zero) times so that we get $$$y$$$ as a result. For example, $$$102$$$ is reachable from $$$10098$$$ because $$$f(f(f(10098))) = f(f(10099)) = f(101) = 102$$$; and any number is reachable from itself. You are given a number $$$n$$$; your task is to count how many different numbers are reachable from $$$n$$$. Input The first line contains one integer $$$n$$$ ($$$1 le n le 10^9$$$). Output Print one integer: the number of different numbers that are reachable from $$$n$$$. Examples Input 1098 Output 20 Input 10 Output 19 Note The numbers that are reachable from $$$1098$$$ are: $$$1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1098, 1099$$$. | 1,100 | false | false | true | false | false | false | false | false | false | false | 4,923 |
1901A | There is a road, which can be represented as a number line. You are located in the point $$$0$$$ of the number line, and you want to travel from the point $$$0$$$ to the point $$$x$$$, and back to the point $$$0$$$. You travel by car, which spends $$$1$$$ liter of gasoline per $$$1$$$ unit of distance travelled. When you start at the point $$$0$$$, your car is fully fueled (its gas tank contains the maximum possible amount of fuel). There are $$$n$$$ gas stations, located in points $$$a_1, a_2, dots, a_n$$$. When you arrive at a gas station, you fully refuel your car. Note that you can refuel only at gas stations, and there are no gas stations in points $$$0$$$ and $$$x$$$. You have to calculate the minimum possible volume of the gas tank in your car (in liters) that will allow you to travel from the point $$$0$$$ to the point $$$x$$$ and back to the point $$$0$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. Each test case consists of two lines: the first line contains two integers $$$n$$$ and $$$x$$$ ($$$1 le n le 50$$$; $$$2 le x le 100$$$); the second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 < a_1 < a_2 < dots < a_n < x$$$). Output For each test case, print one integer — the minimum possible volume of the gas tank in your car that will allow you to travel from the point $$$0$$$ to the point $$$x$$$ and back. Example Input 3 3 7 1 2 5 3 6 1 2 5 1 10 7 Note In the first test case of the example, if the car has a gas tank of $$$4$$$ liters, you can travel to $$$x$$$ and back as follows: travel to the point $$$1$$$, then your car's gas tank contains $$$3$$$ liters of fuel; refuel at the point $$$1$$$, then your car's gas tank contains $$$4$$$ liters of fuel; travel to the point $$$2$$$, then your car's gas tank contains $$$3$$$ liters of fuel; refuel at the point $$$2$$$, then your car's gas tank contains $$$4$$$ liters of fuel; travel to the point $$$5$$$, then your car's gas tank contains $$$1$$$ liter of fuel; refuel at the point $$$5$$$, then your car's gas tank contains $$$4$$$ liters of fuel; travel to the point $$$7$$$, then your car's gas tank contains $$$2$$$ liters of fuel; travel to the point $$$5$$$, then your car's gas tank contains $$$0$$$ liters of fuel; refuel at the point $$$5$$$, then your car's gas tank contains $$$4$$$ liters of fuel; travel to the point $$$2$$$, then your car's gas tank contains $$$1$$$ liter of fuel; refuel at the point $$$2$$$, then your car's gas tank contains $$$4$$$ liters of fuel; travel to the point $$$1$$$, then your car's gas tank contains $$$3$$$ liters of fuel; refuel at the point $$$1$$$, then your car's gas tank contains $$$4$$$ liters of fuel; travel to the point $$$0$$$, then your car's gas tank contains $$$3$$$ liters of fuel. | 800 | true | true | false | false | false | false | false | false | false | false | 909 |
19B | Problem - 19B - Codeforces =============== xa0 . In each of the following _n_ lines each item is described by a pair of numbers _t__i_, _c__i_ (0u2009≤u2009_t__i_u2009≤u20092000,u20091u2009≤u2009_c__i_u2009≤u2009109). If _t__i_ is 0, Bob won't be able to steal anything, while the checkout assistant is occupied with item _i_. Output Output one number — answer to the problem: what is the minimum amount of money that Bob will have to pay. Examples Input 4 2 10 0 20 1 5 1 3 Output 8 Input 3 0 1 0 10 0 100 Output 111 | 1,900 | false | false | false | true | false | false | false | false | false | false | 9,899 |
1620F | You are given a permutation $$$p$$$ consisting of $$$n$$$ integers $$$1, 2, dots, n$$$ (a permutation is an array where each element from $$$1$$$ to $$$n$$$ occurs exactly once). Let's call an array $$$a$$$ bipartite if the following undirected graph is bipartite: the graph consists of $$$n$$$ vertices; two vertices $$$i$$$ and $$$j$$$ are connected by an edge if $$$i < j$$$ and $$$a_i > a_j$$$. Your task is to find a bipartite array of integers $$$a$$$ of size $$$n$$$, such that $$$a_i = p_i$$$ or $$$a_i = -p_i$$$, or report that no such array exists. If there are multiple answers, print any of them. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 2 cdot 10^5$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^6$$$)xa0— the size of the permutation. The second line contains $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$. The sum of $$$n$$$ over all test cases doesn't exceed $$$10^6$$$. Output For each test case, print the answer in the following format. If such an array $$$a$$$ does not exist, print "NO" in a single line. Otherwise, print "YES" in the first line and $$$n$$$ integersxa0— array $$$a$$$ in the second line. Example Input 4 3 1 2 3 6 1 3 2 6 5 4 4 4 1 3 2 8 3 2 1 6 7 8 5 4 Output YES 1 2 3 NO YES -4 -1 -3 -2 YES -3 -2 1 6 7 -8 -5 -4 | 2,800 | false | true | false | true | false | false | false | false | false | false | 2,522 |
891C | For a connected undirected weighted graph _G_, MST (minimum spanning tree) is a subgraph of _G_ that contains all of _G_'s vertices, is a tree, and sum of its edges is minimum possible. You are given a graph _G_. If you run a MST algorithm on graph it would give you only one MST and it causes other edges to become jealous. You are given some queries, each query contains a set of edges of graph _G_, and you should determine whether there is a MST containing all these edges or not. Input The first line contains two integers _n_, _m_ (2u2009u2009≤u2009_n_,u2009_m_u2009u2009≤u20095·105, _n_u2009-u20091u2009≤u2009_m_)xa0— the number of vertices and edges in the graph and the number of queries. The _i_-th of the next _m_ lines contains three integers _u__i_, _v__i_, _w__i_ (_u__i_u2009≠u2009_v__i_, 1u2009≤u2009_w__i_u2009≤u20095·105)xa0— the endpoints and weight of the _i_-th edge. There can be more than one edges between two vertices. It's guaranteed that the given graph is connected. The next line contains a single integer _q_ (1u2009≤u2009_q_u2009≤u20095·105)xa0— the number of queries. _q_ lines follow, the _i_-th of them contains the _i_-th query. It starts with an integer _k__i_ (1u2009≤u2009_k__i_u2009≤u2009_n_u2009-u20091)xa0— the size of edges subset and continues with _k__i_ distinct space-separated integers from 1 to _m_xa0— the indices of the edges. It is guaranteed that the sum of _k__i_ for 1u2009≤u2009_i_u2009≤u2009_q_ does not exceed 5·105. Output For each query you should print "YES" (without quotes) if there's a MST containing these edges and "NO" (of course without quotes again) otherwise. Example Input 5 7 1 2 2 1 3 2 2 3 1 2 4 1 3 4 1 3 5 2 4 5 2 4 2 3 4 3 3 4 5 2 1 7 2 1 2 Note This is the graph of sample: Weight of minimum spanning tree on this graph is 6. MST with edges (1,u20093,u20094,u20096), contains all of edges from the first query, so answer on the first query is "YES". Edges from the second query form a cycle of length 3, so there is no spanning tree including these three edges. Thus, answer is "NO". | 2,300 | false | false | false | false | true | false | false | false | false | true | 6,186 |
1495F | There are $$$n$$$ squares drawn from left to right on the floor. The $$$i$$$-th square has three integers $$$p_i,a_i,b_i$$$, written on it. The sequence $$$p_1,p_2,dots,p_n$$$ forms a permutation. Each round you will start from the leftmost square $$$1$$$ and jump to the right. If you are now on the $$$i$$$-th square, you can do one of the following two operations: 1. Jump to the $$$i+1$$$-th square and pay the cost $$$a_i$$$. If $$$i=n$$$, then you can end the round and pay the cost $$$a_i$$$. 2. Jump to the $$$j$$$-th square and pay the cost $$$b_i$$$, where $$$j$$$ is the leftmost square that satisfies $$$j > i, p_j > p_i$$$. If there is no such $$$j$$$ then you can end the round and pay the cost $$$b_i$$$. There are $$$q$$$ rounds in the game. To make the game more difficult, you need to maintain a square set $$$S$$$ (initially it is empty). You must pass through these squares during the round (other squares can also be passed through). The square set $$$S$$$ for the $$$i$$$-th round is obtained by adding or removing a square from the square set for the $$$(i-1)$$$-th round. For each round find the minimum cost you should pay to end it. Input The first line contains two integers $$$n$$$, $$$q$$$ ($$$1le n,qle 2 cdot 10^5$$$) xa0— the number of squares and the number of rounds. The second line contains $$$n$$$ distinct integers $$$p_1,p_2,dots,p_n$$$ ($$$1le p_ile n$$$). It is guaranteed that the sequence $$$p_1,p_2,dots,p_n$$$ forms a permutation. The third line contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$-10^9le a_ile 10^9$$$). The fourth line contains $$$n$$$ integers $$$b_1,b_2,dots,b_n$$$ ($$$-10^9le b_ile 10^9$$$). Then $$$q$$$ lines follow, $$$i$$$-th of them contains a single integer $$$x_i$$$ ($$$1le x_ile n$$$). If $$$x_i$$$ was in the set $$$S$$$ on the $$$(i-1)$$$-th round you should remove it, otherwise, you should add it. Output Print $$$q$$$ lines, each of them should contain a single integer xa0— the minimum cost you should pay to end the corresponding round. Examples Input 3 2 2 1 3 10 -5 4 3 -2 3 1 2 Input 5 4 2 1 5 3 4 6 -5 3 -10 -1 0 3 2 7 2 1 2 3 2 Note Let's consider the character $$$T$$$ as the end of a round. Then we can draw two graphs for the first and the second test. In the first round of the first test, the set that you must pass through is $$${1}$$$. The path you can use is $$$1 o 3 o T$$$ and its cost is $$$6$$$. In the second round of the first test, the set that you must pass through is $$${1,2}$$$. The path you can use is $$$1 o 2 o 3 o T$$$ and its cost is $$$8$$$. | 3,300 | false | false | false | true | true | true | false | false | false | true | 3,191 |
144D | A country called Berland consists of _n_ cities, numbered with integer numbers from 1 to _n_. Some of them are connected by bidirectional roads. Each road has some length. There is a path from each city to any other one by these roads. According to some Super Duper Documents, Berland is protected by the Super Duper Missiles. The exact position of the Super Duper Secret Missile Silos is kept secret but Bob managed to get hold of the information. That information says that all silos are located exactly at a distance _l_ from the capital. The capital is located in the city with number _s_. The documents give the formal definition: the Super Duper Secret Missile Silo is located at some place (which is either city or a point on a road) if and only if the shortest distance from this place to the capital along the roads of the country equals exactly _l_. Bob wants to know how many missile silos are located in Berland to sell the information then to enemy spies. Help Bob. Input The first line contains three integers _n_, _m_ and _s_ (2u2009≤u2009_n_u2009≤u2009105, , 1u2009≤u2009_s_u2009≤u2009_n_) — the number of cities, the number of roads in the country and the number of the capital, correspondingly. Capital is the city no. _s_. Then _m_ lines contain the descriptions of roads. Each of them is described by three integers _v__i_, _u__i_, _w__i_ (1u2009≤u2009_v__i_,u2009_u__i_u2009≤u2009_n_, _v__i_u2009≠u2009_u__i_, 1u2009≤u2009_w__i_u2009≤u20091000), where _v__i_, _u__i_ are numbers of the cities connected by this road and _w__i_ is its length. The last input line contains integer _l_ (0u2009≤u2009_l_u2009≤u2009109) — the distance from the capital to the missile silos. It is guaranteed that: between any two cities no more than one road exists; each road connects two different cities; from each city there is at least one way to any other city by the roads. Output Print the single number — the number of Super Duper Secret Missile Silos that are located in Berland. Examples Input 4 6 1 1 2 1 1 3 3 2 3 1 2 4 1 3 4 1 1 4 2 2 Input 5 6 3 3 1 1 3 2 1 3 4 1 3 5 1 1 2 6 4 5 8 4 Note In the first sample the silos are located in cities 3 and 4 and on road (1,u20093) at a distance 2 from city 1 (correspondingly, at a distance 1 from city 3). In the second sample one missile silo is located right in the middle of the road (1,u20092). Two more silos are on the road (4,u20095) at a distance 3 from city 4 in the direction to city 5 and at a distance 3 from city 5 to city 4. | 1,900 | false | false | false | false | true | false | false | false | false | true | 9,297 |
1405B | You're given an array $$$a$$$ of $$$n$$$ integers, such that $$$a_1 + a_2 + cdots + a_n = 0$$$. In one operation, you can choose two different indices $$$i$$$ and $$$j$$$ ($$$1 le i, j le n$$$), decrement $$$a_i$$$ by one and increment $$$a_j$$$ by one. If $$$i < j$$$ this operation is free, otherwise it costs one coin. How many coins do you have to spend in order to make all elements equal to $$$0$$$? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 5000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 10^5$$$) xa0— the number of elements. The next line contains $$$n$$$ integers $$$a_1, ldots, a_n$$$ ($$$-10^9 le a_i le 10^9$$$). It is given that $$$sum_{i=1}^n a_i = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, print the minimum number of coins we have to spend in order to make all elements equal to $$$0$$$. Example Input 7 4 -3 5 -3 1 2 1 -1 4 -3 2 -3 4 4 -1 1 1 -1 7 -5 7 -6 -4 17 -13 4 6 -1000000000 -1000000000 -1000000000 1000000000 1000000000 1000000000 1 0 Output 3 0 4 1 8 3000000000 0 Note Possible strategy for the first test case: Do $$$(i=2, j=3)$$$ three times (free), $$$a = [-3, 2, 0, 1]$$$. Do $$$(i=2, j=1)$$$ two times (pay two coins), $$$a = [-1, 0, 0, 1]$$$. Do $$$(i=4, j=1)$$$ one time (pay one coin), $$$a = [0, 0, 0, 0]$$$. | 1,000 | false | false | true | false | false | true | false | false | false | false | 3,647 |
359C | Problem - 359C - 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 math number theory *1900 No tag edit access → Contest materials ") ") . Input The first line contains two positive integers _n_ and _x_ (1u2009≤u2009_n_u2009≤u2009105, 2u2009≤u2009_x_u2009≤u2009109) — the size of the array and the prime number. The second line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a_1u2009≤u2009_a_2u2009≤u2009...u2009≤u2009_a__n_u2009≤u2009109). Output Print a single number — the answer to the problem modulo 1000000007 (109u2009+u20097). Examples Input 2 2 2 2 Output 8 Input 3 3 1 2 3 Output 27 Input 2 2 29 29 Output 73741817 Input 4 5 0 0 0 0 Output 1 Note In the first sample . Thus, the answer to the problem is 8. In the second sample, . The answer to the problem is 27, as 351u2009=u200913·27, 729u2009=u200927·27. In the third sample the answer to the problem is 1073741824xa0_mod_xa01000000007u2009=u200973741817. In the fourth sample . Thus, the answer to the problem is 1. | 1,900 | true | false | false | false | false | false | false | false | false | false | 8,397 |
412E | One of the most important products of the R1 company is a popular @r1.com mail service. The R1 mailboxes receive and send millions of emails every day. Today, the online news thundered with terrible information. The R1 database crashed and almost no data could be saved except for one big string. The developers assume that the string contains the letters of some users of the R1 mail. Recovering letters is a tedious mostly manual work. So before you start this process, it was decided to estimate the difficulty of recovering. Namely, we need to calculate the number of different substrings of the saved string that form correct e-mail addresses. We assume that valid addresses are only the e-mail addresses which meet the following criteria: the address should begin with a non-empty sequence of letters, numbers, characters '_', starting with a letter; then must go character '@'; then must go a non-empty sequence of letters or numbers; then must go character '.'; the address must end with a non-empty sequence of letters. You got lucky again and the job was entrusted to you! Please note that the substring is several consecutive characters in a string. Two substrings, one consisting of the characters of the string with numbers _l_1,u2009_l_1u2009+u20091,u2009_l_1u2009+u20092,u2009...,u2009_r_1 and the other one consisting of the characters of the string with numbers _l_2,u2009_l_2u2009+u20091,u2009_l_2u2009+u20092,u2009...,u2009_r_2, are considered distinct if _l_1u2009≠u2009_l_2 or _r_1u2009≠u2009_r_2. Input The first and the only line contains the sequence of characters _s_1_s_2... _s__n_ (1u2009≤u2009_n_u2009≤u2009106) — the saved string. It is guaranteed that the given string contains only small English letters, digits and characters '.', '_', '@'. Output Print in a single line the number of substrings that are valid e-mail addresses. Examples Input gerald.agapov1991@gmail.com Note In the first test case all the substrings that are correct e-mail addresses begin from one of the letters of the word agapov and end in one of the letters of the word com. In the second test case note that the e-mail x@x.x is considered twice in the answer. Note that in this example the e-mail entries overlap inside the string. | 1,900 | false | false | true | false | false | false | false | false | false | false | 8,175 |
1970G2 | In the medium version, $$$2 leq sum n leq 300$$$ and $$$1 leq sum m leq 300$$$ After a worker's strike organized by the Dementors asking for equal rights, the prison of Azkaban has suffered some damage. After settling the spirits, the Ministry of Magic is looking to renovate the prison to ensure that the Dementors are kept in check. The prison consists of $$$n$$$ prison cells and $$$m$$$ bi-directional corridors. The $$$i^{th}$$$ corridor is from cells $$$u_i$$$ to $$$v_i$$$. A subset of these cells $$$S$$$ is called a complex if any cell in $$$S$$$ is reachable from any other cell in $$$S$$$. Formally, a subset of cells $$$S$$$ is a complex if $$$x$$$ and $$$y$$$ are reachable from each other for all $$$x, y in S$$$, using only cells from $$$S$$$ on the way. The funding required for a complex $$$S$$$ consisting of $$$k$$$ cells is defined as $$$k^2$$$. As part of your Intro to Magical Interior Design course at Hogwarts, you have been tasked with designing the prison. The Ministry of Magic has asked that you divide the prison into $$$2$$$ complexes with $$$ extbf{exactly one corridor}$$$ connecting them, so that the Dementors can't organize union meetings. For this purpose, you are allowed to build bi-directional corridors. The funding required to build a corridor between any $$$2$$$ cells is $$$c$$$. Due to budget cuts and the ongoing fight against the Death Eaters, you must find the $$$ extbf{minimum total funding}$$$ required to divide the prison as per the Ministry's requirements or $$$-1$$$ if no division is possible. Note: The total funding is the sum of the funding required for the $$$2$$$ complexes and the corridors built. If after the division, the two complexes have $$$x$$$ and $$$y$$$ cells respectively and you have built a total of $$$a$$$ corridors, the total funding will be $$$x^2 + y^2 + c imes a$$$. Note that $$$x+y=n$$$. Input The first line contains one integer $$$t$$$ ($$$1 leq t leq 300$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case consists of three integers $$$n, m$$$ and $$$c$$$ ($$$2 leq n leq 300$$$, $$$1 leq m leq 300$$$, $$$1 leq c leq 10^9$$$) $$$m$$$ lines follow, each consisting of 2 integers — $$$u_i, v_i$$$ indicating a corridor is present between cells $$$u_i$$$ and $$$v_i$$$ ($$$1 leq u_i, v_i leq n$$$, $$$u_i eq v_i$$$) It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$300$$$. It is guaranteed that the sum of $$$m$$$ over all test cases does not exceed $$$300$$$. It is guaranteed that there exists at most one corridor between any two cells. Output Print the $$$ extbf{minimum funding}$$$ required to divide the prison as per the Ministry's requirements or $$$-1$$$ if no division is possible. Example Input 4 4 6 5 4 3 2 3 2 4 1 2 4 1 3 1 6 6 2 1 4 2 5 3 6 1 5 3 5 6 5 6 5 7 1 4 2 5 3 6 3 5 6 5 7 5 4 1 4 3 6 3 5 6 5 2 7 Note In the first test case of the sample input, there is no way to divide the prison according to the Ministry's requirements. In the second test case, consider the corridor between cells $$$1$$$ and $$$5$$$ as the connection between the $$$2$$$ complexes consisting of $$${2, 3, 5, 6}$$$ and $$${1, 4}$$$ cells respectively. There are no new corridors built. The total funding is $$$4^2 + 2^2 = 20$$$. You can verify this is the minimum funding required. In the third test case, build a corridor between $$$2$$$ and $$$4$$$. Consider the corridor between cells $$$1$$$ and $$$5$$$ as the connection between the $$$2$$$ complexes consisting of $$${3, 5, 6}$$$ and $$${1, 2, 4}$$$ cells respectively. The total funding is $$$3^2 + 3^2 + 7 imes 1 = 25$$$. You can verify this is the minimum funding required. In the fourth test case, build a corridor between $$$2$$$ and $$$4$$$ and between $$$5$$$ and $$$7$$$. Consider the corridor between cells $$$5$$$ and $$$7$$$ as the connection between the $$$2$$$ complexes consisting of $$${1, 2, 4, 7}$$$ and $$${3, 5, 6}$$$ cells respectively. The total funding is $$$4^2 + 3^2 + 4 imes 2 = 33$$$. You can verify this is the minimum funding required. Note for all test cases that there may be multiple ways to get the same funding but there is no other division which will have a more optimal minimum funding. | 2,200 | false | false | false | true | false | false | true | false | false | true | 467 |
685D | Snow Queen told Kay to form a word "eternity" using pieces of ice. Kay is eager to deal with the task, because he will then become free, and Snow Queen will give him all the world and a pair of skates. Behind the palace of the Snow Queen there is an infinite field consisting of cells. There are _n_ pieces of ice spread over the field, each piece occupying exactly one cell and no two pieces occupying the same cell. To estimate the difficulty of the task Kay looks at some squares of size _k_u2009×u2009_k_ cells, with corners located at the corners of the cells and sides parallel to coordinate axis and counts the number of pieces of the ice inside them. This method gives an estimation of the difficulty of some part of the field. However, Kay also wants to estimate the total difficulty, so he came up with the following criteria: for each _x_ (1u2009≤u2009_x_u2009≤u2009_n_) he wants to count the number of squares of size _k_u2009×u2009_k_, such that there are exactly _x_ pieces of the ice inside. Please, help Kay estimate the difficulty of the task given by the Snow Queen. Input The first line of the input contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009100u2009000, 1u2009≤u2009_k_u2009≤u2009300)xa0— the number of pieces of the ice and the value _k_, respectively. Each of the next _n_ lines contains two integers _x__i_ and _y__i_ (u2009-u2009109u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009109)xa0— coordinates of the cell containing _i_-th piece of the ice. It's guaranteed, that no two pieces of the ice occupy the same cell. Output Print _n_ integers: the number of squares of size _k_u2009×u2009_k_ containing exactly 1,u20092,u2009...,u2009_n_ pieces of the ice. | 2,600 | false | false | true | false | false | false | true | false | true | false | 7,080 |
1744A | An integer array $$$a_1, a_2, ldots, a_n$$$ is being transformed into an array of lowercase English letters using the following prodecure: While there is at least one number in the array: Choose any number $$$x$$$ from the array $$$a$$$, and any letter of the English alphabet $$$y$$$. Replace all occurrences of number $$$x$$$ with the letter $$$y$$$. For example, if we initially had an array $$$a = [2, 3, 2, 4, 1]$$$, then we could transform it the following way: Choose the number $$$2$$$ and the letter c. After that $$$a = [c, 3, c, 4, 1]$$$. Choose the number $$$3$$$ and the letter a. After that $$$a = [c, a, c, 4, 1]$$$. Choose the number $$$4$$$ and the letter t. After that $$$a = [c, a, c, t, 1]$$$. Choose the number $$$1$$$ and the letter a. After that $$$a = [c, a, c, t, a]$$$. After the transformation all letters are united into a string, in our example we get the string "cacta". Having the array $$$a$$$ and the string $$$s$$$ determine if the string $$$s$$$ could be got from the array $$$a$$$ after the described transformation? Input The first line contains a single integer $$$t$$$ $$$(1 leq t leq 10^3$$$) — the number of test cases. Then the description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 50$$$) — the length of the array $$$a$$$ and the string $$$s$$$. The second line of each test case contains exactly $$$n$$$ integers: $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 50$$$) — the elements of the array $$$a$$$. The third line of each test case contains a string $$$s$$$ of length $$$n$$$, consisting of lowercase English letters. Output For each test case, output "YES", if we can get the string $$$s$$$ from the array $$$a$$$, and "NO" otherwise. You can output each letter in any case. Example Input 7 5 2 3 2 4 1 cacta 1 50 a 2 11 22 ab 4 1 2 2 1 aaab 5 1 2 3 2 1 aaaaa 6 1 10 2 9 3 8 azzfdb 7 1 2 3 4 1 1 2 abababb Output YES YES YES NO YES YES NO Note The first test case corresponds to the sample described in the statement. In the second test case we can choose the number $$$50$$$ and the letter a. In the third test case we can choose the number $$$11$$$ and the letter a, after that $$$a = [a, 22]$$$. Then we choose the number $$$22$$$ and the letter b and get $$$a = [a, b]$$$. In the fifth test case we can change all numbers one by one to the letter a. | 800 | false | true | true | false | false | false | false | false | false | false | 1,817 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.