contestId int64 0 1.01k | name stringlengths 2 58 | tags listlengths 0 11 | title stringclasses 523
values | time-limit stringclasses 8
values | memory-limit stringclasses 8
values | problem-description stringlengths 0 7.15k | input-specification stringlengths 0 2.05k | output-specification stringlengths 0 1.5k | demo-input listlengths 0 7 | demo-output listlengths 0 7 | note stringlengths 0 5.24k | test_cases listlengths 0 402 | timeConsumedMillis int64 0 8k | memoryConsumedBytes int64 0 537M | score float64 -1 3.99 | __index_level_0__ int64 0 621k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
61 | Enemy is weak | [
"data structures",
"trees"
] | E. Enemy is weak | 5 | 256 | The Romans have attacked again. This time they are much more than the Persians but Shapur is ready to defeat them. He says: "A lion is never afraid of a hundred sheep".
Nevertheless Shapur has to find weaknesses in the Roman army to defeat them. So he gives the army a weakness number.
In Shapur's opinion the weakness of an army is equal to the number of triplets *i*,<=*j*,<=*k* such that *i*<=<<=*j*<=<<=*k* and *a**i*<=><=*a**j*<=><=*a**k* where *a**x* is the power of man standing at position *x*. The Roman army has one special trait β powers of all the people in it are distinct.
Help Shapur find out how weak the Romans are. | The first line of input contains a single number *n* (3<=β€<=*n*<=β€<=106) β the number of men in Roman army. Next line contains *n* different positive integers *a**i* (1<=β€<=*i*<=β€<=*n*,<=1<=β€<=*a**i*<=β€<=109) β powers of men in the Roman army. | A single integer number, the weakness of the Roman army.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). | [
"3\n3 2 1\n",
"3\n2 3 1\n",
"4\n10 8 3 1\n",
"4\n1 5 4 3\n"
] | [
"1\n",
"0\n",
"4\n",
"1\n"
] | none | [
{
"input": "3\n3 2 1",
"output": "1"
},
{
"input": "3\n2 3 1",
"output": "0"
},
{
"input": "4\n10 8 3 1",
"output": "4"
},
{
"input": "4\n1 5 4 3",
"output": "1"
},
{
"input": "9\n10 9 5 6 8 3 4 7 11",
"output": "20"
},
{
"input": "7\n11 3 8 4 2 9 6",
... | 2,340 | 213,606,400 | 3.368127 | 14,724 |
873 | Awards For Contestants | [
"brute force",
"data structures",
"dp"
] | null | null | Alexey recently held a programming contest for students from Berland. *n* students participated in a contest, *i*-th of them solved *a**i* problems. Now he wants to award some contestants. Alexey can award the students with diplomas of three different degrees. Each student either will receive one diploma of some degree, or won't receive any diplomas at all. Let *cnt**x* be the number of students that are awarded with diplomas of degree *x* (1<=β€<=*x*<=β€<=3). The following conditions must hold:
- For each *x* (1<=β€<=*x*<=β€<=3) *cnt**x*<=><=0; - For any two degrees *x* and *y* *cnt**x*<=β€<=2Β·*cnt**y*.
Of course, there are a lot of ways to distribute the diplomas. Let *b**i* be the degree of diploma *i*-th student will receive (or <=-<=1 if *i*-th student won't receive any diplomas). Also for any *x* such that 1<=β€<=*x*<=β€<=3 let *c**x* be the maximum number of problems solved by a student that receives a diploma of degree *x*, and *d**x* be the minimum number of problems solved by a student that receives a diploma of degree *x*. Alexey wants to distribute the diplomas in such a way that:
1. If student *i* solved more problems than student *j*, then he has to be awarded not worse than student *j* (it's impossible that student *j* receives a diploma and *i* doesn't receive any, and also it's impossible that both of them receive a diploma, but *b**j*<=<<=*b**i*); 1. *d*1<=-<=*c*2 is maximum possible; 1. Among all ways that maximize the previous expression, *d*2<=-<=*c*3 is maximum possible; 1. Among all ways that correspond to the two previous conditions, *d*3<=-<=*c*<=-<=1 is maximum possible, where *c*<=-<=1 is the maximum number of problems solved by a student that doesn't receive any diploma (or 0 if each student is awarded with some diploma).
Help Alexey to find a way to award the contestants! | The first line contains one integer number *n* (3<=β€<=*n*<=β€<=3000).
The second line contains *n* integer numbers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=5000). | Output *n* numbers. *i*-th number must be equal to the degree of diploma *i*-th contestant will receive (or <=-<=1 if he doesn't receive any diploma).
If there are multiple optimal solutions, print any of them. It is guaranteed that the answer always exists. | [
"4\n1 2 3 4\n",
"6\n1 4 3 1 1 2\n"
] | [
"3 3 2 1 \n",
"-1 1 2 -1 -1 3 \n"
] | none | [
{
"input": "4\n1 2 3 4",
"output": "3 3 2 1 "
},
{
"input": "6\n1 4 3 1 1 2",
"output": "-1 1 2 -1 -1 3 "
},
{
"input": "100\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... | 140 | 0 | 0 | 14,759 | |
145 | Lucky Subsequence | [
"combinatorics",
"dp",
"math"
] | null | null | Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya has sequence *a* consisting of *n* integers.
The subsequence of the sequence *a* is such subsequence that can be obtained from *a* by removing zero or more of its elements.
Two sequences are considered different if index sets of numbers included in them are different. That is, the values βof the elements βdo not matter in the comparison of subsequences. In particular, any sequence of length *n* has exactly 2*n* different subsequences (including an empty subsequence).
A subsequence is considered lucky if it has a length exactly *k* and does not contain two identical lucky numbers (unlucky numbers can be repeated any number of times).
Help Petya find the number of different lucky subsequences of the sequence *a*. As Petya's parents don't let him play with large numbers, you should print the result modulo prime number 1000000007 (109<=+<=7). | The first line contains two integers *n* and *k* (1<=β€<=*k*<=β€<=*n*<=β€<=105). The next line contains *n* integers *a**i* (1<=β€<=*a**i*<=β€<=109) β the sequence *a*. | On the single line print the single number β the answer to the problem modulo prime number 1000000007 (109<=+<=7). | [
"3 2\n10 10 10\n",
"4 2\n4 4 7 7\n"
] | [
"3\n",
"4\n"
] | In the first sample all 3 subsequences of the needed length are considered lucky.
In the second sample there are 4 lucky subsequences. For them the sets of indexes equal (the indexation starts from 1): {1,β3}, {1,β4}, {2,β3} and {2,β4}. | [
{
"input": "3 2\n10 10 10",
"output": "3"
},
{
"input": "4 2\n4 4 7 7",
"output": "4"
},
{
"input": "7 4\n1 2 3 4 5 6 7",
"output": "35"
},
{
"input": "7 4\n7 7 7 7 7 7 7",
"output": "0"
},
{
"input": "10 1\n1 2 3 4 5 6 7 8 9 10",
"output": "10"
},
{
"... | 62 | 0 | 0 | 14,760 | |
378 | Semifinals | [
"implementation",
"sortings"
] | null | null | Two semifinals have just been in the running tournament. Each semifinal had *n* participants. There are *n* participants advancing to the finals, they are chosen as follows: from each semifinal, we choose *k* people (0<=β€<=2*k*<=β€<=*n*) who showed the best result in their semifinals and all other places in the finals go to the people who haven't ranked in the top *k* in their semifinal but got to the *n*<=-<=2*k* of the best among the others.
The tournament organizers hasn't yet determined the *k* value, so the participants want to know who else has any chance to get to the finals and who can go home. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=105) β the number of participants in each semifinal.
Each of the next *n* lines contains two integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=109)Β β the results of the *i*-th participant (the number of milliseconds he needs to cover the semifinals distance) of the first and second semifinals, correspondingly. All results are distinct. Sequences *a*1, *a*2, ..., *a**n* and *b*1, *b*2, ..., *b**n* are sorted in ascending order, i.e. in the order the participants finished in the corresponding semifinal. | Print two strings consisting of *n* characters, each equals either "0" or "1". The first line should correspond to the participants of the first semifinal, the second line should correspond to the participants of the second semifinal. The *i*-th character in the *j*-th line should equal "1" if the *i*-th participant of the *j*-th semifinal has any chances to advance to the finals, otherwise it should equal a "0". | [
"4\n9840 9920\n9860 9980\n9930 10020\n10040 10090\n",
"4\n9900 9850\n9940 9930\n10000 10020\n10060 10110\n"
] | [
"1110\n1100\n",
"1100\n1100\n"
] | Consider the first sample. Each semifinal has 4 participants. The results of the first semifinal are 9840, 9860, 9930, 10040. The results of the second semifinal are 9920, 9980, 10020, 10090.
- If *k*β=β0, the finalists are determined by the time only, so players 9840, 9860, 9920 and 9930 advance to the finals. - If *k*β=β1, the winners from both semifinals move to the finals (with results 9840 and 9920), and the other places are determined by the time (these places go to the sportsmen who run the distance in 9860 and 9930 milliseconds). - If *k*β=β2, then first and second places advance from each seminfial, these are participants with results 9840, 9860, 9920 and 9980 milliseconds. | [
{
"input": "4\n9840 9920\n9860 9980\n9930 10020\n10040 10090",
"output": "1110\n1100"
},
{
"input": "4\n9900 9850\n9940 9930\n10000 10020\n10060 10110",
"output": "1100\n1100"
},
{
"input": "1\n1 2",
"output": "1\n0"
},
{
"input": "1\n2 1",
"output": "0\n1"
},
{
"... | 1,000 | 38,707,200 | 0 | 14,767 | |
108 | Datatypes | [
"math",
"sortings"
] | B. Datatypes | 2 | 256 | Tattah's youngest brother, Tuftuf, is new to programming.
Since his older brother is such a good programmer, his biggest dream is to outshine him. Tuftuf is a student at the German University in Cairo (GUC) where he learns to write programs in Gava.
Today, Tuftuf was introduced to Gava's unsigned integer datatypes. Gava has *n* unsigned integer datatypes of sizes (in bits) *a*1,<=*a*2,<=... *a**n*. The *i*-th datatype have size *a**i* bits, so it can represent every integer between 0 and 2*a**i*<=-<=1 inclusive.
Tuftuf is thinking of learning a better programming language. If there exists an integer *x*, such that *x* fits in some type *i* (in *a**i* bits) and *x*Β·*x* does not fit in some other type *j* (in *a**j* bits) where *a**i*<=<<=*a**j*, then Tuftuf will stop using Gava.
Your task is to determine Tuftuf's destiny. | The first line contains integer *n* (2<=β€<=*n*<=β€<=105) β the number of Gava's unsigned integer datatypes' sizes. The second line contains a single-space-separated list of *n* integers (1<=β€<=*a**i*<=β€<=109) β sizes of datatypes in bits. Some datatypes may have equal sizes. | Print "YES" if Tuftuf will stop using Gava, and "NO" otherwise. | [
"3\n64 16 32\n",
"4\n4 2 1 3\n"
] | [
"NO\n",
"YES\n"
] | In the second example, *x*β=β7 (111<sub class="lower-index">2</sub>) fits in 3 bits, but *x*<sup class="upper-index">2</sup>β=β49 (110001<sub class="lower-index">2</sub>) does not fit in 4 bits. | [
{
"input": "3\n64 16 32",
"output": "NO"
},
{
"input": "4\n4 2 1 3",
"output": "YES"
},
{
"input": "5\n1 5 3 3 2",
"output": "YES"
},
{
"input": "52\n474 24 24 954 9 234 474 114 24 114 234 24 114 114 234 9 9 24 9 54 234 54 9 954 474 9 54 54 54 234 9 114 24 54 114 954 954 474 ... | 466 | 10,854,400 | 3.863282 | 14,768 |
670 | Restore a Number | [
"brute force",
"constructive algorithms",
"strings"
] | null | null | Vasya decided to pass a very large integer *n* to Kate. First, he wrote that number as a string, then he appended to the right integer *k*Β β the number of digits in *n*.
Magically, all the numbers were shuffled in arbitrary order while this note was passed to Kate. The only thing that Vasya remembers, is a non-empty substring of *n* (a substring of *n* is a sequence of consecutive digits of the number *n*).
Vasya knows that there may be more than one way to restore the number *n*. Your task is to find the smallest possible initial integer *n*. Note that decimal representation of number *n* contained no leading zeroes, except the case the integer *n* was equal to zero itself (in this case a single digit 0 was used). | The first line of the input contains the string received by Kate. The number of digits in this string does not exceed 1<=000<=000.
The second line contains the substring of *n* which Vasya remembers. This string can contain leading zeroes.
It is guaranteed that the input data is correct, and the answer always exists. | Print the smalles integer *n* which Vasya could pass to Kate. | [
"003512\n021\n",
"199966633300\n63\n"
] | [
"30021\n",
"3036366999\n"
] | none | [
{
"input": "003512\n021",
"output": "30021"
},
{
"input": "199966633300\n63",
"output": "3036366999"
},
{
"input": "01\n0",
"output": "0"
},
{
"input": "0000454312911\n9213544",
"output": "92135440000"
},
{
"input": "13\n3",
"output": "3"
},
{
"input":... | 46 | 0 | 0 | 14,801 | |
17 | Hierarchy | [
"dfs and similar",
"dsu",
"greedy",
"shortest paths"
] | B. Hierarchy | 2 | 64 | Nick's company employed *n* people. Now Nick needs to build a tree hierarchy of Β«supervisor-surbodinateΒ» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are *m* applications written in the following form: Β«employee *a**i* is ready to become a supervisor of employee *b**i* at extra cost *c**i*Β». The qualification *q**j* of each employee is known, and for each application the following is true: *q**a**i*<=><=*q**b**i*.
Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it. | The first input line contains integer *n* (1<=β€<=*n*<=β€<=1000) β amount of employees in the company. The following line contains *n* space-separated numbers *q**j* (0<=β€<=*q**j*<=β€<=106)β the employees' qualifications. The following line contains number *m* (0<=β€<=*m*<=β€<=10000) β amount of received applications. The following *m* lines contain the applications themselves, each of them in the form of three space-separated numbers: *a**i*, *b**i* and *c**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*, 0<=β€<=*c**i*<=β€<=106). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application *q**a**i*<=><=*q**b**i*. | Output the only line β the minimum cost of building such a hierarchy, or -1 if it is impossible to build it. | [
"4\n7 2 3 1\n4\n1 2 5\n2 4 1\n3 4 1\n1 3 5\n",
"3\n1 2 3\n2\n3 1 2\n3 1 3\n"
] | [
"11\n",
"-1\n"
] | In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1. | [
{
"input": "4\n7 2 3 1\n4\n1 2 5\n2 4 1\n3 4 1\n1 3 5",
"output": "11"
},
{
"input": "3\n1 2 3\n2\n3 1 2\n3 1 3",
"output": "-1"
},
{
"input": "1\n2\n0",
"output": "0"
},
{
"input": "2\n5 3\n4\n1 2 0\n1 2 5\n1 2 0\n1 2 7",
"output": "0"
},
{
"input": "3\n9 4 5\n5\... | 154 | 0 | 0 | 14,806 |
675 | Trains and Statistic | [
"data structures",
"dp",
"greedy"
] | null | null | Vasya commutes by train every day. There are *n* train stations in the city, and at the *i*-th station it's possible to buy only tickets to stations from *i*<=+<=1 to *a**i* inclusive. No tickets are sold at the last station.
Let Ο*i*,<=*j* be the minimum number of tickets one needs to buy in order to get from stations *i* to station *j*. As Vasya is fond of different useless statistic he asks you to compute the sum of all values Ο*i*,<=*j* among all pairs 1<=β€<=*i*<=<<=*j*<=β€<=*n*. | The first line of the input contains a single integer *n* (2<=β€<=*n*<=β€<=100<=000)Β β the number of stations.
The second line contains *n*<=-<=1 integer *a**i* (*i*<=+<=1<=β€<=*a**i*<=β€<=*n*), the *i*-th of them means that at the *i*-th station one may buy tickets to each station from *i*<=+<=1 to *a**i* inclusive. | Print the sum of Ο*i*,<=*j* among all pairs of 1<=β€<=*i*<=<<=*j*<=β€<=*n*. | [
"4\n4 4 4\n",
"5\n2 3 5 5\n"
] | [
"6\n",
"17\n"
] | In the first sample it's possible to get from any station to any other (with greater index) using only one ticket. The total number of pairs is 6, so the answer is also 6.
Consider the second sample:
- Ο<sub class="lower-index">1,β2</sub>β=β1 - Ο<sub class="lower-index">1,β3</sub>β=β2 - Ο<sub class="lower-index">1,β4</sub>β=β3 - Ο<sub class="lower-index">1,β5</sub>β=β3 - Ο<sub class="lower-index">2,β3</sub>β=β1 - Ο<sub class="lower-index">2,β4</sub>β=β2 - Ο<sub class="lower-index">2,β5</sub>β=β2 - Ο<sub class="lower-index">3,β4</sub>β=β1 - Ο<sub class="lower-index">3,β5</sub>β=β1 - Ο<sub class="lower-index">4,β5</sub>β=β1
Thus the answer equals 1β+β2β+β3β+β3β+β1β+β2β+β2β+β1β+β1β+β1β=β17. | [
{
"input": "4\n4 4 4",
"output": "6"
},
{
"input": "5\n2 3 5 5",
"output": "17"
},
{
"input": "2\n2",
"output": "1"
},
{
"input": "10\n2 10 8 7 8 8 10 9 10",
"output": "63"
},
{
"input": "3\n3 3",
"output": "3"
},
{
"input": "4\n3 3 4",
"output": "... | 108 | 23,142,400 | 0 | 14,811 | |
638 | Three-dimensional Turtle Super Computer | [
"brute force",
"dfs and similar",
"graphs"
] | null | null | A super computer has been built in the Turtle Academy of Sciences. The computer consists of *n*Β·*m*Β·*k* CPUs. The architecture was the paralellepiped of size *n*<=Γ<=*m*<=Γ<=*k*, split into 1<=Γ<=1<=Γ<=1 cells, each cell contains exactly one CPU. Thus, each CPU can be simultaneously identified as a group of three numbers from the layer number from 1 to *n*, the line number from 1 to *m* and the column number from 1 to *k*.
In the process of the Super Computer's work the CPUs can send each other messages by the famous turtle scheme: CPU (*x*,<=*y*,<=*z*) can send messages to CPUs (*x*<=+<=1,<=*y*,<=*z*), (*x*,<=*y*<=+<=1,<=*z*) and (*x*,<=*y*,<=*z*<=+<=1) (of course, if they exist), there is no feedback, that is, CPUs (*x*<=+<=1,<=*y*,<=*z*), (*x*,<=*y*<=+<=1,<=*z*) and (*x*,<=*y*,<=*z*<=+<=1) cannot send messages to CPU (*x*,<=*y*,<=*z*).
Over time some CPUs broke down and stopped working. Such CPUs cannot send messages, receive messages or serve as intermediates in transmitting messages. We will say that CPU (*a*,<=*b*,<=*c*) controls CPU (*d*,<=*e*,<=*f*) , if there is a chain of CPUs (*x**i*,<=*y**i*,<=*z**i*), such that (*x*1<==<=*a*,<=*y*1<==<=*b*,<=*z*1<==<=*c*), (*x**p*<==<=*d*,<=*y**p*<==<=*e*,<=*z**p*<==<=*f*) (here and below *p* is the length of the chain) and the CPU in the chain with number *i* (*i*<=<<=*p*) can send messages to CPU *i*<=+<=1.
Turtles are quite concerned about the denial-proofness of the system of communication between the remaining CPUs. For that they want to know the number of critical CPUs. A CPU (*x*,<=*y*,<=*z*) is critical, if turning it off will disrupt some control, that is, if there are two distinctive from (*x*,<=*y*,<=*z*) CPUs: (*a*,<=*b*,<=*c*) and (*d*,<=*e*,<=*f*), such that (*a*,<=*b*,<=*c*) controls (*d*,<=*e*,<=*f*) before (*x*,<=*y*,<=*z*) is turned off and stopped controlling it after the turning off. | The first line contains three integers *n*, *m* and *k* (1<=β€<=*n*,<=*m*,<=*k*<=β€<=100)Β β the dimensions of the Super Computer.
Then *n* blocks follow, describing the current state of the processes. The blocks correspond to the layers of the Super Computer in the order from 1 to *n*. Each block consists of *m* lines, *k* characters in each β the description of a layer in the format of an *m*<=Γ<=*k* table. Thus, the state of the CPU (*x*,<=*y*,<=*z*) is corresponded to the *z*-th character of the *y*-th line of the block number *x*. Character "1" corresponds to a working CPU and character "0" corresponds to a malfunctioning one. The blocks are separated by exactly one empty line. | Print a single integer β the number of critical CPUs, that is, such that turning only this CPU off will disrupt some control. | [
"2 2 3\n000\n000\n\n111\n111\n",
"3 3 3\n111\n111\n111\n\n111\n111\n111\n\n111\n111\n111\n",
"1 1 10\n0101010101\n"
] | [
"2\n",
"19\n",
"0\n"
] | In the first sample the whole first layer of CPUs is malfunctional. In the second layer when CPU (2,β1,β2) turns off, it disrupts the control by CPU (2,β1,β3) over CPU (2,β1,β1), and when CPU (2,β2,β2) is turned off, it disrupts the control over CPU (2,β2,β3) by CPU (2,β2,β1).
In the second sample all processors except for the corner ones are critical.
In the third sample there is not a single processor controlling another processor, so the answer is 0. | [
{
"input": "2 2 3\n000\n000\n\n111\n111",
"output": "2"
},
{
"input": "3 3 3\n111\n111\n111\n\n111\n111\n111\n\n111\n111\n111",
"output": "19"
},
{
"input": "1 1 10\n0101010101",
"output": "0"
},
{
"input": "1 1 1\n0",
"output": "0"
},
{
"input": "1 1 1\n1",
"... | 46 | 4,812,800 | 0 | 14,812 | |
606 | Testing Robots | [
"implementation"
] | null | null | The Cybernetics Failures (CF) organisation made a prototype of a bomb technician robot. To find the possible problems it was decided to carry out a series of tests. At the beginning of each test the robot prototype will be placed in cell (*x*0,<=*y*0) of a rectangular squared field of size *x*<=Γ<=*y*, after that a mine will be installed into one of the squares of the field. It is supposed to conduct exactly *x*Β·*y* tests, each time a mine is installed into a square that has never been used before. The starting cell of the robot always remains the same.
After placing the objects on the field the robot will have to run a sequence of commands given by string *s*, consisting only of characters 'L', 'R', 'U', 'D'. These commands tell the robot to move one square to the left, to the right, up or down, or stay idle if moving in the given direction is impossible. As soon as the robot fulfills all the sequence of commands, it will blow up due to a bug in the code. But if at some moment of time the robot is at the same square with the mine, it will also blow up, but not due to a bug in the code.
Moving to the left decreases coordinate *y*, and moving to the right increases it. Similarly, moving up decreases the *x* coordinate, and moving down increases it.
The tests can go on for very long, so your task is to predict their results. For each *k* from 0 to *length*(*s*) your task is to find in how many tests the robot will run exactly *k* commands before it blows up. | The first line of the input contains four integers *x*, *y*, *x*0, *y*0 (1<=β€<=*x*,<=*y*<=β€<=500,<=1<=β€<=*x*0<=β€<=*x*,<=1<=β€<=*y*0<=β€<=*y*)Β β the sizes of the field and the starting coordinates of the robot. The coordinate axis *X* is directed downwards and axis *Y* is directed to the right.
The second line contains a sequence of commands *s*, which should be fulfilled by the robot. It has length from 1 to 100<=000 characters and only consists of characters 'L', 'R', 'U', 'D'. | Print the sequence consisting of (*length*(*s*)<=+<=1) numbers. On the *k*-th position, starting with zero, print the number of tests where the robot will run exactly *k* commands before it blows up. | [
"3 4 2 2\nUURDRDRL\n",
"2 2 2 2\nULD\n"
] | [
"1 1 0 1 1 1 1 0 6\n",
"1 1 1 1\n"
] | In the first sample, if we exclude the probable impact of the mines, the robot's route will look like that: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/16bfda1e4f41cc00665c31f0a1d754d68cd9b4ab.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [
{
"input": "3 4 2 2\nUURDRDRL",
"output": "1 1 0 1 1 1 1 0 6"
},
{
"input": "2 2 2 2\nULD",
"output": "1 1 1 1"
},
{
"input": "1 1 1 1\nURDLUURRDDLLURDL",
"output": "1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
},
{
"input": "15 17 8 9\nURRDLUULLDD",
"output": "1 1 1 1 1 1 0 1 1 1 ... | 109 | 0 | 0 | 14,883 | |
15 | Industrial Nim | [
"games"
] | C. Industrial Nim | 2 | 64 | There are *n* stone quarries in Petrograd.
Each quarry owns *m**i* dumpers (1<=β€<=*i*<=β€<=*n*). It is known that the first dumper of the *i*-th quarry has *x**i* stones in it, the second dumper has *x**i*<=+<=1 stones in it, the third has *x**i*<=+<=2, and the *m**i*-th dumper (the last for the *i*-th quarry) has *x**i*<=+<=*m**i*<=-<=1 stones in it.
Two oligarchs play a well-known game Nim. Players take turns removing stones from dumpers. On each turn, a player can select any dumper and remove any non-zero amount of stones from it. The player who cannot take a stone loses.
Your task is to find out which oligarch will win, provided that both of them play optimally. The oligarchs asked you not to reveal their names. So, let's call the one who takes the first stone Β«tolikΒ» and the other one Β«bolikΒ». | The first line of the input contains one integer number *n* (1<=β€<=*n*<=β€<=105) β the amount of quarries. Then there follow *n* lines, each of them contains two space-separated integers *x**i* and *m**i* (1<=β€<=*x**i*,<=*m**i*<=β€<=1016) β the amount of stones in the first dumper of the *i*-th quarry and the number of dumpers at the *i*-th quarry. | Output Β«tolikΒ» if the oligarch who takes a stone first wins, and Β«bolikΒ» otherwise. | [
"2\n2 1\n3 2\n",
"4\n1 1\n1 1\n1 1\n1 1\n"
] | [
"tolik\n",
"bolik\n"
] | none | [
{
"input": "2\n2 1\n3 2",
"output": "tolik"
},
{
"input": "4\n1 1\n1 1\n1 1\n1 1",
"output": "bolik"
},
{
"input": "10\n2 3\n1 4\n5 8\n4 10\n10 8\n7 2\n1 2\n1 7\n4 10\n5 3",
"output": "tolik"
},
{
"input": "20\n8 6\n6 3\n2 9\n7 8\n9 1\n2 4\n3 6\n6 3\n5 6\n5 3\n6 5\n2 10\n2 9\... | 216 | 6,656,000 | 0 | 14,885 |
986 | Oppa Funcan Style Remastered | [
"graphs",
"math",
"number theory",
"shortest paths"
] | null | null | Surely you have seen insane videos by South Korean rapper PSY, such as "Gangnam Style", "Gentleman" and "Daddy". You might also hear that PSY has been recording video "Oppa Funcan Style" two years ago (unfortunately we couldn't find it on the internet). We will remind you what this hit looked like (you can find original description [here](http://acm.timus.ru/problem.aspx?space=1&num=2107&locale=en)):
On the ground there are $n$ platforms, which are numbered with integers from $1$ to $n$, on $i$-th platform there is a dancer with number $i$. Further, every second all the dancers standing on the platform with number $i$ jump to the platform with the number $f(i)$. The moving rule $f$ is selected in advance and is not changed throughout the clip.
The duration of the clip was $k$ seconds and the rule $f$ was chosen in such a way that after $k$ seconds all dancers were in their initial positions (i.e. the $i$-th dancer stood on the platform with the number $i$). That allowed to loop the clip and collect even more likes.
PSY knows that enhanced versions of old artworks become more and more popular every day. So he decided to release a remastered-version of his video.
In his case "enhanced version" means even more insanity, so the number of platforms can be up to $10^{18}$! But the video director said that if some dancer stays on the same platform all the time, then the viewer will get bored and will turn off the video immediately. Therefore, for all $x$ from $1$ to $n$ $f(x) \neq x$ must hold.
Big part of classic video's success was in that looping, so in the remastered version all dancers should return to their initial positions in the end of the clip as well.
PSY hasn't decided on the exact number of platforms and video duration yet, so he asks you to check if there is a good rule $f$ for different options. | In the first line of input there is one integer $t$ ($1 \le t \le 10^{4}$)Β β the number of options for $n$ and $k$ to check.
In the next $t$ lines the options are given: each option is described with two integers $n$ and $k$ ($1 \le n \le 10^{18}$, $1 \le k \le 10^{15}$)Β β the number of dancers and the duration in seconds.
It is guaranteed that the number of different values of $k$ in one test is not greater than $50$. | Print $t$ lines. If the $i$-th option of the video is feasible, print "YES" (without quotes) in $i$-th line, otherwise print "NO" (without quotes). | [
"3\n7 7\n3 8\n5 6\n"
] | [
"YES\nNO\nYES\n"
] | none | [] | 93 | 0 | 0 | 14,887 | |
59 | Shortest Path | [
"graphs",
"shortest paths"
] | E. Shortest Path | 3 | 256 | In Ancient Berland there were *n* cities and *m* two-way roads of equal length. The cities are numbered with integers from 1 to *n* inclusively. According to an ancient superstition, if a traveller visits three cities *a**i*, *b**i*, *c**i* in row, without visiting other cities between them, a great disaster awaits him. Overall there are *k* such city triplets. Each triplet is ordered, which means that, for example, you are allowed to visit the cities in the following order: *a**i*, *c**i*, *b**i*. Vasya wants to get from the city 1 to the city *n* and not fulfil the superstition. Find out which minimal number of roads he should take. Also you are required to find one of his possible path routes. | The first line contains three integers *n*, *m*, *k* (2<=β€<=*n*<=β€<=3000,<=1<=β€<=*m*<=β€<=20000,<=0<=β€<=*k*<=β€<=105) which are the number of cities, the number of roads and the number of the forbidden triplets correspondingly.
Then follow *m* lines each containing two integers *x**i*, *y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=*n*) which are the road descriptions. The road is described by the numbers of the cities it joins. No road joins a city with itself, there cannot be more than one road between a pair of cities.
Then follow *k* lines each containing three integers *a**i*, *b**i*, *c**i* (1<=β€<=*a**i*,<=*b**i*,<=*c**i*<=β€<=*n*) which are the forbidden triplets. Each ordered triplet is listed mo more than one time. All three cities in each triplet are distinct.
City *n* can be unreachable from city 1 by roads. | If there are no path from 1 to *n* print -1. Otherwise on the first line print the number of roads *d* along the shortest path from the city 1 to the city *n*. On the second line print *d*<=+<=1 numbers β any of the possible shortest paths for Vasya. The path should start in the city 1 and end in the city *n*. | [
"4 4 1\n1 2\n2 3\n3 4\n1 3\n1 4 3\n",
"3 1 0\n1 2\n",
"4 4 2\n1 2\n2 3\n3 4\n1 3\n1 2 3\n1 3 4\n"
] | [
"2\n1 3 4\n",
"-1\n",
"4\n1 3 2 3 4\n"
] | none | [
{
"input": "4 4 1\n1 2\n2 3\n3 4\n1 3\n1 4 3",
"output": "2\n1 3 4"
},
{
"input": "3 1 0\n1 2",
"output": "-1"
},
{
"input": "4 4 2\n1 2\n2 3\n3 4\n1 3\n1 2 3\n1 3 4",
"output": "4\n1 3 2 3 4"
},
{
"input": "4 4 1\n1 2\n2 3\n3 4\n1 3\n1 2 3",
"output": "2\n1 3 4"
},
{... | 3,000 | 21,401,600 | 0 | 14,916 |
894 | Ralph And His Magic Field | [
"combinatorics",
"constructive algorithms",
"math",
"number theory"
] | null | null | Ralph has a magic field which is divided into *n*<=Γ<=*m* blocks. That is to say, there are *n* rows and *m* columns on the field. Ralph can put an integer in each block. However, the magic field doesn't always work properly. It works only if the product of integers in each row and each column equals to *k*, where *k* is either 1 or -1.
Now Ralph wants you to figure out the number of ways to put numbers in each block in such a way that the magic field works properly. Two ways are considered different if and only if there exists at least one block where the numbers in the first way and in the second way are different. You are asked to output the answer modulo 1000000007<==<=109<=+<=7.
Note that there is no range of the numbers to put in the blocks, but we can prove that the answer is not infinity. | The only line contains three integers *n*, *m* and *k* (1<=β€<=*n*,<=*m*<=β€<=1018, *k* is either 1 or -1). | Print a single number denoting the answer modulo 1000000007. | [
"1 1 -1\n",
"1 3 1\n",
"3 3 -1\n"
] | [
"1\n",
"1\n",
"16\n"
] | In the first example the only way is to put -1 into the only block.
In the second example the only way is to put 1 into every block. | [
{
"input": "1 1 -1",
"output": "1"
},
{
"input": "1 3 1",
"output": "1"
},
{
"input": "3 3 -1",
"output": "16"
},
{
"input": "2 7 1",
"output": "64"
},
{
"input": "1 1 1",
"output": "1"
},
{
"input": "2 4 -1",
"output": "8"
},
{
"input": "1... | 62 | 0 | 0 | 14,937 | |
527 | Error Correct System | [
"greedy"
] | null | null | Ford Prefect got a job as a web developer for a small company that makes towels. His current work task is to create a search engine for the website of the company. During the development process, he needs to write a subroutine for comparing strings *S* and *T* of equal length to be "similar". After a brief search on the Internet, he learned about the Hamming distance between two strings *S* and *T* of the same length, which is defined as the number of positions in which *S* and *T* have different characters. For example, the Hamming distance between words "permanent" and "pergament" is two, as these words differ in the fourth and sixth letters.
Moreover, as he was searching for information, he also noticed that modern search engines have powerful mechanisms to correct errors in the request to improve the quality of search. Ford doesn't know much about human beings, so he assumed that the most common mistake in a request is swapping two arbitrary letters of the string (not necessarily adjacent). Now he wants to write a function that determines which two letters should be swapped in string *S*, so that the Hamming distance between a new string *S* and string *T* would be as small as possible, or otherwise, determine that such a replacement cannot reduce the distance between the strings.
Help him do this! | The first line contains integer *n* (1<=β€<=*n*<=β€<=200<=000) β the length of strings *S* and *T*.
The second line contains string *S*.
The third line contains string *T*.
Each of the lines only contains lowercase Latin letters. | In the first line, print number *x* β the minimum possible Hamming distance between strings *S* and *T* if you swap at most one pair of letters in *S*.
In the second line, either print the indexes *i* and *j* (1<=β€<=*i*,<=*j*<=β€<=*n*, *i*<=β <=*j*), if reaching the minimum possible distance is possible by swapping letters on positions *i* and *j*, or print "-1 -1", if it is not necessary to swap characters.
If there are multiple possible answers, print any of them. | [
"9\npergament\npermanent\n",
"6\nwookie\ncookie\n",
"4\npetr\negor\n",
"6\ndouble\nbundle\n"
] | [
"1\n4 6\n",
"1\n-1 -1\n",
"2\n1 2\n",
"2\n4 1\n"
] | In the second test it is acceptable to print *i*β=β2, *j*β=β3. | [
{
"input": "9\npergament\npermanent",
"output": "1\n4 6"
},
{
"input": "6\nwookie\ncookie",
"output": "1\n-1 -1"
},
{
"input": "4\npetr\negor",
"output": "2\n1 2"
},
{
"input": "6\ndouble\nbundle",
"output": "2\n4 1"
},
{
"input": "1\na\na",
"output": "0\n-1 -... | 499 | 1,126,400 | 3 | 14,961 | |
567 | One-Dimensional Battle Ships | [
"binary search",
"data structures",
"greedy",
"sortings"
] | null | null | Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting of *n* square cells (that is, on a 1<=Γ<=*n* table).
At the beginning of the game Alice puts *k* ships on the field without telling their positions to Bob. Each ship looks as a 1<=Γ<=*a* rectangle (that is, it occupies a sequence of *a* consecutive squares of the field). The ships cannot intersect and even touch each other.
After that Bob makes a sequence of "shots". He names cells of the field and Alice either says that the cell is empty ("miss"), or that the cell belongs to some ship ("hit").
But here's the problem! Alice like to cheat. May be that is why she responds to each Bob's move with a "miss".
Help Bob catch Alice cheating β find Bob's first move, such that after it you can be sure that Alice cheated. | The first line of the input contains three integers: *n*, *k* and *a* (1<=β€<=*n*,<=*k*,<=*a*<=β€<=2Β·105) β the size of the field, the number of the ships and the size of each ship. It is guaranteed that the *n*, *k* and *a* are such that you can put *k* ships of size *a* on the field, so that no two ships intersect or touch each other.
The second line contains integer *m* (1<=β€<=*m*<=β€<=*n*) β the number of Bob's moves.
The third line contains *m* distinct integers *x*1,<=*x*2,<=...,<=*x**m*, where *x**i* is the number of the cell where Bob made the *i*-th shot. The cells are numbered from left to right from 1 to *n*. | Print a single integer β the number of such Bob's first move, after which you can be sure that Alice lied. Bob's moves are numbered from 1 to *m* in the order the were made. If the sought move doesn't exist, then print "-1". | [
"11 3 3\n5\n4 8 6 1 11\n",
"5 1 3\n2\n1 5\n",
"5 1 3\n1\n3\n"
] | [
"3\n",
"-1\n",
"1\n"
] | none | [
{
"input": "11 3 3\n5\n4 8 6 1 11",
"output": "3"
},
{
"input": "5 1 3\n2\n1 5",
"output": "-1"
},
{
"input": "5 1 3\n1\n3",
"output": "1"
},
{
"input": "1 1 1\n1\n1",
"output": "1"
},
{
"input": "5000 1660 2\n20\n1 100 18 102 300 81 19 25 44 88 1337 4999 1054 120... | 92 | 0 | 0 | 15,025 | |
621 | Wet Shark and Flowers | [
"combinatorics",
"math",
"number theory",
"probabilities"
] | null | null | There are *n* sharks who grow flowers for Wet Shark. They are all sitting around the table, such that sharks *i* and *i*<=+<=1 are neighbours for all *i* from 1 to *n*<=-<=1. Sharks *n* and 1 are neighbours too.
Each shark will grow some number of flowers *s**i*. For *i*-th shark value *s**i* is random integer equiprobably chosen in range from *l**i* to *r**i*. Wet Shark has it's favourite prime number *p*, and he really likes it! If for any pair of neighbouring sharks *i* and *j* the product *s**i*Β·*s**j* is divisible by *p*, then Wet Shark becomes happy and gives 1000 dollars to each of these sharks.
At the end of the day sharks sum all the money Wet Shark granted to them. Find the expectation of this value. | The first line of the input contains two space-separated integers *n* and *p* (3<=β€<=*n*<=β€<=100<=000,<=2<=β€<=*p*<=β€<=109)Β β the number of sharks and Wet Shark's favourite prime number. It is guaranteed that *p* is prime.
The *i*-th of the following *n* lines contains information about *i*-th sharkΒ β two space-separated integers *l**i* and *r**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=109), the range of flowers shark *i* can produce. Remember that *s**i* is chosen equiprobably among all integers from *l**i* to *r**i*, inclusive. | Print a single real number β the expected number of dollars that the sharks receive in total. You answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6.
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 . | [
"3 2\n1 2\n420 421\n420420 420421\n",
"3 5\n1 4\n2 3\n11 14\n"
] | [
"4500.0\n",
"0.0\n"
] | A prime number is a positive integer number that is divisible only by 1 and itself. 1 is not considered to be prime.
Consider the first sample. First shark grows some number of flowers from 1 to 2, second sharks grows from 420 to 421 flowers and third from 420420 to 420421. There are eight cases for the quantities of flowers (*s*<sub class="lower-index">0</sub>,β*s*<sub class="lower-index">1</sub>,β*s*<sub class="lower-index">2</sub>) each shark grows:
1. (1,β420,β420420): note that *s*<sub class="lower-index">0</sub>Β·*s*<sub class="lower-index">1</sub>β=β420, *s*<sub class="lower-index">1</sub>Β·*s*<sub class="lower-index">2</sub>β=β176576400, and *s*<sub class="lower-index">2</sub>Β·*s*<sub class="lower-index">0</sub>β=β420420. For each pair, 1000 dollars will be awarded to each shark. Therefore, each shark will be awarded 2000 dollars, for a total of 6000 dollars.1. (1,β420,β420421): now, the product *s*<sub class="lower-index">2</sub>Β·*s*<sub class="lower-index">0</sub> is not divisible by 2. Therefore, sharks *s*<sub class="lower-index">0</sub> and *s*<sub class="lower-index">2</sub> will receive 1000 dollars, while shark *s*<sub class="lower-index">1</sub> will receive 2000. The total is 4000.1. (1,β421,β420420): total is 4000 1. (1,β421,β420421): total is 0. 1. (2,β420,β420420): total is 6000. 1. (2,β420,β420421): total is 6000. 1. (2,β421,β420420): total is 6000. 1. (2,β421,β420421): total is 4000.
The expected value is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/dfe520d00a8615f7c270ccbccbebe182cc7db883.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the second sample, no combination of quantities will garner the sharks any money. | [
{
"input": "3 2\n1 2\n420 421\n420420 420421",
"output": "4500.0"
},
{
"input": "3 5\n1 4\n2 3\n11 14",
"output": "0.0"
},
{
"input": "3 3\n3 3\n2 4\n1 1",
"output": "4666.666666666667"
},
{
"input": "5 5\n5 204\n420 469\n417 480\n442 443\n44 46",
"output": "3451.25"
},... | 327 | 7,475,200 | 3 | 15,086 | |
831 | Jury Marks | [
"brute force",
"constructive algorithms"
] | null | null | Polycarp watched TV-show where *k* jury members one by one rated a participant by adding him a certain number of points (may be negative, i.Β e. points were subtracted). Initially the participant had some score, and each the marks were one by one added to his score. It is known that the *i*-th jury member gave *a**i* points.
Polycarp does not remember how many points the participant had before this *k* marks were given, but he remembers that among the scores announced after each of the *k* judges rated the participant there were *n* (*n*<=β€<=*k*) values *b*1,<=*b*2,<=...,<=*b**n* (it is guaranteed that all values *b**j* are distinct). It is possible that Polycarp remembers not all of the scores announced, i.Β e. *n*<=<<=*k*. Note that the initial score wasn't announced.
Your task is to determine the number of options for the score the participant could have before the judges rated the participant. | The first line contains two integers *k* and *n* (1<=β€<=*n*<=β€<=*k*<=β€<=2<=000) β the number of jury members and the number of scores Polycarp remembers.
The second line contains *k* integers *a*1,<=*a*2,<=...,<=*a**k* (<=-<=2<=000<=β€<=*a**i*<=β€<=2<=000) β jury's marks in chronological order.
The third line contains *n* distinct integers *b*1,<=*b*2,<=...,<=*b**n* (<=-<=4<=000<=000<=β€<=*b**j*<=β€<=4<=000<=000) β the values of points Polycarp remembers. Note that these values are not necessarily given in chronological order. | Print the number of options for the score the participant could have before the judges rated the participant. If Polycarp messes something up and there is no options, print "0" (without quotes). | [
"4 1\n-5 5 0 20\n10\n",
"2 2\n-2000 -2000\n3998000 4000000\n"
] | [
"3\n",
"1\n"
] | The answer for the first example is 3 because initially the participant could have β-β10, 10 or 15 points.
In the second example there is only one correct initial score equaling to 4β002β000. | [
{
"input": "4 1\n-5 5 0 20\n10",
"output": "3"
},
{
"input": "2 2\n-2000 -2000\n3998000 4000000",
"output": "1"
},
{
"input": "1 1\n-577\n1273042",
"output": "1"
},
{
"input": "2 1\n614 -1943\n3874445",
"output": "2"
},
{
"input": "3 1\n1416 -1483 1844\n3261895",
... | 170 | 138,342,400 | 3 | 15,112 | |
762 | Tree nesting | [
"combinatorics",
"graphs",
"trees"
] | null | null | You are given two trees (connected undirected acyclic graphs) *S* and *T*.
Count the number of subtrees (connected subgraphs) of *S* that are isomorphic to tree *T*. Since this number can get quite large, output it modulo 109<=+<=7.
Two subtrees of tree *S* are considered different, if there exists a vertex in *S* that belongs to exactly one of them.
Tree *G* is called isomorphic to tree *H* if there exists a bijection *f* from the set of vertices of *G* to the set of vertices of *H* that has the following property: if there is an edge between vertices *A* and *B* in tree *G*, then there must be an edge between vertices *f*(*A*) and *f*(*B*) in tree *H*. And vice versaΒ β if there is an edge between vertices *A* and *B* in tree *H*, there must be an edge between *f*<=-<=1(*A*) and *f*<=-<=1(*B*) in tree *G*. | The first line contains a single integer |*S*| (1<=β€<=|*S*|<=β€<=1000) β the number of vertices of tree *S*.
Next |*S*|<=-<=1 lines contain two integers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=|*S*|) and describe edges of tree *S*.
The next line contains a single integer |*T*| (1<=β€<=|*T*|<=β€<=12) β the number of vertices of tree *T*.
Next |*T*|<=-<=1 lines contain two integers *x**i* and *y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=|*T*|) and describe edges of tree *T*. | On the first line output a single integer β the answer to the given task modulo 109<=+<=7. | [
"5\n1 2\n2 3\n3 4\n4 5\n3\n1 2\n2 3\n",
"3\n2 3\n3 1\n3\n1 2\n1 3\n",
"7\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n4\n4 1\n4 2\n4 3\n",
"5\n1 2\n2 3\n3 4\n4 5\n4\n4 1\n4 2\n4 3\n"
] | [
"3\n",
"1\n",
"20\n",
"0\n"
] | none | [
{
"input": "5\n1 2\n2 3\n3 4\n4 5\n3\n1 2\n2 3",
"output": "3"
},
{
"input": "3\n2 3\n3 1\n3\n1 2\n1 3",
"output": "1"
},
{
"input": "7\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n4\n4 1\n4 2\n4 3",
"output": "20"
},
{
"input": "5\n1 2\n2 3\n3 4\n4 5\n4\n4 1\n4 2\n4 3",
"output": "0"
... | 61 | 2,048,000 | 0 | 15,123 | |
420 | Online Meeting | [
"implementation"
] | null | null | Nearly each project of the F company has a whole team of developers working on it. They often are in different rooms of the office in different cities and even countries. To keep in touch and track the results of the project, the F company conducts shared online meetings in a Spyke chat.
One day the director of the F company got hold of the records of a part of an online meeting of one successful team. The director watched the record and wanted to talk to the team leader. But how can he tell who the leader is? The director logically supposed that the leader is the person who is present at any conversation during a chat meeting. In other words, if at some moment of time at least one person is present on the meeting, then the leader is present on the meeting.
You are the assistant director. Given the 'user logged on'/'user logged off' messages of the meeting in the chronological order, help the director determine who can be the leader. Note that the director has the record of only a continuous part of the meeting (probably, it's not the whole meeting). | The first line contains integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=105) β the number of team participants and the number of messages. Each of the next *m* lines contains a message in the format:
- '+ *id*': the record means that the person with number *id* (1<=β€<=*id*<=β€<=*n*) has logged on to the meeting. - '- *id*': the record means that the person with number *id* (1<=β€<=*id*<=β€<=*n*) has logged off from the meeting.
Assume that all the people of the team are numbered from 1 to *n* and the messages are given in the chronological order. It is guaranteed that the given sequence is the correct record of a continuous part of the meeting. It is guaranteed that no two log on/log off events occurred simultaneously. | In the first line print integer *k* (0<=β€<=*k*<=β€<=*n*) β how many people can be leaders. In the next line, print *k* integers in the increasing order β the numbers of the people who can be leaders.
If the data is such that no member of the team can be a leader, print a single number 0. | [
"5 4\n+ 1\n+ 2\n- 2\n- 1\n",
"3 2\n+ 1\n- 2\n",
"2 4\n+ 1\n- 1\n+ 2\n- 2\n",
"5 6\n+ 1\n- 1\n- 3\n+ 3\n+ 4\n- 4\n",
"2 4\n+ 1\n- 2\n+ 2\n- 1\n"
] | [
"4\n1 3 4 5 ",
"1\n3 ",
"0\n",
"3\n2 3 5 ",
"0\n"
] | none | [
{
"input": "5 4\n+ 1\n+ 2\n- 2\n- 1",
"output": "4\n1 3 4 5 "
},
{
"input": "3 2\n+ 1\n- 2",
"output": "1\n3 "
},
{
"input": "2 4\n+ 1\n- 1\n+ 2\n- 2",
"output": "0"
},
{
"input": "5 6\n+ 1\n- 1\n- 3\n+ 3\n+ 4\n- 4",
"output": "3\n2 3 5 "
},
{
"input": "2 4\n+ 1\n... | 124 | 0 | 0 | 15,202 | |
949 | A Leapfrog in the Array | [
"constructive algorithms",
"math"
] | null | null | Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm.
Let's consider that initially array contains *n* numbers from 1 to *n* and the number *i* is located in the cell with the index 2*i*<=-<=1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all *n* numbers will appear in the first *n* cells of the array. For example if *n*<==<=4, the array is changing as follows:
You have to write a program that allows you to determine what number will be in the cell with index *x* (1<=β€<=*x*<=β€<=*n*) after Dima's algorithm finishes. | The first line contains two integers *n* and *q* (1<=β€<=*n*<=β€<=1018, 1<=β€<=*q*<=β€<=200<=000), the number of elements in the array and the number of queries for which it is needed to find the answer.
Next *q* lines contain integers *x**i* (1<=β€<=*x**i*<=β€<=*n*), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes. | For each of *q* queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes. | [
"4 3\n2\n3\n4\n",
"13 4\n10\n5\n4\n8\n"
] | [
"3\n2\n4\n",
"13\n3\n8\n9\n"
] | The first example is shown in the picture.
In the second example the final array is [1,β12,β2,β8,β3,β11,β4,β9,β5,β13,β6,β10,β7]. | [
{
"input": "4 3\n2\n3\n4",
"output": "3\n2\n4"
},
{
"input": "13 4\n10\n5\n4\n8",
"output": "13\n3\n8\n9"
},
{
"input": "2 2\n1\n2",
"output": "1\n2"
},
{
"input": "1 1\n1",
"output": "1"
},
{
"input": "3 3\n3\n2\n1",
"output": "2\n3\n1"
},
{
"input": ... | 2,000 | 5,632,000 | 0 | 15,218 | |
430 | Balls Game | [
"brute force",
"two pointers"
] | null | null | Iahub is training for the IOI. What is a better way to train than playing a Zuma-like game?
There are *n* balls put in a row. Each ball is colored in one of *k* colors. Initially the row doesn't contain three or more contiguous balls with the same color. Iahub has a single ball of color *x*. He can insert his ball at any position in the row (probably, between two other balls). If at any moment there are three or more contiguous balls of the same color in the row, they are destroyed immediately. This rule is applied multiple times, until there are no more sets of 3 or more contiguous balls of the same color.
For example, if Iahub has the row of balls [black, black, white, white, black, black] and a white ball, he can insert the ball between two white balls. Thus three white balls are destroyed, and then four black balls become contiguous, so all four balls are destroyed. The row will not contain any ball in the end, so Iahub can destroy all 6 balls.
Iahub wants to destroy as many balls as possible. You are given the description of the row of balls, and the color of Iahub's ball. Help Iahub train for the IOI by telling him the maximum number of balls from the row he can destroy. | The first line of input contains three integers: *n* (1<=β€<=*n*<=β€<=100), *k* (1<=β€<=*k*<=β€<=100) and *x* (1<=β€<=*x*<=β€<=*k*). The next line contains *n* space-separated integers *c*1,<=*c*2,<=...,<=*c**n* (1<=β€<=*c**i*<=β€<=*k*). Number *c**i* means that the *i*-th ball in the row has color *c**i*.
It is guaranteed that the initial row of balls will never contain three or more contiguous balls of the same color. | Print a single integer β the maximum number of balls Iahub can destroy. | [
"6 2 2\n1 1 2 2 1 1\n",
"1 1 1\n1\n"
] | [
"6\n",
"0\n"
] | none | [
{
"input": "6 2 2\n1 1 2 2 1 1",
"output": "6"
},
{
"input": "1 1 1\n1",
"output": "0"
},
{
"input": "10 2 1\n2 1 2 2 1 2 2 1 1 2",
"output": "5"
},
{
"input": "50 2 1\n1 1 2 2 1 2 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 2 1 2 1 2 1 2 2 1 1 2 2 1 1 2 2 1 2 1 1 2 1 1 2 2 1 1 2",
"... | 109 | 0 | 0 | 15,301 | |
900 | Unusual Sequences | [
"bitmasks",
"combinatorics",
"dp",
"math",
"number theory"
] | null | null | Count the number of distinct sequences *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*) consisting of positive integers such that *gcd*(*a*1,<=*a*2,<=...,<=*a**n*)<==<=*x* and . As this number could be large, print the answer modulo 109<=+<=7.
*gcd* here means the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor). | The only line contains two positive integers *x* and *y* (1<=β€<=*x*,<=*y*<=β€<=109). | Print the number of such sequences modulo 109<=+<=7. | [
"3 9\n",
"5 8\n"
] | [
"3\n",
"0\n"
] | There are three suitable sequences in the first test: (3,β3,β3), (3,β6), (6,β3).
There are no suitable sequences in the second test. | [
{
"input": "3 9",
"output": "3"
},
{
"input": "5 8",
"output": "0"
},
{
"input": "2 12",
"output": "27"
},
{
"input": "1 8",
"output": "120"
},
{
"input": "1 9",
"output": "252"
},
{
"input": "1000000000 1000000000",
"output": "1"
},
{
"inp... | 93 | 0 | 0 | 15,324 | |
963 | Destruction of a Tree | [
"constructive algorithms",
"dfs and similar",
"dp",
"greedy",
"trees"
] | null | null | You are given a tree (a graph with *n* vertices and *n*<=-<=1 edges in which it's possible to reach any vertex from any other vertex using only its edges).
A vertex can be destroyed if this vertex has even degree. If you destroy a vertex, all edges connected to it are also deleted.
Destroy all vertices in the given tree or determine that it is impossible. | The first line contains integer *n* (1<=β€<=*n*<=β€<=2Β·105)Β β number of vertices in a tree.
The second line contains *n* integers *p*1,<=*p*2,<=...,<=*p**n* (0<=β€<=*p**i*<=β€<=*n*). If *p**i*<=β <=0 there is an edge between vertices *i* and *p**i*. It is guaranteed that the given graph is a tree. | If it's possible to destroy all vertices, print "YES" (without quotes), otherwise print "NO" (without quotes).
If it's possible to destroy all vertices, in the next *n* lines print the indices of the vertices in order you destroy them. If there are multiple correct answers, print any. | [
"5\n0 1 2 1 2\n",
"4\n0 1 2 3\n"
] | [
"YES\n1\n2\n3\n5\n4\n",
"NO\n"
] | In the first example at first you have to remove the vertex with index 1 (after that, the edges (1, 2) and (1, 4) are removed), then the vertex with index 2 (and edges (2, 3) and (2, 5) are removed). After that there are no edges in the tree, so you can remove remaining vertices in any order. | [
{
"input": "5\n0 1 2 1 2",
"output": "YES\n1\n2\n3\n5\n4"
},
{
"input": "4\n0 1 2 3",
"output": "NO"
},
{
"input": "1\n0",
"output": "YES\n1"
},
{
"input": "8\n3 1 4 0 4 2 4 5",
"output": "NO"
},
{
"input": "100\n81 96 65 28 4 40 5 49 5 89 48 70 94 70 17 58 58 1 6... | 46 | 0 | 0 | 15,402 | |
533 | Berland Miners | [
"binary search",
"data structures",
"dfs and similar",
"greedy",
"trees"
] | null | null | The biggest gold mine in Berland consists of *n* caves, connected by *n*<=-<=1 transitions. The entrance to the mine leads to the cave number 1, it is possible to go from it to any remaining cave of the mine by moving along the transitions.
The mine is being developed by the InMine Inc., *k* miners work for it. Each day the corporation sorts miners into caves so that each cave has at most one miner working there.
For each cave we know the height of its ceiling *h**i* in meters, and for each miner we know his height *s**j*, also in meters. If a miner's height doesn't exceed the height of the cave ceiling where he is, then he can stand there comfortably, otherwise, he has to stoop and that makes him unhappy.
Unfortunately, miners typically go on strike in Berland, so InMine makes all the possible effort to make miners happy about their work conditions. To ensure that no miner goes on strike, you need make sure that no miner has to stoop at any moment on his way from the entrance to the mine to his cave (in particular, he must be able to stand comfortably in the cave where he works).
To reach this goal, you can choose exactly one cave and increase the height of its ceiling by several meters. However enlarging a cave is an expensive and complex procedure. That's why InMine Inc. asks you either to determine the minimum number of meters you should raise the ceiling of some cave so that it is be possible to sort the miners into the caves and keep all miners happy with their working conditions or to determine that it is impossible to achieve by raising ceiling in exactly one cave. | The first line contains integer *n* (1<=β€<=*n*<=β€<=5Β·105) β the number of caves in the mine.
Then follows a line consisting of *n* positive integers *h*1,<=*h*2,<=...,<=*h**n* (1<=β€<=*h**i*<=β€<=109), where *h**i* is the height of the ceiling in the *i*-th cave.
Next *n*<=-<=1 lines contain the descriptions of transitions between the caves. Each line has the form *a**i*,<=*b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*, *a**i*<=β <=*b**i*), where *a**i* and *b**i* are the numbers of the caves connected by a path.
The next line contains integer *k* (1<=β€<=*k*<=β€<=*n*).
The last line contains *k* integers *s*1,<=*s*2,<=...,<=*s**k* (1<=β€<=*s**j*<=β€<=109), where *s**j* is the *j*-th miner's height. | In the single line print the minimum number of meters that you need to raise the ceiling by in some cave so that all miners could be sorted into caves and be happy about the work conditions. If it is impossible to do, print <=-<=1. If it is initially possible and there's no need to raise any ceiling, print 0. | [
"6\n5 8 4 6 3 12\n1 2\n1 3\n4 2\n2 5\n6 3\n6\n7 4 2 5 3 11\n",
"7\n10 14 7 12 4 50 1\n1 2\n2 3\n2 4\n5 1\n6 5\n1 7\n6\n7 3 4 8 8 10\n",
"3\n4 2 8\n1 2\n1 3\n2\n17 15\n"
] | [
"6\n",
"0\n",
"-1\n"
] | In the first sample test we should increase ceiling height in the first cave from 5 to 11. After that we can distribute miners as following (first goes index of a miner, then index of a cave): <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5d9b256bdaa3f5b0f9a3fc3b9f56256306a7a570.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the second sample test there is no need to do anything since it is already possible to distribute miners as following: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ba4a2592a4bea5feafeae1ab8dec98663bf2c557.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the third sample test it is impossible. | [] | 46 | 0 | 0 | 15,480 | |
846 | Monitor | [
"binary search",
"data structures"
] | null | null | Recently Luba bought a monitor. Monitor is a rectangular matrix of size *n*<=Γ<=*m*. But then she started to notice that some pixels cease to work properly. Luba thinks that the monitor will become broken the first moment when it contains a square *k*<=Γ<=*k* consisting entirely of broken pixels. She knows that *q* pixels are already broken, and for each of them she knows the moment when it stopped working. Help Luba to determine when the monitor became broken (or tell that it's still not broken even after all *q* pixels stopped working). | The first line contains four integer numbers *n*,<=*m*,<=*k*,<=*q*Β (1<=β€<=*n*,<=*m*<=β€<=500,<=1<=β€<=*k*<=β€<=*min*(*n*,<=*m*),<=0<=β€<=*q*<=β€<=*n*Β·*m*) β the length and width of the monitor, the size of a rectangle such that the monitor is broken if there is a broken rectangle with this size, and the number of broken pixels.
Each of next *q* lines contain three integer numbers *x**i*,<=*y**i*,<=*t**i*Β (1<=β€<=*x**i*<=β€<=*n*,<=1<=β€<=*y**i*<=β€<=*m*,<=0<=β€<=*t*<=β€<=109) β coordinates of *i*-th broken pixel (its row and column in matrix) and the moment it stopped working. Each pixel is listed at most once.
We consider that pixel is already broken at moment *t**i*. | Print one number β the minimum moment the monitor became broken, or "-1" if it's still not broken after these *q* pixels stopped working. | [
"2 3 2 5\n2 1 8\n2 2 8\n1 2 1\n1 3 4\n2 3 2\n",
"3 3 2 5\n1 2 2\n2 2 1\n2 3 5\n3 2 10\n2 1 100\n"
] | [
"8\n",
"-1\n"
] | none | [
{
"input": "2 3 2 5\n2 1 8\n2 2 8\n1 2 1\n1 3 4\n2 3 2",
"output": "8"
},
{
"input": "3 3 2 5\n1 2 2\n2 2 1\n2 3 5\n3 2 10\n2 1 100",
"output": "-1"
},
{
"input": "29 50 5 29\n21 42 1565821\n21 43 53275635\n21 44 2717830\n21 45 9579585\n21 46 20725775\n22 42 2568372\n22 43 9584662\n22 44... | 2,000 | 5,734,400 | 0 | 15,482 | |
203 | Game on Paper | [
"brute force",
"implementation"
] | null | null | One not particularly beautiful evening Valera got very bored. To amuse himself a little bit, he found the following game.
He took a checkered white square piece of paper, consisting of *n*<=Γ<=*n* cells. After that, he started to paint the white cells black one after the other. In total he painted *m* different cells on the piece of paper. Since Valera was keen on everything square, he wondered, how many moves (i.e. times the boy paints a square black) he should make till a black square with side 3 can be found on the piece of paper. But Valera does not know the answer to this question, so he asks you to help him.
Your task is to find the minimum number of moves, till the checkered piece of paper has at least one black square with side of 3. Otherwise determine that such move does not exist. | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=1000, 1<=β€<=*m*<=β€<=*min*(*n*Β·*n*,<=105)) β the size of the squared piece of paper and the number of moves, correspondingly.
Then, *m* lines contain the description of the moves. The *i*-th line contains two integers *x**i*, *y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=*n*) β the number of row and column of the square that gets painted on the *i*-th move.
All numbers on the lines are separated by single spaces. It is guaranteed that all moves are different. The moves are numbered starting from 1 in the order, in which they are given in the input. The columns of the squared piece of paper are numbered starting from 1, from the left to the right. The rows of the squared piece of paper are numbered starting from 1, from top to bottom. | On a single line print the answer to the problem β the minimum number of the move after which the piece of paper has a black square with side 3. If no such move exists, print -1. | [
"4 11\n1 1\n1 2\n1 3\n2 2\n2 3\n1 4\n2 4\n3 4\n3 2\n3 3\n4 1\n",
"4 12\n1 1\n1 2\n1 3\n2 2\n2 3\n1 4\n2 4\n3 4\n3 2\n4 2\n4 1\n3 1\n"
] | [
"10\n",
"-1\n"
] | none | [
{
"input": "4 11\n1 1\n1 2\n1 3\n2 2\n2 3\n1 4\n2 4\n3 4\n3 2\n3 3\n4 1",
"output": "10"
},
{
"input": "4 12\n1 1\n1 2\n1 3\n2 2\n2 3\n1 4\n2 4\n3 4\n3 2\n4 2\n4 1\n3 1",
"output": "-1"
},
{
"input": "3 1\n1 3",
"output": "-1"
},
{
"input": "3 8\n1 3\n3 3\n2 2\n3 2\n1 1\n1 2\... | 1,964 | 4,812,800 | 3 | 15,527 | |
195 | Try and Catch | [
"expression parsing",
"implementation"
] | null | null | Vasya is developing his own programming language VPL (Vasya Programming Language). Right now he is busy making the system of exceptions. He thinks that the system of exceptions must function like that.
The exceptions are processed by try-catch-blocks. There are two operators that work with the blocks:
1. The try operator. It opens a new try-catch-block. 1. The catch(<exception_type>, <message>) operator. It closes the try-catch-block that was started last and haven't yet been closed. This block can be activated only via exception of type <exception_type>. When we activate this block, the screen displays the <message>. If at the given moment there is no open try-catch-block, then we can't use the catch operator.
The exceptions can occur in the program in only one case: when we use the throw operator. The throw(<exception_type>) operator creates the exception of the given type.
Let's suggest that as a result of using some throw operator the program created an exception of type *a*. In this case a try-catch-block is activated, such that this block's try operator was described in the program earlier than the used throw operator. Also, this block's catch operator was given an exception type *a* as a parameter and this block's catch operator is described later that the used throw operator. If there are several such try-catch-blocks, then the system activates the block whose catch operator occurs earlier than others. If no try-catch-block was activated, then the screen displays message "Unhandled Exception".
To test the system, Vasya wrote a program that contains only try, catch and throw operators, one line contains no more than one operator, the whole program contains exactly one throw operator.
Your task is: given a program in VPL, determine, what message will be displayed on the screen. | The first line contains a single integer: *n* (1<=β€<=*n*<=β€<=105) the number of lines in the program. Next *n* lines contain the program in language VPL. Each line contains no more than one operator. It means that input file can contain empty lines and lines, consisting only of spaces.
The program contains only operators try, catch and throw. It is guaranteed that the program is correct. It means that each started try-catch-block was closed, the catch operators aren't used unless there is an open try-catch-block. The program has exactly one throw operator. The program may have spaces at the beginning of a line, at the end of a line, before and after a bracket, a comma or a quote mark.
The exception type is a nonempty string, that consists only of upper and lower case english letters. The length of the string does not exceed 20 symbols. Message is a nonempty string, that consists only of upper and lower case english letters, digits and spaces. Message is surrounded with quote marks. Quote marks shouldn't be printed. The length of the string does not exceed 20 symbols.
Length of any line in the input file does not exceed 50 symbols. | Print the message the screen will show after the given program is executed. | [
"8\ntry\n try\n throw ( AE ) \n catch ( BE, \"BE in line 3\")\n\n try\n catch(AE, \"AE in line 5\") \ncatch(AE,\"AE somewhere\")\n",
"8\ntry\n try\n throw ( AE ) \n catch ( AE, \"AE in line 3\")\n\n try\n catch(BE, \"BE in line 5\") \ncatch(AE,\"AE somewhere\")\n",
"8\ntry\... | [
"AE somewhere\n",
"AE in line 3\n",
"Unhandled Exception\n"
] | In the first sample there are 2 try-catch-blocks such that try operator is described earlier than throw operator and catch operator is described later than throw operator: try-catch(BE,"BE in line 3") and try-catch(AE,"AE somewhere"). Exception type is AE, so the second block will be activated, because operator catch(AE,"AE somewhere") has exception type AE as parameter and operator catch(BE,"BE in line 3") has exception type BE.
In the second sample there are 2 try-catch-blocks such that try operator is described earlier than throw operator and catch operator is described later than throw operator: try-catch(AE,"AE in line 3") and try-catch(AE,"AE somewhere"). Exception type is AE, so both blocks can be activated, but only the first one will be activated, because operator catch(AE,"AE in line 3") is described earlier than catch(AE,"AE somewhere")
In the third sample there is no blocks that can be activated by an exception of type CE. | [
{
"input": "8\ntry\n try\n throw ( AE ) \n catch ( BE, \"BE in line 3\")\n\n try\n catch(AE, \"AE in line 5\") \ncatch(AE,\"AE somewhere\")",
"output": "AE somewhere"
},
{
"input": "8\ntry\n try\n throw ( AE ) \n catch ( AE, \"AE in line 3\")\n\n try\n catch(BE,... | 1,278 | 10,240,000 | 0 | 15,549 | |
792 | Paths in a Complete Binary Tree | [
"bitmasks",
"trees"
] | null | null | *T* is a complete binary tree consisting of *n* vertices. It means that exactly one vertex is a root, and each vertex is either a leaf (and doesn't have children) or an inner node (and has exactly two children). All leaves of a complete binary tree have the same depth (distance from the root). So *n* is a number such that *n*<=+<=1 is a power of 2.
In the picture you can see a complete binary tree with *n*<==<=15.
Vertices are numbered from 1 to *n* in a special recursive way: we recursively assign numbers to all vertices from the left subtree (if current vertex is not a leaf), then assign a number to the current vertex, and then recursively assign numbers to all vertices from the right subtree (if it exists). In the picture vertices are numbered exactly using this algorithm. It is clear that for each size of a complete binary tree exists exactly one way to give numbers to all vertices. This way of numbering is called symmetric.
You have to write a program that for given *n* answers *q* queries to the tree.
Each query consists of an integer number *u**i* (1<=β€<=*u**i*<=β€<=*n*) and a string *s**i*, where *u**i* is the number of vertex, and *s**i* represents the path starting from this vertex. String *s**i* doesn't contain any characters other than 'L', 'R' and 'U', which mean traverse to the left child, to the right child and to the parent, respectively. Characters from *s**i* have to be processed from left to right, considering that *u**i* is the vertex where the path starts. If it's impossible to process a character (for example, to go to the left child of a leaf), then you have to skip it. The answer is the number of vertex where the path represented by *s**i* ends.
For example, if *u**i*<==<=4 and *s**i*<==<=Β«UURLΒ», then the answer is 10. | The first line contains two integer numbers *n* and *q* (1<=β€<=*n*<=β€<=1018, *q*<=β₯<=1). *n* is such that *n*<=+<=1 is a power of 2.
The next 2*q* lines represent queries; each query consists of two consecutive lines. The first of these two lines contains *u**i* (1<=β€<=*u**i*<=β€<=*n*), the second contains non-empty string *s**i*. *s**i* doesn't contain any characters other than 'L', 'R' and 'U'.
It is guaranteed that the sum of lengths of *s**i* (for each *i* such that 1<=β€<=*i*<=β€<=*q*) doesn't exceed 105. | Print *q* numbers, *i*-th number must be the answer to the *i*-th query. | [
"15 2\n4\nUURL\n8\nLRLLLLLLLL\n"
] | [
"10\n5\n"
] | none | [
{
"input": "15 2\n4\nUURL\n8\nLRLLLLLLLL",
"output": "10\n5"
},
{
"input": "1 1\n1\nL",
"output": "1"
},
{
"input": "1 1\n1\nR",
"output": "1"
},
{
"input": "1 1\n1\nU",
"output": "1"
},
{
"input": "1 10\n1\nURLRLULUR\n1\nLRRRURULULL\n1\nLURURRUUUU\n1\nRRULLLRRUL\... | 108 | 409,600 | 0 | 15,550 | |
845 | Shortest Path Problem? | [
"dfs and similar",
"graphs",
"math"
] | null | null | You are given an undirected graph with weighted edges. The length of some path between two vertices is the bitwise xor of weights of all edges belonging to this path (if some edge is traversed more than once, then it is included in bitwise xor the same number of times). You have to find the minimum length of path between vertex 1 and vertex *n*.
Note that graph can contain multiple edges and loops. It is guaranteed that the graph is connected. | The first line contains two numbers *n* and *m* (1<=β€<=*n*<=β€<=100000, *n*<=-<=1<=β€<=*m*<=β€<=100000) β the number of vertices and the number of edges, respectively.
Then *m* lines follow, each line containing three integer numbers *x*, *y* and *w* (1<=β€<=*x*,<=*y*<=β€<=*n*, 0<=β€<=*w*<=β€<=108). These numbers denote an edge that connects vertices *x* and *y* and has weight *w*. | Print one number β the minimum length of path between vertices 1 and *n*. | [
"3 3\n1 2 3\n1 3 2\n3 2 0\n",
"2 2\n1 1 3\n1 2 3\n"
] | [
"2\n",
"0\n"
] | none | [
{
"input": "3 3\n1 2 3\n1 3 2\n3 2 0",
"output": "2"
},
{
"input": "2 2\n1 1 3\n1 2 3",
"output": "0"
},
{
"input": "10 20\n8 5 64\n5 6 48\n4 5 91\n10 1 2\n3 4 51\n8 2 74\n6 1 98\n3 10 24\n2 10 35\n8 7 52\n10 5 72\n5 9 25\n2 9 65\n7 4 69\n5 7 26\n7 2 44\n6 8 61\n3 5 43\n10 7 33\n4 2 28",... | 545 | 19,968,000 | -1 | 15,577 | |
999 | Equalize the Remainders | [
"data structures",
"greedy",
"implementation"
] | null | null | You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$.
In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$.
Let's calculate $c_r$ ($0 \le r \le m-1)$ β the number of elements having remainder $r$ when divided by $m$. In other words, for each remainder, let's find the number of corresponding elements in $a$ with that remainder.
Your task is to change the array in such a way that $c_0 = c_1 = \dots = c_{m-1} = \frac{n}{m}$.
Find the minimum number of moves to satisfy the above requirement. | The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array. | In the first line, print a single integer β the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$.
In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $10^{18}$. | [
"6 3\n3 2 0 6 10 12\n",
"4 2\n0 1 2 3\n"
] | [
"3\n3 2 0 7 10 14 \n",
"0\n0 1 2 3 \n"
] | none | [
{
"input": "6 3\n3 2 0 6 10 12",
"output": "3\n3 2 0 7 10 14 "
},
{
"input": "4 2\n0 1 2 3",
"output": "0\n0 1 2 3 "
},
{
"input": "1 1\n1000000000",
"output": "0\n1000000000 "
},
{
"input": "6 3\n3 2 0 6 10 11",
"output": "1\n3 2 0 7 10 11 "
},
{
"input": "100 25... | 3,000 | 40,448,000 | 0 | 15,593 | |
730 | Car Repair Shop | [
"implementation"
] | null | null | Polycarp starts his own business. Tomorrow will be the first working day of his car repair shop. For now the car repair shop is very small and only one car can be repaired at a given time.
Polycarp is good at marketing, so he has already collected *n* requests from clients. The requests are numbered from 1 to *n* in order they came.
The *i*-th request is characterized by two values: *s**i* β the day when a client wants to start the repair of his car, *d**i* β duration (in days) to repair the car. The days are enumerated from 1, the first day is tomorrow, the second day is the day after tomorrow and so on.
Polycarp is making schedule by processing requests in the order from the first to the *n*-th request. He schedules the *i*-th request as follows:
- If the car repair shop is idle for *d**i* days starting from *s**i* (*s**i*,<=*s**i*<=+<=1,<=...,<=*s**i*<=+<=*d**i*<=-<=1), then these days are used to repair a car of the *i*-th client. - Otherwise, Polycarp finds the first day *x* (from 1 and further) that there are *d**i* subsequent days when no repair is scheduled starting from *x*. In other words he chooses the smallest positive *x* that all days *x*,<=*x*<=+<=1,<=...,<=*x*<=+<=*d**i*<=-<=1 are not scheduled for repair of any car. So, the car of the *i*-th client will be repaired in the range [*x*,<=*x*<=+<=*d**i*<=-<=1]. It is possible that the day *x* when repair is scheduled to start will be less than *s**i*.
Given *n* requests, you are asked to help Polycarp schedule all of them according to the rules above. | The first line contains integer *n* (1<=β€<=*n*<=β€<=200) β the number of requests from clients.
The following *n* lines contain requests, one request per line. The *i*-th request is given as the pair of integers *s**i*,<=*d**i* (1<=β€<=*s**i*<=β€<=109, 1<=β€<=*d**i*<=β€<=5Β·106), where *s**i* is the preferred time to start repairing the *i*-th car, *d**i* is the number of days to repair the *i*-th car.
The requests should be processed in the order they are given in the input. | Print *n* lines. The *i*-th line should contain two integers β the start day to repair the *i*-th car and the finish day to repair the *i*-th car. | [
"3\n9 2\n7 3\n2 4\n",
"4\n1000000000 1000000\n1000000000 1000000\n100000000 1000000\n1000000000 1000000\n"
] | [
"9 10\n1 3\n4 7\n",
"1000000000 1000999999\n1 1000000\n100000000 100999999\n1000001 2000000\n"
] | none | [
{
"input": "3\n9 2\n7 3\n2 4",
"output": "9 10\n1 3\n4 7"
},
{
"input": "4\n1000000000 1000000\n1000000000 1000000\n100000000 1000000\n1000000000 1000000",
"output": "1000000000 1000999999\n1 1000000\n100000000 100999999\n1000001 2000000"
},
{
"input": "1\n1 1",
"output": "1 1"
},
... | 0 | 0 | -1 | 15,604 | |
439 | Devu and his Brother | [
"binary search",
"sortings",
"ternary search",
"two pointers"
] | null | null | Devu and his brother love each other a lot. As they are super geeks, they only like to play with arrays. They are given two arrays *a* and *b* by their father. The array *a* is given to Devu and *b* to his brother.
As Devu is really a naughty kid, he wants the minimum value of his array *a* should be at least as much as the maximum value of his brother's array *b*.
Now you have to help Devu in achieving this condition. You can perform multiple operations on the arrays. In a single operation, you are allowed to decrease or increase any element of any of the arrays by 1. Note that you are allowed to apply the operation on any index of the array multiple times.
You need to find minimum number of operations required to satisfy Devu's condition so that the brothers can play peacefully without fighting. | The first line contains two space-separated integers *n*, *m* (1<=β€<=*n*,<=*m*<=β€<=105). The second line will contain *n* space-separated integers representing content of the array *a* (1<=β€<=*a**i*<=β€<=109). The third line will contain *m* space-separated integers representing content of the array *b* (1<=β€<=*b**i*<=β€<=109). | You need to output a single integer representing the minimum number of operations needed to satisfy Devu's condition. | [
"2 2\n2 3\n3 5\n",
"3 2\n1 2 3\n3 4\n",
"3 2\n4 5 6\n1 2\n"
] | [
"3\n",
"4\n",
"0\n"
] | In example 1, you can increase *a*<sub class="lower-index">1</sub> by 1 and decrease *b*<sub class="lower-index">2</sub> by 1 and then again decrease *b*<sub class="lower-index">2</sub> by 1. Now array *a* will be [3; 3] and array *b* will also be [3; 3]. Here minimum element of *a* is at least as large as maximum element of *b*. So minimum number of operations needed to satisfy Devu's condition are 3.
In example 3, you don't need to do any operation, Devu's condition is already satisfied. | [
{
"input": "2 2\n2 3\n3 5",
"output": "3"
},
{
"input": "3 2\n1 2 3\n3 4",
"output": "4"
},
{
"input": "3 2\n4 5 6\n1 2",
"output": "0"
},
{
"input": "10 10\n23 100 38 38 73 54 59 69 44 86\n100 100 100 100 100 100 100 100 100 100",
"output": "416"
},
{
"input": "1... | 171 | 10,444,800 | 0 | 15,626 | |
0 | none | [
"none"
] | null | null | Iahub and Iahubina went to a picnic in a forest full of trees. Less than 5 minutes passed before Iahub remembered of trees from programming. Moreover, he invented a new problem and Iahubina has to solve it, otherwise Iahub won't give her the food.
Iahub asks Iahubina: can you build a rooted tree, such that
- each internal node (a node with at least one son) has at least two sons; - node *i* has *c**i* nodes in its subtree?
Iahubina has to guess the tree. Being a smart girl, she realized that it's possible no tree can follow Iahub's restrictions. In this way, Iahub will eat all the food. You need to help Iahubina: determine if there's at least one tree following Iahub's restrictions. The required tree must contain *n* nodes. | The first line of the input contains integer *n* (1<=β€<=*n*<=β€<=24). Next line contains *n* positive integers: the *i*-th number represents *c**i* (1<=β€<=*c**i*<=β€<=*n*). | Output on the first line "YES" (without quotes) if there exist at least one tree following Iahub's restrictions, otherwise output "NO" (without quotes). | [
"4\n1 1 1 4\n",
"5\n1 1 5 2 1\n"
] | [
"YES",
"NO"
] | none | [] | 61 | 7,372,800 | 0 | 15,645 | |
0 | none | [
"none"
] | null | null | After years of hard work scientists invented an absolutely new e-reader display. The new display has a larger resolution, consumes less energy and its production is cheaper. And besides, one can bend it. The only inconvenience is highly unusual management. For that very reason the developers decided to leave the e-readers' software to programmers.
The display is represented by *n*<=Γ<=*n* square of pixels, each of which can be either black or white. The display rows are numbered with integers from 1 to *n* upside down, the columns are numbered with integers from 1 to *n* from the left to the right. The display can perform commands like "*x*,<=*y*". When a traditional display fulfills such command, it simply inverts a color of (*x*,<=*y*), where *x* is the row number and *y* is the column number. But in our new display every pixel that belongs to at least one of the segments (*x*,<=*x*)<=-<=(*x*,<=*y*) and (*y*,<=*y*)<=-<=(*x*,<=*y*) (both ends of both segments are included) inverts a color.
For example, if initially a display 5<=Γ<=5 in size is absolutely white, then the sequence of commands (1,<=4), (3,<=5), (5,<=1), (3,<=3) leads to the following changes:
You are an e-reader software programmer and you should calculate minimal number of commands needed to display the picture. You can regard all display pixels as initially white. | The first line contains number *n* (1<=β€<=*n*<=β€<=2000).
Next *n* lines contain *n* characters each: the description of the picture that needs to be shown. "0" represents the white color and "1" represents the black color. | Print one integer *z* β the least number of commands needed to display the picture. | [
"5\n01110\n10010\n10001\n10011\n11110\n"
] | [
"4\n"
] | none | [
{
"input": "5\n01110\n10010\n10001\n10011\n11110",
"output": "4"
},
{
"input": "4\n0000\n0111\n0001\n0001",
"output": "1"
},
{
"input": "6\n100000\n010000\n001000\n000100\n000000\n000001",
"output": "5"
},
{
"input": "10\n0000000000\n0000110000\n1001000000\n1000011110\n101111... | 62 | 0 | 0 | 15,650 | |
28 | Bath Queue | [
"combinatorics",
"dp",
"probabilities"
] | C. Bath Queue | 2 | 256 | There are *n* students living in the campus. Every morning all students wake up at the same time and go to wash. There are *m* rooms with wash basins. The *i*-th of these rooms contains *a**i* wash basins. Every student independently select one the rooms with equal probability and goes to it. After all students selected their rooms, students in each room divide into queues by the number of wash basins so that the size of the largest queue is the least possible. Calculate the expected value of the size of the largest queue among all rooms. | The first line contains two positive integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=50) β the amount of students and the amount of rooms. The second line contains *m* integers *a*1,<=*a*2,<=... ,<=*a**m* (1<=β€<=*a**i*<=β€<=50). *a**i* means the amount of wash basins in the *i*-th room. | Output single number: the expected value of the size of the largest queue. Your answer must have an absolute or relative error less than 10<=-<=9. | [
"1 1\n2\n",
"2 2\n1 1\n",
"2 3\n1 1 1\n",
"7 5\n1 1 2 3 1\n"
] | [
"1.00000000000000000000\n",
"1.50000000000000000000\n",
"1.33333333333333350000\n",
"2.50216960000000070000\n"
] | none | [
{
"input": "1 1\n2",
"output": "1.00000000000000000000"
},
{
"input": "2 2\n1 1",
"output": "1.50000000000000000000"
},
{
"input": "2 3\n1 1 1",
"output": "1.33333333333333350000"
},
{
"input": "7 5\n1 1 2 3 1",
"output": "2.50216960000000070000"
},
{
"input": "10... | 1,778 | 9,932,800 | 3.536999 | 15,685 |
862 | Mahmoud and Ehab and the function | [
"binary search",
"data structures",
"sortings"
] | null | null | Dr. Evil is interested in math and functions, so he gave Mahmoud and Ehab array *a* of length *n* and array *b* of length *m*. He introduced a function *f*(*j*) which is defined for integers *j*, which satisfy 0<=β€<=*j*<=β€<=*m*<=-<=*n*. Suppose, *c**i*<==<=*a**i*<=-<=*b**i*<=+<=*j*. Then *f*(*j*)<==<=|*c*1<=-<=*c*2<=+<=*c*3<=-<=*c*4... *c**n*|. More formally, .
Dr. Evil wants Mahmoud and Ehab to calculate the minimum value of this function over all valid *j*. They found it a bit easy, so Dr. Evil made their task harder. He will give them *q* update queries. During each update they should add an integer *x**i* to all elements in *a* in range [*l**i*;*r**i*] i.e. they should add *x**i* to *a**l**i*,<=*a**l**i*<=+<=1,<=... ,<=*a**r**i* and then they should calculate the minimum value of *f*(*j*) for all valid *j*.
Please help Mahmoud and Ehab. | The first line contains three integers *n*,<=*m* and *q* (1<=β€<=*n*<=β€<=*m*<=β€<=105, 1<=β€<=*q*<=β€<=105)Β β number of elements in *a*, number of elements in *b* and number of queries, respectively.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n*. (<=-<=109<=β€<=*a**i*<=β€<=109)Β β elements of *a*.
The third line contains *m* integers *b*1,<=*b*2,<=...,<=*b**m*. (<=-<=109<=β€<=*b**i*<=β€<=109)Β β elements of *b*.
Then *q* lines follow describing the queries. Each of them contains three integers *l**i* *r**i* *x**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*n*, <=-<=109<=β€<=*x*<=β€<=109)Β β range to be updated and added value. | The first line should contain the minimum value of the function *f* before any update.
Then output *q* lines, the *i*-th of them should contain the minimum value of the function *f* after performing the *i*-th update . | [
"5 6 3\n1 2 3 4 5\n1 2 3 4 5 6\n1 1 10\n1 1 -9\n1 5 -1\n"
] | [
"0\n9\n0\n0\n"
] | For the first example before any updates it's optimal to choose *j*β=β0, *f*(0)β=β|(1β-β1)β-β(2β-β2)β+β(3β-β3)β-β(4β-β4)β+β(5β-β5)|β=β|0|β=β0.
After the first update *a* becomes {11,β2,β3,β4,β5} and it's optimal to choose *j*β=β1, *f*(1)β=β|(11β-β2)β-β(2β-β3)β+β(3β-β4)β-β(4β-β5)β+β(5β-β6)β=β|9|β=β9.
After the second update *a* becomes {2,β2,β3,β4,β5} and it's optimal to choose *j*β=β1, *f*(1)β=β|(2β-β2)β-β(2β-β3)β+β(3β-β4)β-β(4β-β5)β+β(5β-β6)|β=β|0|β=β0.
After the third update *a* becomes {1,β1,β2,β3,β4} and it's optimal to choose *j*β=β0, *f*(0)β=β|(1β-β1)β-β(1β-β2)β+β(2β-β3)β-β(3β-β4)β+β(4β-β5)|β=β|0|β=β0. | [
{
"input": "5 6 3\n1 2 3 4 5\n1 2 3 4 5 6\n1 1 10\n1 1 -9\n1 5 -1",
"output": "0\n9\n0\n0"
},
{
"input": "1 1 1\n937982044\n179683049\n1 1 821220804",
"output": "758298995\n1579519799"
}
] | 46 | 5,529,600 | 0 | 15,694 | |
156 | Clues | [
"combinatorics",
"graphs"
] | null | null | As Sherlock Holmes was investigating another crime, he found a certain number of clues. Also, he has already found direct links between some of those clues. The direct links between the clues are mutual. That is, the direct link between clues *A* and *B* and the direct link between clues *B* and *A* is the same thing. No more than one direct link can exist between two clues.
Of course Sherlock is able to find direct links between all clues. But it will take too much time and the criminals can use this extra time to hide. To solve the crime, Sherlock needs each clue to be linked to all other clues (maybe not directly, via some other clues). Clues *A* and *B* are considered linked either if there is a direct link between them or if there is a direct link between *A* and some other clue *C* which is linked to *B*.
Sherlock Holmes counted the minimum number of additional direct links that he needs to find to solve the crime. As it turns out, it equals *T*.
Please count the number of different ways to find exactly *T* direct links between the clues so that the crime is solved in the end. Two ways to find direct links are considered different if there exist two clues which have a direct link in one way and do not have a direct link in the other way.
As the number of different ways can turn out rather big, print it modulo *k*. | The first line contains three space-separated integers *n*,<=*m*,<=*k* (1<=β€<=*n*<=β€<=105,<=0<=β€<=*m*<=β€<=105, 1<=β€<=*k*<=β€<=109) β the number of clues, the number of direct clue links that Holmes has already found and the divisor for the modulo operation.
Each of next *m* lines contains two integers *a* and *b* (1<=β€<=*a*,<=*b*<=β€<=*n*,<=*a*<=β <=*b*), that represent a direct link between clues. It is guaranteed that any two clues are linked by no more than one direct link. Note that the direct links between the clues are mutual. | Print the single number β the answer to the problem modulo *k*. | [
"2 0 1000000000\n",
"3 0 100\n",
"4 1 1000000000\n1 4\n"
] | [
"1\n",
"3\n",
"8\n"
] | The first sample only has two clues and Sherlock hasn't found any direct link between them yet. The only way to solve the crime is to find the link.
The second sample has three clues and Sherlock hasn't found any direct links between them. He has to find two of three possible direct links between clues to solve the crime β there are 3 ways to do it.
The third sample has four clues and the detective has already found one direct link between the first and the fourth clue. There are 8 ways to find two remaining clues to solve the crime. | [
{
"input": "2 0 1000000000",
"output": "1"
},
{
"input": "3 0 100",
"output": "3"
},
{
"input": "4 1 1000000000\n1 4",
"output": "8"
},
{
"input": "6 4 100000\n1 4\n4 6\n6 1\n2 5",
"output": "36"
},
{
"input": "10 0 123456789",
"output": "100000000"
},
{
... | 1,964 | 13,721,600 | -1 | 15,695 | |
257 | Sum | [
"greedy",
"math"
] | null | null | Vasya has found a piece of paper with an array written on it. The array consists of *n* integers *a*1,<=*a*2,<=...,<=*a**n*. Vasya noticed that the following condition holds for the array *a**i*<=β€<=*a**i*<=+<=1<=β€<=2Β·*a**i* for any positive integer *i* (*i*<=<<=*n*).
Vasya wants to add either a "+" or a "-" before each number of array. Thus, Vasya will get an expression consisting of *n* summands. The value of the resulting expression is the sum of all its elements. The task is to add signs "+" and "-" before each number so that the value of expression *s* meets the limits 0<=β€<=*s*<=β€<=*a*1. Print a sequence of signs "+" and "-", satisfying the given limits. It is guaranteed that the solution for the problem exists. | The first line contains integer *n* (1<=β€<=*n*<=β€<=105) β the size of the array. The second line contains space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=109) β the original array.
It is guaranteed that the condition *a**i*<=β€<=*a**i*<=+<=1<=β€<=2Β·*a**i* fulfills for any positive integer *i* (*i*<=<<=*n*). | In a single line print the sequence of *n* characters "+" and "-", where the *i*-th character is the sign that is placed in front of number *a**i*. The value of the resulting expression *s* must fit into the limits 0<=β€<=*s*<=β€<=*a*1. If there are multiple solutions, you are allowed to print any of them. | [
"4\n1 2 3 5\n",
"3\n3 3 5\n"
] | [
"+++-",
"++-"
] | none | [
{
"input": "4\n1 2 3 5",
"output": "+++-"
},
{
"input": "3\n3 3 5",
"output": "++-"
},
{
"input": "4\n2 4 5 6",
"output": "-++-"
},
{
"input": "6\n3 5 10 11 12 20",
"output": "++-++-"
},
{
"input": "10\n10 14 17 22 43 72 74 84 88 93",
"output": "++---++--+"
... | 2,000 | 10,956,800 | 0 | 15,698 | |
490 | Restoring Increasing Sequence | [
"binary search",
"brute force",
"greedy",
"implementation"
] | null | null | Peter wrote on the board a strictly increasing sequence of positive integers *a*1,<=*a*2,<=...,<=*a**n*. Then Vasil replaced some digits in the numbers of this sequence by question marks. Thus, each question mark corresponds to exactly one lost digit.
Restore the the original sequence knowing digits remaining on the board. | The first line of the input contains integer *n* (1<=β€<=*n*<=β€<=105) β the length of the sequence. Next *n* lines contain one element of the sequence each. Each element consists only of digits and question marks. No element starts from digit 0. Each element has length from 1 to 8 characters, inclusive. | If the answer exists, print in the first line "YES" (without the quotes). Next *n* lines must contain the sequence of positive integers β a possible variant of Peter's sequence. The found sequence must be strictly increasing, it must be transformed from the given one by replacing each question mark by a single digit. All numbers on the resulting sequence must be written without leading zeroes. If there are multiple solutions, print any of them.
If there is no answer, print a single line "NO" (without the quotes). | [
"3\n?\n18\n1?\n",
"2\n??\n?\n",
"5\n12224\n12??5\n12226\n?0000\n?00000\n"
] | [
"YES\n1\n18\n19\n",
"NO\n",
"YES\n12224\n12225\n12226\n20000\n100000\n"
] | none | [
{
"input": "3\n?\n18\n1?",
"output": "YES\n1\n18\n19"
},
{
"input": "2\n??\n?",
"output": "NO"
},
{
"input": "5\n12224\n12??5\n12226\n?0000\n?00000",
"output": "YES\n12224\n12225\n12226\n20000\n100000"
},
{
"input": "10\n473883\n3499005\n4?74792\n58146??\n8?90593\n9203?71\n?3... | 77 | 2,867,200 | -1 | 15,704 | |
930 | Game with Tokens | [
"data structures",
"games",
"implementation"
] | null | null | Consider the following game for two players. There is one white token and some number of black tokens. Each token is placed on a plane in a point with integer coordinates *x* and *y*.
The players take turn making moves, white starts. On each turn, a player moves all tokens of their color by 1 to up, down, left or right. Black player can choose directions for each token independently.
After a turn of the white player the white token can not be in a point where a black token is located. There are no other constraints on locations of the tokens: positions of black tokens can coincide, after a turn of the black player and initially the white token can be in the same point with some black point. If at some moment the white player can't make a move, he loses. If the white player makes 10100500 moves, he wins.
You are to solve the following problem. You are given initial positions of all black tokens. It is guaranteed that initially all these positions are distinct. In how many places can the white token be located initially so that if both players play optimally, the black player wins? | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=105) β the number of black points.
The (*i*<=+<=1)-th line contains two integers *x**i*, *y**i* (<=-<=105<=β€<=*x**i*,<=*y**i*,<=<=β€<=105) β the coordinates of the point where the *i*-th black token is initially located.
It is guaranteed that initial positions of black tokens are distinct. | Print the number of points where the white token can be located initially, such that if both players play optimally, the black player wins. | [
"4\n-2 -1\n0 1\n0 -3\n2 -1\n",
"4\n-2 0\n-1 1\n0 -2\n1 -1\n",
"16\n2 1\n1 2\n-1 1\n0 1\n0 0\n1 1\n2 -1\n2 0\n1 0\n-1 -1\n1 -1\n2 2\n0 -1\n-1 0\n0 2\n-1 2\n"
] | [
"4\n",
"2\n",
"4\n"
] | In the first and second examples initial positions of black tokens are shown with black points, possible positions of the white token (such that the black player wins) are shown with white points.
The first example: <img class="tex-graphics" src="https://espresso.codeforces.com/5054b8d2df2fac92c92f96fae82d21c365d12983.png" style="max-width: 100.0%;max-height: 100.0%;"/>
The second example: <img class="tex-graphics" src="https://espresso.codeforces.com/eb795dd6abb95cfafb1d1cb7d8c8798825dcc180.png" style="max-width: 100.0%;max-height: 100.0%;"/>
In the third example the white tokens should be located in the inner square 2βΓβ2, to make the black player win. <img class="tex-graphics" src="https://espresso.codeforces.com/6dfd863f649b92860dfd6b446ea004abc01b71a6.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "4\n-2 -1\n0 1\n0 -3\n2 -1",
"output": "4"
},
{
"input": "4\n-2 0\n-1 1\n0 -2\n1 -1",
"output": "2"
},
{
"input": "16\n2 1\n1 2\n-1 1\n0 1\n0 0\n1 1\n2 -1\n2 0\n1 0\n-1 -1\n1 -1\n2 2\n0 -1\n-1 0\n0 2\n-1 2",
"output": "4"
},
{
"input": "1\n1 2",
"output": "0"
... | 109 | 0 | -1 | 15,736 | |
818 | Sofa Thief | [
"brute force",
"implementation"
] | null | null | Yet another round on DecoForces is coming! Grandpa Maks wanted to participate in it but someone has stolen his precious sofa! And how can one perform well with such a major loss?
Fortunately, the thief had left a note for Grandpa Maks. This note got Maks to the sofa storehouse. Still he had no idea which sofa belongs to him as they all looked the same!
The storehouse is represented as matrix *n*<=Γ<=*m*. Every sofa takes two neighbouring by some side cells. No cell is covered by more than one sofa. There can be empty cells.
Sofa *A* is standing to the left of sofa *B* if there exist two such cells *a* and *b* that *x**a*<=<<=*x**b*, *a* is covered by *A* and *b* is covered by *B*. Sofa *A* is standing to the top of sofa *B* if there exist two such cells *a* and *b* that *y**a*<=<<=*y**b*, *a* is covered by *A* and *b* is covered by *B*. Right and bottom conditions are declared the same way.
Note that in all conditions *A*<=β <=*B*. Also some sofa *A* can be both to the top of another sofa *B* and to the bottom of it. The same is for left and right conditions.
The note also stated that there are *cnt**l* sofas to the left of Grandpa Maks's sofa, *cnt**r* β to the right, *cnt**t* β to the top and *cnt**b* β to the bottom.
Grandpa Maks asks you to help him to identify his sofa. It is guaranteed that there is no more than one sofa of given conditions.
Output the number of Grandpa Maks's sofa. If there is no such sofa that all the conditions are met for it then output -1. | The first line contains one integer number *d* (1<=β€<=*d*<=β€<=105) β the number of sofas in the storehouse.
The second line contains two integer numbers *n*, *m* (1<=β€<=*n*,<=*m*<=β€<=105) β the size of the storehouse.
Next *d* lines contains four integer numbers *x*1, *y*1, *x*2, *y*2 (1<=β€<=*x*1,<=*x*2<=β€<=*n*, 1<=β€<=*y*1,<=*y*2<=β€<=*m*) β coordinates of the *i*-th sofa. It is guaranteed that cells (*x*1,<=*y*1) and (*x*2,<=*y*2) have common side, (*x*1,<=*y*1) <=β <= (*x*2,<=*y*2) and no cell is covered by more than one sofa.
The last line contains four integer numbers *cnt**l*, *cnt**r*, *cnt**t*, *cnt**b* (0<=β€<=*cnt**l*,<=*cnt**r*,<=*cnt**t*,<=*cnt**b*<=β€<=*d*<=-<=1). | Print the number of the sofa for which all the conditions are met. Sofas are numbered 1 through *d* as given in input. If there is no such sofa then print -1. | [
"2\n3 2\n3 1 3 2\n1 2 2 2\n1 0 0 1\n",
"3\n10 10\n1 2 1 1\n5 5 6 5\n6 4 5 4\n2 1 2 0\n",
"2\n2 2\n2 1 1 1\n1 2 2 2\n1 0 0 0\n"
] | [
"1\n",
"2\n",
"-1\n"
] | Let's consider the second example.
- The first sofa has 0 to its left, 2 sofas to its right ((1,β1) is to the left of both (5,β5) and (5,β4)), 0 to its top and 2 to its bottom (both 2nd and 3rd sofas are below). - The second sofa has *cnt*<sub class="lower-index">*l*</sub>β=β2, *cnt*<sub class="lower-index">*r*</sub>β=β1, *cnt*<sub class="lower-index">*t*</sub>β=β2 and *cnt*<sub class="lower-index">*b*</sub>β=β0. - The third sofa has *cnt*<sub class="lower-index">*l*</sub>β=β2, *cnt*<sub class="lower-index">*r*</sub>β=β1, *cnt*<sub class="lower-index">*t*</sub>β=β1 and *cnt*<sub class="lower-index">*b*</sub>β=β1.
So the second one corresponds to the given conditions.
In the third example
- The first sofa has *cnt*<sub class="lower-index">*l*</sub>β=β1, *cnt*<sub class="lower-index">*r*</sub>β=β1, *cnt*<sub class="lower-index">*t*</sub>β=β0 and *cnt*<sub class="lower-index">*b*</sub>β=β1. - The second sofa has *cnt*<sub class="lower-index">*l*</sub>β=β1, *cnt*<sub class="lower-index">*r*</sub>β=β1, *cnt*<sub class="lower-index">*t*</sub>β=β1 and *cnt*<sub class="lower-index">*b*</sub>β=β0.
And there is no sofa with the set (1,β0,β0,β0) so the answer is -1. | [
{
"input": "2\n3 2\n3 1 3 2\n1 2 2 2\n1 0 0 1",
"output": "1"
},
{
"input": "3\n10 10\n1 2 1 1\n5 5 6 5\n6 4 5 4\n2 1 2 0",
"output": "2"
},
{
"input": "2\n2 2\n2 1 1 1\n1 2 2 2\n1 0 0 0",
"output": "-1"
},
{
"input": "1\n1 2\n1 1 1 2\n0 0 0 0",
"output": "1"
},
{
... | 763 | 26,828,800 | 0 | 15,774 | |
172 | Bus | [
"*special",
"implementation",
"sortings"
] | null | null | There is a bus stop near the university. The lessons are over, and *n* students come to the stop. The *i*-th student will appear at the bus stop at time *t**i* (all *t**i*'s are distinct).
We shall assume that the stop is located on the coordinate axis *Ox*, at point *x*<==<=0, and the bus goes along the ray *Ox*, that is, towards the positive direction of the coordinate axis, and back. The *i*-th student needs to get to the point with coordinate *x**i* (*x**i*<=><=0).
The bus moves by the following algorithm. Initially it is at point 0. The students consistently come to the stop and get on it. The bus has a seating capacity which is equal to *m* passengers. At the moment when *m* students get on the bus, it starts moving in the positive direction of the coordinate axis. Also it starts moving when the last (*n*-th) student gets on the bus. The bus is moving at a speed of 1 unit of distance per 1 unit of time, i.e. it covers distance *y* in time *y*.
Every time the bus passes the point at which at least one student needs to get off, it stops and these students get off the bus. The students need 1<=+<=[*k*<=/<=2] units of time to get off the bus, where *k* is the number of students who leave at this point. Expression [*k*<=/<=2] denotes rounded down *k*<=/<=2. As soon as the last student leaves the bus, the bus turns around and goes back to the point *x*<==<=0. It doesn't make any stops until it reaches the point. At the given point the bus fills with students once more, and everything is repeated.
If students come to the stop when there's no bus, they form a line (queue) and get on the bus in the order in which they came. Any number of students get on the bus in negligible time, you should assume that it doesn't take any time. Any other actions also take no time. The bus has no other passengers apart from the students.
Write a program that will determine for each student the time when he got off the bus. The moment a student got off the bus is the moment the bus stopped at the student's destination stop (despite the fact that the group of students need some time to get off). | The first line contains two space-separated integers *n*,<=*m* (1<=β€<=*n*,<=*m*<=β€<=105) β the number of students and the number of passengers the bus can transport, correspondingly. Next *n* lines contain descriptions of the students, one per line. Each line contains a pair of integers *t**i*,<=*x**i* (1<=β€<=*t**i*<=β€<=105,<=1<=β€<=*x**i*<=β€<=104). The lines are given in the order of strict increasing of *t**i*. Values of *x**i* can coincide. | Print *n* numbers *w*1,<=*w*2,<=...,<=*w**n*, *w**i* β the moment of time when the *i*-th student got off the bus. Print the numbers on one line and separate them with single spaces. | [
"1 10\n3 5\n",
"2 1\n3 5\n4 5\n",
"5 4\n3 5\n4 5\n5 5\n6 5\n7 1\n",
"20 4\n28 13\n31 13\n35 6\n36 4\n52 6\n53 4\n83 2\n84 4\n87 1\n93 6\n108 4\n113 6\n116 1\n125 2\n130 2\n136 13\n162 2\n166 4\n184 1\n192 2\n"
] | [
"8\n",
"8 19\n",
"11 11 11 11 20\n",
"51 51 43 40 93 89 86 89 114 121 118 121 137 139 139 152 195 199 193 195\n"
] | In the first sample the bus waits for the first student for 3 units of time and drives him to his destination in additional 5 units of time. So the student leaves the bus at the moment of time 3β+β5β=β8.
In the second sample the capacity of the bus equals 1, that's why it will drive the first student alone. This student is the same as the student from the first sample. So the bus arrives to his destination at the moment of time 8, spends 1β+β[1β/β2]β=β1 units of time on getting him off, and returns back to 0 in additional 5 units of time. That is, the bus returns to the bus stop at the moment of time 14. By this moment the second student has already came to the bus stop. So he immediately gets in the bus, and is driven to his destination in additional 5 units of time. He gets there at the moment 14β+β5β=β19.
In the third sample the bus waits for the fourth student for 6 units of time, then drives for 5 units of time, then gets the passengers off for 1β+β[4β/β2]β=β3 units of time, then returns for 5 units of time, and then drives the fifth student for 1 unit of time. | [
{
"input": "1 10\n3 5",
"output": "8"
},
{
"input": "2 1\n3 5\n4 5",
"output": "8 19"
},
{
"input": "5 4\n3 5\n4 5\n5 5\n6 5\n7 1",
"output": "11 11 11 11 20"
},
{
"input": "20 4\n28 13\n31 13\n35 6\n36 4\n52 6\n53 4\n83 2\n84 4\n87 1\n93 6\n108 4\n113 6\n116 1\n125 2\n130 2\... | 1,000 | 12,595,200 | 0 | 15,776 | |
442 | Adam and Tree | [
"data structures",
"trees"
] | null | null | When Adam gets a rooted tree (connected non-directed graph without cycles), he immediately starts coloring it. More formally, he assigns a color to each edge of the tree so that it meets the following two conditions:
- There is no vertex that has more than two incident edges painted the same color. - For any two vertexes that have incident edges painted the same color (say, *c*), the path between them consists of the edges of the color *c*.
Not all tree paintings are equally good for Adam. Let's consider the path from some vertex to the root. Let's call the number of distinct colors on this path the cost of the vertex. The cost of the tree's coloring will be the maximum cost among all the vertexes. Help Adam determine the minimum possible cost of painting the tree.
Initially, Adam's tree consists of a single vertex that has number one and is the root. In one move Adam adds a new vertex to the already existing one, the new vertex gets the number equal to the minimum positive available integer. After each operation you need to calculate the minimum cost of coloring the resulting tree. | The first line contains integer *n* (1<=β€<=*n*<=β€<=106) β the number of times a new vertex is added. The second line contains *n* numbers *p**i* (1<=β€<=*p**i*<=β€<=*i*) β the numbers of the vertexes to which we add another vertex. | Print *n* integers β the minimum costs of the tree painting after each addition. | [
"11\n1 1 1 3 4 4 7 3 7 6 6\n"
] | [
"1 1 1 1 1 2 2 2 2 2 3 "
] | The figure below shows one of the possible variants to paint a tree from the sample at the last moment. The cost of the vertexes with numbers 11 and 12 equals 3.
<img class="tex-graphics" src="https://espresso.codeforces.com/3e0ae59416472763f3e14b7c4a5094de154d3b50.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [] | 46 | 0 | 0 | 15,796 | |
1,004 | Sonya and Ice Cream | [
"binary search",
"data structures",
"dp",
"greedy",
"shortest paths",
"trees"
] | null | null | Sonya likes ice cream very much. She eats it even during programming competitions. That is why the girl decided that she wants to open her own ice cream shops.
Sonya lives in a city with $n$ junctions and $n-1$ streets between them. All streets are two-way and connect two junctions. It is possible to travel from any junction to any other using one or more streets. City Hall allows opening shops only on junctions. The girl cannot open shops in the middle of streets.
Sonya has exactly $k$ friends whom she can trust. If she opens a shop, one of her friends has to work there and not to allow anybody to eat an ice cream not paying for it. Since Sonya does not want to skip an important competition, she will not work in shops personally.
Sonya wants all her ice cream shops to form a simple path of the length $r$ ($1 \le r \le k$), i.e. to be located in different junctions $f_1, f_2, \dots, f_r$ and there is street between $f_i$ and $f_{i+1}$ for each $i$ from $1$ to $r-1$.
The girl takes care of potential buyers, so she also wants to minimize the maximum distance between the junctions to the nearest ice cream shop. The distance between two junctions $a$ and $b$ is equal to the sum of all the street lengths that you need to pass to get from the junction $a$ to the junction $b$. So Sonya wants to minimize
$$\max_{a} \min_{1 \le i \le r} d_{a,f_i}$$
where $a$ takes a value of all possible $n$ junctions, $f_i$Β β the junction where the $i$-th Sonya's shop is located, and $d_{x,y}$Β β the distance between the junctions $x$ and $y$.
Sonya is not sure that she can find the optimal shops locations, that is why she is asking you to help her to open not more than $k$ shops that will form a simple path and the maximum distance between any junction and the nearest shop would be minimal. | The first line contains two integers $n$ and $k$ ($1\leq k\leq n\leq 10^5$)Β β the number of junctions and friends respectively.
Each of the next $n-1$ lines contains three integers $u_i$, $v_i$, and $d_i$ ($1\leq u_i, v_i\leq n$, $v_i\neq u_i$, $1\leq d\leq 10^4$)Β β junctions that are connected by a street and the length of this street. It is guaranteed that each pair of junctions is connected by at most one street. It is guaranteed that you can get from any junctions to any other. | Print one numberΒ β the minimal possible maximum distance that you need to pass to get from any junction to the nearest ice cream shop. Sonya's shops must form a simple path and the number of shops must be at most $k$. | [
"6 2\n1 2 3\n2 3 4\n4 5 2\n4 6 3\n2 4 6\n",
"10 3\n1 2 5\n5 7 2\n3 2 6\n10 6 3\n3 8 1\n6 4 2\n4 1 6\n6 9 4\n5 2 5\n"
] | [
"4\n",
"7\n"
] | In the first example, you can choose the path 2-4, so the answer will be 4.
In the second example, you can choose the path 4-1-2, so the answer will be 7. | [
{
"input": "6 2\n1 2 3\n2 3 4\n4 5 2\n4 6 3\n2 4 6",
"output": "4"
},
{
"input": "10 3\n1 2 5\n5 7 2\n3 2 6\n10 6 3\n3 8 1\n6 4 2\n4 1 6\n6 9 4\n5 2 5",
"output": "7"
},
{
"input": "8 4\n8 7 4\n5 6 7\n7 3 4\n8 4 3\n1 2 1\n2 3 5\n5 4 4",
"output": "10"
},
{
"input": "1 1",
... | 155 | 0 | 0 | 15,798 | |
534 | Handshakes | [
"binary search",
"constructive algorithms",
"data structures",
"greedy"
] | null | null | On February, 30th *n* students came in the Center for Training Olympiad Programmers (CTOP) of the Berland State University. They came one by one, one after another. Each of them went in, and before sitting down at his desk, greeted with those who were present in the room by shaking hands. Each of the students who came in stayed in CTOP until the end of the day and never left.
At any time any three students could join together and start participating in a team contest, which lasted until the end of the day. The team did not distract from the contest for a minute, so when another student came in and greeted those who were present, he did not shake hands with the members of the contest writing team. Each team consisted of exactly three students, and each student could not become a member of more than one team. Different teams could start writing contest at different times.
Given how many present people shook the hands of each student, get a possible order in which the students could have come to CTOP. If such an order does not exist, then print that this is impossible.
Please note that some students could work independently until the end of the day, without participating in a team contest. | The first line contains integer *n* (1<=β€<=*n*<=β€<=2Β·105) β the number of students who came to CTOP. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=<<=*n*), where *a**i* is the number of students with who the *i*-th student shook hands. | If the sought order of students exists, print in the first line "Possible" and in the second line print the permutation of the students' numbers defining the order in which the students entered the center. Number *i* that stands to the left of number *j* in this permutation means that the *i*-th student came earlier than the *j*-th student. If there are multiple answers, print any of them.
If the sought order of students doesn't exist, in a single line print "Impossible". | [
"5\n2 1 3 0 1\n",
"9\n0 2 3 4 1 1 0 2 2\n",
"4\n0 2 1 1\n"
] | [
"Possible\n4 5 1 3 2 ",
"Possible\n7 5 2 1 6 8 3 4 9",
"Impossible\n"
] | In the first sample from the statement the order of events could be as follows:
- student 4 comes in (*a*<sub class="lower-index">4</sub>β=β0), he has no one to greet; - student 5 comes in (*a*<sub class="lower-index">5</sub>β=β1), he shakes hands with student 4; - student 1 comes in (*a*<sub class="lower-index">1</sub>β=β2), he shakes hands with two students (students 4, 5); - student 3 comes in (*a*<sub class="lower-index">3</sub>β=β3), he shakes hands with three students (students 4, 5, 1); - students 4, 5, 3 form a team and start writing a contest; - student 2 comes in (*a*<sub class="lower-index">2</sub>β=β1), he shakes hands with one student (number 1).
In the second sample from the statement the order of events could be as follows:
- student 7 comes in (*a*<sub class="lower-index">7</sub>β=β0), he has nobody to greet; - student 5 comes in (*a*<sub class="lower-index">5</sub>β=β1), he shakes hands with student 7; - student 2 comes in (*a*<sub class="lower-index">2</sub>β=β2), he shakes hands with two students (students 7, 5); - students 7, 5, 2 form a team and start writing a contest; - student 1 comes in(*a*<sub class="lower-index">1</sub>β=β0), he has no one to greet (everyone is busy with the contest); - student 6 comes in (*a*<sub class="lower-index">6</sub>β=β1), he shakes hands with student 1; - student 8 comes in (*a*<sub class="lower-index">8</sub>β=β2), he shakes hands with two students (students 1, 6); - student 3 comes in (*a*<sub class="lower-index">3</sub>β=β3), he shakes hands with three students (students 1, 6, 8); - student 4 comes in (*a*<sub class="lower-index">4</sub>β=β4), he shakes hands with four students (students 1, 6, 8, 3); - students 8, 3, 4 form a team and start writing a contest; - student 9 comes in (*a*<sub class="lower-index">9</sub>β=β2), he shakes hands with two students (students 1, 6).
In the third sample from the statement the order of events is restored unambiguously:
- student 1 comes in (*a*<sub class="lower-index">1</sub>β=β0), he has no one to greet; - student 3 comes in (or student 4) (*a*<sub class="lower-index">3</sub>β=β*a*<sub class="lower-index">4</sub>β=β1), he shakes hands with student 1; - student 2 comes in (*a*<sub class="lower-index">2</sub>β=β2), he shakes hands with two students (students 1, 3 (or 4)); - the remaining student 4 (or student 3), must shake one student's hand (*a*<sub class="lower-index">3</sub>β=β*a*<sub class="lower-index">4</sub>β=β1) but it is impossible as there are only two scenarios: either a team formed and he doesn't greet anyone, or he greets all the three present people who work individually. | [
{
"input": "5\n2 1 3 0 1",
"output": "Possible\n4 5 1 3 2 "
},
{
"input": "9\n0 2 3 4 1 1 0 2 2",
"output": "Possible\n7 6 9 3 4 8 1 5 2 "
},
{
"input": "4\n0 2 1 1",
"output": "Impossible"
},
{
"input": "5\n1 0 2 1 0",
"output": "Possible\n5 4 3 2 1 "
},
{
"input... | 93 | 0 | 0 | 15,831 | |
576 | Points on Plane | [
"constructive algorithms",
"divide and conquer",
"geometry",
"greedy",
"sortings"
] | null | null | On a plane are *n* points (*x**i*, *y**i*) with integer coordinates between 0 and 106. The distance between the two points with numbers *a* and *b* is said to be the following value: (the distance calculated by such formula is called Manhattan distance).
We call a hamiltonian path to be some permutation *p**i* of numbers from 1 to *n*. We say that the length of this path is value .
Find some hamiltonian path with a length of no more than 25<=Γ<=108. Note that you do not have to minimize the path length. | The first line contains integer *n* (1<=β€<=*n*<=β€<=106).
The *i*<=+<=1-th line contains the coordinates of the *i*-th point: *x**i* and *y**i* (0<=β€<=*x**i*,<=*y**i*<=β€<=106).
It is guaranteed that no two points coincide. | Print the permutation of numbers *p**i* from 1 to *n* β the sought Hamiltonian path. The permutation must meet the inequality .
If there are multiple possible answers, print any of them.
It is guaranteed that the answer exists. | [
"5\n0 7\n8 10\n3 4\n5 0\n9 12\n"
] | [
"4 3 1 2 5 \n"
] | In the sample test the total distance is:
<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c772e61c616e1c27114e3facb9e6db6c5cf93b82.png" style="max-width: 100.0%;max-height: 100.0%;"/>
(|5β-β3|β+β|0β-β4|)β+β(|3β-β0|β+β|4β-β7|)β+β(|0β-β8|β+β|7β-β10|)β+β(|8β-β9|β+β|10β-β12|)β=β2β+β4β+β3β+β3β+β8β+β3β+β1β+β2β=β26 | [] | 2,000 | 39,116,800 | 0 | 15,850 | |
981 | Addition on Segments | [
"bitmasks",
"data structures",
"divide and conquer",
"dp"
] | null | null | Grisha come to a contest and faced the following problem.
You are given an array of size $n$, initially consisting of zeros. The elements of the array are enumerated from $1$ to $n$. You perform $q$ operations on the array. The $i$-th operation is described with three integers $l_i$, $r_i$ and $x_i$ ($1 \leq l_i \leq r_i \leq n$, $1 \leq x_i \leq n$) and means that you should add $x_i$ to each of the elements with indices $l_i, l_i + 1, \ldots, r_i$. After all operations you should find the maximum in the array.
Grisha is clever, so he solved the problem quickly.
However something went wrong inside his head and now he thinks of the following question: "consider we applied some subset of the operations to the array. What are the possible values of the maximum in the array?"
Help Grisha, find all integers $y$ between $1$ and $n$ such that if you apply some subset (possibly empty) of the operations, then the maximum in the array becomes equal to $y$. | The first line contains two integers $n$ and $q$ ($1 \leq n, q \leq 10^{4}$)Β β the length of the array and the number of queries in the initial problem.
The following $q$ lines contain queries, one per line. The $i$-th of these lines contains three integers $l_i$, $r_i$ and $x_i$ ($1 \leq l_i \leq r_i \leq n$, $1 \leq x_i \leq n$), denoting a query of adding $x_i$ to the segment from $l_i$-th to $r_i$-th elements of the array, inclusive. | In the first line print the only integer $k$, denoting the number of integers from $1$ to $n$, inclusive, that can be equal to the maximum in the array after applying some subset (possibly empty) of the given operations.
In the next line print these $k$ integers from $1$ to $n$Β β the possible values of the maximum. Print these integers in increasing order. | [
"4 3\n1 3 1\n2 4 2\n3 4 4\n",
"7 2\n1 5 1\n3 7 2\n",
"10 3\n1 1 2\n1 1 3\n1 1 6\n"
] | [
"4\n1 2 3 4 \n",
"3\n1 2 3 \n",
"6\n2 3 5 6 8 9 \n"
] | Consider the first example. If you consider the subset only of the first query, the maximum is equal to $1$. If you take only the second query, the maximum equals to $2$. If you take the first two queries, the maximum becomes $3$. If you take only the fourth query, the maximum becomes $4$. If you take the fourth query and something more, the maximum becomes greater that $n$, so you shouldn't print it.
In the second example you can take the first query to obtain $1$. You can take only the second query to obtain $2$. You can take all queries to obtain $3$.
In the third example you can obtain the following maximums:
- You can achieve the maximim of $2$ by using queries: $(1)$. - You can achieve the maximim of $3$ by using queries: $(2)$. - You can achieve the maximim of $5$ by using queries: $(1, 2)$. - You can achieve the maximim of $6$ by using queries: $(3)$. - You can achieve the maximim of $8$ by using queries: $(1, 3)$. - You can achieve the maximim of $9$ by using queries: $(2, 3)$. | [
{
"input": "4 3\n1 3 1\n2 4 2\n3 4 4",
"output": "4\n1 2 3 4 "
},
{
"input": "7 2\n1 5 1\n3 7 2",
"output": "3\n1 2 3 "
},
{
"input": "10 3\n1 1 2\n1 1 3\n1 1 6",
"output": "6\n2 3 5 6 8 9 "
},
{
"input": "45 5\n37 38 16\n5 7 34\n1 42 31\n8 27 19\n15 28 39",
"output": "5\... | 202 | 28,774,400 | -1 | 15,879 | |
43 | Journey | [
"brute force",
"constructive algorithms",
"implementation"
] | D. Journey | 2 | 256 | The territory of Berland is represented by a rectangular field *n*<=Γ<=*m* in size. The king of Berland lives in the capital, located on the upper left square (1,<=1). The lower right square has coordinates (*n*,<=*m*). One day the king decided to travel through the whole country and return back to the capital, having visited every square (except the capital) exactly one time. The king must visit the capital exactly two times, at the very beginning and at the very end of his journey. The king can only move to the side-neighboring squares. However, the royal advise said that the King possibly will not be able to do it. But there is a way out β one can build the system of one way teleporters between some squares so that the king could fulfill his plan. No more than one teleporter can be installed on one square, every teleporter can be used any number of times, however every time it is used, it transports to the same given for any single teleporter square. When the king reaches a square with an installed teleporter he chooses himself whether he is or is not going to use the teleport. What minimum number of teleporters should be installed for the king to complete the journey? You should also compose the journey path route for the king. | The first line contains two space-separated integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=100,<=2<=β€<= *n* Β· *m*) β the field size. The upper left square has coordinates (1,<=1), and the lower right square has coordinates of (*n*,<=*m*). | On the first line output integer *k* β the minimum number of teleporters. Then output *k* lines each containing 4 integers *x*1 *y*1 *x*2 *y*2 (1<=β€<=*x*1,<=*x*2<=β€<=*n*,<=1<=β€<=*y*1,<=*y*2<=β€<=*m*) β the coordinates of the square where the teleporter is installed (*x*1,<=*y*1), and the coordinates of the square where the teleporter leads (*x*2,<=*y*2).
Then print *nm*<=+<=1 lines containing 2 numbers each β the coordinates of the squares in the order in which they are visited by the king. The travel path must start and end at (1,<=1). The king can move to side-neighboring squares and to the squares where a teleporter leads. Besides, he also should visit the capital exactly two times and he should visit other squares exactly one time. | [
"2 2\n",
"3 3\n"
] | [
"0\n1 1\n1 2\n2 2\n2 1\n1 1\n",
"1\n3 3 1 1\n1 1\n1 2\n1 3\n2 3\n2 2\n2 1\n3 1\n3 2\n3 3\n1 1\n"
] | none | [
{
"input": "2 2",
"output": "0\n1 1\n1 2\n2 2\n2 1\n1 1"
},
{
"input": "3 3",
"output": "1\n3 3 1 1\n1 1\n1 2\n1 3\n2 3\n2 2\n2 1\n3 1\n3 2\n3 3\n1 1"
},
{
"input": "3 2",
"output": "0\n1 1\n2 1\n3 1\n3 2\n2 2\n1 2\n1 1"
},
{
"input": "2 3",
"output": "0\n1 1\n1 2\n1 3\n2... | 0 | 0 | -1 | 15,897 |
29 | Traffic Lights | [
"implementation"
] | B. Traffic Lights | 2 | 256 | A car moves from point A to point B at speed *v* meters per second. The action takes place on the X-axis. At the distance *d* meters from A there are traffic lights. Starting from time 0, for the first *g* seconds the green light is on, then for the following *r* seconds the red light is on, then again the green light is on for the *g* seconds, and so on.
The car can be instantly accelerated from 0 to *v* and vice versa, can instantly slow down from the *v* to 0. Consider that it passes the traffic lights at the green light instantly. If the car approaches the traffic lights at the moment when the red light has just turned on, it doesn't have time to pass it. But if it approaches the traffic lights at the moment when the green light has just turned on, it can move. The car leaves point A at the time 0.
What is the minimum time for the car to get from point A to point B without breaking the traffic rules? | The first line contains integers *l*, *d*, *v*, *g*, *r* (1<=β€<=*l*,<=*d*,<=*v*,<=*g*,<=*r*<=β€<=1000,<=*d*<=<<=*l*) β the distance between A and B (in meters), the distance from A to the traffic lights, car's speed, the duration of green light and the duration of red light. | Output a single number β the minimum time that the car needs to get from point A to point B. Your output must have relative or absolute error less than 10<=-<=6. | [
"2 1 3 4 5\n",
"5 4 3 1 1\n"
] | [
"0.66666667\n",
"2.33333333\n"
] | none | [
{
"input": "2 1 3 4 5",
"output": "0.66666667"
},
{
"input": "5 4 3 1 1",
"output": "2.33333333"
},
{
"input": "862 33 604 888 704",
"output": "1.42715232"
},
{
"input": "458 251 49 622 472",
"output": "9.34693878"
},
{
"input": "772 467 142 356 889",
"output"... | 92 | 0 | 3.977 | 15,909 |
173 | Rock-Paper-Scissors | [
"implementation",
"math"
] | null | null | Nikephoros and Polycarpus play rock-paper-scissors. The loser gets pinched (not too severely!).
Let us remind you the rules of this game. Rock-paper-scissors is played by two players. In each round the players choose one of three items independently from each other. They show the items with their hands: a rock, scissors or paper. The winner is determined by the following rules: the rock beats the scissors, the scissors beat the paper and the paper beats the rock. If the players choose the same item, the round finishes with a draw.
Nikephoros and Polycarpus have played *n* rounds. In each round the winner gave the loser a friendly pinch and the loser ended up with a fresh and new red spot on his body. If the round finished in a draw, the players did nothing and just played on.
Nikephoros turned out to have worked out the following strategy: before the game began, he chose some sequence of items *A*<==<=(*a*1,<=*a*2,<=...,<=*a**m*), and then he cyclically showed the items from this sequence, starting from the first one. Cyclically means that Nikephoros shows signs in the following order: *a*1, *a*2, ..., *a**m*, *a*1, *a*2, ..., *a**m*, *a*1, ... and so on. Polycarpus had a similar strategy, only he had his own sequence of items *B*<==<=(*b*1,<=*b*2,<=...,<=*b**k*).
Determine the number of red spots on both players after they've played *n* rounds of the game. You can consider that when the game began, the boys had no red spots on them. | The first line contains integer *n* (1<=β€<=*n*<=β€<=2Β·109) β the number of the game's rounds.
The second line contains sequence *A* as a string of *m* characters and the third line contains sequence *B* as a string of *k* characters (1<=β€<=*m*,<=*k*<=β€<=1000). The given lines only contain characters "R", "S" and "P". Character "R" stands for the rock, character "S" represents the scissors and "P" represents the paper. | Print two space-separated integers: the numbers of red spots Nikephoros and Polycarpus have. | [
"7\nRPS\nRSPP\n",
"5\nRRRRRRRR\nR\n"
] | [
"3 2",
"0 0"
] | In the first sample the game went like this:
- R - R. Draw. - P - S. Nikephoros loses. - S - P. Polycarpus loses. - R - P. Nikephoros loses. - P - R. Polycarpus loses. - S - S. Draw. - R - P. Nikephoros loses.
Thus, in total Nikephoros has 3 losses (and 3 red spots), and Polycarpus only has 2. | [
{
"input": "7\nRPS\nRSPP",
"output": "3 2"
},
{
"input": "5\nRRRRRRRR\nR",
"output": "0 0"
},
{
"input": "23\nRSP\nRPSS",
"output": "7 8"
},
{
"input": "52\nRRPSS\nRSSPRPRPPP",
"output": "15 21"
},
{
"input": "1293\nRRPSSRSSPRPRPPPRPPPRPPPPPRPSPRSSRPSPPRPRR\nSSPSS... | 2,744 | 0 | 3 | 15,941 | |
367 | Sereja and the Arrangement of Numbers | [
"graphs",
"greedy",
"sortings"
] | null | null | Let's call an array consisting of *n* integer numbers *a*1, *a*2, ..., *a**n*, beautiful if it has the following property:
- consider all pairs of numbers *x*,<=*y* (*x*<=β <=*y*), such that number *x* occurs in the array *a* and number *y* occurs in the array *a*; - for each pair *x*,<=*y* must exist some position *j* (1<=β€<=*j*<=<<=*n*), such that at least one of the two conditions are met, either *a**j*<==<=*x*,<=*a**j*<=+<=1<==<=*y*, or *a**j*<==<=*y*,<=*a**j*<=+<=1<==<=*x*.
Sereja wants to build a beautiful array *a*, consisting of *n* integers. But not everything is so easy, Sereja's friend Dima has *m* coupons, each contains two integers *q**i*,<=*w**i*. Coupon *i* costs *w**i* and allows you to use as many numbers *q**i* as you want when constructing the array *a*. Values *q**i* are distinct. Sereja has no coupons, so Dima and Sereja have made the following deal. Dima builds some beautiful array *a* of *n* elements. After that he takes *w**i* rubles from Sereja for each *q**i*, which occurs in the array *a*. Sereja believed his friend and agreed to the contract, and now he is wondering, what is the maximum amount of money he can pay.
Help Sereja, find the maximum amount of money he can pay to Dima. | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=2Β·106,<=1<=β€<=*m*<=β€<=105). Next *m* lines contain pairs of integers. The *i*-th line contains numbers *q**i*,<=*w**i* (1<=β€<=*q**i*,<=*w**i*<=β€<=105).
It is guaranteed that all *q**i* are distinct. | In a single line print maximum amount of money (in rubles) Sereja can pay.
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. | [
"5 2\n1 2\n2 3\n",
"100 3\n1 2\n2 1\n3 1\n",
"1 2\n1 1\n2 100\n"
] | [
"5\n",
"4\n",
"100\n"
] | In the first sample Sereja can pay 5 rubles, for example, if Dima constructs the following array: [1,β2,β1,β2,β2]. There are another optimal arrays for this test.
In the third sample Sereja can pay 100 rubles, if Dima constructs the following array: [2]. | [
{
"input": "5 2\n1 2\n2 3",
"output": "5"
},
{
"input": "100 3\n1 2\n2 1\n3 1",
"output": "4"
},
{
"input": "1 2\n1 1\n2 100",
"output": "100"
},
{
"input": "25 29\n82963 53706\n63282 73962\n14996 48828\n84392 31903\n96293 41422\n31719 45448\n46772 17870\n9668 85036\n36704 83... | 670 | 8,294,400 | 0 | 15,960 | |
762 | Two strings | [
"binary search",
"hashing",
"strings",
"two pointers"
] | null | null | You are given two strings *a* and *b*. You have to remove the minimum possible number of consecutive (standing one after another) characters from string *b* in such a way that it becomes a subsequence of string *a*. It can happen that you will not need to remove any characters at all, or maybe you will have to remove all of the characters from *b* and make it empty.
Subsequence of string *s* is any such string that can be obtained by erasing zero or more characters (not necessarily consecutive) from string *s*. | The first line contains string *a*, and the second lineΒ β string *b*. Both of these strings are nonempty and consist of lowercase letters of English alphabet. The length of each string is no bigger than 105 characters. | On the first line output a subsequence of string *a*, obtained from *b* by erasing the minimum number of consecutive characters.
If the answer consists of zero characters, output Β«-Β» (a minus sign). | [
"hi\nbob\n",
"abca\naccepted\n",
"abacaba\nabcdcba\n"
] | [
"-\n",
"ac\n",
"abcba\n"
] | In the first example strings *a* and *b* don't share any symbols, so the longest string that you can get is empty.
In the second example ac is a subsequence of *a*, and at the same time you can obtain it by erasing consecutive symbols cepted from string *b*. | [
{
"input": "hi\nbob",
"output": "-"
},
{
"input": "abca\naccepted",
"output": "ac"
},
{
"input": "abacaba\nabcdcba",
"output": "abcba"
},
{
"input": "lo\neuhaqdhhzlnkmqnakgwzuhurqlpmdm",
"output": "-"
},
{
"input": "aaeojkdyuilpdvyewjfrftkpcobhcumwlaoiocbfdtvjkhgd... | 109 | 6,963,200 | 3 | 16,025 | |
835 | The penguin's game | [
"binary search",
"constructive algorithms",
"interactive"
] | null | null | Pay attention: this problem is interactive.
Penguin Xoriy came up with a new game recently. He has *n* icicles numbered from 1 to *n*. Each icicle has a temperatureΒ β an integer from 1 to 109. Exactly two of these icicles are special: their temperature is *y*, while a temperature of all the others is *x*<=β <=*y*. You have to find those special icicles. You can choose a non-empty subset of icicles and ask the penguin what is the bitwise exclusive OR (XOR) of the temperatures of the icicles in this subset. Note that you can't ask more than 19 questions.
You are to find the special icicles. | The first line contains three integers *n*, *x*, *y* (2<=β€<=*n*<=β€<=1000, 1<=β€<=*x*,<=*y*<=β€<=109, *x*<=β <=*y*)Β β the number of icicles, the temperature of non-special icicles and the temperature of the special icicles. | To give your answer to the penguin you have to print character "!" (without quotes), then print two integers *p*1, *p*2 (*p*1<=<<=*p*2)Β β the indexes of the special icicles in ascending order. Note that "!" and *p*1 should be separated by a space; the indexes should be separated by a space too. After you gave the answer your program should terminate immediately. | [
"4 2 1\n2\n1\n1"
] | [
"? 3 1 2 3\n? 1 1\n? 1 3\n! 1 3"
] | The answer for the first question is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/b32d8d96beb0d2be8d8a600f458c8cf2e2e28c54.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
The answer for the second and the third questions is 1, therefore, special icicles are indexes 1 and 3.
You can read more about bitwise XOR operation here: [https://en.wikipedia.org/wiki/Bitwise_operation#XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR). | [
{
"input": "4 2 1 1 3",
"output": "Correct answer 1 3, queries: 4."
},
{
"input": "6 1 2 5 6",
"output": "Correct answer 5 6, queries: 5."
},
{
"input": "2 4523 4235 1 2",
"output": "Correct answer 1 2, queries: 2."
},
{
"input": "511 42 1000000000 255 511",
"output": "Co... | 46 | 0 | 0 | 16,037 | |
914 | Travelling Salesman and Special Numbers | [
"brute force",
"combinatorics",
"dp"
] | null | null | The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer *x* and reduce it to the number of bits set to 1 in the binary representation of *x*. For example for number 13 it's true that 1310<==<=11012, so it has 3 bits set and 13 will be reduced to 3 in one operation.
He calls a number special if the minimum number of operations to reduce it to 1 is *k*.
He wants to find out how many special numbers exist which are not greater than *n*. Please help the Travelling Salesman, as he is about to reach his destination!
Since the answer can be large, output it modulo 109<=+<=7. | The first line contains integer *n* (1<=β€<=*n*<=<<=21000).
The second line contains integer *k* (0<=β€<=*k*<=β€<=1000).
Note that *n* is given in its binary representation without any leading zeros. | Output a single integerΒ β the number of special numbers not greater than *n*, modulo 109<=+<=7. | [
"110\n2\n",
"111111011\n2\n"
] | [
"3\n",
"169\n"
] | In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2). | [
{
"input": "110\n2",
"output": "3"
},
{
"input": "111111011\n2",
"output": "169"
},
{
"input": "100011110011110110100\n7",
"output": "0"
},
{
"input": "110100110\n0",
"output": "1"
},
{
"input": "10000000000000000000000000000000000000000000\n2",
"output": "792... | 124 | 21,606,400 | 0 | 16,066 | |
0 | none | [
"none"
] | null | null | You are given a sequence *a*1,<=*a*2,<=...,<=*a**n* consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order.
Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places.
Every element of the sequence must appear in exactly one subsequence. | The first line of input data contains integer *n* (1<=β€<=*n*<=β€<=105)Β β the length of the sequence.
The second line of input data contains *n* different integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=β€<=*a**i*<=β€<=109)Β β the elements of the sequence. It is guaranteed that all elements of the sequence are distinct. | In the first line print the maximum number of subsequences *k*, which the original sequence can be split into while fulfilling the requirements.
In the next *k* lines print the description of subsequences in the following format: the number of elements in subsequence *c**i* (0<=<<=*c**i*<=β€<=*n*), then *c**i* integers *l*1,<=*l*2,<=...,<=*l**c**i* (1<=β€<=*l**j*<=β€<=*n*)Β β indices of these elements in the original sequence.
Indices could be printed in any order. Every index from 1 to *n* must appear in output exactly once.
If there are several possible answers, print any of them. | [
"6\n3 2 1 6 5 4\n",
"6\n83 -75 -49 11 37 62\n"
] | [
"4\n2 1 3\n1 2\n2 4 6\n1 5\n",
"1\n6 1 2 3 4 5 6\n"
] | In the first sample output:
After sorting the first subsequence we will get sequence 1Β 2Β 3Β 6Β 5Β 4.
Sorting the second subsequence changes nothing.
After sorting the third subsequence we will get sequence 1Β 2Β 3Β 4Β 5Β 6.
Sorting the last subsequence changes nothing. | [
{
"input": "6\n3 2 1 6 5 4",
"output": "4\n2 1 3\n1 2\n2 4 6\n1 5"
},
{
"input": "6\n83 -75 -49 11 37 62",
"output": "1\n6 1 2 3 4 5 6"
},
{
"input": "1\n1",
"output": "1\n1 1"
},
{
"input": "2\n1 2",
"output": "2\n1 1\n1 2"
},
{
"input": "2\n2 1",
"output": "... | 1,000 | 9,932,800 | 0 | 16,082 | |
1,003 | Abbreviation | [
"dp",
"hashing",
"strings"
] | null | null | You are given a text consisting of $n$ space-separated words. There is exactly one space character between any pair of adjacent words. There are no spaces before the first word and no spaces after the last word. The length of text is the number of letters and spaces in it. $w_i$ is the $i$-th word of text. All words consist only of lowercase Latin letters.
Let's denote a segment of words $w[i..j]$ as a sequence of words $w_i, w_{i + 1}, \dots, w_j$. Two segments of words $w[i_1 .. j_1]$ and $w[i_2 .. j_2]$ are considered equal if $j_1 - i_1 = j_2 - i_2$, $j_1 \ge i_1$, $j_2 \ge i_2$, and for every $t \in [0, j_1 - i_1]$ $w_{i_1 + t} = w_{i_2 + t}$. For example, for the text "to be or not to be" the segments $w[1..2]$ and $w[5..6]$ are equal, they correspond to the words "to be".
An abbreviation is a replacement of some segments of words with their first uppercase letters. In order to perform an abbreviation, you have to choose at least two non-intersecting equal segments of words, and replace each chosen segment with the string consisting of first letters of the words in the segment (written in uppercase). For example, for the text "a ab a a b ab a a b c" you can replace segments of words $w[2..4]$ and $w[6..8]$ with an abbreviation "AAA" and obtain the text "a AAA b AAA b c", or you can replace segments of words $w[2..5]$ and $w[6..9]$ with an abbreviation "AAAB" and obtain the text "a AAAB AAAB c".
What is the minimum length of the text after at most one abbreviation? | The first line of the input contains one integer $n$ ($1 \le n \le 300$) β the number of words in the text.
The next line contains $n$ space-separated words of the text $w_1, w_2, \dots, w_n$. Each word consists only of lowercase Latin letters.
It is guaranteed that the length of text does not exceed $10^5$. | Print one integer β the minimum length of the text after at most one abbreviation. | [
"6\nto be or not to be\n",
"10\na ab a a b ab a a b c\n",
"6\naa bb aa aa bb bb\n"
] | [
"12\n",
"13\n",
"11\n"
] | In the first example you can obtain the text "TB or not TB".
In the second example you can obtain the text "a AAAB AAAB c".
In the third example you can obtain the text "AB aa AB bb". | [
{
"input": "6\nto be or not to be",
"output": "12"
},
{
"input": "10\na ab a a b ab a a b c",
"output": "13"
},
{
"input": "6\naa bb aa aa bb bb",
"output": "11"
},
{
"input": "45\nxr l pl sx c c u py sv j f x h u y w w bs u cp e ad ib b tz gy lm e s n ln kg fs rd ln v f sh t... | 92 | 2,150,400 | 0 | 16,104 | |
416 | Population Size | [
"greedy",
"implementation",
"math"
] | null | null | Polycarpus develops an interesting theory about the interrelation of arithmetic progressions with just everything in the world. His current idea is that the population of the capital of Berland changes over time like an arithmetic progression. Well, or like multiple arithmetic progressions.
Polycarpus believes that if he writes out the population of the capital for several consecutive years in the sequence *a*1,<=*a*2,<=...,<=*a**n*, then it is convenient to consider the array as several arithmetic progressions, written one after the other. For example, sequence (8,<=6,<=4,<=2,<=1,<=4,<=7,<=10,<=2) can be considered as a sequence of three arithmetic progressions (8,<=6,<=4,<=2), (1,<=4,<=7,<=10) and (2), which are written one after another.
Unfortunately, Polycarpus may not have all the data for the *n* consecutive years (a census of the population doesn't occur every year, after all). For this reason, some values of *a**i* ββmay be unknown. Such values are represented by number -1.
For a given sequence *a*<==<=(*a*1,<=*a*2,<=...,<=*a**n*), which consists of positive integers and values ββ-1, find the minimum number of arithmetic progressions Polycarpus needs to get *a*. To get *a*, the progressions need to be written down one after the other. Values ββ-1 may correspond to an arbitrary positive integer and the values *a**i*<=><=0 must be equal to the corresponding elements of sought consecutive record of the progressions.
Let us remind you that a finite sequence *c* is called an arithmetic progression if the difference *c**i*<=+<=1<=-<=*c**i* of any two consecutive elements in it is constant. By definition, any sequence of length 1 is an arithmetic progression. | The first line of the input contains integer *n* (1<=β€<=*n*<=β€<=2Β·105) β the number of elements in the sequence. The second line contains integer values *a*1,<=*a*2,<=...,<=*a**n* separated by a space (1<=β€<=*a**i*<=β€<=109 or *a**i*<==<=<=-<=1). | Print the minimum number of arithmetic progressions that you need to write one after another to get sequence *a*. The positions marked as -1 in *a* can be represented by any positive integers. | [
"9\n8 6 4 2 1 4 7 10 2\n",
"9\n-1 6 -1 2 -1 4 7 -1 2\n",
"5\n-1 -1 -1 -1 -1\n",
"7\n-1 -1 4 5 1 2 3\n"
] | [
"3\n",
"3\n",
"1\n",
"2\n"
] | none | [
{
"input": "9\n8 6 4 2 1 4 7 10 2",
"output": "3"
},
{
"input": "9\n-1 6 -1 2 -1 4 7 -1 2",
"output": "3"
},
{
"input": "5\n-1 -1 -1 -1 -1",
"output": "1"
},
{
"input": "7\n-1 -1 4 5 1 2 3",
"output": "2"
},
{
"input": "1\n1",
"output": "1"
},
{
"input... | 31 | 6,656,000 | 0 | 16,139 | |
0 | none | [
"none"
] | null | null | A two dimensional array is called a bracket array if each grid contains one of the two possible brackets β "(" or ")". A path through the two dimensional array cells is called monotonous if any two consecutive cells in the path are side-adjacent and each cell of the path is located below or to the right from the previous one.
A two dimensional array whose size equals *n*<=Γ<=*m* is called a correct bracket array, if any string formed by writing out the brackets on some monotonous way from cell (1,<=1) to cell (*n*,<=*m*) forms a correct bracket sequence.
Let's define the operation of comparing two correct bracket arrays of equal size (*a* and *b*) like that. Let's consider a given two dimensional array of priorities (*c*) β a two dimensional array of same size, containing different integers from 1 to *nm*. Let's find such position (*i*,<=*j*) in the two dimensional array, that *a**i*,<=*j*<=β <=*b**i*,<=*j*. If there are several such positions, let's choose the one where number *c**i*,<=*j* is minimum. If *a**i*,<=*j*<==<="(", then *a*<=<<=*b*, otherwise *a*<=><=*b*. If the position (*i*,<=*j*) is not found, then the arrays are considered equal.
Your task is to find a *k*-th two dimensional correct bracket array. It is guaranteed that for the given sizes of *n* and *m* there will be no less than *k* two dimensional correct bracket arrays. | The first line contains integers *n*, *m* and *k* β the sizes of the array and the number of the sought correct bracket array (1<=β€<=*n*,<=*m*<=β€<=100, 1<=β€<=*k*<=β€<=1018). Then an array of priorities is given, *n* lines each containing *m* numbers, number *p**i*,<=*j* shows the priority of character *j* in line *i* (1<=β€<=*p**i*,<=*j*<=β€<=*nm*, all *p**i*,<=*j* are different).
Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator. | Print the *k*-th two dimensional correct bracket array. | [
"1 2 1\n1 2\n",
"2 3 1\n1 2 3\n4 5 6\n",
"3 2 2\n3 6\n1 4\n2 5\n"
] | [
"()\n",
"(()\n())\n",
"()\n)(\n()\n"
] | In the first sample exists only one correct two-dimensional bracket array.
In the second and in the third samples two arrays exist.
A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters Β«+Β» and Β«1Β» into this sequence. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. | [] | 60 | 0 | 0 | 16,182 | |
148 | Escape | [
"implementation",
"math"
] | null | null | The princess is going to escape the dragon's cave, and she needs to plan it carefully.
The princess runs at *v**p* miles per hour, and the dragon flies at *v**d* miles per hour. The dragon will discover the escape after *t* hours and will chase the princess immediately. Looks like there's no chance to success, but the princess noticed that the dragon is very greedy and not too smart. To delay him, the princess decides to borrow a couple of bijous from his treasury. Once the dragon overtakes the princess, she will drop one bijou to distract him. In this case he will stop, pick up the item, return to the cave and spend *f* hours to straighten the things out in the treasury. Only after this will he resume the chase again from the very beginning.
The princess is going to run on the straight. The distance between the cave and the king's castle she's aiming for is *c* miles. How many bijous will she need to take from the treasury to be able to reach the castle? If the dragon overtakes the princess at exactly the same moment she has reached the castle, we assume that she reached the castle before the dragon reached her, and doesn't need an extra bijou to hold him off. | The input data contains integers *v**p*,<=*v**d*,<=*t*,<=*f* and *c*, one per line (1<=β€<=*v**p*,<=*v**d*<=β€<=100, 1<=β€<=*t*,<=*f*<=β€<=10, 1<=β€<=*c*<=β€<=1000). | Output the minimal number of bijous required for the escape to succeed. | [
"1\n2\n1\n1\n10\n",
"1\n2\n1\n1\n8\n"
] | [
"2\n",
"1\n"
] | In the first case one hour after the escape the dragon will discover it, and the princess will be 1 mile away from the cave. In two hours the dragon will overtake the princess 2 miles away from the cave, and she will need to drop the first bijou. Return to the cave and fixing the treasury will take the dragon two more hours; meanwhile the princess will be 4 miles away from the cave. Next time the dragon will overtake the princess 8 miles away from the cave, and she will need the second bijou, but after this she will reach the castle without any further trouble.
The second case is similar to the first one, but the second time the dragon overtakes the princess when she has reached the castle, and she won't need the second bijou. | [
{
"input": "1\n2\n1\n1\n10",
"output": "2"
},
{
"input": "1\n2\n1\n1\n8",
"output": "1"
},
{
"input": "5\n8\n1\n2\n100",
"output": "2"
},
{
"input": "2\n100\n10\n10\n739",
"output": "22"
},
{
"input": "17\n99\n2\n3\n293",
"output": "3"
},
{
"input": "5... | 122 | 0 | 3 | 16,183 | |
0 | none | [
"none"
] | null | null | It's a beautiful April day and Wallace is playing football with his friends. But his friends do not know that Wallace actually stayed home with Gromit and sent them his robotic self instead. Robo-Wallace has several advantages over the other guys. For example, he can hit the ball directly to the specified point. And yet, the notion of a giveaway is foreign to him. The combination of these features makes the Robo-Wallace the perfect footballer β as soon as the ball gets to him, he can just aim and hit the goal. He followed this tactics in the first half of the match, but he hit the goal rarely. The opposing team has a very good goalkeeper who catches most of the balls that fly directly into the goal. But Robo-Wallace is a quick thinker, he realized that he can cheat the goalkeeper. After all, they are playing in a football box with solid walls. Robo-Wallace can kick the ball to the other side, then the goalkeeper will not try to catch the ball. Then, if the ball bounces off the wall and flies into the goal, the goal will at last be scored.
Your task is to help Robo-Wallace to detect a spot on the wall of the football box, to which the robot should kick the ball, so that the ball bounces once and only once off this wall and goes straight to the goal. In the first half of the match Robo-Wallace got a ball in the head and was severely hit. As a result, some of the schemes have been damaged. Because of the damage, Robo-Wallace can only aim to his right wall (Robo-Wallace is standing with his face to the opposing team's goal).
The football box is rectangular. Let's introduce a two-dimensional coordinate system so that point (0, 0) lies in the lower left corner of the field, if you look at the box above. Robo-Wallace is playing for the team, whose goal is to the right. It is an improvised football field, so the gate of Robo-Wallace's rivals may be not in the middle of the left wall.
In the given coordinate system you are given:
- *y*1, *y*2 β the *y*-coordinates of the side pillars of the goalposts of robo-Wallace's opponents; - *y**w* β the *y*-coordinate of the wall to which Robo-Wallace is aiming; - *x**b*, *y**b* β the coordinates of the ball's position when it is hit; - *r* β the radius of the ball.
A goal is scored when the center of the ball crosses the *OY* axis in the given coordinate system between (0, *y*1) and (0, *y*2). The ball moves along a straight line. The ball's hit on the wall is perfectly elastic (the ball does not shrink from the hit), the angle of incidence equals the angle of reflection. If the ball bounces off the wall not to the goal, that is, if it hits the other wall or the goal post, then the opposing team catches the ball and Robo-Wallace starts looking for miscalculation and gets dysfunctional. Such an outcome, if possible, should be avoided. We assume that the ball touches an object, if the distance from the center of the ball to the object is no greater than the ball radius *r*. | The first and the single line contains integers *y*1, *y*2, *y**w*, *x**b*, *y**b*, *r* (1<=β€<=*y*1,<=*y*2,<=*y**w*,<=*x**b*,<=*y**b*<=β€<=106; *y*1<=<<=*y*2<=<<=*y**w*; *y**b*<=+<=*r*<=<<=*y**w*; 2Β·*r*<=<<=*y*2<=-<=*y*1).
It is guaranteed that the ball is positioned correctly in the field, doesn't cross any wall, doesn't touch the wall that Robo-Wallace is aiming at. The goal posts can't be located in the field corners. | If Robo-Wallace can't score a goal in the described manner, print "-1" (without the quotes). Otherwise, print a single number *x**w* β the abscissa of his point of aiming.
If there are multiple points of aiming, print the abscissa of any of them. When checking the correctness of the answer, all comparisons are made with the permissible absolute error, equal to 10<=-<=8.
It is recommended to print as many characters after the decimal point as possible. | [
"4 10 13 10 3 1\n",
"1 4 6 2 2 1\n",
"3 10 15 17 9 2\n"
] | [
"4.3750000000\n",
"-1\n",
"11.3333333333\n"
] | Note that in the first and third samples other correct values of abscissa *x*<sub class="lower-index">*w*</sub> are also possible. | [
{
"input": "4 10 13 10 3 1",
"output": "4.3750000000"
},
{
"input": "1 4 6 2 2 1",
"output": "-1"
},
{
"input": "3 10 15 17 9 2",
"output": "11.3333333333"
},
{
"input": "4 9 30 3 3 1",
"output": "-1"
},
{
"input": "4 9 13 2 3 1",
"output": "-1"
},
{
"... | 92 | 0 | 0 | 16,190 | |
297 | Fish Weight | [
"constructive algorithms",
"greedy"
] | null | null | It is known that there are *k* fish species in the polar ocean, numbered from 1 to *k*. They are sorted by non-decreasing order of their weight, which is a positive number. Let the weight of the *i*-th type of fish be *w**i*, then 0<=<<=*w*1<=β€<=*w*2<=β€<=...<=β€<=*w**k* holds.
Polar bears Alice and Bob each have caught some fish, and they are guessing who has the larger sum of weight of the fish he/she's caught. Given the type of the fish they've caught, determine whether it is possible that the fish caught by Alice has a strictly larger total weight than Bob's. In other words, does there exist a sequence of weights *w**i* (not necessary integers), such that the fish caught by Alice has a strictly larger total weight? | The first line contains three integers *n*,<=*m*,<=*k* (1<=β€<=*n*,<=*m*<=β€<=105,<=1<=β€<=*k*<=β€<=109) β the number of fish caught by Alice and Bob respectively, and the number of fish species.
The second line contains *n* integers each from 1 to *k*, the list of fish type caught by Alice. The third line contains *m* integers each from 1 to *k*, the list of fish type caught by Bob.
Note that one may have caught more than one fish for a same species. | Output "YES" (without quotes) if it is possible, and "NO" (without quotes) otherwise. | [
"3 3 3\n2 2 2\n1 1 3\n",
"4 7 9\n5 2 7 3\n3 5 2 7 3 8 7\n"
] | [
"YES\n",
"NO\n"
] | In the first sample, if *w*<sub class="lower-index">1</sub>β=β1,β*w*<sub class="lower-index">2</sub>β=β2,β*w*<sub class="lower-index">3</sub>β=β2.5, then Alice has a total of 2β+β2β+β2β=β6 weight units, while Bob only has 1β+β1β+β2.5β=β4.5.
In the second sample, the fish that Alice caught is a subset of Bob's. Therefore, the total weight of Bobβs fish is always not less than the total weight of Aliceβs fish. | [
{
"input": "3 3 3\n2 2 2\n1 1 3",
"output": "YES"
},
{
"input": "4 7 9\n5 2 7 3\n3 5 2 7 3 8 7",
"output": "NO"
},
{
"input": "5 5 10\n8 2 8 5 9\n9 1 7 5 1",
"output": "YES"
},
{
"input": "7 7 10\n8 2 8 10 6 9 10\n2 4 9 5 6 2 5",
"output": "YES"
},
{
"input": "15 ... | 30 | 0 | 0 | 16,217 | |
191 | Metro Scheme | [
"graphs",
"greedy"
] | null | null | Berland is very concerned with privacy, so almost all plans and blueprints are secret. However, a spy of the neighboring state managed to steal the Bertown subway scheme.
The Bertown Subway has *n* stations, numbered from 1 to *n*, and *m* bidirectional tunnels connecting them. All Bertown Subway consists of lines. To be more precise, there are two types of lines: circular and radial.
A radial line is a sequence of stations *v*1,<=...,<=*v**k* (*k*<=><=1), where stations *v**i* and *v**i*<=+<=1 (*i*<=<<=*k*) are connected by a tunnel and no station occurs in the line more than once (*v**i*<=β <=*v**j* for *i*<=β <=*j*).
A loop line is a series of stations, *v*1,<=...,<=*v**k* (*k*<=><=2), where stations *v**i* ΠΈ *v**i*<=+<=1 are connected by a tunnel. In addition, stations *v*1 and *v**k* are also connected by a tunnel. No station is occurs in the loop line more than once.
Note that a single station can be passed by any number of lines.
According to Berland standards, there can't be more than one tunnel between two stations and each tunnel belongs to exactly one line. Naturally, each line has at least one tunnel. Between any two stations there is the way along the subway tunnels. In addition, in terms of graph theory, a subway is a vertex cactus: if we consider the subway as a graph in which the stations are the vertexes and the edges are tunnels, then each vertex lies on no more than one simple cycle.
Unfortunately, scheme, stolen by the spy, had only the stations and the tunnels. It was impossible to determine to which line every tunnel corresponds. But to sabotage successfully, the spy needs to know what minimum and maximum number of lines may be in the Bertown subway.
Help him! | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=105, 0<=β€<=*m*<=β€<=3Β·105) β the number of stations and the number of tunnels, correspondingly.
Each of the next *m* lines contain two integers β the numbers of stations connected by the corresponding tunnel. The stations are numbered with integers from 1 to *n*.
It is guaranteed that the graph that corresponds to the subway has no multiple edges or loops, it is connected and it is a vertex cactus. | Print two numbers β the minimum and maximum number of lines correspondingly. | [
"3 3\n1 2\n2 3\n3 1\n",
"8 8\n1 2\n2 3\n3 4\n4 5\n6 4\n4 7\n7 2\n2 8\n",
"6 6\n1 2\n2 3\n2 5\n5 6\n3 4\n3 5\n"
] | [
"1 3\n",
"2 8\n",
"3 6\n"
] | The subway scheme with minimum possible number of lines for the second sample is: | [] | 186 | 0 | -1 | 16,220 | |
106 | Buns | [
"dp"
] | C. Buns | 2 | 256 | Lavrenty, a baker, is going to make several buns with stuffings and sell them.
Lavrenty has *n* grams of dough as well as *m* different stuffing types. The stuffing types are numerated from 1 to *m*. Lavrenty knows that he has *a**i* grams left of the *i*-th stuffing. It takes exactly *b**i* grams of stuffing *i* and *c**i* grams of dough to cook a bun with the *i*-th stuffing. Such bun can be sold for *d**i* tugriks.
Also he can make buns without stuffings. Each of such buns requires *c*0 grams of dough and it can be sold for *d*0 tugriks. So Lavrenty can cook any number of buns with different stuffings or without it unless he runs out of dough and the stuffings. Lavrenty throws away all excess material left after baking.
Find the maximum number of tugriks Lavrenty can earn. | The first line contains 4 integers *n*, *m*, *c*0 and *d*0 (1<=β€<=*n*<=β€<=1000, 1<=β€<=*m*<=β€<=10, 1<=β€<=*c*0,<=*d*0<=β€<=100). Each of the following *m* lines contains 4 integers. The *i*-th line contains numbers *a**i*, *b**i*, *c**i* and *d**i* (1<=β€<=*a**i*,<=*b**i*,<=*c**i*,<=*d**i*<=β€<=100). | Print the only number β the maximum number of tugriks Lavrenty can earn. | [
"10 2 2 1\n7 3 2 100\n12 3 1 10\n",
"100 1 25 50\n15 5 20 10\n"
] | [
"241",
"200"
] | To get the maximum number of tugriks in the first sample, you need to cook 2 buns with stuffing 1, 4 buns with stuffing 2 and a bun without any stuffing.
In the second sample Lavrenty should cook 4 buns without stuffings. | [
{
"input": "10 2 2 1\n7 3 2 100\n12 3 1 10",
"output": "241"
},
{
"input": "100 1 25 50\n15 5 20 10",
"output": "200"
},
{
"input": "10 1 5 2\n100 1 2 3",
"output": "15"
},
{
"input": "10 1 5 11\n3 1 3 8",
"output": "24"
},
{
"input": "10 2 11 5\n100 1 3 10\n100 1... | 1,620 | 6,963,200 | 3.58203 | 16,239 |
754 | Vladik and chat | [
"brute force",
"constructive algorithms",
"dp",
"implementation",
"strings"
] | null | null | Recently Vladik discovered a new entertainmentΒ β coding bots for social networks. He would like to use machine learning in his bots so now he want to prepare some learning data for them.
At first, he need to download *t* chats. Vladik coded a script which should have downloaded the chats, however, something went wrong. In particular, some of the messages have no information of their sender. It is known that if a person sends several messages in a row, they all are merged into a single message. It means that there could not be two or more messages in a row with the same sender. Moreover, a sender never mention himself in his messages.
Vladik wants to recover senders of all the messages so that each two neighboring messages will have different senders and no sender will mention himself in his messages.
He has no idea of how to do this, and asks you for help. Help Vladik to recover senders in each of the chats! | The first line contains single integer *t* (1<=β€<=*t*<=β€<=10) β the number of chats. The *t* chats follow. Each chat is given in the following format.
The first line of each chat description contains single integer *n* (1<=β€<=*n*<=β€<=100)Β β the number of users in the chat.
The next line contains *n* space-separated distinct usernames. Each username consists of lowercase and uppercase English letters and digits. The usernames can't start with a digit. Two usernames are different even if they differ only with letters' case. The length of username is positive and doesn't exceed 10 characters.
The next line contains single integer *m* (1<=β€<=*m*<=β€<=100)Β β the number of messages in the chat. The next *m* line contain the messages in the following formats, one per line:
- <username>:<text>Β β the format of a message with known sender. The username should appear in the list of usernames of the chat. - <?>:<text>Β β the format of a message with unknown sender.
The text of a message can consist of lowercase and uppercase English letter, digits, characters '.' (dot), ',' (comma), '!' (exclamation mark), '?' (question mark) and ' ' (space). The text doesn't contain trailing spaces. The length of the text is positive and doesn't exceed 100 characters.
We say that a text mention a user if his username appears in the text as a word. In other words, the username appears in a such a position that the two characters before and after its appearance either do not exist or are not English letters or digits. For example, the text "Vasya, masha13 and Kate!" can mention users "Vasya", "masha13", "and" and "Kate", but not "masha".
It is guaranteed that in each chat no known sender mention himself in his messages and there are no two neighboring messages with the same known sender. | Print the information about the *t* chats in the following format:
If it is not possible to recover senders, print single line "Impossible" for this chat. Otherwise print *m* messages in the following format:
<username>:<text>
If there are multiple answers, print any of them. | [
"1\n2\nVladik netman\n2\n?: Hello, Vladik!\n?: Hi\n",
"1\n2\nnetman vladik\n3\nnetman:how are you?\n?:wrong message\nvladik:im fine\n",
"2\n3\nnetman vladik Fedosik\n2\n?: users are netman, vladik, Fedosik\nvladik: something wrong with this chat\n4\nnetman tigerrrrr banany2001 klinchuh\n4\n?: tigerrrrr, banany2... | [
"netman: Hello, Vladik!\nVladik: Hi\n",
"Impossible\n",
"Impossible\nnetman: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?\nklinchuh: yes, coach!\ntigerrrrr: yes, netman\nbanany2001: yes of course.\n"
] | none | [
{
"input": "1\n2\nVladik netman\n2\n?: Hello, Vladik!\n?: Hi",
"output": "netman: Hello, Vladik!\nVladik: Hi"
},
{
"input": "1\n2\nnetman vladik\n3\nnetman:how are you?\n?:wrong message\nvladik:im fine",
"output": "Impossible"
},
{
"input": "2\n3\nnetman vladik Fedosik\n2\n?: users are n... | 124 | 0 | 0 | 16,270 | |
416 | President's Path | [
"dp",
"graphs",
"shortest paths"
] | null | null | Good old Berland has *n* cities and *m* roads. Each road connects a pair of distinct cities and is bidirectional. Between any pair of cities, there is at most one road. For each road, we know its length.
We also know that the President will soon ride along the Berland roads from city *s* to city *t*. Naturally, he will choose one of the shortest paths from *s* to *t*, but nobody can say for sure which path he will choose.
The Minister for Transport is really afraid that the President might get upset by the state of the roads in the country. That is the reason he is planning to repair the roads in the possible President's path.
Making the budget for such an event is not an easy task. For all possible distinct pairs *s*,<=*t* (*s*<=<<=*t*) find the number of roads that lie on at least one shortest path from *s* to *t*. | The first line of the input contains integers *n*,<=*m* (2<=β€<=*n*<=β€<=500, 0<=β€<=*m*<=β€<=*n*Β·(*n*<=-<=1)<=/<=2) β the number of cities and roads, correspondingly. Then *m* lines follow, containing the road descriptions, one description per line. Each description contains three integers *x**i*,<=*y**i*,<=*l**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=*n*,<=*x**i*<=β <=*y**i*,<=1<=β€<=*l**i*<=β€<=106), where *x**i*,<=*y**i* are the numbers of the cities connected by the *i*-th road and *l**i* is its length. | Print the sequence of integers *c*12,<=*c*13,<=...,<=*c*1*n*,<=*c*23,<=*c*24,<=...,<=*c*2*n*,<=...,<=*c**n*<=-<=1,<=*n*, where *c**st* is the number of roads that can lie on the shortest path from *s* to *t*. Print the elements of sequence *c* in the described order. If the pair of cities *s* and *t* don't have a path between them, then *c**st*<==<=0. | [
"5 6\n1 2 1\n2 3 1\n3 4 1\n4 1 1\n2 4 2\n4 5 4\n"
] | [
"1 4 1 2 1 5 6 1 2 1 "
] | none | [] | 31 | 0 | 0 | 16,271 | |
117 | Cycle | [
"dfs and similar",
"graphs"
] | null | null | A tournament is a directed graph without self-loops in which every pair of vertexes is connected by exactly one directed edge. That is, for any two vertexes *u* and *v* (*u*<=β <=*v*) exists either an edge going from *u* to *v*, or an edge from *v* to *u*.
You are given a tournament consisting of *n* vertexes. Your task is to find there a cycle of length three. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=5000). Next *n* lines contain the adjacency matrix *A* of the graph (without spaces). *A**i*,<=*j*<==<=1 if the graph has an edge going from vertex *i* to vertex *j*, otherwise *A**i*,<=*j*<==<=0. *A**i*,<=*j* stands for the *j*-th character in the *i*-th line.
It is guaranteed that the given graph is a tournament, that is, *A**i*,<=*i*<==<=0,<=*A**i*,<=*j*<=β <=*A**j*,<=*i* (1<=β€<=*i*,<=*j*<=β€<=*n*,<=*i*<=β <=*j*). | Print three distinct vertexes of the graph *a*1, *a*2, *a*3 (1<=β€<=*a**i*<=β€<=*n*), such that *A**a*1,<=*a*2<==<=*A**a*2,<=*a*3<==<=*A**a*3,<=*a*1<==<=1, or "-1", if a cycle whose length equals three does not exist.
If there are several solutions, print any of them. | [
"5\n00100\n10000\n01001\n11101\n11000\n",
"5\n01111\n00000\n01000\n01100\n01110\n"
] | [
"1 3 2 ",
"-1\n"
] | none | [
{
"input": "5\n00100\n10000\n01001\n11101\n11000",
"output": "1 3 2 "
},
{
"input": "5\n01111\n00000\n01000\n01100\n01110",
"output": "-1"
},
{
"input": "5\n01000\n00101\n10010\n11001\n10100",
"output": "1 2 3 "
},
{
"input": "5\n00110\n10110\n00011\n00000\n11010",
"outpu... | 0 | 0 | -1 | 16,292 | |
852 | Dating | [
"brute force",
"dfs and similar",
"graphs",
"trees"
] | null | null | This story is happening in a town named BubbleLand. There are *n* houses in BubbleLand. In each of these *n* houses lives a boy or a girl. People there really love numbers and everyone has their favorite number *f*. That means that the boy or girl that lives in the *i*-th house has favorite number equal to *f**i*.
The houses are numerated with numbers 1 to *n*.
The houses are connected with *n*<=-<=1 bidirectional roads and you can travel from any house to any other house in the town. There is exactly one path between every pair of houses.
A new dating had agency opened their offices in this mysterious town and the citizens were very excited. They immediately sent *q* questions to the agency and each question was of the following format:
- *a* *b*Β β asking how many ways are there to choose a couple (boy and girl) that have the same favorite number and live in one of the houses on the unique path from house *a* to house *b*.
Help the dating agency to answer the questions and grow their business. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=105), the number of houses in the town.
The second line contains *n* integers, where the *i*-th number is 1 if a boy lives in the *i*-th house or 0 if a girl lives in *i*-th house.
The third line contains *n* integers, where the *i*-th number represents the favorite number *f**i* (1<=β€<=*f**i*<=β€<=109) of the girl or boy that lives in the *i*-th house.
The next *n*<=-<=1 lines contain information about the roads and the *i*-th line contains two integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*) which means that there exists road between those two houses. It is guaranteed that it's possible to reach any house from any other.
The following line contains an integer *q* (1<=β€<=*q*<=β€<=105), the number of queries.
Each of the following *q* lines represents a question and consists of two integers *a* and *b* (1<=β€<=*a*,<=*b*<=β€<=*n*). | For each of the *q* questions output a single number, the answer to the citizens question. | [
"7\n1 0 0 1 0 1 0\n9 2 9 2 2 9 9\n2 6\n1 2\n4 2\n6 5\n3 6\n7 4\n2\n1 3\n7 5\n"
] | [
"2\n3\n"
] | In the first question from house 1 to house 3, the potential couples are (1,β3) and (6,β3).
In the second question from house 7 to house 5, the potential couples are (7,β6), (4,β2) and (4,β5). | [] | 46 | 0 | 0 | 16,294 | |
0 | none | [
"none"
] | null | null | Emuskald considers himself a master of flow algorithms. Now he has completed his most ingenious program yet β it calculates the maximum flow in an undirected graph. The graph consists of *n* vertices and *m* edges. Vertices are numbered from 1 to *n*. Vertices 1 and *n* being the source and the sink respectively.
However, his max-flow algorithm seems to have a little flaw β it only finds the flow volume for each edge, but not its direction. Help him find for each edge the direction of the flow through this edges. Note, that the resulting flow should be correct maximum flow.
More formally. You are given an undirected graph. For each it's undirected edge (*a**i*, *b**i*) you are given the flow volume *c**i*. You should direct all edges in such way that the following conditions hold:
1. for each vertex *v* (1<=<<=*v*<=<<=*n*), sum of *c**i* of incoming edges is equal to the sum of *c**i* of outcoming edges; 1. vertex with number 1 has no incoming edges; 1. the obtained directed graph does not have cycles. | The first line of input contains two space-separated integers *n* and *m* (2<=β€<=*n*<=β€<=2Β·105, *n*<=-<=1<=β€<=*m*<=β€<=2Β·105), the number of vertices and edges in the graph. The following *m* lines contain three space-separated integers *a**i*, *b**i* and *c**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*, *a**i*<=β <=*b**i*, 1<=β€<=*c**i*<=β€<=104), which means that there is an undirected edge from *a**i* to *b**i* with flow volume *c**i*.
It is guaranteed that there are no two edges connecting the same vertices; the given graph is connected; a solution always exists. | Output *m* lines, each containing one integer *d**i*, which should be 0 if the direction of the *i*-th edge is *a**i*<=β<=*b**i* (the flow goes from vertex *a**i* to vertex *b**i*) and should be 1 otherwise. The edges are numbered from 1 to *m* in the order they are given in the input.
If there are several solutions you can print any of them. | [
"3 3\n3 2 10\n1 2 10\n3 1 5\n",
"4 5\n1 2 10\n1 3 10\n2 3 5\n4 2 15\n3 4 5\n"
] | [
"1\n0\n1\n",
"0\n0\n1\n1\n0\n"
] | In the first test case, 10 flow units pass through path <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/609340f155794c4e9eebcd9cdfa23c73cf982f28.png" style="max-width: 100.0%;max-height: 100.0%;"/>, and 5 flow units pass directly from source to sink: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/04481aced8a9d501ae5d785ab654c542ff5497a1.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [
{
"input": "3 3\n3 2 10\n1 2 10\n3 1 5",
"output": "1\n0\n1"
},
{
"input": "4 5\n1 2 10\n1 3 10\n2 3 5\n4 2 15\n3 4 5",
"output": "0\n0\n1\n1\n0"
},
{
"input": "10 17\n8 1 1\n4 8 2\n7 10 8\n1 4 1\n5 4 3\n6 9 6\n3 5 4\n1 9 1\n3 9 5\n7 1 1\n1 2 1\n1 3 1\n6 7 7\n8 2 1\n1 10 1\n1 5 1\n6 1 1"... | 2,000 | 67,174,400 | 0 | 16,315 | |
0 | none | [
"none"
] | null | null | ΠΠ°ΠΌΡΡΡ ΠΊΠΎΠΌΠΏΡΡΡΠ΅ΡΠ° ΡΠΎΡΡΠΎΠΈΡ ΠΈΠ· *n* ΡΡΠ΅Π΅ΠΊ, ΠΊΠΎΡΠΎΡΡΠ΅ Π²ΡΡΡΡΠΎΠ΅Π½Ρ Π² ΡΡΠ΄. ΠΡΠΎΠ½ΡΠΌΠ΅ΡΡΠ΅ΠΌ ΡΡΠ΅ΠΉΠΊΠΈ ΠΎΡ 1 Π΄ΠΎ *n* ΡΠ»Π΅Π²Π° Π½Π°ΠΏΡΠ°Π²ΠΎ. ΠΡΠΎ ΠΊΠ°ΠΆΠ΄ΡΡ ΡΡΠ΅ΠΉΠΊΡ ΠΈΠ·Π²Π΅ΡΡΠ½ΠΎ, ΡΠ²ΠΎΠ±ΠΎΠ΄Π½Π° ΠΎΠ½Π° ΠΈΠ»ΠΈ ΠΏΡΠΈΠ½Π°Π΄Π»Π΅ΠΆΠΈΡ ΠΊΠ°ΠΊΠΎΠΌΡ-Π»ΠΈΠ±ΠΎ ΠΏΡΠΎΡΠ΅ΡΡΡ (Π² ΡΠ°ΠΊΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ ΠΈΠ·Π²Π΅ΡΡΠ΅Π½ ΠΏΡΠΎΡΠ΅ΡΡ, ΠΊΠΎΡΠΎΡΠΎΠΌΡ ΠΎΠ½Π° ΠΏΡΠΈΠ½Π°Π΄Π»Π΅ΠΆΠΈΡ).
ΠΠ»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΏΡΠΎΡΠ΅ΡΡΠ° ΠΈΠ·Π²Π΅ΡΡΠ½ΠΎ, ΡΡΠΎ ΠΏΡΠΈΠ½Π°Π΄Π»Π΅ΠΆΠ°ΡΠΈΠ΅ Π΅ΠΌΡ ΡΡΠ΅ΠΉΠΊΠΈ Π·Π°Π½ΠΈΠΌΠ°ΡΡ Π² ΠΏΠ°ΠΌΡΡΠΈ Π½Π΅ΠΏΡΠ΅ΡΡΠ²Π½ΡΠΉ ΡΡΠ°ΡΡΠΎΠΊ. Π‘ ΠΏΠΎΠΌΠΎΡΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ Π²ΠΈΠ΄Π° Β«ΠΏΠ΅ΡΠ΅ΠΏΠΈΡΠ°ΡΡ Π΄Π°Π½Π½ΡΠ΅ ΠΈΠ· Π·Π°Π½ΡΡΠΎΠΉ ΡΡΠ΅ΠΉΠΊΠΈ Π² ΡΠ²ΠΎΠ±ΠΎΠ΄Π½ΡΡ, Π° Π·Π°Π½ΡΡΡΡ ΡΠ΅ΠΏΠ΅ΡΡ ΡΡΠΈΡΠ°ΡΡ ΡΠ²ΠΎΠ±ΠΎΠ΄Π½ΠΎΠΉΒ» ΡΡΠ΅Π±ΡΠ΅ΡΡΡ ΡΠ°ΡΠΏΠΎΠ»ΠΎΠΆΠΈΡΡ Π²ΡΠ΅ ΠΏΡΠΈΠ½Π°Π΄Π»Π΅ΠΆΠ°ΡΠΈΠ΅ ΠΏΡΠΎΡΠ΅ΡΡΠ°ΠΌ ΡΡΠ΅ΠΉΠΊΠΈ Π² Π½Π°ΡΠ°Π»Π΅ ΠΏΠ°ΠΌΡΡΠΈ ΠΊΠΎΠΌΠΏΡΡΡΠ΅ΡΠ°. ΠΡΡΠ³ΠΈΠΌΠΈ ΡΠ»ΠΎΠ²Π°ΠΌΠΈ, Π»ΡΠ±Π°Ρ ΡΠ²ΠΎΠ±ΠΎΠ΄Π½Π°Ρ ΡΡΠ΅ΠΉΠΊΠ° Π΄ΠΎΠ»ΠΆΠ½Π° ΡΠ°ΡΠΏΠΎΠ»Π°Π³Π°ΡΡΡΡ ΠΏΡΠ°Π²Π΅Π΅ (ΠΈΠΌΠ΅ΡΡ Π±ΠΎΠ»ΡΡΠΈΠΉ Π½ΠΎΠΌΠ΅Ρ) Π»ΡΠ±ΠΎΠΉ Π·Π°Π½ΡΡΠΎΠΉ.
ΠΠ°ΠΌ Π½Π΅ΠΎΠ±Ρ
ΠΎΠ΄ΠΈΠΌΠΎ Π½Π°ΠΉΡΠΈ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ ΠΏΠ΅ΡΠ΅ΠΏΠΈΡΡΠ²Π°Π½ΠΈΡ Π΄Π°Π½Π½ΡΡ
ΠΈΠ· ΠΎΠ΄Π½ΠΎΠΉ ΡΡΠ΅ΠΉΠΊΠΈ Π² Π΄ΡΡΠ³ΡΡ, Ρ ΠΏΠΎΠΌΠΎΡΡΡ ΠΊΠΎΡΠΎΡΡΡ
ΠΌΠΎΠΆΠ½ΠΎ Π΄ΠΎΡΡΠΈΡΡ ΠΎΠΏΠΈΡΠ°Π½Π½ΡΡ
ΡΡΠ»ΠΎΠ²ΠΈΠΉ. ΠΠΎΠΏΡΡΡΠΈΠΌΠΎ, ΡΡΠΎ ΠΎΡΠ½ΠΎΡΠΈΡΠ΅Π»ΡΠ½ΡΠΉ ΠΏΠΎΡΡΠ΄ΠΎΠΊ ΡΡΠ΅Π΅ΠΊ Π² ΠΏΠ°ΠΌΡΡΠΈ Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΈΠ· ΠΏΡΠΎΡΠ΅ΡΡΠΎΠ² ΠΈΠ·ΠΌΠ΅Π½ΠΈΡΡΡ ΠΏΠΎΡΠ»Π΅ Π΄Π΅ΡΡΠ°Π³ΠΌΠ΅Π½ΡΠ°ΡΠΈΠΈ, Π½ΠΎ ΠΎΡΠ½ΠΎΡΠΈΡΠ΅Π»ΡΠ½ΡΠΉ ΠΏΠΎΡΡΠ΄ΠΎΠΊ ΡΠ°ΠΌΠΈΡ
ΠΏΡΠΎΡΠ΅ΡΡΠΎΠ² Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΎΡΡΠ°ΡΡΡΡ Π±Π΅Π· ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠΉ. ΠΡΠΎ Π·Π½Π°ΡΠΈΡ, ΡΡΠΎ Π΅ΡΠ»ΠΈ Π²ΡΠ΅ ΡΡΠ΅ΠΉΠΊΠΈ, ΠΏΡΠΈΠ½Π°Π΄Π»Π΅ΠΆΠ°ΡΠΈΠ΅ ΠΏΡΠΎΡΠ΅ΡΡΡ *i*, Π½Π°Ρ
ΠΎΠ΄ΠΈΠ»ΠΈΡΡ Π² ΠΏΠ°ΠΌΡΡΠΈ ΡΠ°Π½ΡΡΠ΅ Π²ΡΠ΅Ρ
ΡΡΠ΅Π΅ΠΊ ΠΏΡΠΎΡΠ΅ΡΡΠ° *j*, ΡΠΎ ΠΈ ΠΏΠΎΡΠ»Π΅ ΠΏΠ΅ΡΠ΅ΠΌΠ΅ΡΠ΅Π½ΠΈΠΉ ΡΡΠΎ ΡΡΠ»ΠΎΠ²ΠΈΠ΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ Π²ΡΠΏΠΎΠ»Π½ΡΡΡΡΡ.
Π‘ΡΠΈΡΠ°ΠΉΡΠ΅, ΡΡΠΎ Π½ΠΎΠΌΠ΅ΡΠ° Π²ΡΠ΅Ρ
ΠΏΡΠΎΡΠ΅ΡΡΠΎΠ² ΡΠ½ΠΈΠΊΠ°Π»ΡΠ½Ρ, Ρ
ΠΎΡΡ Π±Ρ ΠΎΠ΄Π½Π° ΡΡΠ΅ΠΉΠΊΠ° ΠΏΠ°ΠΌΡΡΠΈ Π·Π°Π½ΡΡΠ° ΠΊΠ°ΠΊΠΈΠΌ-Π»ΠΈΠ±ΠΎ ΠΏΡΠΎΡΠ΅ΡΡΠΎΠΌ. | Π ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
Π·Π°ΠΏΠΈΡΠ°Π½ΠΎ ΡΠΈΡΠ»ΠΎ *n* (1<=β€<=*n*<=β€<=200<=000)Β β ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΡΠ΅Π΅ΠΊ Π² ΠΏΠ°ΠΌΡΡΠΈ ΠΊΠΎΠΌΠΏΡΡΡΠ΅ΡΠ°.
ΠΠΎ Π²ΡΠΎΡΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
ΡΠ»Π΅Π΄ΡΡΡ *n* ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π» *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=*n*), Π³Π΄Π΅ *a**i* ΡΠ°Π²Π½ΠΎ Π»ΠΈΠ±ΠΎ 0 (ΡΡΠΎ ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ, ΡΡΠΎ *i*-Ρ ΡΡΠ΅ΠΉΠΊΠ° ΠΏΠ°ΠΌΡΡΠΈ ΡΠ²ΠΎΠ±ΠΎΠ΄Π½Π°), Π»ΠΈΠ±ΠΎ Π½ΠΎΠΌΠ΅ΡΡ ΠΏΡΠΎΡΠ΅ΡΡΠ°, ΠΊΠΎΡΠΎΡΠΎΠΌΡ ΠΏΡΠΈΠ½Π°Π΄Π»Π΅ΠΆΠΈΡ *i*-Ρ ΡΡΠ΅ΠΉΠΊΠ° ΠΏΠ°ΠΌΡΡΠΈ. ΠΠ°ΡΠ°Π½ΡΠΈΡΡΠ΅ΡΡΡ, ΡΡΠΎ Ρ
ΠΎΡΡ Π±Ρ ΠΎΠ΄Π½ΠΎ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ *a**i* Π½Π΅ ΡΠ°Π²Π½ΠΎ 0.
ΠΡΠΎΡΠ΅ΡΡΡ ΠΏΡΠΎΠ½ΡΠΌΠ΅ΡΠΎΠ²Π°Π½Ρ ΡΠ΅Π»ΡΠΌΠΈ ΡΠΈΡΠ»Π°ΠΌΠΈ ΠΎΡ 1 Π΄ΠΎ *n* Π² ΠΏΡΠΎΠΈΠ·Π²ΠΎΠ»ΡΠ½ΠΎΠΌ ΠΏΠΎΡΡΠ΄ΠΊΠ΅. ΠΡΠΈ ΡΡΠΎΠΌ ΠΏΡΠΎΡΠ΅ΡΡΡ Π½Π΅ ΠΎΠ±ΡΠ·Π°ΡΠ΅Π»ΡΠ½ΠΎ ΠΏΡΠΎΠ½ΡΠΌΠ΅ΡΠΎΠ²Π°Π½Ρ ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°ΡΠ΅Π»ΡΠ½ΡΠΌΠΈ ΡΠΈΡΠ»Π°ΠΌΠΈ. | ΠΡΠ²Π΅Π΄ΠΈΡΠ΅ ΠΎΠ΄Π½ΠΎ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎΒ β ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π½ΡΠΆΠ½ΠΎ ΡΠ΄Π΅Π»Π°ΡΡ Π΄Π»Ρ Π΄Π΅ΡΡΠ°Π³ΠΌΠ΅Π½ΡΠ°ΡΠΈΠΈ ΠΏΠ°ΠΌΡΡΠΈ. | [
"4\n0 2 2 1\n",
"8\n0 8 8 8 0 4 4 2\n"
] | [
"2\n",
"4\n"
] | Π ΠΏΠ΅ΡΠ²ΠΎΠΌ ΡΠ΅ΡΡΠΎΠ²ΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ Π΄ΠΎΡΡΠ°ΡΠΎΡΠ½ΠΎ Π΄Π²ΡΡ
ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ:
1. ΠΠ΅ΡΠ΅ΠΏΠΈΡΠ°ΡΡ Π΄Π°Π½Π½ΡΠ΅ ΠΈΠ· ΡΡΠ΅ΡΡΠ΅ΠΉ ΡΡΠ΅ΠΉΠΊΠΈ Π² ΠΏΠ΅ΡΠ²ΡΡ. ΠΠΎΡΠ»Π΅ ΡΡΠΎΠ³ΠΎ ΠΏΠ°ΠΌΡΡΡ ΠΊΠΎΠΌΠΏΡΡΡΠ΅ΡΠ° ΠΏΡΠΈΠΌΠ΅Ρ Π²ΠΈΠ΄: 2Β 2Β 0Β 1. 1. ΠΠ΅ΡΠ΅ΠΏΠΈΡΠ°ΡΡ Π΄Π°Π½Π½ΡΠ΅ ΠΈΠ· ΡΠ΅ΡΠ²Π΅ΡΡΠΎΠΉ ΡΡΠ΅ΠΉΠΊΠΈ Π² ΡΡΠ΅ΡΡΡ. ΠΠΎΡΠ»Π΅ ΡΡΠΎΠ³ΠΎ ΠΏΠ°ΠΌΡΡΡ ΠΊΠΎΠΌΠΏΡΡΡΠ΅ΡΠ° ΠΏΡΠΈΠΌΠ΅Ρ Π²ΠΈΠ΄: 2Β 2Β 1Β 0. | [
{
"input": "4\n0 2 2 1",
"output": "2"
},
{
"input": "8\n0 8 8 8 0 4 4 2",
"output": "4"
},
{
"input": "5\n0 0 0 1 1",
"output": "2"
},
{
"input": "6\n0 0 0 3 0 0",
"output": "1"
},
{
"input": "10\n0 10 10 0 0 3 3 0 0 0",
"output": "3"
},
{
"input": "1... | 2,000 | 13,209,600 | 0 | 16,327 | |
362 | Petya and Pipes | [
"flows",
"graphs",
"shortest paths"
] | null | null | A little boy Petya dreams of growing up and becoming the Head Berland Plumber. He is thinking of the problems he will have to solve in the future. Unfortunately, Petya is too inexperienced, so you are about to solve one of such problems for Petya, the one he's the most interested in.
The Berland capital has *n* water tanks numbered from 1 to *n*. These tanks are connected by unidirectional pipes in some manner. Any pair of water tanks is connected by at most one pipe in each direction. Each pipe has a strictly positive integer width. Width determines the number of liters of water per a unit of time this pipe can transport. The water goes to the city from the main water tank (its number is 1). The water must go through some pipe path and get to the sewer tank with cleaning system (its number is *n*).
Petya wants to increase the width of some subset of pipes by at most *k* units in total so that the width of each pipe remains integer. Help him determine the maximum amount of water that can be transmitted per a unit of time from the main tank to the sewer tank after such operation is completed. | The first line contains two space-separated integers *n* and *k* (2<=β€<=*n*<=β€<=50, 0<=β€<=*k*<=β€<=1000). Then follow *n* lines, each line contains *n* integers separated by single spaces. The *i*<=+<=1-th row and *j*-th column contain number *c**ij* β the width of the pipe that goes from tank *i* to tank *j* (0<=β€<=*c**ij*<=β€<=106,<=*c**ii*<==<=0). If *c**ij*<==<=0, then there is no pipe from tank *i* to tank *j*. | Print a single integer β the maximum amount of water that can be transmitted from the main tank to the sewer tank per a unit of time. | [
"5 7\n0 1 0 2 0\n0 0 4 10 0\n0 0 0 0 5\n0 0 0 0 10\n0 0 0 0 0\n",
"5 10\n0 1 0 0 0\n0 0 2 0 0\n0 0 0 3 0\n0 0 0 0 4\n100 0 0 0 0\n"
] | [
"10\n",
"5\n"
] | In the first test Petya can increase width of the pipe that goes from the 1st to the 2nd water tank by 7 units.
In the second test Petya can increase width of the pipe that goes from the 1st to the 2nd water tank by 4 units, from the 2nd to the 3rd water tank by 3 units, from the 3rd to the 4th water tank by 2 units and from the 4th to 5th water tank by 1 unit. | [] | 31 | 0 | 0 | 16,358 | |
883 | Orientation of Edges | [
"dfs and similar",
"graphs"
] | null | null | Vasya has a graph containing both directed (oriented) and undirected (non-oriented) edges. There can be multiple edges between a pair of vertices.
Vasya has picked a vertex *s* from the graph. Now Vasya wants to create two separate plans:
1. to orient each undirected edge in one of two possible directions to maximize number of vertices reachable from vertex *s*; 1. to orient each undirected edge in one of two possible directions to minimize number of vertices reachable from vertex *s*.
In each of two plans each undirected edge must become directed. For an edge chosen directions can differ in two plans.
Help Vasya find the plans. | The first line contains three integers *n*, *m* and *s* (2<=β€<=*n*<=β€<=3Β·105, 1<=β€<=*m*<=β€<=3Β·105, 1<=β€<=*s*<=β€<=*n*) β number of vertices and edges in the graph, and the vertex Vasya has picked.
The following *m* lines contain information about the graph edges. Each line contains three integers *t**i*, *u**i* and *v**i* (1<=β€<=*t**i*<=β€<=2, 1<=β€<=*u**i*,<=*v**i*<=β€<=*n*, *u**i*<=β <=*v**i*) β edge type and vertices connected by the edge. If *t**i*<==<=1 then the edge is directed and goes from the vertex *u**i* to the vertex *v**i*. If *t**i*<==<=2 then the edge is undirected and it connects the vertices *u**i* and *v**i*.
It is guaranteed that there is at least one undirected edge in the graph. | The first two lines should describe the plan which maximizes the number of reachable vertices. The lines three and four should describe the plan which minimizes the number of reachable vertices.
A description of each plan should start with a line containing the number of reachable vertices. The second line of a plan should consist of *f* symbols '+' and '-', where *f* is the number of undirected edges in the initial graph. Print '+' as the *j*-th symbol of the string if the *j*-th undirected edge (*u*,<=*v*) from the input should be oriented from *u* to *v*. Print '-' to signify the opposite direction (from *v* to *u*). Consider undirected edges to be numbered in the same order they are given in the input.
If there are multiple solutions, print any of them. | [
"2 2 1\n1 1 2\n2 2 1\n",
"6 6 3\n2 2 6\n1 4 5\n2 3 4\n1 4 1\n1 3 1\n2 2 3\n"
] | [
"2\n-\n2\n+\n",
"6\n++-\n2\n+-+\n"
] | none | [
{
"input": "2 2 1\n1 1 2\n2 2 1",
"output": "2\n-\n2\n+"
},
{
"input": "6 6 3\n2 2 6\n1 4 5\n2 3 4\n1 4 1\n1 3 1\n2 2 3",
"output": "6\n++-\n2\n+-+"
},
{
"input": "5 5 5\n2 5 3\n1 2 3\n1 4 5\n2 5 2\n1 2 1",
"output": "4\n++\n1\n--"
},
{
"input": "13 18 9\n2 3 10\n1 12 10\n1 1... | 530 | 20,480,000 | -1 | 16,378 | |
656 | Ace It! | [
"*special"
] | null | null | The only line of the input is a string of 7 characters. The first character is letter A, followed by 6 digits. The input is guaranteed to be valid (for certain definition of "valid").
Output a single integer. | The only line of the input is a string of 7 characters. The first character is letter A, followed by 6 digits. The input is guaranteed to be valid (for certain definition of "valid"). | Output a single integer. | [
"A221033\n",
"A223635\n",
"A232726\n"
] | [
"21\n",
"22\n",
"23\n"
] | none | [
{
"input": "A221033",
"output": "21"
},
{
"input": "A223635",
"output": "22"
},
{
"input": "A232726",
"output": "23"
},
{
"input": "A102210",
"output": "25"
},
{
"input": "A231010",
"output": "26"
},
{
"input": "A222222",
"output": "13"
},
{
... | 46 | 0 | 3 | 16,408 | |
786 | Berzerk | [
"dfs and similar",
"dp",
"games"
] | null | null | Rick and Morty are playing their own version of Berzerk (which has nothing in common with the famous Berzerk game). This game needs a huge space, so they play it with a computer.
In this game there are *n* objects numbered from 1 to *n* arranged in a circle (in clockwise order). Object number 1 is a black hole and the others are planets. There's a monster in one of the planet. Rick and Morty don't know on which one yet, only that he's not initially in the black hole, but Unity will inform them before the game starts. But for now, they want to be prepared for every possible scenario.
Each one of them has a set of numbers between 1 and *n*<=-<=1 (inclusive). Rick's set is *s*1 with *k*1 elements and Morty's is *s*2 with *k*2 elements. One of them goes first and the player changes alternatively. In each player's turn, he should choose an arbitrary number like *x* from his set and the monster will move to his *x*-th next object from its current position (clockwise). If after his move the monster gets to the black hole he wins.
Your task is that for each of monster's initial positions and who plays first determine if the starter wins, loses, or the game will stuck in an infinite loop. In case when player can lose or make game infinity, it more profitable to choose infinity game. | The first line of input contains a single integer *n* (2<=β€<=*n*<=β€<=7000) β number of objects in game.
The second line contains integer *k*1 followed by *k*1 distinct integers *s*1,<=1,<=*s*1,<=2,<=...,<=*s*1,<=*k*1 β Rick's set.
The third line contains integer *k*2 followed by *k*2 distinct integers *s*2,<=1,<=*s*2,<=2,<=...,<=*s*2,<=*k*2 β Morty's set
1<=β€<=*k**i*<=β€<=*n*<=-<=1 and 1<=β€<=*s**i*,<=1,<=*s**i*,<=2,<=...,<=*s**i*,<=*k**i*<=β€<=*n*<=-<=1 for 1<=β€<=*i*<=β€<=2. | In the first line print *n*<=-<=1 words separated by spaces where *i*-th word is "Win" (without quotations) if in the scenario that Rick plays first and monster is initially in object number *i*<=+<=1 he wins, "Lose" if he loses and "Loop" if the game will never end.
Similarly, in the second line print *n*<=-<=1 words separated by spaces where *i*-th word is "Win" (without quotations) if in the scenario that Morty plays first and monster is initially in object number *i*<=+<=1 he wins, "Lose" if he loses and "Loop" if the game will never end. | [
"5\n2 3 2\n3 1 2 3\n",
"8\n4 6 2 3 4\n2 3 6\n"
] | [
"Lose Win Win Loop\nLoop Win Win Win\n",
"Win Win Win Win Win Win Win\nLose Win Lose Lose Win Lose Lose\n"
] | none | [
{
"input": "5\n2 3 2\n3 1 2 3",
"output": "Lose Win Win Loop\nLoop Win Win Win"
},
{
"input": "8\n4 6 2 3 4\n2 3 6",
"output": "Win Win Win Win Win Win Win\nLose Win Lose Lose Win Lose Lose"
},
{
"input": "10\n3 4 7 5\n2 8 5",
"output": "Win Win Win Win Win Win Win Loop Win\nLose Win... | 15 | 0 | -1 | 16,431 | |
35 | Animals | [
"dp",
"greedy"
] | D. Animals | 2 | 64 | Once upon a time DravDe, an outstanding person famous for his professional achievements (as you must remember, he works in a warehouse storing Ogudar-Olok, a magical but non-alcoholic drink) came home after a hard day. That day he had to drink 9875 boxes of the drink and, having come home, he went to bed at once.
DravDe dreamt about managing a successful farm. He dreamt that every day one animal came to him and asked him to let it settle there. However, DravDe, being unimaginably kind, could send the animal away and it went, rejected. There were exactly *n* days in DravDeβs dream and the animal that came on the *i*-th day, ate exactly *c**i* tons of food daily starting from day *i*. But if one day the animal could not get the food it needed, it got really sad. At the very beginning of the dream there were exactly *X* tons of food on the farm.
DravDe woke up terrified...
When he retold the dream to you, he couldnβt remember how many animals were on the farm by the end of the *n*-th day any more, but he did remember that nobody got sad (as it was a happy farm) and that there was the maximum possible amount of the animals. Thatβs the number he wants you to find out.
It should be noticed that the animals arrived in the morning and DravDe only started to feed them in the afternoon, so that if an animal willing to join them is rejected, it canβt eat any farm food. But if the animal does join the farm, it eats daily from that day to the *n*-th. | The first input line contains integers *n* and *X* (1<=β€<=*n*<=β€<=100,<=1<=β€<=*X*<=β€<=104) β amount of days in DravDeβs dream and the total amount of food (in tons) that was there initially. The second line contains integers *c**i* (1<=β€<=*c**i*<=β€<=300). Numbers in the second line are divided by a space. | Output the only number β the maximum possible amount of animals on the farm by the end of the *n*-th day given that the food was enough for everybody. | [
"3 4\n1 1 1\n",
"3 6\n1 1 1\n"
] | [
"2\n",
"3\n"
] | Note to the first example: DravDe leaves the second and the third animal on the farm. The second animal will eat one ton of food on the second day and one ton on the third day. The third animal will eat one ton of food on the third day. | [
{
"input": "3 4\n1 1 1",
"output": "2"
},
{
"input": "3 6\n1 1 1",
"output": "3"
},
{
"input": "1 12\n1",
"output": "1"
},
{
"input": "3 100\n1 1 1",
"output": "3"
},
{
"input": "5 75\n1 1 1 1 1",
"output": "5"
},
{
"input": "7 115\n1 1 1 1 1 1 1",
... | 92 | 0 | -1 | 16,451 |
59 | Fortune Telling | [
"implementation",
"number theory"
] | B. Fortune Telling | 2 | 256 | Marina loves Sasha. But she keeps wondering whether Sasha loves her. Of course, the best way to know it is fortune telling. There are many ways of telling fortune, but Marina has picked the easiest one. She takes in her hand one or several camomiles and tears off the petals one by one. After each petal she pronounces alternatively "Loves" and "Doesn't love", at that Marina always starts with "Loves". There are *n* camomiles growing in the field, possessing the numbers of petals equal to *a*1,<=*a*2,<=... *a**n*. Marina wants to pick a bouquet with the maximal possible total number of petals so that the result would still be "Loves". Help her do that; find the maximal number of petals possible in the bouquet. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=100), which is the number of flowers growing in the field. The second line contains *n* integers *a**i* (1<=β€<=*a**i*<=β€<=100) which represent the number of petals on a given *i*-th camomile. | Print a single number which is the maximal number of petals in the bouquet, the fortune telling on which would result in "Loves". If there are no such bouquet, print 0 instead. The bouquet may consist of a single flower. | [
"1\n1\n",
"1\n2\n",
"3\n5 6 7\n"
] | [
"1\n",
"0\n",
"13\n"
] | none | [
{
"input": "1\n1",
"output": "1"
},
{
"input": "1\n2",
"output": "0"
},
{
"input": "3\n5 6 7",
"output": "13"
},
{
"input": "2\n5 7",
"output": "7"
},
{
"input": "3\n1 2 3",
"output": "5"
},
{
"input": "4\n4 3 1 2",
"output": "9"
},
{
"inpu... | 92 | 0 | 0 | 16,457 |
993 | Compute Power | [
"binary search",
"dp",
"greedy"
] | null | null | You need to execute several tasks, each associated with number of processors it needs, and the compute power it will consume.
You have sufficient number of analog computers, each with enough processors for any task. Each computer can execute up to one task at a time, and no more than two tasks total. The first task can be any, the second task on each computer must use strictly less power than the first. You will assign between 1 and 2 tasks to each computer. You will then first execute the first task on each computer, wait for all of them to complete, and then execute the second task on each computer that has two tasks assigned.
If the average compute power per utilized processor (the sum of all consumed powers for all tasks presently running divided by the number of utilized processors) across all computers exceeds some unknown threshold during the execution of the first tasks, the entire system will blow up. There is no restriction on the second tasks execution. Find the lowest threshold for which it is possible.
Due to the specifics of the task, you need to print the answer multiplied by 1000 and rounded up. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=50) β the number of tasks.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=108), where *a**i* represents the amount of power required for the *i*-th task.
The third line contains *n* integers *b*1,<=*b*2,<=...,<=*b**n* (1<=β€<=*b**i*<=β€<=100), where *b**i* is the number of processors that *i*-th task will utilize. | Print a single integer value β the lowest threshold for which it is possible to assign all tasks in such a way that the system will not blow up after the first round of computation, multiplied by 1000 and rounded up. | [
"6\n8 10 9 9 8 10\n1 1 1 1 1 1\n",
"6\n8 10 9 9 8 10\n1 10 5 5 1 10\n"
] | [
"9000\n",
"1160\n"
] | In the first example the best strategy is to run each task on a separate computer, getting average compute per processor during the first round equal to 9.
In the second task it is best to run tasks with compute 10 and 9 on one computer, tasks with compute 10 and 8 on another, and tasks with compute 9 and 8 on the last, averaging (10 + 10 + 9) / (10 + 10 + 5) = 1.16 compute power per processor during the first round. | [
{
"input": "6\n8 10 9 9 8 10\n1 1 1 1 1 1",
"output": "9000"
},
{
"input": "6\n8 10 9 9 8 10\n1 10 5 5 1 10",
"output": "1160"
},
{
"input": "1\n1\n100",
"output": "10"
},
{
"input": "50\n83 43 73 75 11 53 6 43 67 38 83 12 70 27 60 13 9 79 61 30 29 71 10 11 95 87 26 26 19 99 ... | 30 | 0 | 0 | 16,461 | |
187 | Permutations | [
"greedy"
] | null | null | Happy PMP is freshman and he is learning about algorithmic problems. He enjoys playing algorithmic games a lot.
One of the seniors gave Happy PMP a nice game. He is given two permutations of numbers 1 through *n* and is asked to convert the first one to the second. In one move he can remove the last number from the permutation of numbers and inserts it back in an arbitrary position. He can either insert last number between any two consecutive numbers, or he can place it at the beginning of the permutation.
Happy PMP has an algorithm that solves the problem. But it is not fast enough. He wants to know the minimum number of moves to convert the first permutation to the second. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=2Β·105) β the quantity of the numbers in the both given permutations.
Next line contains *n* space-separated integers β the first permutation. Each number between 1 to *n* will appear in the permutation exactly once.
Next line describe the second permutation in the same format. | Print a single integer denoting the minimum number of moves required to convert the first permutation to the second. | [
"3\n3 2 1\n1 2 3\n",
"5\n1 2 3 4 5\n1 5 2 3 4\n",
"5\n1 5 2 3 4\n1 2 3 4 5\n"
] | [
"2\n",
"1\n",
"3\n"
] | In the first sample, he removes number 1 from end of the list and places it at the beginning. After that he takes number 2 and places it between 1 and 3.
In the second sample, he removes number 5 and inserts it after 1.
In the third sample, the sequence of changes are like this:
- 1 5 2 3 4 - 1 4 5 2 3 - 1 3 4 5 2 - 1 2 3 4 5 | [
{
"input": "3\n3 2 1\n1 2 3",
"output": "2"
},
{
"input": "5\n1 2 3 4 5\n1 5 2 3 4",
"output": "1"
},
{
"input": "5\n1 5 2 3 4\n1 2 3 4 5",
"output": "3"
},
{
"input": "1\n1\n1",
"output": "0"
},
{
"input": "7\n6 1 7 3 4 5 2\n6 1 7 3 4 5 2",
"output": "0"
},... | 92 | 0 | 0 | 16,481 | |
794 | Leha and security system | [
"data structures"
] | null | null | Bankopolis, the city you already know, finally got a new bank opened! Unfortunately, its security system is not yet working fine... Meanwhile hacker Leha arrived in Bankopolis and decided to test the system!
Bank has *n* cells for clients' money. A sequence from *n* numbers *a*1,<=*a*2,<=...,<=*a**n* describes the amount of money each client has. Leha wants to make requests to the database of the bank, finding out the total amount of money on some subsegments of the sequence and changing values of the sequence on some subsegments. Using a bug in the system, Leha can requests two types of queries to the database:
- 1 l r x y denoting that Leha changes each digit *x* to digit *y* in each element of sequence *a**i*, for which *l*<=β€<=*i*<=β€<=*r* is holds. For example, if we change in number 11984381 digit 8 to 4, we get 11944341. It's worth noting that Leha, in order to stay in the shadow, never changes digits in the database to 0, i.e. *y*<=β <=0. - 2 l r denoting that Leha asks to calculate and print the sum of such elements of sequence *a**i*, for which *l*<=β€<=*i*<=β€<=*r* holds.
As Leha is a white-hat hacker, he don't want to test this vulnerability on a real database. You are to write a similar database for Leha to test. | The first line of input contains two integers *n* and *q* (1<=β€<=*n*<=β€<=105, 1<=β€<=*q*<=β€<=105) denoting amount of cells in the bank and total amount of queries respectively.
The following line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=<<=109) denoting the amount of money in each cell initially. These integers do not contain leading zeros.
Each of the following *q* lines has one of the formats:
- 1 l r x y (1<=β€<=*l*<=β€<=*r*<=β€<=*n*, 0<=β€<=*x*<=β€<=9, 1<=β€<=*y*<=β€<=9), denoting Leha asks to change each digit *x* on digit *y* for each element *a**i* of the sequence for which *l*<=β€<=*i*<=β€<=*r* holds; - 2 l r (1<=β€<=*l*<=β€<=*r*<=β€<=*n*), denoting you have to calculate and print the sum of elements *a**i* for which *l*<=β€<=*i*<=β€<=*r* holds. | For each second type query print a single number denoting the required sum. | [
"5 5\n38 43 4 12 70\n1 1 3 4 8\n2 2 4\n1 4 5 0 8\n1 2 5 8 7\n2 1 5\n",
"5 5\n25 36 39 40 899\n1 1 3 2 7\n2 1 2\n1 3 5 9 1\n1 4 4 0 9\n2 1 5\n"
] | [
"103\n207\n",
"111\n1002\n"
] | Let's look at the example testcase.
Initially the sequence is [38,β43,β4,β12,β70].
After the first change each digit equal to 4 becomes 8 for each element with index in interval [1;Β 3]. Thus, the new sequence is [38,β83,β8,β12,β70].
The answer for the first sum's query is the sum in the interval [2;Β 4], which equal 83β+β8β+β12β=β103, so the answer to this query is 103.
The sequence becomes [38,β83,β8,β12,β78] after the second change and [38,β73,β7,β12,β77] after the third.
The answer for the second sum's query is 38β+β73β+β7β+β12β+β77β=β207. | [] | 108 | 0 | 0 | 16,493 | |
510 | Fox And Jumping | [
"bitmasks",
"brute force",
"dp",
"math"
] | null | null | Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0.
There are also *n* cards, each card has 2 attributes: length *l**i* and cost *c**i*. If she pays *c**i* dollars then she can apply *i*-th card. After applying *i*-th card she becomes able to make jumps of length *l**i*, i. e. from cell *x* to cell (*x*<=-<=*l**i*) or cell (*x*<=+<=*l**i*).
She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible.
If this is possible, calculate the minimal cost. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=300), number of cards.
The second line contains *n* numbers *l**i* (1<=β€<=*l**i*<=β€<=109), the jump lengths of cards.
The third line contains *n* numbers *c**i* (1<=β€<=*c**i*<=β€<=105), the costs of cards. | If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards. | [
"3\n100 99 9900\n1 1 1\n",
"5\n10 20 30 40 50\n1 1 1 1 1\n",
"7\n15015 10010 6006 4290 2730 2310 1\n1 1 1 1 1 1 10\n",
"8\n4264 4921 6321 6984 2316 8432 6120 1026\n4264 4921 6321 6984 2316 8432 6120 1026\n"
] | [
"2\n",
"-1\n",
"6\n",
"7237\n"
] | In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell.
In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1. | [
{
"input": "3\n100 99 9900\n1 1 1",
"output": "2"
},
{
"input": "5\n10 20 30 40 50\n1 1 1 1 1",
"output": "-1"
},
{
"input": "7\n15015 10010 6006 4290 2730 2310 1\n1 1 1 1 1 1 10",
"output": "6"
},
{
"input": "8\n4264 4921 6321 6984 2316 8432 6120 1026\n4264 4921 6321 6984 23... | 46 | 0 | 0 | 16,514 | |
21 | Traveling Graph | [
"bitmasks",
"graph matchings",
"graphs"
] | D. Traveling Graph | 0 | 64 | You are given undirected weighted graph. Find the length of the shortest cycle which starts from the vertex 1 and passes throught all the edges at least once. Graph may contain multiply edges between a pair of vertices and loops (edges from the vertex to itself). | The first line of the input contains two integers *n* and *m* (1<=β€<=*n*<=β€<=15,<=0<=β€<=*m*<=β€<=2000), *n* is the amount of vertices, and *m* is the amount of edges. Following *m* lines contain edges as a triples *x*,<=*y*,<=*w* (1<=β€<=*x*,<=*y*<=β€<=*n*,<=1<=β€<=*w*<=β€<=10000), *x*,<=*y* are edge endpoints, and *w* is the edge length. | Output minimal cycle length or -1 if it doesn't exists. | [
"3 3\n1 2 1\n2 3 1\n3 1 1\n",
"3 2\n1 2 3\n2 3 4\n"
] | [
"3\n",
"14\n"
] | none | [
{
"input": "4 6\n1 2 10\n2 3 1000\n3 4 10\n4 1 1000\n4 2 5000\n1 3 2",
"output": "7042"
},
{
"input": "2 9\n1 2 9\n1 2 9\n2 1 9\n1 2 8\n2 1 9\n1 2 9\n1 2 9\n1 2 11\n1 2 9",
"output": "90"
},
{
"input": "2 10\n1 2 9\n1 2 9\n2 1 9\n1 2 8\n2 1 9\n1 2 9\n1 2 9\n1 2 11\n1 2 9\n1 2 9",
"ou... | 434 | 5,120,000 | 3 | 16,613 |
578 | Weakness and Poorness | [
"ternary search"
] | null | null | You are given a sequence of n integers *a*1,<=*a*2,<=...,<=*a**n*.
Determine a real number *x* such that the weakness of the sequence *a*1<=-<=*x*,<=*a*2<=-<=*x*,<=...,<=*a**n*<=-<=*x* is as small as possible.
The weakness of a sequence is defined as the maximum value of the poorness over all segments (contiguous subsequences) of a sequence.
The poorness of a segment is defined as the absolute value of sum of the elements of segment. | The first line contains one integer *n* (1<=β€<=*n*<=β€<=200<=000), the length of a sequence.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (|*a**i*|<=β€<=10<=000). | Output a real number denoting the minimum possible weakness of *a*1<=-<=*x*,<=*a*2<=-<=*x*,<=...,<=*a**n*<=-<=*x*. Your answer will be considered correct if its relative or absolute error doesn't exceed 10<=-<=6. | [
"3\n1 2 3\n",
"4\n1 2 3 4\n",
"10\n1 10 2 9 3 8 4 7 5 6\n"
] | [
"1.000000000000000\n",
"2.000000000000000\n",
"4.500000000000000\n"
] | For the first case, the optimal value of *x* is 2 so the sequence becomes β-β1, 0, 1 and the max poorness occurs at the segment "-1" or segment "1". The poorness value (answer) equals to 1 in this case.
For the second sample the optimal value of *x* is 2.5 so the sequence becomes β-β1.5,ββ-β0.5,β0.5,β1.5 and the max poorness occurs on segment "-1.5 -0.5" or "0.5 1.5". The poorness value (answer) equals to 2 in this case. | [
{
"input": "3\n1 2 3",
"output": "1.000000000000000"
},
{
"input": "4\n1 2 3 4",
"output": "2.000000000000000"
},
{
"input": "10\n1 10 2 9 3 8 4 7 5 6",
"output": "4.500000000000000"
},
{
"input": "1\n-10000",
"output": "0.000000000000000"
},
{
"input": "3\n10000 ... | 92 | 0 | 0 | 16,623 | |
425 | Sereja and Two Sequences | [
"data structures",
"dp"
] | null | null | Sereja has two sequences *a*1,<=*a*2,<=...,<=*a**n* and *b*1,<=*b*2,<=...,<=*b**m*, consisting of integers. One day Sereja got bored and he decided two play with them. The rules of the game was very simple. Sereja makes several moves, in one move he can perform one of the following actions:
1. Choose several (at least one) first elements of sequence *a* (non-empty prefix of *a*), choose several (at least one) first elements of sequence *b* (non-empty prefix of *b*); the element of sequence *a* with the maximum index among the chosen ones must be equal to the element of sequence *b* with the maximum index among the chosen ones; remove the chosen elements from the sequences. 1. Remove all elements of both sequences.
The first action is worth *e* energy units and adds one dollar to Sereja's electronic account. The second action is worth the number of energy units equal to the number of elements Sereja removed from the sequences before performing this action. After Sereja performed the second action, he gets all the money that he earned on his electronic account during the game.
Initially Sereja has *s* energy units and no money on his account. What maximum number of money can Sereja get? Note, the amount of Seraja's energy mustn't be negative at any time moment. | The first line contains integers *n*, *m*, *s*, *e* (1<=β€<=*n*,<=*m*<=β€<=105;Β 1<=β€<=*s*<=β€<=3Β·105;Β 103<=β€<=*e*<=β€<=104). The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=105). The third line contains *m* integers *b*1, *b*2, ..., *b**m* (1<=β€<=*b**i*<=β€<=105). | Print a single integer β maximum number of money in dollars that Sereja can get. | [
"5 5 100000 1000\n1 2 3 4 5\n3 2 4 5 1\n",
"3 4 3006 1000\n1 2 3\n1 2 4 3\n"
] | [
"3\n",
"2\n"
] | none | [] | 30 | 0 | 0 | 16,627 | |
18 | Flag 2 | [
"dp"
] | E. Flag 2 | 2 | 128 | According to a new ISO standard, a flag of every country should have, strangely enough, a chequered field *n*<=Γ<=*m*, each square should be wholly painted one of 26 colours. The following restrictions are set:
- In each row at most two different colours can be used. - No two adjacent squares can be painted the same colour.
Pay attention, please, that in one column more than two different colours can be used.
Berland's government took a decision to introduce changes into their country's flag in accordance with the new standard, at the same time they want these changes to be minimal. By the given description of Berland's flag you should find out the minimum amount of squares that need to be painted different colour to make the flag meet the new ISO standard. You are as well to build one of the possible variants of the new Berland's flag. | The first input line contains 2 integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=500) β amount of rows and columns in Berland's flag respectively. Then there follows the flag's description: each of the following *n* lines contains *m* characters. Each character is a letter from a to z, and it stands for the colour of the corresponding square. | In the first line output the minimum amount of squares that need to be repainted to make the flag meet the new ISO standard. The following *n* lines should contain one of the possible variants of the new flag. Don't forget that the variant of the flag, proposed by you, should be derived from the old flag with the minimum amount of repainted squares. If the answer isn't unique, output any. | [
"3 4\naaaa\nbbbb\ncccc\n",
"3 3\naba\naba\nzzz\n"
] | [
"6\nabab\nbaba\nacac\n",
"4\naba\nbab\nzbz\n"
] | none | [
{
"input": "3 4\naaaa\nbbbb\ncccc",
"output": "6\nabab\nbaba\nacac"
},
{
"input": "3 3\naba\naba\nzzz",
"output": "4\naba\nbab\nzbz"
},
{
"input": "5 6\nababab\nbababa\nbbbbbb\nbababa\nababab",
"output": "3\nababab\nbababa\nababab\nbababa\nababab"
},
{
"input": "1 1\nq",
... | 30 | 0 | -1 | 16,676 |
439 | Devu and Birthday Celebration | [
"combinatorics",
"dp",
"math"
] | null | null | Today is Devu's birthday. For celebrating the occasion, he bought *n* sweets from the nearby market. He has invited his *f* friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend.
He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed *n* sweets to his friends such that *i**th* friend is given *a**i* sweets. He wants to make sure that there should not be any positive integer *x*<=><=1, which divides every *a**i*.
Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109<=+<=7).
To make the problem more interesting, you are given *q* queries. Each query contains an *n*, *f* pair. For each query please output the required number of ways modulo 1000000007 (109<=+<=7). | The first line contains an integer *q* representing the number of queries (1<=β€<=*q*<=β€<=105). Each of the next *q* lines contains two space space-separated integers *n*, *f* (1<=β€<=*f*<=β€<=*n*<=β€<=105). | For each query, output a single integer in a line corresponding to the answer of each query. | [
"5\n6 2\n7 2\n6 3\n6 4\n7 4\n"
] | [
"2\n6\n9\n10\n20\n"
] | For first query: *n*β=β6,β*f*β=β2. Possible partitions are [1, 5] and [5, 1].
For second query: *n*β=β7,β*f*β=β2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning. | [
{
"input": "5\n6 2\n7 2\n6 3\n6 4\n7 4",
"output": "2\n6\n9\n10\n20"
},
{
"input": "10\n1 1\n1 1\n1 1\n7 2\n6 3\n9 5\n4 1\n2 1\n3 1\n2 2",
"output": "1\n1\n1\n6\n9\n70\n0\n0\n0\n1"
},
{
"input": "40\n37 15\n48 10\n16 5\n25 23\n32 20\n24 4\n46 19\n16 13\n1 1\n37 22\n44 29\n24 6\n27 10\n39... | 498 | 49,254,400 | 0 | 16,703 | |
590 | Median Smoothing | [
"implementation"
] | null | null | A schoolboy named Vasya loves reading books on programming and mathematics. He has recently read an encyclopedia article that described the method of median smoothing (or median filter) and its many applications in science and engineering. Vasya liked the idea of the method very much, and he decided to try it in practice.
Applying the simplest variant of median smoothing to the sequence of numbers *a*1,<=*a*2,<=...,<=*a**n* will result a new sequence *b*1,<=*b*2,<=...,<=*b**n* obtained by the following algorithm:
- *b*1<==<=*a*1, *b**n*<==<=*a**n*, that is, the first and the last number of the new sequence match the corresponding numbers of the original sequence. - For *i*<==<=2,<=...,<=*n*<=-<=1 value *b**i* is equal to the median of three values *a**i*<=-<=1, *a**i* and *a**i*<=+<=1.
The median of a set of three numbers is the number that goes on the second place, when these three numbers are written in the non-decreasing order. For example, the median of the set 5, 1, 2 is number 2, and the median of set 1, 0, 1 is equal to 1.
In order to make the task easier, Vasya decided to apply the method to sequences consisting of zeros and ones only.
Having made the procedure once, Vasya looked at the resulting sequence and thought: what if I apply the algorithm to it once again, and then apply it to the next result, and so on? Vasya tried a couple of examples and found out that after some number of median smoothing algorithm applications the sequence can stop changing. We say that the sequence is stable, if it does not change when the median smoothing is applied to it.
Now Vasya wonders, whether the sequence always eventually becomes stable. He asks you to write a program that, given a sequence of zeros and ones, will determine whether it ever becomes stable. Moreover, if it ever becomes stable, then you should determine what will it look like and how many times one needs to apply the median smoothing algorithm to initial sequence in order to obtain a stable one. | The first input line of the input contains a single integer *n* (3<=β€<=*n*<=β€<=500<=000)Β β the length of the initial sequence.
The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (*a**i*<==<=0 or *a**i*<==<=1), giving the initial sequence itself. | If the sequence will never become stable, print a single number <=-<=1.
Otherwise, first print a single integerΒ β the minimum number of times one needs to apply the median smoothing algorithm to the initial sequence before it becomes is stable. In the second line print *n* numbers separated by a space Β β the resulting sequence itself. | [
"4\n0 0 1 1\n",
"5\n0 1 0 1 0\n"
] | [
"0\n0 0 1 1\n",
"2\n0 0 0 0 0\n"
] | In the second sample the stabilization occurs in two steps: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5a983e7baab048cbe43812cb997c15e9d7100231.png" style="max-width: 100.0%;max-height: 100.0%;"/>, and the sequence 00000 is obviously stable. | [
{
"input": "4\n0 0 1 1",
"output": "0\n0 0 1 1"
},
{
"input": "5\n0 1 0 1 0",
"output": "2\n0 0 0 0 0"
},
{
"input": "3\n1 0 0",
"output": "0\n1 0 0"
},
{
"input": "4\n1 0 0 1",
"output": "0\n1 0 0 1"
},
{
"input": "7\n1 0 1 1 1 0 1",
"output": "1\n1 1 1 1 1 1... | 46 | 0 | 0 | 16,745 | |
630 | Cracking the Code | [
"implementation",
"math"
] | null | null | The protection of a popular program developed by one of IT City companies is organized the following way. After installation it outputs a random five digit number which should be sent in SMS to a particular phone number. In response an SMS activation code arrives.
A young hacker Vasya disassembled the program and found the algorithm that transforms the shown number into the activation code. Note: it is clear that Vasya is a law-abiding hacker, and made it for a noble purpose β to show the developer the imperfection of their protection.
The found algorithm looks the following way. At first the digits of the number are shuffled in the following order <first digit><third digit><fifth digit><fourth digit><second digit>. For example the shuffle of 12345 should lead to 13542. On the second stage the number is raised to the fifth power. The result of the shuffle and exponentiation of the number 12345 is 455Β 422Β 043Β 125Β 550Β 171Β 232. The answer is the 5 last digits of this result. For the number 12345 the answer should be 71232.
Vasya is going to write a keygen program implementing this algorithm. Can you do the same? | The only line of the input contains a positive integer five digit number for which the activation code should be found. | Output exactly 5 digits without spaces between them β the found activation code of the program. | [
"12345\n"
] | [
"71232"
] | none | [
{
"input": "12345",
"output": "71232"
},
{
"input": "13542",
"output": "84443"
},
{
"input": "71232",
"output": "10151"
},
{
"input": "11111",
"output": "36551"
},
{
"input": "10000",
"output": "00000"
},
{
"input": "99999",
"output": "99999"
},
... | 46 | 0 | 3 | 16,751 | |
538 | Weird Chess | [
"brute force",
"constructive algorithms",
"implementation"
] | null | null | Igor has been into chess for a long time and now he is sick of the game by the ordinary rules. He is going to think of new rules of the game and become world famous.
Igor's chessboard is a square of size *n*<=Γ<=*n* cells. Igor decided that simple rules guarantee success, that's why his game will have only one type of pieces. Besides, all pieces in his game are of the same color. The possible moves of a piece are described by a set of shift vectors. The next passage contains a formal description of available moves.
Let the rows of the board be numbered from top to bottom and the columns be numbered from left to right from 1 to *n*. Let's assign to each square a pair of integers (*x*,<=*y*)Β β the number of the corresponding column and row. Each of the possible moves of the piece is defined by a pair of integers (*dx*,<=*dy*); using this move, the piece moves from the field (*x*,<=*y*) to the field (*x*<=+<=*dx*,<=*y*<=+<=*dy*). You can perform the move if the cell (*x*<=+<=*dx*,<=*y*<=+<=*dy*) is within the boundaries of the board and doesn't contain another piece. Pieces that stand on the cells other than (*x*,<=*y*) and (*x*<=+<=*dx*,<=*y*<=+<=*dy*) are not important when considering the possibility of making the given move (for example, like when a knight moves in usual chess).
Igor offers you to find out what moves his chess piece can make. He placed several pieces on the board and for each unoccupied square he told you whether it is attacked by any present piece (i.e. whether some of the pieces on the field can move to that cell). Restore a possible set of shift vectors of the piece, or else determine that Igor has made a mistake and such situation is impossible for any set of shift vectors. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=50).
The next *n* lines contain *n* characters each describing the position offered by Igor. The *j*-th character of the *i*-th string can have the following values:
- o β in this case the field (*i*,<=*j*) is occupied by a piece and the field may or may not be attacked by some other piece;- x β in this case field (*i*,<=*j*) is attacked by some piece;- . β in this case field (*i*,<=*j*) isn't attacked by any piece.
It is guaranteed that there is at least one piece on the board. | If there is a valid set of moves, in the first line print a single word 'YES' (without the quotes). Next, print the description of the set of moves of a piece in the form of a (2*n*<=-<=1)<=Γ<=(2*n*<=-<=1) board, the center of the board has a piece and symbols 'x' mark cells that are attacked by it, in a format similar to the input. See examples of the output for a full understanding of the format. If there are several possible answers, print any of them.
If a valid set of moves does not exist, print a single word 'NO'. | [
"5\noxxxx\nx...x\nx...x\nx...x\nxxxxo\n",
"6\n.x.x..\nx.x.x.\n.xo..x\nx..ox.\n.x.x.x\n..x.x.\n",
"3\no.x\noxx\no.x\n"
] | [
"YES\n....x....\n....x....\n....x....\n....x....\nxxxxoxxxx\n....x....\n....x....\n....x....\n....x....\n",
"YES\n...........\n...........\n...........\n....x.x....\n...x...x...\n.....o.....\n...x...x...\n....x.x....\n...........\n...........\n...........\n",
"NO\n"
] | In the first sample test the piece is a usual chess rook, and in the second sample test the piece is a usual chess knight. | [
{
"input": "5\noxxxx\nx...x\nx...x\nx...x\nxxxxo",
"output": "YES\nxxxxxxxxx\nx...xxxxx\nx...xxxxx\nx...xxxxx\nxxxxoxxxx\nxxxxx...x\nxxxxx...x\nxxxxx...x\nxxxxxxxxx"
},
{
"input": "6\n.x.x..\nx.x.x.\n.xo..x\nx..ox.\n.x.x.x\n..x.x.",
"output": "YES\nxxxxxxxxxxx\nxxxxxxxxxxx\nxx.x.x..xxx\nxxx.x.x.... | 2,000 | 4,812,800 | 0 | 16,778 | |
540 | Bad Luck Island | [
"dp",
"probabilities"
] | null | null | The Bad Luck Island is inhabited by three kinds of species: *r* rocks, *s* scissors and *p* papers. At some moments of time two random individuals meet (all pairs of individuals can meet equiprobably), and if they belong to different species, then one individual kills the other one: a rock kills scissors, scissors kill paper, and paper kills a rock. Your task is to determine for each species what is the probability that this species will be the only one to inhabit this island after a long enough period of time. | The single line contains three integers *r*, *s* and *p* (1<=β€<=*r*,<=*s*,<=*p*<=β€<=100)Β β the original number of individuals in the species of rock, scissors and paper, respectively. | Print three space-separated real numbers: the probabilities, at which the rocks, the scissors and the paper will be the only surviving species, respectively. The answer will be considered correct if the relative or absolute error of each number doesn't exceed 10<=-<=9. | [
"2 2 2\n",
"2 1 2\n",
"1 1 3\n"
] | [
"0.333333333333 0.333333333333 0.333333333333\n",
"0.150000000000 0.300000000000 0.550000000000\n",
"0.057142857143 0.657142857143 0.285714285714\n"
] | none | [
{
"input": "2 2 2",
"output": "0.333333333333 0.333333333333 0.333333333333"
},
{
"input": "2 1 2",
"output": "0.150000000000 0.300000000000 0.550000000000"
},
{
"input": "1 1 3",
"output": "0.057142857143 0.657142857143 0.285714285714"
},
{
"input": "3 2 1",
"output": "0... | 1,107 | 81,510,400 | 3 | 16,808 | |
0 | none | [
"none"
] | null | null | Manao is taking part in a quiz. The quiz consists of *n* consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number *k*, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero.
Manao remembers that he has answered exactly *m* questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (109<=+<=9). | The single line contains three space-separated integers *n*, *m* and *k* (2<=β€<=*k*<=β€<=*n*<=β€<=109;Β 0<=β€<=*m*<=β€<=*n*). | Print a single integer β the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109<=+<=9). | [
"5 3 2\n",
"5 4 2\n"
] | [
"3\n",
"6\n"
] | Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points.
Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4.
Also note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000Β *mod*Β 1000000009, even though 2000000020Β *mod*Β 1000000009 is a smaller number. | [
{
"input": "5 3 2",
"output": "3"
},
{
"input": "5 4 2",
"output": "6"
},
{
"input": "300 300 3",
"output": "17717644"
},
{
"input": "300 282 7",
"output": "234881124"
},
{
"input": "1000000000 1000000000 1000000000",
"output": "999999991"
},
{
"input"... | 218 | 0 | 3 | 16,837 | |
440 | One-Based Arithmetic | [
"brute force",
"dfs and similar",
"divide and conquer"
] | null | null | Prof. Vasechkin wants to represent positive integer *n* as a sum of addends, where each addends is an integer number containing only 1s. For example, he can represent 121 as 121=111+11+β1. Help him to find the least number of digits 1 in such sum. | The first line of the input contains integer *n* (1<=β€<=*n*<=<<=1015). | Print expected minimal number of digits 1. | [
"121\n"
] | [
"6\n"
] | none | [
{
"input": "121",
"output": "6"
},
{
"input": "10",
"output": "3"
},
{
"input": "72",
"output": "15"
},
{
"input": "1",
"output": "1"
},
{
"input": "2",
"output": "2"
},
{
"input": "3",
"output": "3"
},
{
"input": "4",
"output": "4"
}... | 124 | 0 | 0 | 16,861 | |
660 | Different Subsets For All Tuples | [
"combinatorics",
"math"
] | null | null | For a sequence *a* of *n* integers between 1 and *m*, inclusive, denote *f*(*a*) as the number of distinct subsequences of *a* (including the empty subsequence).
You are given two positive integers *n* and *m*. Let *S* be the set of all sequences of length *n* consisting of numbers from 1 to *m*. Compute the sum *f*(*a*) over all *a* in *S* modulo 109<=+<=7. | The only line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=106) β the number of elements in arrays and the upper bound for elements. | Print the only integer *c* β the desired sum modulo 109<=+<=7. | [
"1 3\n",
"2 2\n",
"3 3\n"
] | [
"6\n",
"14\n",
"174\n"
] | none | [
{
"input": "1 3",
"output": "6"
},
{
"input": "2 2",
"output": "14"
},
{
"input": "3 3",
"output": "174"
},
{
"input": "1 1000000",
"output": "2000000"
},
{
"input": "1000000 1",
"output": "1000001"
},
{
"input": "500 500",
"output": "383255233"
... | 46 | 0 | 0 | 16,872 | |
685 | Kay and Snowflake | [
"data structures",
"dfs and similar",
"dp",
"trees"
] | null | null | After the piece of a devilish mirror hit the Kay's eye, he is no longer interested in the beauty of the roses. Now he likes to watch snowflakes.
Once upon a time, he found a huge snowflake that has a form of the tree (connected acyclic graph) consisting of *n* nodes. The root of tree has index 1. Kay is very interested in the structure of this tree.
After doing some research he formed *q* queries he is interested in. The *i*-th query asks to find a centroid of the subtree of the node *v**i*. Your goal is to answer all queries.
Subtree of a node is a part of tree consisting of this node and all it's descendants (direct or not). In other words, subtree of node *v* is formed by nodes *u*, such that node *v* is present on the path from *u* to root.
Centroid of a tree (or a subtree) is a node, such that if we erase it from the tree, the maximum size of the connected component will be at least two times smaller than the size of the initial tree (or a subtree). | The first line of the input contains two integers *n* and *q* (2<=β€<=*n*<=β€<=300<=000, 1<=β€<=*q*<=β€<=300<=000)Β β the size of the initial tree and the number of queries respectively.
The second line contains *n*<=-<=1 integer *p*2,<=*p*3,<=...,<=*p**n* (1<=β€<=*p**i*<=β€<=*n*)Β β the indices of the parents of the nodes from 2 to *n*. Node 1 is a root of the tree. It's guaranteed that *p**i* define a correct tree.
Each of the following *q* lines contain a single integer *v**i* (1<=β€<=*v**i*<=β€<=*n*)Β β the index of the node, that define the subtree, for which we want to find a centroid. | For each query print the index of a centroid of the corresponding subtree. If there are many suitable nodes, print any of them. It's guaranteed, that each subtree has at least one centroid. | [
"7 4\n1 1 3 3 5 3\n1\n2\n3\n5\n"
] | [
"3\n2\n3\n6\n"
] | The first query asks for a centroid of the whole treeΒ β this is node 3. If we delete node 3 the tree will split in four components, two of size 1 and two of size 2.
The subtree of the second node consists of this node only, so the answer is 2.
Node 3 is centroid of its own subtree.
The centroids of the subtree of the node 5 are nodes 5 and 6Β β both answers are considered correct. | [
{
"input": "7 4\n1 1 3 3 5 3\n1\n2\n3\n5",
"output": "3\n2\n3\n6"
},
{
"input": "2 2\n1\n1\n2",
"output": "2\n2"
}
] | 872 | 70,656,000 | 0 | 16,883 | |
460 | Present | [
"binary search",
"data structures",
"greedy"
] | null | null | Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted *n* flowers in a row on his windowsill and started waiting for them to grow. However, after some time the beaver noticed that the flowers stopped growing. The beaver thinks it is bad manners to present little flowers. So he decided to come up with some solutions.
There are *m* days left to the birthday. The height of the *i*-th flower (assume that the flowers in the row are numbered from 1 to *n* from left to right) is equal to *a**i* at the moment. At each of the remaining *m* days the beaver can take a special watering and water *w* contiguous flowers (he can do that only once at a day). At that each watered flower grows by one height unit on that day. The beaver wants the height of the smallest flower be as large as possible in the end. What maximum height of the smallest flower can he get? | The first line contains space-separated integers *n*, *m* and *w* (1<=β€<=*w*<=β€<=*n*<=β€<=105;Β 1<=β€<=*m*<=β€<=105). The second line contains space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109). | Print a single integer β the maximum final height of the smallest flower. | [
"6 2 3\n2 2 2 2 1 1\n",
"2 5 1\n5 8\n"
] | [
"2\n",
"9\n"
] | In the first sample beaver can water the last 3 flowers at the first day. On the next day he may not to water flowers at all. In the end he will get the following heights: [2, 2, 2, 3, 2, 2]. The smallest flower has height equal to 2. It's impossible to get height 3 in this test. | [
{
"input": "6 2 3\n2 2 2 2 1 1",
"output": "2"
},
{
"input": "2 5 1\n5 8",
"output": "9"
},
{
"input": "1 1 1\n1",
"output": "2"
},
{
"input": "3 2 3\n999999998 999999998 999999998",
"output": "1000000000"
},
{
"input": "10 8 3\n499 498 497 497 497 497 497 497 498... | 109 | 20,992,000 | 3 | 16,938 | |
846 | Chemistry in Berland | [
"dfs and similar",
"greedy",
"trees"
] | null | null | Igor is a post-graduate student of chemistry faculty in Berland State University (BerSU). He needs to conduct a complicated experiment to write his thesis, but laboratory of BerSU doesn't contain all the materials required for this experiment.
Fortunately, chemical laws allow material transformations (yes, chemistry in Berland differs from ours). But the rules of transformation are a bit strange.
Berland chemists are aware of *n* materials, numbered in the order they were discovered. Each material can be transformed into some other material (or vice versa). Formally, for each *i* (2<=β€<=*i*<=β€<=*n*) there exist two numbers *x**i* and *k**i* that denote a possible transformation: *k**i* kilograms of material *x**i* can be transformed into 1 kilogram of material *i*, and 1 kilogram of material *i* can be transformed into 1 kilogram of material *x**i*. Chemical processing equipment in BerSU allows only such transformation that the amount of resulting material is always an integer number of kilograms.
For each *i* (1<=β€<=*i*<=β€<=*n*) Igor knows that the experiment requires *a**i* kilograms of material *i*, and the laboratory contains *b**i* kilograms of this material. Is it possible to conduct an experiment after transforming some materials (or none)? | The first line contains one integer number *n* (1<=β€<=*n*<=β€<=105) β the number of materials discovered by Berland chemists.
The second line contains *n* integer numbers *b*1,<=*b*2... *b**n* (1<=β€<=*b**i*<=β€<=1012) β supplies of BerSU laboratory.
The third line contains *n* integer numbers *a*1,<=*a*2... *a**n* (1<=β€<=*a**i*<=β€<=1012) β the amounts required for the experiment.
Then *n*<=-<=1 lines follow. *j*-th of them contains two numbers *x**j*<=+<=1 and *k**j*<=+<=1 that denote transformation of (*j*<=+<=1)-th material (1<=β€<=*x**j*<=+<=1<=β€<=*j*,<=1<=β€<=*k**j*<=+<=1<=β€<=109). | Print YES if it is possible to conduct an experiment. Otherwise print NO. | [
"3\n1 2 3\n3 2 1\n1 1\n1 1\n",
"3\n3 2 1\n1 2 3\n1 1\n1 2\n"
] | [
"YES\n",
"NO\n"
] | none | [
{
"input": "3\n1 2 3\n3 2 1\n1 1\n1 1",
"output": "YES"
},
{
"input": "3\n3 2 1\n1 2 3\n1 1\n1 2",
"output": "NO"
},
{
"input": "5\n2 1 1 2 3\n1 2 2 2 1\n1 2\n1 3\n2 4\n1 4",
"output": "NO"
},
{
"input": "10\n2 8 6 1 2 7 6 9 2 8\n4 9 4 3 5 2 9 3 7 3\n1 8\n2 8\n3 8\n4 10\n5 1\... | 1,419 | 268,390,400 | 0 | 16,954 | |
201 | Fragile Bridges | [
"dp"
] | null | null | You are playing a video game and you have just reached the bonus level, where the only possible goal is to score as many points as possible. Being a perfectionist, you've decided that you won't leave this level until you've gained the maximum possible number of points there.
The bonus level consists of *n* small platforms placed in a line and numbered from 1 to *n* from left to right and (*n*<=-<=1) bridges connecting adjacent platforms. The bridges between the platforms are very fragile, and for each bridge the number of times one can pass this bridge from one of its ends to the other before it collapses forever is known in advance.
The player's actions are as follows. First, he selects one of the platforms to be the starting position for his hero. After that the player can freely move the hero across the platforms moving by the undestroyed bridges. As soon as the hero finds himself on a platform with no undestroyed bridge attached to it, the level is automatically ended. The number of points scored by the player at the end of the level is calculated as the number of transitions made by the hero between the platforms. Note that if the hero started moving by a certain bridge, he has to continue moving in the same direction until he is on a platform.
Find how many points you need to score to be sure that nobody will beat your record, and move to the next level with a quiet heart. | The first line contains a single integer *n* (2<=β€<=*n*<=β€<=105) β the number of platforms on the bonus level. The second line contains (*n*<=-<=1) integers *a**i* (1<=β€<=*a**i*<=β€<=109, 1<=β€<=*i*<=<<=*n*) β the number of transitions from one end to the other that the bridge between platforms *i* and *i*<=+<=1 can bear. | Print a single integer β the maximum number of points a player can get on the bonus level.
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. | [
"5\n2 1 2 1\n"
] | [
"5\n"
] | One possibility of getting 5 points in the sample is starting from platform 3 and consequently moving to platforms 4, 3, 2, 1 and 2. After that the only undestroyed bridge is the bridge between platforms 4 and 5, but this bridge is too far from platform 2 where the hero is located now. | [
{
"input": "5\n2 1 2 1",
"output": "5"
},
{
"input": "3\n2 2",
"output": "4"
},
{
"input": "7\n1 2 3 4 5 6",
"output": "19"
},
{
"input": "12\n10 14 8 6 2 7 5 9 9 4 8",
"output": "82"
},
{
"input": "13\n1 1 1 1 1 1 1 1 1 1 1 1",
"output": "12"
},
{
"in... | 0 | 0 | -1 | 16,979 | |
981 | Magic multisets | [
"data structures"
] | null | null | In the School of Magic in Dirtpolis a lot of interesting objects are studied on Computer Science lessons.
Consider, for example, the magic multiset. If you try to add an integer to it that is already presented in the multiset, each element in the multiset duplicates. For example, if you try to add the integer $2$ to the multiset $\{1, 2, 3, 3\}$, you will get $\{1, 1, 2, 2, 3, 3, 3, 3\}$.
If you try to add an integer that is not presented in the multiset, it is simply added to it. For example, if you try to add the integer $4$ to the multiset $\{1, 2, 3, 3\}$, you will get $\{1, 2, 3, 3, 4\}$.
Also consider an array of $n$ initially empty magic multisets, enumerated from $1$ to $n$.
You are to answer $q$ queries of the form "add an integer $x$ to all multisets with indices $l, l + 1, \ldots, r$" and "compute the sum of sizes of multisets with indices $l, l + 1, \ldots, r$". The answers for the second type queries can be large, so print the answers modulo $998244353$. | The first line contains two integers $n$ and $q$ ($1 \leq n, q \leq 2 \cdot 10^{5}$)Β β the number of magic multisets in the array and the number of queries, respectively.
The next $q$ lines describe queries, one per line. Each line starts with an integer $t$ ($1 \leq t \leq 2$)Β β the type of the query. If $t$ equals $1$, it is followed by three integers $l$, $r$, $x$ ($1 \leq l \leq r \leq n$, $1 \leq x \leq n$) meaning that you should add $x$ to all multisets with indices from $l$ to $r$ inclusive. If $t$ equals $2$, it is followed by two integers $l$, $r$ ($1 \leq l \leq r \leq n$) meaning that you should compute the sum of sizes of all multisets with indices from $l$ to $r$ inclusive. | For each query of the second type print the sum of sizes of multisets on the given segment.
The answers can be large, so print them modulo $998244353$. | [
"4 4\n1 1 2 1\n1 1 2 2\n1 1 4 1\n2 1 4\n",
"3 7\n1 1 1 3\n1 1 1 3\n1 1 1 2\n1 1 1 1\n2 1 1\n1 1 1 2\n2 1 1\n"
] | [
"10\n",
"4\n8\n"
] | In the first example after the first two queries the multisets are equal to $[\{1, 2\},\{1, 2\},\{\},\{\}]$, after the third query they are equal to $[\{1, 1, 2, 2\},\{1, 1, 2, 2\},\{1\},\{1\}]$.
In the second example the first multiset evolves as follows:
$\{\} \to \{3\} \to \{3, 3\} \to \{2, 3, 3\} \to \{1, 2, 3, 3\} \to \{1, 1, 2, 2, 3, 3, 3, 3\}$. | [] | 1,809 | 268,390,400 | 0 | 17,040 | |
134 | Pairs of Numbers | [
"brute force",
"dfs and similar",
"math",
"number theory"
] | null | null | Let's assume that we have a pair of numbers (*a*,<=*b*). We can get a new pair (*a*<=+<=*b*,<=*b*) or (*a*,<=*a*<=+<=*b*) from the given pair in a single step.
Let the initial pair of numbers be (1,1). Your task is to find number *k*, that is, the least number of steps needed to transform (1,1) into the pair where at least one number equals *n*. | The input contains the only integer *n* (1<=β€<=*n*<=β€<=106). | Print the only integer *k*. | [
"5\n",
"1\n"
] | [
"3\n",
"0\n"
] | The pair (1,1) can be transformed into a pair containing 5 in three moves: (1,1) βββ (1,2) βββ (3,2) βββ (5,2). | [
{
"input": "5",
"output": "3"
},
{
"input": "1",
"output": "0"
},
{
"input": "2",
"output": "1"
},
{
"input": "3",
"output": "2"
},
{
"input": "4",
"output": "3"
},
{
"input": "10",
"output": "5"
},
{
"input": "1009",
"output": "15"
}... | 1,000 | 4,608,000 | 0 | 17,060 | |
15 | Laser | [
"math"
] | B. Laser | 1 | 64 | Petya is the most responsible worker in the Research Institute. So he was asked to make a very important experiment: to melt the chocolate bar with a new laser device. The device consists of a rectangular field of *n*<=Γ<=*m* cells and a robotic arm. Each cell of the field is a 1<=Γ<=1 square. The robotic arm has two lasers pointed at the field perpendicularly to its surface. At any one time lasers are pointed at the centres of some two cells. Since the lasers are on the robotic hand, their movements are synchronized β if you move one of the lasers by a vector, another one moves by the same vector.
The following facts about the experiment are known:
- initially the whole field is covered with a chocolate bar of the size *n*<=Γ<=*m*, both lasers are located above the field and are active; - the chocolate melts within one cell of the field at which the laser is pointed; - all moves of the robotic arm should be parallel to the sides of the field, after each move the lasers should be pointed at the centres of some two cells; - at any one time both lasers should be pointed at the field. Petya doesn't want to become a second Gordon Freeman.
You are given *n*, *m* and the cells (*x*1,<=*y*1) and (*x*2,<=*y*2), where the lasers are initially pointed at (*x**i* is a column number, *y**i* is a row number). Rows are numbered from 1 to *m* from top to bottom and columns are numbered from 1 to *n* from left to right. You are to find the amount of cells of the field on which the chocolate can't be melted in the given conditions. | The first line contains one integer number *t* (1<=β€<=*t*<=β€<=10000) β the number of test sets. Each of the following *t* lines describes one test set. Each line contains integer numbers *n*, *m*, *x*1, *y*1, *x*2, *y*2, separated by a space (2<=β€<=*n*,<=*m*<=β€<=109, 1<=β€<=*x*1,<=*x*2<=β€<=*n*, 1<=β€<=*y*1,<=*y*2<=β€<=*m*). Cells (*x*1,<=*y*1) and (*x*2,<=*y*2) are distinct. | Each of the *t* lines of the output should contain the answer to the corresponding input test set. | [
"2\n4 4 1 1 3 3\n4 3 1 1 2 2\n"
] | [
"8\n2\n"
] | none | [
{
"input": "2\n4 4 1 1 3 3\n4 3 1 1 2 2",
"output": "8\n2"
},
{
"input": "1\n2 2 1 2 2 1",
"output": "2"
},
{
"input": "1\n2 2 1 2 2 1",
"output": "2"
},
{
"input": "1\n3 3 3 2 1 1",
"output": "5"
},
{
"input": "1\n3 4 1 1 1 2",
"output": "0"
},
{
"inp... | 0 | 0 | -1 | 17,070 |
83 | Numbers | [
"dp",
"math",
"number theory"
] | D. Numbers | 3 | 256 | One quite ordinary day Valera went to school (there's nowhere else he should go on a week day). In a maths lesson his favorite teacher Ms. Evans told students about divisors. Despite the fact that Valera loved math, he didn't find this particular topic interesting. Even more, it seemed so boring that he fell asleep in the middle of a lesson. And only a loud ringing of a school bell could interrupt his sweet dream.
Of course, the valuable material and the teacher's explanations were lost. However, Valera will one way or another have to do the homework. As he does not know the new material absolutely, he cannot do the job himself. That's why he asked you to help. You're his best friend after all, you just cannot refuse to help.
Valera's home task has only one problem, which, though formulated in a very simple way, has not a trivial solution. Its statement looks as follows: if we consider all positive integers in the interval [*a*;*b*] then it is required to count the amount of such numbers in this interval that their smallest divisor will be a certain integer *k* (you do not have to consider divisor equal to one). In other words, you should count the amount of such numbers from the interval [*a*;*b*], that are not divisible by any number between 2 and *k*<=-<=1 and yet are divisible by *k*. | The first and only line contains three positive integers *a*, *b*, *k* (1<=β€<=*a*<=β€<=*b*<=β€<=2Β·109,<=2<=β€<=*k*<=β€<=2Β·109). | Print on a single line the answer to the given problem. | [
"1 10 2\n",
"12 23 3\n",
"6 19 5\n"
] | [
"5\n",
"2\n",
"0\n"
] | Comments to the samples from the statement:
In the first sample the answer is numbers 2,β4,β6,β8,β10.
In the second one β 15,β21
In the third one there are no such numbers. | [
{
"input": "1 10 2",
"output": "5"
},
{
"input": "12 23 3",
"output": "2"
},
{
"input": "6 19 5",
"output": "0"
},
{
"input": "1 80 7",
"output": "3"
},
{
"input": "100 1000 1009",
"output": "0"
},
{
"input": "11 124 11",
"output": "2"
},
{
... | 3,000 | 0 | 0 | 17,083 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.