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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
975 | Mancala | [
"brute force",
"implementation"
] | null | null | Mancala is a game famous in the Middle East. It is played on a board that consists of 14 holes.
Initially, each hole has $a_i$ stones. When a player makes a move, he chooses a hole which contains a positive number of stones. He takes all the stones inside it and then redistributes these stones one by one in the next holes in a counter-clockwise direction.
Note that the counter-clockwise order means if the player takes the stones from hole $i$, he will put one stone in the $(i+1)$-th hole, then in the $(i+2)$-th, etc. If he puts a stone in the $14$-th hole, the next one will be put in the first hole.
After the move, the player collects all the stones from holes that contain even number of stones. The number of stones collected by player is the score, according to Resli.
Resli is a famous Mancala player. He wants to know the maximum score he can obtain after one move. | The only line contains 14 integers $a_1, a_2, \ldots, a_{14}$ ($0 \leq a_i \leq 10^9$)Β β the number of stones in each hole.
It is guaranteed that for any $i$ ($1\leq i \leq 14$) $a_i$ is either zero or odd, and there is at least one stone in the board. | Output one integer, the maximum possible score after one move. | [
"0 1 1 0 0 0 0 0 0 7 0 0 0 0\n",
"5 1 1 1 1 0 0 0 0 0 0 0 0 0\n"
] | [
"4\n",
"8\n"
] | In the first test case the board after the move from the hole with $7$ stones will look like 1 2 2 0 0 0 0 0 0 0 1 1 1 1. Then the player collects the even numbers and ends up with a score equal to $4$. | [
{
"input": "0 1 1 0 0 0 0 0 0 7 0 0 0 0",
"output": "4"
},
{
"input": "5 1 1 1 1 0 0 0 0 0 0 0 0 0",
"output": "8"
},
{
"input": "10001 10001 10001 10001 10001 10001 10001 10001 10001 10001 10001 10001 10001 1",
"output": "54294"
},
{
"input": "0 0 0 0 0 0 0 0 0 0 0 0 0 15",
... | 0 | 0 | -1 | 3,363 | |
920 | Water The Garden | [
"implementation"
] | null | null | It is winter now, and Max decided it's about time he watered the garden.
The garden can be represented as *n* consecutive garden beds, numbered from 1 to *n*. *k* beds contain water taps (*i*-th tap is located in the bed *x**i*), which, if turned on, start delivering water to neighbouring beds. If the tap on the bed *x**i* is turned on, then after one second has passed, the bed *x**i* will be watered; after two seconds have passed, the beds from the segment [*x**i*<=-<=1,<=*x**i*<=+<=1] will be watered (if they exist); after *j* seconds have passed (*j* is an integer number), the beds from the segment [*x**i*<=-<=(*j*<=-<=1),<=*x**i*<=+<=(*j*<=-<=1)] will be watered (if they exist). Nothing changes during the seconds, so, for example, we can't say that the segment [*x**i*<=-<=2.5,<=*x**i*<=+<=2.5] will be watered after 2.5 seconds have passed; only the segment [*x**i*<=-<=2,<=*x**i*<=+<=2] will be watered at that moment.
Max wants to turn on all the water taps at the same moment, and now he wonders, what is the minimum number of seconds that have to pass after he turns on some taps until the whole garden is watered. Help him to find the answer! | The first line contains one integer *t* β the number of test cases to solve (1<=β€<=*t*<=β€<=200).
Then *t* test cases follow. The first line of each test case contains two integers *n* and *k* (1<=β€<=*n*<=β€<=200, 1<=β€<=*k*<=β€<=*n*) β the number of garden beds and water taps, respectively.
Next line contains *k* integers *x**i* (1<=β€<=*x**i*<=β€<=*n*) β the location of *i*-th water tap. It is guaranteed that for each condition *x**i*<=-<=1<=<<=*x**i* holds.
It is guaranteed that the sum of *n* over all test cases doesn't exceed 200.
Note that in hacks you have to set *t*<==<=1. | For each test case print one integer β the minimum number of seconds that have to pass after Max turns on some of the water taps, until the whole garden is watered. | [
"3\n5 1\n3\n3 3\n1 2 3\n4 1\n1\n"
] | [
"3\n1\n4\n"
] | The first example consists of 3 tests:
1. There are 5 garden beds, and a water tap in the bed 3. If we turn it on, then after 1 second passes, only bed 3 will be watered; after 2 seconds pass, beds [1,β3] will be watered, and after 3 seconds pass, everything will be watered. 1. There are 3 garden beds, and there is a water tap in each one. If we turn all of them on, then everything will be watered after 1 second passes. 1. There are 4 garden beds, and only one tap in the bed 1. It will take 4 seconds to water, for example, bed 4. | [
{
"input": "3\n5 1\n3\n3 3\n1 2 3\n4 1\n1",
"output": "3\n1\n4"
},
{
"input": "26\n1 1\n1\n2 1\n2\n2 1\n1\n2 2\n1 2\n3 1\n3\n3 1\n2\n3 2\n2 3\n3 1\n1\n3 2\n1 3\n3 2\n1 2\n3 3\n1 2 3\n4 1\n4\n4 1\n3\n4 2\n3 4\n4 1\n2\n4 2\n2 4\n4 2\n2 3\n4 3\n2 3 4\n4 1\n1\n4 2\n1 4\n4 2\n1 3\n4 3\n1 3 4\n4 2\n1 2\n4... | 93 | 3,584,000 | -1 | 3,370 | |
733 | Parade | [
"math"
] | null | null | Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be *n* columns participating in the parade, the *i*-th column consists of *l**i* soldiers, who start to march from left leg, and *r**i* soldiers, who start to march from right leg.
The beauty of the parade is calculated by the following formula: if *L* is the total number of soldiers on the parade who start to march from the left leg, and *R* is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |*L*<=-<=*R*|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index *i* and swap values *l**i* and *r**i*.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. | The first line contains single integer *n* (1<=β€<=*n*<=β€<=105)Β β the number of columns.
The next *n* lines contain the pairs of integers *l**i* and *r**i* (1<=β€<=*l**i*,<=*r**i*<=β€<=500)Β β the number of soldiers in the *i*-th column which start to march from the left or the right leg respectively. | Print single integer *k*Β β the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.
Consider that columns are numbered from 1 to *n* in the order they are given in the input data.
If there are several answers, print any of them. | [
"3\n5 6\n8 9\n10 3\n",
"2\n6 5\n5 6\n",
"6\n5 9\n1 3\n4 8\n4 5\n23 54\n12 32\n"
] | [
"3\n",
"1\n",
"0\n"
] | In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5β+β8β+β10β=β23, and from the right legΒ β 6β+β9β+β3β=β18. In this case the beauty of the parade will equal |23β-β18|β=β5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5β+β8β+β3β=β16, and who march from the right legΒ β 6β+β9β+β10β=β25. In this case the beauty equals |16β-β25|β=β9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9. | [
{
"input": "3\n5 6\n8 9\n10 3",
"output": "3"
},
{
"input": "2\n6 5\n5 6",
"output": "1"
},
{
"input": "6\n5 9\n1 3\n4 8\n4 5\n23 54\n12 32",
"output": "0"
},
{
"input": "2\n500 499\n500 500",
"output": "0"
},
{
"input": "1\n139 252",
"output": "0"
},
{
... | 1,000 | 10,240,000 | 0 | 3,371 | |
809 | Do you want a date? | [
"implementation",
"math",
"sortings"
] | null | null | Leha decided to move to a quiet town ViΔkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the week Leha managed to get access to *n* computers throughout the town. Incidentally all the computers, which were hacked by Leha, lie on the same straight line, due to the reason that there is the only one straight street in ViΔkopolis.
Let's denote the coordinate system on this street. Besides let's number all the hacked computers with integers from 1 to *n*. So the *i*-th hacked computer is located at the point *x**i*. Moreover the coordinates of all computers are distinct.
Leha is determined to have a little rest after a hard week. Therefore he is going to invite his friend Noora to a restaurant. However the girl agrees to go on a date with the only one condition: Leha have to solve a simple task.
Leha should calculate a sum of *F*(*a*) for all *a*, where *a* is a non-empty subset of the set, that consists of all hacked computers. Formally, let's denote *A* the set of all integers from 1 to *n*. Noora asks the hacker to find value of the expression . Here *F*(*a*) is calculated as the maximum among the distances between all pairs of computers from the set *a*. Formally, . Since the required sum can be quite large Noora asks to find it modulo 109<=+<=7.
Though, Leha is too tired. Consequently he is not able to solve this task. Help the hacker to attend a date. | The first line contains one integer *n* (1<=β€<=*n*<=β€<=3Β·105) denoting the number of hacked computers.
The second line contains *n* integers *x*1,<=*x*2,<=...,<=*x**n* (1<=β€<=*x**i*<=β€<=109) denoting the coordinates of hacked computers. It is guaranteed that all *x**i* are distinct. | Print a single integerΒ β the required sum modulo 109<=+<=7. | [
"2\n4 7\n",
"3\n4 3 1\n"
] | [
"3\n",
"9\n"
] | There are three non-empty subsets in the first sample test:<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/02b2d12556dad85f1c6c6912786eb87d4be2ea17.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/22f6a537962c86b3e28ddb8aaca28a7cdd219a8c.png" style="max-width: 100.0%;max-height: 100.0%;"/> and <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7d0f73b3e94e13cb797f39e93d9da74835c5a02d.png" style="max-width: 100.0%;max-height: 100.0%;"/>. The first and the second subset increase the sum by 0 and the third subset increases the sum by 7β-β4β=β3. In total the answer is 0β+β0β+β3β=β3.
There are seven non-empty subsets in the second sample test. Among them only the following subsets increase the answer: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/f368c407c8e85e2b5fedfffaff39d471d765f026.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/bb8f2118a3ac352db393b1f067b28e398ce7f816.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/049032074c04b16bc0cc153f95471c40b222072b.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/dc93c7f5b3d122314c9c5a707fae556a8f72a574.png" style="max-width: 100.0%;max-height: 100.0%;"/>. In total the sum is (4β-β3)β+β(4β-β1)β+β(3β-β1)β+β(4β-β1)β=β9. | [
{
"input": "2\n4 7",
"output": "3"
},
{
"input": "3\n4 3 1",
"output": "9"
},
{
"input": "20\n8 11 13 19 21 34 36 44 57 58 61 63 76 78 79 81 85 86 90 95",
"output": "83396599"
},
{
"input": "20\n1 8 9 12 15 17 18 24 30 33 36 41 53 54 59 62 64 66 72 73",
"output": "6805914... | 545 | 268,390,400 | 0 | 3,373 | |
911 | Three Garlands | [
"brute force",
"constructive algorithms"
] | null | null | Mishka is decorating the Christmas tree. He has got three garlands, and all of them will be put on the tree. After that Mishka will switch these garlands on.
When a garland is switched on, it periodically changes its state β sometimes it is lit, sometimes not. Formally, if *i*-th garland is switched on during *x*-th second, then it is lit only during seconds *x*, *x*<=+<=*k**i*, *x*<=+<=2*k**i*, *x*<=+<=3*k**i* and so on.
Mishka wants to switch on the garlands in such a way that during each second after switching the garlands on there would be at least one lit garland. Formally, Mishka wants to choose three integers *x*1, *x*2 and *x*3 (not necessarily distinct) so that he will switch on the first garland during *x*1-th second, the second one β during *x*2-th second, and the third one β during *x*3-th second, respectively, and during each second starting from *max*(*x*1,<=*x*2,<=*x*3) at least one garland will be lit.
Help Mishka by telling him if it is possible to do this! | The first line contains three integers *k*1, *k*2 and *k*3 (1<=β€<=*k**i*<=β€<=1500) β time intervals of the garlands. | If Mishka can choose moments of time to switch on the garlands in such a way that each second after switching the garlands on at least one garland will be lit, print YES.
Otherwise, print NO. | [
"2 2 3\n",
"4 2 3\n"
] | [
"YES\n",
"NO\n"
] | In the first example Mishka can choose *x*<sub class="lower-index">1</sub>β=β1, *x*<sub class="lower-index">2</sub>β=β2, *x*<sub class="lower-index">3</sub>β=β1. The first garland will be lit during seconds 1,β3,β5,β7,β..., the second β 2,β4,β6,β8,β..., which already cover all the seconds after the 2-nd one. It doesn't even matter what *x*<sub class="lower-index">3</sub> is chosen. Our choice will lead third to be lit during seconds 1,β4,β7,β10,β..., though.
In the second example there is no way to choose such moments of time, there always be some seconds when no garland is lit. | [
{
"input": "2 2 3",
"output": "YES"
},
{
"input": "4 2 3",
"output": "NO"
},
{
"input": "1499 1498 1500",
"output": "NO"
},
{
"input": "1500 1500 1500",
"output": "NO"
},
{
"input": "100 4 1",
"output": "YES"
},
{
"input": "4 2 4",
"output": "YES"
... | 61 | 5,632,000 | 0 | 3,377 | |
216 | Forming Teams | [
"dfs and similar",
"implementation"
] | null | null | One day *n* students come to the stadium. They want to play football, and for that they need to split into teams, the teams must have an equal number of people.
We know that this group of people has archenemies. Each student has at most two archenemies. Besides, if student *A* is an archenemy to student *B*, then student *B* is an archenemy to student *A*.
The students want to split so as no two archenemies were in one team. If splitting in the required manner is impossible, some students will have to sit on the bench.
Determine the minimum number of students you will have to send to the bench in order to form the two teams in the described manner and begin the game at last. | The first line contains two integers *n* and *m* (2<=β€<=*n*<=β€<=100, 1<=β€<=*m*<=β€<=100) β the number of students and the number of pairs of archenemies correspondingly.
Next *m* lines describe enmity between students. Each enmity is described as two numbers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*, *a**i*<=β <=*b**i*) β the indexes of the students who are enemies to each other. Each enmity occurs in the list exactly once. It is guaranteed that each student has no more than two archenemies.
You can consider the students indexed in some manner with distinct integers from 1 to *n*. | Print a single integer β the minimum number of students you will have to send to the bench in order to start the game. | [
"5 4\n1 2\n2 4\n5 3\n1 4\n",
"6 2\n1 4\n3 4\n",
"6 6\n1 2\n2 3\n3 1\n4 5\n5 6\n6 4\n"
] | [
"1",
"0",
"2"
] | none | [
{
"input": "5 4\n1 2\n2 4\n5 3\n1 4",
"output": "1"
},
{
"input": "6 2\n1 4\n3 4",
"output": "0"
},
{
"input": "6 6\n1 2\n2 3\n3 1\n4 5\n5 6\n6 4",
"output": "2"
},
{
"input": "5 1\n1 2",
"output": "1"
},
{
"input": "8 8\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 1",
... | 810 | 31,539,200 | 0 | 3,379 | |
702 | Cellular Network | [
"binary search",
"implementation",
"two pointers"
] | null | null | You are given *n* points on the straight line β the positions (*x*-coordinates) of the cities and *m* points on the same line β the positions (*x*-coordinates) of the cellular towers. All towers work in the same way β they provide cellular network for all cities, which are located at the distance which is no more than *r* from this tower.
Your task is to find minimal *r* that each city has been provided by cellular network, i.e. for each city there is at least one cellular tower at the distance which is no more than *r*.
If *r*<==<=0 then a tower provides cellular network only for the point where it is located. One tower can provide cellular network for any number of cities, but all these cities must be at the distance which is no more than *r* from this tower. | The first line contains two positive integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=105) β the number of cities and the number of cellular towers.
The second line contains a sequence of *n* integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=β€<=*a**i*<=β€<=109) β the coordinates of cities. It is allowed that there are any number of cities in the same point. All coordinates *a**i* are given in non-decreasing order.
The third line contains a sequence of *m* integers *b*1,<=*b*2,<=...,<=*b**m* (<=-<=109<=β€<=*b**j*<=β€<=109) β the coordinates of cellular towers. It is allowed that there are any number of towers in the same point. All coordinates *b**j* are given in non-decreasing order. | Print minimal *r* so that each city will be covered by cellular network. | [
"3 2\n-2 2 4\n-3 0\n",
"5 3\n1 5 10 14 17\n4 11 15\n"
] | [
"4\n",
"3\n"
] | none | [
{
"input": "3 2\n-2 2 4\n-3 0",
"output": "4"
},
{
"input": "5 3\n1 5 10 14 17\n4 11 15",
"output": "3"
},
{
"input": "1 1\n-1000000000\n1000000000",
"output": "2000000000"
},
{
"input": "1 1\n1000000000\n-1000000000",
"output": "2000000000"
},
{
"input": "10 10\n... | 31 | 0 | -1 | 3,382 | |
221 | Little Elephant and Numbers | [
"implementation"
] | null | null | The Little Elephant loves numbers.
He has a positive integer *x*. The Little Elephant wants to find the number of positive integers *d*, such that *d* is the divisor of *x*, and *x* and *d* have at least one common (the same) digit in their decimal representations.
Help the Little Elephant to find the described number. | A single line contains a single integer *x* (1<=β€<=*x*<=β€<=109). | In a single line print an integer β the answer to the problem. | [
"1\n",
"10\n"
] | [
"1\n",
"2\n"
] | none | [
{
"input": "1",
"output": "1"
},
{
"input": "10",
"output": "2"
},
{
"input": "47",
"output": "1"
},
{
"input": "100",
"output": "5"
},
{
"input": "128",
"output": "6"
},
{
"input": "2",
"output": "1"
},
{
"input": "17",
"output": "2"
... | 248 | 21,606,400 | 3 | 3,394 | |
811 | Vladik and Memorable Trip | [
"dp",
"implementation"
] | null | null | Vladik often travels by trains. He remembered some of his trips especially well and I would like to tell you about one of these trips:
Vladik is at initial train station, and now *n* people (including Vladik) want to get on the train. They are already lined up in some order, and for each of them the city code *a**i* is known (the code of the city in which they are going to).
Train chief selects some number of disjoint segments of the original sequence of people (covering entire sequence by segments is not necessary). People who are in the same segment will be in the same train carriage. The segments are selected in such way that if at least one person travels to the city *x*, then all people who are going to city *x* should be in the same railway carriage. This means that they canβt belong to different segments. Note, that all people who travel to the city *x*, either go to it and in the same railway carriage, or do not go anywhere at all.
Comfort of a train trip with people on segment from position *l* to position *r* is equal to XOR of all distinct codes of cities for people on the segment from position *l* to position *r*. XOR operation also known as exclusive OR.
Total comfort of a train trip is equal to sum of comfort for each segment.
Help Vladik to know maximal possible total comfort. | First line contains single integer *n* (1<=β€<=*n*<=β€<=5000)Β β number of people.
Second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=5000), where *a**i* denotes code of the city to which *i*-th person is going. | The output should contain a single integerΒ β maximal possible total comfort. | [
"6\n4 4 2 5 2 3\n",
"9\n5 1 3 1 5 2 4 2 5\n"
] | [
"14\n",
"9\n"
] | In the first test case best partition into segments is: [4,β4] [2,β5,β2] [3], answer is calculated as follows: 4β+β(2 *xor* 5)β+β3β=β4β+β7β+β3β=β14
In the second test case best partition into segments is: 5 1 [3] 1 5 [2,β4,β2] 5, answer calculated as follows: 3β+β(2 *xor* 4)β=β3β+β6β=β9. | [
{
"input": "6\n4 4 2 5 2 3",
"output": "14"
},
{
"input": "9\n5 1 3 1 5 2 4 2 5",
"output": "9"
},
{
"input": "5\n1558 4081 3591 1700 3232",
"output": "14162"
},
{
"input": "10\n3838 1368 4825 2068 4755 2048 1342 4909 2837 4854",
"output": "32844"
},
{
"input": "1... | 30 | 0 | 0 | 3,396 | |
567 | Berland National Library | [
"implementation"
] | null | null | Berland National Library has recently been built in the capital of Berland. In addition, in the library you can take any of the collected works of Berland leaders, the library has a reading room.
Today was the pilot launch of an automated reading room visitors' accounting system! The scanner of the system is installed at the entrance to the reading room. It records the events of the form "reader entered room", "reader left room". Every reader is assigned a registration number during the registration procedure at the library β it's a unique integer from 1 to 106. Thus, the system logs events of two forms:
- "+ *r**i*" β the reader with registration number *r**i* entered the room; - "- *r**i*" β the reader with registration number *r**i* left the room.
The first launch of the system was a success, it functioned for some period of time, and, at the time of its launch and at the time of its shutdown, the reading room may already have visitors.
Significant funds of the budget of Berland have been spent on the design and installation of the system. Therefore, some of the citizens of the capital now demand to explain the need for this system and the benefits that its implementation will bring. Now, the developers of the system need to urgently come up with reasons for its existence.
Help the system developers to find the minimum possible capacity of the reading room (in visitors) using the log of the system available to you. | The first line contains a positive integer *n* (1<=β€<=*n*<=β€<=100) β the number of records in the system log. Next follow *n* events from the system journal in the order in which the were made. Each event was written on a single line and looks as "+ *r**i*" or "- *r**i*", where *r**i* is an integer from 1 to 106, the registration number of the visitor (that is, distinct visitors always have distinct registration numbers).
It is guaranteed that the log is not contradictory, that is, for every visitor the types of any of his two consecutive events are distinct. Before starting the system, and after stopping the room may possibly contain visitors. | Print a single integer β the minimum possible capacity of the reading room. | [
"6\n+ 12001\n- 12001\n- 1\n- 1200\n+ 1\n+ 7\n",
"2\n- 1\n- 2\n",
"2\n+ 1\n- 1\n"
] | [
"3",
"2",
"1"
] | In the first sample test, the system log will ensure that at some point in the reading room were visitors with registration numbers 1, 1200 and 12001. More people were not in the room at the same time based on the log. Therefore, the answer to the test is 3. | [
{
"input": "6\n+ 12001\n- 12001\n- 1\n- 1200\n+ 1\n+ 7",
"output": "3"
},
{
"input": "2\n- 1\n- 2",
"output": "2"
},
{
"input": "2\n+ 1\n- 1",
"output": "1"
},
{
"input": "5\n+ 1\n- 1\n+ 2\n+ 3\n- 4",
"output": "3"
},
{
"input": "3\n- 1\n- 2\n- 3",
"output": "... | 46 | 102,400 | 0 | 3,411 | |
746 | Compote | [
"implementation",
"math"
] | null | null | Nikolay has *a* lemons, *b* apples and *c* pears. He decided to cook a compote. According to the recipe the fruits should be in the ratio 1:<=2:<=4. It means that for each lemon in the compote should be exactly 2 apples and exactly 4 pears. You can't crumble up, break up or cut these fruits into pieces. These fruitsΒ β lemons, apples and pearsΒ β should be put in the compote as whole fruits.
Your task is to determine the maximum total number of lemons, apples and pears from which Nikolay can cook the compote. It is possible that Nikolay can't use any fruits, in this case print 0. | The first line contains the positive integer *a* (1<=β€<=*a*<=β€<=1000)Β β the number of lemons Nikolay has.
The second line contains the positive integer *b* (1<=β€<=*b*<=β€<=1000)Β β the number of apples Nikolay has.
The third line contains the positive integer *c* (1<=β€<=*c*<=β€<=1000)Β β the number of pears Nikolay has. | Print the maximum total number of lemons, apples and pears from which Nikolay can cook the compote. | [
"2\n5\n7\n",
"4\n7\n13\n",
"2\n3\n2\n"
] | [
"7\n",
"21\n",
"0\n"
] | In the first example Nikolay can use 1 lemon, 2 apples and 4 pears, so the answer is 1β+β2β+β4β=β7.
In the second example Nikolay can use 3 lemons, 6 apples and 12 pears, so the answer is 3β+β6β+β12β=β21.
In the third example Nikolay don't have enough pears to cook any compote, so the answer is 0. | [
{
"input": "2\n5\n7",
"output": "7"
},
{
"input": "4\n7\n13",
"output": "21"
},
{
"input": "2\n3\n2",
"output": "0"
},
{
"input": "1\n1\n1",
"output": "0"
},
{
"input": "1\n2\n4",
"output": "7"
},
{
"input": "1000\n1000\n1000",
"output": "1750"
}... | 77 | 6,758,400 | 3 | 3,414 | |
0 | none | [
"none"
] | null | null | Ivan had string *s* consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string *s*. Ivan preferred making a new string to finding the old one.
Ivan knows some information about the string *s*. Namely, he remembers, that string *t**i* occurs in string *s* at least *k**i* times or more, he also remembers exactly *k**i* positions where the string *t**i* occurs in string *s*: these positions are *x**i*,<=1,<=*x**i*,<=2,<=...,<=*x**i*,<=*k**i*. He remembers *n* such strings *t**i*.
You are to reconstruct lexicographically minimal string *s* such that it fits all the information Ivan remembers. Strings *t**i* and string *s* consist of small English letters only. | The first line contains single integer *n* (1<=β€<=*n*<=β€<=105) β the number of strings Ivan remembers.
The next *n* lines contain information about the strings. The *i*-th of these lines contains non-empty string *t**i*, then positive integer *k**i*, which equal to the number of times the string *t**i* occurs in string *s*, and then *k**i* distinct positive integers *x**i*,<=1,<=*x**i*,<=2,<=...,<=*x**i*,<=*k**i* in increasing order β positions, in which occurrences of the string *t**i* in the string *s* start. It is guaranteed that the sum of lengths of strings *t**i* doesn't exceed 106, 1<=β€<=*x**i*,<=*j*<=β€<=106, 1<=β€<=*k**i*<=β€<=106, and the sum of all *k**i* doesn't exceed 106. The strings *t**i* can coincide.
It is guaranteed that the input data is not self-contradictory, and thus at least one answer always exists. | Print lexicographically minimal string that fits all the information Ivan remembers. | [
"3\na 4 1 3 5 7\nab 2 1 5\nca 1 4\n",
"1\na 1 3\n",
"3\nab 1 1\naba 1 3\nab 2 3 5\n"
] | [
"abacaba\n",
"aaa\n",
"ababab\n"
] | none | [
{
"input": "3\na 4 1 3 5 7\nab 2 1 5\nca 1 4",
"output": "abacaba"
},
{
"input": "1\na 1 3",
"output": "aaa"
},
{
"input": "3\nab 1 1\naba 1 3\nab 2 3 5",
"output": "ababab"
},
{
"input": "6\nba 2 16 18\na 1 12\nb 3 4 13 20\nbb 2 6 8\nababbbbbaab 1 3\nabababbbbb 1 1",
"ou... | 46 | 4,812,800 | -1 | 3,419 | |
140 | New Year Table | [
"geometry",
"math"
] | null | null | Gerald is setting the New Year table. The table has the form of a circle; its radius equals *R*. Gerald invited many guests and is concerned whether the table has enough space for plates for all those guests. Consider all plates to be round and have the same radii that equal *r*. Each plate must be completely inside the table and must touch the edge of the table. Of course, the plates must not intersect, but they can touch each other. Help Gerald determine whether the table is large enough for *n* plates. | The first line contains three integers *n*, *R* and *r* (1<=β€<=*n*<=β€<=100, 1<=β€<=*r*,<=*R*<=β€<=1000) β the number of plates, the radius of the table and the plates' radius. | Print "YES" (without the quotes) if it is possible to place *n* plates on the table by the rules given above. If it is impossible, print "NO".
Remember, that each plate must touch the edge of the table. | [
"4 10 4\n",
"5 10 4\n",
"1 10 10\n"
] | [
"YES\n",
"NO\n",
"YES\n"
] | The possible arrangement of the plates for the first sample is: | [
{
"input": "4 10 4",
"output": "YES"
},
{
"input": "5 10 4",
"output": "NO"
},
{
"input": "1 10 10",
"output": "YES"
},
{
"input": "3 10 20",
"output": "NO"
},
{
"input": "2 20 11",
"output": "NO"
},
{
"input": "6 9 3",
"output": "YES"
},
{
... | 92 | 0 | 0 | 3,420 | |
710 | Optimal Point on a Line | [
"brute force",
"sortings"
] | null | null | You are given *n* points on a line with their coordinates *x**i*. Find the point *x* so the sum of distances to the given points is minimal. | The first line contains integer *n* (1<=β€<=*n*<=β€<=3Β·105) β the number of points on the line.
The second line contains *n* integers *x**i* (<=-<=109<=β€<=*x**i*<=β€<=109) β the coordinates of the given *n* points. | Print the only integer *x* β the position of the optimal point on the line. If there are several optimal points print the position of the leftmost one. It is guaranteed that the answer is always the integer. | [
"4\n1 2 3 4\n"
] | [
"2\n"
] | none | [
{
"input": "4\n1 2 3 4",
"output": "2"
},
{
"input": "5\n-1 -10 2 6 7",
"output": "2"
},
{
"input": "10\n-68 10 87 22 30 89 82 -97 -52 25",
"output": "22"
},
{
"input": "100\n457 827 807 17 871 935 907 -415 536 170 551 -988 865 758 -457 -892 -875 -488 684 19 0 555 -807 -624 -... | 249 | 37,171,200 | -1 | 3,424 | |
0 | none | [
"none"
] | null | null | Polycarpus has a finite sequence of opening and closing brackets. In order not to fall asleep in a lecture, Polycarpus is having fun with his sequence. He is able to perform two operations:
- adding any bracket in any position (in the beginning, the end, or between any two existing brackets); - cyclic shift β moving the last bracket from the end of the sequence to the beginning.
Polycarpus can apply any number of operations to his sequence and adding a cyclic shift in any order. As a result, he wants to get the correct bracket sequence of the minimum possible length. If there are several such sequences, Polycarpus is interested in the lexicographically smallest one. Help him find such a sequence.
Acorrect bracket sequence is a sequence of opening and closing brackets, from which you can get a correct arithmetic expression by adding characters "1" and "+" . Each opening bracket must correspond to a closed one. For example, the sequences "(())()", "()", "(()(()))" are correct and ")(", "(()" and "(()))(" are not.
The sequence *a*1 *a*2... *a**n* is lexicographically smaller than sequence *b*1 *b*2... *b**n*, if there is such number *i* from 1 to *n*, that*a**k*<==<=*b**k* for 1<=β€<=*k*<=<<=*i* and *a**i*<=<<=*b**i*. Consider that "(" <=<<= ")". | The first line contains Polycarpus's sequence consisting of characters "(" and ")". The length of a line is from 1 to 1<=000<=000. | Print a correct bracket sequence of the minimum length that Polycarpus can obtain by his operations. If there are multiple such sequences, print the lexicographically minimum one. | [
"()(())\n",
"()(\n"
] | [
"(())()",
"(())"
] | The sequence in the first example is already correct, but to get the lexicographically minimum answer, you need to perform four cyclic shift operations. In the second example you need to add a closing parenthesis between the second and third brackets and make a cyclic shift. You can first make the shift, and then add the bracket at the end. | [] | 62 | 0 | 0 | 3,433 | |
399 | Pages | [
"implementation"
] | null | null | User ainta is making a web site. This time he is going to make a navigation of the pages. In his site, there are *n* pages numbered by integers from 1 to *n*. Assume that somebody is on the *p*-th page now. The navigation will look like this:
When someone clicks the button "<<" he is redirected to page 1, and when someone clicks the button ">>" he is redirected to page *n*. Of course if someone clicks on a number, he is redirected to the corresponding page.
There are some conditions in the navigation:
- If page 1 is in the navigation, the button "<<" must not be printed. - If page *n* is in the navigation, the button ">>" must not be printed. - If the page number is smaller than 1 or greater than *n*, it must not be printed.
You can see some examples of the navigations. Make a program that prints the navigation. | The first and the only line contains three integers *n*, *p*, *k* (3<=β€<=*n*<=β€<=100; 1<=β€<=*p*<=β€<=*n*; 1<=β€<=*k*<=β€<=*n*) | Print the proper navigation. Follow the format of the output from the test samples. | [
"17 5 2\n",
"6 5 2\n",
"6 1 2\n",
"6 2 2\n",
"9 6 3\n",
"10 6 3\n",
"8 5 4\n"
] | [
"<< 3 4 (5) 6 7 >> ",
"<< 3 4 (5) 6 ",
"(1) 2 3 >> ",
"1 (2) 3 4 >>",
"<< 3 4 5 (6) 7 8 9",
"<< 3 4 5 (6) 7 8 9 >>",
"1 2 3 4 (5) 6 7 8 "
] | none | [
{
"input": "17 5 2",
"output": "<< 3 4 (5) 6 7 >> "
},
{
"input": "6 5 2",
"output": "<< 3 4 (5) 6 "
},
{
"input": "6 1 2",
"output": "(1) 2 3 >> "
},
{
"input": "6 2 2",
"output": "1 (2) 3 4 >> "
},
{
"input": "9 6 3",
"output": "<< 3 4 5 (6) 7 8 9 "
},
{... | 109 | 0 | 3 | 3,437 | |
620 | Professor GukiZ's Robot | [
"implementation",
"math"
] | null | null | Professor GukiZ makes a new robot. The robot are in the point with coordinates (*x*1,<=*y*1) and should go to the point (*x*2,<=*y*2). In a single step the robot can change any of its coordinates (maybe both of them) by one (decrease or increase). So the robot can move in one of the 8 directions. Find the minimal number of steps the robot should make to get the finish position. | The first line contains two integers *x*1,<=*y*1 (<=-<=109<=β€<=*x*1,<=*y*1<=β€<=109) β the start position of the robot.
The second line contains two integers *x*2,<=*y*2 (<=-<=109<=β€<=*x*2,<=*y*2<=β€<=109) β the finish position of the robot. | Print the only integer *d* β the minimal number of steps to get the finish position. | [
"0 0\n4 5\n",
"3 4\n6 1\n"
] | [
"5\n",
"3\n"
] | In the first example robot should increase both of its coordinates by one four times, so it will be in position (4,β4). After that robot should simply increase its *y* coordinate and get the finish position.
In the second example robot should simultaneously increase *x* coordinate and decrease *y* coordinate by one three times. | [
{
"input": "0 0\n4 5",
"output": "5"
},
{
"input": "3 4\n6 1",
"output": "3"
},
{
"input": "0 0\n4 6",
"output": "6"
},
{
"input": "1 1\n-3 -5",
"output": "6"
},
{
"input": "-1 -1\n-10 100",
"output": "101"
},
{
"input": "1 -1\n100 -100",
"output":... | 92 | 20,172,800 | 0 | 3,441 | |
712 | Memory and De-Evolution | [
"greedy",
"math"
] | null | null | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length *x*, and he wishes to perform operations to obtain an equilateral triangle of side length *y*.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length *y*? | The first and only line contains two integers *x* and *y* (3<=β€<=*y*<=<<=*x*<=β€<=100<=000)Β β the starting and ending equilateral triangle side lengths respectively. | Print a single integerΒ β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length *y* if he starts with the equilateral triangle of side length *x*. | [
"6 3\n",
"8 5\n",
"22 4\n"
] | [
"4\n",
"3\n",
"6\n"
] | In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides *a*, *b*, and *c* as (*a*,β*b*,β*c*). Then, Memory can do <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/18af21f738bad490df83097a90e1f2879a4b21c6.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the second sample test, Memory can do <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/bcfd51d1b2d764a1cf5fbc255cc02e6f5aaed3b1.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the third sample test, Memory can do: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/0969b7d413854c1e7528991d926bef1f7ffba008.png" style="max-width: 100.0%;max-height: 100.0%;"/>
<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/63e9e66b882c03e4c73e93ad92204dc255329309.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [
{
"input": "6 3",
"output": "4"
},
{
"input": "8 5",
"output": "3"
},
{
"input": "22 4",
"output": "6"
},
{
"input": "4 3",
"output": "3"
},
{
"input": "57 27",
"output": "4"
},
{
"input": "61 3",
"output": "9"
},
{
"input": "5 4",
"out... | 124 | 0 | 3 | 3,444 | |
979 | Kuro and GCD and XOR and SUM | [
"binary search",
"bitmasks",
"brute force",
"data structures",
"dp",
"dsu",
"greedy",
"math",
"number theory",
"strings",
"trees"
] | null | null | Kuro is currently playing an educational game about numbers. The game focuses on the greatest common divisor (GCD), the XOR value, and the sum of two numbers. Kuro loves the game so much that he solves levels by levels day by day.
Sadly, he's going on a vacation for a day, and he isn't able to continue his solving streak on his own. As Katie is a reliable person, Kuro kindly asked her to come to his house on this day to play the game for him.
Initally, there is an empty array $a$. The game consists of $q$ tasks of two types. The first type asks Katie to add a number $u_i$ to $a$. The second type asks Katie to find a number $v$ existing in $a$ such that $k_i \mid GCD(x_i, v)$, $x_i + v \leq s_i$, and $x_i \oplus v$ is maximized, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR), $GCD(c, d)$ denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers $c$ and $d$, and $y \mid x$ means $x$ is divisible by $y$, or report -1 if no such numbers are found.
Since you are a programmer, Katie needs you to automatically and accurately perform the tasks in the game to satisfy her dear friend Kuro. Let's help her! | The first line contains one integer $q$ ($2 \leq q \leq 10^{5}$) β the number of tasks the game wants you to perform.
$q$ lines follow, each line begins with an integer $t_i$ β the type of the task:
- If $t_i = 1$, an integer $u_i$ follow ($1 \leq u_i \leq 10^{5}$) β you have to add $u_i$ to the array $a$. - If $t_i = 2$, three integers $x_i$, $k_i$, and $s_i$ follow ($1 \leq x_i, k_i, s_i \leq 10^{5}$) β you must find a number $v$ existing in the array $a$ such that $k_i \mid GCD(x_i, v)$, $x_i + v \leq s_i$, and $x_i \oplus v$ is maximized, where $\oplus$ denotes the XOR operation, or report -1 if no such numbers are found.
It is guaranteed that the type of the first task is type $1$, and there exists at least one task of type $2$. | For each task of type $2$, output on one line the desired number $v$, or -1 if no such numbers are found. | [
"5\n1 1\n1 2\n2 1 1 3\n2 1 1 2\n2 1 1 1\n",
"10\n1 9\n2 9 9 22\n2 3 3 18\n1 25\n2 9 9 20\n2 25 25 14\n1 20\n2 26 26 3\n1 14\n2 20 20 9\n"
] | [
"2\n1\n-1\n",
"9\n9\n9\n-1\n-1\n-1\n"
] | In the first example, there are 5 tasks:
- The first task requires you to add $1$ into $a$. $a$ is now $\left\{1\right\}$. - The second task requires you to add $2$ into $a$. $a$ is now $\left\{1, 2\right\}$. - The third task asks you a question with $x = 1$, $k = 1$ and $s = 3$. Taking both $1$ and $2$ as $v$ satisfies $1 \mid GCD(1, v)$ and $1 + v \leq 3$. Because $2 \oplus 1 = 3 > 1 \oplus 1 = 0$, $2$ is the answer to this task. - The fourth task asks you a question with $x = 1$, $k = 1$ and $s = 2$. Only $v = 1$ satisfies $1 \mid GCD(1, v)$ and $1 + v \leq 2$, so $1$ is the answer to this task. - The fifth task asks you a question with $x = 1$, $k = 1$ and $s = 1$. There are no elements in $a$ that satisfy the conditions, so we report -1 as the answer to this task. | [] | 2,000 | 104,243,200 | 0 | 3,451 | |
171 | Star | [
"*special",
"combinatorics"
] | null | null | The input contains a single integer *a* (1<=β€<=*a*<=β€<=18257). | Print a single integer *output* (1<=β€<=*output*<=β€<=2Β·109). | [
"2\n"
] | [
"13"
] | none | [
{
"input": "2",
"output": "13"
},
{
"input": "1",
"output": "1"
},
{
"input": "3",
"output": "37"
},
{
"input": "4",
"output": "73"
},
{
"input": "5",
"output": "121"
},
{
"input": "6",
"output": "181"
},
{
"input": "7",
"output": "253"... | 248 | 0 | 3 | 3,453 | ||
607 | Zuma | [
"dp"
] | null | null | Genos recently installed the game Zuma on his phone. In Zuma there exists a line of *n* gemstones, the *i*-th of which has color *c**i*. The goal of the game is to destroy all the gemstones in the line as quickly as possible.
In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?
Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on. | The first line of input contains a single integer *n* (1<=β€<=*n*<=β€<=500)Β β the number of gemstones.
The second line contains *n* space-separated integers, the *i*-th of which is *c**i* (1<=β€<=*c**i*<=β€<=*n*)Β β the color of the *i*-th gemstone in a line. | Print a single integerΒ β the minimum number of seconds needed to destroy the entire line. | [
"3\n1 2 1\n",
"3\n1 2 3\n",
"7\n1 4 4 2 3 2 1\n"
] | [
"1\n",
"3\n",
"2\n"
] | In the first sample, Genos can destroy the entire line in one second.
In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.
In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1. | [
{
"input": "3\n1 2 1",
"output": "1"
},
{
"input": "3\n1 2 3",
"output": "3"
},
{
"input": "7\n1 4 4 2 3 2 1",
"output": "2"
},
{
"input": "1\n1",
"output": "1"
},
{
"input": "2\n1 1",
"output": "1"
},
{
"input": "2\n1 2",
"output": "2"
},
{
... | 2,000 | 5,120,000 | 0 | 3,457 | |
346 | Lucky Common Subsequence | [
"dp",
"strings"
] | null | null | In mathematics, a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. For example, the sequence BDF is a subsequence of ABCDEF. A substring of a string is a continuous subsequence of the string. For example, BCD is a substring of ABCDEF.
You are given two strings *s*1, *s*2 and another string called *virus*. Your task is to find the longest common subsequence of *s*1 and *s*2, such that it doesn't contain *virus* as a substring. | The input contains three strings in three separate lines: *s*1, *s*2 and *virus* (1<=β€<=|*s*1|,<=|*s*2|,<=|*virus*|<=β€<=100). Each string consists only of uppercase English letters. | Output the longest common subsequence of *s*1 and *s*2 without *virus* as a substring. If there are multiple answers, any of them will be accepted.
If there is no valid common subsequence, output 0. | [
"AJKEQSLOBSROFGZ\nOVGURWZLWVLUXTH\nOZ\n",
"AA\nA\nA\n"
] | [
"ORZ\n",
"0\n"
] | none | [
{
"input": "AJKEQSLOBSROFGZ\nOVGURWZLWVLUXTH\nOZ",
"output": "ORZ"
},
{
"input": "AA\nA\nA",
"output": "0"
},
{
"input": "PWBJTZPQHA\nZJMKLWSROQ\nUQ",
"output": "WQ"
},
{
"input": "QNHRPFYMAAPJDUHBAEXNEEZSTMYHVGQPYKNMVKMBVSVLIYGUVMJHEFLJEPIWFHSLISTGOKRXNMSCXYKMAXBPKCOCNTIRPCU... | 62 | 0 | 0 | 3,459 | |
776 | Molly's Chemicals | [
"binary search",
"brute force",
"data structures",
"implementation",
"math"
] | null | null | Molly Hooper has *n* different kinds of chemicals arranged in a line. Each of the chemicals has an affection value, The *i*-th of them has affection value *a**i*.
Molly wants Sherlock to fall in love with her. She intends to do this by mixing a contiguous segment of chemicals together to make a love potion with total affection value as a non-negative integer power of *k*. Total affection value of a continuous segment of chemicals is the sum of affection values of each chemical in that segment.
Help her to do so in finding the total number of such segments. | The first line of input contains two integers, *n* and *k*, the number of chemicals and the number, such that the total affection value is a non-negative power of this number *k*. (1<=β€<=*n*<=β€<=105, 1<=β€<=|*k*|<=β€<=10).
Next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=β€<=*a**i*<=β€<=109)Β β affection values of chemicals. | Output a single integerΒ β the number of valid segments. | [
"4 2\n2 2 2 2\n",
"4 -3\n3 -6 -3 12\n"
] | [
"8\n",
"3\n"
] | Do keep in mind that *k*<sup class="upper-index">0</sup>β=β1.
In the first sample, Molly can get following different affection values:
- 2: segments [1,β1], [2,β2], [3,β3], [4,β4]; - 4: segments [1,β2], [2,β3], [3,β4]; - 6: segments [1,β3], [2,β4]; - 8: segments [1,β4].
Out of these, 2, 4 and 8 are powers of *k*β=β2. Therefore, the answer is 8.
In the second sample, Molly can choose segments [1,β2], [3,β3], [3,β4]. | [
{
"input": "4 2\n2 2 2 2",
"output": "8"
},
{
"input": "4 -3\n3 -6 -3 12",
"output": "3"
},
{
"input": "14 -9\n-2 -4 62 53 90 41 35 21 85 74 85 57 10 39",
"output": "0"
},
{
"input": "20 9\n90 21 -6 -61 14 -21 -17 -65 -84 -75 -48 56 67 -50 16 65 -79 -61 92 85",
"output": ... | 2,500 | 63,488,000 | 0 | 3,461 | |
0 | none | [
"none"
] | null | null | Vasya became interested in bioinformatics. He's going to write an article about similar cyclic DNA sequences, so he invented a new method for determining the similarity of cyclic sequences.
Let's assume that strings *s* and *t* have the same length *n*, then the function *h*(*s*,<=*t*) is defined as the number of positions in which the respective symbols of *s* and *t* are the same. Function *h*(*s*,<=*t*) can be used to define the function of Vasya distance Ο(*s*,<=*t*):
Vasya found a string *s* of length *n* on the Internet. Now he wants to count how many strings *t* there are such that the Vasya distance from the string *s* attains maximum possible value. Formally speaking, *t* must satisfy the equation: .
Vasya could not try all possible strings to find an answer, so he needs your help. As the answer may be very large, count the number of such strings modulo 109<=+<=7. | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=105).
The second line of the input contains a single string of length *n*, consisting of characters "ACGT". | Print a single numberΒ β the answer modulo 109<=+<=7. | [
"1\nC\n",
"2\nAG\n",
"3\nTTT\n"
] | [
"1\n",
"4\n",
"1\n"
] | Please note that if for two distinct strings *t*<sub class="lower-index">1</sub> and *t*<sub class="lower-index">2</sub> values Ο(*s*,β*t*<sub class="lower-index">1</sub>) ΠΈ Ο(*s*,β*t*<sub class="lower-index">2</sub>) are maximum among all possible *t*, then both strings must be taken into account in the answer even if one of them can be obtained by a circular shift of another one.
In the first sample, there is Ο("*C*",β"*C*")β=β1, for the remaining strings *t* of length 1 the value of Ο(*s*,β*t*) is 0.
In the second sample, Ο("*AG*",β"*AG*")β=βΟ("*AG*",β"*GA*")β=βΟ("*AG*",β"*AA*")β=βΟ("*AG*",β"*GG*")β=β4.
In the third sample, Ο("*TTT*",β"*TTT*")β=β27 | [
{
"input": "1\nC",
"output": "1"
},
{
"input": "2\nAG",
"output": "4"
},
{
"input": "3\nTTT",
"output": "1"
},
{
"input": "4\nGACT",
"output": "256"
},
{
"input": "1\nT",
"output": "1"
},
{
"input": "2\nAG",
"output": "4"
},
{
"input": "3\n... | 0 | 0 | -1 | 3,463 | |
39 | Cubical Planet | [
"math"
] | D. Cubical Planet | 2 | 64 | You can find anything whatsoever in our Galaxy! A cubical planet goes round an icosahedral star. Let us introduce a system of axes so that the edges of the cubical planet are parallel to the coordinate axes and two opposite vertices lay in the points (0,<=0,<=0) and (1,<=1,<=1). Two flies live on the planet. At the moment they are sitting on two different vertices of the cubical planet. Your task is to determine whether they see each other or not. The flies see each other when the vertices they occupy lie on the same face of the cube. | The first line contains three space-separated integers (0 or 1) β the coordinates of the first fly, the second line analogously contains the coordinates of the second fly. | Output "YES" (without quotes) if the flies see each other. Otherwise, output "NO". | [
"0 0 0\n0 1 0\n",
"1 1 0\n0 1 0\n",
"0 0 0\n1 1 1\n"
] | [
"YES\n",
"YES\n",
"NO\n"
] | none | [
{
"input": "0 0 0\n0 1 0",
"output": "YES"
},
{
"input": "1 1 0\n0 1 0",
"output": "YES"
},
{
"input": "0 0 0\n1 1 1",
"output": "NO"
},
{
"input": "0 0 0\n1 0 0",
"output": "YES"
},
{
"input": "0 0 0\n0 1 0",
"output": "YES"
},
{
"input": "0 0 0\n1 1 ... | 218 | 0 | 3.9455 | 3,467 |
936 | Save Energy! | [
"binary search",
"implementation",
"math"
] | null | null | Julia is going to cook a chicken in the kitchen of her dormitory. To save energy, the stove in the kitchen automatically turns off after *k* minutes after turning on.
During cooking, Julia goes to the kitchen every *d* minutes and turns on the stove if it is turned off. While the cooker is turned off, it stays warm. The stove switches on and off instantly.
It is known that the chicken needs *t* minutes to be cooked on the stove, if it is turned on, and 2*t* minutes, if it is turned off. You need to find out, how much time will Julia have to cook the chicken, if it is considered that the chicken is cooked evenly, with constant speed when the stove is turned on and at a constant speed when it is turned off. | The single line contains three integers *k*, *d* and *t* (1<=β€<=*k*,<=*d*,<=*t*<=β€<=1018). | Print a single number, the total time of cooking in minutes. The relative or absolute error must not exceed 10<=-<=9.
Namely, let's assume that your answer is *x* and the answer of the jury is *y*. The checker program will consider your answer correct if . | [
"3 2 6\n",
"4 2 20\n"
] | [
"6.5\n",
"20.0\n"
] | In the first example, the chicken will be cooked for 3 minutes on the turned on stove, after this it will be cooked for <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/cce5d3f2f46552034d5ae5d487725705429ec7a5.png" style="max-width: 100.0%;max-height: 100.0%;"/>. Then the chicken will be cooked for one minute on a turned off stove, it will be cooked for <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/a10fa55d1324328f9ba60c9343ed0ecb0506d678.png" style="max-width: 100.0%;max-height: 100.0%;"/>. Thus, after four minutes the chicken will be cooked for <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/6fcc8bd6c2188b260d9d18e7b6c9e3908848df71.png" style="max-width: 100.0%;max-height: 100.0%;"/>. Before the fifth minute Julia will turn on the stove and after 2.5 minutes the chicken will be ready <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/87a86c8e9632089279245fff912c077126c4e704.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the second example, when the stove is turned off, Julia will immediately turn it on, so the stove will always be turned on and the chicken will be cooked in 20 minutes. | [
{
"input": "3 2 6",
"output": "6.5"
},
{
"input": "4 2 20",
"output": "20.0"
},
{
"input": "8 10 9",
"output": "10.0"
},
{
"input": "43 50 140",
"output": "150.5"
},
{
"input": "251 79 76",
"output": "76.0"
},
{
"input": "892 67 1000",
"output": "1... | 77 | 0 | 0 | 3,473 | |
961 | Tufurama | [
"data structures"
] | null | null | One day Polycarp decided to rewatch his absolute favourite episode of well-known TV series "Tufurama". He was pretty surprised when he got results only for season 7 episode 3 with his search query of "Watch Tufurama season 3 episode 7 online full hd free". This got Polycarp confused β what if he decides to rewatch the entire series someday and won't be able to find the right episodes to watch? Polycarp now wants to count the number of times he will be forced to search for an episode using some different method.
TV series have *n* seasons (numbered 1 through *n*), the *i*-th season has *a**i* episodes (numbered 1 through *a**i*). Polycarp thinks that if for some pair of integers *x* and *y* (*x*<=<<=*y*) exist both season *x* episode *y* and season *y* episode *x* then one of these search queries will include the wrong results. Help Polycarp to calculate the number of such pairs! | The first line contains one integer *n* (1<=<=β€<=*n*<=<=β€<=<=2Β·105) β the number of seasons.
The second line contains *n* integers separated by space *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109) β number of episodes in each season. | Print one integer β the number of pairs *x* and *y* (*x*<=<<=*y*) such that there exist both season *x* episode *y* and season *y* episode *x*. | [
"5\n1 2 3 4 5\n",
"3\n8 12 7\n",
"3\n3 2 1\n"
] | [
"0\n",
"3\n",
"2\n"
] | Possible pairs in the second example:
1. *x*β=β1, *y*β=β2 (season 1 episode 2 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/8774ca35b6e628888a4670e4539d47857e5e5841.png" style="max-width: 100.0%;max-height: 100.0%;"/> season 2 episode 1); 1. *x*β=β2, *y*β=β3 (season 2 episode 3 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/8774ca35b6e628888a4670e4539d47857e5e5841.png" style="max-width: 100.0%;max-height: 100.0%;"/> season 3 episode 2); 1. *x*β=β1, *y*β=β3 (season 1 episode 3 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/8774ca35b6e628888a4670e4539d47857e5e5841.png" style="max-width: 100.0%;max-height: 100.0%;"/> season 3 episode 1).
In the third example:
1. *x*β=β1, *y*β=β2 (season 1 episode 2 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/8774ca35b6e628888a4670e4539d47857e5e5841.png" style="max-width: 100.0%;max-height: 100.0%;"/> season 2 episode 1); 1. *x*β=β1, *y*β=β3 (season 1 episode 3 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/8774ca35b6e628888a4670e4539d47857e5e5841.png" style="max-width: 100.0%;max-height: 100.0%;"/> season 3 episode 1). | [
{
"input": "5\n1 2 3 4 5",
"output": "0"
},
{
"input": "3\n8 12 7",
"output": "3"
},
{
"input": "3\n3 2 1",
"output": "2"
},
{
"input": "5\n2 3 4 5 6",
"output": "4"
},
{
"input": "8\n7 2 6 6 5 1 4 9",
"output": "9"
},
{
"input": "10\n1000000000 100000... | 655 | 22,528,000 | 3 | 3,474 | |
417 | Football | [
"constructive algorithms",
"graphs",
"implementation"
] | null | null | One day, at the "Russian Code Cup" event it was decided to play football as an out of competition event. All participants was divided into *n* teams and played several matches, two teams could not play against each other more than once.
The appointed Judge was the most experienced member β Pavel. But since he was the wisest of all, he soon got bored of the game and fell asleep. Waking up, he discovered that the tournament is over and the teams want to know the results of all the matches.
Pavel didn't want anyone to discover about him sleeping and not keeping an eye on the results, so he decided to recover the results of all games. To do this, he asked all the teams and learned that the real winner was friendship, that is, each team beat the other teams exactly *k* times. Help Pavel come up with chronology of the tournir that meets all the conditions, or otherwise report that there is no such table. | The first line contains two integers β *n* and *k* (1<=β€<=*n*,<=*k*<=β€<=1000). | In the first line print an integer *m* β number of the played games. The following *m* lines should contain the information about all the matches, one match per line. The *i*-th line should contain two integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*; *a**i*<=β <=*b**i*). The numbers *a**i* and *b**i* mean, that in the *i*-th match the team with number *a**i* won against the team with number *b**i*. You can assume, that the teams are numbered from 1 to *n*.
If a tournir that meets the conditions of the problem does not exist, then print -1. | [
"3 1\n"
] | [
"3\n1 2\n2 3\n3 1\n"
] | none | [
{
"input": "3 1",
"output": "3\n1 2\n2 3\n3 1"
},
{
"input": "7 3",
"output": "21\n1 2\n1 3\n1 4\n2 3\n2 4\n2 5\n3 4\n3 5\n3 6\n4 5\n4 6\n4 7\n5 6\n5 7\n5 1\n6 7\n6 1\n6 2\n7 1\n7 2\n7 3"
},
{
"input": "4 1",
"output": "4\n1 2\n2 3\n3 4\n4 1"
},
{
"input": "5 2",
"output"... | 1,000 | 307,200 | 0 | 3,475 | |
427 | Prison Transfer | [
"data structures",
"implementation"
] | null | null | The prison of your city has *n* prisoners. As the prison can't accommodate all of them, the city mayor has decided to transfer *c* of the prisoners to a prison located in another city.
For this reason, he made the *n* prisoners to stand in a line, with a number written on their chests. The number is the severity of the crime he/she has committed. The greater the number, the more severe his/her crime was.
Then, the mayor told you to choose the *c* prisoners, who will be transferred to the other prison. He also imposed two conditions. They are,
- The chosen *c* prisoners has to form a contiguous segment of prisoners. - Any of the chosen prisoner's crime level should not be greater then *t*. Because, that will make the prisoner a severe criminal and the mayor doesn't want to take the risk of his running away during the transfer.
Find the number of ways you can choose the *c* prisoners. | The first line of input will contain three space separated integers *n*Β (1<=β€<=*n*<=β€<=2Β·105), *t*Β (0<=β€<=*t*<=β€<=109) and *c*Β (1<=β€<=*c*<=β€<=*n*). The next line will contain *n* space separated integers, the *i**th* integer is the severity *i**th* prisoner's crime. The value of crime severities will be non-negative and will not exceed 109. | Print a single integer β the number of ways you can choose the *c* prisoners. | [
"4 3 3\n2 3 1 1\n",
"1 1 1\n2\n",
"11 4 2\n2 2 0 7 3 2 2 4 9 1 4\n"
] | [
"2\n",
"0\n",
"6\n"
] | none | [
{
"input": "4 3 3\n2 3 1 1",
"output": "2"
},
{
"input": "1 1 1\n2",
"output": "0"
},
{
"input": "11 4 2\n2 2 0 7 3 2 2 4 9 1 4",
"output": "6"
},
{
"input": "57 2 10\n7 5 2 7 4 1 0 5 2 9 2 9 8 6 6 5 9 6 8 1 0 1 0 3 2 6 5 2 8 8 8 8 0 9 4 3 6 6 2 4 5 1 2 0 1 7 1 1 5 4 5 0 7 5 ... | 0 | 0 | -1 | 3,486 | |
39 | Pacifist frogs | [
"implementation"
] | F. Pacifist frogs | 2 | 64 | Thumbelina has had an accident. She has found herself on a little island in the middle of a swamp and wants to get to the shore very much.
One can get to the shore only by hills that are situated along a straight line that connects the little island with the shore. Let us assume that the hills are numbered from 1 to *n* and the number of a hill is equal to the distance in meters between it and the island. The distance between the *n*-th hill and the shore is also 1 meter.
Thumbelina is too small to make such jumps. Fortunately, a family of frogs living in the swamp suggests to help her. Each frog agrees to give Thumbelina a ride but Thumbelina should choose only one frog. Each frog has a certain jump length. If Thumbelina agrees to accept help from a frog whose jump length is *d*, the frog will jump from the island on the hill *d*, then β on the hill 2*d*, then 3*d* and so on until they get to the shore (i.e. find itself beyond the hill *n*).
However, there is one more problem: mosquitoes also live in the swamp. At the moment they have a siesta, and they are having a nap on some hills. If the frog jumps on a hill with a mosquito the frog will smash it. The frogs Thumbelina has met are pacifists, so they will find the death of each mosquito very much sad. Help Thumbelina choose a frog that will bring her to the shore and smash as small number of mosquitoes as possible. | The first line contains three integers *n*, *m* and *k* (1<=β€<=*n*<=β€<=109, 1<=β€<=*m*,<=*k*<=β€<=100) β the number of hills, frogs and mosquitoes respectively. The second line contains *m* integers *d**i* (1<=β€<=*d**i*<=β€<=109) β the lengths of the frogsβ jumps. The third line contains *k* integers β the numbers of the hills on which each mosquito is sleeping. No more than one mosquito can sleep on each hill. The numbers in the lines are separated by single spaces. | In the first line output the number of frogs that smash the minimal number of mosquitoes, in the second line β their numbers in increasing order separated by spaces. The frogs are numbered from 1 to *m* in the order of the jump length given in the input data. | [
"5 3 5\n2 3 4\n1 2 3 4 5\n",
"1000000000 2 3\n2 5\n999999995 999999998 999999996\n"
] | [
"2\n2 3\n",
"1\n2\n"
] | none | [
{
"input": "5 3 5\n2 3 4\n1 2 3 4 5",
"output": "2\n2 3"
},
{
"input": "1000000000 2 3\n2 5\n999999995 999999998 999999996",
"output": "1\n2"
},
{
"input": "1 1 1\n1\n1",
"output": "1\n1"
},
{
"input": "2 2 1\n2 1\n1",
"output": "1\n1"
},
{
"input": "3 2 2\n2 4\n3... | 154 | 2,150,400 | 3.945478 | 3,488 |
269 | Greenhouse Effect | [
"dp"
] | null | null | Emuskald is an avid horticulturist and owns the world's longest greenhouse β it is effectively infinite in length.
Over the years Emuskald has cultivated *n* plants in his greenhouse, of *m* different plant species numbered from 1 to *m*. His greenhouse is very narrow and can be viewed as an infinite line, with each plant occupying a single point on that line.
Emuskald has discovered that each species thrives at a different temperature, so he wants to arrange *m*<=-<=1 borders that would divide the greenhouse into *m* sections numbered from 1 to *m* from left to right with each section housing a single species. He is free to place the borders, but in the end all of the *i*-th species plants must reside in *i*-th section from the left.
Of course, it is not always possible to place the borders in such way, so Emuskald needs to replant some of his plants. He can remove each plant from its position and place it anywhere in the greenhouse (at any real coordinate) with no plant already in it. Since replanting is a lot of stress for the plants, help Emuskald find the minimum number of plants he has to replant to be able to place the borders. | The first line of input contains two space-separated integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=5000, *n*<=β₯<=*m*), the number of plants and the number of different species. Each of the following *n* lines contain two space-separated numbers: one integer number *s**i* (1<=β€<=*s**i*<=β€<=*m*), and one real number *x**i* (0<=β€<=*x**i*<=β€<=109), the species and position of the *i*-th plant. Each *x**i* will contain no more than 6 digits after the decimal point.
It is guaranteed that all *x**i* are different; there is at least one plant of each species; the plants are given in order "from left to the right", that is in the ascending order of their *x**i* coordinates (*x**i*<=<<=*x**i*<=+<=1,<=1<=β€<=*i*<=<<=*n*). | Output a single integer β the minimum number of plants to be replanted. | [
"3 2\n2 1\n1 2.0\n1 3.100\n",
"3 3\n1 5.0\n2 5.5\n3 6.0\n",
"6 3\n1 14.284235\n2 17.921382\n1 20.328172\n3 20.842331\n1 25.790145\n1 27.204125\n"
] | [
"1\n",
"0\n",
"2\n"
] | In the first test case, Emuskald can replant the first plant to the right of the last plant, so the answer is 1.
In the second test case, the species are already in the correct order, so no replanting is needed. | [
{
"input": "3 2\n2 1\n1 2.0\n1 3.100",
"output": "1"
},
{
"input": "3 3\n1 5.0\n2 5.5\n3 6.0",
"output": "0"
},
{
"input": "6 3\n1 14.284235\n2 17.921382\n1 20.328172\n3 20.842331\n1 25.790145\n1 27.204125",
"output": "2"
},
{
"input": "1 1\n1 0",
"output": "0"
},
{
... | 30 | 0 | -1 | 3,496 | |
346 | Alice and Bob | [
"games",
"math",
"number theory"
] | null | null | It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of *n* distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers *x* and *y* from the set, such that the set doesn't contain their absolute difference |*x*<=-<=*y*|. Then this player adds integer |*x*<=-<=*y*| to the set (so, the size of the set increases by one).
If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first. | The first line contains an integer *n* (2<=β€<=*n*<=β€<=100) β the initial number of elements in the set. The second line contains *n* distinct space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109) β the elements of the set. | Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes). | [
"2\n2 3\n",
"2\n5 3\n",
"3\n5 6 7\n"
] | [
"Alice\n",
"Alice\n",
"Bob\n"
] | Consider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice. | [
{
"input": "2\n2 3",
"output": "Alice"
},
{
"input": "2\n5 3",
"output": "Alice"
},
{
"input": "3\n5 6 7",
"output": "Bob"
},
{
"input": "10\n72 96 24 66 6 18 12 30 60 48",
"output": "Bob"
},
{
"input": "10\n78 66 6 60 18 84 36 96 72 48",
"output": "Bob"
},
... | 186 | 0 | 0 | 3,501 | |
707 | Pythagorean Triples | [
"math",
"number theory"
] | null | null | Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples.
For example, triples (3,<=4,<=5), (5,<=12,<=13) and (6,<=8,<=10) are Pythagorean triples.
Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse.
Katya had no problems with completing this task. Will you do the same? | The only line of the input contains single integer *n* (1<=β€<=*n*<=β€<=109)Β β the length of some side of a right triangle. | Print two integers *m* and *k* (1<=β€<=*m*,<=*k*<=β€<=1018), such that *n*, *m* and *k* form a Pythagorean triple, in the only line.
In case if there is no any Pythagorean triple containing integer *n*, print <=-<=1 in the only line. If there are many answers, print any of them. | [
"3\n",
"6\n",
"1\n",
"17\n",
"67\n"
] | [
"4 5",
"8 10",
"-1",
"144 145",
"2244 2245"
] | Illustration for the first sample. | [
{
"input": "3",
"output": "4 5"
},
{
"input": "6",
"output": "8 10"
},
{
"input": "1",
"output": "-1"
},
{
"input": "17",
"output": "144 145"
},
{
"input": "67",
"output": "2244 2245"
},
{
"input": "10",
"output": "24 26"
},
{
"input": "14"... | 62 | 0 | 3 | 3,502 | |
229 | Planets | [
"binary search",
"data structures",
"graphs",
"shortest paths"
] | null | null | Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet.
Overall the galaxy has *n* planets, indexed with numbers from 1 to *n*. Jack is on the planet with index 1, and Apophis will land on the planet with index *n*. Jack can move between some pairs of planets through stargates (he can move in both directions); the transfer takes a positive, and, perhaps, for different pairs of planets unequal number of seconds. Jack begins his journey at time 0.
It can be that other travellers are arriving to the planet where Jack is currently located. In this case, Jack has to wait for exactly 1 second before he can use the stargate. That is, if at time *t* another traveller arrives to the planet, Jack can only pass through the stargate at time *t*<=+<=1, unless there are more travellers arriving at time *t*<=+<=1 to the same planet.
Knowing the information about travel times between the planets, and the times when Jack would not be able to use the stargate on particular planets, determine the minimum time in which he can get to the planet with index *n*. | The first line contains two space-separated integers: *n* (2<=β€<=*n*<=β€<=105), the number of planets in the galaxy, and *m* (0<=β€<=*m*<=β€<=105) β the number of pairs of planets between which Jack can travel using stargates. Then *m* lines follow, containing three integers each: the *i*-th line contains numbers of planets *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*, *a**i*<=β <=*b**i*), which are connected through stargates, and the integer transfer time (in seconds) *c**i* (1<=β€<=*c**i*<=β€<=104) between these planets. It is guaranteed that between any pair of planets there is at most one stargate connection.
Then *n* lines follow: the *i*-th line contains an integer *k**i* (0<=β€<=*k**i*<=β€<=105) that denotes the number of moments of time when other travellers arrive to the planet with index *i*. Then *k**i* distinct space-separated integers *t**ij* (0<=β€<=*t**ij*<=<<=109) follow, sorted in ascending order. An integer *t**ij* means that at time *t**ij* (in seconds) another traveller arrives to the planet *i*. It is guaranteed that the sum of all *k**i* does not exceed 105. | Print a single number β the least amount of time Jack needs to get from planet 1 to planet *n*. If Jack can't get to planet *n* in any amount of time, print number -1. | [
"4 6\n1 2 2\n1 3 3\n1 4 8\n2 3 4\n2 4 5\n3 4 3\n0\n1 3\n2 3 4\n0\n",
"3 1\n1 2 3\n0\n1 3\n0\n"
] | [
"7\n",
"-1\n"
] | In the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves to planet 2, and then β to planet 4, then he spends a total of only 2β+β5β=β7 seconds.
In the second sample one can't get from planet 1 to planet 3 by moving through stargates. | [
{
"input": "4 6\n1 2 2\n1 3 3\n1 4 8\n2 3 4\n2 4 5\n3 4 3\n0\n1 3\n2 3 4\n0",
"output": "7"
},
{
"input": "3 1\n1 2 3\n0\n1 3\n0",
"output": "-1"
},
{
"input": "2 1\n1 2 3\n0\n1 3",
"output": "3"
},
{
"input": "2 1\n1 2 3\n1 0\n0",
"output": "4"
},
{
"input": "3 3... | 2,000 | 19,968,000 | 0 | 3,507 | |
813 | The Tag Game | [
"dfs and similar",
"graphs"
] | null | null | Alice got tired of playing the tag game by the usual rules so she offered Bob a little modification to it. Now the game should be played on an undirected rooted tree of *n* vertices. Vertex 1 is the root of the tree.
Alice starts at vertex 1 and Bob starts at vertex *x* (*x*<=β <=1). The moves are made in turns, Bob goes first. In one move one can either stay at the current vertex or travel to the neighbouring one.
The game ends when Alice goes to the same vertex where Bob is standing. Alice wants to minimize the total number of moves and Bob wants to maximize it.
You should write a program which will determine how many moves will the game last. | The first line contains two integer numbers *n* and *x* (2<=β€<=*n*<=β€<=2Β·105, 2<=β€<=*x*<=β€<=*n*).
Each of the next *n*<=-<=1 lines contains two integer numbers *a* and *b* (1<=β€<=*a*,<=*b*<=β€<=*n*) β edges of the tree. It is guaranteed that the edges form a valid tree. | Print the total number of moves Alice and Bob will make. | [
"4 3\n1 2\n2 3\n2 4\n",
"5 2\n1 2\n2 3\n3 4\n2 5\n"
] | [
"4\n",
"6\n"
] | In the first example the tree looks like this:
The red vertex is Alice's starting position, the blue one is Bob's. Bob will make the game run the longest by standing at the vertex 3 during all the game. So here are the moves:
B: stay at vertex 3
A: go to vertex 2
B: stay at vertex 3
A: go to vertex 3
In the second example the tree looks like this:
The moves in the optimal strategy are:
B: go to vertex 3
A: go to vertex 2
B: go to vertex 4
A: go to vertex 3
B: stay at vertex 4
A: go to vertex 4 | [
{
"input": "4 3\n1 2\n2 3\n2 4",
"output": "4"
},
{
"input": "5 2\n1 2\n2 3\n3 4\n2 5",
"output": "6"
},
{
"input": "2 2\n2 1",
"output": "2"
},
{
"input": "3 3\n2 1\n3 1",
"output": "2"
},
{
"input": "3 3\n1 2\n3 2",
"output": "4"
},
{
"input": "10 4\... | 62 | 0 | 0 | 3,512 | |
490 | Queue | [
"dsu",
"implementation"
] | null | null | During the lunch break all *n* Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.
Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).
After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.
Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue. | The first line contains integer *n* (2<=β€<=*n*<=β€<=2Β·105) β the number of students in the queue.
Then *n* lines follow, *i*-th line contains the pair of integers *a**i*,<=*b**i* (0<=β€<=*a**i*,<=*b**i*<=β€<=106), where *a**i* is the ID number of a person in front of a student and *b**i* is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist.
The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order. | Print a sequence of *n* integers *x*1,<=*x*2,<=...,<=*x**n* β the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one. | [
"4\n92 31\n0 7\n31 0\n7 141\n"
] | [
"92 7 31 141 \n"
] | The picture illustrates the queue for the first sample. | [
{
"input": "4\n92 31\n0 7\n31 0\n7 141",
"output": "92 7 31 141 "
},
{
"input": "2\n0 1\n2 0",
"output": "2 1 "
},
{
"input": "3\n0 2\n1 3\n2 0",
"output": "1 2 3 "
},
{
"input": "4\n101 0\n0 102\n102 100\n103 101",
"output": "103 102 101 100 "
},
{
"input": "5\n0... | 218 | 10,444,800 | -1 | 3,513 | |
49 | Disposition | [
"constructive algorithms",
"math"
] | C. Disposition | 2 | 256 | Vasya bought the collected works of a well-known Berland poet Petya in *n* volumes. The volumes are numbered from 1 to *n*. He thinks that it does not do to arrange the book simply according to their order. Vasya wants to minimize the number of the dispositionβs divisors β the positive integers *i* such that for at least one *j* (1<=β€<=*j*<=β€<=*n*) is true both: *j* *mod* *i*<==<=0 and at the same time *p*(*j*) *mod* *i*<==<=0, where *p*(*j*) is the number of the tome that stands on the *j*-th place and *mod* is the operation of taking the division remainder. Naturally, one volume can occupy exactly one place and in one place can stand exactly one volume.
Help Vasya β find the volume disposition with the minimum number of divisors. | The first line contains number *n* (1<=β€<=*n*<=β€<=100000) which represents the number of volumes and free places. | Print *n* numbers β the sought disposition with the minimum divisor number. The *j*-th number (1<=β€<=*j*<=β€<=*n*) should be equal to *p*(*j*) β the number of tome that stands on the *j*-th place. If there are several solutions, print any of them. | [
"2\n",
"3\n"
] | [
"2 1 \n",
"1 3 2 \n"
] | none | [
{
"input": "2",
"output": "2 1 "
},
{
"input": "3",
"output": "1 3 2 "
},
{
"input": "4",
"output": "2 1 4 3 "
},
{
"input": "5",
"output": "1 3 2 5 4 "
},
{
"input": "6",
"output": "2 1 4 3 6 5 "
},
{
"input": "1",
"output": "1 "
},
{
"inp... | 218 | 12,288,000 | 3.922612 | 3,518 |
351 | Jeff and Brackets | [
"dp",
"matrices"
] | null | null | Jeff loves regular bracket sequences.
Today Jeff is going to take a piece of paper and write out the regular bracket sequence, consisting of *nm* brackets. Let's number all brackets of this sequence from 0 to *nm* - 1 from left to right. Jeff knows that he is going to spend *a**i* *mod* *n* liters of ink on the *i*-th bracket of the sequence if he paints it opened and *b**i* *mod* *n* liters if he paints it closed.
You've got sequences *a*, *b* and numbers *n*, *m*. What minimum amount of ink will Jeff need to paint a regular bracket sequence of length *nm*?
Operation *x* *mod* *y* means taking the remainder after dividing number *x* by number *y*. | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=20;Β 1<=β€<=*m*<=β€<=107; *m* is even). The next line contains *n* integers: *a*0, *a*1, ..., *a**n*<=-<=1 (1<=β€<=*a**i*<=β€<=10). The next line contains *n* integers: *b*0, *b*1, ..., *b**n*<=-<=1 (1<=β€<=*b**i*<=β€<=10). The numbers are separated by spaces. | In a single line print the answer to the problem β the minimum required amount of ink in liters. | [
"2 6\n1 2\n2 1\n",
"1 10000000\n2\n3\n"
] | [
"12\n",
"25000000\n"
] | In the first test the optimal sequence is: ()()()()()(), the required number of ink liters is 12. | [
{
"input": "2 6\n1 2\n2 1",
"output": "12"
},
{
"input": "1 10000000\n2\n3",
"output": "25000000"
},
{
"input": "3 184\n3 2 8\n3 9 2",
"output": "1288"
},
{
"input": "4 26\n10 2 5 9\n5 4 2 5",
"output": "444"
},
{
"input": "3 76\n4 7 9\n10 1 1",
"output": "684... | 62 | 0 | 0 | 3,521 | |
23 | Party | [
"constructive algorithms",
"graphs",
"math"
] | B. Party | 2 | 256 | *n* people came to a party. Then those, who had no friends among people at the party, left. Then those, who had exactly 1 friend among those who stayed, left as well. Then those, who had exactly 2,<=3,<=...,<=*n*<=-<=1 friends among those who stayed by the moment of their leaving, did the same.
What is the maximum amount of people that could stay at the party in the end? | The first input line contains one number *t* β amount of tests (1<=β€<=*t*<=β€<=105). Each of the following *t* lines contains one integer number *n* (1<=β€<=*n*<=β€<=105). | For each test output in a separate line one number β the maximum amount of people that could stay in the end. | [
"1\n3\n"
] | [
"1\n"
] | none | [
{
"input": "1\n3",
"output": "1"
}
] | 1,496 | 0 | 3.626 | 3,522 |
300 | Beautiful Numbers | [
"brute force",
"combinatorics"
] | null | null | Vitaly is a very weird man. He's got two favorite digits *a* and *b*. Vitaly calls a positive integer good, if the decimal representation of this integer only contains digits *a* and *b*. Vitaly calls a good number excellent, if the sum of its digits is a good number.
For example, let's say that Vitaly's favourite digits are 1 and 3, then number 12 isn't good and numbers 13 or 311 are. Also, number 111 is excellent and number 11 isn't.
Now Vitaly is wondering, how many excellent numbers of length exactly *n* are there. As this number can be rather large, he asks you to count the remainder after dividing it by 1000000007 (109<=+<=7).
A number's length is the number of digits in its decimal representation without leading zeroes. | The first line contains three integers: *a*, *b*, *n* (1<=β€<=*a*<=<<=*b*<=β€<=9,<=1<=β€<=*n*<=β€<=106). | Print a single integer β the answer to the problem modulo 1000000007 (109<=+<=7). | [
"1 3 3\n",
"2 3 10\n"
] | [
"1\n",
"165\n"
] | none | [
{
"input": "1 3 3",
"output": "1"
},
{
"input": "2 3 10",
"output": "165"
},
{
"input": "6 8 14215",
"output": "651581472"
},
{
"input": "4 9 104671",
"output": "329390901"
},
{
"input": "6 7 78755",
"output": "0"
},
{
"input": "1 8 265",
"output":... | 810 | 62,054,400 | 3 | 3,525 | |
893 | Chess For Three | [
"implementation"
] | null | null | Alex, Bob and Carl will soon participate in a team chess tournament. Since they are all in the same team, they have decided to practise really hard before the tournament. But it's a bit difficult for them because chess is a game for two players, not three.
So they play with each other according to following rules:
- Alex and Bob play the first game, and Carl is spectating; - When the game ends, the one who lost the game becomes the spectator in the next game, and the one who was spectating plays against the winner.
Alex, Bob and Carl play in such a way that there are no draws.
Today they have played *n* games, and for each of these games they remember who was the winner. They decided to make up a log of games describing who won each game. But now they doubt if the information in the log is correct, and they want to know if the situation described in the log they made up was possible (that is, no game is won by someone who is spectating if Alex, Bob and Carl play according to the rules). Help them to check it! | The first line contains one integer *n* (1<=β€<=*n*<=β€<=100) β the number of games Alex, Bob and Carl played.
Then *n* lines follow, describing the game log. *i*-th line contains one integer *a**i* (1<=β€<=*a**i*<=β€<=3) which is equal to 1 if Alex won *i*-th game, to 2 if Bob won *i*-th game and 3 if Carl won *i*-th game. | Print YES if the situation described in the log was possible. Otherwise print NO. | [
"3\n1\n1\n2\n",
"2\n1\n2\n"
] | [
"YES\n",
"NO\n"
] | In the first example the possible situation is:
1. Alex wins, Carl starts playing instead of Bob; 1. Alex wins, Bob replaces Carl; 1. Bob wins.
The situation in the second example is impossible because Bob loses the first game, so he cannot win the second one. | [
{
"input": "3\n1\n1\n2",
"output": "YES"
},
{
"input": "2\n1\n2",
"output": "NO"
},
{
"input": "100\n2\n3\n1\n2\n3\n3\n3\n1\n1\n1\n1\n3\n3\n3\n3\n1\n2\n3\n3\n3\n3\n3\n3\n3\n1\n2\n2\n2\n3\n1\n1\n3\n3\n3\n3\n3\n3\n3\n3\n1\n2\n3\n3\n3\n1\n1\n1\n1\n3\n3\n3\n3\n1\n2\n3\n1\n2\n2\n2\n3\n3\n2\n1... | 46 | 0 | 3 | 3,533 | |
52 | 123-sequence | [
"implementation"
] | A. 123-sequence | 2 | 256 | There is a given sequence of integers *a*1,<=*a*2,<=...,<=*a**n*, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=106). The second line contains a sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=3). | Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal. | [
"9\n1 3 2 2 2 1 1 2 3\n"
] | [
"5\n"
] | In the example all the numbers equal to 1 and 3 should be replaced by 2. | [
{
"input": "9\n1 3 2 2 2 1 1 2 3",
"output": "5"
},
{
"input": "6\n3 3 2 2 1 3",
"output": "3"
},
{
"input": "12\n3 1 3 1 2 1 3 2 2 1 2 1",
"output": "7"
},
{
"input": "15\n3 2 1 1 1 1 3 2 2 3 3 1 2 3 2",
"output": "10"
},
{
"input": "2\n2 1",
"output": "1"
... | 498 | 13,619,200 | 3.850132 | 3,549 |
446 | DZY Loves Sequences | [
"dp",
"implementation",
"two pointers"
] | null | null | DZY has a sequence *a*, consisting of *n* integers.
We'll call a sequence *a**i*,<=*a**i*<=+<=1,<=...,<=*a**j* (1<=β€<=*i*<=β€<=*j*<=β€<=*n*) a subsegment of the sequence *a*. The value (*j*<=-<=*i*<=+<=1) denotes the length of the subsegment.
Your task is to find the longest subsegment of *a*, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.
You only need to output the length of the subsegment you find. | The first line contains integer *n*Β (1<=β€<=*n*<=β€<=105). The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n*Β (1<=β€<=*a**i*<=β€<=109). | In a single line print the answer to the problem β the maximum length of the required subsegment. | [
"6\n7 2 3 1 5 6\n"
] | [
"5\n"
] | You can choose subsegment *a*<sub class="lower-index">2</sub>,β*a*<sub class="lower-index">3</sub>,β*a*<sub class="lower-index">4</sub>,β*a*<sub class="lower-index">5</sub>,β*a*<sub class="lower-index">6</sub> and change its 3rd element (that is *a*<sub class="lower-index">4</sub>) to 4. | [
{
"input": "6\n7 2 3 1 5 6",
"output": "5"
},
{
"input": "10\n424238336 649760493 681692778 714636916 719885387 804289384 846930887 957747794 596516650 189641422",
"output": "9"
},
{
"input": "50\n804289384 846930887 681692778 714636916 957747794 424238336 719885387 649760493 596516650 1... | 108 | 0 | 0 | 3,552 | |
710 | King Moves | [
"implementation"
] | null | null | The only king stands on the standard chess board. You are given his position in format "cd", where *c* is the column from 'a' to 'h' and *d* is the row from '1' to '8'. Find the number of moves permitted for the king.
Check the king's moves here [https://en.wikipedia.org/wiki/King_(chess)](https://en.wikipedia.org/wiki/King_(chess)). | The only line contains the king's position in the format "cd", where 'c' is the column from 'a' to 'h' and 'd' is the row from '1' to '8'. | Print the only integer *x* β the number of moves permitted for the king. | [
"e4\n"
] | [
"8\n"
] | none | [
{
"input": "e4",
"output": "8"
},
{
"input": "a1",
"output": "3"
},
{
"input": "h8",
"output": "3"
},
{
"input": "a4",
"output": "5"
},
{
"input": "g7",
"output": "8"
},
{
"input": "e1",
"output": "5"
},
{
"input": "b2",
"output": "8"
... | 46 | 0 | 3 | 3,555 | |
448 | Suffix Structures | [
"implementation",
"strings"
] | null | null | Bizon the Champion isn't just a bison. He also is a favorite of the "Bizons" team.
At a competition the "Bizons" got the following problem: "You are given two distinct words (strings of English letters), *s* and *t*. You need to transform word *s* into word *t*". The task looked simple to the guys because they know the suffix data structures well. Bizon Senior loves suffix automaton. By applying it once to a string, he can remove from this string any single character. Bizon Middle knows suffix array well. By applying it once to a string, he can swap any two characters of this string. The guys do not know anything about the suffix tree, but it can help them do much more.
Bizon the Champion wonders whether the "Bizons" can solve the problem. Perhaps, the solution do not require both data structures. Find out whether the guys can solve the problem and if they can, how do they do it? Can they solve it either only with use of suffix automaton or only with use of suffix array or they need both structures? Note that any structure may be used an unlimited number of times, the structures may be used in any order. | The first line contains a non-empty word *s*. The second line contains a non-empty word *t*. Words *s* and *t* are different. Each word consists only of lowercase English letters. Each word contains at most 100 letters. | In the single line print the answer to the problem. Print "need tree" (without the quotes) if word *s* cannot be transformed into word *t* even with use of both suffix array and suffix automaton. Print "automaton" (without the quotes) if you need only the suffix automaton to solve the problem. Print "array" (without the quotes) if you need only the suffix array to solve the problem. Print "both" (without the quotes), if you need both data structures to solve the problem.
It's guaranteed that if you can solve the problem only with use of suffix array, then it is impossible to solve it only with use of suffix automaton. This is also true for suffix automaton. | [
"automaton\ntomat\n",
"array\narary\n",
"both\nhot\n",
"need\ntree\n"
] | [
"automaton\n",
"array\n",
"both\n",
"need tree\n"
] | In the third sample you can act like that: first transform "both" into "oth" by removing the first character using the suffix automaton and then make two swaps of the string using the suffix array and get "hot". | [
{
"input": "automaton\ntomat",
"output": "automaton"
},
{
"input": "array\narary",
"output": "array"
},
{
"input": "both\nhot",
"output": "both"
},
{
"input": "need\ntree",
"output": "need tree"
},
{
"input": "abacaba\naaaa",
"output": "automaton"
},
{
... | 93 | 0 | 0 | 3,556 | |
691 | Exponential notation | [
"implementation",
"strings"
] | null | null | You are given a positive decimal number *x*.
Your task is to convert it to the "simple exponential notation".
Let *x*<==<=*a*Β·10*b*, where 1<=β€<=*a*<=<<=10, then in general case the "simple exponential notation" looks like "aEb". If *b* equals to zero, the part "Eb" should be skipped. If *a* is an integer, it should be written without decimal point. Also there should not be extra zeroes in *a* and *b*. | The only line contains the positive decimal number *x*. The length of the line will not exceed 106. Note that you are given too large number, so you can't use standard built-in data types "float", "double" and other. | Print the only line β the "simple exponential notation" of the given number *x*. | [
"16\n",
"01.23400\n",
".100\n",
"100.\n"
] | [
"1.6E1\n",
"1.234\n",
"1E-1\n",
"1E2\n"
] | none | [
{
"input": "16",
"output": "1.6E1"
},
{
"input": "01.23400",
"output": "1.234"
},
{
"input": ".100",
"output": "1E-1"
},
{
"input": "100.",
"output": "1E2"
},
{
"input": "9000",
"output": "9E3"
},
{
"input": "0.0012",
"output": "1.2E-3"
},
{
... | 62 | 9,011,200 | 3 | 3,557 | |
0 | none | [
"none"
] | 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... | 77 | 2,867,200 | -1 | 3,560 | |
757 | Bash's Big Day | [
"greedy",
"math",
"number theory"
] | null | null | Bash has set out on a journey to become the greatest Pokemon master. To get his first Pokemon, he went to Professor Zulu's Lab. Since Bash is Professor Zulu's favourite student, Zulu allows him to take as many Pokemon from his lab as he pleases.
But Zulu warns him that a group of *k*<=><=1 Pokemon with strengths {*s*1,<=*s*2,<=*s*3,<=...,<=*s**k*} tend to fight among each other if *gcd*(*s*1,<=*s*2,<=*s*3,<=...,<=*s**k*)<==<=1 (see notes for *gcd* definition).
Bash, being smart, does not want his Pokemon to fight among each other. However, he also wants to maximize the number of Pokemon he takes from the lab. Can you help Bash find out the maximum number of Pokemon he can take?
Note: A Pokemon cannot fight with itself. | The input consists of two lines.
The first line contains an integer *n* (1<=β€<=*n*<=β€<=105), the number of Pokemon in the lab.
The next line contains *n* space separated integers, where the *i*-th of them denotes *s**i* (1<=β€<=*s**i*<=β€<=105), the strength of the *i*-th Pokemon. | Print single integerΒ β the maximum number of Pokemons Bash can take. | [
"3\n2 3 4\n",
"5\n2 3 4 6 7\n"
] | [
"2\n",
"3\n"
] | *gcd* (greatest common divisor) of positive integers set {*a*<sub class="lower-index">1</sub>,β*a*<sub class="lower-index">2</sub>,β...,β*a*<sub class="lower-index">*n*</sub>} is the maximum positive integer that divides all the integers {*a*<sub class="lower-index">1</sub>,β*a*<sub class="lower-index">2</sub>,β...,β*a*<sub class="lower-index">*n*</sub>}.
In the first sample, we can take Pokemons with strengths {2,β4} since *gcd*(2,β4)β=β2.
In the second sample, we can take Pokemons with strengths {2,β4,β6}, and there is no larger group with *gcd*ββ β1. | [
{
"input": "3\n2 3 4",
"output": "2"
},
{
"input": "5\n2 3 4 6 7",
"output": "3"
},
{
"input": "3\n5 6 4",
"output": "2"
},
{
"input": "8\n41 74 4 27 85 39 100 36",
"output": "4"
},
{
"input": "6\n89 20 86 81 62 23",
"output": "3"
},
{
"input": "71\n23... | 1,419 | 13,209,600 | 3 | 3,564 | |
452 | Eevee | [
"brute force",
"implementation",
"strings"
] | null | null | You are solving the crossword problem K from IPSC 2014. You solved all the clues except for one: who does Eevee evolve into? You are not very into pokemons, but quick googling helped you find out, that Eevee can evolve into eight different pokemons: Vaporeon, Jolteon, Flareon, Espeon, Umbreon, Leafeon, Glaceon, and Sylveon.
You know the length of the word in the crossword, and you already know some letters. Designers of the crossword made sure that the answer is unambiguous, so you can assume that exactly one pokemon out of the 8 that Eevee evolves into fits the length and the letters given. Your task is to find it. | First line contains an integer *n* (6<=β€<=*n*<=β€<=8) β the length of the string.
Next line contains a string consisting of *n* characters, each of which is either a lower case english letter (indicating a known letter) or a dot character (indicating an empty cell in the crossword). | Print a name of the pokemon that Eevee can evolve into that matches the pattern in the input. Use lower case letters only to print the name (in particular, do not capitalize the first letter). | [
"7\nj......\n",
"7\n...feon\n",
"7\n.l.r.o.\n"
] | [
"jolteon\n",
"leafeon\n",
"flareon\n"
] | Here's a set of names in a form you can paste into your solution:
["vaporeon", "jolteon", "flareon", "espeon", "umbreon", "leafeon", "glaceon", "sylveon"]
{"vaporeon", "jolteon", "flareon", "espeon", "umbreon", "leafeon", "glaceon", "sylveon"} | [
{
"input": "7\n...feon",
"output": "leafeon"
},
{
"input": "7\n.l.r.o.",
"output": "flareon"
},
{
"input": "6\n.s..o.",
"output": "espeon"
},
{
"input": "7\nglaceon",
"output": "glaceon"
},
{
"input": "8\n.a.o.e.n",
"output": "vaporeon"
},
{
"input": "... | 46 | 0 | 0 | 3,575 | |
625 | K-special Tables | [
"constructive algorithms",
"implementation"
] | null | null | People do many crazy things to stand out in a crowd. Some of them dance, some learn by heart rules of Russian language, some try to become an outstanding competitive programmers, while others collect funny math objects.
Alis is among these collectors. Right now she wants to get one of *k*-special tables. In case you forget, the table *n*<=Γ<=*n* is called *k*-special if the following three conditions are satisfied:
- every integer from 1 to *n*2 appears in the table exactly once; - in each row numbers are situated in increasing order; - the sum of numbers in the *k*-th column is maximum possible.
Your goal is to help Alice and find at least one *k*-special table of size *n*<=Γ<=*n*. Both rows and columns are numbered from 1 to *n*, with rows numbered from top to bottom and columns numbered from left to right. | The first line of the input contains two integers *n* and *k* (1<=β€<=*n*<=β€<=500,<=1<=β€<=*k*<=β€<=*n*)Β β the size of the table Alice is looking for and the column that should have maximum possible sum. | First print the sum of the integers in the *k*-th column of the required table.
Next *n* lines should contain the description of the table itself: first line should contains *n* elements of the first row, second line should contain *n* elements of the second row and so on.
If there are multiple suitable table, you are allowed to print any. | [
"4 1\n",
"5 3\n"
] | [
"28\n1 2 3 4\n5 6 7 8\n9 10 11 12\n13 14 15 16\n",
"85\n5 6 17 18 19\n9 10 23 24 25\n7 8 20 21 22\n3 4 14 15 16\n1 2 11 12 13\n\n"
] | none | [
{
"input": "4 1",
"output": "28\n1 2 3 4\n5 6 7 8\n9 10 11 12\n13 14 15 16"
},
{
"input": "5 3",
"output": "85\n1 2 11 12 13\n3 4 14 15 16\n5 6 17 18 19\n7 8 20 21 22\n9 10 23 24 25"
},
{
"input": "1 1",
"output": "1\n1"
},
{
"input": "2 1",
"output": "4\n1 2\n3 4"
},
... | 358 | 7,680,000 | 3 | 3,581 | |
171 | A Piece of Cake | [
"*special",
"implementation"
] | null | null | How to make a cake you'll never eat.
Ingredients.
- 2 carrots - 0 calories - 100 g chocolate spread - 1 pack of flour - 1 egg
Method.
1. Put calories into the mixing bowl. 1. Take carrots from refrigerator. 1. Chop carrots. 1. Take chocolate spread from refrigerator. 1. Put chocolate spread into the mixing bowl. 1. Combine pack of flour into the mixing bowl. 1. Fold chocolate spread into the mixing bowl. 1. Add chocolate spread into the mixing bowl. 1. Put pack of flour into the mixing bowl. 1. Add egg into the mixing bowl. 1. Fold pack of flour into the mixing bowl. 1. Chop carrots until choped. 1. Pour contents of the mixing bowl into the baking dish.
Serves 1. | The only line of input contains a sequence of integers *a*0,<=*a*1,<=... (1<=β€<=*a*0<=β€<=100, 0<=β€<=*a**i*<=β€<=1000 for *i*<=β₯<=1). | Output a single integer. | [
"4 1 2 3 4\n"
] | [
"30\n"
] | none | [
{
"input": "4 1 2 3 4",
"output": "30"
},
{
"input": "4 802 765 992 1",
"output": "5312"
},
{
"input": "4 220 380 729 969",
"output": "7043"
},
{
"input": "3 887 104 641",
"output": "3018"
},
{
"input": "12 378 724 582 387 583 241 294 159 198 653 369 418",
"ou... | 62 | 0 | 3 | 3,589 | |
1,010 | Fly | [
"binary search",
"math"
] | null | null | Natasha is going to fly on a rocket to Mars and return to Earth. Also, on the way to Mars, she will land on $n - 2$ intermediate planets. Formally: we number all the planets from $1$ to $n$. $1$ is Earth, $n$ is Mars. Natasha will make exactly $n$ flights: $1 \to 2 \to \ldots n \to 1$.
Flight from $x$ to $y$ consists of two phases: take-off from planet $x$ and landing to planet $y$. This way, the overall itinerary of the trip will be: the $1$-st planet $\to$ take-off from the $1$-st planet $\to$ landing to the $2$-nd planet $\to$ $2$-nd planet $\to$ take-off from the $2$-nd planet $\to$ $\ldots$ $\to$ landing to the $n$-th planet $\to$ the $n$-th planet $\to$ take-off from the $n$-th planet $\to$ landing to the $1$-st planet $\to$ the $1$-st planet.
The mass of the rocket together with all the useful cargo (but without fuel) is $m$ tons. However, Natasha does not know how much fuel to load into the rocket. Unfortunately, fuel can only be loaded on Earth, so if the rocket runs out of fuel on some other planet, Natasha will not be able to return home. Fuel is needed to take-off from each planet and to land to each planet. It is known that $1$ ton of fuel can lift off $a_i$ tons of rocket from the $i$-th planet or to land $b_i$ tons of rocket onto the $i$-th planet.
For example, if the weight of rocket is $9$ tons, weight of fuel is $3$ tons and take-off coefficient is $8$ ($a_i = 8$), then $1.5$ tons of fuel will be burnt (since $1.5 \cdot 8 = 9 + 3$). The new weight of fuel after take-off will be $1.5$ tons.
Please note, that it is allowed to burn non-integral amount of fuel during take-off or landing, and the amount of initial fuel can be non-integral as well.
Help Natasha to calculate the minimum mass of fuel to load into the rocket. Note, that the rocket must spend fuel to carry both useful cargo and the fuel itself. However, it doesn't need to carry the fuel which has already been burnt. Assume, that the rocket takes off and lands instantly. | The first line contains a single integer $n$ ($2 \le n \le 1000$)Β β number of planets.
The second line contains the only integer $m$ ($1 \le m \le 1000$)Β β weight of the payload.
The third line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 1000$), where $a_i$ is the number of tons, which can be lifted off by one ton of fuel.
The fourth line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 1000$), where $b_i$ is the number of tons, which can be landed by one ton of fuel.
It is guaranteed, that if Natasha can make a flight, then it takes no more than $10^9$ tons of fuel. | If Natasha can fly to Mars through $(n - 2)$ planets and return to Earth, print the minimum mass of fuel (in tons) that Natasha should take. Otherwise, print a single number $-1$.
It is guaranteed, that if Natasha can make a flight, then it takes no more than $10^9$ tons of fuel.
The answer will be considered correct if its absolute or relative error doesn't exceed $10^{-6}$. Formally, let your answer be $p$, and the jury's answer be $q$. Your answer is considered correct if $\frac{|p - q|}{\max{(1, |q|)}} \le 10^{-6}$. | [
"2\n12\n11 8\n7 5\n",
"3\n1\n1 4 1\n2 5 3\n",
"6\n2\n4 6 3 3 5 6\n2 6 3 6 5 3\n"
] | [
"10.0000000000\n",
"-1\n",
"85.4800000000\n"
] | Let's consider the first example.
Initially, the mass of a rocket with fuel is $22$ tons.
- At take-off from Earth one ton of fuel can lift off $11$ tons of cargo, so to lift off $22$ tons you need to burn $2$ tons of fuel. Remaining weight of the rocket with fuel is $20$ tons.- During landing on Mars, one ton of fuel can land $5$ tons of cargo, so for landing $20$ tons you will need to burn $4$ tons of fuel. There will be $16$ tons of the rocket with fuel remaining.- While taking off from Mars, one ton of fuel can raise $8$ tons of cargo, so to lift off $16$ tons you will need to burn $2$ tons of fuel. There will be $14$ tons of rocket with fuel after that.- During landing on Earth, one ton of fuel can land $7$ tons of cargo, so for landing $14$ tons you will need to burn $2$ tons of fuel. Remaining weight is $12$ tons, that is, a rocket without any fuel.
In the second case, the rocket will not be able even to take off from Earth. | [
{
"input": "2\n12\n11 8\n7 5",
"output": "10.0000000000"
},
{
"input": "3\n1\n1 4 1\n2 5 3",
"output": "-1"
},
{
"input": "6\n2\n4 6 3 3 5 6\n2 6 3 6 5 3",
"output": "85.4800000000"
},
{
"input": "3\n3\n1 2 1\n2 2 2",
"output": "-1"
},
{
"input": "4\n4\n2 3 2 2\n2... | 77 | 1,843,200 | 3 | 3,615 | |
698 | LRU | [
"bitmasks",
"dp",
"math",
"probabilities"
] | null | null | While creating high loaded systems one should pay a special attention to caching. This problem will be about one of the most popular caching algorithms called LRU (Least Recently Used).
Suppose the cache may store no more than *k* objects. At the beginning of the workflow the cache is empty. When some object is queried we check if it is present in the cache and move it here if it's not. If there are more than *k* objects in the cache after this, the least recently used one should be removed. In other words, we remove the object that has the smallest time of the last query.
Consider there are *n* videos being stored on the server, all of the same size. Cache can store no more than *k* videos and caching algorithm described above is applied. We know that any time a user enters the server he pick the video *i* with probability *p**i*. The choice of the video is independent to any events before.
The goal of this problem is to count for each of the videos the probability it will be present in the cache after 10100 queries. | The first line of the input contains two integers *n* and *k* (1<=β€<=*k*<=β€<=*n*<=β€<=20)Β β the number of videos and the size of the cache respectively. Next line contains *n* real numbers *p**i* (0<=β€<=*p**i*<=β€<=1), each of them is given with no more than two digits after decimal point.
It's guaranteed that the sum of all *p**i* is equal to 1. | Print *n* real numbers, the *i*-th of them should be equal to the probability that the *i*-th video will be present in the cache after 10100 queries. 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 1\n0.3 0.2 0.5\n",
"2 1\n0.0 1.0\n",
"3 2\n0.3 0.2 0.5\n",
"3 3\n0.2 0.3 0.5\n"
] | [
"0.3 0.2 0.5 ",
"0.0 1.0 ",
"0.675 0.4857142857142857 0.8392857142857143 ",
"1.0 1.0 1.0 "
] | none | [
{
"input": "3 1\n0.3 0.2 0.5",
"output": "0.3 0.2 0.5 "
},
{
"input": "2 1\n0.0 1.0",
"output": "0.0 1.0 "
},
{
"input": "3 2\n0.3 0.2 0.5",
"output": "0.675 0.4857142857142857 0.8392857142857143 "
},
{
"input": "3 3\n0.2 0.3 0.5",
"output": "1.0 1.0 1.0 "
},
{
"i... | 77 | 2,867,200 | -1 | 3,627 | |
296 | Yaroslav and Two Strings | [
"combinatorics",
"dp"
] | null | null | Yaroslav thinks that two strings *s* and *w*, consisting of digits and having length *n* are non-comparable if there are two numbers, *i* and *j* (1<=β€<=*i*,<=*j*<=β€<=*n*), such that *s**i*<=><=*w**i* and *s**j*<=<<=*w**j*. Here sign *s**i* represents the *i*-th digit of string *s*, similarly, *w**j* represents the *j*-th digit of string *w*.
A string's template is a string that consists of digits and question marks ("?").
Yaroslav has two string templates, each of them has length *n*. Yaroslav wants to count the number of ways to replace all question marks by some integers in both templates, so as to make the resulting strings incomparable. Note that the obtained strings can contain leading zeroes and that distinct question marks can be replaced by distinct or the same integers.
Help Yaroslav, calculate the remainder after dividing the described number of ways by 1000000007 (109<=+<=7). | The first line contains integer *n* (1<=β€<=*n*<=β€<=105) β the length of both templates. The second line contains the first template β a string that consists of digits and characters "?". The string's length equals *n*. The third line contains the second template in the same format. | In a single line print the remainder after dividing the answer to the problem by number 1000000007 (109<=+<=7). | [
"2\n90\n09\n",
"2\n11\n55\n",
"5\n?????\n?????\n"
] | [
"1\n",
"0\n",
"993531194\n"
] | The first test contains no question marks and both strings are incomparable, so the answer is 1.
The second test has no question marks, but the given strings are comparable, so the answer is 0. | [
{
"input": "2\n90\n09",
"output": "1"
},
{
"input": "2\n11\n55",
"output": "0"
},
{
"input": "5\n?????\n?????",
"output": "993531194"
},
{
"input": "10\n104?3?1??3\n?1755?1??7",
"output": "91015750"
},
{
"input": "10\n6276405116\n6787?352?9",
"output": "46"
... | 92 | 5,529,600 | 0 | 3,629 | |
681 | Heap Operations | [
"constructive algorithms",
"data structures",
"greedy"
] | null | null | Petya has recently learned data structure named "Binary heap".
The heap he is now operating with allows the following operations:
- put the given number into the heap; - get the value of the minimum element in the heap; - extract the minimum element from the heap;
Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.
In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format:
- insert *x*Β β put the element with value *x* in the heap; - getMin *x*Β β the value of the minimum element contained in the heap was equal to *x*; - removeMinΒ β the minimum element was extracted from the heap (only one instance, if there were many).
All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.
While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats.
Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.
Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log. | The first line of the input contains the only integer *n* (1<=β€<=*n*<=β€<=100<=000)Β β the number of the records left in Petya's journal.
Each of the following *n* lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 109 by their absolute value. | The first line of the output should contain a single integer *m*Β β the minimum possible number of records in the modified sequence of operations.
Next *m* lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 109 by their absolute value.
Note that the input sequence of operations must be the subsequence of the output sequence.
It's guaranteed that there exists the correct answer consisting of no more than 1<=000<=000 operations. | [
"2\ninsert 3\ngetMin 4\n",
"4\ninsert 1\ninsert 1\nremoveMin\ngetMin 2\n"
] | [
"4\ninsert 3\nremoveMin\ninsert 4\ngetMin 4\n",
"6\ninsert 1\ninsert 1\nremoveMin\nremoveMin\ninsert 2\ngetMin 2\n"
] | In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap.
In the second sample case number 1 is inserted two times, so should be similarly removed twice. | [
{
"input": "2\ninsert 3\ngetMin 4",
"output": "4\ninsert 3\nremoveMin\ninsert 4\ngetMin 4"
},
{
"input": "4\ninsert 1\ninsert 1\nremoveMin\ngetMin 2",
"output": "6\ninsert 1\ninsert 1\nremoveMin\nremoveMin\ninsert 2\ngetMin 2"
},
{
"input": "1\ninsert 1",
"output": "1\ninsert 1"
},... | 46 | 0 | 0 | 3,631 | |
868 | Bark to Unlock | [
"brute force",
"implementation",
"strings"
] | null | null | As technologies develop, manufacturers are making the process of unlocking a phone as user-friendly as possible. To unlock its new phone, Arkady's pet dog Mu-mu has to bark the password once. The phone represents a password as a string of two lowercase English letters.
Mu-mu's enemy Kashtanka wants to unlock Mu-mu's phone to steal some sensible information, but it can only bark *n* distinct words, each of which can be represented as a string of two lowercase English letters. Kashtanka wants to bark several words (not necessarily distinct) one after another to pronounce a string containing the password as a substring. Tell if it's possible to unlock the phone in this way, or not. | The first line contains two lowercase English lettersΒ β the password on the phone.
The second line contains single integer *n* (1<=β€<=*n*<=β€<=100)Β β the number of words Kashtanka knows.
The next *n* lines contain two lowercase English letters each, representing the words Kashtanka knows. The words are guaranteed to be distinct. | Print "YES" if Kashtanka can bark several words in a line forming a string containing the password, and "NO" otherwise.
You can print each letter in arbitrary case (upper or lower). | [
"ya\n4\nah\noy\nto\nha\n",
"hp\n2\nht\ntp\n",
"ah\n1\nha\n"
] | [
"YES\n",
"NO\n",
"YES\n"
] | In the first example the password is "ya", and Kashtanka can bark "oy" and then "ah", and then "ha" to form the string "oyahha" which contains the password. So, the answer is "YES".
In the second example Kashtanka can't produce a string containing password as a substring. Note that it can bark "ht" and then "tp" producing "http", but it doesn't contain the password "hp" as a substring.
In the third example the string "hahahaha" contains "ah" as a substring. | [
{
"input": "ya\n4\nah\noy\nto\nha",
"output": "YES"
},
{
"input": "hp\n2\nht\ntp",
"output": "NO"
},
{
"input": "ah\n1\nha",
"output": "YES"
},
{
"input": "bb\n4\nba\nab\naa\nbb",
"output": "YES"
},
{
"input": "bc\n4\nca\nba\nbb\ncc",
"output": "YES"
},
{
... | 108 | 0 | 0 | 3,639 | |
0 | none | [
"none"
] | null | null | Π ΠΠ΅ΡΠ»ΡΠ½Π΄ΡΠΊΠΎΠΌ Π³ΠΎΡΡΠ΄Π°ΡΡΡΠ²Π΅Π½Π½ΠΎΠΌ ΡΠ½ΠΈΠ²Π΅ΡΡΠΈΡΠ΅ΡΠ΅ Π»ΠΎΠΊΠ°Π»ΡΠ½Π°Ρ ΡΠ΅ΡΡ ΠΌΠ΅ΠΆΠ΄Ρ ΡΠ΅ΡΠ²Π΅ΡΠ°ΠΌΠΈ Π½Π΅ Π²ΡΠ΅Π³Π΄Π° ΡΠ°Π±ΠΎΡΠ°Π΅Ρ Π±Π΅Π· ΠΎΡΠΈΠ±ΠΎΠΊ. ΠΡΠΈ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΠ΅ Π΄Π²ΡΡ
ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²ΡΡ
ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠΉ ΠΏΠΎΠ΄ΡΡΠ΄ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Π° ΠΎΡΠΈΠ±ΠΊΠ°, Π² ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ΅ ΠΊΠΎΡΠΎΡΠΎΠΉ ΡΡΠΈ Π΄Π²Π° ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ ΡΠ»ΠΈΠ²Π°ΡΡΡΡ Π² ΠΎΠ΄Π½ΠΎ. ΠΡΠΈ ΡΠ°ΠΊΠΎΠΌ ΡΠ»ΠΈΡΠ½ΠΈΠΈ ΠΊΠΎΠ½Π΅Ρ ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ ΡΠΎΠ²ΠΌΠ΅ΡΠ°Π΅ΡΡΡ Ρ Π½Π°ΡΠ°Π»ΠΎΠΌ Π²ΡΠΎΡΠΎΠ³ΠΎ. ΠΠΎΠ½Π΅ΡΠ½ΠΎ, ΡΠΎΠ²ΠΌΠ΅ΡΠ΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΠΏΡΠΎΠΈΡΡ
ΠΎΠ΄ΠΈΡΡ ΡΠΎΠ»ΡΠΊΠΎ ΠΏΠΎ ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²ΡΠΌ ΡΠΈΠΌΠ²ΠΎΠ»Π°ΠΌ. ΠΠ»ΠΈΠ½Π° ΡΠΎΠ²ΠΌΠ΅ΡΠ΅Π½ΠΈΡ Π΄ΠΎΠ»ΠΆΠ½Π° Π±ΡΡΡ ΠΏΠΎΠ»ΠΎΠΆΠΈΡΠ΅Π»ΡΠ½ΡΠΌ ΡΠΈΡΠ»ΠΎΠΌ, ΠΌΠ΅Π½ΡΡΠΈΠΌ Π΄Π»ΠΈΠ½Ρ ΡΠ΅ΠΊΡΡΠ° ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ.
ΠΠ°ΠΏΡΠΈΠΌΠ΅Ρ, ΠΏΡΠΈ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΠ΅ Π΄Π²ΡΡ
ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠΉ Β«abrakadabraΒ» ΠΏΠΎΠ΄ΡΡΠ΄ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ, ΡΡΠΎ ΠΎΠ½ΠΎ Π±ΡΠ΄Π΅Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ ΠΎΠΏΠΈΡΠ°Π½Π½ΠΎΠ³ΠΎ Π²ΠΈΠ΄Π°, ΠΈ ΡΠΎΠ³Π΄Π° Π±ΡΠ΄Π΅Ρ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΎ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ Π²ΠΈΠ΄Π° Β«abrakadabrabrakadabraΒ» ΠΈΠ»ΠΈ Β«abrakadabrakadabraΒ» (Π² ΠΏΠ΅ΡΠ²ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ ΡΠΎΠ²ΠΌΠ΅ΡΠ΅Π½ΠΈΠ΅ ΠΏΡΠΎΠΈΠ·ΠΎΡΠ»ΠΎ ΠΏΠΎ ΠΎΠ΄Π½ΠΎΠΌΡ ΡΠΈΠΌΠ²ΠΎΠ»Ρ, Π° Π²ΠΎ Π²ΡΠΎΡΠΎΠΌ β ΠΏΠΎ ΡΠ΅ΡΡΡΠ΅ΠΌ).
ΠΠΎ ΠΏΠΎΠ»ΡΡΠ΅Π½Π½ΠΎΠΌΡ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ *t* ΠΎΠΏΡΠ΅Π΄Π΅Π»ΠΈΡΠ΅, Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Π»ΠΈ, ΡΡΠΎ ΡΡΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΠΎΡΠΈΠ±ΠΊΠΈ ΠΎΠΏΠΈΡΠ°Π½Π½ΠΎΠ³ΠΎ Π²ΠΈΠ΄Π° ΡΠ°Π±ΠΎΡΡ Π»ΠΎΠΊΠ°Π»ΡΠ½ΠΎΠΉ ΡΠ΅ΡΠΈ, ΠΈ Π΅ΡΠ»ΠΈ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ, ΠΎΠΏΡΠ΅Π΄Π΅Π»ΠΈΡΠ΅ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ *s*.
ΠΠ΅ ΡΠ»Π΅Π΄ΡΠ΅Ρ ΡΡΠΈΡΠ°ΡΡ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ ΡΠΈΡΡΠ°ΡΠΈΡ ΠΏΠΎΠ»Π½ΠΎΠ³ΠΎ Π½Π°Π»ΠΎΠΆΠ΅Π½ΠΈΡ Π΄ΡΡΠ³Π° Π½Π° Π΄ΡΡΠ³Π° Π΄Π²ΡΡ
ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠΉ. Π ΠΏΡΠΈΠΌΠ΅ΡΡ, Π΅ΡΠ»ΠΈ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΎ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ Β«abcdΒ», ΡΠ»Π΅Π΄ΡΠ΅Ρ ΡΡΠΈΡΠ°ΡΡ, ΡΡΠΎ Π² Π½ΡΠΌ ΠΎΡΠΈΠ±ΠΊΠΈ Π½Π΅Ρ. ΠΠ½Π°Π»ΠΎΠ³ΠΈΡΠ½ΠΎ, ΠΏΡΠΎΡΡΠΎΠ΅ Π΄ΠΎΠΏΠΈΡΡΠ²Π°Π½ΠΈΠ΅ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ Π²ΡΠ»Π΅Π΄ Π·Π° Π΄ΡΡΠ³ΠΈΠΌ Π½Π΅ ΡΠ²Π»ΡΠ΅ΡΡΡ ΠΏΡΠΈΠ·Π½Π°ΠΊΠΎΠΌ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°ΠΏΡΠΈΠΌΠ΅Ρ, Π΅ΡΠ»ΠΈ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΎ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ Β«abcabcΒ», ΡΠ»Π΅Π΄ΡΠ΅Ρ ΡΡΠΈΡΠ°ΡΡ, ΡΡΠΎ Π² Π½ΡΠΌ ΠΎΡΠΈΠ±ΠΊΠΈ Π½Π΅Ρ. | Π Π΅Π΄ΠΈΠ½ΡΡΠ²Π΅Π½Π½ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π²ΡΡ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
ΡΠ»Π΅Π΄ΡΠ΅Ρ Π½Π΅ΠΏΡΡΡΠ°Ρ ΡΡΡΠΎΠΊΠ° *t*, ΡΠΎΡΡΠΎΡΡΠ°Ρ ΠΈΠ· ΡΡΡΠΎΡΠ½ΡΡ
Π±ΡΠΊΠ² Π»Π°ΡΠΈΠ½ΡΠΊΠΎΠ³ΠΎ Π°Π»ΡΠ°Π²ΠΈΡΠ°. ΠΠ»ΠΈΠ½Π° ΡΡΡΠΎΠΊΠΈ *t* Π½Π΅ ΠΏΡΠ΅Π²ΠΎΡΡ
ΠΎΠ΄ΠΈΡ 100 ΡΠΈΠΌΠ²ΠΎΠ»ΠΎΠ². | ΠΡΠ»ΠΈ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ *t* Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ ΡΠΎΠ΄Π΅ΡΠΆΠ°ΡΡ ΠΎΡΠΈΠ±ΠΊΠΈ, Π²ΡΠ²Π΅Π΄ΠΈΡΠ΅ Β«NOΒ» (Π±Π΅Π· ΠΊΠ°Π²ΡΡΠ΅ΠΊ) Π² Π΅Π΄ΠΈΠ½ΡΡΠ²Π΅Π½Π½ΡΡ ΡΡΡΠΎΠΊΡ Π²ΡΡ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
.
Π ΠΏΡΠΎΡΠΈΠ²Π½ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ Π² ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π²ΡΠ²Π΅Π΄ΠΈΡΠ΅ Β«YESΒ» (Π±Π΅Π· ΠΊΠ°Π²ΡΡΠ΅ΠΊ), Π° Π² ΡΠ»Π΅Π΄ΡΡΡΠ΅ΠΉ ΡΡΡΠΎΠΊΠ΅ Π²ΡΠ²Π΅Π΄ΠΈΡΠ΅ ΡΡΡΠΎΠΊΡ *s*Β β Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΠ΅ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ ΠΌΠΎΠ³Π»ΠΎ ΠΏΡΠΈΠ²Π΅ΡΡΠΈ ΠΊ ΠΎΡΠΈΠ±ΠΊΠ΅. ΠΡΠ»ΠΈ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΡΡ
ΠΎΡΠ²Π΅ΡΠΎΠ² Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ, ΡΠ°Π·ΡΠ΅ΡΠ°Π΅ΡΡΡ Π²ΡΠ²Π΅ΡΡΠΈ Π»ΡΠ±ΠΎΠΉ ΠΈΠ· Π½ΠΈΡ
. | [
"abrakadabrabrakadabra\n",
"acacacaca\n",
"abcabc\n",
"abababab\n",
"tatbt\n"
] | [
"YES\nabrakadabra\n",
"YES\nacaca\n",
"NO\n",
"YES\nababab\n",
"NO\n"
] | ΠΠΎ Π²ΡΠΎΡΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ ΠΏΠΎΠ΄Ρ
ΠΎΠ΄ΡΡΠΈΠΌ ΠΎΡΠ²Π΅ΡΠΎΠΌ ΡΠ°ΠΊΠΆΠ΅ ΡΠ²Π»ΡΠ΅ΡΡΡ ΡΡΡΠΎΠΊΠ° acacaca. | [
{
"input": "abrakadabrabrakadabra",
"output": "YES\nabrakadabra"
},
{
"input": "acacacaca",
"output": "YES\nacaca"
},
{
"input": "abcabc",
"output": "NO"
},
{
"input": "abababab",
"output": "YES\nababab"
},
{
"input": "tatbt",
"output": "NO"
},
{
"inpu... | 46 | 4,608,000 | 0 | 3,642 | |
689 | Mike and Cellphone | [
"brute force",
"constructive algorithms",
"implementation"
] | null | null | While swimming at the beach, Mike has accidentally dropped his cellphone into the water. There was no worry as he bought a cheap replacement phone with an old-fashioned keyboard. The keyboard has only ten digital equal-sized keys, located in the following way:
Together with his old phone, he lost all his contacts and now he can only remember the way his fingers moved when he put some number in. One can formally consider finger movements as a sequence of vectors connecting centers of keys pressed consecutively to put in a number. For example, the finger movements for number "586" are the same as finger movements for number "253":
Mike has already put in a number by his "finger memory" and started calling it, so he is now worrying, can he be sure that he is calling the correct number? In other words, is there any other number, that has the same finger movements? | The first line of the input contains the only integer *n* (1<=β€<=*n*<=β€<=9)Β β the number of digits in the phone number that Mike put in.
The second line contains the string consisting of *n* digits (characters from '0' to '9') representing the number that Mike put in. | If there is no other phone number with the same finger movements and Mike can be sure he is calling the correct number, print "YES" (without quotes) in the only line.
Otherwise print "NO" (without quotes) in the first line. | [
"3\n586\n",
"2\n09\n",
"9\n123456789\n",
"3\n911\n"
] | [
"NO\n",
"NO\n",
"YES\n",
"YES\n"
] | You can find the picture clarifying the first sample case in the statement above. | [
{
"input": "3\n586",
"output": "NO"
},
{
"input": "2\n09",
"output": "NO"
},
{
"input": "9\n123456789",
"output": "YES"
},
{
"input": "3\n911",
"output": "YES"
},
{
"input": "3\n089",
"output": "NO"
},
{
"input": "3\n159",
"output": "YES"
},
{
... | 46 | 0 | -1 | 3,650 | |
253 | Physics Practical | [
"binary search",
"dp",
"sortings",
"two pointers"
] | null | null | One day Vasya was on a physics practical, performing the task on measuring the capacitance. He followed the teacher's advice and did as much as *n* measurements, and recorded the results in the notebook. After that he was about to show the results to the teacher, but he remembered that at the last lesson, the teacher had made his friend Petya redo the experiment because the largest and the smallest results differed by more than two times. Vasya is lazy, and he does not want to redo the experiment. He wants to do the task and go home play computer games. So he decided to cheat: before Vasya shows the measurements to the teacher, he will erase some of them, so as to make the largest and the smallest results of the remaining measurements differ in no more than two times. In other words, if the remaining measurements have the smallest result *x*, and the largest result *y*, then the inequality *y*<=β€<=2Β·*x* must fulfill. Of course, to avoid the teacher's suspicion, Vasya wants to remove as few measurement results as possible from his notes.
Help Vasya, find what minimum number of measurement results he will have to erase from his notes so that the largest and the smallest of the remaining results of the measurements differed in no more than two times. | The first line contains integer *n* (2<=β€<=*n*<=β€<=105) β the number of measurements Vasya made. The second line contains *n* integers *c*1,<=*c*2,<=...,<=*c**n* (1<=β€<=*c**i*<=β€<=5000) β the results of the measurements. The numbers on the second line are separated by single spaces. | Print a single integer β the minimum number of results Vasya will have to remove. | [
"6\n4 5 3 8 3 7\n",
"4\n4 3 2 4\n"
] | [
"2\n",
"0\n"
] | In the first sample you can remove the fourth and the sixth measurement results (values 8 and 7). Then the maximum of the remaining values will be 5, and the minimum one will be 3. Or else, you can remove the third and fifth results (both equal 3). After that the largest remaining result will be 8, and the smallest one will be 4. | [
{
"input": "6\n4 5 3 8 3 7",
"output": "2"
},
{
"input": "4\n4 3 2 4",
"output": "0"
},
{
"input": "6\n5 6 4 9 4 8",
"output": "1"
},
{
"input": "4\n5 4 1 5",
"output": "1"
},
{
"input": "2\n3 2",
"output": "0"
},
{
"input": "10\n39 9 18 13 6 16 47 15 ... | 30 | 0 | 0 | 3,680 | |
331 | Oh Sweet Beaverette | [
"data structures",
"sortings"
] | null | null | β Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me?
β Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night?
At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming walk. He needed to cut down several trees.
Let's consider the woodland belt as a sequence of trees. Each tree *i* is described by the esthetic appeal *a**i* β some trees are very esthetically pleasing, others are 'so-so', and some trees are positively ugly!
The Smart Beaver calculated that he needed the following effects to win the Beaverette's heart:
- The first objective is to please the Beaverette: the sum of esthetic appeal of the remaining trees must be maximum possible; - the second objective is to surprise the Beaverette: the esthetic appeal of the first and the last trees in the resulting belt must be the same; - and of course, the walk should be successful: there must be at least two trees in the woodland belt left.
Now help the Smart Beaver! Which trees does he need to cut down to win the Beaverette's heart? | The first line contains a single integer *n* β the initial number of trees in the woodland belt, 2<=β€<=*n*. The second line contains space-separated integers *a**i* β the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value.
- to get 30 points, you need to solve the problem with constraints: *n*<=β€<=100 (subproblem A1); - to get 100 points, you need to solve the problem with constraints: *n*<=β€<=3Β·105 (subproblems A1+A2). | In the first line print two integers β the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees *k*.
In the next line print *k* integers β the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to *n* from left to right.
If there are multiple solutions, print any of them. It is guaranteed that at least two trees have equal esthetic appeal. | [
"5\n1 2 3 1 2\n",
"5\n1 -2 3 1 -2\n"
] | [
"8 1\n1 ",
"5 2\n2 5 "
] | none | [
{
"input": "5\n1 2 3 1 2",
"output": "8 1\n1 "
},
{
"input": "5\n1 -2 3 1 -2",
"output": "5 2\n2 5 "
},
{
"input": "2\n0 0",
"output": "0 0"
},
{
"input": "3\n0 -1 0",
"output": "0 1\n2 "
},
{
"input": "3\n1 1 1",
"output": "3 0"
},
{
"input": "4\n-1 1... | 310 | 20,275,200 | 0 | 3,685 | |
15 | Cottage Village | [
"implementation",
"sortings"
] | A. Cottage Village | 2 | 64 | A new cottage village called Β«FlatvilleΒ» is being built in Flatland. By now they have already built in Β«FlatvilleΒ» *n* square houses with the centres on the *Πx*-axis. The houses' sides are parallel to the coordinate axes. It's known that no two houses overlap, but they can touch each other.
The architect bureau, where Peter works, was commissioned to build a new house in Β«FlatvilleΒ». The customer wants his future house to be on the *Πx*-axis, to be square in shape, have a side *t*, and touch at least one of the already built houses. For sure, its sides should be parallel to the coordinate axes, its centre should be on the *Ox*-axis and it shouldn't overlap any of the houses in the village.
Peter was given a list of all the houses in Β«FlatvilleΒ». Would you help him find the amount of possible positions of the new house? | The first line of the input data contains numbers *n* and *t* (1<=β€<=*n*,<=*t*<=β€<=1000). Then there follow *n* lines, each of them contains two space-separated integer numbers: *x**i* *a**i*, where *x**i* β *x*-coordinate of the centre of the *i*-th house, and *a**i* β length of its side (<=-<=1000<=β€<=*x**i*<=β€<=1000, 1<=β€<=*a**i*<=β€<=1000). | Output the amount of possible positions of the new house. | [
"2 2\n0 4\n6 2\n",
"2 2\n0 4\n5 2\n",
"2 3\n0 4\n5 2\n"
] | [
"4\n",
"3\n",
"2\n"
] | It is possible for the *x*-coordinate of the new house to have non-integer value. | [
{
"input": "2 2\n0 4\n6 2",
"output": "4"
},
{
"input": "2 2\n0 4\n5 2",
"output": "3"
},
{
"input": "2 3\n0 4\n5 2",
"output": "2"
},
{
"input": "1 1\n1 1",
"output": "2"
},
{
"input": "1 2\n2 1",
"output": "2"
},
{
"input": "2 1\n2 1\n1 1",
"outp... | 746 | 0 | 3.8135 | 3,691 |
789 | Anastasia and pebbles | [
"implementation",
"math"
] | null | null | Anastasia loves going for a walk in Central Uzhlyandian Park. But she became uninterested in simple walking, so she began to collect Uzhlyandian pebbles. At first, she decided to collect all the pebbles she could find in the park.
She has only two pockets. She can put at most *k* pebbles in each pocket at the same time. There are *n* different pebble types in the park, and there are *w**i* pebbles of the *i*-th type. Anastasia is very responsible, so she never mixes pebbles of different types in same pocket. However, she can put different kinds of pebbles in different pockets at the same time. Unfortunately, she can't spend all her time collecting pebbles, so she can collect pebbles from the park only once a day.
Help her to find the minimum number of days needed to collect all the pebbles of Uzhlyandian Central Park, taking into consideration that Anastasia can't place pebbles of different types in same pocket. | The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=105, 1<=β€<=*k*<=β€<=109)Β β the number of different pebble types and number of pebbles Anastasia can place in one pocket.
The second line contains *n* integers *w*1,<=*w*2,<=...,<=*w**n* (1<=β€<=*w**i*<=β€<=104)Β β number of pebbles of each type. | The only line of output contains one integerΒ β the minimum number of days Anastasia needs to collect all the pebbles. | [
"3 2\n2 3 4\n",
"5 4\n3 1 8 9 7\n"
] | [
"3\n",
"5\n"
] | In the first sample case, Anastasia can collect all pebbles of the first type on the first day, of second typeΒ β on the second day, and of third typeΒ β on the third day.
Optimal sequence of actions in the second sample case:
- In the first day Anastasia collects 8 pebbles of the third type. - In the second day she collects 8 pebbles of the fourth type. - In the third day she collects 3 pebbles of the first type and 1 pebble of the fourth type. - In the fourth day she collects 7 pebbles of the fifth type. - In the fifth day she collects 1 pebble of the second type. | [
{
"input": "3 2\n2 3 4",
"output": "3"
},
{
"input": "5 4\n3 1 8 9 7",
"output": "5"
},
{
"input": "1 22\n1",
"output": "1"
},
{
"input": "3 57\n78 165 54",
"output": "3"
},
{
"input": "5 72\n74 10 146 189 184",
"output": "6"
},
{
"input": "9 13\n132 8... | 1,000 | 11,059,200 | 0 | 3,695 | |
467 | George and Job | [
"dp",
"implementation"
] | null | null | The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, he didn't have enough money, so George was going to work as a programmer. Now he faced the following problem at the work.
Given a sequence of *n* integers *p*1,<=*p*2,<=...,<=*p**n*. You are to choose *k* pairs of integers:
in such a way that the value of sum is maximal possible. Help George to cope with the task. | The first line contains three integers *n*, *m* and *k* (1<=β€<=(*m*<=Γ<=*k*)<=β€<=*n*<=β€<=5000). The second line contains *n* integers *p*1,<=*p*2,<=...,<=*p**n* (0<=β€<=*p**i*<=β€<=109). | Print an integer in a single line β the maximum possible value of sum. | [
"5 2 1\n1 2 3 4 5\n",
"7 1 3\n2 10 7 18 5 33 0\n"
] | [
"9\n",
"61\n"
] | none | [
{
"input": "5 2 1\n1 2 3 4 5",
"output": "9"
},
{
"input": "7 1 3\n2 10 7 18 5 33 0",
"output": "61"
},
{
"input": "13 8 1\n73 7 47 91 54 74 99 11 67 35 84 18 19",
"output": "515"
},
{
"input": "8 3 1\n8 46 37 81 81 57 11 2",
"output": "219"
},
{
"input": "20 5 3\... | 1,000 | 0 | 0 | 3,708 | |
722 | Destroying Array | [
"data structures",
"dsu"
] | null | null | You are given an array consisting of *n* non-negative integers *a*1,<=*a*2,<=...,<=*a**n*.
You are going to destroy integers in the array one by one. Thus, you are given the permutation of integers from 1 to *n* defining the order elements of the array are destroyed.
After each element is destroyed you have to find out the segment of the array, such that it contains no destroyed elements and the sum of its elements is maximum possible. The sum of elements in the empty segment is considered to be 0. | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=100<=000)Β β the length of the array.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=109).
The third line contains a permutation of integers from 1 to *n*Β β the order used to destroy elements. | Print *n* lines. The *i*-th line should contain a single integerΒ β the maximum possible sum of elements on the segment containing no destroyed elements, after first *i* operations are performed. | [
"4\n1 3 2 5\n3 4 1 2\n",
"5\n1 2 3 4 5\n4 2 3 5 1\n",
"8\n5 5 4 4 6 6 5 5\n5 2 8 7 1 3 4 6\n"
] | [
"5\n4\n3\n0\n",
"6\n5\n5\n1\n0\n",
"18\n16\n11\n8\n8\n6\n6\n0\n"
] | Consider the first sample:
1. Third element is destroyed. Array is now 1Β 3Β β*βΒ 5. Segment with maximum sum 5 consists of one integer 5. 1. Fourth element is destroyed. Array is now 1Β 3Β β*βΒ β*β. Segment with maximum sum 4 consists of two integers 1Β 3. 1. First element is destroyed. Array is now β*βΒ 3Β β*βΒ β*β. Segment with maximum sum 3 consists of one integer 3. 1. Last element is destroyed. At this moment there are no valid nonempty segments left in this array, so the answer is equal to 0. | [
{
"input": "4\n1 3 2 5\n3 4 1 2",
"output": "5\n4\n3\n0"
},
{
"input": "5\n1 2 3 4 5\n4 2 3 5 1",
"output": "6\n5\n5\n1\n0"
},
{
"input": "8\n5 5 4 4 6 6 5 5\n5 2 8 7 1 3 4 6",
"output": "18\n16\n11\n8\n8\n6\n6\n0"
},
{
"input": "10\n3 3 3 5 6 9 3 1 7 3\n3 4 6 7 5 1 10 9 2 8"... | 1,000 | 6,451,200 | 0 | 3,710 | |
53 | Autocomplete | [
"implementation"
] | A. Autocomplete | 2 | 256 | Autocomplete is a program function that enables inputting the text (in editors, command line shells, browsers etc.) completing the text by its inputted part. Vasya is busy working on a new browser called 'BERowser'. He happens to be working on the autocomplete function in the address line at this very moment. A list consisting of *n* last visited by the user pages and the inputted part *s* are known. Your task is to complete *s* to make it an address of one of the pages from the list. You have to find the lexicographically smallest address having a prefix *s*. | The first line contains the *s* line which is the inputted part. The second line contains an integer *n* (1<=β€<=*n*<=β€<=100) which is the number of visited pages. Then follow *n* lines which are the visited pages, one on each line. All the lines have lengths of from 1 to 100 symbols inclusively and consist of lowercase Latin letters only. | If *s* is not the beginning of any of *n* addresses of the visited pages, print *s*. Otherwise, print the lexicographically minimal address of one of the visited pages starting from *s*.
The lexicographical order is the order of words in a dictionary. The lexicographical comparison of lines is realized by the '<' operator in the modern programming languages. | [
"next\n2\nnextpermutation\nnextelement\n",
"find\n4\nfind\nfindfirstof\nfindit\nfand\n",
"find\n4\nfondfind\nfondfirstof\nfondit\nfand\n"
] | [
"nextelement\n",
"find\n",
"find\n"
] | none | [
{
"input": "next\n2\nnextpermutation\nnextelement",
"output": "nextelement"
},
{
"input": "find\n4\nfind\nfindfirstof\nfindit\nfand",
"output": "find"
},
{
"input": "find\n4\nfondfind\nfondfirstof\nfondit\nfand",
"output": "find"
},
{
"input": "kudljmxcse\n4\nkudljmxcse\nszje... | 216 | 0 | 3.946 | 3,715 |
471 | MUH and Cube Walls | [
"string suffix structures",
"strings"
] | null | null | Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev got hold of lots of wooden cubes somewhere. They started making cube towers by placing the cubes one on top of the other. They defined multiple towers standing in a line as a wall. A wall can consist of towers of different heights.
Horace was the first to finish making his wall. He called his wall an elephant. The wall consists of *w* towers. The bears also finished making their wall but they didn't give it a name. Their wall consists of *n* towers. Horace looked at the bears' tower and wondered: in how many parts of the wall can he "see an elephant"? He can "see an elephant" on a segment of *w* contiguous towers if the heights of the towers on the segment match as a sequence the heights of the towers in Horace's wall. In order to see as many elephants as possible, Horace can raise and lower his wall. He even can lower the wall below the ground level (see the pictures to the samples for clarification).
Your task is to count the number of segments where Horace can "see an elephant". | The first line contains two integers *n* and *w* (1<=β€<=*n*,<=*w*<=β€<=2Β·105) β the number of towers in the bears' and the elephant's walls correspondingly. The second line contains *n* integers *a**i* (1<=β€<=*a**i*<=β€<=109) β the heights of the towers in the bears' wall. The third line contains *w* integers *b**i* (1<=β€<=*b**i*<=β€<=109) β the heights of the towers in the elephant's wall. | Print the number of segments in the bears' wall where Horace can "see an elephant". | [
"13 5\n2 4 5 5 4 3 2 2 2 3 3 2 1\n3 4 4 3 2\n"
] | [
"2"
] | The picture to the left shows Horace's wall from the sample, the picture to the right shows the bears' wall. The segments where Horace can "see an elephant" are in gray. | [
{
"input": "13 5\n2 4 5 5 4 3 2 2 2 3 3 2 1\n3 4 4 3 2",
"output": "2"
},
{
"input": "5 1\n8 71 1 24 2\n31",
"output": "5"
},
{
"input": "6 3\n2 2 2 2 2 2\n5 5 5",
"output": "4"
},
{
"input": "1 1\n576560149\n691846236",
"output": "1"
},
{
"input": "10 5\n5 10 8 1... | 31 | 0 | 0 | 3,718 | |
361 | Levko and Permutation | [
"constructive algorithms",
"math",
"number theory"
] | null | null | Levko loves permutations very much. A permutation of length *n* is a sequence of distinct positive integers, each is at most *n*.
Letβs assume that value *gcd*(*a*,<=*b*) shows the greatest common divisor of numbers *a* and *b*. Levko assumes that element *p**i* of permutation *p*1,<=*p*2,<=... ,<=*p**n* is good if *gcd*(*i*,<=*p**i*)<=><=1. Levko considers a permutation beautiful, if it has exactly *k* good elements. Unfortunately, he doesnβt know any beautiful permutation. Your task is to help him to find at least one of them. | The single line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=105, 0<=β€<=*k*<=β€<=*n*). | In a single line print either any beautiful permutation or -1, if such permutation doesnβt exist.
If there are multiple suitable permutations, you are allowed to print any of them. | [
"4 2\n",
"1 1\n"
] | [
"2 4 3 1",
"-1\n"
] | In the first sample elements 4 and 3 are good because *gcd*(2,β4)β=β2β>β1 and *gcd*(3,β3)β=β3β>β1. Elements 2 and 1 are not good because *gcd*(1,β2)β=β1 and *gcd*(4,β1)β=β1. As there are exactly 2 good elements, the permutation is beautiful.
The second sample has no beautiful permutations. | [
{
"input": "4 2",
"output": "2 1 3 4 "
},
{
"input": "1 1",
"output": "-1"
},
{
"input": "7 4",
"output": "3 1 2 4 5 6 7 "
},
{
"input": "10 9",
"output": "1 2 3 4 5 6 7 8 9 10 "
},
{
"input": "10000 5000",
"output": "5000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1... | 77 | 0 | 0 | 3,723 | |
0 | none | [
"none"
] | null | null | Let's introduce a number system which is based on a roman digits. There are digits I, V, X, L which correspond to the numbers $1$, $5$, $10$ and $50$ respectively. The use of other roman digits is not allowed.
Numbers in this system are written as a sequence of one or more digits. We define the value of the sequence simply as the sum of digits in it.
For example, the number XXXV evaluates to $35$ and the number IXIΒ β to $12$.
Pay attention to the difference to the traditional roman systemΒ β in our system any sequence of digits is valid, moreover the order of digits doesn't matter, for example IX means $11$, not $9$.
One can notice that this system is ambiguous, and some numbers can be written in many different ways. Your goal is to determine how many distinct integers can be represented by exactly $n$ roman digits I, V, X, L. | The only line of the input file contains a single integer $n$ ($1 \le n \le 10^9$)Β β the number of roman digits to use. | Output a single integerΒ β the number of distinct integers which can be represented using $n$ roman digits exactly. | [
"1\n",
"2\n",
"10\n"
] | [
"4\n",
"10\n",
"244\n"
] | In the first sample there are exactly $4$ integers which can be representedΒ β I, V, X and L.
In the second sample it is possible to represent integers $2$ (II), $6$ (VI), $10$ (VV), $11$ (XI), $15$ (XV), $20$ (XX), $51$ (IL), $55$ (VL), $60$ (XL) and $100$ (LL). | [
{
"input": "1",
"output": "4"
},
{
"input": "2",
"output": "10"
},
{
"input": "10",
"output": "244"
},
{
"input": "1000",
"output": "48753"
},
{
"input": "2000",
"output": "97753"
},
{
"input": "5000",
"output": "244753"
},
{
"input": "1000... | 124 | 409,600 | 3 | 3,746 | |
729 | Road to Cinema | [
"binary search",
"greedy",
"sortings"
] | null | null | Vasya is currently at a car rental service, and he wants to reach cinema. The film he has bought a ticket for starts in *t* minutes. There is a straight road of length *s* from the service to the cinema. Let's introduce a coordinate system so that the car rental service is at the point 0, and the cinema is at the point *s*.
There are *k* gas stations along the road, and at each of them you can fill a car with any amount of fuel for free! Consider that this operation doesn't take any time, i.e. is carried out instantly.
There are *n* cars in the rental service, *i*-th of them is characterized with two integers *c**i* and *v**i*Β β the price of this car rent and the capacity of its fuel tank in liters. It's not allowed to fuel a car with more fuel than its tank capacity *v**i*. All cars are completely fueled at the car rental service.
Each of the cars can be driven in one of two speed modes: normal or accelerated. In the normal mode a car covers 1 kilometer in 2 minutes, and consumes 1 liter of fuel. In the accelerated mode a car covers 1 kilometer in 1 minutes, but consumes 2 liters of fuel. The driving mode can be changed at any moment and any number of times.
Your task is to choose a car with minimum price such that Vasya can reach the cinema before the show starts, i.e. not later than in *t* minutes. Assume that all cars are completely fueled initially. | The first line contains four positive integers *n*, *k*, *s* and *t* (1<=β€<=*n*<=β€<=2Β·105, 1<=β€<=*k*<=β€<=2Β·105, 2<=β€<=*s*<=β€<=109, 1<=β€<=*t*<=β€<=2Β·109)Β β the number of cars at the car rental service, the number of gas stations along the road, the length of the road and the time in which the film starts.
Each of the next *n* lines contains two positive integers *c**i* and *v**i* (1<=β€<=*c**i*,<=*v**i*<=β€<=109)Β β the price of the *i*-th car and its fuel tank capacity.
The next line contains *k* distinct integers *g*1,<=*g*2,<=...,<=*g**k* (1<=β€<=*g**i*<=β€<=*s*<=-<=1)Β β the positions of the gas stations on the road in arbitrary order. | Print the minimum rent price of an appropriate car, i.e. such car that Vasya will be able to reach the cinema before the film starts (not later than in *t* minutes). If there is no appropriate car, print -1. | [
"3 1 8 10\n10 8\n5 7\n11 9\n3\n",
"2 2 10 18\n10 4\n20 6\n5 3\n"
] | [
"10\n",
"20\n"
] | In the first sample, Vasya can reach the cinema in time using the first or the third cars, but it would be cheaper to choose the first one. Its price is equal to 10, and the capacity of its fuel tank is 8. Then Vasya can drive to the first gas station in the accelerated mode in 3 minutes, spending 6 liters of fuel. After that he can full the tank and cover 2 kilometers in the normal mode in 4 minutes, spending 2 liters of fuel. Finally, he drives in the accelerated mode covering the remaining 3 kilometers in 3 minutes and spending 6 liters of fuel. | [
{
"input": "3 1 8 10\n10 8\n5 7\n11 9\n3",
"output": "10"
},
{
"input": "2 2 10 18\n10 4\n20 6\n5 3",
"output": "20"
},
{
"input": "2 1 1000000000 2000000000\n111 1000000000\n101 1000000000\n5",
"output": "101"
},
{
"input": "2 1 1000000000 2000000000\n111 999999998\n101 9999... | 1,000 | 37,376,000 | 0 | 3,770 | |
52 | Right Triangles | [
"combinatorics"
] | B. Right Triangles | 2 | 256 | You are given a *n*<=Γ<=*m* field consisting only of periods ('.') and asterisks ('*'). Your task is to count all right triangles with two sides parallel to the square sides, whose vertices are in the centers of '*'-cells. A right triangle is a triangle in which one angle is a right angle (that is, a 90 degree angle). | The first line contains two positive integer numbers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=1000). The following *n* lines consist of *m* characters each, describing the field. Only '.' and '*' are allowed. | Output a single number β total number of square triangles in the field. 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). | [
"2 2\n**\n*.\n",
"3 4\n*..*\n.**.\n*.**\n"
] | [
"1\n",
"9\n"
] | none | [
{
"input": "2 2\n**\n*.",
"output": "1"
},
{
"input": "3 4\n*..*\n.**.\n*.**",
"output": "9"
},
{
"input": "3 2\n..\n..\n*.",
"output": "0"
},
{
"input": "1 2\n**",
"output": "0"
},
{
"input": "1 3\n*.*",
"output": "0"
},
{
"input": "5 2\n*.\n**\n.*\n.... | 218 | 3,891,200 | 3.938252 | 3,785 |
898 | Squares and not squares | [
"constructive algorithms",
"greedy"
] | null | null | Ann and Borya have *n* piles with candies and *n* is even number. There are *a**i* candies in pile with number *i*.
Ann likes numbers which are square of some integer and Borya doesn't like numbers which are square of any integer. During one move guys can select some pile with candies and add one candy to it (this candy is new and doesn't belong to any other pile) or remove one candy (if there is at least one candy in this pile).
Find out minimal number of moves that is required to make exactly *n*<=/<=2 piles contain number of candies that is a square of some integer and exactly *n*<=/<=2 piles contain number of candies that is not a square of any integer. | First line contains one even integer *n* (2<=β€<=*n*<=β€<=200<=000)Β β number of piles with candies.
Second line contains sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=109)Β β amounts of candies in each pile. | Output minimal number of steps required to make exactly *n*<=/<=2 piles contain number of candies that is a square of some integer and exactly *n*<=/<=2 piles contain number of candies that is not a square of any integer. If condition is already satisfied output 0. | [
"4\n12 14 30 4\n",
"6\n0 0 0 0 0 0\n",
"6\n120 110 23 34 25 45\n",
"10\n121 56 78 81 45 100 1 0 54 78\n"
] | [
"2\n",
"6\n",
"3\n",
"0\n"
] | In first example you can satisfy condition in two moves. During each move you should add one candy to second pile. After it size of second pile becomes 16. After that Borya and Ann will have two piles with number of candies which is a square of integer (second and fourth pile) and two piles with number of candies which is not a square of any integer (first and third pile).
In second example you should add two candies to any three piles. | [
{
"input": "4\n12 14 30 4",
"output": "2"
},
{
"input": "6\n0 0 0 0 0 0",
"output": "6"
},
{
"input": "6\n120 110 23 34 25 45",
"output": "3"
},
{
"input": "10\n121 56 78 81 45 100 1 0 54 78",
"output": "0"
},
{
"input": "10\n0 675178538 310440616 608075179 0 0 0 ... | 1,684 | 21,708,800 | 3 | 3,791 | |
490 | Hacking Cypher | [
"brute force",
"math",
"number theory",
"strings"
] | null | null | Polycarpus participates in a competition for hacking into a new secure messenger. He's almost won.
Having carefully studied the interaction protocol, Polycarpus came to the conclusion that the secret key can be obtained if he properly cuts the public key of the application into two parts. The public key is a long integer which may consist of even a million digits!
Polycarpus needs to find such a way to cut the public key into two nonempty parts, that the first (left) part is divisible by *a* as a separate number, and the second (right) part is divisible by *b* as a separate number. Both parts should be positive integers that have no leading zeros. Polycarpus knows values *a* and *b*.
Help Polycarpus and find any suitable method to cut the public key. | The first line of the input contains the public key of the messenger β an integer without leading zeroes, its length is in range from 1 to 106 digits. The second line contains a pair of space-separated positive integers *a*, *b* (1<=β€<=*a*,<=*b*<=β€<=108). | In the first line print "YES" (without the quotes), if the method satisfying conditions above exists. In this case, next print two lines β the left and right parts after the cut. These two parts, being concatenated, must be exactly identical to the public key. The left part must be divisible by *a*, and the right part must be divisible by *b*. The two parts must be positive integers having no leading zeros. If there are several answers, print any of them.
If there is no answer, print in a single line "NO" (without the quotes). | [
"116401024\n97 1024\n",
"284254589153928171911281811000\n1009 1000\n",
"120\n12 1\n"
] | [
"YES\n11640\n1024\n",
"YES\n2842545891539\n28171911281811000\n",
"NO\n"
] | none | [
{
"input": "116401024\n97 1024",
"output": "YES\n11640\n1024"
},
{
"input": "284254589153928171911281811000\n1009 1000",
"output": "YES\n2842545891539\n28171911281811000"
},
{
"input": "120\n12 1",
"output": "NO"
},
{
"input": "604\n6 4",
"output": "YES\n60\n4"
},
{
... | 77 | 2,867,200 | -1 | 3,792 | |
606 | Magic Spheres | [
"implementation"
] | null | null | Carl is a beginner magician. He has *a* blue, *b* violet and *c* orange magic spheres. In one move he can transform two spheres of the same color into one sphere of any other color. To make a spell that has never been seen before, he needs at least *x* blue, *y* violet and *z* orange spheres. Can he get them (possible, in multiple actions)? | The first line of the input contains three integers *a*, *b* and *c* (0<=β€<=*a*,<=*b*,<=*c*<=β€<=1<=000<=000)Β β the number of blue, violet and orange spheres that are in the magician's disposal.
The second line of the input contains three integers, *x*, *y* and *z* (0<=β€<=*x*,<=*y*,<=*z*<=β€<=1<=000<=000)Β β the number of blue, violet and orange spheres that he needs to get. | If the wizard is able to obtain the required numbers of spheres, print "Yes". Otherwise, print "No". | [
"4 4 0\n2 1 2\n",
"5 6 1\n2 7 2\n",
"3 3 3\n2 2 2\n"
] | [
"Yes\n",
"No\n",
"Yes\n"
] | In the first sample the wizard has 4 blue and 4 violet spheres. In his first action he can turn two blue spheres into one violet one. After that he will have 2 blue and 5 violet spheres. Then he turns 4 violet spheres into 2 orange spheres and he ends up with 2 blue, 1 violet and 2 orange spheres, which is exactly what he needs. | [
{
"input": "4 4 0\n2 1 2",
"output": "Yes"
},
{
"input": "5 6 1\n2 7 2",
"output": "No"
},
{
"input": "3 3 3\n2 2 2",
"output": "Yes"
},
{
"input": "0 0 0\n0 0 0",
"output": "Yes"
},
{
"input": "0 0 0\n0 0 1",
"output": "No"
},
{
"input": "0 1 0\n0 0 0... | 62 | 0 | 0 | 3,802 | |
975 | Aramic script | [
"implementation",
"strings"
] | null | null | In Aramic language words can only represent objects.
Words in Aramic have special properties:
- A word is a root if it does not contain the same letter more than once. - A root and all its permutations represent the same object. - The root $x$ of a word $y$ is the word that contains all letters that appear in $y$ in a way that each letter appears once. For example, the root of "aaaa", "aa", "aaa" is "a", the root of "aabb", "bab", "baabb", "ab" is "ab". - Any word in Aramic represents the same object as its root.
You have an ancient script in Aramic. What is the number of different objects mentioned in the script? | The first line contains one integer $n$ ($1 \leq n \leq 10^3$)Β β the number of words in the script.
The second line contains $n$ words $s_1, s_2, \ldots, s_n$Β β the script itself. The length of each string does not exceed $10^3$.
It is guaranteed that all characters of the strings are small latin letters. | Output one integerΒ β the number of different objects mentioned in the given ancient Aramic script. | [
"5\na aa aaa ab abb\n",
"3\namer arem mrea\n"
] | [
"2",
"1"
] | In the first test, there are two objects mentioned. The roots that represent them are "a","ab".
In the second test, there is only one object, its root is "amer", the other strings are just permutations of "amer". | [
{
"input": "5\na aa aaa ab abb",
"output": "2"
},
{
"input": "3\namer arem mrea",
"output": "1"
},
{
"input": "10\nbda bbb cda dca dda dcb bcd dcb ada ddd",
"output": "6"
},
{
"input": "2\nfhjlqs aceginpr",
"output": "2"
},
{
"input": "2\nbcdfghimn efghijlmo",
... | 93 | 2,969,600 | 3 | 3,808 | |
745 | Hongcow Solves A Puzzle | [
"implementation"
] | null | null | Hongcow likes solving puzzles.
One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an *n* by *m* grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified.
The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap.
You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. | The first line of input will contain two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=500), the dimensions of the puzzle piece.
The next *n* lines will describe the jigsaw piece. Each line will have length *m* and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space.
It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. | Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. | [
"2 3\nXXX\nXXX\n",
"2 2\n.X\nXX\n",
"5 5\n.....\n..X..\n.....\n.....\n.....\n"
] | [
"YES\n",
"NO\n",
"YES\n"
] | For the first sample, one example of a rectangle we can form is as follows
For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle.
In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: | [
{
"input": "2 3\nXXX\nXXX",
"output": "YES"
},
{
"input": "2 2\n.X\nXX",
"output": "NO"
},
{
"input": "1 500\n.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX... | 62 | 5,529,600 | 3 | 3,817 | |
859 | Declined Finalists | [
"greedy",
"implementation"
] | null | null | This year, as in previous years, MemSQL is inviting the top 25 competitors from the Start[c]up qualification round to compete onsite for the final round. Not everyone who is eligible to compete onsite can afford to travel to the office, though. Initially the top 25 contestants are invited to come onsite. Each eligible contestant must either accept or decline the invitation. Whenever a contestant declines, the highest ranked contestant not yet invited is invited to take the place of the one that declined. This continues until 25 contestants have accepted invitations.
After the qualifying round completes, you know *K* of the onsite finalists, as well as their qualifying ranks (which start at 1, there are no ties). Determine the minimum possible number of contestants that declined the invitation to compete onsite in the final round. | The first line of input contains *K* (1<=β€<=*K*<=β€<=25), the number of onsite finalists you know. The second line of input contains *r*1,<=*r*2,<=...,<=*r**K* (1<=β€<=*r**i*<=β€<=106), the qualifying ranks of the finalists you know. All these ranks are distinct. | Print the minimum possible number of contestants that declined the invitation to compete onsite. | [
"25\n2 3 4 5 6 7 8 9 10 11 12 14 15 16 17 18 19 20 21 22 23 24 25 26 28\n",
"5\n16 23 8 15 4\n",
"3\n14 15 92\n"
] | [
"3\n",
"0\n",
"67\n"
] | In the first example, you know all 25 onsite finalists. The contestants who ranked 1-st, 13-th, and 27-th must have declined, so the answer is 3. | [
{
"input": "25\n2 3 4 5 6 7 8 9 10 11 12 14 15 16 17 18 19 20 21 22 23 24 25 26 28",
"output": "3"
},
{
"input": "5\n16 23 8 15 4",
"output": "0"
},
{
"input": "3\n14 15 92",
"output": "67"
},
{
"input": "1\n1000000",
"output": "999975"
},
{
"input": "25\n1000000 ... | 31 | 0 | -1 | 3,840 | |
105 | Transmigration | [
"implementation"
] | A. Transmigration | 2 | 256 | In Disgaea as in most role-playing games, characters have skills that determine the character's ability to use certain weapons or spells. If the character does not have the necessary skill, he cannot use it. The skill level is represented as an integer that increases when you use this skill. Different character classes are characterized by different skills.
Unfortunately, the skills that are uncommon for the given character's class are quite difficult to obtain. To avoid this limitation, there is the so-called transmigration.
Transmigration is reincarnation of the character in a new creature. His soul shifts to a new body and retains part of his experience from the previous life.
As a result of transmigration the new character gets all the skills of the old character and the skill levels are reduced according to the *k* coefficient (if the skill level was equal to *x*, then after transmigration it becomes equal to [*kx*], where [*y*] is the integral part of *y*). If some skill's levels are strictly less than 100, these skills are forgotten (the character does not have them any more). After that the new character also gains the skills that are specific for his class, but are new to him. The levels of those additional skills are set to 0.
Thus, one can create a character with skills specific for completely different character classes via transmigrations. For example, creating a mage archer or a thief warrior is possible.
You are suggested to solve the following problem: what skills will the character have after transmigration and what will the levels of those skills be? | The first line contains three numbers *n*, *m* and *k* β the number of skills the current character has, the number of skills specific for the class into which the character is going to transmigrate and the reducing coefficient respectively; *n* and *m* are integers, and *k* is a real number with exactly two digits after decimal point (1<=β€<=*n*,<=*m*<=β€<=20, 0.01<=β€<=*k*<=β€<=0.99).
Then follow *n* lines, each of which describes a character's skill in the form "*name* *exp*" β the skill's name and the character's skill level: *name* is a string and *exp* is an integer in range from 0 to 9999, inclusive.
Then follow *m* lines each of which contains names of skills specific for the class, into which the character transmigrates.
All names consist of lowercase Latin letters and their lengths can range from 1 to 20 characters, inclusive. All character's skills have distinct names. Besides the skills specific for the class into which the player transmigrates also have distinct names. | Print on the first line number *z* β the number of skills the character will have after the transmigration. Then print *z* lines, on each of which print a skill's name and level, separated by a single space. The skills should be given in the lexicographical order. | [
"5 4 0.75\naxe 350\nimpaler 300\nionize 80\nmegafire 120\nmagicboost 220\nheal\nmegafire\nshield\nmagicboost\n"
] | [
"6\naxe 262\nheal 0\nimpaler 225\nmagicboost 165\nmegafire 0\nshield 0\n"
] | none | [
{
"input": "5 4 0.75\naxe 350\nimpaler 300\nionize 80\nmegafire 120\nmagicboost 220\nheal\nmegafire\nshield\nmagicboost",
"output": "6\naxe 262\nheal 0\nimpaler 225\nmagicboost 165\nmegafire 0\nshield 0"
},
{
"input": "1 1 0.50\nstaff 1005\nionize",
"output": "2\nionize 0\nstaff 502"
},
{
... | 156 | 0 | 0 | 3,846 |
888 | Buggy Robot | [
"greedy"
] | null | null | Ivan has a robot which is situated on an infinite grid. Initially the robot is standing in the starting cell (0,<=0). The robot can process commands. There are four types of commands it can perform:
- U β move from the cell (*x*,<=*y*) to (*x*,<=*y*<=+<=1); - D β move from (*x*,<=*y*) to (*x*,<=*y*<=-<=1); - L β move from (*x*,<=*y*) to (*x*<=-<=1,<=*y*); - R β move from (*x*,<=*y*) to (*x*<=+<=1,<=*y*).
Ivan entered a sequence of *n* commands, and the robot processed it. After this sequence the robot ended up in the starting cell (0,<=0), but Ivan doubts that the sequence is such that after performing it correctly the robot ends up in the same cell. He thinks that some commands were ignored by robot. To acknowledge whether the robot is severely bugged, he needs to calculate the maximum possible number of commands that were performed correctly. Help Ivan to do the calculations! | The first line contains one number *n* β the length of sequence of commands entered by Ivan (1<=β€<=*n*<=β€<=100).
The second line contains the sequence itself β a string consisting of *n* characters. Each character can be U, D, L or R. | Print the maximum possible number of commands from the sequence the robot could perform to end up in the starting cell. | [
"4\nLDUR\n",
"5\nRRRUU\n",
"6\nLLRRRR\n"
] | [
"4\n",
"0\n",
"4\n"
] | none | [
{
"input": "4\nLDUR",
"output": "4"
},
{
"input": "5\nRRRUU",
"output": "0"
},
{
"input": "6\nLLRRRR",
"output": "4"
},
{
"input": "88\nLLUUULRDRRURDDLURRLRDRLLRULRUUDDLLLLRRDDURDURRLDURRLDRRRUULDDLRRRDDRRLUULLURDURUDDDDDLDR",
"output": "76"
},
{
"input": "89\nLDL... | 62 | 5,632,000 | 0 | 3,848 | |
363 | Soroban | [
"implementation"
] | null | null | You know that Japan is the country with almost the largest 'electronic devices per person' ratio. So you might be quite surprised to find out that the primary school in Japan teaches to count using a Soroban β an abacus developed in Japan. This phenomenon has its reasons, of course, but we are not going to speak about them. Let's have a look at the Soroban's construction.
Soroban consists of some number of rods, each rod contains five beads. We will assume that the rods are horizontal lines. One bead on each rod (the leftmost one) is divided from the others by a bar (the reckoning bar). This single bead is called go-dama and four others are ichi-damas. Each rod is responsible for representing a single digit from 0 to 9. We can obtain the value of a digit by following simple algorithm:
- Set the value of a digit equal to 0. - If the go-dama is shifted to the right, add 5. - Add the number of ichi-damas shifted to the left.
Thus, the upper rod on the picture shows digit 0, the middle one shows digit 2 and the lower one shows 7. We will consider the top rod to represent the last decimal digit of a number, so the picture shows number 720.
Write the program that prints the way Soroban shows the given number *n*. | The first line contains a single integer *n* (0<=β€<=*n*<=<<=109). | Print the description of the decimal digits of number *n* from the last one to the first one (as mentioned on the picture in the statement), one per line. Print the beads as large English letters 'O', rod pieces as character '-' and the reckoning bar as '|'. Print as many rods, as many digits are in the decimal representation of number *n* without leading zeroes. We can assume that number 0 has no leading zeroes. | [
"2\n",
"13\n",
"720\n"
] | [
"O-|OO-OO\n",
"O-|OOO-O\nO-|O-OOO\n",
"O-|-OOOO\nO-|OO-OO\n-O|OO-OO\n"
] | none | [
{
"input": "2",
"output": "O-|OO-OO"
},
{
"input": "13",
"output": "O-|OOO-O\nO-|O-OOO"
},
{
"input": "720",
"output": "O-|-OOOO\nO-|OO-OO\n-O|OO-OO"
},
{
"input": "0",
"output": "O-|-OOOO"
},
{
"input": "1",
"output": "O-|O-OOO"
},
{
"input": "3",
... | 62 | 0 | 3 | 3,853 | |
633 | A Trivial Problem | [
"brute force",
"constructive algorithms",
"math",
"number theory"
] | null | null | Mr. Santa asks all the great programmers of the world to solve a trivial problem. He gives them an integer *m* and asks for the number of positive integers *n*, such that the factorial of *n* ends with exactly *m* zeroes. Are you among those great programmers who can solve this problem? | The only line of input contains an integer *m* (1<=β€<=*m*<=β€<=100<=000)Β β the required number of trailing zeroes in factorial. | First print *k*Β β the number of values of *n* such that the factorial of *n* ends with *m* zeroes. Then print these *k* integers in increasing order. | [
"1\n",
"5\n"
] | [
"5\n5 6 7 8 9 ",
"0"
] | The factorial of *n* is equal to the product of all integers from 1 to *n* inclusive, that is *n*!β=β1Β·2Β·3Β·...Β·*n*.
In the first sample, 5!β=β120, 6!β=β720, 7!β=β5040, 8!β=β40320 and 9!β=β362880. | [
{
"input": "1",
"output": "5\n5 6 7 8 9 "
},
{
"input": "5",
"output": "0"
},
{
"input": "2",
"output": "5\n10 11 12 13 14 "
},
{
"input": "3",
"output": "5\n15 16 17 18 19 "
},
{
"input": "7",
"output": "5\n30 31 32 33 34 "
},
{
"input": "12",
"ou... | 124 | 0 | 0 | 3,860 | |
656 | Scrambled | [
"*special",
"implementation"
] | null | null | Btoh yuo adn yuor roomatme lhoate wianshg disehs, btu stlil sdmoeboy msut peorrfm tihs cohre dialy. Oen dya yuo decdie to idourtcne smoe syestm. Yuor rmmotaoe sstgegus teh fooniwllg dael. Yuo argee on tow arayrs of ientgres M adn R, nmebur upmicnog dyas (induiclng teh cunrret oen) wtih sicsescuve irnegets (teh ceurrnt dya is zreo), adn yuo wsah teh diehss on dya D if adn olny if terhe etsixs an iednx i scuh taht *D*Β *mod*Β *M*[*i*]<==<=*R*[*i*], otwsehrie yuor rmootmae deos it. Yuo lkie teh cncepot, btu yuor rmotaome's cuinnng simle meaks yuo ssecupt sthnoemig, so yuo itennd to vefriy teh fnerisas of teh aemnrgeet.
Yuo aer geivn ayarrs M adn R. Cuaclatle teh pceanregte of dyas on wchih yuo edn up dnoig teh wisahng. Amsuse taht yuo hvae iiiftlneny mnay dyas aehad of yuo. | The first line of input contains a single integer N (1<=β€<=*N*<=β€<=16).
The second and third lines of input contain N integers each, all between 0 and 16, inclusive, and represent arrays M and R, respectively. All *M*[*i*] are positive, for each *i* *R*[*i*]<=<<=*M*[*i*]. | Output a single real number. The answer is considered to be correct if its absolute or relative error does not exceed 10<=-<=4. | [
"1\n2\n0\n",
"2\n2 3\n1 0\n"
] | [
"0.500000\n",
"0.666667\n"
] | none | [
{
"input": "1\n2\n0",
"output": "0.500000"
},
{
"input": "2\n2 3\n1 0",
"output": "0.666667"
},
{
"input": "3\n2 4 4\n0 1 3",
"output": "1.000000"
},
{
"input": "1\n16\n15",
"output": "0.062500"
},
{
"input": "16\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16\n0 1 2 3 4 ... | 77 | 5,836,800 | 0 | 3,881 | |
920 | Connected Components? | [
"data structures",
"dfs and similar",
"dsu",
"graphs"
] | null | null | You are given an undirected graph consisting of *n* vertices and edges. Instead of giving you the edges that exist in the graph, we give you *m* unordered pairs (*x*,<=*y*) such that there is no edge between *x* and *y*, and if some pair of vertices is not listed in the input, then there is an edge between these vertices.
You have to find the number of connected components in the graph and the size of each component. A connected component is a set of vertices *X* such that for every two vertices from this set there exists at least one path in the graph connecting these vertices, but adding any other vertex to *X* violates this rule. | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=200000, ).
Then *m* lines follow, each containing a pair of integers *x* and *y* (1<=β€<=*x*,<=*y*<=β€<=*n*, *x*<=β <=*y*) denoting that there is no edge between *x* and *y*. Each pair is listed at most once; (*x*,<=*y*) and (*y*,<=*x*) are considered the same (so they are never listed in the same test). If some pair of vertices is not listed in the input, then there exists an edge between those vertices. | Firstly print *k* β the number of connected components in this graph.
Then print *k* integers β the sizes of components. You should output these integers in non-descending order. | [
"5 5\n1 2\n3 4\n3 2\n4 2\n2 5\n"
] | [
"2\n1 4 "
] | none | [
{
"input": "5 5\n1 2\n3 4\n3 2\n4 2\n2 5",
"output": "2\n1 4 "
},
{
"input": "8 15\n2 1\n4 5\n2 4\n3 4\n2 5\n3 5\n2 6\n3 6\n5 6\n4 6\n2 7\n3 8\n2 8\n3 7\n6 7",
"output": "1\n8 "
},
{
"input": "12 58\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 10\n1 11\n1 12\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n... | 561 | 54,272,000 | 3 | 3,884 | |
98 | Help King | [
"implementation",
"probabilities",
"trees"
] | B. Help King | 2 | 256 | This is the modification of the problem used during the official round. Unfortunately, author's solution of the original problem appeared wrong, so the problem was changed specially for the archive.
Once upon a time in a far away kingdom lived the King. The King had a beautiful daughter, Victoria. They lived happily, but not happily ever after: one day a vicious dragon attacked the kingdom and stole Victoria. The King was full of grief, yet he gathered his noble knights and promised half of his kingdom and Victoria's hand in marriage to the one who will save the girl from the infernal beast.
Having travelled for some time, the knights found the dragon's lair and all of them rushed there to save Victoria. Each knight spat on the dragon once and, as the dragon had quite a fragile and frail heart, his heart broke and poor beast died. As for the noble knights, they got Victoria right to the King and started brawling as each one wanted the girl's hand in marriage.
The problem was that all the noble knights were equally noble and equally handsome, and Victoria didn't want to marry any of them anyway. Then the King (and he was a very wise man and didn't want to hurt anybody's feelings) decided to find out who will get his daughter randomly, i.e. tossing a coin. However, there turned out to be *n* noble knights and the coin only has two sides. The good thing is that when a coin is tossed, the coin falls on each side with equal probability. The King got interested how to pick one noble knight using this coin so that all knights had equal probability of being chosen (the probability in that case should always be equal to 1<=/<=*n*). First the King wants to know the expected number of times he will need to toss a coin to determine the winner. Besides, while tossing the coin, the King should follow the optimal tossing strategy (i.e. the strategy that minimizes the expected number of tosses). Help the King in this challenging task. | The first line contains a single integer *n* from the problem's statement (1<=β€<=*n*<=β€<=10000). | Print the sought expected number of tosses as an irreducible fraction in the following form: "*a*/*b*" (without the quotes) without leading zeroes. | [
"2\n",
"3\n",
"4\n"
] | [
"1/1\n",
"8/3\n",
"2/1\n"
] | none | [
{
"input": "2",
"output": "1/1"
},
{
"input": "3",
"output": "8/3"
},
{
"input": "4",
"output": "2/1"
},
{
"input": "8",
"output": "3/1"
},
{
"input": "7",
"output": "24/7"
},
{
"input": "6",
"output": "11/3"
},
{
"input": "1",
"output"... | 30 | 0 | 0 | 3,887 |
237 | Build String | [
"flows",
"graphs"
] | null | null | You desperately need to build some string *t*. For that you've got *n* more strings *s*1,<=*s*2,<=...,<=*s**n*. To build string *t*, you are allowed to perform exactly |*t*| (|*t*| is the length of string *t*) operations on these strings. Each operation looks like that:
1. choose any non-empty string from strings *s*1,<=*s*2,<=...,<=*s**n*; 1. choose an arbitrary character from the chosen string and write it on a piece of paper; 1. remove the chosen character from the chosen string.
Note that after you perform the described operation, the total number of characters in strings *s*1,<=*s*2,<=...,<=*s**n* decreases by 1. We are assumed to build string *t*, if the characters, written on the piece of paper, in the order of performed operations form string *t*.
There are other limitations, though. For each string *s**i* you know number *a**i* β the maximum number of characters you are allowed to delete from string *s**i*. You also know that each operation that results in deleting a character from string *s**i*, costs *i* rubles. That is, an operation on string *s*1 is the cheapest (it costs 1 ruble), and the operation on string *s**n* is the most expensive one (it costs *n* rubles).
Your task is to count the minimum amount of money (in rubles) you will need to build string *t* by the given rules. Consider the cost of building string *t* to be the sum of prices of the operations you use. | The first line of the input contains string *t* β the string that you need to build.
The second line contains a single integer *n* (1<=β€<=*n*<=β€<=100) β the number of strings to which you are allowed to apply the described operation. Each of the next *n* lines contains a string and an integer. The *i*-th line contains space-separated string *s**i* and integer *a**i* (0<=β€<=*a**i*<=β€<=100). Number *a**i* represents the maximum number of characters that can be deleted from string *s**i*.
All strings in the input only consist of lowercase English letters. All strings are non-empty. The lengths of all strings do not exceed 100 characters. | Print a single number β the minimum money (in rubles) you need in order to build string *t*. If there is no solution, print -1. | [
"bbaze\n3\nbzb 2\naeb 3\nba 10\n",
"abacaba\n4\naba 2\nbcc 1\ncaa 2\nbbb 5\n",
"xyz\n4\naxx 8\nza 1\nefg 4\nt 1\n"
] | [
"8\n",
"18\n",
"-1\n"
] | Notes to the samples:
In the first sample from the first string you should take characters "b" and "z" with price 1 ruble, from the second string characters "a", "e" ΠΈ "b" with price 2 rubles. The price of the string *t* in this case is 2Β·1β+β3Β·2β=β8.
In the second sample from the first string you should take two characters "a" with price 1 ruble, from the second string character "c" with price 2 rubles, from the third string two characters "a" with price 3 rubles, from the fourth string two characters "b" with price 4 rubles. The price of the string *t* in this case is 2Β·1β+β1Β·2β+β2Β·3β+β2Β·4β=β18.
In the third sample the solution doesn't exist because there is no character "y" in given strings. | [
{
"input": "bbaze\n3\nbzb 2\naeb 3\nba 10",
"output": "8"
},
{
"input": "abacaba\n4\naba 2\nbcc 1\ncaa 2\nbbb 5",
"output": "18"
},
{
"input": "xyz\n4\naxx 8\nza 1\nefg 4\nt 1",
"output": "-1"
},
{
"input": "aaabbtttefg\n6\nabbbca 3\nffatgg 2\nyioa 4\nppaeg 2\naetgffff 4\ntre... | 92 | 0 | 0 | 3,892 | |
982 | Billiard | [
"geometry",
"number theory"
] | null | null | Consider a [billiard table](https://en.wikipedia.org/wiki/Billiard_table) of rectangular size $n \times m$ with four pockets. Let's introduce a coordinate system with the origin at the lower left corner (see the picture).
There is one ball at the point $(x, y)$ currently. Max comes to the table and strikes the ball. The ball starts moving along a line that is parallel to one of the axes or that makes a $45^{\circ}$ angle with them. We will assume that:
1. the angles between the directions of the ball before and after a collision with a side are equal, 1. the ball moves indefinitely long, it only stops when it falls into a pocket, 1. the ball can be considered as a point, it falls into a pocket if and only if its coordinates coincide with one of the pockets, 1. initially the ball is not in a pocket.
Note that the ball can move along some side, in this case the ball will just fall into the pocket at the end of the side.
Your task is to determine whether the ball will fall into a pocket eventually, and if yes, which of the four pockets it will be. | The only line contains $6$ integers $n$, $m$, $x$, $y$, $v_x$, $v_y$ ($1 \leq n, m \leq 10^9$, $0 \leq x \leq n$; $0 \leq y \leq m$; $-1 \leq v_x, v_y \leq 1$; $(v_x, v_y) \neq (0, 0)$)Β β the width of the table, the length of the table, the $x$-coordinate of the initial position of the ball, the $y$-coordinate of the initial position of the ball, the $x$-component of its initial speed and the $y$-component of its initial speed, respectively. It is guaranteed that the ball is not initially in a pocket. | Print the coordinates of the pocket the ball will fall into, or $-1$ if the ball will move indefinitely. | [
"4 3 2 2 -1 1\n",
"4 4 2 0 1 1\n",
"10 10 10 1 -1 0\n"
] | [
"0 0",
"-1",
"-1"
] | The first sample:
The second sample:
In the third sample the ball will never change its $y$ coordinate, so the ball will never fall into a pocket. | [
{
"input": "4 3 2 2 -1 1",
"output": "0 0"
},
{
"input": "4 4 2 0 1 1",
"output": "-1"
},
{
"input": "10 10 10 1 -1 0",
"output": "-1"
},
{
"input": "1000000000 1000000000 1 1000000000 0 1",
"output": "-1"
},
{
"input": "2 1 1 0 -1 -1",
"output": "0 1"
},
... | 78 | 307,200 | 0 | 3,901 | |
156 | Message | [
"brute force"
] | null | null | Dr. Moriarty is about to send a message to Sherlock Holmes. He has a string *s*.
String *p* is called a substring of string *s* if you can read it starting from some position in the string *s*. For example, string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba".
Dr. Moriarty plans to take string *s* and cut out some substring from it, let's call it *t*. Then he needs to change the substring *t* zero or more times. As a result, he should obtain a fixed string *u* (which is the string that should be sent to Sherlock Holmes). One change is defined as making one of the following actions:
- Insert one letter to any end of the string. - Delete one letter from any end of the string. - Change one letter into any other one.
Moriarty is very smart and after he chooses some substring *t*, he always makes the minimal number of changes to obtain *u*.
Help Moriarty choose the best substring *t* from all substrings of the string *s*. The substring *t* should minimize the number of changes Moriarty should make to obtain the string *u* from it. | The first line contains a non-empty string *s*, consisting of lowercase Latin letters. The second line contains a non-empty string *u*, consisting of lowercase Latin letters. The lengths of both strings are in the range from 1 to 2000, inclusive. | Print the only integer β the minimum number of changes that Dr. Moriarty has to make with the string that you choose. | [
"aaaaa\naaa\n",
"abcabc\nbcd\n",
"abcdef\nklmnopq\n"
] | [
"0\n",
"1\n",
"7\n"
] | In the first sample Moriarty can take any substring of length 3, and it will be equal to the required message *u*, so Moriarty won't have to make any changes.
In the second sample you should take a substring consisting of characters from second to fourth ("bca") or from fifth to sixth ("bc"). Then you will only have to make one change: to change or to add the last character.
In the third sample the initial string *s* doesn't contain any character that the message should contain, so, whatever string you choose, you will have to make at least 7 changes to obtain the required message. | [
{
"input": "aaaaa\naaa",
"output": "0"
},
{
"input": "abcabc\nbcd",
"output": "1"
},
{
"input": "abcdef\nklmnopq",
"output": "7"
},
{
"input": "aaabbbaaa\naba",
"output": "1"
},
{
"input": "a\na",
"output": "0"
},
{
"input": "z\nz",
"output": "0"
... | 342 | 2,048,000 | 3 | 3,903 | |
191 | Dynasty Puzzles | [
"dp"
] | null | null | The ancient Berlanders believed that the longer the name, the more important its bearer is. Thus, Berland kings were famous for their long names. But long names are somewhat inconvenient, so the Berlanders started to abbreviate the names of their kings. They called every king by the first letters of its name. Thus, the king, whose name was Victorious Vasily Pupkin, was always called by the berlanders VVP.
In Berland over its long history many dynasties of kings replaced each other, but they were all united by common traditions. Thus, according to one Berland traditions, to maintain stability in the country, the first name of the heir should be the same as the last name his predecessor (hence, the first letter of the abbreviated name of the heir coincides with the last letter of the abbreviated name of the predecessor). Berlanders appreciate stability, so this tradition has never been broken. Also Berlanders like perfection, so another tradition requires that the first name of the first king in the dynasty coincides with the last name of the last king in this dynasty (hence, the first letter of the abbreviated name of the first king coincides with the last letter of the abbreviated name of the last king). This tradition, of course, has also been always observed.
The name of a dynasty is formed by very simple rules: we take all the short names of the kings in the order in which they ruled, and write them in one line. Thus, a dynasty of kings "ab" and "ba" is called "abba", and the dynasty, which had only the king "abca", is called "abca".
Vasya, a historian, has recently found a list of abbreviated names of all Berland kings and their relatives. Help Vasya to find the maximally long name of the dynasty that could have existed in Berland.
Note that in his list all the names are ordered by the time, that is, if name *A* is earlier in the list than *B*, then if *A* and *B* were kings, then king *A* ruled before king *B*. | The first line contains integer *n* (1<=β€<=*n*<=β€<=5Β·105) β the number of names in Vasya's list. Next *n* lines contain *n* abbreviated names, one per line. An abbreviated name is a non-empty sequence of lowercase Latin letters. Its length does not exceed 10 characters. | Print a single number β length of the sought dynasty's name in letters.
If Vasya's list is wrong and no dynasty can be found there, print a single number 0. | [
"3\nabc\nca\ncba\n",
"4\nvvp\nvvp\ndam\nvvp\n",
"3\nab\nc\ndef\n"
] | [
"6\n",
"0\n",
"1\n"
] | In the first sample two dynasties can exist: the one called "abcca" (with the first and second kings) and the one called "abccba" (with the first and third kings).
In the second sample there aren't acceptable dynasties.
The only dynasty in the third sample consists of one king, his name is "c". | [
{
"input": "3\nabc\nca\ncba",
"output": "6"
},
{
"input": "4\nvvp\nvvp\ndam\nvvp",
"output": "0"
},
{
"input": "3\nab\nc\ndef",
"output": "1"
},
{
"input": "5\nab\nbc\ncd\nde\nffffffffff",
"output": "10"
},
{
"input": "5\ncab\nbbc\ncaa\nccc\naca",
"output": "9... | 2,000 | 29,388,800 | 0 | 3,908 | |
952 | I'm Feeling Lucky! | [
"probabilities"
] | null | null | You have one chip and one chance to play roulette. Are you feeling lucky? | none | Print your bet. Your chip must be placed entirely within some square (not on an edge or a corner shared by adjacent squares). | [] | [] | none | [
{
"input": "1",
"output": "Red"
},
{
"input": "2",
"output": "Red"
}
] | 77 | 0 | 0 | 3,909 | |
84 | Biathlon | [
"binary search",
"implementation"
] | C. Biathlon | 1 | 256 | Perhaps many have heard that the World Biathlon Championship has finished. Although our hero Valera was not present at this spectacular event himself and only watched it on TV, it excited him so much that he decided to enroll in a biathlon section.
Of course, biathlon as any sport, proved very difficult in practice. It takes much time and effort. Workouts, workouts, and workouts, β that's what awaited Valera on his way to great achievements in biathlon.
As for the workouts, you all probably know that every professional biathlete should ski fast and shoot precisely at the shooting range. Only in this case you can hope to be successful, because running and shooting are the two main components of biathlon. Valera has been diligent in his ski trainings, which is why he runs really fast, however, his shooting accuracy is nothing to write home about.
On a biathlon base where Valera is preparing for the competition, there is a huge rifle range with *n* targets. Each target have shape of a circle, and the center of each circle is located on the *Ox* axis. At the last training session Valera made the total of *m* shots. To make monitoring of his own results easier for him, one rather well-known programmer (of course it is you) was commissioned to write a program that would reveal how many and which targets Valera hit. More specifically, for each target the program must print the number of the first successful shot (in the target), or "-1" if this was not hit. The target is considered hit if the shot is inside the circle or on its boundary. Valera is counting on you and perhaps, thanks to you he will one day win international competitions. | The first line of the input file contains the integer *n* (1<=β€<=*n*<=β€<=104), which is the number of targets. The next *n* lines contain descriptions of the targets. Each target is a circle whose center is located on the *Ox* axis. Each circle is given by its coordinate of the center *x* (<=-<=2Β·104<=β€<=*x*<=β€<=2Β·104) and its radius *r* (1<=β€<=*r*<=β€<=1000). It is guaranteed that no two targets coincide, intersect or are nested into each other, but they can touch each other.
The next line contains integer *m* (1<=β€<=*m*<=β€<=2Β·105), which is the number of shots. Next *m* lines contain descriptions of the shots, which are points on the plane, given by their coordinates *x* and *y* (<=-<=2Β·104<=β€<=*x*,<=*y*<=β€<=2Β·104).
All the numbers in the input are integers.
Targets and shots are numbered starting from one in the order of the input. | Print on the first line a single number, the number of targets hit by Valera. Print on the second line for each of the targets the number of its first hit or "-1" (without quotes) if this number does not exist. Separate numbers with spaces. | [
"3\n2 1\n5 2\n10 1\n5\n0 1\n1 3\n3 0\n4 0\n4 0\n",
"3\n3 2\n7 1\n11 2\n4\n2 1\n6 0\n6 4\n11 2\n"
] | [
"2\n3 3 -1 \n",
"3\n1 2 4 \n"
] | none | [
{
"input": "3\n2 1\n5 2\n10 1\n5\n0 1\n1 3\n3 0\n4 0\n4 0",
"output": "2\n3 3 -1 "
},
{
"input": "3\n3 2\n7 1\n11 2\n4\n2 1\n6 0\n6 4\n11 2",
"output": "3\n1 2 4 "
},
{
"input": "2\n0 5\n10 5\n2\n7 2\n6 1",
"output": "1\n-1 1 "
},
{
"input": "3\n-3 3\n-10 2\n10 2\n4\n10 2\n2 ... | 623 | 27,340,800 | 3.637574 | 3,913 |
196 | Lexicographically Maximum Subsequence | [
"greedy",
"strings"
] | null | null | You've got string *s*, consisting of only lowercase English letters. Find its lexicographically maximum subsequence.
We'll call a non-empty string *s*[*p*1*p*2... *p**k*]<==<=*s**p*1*s**p*2... *s**p**k*(1<=β€<=*p*1<=<<=*p*2<=<<=...<=<<=*p**k*<=β€<=|*s*|) a subsequence of string *s*<==<=*s*1*s*2... *s*|*s*|.
String *x*<==<=*x*1*x*2... *x*|*x*| is lexicographically larger than string *y*<==<=*y*1*y*2... *y*|*y*|, if either |*x*|<=><=|*y*| and *x*1<==<=*y*1,<=*x*2<==<=*y*2,<=... ,<=*x*|*y*|<==<=*y*|*y*|, or exists such number *r* (*r*<=<<=|*x*|,<=*r*<=<<=|*y*|), that *x*1<==<=*y*1,<=*x*2<==<=*y*2,<=... ,<=*x**r*<==<=*y**r* and *x**r*<=+<=1<=><=*y**r*<=+<=1. Characters in lines are compared like their ASCII codes. | The single line contains a non-empty string *s*, consisting only of lowercase English letters. The string's length doesn't exceed 105. | Print the lexicographically maximum subsequence of string *s*. | [
"ababba\n",
"abbcbccacbbcbaaba\n"
] | [
"bbba\n",
"cccccbba\n"
] | Let's look at samples and see what the sought subsequences look like (they are marked with uppercase bold letters).
The first sample: aBaBBA
The second sample: abbCbCCaCbbCBaaBA | [
{
"input": "ababba",
"output": "bbba"
},
{
"input": "abbcbccacbbcbaaba",
"output": "cccccbba"
},
{
"input": "thankstosamarasauteddybearsforthiscontest",
"output": "yttt"
},
{
"input": "cantouristsolveitlessthaninoneminute",
"output": "vute"
},
{
"input": "areprete... | 1,308 | 614,400 | 3 | 3,922 | |
9 | Running Student | [
"brute force",
"geometry",
"implementation"
] | B. Running Student | 1 | 64 | And again a misfortune fell on Poor Student. He is being late for an exam.
Having rushed to a bus stop that is in point (0,<=0), he got on a minibus and they drove along a straight line, parallel to axis *OX*, in the direction of increasing *x*.
Poor Student knows the following:
- during one run the minibus makes *n* stops, the *i*-th stop is in point (*x**i*,<=0) - coordinates of all the stops are different - the minibus drives at a constant speed, equal to *v**b* - it can be assumed the passengers get on and off the minibus at a bus stop momentarily - Student can get off the minibus only at a bus stop - Student will have to get off the minibus at a terminal stop, if he does not get off earlier - the University, where the exam will be held, is in point (*x**u*,<=*y**u*) - Student can run from a bus stop to the University at a constant speed *v**s* as long as needed - a distance between two points can be calculated according to the following formula: - Student is already on the minibus, so, he cannot get off at the first bus stop
Poor Student wants to get to the University as soon as possible. Help him to choose the bus stop, where he should get off. If such bus stops are multiple, choose the bus stop closest to the University. | The first line contains three integer numbers: 2<=β€<=*n*<=β€<=100, 1<=β€<=*v**b*,<=*v**s*<=β€<=1000. The second line contains *n* non-negative integers in ascending order: coordinates *x**i* of the bus stop with index *i*. It is guaranteed that *x*1 equals to zero, and *x**n*<=β€<=105. The third line contains the coordinates of the University, integers *x**u* and *y**u*, not exceeding 105 in absolute value. | In the only line output the answer to the problem β index of the optimum bus stop. | [
"4 5 2\n0 2 4 6\n4 1\n",
"2 1 1\n0 100000\n100000 100000\n"
] | [
"3",
"2"
] | As you know, students are a special sort of people, and minibuses usually do not hurry. That's why you should not be surprised, if Student's speed is higher than the speed of the minibus. | [
{
"input": "4 5 2\n0 2 4 6\n4 1",
"output": "3"
},
{
"input": "2 1 1\n0 100000\n100000 100000",
"output": "2"
},
{
"input": "6 5 1\n0 1 2 3 4 5\n0 0",
"output": "2"
},
{
"input": "4 100 10\n0 118 121 178\n220 220",
"output": "4"
},
{
"input": "4 3 3\n0 6 8 10\n7 -... | 218 | 307,200 | 3.888711 | 3,925 |
149 | Coloring Brackets | [
"dp"
] | null | null | Once Petya read a problem about a bracket sequence. He gave it much thought but didn't find a solution. Today you will face it.
You are given string *s*. It represents a correct bracket sequence. A correct bracket sequence is the sequence of opening ("(") and closing (")") brackets, such that it is possible to obtain a correct mathematical expression from it, inserting numbers and operators between the brackets. For example, such sequences as "(())()" and "()" are correct bracket sequences and such sequences as ")()" and "(()" are not.
In a correct bracket sequence each bracket corresponds to the matching bracket (an opening bracket corresponds to the matching closing bracket and vice versa). For example, in a bracket sequence shown of the figure below, the third bracket corresponds to the matching sixth one and the fifth bracket corresponds to the fourth one.
You are allowed to color some brackets in the bracket sequence so as all three conditions are fulfilled:
- Each bracket is either not colored any color, or is colored red, or is colored blue. - For any pair of matching brackets exactly one of them is colored. In other words, for any bracket the following is true: either it or the matching bracket that corresponds to it is colored. - No two neighboring colored brackets have the same color.
Find the number of different ways to color the bracket sequence. The ways should meet the above-given conditions. Two ways of coloring are considered different if they differ in the color of at least one bracket. As the result can be quite large, print it modulo 1000000007 (109<=+<=7). | The first line contains the single string *s* (2<=β€<=|*s*|<=β€<=700) which represents a correct bracket sequence. | Print the only number β the number of ways to color the bracket sequence that meet the above given conditions modulo 1000000007 (109<=+<=7). | [
"(())\n",
"(()())\n",
"()\n"
] | [
"12\n",
"40\n",
"4\n"
] | Let's consider the first sample test. The bracket sequence from the sample can be colored, for example, as is shown on two figures below.
The two ways of coloring shown below are incorrect. | [
{
"input": "(())",
"output": "12"
},
{
"input": "(()())",
"output": "40"
},
{
"input": "()",
"output": "4"
},
{
"input": "((()))",
"output": "36"
},
{
"input": "()(())",
"output": "42"
},
{
"input": "()()()",
"output": "48"
},
{
"input": "(... | 62 | 0 | 0 | 3,929 | |
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... | 140 | 1,433,600 | 0 | 3,957 | |
667 | Pouring Rain | [
"geometry",
"math"
] | null | null | A lot of people in Berland hates rain, but you do not. Rain pacifies, puts your thoughts in order. By these years you have developed a good tradition β when it rains, you go on the street and stay silent for a moment, contemplate all around you, enjoy freshness, think about big deeds you have to do.
Today everything had changed quietly. You went on the street with a cup contained water, your favorite drink. In a moment when you were drinking a water you noticed that the process became quite long: the cup still contained water because of rain. You decided to make a formal model of what was happening and to find if it was possible to drink all water in that situation.
Thus, your cup is a cylinder with diameter equals *d* centimeters. Initial level of water in cup equals *h* centimeters from the bottom.
You drink a water with a speed equals *v* milliliters per second. But rain goes with such speed that if you do not drink a water from the cup, the level of water increases on *e* centimeters per second. The process of drinking water from the cup and the addition of rain to the cup goes evenly and continuously.
Find the time needed to make the cup empty or find that it will never happen. It is guaranteed that if it is possible to drink all water, it will happen not later than after 104 seconds.
Note one milliliter equals to one cubic centimeter. | The only line of the input contains four integer numbers *d*,<=*h*,<=*v*,<=*e* (1<=β€<=*d*,<=*h*,<=*v*,<=*e*<=β€<=104), where:
- *d* β the diameter of your cylindrical cup, - *h* β the initial level of water in the cup, - *v* β the speed of drinking process from the cup in milliliters per second, - *e* β the growth of water because of rain if you do not drink from the cup. | If it is impossible to make the cup empty, print "NO" (without quotes).
Otherwise print "YES" (without quotes) in the first line. In the second line print a real number β time in seconds needed the cup will be empty. The answer will be considered correct if its relative or absolute error doesn't exceed 10<=-<=4. It is guaranteed that if the answer exists, it doesn't exceed 104. | [
"1 2 3 100\n",
"1 1 1 1\n"
] | [
"NO\n",
"YES\n3.659792366325\n"
] | In the first example the water fills the cup faster than you can drink from it.
In the second example area of the cup's bottom equals to <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/419dc74dcd7bc392019c9fe748fe1fdb08ab521a.png" style="max-width: 100.0%;max-height: 100.0%;"/>, thus we can conclude that you decrease the level of water by <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e8edb237e1f805fe83c2f47e48d3a9d03f2ee304.png" style="max-width: 100.0%;max-height: 100.0%;"/> centimeters per second. At the same time water level increases by 1 centimeter per second due to rain. Thus, cup will be empty in <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/9dae615d7e2c5c7c03cb478848fb06aba1a8942e.png" style="max-width: 100.0%;max-height: 100.0%;"/> seconds. | [
{
"input": "1 2 3 100",
"output": "NO"
},
{
"input": "1 1 1 1",
"output": "YES\n3.659792366325"
},
{
"input": "48 7946 7992 72",
"output": "NO"
},
{
"input": "72 6791 8546 46",
"output": "NO"
},
{
"input": "100 5635 9099 23",
"output": "NO"
},
{
"input... | 46 | 0 | 3 | 3,961 | |
978 | File Name | [
"greedy",
"strings"
] | null | null | You can not just take the file and send it. When Polycarp trying to send a file in the social network "Codehorses", he encountered an unexpected problem. If the name of the file contains three or more "x" (lowercase Latin letters "x") in a row, the system considers that the file content does not correspond to the social network topic. In this case, the file is not sent and an error message is displayed.
Determine the minimum number of characters to remove from the file name so after that the name does not contain "xxx" as a substring. Print 0 if the file name does not initially contain a forbidden substring "xxx".
You can delete characters in arbitrary positions (not necessarily consecutive). If you delete a character, then the length of a string is reduced by $1$. For example, if you delete the character in the position $2$ from the string "exxxii", then the resulting string is "exxii". | The first line contains integer $n$ $(3 \le n \le 100)$ β the length of the file name.
The second line contains a string of length $n$ consisting of lowercase Latin letters only β the file name. | Print the minimum number of characters to remove from the file name so after that the name does not contain "xxx" as a substring. If initially the file name dost not contain a forbidden substring "xxx", print 0. | [
"6\nxxxiii\n",
"5\nxxoxx\n",
"10\nxxxxxxxxxx\n"
] | [
"1\n",
"0\n",
"8\n"
] | In the first example Polycarp tried to send a file with name contains number $33$, written in Roman numerals. But he can not just send the file, because it name contains three letters "x" in a row. To send the file he needs to remove any one of this letters. | [
{
"input": "6\nxxxiii",
"output": "1"
},
{
"input": "5\nxxoxx",
"output": "0"
},
{
"input": "10\nxxxxxxxxxx",
"output": "8"
},
{
"input": "100\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"output": "98"
},
{
... | 46 | 0 | 3 | 3,962 | |
62 | A Student's Dream | [
"greedy",
"math"
] | A. A Student's Dream | 2 | 256 | Statistics claims that students sleep no more than three hours a day. But even in the world of their dreams, while they are snoring peacefully, the sense of impending doom is still upon them.
A poor student is dreaming that he is sitting the mathematical analysis exam. And he is examined by the most formidable professor of all times, a three times Soviet Union Hero, a Noble Prize laureate in student expulsion, venerable Petr Palych.
The poor student couldn't answer a single question. Thus, instead of a large spacious office he is going to apply for a job to thorium mines. But wait a minute! Petr Palych decided to give the student the last chance! Yes, that is possible only in dreams.
So the professor began: "Once a Venusian girl and a Marsian boy met on the Earth and decided to take a walk holding hands. But the problem is the girl has *a**l* fingers on her left hand and *a**r* fingers on the right one. The boy correspondingly has *b**l* and *b**r* fingers. They can only feel comfortable when holding hands, when no pair of the girl's fingers will touch each other. That is, they are comfortable when between any two girl's fingers there is a boy's finger. And in addition, no three fingers of the boy should touch each other. Determine if they can hold hands so that the both were comfortable."
The boy any the girl don't care who goes to the left and who goes to the right. The difference is only that if the boy goes to the left of the girl, he will take her left hand with his right one, and if he goes to the right of the girl, then it is vice versa. | The first line contains two positive integers not exceeding 100. They are the number of fingers on the Venusian girl's left and right hand correspondingly. The second line contains two integers not exceeding 100. They are the number of fingers on the Marsian boy's left and right hands correspondingly. | Print YES or NO, that is, the answer to Petr Palych's question. | [
"5 1\n10 5\n",
"4 5\n3 3\n",
"1 2\n11 6\n"
] | [
"YES",
"YES",
"NO"
] | The boy and the girl don't really care who goes to the left. | [
{
"input": "5 1\n10 5",
"output": "YES"
},
{
"input": "4 5\n3 3",
"output": "YES"
},
{
"input": "1 2\n11 6",
"output": "NO"
},
{
"input": "1 1\n1 1",
"output": "YES"
},
{
"input": "2 2\n1 1",
"output": "YES"
},
{
"input": "3 3\n1 1",
"output": "NO"... | 92 | 0 | 0 | 3,969 |
886 | ACM ICPC | [
"brute force"
] | null | null | In a small but very proud high school it was decided to win ACM ICPC. This goal requires to compose as many teams of three as possible, but since there were only 6 students who wished to participate, the decision was to build exactly two teams.
After practice competition, participant number *i* got a score of *a**i*. Team score is defined as sum of scores of its participants. High school management is interested if it's possible to build two teams with equal scores. Your task is to answer that question. | The single line contains six integers *a*1,<=...,<=*a*6 (0<=β€<=*a**i*<=β€<=1000) β scores of the participants | Print "YES" (quotes for clarity), if it is possible to build teams with equal score, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES"). | [
"1 3 2 1 2 1\n",
"1 1 1 1 1 99\n"
] | [
"YES\n",
"NO\n"
] | In the first sample, first team can be composed of 1st, 2nd and 6th participant, second β of 3rd, 4th and 5th: team scores are 1β+β3β+β1β=β2β+β1β+β2β=β5.
In the second sample, score of participant number 6 is too high: his team score will be definitely greater. | [
{
"input": "1 3 2 1 2 1",
"output": "YES"
},
{
"input": "1 1 1 1 1 99",
"output": "NO"
},
{
"input": "1000 1000 1000 1000 1000 1000",
"output": "YES"
},
{
"input": "0 0 0 0 0 0",
"output": "YES"
},
{
"input": "633 609 369 704 573 416",
"output": "NO"
},
{
... | 62 | 5,632,000 | 0 | 3,970 | |
847 | Preparing for Merge Sort | [
"binary search",
"data structures"
] | null | null | Ivan has an array consisting of *n* different integers. He decided to reorder all elements in increasing order. Ivan loves merge sort so he decided to represent his array with one or several increasing sequences which he then plans to merge into one sorted array.
Ivan represent his array with increasing sequences with help of the following algorithm.
While there is at least one unused number in array Ivan repeats the following procedure:
- iterate through array from the left to the right; - Ivan only looks at unused numbers on current iteration; - if current number is the first unused number on this iteration or this number is greater than previous unused number on current iteration, then Ivan marks the number as used and writes it down.
For example, if Ivan's array looks like [1, 3, 2, 5, 4] then he will perform two iterations. On first iteration Ivan will use and write numbers [1, 3, 5], and on second one β [2, 4].
Write a program which helps Ivan and finds representation of the given array with one or several increasing sequences in accordance with algorithm described above. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=2Β·105) β the number of elements in Ivan's array.
The second line contains a sequence consisting of distinct integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109) β Ivan's array. | Print representation of the given array in the form of one or more increasing sequences in accordance with the algorithm described above. Each sequence must be printed on a new line. | [
"5\n1 3 2 5 4\n",
"4\n4 3 2 1\n",
"4\n10 30 50 101\n"
] | [
"1 3 5 \n2 4 \n",
"4 \n3 \n2 \n1 \n",
"10 30 50 101 \n"
] | none | [
{
"input": "5\n1 3 2 5 4",
"output": "1 3 5 \n2 4 "
},
{
"input": "4\n4 3 2 1",
"output": "4 \n3 \n2 \n1 "
},
{
"input": "4\n10 30 50 101",
"output": "10 30 50 101 "
},
{
"input": "1\n1",
"output": "1 "
},
{
"input": "1\n200000",
"output": "200000 "
},
{
... | 2,000 | 7,372,800 | 0 | 3,976 | |
896 | Willem, Chtholly and Seniorious | [
"data structures",
"probabilities"
] | null | null | β Willem...
β What's the matter?
β It seems that there's something wrong with Seniorious...
β I'll have a look...
Seniorious is made by linking special talismans in particular order.
After over 500 years, the carillon is now in bad condition, so Willem decides to examine it thoroughly.
Seniorious has *n* pieces of talisman. Willem puts them in a line, the *i*-th of which is an integer *a**i*.
In order to maintain it, Willem needs to perform *m* operations.
There are four types of operations:
- 1 *l* *r* *x*: For each *i* such that *l*<=β€<=*i*<=β€<=*r*, assign *a**i*<=+<=*x* to *a**i*.- 2 *l* *r* *x*: For each *i* such that *l*<=β€<=*i*<=β€<=*r*, assign *x* to *a**i*.- 3 *l* *r* *x*: Print the *x*-th smallest number in the index range [*l*,<=*r*], i.e. the element at the *x*-th position if all the elements *a**i* such that *l*<=β€<=*i*<=β€<=*r* are taken and sorted into an array of non-decreasing integers. It's guaranteed that 1<=β€<=*x*<=β€<=*r*<=-<=*l*<=+<=1.- 4 *l* *r* *x* *y*: Print the sum of the *x*-th power of *a**i* such that *l*<=β€<=*i*<=β€<=*r*, modulo *y*, i.e. . | The only line contains four integers *n*,<=*m*,<=*seed*,<=*v**max* (1<=β€<=*n*,<=*m*<=β€<=105,<=0<=β€<=*seed*<=<<=109<=+<=7,<=1<=β€<=*vmax*<=β€<=109).
The initial values and operations are generated using following pseudo code:
Here *op* is the type of the operation mentioned in the legend. | For each operation of types 3 or 4, output a line containing the answer. | [
"10 10 7 9\n",
"10 10 9 9\n"
] | [
"2\n1\n0\n3\n",
"1\n1\n3\n3\n"
] | In the first example, the initial array is {8,β9,β7,β2,β3,β1,β5,β6,β4,β8}.
The operations are:
- 2 6 7 9 - 1 3 10 8 - 4 4 6 2 4 - 1 4 5 8 - 2 1 7 1 - 4 7 9 4 4 - 1 2 7 9 - 4 5 8 1 1 - 2 5 7 5 - 4 3 10 8 5 | [
{
"input": "10 10 7 9",
"output": "2\n1\n0\n3"
},
{
"input": "10 10 9 9",
"output": "1\n1\n3\n3"
},
{
"input": "1000 1000 658073485 946088556",
"output": "375432604\n52885108\n732131239\n335583873\n375432604\n582199284\n235058938\n682619432\n62026709\n631048460\n51394660\n25596188\n2... | 0 | 0 | -1 | 3,977 | |
294 | Shaass and Lights | [
"combinatorics",
"number theory"
] | null | null | There are *n* lights aligned in a row. These lights are numbered 1 to *n* from left to right. Initially some of the lights are switched on. Shaass wants to switch all the lights on. At each step he can switch a light on (this light should be switched off at that moment) if there's at least one adjacent light which is already switched on.
He knows the initial state of lights and he's wondering how many different ways there exist to switch all the lights on. Please find the required number of ways modulo 1000000007Β (109<=+<=7). | The first line of the input contains two integers *n* and *m* where *n* is the number of lights in the sequence and *m* is the number of lights which are initially switched on, (1<=β€<=*n*<=β€<=1000,<=1<=β€<=*m*<=β€<=*n*). The second line contains *m* distinct integers, each between 1 to *n* inclusive, denoting the indices of lights which are initially switched on. | In the only line of the output print the number of different possible ways to switch on all the lights modulo 1000000007Β (109<=+<=7). | [
"3 1\n1\n",
"4 2\n1 4\n",
"11 2\n4 8\n"
] | [
"1\n",
"2\n",
"6720\n"
] | none | [
{
"input": "3 1\n1",
"output": "1"
},
{
"input": "4 2\n1 4",
"output": "2"
},
{
"input": "11 2\n4 8",
"output": "6720"
},
{
"input": "4 2\n1 3",
"output": "2"
},
{
"input": "4 4\n1 2 3 4",
"output": "1"
},
{
"input": "4 2\n1 3",
"output": "2"
},
... | 108 | 307,200 | 3 | 3,984 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.