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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
100E | Problem - 100E - Codeforces =============== xa0 . The following line contains _n_ words. The _i_-th word describes the initial state of lamp number _i_ (see samples for details). The following line contains a single integer _k_ (1u2009≤u2009_k_u2009≤u2009104), the number of times a key is pressed. Then in the next line come _k_ integers in range | 1,600 | true | false | false | false | false | false | false | false | false | false | 9,485 |
834B | It's the end of Julyxa0– the time when a festive evening is held at Jelly Castle! Guests from all over the kingdom gather here to discuss new trends in the world of confectionery. Yet some of the things discussed here are not supposed to be disclosed to the general public: the information can cause discord in the kingdom of Sweetland in case it turns out to reach the wrong hands. So it's a necessity to not let any uninvited guests in. There are 26 entrances in Jelly Castle, enumerated with uppercase English letters from A to Z. Because of security measures, each guest is known to be assigned an entrance he should enter the castle through. The door of each entrance is opened right before the first guest's arrival and closed right after the arrival of the last guest that should enter the castle through this entrance. No two guests can enter the castle simultaneously. For an entrance to be protected from possible intrusion, a candy guard should be assigned to it. There are _k_ such guards in the castle, so if there are more than _k_ opened doors, one of them is going to be left unguarded! Notice that a guard can't leave his post until the door he is assigned to is closed. Slastyona had a suspicion that there could be uninvited guests at the evening. She knows the order in which the invited guests entered the castle, and wants you to help her check whether there was a moment when more than _k_ doors were opened. Input Two integers are given in the first string: the number of guests _n_ and the number of guards _k_ (1u2009≤u2009_n_u2009≤u2009106, 1u2009≤u2009_k_u2009≤u200926). In the second string, _n_ uppercase English letters _s_1_s_2... _s__n_ are given, where _s__i_ is the entrance used by the _i_-th guest. Output Output «YES» if at least one door was unguarded during some time, and «NO» otherwise. You can output each letter in arbitrary case (upper or lower). Note In the first sample case, the door A is opened right before the first guest's arrival and closed when the second guest enters the castle. The door B is opened right before the arrival of the third guest, and closed after the fifth one arrives. One guard can handle both doors, as the first one is closed before the second one is opened. In the second sample case, the door B is opened before the second guest's arrival, but the only guard can't leave the door A unattended, as there is still one more guest that should enter the castle through this door. | 1,100 | false | false | true | false | true | false | false | false | false | false | 6,428 |
1777A | An array $$$a$$$ is good if for all pairs of adjacent elements, $$$a_i$$$ and $$$a_{i+1}$$$ ($$$1le i lt n$$$) are of different parity. Note that an array of size $$$1$$$ is trivially good. You are given an array of size $$$n$$$. In one operation you can select any pair of adjacent elements in which both elements are of the same parity, delete them, and insert their product in the same position. Find the minimum number of operations to form a good array. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 500$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 100$$$). The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$1 le a_i le 10^{9}$$$). Output For each test case print an integer, the minimum number of operations required to form a good array. Example Input 3 5 1 7 11 2 13 4 1 2 3 4 6 1 1 1 2 2 3 Note Consider the first test case. Select the $$$2$$$-nd and the $$$3$$$-rd integers and apply the operation on them. The array changes from $$$[1, color{red}{7}, color{red}{11}, 2, 13]$$$ to $$$[1, color{red}{77}, 2, 13]$$$. Next, select the $$$1$$$-st and the $$$2$$$-nd integers, array changes from $$$[color{red}{1}, color{red}{77}, 2, 13]$$$ to $$$[color{red}{77}, 2, 13]$$$. Thus we require $$$2$$$ operations. It can be proved that this is the minimum number of operations. In the second test case, the given array is already good. So we require $$$0$$$ operations. | 800 | true | true | false | false | false | false | false | false | false | false | 1,614 |
632D | Problem - 632D - Codeforces =============== xa0 of its elements. Denote LCM as _l_. Find any longest subsequence of _a_ with the value _l_u2009≤u2009_m_. A subsequence of _a_ is an array we can get by erasing some elements of _a_. It is allowed to erase zero or all elements. The LCM of an empty array equals 1. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009106) — the size of the array _a_ and the parameter from the problem statement. The second line contains _n_ integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009109) — the elements of _a_. Output In the first line print two integers _l_ and _k__max_ (1u2009≤u2009_l_u2009≤u2009_m_,u20090u2009≤u2009_k__max_u2009≤u2009_n_) — the value of LCM and the number of elements in optimal subsequence. In the second line print _k__max_ integers — the positions of the elements from the optimal subsequence in the ascending order. Note that you can find and print any subsequence with the maximum length. Examples Input 7 8 6 2 9 2 7 2 3 Output 6 5 1 2 4 6 7 Input 6 4 2 2 2 3 3 3 Output 2 3 1 2 3 | 2,100 | true | false | false | false | false | false | true | false | false | false | 7,278 |
1800E2 | This is a complex version of the problem. This version has no additional restrictions on the number $$$k$$$. The chief wizard of the Wizengamot once caught the evil wizard Drahyrt, but the evil wizard has returned and wants revenge on the chief wizard. So he stole spell $$$s$$$ from his student Harry. The spellxa0— is a $$$n$$$-length string of lowercase Latin letters. Drahyrt wants to replace spell with an unforgivable cursexa0— string $$$t$$$. Dragirt, using ancient magic, can swap letters at a distance $$$k$$$ or $$$k+1$$$ in spell as many times as he wants. In other words, Drahyrt can change letters in positions $$$i$$$ and $$$j$$$ in spell $$$s$$$ if $$$i-j=k$$$ or $$$i-j=k+1$$$. For example, if $$$k = 3, s = $$$ "talant" and $$$t = $$$ "atltna", Drahyrt can act as follows: swap the letters at positions $$$1$$$ and $$$4$$$ to get spell "aaltnt". swap the letters at positions $$$2$$$ and $$$6$$$ to get spell "atltna". You are given spells $$$s$$$ and $$$t$$$. Can Drahyrt change spell $$$s$$$ to $$$t$$$? Input The first line of input gives a single integer $$$T$$$ ($$$1 le T le 10^4$$$)xa0— the number of test cases in the test. Descriptions of the test cases are follow. The first line contains two integers $$$n, k$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$1 le k le 2 cdot 10^5$$$)xa0— the length spells and the number $$$k$$$ such that Drahyrt can change letters in a spell at a distance $$$k$$$ or $$$k+1$$$. The second line gives spell $$$s$$$xa0— a string of length $$$n$$$ consisting of lowercase Latin letters. The third line gives spell $$$t$$$xa0— a string of length $$$n$$$ consisting of lowercase Latin letters. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$2 cdot 10^5$$$. Note that there is no limit on the sum of $$$k$$$ values over all test cases. Output For each test case, output on a separate line "YES" if Drahyrt can change spell $$$s$$$ to $$$t$$$ and "NO" otherwise. You can output the answer in any case (for example, lines "yEs", "yes", "Yes" and "YES" will be recognized as positive answer). Example Input 7 6 3 talant atltna 7 1 abacaba aaaabbc 12 6 abracadabraa avadakedavra 5 3 accio cicao 5 4 lumos molus 4 3 uwjt twju 4 3 kvpx vxpk Output YES YES NO YES NO YES NO Note The first case is explained in the condition. In the second case, we can swap adjacent letters, so we can sort the string using bubble sorting, for example. In the third case, we can show that from the string $$$s$$$ we cannot get the string $$$t$$$ by swapping letters at a distance of $$$6$$$ or $$$7$$$. In the fourth case, for example, the following sequence of transformations is appropriate: "accio" $$$ ightarrow$$$ "aocic" $$$ ightarrow$$$ "cocia" $$$ ightarrow$$$ "iocca" $$$ ightarrow$$$ "aocci" $$$ ightarrow$$$ "aicco" $$$ ightarrow$$$ "cicao" In the fifth case, we can show that it is impossible to get the string $$$s$$$ from the string $$$t$$$. In the sixth example, it is enough to swap the two outermost letters. | 1,500 | false | true | false | false | false | true | true | false | false | true | 1,469 |
1863I | You are given a tree with $$$n$$$ vertices labeled $$$1, 2, ldots, n$$$. The length of a simple path in the tree is the number of vertices in it. You are to select a set of simple paths of length at least $$$2$$$ each, but you cannot simultaneously select two distinct paths contained one in another. Find the largest possible size of such a set. Formally, a set $$$S$$$ of vertices is called a route if it contains at least two vertices and coincides with the set of vertices of a simple path in the tree. A collection of distinct routes is called a timetable. A route $$$S$$$ in a timetable $$$T$$$ is called redundant if there is a different route $$$S' in T$$$ such that $$$S subset S'$$$. A timetable is called efficient if it contains no redundant routes. Find the largest possible number of routes in an efficient timetable. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 3000$$$). The $$$i$$$-th of the following $$$n - 1$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i, v_i le n$$$, $$$u_i eq v_i$$$)xa0— the numbers of vertices connected by the $$$i$$$-th edge. It is guaranteed that the given edges form a tree. Note In the first example, possible efficient timetables are $$${{1, 2}, {1, 3}, {1, 4}}$$$ and $$${{1, 2, 3}, {1, 2, 4}, {1, 3, 4}}$$$. In the second example, we can choose $$${ {1, 2, 3}, {2, 3, 4}, {3, 4, 6}, {2, 3, 5}, {3, 4, 5}, {3, 4, 7}, {4, 6, 7}}$$$. | 3,500 | false | false | false | true | false | true | false | false | false | false | 1,098 |
1578J | The Just Kingdom is ruled by a king and his $$$n$$$ lords, numbered $$$1$$$ to $$$n$$$. Each of the lords is a vassal of some overlord, who might be the king himself, or a different lord closer to the king. The king, and all his lords, are just and kind. Each lord has certain needs, which can be expressed as a certain amount of money they need. However, if a lord, or the king, receives any money, they will first split it equally between all their vassals who still have unmet needs. Only if all the needs of all their vassals are met, they will take the money to fulfill their own needs. If there is any money left over, they will return the excess to their overlord (who follows the standard procedure for distributing money). At the beginning of the year, the king receives a certain sum of tax money and proceeds to split it according to the rules above. If the amount of tax money is greater than the total needs of all the lords, the procedure guarantees everybody's needs will be fulfilled, and the excess money will be left with the king. However, if there is not enough money, some lords will not have their needs met. For each lord, determine the minimum amount of tax money the king has to receive so that this lord's needs are met. Input The first line of the input contains the number of lords $$$n$$$ ($$$0 le n le 3 cdot 10^5$$$). Each of the next $$$n$$$ lines describes one of the lords. The $$$i$$$-th line contains two integers: $$$o_i$$$ ($$$0 le o_i < i$$$)xa0— the index of the overlord of the $$$i$$$-th lord (with zero meaning the king is the overlord), and $$$m_i$$$ ($$$1 le m_i le 10^6$$$)xa0— the amount of money the $$$i$$$-th lord needs. Note In the sample input, if the king receives $$$5$$$ units of tax money, he will split it equally between his vassalsxa0— the lords $$$1$$$, $$$3$$$, and $$$5$$$, with each receiving $$$frac{5}{3}$$$ of money. Lord $$$1$$$ will split the money equally between his vassals — $$$2$$$ and $$$4$$$, with each receiving $$$frac{5}{6}$$$. Lord $$$5$$$ will keep the money (having no vassals). Lord $$$3$$$ will keep $$$1$$$ unit of money, and give the remaining $$$frac{2}{3}$$$ to the king. The king will then split the $$$frac{2}{3}$$$ between the vassals with unmet needs — $$$1$$$ and $$$5$$$, passing $$$frac{1}{3}$$$ to each. Lord $$$5$$$ will keep the extra cash (now having a total of $$$2$$$, still not enough to meet his needs). Lord $$$1$$$ will split it equally between his vassals, and the extra $$$frac{1}{6}$$$ will be enough to meet the needs of lord $$$4$$$. | 3,100 | false | false | false | false | true | false | true | false | false | false | 2,745 |
506D | Mr. Kitayuta has just bought an undirected graph with _n_ vertices and _m_ edges. The vertices of the graph are numbered from 1 to _n_. Each edge, namely edge _i_, has a color _c__i_, connecting vertex _a__i_ and _b__i_. Mr. Kitayuta wants you to process the following _q_ queries. In the _i_-th query, he gives you two integers - _u__i_ and _v__i_. Find the number of the colors that satisfy the following condition: the edges of that color connect vertex _u__i_ and vertex _v__i_ directly or indirectly. Input The first line of the input contains space-separated two integers - _n_ and _m_(2u2009≤u2009_n_u2009≤u2009105,u20091u2009≤u2009_m_u2009≤u2009105), denoting the number of the vertices and the number of the edges, respectively. The next _m_ lines contain space-separated three integers - _a__i_, _b__i_(1u2009≤u2009_a__i_u2009<u2009_b__i_u2009≤u2009_n_) and _c__i_(1u2009≤u2009_c__i_u2009≤u2009_m_). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if _i_u2009≠u2009_j_,u2009(_a__i_,u2009_b__i_,u2009_c__i_)u2009≠u2009(_a__j_,u2009_b__j_,u2009_c__j_). The next line contains a integer- _q_(1u2009≤u2009_q_u2009≤u2009105), denoting the number of the queries. Then follows _q_ lines, containing space-separated two integers - _u__i_ and _v__i_(1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_). It is guaranteed that _u__i_u2009≠u2009_v__i_. Output For each query, print the answer in a separate line. Examples Input 4 5 1 2 1 1 2 2 2 3 1 2 3 3 2 4 3 3 1 2 3 4 1 4 Input 5 7 1 5 1 2 5 1 3 5 1 4 5 1 1 2 2 2 3 2 3 4 2 5 1 5 5 1 2 5 1 5 1 4 Note Let's consider the first sample. The figure above shows the first sample. Vertex 1 and vertex 2 are connected by color 1 and 2. Vertex 3 and vertex 4 are connected by color 3. Vertex 1 and vertex 4 are not connected by any single color. | 2,400 | false | false | false | false | false | false | true | false | false | true | 7,818 |
1059C | Problem - 1059C - 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 math *1600 No tag edit access → Contest materials ") Editorial") of all the elements of the sequence to the result and remove one arbitrary element from the sequence. Thus, when the process ends, we have a sequence of $$$n$$$ integers: the greatest common divisors of all the elements in the sequence before each deletion. You are given an integer sequence $$$1, 2, dots, n$$$. Find the lexicographically maximum result of its transformation. A sequence $$$a_1, a_2, ldots, a_n$$$ is lexicographically larger than a sequence $$$b_1, b_2, ldots, b_n$$$, if there is an index $$$i$$$ such that $$$a_j = b_j$$$ for all $$$j < i$$$, and $$$a_i > b_i$$$. Input The first and only line of input contains one integer $$$n$$$ ($$$1le nle 10^6$$$). Output Output $$$n$$$ integers xa0— the lexicographically maximum result of the transformation. Examples Input 3 Output 1 1 3 Input 2 Output 1 2 Input 1 Output 1 Note In the first sample the answer may be achieved this way: Append GCD$$$(1, 2, 3) = 1$$$, remove $$$2$$$. Append GCD$$$(1, 3) = 1$$$, remove $$$1$$$. Append GCD$$$(3) = 3$$$, remove $$$3$$$. We get the sequence $$$ | 1,600 | true | false | false | false | false | true | false | false | false | false | 5,419 |
1523B | While trading on his favorite exchange trader William realized that he found a vulnerability. Using this vulnerability he could change the values of certain internal variables to his advantage. To play around he decided to change the values of all internal variables from $$$a_1, a_2, ldots, a_n$$$ to $$$-a_1, -a_2, ldots, -a_n$$$. For some unknown reason, the number of service variables is always an even number. William understands that with his every action he attracts more and more attention from the exchange's security team, so the number of his actions must not exceed $$$5,000$$$ and after every operation no variable can have an absolute value greater than $$$10^{18}$$$. William can perform actions of two types for two chosen variables with indices $$$i$$$ and $$$j$$$, where $$$i < j$$$: 1. Perform assignment $$$a_i = a_i + a_j$$$ 2. Perform assignment $$$a_j = a_j - a_i$$$ William wants you to develop a strategy that will get all the internal variables to the desired values. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 20$$$). Description of the test cases follows. The first line of each test case contains a single even integer $$$n$$$ ($$$2 le n le 10^3$$$), which is the number of internal variables. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ $$$(1 le a_i le 10^9)$$$, which are initial values of internal variables. Output For each test case print the answer in the following format: The first line of output must contain the total number of actions $$$k$$$, which the strategy will perform. Note that you do not have to minimize $$$k$$$. The inequality $$$k le 5,000$$$ must be satisfied. Each of the next $$$k$$$ lines must contain actions formatted as "type i j", where "type" is equal to "1" if the strategy needs to perform an assignment of the first type and "2" if the strategy needs to perform an assignment of the second type. Note that $$$i < j$$$ should hold. We can show that an answer always exists. Example Input 2 4 1 1 1 1 4 4 3 1 2 Output 8 2 1 2 2 1 2 2 1 3 2 1 3 2 1 4 2 1 4 1 1 2 1 1 2 8 2 1 4 1 2 4 1 2 4 1 2 4 1 3 4 1 1 2 1 1 2 1 1 4 Note For the first sample test case one possible sequence of operations is as follows: 1. "2 1 2". Values of variables after performing the operation: [1, 0, 1, 1] 2. "2 1 2". Values of variables after performing the operation: [1, -1, 1, 1] 3. "2 1 3". Values of variables after performing the operation: [1, -1, 0, 1] 4. "2 1 3". Values of variables after performing the operation: [1, -1, -1, 1] 5. "2 1 4". Values of variables after performing the operation: [1, -1, -1, 0] 6. "2 1 4". Values of variables after performing the operation: [1, -1, -1, -1] 7. "1 1 2". Values of variables after performing the operation: [0, -1, -1, -1] 8. "1 1 2". Values of variables after performing the operation: [-1, -1, -1, -1] For the second sample test case one possible sequence of operations is as follows: 1. "2 1 4". Values of variables after performing the operation: [4, 3, 1, -2] 2. "1 2 4". Values of variables after performing the operation: [4, 1, 1, -2] 3. "1 2 4". Values of variables after performing the operation: [4, -1, 1, -2] 4. "1 2 4". Values of variables after performing the operation: [4, -3, 1, -2] 5. "1 3 4". Values of variables after performing the operation: [4, -3, -1, -2] 6. "1 1 2". Values of variables after performing the operation: [1, -3, -1, -2] 7. "1 1 2". Values of variables after performing the operation: [-2, -3, -1, -2] 8. "1 1 4". Values of variables after performing the operation: [-4, -3, -1, -2] | 1,100 | false | false | false | false | false | true | false | false | false | false | 3,049 |
613D | Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. However, in order not to lose face in front of the relatives, the King should first finish reforms in his kingdom. As the King can not wait for his daughter's marriage, reforms must be finished as soon as possible. The kingdom currently consists of _n_ cities. Cities are connected by _n_u2009-u20091 bidirectional road, such that one can get from any city to any other city. As the King had to save a lot, there is only one path between any two cities. What is the point of the reform? The key ministries of the state should be relocated to distinct cities (we call such cities important). However, due to the fact that there is a high risk of an attack by barbarians it must be done carefully. The King has made several plans, each of which is described by a set of important cities, and now wonders what is the best plan. Barbarians can capture some of the cities that are not important (the important ones will have enough protection for sure), after that the captured city becomes impassable. In particular, an interesting feature of the plan is the minimum number of cities that the barbarians need to capture in order to make all the important cities isolated, that is, from all important cities it would be impossible to reach any other important city. Help the King to calculate this characteristic for each of his plan. Input The first line of the input contains integer _n_ (1u2009≤u2009_n_u2009≤u2009100u2009000)xa0— the number of cities in the kingdom. Each of the next _n_u2009-u20091 lines contains two distinct integers _u__i_, _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_)xa0— the indices of the cities connected by the _i_-th road. It is guaranteed that you can get from any city to any other one moving only along the existing roads. The next line contains a single integer _q_ (1u2009≤u2009_q_u2009≤u2009100u2009000)xa0— the number of King's plans. Each of the next _q_ lines looks as follows: first goes number _k__i_xa0— the number of important cities in the King's plan, (1u2009≤u2009_k__i_u2009≤u2009_n_), then follow exactly _k__i_ space-separated pairwise distinct numbers from 1 to _n_xa0— the numbers of important cities in this plan. The sum of all _k__i_'s does't exceed 100u2009000. Output For each plan print a single integer — the minimum number of cities that the barbarians need to capture, or print u2009-u20091 if all the barbarians' attempts to isolate important cities will not be effective. Note In the first sample, in the first and the third King's plan barbarians can capture the city 3, and that will be enough. In the second and the fourth plans all their attempts will not be effective. In the second sample the cities to capture are 3 and 5. | 2,800 | false | false | false | true | false | false | false | false | true | true | 7,384 |
847I | The Berland's capital has the form of a rectangle with sizes _n_u2009×u2009_m_ quarters. All quarters are divided into three types: regular (labeled with the character '.') — such quarters do not produce the noise but are not obstacles to the propagation of the noise; sources of noise (labeled with an uppercase Latin letter from 'A' to 'Z') — such quarters are noise sources and are not obstacles to the propagation of the noise; heavily built-up (labeled with the character '*') — such quarters are soundproofed, the noise does not penetrate into them and they themselves are obstacles to the propagation of noise. A quarter labeled with letter 'A' produces _q_ units of noise. A quarter labeled with letter 'B' produces 2·_q_ units of noise. And so on, up to a quarter labeled with letter 'Z', which produces 26·_q_ units of noise. There can be any number of quarters labeled with each letter in the city. When propagating from the source of the noise, the noise level is halved when moving from one quarter to a quarter that shares a side with it (when an odd number is to be halved, it's rounded down). The noise spreads along the chain. For example, if some quarter is located at a distance 2 from the noise source, then the value of noise which will reach the quarter is divided by 4. So the noise level that comes from the source to the quarter is determined solely by the length of the shortest path between them. Heavily built-up quarters are obstacles, the noise does not penetrate into them. The values in the cells of the table on the right show the total noise level in the respective quarters for _q_u2009=u2009100, the first term in each sum is the noise from the quarter 'A', the second — the noise from the quarter 'B'. The noise level in quarter is defined as the sum of the noise from all sources. To assess the quality of life of the population of the capital of Berland, it is required to find the number of quarters whose noise level exceeds the allowed level _p_. Input The first line contains four integers _n_, _m_, _q_ and _p_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009250, 1u2009≤u2009_q_,u2009_p_u2009≤u2009106) — the sizes of Berland's capital, the number of noise units that a quarter 'A' produces, and the allowable noise level. Each of the following _n_ lines contains _m_ characters — the description of the capital quarters, in the format that was described in the statement above. It is possible that in the Berland's capital there are no quarters of any type. Output Print the number of quarters, in which the noise level exceeds the allowed level _p_. Examples Input 3 3 100 140 ... A*. .B. Note The illustration to the first example is in the main part of the statement. | 1,900 | true | false | true | false | false | false | false | false | false | false | 6,363 |
1763D | You are given five integers $$$n$$$, $$$i$$$, $$$j$$$, $$$x$$$, and $$$y$$$. Find the number of bitonic permutations $$$B$$$, of the numbers $$$1$$$ to $$$n$$$, such that $$$B_i=x$$$, and $$$B_j=y$$$. Since the answer can be large, compute it modulo $$$10^9+7$$$. A bitonic permutation is a permutation of numbers, such that the elements of the permutation first increase till a certain index $$$k$$$, $$$2 le k le n-1$$$, and then decrease till the end. Refer to notes for further clarification. Note A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). An array of $$$n ge 3$$$ elements is bitonic if its elements are first increasing till an index $$$k$$$, $$$2 le k le n-1$$$, and then decreasing till the end. For example, $$$[2,5,8,6,1]$$$ is a bitonic array with $$$k=3$$$, but $$$[2,5,8,1,6]$$$ is not a bitonic array (elements first increase till $$$k=3$$$, then decrease, and then increase again). A bitonic permutation is a permutation in which the elements follow the above-mentioned bitonic property. For example, $$$[2,3,5,4,1]$$$ is a bitonic permutation, but $$$[2,3,5,1,4]$$$ is not a bitonic permutation (since it is not a bitonic array) and $$$[2,3,4,4,1]$$$ is also not a bitonic permutation (since it is not a permutation). Sample Test Case Description For $$$n=3$$$, possible permutations are $$$[1,2,3]$$$, $$$[1,3,2]$$$, $$$[2,1,3]$$$, $$$[2,3,1]$$$, $$$[3,1,2]$$$, and $$$[3,2,1]$$$. Among the given permutations, the bitonic permutations are $$$[1,3,2]$$$ and $$$[2,3,1]$$$. In the first test case, the expected permutation must be of the form $$$[2,?,3]$$$, which does not satisfy either of the two bitonic permutations with $$$n=3$$$, therefore the answer is 0. In the second test case, the expected permutation must be of the form $$$[?,3,2]$$$, which only satisfies the bitonic permutation $$$[1,3,2]$$$, therefore, the answer is 1. | 2,200 | true | false | true | true | false | false | false | false | false | false | 1,730 |
1088A | Problem - 1088A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force constructive algorithms *800 No tag edit access → Contest materials . $$$a cdot b>x$$$. $$$frac{a}{b}<x$$$. Input The only line contains the integer $$$x$$$ $$$(1 le x le 100)$$$. Output You should output two integers $$$a$$$ and $$$b$$$, satisfying the given conditions, separated by a space. If no pair of integers satisfy the conditions above, print "-1" (without quotes). Examples Input 10 Output 6 3 Input 1 Output -1 | 800 | false | false | false | false | false | true | true | false | false | false | 5,291 |
1167D | A string is called bracket sequence if it does not contain any characters other than "(" and ")". A bracket sequence is called regular (shortly, RBS) if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, "", "(())" and "()()" are RBS and ")(" and "(()" are not. We can see that each opening bracket in RBS is paired with some closing bracket, and, using this fact, we can define nesting depth of the RBS as maximum number of bracket pairs, such that the $$$2$$$-nd pair lies inside the $$$1$$$-st one, the $$$3$$$-rd one — inside the $$$2$$$-nd one and so on. For example, nesting depth of "" is $$$0$$$, "()()()" is $$$1$$$ and "()((())())" is $$$3$$$. Now, you are given RBS $$$s$$$ of even length $$$n$$$. You should color each bracket of $$$s$$$ into one of two colors: red or blue. Bracket sequence $$$r$$$, consisting only of red brackets, should be RBS, and bracket sequence, consisting only of blue brackets $$$b$$$, should be RBS. Any of them can be empty. You are not allowed to reorder characters in $$$s$$$, $$$r$$$ or $$$b$$$. No brackets can be left uncolored. Among all possible variants you should choose one that minimizes maximum of $$$r$$$'s and $$$b$$$'s nesting depth. If there are multiple solutions you can print any of them. Input The first line contains an even integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$) — the length of RBS $$$s$$$. The second line contains regular bracket sequence $$$s$$$ ($$$s = n$$$, $$$s_i in {$$$"(", ")"$$$}$$$). Note In the first example one of optimal solutions is $$$s = $$$ "$$$color{blue}{()}$$$". $$$r$$$ is empty and $$$b = $$$ "$$$()$$$". The answer is $$$max(0, 1) = 1$$$. In the second example it's optimal to make $$$s = $$$ "$$$color{red}{(}color{blue}{(}color{red}{)}color{blue}{)}$$$". $$$r = b = $$$ "$$$()$$$" and the answer is $$$1$$$. In the third example we can make $$$s = $$$ "$$$color{red}{(}color{blue}{((}color{red}{)()}color{blue}{)())}$$$". $$$r = $$$ "$$$()()$$$" and $$$b = $$$ "$$$(()())$$$" and the answer is $$$2$$$. | 1,500 | false | true | false | false | false | true | false | false | false | false | 4,880 |
1385D | You are given a string $$$s[1 dots n]$$$ consisting of lowercase Latin letters. It is guaranteed that $$$n = 2^k$$$ for some integer $$$k ge 0$$$. The string $$$s[1 dots n]$$$ is called $$$c$$$-good if at least one of the following three conditions is satisfied: The length of $$$s$$$ is $$$1$$$, and it consists of the character $$$c$$$ (i.e. $$$s_1=c$$$); The length of $$$s$$$ is greater than $$$1$$$, the first half of the string consists of only the character $$$c$$$ (i.e. $$$s_1=s_2=dots=s_{frac{n}{2}}=c$$$) and the second half of the string (i.e. the string $$$s_{frac{n}{2} + 1}s_{frac{n}{2} + 2} dots s_n$$$) is a $$$(c+1)$$$-good string; The length of $$$s$$$ is greater than $$$1$$$, the second half of the string consists of only the character $$$c$$$ (i.e. $$$s_{frac{n}{2} + 1}=s_{frac{n}{2} + 2}=dots=s_n=c$$$) and the first half of the string (i.e. the string $$$s_1s_2 dots s_{frac{n}{2}}$$$) is a $$$(c+1)$$$-good string. For example: "aabc" is 'a'-good, "ffgheeee" is 'e'-good. In one move, you can choose one index $$$i$$$ from $$$1$$$ to $$$n$$$ and replace $$$s_i$$$ with any lowercase Latin letter (any character from 'a' to 'z'). Your task is to find the minimum number of moves required to obtain an 'a'-good string from $$$s$$$ (i.e. $$$c$$$-good string for $$$c=$$$ 'a'). It is guaranteed that the answer always exists. You have to answer $$$t$$$ independent test cases. Another example of an 'a'-good string is as follows. Consider the string $$$s = $$$"cdbbaaaa". It is an 'a'-good string, because: the second half of the string ("aaaa") consists of only the character 'a'; the first half of the string ("cdbb") is 'b'-good string, because: the second half of the string ("bb") consists of only the character 'b'; the first half of the string ("cd") is 'c'-good string, because: the first half of the string ("c") consists of only the character 'c'; the second half of the string ("d") is 'd'-good string. 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 131~072$$$) — the length of $$$s$$$. It is guaranteed that $$$n = 2^k$$$ for some integer $$$k ge 0$$$. The second line of the test case contains the string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. 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 minimum number of moves required to obtain an 'a'-good string from $$$s$$$ (i.e. $$$c$$$-good string with $$$c =$$$ 'a'). It is guaranteed that the answer exists. Example Input 6 8 bbdcaaaa 8 asdfghjk 8 ceaaaabb 8 bbaaddcc 1 z 2 ac | 1,500 | false | false | true | true | false | false | true | false | false | false | 3,744 |
729A | Polycarp has interviewed Oleg and has written the interview down without punctuation marks and spaces to save time. Thus, the interview is now a string _s_ consisting of _n_ lowercase English letters. There is a filler word ogo in Oleg's speech. All words that can be obtained from ogo by adding go several times to the end of it are also considered to be fillers. For example, the words ogo, ogogo, ogogogo are fillers, but the words go, og, ogog, ogogog and oggo are not fillers. The fillers have maximal size, for example, for ogogoo speech we can't consider ogo a filler and goo as a normal phrase. We should consider ogogo as a filler here. To print the interview, Polycarp has to replace each of the fillers with three asterisks. Note that a filler word is replaced with exactly three asterisks regardless of its length. Polycarp has dealt with this problem in no time. Can you do the same? The clock is ticking! Input The first line contains a positive integer _n_ (1u2009≤u2009_n_u2009≤u2009100)xa0— the length of the interview. The second line contains the string _s_ of length _n_, consisting of lowercase English letters. Output Print the interview text after the replacement of each of the fillers with "***". It is allowed for the substring "***" to have several consecutive occurences. Note The first sample contains one filler word ogogo, so the interview for printing is "a***b". The second sample contains two fillers ogo and ogogogo. Thus, the interview is transformed to "***gmg***". | 900 | false | false | true | false | false | false | false | false | false | false | 6,894 |
1368G | # Shifting Dominoes Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 512 megabytes Bill likes to play with dominoes. He took an n × m board divided into equal square cells, and covered it with dominoes. Each domino covers two adjacent cells of the board either horizontally or vertically, and each cell is covered exactly once with a half of one domino (that is, there are no uncovered cells, and no two dominoes cover the same cell twice). After that Bill decided to play with the covered board and share some photos of it on social media. First, he removes exactly one domino from the board, freeing two of the cells. Next, he moves dominoes around. A domino can only be moved along the line parallel to its longer side . A move in the chosen direction is possible if the next cell in this direction is currently free. Bill doesn’t want to lose track of what the original tiling looks like, so he makes sure that at any point each domino shares at least one cell with its original position .After removing a domino and making several (possibly, zero) moves Bill takes a photo of the board and posts it. However, with the amount of filters Bill is using, domino borders are not visible, so only the two free cells of the board can be identified. When the photo is posted, Bill reverts the board to its original state and starts the process again. Bill wants to post as many photos as possible, but he will not post any photo twice. How many distinct photos can he take? Recall that photos are different if the pairs of free cells in the photos are different. # Input The first line contains two positive integers n and m (nm ≤ 2 · 10 5) x16 height and width of the board respectively. The next n lines describe the tiling of the board, row by row from top to bottom. Each of these lines contains m characters, describing the cells in the corresponding row left to right. Each character is one of U, D, L, or R, meaning that the cell is covered with a top, bottom, left, or right half of a domino respectively. It is guaranteed that the described tiling is valid, that is, each half-domino has a counterpart in the relevant location. In particular, since tiling is possible, the number of cells in the board is even. # Output Print a single integer x16 the number of distinct photos Bill can take. Page 1 of 2 Examples standard input standard output 2 4 UUUU DDDD 42 3 ULR DLR 66 6 ULRUUU DUUDDD UDDLRU DLRLRD ULRULR DLRDLR 133 # Note In the first sample case, no moves are possible after removing any domino, thus there are four distinct photos. In the second sample case, four photos are possible after removing the leftmost domino by independently moving/not moving the remaining two dominoes. Two more different photos are obtained by removing one of the dominoes on the right. Page 2 of 2 | 3,200 | false | false | false | false | true | false | false | false | false | true | 3,830 |
120F | One day mum asked Petya to sort his toys and get rid of some of them. Petya found a whole box of toy spiders. They were quite dear to him and the boy didn't want to throw them away. Petya conjured a cunning plan: he will glue all the spiders together and attach them to the ceiling. Besides, Petya knows that the lower the spiders will hang, the more mum is going to like it and then she won't throw his favourite toys away. Help Petya carry out the plan. A spider consists of _k_ beads tied together by _k_u2009-u20091 threads. Each thread connects two different beads, at that any pair of beads that make up a spider is either directly connected by a thread, or is connected via some chain of threads and beads. Petya may glue spiders together directly gluing their beads. The length of each thread equals 1. The sizes of the beads can be neglected. That's why we can consider that gluing spiders happens by identifying some of the beads (see the picture). Besides, the construction resulting from the gluing process should also represent a spider, that is, it should have the given features. After Petya glues all spiders together, he measures the length of the resulting toy. The distance between a pair of beads is identified as the total length of the threads that connect these two beads. The length of the resulting construction is the largest distance between all pairs of beads. Petya wants to make the spider whose length is as much as possible. The picture two shows two spiders from the second sample. We can glue to the bead number 2 of the first spider the bead number 1 of the second spider. The threads in the spiders that form the sequence of threads of maximum lengths are highlighted on the picture. Input The first input file line contains one integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of spiders. Next _n_ lines contain the descriptions of each spider: integer _n__i_ (2u2009≤u2009_n__i_u2009≤u2009100) — the number of beads, then _n__i_u2009-u20091 pairs of numbers denoting the numbers of the beads connected by threads. The beads that make up each spider are numbered from 1 to _n__i_. | 1,400 | false | true | false | true | false | false | false | false | false | false | 9,401 |
1006C | You are given an array $$$d_1, d_2, dots, d_n$$$ consisting of $$$n$$$ integer numbers. Your task is to split this array into three parts (some of which may be empty) in such a way that each element of the array belongs to exactly one of the three parts, and each of the parts forms a consecutive contiguous subsegment (possibly, empty) of the original array. Let the sum of elements of the first part be $$$sum_1$$$, the sum of elements of the second part be $$$sum_2$$$ and the sum of elements of the third part be $$$sum_3$$$. Among all possible ways to split the array you have to choose a way such that $$$sum_1 = sum_3$$$ and $$$sum_1$$$ is maximum possible. More formally, if the first part of the array contains $$$a$$$ elements, the second part of the array contains $$$b$$$ elements and the third part contains $$$c$$$ elements, then: $$$$$$sum_1 = sumlimits_{1 le i le a}d_i,$$$$$$ $$$$$$sum_2 = sumlimits_{a + 1 le i le a + b}d_i,$$$$$$ $$$$$$sum_3 = sumlimits_{a + b + 1 le i le a + b + c}d_i.$$$$$$ The sum of an empty array is $$$0$$$. Your task is to find a way to split the array such that $$$sum_1 = sum_3$$$ and $$$sum_1$$$ is maximum possible. Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of elements in the array $$$d$$$. The second line of the input contains $$$n$$$ integers $$$d_1, d_2, dots, d_n$$$ ($$$1 le d_i le 10^9$$$) — the elements of the array $$$d$$$. Output Print a single integer — the maximum possible value of $$$sum_1$$$, considering that the condition $$$sum_1 = sum_3$$$ must be met. Obviously, at least one valid way to split the array exists (use $$$a=c=0$$$ and $$$b=n$$$). Note In the first example there is only one possible splitting which maximizes $$$sum_1$$$: $$$[1, 3, 1], [~], [1, 4]$$$. In the second example the only way to have $$$sum_1=4$$$ is: $$$[1, 3], [2, 1], [4]$$$. In the third example there is only one way to split the array: $$$[~], [4, 1, 2], [~]$$$. | 1,200 | false | false | false | false | true | false | false | true | false | false | 5,653 |
2002B | Alice got a permutation $$$a_1, a_2, ldots, a_n$$$ of $$$[1,2,ldots,n]$$$, and Bob got another permutation $$$b_1, b_2, ldots, b_n$$$ of $$$[1,2,ldots,n]$$$. They are going to play a game with these arrays. In each turn, the following events happen in order: Alice chooses either the first or the last element of her array and removes it from the array; Bob chooses either the first or the last element of his array and removes it from the array. The game continues for $$$n-1$$$ turns, after which both arrays will have exactly one remaining element: $$$x$$$ in the array $$$a$$$ and $$$y$$$ in the array $$$b$$$. If $$$x=y$$$, Bob wins; otherwise, Alice wins. Find which player will win if both players play optimally. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1le tle10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1le nle 3cdot 10^5$$$). The next line contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$1le a_ile n$$$, all $$$a_i$$$ are distinct)xa0— the permutation of Alice. The next line contains $$$n$$$ integers $$$b_1,b_2,ldots,b_n$$$ ($$$1le b_ile n$$$, all $$$b_i$$$ are distinct)xa0— the permutation of Bob. It is guaranteed that the sum of all $$$n$$$ does not exceed $$$3cdot 10^5$$$. Output For each test case, print a single line with the name of the winner, assuming both players play optimally. If Alice wins, print $$$ exttt{Alice}$$$; otherwise, print $$$ exttt{Bob}$$$. Note In the first test case, Bob can win the game by deleting the same element as Alice did. In the second test case, Alice can delete $$$3$$$ in the first turn, and then in the second turn, delete the element that is different from the one Bob deleted in the first turn to win the game. | 1,000 | false | false | false | false | false | true | false | false | false | false | 248 |
731F | Little Vlad is fond of popular computer game Bota-2. Recently, the developers announced the new add-on named Bota-3. Of course, Vlad immediately bought only to find out his computer is too old for the new game and needs to be updated. There are _n_ video cards in the shop, the power of the _i_-th video card is equal to integer value _a__i_. As Vlad wants to be sure the new game will work he wants to buy not one, but several video cards and unite their powers using the cutting-edge technology. To use this technology one of the cards is chosen as the leading one and other video cards are attached to it as secondary. For this new technology to work it's required that the power of each of the secondary video cards is divisible by the power of the leading video card. In order to achieve that the power of any secondary video card can be reduced to any integer value less or equal than the current power. However, the power of the leading video card should remain unchanged, i.e. it can't be reduced. Vlad has an infinite amount of money so he can buy any set of video cards. Help him determine which video cards he should buy such that after picking the leading video card and may be reducing some powers of others to make them work together he will get the maximum total value of video power. Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009200u2009000)xa0— the number of video cards in the shop. The second line contains _n_ integers _a_1, _a_2, ..., _a__n_ (1u2009≤u2009_a__i_u2009≤u2009200u2009000)xa0— powers of video cards. Output The only line of the output should contain one integer valuexa0— the maximum possible total power of video cards working together. Note In the first sample, it would be optimal to buy video cards with powers 3, 15 and 9. The video card with power 3 should be chosen as the leading one and all other video cards will be compatible with it. Thus, the total power would be 3u2009+u200915u2009+u20099u2009=u200927. If he buys all the video cards and pick the one with the power 2 as the leading, the powers of all other video cards should be reduced by 1, thus the total power would be 2u2009+u20092u2009+u200914u2009+u20098u2009=u200926, that is less than 27. Please note, that it's not allowed to reduce the power of the leading video card, i.e. one can't get the total power 3u2009+u20091u2009+u200915u2009+u20099u2009=u200928. In the second sample, the optimal answer is to buy all video cards and pick the one with the power 2 as the leading. The video card with the power 7 needs it power to be reduced down to 6. The total power would be 8u2009+u20092u2009+u20092u2009+u20096u2009=u200918. | 1,900 | true | false | true | false | true | false | true | false | false | false | 6,871 |
1332B | A positive integer is called composite if it can be represented as a product of two positive integers, both greater than $$$1$$$. For example, the following numbers are composite: $$$6$$$, $$$4$$$, $$$120$$$, $$$27$$$. The following numbers aren't: $$$1$$$, $$$2$$$, $$$3$$$, $$$17$$$, $$$97$$$. Alice is given a sequence of $$$n$$$ composite numbers $$$a_1,a_2,ldots,a_n$$$. She wants to choose an integer $$$m le 11$$$ and color each element one of $$$m$$$ colors from $$$1$$$ to $$$m$$$ so that: for each color from $$$1$$$ to $$$m$$$ there is at least one element of this color; each element is colored and colored exactly one color; the greatest common divisor of any two elements that are colored the same color is greater than $$$1$$$, i.e. $$$gcd(a_i, a_j)>1$$$ for each pair $$$i, j$$$ if these elements are colored the same color. Note that equal elements can be colored different colorsxa0— you just have to choose one of $$$m$$$ colors for each of the indices from $$$1$$$ to $$$n$$$. Alice showed already that if all $$$a_i le 1000$$$ then she can always solve the task by choosing some $$$m le 11$$$. Help Alice to find the required coloring. Note that you don't have to minimize or maximize the number of colors, you just have to find the solution with some $$$m$$$ from $$$1$$$ to $$$11$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. Then the descriptions of the test cases follow. The first line of the test case contains a single integer $$$n$$$ ($$$1 le n le 1000$$$) — the amount of numbers in a sequence $$$a$$$. The second line of the test case contains $$$n$$$ composite integers $$$a_1,a_2,ldots,a_n$$$ ($$$4 le a_i le 1000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^4$$$. Output For each test case print $$$2$$$ lines. The first line should contain a single integer $$$m$$$ ($$$1 le m le 11$$$) — the number of used colors. Consider colors to be numbered from $$$1$$$ to $$$m$$$. The second line should contain any coloring that satisfies the above conditions. Print $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$ ($$$1 le c_i le m$$$), where $$$c_i$$$ is the color of the $$$i$$$-th element. If there are multiple solutions then you can print any of them. Note that you don't have to minimize or maximize the number of colors, you just have to find the solution with some $$$m$$$ from $$$1$$$ to $$$11$$$. Remember that each color from $$$1$$$ to $$$m$$$ should be used at least once. Any two elements of the same color should not be coprime (i.e. their GCD should be greater than $$$1$$$). Example Input 3 3 6 10 15 2 4 9 23 437 519 865 808 909 391 194 291 237 395 323 365 511 497 781 737 871 559 731 697 779 841 961 Output 1 1 1 1 2 2 1 11 4 7 8 10 7 3 10 7 7 8 3 1 1 5 5 9 2 2 3 3 4 11 6 Note In the first test case, $$$gcd(6,10)=2$$$, $$$gcd(6,15)=3$$$ and $$$gcd(10,15)=5$$$. Therefore, it's valid to color all elements the same color. Note that there are other colorings which satisfy Alice's requirement in this test case. In the second test case there is only one element of each color, so the coloring definitely satisfies Alice's requirement. | 1,400 | true | true | false | false | false | true | true | false | false | false | 4,049 |
1253F | You're given a simple, undirected, connected, weighted graph with $$$n$$$ nodes and $$$m$$$ edges. Nodes are numbered from $$$1$$$ to $$$n$$$. There are exactly $$$k$$$ centrals (recharge points), which are nodes $$$1, 2, ldots, k$$$. We consider a robot moving into this graph, with a battery of capacity $$$c$$$, not fixed by the constructor yet. At any time, the battery contains an integer amount $$$x$$$ of energy between $$$0$$$ and $$$c$$$ inclusive. Traversing an edge of weight $$$w_i$$$ is possible only if $$$x ge w_i$$$, and costs $$$w_i$$$ energy points ($$$x := x - w_i$$$). Moreover, when the robot reaches a central, its battery is entirely recharged ($$$x := c$$$). You're given $$$q$$$ independent missions, the $$$i$$$-th mission requires to move the robot from central $$$a_i$$$ to central $$$b_i$$$. For each mission, you should tell the minimum capacity required to acheive it. Input The first line contains four integers $$$n$$$, $$$m$$$, $$$k$$$ and $$$q$$$ ($$$2 le k le n le 10^5$$$ and $$$1 le m, q le 3 cdot 10^5$$$). The $$$i$$$-th of the next $$$m$$$ lines contains three integers $$$u_i$$$, $$$v_i$$$ and $$$w_i$$$ ($$$1 le u_i, v_i le n$$$, $$$u_i eq v_i$$$, $$$1 le w_i le 10^9$$$), that mean that there's an edge between nodes $$$u$$$ and $$$v$$$, with a weight $$$w_i$$$. It is guaranteed that the given graph is simple (there is no self-loop, and there is at most one edge between every pair of nodes) and connected. The $$$i$$$-th of the next $$$q$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i, b_i le k$$$, $$$a_i eq b_i$$$). Output You have to output $$$q$$$ lines, where the $$$i$$$-th line contains a single integer : the minimum capacity required to acheive the $$$i$$$-th mission. Examples Input 10 9 3 1 10 9 11 9 2 37 2 4 4 4 1 8 1 5 2 5 7 3 7 3 2 3 8 4 8 6 13 2 3 Input 9 11 3 2 1 3 99 1 4 5 4 5 3 5 6 3 6 4 11 6 7 21 7 2 6 7 8 4 8 9 3 9 2 57 9 3 2 3 1 2 3 Note In the first example, the graph is the chain $$$10 - 9 - 2^C - 4 - 1^C - 5 - 7 - 3^C - 8 - 6$$$, where centrals are nodes $$$1$$$, $$$2$$$ and $$$3$$$. For the mission $$$(2, 3)$$$, there is only one simple path possible. Here is a simulation of this mission when the capacity is $$$12$$$. The robot begins on the node $$$2$$$, with $$$c = 12$$$ energy points. The robot uses an edge of weight $$$4$$$. The robot reaches the node $$$4$$$, with $$$12 - 4 = 8$$$ energy points. The robot uses an edge of weight $$$8$$$. The robot reaches the node $$$1$$$ with $$$8 - 8 = 0$$$ energy points. The robot is on a central, so its battery is recharged. He has now $$$c = 12$$$ energy points. The robot uses an edge of weight $$$2$$$. The robot is on the node $$$5$$$, with $$$12 - 2 = 10$$$ energy points. The robot uses an edge of weight $$$3$$$. The robot is on the node $$$7$$$, with $$$10 - 3 = 7$$$ energy points. The robot uses an edge of weight $$$2$$$. The robot is on the node $$$3$$$, with $$$7 - 2 = 5$$$ energy points. The robot is on a central, so its battery is recharged. He has now $$$c = 12$$$ energy points. End of the simulation. Note that if value of $$$c$$$ was lower than $$$12$$$, we would have less than $$$8$$$ energy points on node $$$4$$$, and we would be unable to use the edge $$$4 leftrightarrow 1$$$ of weight $$$8$$$. Hence $$$12$$$ is the minimum capacity required to acheive the mission. — The graph of the second example is described here (centrals are red nodes): The robot can acheive the mission $$$(3, 1)$$$ with a battery of capacity $$$c = 38$$$, using the path $$$3 ightarrow 9 ightarrow 8 ightarrow 7 ightarrow 2 ightarrow 7 ightarrow 6 ightarrow 5 ightarrow 4 ightarrow 1$$$ The robot can acheive the mission $$$(2, 3)$$$ with a battery of capacity $$$c = 15$$$, using the path $$$2 ightarrow 7 ightarrow 8 ightarrow 9 ightarrow 3$$$ | 2,500 | false | false | false | false | false | false | false | true | false | true | 4,417 |
1060E | Sergey Semyonovich is a mayor of a county city N and he used to spend his days and nights in thoughts of further improvements of Nkers' lives. Unfortunately for him, anything and everything has been done already, and there are no more possible improvements he can think of during the day (he now prefers to sleep at night). However, his assistants have found a solution and they now draw an imaginary city on a paper sheet and suggest the mayor can propose its improvements. Right now he has a map of some imaginary city with $$$n$$$ subway stations. Some stations are directly connected with tunnels in such a way that the whole map is a tree (assistants were short on time and enthusiasm). It means that there exists exactly one simple path between each pair of station. We call a path simple if it uses each tunnel no more than once. One of Sergey Semyonovich's favorite quality objectives is the sum of all pairwise distances between every pair of stations. The distance between two stations is the minimum possible number of tunnels on a path between them. Sergey Semyonovich decided to add new tunnels to the subway map. In particular, he connected any two stations $$$u$$$ and $$$v$$$ that were not connected with a direct tunnel but share a common neighbor, i.e. there exists such a station $$$w$$$ that the original map has a tunnel between $$$u$$$ and $$$w$$$ and a tunnel between $$$w$$$ and $$$v$$$. You are given a task to compute the sum of pairwise distances between all pairs of stations in the new map. Input The first line of the input contains a single integer $$$n$$$ ($$$2 leq n leq 200,000$$$)xa0— the number of subway stations in the imaginary city drawn by mayor's assistants. Each of the following $$$n - 1$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 leq u_i, v_i leq n$$$, $$$u_i e v_i$$$), meaning the station with these indices are connected with a direct tunnel. It is guaranteed that these $$$n$$$ stations and $$$n - 1$$$ tunnels form a tree. Output Print one integer that is equal to the sum of distances between all pairs of stations after Sergey Semyonovich draws new tunnels between all pairs of stations that share a common neighbor in the original map. Note In the first sample, in the new map all pairs of stations share a direct connection, so the sum of distances is $$$6$$$. In the second sample, the new map has a direct tunnel between all pairs of stations except for the pair $$$(1, 4)$$$. For these two stations the distance is $$$2$$$. | 2,000 | false | false | false | true | false | false | false | false | false | false | 5,412 |
1329E | There is a string of length $$$n+1$$$ of characters 'A' and 'B'. The first character and last character of the string are equal to 'A'. You are given $$$m$$$ indices $$$p_1, p_2, ldots, p_m$$$ ($$$0$$$-indexation) denoting the other indices of characters 'A' in the string. Let's denote the minimum distance between two neighboring 'A' as $$$l$$$, and maximum distance between neighboring 'A' as $$$r$$$. For example, $$$(l,r)$$$ of string "ABBAABBBA" is $$$(1,4)$$$. And let's denote the balance degree of a string as the value of $$$r-l$$$. Now Dreamoon wants to change exactly $$$k$$$ characters from 'B' to 'A', and he wants to make the balance degree of the string as small as possible. Please calculate the required minimum possible value of balance degree. Input The first line contains one integer $$$t$$$ denoting the number of test cases ($$$1 leq t leq 400,000$$$). For each test case, the first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 leq n leq 10^{15}, 0 leq m leq 400,000, 0 leq k < n - m$$$). The second line contains $$$m$$$ integers $$$p_1, p_2, ldots, p_m$$$, ($$$0 < p_1 < p_2 < ldots < p_m < n$$$). The total sum of $$$m$$$ is at most $$$400,000$$$. Output For each test case, print one integer: the smallest possible value of balance degree after $$$k$$$ changes of 'B' to 'A'. Example Input 5 80 3 5 11 24 50 81 7 12 4 10 17 26 37 48 61 25 10 14 3 4 7 12 13 15 17 19 21 23 1 0 0 10 2 0 2 4 | 3,300 | false | true | false | false | false | false | false | true | false | false | 4,061 |
772A | You have _n_ devices that you want to use simultaneously. The _i_-th device uses _a__i_ units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·_a__i_ units of power. The _i_-th device currently has _b__i_ units of power stored. All devices can store an arbitrary amount of power. You have a single charger that can plug to any single device. The charger will add _p_ units of power per second to a device. This charging is continuous. That is, if you plug in a device for λ seconds, it will gain λ·_p_ units of power. You can switch which device is charging at any arbitrary unit of time (including real numbers), and the time it takes to switch is negligible. You are wondering, what is the maximum amount of time you can use the devices until one of them hits 0 units of power. If you can use the devices indefinitely, print -1. Otherwise, print the maximum amount of time before any one device hits 0 power. Input The first line contains two integers, _n_ and _p_ (1u2009≤u2009_n_u2009≤u2009100u2009000, 1u2009≤u2009_p_u2009≤u2009109)xa0— the number of devices and the power of the charger. This is followed by _n_ lines which contain two integers each. Line _i_ contains the integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009100u2009000)xa0— the power of the device and the amount of power stored in the device in the beginning. Output If you can use the devices indefinitely, print -1. Otherwise, print the maximum amount of time before any one device hits 0 power. Your answer will be considered correct if its absolute or relative error does not exceed 10u2009-u20094. Namely, let's assume that your answer is _a_ and the answer of the jury is _b_. The checker program will consider your answer correct if . Note In sample test 1, you can charge the first device for the entire time until it hits zero power. The second device has enough power to last this time without being charged. In sample test 2, you can use the device indefinitely. In sample test 3, we can charge the third device for 2u2009/u20095 of a second, then switch to charge the second device for a 1u2009/u200910 of a second. | 1,800 | true | false | false | false | false | false | false | true | false | false | 6,692 |
460D | Problem - 460D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force constructive algorithms math *2300 No tag edit access → Contest materials ). Output Print the minimum possible value of _f_(_S_). Then print the cardinality of set _S_. Then print the elements of the set in any order. If there are multiple optimal sets, you can print any of them. Examples Input 8 15 3 Output 1 2 10 11 Input 8 30 7 Output 0 5 14 9 28 11 16 Note Operation represents the operation of bitwise exclusive OR. In other words, it is the XOR operation. | 2,300 | true | false | false | false | false | true | true | false | false | false | 7,998 |
1055B | Alice's hair is growing by leaps and bounds. Maybe the cause of it is the excess of vitamins, or maybe it is some black magic... To prevent this, Alice decided to go to the hairdresser. She wants for her hair length to be at most $$$l$$$ centimeters after haircut, where $$$l$$$ is her favorite number. Suppose, that the Alice's head is a straight line on which $$$n$$$ hairlines grow. Let's number them from $$$1$$$ to $$$n$$$. With one swing of the scissors the hairdresser can shorten all hairlines on any segment to the length $$$l$$$, given that all hairlines on that segment had length strictly greater than $$$l$$$. The hairdresser wants to complete his job as fast as possible, so he will make the least possible number of swings of scissors, since each swing of scissors takes one second. Alice hasn't decided yet when she would go to the hairdresser, so she asked you to calculate how much time the haircut would take depending on the time she would go to the hairdresser. In particular, you need to process queries of two types: $$$0$$$xa0— Alice asks how much time the haircut would take if she would go to the hairdresser now. $$$1$$$ $$$p$$$ $$$d$$$xa0— $$$p$$$-th hairline grows by $$$d$$$ centimeters. Note, that in the request $$$0$$$ Alice is interested in hypothetical scenario of taking a haircut now, so no hairlines change their length. Input The first line contains three integers $$$n$$$, $$$m$$$ and $$$l$$$ ($$$1 le n, m le 100,000$$$, $$$1 le l le 10^9$$$)xa0— the number of hairlines, the number of requests and the favorite number of Alice. The second line contains $$$n$$$ integers $$$a_i$$$ ($$$1 le a_i le 10^9$$$)xa0— the initial lengths of all hairlines of Alice. Each of the following $$$m$$$ lines contains a request in the format described in the statement. The request description starts with an integer $$$t_i$$$. If $$$t_i = 0$$$, then you need to find the time the haircut would take. Otherwise, $$$t_i = 1$$$ and in this moment one hairline grows. The rest of the line than contains two more integers: $$$p_i$$$ and $$$d_i$$$ ($$$1 le p_i le n$$$, $$$1 le d_i le 10^9$$$)xa0— the number of the hairline and the length it grows by. Output For each query of type $$$0$$$ print the time the haircut would take. Example Input 4 7 3 1 2 3 4 0 1 2 3 0 1 1 3 0 1 3 1 0 Note Consider the first example: Initially lengths of hairlines are equal to $$$1, 2, 3, 4$$$ and only $$$4$$$-th hairline is longer $$$l=3$$$, and hairdresser can cut it in $$$1$$$ second. Then Alice's second hairline grows, the lengths of hairlines are now equal to $$$1, 5, 3, 4$$$ Now haircut takes two seonds: two swings are required: for the $$$4$$$-th hairline and for the $$$2$$$-nd. Then Alice's first hairline grows, the lengths of hairlines are now equal to $$$4, 5, 3, 4$$$ The haircut still takes two seconds: with one swing hairdresser can cut $$$4$$$-th hairline and with one more swing cut the segment from $$$1$$$-st to $$$2$$$-nd hairline. Then Alice's third hairline grows, the lengths of hairlines are now equal to $$$4, 5, 4, 4$$$ Now haircut takes only one second: with one swing it is possible to cut the segment from $$$1$$$-st hairline to the $$$4$$$-th. | 1,300 | false | false | true | false | false | false | false | false | false | false | 5,438 |
1704C | There are $$$n$$$ houses numbered from $$$1$$$ to $$$n$$$ on a circle. For each $$$1 leq i leq n - 1$$$, house $$$i$$$ and house $$$i + 1$$$ are neighbours; additionally, house $$$n$$$ and house $$$1$$$ are also neighbours. Initially, $$$m$$$ of these $$$n$$$ houses are infected by a deadly virus. Each morning, Cirno can choose a house which is uninfected and protect the house from being infected permanently. Every day, the following things happen in order: Cirno chooses an uninfected house, and protect it permanently. All uninfected, unprotected houses which have at least one infected neighbor become infected. Cirno wants to stop the virus from spreading. Find the minimum number of houses that will be infected in the end, if she optimally choose the houses to protect. Note that every day Cirno always chooses a house to protect before the virus spreads. Also, a protected house will not be infected forever. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of test cases. Description of test cases follows. The first line of each test case consists of two positive integers $$$n, m$$$ ($$$5 leq n leq 10^9$$$, $$$1 leq m leq min(n, 10^5)$$$) — the number of houses on the circle, and the number of houses that are initially infected. The second line of each test case consists of $$$m$$$ distinct positive integers $$$a_1, a_2, cdots , a_m$$$ ($$$1 leq a_i leq n$$$) — the indices of the houses infected initially. It is guaranteed that the sum of $$$m$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output an integer on a separate line, which is the minimum number of infected houses in the end. Example Input 8 10 3 3 6 8 6 2 2 5 20 3 3 7 12 41 5 1 11 21 31 41 10 5 2 4 6 8 10 5 5 3 2 5 4 1 1000000000 1 1 1000000000 4 1 1000000000 10 16 Note In the first test case: At the start of the first day, house $$$3$$$, $$$6$$$, $$$8$$$ are infected. Choose house $$$2$$$ to protect. At the start of the second day, house $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$, $$$8$$$, $$$9$$$ are infected. Choose house $$$10$$$ to protect. At the start of the third day, no more houses are infected. In the second test case: At the start of the first day, house $$$2$$$, $$$5$$$ are infected. Choose house $$$1$$$ to protect. At the start of the second day, house $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$ are infected. No more available houses can be protected. | 1,200 | false | true | true | false | false | false | false | false | true | false | 2,053 |
1969C | You are given an integer array $$$a$$$ of length $$$n$$$. You can perform the following operation: choose an element of the array and replace it with any of its neighbor's value. For example, if $$$a=[3, 1, 2]$$$, you can get one of the arrays $$$[3, 3, 2]$$$, $$$[3, 2, 2]$$$ and $$$[1, 1, 2]$$$ using one operation, but not $$$[2, 1, 2$$$] or $$$[3, 4, 2]$$$. Your task is to calculate the minimum possible total sum of the array if you can perform the aforementioned operation at most $$$k$$$ times. 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 two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 3 cdot 10^5$$$; $$$0 le k le 10$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$). Additional constraint on the input: the sum of $$$n$$$ over all test cases doesn't exceed $$$3 cdot 10^5$$$. Note In the first example, one of the possible sequences of operations is the following: $$$[3, 1, 2] ightarrow [1, 1, 2$$$]. In the second example, you do not need to apply the operation. In the third example, one of the possible sequences of operations is the following: $$$[2, 2, 1, 3] ightarrow [2, 1, 1, 3] ightarrow [2, 1, 1, 1]$$$. In the fourth example, one of the possible sequences of operations is the following: $$$[4, 1, 2, 2, 4, 3] ightarrow [1, 1, 2, 2, 4, 3] ightarrow [1, 1, 1, 2, 4, 3] ightarrow [1, 1, 1, 2, 2, 3]$$$. | 1,700 | false | false | true | true | false | false | false | false | false | false | 490 |
231E | A connected undirected graph is called a vertex cactus, if each vertex of this graph belongs to at most one simple cycle. A simple cycle in a undirected graph is a sequence of distinct vertices _v_1,u2009_v_2,u2009...,u2009_v__t_ (_t_u2009>u20092), such that for any _i_ (1u2009≤u2009_i_u2009<u2009_t_) exists an edge between vertices _v__i_ and _v__i_u2009+u20091, and also exists an edge between vertices _v_1 and _v__t_. A simple path in a undirected graph is a sequence of not necessarily distinct vertices _v_1,u2009_v_2,u2009...,u2009_v__t_ (_t_u2009>u20090), such that for any _i_ (1u2009≤u2009_i_u2009<u2009_t_) exists an edge between vertices _v__i_ and _v__i_u2009+u20091 and furthermore each edge occurs no more than once. We'll say that a simple path _v_1,u2009_v_2,u2009...,u2009_v__t_ starts at vertex _v_1 and ends at vertex _v__t_. You've got a graph consisting of _n_ vertices and _m_ edges, that is a vertex cactus. Also, you've got a list of _k_ pairs of interesting vertices _x__i_,u2009_y__i_, for which you want to know the following information — the number of distinct simple paths that start at vertex _x__i_ and end at vertex _y__i_. We will consider two simple paths distinct if the sets of edges of the paths are distinct. For each pair of interesting vertices count the number of distinct simple paths between them. As this number can be rather large, you should calculate it modulo 1000000007 (109u2009+u20097). Input The first line contains two space-separated integers _n_,u2009_m_ (2u2009≤u2009_n_u2009≤u2009105;xa01u2009≤u2009_m_u2009≤u2009105) — the number of vertices and edges in the graph, correspondingly. Next _m_ lines contain the description of the edges: the _i_-th line contains two space-separated integers _a__i_,u2009_b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_) — the indexes of the vertices connected by the _i_-th edge. The next line contains a single integer _k_ (1u2009≤u2009_k_u2009≤u2009105) — the number of pairs of interesting vertices. Next _k_ lines contain the list of pairs of interesting vertices: the _i_-th line contains two space-separated numbers _x__i_, _y__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_n_;xa0_x__i_u2009≠u2009_y__i_) — the indexes of interesting vertices in the _i_-th pair. It is guaranteed that the given graph is a vertex cactus. It is guaranteed that the graph contains no loops or multiple edges. Consider the graph vertices are numbered from 1 to _n_. Output Print _k_ lines: in the _i_-th line print a single integer — the number of distinct simple ways, starting at _x__i_ and ending at _y__i_, modulo 1000000007 (109u2009+u20097). | 2,100 | false | false | false | true | true | false | false | false | false | true | 8,912 |
1392E | This is an interactive problem. Omkar has just come across a duck! The duck is walking on a grid with $$$n$$$ rows and $$$n$$$ columns ($$$2 leq n leq 25$$$) so that the grid contains a total of $$$n^2$$$ cells. Let's denote by $$$(x, y)$$$ the cell in the $$$x$$$-th row from the top and the $$$y$$$-th column from the left. Right now, the duck is at the cell $$$(1, 1)$$$ (the cell in the top left corner) and would like to reach the cell $$$(n, n)$$$ (the cell in the bottom right corner) by moving either down $$$1$$$ cell or to the right $$$1$$$ cell each second. Since Omkar thinks ducks are fun, he wants to play a game with you based on the movement of the duck. First, for each cell $$$(x, y)$$$ in the grid, you will tell Omkar a nonnegative integer $$$a_{x,y}$$$ not exceeding $$$10^{16}$$$, and Omkar will then put $$$a_{x,y}$$$ uninteresting problems in the cell $$$(x, y)$$$. After that, the duck will start their journey from $$$(1, 1)$$$ to $$$(n, n)$$$. For each cell $$$(x, y)$$$ that the duck crosses during their journey (including the cells $$$(1, 1)$$$ and $$$(n, n)$$$), the duck will eat the $$$a_{x,y}$$$ uninteresting problems in that cell. Once the duck has completed their journey, Omkar will measure their mass to determine the total number $$$k$$$ of uninteresting problems that the duck ate on their journey, and then tell you $$$k$$$. Your challenge, given $$$k$$$, is to exactly reproduce the duck's path, i. e. to tell Omkar precisely which cells the duck crossed on their journey. To be sure of your mastery of this game, Omkar will have the duck complete $$$q$$$ different journeys ($$$1 leq q leq 10^3$$$). Note that all journeys are independent: at the beginning of each journey, the cell $$$(x, y)$$$ will still contain $$$a_{x,y}$$$ uninteresting tasks. Interaction The interaction will begin with a line containing a single integer $$$n$$$ ($$$2 leq n leq 25$$$), the amount of rows and columns in the grid. Read it. Your program should then print $$$n$$$ lines. The $$$x$$$-th line should contain $$$n$$$ integers $$$a_{x,1}, a_{x,2}, dotsc, a_{x,n}$$$ satisfying $$$0 leq a_{x,y} leq 10^{16}$$$, where $$$a_{x,y}$$$ is the amount of uninteresting problems Omkar should place in the cell $$$(x, y)$$$. After that, you will first receive a single integer $$$q$$$, the amount of journeys that the duck will take. $$$q$$$ queries will follow; each query will consist of a single line containing an integer $$$k$$$, the amount of uninteresting problems that the duck ate on that journey. After each query, given that you have determined that the duck visited the cells $$$(x_1, y_1), (x_2, y_2), dotsc, (x_{2n - 1}, y_{2n - 1})$$$ in that order (it should always be true that $$$(x_1, y_1) = (1, 1)$$$ and $$$(x_{2n - 1}, y_{2n - 1}) = (n, n)$$$), you should output $$$2n - 1$$$ lines so that the $$$j$$$-th line contains the two integers $$$x_j, y_j$$$. Bear in mind that if the sum on your path is $$$k$$$, but your path is different from the actual hidden path, then your solution is still wrong! After printing each line do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Hack Format To hack, first output a line containing $$$n$$$ and another line containing $$$q$$$. It must be true that $$$2 leq n leq 25$$$ and $$$1 leq q leq 1000$$$. Then, output the $$$q$$$ journeys taken by the duck in the same format as described above: for each journey, given that the duck visited the cells $$$(x_1, y_1), (x_2, y_2), dotsc, (x_{2n - 1}, y_{2n - 1})$$$ in that order, you should output $$$2n - 1$$$ lines so that the $$$j$$$-th line contains the two integers $$$x_j, y_j$$$. It must be true that $$$(x_1, y_1) = (1, 1)$$$ and $$$(x_{2n - 1}, y_{2n - 1}) = (n, n)$$$. Additionally, for each $$$j$$$ such that $$$2 leq j leq 2n - 1$$$, it must be true that $$$1 leq x_j, y_j leq n$$$ and either $$$(x_j, y_j) = (x_{j - 1} + 1, y_{j - 1})$$$ or $$$(x_j, y_j) = (x_{j - 1}, y_{j - 1} + 1)$$$. | 2,100 | true | false | false | false | false | true | false | false | false | false | 3,712 |
1558F | You have a permutation: an array $$$a = [a_1, a_2, ldots, a_n]$$$ of distinct integers from $$$1$$$ to $$$n$$$. The length of the permutation $$$n$$$ is odd. Consider the following algorithm of sorting the permutation in increasing order. A helper procedure of the algorithm, $$$f(i)$$$, takes a single argument $$$i$$$ ($$$1 le i le n-1$$$) and does the following. If $$$a_i > a_{i+1}$$$, the values of $$$a_i$$$ and $$$a_{i+1}$$$ are exchanged. Otherwise, the permutation doesn't change. The algorithm consists of iterations, numbered with consecutive integers starting with $$$1$$$. On the $$$i$$$-th iteration, the algorithm does the following: if $$$i$$$ is odd, call $$$f(1), f(3), ldots, f(n - 2)$$$; if $$$i$$$ is even, call $$$f(2), f(4), ldots, f(n - 1)$$$. It can be proven that after a finite number of iterations the permutation will be sorted in increasing order. After how many iterations will this happen for the first time? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 le n le 2 cdot 10^5 - 1$$$; $$$n$$$ is odd)xa0— the length of the permutation. The second line contains $$$n$$$ distinct integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$)xa0— the permutation itself. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5 - 1$$$. Output For each test case print the number of iterations after which the permutation will become sorted in increasing order for the first time. If the given permutation is already sorted, print $$$0$$$. Example Input 3 3 3 2 1 7 4 5 7 1 3 2 6 5 1 2 3 4 5 Note In the first test case, the permutation will be changing as follows: after the $$$1$$$-st iteration: $$$[2, 3, 1]$$$; after the $$$2$$$-nd iteration: $$$[2, 1, 3]$$$; after the $$$3$$$-rd iteration: $$$[1, 2, 3]$$$. In the second test case, the permutation will be changing as follows: after the $$$1$$$-st iteration: $$$[4, 5, 1, 7, 2, 3, 6]$$$; after the $$$2$$$-nd iteration: $$$[4, 1, 5, 2, 7, 3, 6]$$$; after the $$$3$$$-rd iteration: $$$[1, 4, 2, 5, 3, 7, 6]$$$; after the $$$4$$$-th iteration: $$$[1, 2, 4, 3, 5, 6, 7]$$$; after the $$$5$$$-th iteration: $$$[1, 2, 3, 4, 5, 6, 7]$$$. In the third test case, the permutation is already sorted and the answer is $$$0$$$. | 3,300 | false | false | false | false | true | false | false | false | true | false | 2,837 |
41B | Problem - 41B - 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 *1400 No tag edit access → Contest materials ") — the number of days and the initial number of money in bourles. The next line contains _n_ integers _a__i_ (1u2009≤u2009_a__i_u2009≤u20092000) — the prices of Martian dollars. Output Print the single number — which maximal sum of money in bourles can Vasya get by the end of day _n_. Examples Input 2 4 3 7 Output 8 Input 4 10 4 3 2 1 Output 10 Input 4 10 4 2 3 1 Output 15 | 1,400 | false | false | false | false | false | false | true | false | false | false | 9,783 |
1654E | You are given an array of integers $$$a_1, a_2, ldots, a_n$$$. You can do the following operation any number of times (possibly zero): Choose any index $$$i$$$ and set $$$a_i$$$ to any integer (positive, negative or $$$0$$$). What is the minimum number of operations needed to turn $$$a$$$ into an arithmetic progression? The array $$$a$$$ is an arithmetic progression if $$$a_{i+1}-a_i=a_i-a_{i-1}$$$ for any $$$2 leq i leq n-1$$$. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^5$$$). Note In the first test, you can get the array $$$a = [11, 10, 9, 8, 7, 6, 5, 4, 3]$$$ by performing $$$6$$$ operations: Set $$$a_3$$$ to $$$9$$$: the array becomes $$$[3, 2, 9, 8, 6, 9, 5, 4, 1]$$$; Set $$$a_2$$$ to $$$10$$$: the array becomes $$$[3, 10, 9, 8, 6, 9, 5, 4, 1]$$$; Set $$$a_6$$$ to $$$6$$$: the array becomes $$$[3, 10, 9, 8, 6, 6, 5, 4, 1]$$$; Set $$$a_9$$$ to $$$3$$$: the array becomes $$$[3, 10, 9, 8, 6, 6, 5, 4, 3]$$$; Set $$$a_5$$$ to $$$7$$$: the array becomes $$$[3, 10, 9, 8, 7, 6, 5, 4, 3]$$$; Set $$$a_1$$$ to $$$11$$$: the array becomes $$$[11, 10, 9, 8, 7, 6, 5, 4, 3]$$$. $$$a$$$ is an arithmetic progression: in fact, $$$a_{i+1}-a_i=a_i-a_{i-1}=-1$$$ for any $$$2 leq i leq n-1$$$. There is no sequence of less than $$$6$$$ operations that makes $$$a$$$ an arithmetic progression. In the second test, you can get the array $$$a = [-1, 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38]$$$ by performing $$$10$$$ operations. In the third test, you can get the array $$$a = [100000, 80000, 60000, 40000, 20000, 0, -20000, -40000, -60000, -80000]$$$ by performing $$$7$$$ operations. | 2,300 | true | false | false | false | true | false | true | false | false | true | 2,358 |
1850A | Suneet has three digits $$$a$$$, $$$b$$$, and $$$c$$$. Since math isn't his strongest point, he asks you to determine if you can choose any two digits to make a sum greater or equal to $$$10$$$. Output "YES" if there is such a pair, and "NO" otherwise. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The only line of each test case contains three digits $$$a$$$, $$$b$$$, $$$c$$$ ($$$0 leq a, b, c leq 9$$$). Output For each test case, output "YES" if such a pair exists, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer). Example Input 5 8 1 2 4 4 5 9 9 9 0 0 0 8 5 3 Note For the first test case, by choosing the digits $$$8$$$ and $$$2$$$ we can obtain a sum of $$$8 + 2 = 10$$$ which satisfies the condition, thus the output should be "YES". For the second test case, any combination of chosen digits won't be at least $$$10$$$, thus the output should be "NO" (note that we can not choose the digit on the same position twice). For the third test case, any combination of chosen digits will have a sum equal to $$$18$$$, thus the output should be "YES". | 800 | false | false | true | false | false | false | false | false | true | false | 1,182 |
1660E | You are given a binary matrix $$$A$$$ of size $$$n imes n$$$. Rows are numbered from top to bottom from $$$1$$$ to $$$n$$$, columns are numbered from left to right from $$$1$$$ to $$$n$$$. The element located at the intersection of row $$$i$$$ and column $$$j$$$ is called $$$A_{ij}$$$. Consider a set of $$$4$$$ operations: 1. Cyclically shift all rows up. The row with index $$$i$$$ will be written in place of the row $$$i-1$$$ ($$$2 le i le n$$$), the row with index $$$1$$$ will be written in place of the row $$$n$$$. 2. Cyclically shift all rows down. The row with index $$$i$$$ will be written in place of the row $$$i+1$$$ ($$$1 le i le n - 1$$$), the row with index $$$n$$$ will be written in place of the row $$$1$$$. 3. Cyclically shift all columns to the left. The column with index $$$j$$$ will be written in place of the column $$$j-1$$$ ($$$2 le j le n$$$), the column with index $$$1$$$ will be written in place of the column $$$n$$$. 4. Cyclically shift all columns to the right. The column with index $$$j$$$ will be written in place of the column $$$j+1$$$ ($$$1 le j le n - 1$$$), the column with index $$$n$$$ will be written in place of the column $$$1$$$. The $$$3 imes 3$$$ matrix is shown on the left before the $$$3$$$-rd operation is applied to it, on the rightxa0— after. You can perform an arbitrary (possibly zero) number of operations on the matrix; the operations can be performed in any order. After that, you can perform an arbitrary (possibly zero) number of new xor-operations: Select any element $$$A_{ij}$$$ and assign it with new value $$$A_{ij} oplus 1$$$. In other words, the value of $$$(A_{ij} + 1) bmod 2$$$ will have to be written into element $$$A_{ij}$$$. Each application of this xor-operation costs one burl. Note that the $$$4$$$ shift operationsxa0— are free. These $$$4$$$ operations can only be performed before xor-operations are performed. Output the minimum number of burles you would have to pay to make the $$$A$$$ matrix unitary. A unitary matrix is a matrix with ones on the main diagonal and the rest of its elements are zeros (that is, $$$A_{ij} = 1$$$ if $$$i = j$$$ and $$$A_{ij} = 0$$$ otherwise). Input The first line of the input contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0—the number of test cases in the test. The descriptions of the test cases follow. Before each test case, an empty line is written in the input. The first line of each test case contains a single number $$$n$$$ ($$$1 le n le 2000$$$) This is followed by $$$n$$$ lines, each containing exactly $$$n$$$ characters and consisting only of zeros and ones. These lines describe the values in the elements of the matrix. It is guaranteed that the sum of $$$n^2$$$ values over all test cases does not exceed $$$4 cdot 10^6$$$. Output For each test case, output the minimum number of burles you would have to pay to make the $$$A$$$ matrix unitary. In other words, print the minimum number of xor-operations it will take after applying cyclic shifts to the matrix for the $$$A$$$ matrix to become unitary. Example Input 4 3 010 011 100 5 00010 00001 10000 01000 00100 2 10 10 4 1111 1011 1111 1111 Note In the first test case, you can do the following: first, shift all the rows down cyclically, then the main diagonal of the matrix will contain only "1". Then it will be necessary to apply xor-operation to the only "1" that is not on the main diagonal. In the second test case, you can make a unitary matrix by applying the operation $$$2$$$xa0— cyclic shift of rows upward twice to the matrix. | 1,600 | false | true | true | false | false | true | true | false | false | false | 2,322 |
1904B | You are given an array $$$a$$$ of $$$n$$$ positive integers and a score. If your score is greater than or equal to $$$a_i$$$, then you can increase your score by $$$a_i$$$ and remove $$$a_i$$$ from the array. For each index $$$i$$$, output the maximum number of additional array elements that you can remove if you remove $$$a_i$$$ and then set your score to $$$a_i$$$. Note that the removal of $$$a_i$$$ should not be counted in the answer. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 5000$$$)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 10^5$$$)xa0— the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output $$$n$$$ integers, the $$$i$$$-th of which denotes the maximum number of additional array elements that you can remove if you remove $$$a_i$$$ from the array and then set your score to $$$a_i$$$. Example Input 4 5 20 5 1 4 2 3 1434 7 1442 1 1 5 999999999 999999999 999999999 1000000000 1000000000 Output 4 3 0 3 1 1 0 2 0 4 4 4 4 4 Note In the first test case, the answers are as follows: If we start with $$$i=4$$$, our initial score is $$$a_4=4$$$ and $$$a=[20,5,1,2]$$$. We can remove $$$3$$$ additional elements in the following order: 1. Since $$$4 ge 1$$$, we can remove $$$1$$$ and our score becomes $$$5$$$. After this, $$$a=[20,5,2]$$$. 2. Since $$$5 ge 5$$$, we can remove $$$5$$$ and our score becomes $$$10$$$. After this, $$$a=[20,2]$$$. 3. Since $$$10 ge 2$$$, we can remove $$$2$$$ and our score becomes $$$12$$$. After this, $$$a=[20]$$$. If we start with $$$i=1$$$ we can remove all remaining elements in the array, so the answer is $$$4$$$. If we start with $$$i=2$$$, we can remove $$$3$$$ additional elements in the following order: $$$1$$$, $$$4$$$, $$$2$$$. If we start with $$$i=3$$$, we can remove no additional elements. If we start with $$$i=5$$$, we can remove $$$1$$$ additional element: $$$1$$$. | 1,100 | false | true | false | true | false | false | false | true | true | false | 889 |
1167G | So we got bored and decided to take our own guess at how would "Inception" production go if the budget for the film had been terribly low. The first scene we remembered was the one that features the whole city bending onto itself: It feels like it will require high CGI expenses, doesn't it? Luckily, we came up with a similar-looking scene which was a tiny bit cheaper to make. Firstly, forget about 3D, that's hard and expensive! The city is now represented as a number line (infinite to make it easier, of course). Secondly, the city doesn't have to look natural at all. There are $$$n$$$ buildings on the line. Each building is a square $$$1 imes 1$$$. Buildings are numbered from $$$1$$$ to $$$n$$$ in ascending order of their positions. Lower corners of building $$$i$$$ are at integer points $$$a_i$$$ and $$$a_i + 1$$$ of the number line. Also the distance between any two neighbouring buildings $$$i$$$ and $$$i + 1$$$ doesn't exceed $$$d$$$ (really, this condition is here just to make the city look not that sparse). Distance between some neighbouring buildings $$$i$$$ and $$$i + 1$$$ is calculated from the lower right corner of building $$$i$$$ to the lower left corner of building $$$i + 1$$$. Finally, curvature of the bend is also really hard to simulate! Let the bend at some integer coordinate $$$x$$$ be performed with the following algorithm. Take the ray from $$$x$$$ to $$$+infty$$$ and all the buildings which are on this ray and start turning the ray and the buildings counter-clockwise around point $$$x$$$. At some angle some building will touch either another building or a part of the line. You have to stop bending there (implementing buildings crushing is also not worth its money). Let's call the angle between two rays in the final state the terminal angle $$$alpha_x$$$. The only thing left is to decide what integer point $$$x$$$ is the best to start bending around. Fortunately, we've already chosen $$$m$$$ candidates to perform the bending. So, can you please help us to calculate terminal angle $$$alpha_x$$$ for each bend $$$x$$$ from our list of candidates? Input The first line contains two integer numbers $$$n$$$ and $$$d$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$0 le d le 7000$$$) — the number of buildings and the maximum distance between any pair of neighbouring buildings, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$a_1 = 0$$$, $$$0 < a_{i + 1} - a_i le d + 1$$$) — coordinates of left corners of corresponding buildings in ascending order. The third line contains single integer $$$m$$$ ($$$1 le m le 2 cdot 10^5$$$) — the number of candidates. The fourth line contains $$$m$$$ integers $$$x_1, x_2, dots, x_m$$$ ($$$0 le x_i le a_n + 1$$$, $$$x_i < x_{i + 1}$$$) — the coordinates of bends you need to calculate terminal angles for in ascending order. Output Print $$$m$$$ numbers. For each bend $$$x_i$$$ print terminal angle $$$alpha_{x_i}$$$ (in radians). Your answer is considered correct if its absolute error does not exceed $$$10^{-9}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$a - b le 10^{-9}$$$. Examples Input 3 5 0 5 7 9 0 1 2 3 4 5 6 7 8 Output 1.570796326794897 1.570796326794897 0.785398163397448 0.927295218001612 0.785398163397448 1.570796326794897 1.570796326794897 1.570796326794897 1.570796326794897 Output 1.570796326794897 0.927295218001612 1.570796326794897 Input 5 0 0 1 2 3 4 6 0 1 2 3 4 5 Output 1.570796326794897 3.141592653589793 3.141592653589793 3.141592653589793 3.141592653589793 1.570796326794897 Note Here you can see the picture of the city for the first example and the bend at position $$$2$$$ for it. The angle you need to measure is marked blue. You can see that it's equal to $$$frac pi 4$$$. You can see that no pair of neighbouring buildings have distance more than $$$4$$$ between them. $$$d = 4$$$ would also suffice for that test. | 3,100 | false | false | false | false | false | false | true | false | false | false | 4,877 |
335A | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly _n_ stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length _n_. Piegirl wants to create a string _s_ using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length _n_ for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form _s_. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. Input The first line contains string _s_ (1u2009≤u2009_s_u2009≤u20091000), consisting of lowercase English characters only. The second line contains an integer _n_ (1u2009≤u2009_n_u2009≤u20091000). Output On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of _n_ lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string _s_, print instead a single line with the number -1. Note In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | 1,400 | false | true | false | false | false | true | false | true | false | false | 8,498 |
207A3 | The Smart Beaver from ABBYY has once again surprised us! He has developed a new calculating device, which he called the "Beaver's Calculator 1.0". It is very peculiar and it is planned to be used in a variety of scientific problems. To test it, the Smart Beaver invited _n_ scientists, numbered from 1 to _n_. The _i_-th scientist brought _k__i_ calculating problems for the device developed by the Smart Beaver from ABBYY. The problems of the _i_-th scientist are numbered from 1 to _k__i_, and they must be calculated sequentially in the described order, since calculating each problem heavily depends on the results of calculating of the previous ones. Each problem of each of the _n_ scientists is described by one integer _a__i_,u2009_j_, where _i_ (1u2009≤u2009_i_u2009≤u2009_n_) is the number of the scientist, _j_ (1u2009≤u2009_j_u2009≤u2009_k__i_) is the number of the problem, and _a__i_,u2009_j_ is the number of resource units the calculating device needs to solve this problem. The calculating device that is developed by the Smart Beaver is pretty unusual. It solves problems sequentially, one after another. After some problem is solved and before the next one is considered, the calculating device allocates or frees resources. The most expensive operation for the calculating device is freeing resources, which works much slower than allocating them. It is therefore desirable that each next problem for the calculating device requires no less resources than the previous one. You are given the information about the problems the scientists offered for the testing. You need to arrange these problems in such an order that the number of adjacent "bad" pairs of problems in this list is minimum possible. We will call two consecutive problems in this list a "bad pair" if the problem that is performed first requires more resources than the one that goes after it. Do not forget that the problems of the same scientist must be solved in a fixed order. Input The first line contains integer _n_ — the number of scientists. To lessen the size of the input, each of the next _n_ lines contains five integers _k__i_, _a__i_,u20091, _x__i_, _y__i_, _m__i_ (0u2009≤u2009_a__i_,u20091u2009<u2009_m__i_u2009≤u2009109, 1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009109) — the number of problems of the _i_-th scientist, the resources the first problem requires and three parameters that generate the subsequent values of _a__i_,u2009_j_. For all _j_ from 2 to _k__i_, inclusive, you should calculate value _a__i_,u2009_j_ by formula _a__i_,u2009_j_u2009=u2009(_a__i_,u2009_j_u2009-u20091u2009*u2009_x__i_u2009+u2009_y__i_) _mod_ _m__i_, where _a_ _mod_ _b_ is the operation of taking the remainder of division of number _a_ by number _b_. To get the full points for the first group of tests it is sufficient to solve the problem with _n_u2009=u20092, 1u2009≤u2009_k__i_u2009≤u20092000. To get the full points for the second group of tests it is sufficient to solve the problem with _n_u2009=u20092, 1u2009≤u2009_k__i_u2009≤u2009200000. To get the full points for the third group of tests it is sufficient to solve the problem with 1u2009≤u2009_n_u2009≤u20095000, 1u2009≤u2009_k__i_u2009≤u20095000. Output On the first line print a single number — the number of "bad" pairs in the optimal order. If the total number of problems does not exceed 200000, also print lines — the optimal order of the problems. On each of these lines print two integers separated by a single space — the required number of resources for the problem and the number of the scientist who offered this problem, respectively. The scientists are numbered from 1 to _n_ in the order of input. Examples Input 2 2 1 1 1 10 2 3 1 1 10 Input 2 3 10 2 3 1000 3 100 1 999 1000 Output 2 10 1 23 1 49 1 100 2 99 2 98 2 Note In the first sample _n_u2009=u20092, _k_1u2009=u20092, _a_1,u20091u2009=u20091, _a_1,u20092u2009=u20092, _k_2u2009=u20092, _a_2,u20091u2009=u20093, _a_2,u20092u2009=u20094. We've got two scientists, each of them has two calculating problems. The problems of the first scientist require 1 and 2 resource units, the problems of the second one require 3 and 4 resource units. Let's list all possible variants of the calculating order (each problem is characterized only by the number of resource units it requires): (1,u20092,u20093,u20094), (1,u20093,u20092,u20094), (3,u20091,u20092,u20094), (1,u20093,u20094,u20092), (3,u20094,u20091,u20092), (3,u20091,u20094,u20092). Sequence of problems (1,u20093,u20092,u20094) has one "bad" pair (3 and 2), (3,u20091,u20094,u20092) has two "bad" pairs (3 and 1, 4 and 2), and (1,u20092,u20093,u20094) has no "bad" pairs. | 2,000 | false | true | false | false | false | false | false | false | false | false | 9,018 |
464B | Peter had a cube with non-zero length of a side. He put the cube into three-dimensional space in such a way that its vertices lay at integer points (it is possible that the cube's sides are not parallel to the coordinate axes). Then he took a piece of paper and wrote down eight lines, each containing three integers — coordinates of cube's vertex (a single line contains coordinates of a single vertex, each vertex is written exactly once), put the paper on the table and left. While Peter was away, his little brother Nick decided to play with the numbers on the paper. In one operation Nick could swap some numbers inside a single line (Nick didn't swap numbers from distinct lines). Nick could have performed any number of such operations. When Peter returned and found out about Nick's mischief, he started recollecting the original coordinates. Help Peter restore the original position of the points or else state that this is impossible and the numbers were initially recorded incorrectly. Input Each of the eight lines contains three space-separated integers — the numbers written on the piece of paper after Nick's mischief. All numbers do not exceed 106 in their absolute value. Output If there is a way to restore the cube, then print in the first line "YES". In each of the next eight lines print three integers — the restored coordinates of the points. The numbers in the _i_-th output line must be a permutation of the numbers in _i_-th input line. The numbers should represent the vertices of a cube with non-zero length of a side. If there are multiple possible ways, print any of them. If there is no valid way, print "NO" (without the quotes) in the first line. Do not print anything else. Examples Input 0 0 0 0 0 1 0 0 1 0 0 1 0 1 1 0 1 1 0 1 1 1 1 1 Output YES 0 0 0 0 0 1 0 1 0 1 0 0 0 1 1 1 0 1 1 1 0 1 1 1 Input 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 | 2,000 | false | false | false | false | false | false | true | false | false | false | 7,983 |
126D | Fibonacci numbers have the following form: _F_1u2009=u20091,u2009 _F_2u2009=u20092,u2009 _F__i_u2009=u2009_F__i_u2009-u20091u2009+u2009_F__i_u2009-u20092,u2009_i_u2009>u20092. Let's consider some non-empty set _S_u2009=u2009{_s_1,u2009_s_2,u2009...,u2009_s__k_}, consisting of different Fibonacci numbers. Let's find the sum of values of this set's elements: Let's call the set _S_ a number _n_'s decomposition into Fibonacci sum. It's easy to see that several numbers have several decompositions into Fibonacci sum. For example, for 13 we have 13,u20095u2009+u20098,u20092u2009+u20093u2009+u20098 — three decompositions, and for 16: 3u2009+u200913,u20091u2009+u20092u2009+u200913,u20093u2009+u20095u2009+u20098,u20091u2009+u20092u2009+u20095u2009+u20098 — four decompositions. By the given number _n_ determine the number of its possible different decompositions into Fibonacci sum. Input The first line contains an integer _t_ — the number of tests (1u2009≤u2009_t_u2009≤u2009105). Each of the following _t_ lines contains one test. Each test is an integer _n_ (1u2009≤u2009_n_u2009≤u20091018). Please do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specificator. Output For each input data test print a single number on a single line — the answer to the problem. Note Two decompositions are different if there exists a number that is contained in the first decomposition, but is not contained in the second one. Decompositions that differ only in the order of summands are considered equal. | 2,300 | true | false | false | true | false | false | false | false | false | false | 9,374 |
1890A | An array $$$b_1, b_2, ldots, b_n$$$ of positive integers is good if all the sums of two adjacent elements are equal to the same value. More formally, the array is good if there exists a $$$k$$$ such that $$$b_1 + b_2 = b_2 + b_3 = ldots = b_{n-1} + b_n = k$$$. Doremy has an array $$$a$$$ of length $$$n$$$. Now Doremy can permute its elements (change their order) however she wants. Determine if she can make the array good. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1le tle 100$$$)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$$$ ($$$2 le n le 100$$$)xa0— the length of the array $$$a$$$. The second line of each test case contains $$$ n $$$ integers $$$ a_1,a_2,ldots,a_n $$$ ($$$1 le a_i le 10^5$$$). There are no constraints on the sum of $$$n$$$ over all test cases. Output For each test case, print "Yes" (without quotes), if it is possible to make the array good, and "No" (without quotes) otherwise. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Example Input 5 2 8 9 3 1 1 2 4 1 1 4 5 5 2 3 3 3 3 4 100000 100000 100000 100000 Note In the first test case, $$$[8,9]$$$ and $$$[9,8]$$$ are good. In the second test case, $$$[1,2,1]$$$ is good because $$$a_1+a_2=a_2+a_3=3$$$. In the third test case, it can be shown that no permutation is good. | 800 | false | false | false | false | false | true | false | false | false | false | 959 |
1611E2 | The only difference with E1 is the question of the problem. Vlad built a maze out of $$$n$$$ rooms and $$$n-1$$$ bidirectional corridors. From any room $$$u$$$ any other room $$$v$$$ can be reached through a sequence of corridors. Thus, the room system forms an undirected tree. Vlad invited $$$k$$$ friends to play a game with them. Vlad starts the game in the room $$$1$$$ and wins if he reaches a room other than $$$1$$$, into which exactly one corridor leads. Friends are placed in the maze: the friend with number $$$i$$$ is in the room $$$x_i$$$, and no two friends are in the same room (that is, $$$x_i eq x_j$$$ for all $$$i eq j$$$). Friends win if one of them meets Vlad in any room or corridor before he wins. For one unit of time, each participant of the game can go through one corridor. All participants move at the same time. Participants may not move. Each room can fit all participants at the same time. Friends know the plan of a maze and intend to win. They don't want to waste too much energy. They ask you to determine if they can win and if they can, what minimum number of friends must remain in the maze so that they can always catch Vlad. In other words, you need to determine the size of the minimum (by the number of elements) subset of friends who can catch Vlad or say that such a subset does not exist. Input The first line of the input contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases in the input. The input contains an empty string before each test case. The first line of the test case contains two numbers $$$n$$$ and $$$k$$$ ($$$1 le k < n le 2cdot 10^5$$$) — the number of rooms and friends, respectively. The next line of the test case contains $$$k$$$ integers $$$x_1, x_2, dots, x_k$$$ ($$$2 le x_i le n$$$) — numbers of rooms with friends. All $$$x_i$$$ are different. The next $$$n-1$$$ lines contain descriptions of the corridors, two numbers per line $$$v_j$$$ and $$$u_j$$$ ($$$1 le u_j, v_j le n$$$) — numbers of rooms that connect the $$$j$$$ corridor. All corridors are bidirectional. From any room, you can go to any other by moving along the corridors. It is guaranteed that the sum of the values $$$n$$$ over all test cases in the test is not greater than $$$2cdot10^5$$$. Output Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be $$$-1$$$ if Vlad wins anyway and a minimal number of friends otherwise. Example Input 4 8 2 5 3 4 7 2 5 1 6 3 6 7 2 1 7 6 8 8 4 6 5 7 3 4 7 2 5 1 6 3 6 7 2 1 7 6 8 3 1 2 1 2 2 3 3 2 2 3 3 1 1 2 Note In the first set of inputs, even if all the friends stay in the maze, Vlad can still win. Therefore, the answer is "-1". In the second set of inputs it is enough to leave friends from rooms $$$6$$$ and $$$7$$$. Then Vlad will not be able to win. The answer is "2". In the third and fourth sets of inputs Vlad cannot win only if all his friends stay in the maze. Therefore the answers are "1" and "2". | 1,900 | false | true | false | true | false | false | false | false | false | false | 2,586 |
1904C | You are given an array $$$a$$$ of $$$n$$$ positive integers. In one operation, you must pick some $$$(i, j)$$$ such that $$$1leq i < jleq a$$$ and append $$$a_i - a_j$$$ to the end of the $$$a$$$ (i.e. increase $$$n$$$ by $$$1$$$ and set $$$a_n$$$ to $$$a_i - a_j$$$). Your task is to minimize and print the minimum value of $$$a$$$ after performing $$$k$$$ operations. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2le nle 2cdot 10^3$$$, $$$1le kle 10^9$$$)xa0— the length of the array and the number of operations you should perform. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1le a_ile 10^{18}$$$)xa0— the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$4cdot 10^6$$$. Output For each test case, print a single integerxa0— the smallest possible value of the minimum of array $$$a$$$ after performing $$$k$$$ operations. Example Input 4 5 2 3 9 7 15 1 4 3 7 4 15 12 6 2 42 47 50 54 62 79 2 1 500000000000000000 1000000000000000000 Output 1 0 3 500000000000000000 Note In the first test case, after any $$$k=2$$$ operations, the minimum value of $$$a$$$ will be $$$1$$$. In the second test case, an optimal strategy is to first pick $$$i=1, j=2$$$ and append $$$a_1 - a_2 = 3$$$ to the end of $$$a$$$, creating $$$a=[7, 4, 15, 12, 3]$$$. Then, pick $$$i=3, j=4$$$ and append $$$a_3 - a_4 = 3$$$ to the end of $$$a$$$, creating $$$a=[7, 4, 15, 12, 3, 3]$$$. In the final operation, pick $$$i=5, j=6$$$ and append $$$a_5 - a_6 = 0$$$ to the end of $$$a$$$. Then the minimum value of $$$a$$$ will be $$$0$$$. In the third test case, an optimal strategy is to first pick $$$i=2, j=3$$$ to append $$$a_2 - a_3 = 3$$$ to the end of $$$a$$$. Any second operation will still not make the minimum value of $$$a$$$ be less than $$$3$$$. | 1,400 | false | false | false | false | true | false | true | true | true | false | 888 |
474F | Mole is hungry again. He found one ant colony, consisting of _n_ ants, ordered in a row. Each ant _i_ (1u2009≤u2009_i_u2009≤u2009_n_) has a strength _s__i_. In order to make his dinner more interesting, Mole organizes a version of «Hunger Games» for the ants. He chooses two numbers _l_ and _r_ (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_) and each pair of ants with indices between _l_ and _r_ (inclusively) will fight. When two ants _i_ and _j_ fight, ant _i_ gets one battle point only if _s__i_ divides _s__j_ (also, ant _j_ gets one battle point only if _s__j_ divides _s__i_). After all fights have been finished, Mole makes the ranking. An ant _i_, with _v__i_ battle points obtained, is going to be freed only if _v__i_u2009=u2009_r_u2009-u2009_l_, or in other words only if it took a point in every fight it participated. After that, Mole eats the rest of the ants. Note that there can be many ants freed or even none. In order to choose the best sequence, Mole gives you _t_ segments [_l__i_,u2009_r__i_] and asks for each of them how many ants is he going to eat if those ants fight. Input The first line contains one integer _n_ (1u2009≤u2009_n_u2009≤u2009105), the size of the ant colony. The second line contains _n_ integers _s_1,u2009_s_2,u2009...,u2009_s__n_ (1u2009≤u2009_s__i_u2009≤u2009109), the strengths of the ants. The third line contains one integer _t_ (1u2009≤u2009_t_u2009≤u2009105), the number of test cases. Each of the next _t_ lines contains two integers _l__i_ and _r__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_), describing one query. Output Print to the standard output _t_ lines. The _i_-th line contains number of ants that Mole eats from the segment [_l__i_,u2009_r__i_]. Examples Input 5 1 3 2 4 2 4 1 5 2 5 3 5 4 5 Note In the first test battle points for each ant are _v_u2009=u2009[4,u20090,u20092,u20090,u20092], so ant number 1 is freed. Mole eats the ants 2, 3, 4, 5. In the second test case battle points are _v_u2009=u2009[0,u20092,u20090,u20092], so no ant is freed and all of them are eaten by Mole. In the third test case battle points are _v_u2009=u2009[2,u20090,u20092], so ants number 3 and 5 are freed. Mole eats only the ant 4. In the fourth test case battle points are _v_u2009=u2009[0,u20091], so ant number 5 is freed. Mole eats the ant 4. | 2,100 | true | false | false | false | true | false | false | false | false | false | 7,935 |
1408D | There are $$$n$$$ robbers at coordinates $$$(a_1, b_1)$$$, $$$(a_2, b_2)$$$, ..., $$$(a_n, b_n)$$$ and $$$m$$$ searchlight at coordinates $$$(c_1, d_1)$$$, $$$(c_2, d_2)$$$, ..., $$$(c_m, d_m)$$$. In one move you can move each robber to the right (increase $$$a_i$$$ of each robber by one) or move each robber up (increase $$$b_i$$$ of each robber by one). Note that you should either increase all $$$a_i$$$ or all $$$b_i$$$, you can't increase $$$a_i$$$ for some points and $$$b_i$$$ for some other points. Searchlight $$$j$$$ can see a robber $$$i$$$ if $$$a_i leq c_j$$$ and $$$b_i leq d_j$$$. A configuration of robbers is safe if no searchlight can see a robber (i.e. if there is no pair $$$i,j$$$ such that searchlight $$$j$$$ can see a robber $$$i$$$). What is the minimum number of moves you need to perform to reach a safe configuration? Input The first line of input contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 2000$$$): the number of robbers and the number of searchlight. Each of the next $$$n$$$ lines contains two integers $$$a_i$$$, $$$b_i$$$ ($$$0 leq a_i, b_i leq 10^6$$$), coordinates of robbers. Each of the next $$$m$$$ lines contains two integers $$$c_i$$$, $$$d_i$$$ ($$$0 leq c_i, d_i leq 10^6$$$), coordinates of searchlights. Output Print one integer: the minimum number of moves you need to perform to reach a safe configuration. Examples Input 2 3 1 6 6 1 10 1 1 10 7 7 Input 7 3 0 8 3 8 2 7 0 10 5 5 7 0 3 5 6 6 3 11 11 5 Note In the first test, you can move each robber to the right three times. After that there will be one robber in the coordinates $$$(3, 0)$$$. The configuration of the robbers is safe, because the only searchlight can't see the robber, because it is in the coordinates $$$(2, 3)$$$ and $$$3 > 2$$$. In the second test, you can move each robber to the right two times and two times up. After that robbers will be in the coordinates $$$(3, 8)$$$, $$$(8, 3)$$$. It's easy the see that the configuration of the robbers is safe. It can be proved that you can't reach a safe configuration using no more than $$$3$$$ moves. | 2,000 | false | false | true | true | true | false | true | true | true | false | 3,633 |
1625E1 | This is the easy version of the problem. The only difference between the easy and the hard versions are removal queries, they are present only in the hard version. "Interplanetary Software, Inc." together with "Robots of Cydonia, Ltd." has developed and released robot cats. These electronic pets can meow, catch mice and entertain the owner in various ways. The developers from "Interplanetary Software, Inc." have recently decided to release a software update for these robots. After the update, the cats must solve the problems about bracket sequences. One of the problems is described below. First, we need to learn a bit of bracket sequence theory. Consider the strings that contain characters "(", ")" and ".". Call a string regular bracket sequence (RBS), if it can be transformed to an empty string by one or more operations of removing either single "." characters, or a continuous substring "()". For instance, the string "(()(.))" is an RBS, as it can be transformed to an empty string with the following sequence of removals: "(()(.))" $$$ ightarrow$$$ "(()())" $$$ ightarrow$$$ "(())" $$$ ightarrow$$$ "()" $$$ ightarrow$$$ "". We got an empty string, so the initial string was an RBS. At the same time, the string ")(" is not an RBS, as it is not possible to apply such removal operations to it. An RBS is simple if this RBS is not empty, doesn't start with ".", and doesn't end with ".". Denote the substring of the string $$$s$$$ as its sequential subsegment. In particular, $$$s[ldots r] = s_ls_{l+1}dots s_r$$$, where $$$s_i$$$ is the $$$i$$$-th character of the string $$$s$$$. Now, move on to the problem statement itself. You are given a string $$$s$$$, initially consisting of characters "(" and ")". You need to answer the queries of the following kind. Given two indices, $$$l$$$ and $$$r$$$ ($$$1 le l < r le n$$$), and it's guaranteed that the substring $$$s[ldots r]$$$ is a simple RBS. You need to find the number of substrings in $$$s[ldots r]$$$ such that they are simple RBS. In other words, find the number of index pairs $$$i$$$, $$$j$$$ such that $$$l le i < j le r$$$ and $$$s[idots j]$$$ is a simple RBS. You are an employee in "Interplanetary Software, Inc." and you were given the task to teach the cats to solve the problem above, after the update. Note that the "." character cannot appear in the string in this version of the problem. It is only needed for the hard version. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 le n le 3cdot10^5$$$, $$$1 le q le 3cdot10^5$$$), the length of the string, and the number of queries. The second line contains the string $$$s$$$, consisting of $$$n$$$ characters "(" and ")". Each of the following $$$q$$$ lines contains three integers $$$t$$$, $$$l$$$ and $$$r$$$ ($$$t = 2$$$, $$$1 le l < r le n$$$), the queries you need to answer. It is guaranteed that all the queries are valid and correspond to the problem statements. Note that $$$t$$$ is unused and always equal to two in this problem. It is required for the hard version of the problem. Note Consider the example test case. The answer to the first query is $$$3$$$, as there are three suitable substrings: $$$s[3dots6]$$$, $$$s[3dots4]$$$ and $$$s[5dots6]$$$. The answer to the second query is $$$4$$$. The substrings are $$$s[3dots6]$$$, $$$s[3dots4]$$$, $$$s[5dots6]$$$ and $$$s[2dots7]$$$. The answer to the third query is $$$1$$$. The substring is $$$s[8dots9]$$$. The answer to the fourth query is $$$6$$$. The substrings are $$$s[3dots6]$$$, $$$s[3dots4]$$$, $$$s[5dots6]$$$, $$$s[2dots7]$$$, $$$s[8dots9]$$$ and $$$s[2dots9]$$$. | 2,500 | false | false | false | true | true | false | true | false | false | true | 2,489 |
225A | A dice is a cube, its faces contain distinct integers from 1 to 6 as black points. The sum of numbers at the opposite dice faces always equals 7. Please note that there are only two dice (these dices are mirror of each other) that satisfy the given constraints (both of them are shown on the picture on the left). Alice and Bob play dice. Alice has built a tower from _n_ dice. We know that in this tower the adjacent dice contact with faces with distinct numbers. Bob wants to uniquely identify the numbers written on the faces of all dice, from which the tower is built. Unfortunately, Bob is looking at the tower from the face, and so he does not see all the numbers on the faces. Bob sees the number on the top of the tower and the numbers on the two adjacent sides (on the right side of the picture shown what Bob sees). Help Bob, tell whether it is possible to uniquely identify the numbers on the faces of all the dice in the tower, or not. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of dice in the tower. The second line contains an integer _x_ (1u2009≤u2009_x_u2009≤u20096) — the number Bob sees at the top of the tower. Next _n_ lines contain two space-separated integers each: the _i_-th line contains numbers _a__i_,u2009_b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u20096;xa0_a__i_u2009≠u2009_b__i_) — the numbers Bob sees on the two sidelong faces of the _i_-th dice in the tower. Consider the dice in the tower indexed from top to bottom from 1 to _n_. That is, the topmost dice has index 1 (the dice whose top face Bob can see). It is guaranteed that it is possible to make a dice tower that will look as described in the input. Output Print "YES" (without the quotes), if it is possible to to uniquely identify the numbers on the faces of all the dice in the tower. If it is impossible, print "NO" (without the quotes). | 1,100 | false | true | false | false | false | true | false | false | false | false | 8,940 |
1463D | You have $$$2n$$$ integers $$$1, 2, dots, 2n$$$. You have to redistribute these $$$2n$$$ elements into $$$n$$$ pairs. After that, you choose $$$x$$$ pairs and take minimum elements from them, and from the other $$$n - x$$$ pairs, you take maximum elements. Your goal is to obtain the set of numbers $$${b_1, b_2, dots, b_n}$$$ as the result of taking elements from the pairs. What is the number of different $$$x$$$-s ($$$0 le x le n$$$) such that it's possible to obtain the set $$$b$$$ if for each $$$x$$$ you can choose how to distribute numbers into pairs and from which $$$x$$$ pairs choose minimum elements? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The first line of each test case contains the integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$1 le b_1 < b_2 < dots < b_n le 2n$$$)xa0— the set you'd like to get. It's guaranteed that the sum of $$$n$$$ over test cases doesn't exceed $$$2 cdot 10^5$$$. Note In the first test case, $$$x = 1$$$ is the only option: you have one pair $$$(1, 2)$$$ and choose the minimum from this pair. In the second test case, there are three possible $$$x$$$-s. If $$$x = 1$$$, then you can form the following pairs: $$$(1, 6)$$$, $$$(2, 4)$$$, $$$(3, 5)$$$, $$$(7, 9)$$$, $$$(8, 10)$$$. You can take minimum from $$$(1, 6)$$$ (equal to $$$1$$$) and the maximum elements from all other pairs to get set $$$b$$$. If $$$x = 2$$$, you can form pairs $$$(1, 2)$$$, $$$(3, 4)$$$, $$$(5, 6)$$$, $$$(7, 9)$$$, $$$(8, 10)$$$ and take the minimum elements from $$$(1, 2)$$$, $$$(5, 6)$$$ and the maximum elements from the other pairs. If $$$x = 3$$$, you can form pairs $$$(1, 3)$$$, $$$(4, 6)$$$, $$$(5, 7)$$$, $$$(2, 9)$$$, $$$(8, 10)$$$ and take the minimum elements from $$$(1, 3)$$$, $$$(4, 6)$$$, $$$(5, 7)$$$. In the third test case, $$$x = 0$$$ is the only option: you can form pairs $$$(1, 3)$$$, $$$(2, 4)$$$ and take the maximum elements from both of them. | 1,900 | false | true | false | false | false | true | false | true | false | false | 3,370 |
453D | The Elements of Harmony are six supernatural artifacts representing subjective aspects of harmony. They are arguably the most powerful force in Equestria. The inside of Elements of Harmony can be seen as a complete graph with _n_ vertices labeled from 0 to _n_u2009-u20091, where _n_ is a power of two, equal to 2_m_. The energy in Elements of Harmony is in constant movement. According to the ancient book, the energy of vertex _u_ in time _i_ (_e__i_[_u_]) equals to: Here _b_[] is the transformation coefficient — an array of _m_u2009+u20091 integers and _f_(_u_,u2009_v_) is the number of ones in the binary representation of number (_u_xa0_xor_xa0_v_). Given the transformation coefficient and the energy distribution at time 0 (_e_0[]). Help Twilight Sparkle predict the energy distribution at time _t_ (_e__t_[]). The answer can be quite large, so output it modulo _p_. Input The first line contains three integers _m_, _t_ and _p_ (1u2009≤u2009_m_u2009≤u200920;xa00u2009≤u2009_t_u2009≤u20091018;xa02u2009≤u2009_p_u2009≤u2009109). The following line contains _n_ (_n_u2009=u20092_m_) integers _e_0[_i_] (1u2009≤u2009_e_0[_i_]u2009≤u2009109;xa00u2009≤u2009_i_u2009<u2009_n_). The next line contains _m_u2009+u20091 integers _b_[_i_] (0u2009≤u2009_b_[_i_]u2009≤u2009109;xa00u2009≤u2009_i_u2009≤u2009_m_). Output Output _n_ lines, the _i_-th line must contain a single integer _e__t_[_i_] modulo _p_. Examples Input 2 2 10000 4 1 2 3 0 1 0 | 3,000 | false | false | false | true | false | false | false | false | false | false | 8,023 |
673B | There are _n_ problems prepared for the next Codeforces round. They are arranged in ascending order by their difficulty, and no two problems have the same difficulty. Moreover, there are _m_ pairs of similar problems. Authors want to split problems between two division according to the following rules: Problemset of each division should be non-empty. Each problem should be used in exactly one division (yes, it is unusual requirement). Each problem used in division 1 should be harder than any problem used in division 2. If two problems are similar, they should be used in different divisions. Your goal is count the number of ways to split problem between two divisions and satisfy all the rules. Two ways to split problems are considered to be different if there is at least one problem that belongs to division 1 in one of them and to division 2 in the other. Note, that the relation of similarity is not transitive. That is, if problem _i_ is similar to problem _j_ and problem _j_ is similar to problem _k_, it doesn't follow that _i_ is similar to _k_. Input The first line of the input contains two integers _n_ and _m_ (2u2009≤u2009_n_u2009≤u2009100u2009000, 0u2009≤u2009_m_u2009≤u2009100u2009000)xa0— the number of problems prepared for the round and the number of pairs of similar problems, respectively. Each of the following _m_ lines contains a pair of similar problems _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_,u2009_u__i_u2009≠u2009_v__i_). It's guaranteed, that no pair of problems meets twice in the input. Output Print one integerxa0— the number of ways to split problems in two divisions. Note In the first sample, problems 1 and 2 should be used in division 2, while problems 4 and 5 in division 1. Problem 3 may be used either in division 1 or in division 2. In the second sample, all pairs of problems are similar and there is no way to split problem between two divisions without breaking any rules. Third sample reminds you that the similarity relation is not transitive. Problem 3 is similar to both 1 and 2, but 1 is not similar to 2, so they may be used together. | 1,300 | false | true | true | false | false | false | false | false | false | false | 7,132 |
1823A | Consider an array $$$a_1, a_2, dots, a_n$$$ consisting of numbers $$$1$$$ and $$$-1$$$. Define $$$A$$$-characteristic of this array as a number of pairs of indices $$$1 le i < j le n$$$, such that $$$a_i cdot a_j = 1$$$. Find any array $$$a$$$ with given length $$$n$$$ with $$$A$$$-characteristic equal to the given value $$$k$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). The description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 100$$$; $$$0 le k le frac{(n-1) n}{2}$$$)xa0— the length of required array and required $$$A$$$-characteristic. Output For each test case, if there is no array $$$a$$$ with given $$$A$$$-characteristic $$$k$$$, print NO. Otherwise, print YES and $$$n$$$ numbers $$$1$$$ and $$$-1$$$, which form the required array $$$a$$$. If there are multiple answers, print any of them. Example Input 7 2 0 2 1 3 1 3 2 3 3 5 4 5 5 Output YES 1 -1 YES 1 1 YES 1 -1 1 NO YES 1 1 1 YES -1 1 -1 1 1 NO Note In the first test case, there is only one pair of different elements in the array, and their product is $$$a_1 cdot a_2 = -1 eq 1$$$, hence its $$$A$$$-characteristic is $$$0$$$. In the second test case, there is only one pair of different elements in the array, and their product is $$$a_1 cdot a_2 = 1$$$, hence its $$$A$$$-characteristic is $$$1$$$. In the third test case, there are three pairs of different elements in the array, and their product are: $$$a_1 cdot a_2 = -1$$$, $$$a_1 cdot a_3 = 1$$$, $$$a_2 cdot a_3 = -1$$$, hence its $$$A$$$-characteristic is $$$1$$$. In the fourth test case, we can show, that there is no array with length $$$3$$$, which $$$A$$$-characteristic is $$$2$$$. | 800 | true | false | false | false | false | true | false | false | false | false | 1,343 |
1038D | There are $$$n$$$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $$$x$$$ eats a slime with a value $$$y$$$, the eaten slime disappears, and the value of the remaining slime changes to $$$x - y$$$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. Input The first line of the input contains an integer $$$n$$$ ($$$1 le n le 500,000$$$) denoting the number of slimes. The next line contains $$$n$$$ integers $$$a_i$$$ ($$$-10^9 le a_i le 10^9$$$), where $$$a_i$$$ is the value of $$$i$$$-th slime. Output Print an only integerxa0— the maximum possible value of the last slime. Note In the first example, a possible way of getting the last slime with value $$$4$$$ is: Second slime eats the third slime, the row now contains slimes $$$2, -1, 1$$$ Second slime eats the third slime, the row now contains slimes $$$2, -2$$$ First slime eats the second slime, the row now contains $$$4$$$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $$$4$$$. | 1,800 | false | true | true | true | false | false | false | false | false | false | 5,505 |
1012B | Innopolis University scientists continue to investigate the periodic table. There are _n_·_m_ known elements and they form a periodic table: a rectangle with _n_ rows and _m_ columns. Each element can be described by its coordinates (_r_,u2009_c_) (1u2009≤u2009_r_u2009≤u2009_n_, 1u2009≤u2009_c_u2009≤u2009_m_) in the table. Recently scientists discovered that for every four different elements in this table that form a rectangle with sides parallel to the sides of the table, if they have samples of three of the four elements, they can produce a sample of the fourth element using nuclear fusion. So if we have elements in positions (_r_1,u2009_c_1), (_r_1,u2009_c_2), (_r_2,u2009_c_1), where _r_1u2009≠u2009_r_2 and _c_1u2009≠u2009_c_2, then we can produce element (_r_2,u2009_c_2). Samples used in fusion are not wasted and can be used again in future fusions. Newly crafted elements also can be used in future fusions. Innopolis University scientists already have samples of _q_ elements. They want to obtain samples of all _n_·_m_ elements. To achieve that, they will purchase some samples from other laboratories and then produce all remaining elements using an arbitrary number of nuclear fusions in some order. Help them to find the minimal number of elements they need to purchase. Input The first line contains three integers _n_, _m_, _q_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009200u2009000; 0u2009≤u2009_q_u2009≤u2009_min_(_n_·_m_,u2009200u2009000)), the chemical table dimensions and the number of elements scientists already have. The following _q_ lines contain two integers _r__i_, _c__i_ (1u2009≤u2009_r__i_u2009≤u2009_n_, 1u2009≤u2009_c__i_u2009≤u2009_m_), each describes an element that scientists already have. All elements in the input are different. Output Print the minimal number of elements to be purchased. Examples Input 4 3 6 1 2 1 3 2 2 2 3 3 1 3 3 Note For each example you have a picture which illustrates it. The first picture for each example describes the initial set of element samples available. Black crosses represent elements available in the lab initially. The second picture describes how remaining samples can be obtained. Red dashed circles denote elements that should be purchased from other labs (the optimal solution should minimize the number of red circles). Blue dashed circles are elements that can be produced with nuclear fusion. They are numbered in order in which they can be produced. Test 1 We can use nuclear fusion and get the element from three other samples, so we don't need to purchase anything. Test 2 We cannot use any nuclear fusion at all as there is only one row, so we have to purchase all missing elements. Test 3 There are several possible solutions. One of them is illustrated below. Note that after purchasing one element marked as red it's still not possible to immidiately produce the middle element in the bottom row (marked as 4). So we produce the element in the left-top corner first (marked as 1), and then use it in future fusions. | 1,900 | false | false | false | false | false | true | false | false | false | true | 5,626 |
1184A2 | Problem - 1184A2 - 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 number theory *2100 No tag edit access → Contest materials time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output After learning about polynomial hashing, Heidi decided to learn about shift-xor hashing. In particular, she came across this interesting problem. Given a bitstring $$$y in {0,1}^n$$$ find out the number of different $$$k$$$ ($$$0 leq k < n$$$) such that there exists $$$x in {0,1}^n$$$ for which $$$y = x oplus mbox{shift}^k(x).$$$ In the above, $$$oplus$$$ is the xor operation and $$$mbox{shift}^k$$$ is the operation of shifting a bitstring cyclically to the right $$$k$$$ times. For example, $$$001 oplus 111 = 110$$$ and $$$mbox{shift}^3(00010010111000) = 00000010010111$$$. Input The first line contains an integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$), the length of the bitstring $$$y$$$. The second line contains the bitstring $$$y$$$. Output Output a single integer: the number of suitable values of $$$k$$$. Example Input 4 1010 Output 3 Note In the first example: $$$1100oplus mbox{shift}^1(1100) = 1010$$$ $$$1000oplus mbox{shift}^2(1000) = 1010$$$ $$$0110oplus mbox{shift}^3(0110) = 1010$$$ There is no $$$x$$$ such that $$$x oplus x = 1010$$$, hence the answer is $$$3$$$. | 2,100 | false | false | false | false | false | false | true | false | false | false | 4,793 |
1073B | Vasya has got $$$n$$$ books, numbered from $$$1$$$ to $$$n$$$, arranged in a stack. The topmost book has number $$$a_1$$$, the next one — $$$a_2$$$, and so on. The book at the bottom of the stack has number $$$a_n$$$. All numbers are distinct. Vasya wants to move all the books to his backpack in $$$n$$$ steps. During $$$i$$$-th step he wants to move the book number $$$b_i$$$ into his backpack. If the book with number $$$b_i$$$ is in the stack, he takes this book and all the books above the book $$$b_i$$$, and puts them into the backpack; otherwise he does nothing and begins the next step. For example, if books are arranged in the order $$$[1, 2, 3]$$$ (book $$$1$$$ is the topmost), and Vasya moves the books in the order $$$[2, 1, 3]$$$, then during the first step he will move two books ($$$1$$$ and $$$2$$$), during the second step he will do nothing (since book $$$1$$$ is already in the backpack), and during the third step — one book (the book number $$$3$$$). Note that $$$b_1, b_2, dots, b_n$$$ are distinct. Help Vasya! Tell him the number of books he will put into his backpack during each step. Input The first line contains one integer $$$n~(1 le n le 2 cdot 10^5)$$$ — the number of books in the stack. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n~(1 le a_i le n)$$$ denoting the stack of books. The third line contains $$$n$$$ integers $$$b_1, b_2, dots, b_n~(1 le b_i le n)$$$ denoting the steps Vasya is going to perform. All numbers $$$a_1 dots a_n$$$ are distinct, the same goes for $$$b_1 dots b_n$$$. Output Print $$$n$$$ integers. The $$$i$$$-th of them should be equal to the number of books Vasya moves to his backpack during the $$$i$$$-th step. Examples Input 6 6 5 4 3 2 1 6 5 3 4 2 1 Note The first example is described in the statement. In the second example, during the first step Vasya will move the books $$$[3, 1, 4]$$$. After that only books $$$2$$$ and $$$5$$$ remain in the stack ($$$2$$$ is above $$$5$$$). During the second step Vasya will take the books $$$2$$$ and $$$5$$$. After that the stack becomes empty, so during next steps Vasya won't move any books. | 1,000 | true | false | true | false | false | false | false | false | false | false | 5,352 |
1270G | You are given $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$, such that for each $$$1le i le n$$$ holds $$$i-nle a_ile i-1$$$. Find some nonempty subset of these integers, whose sum is equal to $$$0$$$. It can be shown that such a subset exists under given constraints. If there are several possible subsets with zero-sum, you can find any of them. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^6$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1le n le 10^6$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$i-n le a_i le i-1$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, output two lines. In the first line, output $$$s$$$ ($$$1le s le n$$$)xa0— the number of elements in your subset. In the second line, output $$$s$$$ integers $$$i_1, i_2, dots, i_s$$$ ($$$1le i_k le n$$$). All integers have to be pairwise different, and $$$a_{i_1} + a_{i_2} + dots + a_{i_s}$$$ has to be equal to $$$0$$$. If there are several possible subsets with zero-sum, you can find any of them. Example Input 2 5 0 1 2 3 4 4 -3 1 1 1 Note In the first example, we get sum is $$$a_1 = 0$$$. In the second example, we get sum is $$$a_1 + a_4 + a_3 + a_2 = 0$$$. | 2,700 | true | false | false | false | false | true | false | false | false | true | 4,338 |
499A | You have decided to watch the best moments of some movie. There are two buttons on your player: 1. Watch the current minute of the movie. By pressing this button, you watch the current minute of the movie and the player automatically proceeds to the next minute of the movie. 2. Skip exactly _x_ minutes of the movie (_x_ is some fixed positive integer). If the player is now at the _t_-th minute of the movie, then as a result of pressing this button, it proceeds to the minute (_t_u2009+u2009_x_). Initially the movie is turned on in the player on the first minute, and you want to watch exactly _n_ best moments of the movie, the _i_-th best moment starts at the _l__i_-th minute and ends at the _r__i_-th minute (more formally, the _i_-th best moment consists of minutes: _l__i_,u2009_l__i_u2009+u20091,u2009...,u2009_r__i_). Determine, what is the minimum number of minutes of the movie you have to watch if you want to watch all the best moments? Input The first line contains two space-separated integers _n_, _x_ (1u2009≤u2009_n_u2009≤u200950, 1u2009≤u2009_x_u2009≤u2009105) — the number of the best moments of the movie and the value of _x_ for the second button. The following _n_ lines contain the descriptions of the best moments of the movie, the _i_-th line of the description contains two integers separated by a space _l__i_, _r__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009105). It is guaranteed that for all integers _i_ from 2 to _n_ the following condition holds: _r__i_u2009-u20091u2009<u2009_l__i_. Output Output a single number — the answer to the problem. Note In the first sample, the player was initially standing on the first minute. As the minutes from the 1-st to the 4-th one don't contain interesting moments, we press the second button. Now we can not press the second button and skip 3 more minutes, because some of them contain interesting moments. Therefore, we watch the movie from the 4-th to the 6-th minute, after that the current time is 7. Similarly, we again skip 3 minutes and then watch from the 10-th to the 12-th minute of the movie. In total, we watch 6 minutes of the movie. In the second sample, the movie is very interesting, so you'll have to watch all 100000 minutes of the movie. | 1,000 | false | true | true | false | false | false | false | false | false | false | 7,839 |
549D | The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept. Let's consider a rectangular image that is represented with a table of size _n_u2009×u2009_m_. The table elements are integers that specify the brightness of each pixel in the image. A feature also is a rectangular table of size _n_u2009×u2009_m_. Each cell of a feature is painted black or white. To calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The value of a feature in the image is the value of _W_u2009-u2009_B_, where _W_ is the total brightness of the pixels in the image, covered with white feature cells, and _B_ is the total brightness of the pixels covered with black feature cells. Some examples of the most popular Haar features are given below. Your task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles. A prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image. You have a variable _value_, whose value is initially zero. In one operation you can count the sum of pixel values u200bu200bat any prefix rectangle, multiply it by any integer and add to variable _value_. You are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample. Input The first line contains two space-separated integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009100) — the number of rows and columns in the feature. Next _n_ lines contain the description of the feature. Each line consists of _m_ characters, the _j_-th character of the _i_-th line equals to "W", if this element of the feature is white and "B" if it is black. Note The first sample corresponds to feature _B_, the one shown in the picture. The value of this feature in an image of size 6u2009×u20098 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations: 1. add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable _value_ (the rectangle is indicated by a red frame); 2. add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient u2009-u20092 and variable _value_. Thus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1u2009-u20092u2009=u2009u2009-u20091, as required. | 1,900 | false | true | true | false | false | false | false | false | false | false | 7,642 |
1829H | Sadly, the problem setter couldn't think of an interesting story, thus he just asks you to solve the following problem. Given an array $$$a$$$ consisting of $$$n$$$ positive integers, count the number of non-empty subsequences for which the bitwise $$$mathsf{AND}$$$ of the elements in the subsequence has exactly $$$k$$$ set bits in its binary representation. The answer may be large, so output it modulo $$$10^9+7$$$. Recall that the subsequence of an array $$$a$$$ is a sequence that can be obtained from $$$a$$$ by removing some (possibly, zero) elements. For example, $$$. The description of the test cases follows. The first line of each test case consists of two integers $$$n$$$ and $$$k$$$ ($$$1 leq n leq 2 cdot 10^5$$$, $$$0 le k le 6$$$)xa0— the length of the array and the number of set bits that the bitwise $$$mathsf{AND}$$$ the counted subsequences should have in their binary representation. The second line of each test case consists of $$$n$$$ integers $$$a_i$$$ ($$$0 leq a_i leq 63$$$)xa0— the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integer — the number of subsequences that have exactly $$$k$$$ set bits in their bitwise $$$mathsf{AND}$$$ value's binary representation. The answer may be large, so output it modulo $$$10^9+7$$$. | 1,700 | true | false | false | true | false | false | false | false | false | false | 1,307 |
825D | You are given two strings _s_ and _t_ consisting of small Latin letters, string _s_ can also contain '?' characters. Suitability of string _s_ is calculated by following metric: Any two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting strings _s_, you choose the one with the largest number of non-intersecting occurrences of string _t_. Suitability is this number of occurrences. You should replace all '?' characters with small Latin letters in such a way that the suitability of string _s_ is maximal. Input The first line contains string _s_ (1u2009≤u2009_s_u2009≤u2009106). The second line contains string _t_ (1u2009≤u2009_t_u2009≤u2009106). Output Print string _s_ with '?' replaced with small Latin letters in such a way that suitability of that string is maximal. If there are multiple strings with maximal suitability then print any of them. Note In the first example string "baab" can be transformed to "abab" with swaps, this one has suitability of 2. That means that string "baab" also has suitability of 2. In the second example maximal suitability you can achieve is 1 and there are several dozens of such strings, "azbz" is just one of them. In the third example there are no '?' characters and the suitability of the string is 0. | 1,500 | false | true | true | false | false | false | false | true | false | false | 6,459 |
143B | For some time the program of rounding numbers that had been developed by the Codeforces participants during one of the previous rounds, helped the citizens of Far Far Away to convert numbers into a more easily readable format. However, as time went by, the economy of the Far Far Away developed and the scale of operations grew. So the King ordered to found the Bank of Far Far Away and very soon even the rounding didn't help to quickly determine even the order of the numbers involved in operations. Besides, rounding a number to an integer wasn't very convenient as a bank needed to operate with all numbers with accuracy of up to 0.01, and not up to an integer. The King issued yet another order: to introduce financial format to represent numbers denoting amounts of money. The formal rules of storing a number in the financial format are as follows: A number contains the integer part and the fractional part. The two parts are separated with a character "." (decimal point). To make digits in the integer part of a number easier to read, they are split into groups of three digits, starting from the least significant ones. The groups are separated with the character "," (comma). For example, if the integer part of a number equals 12345678, then it will be stored in the financial format as 12,345,678 In the financial format a number's fractional part should contain exactly two digits. So, if the initial number (the number that is converted into the financial format) contains less than two digits in the fractional part (or contains no digits at all), it is complemented with zeros until its length equals 2. If the fractional part contains more than two digits, the extra digits are simply discarded (they are not rounded: see sample tests). When a number is stored in the financial format, the minus sign is not written. Instead, if the initial number had the minus sign, the result is written in round brackets. Please keep in mind that the bank of Far Far Away operates using an exotic foreign currency — snakes ($), that's why right before the number in the financial format we should put the sign "$". If the number should be written in the brackets, then the snake sign should also be inside the brackets. For example, by the above given rules number 2012 will be stored in the financial format as "$2,012.00" and number -12345678.9 will be stored as "($12,345,678.90)". The merchants of Far Far Away visited you again and expressed much hope that you supply them with the program that can convert arbitrary numbers to the financial format. Can you help them? Input The input contains a number that needs to be converted into financial format. The number's notation length does not exceed 100 characters, including (possible) signs "-" (minus) and "." (decimal point). The number's notation is correct, that is: The number's notation only contains characters from the set {"0" – "9", "-", "."}. The decimal point (if it is present) is unique and is preceded and followed by a non-zero quantity on decimal digits A number cannot start with digit 0, except for a case when its whole integer part equals zero (in this case the integer parts is guaranteed to be a single zero: "0"). The minus sign (if it is present) is unique and stands in the very beginning of the number's notation If a number is identically equal to 0 (that is, if it is written as, for example, "0" or "0.000"), than it is not preceded by the minus sign. The input data contains no spaces. The number's notation contains at least one decimal digit. Output Print the number given in the input in the financial format by the rules described in the problem statement. Note Pay attention to the second and third sample tests. They show that the sign of a number in the financial format (and consequently, the presence or absence of brackets) is determined solely by the sign of the initial number. It does not depend on the sign of the number you got after translating the number to the financial format. | 1,200 | false | false | true | false | false | false | false | false | false | false | 9,301 |
1027F | Problem - 1027F - 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 dfs and similar dsu graph matchings graphs *2400 No tag edit access → Contest materials . Polycarp can pass at most one exam during each day. For each exam Polycarp chooses by himself which day he will pass this exam. He has to pass all the $$$n$$$ exams. Polycarp wants to pass all the exams as soon as possible. Print the minimum index of day by which Polycarp can pass all the $$$n$$$ exams, or print -1 if he cannot pass all the exams at all. Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 10^6$$$) — the number of exams. The next $$$n$$$ lines contain two integers each: $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i < b_i le 10^9$$$), where $$$a_i$$$ is the number of day of the first passing the $$$i$$$-th exam and $$$b_i$$$ is the number of day of the second passing the $$$i$$$-th exam. Output If Polycarp cannot pass all the $$$n$$$ exams, print -1. Otherwise print the minimum index of day by which Polycarp can do that. Examples Input 2 1 5 1 7 Output 5 Input 3 5 13 1 5 1 7 Output 7 Input 3 10 40 40 80 10 80 Output 80 Input 3 99 100 99 100 99 100 Output -1 | 2,400 | false | false | false | false | false | false | false | true | false | true | 5,571 |
2035F | This really says a lot about our society. One day, a turtle gives you a tree with $$$n$$$ nodes rooted at node $$$x$$$. Each node has an initial nonnegative value; the $$$i$$$-th node has starting value $$$a_i$$$. You want to make the values of all nodes equal to $$$0$$$. To do so, you will perform a series of operations on the tree, where each operation will be performed on a certain node. Define an operation on node $$$u$$$ as choosing a single node in $$$u$$$'s subtree$$$^{ ext{∗}}$$$ and incrementing or decrementing its value by $$$1$$$. The order in which operations are performed on nodes is as follows: For $$$1 le i le n$$$, the $$$i$$$-th operation will be performed on node $$$i$$$. For $$$i > n$$$, the $$$i$$$-th operation will be performed on the same node as operation $$$i - n$$$. More formally, the $$$i$$$-th operation will be performed on the $$$(((i - 1) bmod n) + 1)$$$-th node.$$$^{ ext{†}}$$$ Note that you cannot skip over operations; that is, you cannot perform the $$$i$$$-th operation without first performing operations $$$1, 2, ldots, i - 1$$$. Find the minimum number of operations you must perform before you can make the values of all nodes equal to $$$0$$$, assuming you pick operations optimally. If it's impossible to make the values of all nodes equal to $$$0$$$ after finite operations, output $$$-1$$$. Input The first line contains a single integer $$$t$$$ ($$$1le tle 100$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 le n le 2000$$$, $$$1 le x le n$$$)xa0— the number of nodes and the root of the tree. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^9$$$)xa0— the starting value of each node. Each of the next $$$n - 1$$$ lines of each test case contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$, $$$u eq v$$$) representing an undirected edge from $$$u$$$ to $$$v$$$. It is guaranteed that the given edges form a tree. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$. Output For each test case, output a single integer denoting the minimum amount of operations needed to make all nodes $$$0$$$. If it's impossible to make all nodes $$$0$$$, output $$$-1$$$. | 2,500 | false | false | false | true | false | false | true | true | false | false | 56 |
814D | The crowdedness of the discotheque would never stop our friends from having fun, but a bit more spaciousness won't hurt, will it? The discotheque can be seen as an infinite _xy_-plane, in which there are a total of _n_ dancers. Once someone starts moving around, they will move only inside their own movement range, which is a circular area _C__i_ described by a center (_x__i_,u2009_y__i_) and a radius _r__i_. No two ranges' borders have more than one common point, that is for every pair (_i_,u2009_j_) (1u2009≤u2009_i_u2009<u2009_j_u2009≤u2009_n_) either ranges _C__i_ and _C__j_ are disjoint, or one of them is a subset of the other. Note that it's possible that two ranges' borders share a single common point, but no two dancers have exactly the same ranges. Tsukihi, being one of them, defines the spaciousness to be the area covered by an odd number of movement ranges of dancers who are moving. An example is shown below, with shaded regions representing the spaciousness if everyone moves at the same time. But no one keeps moving for the whole night after all, so the whole night's time is divided into two halves — before midnight and after midnight. Every dancer moves around in one half, while sitting down with friends in the other. The spaciousness of two halves are calculated separately and their sum should, of course, be as large as possible. The following figure shows an optimal solution to the example above. By different plans of who dances in the first half and who does in the other, different sums of spaciousness over two halves are achieved. You are to find the largest achievable value of this sum. Input The first line of input contains a positive integer _n_ (1u2009≤u2009_n_u2009≤u20091u2009000) — the number of dancers. The following _n_ lines each describes a dancer: the _i_-th line among them contains three space-separated integers _x__i_, _y__i_ and _r__i_ (u2009-u2009106u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009106, 1u2009≤u2009_r__i_u2009≤u2009106), describing a circular movement range centered at (_x__i_,u2009_y__i_) with radius _r__i_. Output Output one decimal number — the largest achievable sum of spaciousness over two halves of the night. The output is considered correct if it has a relative or absolute error of at most 10u2009-u20099. Formally, let your answer be _a_, and the jury's answer be _b_. Your answer is considered correct if . | 2,000 | false | true | false | true | false | false | false | false | false | false | 6,502 |
545A | Little Susie, thanks to her older brother, likes to play with cars. Today she decided to set up a tournament between them. The process of a tournament is described in the next paragraph. There are _n_ toy cars. Each pair collides. The result of a collision can be one of the following: no car turned over, one car turned over, both cars turned over. A car is good if it turned over in no collision. The results of the collisions are determined by an _n_u2009×u2009_n_ matrix _А_: there is a number on the intersection of the _і_-th row and _j_-th column that describes the result of the collision of the _і_-th and the _j_-th car: u2009-u20091: if this pair of cars never collided. u2009-u20091 occurs only on the main diagonal of the matrix. 0: if no car turned over during the collision. 1: if only the _i_-th car turned over during the collision. 2: if only the _j_-th car turned over during the collision. 3: if both cars turned over during the collision. Susie wants to find all the good cars. She quickly determined which cars are good. Can you cope with the task? Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of cars. Each of the next _n_ lines contains _n_ space-separated integers that determine matrix _A_. It is guaranteed that on the main diagonal there are u2009-u20091, and u2009-u20091 doesn't appear anywhere else in the matrix. It is guaranteed that the input is correct, that is, if _A__ij_u2009=u20091, then _A__ji_u2009=u20092, if _A__ij_u2009=u20093, then _A__ji_u2009=u20093, and if _A__ij_u2009=u20090, then _A__ji_u2009=u20090. Output Print the number of good cars and in the next line print their space-separated indices in the increasing order. Examples Input 4 -1 3 3 3 3 -1 3 3 3 3 -1 3 3 3 3 -1 | 900 | false | false | true | false | false | false | false | false | false | false | 7,662 |
2018A | You have some cards. An integer between $$$1$$$ and $$$n$$$ is written on each card: specifically, for each $$$i$$$ from $$$1$$$ to $$$n$$$, you have $$$a_i$$$ cards which have the number $$$i$$$ written on them. There is also a shop which contains unlimited cards of each type. You have $$$k$$$ coins, so you can buy at most $$$k$$$ new cards in total, and the cards you buy can contain any integer between $$$mathbf{1}$$$ and $$$mathbf{n}$$$, inclusive. After buying the new cards, you must partition all your cards into decks, according to the following rules: all the decks must have the same size; there are no pairs of cards with the same value in the same deck. Find the maximum possible size of a deck after buying cards and partitioning them optimally. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 leq n leq 2 cdot 10^5$$$, $$$0 leq k leq 10^{16}$$$)xa0— the number of distinct types of cards and the number of coins. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i leq 10^{10}$$$, $$$sum a_i geq 1$$$)xa0— the number of cards of type $$$i$$$ you have at the beginning, for each $$$1 leq i leq n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Note In the first test case, you can buy one card with the number $$$1$$$, and your cards become $$$[1, 1, 1, 1, 2, 2, 3, 3]$$$. You can partition them into the decks $$$[1, 2], [1, 2], [1, 3], [1, 3]$$$: they all have size $$$2$$$, and they all contain distinct values. You can show that you cannot get a partition with decks of size greater than $$$2$$$, so the answer is $$$2$$$. In the second test case, you can buy two cards with the number $$$1$$$ and one card with the number $$$3$$$, and your cards become $$$[1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 4, 4, 5, 5, 5, 5]$$$, which can be partitioned into $$$[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 2, 5], [2, 3, 5], [2, 4, 5]$$$. You can show that you cannot get a partition with decks of size greater than $$$3$$$, so the answer is $$$3$$$. | 1,600 | true | true | true | false | false | false | true | false | false | false | 163 |
771C | A tree is an undirected connected graph without cycles. The distance between two vertices is the number of edges in a simple path between them. Limak is a little polar bear. He lives in a tree that consists of _n_ vertices, numbered 1 through _n_. Limak recently learned how to jump. He can jump from a vertex to any vertex within distance at most _k_. For a pair of vertices (_s_,u2009_t_) we define _f_(_s_,u2009_t_) as the minimum number of jumps Limak needs to get from _s_ to _t_. Your task is to find the sum of _f_(_s_,u2009_t_) over all pairs of vertices (_s_,u2009_t_) such that _s_u2009<u2009_t_. Input The first line of the input contains two integers _n_ and _k_ (2u2009≤u2009_n_u2009≤u2009200u2009000, 1u2009≤u2009_k_u2009≤u20095)xa0— the number of vertices in the tree and the maximum allowed jump distance respectively. The next _n_u2009-u20091 lines describe edges in the tree. The _i_-th of those lines contains two integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_)xa0— the indices on vertices connected with _i_-th edge. It's guaranteed that the given edges form a tree. Output Print one integer, denoting the sum of _f_(_s_,u2009_t_) over all pairs of vertices (_s_,u2009_t_) such that _s_u2009<u2009_t_. Examples Input 13 3 1 2 3 2 4 2 5 2 3 6 10 6 6 7 6 13 5 8 5 9 9 11 11 12 Note In the first sample, the given tree has 6 vertices and it's displayed on the drawing below. Limak can jump to any vertex within distance at most 2. For example, from the vertex 5 he can jump to any of vertices: 1, 2 and 4 (well, he can also jump to the vertex 5 itself). There are pairs of vertices (_s_,u2009_t_) such that _s_u2009<u2009_t_. For 5 of those pairs Limak would need two jumps: (1,u20096),u2009(3,u20094),u2009(3,u20095),u2009(3,u20096),u2009(5,u20096). For other 10 pairs one jump is enough. So, the answer is 5·2u2009+u200910·1u2009=u200920. In the third sample, Limak can jump between every two vertices directly. There are 3 pairs of vertices (_s_u2009<u2009_t_), so the answer is 3·1u2009=u20093. | 2,100 | false | false | false | true | false | false | false | false | false | false | 6,696 |
1768F | You are given an array of positive integers $$$a_1,a_2,ldots,a_n$$$ of length $$$n$$$. In one operation you can jump from index $$$i$$$ to index $$$j$$$ ($$$1 le i le j le n$$$) by paying $$$min(a_i, a_{i + 1}, ldots, a_j) cdot (j - i)^2$$$ eris. For all $$$k$$$ from $$$1$$$ to $$$n$$$, find the minimum number of eris needed to get from index $$$1$$$ to index $$$k$$$. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 4 cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1,a_2,ldots a_n$$$ ($$$1 le a_i le n$$$). Output Output $$$n$$$ integersxa0— the $$$k$$$-th integer is the minimum number of eris needed to reach index $$$k$$$ if you start from index $$$1$$$. Note In the first example: From $$$1$$$ to $$$1$$$: the cost is $$$0$$$, From $$$1$$$ to $$$2$$$: $$$1 ightarrow 2$$$ — the cost is $$$min(2, 1) cdot (2 - 1) ^ 2=1$$$, From $$$1$$$ to $$$3$$$: $$$1 ightarrow 2 ightarrow 3$$$ — the cost is $$$min(2, 1) cdot (2 - 1) ^ 2 + min(1, 3) cdot (3 - 2) ^ 2 = 1 + 1 = 2$$$. In the fourth example from $$$1$$$ to $$$4$$$: $$$1 ightarrow 3 ightarrow 4$$$ — the cost is $$$min(1, 4, 4) cdot (3 - 1) ^ 2 + min(4, 4) cdot (4 - 3) ^ 2 = 4 + 4 = 8$$$. | 2,900 | false | true | false | true | false | false | false | false | false | false | 1,686 |
1768D | You are given a permutation$$$^dagger$$$ $$$p$$$ of length $$$n$$$. In one operation, you can choose two indices $$$1 le i < j le n$$$ and swap $$$p_i$$$ with $$$p_j$$$. Find the minimum number of operations needed to have exactly one inversion$$$^ddagger$$$ in the permutation. $$$^dagger$$$ A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). $$$^ddagger$$$ The number of inversions of a permutation $$$p$$$ is the number of pairs of indices $$$(i, j)$$$ such that $$$1 le i < j le n$$$ and $$$p_i > p_j$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,ldots, p_n$$$ ($$$1 le p_i le n$$$). It is guaranteed that $$$p$$$ is a permutation. 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 a single integerxa0— the minimum number of operations needed to have exactly one inversion in the permutation. It can be proven that an answer always exists. Example Input 4 2 2 1 2 1 2 4 3 4 1 2 4 2 4 3 1 Note In the first test case, the permutation already satisfies the condition. In the second test case, you can perform the operation with $$$(i,j)=(1,2)$$$, after that the permutation will be $$$[2,1]$$$ which has exactly one inversion. In the third test case, it is not possible to satisfy the condition with less than $$$3$$$ operations. However, if we perform $$$3$$$ operations with $$$(i,j)$$$ being $$$(1,3)$$$,$$$(2,4)$$$, and $$$(3,4)$$$ in that order, the final permutation will be $$$[1, 2, 4, 3]$$$ which has exactly one inversion. In the fourth test case, you can perform the operation with $$$(i,j)=(2,4)$$$, after that the permutation will be $$$[2,1,3,4]$$$ which has exactly one inversion. | 1,800 | false | true | false | false | false | true | false | false | false | true | 1,688 |
895D | At the Byteland State University marks are strings of the same length. Mark _x_ is considered better than _y_ if string _y_ is lexicographically smaller than _x_. Recently at the BSU was an important test work on which Vasya recived the mark _a_. It is very hard for the teacher to remember the exact mark of every student, but he knows the mark _b_, such that every student recieved mark strictly smaller than _b_. Vasya isn't satisfied with his mark so he decided to improve it. He can swap characters in the string corresponding to his mark as many times as he like. Now he want to know only the number of different ways to improve his mark so that his teacher didn't notice something suspicious. More formally: you are given two strings _a_, _b_ of the same length and you need to figure out the number of different strings _c_ such that: 1) _c_ can be obtained from _a_ by swapping some characters, in other words _c_ is a permutation of _a_. 2) String _a_ is lexicographically smaller than _c_. 3) String _c_ is lexicographically smaller than _b_. For two strings _x_ and _y_ of the same length it is true that _x_ is lexicographically smaller than _y_ if there exists such _i_, that _x_1u2009=u2009_y_1,u2009_x_2u2009=u2009_y_2,u2009...,u2009_x__i_u2009-u20091u2009=u2009_y__i_u2009-u20091,u2009_x__i_u2009<u2009_y__i_. Since the answer can be very large, you need to find answer modulo 109u2009+u20097. Input First line contains string _a_, second line contains string _b_. Strings _a_,u2009_b_ consist of lowercase English letters. Their lengths are equal and don't exceed 106. It is guaranteed that _a_ is lexicographically smaller than _b_. Note In first sample from string _abc_ can be obtained strings _acb_,u2009_bac_,u2009_bca_,u2009_cab_,u2009_cba_, all of them are larger than _abc_, but smaller than _ddd_. So the answer is 5. In second sample any string obtained from _abcdef_ is larger than _abcdeg_. So the answer is 0. | 2,100 | true | false | false | false | false | false | false | false | false | false | 6,167 |
453E | Lord Tirek is a centaur and the main antagonist in the season four finale episodes in the series "My Little Pony: Friendship Is Magic". In "Twilight's Kingdom" (Part 1), Tirek escapes from Tartarus and drains magic from ponies to grow stronger. The core skill of Tirek is called Absorb Mana. It takes all mana from a magic creature and gives them to the caster. Now to simplify the problem, assume you have _n_ ponies (numbered from 1 to _n_). Each pony has three attributes: _s__i_ : amount of mana that the pony has at time 0; _m__i_ : maximum mana that the pony can have; _r__i_ : mana regeneration per unit time. Lord Tirek will do _m_ instructions, each of them can be described with three integers: _t__i_,u2009_l__i_,u2009_r__i_. The instruction means that at time _t__i_, Tirek will use Absorb Mana on ponies with numbers from _l__i_ to _r__i_ (both borders inclusive). We'll give you all the _m_ instructions in order, count how much mana Tirek absorbs for each instruction. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of ponies. Each of the next _n_ lines contains three integers _s__i_,u2009_m__i_,u2009_r__i_ (0u2009≤u2009_s__i_u2009≤u2009_m__i_u2009≤u2009105;xa00u2009≤u2009_r__i_u2009≤u2009105), describing a pony. The next line contains an integer _m_ (1u2009≤u2009_m_u2009≤u2009105) — the number of instructions. Each of the next _m_ lines contains three integers _t__i_,u2009_l__i_,u2009_r__i_ (0u2009≤u2009_t__i_u2009≤u2009109;xa01u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_), describing an instruction of Lord Tirek. The instructions are given in strictly increasing order of _t__i_ (all _t__i_ are distinct). Output For each instruction, output a single line which contains a single integer, the total mana absorbed in this instruction. Examples Input 5 0 10 1 0 12 1 0 20 1 0 12 1 0 10 1 2 5 1 5 19 1 5 Note Every pony starts with zero mana. For the first instruction, each pony has 5 mana, so you get 25 mana in total and each pony has 0 mana after the first instruction. For the second instruction, pony 3 has 14 mana and other ponies have mana equal to their _m__i_. | 3,100 | false | false | false | false | true | false | false | false | false | false | 8,022 |
746C | The tram in Berland goes along a straight line from the point 0 to the point _s_ and back, passing 1 meter per _t_1 seconds in both directions. It means that the tram is always in the state of uniform rectilinear motion, instantly turning around at points _x_u2009=u20090 and _x_u2009=u2009_s_. Igor is at the point _x_1. He should reach the point _x_2. Igor passes 1 meter per _t_2 seconds. Your task is to determine the minimum time Igor needs to get from the point _x_1 to the point _x_2, if it is known where the tram is and in what direction it goes at the moment Igor comes to the point _x_1. Igor can enter the tram unlimited number of times at any moment when his and the tram's positions coincide. It is not obligatory that points in which Igor enter and exit the tram are integers. Assume that any boarding and unboarding happens instantly. Igor can move arbitrary along the line (but not faster than 1 meter per _t_2 seconds). He can also stand at some point for some time. Input The first line contains three integers _s_, _x_1 and _x_2 (2u2009≤u2009_s_u2009≤u20091000, 0u2009≤u2009_x_1,u2009_x_2u2009≤u2009_s_, _x_1u2009≠u2009_x_2)xa0— the maximum coordinate of the point to which the tram goes, the point Igor is at, and the point he should come to. The second line contains two integers _t_1 and _t_2 (1u2009≤u2009_t_1,u2009_t_2u2009≤u20091000)xa0— the time in seconds in which the tram passes 1 meter and the time in seconds in which Igor passes 1 meter. The third line contains two integers _p_ and _d_ (1u2009≤u2009_p_u2009≤u2009_s_u2009-u20091, _d_ is either 1 or )xa0— the position of the tram in the moment Igor came to the point _x_1 and the direction of the tram at this moment. If , the tram goes in the direction from the point _s_ to the point 0. If _d_u2009=u20091, the tram goes in the direction from the point 0 to the point _s_. Output Print the minimum time in seconds which Igor needs to get from the point _x_1 to the point _x_2. Note In the first example it is profitable for Igor to go by foot and not to wait the tram. Thus, he has to pass 2 meters and it takes 8 seconds in total, because he passes 1 meter per 4 seconds. In the second example Igor can, for example, go towards the point _x_2 and get to the point 1 in 6 seconds (because he has to pass 3 meters, but he passes 1 meters per 2 seconds). At that moment the tram will be at the point 1, so Igor can enter the tram and pass 1 meter in 1 second. Thus, Igor will reach the point _x_2 in 7 seconds in total. | 1,600 | true | false | true | false | false | true | false | false | false | false | 6,815 |
1316B | Vasya has a string $$$s$$$ of length $$$n$$$. He decides to make the following modification to the string: 1. Pick an integer $$$k$$$, ($$$1 leq k leq n$$$). 2. For $$$i$$$ from $$$1$$$ to $$$n-k+1$$$, reverse the substring $$$s[i:i+k-1]$$$ of $$$s$$$. For example, if string $$$s$$$ is qwer and $$$k = 2$$$, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length $$$2$$$) weqr (after reversing the second substring of length $$$2$$$) werq (after reversing the last substring of length $$$2$$$) Hence, the resulting string after modifying $$$s$$$ with $$$k = 2$$$ is werq. Vasya wants to choose a $$$k$$$ such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of $$$k$$$. Among all such $$$k$$$, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help. 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 Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 5000$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 5000$$$)xa0— the length of the string $$$s$$$. The second line of each test case contains the string $$$s$$$ of $$$n$$$ lowercase latin letters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$. Output For each testcase output two lines: In the first line output the lexicographically smallest string $$$s'$$$ achievable after the above-mentioned modification. In the second line output the appropriate value of $$$k$$$ ($$$1 leq k leq n$$$) that you chose for performing the modification. If there are multiple values of $$$k$$$ that give the lexicographically smallest string, output the smallest value of $$$k$$$ among them. Example Input 6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p Output abab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 Note In the first testcase of the first sample, the string modification results for the sample abab are as follows : for $$$k = 1$$$ : abab for $$$k = 2$$$ : baba for $$$k = 3$$$ : abab for $$$k = 4$$$ : babaThe lexicographically smallest string achievable through modification is abab for $$$k = 1$$$ and $$$3$$$. Smallest value of $$$k$$$ needed to achieve is hence $$$1$$$. | 1,400 | false | false | true | false | false | true | true | false | true | false | 4,120 |
161E | Polycarpus has _t_ safes. The password for each safe is a square matrix consisting of decimal digits '0' ... '9' (the sizes of passwords to the safes may vary). Alas, Polycarpus has forgotten all passwords, so now he has to restore them. Polycarpus enjoys prime numbers, so when he chose the matrix passwords, he wrote a prime number in each row of each matrix. To his surprise, he found that all the matrices turned out to be symmetrical (that is, they remain the same after transposition). Now, years later, Polycarp was irritated to find out that he remembers only the prime numbers _p__i_, written in the first lines of the password matrices. For each safe find the number of matrices which can be passwords to it. The number of digits in _p__i_ determines the number of rows and columns of the _i_-th matrix. One prime number can occur in several rows of the password matrix or in several matrices. The prime numbers that are written not in the first row of the matrix may have leading zeros. Input The first line of the input contains an integer _t_ (1u2009≤u2009_t_u2009≤u200930) — the number of safes. Next _t_ lines contain integers _p__i_ (10u2009≤u2009_p__i_u2009≤u200999999), _p__i_ is a prime number written in the first row of the password matrix for the _i_-th safe. All _p__i_'s are written without leading zeros. Output Print _t_ numbers, the _i_-th of them should be the number of matrices that can be a password to the _i_-th safe. Print the numbers on separate lines. Note Here is a possible password matrix for the second safe: 239 307 977 Here is a possible password matrix for the fourth safe: 9001 0002 0002 1223 | 2,500 | false | false | false | true | false | false | true | false | false | false | 9,226 |
767C | Once at New Year Dima had a dream in which he was presented a fairy garland. A garland is a set of lamps, some pairs of which are connected by wires. Dima remembered that each two lamps in the garland were connected directly or indirectly via some wires. Furthermore, the number of wires was exactly one less than the number of lamps. There was something unusual about the garland. Each lamp had its own brightness which depended on the temperature of the lamp. Temperatures could be positive, negative or zero. Dima has two friends, so he decided to share the garland with them. He wants to cut two different wires so that the garland breaks up into three parts. Each part of the garland should shine equally, i.xa0e. the sums of lamps' temperatures should be equal in each of the parts. Of course, each of the parts should be non-empty, i.xa0e. each part should contain at least one lamp. Help Dima to find a suitable way to cut the garland, or determine that this is impossible. While examining the garland, Dima lifted it up holding by one of the lamps. Thus, each of the lamps, except the one he is holding by, is now hanging on some wire. So, you should print two lamp ids as the answer which denote that Dima should cut the wires these lamps are hanging on. Of course, the lamp Dima is holding the garland by can't be included in the answer. Input The first line contains single integer _n_ (3u2009≤u2009_n_u2009≤u2009106)xa0— the number of lamps in the garland. Then _n_ lines follow. The _i_-th of them contain the information about the _i_-th lamp: the number lamp _a__i_, it is hanging on (and 0, if is there is no such lamp), and its temperature _t__i_ (u2009-u2009100u2009≤u2009_t__i_u2009≤u2009100). The lamps are numbered from 1 to _n_. Output If there is no solution, print -1. Otherwise print two integersxa0— the indexes of the lamps which mean Dima should cut the wires they are hanging on. If there are multiple answers, print any of them. | 2,000 | false | true | false | false | false | false | false | false | false | true | 6,716 |
1528E | Today is Mashtali's birthday! He received a Hagh tree from Haj Davood as his birthday present! A directed tree is called a Hagh tree iff: The length of the longest directed path in it is exactly $$$n$$$. Every vertex has at most three edges attached to it independent of their orientation. Let's call vertices $$$u$$$ and $$$v$$$ friends if one of them has a directed path to the other. For every pair of vertices $$$u$$$ and $$$v$$$ that are not friends, there should exist a vertex $$$w$$$ that is friends with both $$$u$$$ and $$$v$$$ (a mutual friend). After opening his gift, Mashtali found out that the labels on the vertices were gone. Immediately, he asked himself: how many different unlabeled Hagh trees are there? That is, how many possible trees could he have received as his birthday present? At the first glance, the number of such trees seemed to be infinite since there was no limit on the number of vertices; but then he solved the problem and proved that there's a finite number of unlabeled Hagh trees! Amazed by this fact, he shared the task with you so that you could enjoy solving it as well. Since the answer can be rather large he asked you to find the number of different unlabeled Hagh trees modulo $$$998244353$$$. Here two trees are considered different, if they are not isomorphic: if there is no way to map nodes of one tree to the second tree, so that edges are mapped to edges preserving the orientation. Some examples for $$$n = 2$$$: Directed trees $$$D$$$ and $$$E$$$ are Hagh. $$$C$$$ is not Hagh because it has a vertex with $$$4$$$ edges attached to it. $$$A$$$ and $$$B$$$ are not Hagh because their longest directed paths are not equal to $$$n$$$. Also in $$$B$$$ the leftmost and rightmost vertices are not friends neither do they have a mutual friend. | 2,900 | false | false | false | true | false | false | false | false | false | false | 3,019 |
1973D | This is an interactive problem. Fox gave Cat two positive integers $$$n$$$ and $$$k$$$. She has a hidden array $$$a_1, ldots , a_n$$$ of length $$$n$$$, such that $$$1 leq a_i leq n$$$ for every $$$i$$$. Now they are going to play the following game: For any two integers $$$l, r$$$ such that $$$1 leq l leq r leq n$$$, define $$$f(l, r) = (r - l + 1) cdot maxlimits_{x=l}^r a_x$$$. In other words, $$$f(l, r)$$$ is equal to the maximum of the subarray $$$a_l, ldots, a_r$$$ multiplied by its size. Cat can ask Fox at most $$$2 n$$$ questions about the array. He will tell her two integers $$$l$$$ and $$$x$$$ ($$$1 leq l leq n, 1 leq x leq 10^9$$$), and she will tell him one integer $$$p$$$ as the answer — the smallest positive integer $$$r$$$ such that $$$f(l, r) = x$$$, or $$$n+1$$$ if no such $$$r$$$ exists. Now, Cat needs to find the largest value $$$m$$$ such that there exists a sequence $$$c_1, ldots, c_{k-1}$$$ such that $$$1 leq c_1 < ldots < c_{k-1} < n$$$ and $$$f(1, c_1) = f(c_1 + 1, c_2) = ldots = f(c_{k-1}+1, n) = m$$$. If no such $$$m$$$ exists, he should indicate this and take $$$-1$$$ as the answer. Note that for $$$k = 1$$$, $$$m$$$ is always equal to $$$f(1, n)$$$. In other words, the goal is to find the largest $$$m$$$ such that you can split the array into exactly $$$k$$$ subarrays ($$$k$$$ is the constant given to you in the beginning of the interaction) so that all the subarrays have the product of their length and their maximum equal to $$$m$$$, or determine that no such $$$m$$$ exists. Every element should belong in exactly one of the subarrays. Cat doesn't know what he should do, so he asked you to play the game for him. Interaction Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^3$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two positive integers $$$n$$$ and $$$k$$$ ($$$1 leq k leq n leq 10^4$$$) — the length of the hidden array and the number of subarrays in the desired split. Now you are allowed to make queries in the following way — print one line of the form "$$$mathtt{?} l x$$$" (it must hold that $$$1 leq l leq n$$$, $$$1 leq x leq 10^9$$$) and you will receive the smallest integer $$$r$$$ such that $$$l leq r leq n$$$ and $$$f(l, r) = x$$$, or $$$n + 1$$$ if no such $$$r$$$ exists. If you want to print the answer, output "$$$mathtt{!} m$$$" and you will recieve $$$1$$$ if your answer is correct and $$$-1$$$ otherwise. In the first case, the interaction continues with the next test case. Note that printing the answer doesn't count towards the number of queries made. Please note that you don't receive the values for the next test case immediately, you will first have to read whether your answer to the last test case was correct. If you receive the integer $$$-1$$$ at any moment, it means your program has made an invalid query, exceeded the query limit, or gave an incorrect answer. Your program must terminate immediately to receive a Wrong Answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream. After printing a query, do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. It is guaranteed that the total sum of $$$n$$$ over the test cases won't exceed $$$10^4$$$. Hacks The format of the hacks should be the following: the first line should contain one integer $$$t$$$ ($$$1 leq t leq 10^3$$$) — the number of test cases. The description of the test cases should follow. The first line of each test case should contain two integers $$$n$$$ and $$$k$$$ ($$$1 leq k leq n leq 10^4$$$) — the length of the array $$$a$$$ and the number of subarrays you want to split it into. The second line should contain $$$n$$$ integers $$$a_1, a_2, ldots, a_n $$$ ($$$1 leq a_i leq n $$$). The sum of $$$n$$$ over all test cases should not exceed $$$10^4$$$. Example Input 3 1 1 1 2 2 1 3 1 6 3 7 2 3 6 1 Output ! 1 ? 1 1 ? 2 1 ! -1 ? 1 9 ? 1 6 ? 3 6 ? 4 6 ! 6 Note The hidden arrays in the three testcases are $$$[1]$$$, $$$[1, 2]$$$ and $$$[1, 3, 6, 1, 2, 1]$$$. In the second testcase, no split satisfies the constraints, so the answer is $$$-1$$$. The answer for the first query of the third testcase is $$$7$$$ since no valid $$$r$$$ exists. For the second query of the third testcase, since $$$2 cdot max(1, 3) = 6$$$, we will get $$$2$$$ as the answer, since $$$r = 1$$$ doesn't satisfy the constraint. The sample interaction guessed all three answers ($$$1, -1$$$ and $$$6$$$) correctly, so it received $$$1$$$ after each answer. | 2,400 | true | false | false | false | false | false | true | false | false | false | 452 |
639E | Limak is a big polar bear. He prepared _n_ problems for an algorithmic contest. The _i_-th problem has initial score _p__i_. Also, testers said that it takes _t__i_ minutes to solve the _i_-th problem. Problems aren't necessarily sorted by difficulty and maybe harder problems have smaller initial score but it's too late to change itxa0— Limak has already announced initial scores for problems. Though it's still possible to adjust the speed of losing points, denoted by _c_ in this statement. Let _T_ denote the total number of minutes needed to solve all problems (so, _T_u2009=u2009_t_1u2009+u2009_t_2u2009+u2009...u2009+u2009_t__n_). The contest will last exactly _T_ minutes. So it's just enough to solve all problems. Points given for solving a problem decrease linearly. Solving the _i_-th problem after _x_ minutes gives exactly points, where is some real constant that Limak must choose. Let's assume that _c_ is fixed. During a contest a participant chooses some order in which he or she solves problems. There are _n_! possible orders and each of them gives some total number of points, not necessarily integer. We say that an order is optimal if it gives the maximum number of points. In other words, the total number of points given by this order is greater or equal than the number of points given by any other order. It's obvious that there is at least one optimal order. However, there may be more than one optimal order. Limak assumes that every participant will properly estimate _t__i_ at the very beginning and will choose some optimal order. He also assumes that testers correctly predicted time needed to solve each problem. For two distinct problems _i_ and _j_ such that _p__i_u2009<u2009_p__j_ Limak wouldn't be happy to see a participant with strictly more points for problem _i_ than for problem _j_. He calls such a situation a paradox. It's not hard to prove that there will be no paradox for _c_u2009=u20090. The situation may be worse for bigger _c_. What is the maximum real value _c_ (remember that ) for which there is no paradox possible, that is, there will be no paradox for any optimal order of solving problems? It can be proved that the answer (the maximum _c_ as described) always exists. Input The first line contains one integer _n_ (2u2009≤u2009_n_u2009≤u2009150u2009000)xa0— the number of problems. The second line contains _n_ integers _p_1,u2009_p_2,u2009...,u2009_p__n_ (1u2009≤u2009_p__i_u2009≤u2009108)xa0— initial scores. The third line contains _n_ integers _t_1,u2009_t_2,u2009...,u2009_t__n_ (1u2009≤u2009_t__i_u2009≤u2009108) where _t__i_ is the number of minutes needed to solve the _i_-th problem. Output Print one real value on a single linexa0— the maximum value of _c_ that and there is no optimal order with a paradox. Your answer will be considered correct if its absolute or relative error does not exceed 10u2009-u20096. Namely: let's assume that your answer is _a_, and the answer of the jury is _b_. The checker program will consider your answer correct if . Examples Input 4 7 20 15 10 7 20 15 10 Note In the first sample, there are 3 problems. The first is (4,u20091) (initial score is 4 and required time is 1 minute), the second problem is (3,u20091) and the third one is (10,u20098). The total time is _T_u2009=u20091u2009+u20091u2009+u20098u2009=u200910. Let's show that there is a paradox for _c_u2009=u20090.7. Solving problems in order 1, 2, 3 turns out to give the best total score, equal to the sum of: 1. solved 1 minute after the start: 2. solved 2 minutes after the start: 3. solved 10 minutes after the start: So, this order gives 3.72u2009+u20092.58u2009+u20093u2009=u20099.3 points in total and this is the only optimal order (you can calculate total scores for other 5 possible orders too see that they are lower). You should check points for problems 1 and 3 to see a paradox. There is 4u2009<u200910 but 3.72u2009>u20093. It turns out that there is no paradox for _c_u2009=u20090.625 but there is a paradox for any bigger _c_. In the second sample, all 24 orders are optimal. In the third sample, even for _c_u2009=u20091 there is no paradox. | 2,800 | true | true | false | false | false | false | false | true | true | false | 7,253 |
1311C | You want to perform the combo on your opponent in one popular fighting game. The combo is the string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. To perform the combo, you have to press all buttons in the order they appear in $$$s$$$. I.e. if $$$s=$$$"abca" then you have to press 'a', then 'b', 'c' and 'a' again. You know that you will spend $$$m$$$ wrong tries to perform the combo and during the $$$i$$$-th try you will make a mistake right after $$$p_i$$$-th button ($$$1 le p_i < n$$$) (i.e. you will press first $$$p_i$$$ buttons right and start performing the combo from the beginning). It is guaranteed that during the $$$m+1$$$-th try you press all buttons right and finally perform the combo. I.e. if $$$s=$$$"abca", $$$m=2$$$ and $$$p = [1, 3]$$$ then the sequence of pressed buttons will be 'a' (here you're making a mistake and start performing the combo from the beginning), 'a', 'b', 'c', (here you're making a mistake and start performing the combo from the beginning), 'a' (note that at this point you will not perform the combo because of the mistake), 'b', 'c', 'a'. Your task is to calculate for each button (letter) the number of times you'll press it. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 2 cdot 10^5$$$, $$$1 le m le 2 cdot 10^5$$$) — the length of $$$s$$$ and the number of tries correspondingly. The second line of each test case contains the string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. The third line of each test case contains $$$m$$$ integers $$$p_1, p_2, dots, p_m$$$ ($$$1 le p_i < n$$$) — the number of characters pressed right during the $$$i$$$-th try. It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ both does not exceed $$$2 cdot 10^5$$$ ($$$sum n le 2 cdot 10^5$$$, $$$sum m le 2 cdot 10^5$$$). It is guaranteed that the answer for each letter does not exceed $$$2 cdot 10^9$$$. Output For each test case, print the answer — $$$26$$$ integers: the number of times you press the button 'a', the number of times you press the button 'b', $$$dots$$$, the number of times you press the button 'z'. Example Input 3 4 2 abca 1 3 10 5 codeforces 2 8 3 2 9 26 10 qwertyuioplkjhgfdsazxcvbnm 20 10 1 2 3 5 10 5 9 4 Output 4 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 4 5 3 0 0 0 0 0 0 0 0 9 0 0 3 1 0 0 0 0 0 0 0 2 1 1 2 9 2 2 2 5 2 2 2 1 1 5 4 11 8 2 7 5 1 10 1 5 2 Note The first test case is described in the problem statement. Wrong tries are "a", "abc" and the final try is "abca". The number of times you press 'a' is $$$4$$$, 'b' is $$$2$$$ and 'c' is $$$2$$$. In the second test case, there are five wrong tries: "co", "codeforc", "cod", "co", "codeforce" and the final try is "codeforces". The number of times you press 'c' is $$$9$$$, 'd' is $$$4$$$, 'e' is $$$5$$$, 'f' is $$$3$$$, 'o' is $$$9$$$, 'r' is $$$3$$$ and 's' is $$$1$$$. | 1,300 | false | false | false | false | false | false | true | false | false | false | 4,141 |
1768E | Consider a permutation$$$^dagger$$$ $$$p$$$ of length $$$3n$$$. Each time you can do one of the following operations: Sort the first $$$2n$$$ elements in increasing order. Sort the last $$$2n$$$ elements in increasing order. We can show that every permutation can be made sorted in increasing order using only these operations. Let's call $$$f(p)$$$ the minimum number of these operations needed to make the permutation $$$p$$$ sorted in increasing order. Given $$$n$$$, find the sum of $$$f(p)$$$ over all $$$(3n)!$$$ permutations $$$p$$$ of size $$$3n$$$. Since the answer could be very large, output it modulo a prime $$$M$$$. $$$^dagger$$$ A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). Note In the first test case, all the permutations are: $$$[1, 2, 3]$$$, which requires $$$0$$$ operations; $$$[1, 3, 2]$$$, which requires $$$1$$$ operation; $$$[2, 1, 3]$$$, which requires $$$1$$$ operation; $$$[2, 3, 1]$$$, which requires $$$2$$$ operations; $$$[3, 1, 2]$$$, which requires $$$2$$$ operations; $$$[3, 2, 1]$$$, which requires $$$3$$$ operations. Therefore, the answer is $$$0+1+1+2+2+3=9$$$. | 2,300 | true | false | false | false | false | false | false | false | false | false | 1,687 |
1539B | Petya once wrote a sad love song and shared it to Vasya. The song is a string consisting of lowercase English letters. Vasya made up $$$q$$$ questions about this song. Each question is about a subsegment of the song starting from the $$$l$$$-th letter to the $$$r$$$-th letter. Vasya considers a substring made up from characters on this segment and repeats each letter in the subsegment $$$k$$$ times, where $$$k$$$ is the index of the corresponding letter in the alphabet. For example, if the question is about the substring "abbcb", then Vasya repeats letter 'a' once, each of the letters 'b' twice, letter 'c" three times, so that the resulting string is "abbbbcccbb", its length is $$$10$$$. Vasya is interested about the length of the resulting string. Help Petya find the length of each string obtained by Vasya. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1leq nleq 100,000$$$, $$$1leq q leq 100,000$$$)xa0— the length of the song and the number of questions. The second line contains one string $$$s$$$xa0— the song, consisting of $$$n$$$ lowercase letters of English letters. Vasya's questions are contained in the next $$$q$$$ lines. Each line contains two integers $$$l$$$ and $$$r$$$ ($$$1 leq l leq r leq n$$$)xa0— the bounds of the question. Output Print $$$q$$$ lines: for each question print the length of the string obtained by Vasya. Examples Input 7 3 abacaba 1 3 2 5 1 7 Input 7 4 abbabaa 1 3 5 7 6 6 2 4 Input 13 7 sonoshikumiwo 1 5 2 10 7 7 1 13 4 8 2 5 3 9 Output 82 125 9 191 62 63 97 Note In the first example Vasya is interested in three questions. In the first question Vasya considers the substring "aba", that transforms to "abba", so the answer is equal to $$$4$$$. In the second question Vasya considers "baca", that transforms to "bbaccca", so the answer is $$$7$$$. In the third question Vasya considers the string "abacaba",that transforms to "abbacccabba" of length $$$11$$$. | 800 | false | false | true | true | false | false | false | false | false | false | 2,947 |
243D | One day Petya got a set of wooden cubes as a present from his mom. Petya immediately built a whole city from these cubes. The base of the city is an _n_u2009×u2009_n_ square, divided into unit squares. The square's sides are parallel to the coordinate axes, the square's opposite corners have coordinates (0,u20090) and (_n_,u2009_n_). On each of the unit squares Petya built a tower of wooden cubes. The side of a wooden cube also has a unit length. After that Petya went an infinitely large distance away from his masterpiece and looked at it in the direction of vector _v_u2009=u2009(_v__x_,u2009_v__y_,u20090). Petya wonders, how many distinct cubes are visible from this position. Help him, find this number. Each cube includes the border. We think that a cube is visible if there is a ray emanating from some point _p_, belonging to the cube, in the direction of vector u2009-u2009_v_, that doesn't contain any points, belonging to other cubes. Input The first line contains three integers _n_, _v__x_ and _v__y_ (1u2009≤u2009_n_u2009≤u2009103, _v__x_,u2009_v__y_u2009≤u2009104, _v__x_u2009+u2009_v__y_u2009>u20090). Next _n_ lines contain _n_ integers each: the _j_-th integer in the _i_-th line _a__ij_ (0u2009≤u2009_a__ij_u2009≤u2009109, 1u2009≤u2009_i_,u2009_j_u2009≤u2009_n_) represents the height of the cube tower that stands on the unit square with opposite corners at points (_i_u2009-u20091,u2009_j_u2009-u20091) and (_i_,u2009_j_). Output Print a single integer — the number of visible cubes. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 5 -1 2 5 0 0 0 1 0 0 0 0 2 0 0 0 1 2 0 0 0 0 2 2 2 2 2 3 Input 5 1 -2 5 0 0 0 1 0 0 0 0 2 0 0 0 1 2 0 0 0 0 2 2 2 2 2 3 | 2,700 | false | false | false | true | true | false | false | false | false | false | 8,860 |
457D | Problem - 457D - Codeforces =============== xa0 . A player begins by selecting a randomly generated bingo grid (generated uniformly among all available grids). Then _k_ distinct numbers between 1 and _m_ will be called at random (called uniformly among all available sets of _k_ numbers). For each called number that appears on the grid, the player marks that cell. The score at the end is 2 raised to the power of (number of completely marked rows plus number of completely marked columns). Determine the expected value of the score. The expected score may be very large. If the expected score is larger than 1099, print 1099 instead (for example as "1e99" without the quotes). Input Input will consist of three integers _n_, _m_, _k_ (1u2009≤u2009_n_u2009≤u2009300;xa0_n_2u2009≤u2009_m_u2009≤u2009100000;xa0_n_u2009≤u2009_k_u2009≤u2009_m_). Output Print the smaller of 1099 and the expected score. Your answer must be correct within an absolute or relative error of 10u2009-u20099. Examples Input 1 2 1 Output 2.5 Input 2 4 3 Output 4 Input 7 59164 40872 Output 3.1415926538 | 2,700 | true | false | false | false | false | false | false | false | false | false | 8,009 |
463B | Caisa solved the problem with the sugar and now he is on the way back to home. Caisa is playing a mobile game during his path. There are (_n_u2009+u20091) pylons numbered from 0 to _n_ in this game. The pylon with number 0 has zero height, the pylon with number _i_ (_i_u2009>u20090) has height _h__i_. The goal of the game is to reach _n_-th pylon, and the only move the player can do is to jump from the current pylon (let's denote its number as _k_) to the next one (its number will be _k_u2009+u20091). When the player have made such a move, its energy increases by _h__k_u2009-u2009_h__k_u2009+u20091 (if this value is negative the player loses energy). The player must have non-negative amount of energy at any moment of the time. Initially Caisa stand at 0 pylon and has 0 energy. The game provides a special opportunity: one can pay a single dollar and increase the height of anyone pylon by one. Caisa may use that opportunity several times, but he doesn't want to spend too much money. What is the minimal amount of money he must paid to reach the goal of the game? Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105). The next line contains _n_ integers _h_1, _h_2,u2009..., _h__n_ (1u2009u2009≤u2009u2009_h__i_u2009u2009≤u2009u2009105) representing the heights of the pylons. Output Print a single number representing the minimum number of dollars paid by Caisa. Note In the first sample he can pay 4 dollars and increase the height of pylon with number 0 by 4 units. Then he can safely pass to the last pylon. | 1,100 | true | false | true | false | false | false | true | false | false | false | 7,988 |
1118F2 | You are given an undirected tree of $$$n$$$ vertices. Some vertices are colored one of the $$$k$$$ colors, some are uncolored. It is guaranteed that the tree contains at least one vertex of each of the $$$k$$$ colors. There might be no uncolored vertices. You choose a subset of exactly $$$k - 1$$$ edges and remove it from the tree. Tree falls apart into $$$k$$$ connected components. Let's call this subset of edges nice if none of the resulting components contain vertices of different colors. How many nice subsets of edges are there in the given tree? Two subsets are considered different if there is some edge that is present in one subset and absent in the other. The answer may be large, so print it modulo $$$998244353$$$. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 3 cdot 10^5$$$, $$$2 le k le n$$$) — the number of vertices in the tree and the number of colors, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le k$$$) — the colors of the vertices. $$$a_i = 0$$$ means that vertex $$$i$$$ is uncolored, any other value means the vertex $$$i$$$ is colored that color. The $$$i$$$-th of the next $$$n - 1$$$ lines contains two integers $$$v_i$$$ and $$$u_i$$$ ($$$1 le v_i, u_i le n$$$, $$$v_i e u_i$$$) — the edges of the tree. It is guaranteed that the given edges form a tree. It is guaranteed that the tree contains at least one vertex of each of the $$$k$$$ colors. There might be no uncolored vertices. Output Print a single integer — the number of nice subsets of edges in the given tree. Two subsets are considered different if there is some edge that is present in one subset and absent in the other. The answer may be large, so print it modulo $$$998244353$$$. Examples Input 5 2 2 0 0 1 2 1 2 2 3 2 4 2 5 Input 7 3 0 1 0 2 2 3 0 1 3 1 4 1 5 2 7 3 6 4 7 Note Here is the tree from the first example: The only nice subset is edge $$$(2, 4)$$$. Removing it makes the tree fall apart into components $$${4}$$$ and $$${1, 2, 3, 5}$$$. The first component only includes a vertex of color $$$1$$$ and the second component includes only vertices of color $$$2$$$ and uncolored vertices. Here is the tree from the second example: The nice subsets are $$${(1, 3), (4, 7)}$$$, $$${(1, 3), (7, 2)}$$$, $$${(3, 6), (4, 7)}$$$ and $$${(3, 6), (7, 2)}$$$. | 2,700 | false | false | false | true | false | false | false | false | false | false | 5,095 |
1872E | You are given an array of integers $$$a_1, a_2, ldots, a_n$$$, as well as a binary string$$$^{dagger}$$$ $$$s$$$ consisting of $$$n$$$ characters. Augustin is a big fan of data structures. Therefore, he asked you to implement a data structure that can answer $$$q$$$ queries. There are two types of queries: "1 $$$l$$$ $$$r$$$" ($$$1le l le r le n$$$)xa0— replace each character $$$s_i$$$ for $$$l le i le r$$$ with its opposite. That is, replace all $$$ exttt{0}$$$ with $$$ exttt{1}$$$ and all $$$ exttt{1}$$$ with $$$ exttt{0}$$$. "2 $$$g$$$" ($$$g in {0, 1}$$$)xa0— calculate the value of the xa0— the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case description contains an integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the length of the array. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$). The third line of the test case contains the binary string $$$s$$$ of length $$$n$$$. The fourth line of the test case contains one integer $$$q$$$ ($$$1 le q le 10^5$$$)xa0— the number of queries. The subsequent $$$q$$$ lines of the test case describe the queries. The first number of each query, $$$tp in {1, 2}$$$, characterizes the type of the query: if $$$tp = 1$$$, then $$$2$$$ integers $$$1 le l le r le n$$$ follow, meaning that the operation of type $$$1$$$ should be performed with parameters $$$l, r$$$, and if $$$tp = 2$$$, then one integer $$$g in {0, 1}$$$ follows, meaning that the operation of type $$$2$$$ should be performed with parameter $$$g$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$, and also that the sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, and for each query of type $$$2$$$ in it, output the answer to the corresponding query. | 1,500 | false | false | false | true | true | false | false | true | false | false | 1,047 |
1827F | You are given a permutation $$$a_1,a_2,ldots,a_n$$$ of the first $$$n$$$ positive integers. A subarray $$$[l,r]$$$ is called copium if we can rearrange it so that it becomes a sequence of consecutive integers, or more formally, if $$$$$$max(a_l,a_{l+1},ldots,a_r)-min(a_l,a_{l+1},ldots,a_r)=r-l$$$$$$ For each $$$k$$$ in the range $$$[0,n]$$$, print out the maximum number of copium subarrays of $$$a$$$ over all ways of rearranging the last $$$n-k$$$ elements of $$$a$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1le nle 2cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Note In the first test case, the answer permutations for each $$$k$$$ are $$$[1,2,3,4,5]$$$, $$$[5,4,3,2,1]$$$, $$$[5,2,3,4,1]$$$, $$$[5,2,1,3,4]$$$, $$$[5,2,1,4,3]$$$, $$$[5,2,1,4,3]$$$. In the second test case, the answer permutations for each $$$k$$$ are $$$[1,2,3,4]$$$, $$$[2,1,3,4]$$$, $$$[2,1,3,4]$$$, $$$[2,1,4,3]$$$, $$$[2,1,4,3]$$$. | 3,500 | false | true | false | false | true | true | false | false | false | false | 1,317 |
1505C | 1,400 | false | false | true | false | false | false | false | false | false | false | 3,150 | |
1625D | Binary Spiders are species of spiders that live on Mars. These spiders weave their webs to defend themselves from enemies. To weave a web, spiders join in pairs. If the first spider in pair has $$$x$$$ legs, and the second spider has $$$y$$$ legs, then they weave a web with durability $$$x oplus y$$$. Here, $$$oplus$$$ means bitwise XOR. Binary Spiders live in large groups. You observe a group of $$$n$$$ spiders, and the $$$i$$$-th spider has $$$a_i$$$ legs. When the group is threatened, some of the spiders become defenders. Defenders are chosen in the following way. First, there must be at least two defenders. Second, any pair of defenders must be able to weave a web with durability at least $$$k$$$. Third, there must be as much defenders as possible. Scientists have researched the behaviour of Binary Spiders for a long time, and now they have a hypothesis that they can always choose the defenders in an optimal way, satisfying the conditions above. You need to verify this hypothesis on your group of spiders. So, you need to understand how many spiders must become defenders. You are not a Binary Spider, so you decided to use a computer to solve this problem. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 3cdot10^5$$$, $$$0 le k le 2^{30} - 1$$$), the amount of spiders in the group and the minimal allowed durability of a web. The second line contains $$$n$$$ integers $$$a_i$$$ ($$$0 le a_i le 2^{30}-1$$$) — the number of legs the $$$i$$$-th spider has. Output In the first line, print a single integer $$$ell$$$ ($$$2 le ell le n$$$), the maximum possible amount of defenders. In the second line, print $$$ell$$$ integers $$$b_i$$$, separated by a single space ($$$1 le b_i le n$$$) — indices of spiders that will become defenders. If there exists more than one way to choose the defenders, print any of them. Unfortunately, it may appear that it's impossible to choose the defenders. In this case, print a single integer $$$-1$$$. Note Consider the examples above. In the first example, the group of spiders is illustrated on the picture below: We choose the two-legged, the ten-legged and the $$$16$$$-legged spiders. It's not hard to see that each pair may weave a web with enough durability, as $$$2 oplus 10 = 8 ge 8$$$, $$$2 oplus 16 = 18 ge 8$$$ and $$$10 oplus 16 = 26 ge 8$$$. This is not the only way, as you can also choose, for example, the spiders with indices $$$3$$$, $$$4$$$, and $$$6$$$. In the second example, no pair of spiders can weave the web with durability $$$1024$$$ or more, so the answer is $$$-1$$$. | 2,300 | true | false | true | false | true | false | false | false | true | false | 2,490 |
817D | Problem - 817D - Codeforces =============== xa0 , imbalance value is 0; | 1,900 | false | false | false | false | true | false | false | false | true | false | 6,490 |
1669A | Codeforces separates its users into $$$4$$$ divisions by their rating: For Division 1: $$$1900 leq mathrm{rating}$$$ For Division 2: $$$1600 leq mathrm{rating} leq 1899$$$ For Division 3: $$$1400 leq mathrm{rating} leq 1599$$$ For Division 4: $$$mathrm{rating} leq 1399$$$ Given a $$$mathrm{rating}$$$, print in which division the $$$mathrm{rating}$$$ belongs. Input The first line of the input contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of testcases. The description of each test consists of one line containing one integer $$$mathrm{rating}$$$ ($$$-5000 leq mathrm{rating} leq 5000$$$). Output For each test case, output a single line containing the correct division in the format "Division X", where $$$X$$$ is an integer between $$$1$$$ and $$$4$$$ representing the division for the corresponding rating. Example Input 7 -789 1299 1300 1399 1400 1679 2300 Output Division 4 Division 4 Division 4 Division 4 Division 3 Division 2 Division 1 Note For test cases $$$1-4$$$, the corresponding ratings are $$$-789$$$, $$$1299$$$, $$$1300$$$, $$$1399$$$, so all of them are in division $$$4$$$. For the fifth test case, the corresponding rating is $$$1400$$$, so it is in division $$$3$$$. For the sixth test case, the corresponding rating is $$$1679$$$, so it is in division $$$2$$$. For the seventh test case, the corresponding rating is $$$2300$$$, so it is in division $$$1$$$. | 800 | false | false | true | false | false | false | false | false | false | false | 2,265 |
909D | You are given a set of points on a straight line. Each point has a color assigned to it. For point _a_, its neighbors are the points which don't have any other points between them and _a_. Each point has at most two neighbors - one from the left and one from the right. You perform a sequence of operations on this set of points. In one operation, you delete all points which have a neighbor point of a different color than the point itself. Points are deleted simultaneously, i.e. first you decide which points have to be deleted and then delete them. After that you can perform the next operation etc. If an operation would not delete any points, you can't perform it. How many operations will you need to perform until the next operation does not have any points to delete? Input Input contains a single string of lowercase English letters 'a'-'z'. The letters give the points' colors in the order in which they are arranged on the line: the first letter gives the color of the leftmost point, the second gives the color of the second point from the left etc. The number of the points is between 1 and 106. Output Output one line containing an integer - the number of operations which can be performed on the given set of points until there are no more points to delete. Note In the first test case, the first operation will delete two middle points and leave points "ab", which will be deleted with the second operation. There will be no points left to apply the third operation to. In the second test case, the first operation will delete the four points in the middle, leaving points "aa". None of them have neighbors of other colors, so the second operation can't be applied. | 2,100 | false | true | true | false | true | false | false | false | false | false | 6,109 |
1780D | This is an interactive problem. Kira has a hidden positive integer $$$n$$$, and Hayato needs to guess it. Initially, Kira gives Hayato the value $$$mathrm{cnt}$$$ — the number of unit bits in the binary notation of $$$n$$$. To guess $$$n$$$, Hayato can only do operations of one kind: choose an integer $$$x$$$ and subtract it from $$$n$$$. Note that after each operation, the number $$$n$$$ changes. Kira doesn't like bad requests, so if Hayato tries to subtract a number $$$x$$$ greater than $$$n$$$, he will lose to Kira. After each operation, Kira gives Hayato the updated value $$$mathrm{cnt}$$$ — the number of unit bits in the binary notation of the updated value of $$$n$$$. Kira doesn't have much patience, so Hayato must guess the original value of $$$n$$$ after no more than $$$30$$$ operations. Since Hayato is in elementary school, he asks for your help. Write a program that guesses the number $$$n$$$. Kira is an honest person, so he chooses the initial number $$$n$$$ before all operations and does not change it afterward. Input The input data contains several test cases. The first line contains one integer $$$t$$$ ($$$1 le t le 500$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains the number $$$mathrm{cnt}$$$ — the initial number of unit bits in the binary notation $$$n$$$. The hidden integer $$$n$$$ satisfies the following constraint: $$$1 le n le 10^9$$$. Interaction To guess $$$n$$$, you can perform the operation at most $$$30$$$ times. To do that, print a line with the following format: "- x" ($$$1 le x le 10^9$$$). After this operation, the number $$$x$$$ is subtracted from $$$n$$$, and therefore $$$n$$$ is changed. If the number $$$x$$$ is greater than the current value of $$$n$$$, then the request is considered invalid. After the operation read a line containing a single non-negative integer $$$mathrm{cnt}$$$ — the number of unit bits in the binary notation of the current $$$n$$$ after the operation. When you know the initial value of $$$n$$$, print one line in the following format: "! n" ($$$1 le n le 10^9$$$). After that, move on to the next test case, or terminate the program if there are none. If your program performs more than $$$30$$$ operations for one test case, subtracts a number $$$x$$$ greater than $$$n$$$, or makes an incorrect request, then response to the request will be -1, after receiving such response, your program must exit immediately to receive the Wrong Answer verdict. Otherwise, you can get any other verdict. After printing a query or the answer, do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Hacks To make a hack, use the following format. The first line should contain a single integer $$$t$$$ ($$$1 leq t leq 500$$$). Each test case should contain one integer $$$n$$$ ($$$1 leq n leq 10^9$$$) on a separate line. Example Output - 1 ! 1 - 1 - 1 ! 2 - 2 - 1 ! 3 Note For example, the number of unit bits in number $$$6$$$ is $$$2$$$, because binary notation of $$$6$$$ is $$$110$$$. For $$$13$$$ the number of unit bits is $$$3$$$, because $$$13_{10} = 1101_2$$$. In the first test case, $$$n = 1$$$, so the input is the number $$$1$$$. After subtracting one from $$$n$$$, it becomes zero, so the number of unit bits in it is $$$0$$$. In the third test case, $$$n = 3$$$, which in binary representation looks like $$$3_{10} = 11_2$$$, so the input is the number of ones, that is $$$2$$$. After subtracting $$$2$$$, $$$n = 1$$$, so the number of unit bits is now $$$1$$$. After subtracting one from $$$n$$$, it becomes equal to zero. Note that the blank lines in the input and output examples are shown for clarity and are not present in the actual interaction. | 1,800 | false | false | false | false | false | true | false | true | false | false | 1,592 |
1163D | During a normal walk in the forest, Katie has stumbled upon a mysterious code! However, the mysterious code had some characters unreadable. She has written down this code as a string $$$c$$$ consisting of lowercase English characters and asterisks ("*"), where each of the asterisks denotes an unreadable character. Excited with her discovery, Katie has decided to recover the unreadable characters by replacing each asterisk with arbitrary lowercase English letter (different asterisks might be replaced with different letters). Katie has a favorite string $$$s$$$ and a not-so-favorite string $$$t$$$ and she would love to recover the mysterious code so that it has as many occurrences of $$$s$$$ as possible and as little occurrences of $$$t$$$ as possible. Formally, let's denote $$$f(x, y)$$$ as the number of occurrences of $$$y$$$ in $$$x$$$ (for example, $$$f(aababa, ab) = 2$$$). Katie wants to recover the code $$$c'$$$ conforming to the original $$$c$$$, such that $$$f(c', s) - f(c', t)$$$ is largest possible. However, Katie is not very good at recovering codes in general, so she would like you to help her out. Input The first line contains string $$$c$$$ ($$$1 leq c leq 1000$$$)xa0— the mysterious code . It is guaranteed that $$$c$$$ consists of lowercase English characters and asterisks "*" only. The second and third line contain strings $$$s$$$ and $$$t$$$ respectively ($$$1 leq s, t leq 50$$$, $$$s eq t$$$). It is guaranteed that $$$s$$$ and $$$t$$$ consist of lowercase English characters only. Output Print a single integerxa0— the largest possible value of $$$f(c', s) - f(c', t)$$$ of the recovered code. Note In the first example, for $$$c'$$$ equal to "katie" $$$f(c', s) = 1$$$ and $$$f(c', t) = 0$$$, which makes $$$f(c', s) - f(c', t) = 1$$$ which is the largest possible. In the second example, the only $$$c'$$$ conforming to the given $$$c$$$ is "caat". The corresponding $$$f(c', s) - f(c', t) = 1 - 2 = -1$$$. In the third example, there are multiple ways to recover the code such that $$$f(c', s) - f(c', t)$$$ is largest possible, for example "aaa", "aac", or even "zaz". The value of $$$f(c', s) - f(c', t) = 0$$$ for all of these recovered codes. In the fourth example, the optimal recovered code $$$c'$$$ would be "ccc". The corresponding $$$f(c', s) - f(c', t) = 2$$$. | 2,100 | false | false | false | true | false | false | false | false | false | false | 4,899 |
1218A | You are in charge of the BubbleReactor. It consists of $$$N$$$ BubbleCores connected with $$$N$$$ lines of electrical wiring. Each electrical wiring connects two distinct BubbleCores. There are no BubbleCores connected with more than one line of electrical wiring. Your task is to start the BubbleReactor by starting each BubbleCore. In order for a BubbleCore to be started it needs to be receiving power from a directly connected BubbleCore which is already started. However, you can kick-start one BubbleCore manually without needing power. It is guaranteed that all BubbleCores can be started. Before the BubbleCore boot up procedure its potential is calculated as the number of BubbleCores it can power on (the number of inactive BubbleCores which are connected to it directly or with any number of inactive BubbleCores in between, itself included) Start the BubbleReactor so that the sum of all BubbleCores' potentials is maximum. Input First line contains one integer $$$N (3 leq N leq 15.000)$$$, the number of BubbleCores. The following N lines contain two integers $$$U, V (0 leq U eq V < N)$$$ denoting that there exists electrical wiring between BubbleCores $$$U$$$ and $$$V$$$. Output Single integer, the maximum sum of all BubbleCores' potentials. Example Input 10 0 1 0 3 0 4 0 9 1 2 2 3 2 7 4 5 4 6 7 8 Note If we start by kickstarting BubbleCup 8 and then turning on cores 7, 2, 1, 3, 0, 9, 4, 5, 6 in that order we get potentials 10 + 9 + 8 + 7 + 6 + 5 + 1 + 3 + 1 + 1 = 51 | 2,800 | false | false | false | true | false | false | false | false | false | true | 4,584 |
1614A | Businessman Divan loves chocolate! Today he came to a store to buy some chocolate. Like all businessmen, Divan knows the value of money, so he will not buy too expensive chocolate. At the same time, too cheap chocolate tastes bad, so he will not buy it as well. The store he came to has $$$n$$$ different chocolate bars, and the price of the $$$i$$$-th chocolate bar is $$$a_i$$$ dollars. Divan considers a chocolate bar too expensive if it costs strictly more than $$$r$$$ dollars. Similarly, he considers a bar of chocolate to be too cheap if it costs strictly less than $$$l$$$ dollars. Divan will not buy too cheap or too expensive bars. Divan is not going to spend all his money on chocolate bars, so he will spend at most $$$k$$$ dollars on chocolates. Please determine the maximum number of chocolate bars Divan can buy. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). Description of the test cases follows. The description of each test case consists of two lines. The first line contains integers $$$n$$$, $$$l$$$, $$$r$$$, $$$k$$$ ($$$1 le n le 100$$$, $$$1 le l le r le 10^9$$$, $$$1 le k le 10^9$$$)xa0— the lowest acceptable price of a chocolate, the highest acceptable price of a chocolate and Divan's total budget, respectively. The second line contains a sequence $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$) integersxa0— the prices of chocolate bars in the store. Output For each test case print a single integer — the maximum number of chocolate bars Divan can buy. Example Input 8 3 1 100 100 50 100 50 6 3 5 10 1 2 3 4 5 6 6 3 5 21 1 2 3 4 5 6 10 50 69 100 20 30 40 77 1 1 12 4 70 10000 3 50 80 30 20 60 70 10 2 7 100 2 2 2 2 2 7 7 7 7 7 4 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1 1 1 1 1 Note In the first example Divan can buy chocolate bars $$$1$$$ and $$$3$$$ and spend $$$100$$$ dollars on them. In the second example Divan can buy chocolate bars $$$3$$$ and $$$4$$$ and spend $$$7$$$ dollars on them. In the third example Divan can buy chocolate bars $$$3$$$, $$$4$$$, and $$$5$$$ for $$$12$$$ dollars. In the fourth example Divan cannot buy any chocolate bar because each of them is either too cheap or too expensive. In the fifth example Divan cannot buy any chocolate bar because he considers the first bar too cheap, and has no budget for the second or third. In the sixth example Divan can buy all the chocolate bars in the shop. | 800 | false | true | false | false | false | true | true | false | false | false | 2,570 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.