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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
195B | Problem - 195B - 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 data structures implementation math *1300 No tag edit access β Contest materials ") Π Π°Π·Π±ΠΎΡ ΠΠ°Π΄Π°Ρ") β the number of balls and baskets, correspondingly. Output Print _n_ numbers, one per line. The _i_-th line must contain the number of the basket for the _i_-th ball. Examples Input 4 3 Output 2 1 3 2 Input 3 1 Output 1 1 1 | 1,300 | true | false | true | false | true | false | false | false | false | false | 9,062 |
1749F | You are given a tree consisting of $$$n$$$ vertices. Initially, each vertex has a value $$$0$$$. You need to perform $$$m$$$ queries of two types: 1. You are given a vertex index $$$v$$$. Print the value of the vertex $$$v$$$. 2. You are given two vertex indices $$$u$$$ and $$$v$$$ and values $$$k$$$ and $$$d$$$ ($$$d le 20$$$). You need to add $$$k$$$ to the value of each vertex such that the distance from that vertex to the path from $$$u$$$ to $$$v$$$ is less than or equal to $$$d$$$. The distance between two vertices $$$x$$$ and $$$y$$$ is equal to the number of edges on the path from $$$x$$$ to $$$y$$$. For example, the distance from $$$x$$$ to $$$x$$$ itself is equal to $$$0$$$. The distance from the vertex $$$v$$$ to some path from $$$x$$$ to $$$y$$$ is equal to the minimum among distances from $$$v$$$ to any vertex on the path from $$$x$$$ to $$$y$$$. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0β the number of vertices in the tree. Next $$$n - 1$$$ lines contain the edges of the treexa0β one per line. Each line contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$; $$$u eq v$$$) representing one edge of the tree. It's guaranteed that the given edges form a tree. The next line contains a single integer $$$m$$$ ($$$1 le m le 2 cdot 10^5$$$)xa0β the number of queries. Next $$$m$$$ lines contain the queriesxa0β one per line. Each query has one of the following two types: $$$1$$$ $$$v$$$ ($$$1 le v le n$$$)xa0β the query of the first type; $$$2$$$ $$$u$$$ $$$v$$$ $$$k$$$ $$$d$$$ ($$$1 le u, v le n$$$; $$$1 le k le 1000$$$; $$$0 le d le 20$$$)xa0β the query of the second type. Additional constraint on the input: there is at least one query of the first type. Output For each query of the first type, print the value of the corresponding vertex. Example Input 6 1 2 1 3 4 2 5 2 3 6 14 2 4 5 10 2 1 3 1 6 2 1 1 10 20 2 6 6 10 20 1 3 2 3 2 10 0 2 5 2 10 1 1 1 1 2 1 3 1 4 1 5 1 6 Output 10 0 30 50 50 40 40 40 20 Note The tree from the first example: Some query explanations: "$$$2$$$ $$$4$$$ $$$5$$$ $$$10$$$ $$$2$$$": affected vertices are $$${4, 2, 5, 1, 3}$$$; "$$$2$$$ $$$1$$$ $$$1$$$ $$$10$$$ $$$20$$$" and "$$$2$$$ $$$6$$$ $$$6$$$ $$$10$$$ $$$20$$$": all vertices are affected, since distance to $$$1$$$ ($$$6$$$) is less that $$$20$$$ for any vertex; "$$$2$$$ $$$3$$$ $$$2$$$ $$$10$$$ $$$0$$$": affected vertices are $$${3, 1, 2}$$$; "$$$2$$$ $$$5$$$ $$$2$$$ $$$10$$$ $$$1$$$": affected vertices are $$${5, 2, 4, 1}$$$. | 2,800 | false | false | false | false | true | false | false | false | false | false | 1,786 |
70C | In Walrusland public transport tickets are characterized by two integers: by the number of the series and by the number of the ticket in the series. Let the series number be represented by _a_ and the ticket number β by _b_, then a ticket is described by the ordered pair of numbers (_a_,u2009_b_). The walruses believe that a ticket is lucky if _a_u2009*u2009_b_u2009=u2009_rev_(_a_)u2009*u2009_rev_(_b_). The function _rev_(_x_) reverses a number written in the decimal system, at that the leading zeroes disappear. For example, _rev_(12343)u2009=u200934321, _rev_(1200)u2009=u200921. The Public Transport Management Committee wants to release _x_ series, each containing _y_ tickets, so that at least _w_ lucky tickets were released and the total number of released tickets (_x_u2009*u2009_y_) were minimum. The series are numbered from 1 to _x_ inclusive. The tickets in each series are numbered from 1 to _y_ inclusive. The Transport Committee cannot release more than _max__x_ series and more than _max__y_ tickets in one series. Input The first line contains three integers _max__x_, _max__y_, _w_ (1u2009β€u2009_max__x_,u2009_max__y_u2009β€u2009105, 1u2009β€u2009_w_u2009β€u2009107). Output Print on a single line two space-separated numbers, the _x_ and the _y_. If there are several possible variants, print any of them. If such _x_ and _y_ do not exist, print a single number u2009-u20091. | 2,200 | false | false | false | false | true | false | false | true | true | false | 9,619 |
840D | Problem - 840D - 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 data structures probabilities *2500 No tag edit access β Contest materials β number of elements in the array and number of queries respectively. Next line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009β€u2009_a__i_u2009β€u2009_n_) β Leha's array. Each of next _q_ lines contains three integers _l_, _r_ and _k_ (1u2009β€u2009_l_u2009β€u2009_r_u2009β€u2009_n_,u20092u2009β€u2009_k_u2009β€u20095) β description of the queries. Output Output answer for each query in new line. Examples Input 4 2 1 1 2 2 1 3 2 1 4 2 Output 1 -1 Input 5 3 1 2 1 3 2 2 5 3 1 2 3 5 5 2 Output 2 1 2 | 2,500 | false | false | false | false | true | false | false | false | false | false | 6,400 |
1735A | Your working week consists of $$$n$$$ days numbered from $$$1$$$ to $$$n$$$, after day $$$n$$$ goes day $$$1$$$ again. And $$$3$$$ of them are days off. One of the days off is the last day, day $$$n$$$. You have to decide when the other two are. Choosing days off, you pursue two goals: No two days should go one after the other. Note that you can't make day $$$1$$$ a day off because it follows day $$$n$$$. Working segments framed by days off should be as dissimilar as possible in duration. More specifically, if the segments are of size $$$l_1$$$, $$$l_2$$$, and $$$l_3$$$ days long, you want to maximize $$$min(l_1 - l_2, l_2 - l_3, l_3 - l_1)$$$. Output the maximum value of $$$min(l_1 - l_2, l_2 - l_3, l_3 - l_1)$$$ that can be obtained. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 1000$$$) β the number of test cases. The description of test cases follows. The only line of each test case contains the integer $$$n$$$ ($$$6 le n le 10^9$$$). Output For each test case, output one integer β the maximum possible obtained value. Note In the image below you can see the example solutions for the first two test cases. Chosen days off are shown in purple. Working segments are underlined in green. In test case $$$1$$$, the only options for days off are days $$$2$$$, $$$3$$$, and $$$4$$$ (because $$$1$$$ and $$$5$$$ are next to day $$$n$$$). So the only way to place them without selecting neighboring days is to choose days $$$2$$$ and $$$4$$$. Thus, $$$l_1 = l_2 = l_3 = 1$$$, and the answer $$$min(l_1 - l_2, l_2 - l_3, l_3 - l_1) = 0$$$. For test case $$$2$$$, one possible way to choose days off is shown. The working segments have the lengths of $$$2$$$, $$$1$$$, and $$$4$$$ days. So the minimum difference is $$$1 = min(1, 3, 2) = min(2 - 1, 1 - 4, 4 - 2)$$$. It can be shown that there is no way to make it larger. | 800 | true | true | false | false | false | true | false | false | false | false | 1,880 |
69D | Anton and Dasha like to play different games during breaks on checkered paper. By the 11th grade they managed to play all the games of this type and asked Vova the programmer to come up with a new game. Vova suggested to them to play a game under the code name "dot" with the following rules: On the checkered paper a coordinate system is drawn. A dot is initially put in the position (_x_,u2009_y_). A move is shifting a dot to one of the pre-selected vectors. Also each player can once per game symmetrically reflect a dot relatively to the line _y_u2009=u2009_x_. Anton and Dasha take turns. Anton goes first. The player after whose move the distance from the dot to the coordinates' origin exceeds _d_, loses. Help them to determine the winner. Input The first line of the input file contains 4 integers _x_, _y_, _n_, _d_ (u2009-u2009200u2009β€u2009_x_,u2009_y_u2009β€u2009200,u20091u2009β€u2009_d_u2009β€u2009200,u20091u2009β€u2009_n_u2009β€u200920) β the initial coordinates of the dot, the distance _d_ and the number of vectors. It is guaranteed that the initial dot is at the distance less than _d_ from the origin of the coordinates. The following _n_ lines each contain two non-negative numbers _x__i_ and _y__i_ (0u2009β€u2009_x__i_,u2009_y__i_u2009β€u2009200) β the coordinates of the i-th vector. It is guaranteed that all the vectors are nonzero and different. Output You should print "Anton", if the winner is Anton in case of both players play the game optimally, and "Dasha" otherwise. Note In the first test, Anton goes to the vector (1;2), and Dasha loses. In the second test Dasha with her first move shifts the dot so that its coordinates are (2;3), and Anton loses, as he has the only possible move β to reflect relatively to the line _y_u2009=u2009_x_. Dasha will respond to it with the same move and return the dot in position (2;3). | 1,900 | false | false | false | true | false | false | false | false | false | false | 9,623 |
1914F | BerSoft is the biggest IT corporation in Berland. There are $$$n$$$ employees at BerSoft company, numbered from $$$1$$$ to $$$n$$$. The first employee is the head of the company, and he does not have any superiors. Every other employee $$$i$$$ has exactly one direct superior $$$p_i$$$. Employee $$$x$$$ is considered to be a superior (direct or indirect) of employee $$$y$$$ if one of the following conditions holds: employee $$$x$$$ is the direct superior of employee $$$y$$$; employee $$$x$$$ is a superior of the direct superior of employee $$$y$$$. The structure of BerSoft is organized in such a way that the head of the company is superior of every employee. A programming competition is going to be held soon. Two-person teams should be created for this purpose. However, if one employee in a team is the superior of another, they are uncomfortable together. So, teams of two people should be created so that no one is the superior of the other. Note that no employee can participate in more than one team. Your task is to calculate the maximum possible number of teams according to the aforementioned rules. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0β the number of employees. The second line contains $$$n-1$$$ integers $$$p_2, p_3, dots, p_n$$$ ($$$1 le p_i le n$$$), where $$$p_i$$$ is the index of the direct superior of the $$$i$$$-th employee. The sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print a single integerxa0β the maximum possible number of teams according to the aforementioned rules. Example Input 6 4 1 2 1 2 1 5 5 5 5 1 7 1 2 1 1 3 3 7 1 1 3 2 2 4 7 1 2 1 1 1 3 Note In the first test case, team $$$(3, 4)$$$ can be created. In the second test case, no team can be created, because there are only $$$2$$$ employees and one is the superior of another. In the third test case, team $$$(2, 3)$$$ can be created. In the fourth test case, teams $$$(2, 4)$$$, $$$(3, 5)$$$ and $$$(6, 7)$$$ can be created. In the fifth test case, teams $$$(2, 3)$$$, $$$(6, 4)$$$ and $$$(5, 7)$$$ can be created. | 1,900 | false | true | false | true | false | false | false | false | false | false | 813 |
66B | Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1u2009Γu2009_n_ in size, when viewed from above. This rectangle is divided into _n_ equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section. Creating artificial rain is an expensive operation. That's why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1u2009Γu20095 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture: As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him. Input The first line contains a positive integer _n_ (1u2009β€u2009_n_u2009β€u20091000). The second line contains _n_ positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000. Output Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section. | 1,100 | false | false | true | false | false | false | true | false | false | false | 9,640 |
1107G | Vasya got really tired of these credits (from problem F) and now wants to earn the money himself! He decided to make a contest to gain a profit. Vasya has $$$n$$$ problems to choose from. They are numbered from $$$1$$$ to $$$n$$$. The difficulty of the $$$i$$$-th problem is $$$d_i$$$. Moreover, the problems are given in the increasing order by their difficulties. The difficulties of all tasks are pairwise distinct. In order to add the $$$i$$$-th problem to the contest you need to pay $$$c_i$$$ burles to its author. For each problem in the contest Vasya gets $$$a$$$ burles. In order to create a contest he needs to choose a consecutive subsegment of tasks. So the total earnings for the contest are calculated as follows: if Vasya takes problem $$$i$$$ to the contest, he needs to pay $$$c_i$$$ to its author; for each problem in the contest Vasya gets $$$a$$$ burles; let $$$gap(l, r) = maxlimits_{l le i < r} (d_{i + 1} - d_i)^2$$$. If Vasya takes all the tasks with indices from $$$l$$$ to $$$r$$$ to the contest, he also needs to pay $$$gap(l, r)$$$. If $$$l = r$$$ then $$$gap(l, r) = 0$$$. Calculate the maximum profit that Vasya can earn by taking a consecutive segment of tasks. Input The first line contains two integers $$$n$$$ and $$$a$$$ ($$$1 le n le 3 cdot 10^5$$$, $$$1 le a le 10^9$$$) β the number of proposed tasks and the profit for a single problem, respectively. Each of the next $$$n$$$ lines contains two integers $$$d_i$$$ and $$$c_i$$$ ($$$1 le d_i, c_i le 10^9, d_i < d_{i+1}$$$). Output Print one integer β maximum amount of burles Vasya can earn. Examples Input 5 10 1 15 5 3 6 11 7 2 11 22 | 2,400 | false | false | false | true | true | true | false | true | false | false | 5,163 |
1324B | You are given an array $$$a$$$ consisting of $$$n$$$ integers. Your task is to determine if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome. Recall that an array $$$b$$$ is called a subsequence of the array $$$a$$$ if $$$b$$$ can be obtained by removing some (possibly, zero) elements from $$$a$$$ (not necessarily consecutive) without changing the order of remaining elements. For example, $$$[2]$$$, $$$[1, 2, 1, 3]$$$ and $$$[2, 3]$$$ are subsequences of $$$[1, 2, 1, 3]$$$, but $$$[1, 1, 2]$$$ and $$$[4]$$$ are not. Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array $$$a$$$ of length $$$n$$$ is the palindrome if $$$a_i = a_{n - i - 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$. For example, arrays $$$[1234]$$$, $$$[1, 2, 1]$$$, $$$[1, 3, 2, 2, 3, 1]$$$ and $$$[10, 100, 10]$$$ are palindromes, but arrays $$$[1, 2]$$$ and $$$[1, 2, 3, 1]$$$ are not. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 100$$$) β the number of test cases. Next $$$2t$$$ lines describe test cases. The first line of the test case contains one integer $$$n$$$ ($$$3 le n le 5000$$$) β the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$sum n le 5000$$$). Output For each test case, print the answer β "YES" (without quotes) if $$$a$$$ has some subsequence of length at least $$$3$$$ that is a palindrome and "NO" otherwise. Example Input 5 3 1 2 1 5 1 2 2 3 2 3 1 1 2 4 1 2 2 1 10 1 1 2 2 3 3 4 4 5 5 Note In the first test case of the example, the array $$$a$$$ has a subsequence $$$[1, 2, 1]$$$ which is a palindrome. In the second test case of the example, the array $$$a$$$ has two subsequences of length $$$3$$$ which are palindromes: $$$[2, 3, 2]$$$ and $$$[2, 2, 2]$$$. In the third test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. In the fourth test case of the example, the array $$$a$$$ has one subsequence of length $$$4$$$ which is a palindrome: $$$[1, 2, 2, 1]$$$ (and has two subsequences of length $$$3$$$ which are palindromes: both are $$$[1, 2, 1]$$$). In the fifth test case of the example, the array $$$a$$$ has no subsequences of length at least $$$3$$$ which are palindromes. | 1,100 | false | false | false | false | false | false | true | false | false | false | 4,098 |
1077F2 | The only difference between easy and hard versions is the constraints. Vova likes pictures with kittens. The news feed in the social network he uses can be represented as an array of $$$n$$$ consecutive pictures (with kittens, of course). Vova likes all these pictures, but some are more beautiful than the others: the $$$i$$$-th picture has beauty $$$a_i$$$. Vova wants to repost exactly $$$x$$$ pictures in such a way that: each segment of the news feed of at least $$$k$$$ consecutive pictures has at least one picture reposted by Vova; the sum of beauty values of reposted pictures is maximum possible. For example, if $$$k=1$$$ then Vova has to repost all the pictures in the news feed. If $$$k=2$$$ then Vova can skip some pictures, but between every pair of consecutive pictures Vova has to repost at least one of them. Your task is to calculate the maximum possible sum of values of reposted pictures if Vova follows conditions described above, or say that there is no way to satisfy all conditions. Input The first line of the input contains three integers $$$n, k$$$ and $$$x$$$ ($$$1 le k, x le n le 5000$$$) β the number of pictures in the news feed, the minimum length of segment with at least one repost in it and the number of pictures Vova is ready to repost. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$), where $$$a_i$$$ is the beauty of the $$$i$$$-th picture. Output Print -1 if there is no way to repost some pictures to satisfy all the conditions in the problem statement. Otherwise print one integer β the maximum sum of values of reposted pictures if Vova follows conditions described in the problem statement. Examples Input 6 1 5 10 30 30 70 10 10 | 2,100 | false | false | false | true | true | false | false | false | false | false | 5,331 |
1968E | Problem - 1968E - 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 *1600 No tag edit access β Contest materials ") , (x_2,y_2),dots,(x_n,y_n)$$$ in the grid $$$n imes n$$$ where $$$1le x_ile n$$$ and $$$1le y_ile n$$$. Let $$$mathcal{H}$$$ be the set of distinct Manhattan distances between any pair of cells. Your task is to maximize the size of $$$mathcal{H}$$$. Examples of sets and their construction are given in the notes. If there exists more than one solution, you are allowed to output any. Manhattan distance between cells $$$(x_1,y_1)$$$ and $$$(x_2,y_2)$$$ equals $$$x_1-x_2+y_1-y_2$$$. Input The first line contains a single integer $$$t$$$ ($$$1le tle 50$$$)xa0β the number of test cases. Each of the following $$$t$$$ lines contains a single integer $$$n$$$ ($$$2le nle 10^3$$$). Output For each test case, output $$$n$$$ points which maximize the size of $$$mathcal{H}$$$. It is not necessary to output an empty line at the end of the answer for each test case. Example Input 5 2 3 4 5 6 Output 1 1 1 2 2 1 2 3 3 1 1 1 1 3 4 3 4 4 1 1 1 3 1 4 2 1 5 5 1 4 1 5 1 6 5 2 5 5 6 1 Note In the first testcase we have $$$n=2$$$. One of the possible arrangements is: The arrangement with cells located in $$$(1,1)$$$ and $$$(1,2)$$$. In this case $$$mathcal{H}={1-1+1-1,1-1+2-2,1-1+1-2}={0,0,1}={0,1}$$$. Hence, the size of $$$mathcal{H}$$$ is $$$2$$$. It can be shown that it is the greatest possible answer.In the second testcase we have $$$n=3$$$. The optimal arrangement is: The arrangement with cells located in $$$(2,1)$$$, $$$(2,3)$$$ and $$$(3,1)$$$. $$$mathcal{H}$$$=$$${2-2+1-1,2-2+3-3,3-3+1-1,2-2+1-3,2-3+1-1,2-3+3-1}$$$=$$${0,0,0,2,1,3}$$$=$$${0,1,2,3}$$$. For $$$n=4$$$ a possible arrangement is: For $$$n=5$$$ a possible arrangement is: For $$$n=6$$$ a possible arrangement is: | 1,600 | false | false | false | false | false | true | false | false | false | false | 496 |
1198D | Problem - 1198D - 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 dp *2300 No tag edit access β Contest materials ") $$$ to color a rectangle of size $$$h imes w$$$. You are to make all cells white for minimum total cost. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 50$$$)xa0β the size of the square grid. Each of the next $$$n$$$ lines contains a string of length $$$n$$$, consisting of characters '.' and '#'. The $$$j$$$-th character of the $$$i$$$-th line is '#' if the cell with coordinates $$$(i, j)$$$ is black, otherwise it is white. Output Print a single integerxa0β the minimum total cost to paint all cells in white. Examples Input 3 ### #.# ### Output 3 Input 3 ... ... ... Output 0 Input 4 #... .... .... #... Output 2 Input 5 #...# .#.#. ..... .#... #.... Output 5 Note The examples and some of optimal solutions are shown on the pictures below. | 2,300 | false | false | false | true | false | false | false | false | false | false | 4,706 |
1454B | There is a game called "Unique Bid Auction". You can read more about it here: (though you don't have to do it to solve this problem). Let's simplify this game a bit. Formally, there are $$$n$$$ participants, the $$$i$$$-th participant chose the number $$$a_i$$$. The winner of the game is such a participant that the number he chose is unique (i.xa0e. nobody else chose this number except him) and is minimal (i.xa0e. among all unique values of $$$a$$$ the minimum one is the winning one). Your task is to find the index of the participant who won the game (or -1 if there is no winner). Indexing is $$$1$$$-based, i.xa0e. the participants are numbered from $$$1$$$ to $$$n$$$. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$) β the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) β the number of participants. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$), where $$$a_i$$$ is the $$$i$$$-th participant chosen number. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 cdot 10^5$$$ ($$$sum n le 2 cdot 10^5$$$). Output For each test case, print the answer β the index of the participant who won the game (or -1 if there is no winner). Note that the answer is always unique. Example Input 6 2 1 1 3 2 1 3 4 2 2 2 3 1 1 5 2 3 2 4 2 6 1 1 5 5 4 4 | 800 | false | false | true | false | false | false | false | false | false | false | 3,407 |
1476B | You have a statistic of price changes for one product represented as an array of $$$n$$$ positive integers $$$p_0, p_1, dots, p_{n - 1}$$$, where $$$p_0$$$ is the initial price of the product and $$$p_i$$$ is how the price was increased during the $$$i$$$-th month. Using these price changes you are asked to calculate the inflation coefficients for each month as the ratio of current price increase $$$p_i$$$ to the price at the start of this month $$$(p_0 + p_1 + dots + p_{i - 1})$$$. Your boss said you clearly that the inflation coefficients must not exceed $$$k$$$ %, so you decided to increase some values $$$p_i$$$ in such a way, that all $$$p_i$$$ remain integers and the inflation coefficients for each month don't exceed $$$k$$$ %. You know, that the bigger changesxa0β the more obvious cheating. That's why you need to minimize the total sum of changes. What's the minimum total sum of changes you need to make all inflation coefficients not more than $$$k$$$ %? 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 two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 100$$$; $$$1 le k le 100$$$)xa0β the length of array $$$p$$$ and coefficient $$$k$$$. The second line of each test case contains $$$n$$$ integers $$$p_0, p_1, dots, p_{n - 1}$$$ ($$$1 le p_i le 10^9$$$)xa0β the array $$$p$$$. Output For each test case, print the minimum total sum of changes you need to make all inflation coefficients not more than $$$k$$$ %. Example Input 2 4 1 20100 1 202 202 3 100 1 1 1 Note In the first test case, you can, for example, increase $$$p_0$$$ by $$$50$$$ and $$$p_1$$$ by $$$49$$$ and get array $$$[20150, 50, 202, 202]$$$. Then you get the next inflation coefficients: 1. $$$frac{50}{20150} le frac{1}{100}$$$; 2. $$$frac{202}{20150 + 50} le frac{1}{100}$$$; 3. $$$frac{202}{20200 + 202} le frac{1}{100}$$$; In the second test case, you don't need to modify array $$$p$$$, since the inflation coefficients are already good: 1. $$$frac{1}{1} le frac{100}{100}$$$; 2. $$$frac{1}{1 + 1} le frac{100}{100}$$$; | 1,300 | true | true | false | false | false | false | true | true | false | false | 3,296 |
2004G | Problem - 2004G - 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 data structures dp matrices *3200 No tag edit access β Contest materials β Video Tutorial") ") ; write the string $$$t_2$$$ $$$t_1$$$ times, then the string $$$t_4$$$ $$$t_3$$$ times, and so on. For example, for a string "12345", one could do the following: split it into ("1", "23", "4", "5"), and write "235555". Let the function $$$f(t)$$$ for a string $$$t$$$ return the minimum length of the string that can be obtained as a result of that process. You are given a string $$$s$$$, consisting of $$$n$$$ digits from $$$1$$$ to $$$9$$$, and an integer $$$k$$$. Calculate the value of the function $$$f$$$ for all contiguous substrings of $$$s$$$ of length exactly $$$k$$$. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 le k le n le 2 cdot 10^5$$$). The second line contains the string $$$s$$$ ($$$s = n$$$), consisting only of digits from $$$1$$$ to $$$9$$$. Output Output $$$n - k + 1$$$ integersxa0β $$$f(s_{1,k}), f(s_{2,k+1}), dots, f(s_{n - k + 1, n})$$$. Examples Input 4 4 5999 Output 14 Input 10 3 1111111111 Output 2 2 2 2 2 2 2 2 Input 11 4 49998641312 Output 12 18 17 15 12 7 7 2 | 3,200 | false | false | false | true | true | false | false | false | false | false | 225 |
1969E | You are given an integer array $$$a$$$ of length $$$n$$$. A subarray of $$$a$$$ is one of its contiguous subsequences (i.u2009e. an array $$$[a_l, a_{l+1}, dots, a_r]$$$ for some integers $$$l$$$ and $$$r$$$ such that $$$1 le l < r le n$$$). Let's call a subarray unique if there is an integer that occurs exactly once in the subarray. You can perform the following operation any number of times (possibly zero): choose an element of the array and replace it with any integer. Your task is to calculate the minimum number of aforementioned operation in order for all the subarrays of the array $$$a$$$ to be unique. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$). Additional constraint on the input: the sum of $$$n$$$ over all test cases doesn't exceed $$$3 cdot 10^5$$$. Output For each test case, print a single integerxa0β the minimum number of aforementioned operation in order for all the subarrays of the array $$$a$$$ to be unique. Example Input 4 3 2 1 2 4 4 4 4 4 5 3 1 2 1 2 5 1 3 2 1 2 Note In the second test case, you can replace the $$$1$$$-st and the $$$3$$$-rd element, for example, like this: $$$[3, 4, 1, 4]$$$. In the third test case, you can replace the $$$4$$$-th element, for example, like this: $$$[3, 1, 2, 3, 2]$$$. | 2,400 | false | true | false | true | true | false | false | true | false | false | 488 |
1774D | ChthollyNotaSeniorious received a special gift from AquaMoon: $$$n$$$ binary arrays of length $$$m$$$. AquaMoon tells him that in one operation, he can choose any two arrays and any position $$$pos$$$ from $$$1$$$ to $$$m$$$, and swap the elements at positions $$$pos$$$ in these arrays. He is fascinated with this game, and he wants to find the minimum number of operations needed to make the numbers of $$$1$$$s in all arrays the same. He has invited you to participate in this interesting game, so please try to find it! If it is possible, please output specific exchange steps in the format described in the output section. Otherwise, please output $$$-1$$$. Input The first line of the input contains a single integer $$$t$$$ ($$$1 leq t leq 2cdot 10^4$$$) xa0β the number of test cases. The description of test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 leq n leq 10^5$$$, $$$2 leq m leq 10^5$$$). The $$$i$$$-th of the following $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, 1}, a_{i, 2}, ldots, a_{i, m}$$$ $$$(0 le a_{i, j} le 1)$$$ xa0β the elements of the $$$i$$$-th array. It is guaranteed that the sum of $$$n cdot m$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, if the objective is not achievable, output $$$-1$$$. Otherwise, in the first line output $$$k$$$ $$$(0 le k le mn)$$$ xa0β the minimum number of operations required. The $$$i$$$-th of the following $$$k$$$ lines should contain $$$3$$$ integers, $$$x_i, y_i, z_i$$$ $$$(1 le x_i, y_i le n, 1 le z_i le m)$$$, which describe an operation that swap $$$a_{x_i, z_i}, a_{y_i, z_i}$$$: swap the $$$z_i$$$-th number of the $$$x_i$$$-th and $$$y_i$$$-th arrays. Example Input 3 3 4 1 1 1 0 0 0 1 0 1 0 0 1 4 3 1 0 0 0 1 1 0 0 1 0 0 0 2 2 0 0 0 1 Note In the first test case, it's enough to do a single operation: to swap the first element in the second and the first rows. The arrays will become $$$[0, 1, 1, 0], [1, 0, 1, 0], [1, 0, 0, 1]$$$, each of them contains exactly two $$$1$$$s. | 1,600 | false | true | true | false | false | true | true | false | false | false | 1,641 |
1023E | This is an interactive problem. Bob lives in a square grid of size $$$n imes n$$$, with rows numbered $$$1$$$ through $$$n$$$ from top to bottom, and columns numbered $$$1$$$ through $$$n$$$ from left to right. Every cell is either allowed or blocked, but you don't know the exact description of the grid. You are given only an integer $$$n$$$. Bob can move through allowed cells but only in some limited directions. When Bob is in an allowed cell in the grid, he can move down or right to an adjacent cell, if it is allowed. You can ask at most $$$4 cdot n$$$ queries of form "? $$$r_1$$$ $$$c_1$$$ $$$r_2$$$ $$$c_2$$$" ($$$1 le r_1 le r_2 le n$$$, $$$1 le c_1 le c_2 le n$$$). The answer will be "YES" if Bob can get from a cell $$$(r_1, c_1)$$$ to a cell $$$(r_2, c_2)$$$, and "NO" otherwise. In particular, if one of the two cells (or both) is a blocked cell then the answer is "NO" for sure. Since Bob doesn't like short trips, you can only ask queries with the manhattan distance between the two cells at least $$$n - 1$$$, i.e. the following condition must be satisfied: $$$(r_2 - r_1) + (c_2 - c_1) ge n - 1$$$. It's guaranteed that Bob can get from the top-left corner $$$(1, 1)$$$ to the bottom-right corner $$$(n, n)$$$ and your task is to find a way to do it. You should print the answer in form "! S" where $$$S$$$ is a string of length $$$2 cdot n - 2$$$ consisting of characters 'D' and 'R', denoting moves down and right respectively. The down move increases the first coordinate by $$$1$$$, the right move increases the second coordinate by $$$1$$$. If there are multiple solutions, any of them will be accepted. You should terminate immediately after printing the solution. Input The only line of the input contains an integer $$$n$$$ ($$$2 le n le 500$$$) β the size of the grid. Output When you are ready to print the answer, print a single line containing "! S" where where $$$S$$$ is a string of length $$$2 cdot n - 2$$$ consisting of characters 'D' and 'R', denoting moves down and right respectively. The path should be a valid path going from the cell $$$(1, 1)$$$ to the cell $$$(n, n)$$$ passing only through allowed cells. Interaction You can ask at most $$$4 cdot n$$$ queries. To ask a query, print a line containing "? $$$r_1$$$ $$$c_1$$$ $$$r_2$$$ $$$c_2$$$" ($$$1 le r_1 le r_2 le n$$$, $$$1 le c_1 le c_2 le n$$$). After that you should read a single line containing "YES" or "NO" depending on the answer of the query. "YES" means Bob can go from the cell $$$(r_1, c_1)$$$ to the cell $$$(r_2, c_2)$$$, "NO" means the opposite. Note that the grid is fixed before the start of your solution and does not depend on your queries. After printing a query do not forget to output end of line and flush the output. Otherwise you will get Idleness limit exceeded or other negative verdict. 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. Answer "BAD" instead of "YES" or "NO" means that you made an invalid query. Exit immediately after receiving "BAD" and you will see Wrong answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream. Example Output xa0 ? 1 1 4 4? 1 2 4 3 ? 4 1 4 4 ? 1 4 4 4 ! RDRRDD Note The first example is shown on the picture below. To hack, use the following input format: The first line should contain a single integer $$$n$$$ ($$$2 le n le 500$$$)xa0β the size of the grid. Each of the next $$$n$$$ lines should contain a string of $$$n$$$ characters '#' or '.', where '#' denotes a blocked cell, and '.' denotes an allowed cell. For example, the following text encodes the example shown above: 4 ..#. #... ###. .... | 2,100 | false | false | false | false | false | true | false | false | false | false | 5,586 |
6E | There are several days left before the fiftieth birthday of a famous Berland's writer Berlbury. In this connection the local library decided to make an exposition of the works of this famous science-fiction writer. It was decided as well that it is necessary to include into the exposition only those books that were published during a particular time period. It is obvious that if the books differ much in size, the visitors will not like it. That was why the organizers came to the opinion, that the difference between the highest and the lowest books in the exposition should be not more than _k_ millimeters. The library has _n_ volumes of books by Berlbury, arranged in chronological order of their appearance. The height of each book in millimeters is know, it is _h__i_. As Berlbury is highly respected in the city, the organizers want to include into the exposition as many books as possible, and to find out what periods of his creative work they will manage to cover. You are asked to help the organizers cope with this hard task. Input The first line of the input data contains two integer numbers separated by a space _n_ (1u2009β€u2009_n_u2009β€u2009105) and _k_ (0u2009β€u2009_k_u2009β€u2009106) β the amount of books by Berlbury in the library, and the maximum allowed height difference between the lowest and the highest books. The second line contains _n_ integer numbers separated by a space. Each number _h__i_ (1u2009β€u2009_h__i_u2009β€u2009106) is the height of the _i_-th book in millimeters. Output In the first line of the output data print two numbers _a_ and _b_ (separate them by a space), where _a_ is the maximum amount of books the organizers can include into the exposition, and _b_ β the amount of the time periods, during which Berlbury published _a_ books, and the height difference between the lowest and the highest among these books is not more than _k_ milllimeters. In each of the following _b_ lines print two integer numbers separated by a space β indexes of the first and the last volumes from each of the required time periods of Berlbury's creative work. | 1,900 | false | false | false | false | true | false | false | true | false | false | 9,961 |
75A | Problem - 75A - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags implementation *1000 No tag edit access β Contest materials ") Editorial") , my solutions") . There won't be any leading zeros in both. The value of _c_ should be calculated as _c_u2009=u2009_a_u2009+u2009_b_. Output The output will be just one line, you should print "YES" if the equation will remain correct after removing all zeros, and print "NO" otherwise. Examples Input 101 102 Output YES Input 105 106 Output NO | 1,000 | false | false | true | false | false | false | false | false | false | false | 9,591 |
172C | There is a bus stop near the university. The lessons are over, and _n_ students come to the stop. The _i_-th student will appear at the bus stop at time _t__i_ (all _t__i_'s are distinct). We shall assume that the stop is located on the coordinate axis _Ox_, at point _x_u2009=u20090, and the bus goes along the ray _Ox_, that is, towards the positive direction of the coordinate axis, and back. The _i_-th student needs to get to the point with coordinate _x__i_ (_x__i_u2009>u20090). The bus moves by the following algorithm. Initially it is at point 0. The students consistently come to the stop and get on it. The bus has a seating capacity which is equal to _m_ passengers. At the moment when _m_ students get on the bus, it starts moving in the positive direction of the coordinate axis. Also it starts moving when the last (_n_-th) student gets on the bus. The bus is moving at a speed of 1 unit of distance per 1 unit of time, i.e. it covers distance _y_ in time _y_. Every time the bus passes the point at which at least one student needs to get off, it stops and these students get off the bus. The students need 1u2009+u2009[_k_u2009/u20092] units of time to get off the bus, where _k_ is the number of students who leave at this point. Expression [_k_u2009/u20092] denotes rounded down _k_u2009/u20092. As soon as the last student leaves the bus, the bus turns around and goes back to the point _x_u2009=u20090. It doesn't make any stops until it reaches the point. At the given point the bus fills with students once more, and everything is repeated. If students come to the stop when there's no bus, they form a line (queue) and get on the bus in the order in which they came. Any number of students get on the bus in negligible time, you should assume that it doesn't take any time. Any other actions also take no time. The bus has no other passengers apart from the students. Write a program that will determine for each student the time when he got off the bus. The moment a student got off the bus is the moment the bus stopped at the student's destination stop (despite the fact that the group of students need some time to get off). Input The first line contains two space-separated integers _n_,u2009_m_ (1u2009β€u2009_n_,u2009_m_u2009β€u2009105) β the number of students and the number of passengers the bus can transport, correspondingly. Next _n_ lines contain descriptions of the students, one per line. Each line contains a pair of integers _t__i_,u2009_x__i_ (1u2009β€u2009_t__i_u2009β€u2009105,u20091u2009β€u2009_x__i_u2009β€u2009104). The lines are given in the order of strict increasing of _t__i_. Values of _x__i_ can coincide. Output Print _n_ numbers _w_1,u2009_w_2,u2009...,u2009_w__n_, _w__i_ β the moment of time when the _i_-th student got off the bus. Print the numbers on one line and separate them with single spaces. Examples Input 20 4 28 13 31 13 35 6 36 4 52 6 53 4 83 2 84 4 87 1 93 6 108 4 113 6 116 1 125 2 130 2 136 13 162 2 166 4 184 1 192 2 Output 51 51 43 40 93 89 86 89 114 121 118 121 137 139 139 152 195 199 193 195 Note In the first sample the bus waits for the first student for 3 units of time and drives him to his destination in additional 5 units of time. So the student leaves the bus at the moment of time 3u2009+u20095u2009=u20098. In the second sample the capacity of the bus equals 1, that's why it will drive the first student alone. This student is the same as the student from the first sample. So the bus arrives to his destination at the moment of time 8, spends 1u2009+u2009[1u2009/u20092]u2009=u20091 units of time on getting him off, and returns back to 0 in additional 5 units of time. That is, the bus returns to the bus stop at the moment of time 14. By this moment the second student has already came to the bus stop. So he immediately gets in the bus, and is driven to his destination in additional 5 units of time. He gets there at the moment 14u2009+u20095u2009=u200919. In the third sample the bus waits for the fourth student for 6 units of time, then drives for 5 units of time, then gets the passengers off for 1u2009+u2009[4u2009/u20092]u2009=u20093 units of time, then returns for 5 units of time, and then drives the fifth student for 1 unit of time. | 1,500 | false | false | true | false | false | false | false | false | true | false | 9,176 |
312A | One day, liouzhou_101 got a chat record of Freda and Rainbow. Out of curiosity, he wanted to know which sentences were said by Freda, and which were said by Rainbow. According to his experience, he thought that Freda always said "lala." at the end of her sentences, while Rainbow always said "miao." at the beginning of his sentences. For each sentence in the chat record, help liouzhou_101 find whose sentence it is. Input The first line of the input contains an integer _n_ (1u2009β€u2009_n_u2009β€u200910), number of sentences in the chat record. Each of the next _n_ lines contains a sentence. A sentence is a string that contains only Latin letters (A-Z, a-z), underline (_), comma (,), point (.) and space ( ). Its length doesnβt exceed 100. Output For each sentence, output "Freda's" if the sentence was said by Freda, "Rainbow's" if the sentence was said by Rainbow, or "OMG>.< I don't know!" if liouzhou_101 canβt recognize whose sentence it is. He canβt recognize a sentence if it begins with "miao." and ends with "lala.", or satisfies neither of the conditions. Examples Input 5 I will go to play with you lala. wow, welcome. miao.lala. miao. miao . Output Freda's OMG>.< I don't know! OMG>.< I don't know! Rainbow's OMG>.< I don't know! | 1,100 | false | false | true | false | false | false | false | false | false | false | 8,597 |
1037B | You are given an array $$$a$$$ of $$$n$$$ integers and an integer $$$s$$$. It is guaranteed that $$$n$$$ is odd. In one operation you can either increase or decrease any single element by one. Calculate the minimum number of operations required to make the median of the array being equal to $$$s$$$. The median of the array with odd length is the value of the element which is located on the middle position after the array is sorted. For example, the median of the array $$$6, 5, 8$$$ is equal to $$$6$$$, since if we sort this array we will get $$$5, 6, 8$$$, and $$$6$$$ is located on the middle position. Input The first line contains two integers $$$n$$$ and $$$s$$$ ($$$1le nle 2cdot 10^5-1$$$, $$$1le sle 10^9$$$)xa0β the length of the array and the required value of median. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1le a_i le 10^9$$$)xa0β the elements of the array $$$a$$$. It is guaranteed that $$$n$$$ is odd. Output In a single line output the minimum number of operations to make the median being equal to $$$s$$$. Examples Input 7 20 21 15 12 11 20 19 12 Note In the first sample, $$$6$$$ can be increased twice. The array will transform to $$$8, 5, 8$$$, which becomes $$$5, 8, 8$$$ after sorting, hence the median is equal to $$$8$$$. In the second sample, $$$19$$$ can be increased once and $$$15$$$ can be increased five times. The array will become equal to $$$21, 20, 12, 11, 20, 20, 12$$$. If we sort this array we get $$$11, 12, 12, 20, 20, 20, 21$$$, this way the median is $$$20$$$. | 1,300 | false | true | false | false | false | false | false | false | false | false | 5,515 |
1863G | Problem - 1863G - 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 graphs math *2800 No tag edit access β Contest materials . You can perform the following operation several (possibly, zero) times: pick an arbitrary $$$i$$$ and perform swap$$$(a_i, a_{a_i})$$$. How many distinct arrays is it possible to attain? Output the answer modulo $$$(10^9 + 7)$$$. Input The first line contains an integer $$$n$$$ ($$$1 le n le 10^6$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1le a_ile n$$$). Output Output the number of attainable arrays modulo $$$(10^9 + 7)$$$. Examples Input 3 1 1 2 Output 2 Input 4 2 1 4 3 Output 4 Input 6 2 3 1 1 1 2 Output 18 Note In the first example, the initial array is $$$ | 2,800 | true | false | false | true | false | false | false | false | false | true | 1,100 |
173D | The Trinitarian kingdom has exactly _n_u2009=u20093_k_ cities. All of them are located on the shores of river Trissisipi, which flows through the whole kingdom. Some of the cities are located on one side of the river, and all the rest are on the other side. Some cities are connected by bridges built between them. Each bridge connects two cities that are located on the opposite sides of the river. Between any two cities exists no more than one bridge. The recently inaugurated King Tristan the Third is busy distributing his deputies among cities. In total there are _k_ deputies and the king wants to commission each of them to control exactly three cities. However, no deputy can be entrusted to manage the cities, which are connected by a bridge β the deputy can set a too high fee for travelling over the bridge to benefit his pocket, which is bad for the reputation of the king. Help King Tristan the Third distribute the deputies between the cities, if it is possible. Input The first line contains two integers _n_ and _m_ β the number of cities and bridges (3u2009β€u2009_n_u2009<u2009105, _n_u2009=u20093_k_, 0u2009β€u2009_m_u2009β€u2009105). Next _m_ lines describe the bridges. The _i_-th line contains two integers _a__i_ and _b__i_ β the numbers of cities that are connected by the _i_-th bridge (1u2009β€u2009_a__i_,u2009_b__i_u2009β€u2009_n_, _a__i_u2009β u2009_b__i_, 1u2009β€u2009_i_u2009β€u2009_m_). It is guaranteed that no bridge connects a city with itself and that any two cities are connected with no more than one bridge. Output If distributing the deputies in the required manner is impossible, print in a single line "NO" (without the quotes). Otherwise, in the first line print "YES" (without the quotes), and in the second line print which deputy should be put in charge of each city. The _i_-th number should represent the number of the deputy (from 1 to _k_), who should be in charge of city numbered _i_-th in the input β overall there should be _n_ numbers. If there are multiple solutions, print any of them. Examples Input 6 6 1 2 4 1 3 5 6 5 2 6 4 6 | 2,500 | false | true | true | false | false | true | false | false | false | true | 9,170 |
725A | In a new version of the famous Pinball game, one of the most important parts of the game field is a sequence of _n_ bumpers. The bumpers are numbered with integers from 1 to _n_ from left to right. There are two types of bumpers. They are denoted by the characters '<' and '>'. When the ball hits the bumper at position _i_ it goes one position to the right (to the position _i_u2009+u20091) if the type of this bumper is '>', or one position to the left (to _i_u2009-u20091) if the type of the bumper at position _i_ is '<'. If there is no such position, in other words if _i_u2009-u20091u2009<u20091 or _i_u2009+u20091u2009>u2009_n_, the ball falls from the game field. Depending on the ball's starting position, the ball may eventually fall from the game field or it may stay there forever. You are given a string representing the bumpers' types. Calculate the number of positions such that the ball will eventually fall from the game field if it starts at that position. Input The first line of the input contains a single integer _n_ (1u2009β€u2009_n_u2009β€u2009200u2009000)xa0β the length of the sequence of bumpers. The second line contains the string, which consists of the characters '<' and '>'. The character at the _i_-th position of this string corresponds to the type of the _i_-th bumper. Output Print one integerxa0β the number of positions in the sequence such that the ball will eventually fall from the game field if it starts at that position. Note In the first sample, the ball will fall from the field if starts at position 1 or position 2. In the second sample, any starting position will result in the ball falling from the field. | 1,000 | false | false | true | false | false | false | false | false | false | false | 6,907 |
1188A2 | Note that this is the second problem of the two similar problems. You can hack this problem if you solve it. But you can hack the previous problem only if you solve both problems. You are given a tree with $$$n$$$ nodes. In the beginning, $$$0$$$ is written on all edges. In one operation, you can choose any $$$2$$$ distinct leaves $$$u$$$, $$$v$$$ and any integer number $$$x$$$ and add $$$x$$$ to values written on all edges on the simple path between $$$u$$$ and $$$v$$$. Note that in previous subtask $$$x$$$ was allowed to be any real, here it has to be integer. For example, on the picture below you can see the result of applying two operations to the graph: adding $$$2$$$ on the path from $$$7$$$ to $$$6$$$, and then adding $$$-1$$$ on the path from $$$4$$$ to $$$5$$$. You are given some configuration of nonnegative integer pairwise different even numbers, written on the edges. For a given configuration determine if it is possible to achieve it with these operations, and, if it is possible, output the sequence of operations that leads to the given configuration. Constraints on the operations are listed in the output format section. Leave is a node of a tree of degree $$$1$$$. Simple path is a path that doesn't contain any node twice. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 1000$$$)xa0β the number of nodes in a tree. Each of the next $$$n-1$$$ lines contains three integers $$$u$$$, $$$v$$$, $$$val$$$ ($$$1 le u, v le n$$$, $$$u eq v$$$, $$$0 le val le 10,000$$$), meaning that there is an edge between nodes $$$u$$$ and $$$v$$$ with $$$val$$$ written on it. It is guaranteed that these edges form a tree. It is guaranteed that all $$$val$$$ numbers are pairwise different and even. Output If there aren't any sequences of operations which lead to the given configuration, output "NO". If it exists, output "YES" in the first line. In the second line output $$$m$$$xa0β number of operations you are going to apply ($$$0 le m le 10^5$$$). Note that you don't have to minimize the number of the operations! In the next $$$m$$$ lines output the operations in the following format: $$$u, v, x$$$ ($$$1 le u, v le n$$$, $$$u ot = v$$$, $$$x$$$xa0β integer, $$$-10^9 le x le 10^9$$$), where $$$u, v$$$xa0β leaves, $$$x$$$xa0β number we are adding. It is guaranteed that if there exists a sequence of operations producing given configuration, then there exists a sequence of operations producing given configuration, satisfying all the conditions above. Examples Input 5 1 2 2 2 3 4 3 4 10 3 5 18 Input 6 1 2 6 1 3 8 1 4 12 2 5 2 2 6 4 Output YES 4 3 6 1 4 6 3 3 4 7 4 5 2 Note The configuration from the first sample is drawn below, and it is impossible to achieve. The sequence of operations from the second sample is illustrated below. | 2,500 | false | false | true | false | false | true | false | false | false | false | 4,758 |
696F | Barney has finally found the one, a beautiful young lady named Lyanna. The problem is, Lyanna and Barney are trapped in Lord Loss' castle. This castle has shape of a convex polygon of _n_ points. Like most of castles in Demonata worlds, this castle has no ceiling. Barney and Lyanna have an escape plan, but it requires some geometry knowledge, so they asked for your help. Barney knows that demons are organized and move in lines. He and Lyanna want to wait for the appropriate time so they need to watch for the demons. Each of them wants to stay in a point inside the castle (possibly on edges or corners), also they may stay in the same position. They both want to pick a real number _r_ and watch all points in the circles with radius _r_ around each of them (these two circles may overlap). We say that Barney and Lyanna are watching carefully if and only if for every edge of the polygon, at least one of them can see at least one point on the line this edge lies on, thus such point may not be on the edge but it should be on edge's line. Formally, each edge line should have at least one common point with at least one of two circles. The greater _r_ is, the more energy and focus they need. So they asked you to tell them the minimum value of _r_ such that they can watch carefully. Input The first line of input contains a single integer _n_ (3u2009β€u2009_n_u2009β€u2009300)xa0β the number of castle polygon vertices. The next _n_ lines describe the polygon vertices in counter-clockwise order. _i_-th of them contains two integers _x__i_ and _y__i_ (_x__i_,u2009_y__i_u2009β€u2009104)xa0β the coordinates of _i_-th point of the castle. It is guaranteed that given points form a convex polygon, in particular, any three of them do not line on the same line. Output In the first line print the single number _r_xa0β minimum radius of guys' watching circles. In the second line print the pair of coordinates of point where Barney should stay. In the third line print the pair of coordinates of point where Lyanna should stay. Points should lie inside the polygon. Coordinates may not be integers. If there are multiple answers print any of them. Your answer will be considered correct if its absolute or relative error doesn't exceed 10u2009-u20096. | 3,300 | false | false | false | false | false | false | false | true | false | false | 7,036 |
1787B | Given an integer $$$n$$$. Consider all pairs of integer arrays $$$a$$$ and $$$p$$$ of the same length such that $$$n = prod a_i^{p_i}$$$ (i.e. $$$a_1^{p_1}cdot a_2^{p_2}cdotldots$$$) ($$$a_i>1;p_i>0$$$) and $$$a_i$$$ is the product of some (possibly one) distinct prime numbers. For example, for $$$n = 28 = 2^2cdot 7^1 = 4^1 cdot 7^1$$$ the array pair $$$a = [2, 7]$$$, $$$p = [2, 1]$$$ is correct, but the pair of arrays $$$a = [4, 7]$$$, $$$p = [1, 1]$$$ is not, because $$$4=2^2$$$ is a product of non-distinct prime numbers. Your task is to find the maximum value of $$$sum a_i cdot p_i$$$ (i.e. $$$a_1cdot p_1 + a_2cdot p_2 + ldots$$$) over all possible pairs of arrays $$$a$$$ and $$$p$$$. Note that you do not need to minimize or maximize the length of the arrays. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 le t le 1000$$$) β the number of test cases. Each test case contains only one integer $$$n$$$ ($$$2 le n le 10^9$$$). Output For each test case, print the maximum value of $$$sum a_i cdot p_i$$$. Example Input 7 100 10 864 130056192 1000000000 2 999999018 Output 20 10 22 118 90 2 333333009 Note In the first test case, $$$100 = 10^2$$$ so that $$$a = [10]$$$, $$$p = [2]$$$ when $$$sum a_i cdot p_i$$$ hits the maximum value $$$10cdot 2 = 20$$$. Also, $$$a = [100]$$$, $$$p = [1]$$$ does not work since $$$100$$$ is not made of distinct prime factors. In the second test case, we can consider $$$10$$$ as $$$10^1$$$, so $$$a = [10]$$$, $$$p = [1]$$$. Notice that when $$$10 = 2^1cdot 5^1$$$, $$$sum a_i cdot p_i = 7$$$. | 1,100 | true | true | false | false | false | false | false | false | false | false | 1,562 |
200C | Any resemblance to any real championship and sport is accidental. The Berland National team takes part in the local Football championship which now has a group stage. Let's describe the formal rules of the local championship: the team that kicked most balls in the enemy's goal area wins the game; the victory gives 3 point to the team, the draw gives 1 point and the defeat gives 0 points; a group consists of four teams, the teams are ranked by the results of six games: each team plays exactly once with each other team; the teams that get places 1 and 2 in the group stage results, go to the next stage of the championship. In the group stage the team's place is defined by the total number of scored points: the more points, the higher the place is. If two or more teams have the same number of points, then the following criteria are used (the criteria are listed in the order of falling priority, starting from the most important one): the difference between the total number of scored goals and the total number of missed goals in the championship: the team with a higher value gets a higher place; the total number of scored goals in the championship: the team with a higher value gets a higher place; the lexicographical order of the name of the teams' countries: the country with the lexicographically smaller name gets a higher place. The Berland team plays in the group where the results of 5 out of 6 games are already known. To be exact, there is the last game left. There the Berand national team plays with some other team. The coach asks you to find such score _X_:_Y_ (where _X_ is the number of goals Berland scored and _Y_ is the number of goals the opponent scored in the game), that fulfills the following conditions: _X_ > _Y_, that is, Berland is going to win this game; after the game Berland gets the 1st or the 2nd place in the group; if there are multiple variants, you should choose such score _X_:_Y_, where value _X_u2009-u2009_Y_ is minimum; if it is still impossible to come up with one score, you should choose the score where value _Y_ (the number of goals Berland misses) is minimum. Input The input has five lines. Each line describes a game as "_team_1 _team_2 _goals_1:_goals_2" (without the quotes), what means that team _team_1 played a game with team _team_2, besides, _team_1 scored _goals_1 goals and _team_2 scored _goals_2 goals. The names of teams _team_1 and _team_2 are non-empty strings, consisting of uppercase English letters, with length of no more than 20 characters; _goals_1,u2009_goals_2 are integers from 0 to 9. The Berland team is called "BERLAND". It is guaranteed that the Berland team and one more team played exactly 2 games and the the other teams played exactly 3 games. Output Print the required score in the last game as _X_:_Y_, where _X_ is the number of goals Berland scored and _Y_ is the number of goals the opponent scored. If the Berland team does not get the first or the second place in the group, whatever this game's score is, then print on a single line "IMPOSSIBLE" (without the quotes). Note, that the result score can be very huge, 10:0 for example. Examples Input AERLAND DERLAND 2:1 DERLAND CERLAND 0:3 CERLAND AERLAND 0:1 AERLAND BERLAND 2:0 DERLAND BERLAND 4:0 Input AERLAND DERLAND 2:2 DERLAND CERLAND 2:3 CERLAND AERLAND 1:3 AERLAND BERLAND 2:1 DERLAND BERLAND 4:1 Note In the first sample "BERLAND" plays the last game with team "CERLAND". If Berland wins with score 6:0, the results' table looks like that in the end: 1. AERLAND (points: 9, the difference between scored and missed goals: 4, scored goals: 5) 2. BERLAND (points: 3, the difference between scored and missed goals: 0, scored goals: 6) 3. DERLAND (points: 3, the difference between scored and missed goals: 0, scored goals: 5) 4. CERLAND (points: 3, the difference between scored and missed goals: -4, scored goals: 3) In the second sample teams "AERLAND" and "DERLAND" have already won 7 and 4 points, respectively. The Berland team wins only 3 points, which is not enough to advance to the next championship stage. | 1,800 | false | false | true | false | false | false | true | false | false | false | 9,042 |
500C | New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has _n_ books numbered by integers from 1 to _n_. The weight of the _i_-th (1u2009β€u2009_i_u2009β€u2009_n_) book is _w__i_. As Jaehyun's house is not large enough to have a bookshelf, he keeps the _n_ books by stacking them vertically. When he wants to read a certain book _x_, he follows the steps described below. 1. He lifts all the books above book _x_. 2. He pushes book _x_ out of the stack. 3. He puts down the lifted books without changing their order. 4. After reading book _x_, he puts book _x_ on the top of the stack. He decided to read books for _m_ days. In the _j_-th (1u2009β€u2009_j_u2009β€u2009_m_) day, he will read the book that is numbered with integer _b__j_ (1u2009β€u2009_b__j_u2009β€u2009_n_). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times. After making this plan, he realized that the total weight of books he should lift during _m_ days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him? Input The first line contains two space-separated integers _n_ (2u2009β€u2009_n_u2009β€u2009500) and _m_ (1u2009β€u2009_m_u2009β€u20091000) β the number of books, and the number of days for which Jaehyun would read books. The second line contains _n_ space-separated integers _w_1,u2009_w_2,u2009...,u2009_w__n_ (1u2009β€u2009_w__i_u2009β€u2009100) β the weight of each book. The third line contains _m_ space separated integers _b_1,u2009_b_2,u2009...,u2009_b__m_ (1u2009β€u2009_b__j_u2009β€u2009_n_) β the order of books that he would read. Note that he can read the same book more than once. Output Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books. Note Here's a picture depicting the example. Each vertical column presents the stacked books. | 1,600 | true | true | true | false | false | true | false | false | false | false | 7,835 |
1239B | This is a harder version of the problem. In this version, $$$n le 300,000$$$. Vasya is an experienced developer of programming competitions' problems. As all great minds at some time, Vasya faced a creative crisis. To improve the situation, Petya gifted him a string consisting of opening and closing brackets only. Petya believes, that the beauty of the bracket string is a number of its cyclical shifts, which form a correct bracket sequence. To digress from his problems, Vasya decided to select two positions of the string (not necessarily distinct) and swap characters located at this positions with each other. Vasya will apply this operation exactly once. He is curious what is the maximum possible beauty he can achieve this way. Please help him. We remind that bracket sequence $$$s$$$ is called correct if: $$$s$$$ is empty; $$$s$$$ is equal to "($$$t$$$)", where $$$t$$$ is correct bracket sequence; $$$s$$$ is equal to $$$t_1 t_2$$$, i.e. concatenation of $$$t_1$$$ and $$$t_2$$$, where $$$t_1$$$ and $$$t_2$$$ are correct bracket sequences. For example, "(()())", "()" are correct, while ")(" and "())" are not. The cyclical shift of the string $$$s$$$ of length $$$n$$$ by $$$k$$$ ($$$0 leq k < n$$$) is a string formed by a concatenation of the last $$$k$$$ symbols of the string $$$s$$$ with the first $$$n - k$$$ symbols of string $$$s$$$. For example, the cyclical shift of string "(())()" by $$$2$$$ equals "()(())". Cyclical shifts $$$i$$$ and $$$j$$$ are considered different, if $$$i e j$$$. Input The first line contains an integer $$$n$$$ ($$$1 le n le 300,000$$$), the length of the string. The second line contains a string, consisting of exactly $$$n$$$ characters, where each of the characters is either "(" or ")". Output The first line should contain a single integerxa0β the largest beauty of the string, which can be achieved by swapping some two characters. The second line should contain integers $$$l$$$ and $$$r$$$ ($$$1 leq l, r leq n$$$)xa0β the indices of two characters, which should be swapped in order to maximize the string's beauty. In case there are several possible swaps, print any of them. Note In the first example, we can swap $$$7$$$-th and $$$8$$$-th character, obtaining a string "()()()()()". The cyclical shifts by $$$0, 2, 4, 6, 8$$$ of this string form a correct bracket sequence. In the second example, after swapping $$$5$$$-th and $$$10$$$-th character, we obtain a string ")(())()()(()". The cyclical shifts by $$$11, 7, 5, 3$$$ of this string form a correct bracket sequence. In the third example, swap of any two brackets results in $$$0$$$ cyclical shifts being correct bracket sequences. | 2,500 | false | false | true | false | false | false | false | false | false | false | 4,495 |
993B | Two participants are each given a pair of distinct numbers from 1 to 9 such that there's exactly one number that is present in both pairs. They want to figure out the number that matches by using a communication channel you have access to without revealing it to you. Both participants communicated to each other a set of pairs of numbers, that includes the pair given to them. Each pair in the communicated sets comprises two different numbers. Determine if you can with certainty deduce the common number, or if you can determine with certainty that both participants know the number but you do not. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 12$$$) β the number of pairs the first participant communicated to the second and vice versa. The second line contains $$$n$$$ pairs of integers, each between $$$1$$$ and $$$9$$$, β pairs of numbers communicated from first participant to the second. The third line contains $$$m$$$ pairs of integers, each between $$$1$$$ and $$$9$$$, β pairs of numbers communicated from the second participant to the first. All pairs within each set are distinct (in particular, if there is a pair $$$(1,2)$$$, there will be no pair $$$(2,1)$$$ within the same set), and no pair contains the same number twice. It is guaranteed that the two sets do not contradict the statements, in other words, there is pair from the first set and a pair from the second set that share exactly one number. Output If you can deduce the shared number with certainty, print that number. If you can with certainty deduce that both participants know the shared number, but you do not know it, print $$$0$$$. Otherwise print $$$-1$$$. Examples Input 2 3 1 2 4 5 1 2 1 3 2 3 Note In the first example the first participant communicated pairs $$$(1,2)$$$ and $$$(3,4)$$$, and the second communicated $$$(1,5)$$$, $$$(3,4)$$$. Since we know that the actual pairs they received share exactly one number, it can't be that they both have $$$(3,4)$$$. Thus, the first participant has $$$(1,2)$$$ and the second has $$$(1,5)$$$, and at this point you already know the shared number is $$$1$$$. In the second example either the first participant has $$$(1,2)$$$ and the second has $$$(1,5)$$$, or the first has $$$(3,4)$$$ and the second has $$$(6,4)$$$. In the first case both of them know the shared number is $$$1$$$, in the second case both of them know the shared number is $$$4$$$. You don't have enough information to tell $$$1$$$ and $$$4$$$ apart. In the third case if the first participant was given $$$(1,2)$$$, they don't know what the shared number is, since from their perspective the second participant might have been given either $$$(1,3)$$$, in which case the shared number is $$$1$$$, or $$$(2,3)$$$, in which case the shared number is $$$2$$$. While the second participant does know the number with certainty, neither you nor the first participant do, so the output is $$$-1$$$. | 1,900 | false | false | false | false | false | false | true | false | false | false | 5,733 |
1910H | Problem - 1910H - Codeforces =============== xa0 ]( --- Finished β Languages The following languages are only available languages for the problems from the contest Kotlin Heroes: Episode 9 (Unrated, T-Shirts + Prizes!): Kotlin 1.7.20 Kotlin 1.9.21 β 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 *special problem binary search data structures *2400 No tag edit access β Contest materials $$$, where $$$F(x)$$$ is the sum of digits of $$$x$$$. Input The first line contains one integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i < 10^9$$$). Output Print $$$n$$$ integers. The $$$i$$$-th of them should be equal to $$$sum limits_{j=1}^{n} F(a_i + a_j)$$$. Examples Input 4 1 3 3 7 Output 18 17 17 15 Input 3 42 1337 999 Output 38 53 47 | 2,400 | false | false | false | false | true | false | false | true | false | false | 840 |
304A | Problem - 304A - 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 math *1200 No tag edit access β Contest materials is equal to the sum of the areas of the squares whose sides are the two legs (the two sides that meet at a right angle). The theorem can be written as an equation relating the lengths of the sides _a_, _b_ and _c_, often called the Pythagorean equation: _a_2u2009+u2009_b_2u2009=u2009_c_2 where _c_ represents the length of the hypotenuse, and _a_ and _b_ represent the lengths of the other two sides. Given _n_, your task is to count how many right-angled triangles with side-lengths _a_, _b_ and _c_ that satisfied an inequality 1u2009β€u2009_a_u2009β€u2009_b_u2009β€u2009_c_u2009β€u2009_n_. Input The only line contains one integer _n_xa0(1u2009β€u2009_n_u2009β€u2009104) as we mentioned above. Output Print a single integer β the answer to the problem. Examples Input 5 Output 1 Input 74 Output 35 | 1,200 | true | false | false | false | false | false | true | false | false | false | 8,618 |
298A | There is a straight snowy road, divided into _n_ blocks. The blocks are numbered from 1 to _n_ from left to right. If one moves from the _i_-th block to the (_i_u2009+u20091)-th block, he will leave a right footprint on the _i_-th block. Similarly, if one moves from the _i_-th block to the (_i_u2009-u20091)-th block, he will leave a left footprint on the _i_-th block. If there already is a footprint on the _i_-th block, the new footprint will cover the old one. At the beginning, there were no footprints. Then polar bear Alice starts from the _s_-th block, makes a sequence of moves and ends in the _t_-th block. It is known that Alice never moves outside of the road. You are given the description of Alice's footprints. Your task is to find a pair of possible values of _s_,u2009_t_ by looking at the footprints. Input The first line of the input contains integer _n_ (3u2009β€u2009_n_u2009β€u20091000). The second line contains the description of the road β the string that consists of _n_ characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's guaranteed that the given string contains at least one character not equal to ".". Also, the first and the last character will always be ".". It's guaranteed that a solution exists. Output Print two space-separated integers β the values of _s_ and _t_. If there are several possible solutions you can print any of them. Note The first test sample is the one in the picture. | 1,300 | false | true | true | false | false | false | false | false | false | false | 8,639 |
1635E | There are $$$n$$$ cars on a coordinate axis $$$OX$$$. Each car is located at an integer point initially and no two cars are located at the same point. Also, each car is oriented either left or right, and they can move at any constant positive speed in that direction at any moment. More formally, we can describe the $$$i$$$-th car with a letter and an integer: its orientation $$$ori_i$$$ and its location $$$x_i$$$. If $$$ori_i = L$$$, then $$$x_i$$$ is decreasing at a constant rate with respect to time. Similarly, if $$$ori_i = R$$$, then $$$x_i$$$ is increasing at a constant rate with respect to time. We call two cars irrelevant if they never end up in the same point regardless of their speed. In other words, they won't share the same coordinate at any moment. We call two cars destined if they always end up in the same point regardless of their speed. In other words, they must share the same coordinate at some moment. Unfortunately, we lost all information about our cars, but we do remember $$$m$$$ relationships. There are two types of relationships: $$$1$$$ $$$i$$$ $$$j$$$ β$$$i$$$-th car and $$$j$$$-th car are irrelevant. $$$2$$$ $$$i$$$ $$$j$$$ β$$$i$$$-th car and $$$j$$$-th car are destined. Restore the orientations and the locations of the cars satisfying the relationships, or report that it is impossible. If there are multiple solutions, you can output any. Note that if two cars share the same coordinate, they will intersect, but at the same moment they will continue their movement in their directions. Input The first line contains two integers, $$$n$$$ and $$$m$$$ $$$(2 leq n leq 2 cdot 10^5; 1 leq m leq min(2 cdot 10^5, frac{n(n-1)}{2})$$$ β the number of cars and the number of restrictions respectively. Each of the next $$$m$$$ lines contains three integers, $$$type$$$, $$$i$$$, and $$$j$$$ $$$(1 leq type leq 2; 1 leq i,j leq n; iβ j)$$$. If $$$type$$$ = $$$1$$$, $$$i$$$-th car and $$$j$$$-th car are irrelevant. Otherwise, $$$i$$$-th car and $$$j$$$-th car are destined. It is guaranteed that for each pair of cars, there are at most $$$1$$$ relationship between. Output In the first line, print either "YES" or "NO" (in any case), whether it is possible to restore the orientations and the locations of the cars satisfying the relationships. If the answer is "YES", print $$$n$$$ lines each containing a symbol and an integer: $$$ori_i$$$ and $$$x_i$$$ $$$(ori_i in {L, R}; -10^9 leq x_i leq 10^9)$$$ β representing the information of the $$$i$$$-th car. If the orientation is left, then $$$ori_i$$$ = $$$L$$$. Otherwise $$$ori_i$$$ = $$$R$$$. $$$x_i$$$ is the where the $$$i$$$-th car is located. Note that all $$$x_i$$$ should be distinct. We can prove that if there exists a solution, then there must be a solution satisfying the constraints on $$$x_i$$$. | 2,200 | false | true | false | false | false | true | false | false | true | true | 2,436 |
1931B | There are $$$n$$$ containers of water lined up, numbered from left to right from $$$1$$$ to $$$n$$$. Each container can hold any amount of water; initially, the $$$i$$$-th container contains $$$a_i$$$ units of water. The sum of $$$a_i$$$ is divisible by $$$n$$$. You can apply the following operation any (possibly zero) number of times: pour any amount of water from the $$$i$$$-th container to the $$$j$$$-th container, where $$$i$$$ must be less than $$$j$$$ (i.e. $$$i<j$$$). Any index can be chosen as $$$i$$$ or $$$j$$$ any number of times. Determine whether it is possible to make the amount of water in all containers the same using this operation. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0β the number of containers with water. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 10^9$$$)xa0β the amounts of water in the containers. It is guaranteed that the sum of $$$a_i$$$ in each test case does not exceed $$$2 cdot 10^9$$$. Also, the sum of $$$a_i$$$ is divisible by $$$n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases in the input does not exceed $$$2 cdot 10^5$$$. Output Output $$$t$$$ lines, each of which is the answer to the corresponding test case. As the answer, output "YES" if it is possible to make the amount of water in all containers the same using the described operation. Otherwise, output "NO". You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer. Example Input 6 1 43 2 1 3 5 4 5 2 1 3 3 1 2 3 7 4 5 5 0 6 4 4 7 6 5 5 1 3 4 4 Output YES NO YES NO NO YES Note In the third test case of the example ($$$a=[4, 5, 2, 1, 3]$$$), you can proceed as follows: pour $$$1$$$ unit of water from the first vessel to the fourth, then $$$a=[3, 5, 2, 2, 3]$$$; pour $$$1$$$ unit of water from the second vessel to the third, then $$$a=[3, 4, 3, 2, 3]$$$; pour $$$1$$$ unit of water from the second vessel to the fourth, then $$$a=[3, 3, 3, 3, 3]$$$. | 800 | false | true | false | false | false | false | false | false | false | false | 700 |
878E | A sequence of _n_ integers is written on a blackboard. Soon Sasha will come to the blackboard and start the following actions: let _x_ and _y_ be two adjacent numbers (_x_ before _y_), then he can remove them and write _x_u2009+u20092_y_ instead of them. He will perform these operations until one number is left. Sasha likes big numbers and will get the biggest possible number. Nikita wants to get to the blackboard before Sasha and erase some of the numbers. He has _q_ options, in the option _i_ he erases all numbers to the left of the _l__i_-th number and all numbers to the right of _r__i_-th number, i.xa0e. all numbers between the _l__i_-th and the _r__i_-th, inclusive, remain on the blackboard. For each of the options he wants to know how big Sasha's final number is going to be. This number can be very big, so output it modulo 109u2009+u20097. Input The first line contains two integers _n_ and _q_ (1u2009β€u2009_n_,u2009_q_u2009β€u2009105) β the number of integers on the blackboard and the number of Nikita's options. The next line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (u2009-u2009109u2009β€u2009_a__i_u2009β€u2009109)xa0β the sequence on the blackboard. Each of the next _q_ lines contains two integers _l__i_ and _r__i_ (1u2009β€u2009_l__i_u2009β€u2009_r__i_u2009β€u2009_n_), describing Nikita's options. Output For each option output Sasha's result modulo 109u2009+u20097. Note In the second sample Nikita doesn't erase anything. Sasha first erases the numbers 1 and 2 and writes 5. Then he erases 5 and -3 and gets -1. -1 modulo 109u2009+u20097 is 109u2009+u20096. | 3,300 | false | false | false | true | false | false | false | false | false | false | 6,230 |
474C | Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has _n_ regiments, each consisting of 4 moles. Initially, each mole _i_ (1u2009β€u2009_i_u2009β€u20094_n_) is placed at some position (_x__i_,u2009_y__i_) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible. Each mole _i_ has a home placed at the position (_a__i_,u2009_b__i_). Moving this mole one time means rotating his position point (_x__i_,u2009_y__i_) 90 degrees counter-clockwise around it's home point (_a__i_,u2009_b__i_). A regiment is compact only if the position points of the 4 moles form a square with non-zero area. Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible. Input The first line contains one integer _n_ (1u2009β€u2009_n_u2009β€u2009100), the number of regiments. The next 4_n_ lines contain 4 integers _x__i_, _y__i_, _a__i_, _b__i_ (u2009-u2009104u2009β€u2009_x__i_,u2009_y__i_,u2009_a__i_,u2009_b__i_u2009β€u2009104). Output Print _n_ lines to the standard output. If the regiment _i_ can be made compact, the _i_-th line should contain one integer, the minimal number of required moves. Otherwise, on the _i_-th line print "-1" (without quotes). Examples Input 4 1 1 0 0 -1 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -2 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -1 1 0 0 -1 1 0 0 -1 1 0 0 2 2 0 1 -1 0 0 -2 3 0 0 -2 -1 1 -2 0 Note In the first regiment we can move once the second or the third mole. We can't make the second regiment compact. In the third regiment, from the last 3 moles we can move once one and twice another one. In the fourth regiment, we can move twice the first mole and once the third mole. | 2,000 | false | false | false | false | false | false | true | false | false | false | 7,938 |
1849E | Problem - 1849E - 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 divide and conquer dp dsu two pointers *2300 No tag edit access β Contest materials xa0β the number of elements in the permutation. The second line contains $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le n$$$). All $$$p_i$$$ are distinct. Output Print a single integerxa0β the number of subarrays $$$p_{l,r}$$$ such that $$$mathit{maxpos}_{l,r} > mathit{minpos}_{l,r}$$$. Examples Input 3 1 2 3 Output 3 Input 6 5 3 6 1 4 2 Output 4 Input 10 5 1 6 2 8 3 4 10 9 7 Output 38 | 2,300 | false | false | false | true | true | false | false | true | false | false | 1,184 |
1157G | You are given a binary matrix $$$a$$$ of size $$$n imes m$$$. A binary matrix is a matrix where each element is either $$$0$$$ or $$$1$$$. You may perform some (possibly zero) operations with this matrix. During each operation you can inverse the row of this matrix or a column of this matrix. Formally, inverting a row is changing all values in this row to the opposite ($$$0$$$ to $$$1$$$, $$$1$$$ to $$$0$$$). Inverting a column is changing all values in this column to the opposite. Your task is to sort the initial matrix by some sequence of such operations. The matrix is considered sorted if the array $$$[a_{1, 1}, a_{1, 2}, dots, a_{1, m}, a_{2, 1}, a_{2, 2}, dots, a_{2, m}, dots, a_{n, m - 1}, a_{n, m}]$$$ is sorted in non-descending order. Input The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 200$$$) β the number of rows and the number of columns in the matrix. The next $$$n$$$ lines contain $$$m$$$ integers each. The $$$j$$$-th element in the $$$i$$$-th line is $$$a_{i, j}$$$ ($$$0 le a_{i, j} le 1$$$) β the element of $$$a$$$ at position $$$(i, j)$$$. Output If it is impossible to obtain a sorted matrix, print "NO" in the first line. Otherwise print "YES" in the first line. In the second line print a string $$$r$$$ of length $$$n$$$. The $$$i$$$-th character $$$r_i$$$ of this string should be '1' if the $$$i$$$-th row of the matrix is inverted and '0' otherwise. In the third line print a string $$$c$$$ of length $$$m$$$. The $$$j$$$-th character $$$c_j$$$ of this string should be '1' if the $$$j$$$-th column of the matrix is inverted and '0' otherwise. If there are multiple answers, you can print any. | 2,200 | false | false | false | false | false | true | true | false | false | false | 4,916 |
1794D | The prime factorization of a positive integer $$$m$$$ is the unique way to write it as $$$displaystyle m=p_1^{e_1}cdot p_2^{e_2}cdot ldots cdot p_k^{e_k}$$$, where $$$p_1, p_2, ldots, p_k$$$ are prime numbers, $$$p_1 < p_2 < ldots < p_k$$$ and $$$e_1, e_2, ldots, e_k$$$ are positive integers. For each positive integer $$$m$$$, $$$f(m)$$$ is defined as the multiset of all numbers in its prime factorization, that is $$$f(m)={p_1,e_1,p_2,e_2,ldots,p_k,e_k}$$$. For example, $$$f(24)={2,3,3,1}$$$, $$$f(5)={1,5}$$$ and $$$f(1)={}$$$. You are given a list consisting of $$$2n$$$ integers $$$a_1, a_2, ldots, a_{2n}$$$. Count how many positive integers $$$m$$$ satisfy that $$$f(m)={a_1, a_2, ldots, a_{2n}}$$$. Since this value may be large, print it modulo $$$998,244,353$$$. Input The first line contains one integer $$$n$$$ ($$$1le n le 2022$$$). The second line contains $$$2n$$$ integers $$$a_1, a_2, ldots, a_{2n}$$$ ($$$1le a_ile 10^6$$$) xa0β the given list. Output Print one integer, the number of positive integers $$$m$$$ satisfying $$$f(m)={a_1, a_2, ldots, a_{2n}}$$$ modulo $$$998,244,353$$$. Note In the first sample, the two values of $$$m$$$ such that $$$f(m)={1,2,3,3}$$$ are $$$m=24$$$ and $$$m=54$$$. Their prime factorizations are $$$24=2^3cdot 3^1$$$ and $$$54=2^1cdot 3^3$$$. In the second sample, the five values of $$$m$$$ such that $$$f(m)={2,2,3,5}$$$ are $$$200, 225, 288, 500$$$ and $$$972$$$. In the third sample, there is no value of $$$m$$$ such that $$$f(m)={1,4}$$$. Neither $$$1^4$$$ nor $$$4^1$$$ are prime factorizations because $$$1$$$ and $$$4$$$ are not primes. | 1,900 | true | false | false | true | false | false | false | false | false | false | 1,511 |
357A | At the beginning of the school year Berland State University starts two city school programming groups, for beginners and for intermediate coders. The children were tested in order to sort them into groups. According to the results, each student got some score from 1 to _m_ points. We know that _c_1 schoolchildren got 1 point, _c_2 children got 2 points, ..., _c__m_ children got _m_ points. Now you need to set the passing rate _k_ (integer from 1 to _m_): all schoolchildren who got less than _k_ points go to the beginner group and those who get at strictly least _k_ points go to the intermediate group. We know that if the size of a group is more than _y_, then the university won't find a room for them. We also know that if a group has less than _x_ schoolchildren, then it is too small and there's no point in having classes with it. So, you need to split all schoolchildren into two groups so that the size of each group was from _x_ to _y_, inclusive. Help the university pick the passing rate in a way that meets these requirements. Input The first line contains integer _m_ (2u2009β€u2009_m_u2009β€u2009100). The second line contains _m_ integers _c_1, _c_2, ..., _c__m_, separated by single spaces (0u2009β€u2009_c__i_u2009β€u2009100). The third line contains two space-separated integers _x_ and _y_ (1u2009β€u2009_x_u2009β€u2009_y_u2009β€u200910000). At least one _c__i_ is greater than 0. Output If it is impossible to pick a passing rate in a way that makes the size of each resulting groups at least _x_ and at most _y_, print 0. Otherwise, print an integer from 1 to _m_ β the passing rate you'd like to suggest. If there are multiple possible answers, print any of them. Note In the first sample the beginner group has 7 students, the intermediate group has 6 of them. In the second sample another correct answer is 3. | 1,000 | false | true | true | false | false | false | true | false | false | false | 8,406 |
675C | There are _n_ banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank _n_ are neighbours if _n_u2009>u20091. No bank is a neighbour of itself. Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank. There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation. Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero. Input The first line of the input contains a single integer _n_ (1u2009β€u2009_n_u2009β€u2009100u2009000)xa0β the number of banks. The second line contains _n_ integers _a__i_ (u2009-u2009109u2009β€u2009_a__i_u2009β€u2009109), the _i_-th of them is equal to the initial balance of the account in the _i_-th bank. It's guaranteed that the sum of all _a__i_ is equal to 0. Output Print the minimum number of operations required to change balance in each bank to zero. Note In the first sample, Vasya may transfer 5 from the first bank to the third. In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first. In the third sample, the following sequence provides the optimal answer: 1. transfer 1 from the first bank to the second bank; 2. transfer 3 from the second bank to the third; 3. transfer 6 from the third bank to the fourth. | 2,100 | false | true | false | false | true | true | false | false | true | false | 7,129 |
1290C | There are $$$n$$$ lamps on a line, numbered from $$$1$$$ to $$$n$$$. Each one has an initial state off ($$$0$$$) or on ($$$1$$$). You're given $$$k$$$ subsets $$$A_1, ldots, A_k$$$ of $$${1, 2, dots, n}$$$, such that the intersection of any three subsets is empty. In other words, for all $$$1 le i_1 < i_2 < i_3 le k$$$, $$$A_{i_1} cap A_{i_2} cap A_{i_3} = varnothing$$$. In one operation, you can choose one of these $$$k$$$ subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation. Let $$$m_i$$$ be the minimum number of operations you have to do in order to make the $$$i$$$ first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between $$$i+1$$$ and $$$n$$$), they can be either off or on. You have to compute $$$m_i$$$ for all $$$1 le i le n$$$. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n, k le 3 cdot 10^5$$$). The second line contains a binary string of length $$$n$$$, representing the initial state of each lamp (the lamp $$$i$$$ is off if $$$s_i = 0$$$, on if $$$s_i = 1$$$). The description of each one of the $$$k$$$ subsets follows, in the following format: The first line of the description contains a single integer $$$c$$$ ($$$1 le c le n$$$) xa0β the number of elements in the subset. The second line of the description contains $$$c$$$ distinct integers $$$x_1, ldots, x_c$$$ ($$$1 le x_i le n$$$) xa0β the elements of the subset. It is guaranteed that: The intersection of any three subsets is empty; It's possible to make all lamps be simultaneously on using some operations. | 2,400 | false | false | false | false | false | false | false | false | false | true | 4,244 |
722F | You are given _n_ sequences. Each sequence consists of positive integers, not exceeding _m_. All integers in one sequence are distinct, but the same integer may appear in multiple sequences. The length of the _i_-th sequence is _k__i_. Each second integers in each of the sequences are shifted by one to the left, i.e. integers at positions _i_u2009>u20091 go to positions _i_u2009-u20091, while the first integers becomes the last. Each second we take the first integer of each sequence and write it down to a new array. Then, for each value _x_ from 1 to _m_ we compute the longest segment of the array consisting of element _x_ only. The above operation is performed for 10100 seconds. For each integer from 1 to _m_ find out the longest segment found at this time. Input The first line of the input contains two integers _n_ and _m_ (1u2009β€u2009_n_,u2009_m_u2009β€u2009100u2009000)xa0β the number of sequences and the maximum integer that can appear in the sequences. Then follow _n_ lines providing the sequences. Each of them starts with an integer _k__i_ (1u2009β€u2009_k__i_u2009β€u200940)xa0β the number of integers in the sequence, proceeded by _k__i_ positive integersxa0β elements of the sequence. It's guaranteed that all integers in each sequence are pairwise distinct and do not exceed _m_. The total length of all sequences doesn't exceed 200u2009000. Output Print _m_ integers, the _i_-th of them should be equal to the length of the longest segment of the array with all its values equal to _i_ during the first 10100 seconds. Examples Input 3 4 3 3 4 1 4 1 3 4 2 3 3 1 4 Input 5 5 2 3 1 4 5 1 3 2 4 2 1 3 5 1 3 2 5 3 Input 4 6 3 4 5 3 2 6 3 2 3 6 3 3 6 5 | 2,800 | false | false | true | false | true | false | false | false | false | false | 6,921 |
1750D | You are given two integers $$$n$$$ and $$$m$$$ and an array $$$a$$$ of $$$n$$$ integers. For each $$$1 le i le n$$$ it holds that $$$1 le a_i le m$$$. Your task is to count the number of different arrays $$$b$$$ of length $$$n$$$ such that: $$$1 le b_i le m$$$ for each $$$1 le i le n$$$, and $$$gcd(b_1,b_2,b_3,...,b_i) = a_i$$$ for each $$$1 le i le n$$$. Here $$$gcd(a_1,a_2,dots,a_i)$$$ denotes the β the number of test cases. The description of test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$1 le m le 10^9$$$) β the length of the array $$$a$$$ and the maximum possible value of the element. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le m$$$) β the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ across all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print a single integer β the number of different arrays satisfying the conditions above. Since this number can be large, print it modulo $$$998,244,353$$$. Example Input 5 3 5 4 2 1 2 1 1 1 5 50 2 3 5 2 3 4 1000000000 60 30 1 1 2 1000000000 1000000000 2 Output 3 1 0 595458194 200000000 Note In the first test case, the possible arrays $$$b$$$ are: $$$[4,2,1]$$$; $$$[4,2,3]$$$; $$$[4,2,5]$$$. In the second test case, the only array satisfying the demands is $$$[1,1]$$$. In the third test case, it can be proven no such array exists. | 1,800 | true | false | false | false | false | false | false | false | false | false | 1,782 |
933A | A dragon symbolizes wisdom, power and wealth. On Lunar New Year's Day, people model a dragon with bamboo strips and clothes, raise them with rods, and hold the rods high and low to resemble a flying dragon. A performer holding the rod low is represented by a 1, while one holding it high is represented by a 2. Thus, the line of performers can be represented by a sequence _a_1,u2009_a_2,u2009...,u2009_a__n_. Little Tommy is among them. He would like to choose an interval [_l_,u2009_r_] (1u2009β€u2009_l_u2009β€u2009_r_u2009β€u2009_n_), then reverse _a__l_,u2009_a__l_u2009+u20091,u2009...,u2009_a__r_ so that the length of the longest non-decreasing subsequence of the new sequence is maximum. A non-decreasing subsequence is a sequence of indices _p_1,u2009_p_2,u2009...,u2009_p__k_, such that _p_1u2009<u2009_p_2u2009<u2009...u2009<u2009_p__k_ and _a__p_1u2009β€u2009_a__p_2u2009β€u2009...u2009β€u2009_a__p__k_. The length of the subsequence is _k_. Input The first line contains an integer _n_ (1u2009β€u2009_n_u2009β€u20092000), denoting the length of the original sequence. The second line contains _n_ space-separated integers, describing the original sequence _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009β€u2009_a__i_u2009β€u20092,u2009_i_u2009=u20091,u20092,u2009...,u2009_n_). Output Print a single integer, which means the maximum possible length of the longest non-decreasing subsequence of the new sequence. Examples Input 10 1 1 2 2 2 1 1 2 2 1 Note In the first example, after reversing [2,u20093], the array will become [1,u20091,u20092,u20092], where the length of the longest non-decreasing subsequence is 4. In the second example, after reversing [3,u20097], the array will become [1,u20091,u20091,u20091,u20092,u20092,u20092,u20092,u20092,u20091], where the length of the longest non-decreasing subsequence is 9. | 1,800 | false | false | false | true | false | false | false | false | false | false | 5,971 |
1932D | Two players are playing an online card game. The game is played using a 32-card deck. Each card has a suit and a rank. There are four suits: clubs, diamonds, hearts, and spades. We will encode them with characters 'C', 'D', 'H', and 'S', respectively. And there are 8 ranks, in increasing order: '2', '3', '4', '5', '6', '7', '8', '9'. Each card is denoted by two letters: its rank and its suit. For example, the 8 of Hearts is denoted as 8H. At the beginning of the game, one suit is chosen as the trump suit. In each round, players make moves like this: the first player places one of his cards on the table, and the second player must beat this card with one of their cards. After that, both cards are moved to the discard pile. A card can beat another card if both cards have the same suit and the first card has a higher rank than the second. For example, 8S can beat 4S. Additionally, a trump card can beat any non-trump card, regardless of the rank of the cards, for example, if the trump suit is clubs ('C'), then 3C can beat 9D. Note that trump cards can be beaten only by the trump cards of higher rank. There were $$$n$$$ rounds played in the game, so the discard pile now contains $$$2n$$$ cards. You want to reconstruct the rounds played in the game, but the cards in the discard pile are shuffled. Find any possible sequence of $$$n$$$ rounds that might have been played in the game. Input The first line contains integer $$$t$$$ ($$$1 le t le 100$$$) β the number of test cases. Then $$$t$$$ test cases follow. The first line of a test case contains the integer number $$$n$$$ ($$$1le nle 16$$$). The second line of a test case contains one character, the trump suit. It is one of "CDHS". The third line of a test case contains the description of $$$2n$$$ cards. Each card is described by a two-character string, the first character is the rank of the card, which is one of "23456789", and the second one is the suit of the card, which is one of "CDHS". All cards are different. | 1,400 | false | true | true | false | false | false | false | false | false | false | 691 |
749D | There are _n_ people taking part in auction today. The rules of auction are classical. There were _n_ bids made, though it's not guaranteed they were from different people. It might happen that some people made no bids at all. Each bid is define by two integers (_a__i_,u2009_b__i_), where _a__i_ is the index of the person, who made this bid and _b__i_ is its size. Bids are given in chronological order, meaning _b__i_u2009<u2009_b__i_u2009+u20091 for all _i_u2009<u2009_n_. Moreover, participant never makes two bids in a row (no one updates his own bid), i.e. _a__i_u2009β u2009_a__i_u2009+u20091 for all _i_u2009<u2009_n_. Now you are curious with the following question: who (and which bid) will win the auction if some participants were absent? Consider that if someone was absent, all his bids are just removed and no new bids are added. Note, that if during this imaginary exclusion of some participants it happens that some of the remaining participants makes a bid twice (or more times) in a row, only first of these bids is counted. For better understanding take a look at the samples. You have several questions in your mind, compute the answer for each of them. Input The first line of the input contains an integer _n_ (1u2009β€u2009_n_u2009β€u2009200u2009000)xa0β the number of participants and bids. Each of the following _n_ lines contains two integers _a__i_ and _b__i_ (1u2009β€u2009_a__i_u2009β€u2009_n_,u20091u2009β€u2009_b__i_u2009β€u2009109,u2009_b__i_u2009<u2009_b__i_u2009+u20091)xa0β the number of participant who made the _i_-th bid and the size of this bid. Next line contains an integer _q_ (1u2009β€u2009_q_u2009β€u2009200u2009000)xa0β the number of question you have in mind. Each of next _q_ lines contains an integer _k_ (1u2009β€u2009_k_u2009β€u2009_n_), followed by _k_ integers _l__j_ (1u2009β€u2009_l__j_u2009β€u2009_n_)xa0β the number of people who are not coming in this question and their indices. It is guarenteed that _l__j_ values are different for a single question. It's guaranteed that the sum of _k_ over all question won't exceed 200u2009000. | 2,000 | false | false | false | false | true | false | false | true | false | false | 6,795 |
639D | Codeforces is a wonderful platform and one its feature shows how much someone contributes to the community. Every registered user has contributionxa0β an integer number, not necessarily positive. There are _n_ registered users and the _i_-th of them has contribution _t__i_. Limak is a little polar bear and he's new into competitive programming. He doesn't even have an account in Codeforces but he is able to upvote existing blogs and comments. We assume that every registered user has infinitely many blogs and comments. Limak can spend _b_ minutes to read one blog and upvote it. Author's contribution will be increased by 5. Limak can spend _c_ minutes to read one comment and upvote it. Author's contribution will be increased by 1. Note that it's possible that Limak reads blogs faster than comments. Limak likes ties. He thinks it would be awesome to see a tie between at least _k_ registered users. To make it happen he is going to spend some time on reading and upvoting. After that, there should exist an integer value _x_ that at least _k_ registered users have contribution exactly _x_. How much time does Limak need to achieve his goal? Input The first line contains four integers _n_, _k_, _b_ and _c_ (2u2009β€u2009_k_u2009β€u2009_n_u2009β€u2009200u2009000,u20091u2009β€u2009_b_,u2009_c_u2009β€u20091000)xa0β the number of registered users, the required minimum number of users with the same contribution, time needed to read and upvote a blog, and time needed to read and upvote a comment, respectively. The second line contains _n_ integers _t_1,u2009_t_2,u2009...,u2009_t__n_ (_t__i_u2009β€u2009109) where _t__i_ denotes contribution of the _i_-th registered user. Output Print the minimum number of minutes Limak will spend to get a tie between at least _k_ registered users. Examples Input 6 2 987 789 -8 42 -4 -65 -8 -8 Note In the first sample, there are 4 registered users and Limak wants a tie between at least 3 of them. Limak should behave as follows. He spends 100 minutes to read one blog of the 4-th user and increase his contribution from 1 to 6. Then he spends 4Β·30u2009=u2009120 minutes to read four comments of the 2-nd user and increase his contribution from 2 to 6 (four times it was increaded by 1). In the given scenario, Limak spends 100u2009+u20094Β·30u2009=u2009220 minutes and after that each of users 2,u20093,u20094 has contribution 6. In the second sample, Limak needs 30 minutes to read a blog and 100 minutes to read a comment. This time he can get 3 users with contribution equal to 12 by spending 100u2009+u20093Β·30u2009=u2009190 minutes: Spend 2Β·30u2009=u200960 minutes to read two blogs of the 1-st user to increase his contribution from 2 to 12. Spend 30u2009+u2009100 minutes to read one blog and one comment of the 3-rd user. His contribution will change from 6 to 6u2009+u20095u2009+u20091u2009=u200912. | 2,400 | false | true | false | false | true | false | false | false | true | false | 7,254 |
924A | There is a rectangular grid of _n_ rows of _m_ initially-white cells each. Arkady performed a certain number (possibly zero) of operations on it. In the _i_-th operation, a non-empty subset of rows _R__i_ and a non-empty subset of columns _C__i_ are chosen. For each row _r_ in _R__i_ and each column _c_ in _C__i_, the intersection of row _r_ and column _c_ is coloured black. There's another constraint: a row or a column can only be chosen at most once among all operations. In other words, it means that no pair of (_i_,u2009_j_) (_i_u2009<u2009_j_) exists such that or , where denotes intersection of sets, and denotes the empty set. You are to determine whether a valid sequence of operations exists that produces a given final grid. Input The first line contains two space-separated integers _n_ and _m_ (1u2009β€u2009_n_,u2009_m_u2009β€u200950)xa0β the number of rows and columns of the grid, respectively. Each of the following _n_ lines contains a string of _m_ characters, each being either '.' (denoting a white cell) or '#' (denoting a black cell), representing the desired setup. Output If the given grid can be achieved by any valid sequence of operations, output "Yes"; otherwise output "No" (both without quotes). You can print each character in any case (upper or lower). Examples Input 5 8 .#.#..#. .....#.. .#.#..#. #.#....# .....#.. Input 5 5 ..#.. ..#.. ##### ..#.. ..#.. Input 5 9 ........# #........ ..##.#... .......#. ....#.#.# Note For the first example, the desired setup can be produced by 3 operations, as is shown below. For the second example, the desired setup cannot be produced, since in order to colour the center row, the third row and all columns must be selected in one operation, but after that no column can be selected again, hence it won't be possible to colour the other cells in the center column. | 1,300 | false | true | true | false | false | false | false | false | false | false | 6,017 |
373B | Problem - 373B - 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 implementation math *1600 No tag edit access β Contest materials for positive integer _n_ as follows: the number of the _n_'s digits in the decimal base. For example, _S_(893)u2009=u20093, _S_(114514)u2009=u20096. You want to make a consecutive integer sequence starting from number _m_ (_m_,u2009_m_u2009+u20091,u2009...). But you need to pay _S_(_n_)Β·_k_ to add the number _n_ to the sequence. You can spend a cost up to _w_, and you want to make the sequence as long as possible. Write a program that tells sequence's maximum length. Input The first line contains three integers _w_ (1u2009β€u2009_w_u2009β€u20091016), _m_ (1u2009β€u2009_m_u2009β€u20091016), _k_ (1u2009β€u2009_k_u2009β€u2009109). Please, do not write the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Output The first line should contain a single integer β the answer to the problem. Examples Input 9 1 1 Output 9 Input 77 7 7 Output 7 Input 114 5 14 Output 6 Input 1 1 2 Output 0 | 1,600 | true | false | true | false | false | false | false | true | false | false | 8,337 |
37D | When Petya has free from computer games time, he attends university classes. Every day the lessons on Petyaβs faculty consist of two double classes. The floor where the lessons take place is a long corridor with _M_ classrooms numbered from 1 to _M_, situated along it. All the students of Petyaβs year are divided into _N_ groups. Petya has noticed recently that these groupsβ timetable has the following peculiarity: the number of the classroom where the first lesson of a group takes place does not exceed the number of the classroom where the second lesson of this group takes place. Once Petya decided to count the number of ways in which one can make a lesson timetable for all these groups. The timetable is a set of 2_N_ numbers: for each group the number of the rooms where the first and the second lessons take place. Unfortunately, he quickly lost the track of his calculations and decided to count only the timetables that satisfy the following conditions: 1) On the first lesson in classroom _i_ exactly _X__i_ groups must be present. 2) In classroom _i_ no more than _Y__i_ groups may be placed. Help Petya count the number of timetables satisfying all those conditionsΡ As there can be a lot of such timetables, output modulo 109u2009+u20097. Input The first line contains one integer _M_ (1u2009β€u2009_M_u2009β€u2009100) β the number of classrooms. The second line contains _M_ space-separated integers β _X__i_ (0u2009β€u2009_X__i_u2009β€u2009100) the amount of groups present in classroom _i_ during the first lesson. The third line contains _M_ space-separated integers β _Y__i_ (0u2009β€u2009_Y__i_u2009β€u2009100) the maximal amount of groups that can be present in classroom _i_ at the same time. It is guaranteed that all the _X__i_u2009β€u2009_Y__i_, and that the sum of all the _X__i_ is positive and does not exceed 1000. Output In the single line output the answer to the problem modulo 109u2009+u20097. Note In the second sample test the first and the second lessons of each group must take place in the same classroom, thatβs why the timetables will only be different in the rearrangement of the classroomsβ numbers for each group, e.g. 3!u2009=u20096. | 2,300 | true | false | false | true | false | false | false | false | false | false | 9,810 |
251E | Little Petya likes trees a lot. Recently his mother has presented him a tree with 2_n_ nodes. Petya immediately decided to place this tree on a rectangular table consisting of 2 rows and _n_ columns so as to fulfill the following conditions: 1. Each cell of the table corresponds to exactly one tree node and vice versa, each tree node corresponds to exactly one table cell. 2. If two tree nodes are connected by an edge, then the corresponding cells have a common side. Now Petya wonders how many ways are there to place his tree on the table. He calls two placements distinct if there is a tree node which corresponds to distinct table cells in these two placements. Since large numbers can scare Petya, print the answer modulo 1000000007 (109u2009+u20097). Input The first line contains a single integer _n_ (1u2009β€u2009_n_u2009β€u2009105). Next (2_n_u2009-u20091) lines contain two integers each _a__i_ and _b__i_ (1u2009β€u2009_a__i_,u2009_b__i_u2009β€u20092_n_;xa0_a__i_u2009β u2009_b__i_) that determine the numbers of the vertices connected by the corresponding edge. Consider the tree vertexes numbered by integers from 1 to 2_n_. It is guaranteed that the graph given in the input is a tree, that is, a connected acyclic undirected graph. Output Print a single integer β the required number of ways to place the tree on the table modulo 1000000007 (109u2009+u20097). Examples Input 4 1 2 2 3 3 4 4 5 5 6 6 7 7 8 Note Note to the first sample (all 12 variants to place the tree on the table are given below): 1-3-2 2-3-1 5 4 6 6 4 5 5 4 6 6 4 5 1-3-2 2-3-14-3-2 2-3-4 5-1 6 6 1-5 5-1 6 6 1-5 4-3-2 2-3-4 1-3-4 4-3-1 5 2-6 6-2 5 5 2-6 6-2 5 1-3-4 4-3-1 | 3,000 | false | false | true | true | false | false | false | false | false | false | 8,827 |
164D | Problem - 164D - Codeforces =============== xa0 so that the diameter of the set of the remaining _n_u2009-u2009_k_ points were as small as possible. The diameter of a set of points is the maximum pairwise distance between the points of the set. The diameter of a one point set equals zero. Input The first input line contains a pair of integers _n_,u2009_k_ (2u2009β€u2009_n_u2009β€u20091000, 1u2009β€u2009_k_u2009β€u200930, _k_u2009<u2009_n_) β the numbers of points on the plane and the number of points to delete, correspondingly. Next _n_ lines describe the points, one per line. Each description consists of a pair of integers _x__i_,u2009_y__i_ (0u2009β€u2009_x__i_,u2009_y__i_u2009β€u200932000) β the coordinates of the _i_-th point. The given points can coincide. Output Print _k_ different space-separated integers from 1 to _n_ β the numbers of points to delete. The points are numbered in the order, in which they are given in the input from 1 to _n_. You can print the numbers in any order. If there are multiple solutions, print any of them. Examples Input 5 2 1 2 0 0 2 2 1 1 3 3 Output 5 2 Input 4 1 0 0 0 0 1 1 1 1 Output 3 | 3,100 | false | false | false | false | false | false | true | true | false | false | 9,207 |
1601A | You are given array $$$a_1, a_2, ldots, a_n$$$, consisting of non-negative integers. Let's define operation of "elimination" with integer parameter $$$k$$$ ($$$1 leq k leq n$$$) as follows: Choose $$$k$$$ distinct array indices $$$1 leq i_1 < i_2 < ldots < i_k le n$$$. Calculate $$$x = a_{i_1} ~ & ~ a_{i_2} ~ & ~ ldots ~ & ~ a_{i_k}$$$, where $$$&$$$ denotes the . Subtract $$$x$$$ from each of $$$a_{i_1}, a_{i_2}, ldots, a_{i_k}$$$; all other elements remain untouched. Find all possible values of $$$k$$$, such that it's possible to make all elements of array $$$a$$$ equal to $$$0$$$ using a finite number of elimination operations with parameter $$$k$$$. It can be proven that exists at least one possible $$$k$$$ for any array $$$a$$$. Note that you firstly choose $$$k$$$ and only after that perform elimination operations with value $$$k$$$ you've chosen initially. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 10^4$$$). Description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$1 leq n leq 200,000$$$)xa0β the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i < 2^{30}$$$)xa0β array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$200,000$$$. Output For each test case, print all values $$$k$$$, such that it's possible to make all elements of $$$a$$$ equal to $$$0$$$ in a finite number of elimination operations with the given parameter $$$k$$$. Print them in increasing order. Example Input 5 4 4 4 4 4 4 13 7 25 19 6 3 5 3 1 7 1 1 1 5 0 0 0 0 0 Output 1 2 4 1 2 1 1 1 2 3 4 5 Note In the first test case: If $$$k = 1$$$, we can make four elimination operations with sets of indices $$${1}$$$, $$${2}$$$, $$${3}$$$, $$${4}$$$. Since $$$&$$$ of one element is equal to the element itself, then for each operation $$$x = a_i$$$, so $$$a_i - x = a_i - a_i = 0$$$. If $$$k = 2$$$, we can make two elimination operations with, for example, sets of indices $$${1, 3}$$$ and $$${2, 4}$$$: $$$x = a_1 ~ & ~ a_3$$$ $$$=$$$ $$$a_2 ~ & ~ a_4$$$ $$$=$$$ $$$4 ~ & ~ 4 = 4$$$. For both operations $$$x = 4$$$, so after the first operation $$$a_1 - x = 0$$$ and $$$a_3 - x = 0$$$, and after the second operationxa0β $$$a_2 - x = 0$$$ and $$$a_4 - x = 0$$$. If $$$k = 3$$$, it's impossible to make all $$$a_i$$$ equal to $$$0$$$. After performing the first operation, we'll get three elements equal to $$$0$$$ and one equal to $$$4$$$. After that, all elimination operations won't change anything, since at least one chosen element will always be equal to $$$0$$$. If $$$k = 4$$$, we can make one operation with set $$${1, 2, 3, 4}$$$, because $$$x = a_1 ~ & ~ a_2 ~ & ~ a_3 ~ & ~ a_4$$$ $$$= 4$$$. In the second test case, if $$$k = 2$$$ then we can make the following elimination operations: Operation with indices $$${1, 3}$$$: $$$x = a_1 ~ & ~ a_3$$$ $$$=$$$ $$$13 ~ & ~ 25 = 9$$$. $$$a_1 - x = 13 - 9 = 4$$$ and $$$a_3 - x = 25 - 9 = 16$$$. Array $$$a$$$ will become equal to $$$[4, 7, 16, 19]$$$. Operation with indices $$${3, 4}$$$: $$$x = a_3 ~ & ~ a_4$$$ $$$=$$$ $$$16 ~ & ~ 19 = 16$$$. $$$a_3 - x = 16 - 16 = 0$$$ and $$$a_4 - x = 19 - 16 = 3$$$. Array $$$a$$$ will become equal to $$$[4, 7, 0, 3]$$$. Operation with indices $$${2, 4}$$$: $$$x = a_2 ~ & ~ a_4$$$ $$$=$$$ $$$7 ~ & ~ 3 = 3$$$. $$$a_2 - x = 7 - 3 = 4$$$ and $$$a_4 - x = 3 - 3 = 0$$$. Array $$$a$$$ will become equal to $$$[4, 4, 0, 0]$$$. Operation with indices $$${1, 2}$$$: $$$x = a_1 ~ & ~ a_2$$$ $$$=$$$ $$$4 ~ & ~ 4 = 4$$$. $$$a_1 - x = 4 - 4 = 0$$$ and $$$a_2 - x = 4 - 4 = 0$$$. Array $$$a$$$ will become equal to $$$[0, 0, 0, 0]$$$. Formal definition of bitwise AND: Let's define bitwise AND ($$$&$$$) as follows. Suppose we have two non-negative integers $$$x$$$ and $$$y$$$, let's look at their binary representations (possibly, with leading zeroes): $$$x_k dots x_2 x_1 x_0$$$ and $$$y_k dots y_2 y_1 y_0$$$. Here, $$$x_i$$$ is the $$$i$$$-th bit of number $$$x$$$, and $$$y_i$$$ is the $$$i$$$-th bit of number $$$y$$$. Let $$$r = x ~ & ~ y$$$ is a result of operation $$$&$$$ on number $$$x$$$ and $$$y$$$. Then binary representation of $$$r$$$ will be $$$r_k dots r_2 r_1 r_0$$$, where: $$$$$$ r_i = begin{cases} 1, ~ ext{if} ~ x_i = 1 ~ ext{and} ~ y_i = 1 0, ~ ext{if} ~ x_i = 0 ~ ext{or} ~ y_i = 0 end{cases} $$$$$$ | 1,300 | true | true | false | false | false | false | false | false | false | false | 2,651 |
1186D | Vus the Cossack has $$$n$$$ real numbers $$$a_i$$$. It is known that the sum of all numbers is equal to $$$0$$$. He wants to choose a sequence $$$b$$$ the size of which is $$$n$$$ such that the sum of all numbers is $$$0$$$ and each $$$b_i$$$ is either $$$lfloor a_i floor$$$ or $$$lceil a_i ceil$$$. In other words, $$$b_i$$$ equals $$$a_i$$$ rounded up or down. It is not necessary to round to the nearest integer. For example, if $$$a = [4.58413, 1.22491, -2.10517, -3.70387]$$$, then $$$b$$$ can be equal, for example, to $$$[4, 2, -2, -4]$$$. Note that if $$$a_i$$$ is an integer, then there is no difference between $$$lfloor a_i floor$$$ and $$$lceil a_i ceil$$$, $$$b_i$$$ will always be equal to $$$a_i$$$. Help Vus the Cossack find such sequence! Input The first line contains one integer $$$n$$$ ($$$1 leq n leq 10^5$$$)xa0β the number of numbers. Each of the next $$$n$$$ lines contains one real number $$$a_i$$$ ($$$a_i < 10^5$$$). It is guaranteed that each $$$a_i$$$ has exactly $$$5$$$ digits after the decimal point. It is guaranteed that the sum of all the numbers is equal to $$$0$$$. Output In each of the next $$$n$$$ lines, print one integer $$$b_i$$$. For each $$$i$$$, $$$a_i-b_i<1$$$ must be met. If there are multiple answers, print any. Examples Input 4 4.58413 1.22491 -2.10517 -3.70387 Input 5 -6.32509 3.30066 -0.93878 2.00000 1.96321 Note The first example is explained in the legend. In the second example, we can round the first and fifth numbers up, and the second and third numbers down. We can round the fourth number neither up, nor down. | 1,500 | true | true | false | false | false | true | false | false | false | false | 4,769 |
366A | Nothing has changed since the last round. Dima and Inna still love each other and want to be together. They've made a deal with Seryozha and now they need to make a deal with the dorm guards... There are four guardposts in Dima's dorm. Each post contains two guards (in Russia they are usually elderly women). You can bribe a guard by a chocolate bar or a box of juice. For each guard you know the minimum price of the chocolate bar she can accept as a gift and the minimum price of the box of juice she can accept as a gift. If a chocolate bar for some guard costs less than the minimum chocolate bar price for this guard is, or if a box of juice for some guard costs less than the minimum box of juice price for this guard is, then the guard doesn't accept such a gift. In order to pass through a guardpost, one needs to bribe both guards. The shop has an unlimited amount of juice and chocolate of any price starting with 1. Dima wants to choose some guardpost, buy one gift for each guard from the guardpost and spend exactly _n_ rubles on it. Help him choose a post through which he can safely sneak Inna or otherwise say that this is impossible. Mind you, Inna would be very sorry to hear that! Input The first line of the input contains integer _n_xa0(1u2009β€u2009_n_u2009β€u2009105) β the money Dima wants to spend. Then follow four lines describing the guardposts. Each line contains four integers _a_,u2009_b_,u2009_c_,u2009_d_xa0(1u2009β€u2009_a_,u2009_b_,u2009_c_,u2009_d_u2009β€u2009105) β the minimum price of the chocolate and the minimum price of the juice for the first guard and the minimum price of the chocolate and the minimum price of the juice for the second guard, correspondingly. Output In a single line of the output print three space-separated integers: the number of the guardpost, the cost of the first present and the cost of the second present. If there is no guardpost Dima can sneak Inna through at such conditions, print -1 in a single line. The guardposts are numbered from 1 to 4 according to the order given in the input. If there are multiple solutions, you can print any of them. Examples Input 10 5 6 5 6 6 6 7 7 5 8 6 6 9 9 9 9 Input 10 6 6 6 6 7 7 7 7 4 4 4 4 8 8 8 8 Input 5 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 Note Explanation of the first example. The only way to spend 10 rubles to buy the gifts that won't be less than the minimum prices is to buy two 5 ruble chocolates to both guards from the first guardpost. Explanation of the second example. Dima needs 12 rubles for the first guardpost, 14 for the second one, 16 for the fourth one. So the only guardpost we can sneak through is the third one. So, Dima can buy 4 ruble chocolate for the first guard and 6 ruble juice of the second guard. | 1,100 | false | false | true | false | false | false | false | false | false | false | 8,370 |
1840E | You are given two strings of equal length $$$s_1$$$ and $$$s_2$$$, consisting of lowercase Latin letters, and an integer $$$t$$$. You need to answer $$$q$$$ queries, numbered from $$$1$$$ to $$$q$$$. The $$$i$$$-th query comes in the $$$i$$$-th second of time. Each query is one of three types: block the characters at position $$$pos$$$ (indexed from $$$1$$$) in both strings for $$$t$$$ seconds; swap two unblocked characters; determine if the two strings are equal at the time of the query, ignoring blocked characters. Note that in queries of the second type, the characters being swapped can be from the same string or from $$$s_1$$$ and $$$s_2$$$. Input The first line of the input contains a single integer $$$T$$$ ($$$1 le T le 10^4$$$)xa0β the number of test cases. Then follow the descriptions of the test cases. The first line of each test case contains a string $$$s_1$$$ consisting of lowercase Latin letters (length no more than $$$2 cdot 10^5$$$). The second line of each test case contains a string $$$s_2$$$ consisting of lowercase Latin letters (length no more than $$$2 cdot 10^5$$$). The strings have equal length. The third line of each test case contains two integers $$$t$$$ and $$$q$$$ ($$$1 le t, q le 2 cdot 10^5$$$). The number $$$t$$$ indicates the number of seconds for which a character is blocked. The number $$$q$$$ corresponds to the number of queries. Each of the next $$$q$$$ lines of each test case contains a single query. Each query is one of three types: "$$$1 pos$$$"xa0β block the characters at position $$$pos$$$ in both strings for $$$t$$$ seconds; "$$$2 1/;!2 pos_1 1/;!2 pos_2$$$"xa0β swap two unblocked characters. The second number in the query indicates the number of the string from which the first character for the swap is taken. The third number in the query indicates the position in that string of that character. The fourth number in the query indicates the number of the string from which the second character for the swap is taken. The fifth number in the query indicates the position in that string of that character; "$$$3$$$"xa0β determine if the two strings are equal at the time of the query, ignoring blocked characters. For queries of the first type, it is guaranteed that at the time of the query, the characters at position $$$pos$$$ are not blocked. For queries of the second type, it is guaranteed that the characters being swapped are not blocked. All values of $$$pos, pos_1, pos_2$$$ are in the range from $$$1$$$ to the length of the strings. The sum of the values of $$$q$$$ over all test cases, as well as the total length of the strings $$$s_1$$$, does not exceed $$$2 cdot 10^5$$$. Output For each query of the third type, output "YES" if the two strings $$$s_1$$$ and $$$s_2$$$ are equal at the time of the query, ignoring blocked characters, and "NO" otherwise. You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes" and "YES" will be accepted as a positive answer. Example Input 2 codeforces codeblocks 5 7 3 1 5 1 6 1 7 1 9 3 3 cool club 2 5 2 1 2 2 3 2 2 2 2 4 1 2 3 3 Note Let's look at the strings $$$s_1$$$ and $$$s_2$$$ after each of the $$$q$$$ queries. Blocked characters will be denoted in red. First example input: ($$$codeforces$$$, $$$codeblocks$$$) $$$ ightarrow$$$ ($$$codeforces$$$, $$$codeblocks$$$) $$$ ightarrow$$$ ($$$codecolor{red}{f}orces$$$, $$$codecolor{red}{b}locks$$$) $$$ ightarrow$$$ ($$$codecolor{red}{fo}rces$$$, $$$codecolor{red}{bl}ocks$$$) $$$ ightarrow$$$ ($$$codecolor{red}{for}ces$$$, $$$codecolor{red}{blo}cks$$$) $$$ ightarrow$$$ ($$$codecolor{red}{for}ccolor{red}{e}s$$$, $$$codecolor{red}{blo}ccolor{red}{k}s$$$) $$$ ightarrow$$$ ($$$codecolor{red}{for}ccolor{red}{e}s$$$, $$$codecolor{red}{blo}ccolor{red}{k}s$$$) $$$ ightarrow$$$ ($$$codefcolor{red}{or}ccolor{red}{e}s$$$, $$$codebcolor{red}{lo}ccolor{red}{k}s$$$) Second example input: ($$$cool$$$, $$$club$$$) $$$ ightarrow$$$ ($$$cuol$$$, $$$clob$$$) $$$ ightarrow$$$ ($$$cuol$$$, $$$cbol$$$) $$$ ightarrow$$$ ($$$ccolor{red}{u}ol$$$, $$$ccolor{red}{b}ol$$$) $$$ ightarrow$$$ ($$$ccolor{red}{u}ol$$$, $$$ccolor{red}{b}ol$$$) $$$ ightarrow$$$ ($$$cuol$$$, $$$cbol$$$) | 1,600 | false | false | true | false | true | false | false | false | false | false | 1,249 |
583B | Robot Doc is located in the hall, with _n_ computers stand in a line, numbered from left to right from 1 to _n_. Each computer contains exactly one piece of information, each of which Doc wants to get eventually. The computers are equipped with a security system, so to crack the _i_-th of them, the robot needs to collect at least _a__i_ any pieces of information from the other computers. Doc can hack the computer only if he is right next to it. The robot is assembled using modern technologies and can move along the line of computers in either of the two possible directions, but the change of direction requires a large amount of resources from Doc. Tell the minimum number of changes of direction, which the robot will have to make to collect all _n_ parts of information if initially it is next to computer with number 1. It is guaranteed that there exists at least one sequence of the robot's actions, which leads to the collection of all information. Initially Doc doesn't have any pieces of information. Input The first line contains number _n_ (1u2009β€u2009_n_u2009β€u20091000). The second line contains _n_ non-negative integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009β€u2009_a__i_u2009<u2009_n_), separated by a space. It is guaranteed that there exists a way for robot to collect all pieces of the information. Output Print a single number β the minimum number of changes in direction that the robot will have to make in order to collect all _n_ parts of information. Note In the first sample you can assemble all the pieces of information in the optimal manner by assembling first the piece of information in the first computer, then in the third one, then change direction and move to the second one, and then, having 2 pieces of information, collect the last piece. In the second sample to collect all the pieces of information in the optimal manner, Doc can go to the fourth computer and get the piece of information, then go to the fifth computer with one piece and get another one, then go to the second computer in the same manner, then to the third one and finally, to the first one. Changes of direction will take place before moving from the fifth to the second computer, then from the second to the third computer, then from the third to the first computer. In the third sample the optimal order of collecting parts from computers can look like that: 1->3->4->6->2->5->7. | 1,200 | false | true | true | false | false | false | false | false | false | false | 7,511 |
1491I | After realizing that Zookeeper is just a duck, the animals have overthrown Zookeeper. They now have to decide a new ruler among themselves through a fighting tournament of the following format: Initially, animal $$$0$$$ is king, while everyone else queues up with animal $$$1$$$ at the front of the queue and animal $$$n-1$$$ at the back. The animal at the front of the queue will challenge the king to a fight, and the animal with greater strength will win the fight. The winner will become king, while the loser joins the back of the queue. An animal who wins $$$3$$$ times consecutively will be crowned ruler for the whole zoo. The strength of each animal depends on how many consecutive fights he won. Animal $$$i$$$ has strength $$$A_i$$$ with $$$0$$$ consecutive win, $$$B_i$$$ with $$$1$$$ consecutive win, and $$$C_i$$$ with $$$2$$$ consecutive wins. Initially, everyone has $$$0$$$ consecutive win. For all animals, $$$A_i > B_i$$$ and $$$C_i > B_i$$$. Also, the values of $$$A_i$$$, $$$B_i$$$, $$$C_i$$$ are distinct (all $$$3n$$$ values are pairwise different). In other words, an animal who is not a king has strength $$$A_i$$$. A king usually has a strength of $$$B_i$$$ or $$$C_i$$$. The exception is on the first turn, the first king (animal $$$0$$$) has strength $$$A_i$$$. Who is the new ruler, and after how many fights? Or will it end up that animals fight forever with no one ending up as ruler? Input The first line contains one integer $$$n$$$ ($$$4 leq n leq 6000$$$) β number of the animals. $$$i$$$-th of the next $$$n$$$ lines contains $$$3$$$ integers $$$A_i$$$, $$$B_i$$$ and $$$C_i$$$ ($$$0 leq A_i, B_i, C_i leq 10^9$$$). It is guaranteed that $$$A_i > B_i$$$ and $$$C_i > B_i$$$, and that all values of $$$A_i$$$, $$$B_i$$$ and $$$C_i$$$ are distinct. Output Output two integers in a single line. The first is the index of the animal that will become ruler, and the second is the number of fights passed until some animal becomes the ruler. If the animals will fight for infinitely long, output -1 -1 instead. Note The following describes the sequence of events for the second sample. Note that in fight $$$1$$$, the king (animal $$$0$$$) has strength $$$A_0$$$. The tournament ends at fight $$$7$$$ as animal $$$1$$$ wins fight $$$5$$$, $$$6$$$ and $$$7$$$. | 3,500 | false | false | false | false | true | false | true | false | false | false | 3,214 |
500E | Celebrating the new year, many people post videos of falling dominoes; Here's a list of them: ainta, who lives in a 2D world, is going to post a video as well. There are _n_ dominoes on a 2D Cartesian plane. _i_-th domino (1u2009β€u2009_i_u2009β€u2009_n_) can be represented as a line segment which is parallel to the _y_-axis and whose length is _l__i_. The lower point of the domino is on the _x_-axis. Let's denote the _x_-coordinate of the _i_-th domino as _p__i_. Dominoes are placed one after another, so _p_1u2009<u2009_p_2u2009<u2009...u2009<u2009_p__n_u2009-u20091u2009<u2009_p__n_ holds. User ainta wants to take a video of falling dominoes. To make dominoes fall, he can push a single domino to the right. Then, the domino will fall down drawing a circle-shaped orbit until the line segment totally overlaps with the x-axis. Also, if the _s_-th domino touches the _t_-th domino while falling down, the _t_-th domino will also fall down towards the right, following the same procedure above. Domino _s_ touches domino _t_ if and only if the segment representing _s_ and _t_ intersects. See the picture above. If he pushes the leftmost domino to the right, it falls down, touching dominoes (A), (B) and (C). As a result, dominoes (A), (B), (C) will also fall towards the right. However, domino (D) won't be affected by pushing the leftmost domino, but eventually it will fall because it is touched by domino (C) for the first time. The picture above is an example of falling dominoes. Each red circle denotes a touch of two dominoes. User ainta has _q_ plans of posting the video. _j_-th of them starts with pushing the _x__j_-th domino, and lasts until the _y__j_-th domino falls. But sometimes, it could be impossible to achieve such plan, so he has to lengthen some dominoes. It costs one dollar to increase the length of a single domino by 1. User ainta wants to know, for each plan, the minimum cost needed to achieve it. Plans are processed independently, i. e. if domino's length is increased in some plan, it doesn't affect its length in other plans. Set of dominos that will fall except _x__j_-th domino and _y__j_-th domino doesn't matter, but the initial push should be on domino _x__j_. Input The first line contains an integer _n_ (2u2009β€u2009_n_u2009β€u20092u2009Γu2009105)β the number of dominoes. Next _n_ lines describe the dominoes. The _i_-th line (1u2009β€u2009_i_u2009β€u2009_n_) contains two space-separated integers _p__i_, _l__i_ (1u2009β€u2009_p__i_,u2009_l__i_u2009β€u2009109)β the _x_-coordinate and the length of the _i_-th domino. It is guaranteed that _p_1u2009<u2009_p_2u2009<u2009...u2009<u2009_p__n_u2009-u20091u2009<u2009_p__n_. The next line contains an integer _q_ (1u2009β€u2009_q_u2009β€u20092u2009Γu2009105) β the number of plans. Next _q_ lines describe the plans. The _j_-th line (1u2009β€u2009_j_u2009β€u2009_q_) contains two space-separated integers _x__j_, _y__j_ (1u2009β€u2009_x__j_u2009<u2009_y__j_u2009β€u2009_n_). It means the _j_-th plan is, to push the _x__j_-th domino, and shoot a video until the _y__j_-th domino falls. Note Consider the example. The dominoes are set like the picture below. Let's take a look at the 4th plan. To make the 6th domino fall by pushing the 2nd domino, the length of the 3rd domino (whose x-coordinate is 4) should be increased by 1, and the 5th domino (whose x-coordinate is 9) should be increased by 1 (other option is to increase 4th domino instead of 5th also by 1). Then, the dominoes will fall like in the picture below. Each cross denotes a touch between two dominoes. | 2,300 | false | false | false | true | true | false | false | false | false | false | 7,833 |
930E | Arkady and Kirill visited an exhibition of rare coins. The coins were located in a row and enumerated from left to right from 1 to _k_, each coin either was laid with its obverse (front) side up, or with its reverse (back) side up. Arkady and Kirill made some photos of the coins, each photo contained a segment of neighboring coins. Akrady is interested in obverses, so on each photo made by him there is at least one coin with obverse side up. On the contrary, Kirill is interested in reverses, so on each photo made by him there is at least one coin with its reverse side up. The photos are lost now, but Arkady and Kirill still remember the bounds of the segments of coins each photo contained. Given this information, compute the remainder of division by 109u2009+u20097 of the number of ways to choose the upper side of each coin in such a way, that on each Arkady's photo there is at least one coin with obverse side up, and on each Kirill's photo there is at least one coin with reverse side up. Input The first line contains three integers _k_, _n_ and _m_ (1u2009β€u2009_k_u2009β€u2009109, 0u2009β€u2009_n_,u2009_m_u2009β€u2009105)xa0β the total number of coins, the number of photos made by Arkady, and the number of photos made by Kirill, respectively. The next _n_ lines contain the descriptions of Arkady's photos, one per line. Each of these lines contains two integers _l_ and _r_ (1u2009β€u2009_l_u2009β€u2009_r_u2009β€u2009_k_), meaning that among coins from the _l_-th to the _r_-th there should be at least one with obverse side up. The next _m_ lines contain the descriptions of Kirill's photos, one per line. Each of these lines contains two integers _l_ and _r_ (1u2009β€u2009_l_u2009β€u2009_r_u2009β€u2009_k_), meaning that among coins from the _l_-th to the _r_-th there should be at least one with reverse side up. Output Print the only linexa0β the number of ways to choose the side for each coin modulo 109u2009+u20097u2009=u20091000000007. Examples Input 5 3 2 1 3 2 2 3 5 2 2 4 5 Input 60 5 7 1 3 50 60 1 60 30 45 20 40 4 5 6 37 5 18 50 55 22 27 25 31 44 45 Note In the first example the following ways are possible ('O'xa0β obverse, 'R'xa0β reverse side): OROOR, ORORO, ORORR, RROOR, RRORO, RRORR, ORROR, ORRRO. In the second example the information is contradictory: the second coin should have obverse and reverse sides up at the same time, that is impossible. So, the answer is 0. | 2,900 | true | false | false | true | true | false | false | false | false | false | 5,982 |
958E2 | Princess Heidi decided to give orders to all her _K_ Rebel ship commanders in person. Unfortunately, she is currently travelling through hyperspace, and will leave it only at _N_ specific moments _t_1,u2009_t_2,u2009...,u2009_t__N_. The meetings with commanders must therefore start and stop at those times. Namely, each commander will board her ship at some time _t__i_ and disembark at some later time _t__j_. Of course, Heidi needs to meet with all commanders, and no two meetings can be held during the same time. Two commanders cannot even meet at the beginnings/endings of the hyperspace jumps, because too many ships in one position could give out their coordinates to the enemy. Your task is to find minimum time that Princess Heidi has to spend on meetings, with her schedule satisfying the conditions above. Input The first line contains two integers _K_, _N_ (2u2009β€u20092_K_u2009β€u2009_N_u2009β€u2009500000, _K_u2009β€u20095000). The second line contains _N_ distinct integers _t_1,u2009_t_2,u2009...,u2009_t__N_ (1u2009β€u2009_t__i_u2009β€u2009109) representing the times when Heidi leaves hyperspace. Note In the first example, there are five valid schedules: [1,u20094],u2009[6,u20097] with total time 4, [1,u20094],u2009[6,u200912] with total time 9, [1,u20094],u2009[7,u200912] with total time 8, [1,u20096],u2009[7,u200912] with total time 10, and [4,u20096],u2009[7,u200912] with total time 7. So the answer is 4. In the second example, there is only 1 valid schedule: [1,u20092],u2009[3,u20094],u2009[5,u20096]. For the third example, one possible schedule with total time 6 is: [1,u20093],u2009[4,u20095],u2009[14,u200915],u2009[23,u200925]. | 2,200 | false | true | false | true | false | false | false | true | true | false | 5,882 |
1633A | You are given an integer $$$n$$$. You have to change the minimum number of digits in it in such a way that the resulting number does not have any leading zeroes and is divisible by $$$7$$$. If there are multiple ways to do it, print any of them. If the given number is already divisible by $$$7$$$, leave it unchanged. Input The first line contains one integer $$$t$$$ ($$$1 le t le 990$$$) β the number of test cases. Then the test cases follow, each test case consists of one line containing one integer $$$n$$$ ($$$10 le n le 999$$$). Output For each test case, print one integer without any leading zeroes β the result of your changes (i.u2009e. the integer that is divisible by $$$7$$$ and can be obtained by changing the minimum possible number of digits in $$$n$$$). If there are multiple ways to apply changes, print any resulting number. If the given number is already divisible by $$$7$$$, just print it. Note In the first test case of the example, $$$42$$$ is already divisible by $$$7$$$, so there's no need to change it. In the second test case of the example, there are multiple answers β $$$28$$$, $$$21$$$ or $$$63$$$. In the third test case of the example, other possible answers are $$$357$$$, $$$371$$$ and $$$378$$$. Note that you cannot print $$$077$$$ or $$$77$$$. | 800 | false | false | false | false | false | false | true | false | false | false | 2,452 |
1025A | Panic is rising in the committee for doggo standardizationxa0β the puppies of the new brood have been born multi-colored! In total there are 26 possible colors of puppies in the nature and they are denoted by letters from 'a' to 'z' inclusive. The committee rules strictly prohibit even the smallest diversity between doggos and hence all the puppies should be of the same color. Thus Slava, the committee employee, has been assigned the task to recolor some puppies into other colors in order to eliminate the difference and make all the puppies have one common color. Unfortunately, due to bureaucratic reasons and restricted budget, there's only one operation Slava can perform: he can choose a color $$$x$$$ such that there are currently at least two puppies of color $$$x$$$ and recolor all puppies of the color $$$x$$$ into some arbitrary color $$$y$$$. Luckily, this operation can be applied multiple times (including zero). For example, if the number of puppies is $$$7$$$ and their colors are represented as the string "abababc", then in one operation Slava can get the results "zbzbzbc", "bbbbbbc", "aaaaaac", "acacacc" and others. However, if the current color sequence is "abababc", then he can't choose $$$x$$$='c' right now, because currently only one puppy has the color 'c'. Help Slava and the committee determine whether it is possible to standardize all the puppies, i.e. after Slava's operations all the puppies should have the same color. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0β the number of puppies. The second line contains a string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters, where the $$$i$$$-th symbol denotes the $$$i$$$-th puppy's color. Output If it's possible to recolor all puppies into one color, print "Yes". Otherwise print "No". Output the answer without quotation signs. Note In the first example Slava can perform the following steps: 1. take all puppies of color 'a' (a total of two) and recolor them into 'b'; 2. take all puppies of color 'd' (a total of two) and recolor them into 'c'; 3. take all puppies of color 'b' (three puppies for now) and recolor them into 'c'. In the second example it's impossible to recolor any of the puppies. In the third example all the puppies' colors are the same; thus there's no need to recolor anything. | 900 | false | false | true | false | false | false | false | false | true | false | 5,583 |
724A | Problem - 724A - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags implementation *1000 No tag edit access β Contest materials + Photos") if such situation is possible during some non-leap year. Otherwise, print "NO" (without quotes). Examples Input monday tuesday Output NO Input sunday sunday Output YES Input saturday tuesday Output YES Note In the second sample, one can consider February 1 and March 1 of year 2015. Both these days were Sundays. In the third sample, one can consider July 1 and August 1 of year 2017. First of these two days is Saturday, while the second one is Tuesday. | 1,000 | false | false | true | false | false | false | false | false | false | false | 6,914 |
590D | A top-secret military base under the command of Colonel Zuev is expecting an inspection from the Ministry of Defence. According to the charter, each top-secret military base must include a top-secret troop that should... well, we cannot tell you exactly what it should do, it is a top secret troop at the end. The problem is that Zuev's base is missing this top-secret troop for some reasons. The colonel decided to deal with the problem immediately and ordered to line up in a single line all _n_ soldiers of the base entrusted to him. Zuev knows that the loquacity of the _i_-th soldier from the left is equal to _q__i_. Zuev wants to form the top-secret troop using _k_ leftmost soldiers in the line, thus he wants their total loquacity to be as small as possible (as the troop should remain top-secret). To achieve this, he is going to choose a pair of consecutive soldiers and swap them. He intends to do so no more than _s_ times. Note that any soldier can be a participant of such swaps for any number of times. The problem turned out to be unusual, and colonel Zuev asked you to help. Determine, what is the minimum total loquacity of the first _k_ soldiers in the line, that can be achieved by performing no more than _s_ swaps of two consecutive soldiers. Input The first line of the input contains three positive integers _n_, _k_, _s_ (1u2009β€u2009_k_u2009β€u2009_n_u2009β€u2009150, 1u2009β€u2009_s_u2009β€u2009109)xa0β the number of soldiers in the line, the size of the top-secret troop to be formed and the maximum possible number of swap operations of the consecutive pair of soldiers, respectively. The second line of the input contains _n_ integer _q__i_ (1u2009β€u2009_q__i_u2009β€u20091u2009000u2009000)xa0β the values of loquacity of soldiers in order they follow in line from left to right. Output Print a single integer β the minimum possible total loquacity of the top-secret troop. Note In the first sample Colonel has to swap second and third soldiers, he doesn't really need the remaining swap. The resulting soldiers order is: (2, 1, 4). Minimum possible summary loquacity of the secret troop is 3. In the second sample Colonel will perform swaps in the following order: 1. (10, 1, 6 β 2, 5) 2. (10, 1, 2, 6 β 5) The resulting soldiers order is (10, 1, 2, 5, 6). Minimum possible summary loquacity is equal to 18. | 2,300 | false | false | false | true | false | false | false | false | false | false | 7,486 |
36A | Problem - 36A - Codeforces =============== xa0 and Codeforces Beta Round #36") ") ") ") ") ") β amount of days during which Vasya checked if there were any signals. The second line contains _n_ characters 1 or 0 β the record Vasya kept each of those _n_ days. Itβs guaranteed that the given record sequence contains at least three 1s. Output If Vasya has found extra-terrestrial intelligence, output YES, otherwise output NO. Examples Input 8 00111000 Output YES Input 7 1001011 Output NO Input 7 1010100 Output YES | 1,300 | false | false | true | false | false | false | false | false | false | false | 9,818 |
1083B | Recently, the Fair Nut has written $$$k$$$ strings of length $$$n$$$, consisting of letters "a" and "b". He calculated $$$c$$$xa0β the number of strings that are prefixes of at least one of the written strings. Every string was counted only one time. Then, he lost his sheet with strings. He remembers that all written strings were lexicographically not smaller than string $$$s$$$ and not bigger than string $$$t$$$. He is interested: what is the maximum value of $$$c$$$ that he could get. 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 two integers $$$n$$$ and $$$k$$$ ($$$1 leq n leq 5 cdot 10^5$$$, $$$1 leq k leq 10^9$$$). The second line contains a string $$$s$$$ ($$$s = n$$$)xa0β the string consisting of letters "a" and "b. The third line contains a string $$$t$$$ ($$$t = n$$$)xa0β the string consisting of letters "a" and "b. It is guaranteed that string $$$s$$$ is lexicographically not bigger than $$$t$$$. Output Print one numberxa0β maximal value of $$$c$$$. Note In the first example, Nut could write strings "aa", "ab", "ba", "bb". These $$$4$$$ strings are prefixes of at least one of the written strings, as well as "a" and "b". Totally, $$$6$$$ strings. In the second example, Nut could write strings "aba", "baa", "bba". In the third example, there are only two different strings that Nut could write. If both of them are written, $$$c=8$$$. | 2,000 | false | true | false | false | false | false | false | false | false | false | 5,307 |
1339B | You have array of $$$n$$$ numbers $$$a_{1}, a_{2}, ldots, a_{n}$$$. Rearrange these numbers to satisfy $$$a_{1} - a_{2} le a_{2} - a_{3} le ldots le a_{n-1} - a_{n}$$$, where $$$x$$$ denotes absolute value of $$$x$$$. It's always possible to find such rearrangement. Note that all numbers in $$$a$$$ are not necessarily different. In other words, some numbers of $$$a$$$ may be same. You have to answer independent $$$t$$$ test cases. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^{4}$$$)xa0β the number of test cases. The first line of each test case contains single integer $$$n$$$ ($$$3 le n le 10^{5}$$$)xa0β the length of array $$$a$$$. It is guaranteed that the sum of values of $$$n$$$ over all test cases in the input does not exceed $$$10^{5}$$$. The second line of each test case contains $$$n$$$ integers $$$a_{1}, a_{2}, ldots, a_{n}$$$ ($$$-10^{9} le a_{i} le 10^{9}$$$). Output For each test case, print the rearranged version of array $$$a$$$ which satisfies given condition. If there are multiple valid rearrangements, print any of them. Example Input 2 6 5 -2 4 8 6 5 4 8 1 4 2 Output 5 5 4 6 8 -2 1 2 4 8 Note In the first test case, after given rearrangement, $$$a_{1} - a_{2} = 0 le a_{2} - a_{3} = 1 le a_{3} - a_{4} = 2 le a_{4} - a_{5} = 2 le a_{5} - a_{6} = 10$$$. There are other possible answers like "5 4 5 6 -2 8". In the second test case, after given rearrangement, $$$a_{1} - a_{2} = 1 le a_{2} - a_{3} = 2 le a_{3} - a_{4} = 4$$$. There are other possible answers like "2 4 8 1". | 1,200 | false | false | false | false | false | true | false | false | true | false | 4,008 |
1904D2 | This is the hard version of the problem. The only differences between the two versions of this problem are the constraints on $$$n$$$ and the time limit. You can make hacks only if all versions of the problem are solved. You are given two arrays $$$a$$$ and $$$b$$$ of length $$$n$$$. You can perform the following operation some (possibly zero) times: 1. choose $$$l$$$ and $$$r$$$ such that $$$1 leq l leq r leq n$$$. 2. let $$$x=max(a_l,a_{l+1},ldots,a_r)$$$. 3. for all $$$l leq i leq r$$$, set $$$a_i := x$$$. Determine if you can make array $$$a$$$ equal to array $$$b$$$. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0β the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0β the length of the arrays. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$)xa0β the elements of array $$$a$$$. The third line contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 le b_i le n$$$)xa0β the elements of array $$$b$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output "YES" (without quotes) if you can make $$$a$$$ into $$$b$$$ using any number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yES", "yes" and "Yes" will be recognized as a positive response). Example Input 5 5 1 2 3 2 4 1 3 3 2 4 5 3 4 2 2 4 3 4 3 4 4 5 3 2 1 1 1 3 3 3 2 2 2 1 1 1 2 3 1 1 2 2 1 2 Note In the first test case, we can achieve array $$$b$$$ by applying a single operation: $$$(l,r)=(2,3)$$$. In the second test case, it can be shown we cannot achieve array $$$b$$$ in any amount of operations. In the third test case, we can achieve array $$$b$$$ by applying two operations: $$$(l,r)=(2,5)$$$. followed by $$$(l,r)=(1,3)$$$. In the fourth and fifth test cases, it can be shown we cannot achieve array $$$b$$$ in any amount of operations. | 1,800 | false | true | true | false | true | true | false | false | true | false | 886 |
514C | Watto, the owner of a spare parts store, has recently got an order for the mechanism that can process strings in a certain way. Initially the memory of the mechanism is filled with _n_ strings. Then the mechanism should be able to process queries of the following type: "Given string _s_, determine if the memory of the mechanism contains string _t_ that consists of the same number of characters as _s_ and differs from _s_ in exactly one position". Watto has already compiled the mechanism, all that's left is to write a program for it and check it on the data consisting of _n_ initial lines and _m_ queries. He decided to entrust this job to you. Input The first line contains two non-negative numbers _n_ and _m_ (0u2009β€u2009_n_u2009β€u20093Β·105, 0u2009β€u2009_m_u2009β€u20093Β·105) β the number of the initial strings and the number of queries, respectively. Next follow _n_ non-empty strings that are uploaded to the memory of the mechanism. Next follow _m_ non-empty strings that are the queries to the mechanism. The total length of lines in the input doesn't exceed 6Β·105. Each line consists only of letters 'a', 'b', 'c'. Output For each query print on a single line "YES" (without the quotes), if the memory of the mechanism contains the required string, otherwise print "NO" (without the quotes). Examples Input 2 3 aaaaa acacaca aabaa ccacacc caaac | 2,000 | false | false | false | false | true | false | false | true | false | false | 7,778 |
883K | Mayor of city S just hates trees and lawns. They take so much space and there could be a road on the place they occupy! The Mayor thinks that one of the main city streets could be considerably widened on account of lawn nobody needs anyway. Moreover, that might help reduce the car jams which happen from time to time on the street. The street is split into _n_ equal length parts from left to right, the _i_-th part is characterized by two integers: width of road _s__i_ and width of lawn _g__i_. For each of _n_ parts the Mayor should decide the size of lawn to demolish. For the _i_-th part he can reduce lawn width by integer _x__i_ (0u2009β€u2009_x__i_u2009β€u2009_g__i_). After it new road width of the _i_-th part will be equal to _s_'_i_u2009=u2009_s__i_u2009+u2009_x__i_ and new lawn width will be equal to _g_'_i_u2009=u2009_g__i_u2009-u2009_x__i_. On the one hand, the Mayor wants to demolish as much lawn as possible (and replace it with road). On the other hand, he does not want to create a rapid widening or narrowing of the road, which would lead to car accidents. To avoid that, the Mayor decided that width of the road for consecutive parts should differ by at most 1, i.e. for each _i_ (1u2009β€u2009_i_u2009<u2009_n_) the inequation _s_'_i_u2009+u20091u2009-u2009_s_'_i_u2009β€u20091 should hold. Initially this condition might not be true. You need to find the the total width of lawns the Mayor will destroy according to his plan. Input The first line contains integer _n_ (1u2009β€u2009_n_u2009β€u20092Β·105) β number of parts of the street. Each of the following _n_ lines contains two integers _s__i_,u2009_g__i_ (1u2009β€u2009_s__i_u2009β€u2009106, 0u2009β€u2009_g__i_u2009β€u2009106) β current width of road and width of the lawn on the _i_-th part of the street. Output In the first line print the total width of lawns which will be removed. In the second line print _n_ integers _s_'1,u2009_s_'2,u2009...,u2009_s_'_n_ (_s__i_u2009β€u2009_s_'_i_u2009β€u2009_s__i_u2009+u2009_g__i_) β new widths of the road starting from the first part and to the last. If there is no solution, print the only integer -1 in the first line. Examples Input 4 1 100 100 1 1 100 100 1 | 1,800 | false | true | true | false | false | true | false | false | false | false | 6,217 |
799C | Arkady plays Gardenscapes a lot. Arkady wants to build two new fountains. There are _n_ available fountains, for each fountain its beauty and cost are known. There are two types of money in the game: coins and diamonds, so each fountain cost can be either in coins or diamonds. No money changes between the types are allowed. Help Arkady to find two fountains with maximum total beauty so that he can buy both at the same time. Input The first line contains three integers _n_, _c_ and _d_ (2u2009β€u2009_n_u2009β€u2009100u2009000, 0u2009β€u2009_c_,u2009_d_u2009β€u2009100u2009000)xa0β the number of fountains, the number of coins and diamonds Arkady has. The next _n_ lines describe fountains. Each of these lines contain two integers _b__i_ and _p__i_ (1u2009β€u2009_b__i_,u2009_p__i_u2009β€u2009100u2009000)xa0β the beauty and the cost of the _i_-th fountain, and then a letter "C" or "D", describing in which type of money is the cost of fountain _i_: in coins or in diamonds, respectively. Output Print the maximum total beauty of exactly two fountains Arkady can build. If he can't build two fountains, print 0. Examples Input 3 7 6 10 8 C 4 3 C 5 6 D Input 3 10 10 5 5 C 5 5 C 10 11 D Note In the first example Arkady should build the second fountain with beauty 4, which costs 3 coins. The first fountain he can't build because he don't have enough coins. Also Arkady should build the third fountain with beauty 5 which costs 6 diamonds. Thus the total beauty of built fountains is 9. In the second example there are two fountains, but Arkady can't build both of them, because he needs 5 coins for the first fountain, and Arkady has only 4 coins. | 1,800 | false | false | true | false | true | false | false | true | false | false | 6,574 |
1257B | Recently Petya walked in the forest and found a magic stick. Since Petya really likes numbers, the first thing he learned was spells for changing numbers. So far, he knows only two spells that can be applied to a positive integer: 1. If the chosen number $$$a$$$ is even, then the spell will turn it into $$$frac{3a}{2}$$$; 2. If the chosen number $$$a$$$ is greater than one, then the spell will turn it into $$$a-1$$$. Note that if the number is even and greater than one, then Petya can choose which spell to apply. Petya now has only one number $$$x$$$. He wants to know if his favorite number $$$y$$$ can be obtained from $$$x$$$ using the spells he knows. The spells can be used any number of times in any order. It is not required to use spells, Petya can leave $$$x$$$ as it is. Input The first line contains single integer $$$T$$$ ($$$1 le T le 10^4$$$) β the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 le x, y le 10^9$$$) β the current number and the number that Petya wants to get. Output For the $$$i$$$-th test case print the answer on it β YES if Petya can get the number $$$y$$$ from the number $$$x$$$ using known spells, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). Example Input 7 2 3 1 1 3 6 6 8 1 2 4 1 31235 6578234 Output YES YES NO YES NO YES YES | 1,000 | true | false | false | false | false | false | false | false | false | false | 4,400 |
1189C | Consider a sequence of digits of length $$$2^k$$$ $$$[a_1, a_2, ldots, a_{2^k}]$$$. We perform the following operation with it: replace pairs $$$(a_{2i+1}, a_{2i+2})$$$ with $$$(a_{2i+1} + a_{2i+2})bmod 10$$$ for $$$0le i<2^{k-1}$$$. For every $$$i$$$ where $$$a_{2i+1} + a_{2i+2}ge 10$$$ we get a candy! As a result, we will get a sequence of length $$$2^{k-1}$$$. Less formally, we partition sequence of length $$$2^k$$$ into $$$2^{k-1}$$$ pairs, each consisting of 2 numbers: the first pair consists of the first and second numbers, the second of the third and fourth $$$ldots$$$, the last pair consists of the ($$$2^k-1$$$)-th and ($$$2^k$$$)-th numbers. For every pair such that sum of numbers in it is at least $$$10$$$, we get a candy. After that, we replace every pair of numbers with a remainder of the division of their sum by $$$10$$$ (and don't change the order of the numbers). Perform this operation with a resulting array until it becomes of length $$$1$$$. Let $$$f([a_1, a_2, ldots, a_{2^k}])$$$ denote the number of candies we get in this process. For example: if the starting sequence is $$$[8, 7, 3, 1, 7, 0, 9, 4]$$$ then: After the first operation the sequence becomes $$$[(8 + 7)bmod 10, (3 + 1)bmod 10, (7 + 0)bmod 10, (9 + 4)bmod 10]$$$ $$$=$$$ $$$[5, 4, 7, 3]$$$, and we get $$$2$$$ candies as $$$8 + 7 ge 10$$$ and $$$9 + 4 ge 10$$$. After the second operation the sequence becomes $$$[(5 + 4)bmod 10, (7 + 3)bmod 10]$$$ $$$=$$$ $$$[9, 0]$$$, and we get one more candy as $$$7 + 3 ge 10$$$. After the final operation sequence becomes $$$[(9 + 0) bmod 10]$$$ $$$=$$$ $$$[9]$$$. Therefore, $$$f([8, 7, 3, 1, 7, 0, 9, 4]) = 3$$$ as we got $$$3$$$ candies in total. You are given a sequence of digits of length $$$n$$$ $$$s_1, s_2, ldots s_n$$$. You have to answer $$$q$$$ queries of the form $$$(l_i, r_i)$$$, where for $$$i$$$-th query you have to output $$$f([s_{l_i}, s_{l_i+1}, ldots, s_{r_i}])$$$. It is guaranteed that $$$r_i-l_i+1$$$ is of form $$$2^k$$$ for some nonnegative integer $$$k$$$. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0β the length of the sequence. The second line contains $$$n$$$ digits $$$s_1, s_2, ldots, s_n$$$ ($$$0 le s_i le 9$$$). The third line contains a single integer $$$q$$$ ($$$1 le q le 10^5$$$)xa0β the number of queries. Each of the next $$$q$$$ lines contains two integers $$$l_i$$$, $$$r_i$$$ ($$$1 le l_i le r_i le n$$$)xa0β $$$i$$$-th query. It is guaranteed that $$$r_i-l_i+1$$$ is a nonnegative integer power of $$$2$$$. Note The first example illustrates an example from the statement. $$$f([7, 3, 1, 7]) = 1$$$: sequence of operations is $$$[7, 3, 1, 7] o [(7 + 3)bmod 10, (1 + 7)bmod 10]$$$ $$$=$$$ $$$[0, 8]$$$ and one candy as $$$7 + 3 ge 10$$$ $$$ o$$$ $$$[(0 + 8) bmod 10]$$$ $$$=$$$ $$$[8]$$$, so we get only $$$1$$$ candy. $$$f([9]) = 0$$$ as we don't perform operations with it. | 1,400 | true | false | true | true | true | false | false | false | false | false | 4,751 |
1463B | You are given an array $$$[a_1, a_2, dots, a_n]$$$ such that $$$1 le a_i le 10^9$$$. Let $$$S$$$ be the sum of all elements of the array $$$a$$$. Let's call an array $$$b$$$ of $$$n$$$ integers beautiful if: $$$1 le b_i le 10^9$$$ for each $$$i$$$ from $$$1$$$ to $$$n$$$; for every pair of adjacent integers from the array $$$(b_i, b_{i + 1})$$$, either $$$b_i$$$ divides $$$b_{i + 1}$$$, or $$$b_{i + 1}$$$ divides $$$b_i$$$ (or both); $$$2 sum limits_{i = 1}^{n} a_i - b_i le S$$$. Your task is to find any beautiful array. It can be shown that at least one beautiful array always exists. 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 one integer $$$n$$$ ($$$2 le n le 50$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$). Output For each test case, print the beautiful array $$$b_1, b_2, dots, b_n$$$ ($$$1 le b_i le 10^9$$$) on a separate line. It can be shown that at least one beautiful array exists under these circumstances. If there are multiple answers, print any of them. Example Input 4 5 1 2 3 4 5 2 4 6 2 1 1000000000 6 3 4 8 1 2 3 Output 3 3 3 3 3 3 6 1 1000000000 4 4 8 1 3 3 | 1,400 | false | true | false | false | false | true | false | false | false | false | 3,372 |
2009G2 | This is the hard version of the problem. In this version, it is guaranteed that $$$r geq l+k-1$$$ for all queries. For an arbitrary array $$$b$$$, Yunli can perform the following operation any number of times: Select an index $$$i$$$. Set $$$b_i = x$$$ where $$$x$$$ is any integer she desires ($$$x$$$ is not limited to the interval $$$[1,n]$$$). Denote $$$f(b)$$$ as the minimum number of operations she needs to perform until there exists a consecutive subarray$$$^{ ext{β}}$$$ of length at least $$$k$$$ in $$$b$$$. Yunli is given an array $$$a$$$ of size $$$n$$$ and asks you $$$q$$$ queries. In each query, you must output $$$sum_{j=l+k-1}^{r} f([a_l, a_{l+1}, ldots, a_j])$$$. $$$^{ ext{β}}$$$If there exists a consecutive subarray of length $$$k$$$ that starts at index $$$i$$$ ($$$1 leq i leq b-k+1$$$), then $$$b_j = b_{j-1} + 1$$$ for all $$$i < j leq i+k-1$$$. Input The first line contains $$$t$$$ ($$$1 leq t leq 10^4$$$) β the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$k$$$, and $$$q$$$ ($$$1 leq k leq n leq 2 cdot 10^5$$$, $$$1 leq q leq 2 cdot 10^5$$$) β the length of the array, the length of the consecutive subarray, and the number of queries. The following line contains $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$ ($$$1 leq a_i leq n$$$). The following $$$q$$$ lines contain two integers $$$l$$$ and $$$r$$$ ($$$1 leq l leq r leq n$$$, $$$r geq l+k-1$$$) β the bounds of the query. It is guaranteed the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$ and the sum of $$$q$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output Output $$$sum_{j=l+k-1}^{r} f([a_l, a_{l+1}, ldots, a_j])$$$ for each query on a new line. Example Input 3 7 5 3 1 2 3 2 1 2 3 1 7 2 7 3 7 8 4 2 4 3 1 1 2 4 3 2 3 6 1 5 5 4 2 4 5 1 2 3 1 4 1 5 Note In the second query of the first testcase, we calculate the following function values: $$$f([2,3,2,1,2])=3$$$ because Yunli can set $$$b_3=4$$$, $$$b_4=5$$$, and $$$b_5=6$$$, making a consecutive subarray of size $$$5$$$ in $$$3$$$ moves. $$$f([2,3,2,1,2,3])=2$$$ because we can set $$$b_3=0$$$ and $$$b_2=-1$$$, making a consecutive subarray of size $$$5$$$ in $$$2$$$ moves (starting at position $$$2$$$) The answer to this query is $$$3+2=5$$$. | 2,200 | false | false | false | true | true | false | false | true | false | false | 193 |
164C | One remarkable day company "X" received _k_ machines. And they were not simple machines, they were mechanical programmers! This was the last unsuccessful step before switching to android programmers, but that's another story. The company has now _n_ tasks, for each of them we know the start time of its execution _s__i_, the duration of its execution _t__i_, and the company profit from its completion _c__i_. Any machine can perform any task, exactly one at a time. If a machine has started to perform the task, it is busy at all moments of time from _s__i_ to _s__i_u2009+u2009_t__i_u2009-u20091, inclusive, and it cannot switch to another task. You are required to select a set of tasks which can be done with these _k_ machines, and which will bring the maximum total profit. Input The first line contains two integer numbers _n_ and _k_ (1u2009β€u2009_n_u2009β€u20091000, 1u2009β€u2009_k_u2009β€u200950) β the numbers of tasks and machines, correspondingly. The next _n_ lines contain space-separated groups of three integers _s__i_,u2009_t__i_,u2009_c__i_ (1u2009β€u2009_s__i_,u2009_t__i_u2009β€u2009109, 1u2009β€u2009_c__i_u2009β€u2009106), _s__i_ is the time where they start executing the _i_-th task, _t__i_ is the duration of the _i_-th task and _c__i_ is the profit of its execution. Output Print _n_ integers _x_1,u2009_x_2,u2009...,u2009_x__n_. Number _x__i_ should equal 1, if task _i_ should be completed and otherwise it should equal 0. If there are several optimal solutions, print any of them. Examples Input 5 2 1 5 4 1 4 5 1 3 2 4 1 2 5 6 1 Note In the first sample the tasks need to be executed at moments of time 2 ... 8, 1 ... 3 and 4 ... 4, correspondingly. The first task overlaps with the second and the third ones, so we can execute either task one (profit 5) or tasks two and three (profit 6). | 2,400 | false | false | false | false | false | false | false | false | false | true | 9,208 |
1426C | Initially, you have the array $$$a$$$ consisting of one element $$$1$$$ ($$$a = [1]$$$). In one move, you can do one of the following things: Increase some (single) element of $$$a$$$ by $$$1$$$ (choose some $$$i$$$ from $$$1$$$ to the current length of $$$a$$$ and increase $$$a_i$$$ by one); Append the copy of some (single) element of $$$a$$$ to the end of the array (choose some $$$i$$$ from $$$1$$$ to the current length of $$$a$$$ and append $$$a_i$$$ to the end of the array). For example, consider the sequence of five moves: 1. You take the first element $$$a_1$$$, append its copy to the end of the array and get $$$a = [1, 1]$$$. 2. You take the first element $$$a_1$$$, increase it by $$$1$$$ and get $$$a = [2, 1]$$$. 3. You take the second element $$$a_2$$$, append its copy to the end of the array and get $$$a = [2, 1, 1]$$$. 4. You take the first element $$$a_1$$$, append its copy to the end of the array and get $$$a = [2, 1, 1, 2]$$$. 5. You take the fourth element $$$a_4$$$, increase it by $$$1$$$ and get $$$a = [2, 1, 1, 3]$$$. Your task is to find the minimum number of moves required to obtain the array with the sum at least $$$n$$$. 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. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 le n le 10^9$$$) β the lower bound on the sum of the array. Output For each test case, print the answer: the minimum number of moves required to obtain the array with the sum at least $$$n$$$. Example Input 5 1 5 42 1337 1000000000 | 1,100 | true | false | false | false | false | true | false | true | false | false | 3,537 |
1592D | This is an interactive problem! In the last regional contest Hemose, ZeyadKhattab and YahiaSherif β members of the team Carpe Diem β did not qualify to ICPC because of some unknown reasons. Hemose was very sad and had a bad day after the contest, but ZeyadKhattab is very wise and knows Hemose very well, and does not want to see him sad. Zeyad knows that Hemose loves tree problems, so he gave him a tree problem with a very special device. Hemose has a weighted tree with $$$n$$$ nodes and $$$n-1$$$ edges. Unfortunately, Hemose doesn't remember the weights of edges. Let's define $$$Dist(u, v)$$$ for $$$u eq v$$$ as the greatest common divisor of the weights of all edges on the path from node $$$u$$$ to node $$$v$$$. Hemose has a special device. Hemose can give the device a set of nodes, and the device will return the largest $$$Dist$$$ between any two nodes from the set. More formally, if Hemose gives the device a set $$$S$$$ of nodes, the device will return the largest value of $$$Dist(u, v)$$$ over all pairs $$$(u, v)$$$ with $$$u$$$, $$$v$$$ $$$in$$$ $$$S$$$ and $$$u eq v$$$. Hemose can use this Device at most $$$12$$$ times, and wants to find any two distinct nodes $$$a$$$, $$$b$$$, such that $$$Dist(a, b)$$$ is maximum possible. Can you help him? Interaction Begin the interaction from reading a single integer $$$n$$$ ($$$2 le n le 10^3$$$) β the number of nodes in the tree. Next, read $$$n-1$$$ lines. The $$$i$$$-th of the next $$$n-1$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 leq u_i, v_i leq n$$$, $$$u_i eq v_i$$$), which means that there's an edge between nodes $$$u_i$$$ and $$$v_i$$$. It's guaranteed that weights of edges were $$$leq 10^9$$$. It is guaranteed that the given graph is a tree. Now you may begin asking queries. To ask a query about a set of $$$k$$$ nodes $$$v_1, v_2, ldots, v_k$$$ ($$$2 le k le n$$$, $$$1 le v_i le n$$$, all $$$v_i$$$ are distinct), output: ? $$$k$$$ $$$v_1$$$ $$$v_2$$$ $$$ldots$$$ $$$v_k$$$ You will then receive an integer $$$x$$$, the largest $$$Dist(v_i, v_j)$$$ over $$$1 le i, j le k$$$ with $$$i eq j$$$. When you have found $$$a$$$ and $$$b$$$ ($$$1 le a, b le n)$$$, $$$a eq b$$$) such that $$$Dist(a, b)$$$ is the maximum possible, print the answer in the following format: ! $$$a$$$ $$$b$$$ Outputting answer doesn't count towards the limit of $$$12$$$ queries. If there are several pairs $$$(a, b)$$$ with the same largest $$$Dist(a, b)$$$, you can output any. 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. Hacks To hack a solution, use the following format. The first line should contain a single integer $$$n$$$ $$$(2 leq n le 10^3)$$$ β the number of nodes. The $$$i$$$-th of the next $$$n-1$$$ lines should contain three integers $$$u_i$$$, $$$v_i$$$, $$$w_i$$$ ($$$1 le u_i, v_i le n$$$, $$$u_i e v_i$$$, $$$1 le w le 10^9$$$), which means that there's an edge between nodes $$$u_i$$$ and $$$v_i$$$ with weight $$$w_i$$$. These $$$n-1$$$ edges must form a tree. | 2,300 | true | false | true | false | false | false | false | true | false | false | 2,690 |
294B | Problem - 294B - 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 dp greedy *1700 No tag edit access β Contest materials ") . Each of the next _n_ lines contains two integers _t__i_ and _w__i_ denoting the thickness and width of the _i_-th book correspondingly, (1u2009β€u2009_t__i_u2009β€u20092,u20091u2009β€u2009_w__i_u2009β€u2009100). Output On the only line of the output print the minimum total thickness of the vertical books that we can achieve. Examples Input 5 1 12 1 3 2 15 2 5 2 1 Output 5 Input 3 1 10 2 1 2 4 Output 3 | 1,700 | false | true | false | true | false | false | false | false | false | false | 8,655 |
1842G | Yet another random problem. Tenzing has an array $$$a$$$ of length $$$n$$$ and an integer $$$v$$$. Tenzing will perform the following operation $$$m$$$ times: 1. Choose an integer $$$i$$$ such that $$$1 leq i leq n$$$ uniformly at random. 2. For all $$$j$$$ such that $$$i leq j leq n$$$, set $$$a_j := a_j + v$$$. Tenzing wants to know the expected value of $$$prod_{i=1}^n a_i$$$ after performing the $$$m$$$ operations, modulo $$$10^9+7$$$. Formally, let $$$M = 10^9+7$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q ot equiv 0 pmod{M}$$$. Output the integer equal to $$$p cdot q^{-1} bmod M$$$. In other words, output the integer $$$x$$$ that $$$0 le x < M$$$ and $$$x cdot q equiv p pmod{M}$$$. Input The first line of input contains three integers $$$n$$$, $$$m$$$ and $$$v$$$ ($$$1leq nleq 5000$$$, $$$1leq m,vleq 10^9$$$). The second line of input contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$1leq a_ileq 10^9$$$). Output Output the expected value of $$$prod_{i=1}^n a_i$$$ modulo $$$10^9+7$$$. Note There are three types of $$$a$$$ after performing all the $$$m$$$ operations : 1. $$$a_1=2,a_2=12$$$ with $$$frac{1}{4}$$$ probability. 2. $$$a_1=a_2=12$$$ with $$$frac{1}{4}$$$ probability. 3. $$$a_1=7,a_2=12$$$ with $$$frac{1}{2}$$$ probability. So the expected value of $$$a_1cdot a_2$$$ is $$$frac{1}{4}cdot (24+144) + frac{1}{2}cdot 84=84$$$. | 2,800 | true | false | false | true | false | false | false | false | false | false | 1,233 |
665B | Ayush is a cashier at the shopping center. Recently his department has started a ''click and collect" service which allows users to shop online. The store contains _k_ items. _n_ customers have already used the above service. Each user paid for _m_ items. Let _a__ij_ denote the _j_-th item in the _i_-th person's order. Due to the space limitations all the items are arranged in one single row. When Ayush receives the _i_-th order he will find one by one all the items _a__ij_ (1u2009β€u2009_j_u2009β€u2009_m_) in the row. Let _pos_(_x_) denote the position of the item _x_ in the row at the moment of its collection. Then Ayush takes time equal to _pos_(_a__i_1)u2009+u2009_pos_(_a__i_2)u2009+u2009...u2009+u2009_pos_(_a__im_) for the _i_-th customer. When Ayush accesses the _x_-th element he keeps a new stock in the front of the row and takes away the _x_-th element. Thus the values are updating. Your task is to calculate the total time it takes for Ayush to process all the orders. You can assume that the market has endless stock. Input The first line contains three integers _n_, _m_ and _k_ (1u2009β€u2009_n_,u2009_k_u2009β€u2009100,u20091u2009β€u2009_m_u2009β€u2009_k_) β the number of users, the number of items each user wants to buy and the total number of items at the market. The next line contains _k_ distinct integers _p__l_ (1u2009β€u2009_p__l_u2009β€u2009_k_) denoting the initial positions of the items in the store. The items are numbered with integers from 1 to _k_. Each of the next _n_ lines contains _m_ distinct integers _a__ij_ (1u2009β€u2009_a__ij_u2009β€u2009_k_) β the order of the _i_-th person. Note Customer 1 wants the items 1 and 5. _pos_(1)u2009=u20093, so the new positions are: [1,u20093,u20094,u20092,u20095]. _pos_(5)u2009=u20095, so the new positions are: [5,u20091,u20093,u20094,u20092]. Time taken for the first customer is 3u2009+u20095u2009=u20098. Customer 2 wants the items 3 and 1. _pos_(3)u2009=u20093, so the new positions are: [3,u20095,u20091,u20094,u20092]. _pos_(1)u2009=u20093, so the new positions are: [1,u20093,u20095,u20094,u20092]. Time taken for the second customer is 3u2009+u20093u2009=u20096. Total time is 8u2009+u20096u2009=u200914. Formally _pos_(_x_) is the index of _x_ in the current row. | 1,400 | false | false | false | false | false | false | true | false | false | false | 7,160 |
1776A | One of the SWERC judges has a dog named Boy. Besides being a good competitive programmer, Boy loves fresh air, so she wants to be walked at least twice a day. Walking Boy requires $$$120$$$ consecutive minutes. Two walks cannot overlap, but one can start as soon as the previous one has finished. Boy before and after getting ACCEPTED on this problem. Today, the judge sent $$$n$$$ messages to the SWERC Discord server. The $$$i$$$-th message was sent $$$a_i$$$ minutes after midnight. You know that, when walking Boy, the judge does not send any messages, but he can send a message right before or right after a walk. Is it possible that the judge walked Boy at least twice today? Note that a day has $$$1440$$$ minutes, and a walk is considered to happen today if it starts at a minute $$$s ge 0$$$ and ends right before a minute $$$e le 1440$$$. In that case, it must hold that $$$e - s = 120$$$ and, for every $$$i = 1, , 2 , dots, , n$$$, either $$$a_i le s$$$ or $$$a_i ge e$$$. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 le t le 100$$$) β the number of test cases. The descriptions of the $$$t$$$ test cases follow. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 100$$$) β the number of messages sent by the judge. The second line of each test case contains $$$n$$$ integers $$$a_1, , a_2, , dots, , a_n$$$ ($$$0 le a_1 < a_2 < cdots < a_n < 1440$$$) β the times at which the messages have been sent (in minutes elapsed from midnight). Output For each test case, output one line containing $$$ exttt{YES}$$$ if it is possible that Boy has been walked at least twice, and $$$ exttt{NO}$$$ otherwise. Example Input 6 14 100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 12 100 200 300 400 600 700 800 900 1100 1200 1300 1400 13 100 200 300 400 500 600 700 800 900 1100 1200 1300 1400 13 101 189 272 356 463 563 659 739 979 1071 1170 1274 1358 1 42 5 0 1 2 3 4 Output NO YES NO YES YES YES Note In the first test case, the judge has sent a message at each time multiple of $$$100$$$ (excluding $$$0$$$). It is impossible that he has walked Boy even once. In the second test case, the times are the same as above, but $$$500$$$ and $$$1000$$$ are missing. The judge could have walked Boy, for instance, during the time intervals $$$[440, 560]$$$ and $$$[980, 1100]$$$. The situation is illustrated in the picture below, where the walks are represented by green intervals. $$$$$$$$$$$$ In the third test case, the times are the same as in the first test case, but $$$1000$$$ is missing. The judge could have walked Boy at most once. In the fourth test case, Boy could have been walked during the time intervals $$$[739, 859]$$$ and $$$[859, 979]$$$. | 800 | false | true | false | false | false | false | false | false | false | false | 1,628 |
177C2 | To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings. More formally, for each invited person the following conditions should be fulfilled: all his friends should also be invited to the party; the party shouldn't have any people he dislikes; all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people _a_1 and _a__p_ are connected through a chain of common friends if there exists a sequence of people _a_2,u2009_a_3,u2009...,u2009_a__p_u2009-u20091 such that all pairs of people _a__i_ and _a__i_u2009+u20091 (1u2009β€u2009_i_u2009<u2009_p_) are friends. Help the Beaver find the maximum number of acquaintances he can invite. Input The first line of input contains an integer _n_ β the number of the Beaver's acquaintances. The second line contains an integer _k_ β the number of pairs of friends. Next _k_ lines contain space-separated pairs of integers _u__i_,u2009_v__i_ β indices of people who form the _i_-th pair of friends. The next line contains an integer _m_ β the number of pairs of people who dislike each other. Next _m_ lines describe pairs of people who dislike each other in the same format as the pairs of friends were described. Each pair of people is mentioned in the input at most once . In particular, two persons cannot be friends and dislike each other at the same time. The input limitations for getting 30 points are: 2u2009β€u2009_n_u2009β€u200914 The input limitations for getting 100 points are: 2u2009β€u2009_n_u2009β€u20092000 Output Output a single number β the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0. Examples Input 9 8 1 2 1 3 2 3 4 5 6 7 7 8 8 9 9 6 2 1 6 7 9 Note Let's have a look at the example. Two groups of people can be invited: {1,u20092,u20093} and {4,u20095}, thus the answer will be the size of the largest of these groups. Group {6,u20097,u20098,u20099} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1,u20092,u20093,u20094,u20095} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected). | 1,500 | false | false | false | false | false | false | true | false | false | true | 9,149 |
1775D | Mars is home to an unusual species of spidersxa0β Binary spiders. Right now, Martian scientists are observing a colony of $$$n$$$ spiders, the $$$i$$$-th of which has $$$a_i$$$ legs. Some of the spiders are friends with each other. Namely, the $$$i$$$-th and $$$j$$$-th spiders are friends if $$$gcd(a_i, a_j) e 1$$$, i.xa0e., there is some integer $$$k ge 2$$$ such that $$$a_i$$$ and $$$a_j$$$ are simultaneously divided by $$$k$$$ without a remainder. Here $$$gcd(x, y)$$$ denotes the = 1$$$. But he can send a message through the spider with six legs because $$$gcd(8, 6) = 2$$$ and $$$gcd(6, 15) = 3$$$. Thus, the message will arrive in two seconds. Right now, scientists are observing how the $$$s$$$-th spider wants to send a message to the $$$t$$$-th spider. The researchers have a hypothesis that spiders always transmit messages optimally. For this reason, scientists would need a program that could calculate the minimum time to send a message and also deduce one of the optimal routes. Input The first line of input contains an integer $$$n$$$ ($$$2 le n le 3cdot10^5$$$)xa0β the number of spiders in the colony. The second line of input contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 3cdot10^5$$$)xa0β the number of legs the spiders have. The third line of input contains two integers $$$s$$$ and $$$t$$$ ($$$1 le s, t le n$$$)xa0βthe spiders between which the message must be sent. Output If it is impossible to transmit a message between the given pair of spiders, print $$$-1$$$. Otherwise, in the first line of the output print the integer $$$t$$$ ($$$t ge 1$$$)xa0β the number of spiders that participate in the message transmission (i.xa0e. the minimum time of message delivery in seconds plus one). In the second line, print $$$t$$$ different integers $$$b_1, b_2, ldots, b_t$$$ ($$$1 le b_i le n$$$)xa0β the ids of the spiders through which the message should follow, in order from sender to receiver. If there are several optimal routes for the message, output any of them. Note The first example is shown above. It shows that the message from the $$$5$$$-th spider (with eight legs) to the $$$6$$$-th spider (with $$$15$$$ legs) is optimal to pass through the $$$4$$$-th spider (with six legs). In the second example, the spider number $$$7$$$ (with $$$11$$$ legs) is not friends with anyone, so it is impossible to send him a message. | 1,800 | true | false | false | false | false | false | false | false | false | true | 1,631 |
767E | Student Arseny likes to plan his life for _n_ days ahead. He visits a canteen every day and he has already decided what he will order in each of the following _n_ days. Prices in the canteen do not change and that means Arseny will spend _c__i_ rubles during the _i_-th day. There are 1-ruble coins and 100-ruble notes in circulation. At this moment, Arseny has _m_ coins and a sufficiently large amount of notes (you can assume that he has an infinite amount of them). Arseny loves modern technologies, so he uses his credit card everywhere except the canteen, but he has to pay in cash in the canteen because it does not accept cards. Cashier always asks the student to pay change-free. However, it's not always possible, but Arseny tries to minimize the dissatisfaction of the cashier. Cashier's dissatisfaction for each of the days is determined by the total amount of notes and coins in the change. To be precise, if the cashier gives Arseny _x_ notes and coins on the _i_-th day, his dissatisfaction for this day equals _x_Β·_w__i_. Cashier always gives change using as little coins and notes as possible, he always has enough of them to be able to do this. "Caution! Angry cashier" Arseny wants to pay in such a way that the total dissatisfaction of the cashier for _n_ days would be as small as possible. Help him to find out how he needs to pay in each of the _n_ days! Note that Arseny always has enough money to pay, because he has an infinite amount of notes. Arseny can use notes and coins he received in change during any of the following days. Input The first line contains two integers _n_ and _m_ (1u2009β€u2009_n_u2009β€u2009105, 0u2009β€u2009_m_u2009β€u2009109)xa0β the amount of days Arseny planned his actions for and the amount of coins he currently has. The second line contains a sequence of integers _c_1,u2009_c_2,u2009...,u2009_c__n_ (1u2009β€u2009_c__i_u2009β€u2009105)xa0β the amounts of money in rubles which Arseny is going to spend for each of the following days. The third line contains a sequence of integers _w_1,u2009_w_2,u2009...,u2009_w__n_ (1u2009β€u2009_w__i_u2009β€u2009105)xa0β the cashier's dissatisfaction coefficients for each of the following days. Output In the first line print one integerxa0β minimum possible total dissatisfaction of the cashier. Then print _n_ lines, the _i_-th of then should contain two numbersxa0β the amount of notes and the amount of coins which Arseny should use to pay in the canteen on the _i_-th day. Of course, the total amount of money Arseny gives to the casher in any of the days should be no less than the amount of money he has planned to spend. It also shouldn't exceed 106 rubles: Arseny never carries large sums of money with him. If there are multiple answers, print any of them. Examples Input 5 42 117 71 150 243 200 1 1 1 1 1 Output 79 1 17 1 0 2 0 2 43 2 0 Input 5 42 117 71 150 243 200 5 4 3 2 1 Output 230 1 17 1 0 1 50 3 0 2 0 | 2,400 | false | true | false | false | false | false | false | false | false | false | 6,714 |
1602A | You are given a string $$$s$$$. You need to find two non-empty strings $$$a$$$ and $$$b$$$ such that the following conditions are satisfied: 1. Strings $$$a$$$ and $$$b$$$ are both subsequences of $$$s$$$. 2. For each index $$$i$$$, character $$$s_i$$$ of string $$$s$$$ must belong to exactly one of strings $$$a$$$ or $$$b$$$. 3. String $$$a$$$ is lexicographically minimum possible; string $$$b$$$ may be any possible string. Given string $$$s$$$, print any valid $$$a$$$ and $$$b$$$. Reminder: A string $$$a$$$ ($$$b$$$) is a subsequence of a string $$$s$$$ if $$$a$$$ ($$$b$$$) can be obtained from $$$s$$$ by deletion of several (possibly, zero) elements. For example, "dores", "cf", and "for" are subsequences of "codeforces", while "decor" and "fork" are not. A string $$$x$$$ is lexicographically smaller than a string $$$y$$$ if and only if one of the following holds: $$$x$$$ is a prefix of $$$y$$$, but $$$x e y$$$; in the first position where $$$x$$$ and $$$y$$$ differ, the string $$$x$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$y$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1000$$$). Description of the test cases follows. The first and only line of each test case contains one string $$$s$$$ ($$$2 le s le 100$$$ where $$$s$$$ means the length of $$$s$$$). String $$$s$$$ consists of lowercase Latin letters. Output For each test case, print the strings $$$a$$$ and $$$b$$$ that satisfy the given conditions. If there are multiple answers, print any. Example Input 3 fc aaaa thebrightboiler Output c f a aaa b therightboiler Note In the first test case, there are only two choices: either $$$a =$$$ f and $$$b = $$$ c or $$$a = $$$ c and $$$b = $$$ f. And $$$a = $$$c is lexicographically smaller than $$$a = $$$ f. In the second test case, a is the only character in the string. In the third test case, it can be proven that b is the lexicographically smallest subsequence of $$$s$$$. The second string can be of two variants; one of them is given here. | 800 | false | false | true | false | false | false | false | false | false | false | 2,645 |
322B | Problem - 322B - 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 math *1600 No tag edit access β Contest materials β the number of red, green and blue flowers. Output Print the maximal number of bouquets Fox Ciel can make. Examples Input 3 6 9 Output 6 Input 4 4 4 Output 4 Input 0 0 0 Output 0 Note In test case 1, we can make 1 red bouquet, 2 green bouquets and 3 blue bouquets. In test case 2, we can make 1 red, 1 green, 1 blue and 1 mixing bouquet. | 1,600 | true | false | false | false | false | false | false | false | false | false | 8,545 |
869A | Rock... Paper! After Karen have found the deterministic winning (losing?) strategy for rock-paper-scissors, her brother, Koyomi, comes up with a new game as a substitute. The game works as follows. A positive integer _n_ is decided first. Both Koyomi and Karen independently choose _n_ distinct positive integers, denoted by _x_1,u2009_x_2,u2009...,u2009_x__n_ and _y_1,u2009_y_2,u2009...,u2009_y__n_ respectively. They reveal their sequences, and repeat until all of 2_n_ integers become distinct, which is the only final state to be kept and considered. Then they count the number of ordered pairs (_i_,u2009_j_) (1u2009β€u2009_i_,u2009_j_u2009β€u2009_n_) such that the value _x__i_ xor _y__j_ equals to one of the 2_n_ integers. Here xor means the β the length of both sequences. The second line contains _n_ space-separated integers _x_1,u2009_x_2,u2009...,u2009_x__n_ (1u2009β€u2009_x__i_u2009β€u20092Β·106) β the integers finally chosen by Koyomi. The third line contains _n_ space-separated integers _y_1,u2009_y_2,u2009...,u2009_y__n_ (1u2009β€u2009_y__i_u2009β€u20092Β·106) β the integers finally chosen by Karen. Input guarantees that the given 2_n_ integers are pairwise distinct, that is, no pair (_i_,u2009_j_) (1u2009β€u2009_i_,u2009_j_u2009β€u2009_n_) exists such that one of the following holds: _x__i_u2009=u2009_y__j_; _i_u2009β u2009_j_ and _x__i_u2009=u2009_x__j_; _i_u2009β u2009_j_ and _y__i_u2009=u2009_y__j_. Output Output one line β the name of the winner, that is, "Koyomi" or "Karen" (without quotes). Please be aware of the capitalization. Examples Input 5 2 4 6 8 10 9 7 5 3 1 Note In the first example, there are 6 pairs satisfying the constraint: (1,u20091), (1,u20092), (2,u20091), (2,u20093), (3,u20092) and (3,u20093). Thus, Karen wins since 6 is an even number. In the second example, there are 16 such pairs, and Karen wins again. | 1,100 | false | false | true | false | false | false | true | false | false | false | 6,266 |
682D | After returned from forest, Alyona started reading a book. She noticed strings _s_ and _t_, lengths of which are _n_ and _m_ respectively. As usual, reading bored Alyona and she decided to pay her attention to strings _s_ and _t_, which she considered very similar. Alyona has her favourite positive integer _k_ and because she is too small, _k_ does not exceed 10. The girl wants now to choose _k_ disjoint non-empty substrings of string _s_ such that these strings appear as disjoint substrings of string _t_ and in the same order as they do in string _s_. She is also interested in that their length is maximum possible among all variants. Formally, Alyona wants to find a sequence of _k_ non-empty strings _p_1,u2009_p_2,u2009_p_3,u2009...,u2009_p__k_ satisfying following conditions: _s_ can be represented as concatenation _a_1_p_1_a_2_p_2... _a__k__p__k__a__k_u2009+u20091, where _a_1,u2009_a_2,u2009...,u2009_a__k_u2009+u20091 is a sequence of arbitrary strings (some of them may be possibly empty); _t_ can be represented as concatenation _b_1_p_1_b_2_p_2... _b__k__p__k__b__k_u2009+u20091, where _b_1,u2009_b_2,u2009...,u2009_b__k_u2009+u20091 is a sequence of arbitrary strings (some of them may be possibly empty); sum of the lengths of strings in sequence is maximum possible. Please help Alyona solve this complicated problem and find at least the sum of the lengths of the strings in a desired sequence. A substring of a string is a subsequence of consecutive characters of the string. Input In the first line of the input three integers _n_, _m_, _k_ (1u2009β€u2009_n_,u2009_m_u2009β€u20091000, 1u2009β€u2009_k_u2009β€u200910) are givenxa0β the length of the string _s_, the length of the string _t_ and Alyona's favourite number respectively. The second line of the input contains string _s_, consisting of lowercase English letters. The third line of the input contains string _t_, consisting of lowercase English letters. Output In the only line print the only non-negative integerxa0β the sum of the lengths of the strings in a desired sequence. It is guaranteed, that at least one desired sequence exists. Examples Input 9 12 4 bbaaababb abbbabbaaaba Note The following image describes the answer for the second sample case: | 1,900 | false | false | false | true | false | false | false | false | false | false | 7,095 |
986E | Let the main characters of this problem be personages from some recent movie. New Avengers seem to make a lot of buzz. I didn't watch any part of the franchise and don't know its heroes well, but it won't stop me from using them in this problem statement. So, Thanos and Dr. Strange are doing their superhero and supervillain stuff, but then suddenly they stumble across a regular competitive programming problem. You are given a tree with $$$n$$$ vertices. In each vertex $$$v$$$ there is positive integer $$$a_{v}$$$. You have to answer $$$q$$$ queries. Each query has a from $$$u$$$ $$$v$$$ $$$x$$$. You have to calculate $$$prod_{w in P} gcd(x, a_{w}) mod (10^{9} + 7)$$$, where $$$P$$$ is a set of vertices on path from $$$u$$$ to $$$v$$$. In other words, you are to calculate the product of $$$gcd(x, a_{w})$$$ for all vertices $$$w$$$ on the path from $$$u$$$ to $$$v$$$. As it might be large, compute it modulo $$$10^9+7$$$. Here $$$gcd(s, t)$$$ denotes the greatest common divisor of $$$s$$$ and $$$t$$$. Note that the numbers in vertices do not change after queries. I suppose that you are more interested in superhero business of Thanos and Dr. Strange than in them solving the problem. So you are invited to solve this problem instead of them. Input In the first line of input there is one integer $$$n$$$ ($$$1 le n le 10^{5}$$$)xa0β the size of the tree. In the next $$$n-1$$$ lines the edges of the tree are described. The $$$i$$$-th edge is described with two integers $$$u_{i}$$$ and $$$v_{i}$$$ ($$$1 le u_{i}, v_{i} le n$$$) and it connects the vertices $$$u_{i}$$$ and $$$v_{i}$$$. It is guaranteed that graph with these edges is a tree. In the next line there are $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_{v} le 10^{7}$$$). In the next line there is one integer $$$q$$$ ($$$1 le q le 10^{5}$$$)xa0β the number of queries. And in the next $$$q$$$ lines the queries are described. Each query is described with three integers $$$u_{i}$$$, $$$v_{i}$$$ and $$$x_{i}$$$ ($$$1 le u_{i}, v_{i} le n$$$, $$$1 le x_{i} le 10^{7}$$$). Output Print $$$q$$$ numbersxa0β the answers to the queries in the order they are given in the input. Print each answer modulo $$$10^9+7 = 1000000007$$$. Print each number on a separate line. Examples Input 4 1 2 1 3 1 4 6 4 9 5 3 2 3 6 2 3 2 3 4 7 Input 6 1 2 2 3 2 4 1 5 5 6 100000 200000 500000 40000 800000 250000 3 3 5 10000000 6 2 3500000 4 1 64000 Output 196000 12250 999998215 | 2,800 | true | false | false | false | true | false | true | false | false | false | 5,768 |
1783G | You are given a tree of $$$n$$$ vertices and $$$n - 1$$$ edges. The $$$i$$$-th vertex has an initial weight $$$a_i$$$. Let the distance $$$d_v(u)$$$ from vertex $$$v$$$ to vertex $$$u$$$ be the number of edges on the path from $$$v$$$ to $$$u$$$. Note that $$$d_v(u) = d_u(v)$$$ and $$$d_v(v) = 0$$$. Let the weighted distance $$$w_v(u)$$$ from $$$v$$$ to $$$u$$$ be $$$w_v(u) = d_v(u) + a_u$$$. Note that $$$w_v(v) = a_v$$$ and $$$w_v(u) eq w_u(v)$$$ if $$$a_u eq a_v$$$. Analogically to usual distance, let's define the eccentricity $$$e(v)$$$ of vertex $$$v$$$ as the greatest weighted distance from $$$v$$$ to any other vertex (including $$$v$$$ itself), or $$$e(v) = maxlimits_{1 le u le n}{w_v(u)}$$$. Finally, let's define the radius $$$r$$$ of the tree as the minimum eccentricity of any vertex, or $$$r = minlimits_{1 le v le n}{e(v)}$$$. You need to perform $$$m$$$ queries of the following form: $$$v_j$$$ $$$x_j$$$xa0β assign $$$a_{v_j} = x_j$$$. After performing each query, print the radius $$$r$$$ of the current tree. Input The first line contains the single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0β the number of vertices in the tree. The second line contains $$$n$$$ integers $$$a_1, dots, a_n$$$ ($$$0 le a_i le 10^6$$$)xa0β the initial weights of vertices. Next $$$n - 1$$$ lines contain edges of tree. The $$$i$$$-th line contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i, v_i le n$$$; $$$u_i eq v_i$$$)xa0β the corresponding edge. The given edges form a tree. The next line contains the single integer $$$m$$$ ($$$1 le m le 10^5$$$)xa0β the number of queries. Next $$$m$$$ lines contain queriesxa0β one query per line. The $$$j$$$-th query contains two integers $$$v_j$$$ and $$$x_j$$$ ($$$1 le v_j le n$$$; $$$0 le x_j le 10^6$$$)xa0β a vertex and it's new weight. Output Print $$$m$$$ integersxa0β the radius $$$r$$$ of the tree after performing each query. Example Input 6 1 3 3 7 0 1 2 1 1 3 1 4 5 4 4 6 5 4 7 4 0 2 5 5 10 5 5 Note After the first query, you have the following tree: The marked vertex in the picture is the vertex with minimum $$$e(v)$$$, or $$$r = e(4) = 7$$$. The eccentricities of the other vertices are the following: $$$e(1) = 8$$$, $$$e(2) = 9$$$, $$$e(3) = 9$$$, $$$e(5) = 8$$$, $$$e(6) = 8$$$. The tree after the second query: The radius $$$r = e(1) = 4$$$. After the third query, the radius $$$r = e(2) = 5$$$: | 2,800 | false | false | true | false | true | false | false | false | false | false | 1,573 |
409H | , separated by a single space. Output Output the sum of the given integers. | 1,500 | false | false | true | false | false | true | true | false | false | false | 8,184 |
44F | Anfisa the monkey got disappointed in word processors as they aren't good enough at reflecting all the range of her emotions, that's why she decided to switch to graphics editors. Having opened the BerPaint, she saw a white rectangle _W_u2009Γu2009_H_ in size which can be painted on. First Anfisa learnt to navigate the drawing tool which is used to paint segments and quickly painted on that rectangle a certain number of black-colored segments. The resulting picture didn't seem bright enough to Anfisa, that's why she turned her attention to the "fill" tool which is used to find a point on the rectangle to paint and choose a color, after which all the area which is the same color as the point it contains, is completely painted the chosen color. Having applied the fill several times, Anfisa expressed her emotions completely and stopped painting. Your task is by the information on the painted segments and applied fills to find out for every color the total area of the areas painted this color after all the fills. Input The first input line has two integers _W_ and _H_ (3u2009β€u2009_W_,u2009_H_u2009β€u2009104) β the sizes of the initially white rectangular painting area. The second line contains integer _n_ β the number of black segments (0u2009β€u2009_n_u2009β€u2009100). On the next _n_ lines are described the segments themselves, each of which is given by coordinates of their endpoints _x_1,u2009_y_1,u2009_x_2,u2009_y_2 (0u2009<u2009_x_1,u2009_x_2u2009<u2009_W_,u20090u2009<u2009_y_1,u2009_y_2u2009<u2009_H_). All segments have non-zero length. The next line contains preset number of fills _m_ (0u2009β€u2009_m_u2009β€u2009100). Each of the following _m_ lines defines the fill operation in the form of "_x_ _y_ _color_", where (_x_,u2009_y_) are the coordinates of the chosen point (0u2009<u2009_x_u2009<u2009_W_,u20090u2009<u2009_y_u2009<u2009_H_), and _color_ β a line of lowercase Latin letters from 1 to 15 symbols in length, determining the color. All coordinates given in the input are integers. Initially the rectangle is "white" in color, whereas the segments are drawn "black" in color. Output For every color present in the final picture print on the single line the name of the color and the total area of areas painted that color with an accuracy of 10u2009-u20096. Print the colors in any order. Examples Input 4 5 6 1 1 1 3 1 3 3 3 3 3 3 1 3 1 1 1 1 3 3 1 1 1 3 3 2 2 1 red 2 2 blue Output blue 0.00000000 white 20.00000000 Input 5 5 5 1 1 2 2 2 2 4 2 4 2 4 4 4 4 2 4 2 4 2 2 2 3 3 black 3 3 green Output green 4.00000000 white 21.00000000 Input 7 4 9 1 2 2 3 2 3 3 2 3 2 2 1 2 1 1 2 3 2 4 2 4 2 5 3 5 3 6 2 6 2 5 1 5 1 4 2 2 2 2 black 2 2 red Output red 2.00000000 white 26.00000000 Note Initially the black segments painted by Anfisa can also be painted a color if any of the chosen points lays on the segment. The segments have areas equal to 0. That is why if in the final picture only parts of segments is painted some color, then the area, painted the color is equal to 0. | 2,700 | false | false | false | false | false | false | false | false | false | true | 9,764 |