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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
174 | Range Increments | [
"data structures",
"greedy"
] | null | null | Polycarpus is an amateur programmer. Now he is analyzing a friend's program. He has already found there the function rangeIncrement(l, r), that adds 1 to each element of some array *a* for all indexes in the segment [*l*,<=*r*]. In other words, this function does the following:
Polycarpus knows the state of the array *a* after a series of function calls. He wants to determine the minimum number of function calls that lead to such state. In addition, he wants to find what function calls are needed in this case. It is guaranteed that the required number of calls does not exceed 105.
Before calls of function rangeIncrement(l, r) all array elements equal zero. | The first input line contains a single integer *n* (1<=≤<=*n*<=≤<=105) — the length of the array *a*[1... *n*].
The second line contains its integer space-separated elements, *a*[1],<=*a*[2],<=...,<=*a*[*n*] (0<=≤<=*a*[*i*]<=≤<=105) after some series of function calls rangeIncrement(l, r).
It is guaranteed that at least one element of the array is positive. It is guaranteed that the answer contains no more than 105 calls of function rangeIncrement(l, r). | Print on the first line *t* — the minimum number of calls of function rangeIncrement(l, r), that lead to the array from the input data. It is guaranteed that this number will turn out not more than 105.
Then print *t* lines — the descriptions of function calls, one per line. Each line should contain two integers *l**i*,<=*r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*) — the arguments of the *i*-th call rangeIncrement(l, r). Calls can be applied in any order.
If there are multiple solutions, you are allowed to print any of them. | [
"6\n1 2 1 1 4 1\n",
"5\n1 0 1 0 1\n"
] | [
"5\n2 2\n5 5\n5 5\n5 5\n1 6\n",
"3\n1 1\n3 3\n5 5\n"
] | The first sample requires a call for the entire array, and four additional calls:
- one for the segment [2,2] (i.e. the second element of the array), - three for the segment [5,5] (i.e. the fifth element of the array). | [
{
"input": "6\n1 2 1 1 4 1",
"output": "5\n2 2\n5 5\n5 5\n5 5\n1 6"
},
{
"input": "5\n1 0 1 0 1",
"output": "3\n1 1\n3 3\n5 5"
},
{
"input": "1\n1",
"output": "1\n1 1"
},
{
"input": "1\n100000",
"output": "100000\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1... | 60 | 0 | 0 | 10,787 | |
281 | Nearest Fraction | [
"brute force",
"implementation",
"two pointers"
] | null | null | You are given three positive integers *x*,<=*y*,<=*n*. Your task is to find the nearest fraction to fraction whose denominator is no more than *n*.
Formally, you should find such pair of integers *a*,<=*b* (1<=≤<=*b*<=≤<=*n*; 0<=≤<=*a*) that the value is as minimal as possible.
If there are multiple "nearest" fractions, choose the one with the minimum denominator. If there are multiple "nearest" fractions with the minimum denominator, choose the one with the minimum numerator. | A single line contains three integers *x*,<=*y*,<=*n* (1<=≤<=*x*,<=*y*,<=*n*<=≤<=105). | Print the required fraction in the format "*a*/*b*" (without quotes). | [
"3 7 6\n",
"7 2 4\n"
] | [
"2/5\n",
"7/2\n"
] | none | [] | 60 | 0 | 0 | 10,831 | |
0 | none | [
"none"
] | null | null | Once Vasya and Petya assembled a figure of *m* cubes, each of them is associated with a number between 0 and *m*<=-<=1 (inclusive, each number appeared exactly once). Let's consider a coordinate system such that the *OX* is the ground, and the *OY* is directed upwards. Each cube is associated with the coordinates of its lower left corner, these coordinates are integers for each cube.
The figure turned out to be stable. This means that for any cube that is not on the ground, there is at least one cube under it such that those two cubes touch by a side or a corner. More formally, this means that for the cube with coordinates (*x*,<=*y*) either *y*<==<=0, or there is a cube with coordinates (*x*<=-<=1,<=*y*<=-<=1), (*x*,<=*y*<=-<=1) or (*x*<=+<=1,<=*y*<=-<=1).
Now the boys want to disassemble the figure and put all the cubes in a row. In one step the cube is removed from the figure and being put to the right of the blocks that have already been laid. The guys remove the cubes in such order that the figure remains stable. To make the process more interesting, the guys decided to play the following game. The guys take out the cubes from the figure in turns. It is easy to see that after the figure is disassembled, the integers written on the cubes form a number, written in the *m*-ary positional numerical system (possibly, with a leading zero). Vasya wants the resulting number to be maximum possible, and Petya, on the contrary, tries to make it as small as possible. Vasya starts the game.
Your task is to determine what number is formed after the figure is disassembled, if the boys play optimally. Determine the remainder of the answer modulo 109<=+<=9. | The first line contains number *m* (2<=≤<=*m*<=≤<=105).
The following *m* lines contain the coordinates of the cubes *x**i*,<=*y**i* (<=-<=109<=≤<=*x**i*<=≤<=109, 0<=≤<=*y**i*<=≤<=109) in ascending order of numbers written on them. It is guaranteed that the original figure is stable.
No two cubes occupy the same place. | In the only line print the answer to the problem. | [
"3\n2 1\n1 0\n0 1\n",
"5\n0 0\n0 1\n0 2\n0 3\n0 4\n"
] | [
"19\n",
"2930\n"
] | none | [
{
"input": "3\n2 1\n1 0\n0 1",
"output": "19"
},
{
"input": "5\n0 0\n0 1\n0 2\n0 3\n0 4",
"output": "2930"
},
{
"input": "10\n-1 2\n-3 0\n5 5\n4 4\n-2 1\n1 1\n3 3\n2 2\n0 0\n-1000000000 0",
"output": "41236677"
},
{
"input": "10\n-678318184 2\n-678318182 3\n580731357 2\n-6783... | 46 | 307,200 | 0 | 10,850 | |
958 | Death Stars (easy) | [
"implementation"
] | null | null | The stardate is 1977 and the science and art of detecting Death Stars is in its infancy. Princess Heidi has received information about the stars in the nearby solar system from the Rebel spies and now, to help her identify the exact location of the Death Star, she needs to know whether this information is correct.
Two rebel spies have provided her with the maps of the solar system. Each map is an *N*<=×<=*N* grid, where each cell is either occupied by a star or empty. To see whether the information is correct, Heidi needs to know whether the two maps are of the same solar system, or if possibly one of the spies is actually an Empire double agent, feeding her false information.
Unfortunately, spies may have accidentally rotated a map by 90, 180, or 270 degrees, or flipped it along the vertical or the horizontal axis, before delivering it to Heidi. If Heidi can rotate or flip the maps so that two of them become identical, then those maps are of the same solar system. Otherwise, there are traitors in the Rebel ranks! Help Heidi find out. | The first line of the input contains one number *N* (1<=≤<=*N*<=≤<=10) – the dimension of each map. Next *N* lines each contain *N* characters, depicting the first map: 'X' indicates a star, while 'O' indicates an empty quadrant of space. Next *N* lines each contain *N* characters, depicting the second map in the same format. | The only line of output should contain the word Yes if the maps are identical, or No if it is impossible to match them by performing rotations and translations. | [
"4\nXOOO\nXXOO\nOOOO\nXXXX\nXOOO\nXOOO\nXOXO\nXOXX\n",
"2\nXX\nOO\nXO\nOX\n"
] | [
"Yes\n",
"No\n"
] | In the first test, you can match the first map to the second map by first flipping the first map along the vertical axis, and then by rotating it 90 degrees clockwise. | [
{
"input": "4\nXOOO\nXXOO\nOOOO\nXXXX\nXOOO\nXOOO\nXOXO\nXOXX",
"output": "Yes"
},
{
"input": "2\nXX\nOO\nXO\nOX",
"output": "No"
},
{
"input": "1\nO\nO",
"output": "Yes"
},
{
"input": "1\nX\nO",
"output": "No"
},
{
"input": "2\nOX\nXX\nOX\nXX",
"output": "Yes... | 77 | 307,200 | 0 | 10,853 | |
192 | Walking in the Rain | [
"brute force",
"implementation"
] | null | null | In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of *n* tiles that are lain in a row and are numbered from 1 to *n* from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number *n*. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number *i* (*i*<=<<=*n*<=-<=1), you can reach the tiles number *i*<=+<=1 or the tile number *i*<=+<=2 from it (if you stand on the tile number *n*<=-<=1, you can only reach tile number *n*). We can assume that all the opposition movements occur instantaneously.
In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the *i*-th tile is destroyed after *a**i* days of rain (on day *a**i* tile isn't destroyed yet, and on day *a**i*<=+<=1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number *n* is broken, or it is impossible to reach the tile number *n* from the tile number 1 if we can walk on undestroyed tiles.
The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number *n* will be possible. | The first line contains integer *n* (1<=≤<=*n*<=≤<=103) — the boulevard's length in tiles.
The second line contains *n* space-separated integers *a**i* — the number of days after which the *i*-th tile gets destroyed (1<=≤<=*a**i*<=≤<=103). | Print a single number — the sought number of days. | [
"4\n10 3 5 10\n",
"5\n10 2 8 3 5\n"
] | [
"5\n",
"5\n"
] | In the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.
In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted. | [
{
"input": "4\n10 3 5 10",
"output": "5"
},
{
"input": "5\n10 2 8 3 5",
"output": "5"
},
{
"input": "10\n10 3 1 6 7 1 3 3 8 1",
"output": "1"
},
{
"input": "10\n26 72 10 52 2 5 61 2 39 64",
"output": "5"
},
{
"input": "100\n8 2 1 2 8 3 5 8 5 1 9 3 4 1 5 6 4 2 9 10... | 92 | 0 | 3 | 10,860 | |
961 | Chessboard | [
"bitmasks",
"brute force",
"implementation"
] | null | null | Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size *n* by *n*, *n* is always odd. And what's even worse, some squares were of wrong color. *j*-th square of the *i*-th row of *k*-th piece of the board has color *a**k*,<=*i*,<=*j*; 1 being black and 0 being white.
Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2*n* by 2*n*. You are allowed to move pieces but not allowed to rotate or flip them. | The first line contains odd integer *n* (1<=≤<=*n*<=≤<=100) — the size of all pieces of the board.
Then 4 segments follow, each describes one piece of the board. Each consists of *n* lines of *n* characters; *j*-th one of *i*-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line. | Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard. | [
"1\n0\n\n0\n\n1\n\n0\n",
"3\n101\n010\n101\n\n101\n000\n101\n\n010\n101\n011\n\n010\n101\n010\n"
] | [
"1\n",
"2\n"
] | none | [
{
"input": "1\n0\n\n0\n\n1\n\n0",
"output": "1"
},
{
"input": "3\n101\n010\n101\n\n101\n000\n101\n\n010\n101\n011\n\n010\n101\n010",
"output": "2"
},
{
"input": "3\n000\n000\n000\n\n111\n111\n111\n\n111\n111\n111\n\n000\n000\n000",
"output": "16"
},
{
"input": "3\n101\n010\n1... | 171 | 1,945,600 | 3 | 10,870 | |
301 | Yaroslav and Sequence | [
"constructive algorithms"
] | null | null | Yaroslav has an array, consisting of (2·*n*<=-<=1) integers. In a single operation Yaroslav can change the sign of exactly *n* elements in the array. In other words, in one operation Yaroslav can select exactly *n* array elements, and multiply each of them by -1.
Yaroslav is now wondering: what maximum sum of array elements can be obtained if it is allowed to perform any number of described operations?
Help Yaroslav. | The first line contains an integer *n* (2<=≤<=*n*<=≤<=100). The second line contains (2·*n*<=-<=1) integers — the array elements. The array elements do not exceed 1000 in their absolute value. | In a single line print the answer to the problem — the maximum sum that Yaroslav can get. | [
"2\n50 50 50\n",
"2\n-1 -100 -1\n"
] | [
"150\n",
"100\n"
] | In the first sample you do not need to change anything. The sum of elements equals 150.
In the second sample you need to change the sign of the first two elements. Then we get the sum of the elements equal to 100. | [
{
"input": "2\n50 50 50",
"output": "150"
},
{
"input": "2\n-1 -100 -1",
"output": "100"
},
{
"input": "3\n-959 -542 -669 -513 160",
"output": "2843"
},
{
"input": "4\n717 473 344 -51 -548 703 -869",
"output": "3603"
},
{
"input": "5\n270 -181 957 -509 -6 937 -175... | 124 | 0 | 3 | 10,879 | |
0 | none | [
"none"
] | null | null | Stepan likes to repeat vowel letters when he writes words. For example, instead of the word "pobeda" he can write "pobeeeedaaaaa".
Sergey does not like such behavior, so he wants to write a program to format the words written by Stepan. This program must combine all consecutive equal vowels to a single vowel. The vowel letters are "a", "e", "i", "o", "u" and "y".
There are exceptions: if letters "e" or "o" repeat in a row exactly 2 times, like in words "feet" and "foot", the program must skip them and do not transform in one vowel. For example, the word "iiiimpleeemeentatiioon" must be converted to the word "implemeentatioon".
Sergey is very busy and asks you to help him and write the required program. | The first line contains the integer *n* (1<=≤<=*n*<=≤<=100<=000) — the number of letters in the word written by Stepan.
The second line contains the string *s* which has length that equals to *n* and contains only lowercase English letters — the word written by Stepan. | Print the single string — the word written by Stepan converted according to the rules described in the statement. | [
"13\npobeeeedaaaaa\n",
"22\niiiimpleeemeentatiioon\n",
"18\naeiouyaaeeiioouuyy\n",
"24\naaaoooiiiuuuyyyeeeggghhh\n"
] | [
"pobeda\n",
"implemeentatioon\n",
"aeiouyaeeioouy\n",
"aoiuyeggghhh\n"
] | none | [
{
"input": "13\npobeeeedaaaaa",
"output": "pobeda"
},
{
"input": "22\niiiimpleeemeentatiioon",
"output": "implemeentatioon"
},
{
"input": "18\naeiouyaaeeiioouuyy",
"output": "aeiouyaeeioouy"
},
{
"input": "24\naaaoooiiiuuuyyyeeeggghhh",
"output": "aoiuyeggghhh"
},
{
... | 46 | 4,812,800 | 0 | 10,904 | |
340 | Maximal Area Quadrilateral | [
"brute force",
"geometry"
] | null | null | Iahub has drawn a set of *n* points in the cartesian plane which he calls "special points". A quadrilateral is a simple polygon without self-intersections with four sides (also called edges) and four vertices (also called corners). Please note that a quadrilateral doesn't have to be convex. A special quadrilateral is one which has all four vertices in the set of special points. Given the set of special points, please calculate the maximal area of a special quadrilateral. | The first line contains integer *n* (4<=≤<=*n*<=≤<=300). Each of the next *n* lines contains two integers: *x**i*, *y**i* (<=-<=1000<=≤<=*x**i*,<=*y**i*<=≤<=1000) — the cartesian coordinates of *i*th special point. It is guaranteed that no three points are on the same line. It is guaranteed that no two points coincide. | Output a single real number — the maximal area of a special quadrilateral. The answer will be considered correct if its absolute or relative error does't exceed 10<=-<=9. | [
"5\n0 0\n0 4\n4 0\n4 4\n2 3\n"
] | [
"16.000000"
] | In the test example we can choose first 4 points to be the vertices of the quadrilateral. They form a square by side 4, so the area is 4·4 = 16. | [
{
"input": "5\n0 0\n0 4\n4 0\n4 4\n2 3",
"output": "16.000000"
},
{
"input": "10\n-6 -4\n-7 5\n-7 -7\n5 -7\n4 -9\n-6 7\n2 9\n-4 -6\n2 10\n-10 -4",
"output": "166.000000"
},
{
"input": "4\n-3 3\n0 3\n-2 -1\n2 2",
"output": "11.000000"
},
{
"input": "5\n-4 -3\n-3 -2\n3 3\n-1 2\... | 716 | 1,126,400 | 0 | 10,930 | |
69 | Subsegments | [
"data structures",
"implementation"
] | E. Subsegments | 1 | 256 | Programmer Sasha has recently begun to study data structures. His coach Stas told him to solve the problem of finding a minimum on the segment of the array in , which Sasha coped with. For Sasha not to think that he had learned all, Stas gave him a new task. For each segment of the fixed length Sasha must find the maximum element of those that occur on the given segment exactly once. Help Sasha solve this problem. | The first line contains two positive integers *n* and *k* (1<=≤<=*n*<=≤<=105,<=1<=≤<=*k*<=≤<=*n*) — the number of array elements and the length of the segment.
Then follow *n* lines: the *i*-th one contains a single number *a**i* (<=-<=109<=≤<=*a**i*<=≤<=109). | Print *n*–*k*<=+<=1 numbers, one per line: on the *i*-th line print of the maximum number of those numbers from the subarray *a**i* *a**i*<=+<=1 … *a**i*<=+<=*k*<=-<=1 that occur in this subarray exactly 1 time. If there are no such numbers in this subarray, print "Nothing". | [
"5 3\n1\n2\n2\n3\n3\n",
"6 4\n3\n3\n3\n4\n4\n2\n"
] | [
"1\n3\n2\n",
"4\nNothing\n3\n"
] | none | [
{
"input": "5 3\n1\n2\n2\n3\n3",
"output": "1\n3\n2"
},
{
"input": "6 4\n3\n3\n3\n4\n4\n2",
"output": "4\nNothing\n3"
},
{
"input": "10 3\n-55\n-35\n-80\n91\n-96\n-93\n-39\n-77\n4\n29",
"output": "-35\n91\n91\n91\n-39\n-39\n4\n29"
},
{
"input": "10 3\n-13\n26\n-97\n-38\n43\n-... | 1,000 | 3,891,200 | 0 | 10,958 |
540 | School Marks | [
"greedy",
"implementation"
] | null | null | Little Vova studies programming in an elite school. Vova and his classmates are supposed to write *n* progress tests, for each test they will get a mark from 1 to *p*. Vova is very smart and he can write every test for any mark, but he doesn't want to stand out from the crowd too much. If the sum of his marks for all tests exceeds value *x*, then his classmates notice how smart he is and start distracting him asking to let them copy his homework. And if the median of his marks will be lower than *y* points (the definition of a median is given in the notes), then his mom will decide that he gets too many bad marks and forbid him to play computer games.
Vova has already wrote *k* tests and got marks *a*1,<=...,<=*a**k*. He doesn't want to get into the first or the second situation described above and now he needs to determine which marks he needs to get for the remaining tests. Help him do that. | The first line contains 5 space-separated integers: *n*, *k*, *p*, *x* and *y* (1<=≤<=*n*<=≤<=999, *n* is odd, 0<=≤<=*k*<=<<=*n*, 1<=≤<=*p*<=≤<=1000, *n*<=≤<=*x*<=≤<=*n*·*p*, 1<=≤<=*y*<=≤<=*p*). Here *n* is the number of tests that Vova is planned to write, *k* is the number of tests he has already written, *p* is the maximum possible mark for a test, *x* is the maximum total number of points so that the classmates don't yet disturb Vova, *y* is the minimum median point so that mom still lets him play computer games.
The second line contains *k* space-separated integers: *a*1,<=...,<=*a**k* (1<=≤<=*a**i*<=≤<=*p*) — the marks that Vova got for the tests he has already written. | If Vova cannot achieve the desired result, print "-1".
Otherwise, print *n*<=-<=*k* space-separated integers — the marks that Vova should get for the remaining tests. If there are multiple possible solutions, print any of them. | [
"5 3 5 18 4\n3 5 4\n",
"5 3 5 16 4\n5 5 5\n"
] | [
"4 1\n",
"-1\n"
] | The median of sequence *a*<sub class="lower-index">1</sub>, ..., *a*<sub class="lower-index">*n*</sub> where *n* is odd (in this problem *n* is always odd) is the element staying on (*n* + 1) / 2 position in the sorted list of *a*<sub class="lower-index">*i*</sub>.
In the first sample the sum of marks equals 3 + 5 + 4 + 4 + 1 = 17, what doesn't exceed 18, that means that Vova won't be disturbed by his classmates. And the median point of the sequence {1, 3, 4, 4, 5} equals to 4, that isn't less than 4, so his mom lets him play computer games.
Please note that you do not have to maximize the sum of marks or the median mark. Any of the answers: "4 2", "2 4", "5 1", "1 5", "4 1", "1 4" for the first test is correct.
In the second sample Vova got three '5' marks, so even if he gets two '1' marks, the sum of marks will be 17, that is more than the required value of 16. So, the answer to this test is "-1". | [
{
"input": "5 3 5 18 4\n3 5 4",
"output": "4 1"
},
{
"input": "5 3 5 16 4\n5 5 5",
"output": "-1"
},
{
"input": "5 3 5 17 4\n5 5 5",
"output": "1 1"
},
{
"input": "5 3 5 12 1\n5 5 1",
"output": "-1"
},
{
"input": "5 3 5 13 1\n5 5 1",
"output": "1 1"
},
{
... | 62 | 1,433,600 | 0 | 10,979 | |
222 | Olympiad | [
"binary search",
"greedy",
"sortings",
"two pointers"
] | null | null | A boy named Vasya has taken part in an Olympiad. His teacher knows that in total Vasya got at least *x* points for both tours of the Olympiad. The teacher has the results of the first and the second tour of the Olympiad but the problem is, the results have only points, no names. The teacher has to know Vasya's chances.
Help Vasya's teacher, find two numbers — the best and the worst place Vasya could have won. Note that the total results' table sorts the participants by the sum of points for both tours (the first place has the participant who has got the most points). If two or more participants have got the same number of points, it's up to the jury to assign places to them according to their choice. It is guaranteed that each participant of the Olympiad participated in both tours of the Olympiad. | The first line contains two space-separated integers *n*,<=*x* (1<=≤<=*n*<=≤<=105; 0<=≤<=*x*<=≤<=2·105) — the number of Olympiad participants and the minimum number of points Vasya earned.
The second line contains *n* space-separated integers: *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=105) — the participants' points in the first tour.
The third line contains *n* space-separated integers: *b*1,<=*b*2,<=...,<=*b**n* (0<=≤<=*b**i*<=≤<=105) — the participants' points in the second tour.
The participants' points are given in the arbitrary order. It is guaranteed that Vasya was present in the Olympiad — there are two integers *i*,<=*j* (1<=≤<=*i*,<=*j*<=≤<=*n*) such, that *a**i*<=+<=*b**j*<=≥<=*x*. | Print two space-separated integers — the best and the worst place Vasya could have got on the Olympiad. | [
"5 2\n1 1 1 1 1\n1 1 1 1 1\n",
"6 7\n4 3 5 6 4 4\n8 6 0 4 3 4\n"
] | [
"1 5\n",
"1 5\n"
] | In the first text sample all 5 participants earn 2 points each in any case. Depending on the jury's decision, Vasya can get the first (the best) as well as the last (the worst) fifth place.
In the second test sample in the best case scenario Vasya wins again: he can win 12 points and become the absolute winner if the total results' table looks like that — {4:8, 6:4, 3:6, 4:4, 4:3, 5:0}.
In this table all participants are sorted by decreasing points and we can see how much a participant earned in the first and in the second tour.
In the worst case scenario Vasya can get the fifth place if the table looks like that — {4:8, 4:6, 6:4, 5:4, 4:3, 3:0}, and he earned 4 and 3 points in the first and second tours, correspondingly. | [
{
"input": "5 2\n1 1 1 1 1\n1 1 1 1 1",
"output": "1 5"
},
{
"input": "6 7\n4 3 5 6 4 4\n8 6 0 4 3 4",
"output": "1 5"
},
{
"input": "1 100\n56\n44",
"output": "1 1"
},
{
"input": "5 1\n1 2 3 4 5\n1 2 3 4 5",
"output": "1 5"
},
{
"input": "5 5\n2 2 2 2 2\n3 3 3 3 ... | 560 | 13,312,000 | 3 | 10,983 | |
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... | 218 | 6,758,400 | 3 | 10,985 | |
66 | Petya and File System | [
"data structures",
"implementation"
] | C. Petya and File System | 3 | 256 | Recently, on a programming lesson little Petya showed how quickly he can create files and folders on the computer. But he got soon fed up with this activity, and he decided to do a much more useful thing. He decided to calculate what folder contains most subfolders (including nested folders, nested folders of nested folders, and so on) and what folder contains most files (including the files in the subfolders).
More formally, the subfolders of the folder are all its directly nested folders and the subfolders of these nested folders. The given folder is not considered the subfolder of itself. A file is regarded as lying in a folder, if and only if it either lies directly in this folder, or lies in some subfolder of the folder.
For a better understanding of how to count subfolders and files for calculating the answer, see notes and answers to the samples.
You are given a few files that Petya has managed to create. The path to each file looks as follows:
*diskName*:\*folder*1\*folder*2\...\ *folder**n*\*fileName*
- *diskName* is single capital letter from the set {C,D,E,F,G}.- *folder*1, ..., *folder**n* are folder names. Each folder name is nonempty sequence of lowercase Latin letters and digits from 0 to 9. (*n*<=≥<=1)- *fileName* is a file name in the form of *name*.*extension*, where the *name* and the *extension* are nonempty sequences of lowercase Latin letters and digits from 0 to 9.
It is also known that there is no file whose path looks like *diskName*:\*fileName*. That is, each file is stored in some folder, but there are no files directly in the root. Also let us assume that the disk root is not a folder.
Help Petya to find the largest number of subfolders, which can be in some folder, and the largest number of files that can be in some folder, counting all its subfolders. | Each line of input data contains the description of one file path. The length of each line does not exceed 100, and overall there are no more than 100 lines. It is guaranteed, that all the paths are correct and meet the above rules. It is also guaranteed, that there are no two completely equal lines. That is, each file is described exactly once.
There is at least one line in the input data. | Print two space-separated numbers. The first one is the maximal number of possible subfolders in a folder (including nested folders, nested folders of nested folders, and so on). The second one is the maximal number of files in a folder (including nested files in subfolders). Note that the disks are not regarded as folders. | [
"C:\\folder1\\file1.txt",
"C:\\folder1\\folder2\\folder3\\file1.txt\nC:\\folder1\\folder2\\folder4\\file1.txt\nD:\\folder1\\file1.txt\n",
"C:\\file\\file\\file\\file\\file.txt\nC:\\file\\file\\file\\file2\\file.txt"
] | [
"0 1",
"3 2",
"4 2"
] | In the first sample we have one folder on the "C" disk. It has no subfolders, which is why the first number in the answer is 0. But this folder contains one file, so the second number of the answer is 1.
In the second sample we have several different folders. Consider the "folder1" folder on the "C" disk. This folder directly contains one folder, "folder2". The "folder2" folder contains two more folders — "folder3" and "folder4". Thus, the "folder1" folder on the "C" drive has exactly 3 subfolders. Also this folder contains two files, even though they do not lie directly in the folder, but they are located in subfolders of "folder1".
In the third example we see that the names of some folders and some subfolders are identical. Consider the "file" folder, which lies directly on the "C" disk. That folder contains another "file" folder, which in turn contains another "file" folder, which contains two more folders, "file" and "file2". Thus, the "file" folder, which lies directly on the "C" disk, contains 4 subfolders. | [
{
"input": "C:\\folder1\\file1.txt",
"output": "0 1"
},
{
"input": "C:\\folder1\\folder2\\folder3\\file1.txt\nC:\\folder1\\folder2\\folder4\\file1.txt\nD:\\folder1\\file1.txt",
"output": "3 2"
},
{
"input": "C:\\file\\file\\file\\file\\file.txt\nC:\\file\\file\\file\\file2\\file.txt",
... | 310 | 24,780,800 | 0 | 10,991 |
946 | Largest Beautiful Number | [
"greedy",
"implementation"
] | null | null | Yes, that's another problem with definition of "beautiful" numbers.
Let's call a positive integer *x* beautiful if its decimal representation without leading zeroes contains even number of digits, and there exists a permutation of this representation which is palindromic. For example, 4242 is a beautiful number, since it contains 4 digits, and there exists a palindromic permutation 2442.
Given a positive integer *s*, find the largest beautiful number which is less than *s*. | The first line contains one integer *t* (1<=≤<=*t*<=≤<=105) — the number of testcases you have to solve.
Then *t* lines follow, each representing one testcase and containing one string which is the decimal representation of number *s*. It is guaranteed that this string has even length, contains no leading zeroes, and there exists at least one beautiful number less than *s*.
The sum of lengths of *s* over all testcases doesn't exceed 2·105. | For each testcase print one line containing the largest beautiful number which is less than *s* (it is guaranteed that the answer exists). | [
"4\n89\n88\n1000\n28923845\n"
] | [
"88\n77\n99\n28923839\n"
] | none | [
{
"input": "4\n89\n88\n1000\n28923845",
"output": "88\n77\n99\n28923839"
},
{
"input": "1\n100001",
"output": "9999"
},
{
"input": "2\n1001\n100001",
"output": "99\n9999"
},
{
"input": "3\n100001\n100000\n100002",
"output": "9999\n9999\n100001"
},
{
"input": "1\n1... | 15 | 0 | 0 | 10,992 | |
196 | Paint Tree | [
"constructive algorithms",
"divide and conquer",
"geometry",
"sortings",
"trees"
] | null | null | You are given a tree with *n* vertexes and *n* points on a plane, no three points lie on one straight line.
Your task is to paint the given tree on a plane, using the given points as vertexes.
That is, you should correspond each vertex of the tree to exactly one point and each point should correspond to a vertex. If two vertexes of the tree are connected by an edge, then the corresponding points should have a segment painted between them. The segments that correspond to non-adjacent edges, should not have common points. The segments that correspond to adjacent edges should have exactly one common point. | The first line contains an integer *n* (1<=≤<=*n*<=≤<=1500) — the number of vertexes on a tree (as well as the number of chosen points on the plane).
Each of the next *n*<=-<=1 lines contains two space-separated integers *u**i* and *v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*, *u**i*<=≠<=*v**i*) — the numbers of tree vertexes connected by the *i*-th edge.
Each of the next *n* lines contain two space-separated integers *x**i* and *y**i* (<=-<=109<=≤<=*x**i*,<=*y**i*<=≤<=109) — the coordinates of the *i*-th point on the plane. No three points lie on one straight line.
It is guaranteed that under given constraints problem has a solution. | Print *n* distinct space-separated integers from 1 to *n*: the *i*-th number must equal the number of the vertex to place at the *i*-th point (the points are numbered in the order, in which they are listed in the input).
If there are several solutions, print any of them. | [
"3\n1 3\n2 3\n0 0\n1 1\n2 0\n",
"4\n1 2\n2 3\n1 4\n-1 -2\n3 5\n-3 3\n2 0\n"
] | [
"1 3 2\n",
"4 2 1 3\n"
] | The possible solutions for the sample are given below. | [] | 60 | 0 | 0 | 11,002 | |
776 | Sherlock and the Encrypted Data | [
"bitmasks",
"combinatorics",
"dp"
] | null | null | Sherlock found a piece of encrypted data which he thinks will be useful to catch Moriarty. The encrypted data consists of two integer *l* and *r*. He noticed that these integers were in hexadecimal form.
He takes each of the integers from *l* to *r*, and performs the following operations:
1. He lists the distinct digits present in the given number. For example: for 101416, he lists the digits as 1,<=0,<=4. 1. Then he sums respective powers of two for each digit listed in the step above. Like in the above example *sum*<==<=21<=+<=20<=+<=24<==<=1910. 1. He changes the initial number by applying bitwise xor of the initial number and the sum. Example: . Note that xor is done in binary notation.
One more example: for integer 1e the sum is *sum*<==<=21<=+<=214. Letters a, b, c, d, e, f denote hexadecimal digits 10, 11, 12, 13, 14, 15, respertively.
Sherlock wants to count the numbers in the range from *l* to *r* (both inclusive) which decrease on application of the above four steps. He wants you to answer his *q* queries for different *l* and *r*. | First line contains the integer *q* (1<=≤<=*q*<=≤<=10000).
Each of the next *q* lines contain two hexadecimal integers *l* and *r* (0<=≤<=*l*<=≤<=*r*<=<<=1615).
The hexadecimal integers are written using digits from 0 to 9 and/or lowercase English letters a, b, c, d, e, f.
The hexadecimal integers do not contain extra leading zeros. | Output *q* lines, *i*-th line contains answer to the *i*-th query (in decimal notation). | [
"1\n1014 1014\n",
"2\n1 1e\n1 f\n",
"2\n1 abc\nd0e fe23\n"
] | [
"1\n",
"1\n0\n",
"412\n28464\n"
] | For the second input,
14<sub class="lower-index">16</sub> = 20<sub class="lower-index">10</sub>
*sum* = 2<sup class="upper-index">1</sup> + 2<sup class="upper-index">4</sup> = 18
<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/fcc2edc423982d6c5d19cc2a89cc99c492aa2427.png" style="max-width: 100.0%;max-height: 100.0%;"/>
Thus, it reduces. And, we can verify that it is the only number in range 1 to 1*e* that reduces. | [] | 31 | 0 | 0 | 11,037 | |
61 | Eternal Victory | [
"dfs and similar",
"graphs",
"greedy",
"shortest paths",
"trees"
] | D. Eternal Victory | 2 | 256 | Valerian was captured by Shapur. The victory was such a great one that Shapur decided to carve a scene of Valerian's defeat on a mountain. So he had to find the best place to make his victory eternal!
He decided to visit all *n* cities of Persia to find the best available mountain, but after the recent war he was too tired and didn't want to traverse a lot. So he wanted to visit each of these *n* cities at least once with smallest possible traverse. Persian cities are connected with bidirectional roads. You can go from any city to any other one using these roads and there is a unique path between each two cities.
All cities are numbered 1 to *n*. Shapur is currently in the city 1 and he wants to visit all other cities with minimum possible traverse. He can finish his travels in any city.
Help Shapur find how much He should travel. | First line contains a single natural number *n* (1<=≤<=*n*<=≤<=105) — the amount of cities.
Next *n*<=-<=1 lines contain 3 integer numbers each *x**i*, *y**i* and *w**i* (1<=≤<=*x**i*,<=*y**i*<=≤<=*n*,<=0<=≤<=*w**i*<=≤<=2<=×<=104). *x**i* and *y**i* are two ends of a road and *w**i* is the length of that road. | A single integer number, the minimal length of Shapur's travel.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). | [
"3\n1 2 3\n2 3 4\n",
"3\n1 2 3\n1 3 3\n"
] | [
"7\n",
"9\n"
] | none | [
{
"input": "3\n1 2 3\n2 3 4",
"output": "7"
},
{
"input": "3\n1 2 3\n1 3 3",
"output": "9"
},
{
"input": "5\n5 3 60\n4 3 63\n2 1 97\n3 1 14",
"output": "371"
},
{
"input": "3\n2 1 63\n3 1 78",
"output": "204"
},
{
"input": "13\n8 2 58\n2 1 49\n13 10 41\n11 9 67\n6... | 405 | 40,243,200 | 3.823791 | 11,052 |
19 | World Football Cup | [
"implementation"
] | A. World Football Cup | 2 | 64 | Everyone knows that 2010 FIFA World Cup is being held in South Africa now. By the decision of BFA (Berland's Football Association) next World Cup will be held in Berland. BFA took the decision to change some World Cup regulations:
- the final tournament features *n* teams (*n* is always even) - the first *n*<=/<=2 teams (according to the standings) come through to the knockout stage - the standings are made on the following principle: for a victory a team gets 3 points, for a draw — 1 point, for a defeat — 0 points. In the first place, teams are ordered in the standings in decreasing order of their points; in the second place — in decreasing order of the difference between scored and missed goals; in the third place — in the decreasing order of scored goals - it's written in Berland's Constitution that the previous regulation helps to order the teams without ambiguity.
You are asked to write a program that, by the given list of the competing teams and the results of all the matches, will find the list of teams that managed to get through to the knockout stage. | The first input line contains the only integer *n* (1<=≤<=*n*<=≤<=50) — amount of the teams, taking part in the final tournament of World Cup. The following *n* lines contain the names of these teams, a name is a string of lower-case and upper-case Latin letters, its length doesn't exceed 30 characters. The following *n*·(*n*<=-<=1)<=/<=2 lines describe the held matches in the format name1-name2 num1:num2, where *name*1, *name*2 — names of the teams; *num*1, *num*2 (0<=≤<=*num*1,<=*num*2<=≤<=100) — amount of the goals, scored by the corresponding teams. Accuracy of the descriptions is guaranteed: there are no two team names coinciding accurate to the letters' case; there is no match, where a team plays with itself; each match is met in the descriptions only once. | Output *n*<=/<=2 lines — names of the teams, which managed to get through to the knockout stage in lexicographical order. Output each name in a separate line. No odd characters (including spaces) are allowed. It's guaranteed that the described regulations help to order the teams without ambiguity. | [
"4\nA\nB\nC\nD\nA-B 1:1\nA-C 2:2\nA-D 1:0\nB-C 1:0\nB-D 0:3\nC-D 0:3\n",
"2\na\nA\na-A 2:1\n"
] | [
"A\nD\n",
"a\n"
] | none | [
{
"input": "4\nA\nB\nC\nD\nA-B 1:1\nA-C 2:2\nA-D 1:0\nB-C 1:0\nB-D 0:3\nC-D 0:3",
"output": "A\nD"
},
{
"input": "2\na\nA\na-A 2:1",
"output": "a"
},
{
"input": "2\nEULEUbCmfrmqxtzvg\nuHGRmKUhDcxcfqyruwzen\nuHGRmKUhDcxcfqyruwzen-EULEUbCmfrmqxtzvg 13:92",
"output": "EULEUbCmfrmqxtzvg"... | 92 | 4,608,000 | -1 | 11,067 |
659 | Qualifying Contest | [
"constructive algorithms",
"sortings"
] | null | null | Very soon Berland will hold a School Team Programming Olympiad. From each of the *m* Berland regions a team of two people is invited to participate in the olympiad. The qualifying contest to form teams was held and it was attended by *n* Berland students. There were at least two schoolboys participating from each of the *m* regions of Berland. The result of each of the participants of the qualifying competition is an integer score from 0 to 800 inclusive.
The team of each region is formed from two such members of the qualifying competition of the region, that none of them can be replaced by a schoolboy of the same region, not included in the team and who received a greater number of points. There may be a situation where a team of some region can not be formed uniquely, that is, there is more than one school team that meets the properties described above. In this case, the region needs to undertake an additional contest. The two teams in the region are considered to be different if there is at least one schoolboy who is included in one team and is not included in the other team. It is guaranteed that for each region at least two its representatives participated in the qualifying contest.
Your task is, given the results of the qualifying competition, to identify the team from each region, or to announce that in this region its formation requires additional contests. | The first line of the input contains two integers *n* and *m* (2<=≤<=*n*<=≤<=100<=000, 1<=≤<=*m*<=≤<=10<=000, *n*<=≥<=2*m*) — the number of participants of the qualifying contest and the number of regions in Berland.
Next *n* lines contain the description of the participants of the qualifying contest in the following format: Surname (a string of length from 1 to 10 characters and consisting of large and small English letters), region number (integer from 1 to *m*) and the number of points scored by the participant (integer from 0 to 800, inclusive).
It is guaranteed that all surnames of all the participants are distinct and at least two people participated from each of the *m* regions. The surnames that only differ in letter cases, should be considered distinct. | Print *m* lines. On the *i*-th line print the team of the *i*-th region — the surnames of the two team members in an arbitrary order, or a single character "?" (without the quotes) if you need to spend further qualifying contests in the region. | [
"5 2\nIvanov 1 763\nAndreev 2 800\nPetrov 1 595\nSidorov 1 790\nSemenov 2 503\n",
"5 2\nIvanov 1 800\nAndreev 2 763\nPetrov 1 800\nSidorov 1 800\nSemenov 2 503\n"
] | [
"Sidorov Ivanov\nAndreev Semenov\n",
"?\nAndreev Semenov\n"
] | In the first sample region teams are uniquely determined.
In the second sample the team from region 2 is uniquely determined and the team from region 1 can have three teams: "Petrov"-"Sidorov", "Ivanov"-"Sidorov", "Ivanov" -"Petrov", so it is impossible to determine a team uniquely. | [
{
"input": "5 2\nIvanov 1 763\nAndreev 2 800\nPetrov 1 595\nSidorov 1 790\nSemenov 2 503",
"output": "Sidorov Ivanov\nAndreev Semenov"
},
{
"input": "5 2\nIvanov 1 800\nAndreev 2 763\nPetrov 1 800\nSidorov 1 800\nSemenov 2 503",
"output": "?\nAndreev Semenov"
},
{
"input": "10 2\nSHiBIEz... | 545 | 17,203,200 | 3 | 11,075 | |
985 | Sand Fortress | [
"binary search",
"constructive algorithms",
"math"
] | null | null | You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought *n* packs of sand with you. Let height *h**i* of the sand pillar on some spot *i* be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with *H* sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle:
- *h*1<=≤<=*H*: no sand from the leftmost spot should go over the fence; - For any |*h**i*<=-<=*h**i*<=+<=1|<=≤<=1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen; - : you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold. | The only line contains two integer numbers *n* and *H* (1<=≤<=*n*,<=*H*<=≤<=1018) — the number of sand packs you have and the height of the fence, respectively. | Print the minimum number of spots you can occupy so the all the castle building conditions hold. | [
"5 2\n",
"6 8\n"
] | [
"3\n",
"3\n"
] | Here are the heights of some valid castles:
- *n* = 5, *H* = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] - *n* = 6, *H* = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)
The first list for both cases is the optimal answer, 3 spots are occupied in them.
And here are some invalid ones:
- *n* = 5, *H* = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...] - *n* = 6, *H* = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...] | [
{
"input": "5 2",
"output": "3"
},
{
"input": "6 8",
"output": "3"
},
{
"input": "20 4",
"output": "7"
},
{
"input": "1000000000000000000 1000000000000000000",
"output": "1414213562"
},
{
"input": "1 1",
"output": "1"
},
{
"input": "1 10000000000000000... | 62 | 0 | 3 | 11,078 | |
165 | Another Problem on Strings | [
"binary search",
"brute force",
"dp",
"math",
"strings",
"two pointers"
] | null | null | A string is binary, if it consists only of characters "0" and "1".
String *v* is a substring of string *w* if it has a non-zero length and can be read starting from some position in string *w*. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs.
You are given a binary string *s*. Your task is to find the number of its substrings, containing exactly *k* characters "1". | The first line contains the single integer *k* (0<=≤<=*k*<=≤<=106). The second line contains a non-empty binary string *s*. The length of *s* does not exceed 106 characters. | Print the single number — the number of substrings of the given string, containing exactly *k* characters "1".
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"1\n1010\n",
"2\n01010\n",
"100\n01010\n"
] | [
"6\n",
"4\n",
"0\n"
] | In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010".
In the second sample the sought substrings are: "101", "0101", "1010", "01010". | [
{
"input": "1\n1010",
"output": "6"
},
{
"input": "2\n01010",
"output": "4"
},
{
"input": "100\n01010",
"output": "0"
},
{
"input": "0\n01010",
"output": "3"
},
{
"input": "0\n0010100011",
"output": "10"
},
{
"input": "0\n10000",
"output": "10"
}... | 216 | 22,220,800 | -1 | 11,091 | |
749 | Voting | [
"greedy",
"implementation",
"two pointers"
] | null | null | There are *n* employees in Alternative Cake Manufacturing (ACM). They are now voting on some very important question and the leading world media are trying to predict the outcome of the vote.
Each of the employees belongs to one of two fractions: depublicans or remocrats, and these two fractions have opposite opinions on what should be the outcome of the vote. The voting procedure is rather complicated:
1. Each of *n* employees makes a statement. They make statements one by one starting from employees 1 and finishing with employee *n*. If at the moment when it's time for the *i*-th employee to make a statement he no longer has the right to vote, he just skips his turn (and no longer takes part in this voting). 1. When employee makes a statement, he can do nothing or declare that one of the other employees no longer has a right to vote. It's allowed to deny from voting people who already made the statement or people who are only waiting to do so. If someone is denied from voting he no longer participates in the voting till the very end. 1. When all employees are done with their statements, the procedure repeats: again, each employees starting from 1 and finishing with *n* who are still eligible to vote make their statements. 1. The process repeats until there is only one employee eligible to vote remaining and he determines the outcome of the whole voting. Of course, he votes for the decision suitable for his fraction.
You know the order employees are going to vote and that they behave optimal (and they also know the order and who belongs to which fraction). Predict the outcome of the vote. | The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=200<=000) — the number of employees.
The next line contains *n* characters. The *i*-th character is 'D' if the *i*-th employee is from depublicans fraction or 'R' if he is from remocrats. | Print 'D' if the outcome of the vote will be suitable for depublicans and 'R' if remocrats will win. | [
"5\nDDRRR\n",
"6\nDDRRRR\n"
] | [
"D\n",
"R\n"
] | Consider one of the voting scenarios for the first sample:
1. Employee 1 denies employee 5 to vote. 1. Employee 2 denies employee 3 to vote. 1. Employee 3 has no right to vote and skips his turn (he was denied by employee 2). 1. Employee 4 denies employee 2 to vote. 1. Employee 5 has no right to vote and skips his turn (he was denied by employee 1). 1. Employee 1 denies employee 4. 1. Only employee 1 now has the right to vote so the voting ends with the victory of depublicans. | [
{
"input": "5\nDDRRR",
"output": "D"
},
{
"input": "6\nDDRRRR",
"output": "R"
},
{
"input": "1\nD",
"output": "D"
},
{
"input": "1\nR",
"output": "R"
},
{
"input": "2\nDR",
"output": "D"
},
{
"input": "3\nRDD",
"output": "D"
},
{
"input": "... | 171 | 11,673,600 | 3 | 11,122 | |
30 | Codeforces World Finals | [
"implementation"
] | B. Codeforces World Finals | 2 | 256 | The king Copa often has been reported about the Codeforces site, which is rapidly getting more and more popular among the brightest minds of the humanity, who are using it for training and competing. Recently Copa understood that to conquer the world he needs to organize the world Codeforces tournament. He hopes that after it the brightest minds will become his subordinates, and the toughest part of conquering the world will be completed.
The final round of the Codeforces World Finals 20YY is scheduled for *DD*.*MM*.*YY*, where *DD* is the day of the round, *MM* is the month and *YY* are the last two digits of the year. Bob is lucky to be the first finalist form Berland. But there is one problem: according to the rules of the competition, all participants must be at least 18 years old at the moment of the finals. Bob was born on *BD*.*BM*.*BY*. This date is recorded in his passport, the copy of which he has already mailed to the organizers. But Bob learned that in different countries the way, in which the dates are written, differs. For example, in the US the month is written first, then the day and finally the year. Bob wonders if it is possible to rearrange the numbers in his date of birth so that he will be at least 18 years old on the day *DD*.*MM*.*YY*. He can always tell that in his motherland dates are written differently. Help him.
According to another strange rule, eligible participant must be born in the same century as the date of the finals. If the day of the finals is participant's 18-th birthday, he is allowed to participate.
As we are considering only the years from 2001 to 2099 for the year of the finals, use the following rule: the year is leap if it's number is divisible by four. | The first line contains the date *DD*.*MM*.*YY*, the second line contains the date *BD*.*BM*.*BY*. It is guaranteed that both dates are correct, and *YY* and *BY* are always in [01;99].
It could be that by passport Bob was born after the finals. In this case, he can still change the order of numbers in date. | If it is possible to rearrange the numbers in the date of birth so that Bob will be at least 18 years old on the *DD*.*MM*.*YY*, output YES. In the other case, output NO.
Each number contains exactly two digits and stands for day, month or year in a date. Note that it is permitted to rearrange only numbers, not digits. | [
"01.01.98\n01.01.80\n",
"20.10.20\n10.02.30\n",
"28.02.74\n28.02.64\n"
] | [
"YES\n",
"NO\n",
"NO\n"
] | none | [
{
"input": "01.01.98\n01.01.80",
"output": "YES"
},
{
"input": "20.10.20\n10.02.30",
"output": "NO"
},
{
"input": "28.02.74\n28.02.64",
"output": "NO"
},
{
"input": "05.05.25\n06.02.71",
"output": "NO"
},
{
"input": "19.11.54\n29.11.53",
"output": "NO"
},
... | 124 | 5,632,000 | 3.95851 | 11,132 |
832 | Petya and Exam | [
"implementation",
"strings"
] | null | null | It's hard times now. Today Petya needs to score 100 points on Informatics exam. The tasks seem easy to Petya, but he thinks he lacks time to finish them all, so he asks you to help with one..
There is a glob pattern in the statements (a string consisting of lowercase English letters, characters "?" and "*"). It is known that character "*" occurs no more than once in the pattern.
Also, *n* query strings are given, it is required to determine for each of them if the pattern matches it or not.
Everything seemed easy to Petya, but then he discovered that the special pattern characters differ from their usual meaning.
A pattern matches a string if it is possible to replace each character "?" with one good lowercase English letter, and the character "*" (if there is one) with any, including empty, string of bad lowercase English letters, so that the resulting string is the same as the given string.
The good letters are given to Petya. All the others are bad. | The first line contains a string with length from 1 to 26 consisting of distinct lowercase English letters. These letters are good letters, all the others are bad.
The second line contains the pattern — a string *s* of lowercase English letters, characters "?" and "*" (1<=≤<=|*s*|<=≤<=105). It is guaranteed that character "*" occurs in *s* no more than once.
The third line contains integer *n* (1<=≤<=*n*<=≤<=105) — the number of query strings.
*n* lines follow, each of them contains single non-empty string consisting of lowercase English letters — a query string.
It is guaranteed that the total length of all query strings is not greater than 105. | Print *n* lines: in the *i*-th of them print "YES" if the pattern matches the *i*-th query string, and "NO" otherwise.
You can choose the case (lower or upper) for each letter arbitrary. | [
"ab\na?a\n2\naaa\naab\n",
"abc\na?a?a*\n4\nabacaba\nabaca\napapa\naaaaax\n"
] | [
"YES\nNO\n",
"NO\nYES\nNO\nYES\n"
] | In the first example we can replace "?" with good letters "a" and "b", so we can see that the answer for the first query is "YES", and the answer for the second query is "NO", because we can't match the third letter.
Explanation of the second example.
- The first query: "NO", because character "*" can be replaced with a string of bad letters only, but the only way to match the query string is to replace it with the string "ba", in which both letters are good. - The second query: "YES", because characters "?" can be replaced with corresponding good letters, and character "*" can be replaced with empty string, and the strings will coincide. - The third query: "NO", because characters "?" can't be replaced with bad letters. - The fourth query: "YES", because characters "?" can be replaced with good letters "a", and character "*" can be replaced with a string of bad letters "x". | [
{
"input": "ab\na?a\n2\naaa\naab",
"output": "YES\nNO"
},
{
"input": "abc\na?a?a*\n4\nabacaba\nabaca\napapa\naaaaax",
"output": "NO\nYES\nNO\nYES"
},
{
"input": "s\nc*?cb\n26\nbbaa\nb\ncc\ncbaab\nacacc\nca\na\nc\ncb\nabb\nba\nb\nba\ncac\nccccb\nccb\nbbbc\nabbcb\na\nbc\nc\na\nabb\nca\ncac... | 61 | 0 | 0 | 11,136 | |
373 | Making Sequences is Fun | [
"binary search",
"implementation",
"math"
] | null | null | We'll define *S*(*n*) for positive integer *n* as follows: the number of the *n*'s digits in the decimal base. For example, *S*(893)<==<=3, *S*(114514)<==<=6.
You want to make a consecutive integer sequence starting from number *m* (*m*,<=*m*<=+<=1,<=...). But you need to pay *S*(*n*)·*k* to add the number *n* to the sequence.
You can spend a cost up to *w*, and you want to make the sequence as long as possible. Write a program that tells sequence's maximum length. | The first line contains three integers *w* (1<=≤<=*w*<=≤<=1016), *m* (1<=≤<=*m*<=≤<=1016), *k* (1<=≤<=*k*<=≤<=109).
Please, do not write the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. | The first line should contain a single integer — the answer to the problem. | [
"9 1 1\n",
"77 7 7\n",
"114 5 14\n",
"1 1 2\n"
] | [
"9\n",
"7\n",
"6\n",
"0\n"
] | none | [
{
"input": "9 1 1",
"output": "9"
},
{
"input": "77 7 7",
"output": "7"
},
{
"input": "114 5 14",
"output": "6"
},
{
"input": "1 1 2",
"output": "0"
},
{
"input": "462 183 8",
"output": "19"
},
{
"input": "462 183 8",
"output": "19"
},
{
"i... | 2,000 | 0 | 0 | 11,176 | |
238 | Not Wool Sequences | [
"constructive algorithms",
"math"
] | null | null | A sequence of non-negative integers *a*1,<=*a*2,<=...,<=*a**n* of length *n* is called a wool sequence if and only if there exists two integers *l* and *r* (1<=≤<=*l*<=≤<=*r*<=≤<=*n*) such that . In other words each wool sequence contains a subsequence of consecutive elements with xor equal to 0.
The expression means applying the operation of a bitwise xor to numbers *x* and *y*. The given operation exists in all modern programming languages, for example, in languages C++ and Java it is marked as "^", in Pascal — as "xor".
In this problem you are asked to compute the number of sequences made of *n* integers from 0 to 2*m*<=-<=1 that are not a wool sequence. You should print this number modulo 1000000009 (109<=+<=9). | The only line of input contains two space-separated integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=105). | Print the required number of sequences modulo 1000000009 (109<=+<=9) on the only line of output. | [
"3 2\n"
] | [
"6\n"
] | Sequences of length 3 made of integers 0, 1, 2 and 3 that are not a wool sequence are (1, 3, 1), (1, 2, 1), (2, 1, 2), (2, 3, 2), (3, 1, 3) and (3, 2, 3). | [
{
"input": "3 2",
"output": "6"
},
{
"input": "4 2",
"output": "0"
},
{
"input": "1 2",
"output": "3"
},
{
"input": "4 11",
"output": "433239206"
},
{
"input": "5 100",
"output": "345449482"
},
{
"input": "5444 31525",
"output": "637906839"
},
... | 248 | 0 | 0 | 11,189 | |
190 | STL | [
"dfs and similar"
] | null | null | Vasya used to be an accountant before the war began and he is one of the few who knows how to operate a computer, so he was assigned as the programmer.
We all know that programs often store sets of integers. For example, if we have a problem about a weighted directed graph, its edge can be represented by three integers: the number of the starting vertex, the number of the final vertex and the edge's weight. So, as Vasya was trying to represent characteristics of a recently invented robot in his program, he faced the following problem.
Vasya is not a programmer, so he asked his friend Gena, what the convenient way to store *n* integers is. Gena used to code in language X-- and so he can use only the types that occur in this language. Let's define, what a "type" is in language X--:
- First, a type is a string "int". - Second, a type is a string that starts with "pair", then followed by angle brackets listing exactly two comma-separated other types of language X--. This record contains no spaces. - No other strings can be regarded as types.
More formally: type := int | pair<type,type>. For example, Gena uses the following type for graph edges: pair<int,pair<int,int>>.
Gena was pleased to help Vasya, he dictated to Vasya a type of language X--, that stores *n* integers. Unfortunately, Gena was in a hurry, so he omitted the punctuation. Now Gena has already left and Vasya can't find the correct punctuation, resulting in a type of language X--, however hard he tries.
Help Vasya and add the punctuation marks so as to receive the valid type of language X--. Otherwise say that the task is impossible to perform. | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=105), showing how many numbers the type dictated by Gena contains.
The second line contains space-separated words, said by Gena. Each of them is either "pair" or "int" (without the quotes).
It is guaranteed that the total number of words does not exceed 105 and that among all the words that Gena said, there are exactly *n* words "int". | If it is possible to add the punctuation marks so as to get a correct type of language X-- as a result, print a single line that represents the resulting type. Otherwise, print "Error occurred" (without the quotes). Inside the record of a type should not be any extra spaces and other characters.
It is guaranteed that if such type exists, then it is unique.
Note that you should print the type dictated by Gena (if such type exists) and not any type that can contain *n* values. | [
"3\npair pair int int int\n",
"1\npair int\n"
] | [
"pair<pair<int,int>,int>",
"Error occurred"
] | none | [
{
"input": "3\npair pair int int int",
"output": "pair<pair<int,int>,int>"
},
{
"input": "1\npair int",
"output": "Error occurred"
},
{
"input": "4\npair pair int int pair int int",
"output": "pair<pair<int,int>,pair<int,int>>"
},
{
"input": "4\npair pair pair int int int int... | 92 | 0 | 0 | 11,205 | |
195 | Analyzing Polyline | [
"geometry",
"math",
"sortings"
] | null | null | As Valeric and Valerko were watching one of the last Euro Championship games in a sports bar, they broke a mug. Of course, the guys paid for it but the barman said that he will let them watch football in his bar only if they help his son complete a programming task. The task goes like that.
Let's consider a set of functions of the following form:
Valeric and Valerko really want to watch the next Euro Championship game, so they asked you to help them. | The first line contains integer *n* (1<=≤<=*n*<=≤<=105) — the number of functions. Each of the following *n* lines contains two space-separated integer numbers *k**i*,<=*b**i* (<=-<=109<=≤<=*k**i*,<=*b**i*<=≤<=109) that determine the *i*-th function. | Print a single number — the number of angles that do not equal 180 degrees in the graph of the polyline that equals the sum of the given functions. | [
"1\n1 0\n",
"3\n1 0\n0 2\n-1 1\n",
"3\n-2 -4\n1 7\n-5 1\n"
] | [
"1\n",
"2\n",
"3\n"
] | none | [
{
"input": "1\n1 0",
"output": "1"
},
{
"input": "3\n1 0\n0 2\n-1 1",
"output": "2"
},
{
"input": "3\n-2 -4\n1 7\n-5 1",
"output": "3"
},
{
"input": "10\n9 9\n-5 2\n-2 9\n0 6\n6 7\n-1 -10\n-8 6\n3 6\n-3 -9\n0 4",
"output": "8"
},
{
"input": "10\n-4 -9\n5 9\n-4 -1\... | 124 | 4,608,000 | -1 | 11,215 | |
634 | Island Puzzle | [
"constructive algorithms",
"implementation"
] | null | null | A remote island chain contains *n* islands, labeled 1 through *n*. Bidirectional bridges connect the islands to form a simple cycle — a bridge connects islands 1 and 2, islands 2 and 3, and so on, and additionally a bridge connects islands *n* and 1. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: First, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
Determine if it is possible for the islanders to arrange the statues in the desired order. | The first line contains a single integer *n* (2<=≤<=*n*<=≤<=200<=000) — the total number of islands.
The second line contains *n* space-separated integers *a**i* (0<=≤<=*a**i*<=≤<=*n*<=-<=1) — the statue currently placed on the *i*-th island. If *a**i*<==<=0, then the island has no statue. It is guaranteed that the *a**i* are distinct.
The third line contains *n* space-separated integers *b**i* (0<=≤<=*b**i*<=≤<=*n*<=-<=1) — the desired statues of the *i*th island. Once again, *b**i*<==<=0 indicates the island desires no statue. It is guaranteed that the *b**i* are distinct. | Print "YES" (without quotes) if the rearrangement can be done in the existing network, and "NO" otherwise. | [
"3\n1 0 2\n2 0 1\n",
"2\n1 0\n0 1\n",
"4\n1 2 3 0\n0 3 2 1\n"
] | [
"YES\n",
"YES\n",
"NO\n"
] | In the first sample, the islanders can first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2.
In the third sample, no sequence of movements results in the desired position. | [
{
"input": "3\n1 0 2\n2 0 1",
"output": "YES"
},
{
"input": "2\n1 0\n0 1",
"output": "YES"
},
{
"input": "4\n1 2 3 0\n0 3 2 1",
"output": "NO"
},
{
"input": "9\n3 8 4 6 7 1 5 2 0\n6 4 8 5 3 1 2 0 7",
"output": "NO"
},
{
"input": "4\n2 3 1 0\n2 0 1 3",
"output"... | 31 | 4,608,000 | 0 | 11,236 | |
813 | The Contest | [
"implementation"
] | null | null | Pasha is participating in a contest on one well-known website. This time he wants to win the contest and will do anything to get to the first place!
This contest consists of *n* problems, and Pasha solves *i*th problem in *a**i* time units (his solutions are always correct). At any moment of time he can be thinking about a solution to only one of the problems (that is, he cannot be solving two problems at the same time). The time Pasha spends to send his solutions is negligible. Pasha can send any number of solutions at the same moment.
Unfortunately, there are too many participants, and the website is not always working. Pasha received the information that the website will be working only during *m* time periods, *j*th period is represented by its starting moment *l**j* and ending moment *r**j*. Of course, Pasha can send his solution only when the website is working. In other words, Pasha can send his solution at some moment *T* iff there exists a period *x* such that *l**x*<=≤<=*T*<=≤<=*r**x*.
Pasha wants to know his best possible result. We need to tell him the minimal moment of time by which he is able to have solutions to all problems submitted, if he acts optimally, or say that it's impossible no matter how Pasha solves the problems. | The first line contains one integer *n* (1<=≤<=*n*<=≤<=1000) — the number of problems. The second line contains *n* integers *a**i* (1<=≤<=*a**i*<=≤<=105) — the time Pasha needs to solve *i*th problem.
The third line contains one integer *m* (0<=≤<=*m*<=≤<=1000) — the number of periods of time when the website is working. Next *m* lines represent these periods. *j*th line contains two numbers *l**j* and *r**j* (1<=≤<=*l**j*<=<<=*r**j*<=≤<=105) — the starting and the ending moment of *j*th period.
It is guaranteed that the periods are not intersecting and are given in chronological order, so for every *j*<=><=1 the condition *l**j*<=><=*r**j*<=-<=1 is met. | If Pasha can solve and submit all the problems before the end of the contest, print the minimal moment of time by which he can have all the solutions submitted.
Otherwise print "-1" (without brackets). | [
"2\n3 4\n2\n1 4\n7 9\n",
"1\n5\n1\n1 4\n",
"1\n5\n1\n1 5\n"
] | [
"7\n",
"-1\n",
"5\n"
] | In the first example Pasha can act like this: he solves the second problem in 4 units of time and sends it immediately. Then he spends 3 time units to solve the first problem and sends it 7 time units after the contest starts, because at this moment the website starts working again.
In the second example Pasha invents the solution only after the website stops working for the last time.
In the third example Pasha sends the solution exactly at the end of the first period. | [
{
"input": "2\n3 4\n2\n1 4\n7 9",
"output": "7"
},
{
"input": "1\n5\n1\n1 4",
"output": "-1"
},
{
"input": "1\n5\n1\n1 5",
"output": "5"
},
{
"input": "5\n100000 100000 100000 100000 100000\n0",
"output": "-1"
},
{
"input": "5\n886 524 128 4068 298\n3\n416 3755\n4... | 77 | 2,048,000 | -1 | 11,239 | |
991 | Bishwock | [
"dp",
"greedy"
] | null | null | Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square.
Vasya has a board with $2\times n$ squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can't put bishwocks there. However, pawns also don't attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn't occupy the same squares and wouldn't occupy squares with pawns. | The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed $100$. | Output a single integer — the maximum amount of bishwocks that can be placed onto the given board. | [
"00\n00\n",
"00X00X0XXX0\n0XXX0X00X00\n",
"0X0X0\n0X0X0\n",
"0XXX0\n00000\n"
] | [
"1",
"4",
"0",
"2"
] | none | [
{
"input": "00\n00",
"output": "1"
},
{
"input": "00X00X0XXX0\n0XXX0X00X00",
"output": "4"
},
{
"input": "0X0X0\n0X0X0",
"output": "0"
},
{
"input": "0XXX0\n00000",
"output": "2"
},
{
"input": "0\n0",
"output": "0"
},
{
"input": "0\nX",
"output": "... | 109 | 6,963,200 | 3 | 11,277 | |
638 | Road Improvement | [
"*special",
"dfs and similar",
"graphs",
"greedy",
"trees"
] | null | null | In Berland there are *n* cities and *n*<=-<=1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads.
In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day.
Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are. | The first line of the input contains a positive integer *n* (2<=≤<=*n*<=≤<=200<=000) — the number of cities in Berland.
Each of the next *n*<=-<=1 lines contains two numbers *u**i*, *v**i*, meaning that the *i*-th road connects city *u**i* and city *v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*, *u**i*<=≠<=*v**i*). | First print number *k* — the minimum number of days needed to repair all the roads in Berland.
In next *k* lines print the description of the roads that should be repaired on each of the *k* days. On the *i*-th line print first number *d**i* — the number of roads that should be repaired on the *i*-th day, and then *d**i* space-separated integers — the numbers of the roads that should be repaired on the *i*-th day. The roads are numbered according to the order in the input, starting from one.
If there are multiple variants, you can print any of them. | [
"4\n1 2\n3 4\n3 2\n",
"6\n3 4\n5 4\n3 2\n1 3\n4 6\n"
] | [
"2\n2 2 1\n1 3\n",
"3\n1 1 \n2 2 3 \n2 4 5 \n"
] | In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 — on the second day. | [
{
"input": "4\n1 2\n3 4\n3 2",
"output": "2\n2 1 2 \n1 3 "
},
{
"input": "6\n3 4\n5 4\n3 2\n1 3\n4 6",
"output": "3\n1 1 \n2 2 3 \n2 4 5 "
},
{
"input": "8\n1 3\n1 6\n3 4\n6 2\n5 6\n6 7\n7 8",
"output": "4\n3 2 3 7 \n2 1 4 \n1 5 \n1 6 "
},
{
"input": "5\n1 2\n1 3\n1 4\n1 5",
... | 2,000 | 67,686,400 | 0 | 11,287 | |
513 | Second price auction | [
"bitmasks",
"probabilities"
] | null | null | Nowadays, most of the internet advertisements are not statically linked to a web page. Instead, what will be shown to the person opening a web page is determined within 100 milliseconds after the web page is opened. Usually, multiple companies compete for each ad slot on the web page in an auction. Each of them receives a request with details about the user, web page and ad slot and they have to respond within those 100 milliseconds with a bid they would pay for putting an advertisement on that ad slot. The company that suggests the highest bid wins the auction and gets to place its advertisement. If there are several companies tied for the highest bid, the winner gets picked at random.
However, the company that won the auction does not have to pay the exact amount of its bid. In most of the cases, a second-price auction is used. This means that the amount paid by the company is equal to the maximum of all the other bids placed for this ad slot.
Let's consider one such bidding. There are *n* companies competing for placing an ad. The *i*-th of these companies will bid an integer number of microdollars equiprobably randomly chosen from the range between *L**i* and *R**i*, inclusive. In the other words, the value of the *i*-th company bid can be any integer from the range [*L**i*,<=*R**i*] with the same probability.
Determine the expected value that the winner will have to pay in a second-price auction. | The first line of input contains an integer number *n* (2<=≤<=*n*<=≤<=5). *n* lines follow, the *i*-th of them containing two numbers *L**i* and *R**i* (1<=≤<=*L**i*<=≤<=*R**i*<=≤<=10000) describing the *i*-th company's bid preferences.
This problem doesn't have subproblems. You will get 8 points for the correct submission. | Output the answer with absolute or relative error no more than 1*e*<=-<=9. | [
"3\n4 7\n8 10\n5 5\n",
"3\n2 5\n3 4\n1 6\n"
] | [
"5.7500000000\n",
"3.5000000000\n"
] | Consider the first example. The first company bids a random integer number of microdollars in range [4, 7]; the second company bids between 8 and 10, and the third company bids 5 microdollars. The second company will win regardless of the exact value it bids, however the price it will pay depends on the value of first company's bid. With probability 0.5 the first company will bid at most 5 microdollars, and the second-highest price of the whole auction will be 5. With probability 0.25 it will bid 6 microdollars, and with probability 0.25 it will bid 7 microdollars. Thus, the expected value the second company will have to pay is 0.5·5 + 0.25·6 + 0.25·7 = 5.75. | [
{
"input": "3\n4 7\n8 10\n5 5",
"output": "5.7500000000"
},
{
"input": "3\n2 5\n3 4\n1 6",
"output": "3.5000000000"
},
{
"input": "5\n1 10000\n1 10000\n1 10000\n1 10000\n1 10000",
"output": "6667.1666666646"
},
{
"input": "2\n1 2\n1 2",
"output": "1.2500000000"
},
{
... | 405 | 10,342,400 | 3 | 11,346 | |
204 | Little Elephant and Cards | [
"binary search",
"data structures"
] | null | null | The Little Elephant loves to play with color cards.
He has *n* cards, each has exactly two colors (the color of the front side and the color of the back side). Initially, all the cards lay on the table with the front side up. In one move the Little Elephant can turn any card to the other side. The Little Elephant thinks that a set of cards on the table is funny if at least half of the cards have the same color (for each card the color of the upper side is considered).
Help the Little Elephant to find the minimum number of moves needed to make the set of *n* cards funny. | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=105) — the number of the cards. The following *n* lines contain the description of all cards, one card per line. The cards are described by a pair of positive integers not exceeding 109 — colors of both sides. The first number in a line is the color of the front of the card, the second one — of the back. The color of the front of the card may coincide with the color of the back of the card.
The numbers in the lines are separated by single spaces. | On a single line print a single integer — the sought minimum number of moves. If it is impossible to make the set funny, print -1. | [
"3\n4 7\n4 7\n7 4\n",
"5\n4 7\n7 4\n2 11\n9 7\n1 1\n"
] | [
"0\n",
"2\n"
] | In the first sample there initially are three cards lying with colors 4, 4, 7. Since two of the three cards are of the same color 4, you do not need to change anything, so the answer is 0.
In the second sample, you can turn the first and the fourth cards. After that three of the five cards will be of color 7. | [
{
"input": "3\n4 7\n4 7\n7 4",
"output": "0"
},
{
"input": "5\n4 7\n7 4\n2 11\n9 7\n1 1",
"output": "2"
},
{
"input": "1\n1 1",
"output": "0"
},
{
"input": "2\n1 1\n1 1",
"output": "0"
},
{
"input": "7\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8",
"output": "-1"
},
... | 124 | 0 | 0 | 11,347 | |
635 | Orchestra | [
"brute force",
"implementation"
] | null | null | Paul is at the orchestra. The string section is arranged in an *r*<=×<=*c* rectangular grid and is filled with violinists with the exception of *n* violists. Paul really likes violas, so he would like to take a picture including at least *k* of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different. | The first line of input contains four space-separated integers *r*, *c*, *n*, *k* (1<=≤<=*r*,<=*c*,<=*n*<=≤<=10, 1<=≤<=*k*<=≤<=*n*) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next *n* lines each contain two integers *x**i* and *y**i* (1<=≤<=*x**i*<=≤<=*r*, 1<=≤<=*y**i*<=≤<=*c*): the position of the *i*-th viola. It is guaranteed that no location appears more than once in the input. | Print a single integer — the number of photographs Paul can take which include at least *k* violas. | [
"2 2 1 1\n1 2\n",
"3 2 3 3\n1 1\n3 1\n2 2\n",
"3 2 3 2\n1 1\n3 1\n2 2\n"
] | [
"4\n",
"1\n",
"4\n"
] | We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows
In the second sample, the orchestra looks as follows
In the third sample, the orchestra looks the same as in the second sample. | [
{
"input": "2 2 1 1\n1 2",
"output": "4"
},
{
"input": "3 2 3 3\n1 1\n3 1\n2 2",
"output": "1"
},
{
"input": "3 2 3 2\n1 1\n3 1\n2 2",
"output": "4"
},
{
"input": "1 1 1 1\n1 1",
"output": "1"
},
{
"input": "10 10 10 10\n6 1\n3 8\n10 6\n10 3\n10 4\n8 9\n2 3\n5 7\n... | 140 | 3,686,400 | 3 | 11,364 | |
510 | Fox And Two Dots | [
"dfs and similar"
] | null | null | Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size *n*<=×<=*m* cells, like this:
Each cell contains a dot that has some color. We will use different uppercase Latin characters to express different colors.
The key of this game is to find a cycle that contain dots of same color. Consider 4 blue dots on the picture forming a circle as an example. Formally, we call a sequence of dots *d*1,<=*d*2,<=...,<=*d**k* a cycle if and only if it meets the following condition:
1. These *k* dots are different: if *i*<=≠<=*j* then *d**i* is different from *d**j*. 1. *k* is at least 4. 1. All dots belong to the same color. 1. For all 1<=≤<=*i*<=≤<=*k*<=-<=1: *d**i* and *d**i*<=+<=1 are adjacent. Also, *d**k* and *d*1 should also be adjacent. Cells *x* and *y* are called adjacent if they share an edge.
Determine if there exists a cycle on the field. | The first line contains two integers *n* and *m* (2<=≤<=*n*,<=*m*<=≤<=50): the number of rows and columns of the board.
Then *n* lines follow, each line contains a string consisting of *m* characters, expressing colors of dots in each line. Each character is an uppercase Latin letter. | Output "Yes" if there exists a cycle, and "No" otherwise. | [
"3 4\nAAAA\nABCA\nAAAA\n",
"3 4\nAAAA\nABCA\nAADA\n",
"4 4\nYYYR\nBYBY\nBBBY\nBBBY\n",
"7 6\nAAAAAB\nABBBAB\nABAAAB\nABABBB\nABAAAB\nABBBAB\nAAAAAB\n",
"2 13\nABCDEFGHIJKLM\nNOPQRSTUVWXYZ\n"
] | [
"Yes\n",
"No\n",
"Yes\n",
"Yes\n",
"No\n"
] | In first sample test all 'A' form a cycle.
In second sample there is no such cycle.
The third sample is displayed on the picture above ('Y' = Yellow, 'B' = Blue, 'R' = Red). | [
{
"input": "3 4\nAAAA\nABCA\nAAAA",
"output": "Yes"
},
{
"input": "3 4\nAAAA\nABCA\nAADA",
"output": "No"
},
{
"input": "4 4\nYYYR\nBYBY\nBBBY\nBBBY",
"output": "Yes"
},
{
"input": "7 6\nAAAAAB\nABBBAB\nABAAAB\nABABBB\nABAAAB\nABBBAB\nAAAAAB",
"output": "Yes"
},
{
... | 62 | 0 | -1 | 11,370 | |
549 | The Game Of Parity | [
"games"
] | null | null | There are *n* cities in Westeros. The *i*-th city is inhabited by *a**i* people. Daenerys and Stannis play the following game: in one single move, a player chooses a certain town and burns it to the ground. Thus all its residents, sadly, die. Stannis starts the game. The game ends when Westeros has exactly *k* cities left.
The prophecy says that if the total number of surviving residents is even, then Daenerys wins: Stannis gets beheaded, and Daenerys rises on the Iron Throne. If the total number of surviving residents is odd, Stannis wins and everything goes in the completely opposite way.
Lord Petyr Baelish wants to know which candidates to the throne he should support, and therefore he wonders, which one of them has a winning strategy. Answer to this question of Lord Baelish and maybe you will become the next Lord of Harrenholl. | The first line contains two positive space-separated integers, *n* and *k* (1<=≤<=*k*<=≤<=*n*<=≤<=2·105) — the initial number of cities in Westeros and the number of cities at which the game ends.
The second line contains *n* space-separated positive integers *a**i* (1<=≤<=*a**i*<=≤<=106), which represent the population of each city in Westeros. | Print string "Daenerys" (without the quotes), if Daenerys wins and "Stannis" (without the quotes), if Stannis wins. | [
"3 1\n1 2 1\n",
"3 1\n2 2 1\n",
"6 3\n5 20 12 7 14 101\n"
] | [
"Stannis\n",
"Daenerys\n",
"Stannis\n"
] | In the first sample Stannis will use his move to burn a city with two people and Daenerys will be forced to burn a city with one resident. The only survivor city will have one resident left, that is, the total sum is odd, and thus Stannis wins.
In the second sample, if Stannis burns a city with two people, Daenerys burns the city with one resident, or vice versa. In any case, the last remaining city will be inhabited by two people, that is, the total sum is even, and hence Daenerys wins. | [
{
"input": "3 1\n1 2 1",
"output": "Stannis"
},
{
"input": "3 1\n2 2 1",
"output": "Daenerys"
},
{
"input": "6 3\n5 20 12 7 14 101",
"output": "Stannis"
},
{
"input": "6 3\n346 118 330 1403 5244 480",
"output": "Daenerys"
},
{
"input": "7 4\n11 3532 99 3512 12 8 2... | 155 | 13,824,000 | 0 | 11,379 | |
313 | Ilya and Roads | [
"dp"
] | null | null | Everything is great about Ilya's city, except the roads. The thing is, the only ZooVille road is represented as *n* holes in a row. We will consider the holes numbered from 1 to *n*, from left to right.
Ilya is really keep on helping his city. So, he wants to fix at least *k* holes (perharps he can fix more) on a single ZooVille road.
The city has *m* building companies, the *i*-th company needs *c**i* money units to fix a road segment containing holes with numbers of at least *l**i* and at most *r**i*. The companies in ZooVille are very greedy, so, if they fix a segment containing some already fixed holes, they do not decrease the price for fixing the segment.
Determine the minimum money Ilya will need to fix at least *k* holes. | The first line contains three integers *n*,<=*m*,<=*k* (1<=≤<=*n*<=≤<=300,<=1<=≤<=*m*<=≤<=105,<=1<=≤<=*k*<=≤<=*n*). The next *m* lines contain the companies' description. The *i*-th line contains three integers *l**i*,<=*r**i*,<=*c**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*,<=1<=≤<=*c**i*<=≤<=109). | Print a single integer — the minimum money Ilya needs to fix at least *k* holes.
If it is impossible to fix at least *k* holes, print -1.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"10 4 6\n7 9 11\n6 9 13\n7 7 7\n3 5 6\n",
"10 7 1\n3 4 15\n8 9 8\n5 6 8\n9 10 6\n1 4 2\n1 4 10\n8 10 13\n",
"10 1 9\n5 10 14\n"
] | [
"17\n",
"2\n",
"-1\n"
] | none | [
{
"input": "10 4 6\n7 9 11\n6 9 13\n7 7 7\n3 5 6",
"output": "17"
},
{
"input": "10 7 1\n3 4 15\n8 9 8\n5 6 8\n9 10 6\n1 4 2\n1 4 10\n8 10 13",
"output": "2"
},
{
"input": "10 1 9\n5 10 14",
"output": "-1"
},
{
"input": "10 6 9\n6 8 7\n2 8 11\n2 6 10\n8 10 9\n2 5 8\n2 3 8",
... | 60 | 0 | 0 | 11,380 | |
329 | The Evil Temple and the Moving Rocks | [
"constructive algorithms"
] | null | null | Important: All possible tests are in the pretest, so you shouldn't hack on this problem. So, if you passed pretests, you will also pass the system test.
You are an adventurer currently journeying inside an evil temple. After defeating a couple of weak monsters, you arrived at a square room consisting of tiles forming an *n*<=×<=*n* grid, surrounded entirely by walls. At the end of the room lies a door locked with evil magical forces. The following inscriptions are written on the door:
Being a very senior adventurer, you immediately realize what this means. In the room next door lies an infinite number of magical rocks. There are four types of rocks:
- '^': this rock moves upwards; - '<': this rock moves leftwards; - '>': this rock moves rightwards; - 'v': this rock moves downwards.
To open the door, you first need to place the rocks on some of the tiles (one tile can be occupied by at most one rock). Then, you select a single rock that you have placed and activate it. The activated rock will then move in its direction until it hits another rock or hits the walls of the room (the rock will not move if something already blocks it in its chosen direction). The rock then deactivates. If it hits the walls, or if there have been already 107 events of rock becoming activated, the movements end. Otherwise, the rock that was hit becomes activated and this procedure is repeated.
If a rock moves at least one cell before hitting either the wall or another rock, the hit produces a sound. The door will open once the number of produced sounds is at least *x*. It is okay for the rocks to continue moving after producing *x* sounds.
The following picture illustrates the four possible scenarios of moving rocks.
- Moves at least one cell, then hits another rock. A sound is produced, the hit rock becomes activated. - Moves at least one cell, then hits the wall (i.e., the side of the room). A sound is produced, the movements end. - Does not move because a rock is already standing in the path. The blocking rock becomes activated, but no sounds are produced. - Does not move because the wall is in the way. No sounds are produced and the movements end.
Assume there's an infinite number of rocks of each type in the neighboring room. You know what to do: place the rocks and open the door! | The first line will consists of two integers *n* and *x*, denoting the size of the room and the number of sounds required to open the door. There will be exactly three test cases for this problem:
- *n*<==<=5,<=*x*<==<=5; - *n*<==<=3,<=*x*<==<=2; - *n*<==<=100,<=*x*<==<=105.
All of these testcases are in pretest. | Output *n* lines. Each line consists of *n* characters — the *j*-th character of the *i*-th line represents the content of the tile at the *i*-th row and the *j*-th column, and should be one of these:
- '^', '<', '>', or 'v': a rock as described in the problem statement. - '.': an empty tile.
Then, output two integers *r* and *c* (1<=≤<=*r*,<=*c*<=≤<=*n*) on the next line — this means that the rock you activate first is located at the *r*-th row from above and *c*-th column from the left. There must be a rock in this cell.
If there are multiple solutions, you may output any of them. | [
"5 5\n",
"3 2\n"
] | [
">...v\nv.<..\n..^..\n>....\n..^.<\n1 1\n",
">vv\n^<.\n^.<\n1 3\n"
] | Here's a simulation of the first example, accompanied with the number of sounds produced so far.
In the picture above, the activated rock switches between the '^' rock and the '<' rock. However, no sound is produced since the '^' rock didn't move even a single tile. So, still 4 sound.
At this point, 5 sound are already produced, so this solution is already correct. However, for the sake of example, we will continue simulating what happens.
And the movement stops. In total, it produces 8 sounds. Notice that the last move produced sound.
Here's a simulation of the second example:
Now, the activated stone will switch continuously from one to another without producing a sound until it reaches the 10<sup class="upper-index">7</sup> limit, after which the movement will cease.
In total, it produced exactly 2 sounds, so the solution is correct. | [
{
"input": "5 5",
"output": ">...v\nv.<..\n..^..\n>....\n..^.<\n1 1"
},
{
"input": "3 2",
"output": ">vv\n^<.\n^.<\n1 3"
},
{
"input": "100 100000",
"output": ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>v.\n^v<.<.<.<.<.<.<.<.<.<.... | 122 | 0 | 0 | 11,393 | |
0 | none | [
"none"
] | null | null | In the official contest this problem has a different statement, for which jury's solution was working incorrectly, and for this reason it was excluded from the contest. This mistake have been fixed and the current given problem statement and model solution corresponds to what jury wanted it to be during the contest.
Vova and Lesha are friends. They often meet at Vova's place and compete against each other in a computer game named The Ancient Papyri: Swordsink. Vova always chooses a warrior as his fighter and Leshac chooses an archer. After that they should choose initial positions for their characters and start the fight. A warrior is good at melee combat, so Vova will try to make the distance between fighters as small as possible. An archer prefers to keep the enemy at a distance, so Lesha will try to make the initial distance as large as possible.
There are *n* (*n* is always even) possible starting positions for characters marked along the *Ox* axis. The positions are given by their distinct coordinates *x*1,<=*x*2,<=...,<=*x**n*, two characters cannot end up at the same position.
Vova and Lesha take turns banning available positions, Vova moves first. During each turn one of the guys bans exactly one of the remaining positions. Banned positions cannot be used by both Vova and Lesha. They continue to make moves until there are only two possible positions remaining (thus, the total number of moves will be *n*<=-<=2). After that Vova's character takes the position with the lesser coordinate and Lesha's character takes the position with the bigger coordinate and the guys start fighting.
Vova and Lesha are already tired by the game of choosing positions, as they need to play it before every fight, so they asked you (the developer of the The Ancient Papyri: Swordsink) to write a module that would automatically determine the distance at which the warrior and the archer will start fighting if both Vova and Lesha play optimally. | The first line on the input contains a single integer *n* (2<=≤<=*n*<=≤<=200<=000, *n* is even) — the number of positions available initially. The second line contains *n* distinct integers *x*1,<=*x*2,<=...,<=*x**n* (0<=≤<=*x**i*<=≤<=109), giving the coordinates of the corresponding positions. | Print the distance between the warrior and the archer at the beginning of the fight, provided that both Vova and Lesha play optimally. | [
"6\n0 1 3 7 15 31\n",
"2\n73 37\n"
] | [
"7\n",
"36\n"
] | In the first sample one of the optimum behavior of the players looks like that:
1. Vova bans the position at coordinate 15; 1. Lesha bans the position at coordinate 3; 1. Vova bans the position at coordinate 31; 1. Lesha bans the position at coordinate 1.
After these actions only positions 0 and 7 will remain, and the distance between them is equal to 7.
In the second sample there are only two possible positions, so there will be no bans. | [
{
"input": "6\n0 1 3 7 15 31",
"output": "7"
},
{
"input": "2\n73 37",
"output": "36"
},
{
"input": "2\n0 1000000000",
"output": "1000000000"
},
{
"input": "8\n729541013 135019377 88372488 319157478 682081360 558614617 258129110 790518782",
"output": "470242129"
},
{
... | 46 | 0 | -1 | 11,398 | |
939 | Maximize! | [
"binary search",
"greedy",
"ternary search",
"two pointers"
] | null | null | You are given a multiset *S* consisting of positive integers (initially empty). There are two kind of queries:
1. Add a positive integer to *S*, the newly added integer is not less than any number in it. 1. Find a subset *s* of the set *S* such that the value is maximum possible. Here *max*(*s*) means maximum value of elements in *s*, — the average value of numbers in *s*. Output this maximum possible value of . | The first line contains a single integer *Q* (1<=≤<=*Q*<=≤<=5·105) — the number of queries.
Each of the next *Q* lines contains a description of query. For queries of type 1 two integers 1 and *x* are given, where *x* (1<=≤<=*x*<=≤<=109) is a number that you should add to *S*. It's guaranteed that *x* is not less than any number in *S*. For queries of type 2, a single integer 2 is given.
It's guaranteed that the first query has type 1, i. e. *S* is not empty when a query of type 2 comes. | Output the answer for each query of the second type in the order these queries are given in input. Each number should be printed in separate line.
Your answer is considered correct, if each of your answers has absolute or relative error not greater than 10<=-<=6.
Formally, let your answer be *a*, and the jury's answer be *b*. Your answer is considered correct if . | [
"6\n1 3\n2\n1 4\n2\n1 8\n2\n",
"4\n1 1\n1 4\n1 5\n2\n"
] | [
"0.0000000000\n0.5000000000\n3.0000000000\n",
"2.0000000000\n"
] | none | [
{
"input": "6\n1 3\n2\n1 4\n2\n1 8\n2",
"output": "0.0000000000\n0.5000000000\n3.0000000000"
},
{
"input": "4\n1 1\n1 4\n1 5\n2",
"output": "2.0000000000"
},
{
"input": "8\n1 7\n1 26\n1 40\n1 45\n1 64\n2\n1 88\n1 94",
"output": "31.6666666667"
},
{
"input": "9\n1 35\n2\n2\n1 ... | 1,980 | 59,596,800 | 3 | 11,418 | |
118 | Present from Lena | [
"constructive algorithms",
"implementation"
] | null | null | Vasya's birthday is approaching and Lena decided to sew a patterned handkerchief to him as a present. Lena chose digits from 0 to *n* as the pattern. The digits will form a rhombus. The largest digit *n* should be located in the centre. The digits should decrease as they approach the edges. For example, for *n*<==<=5 the handkerchief pattern should look like that:
Your task is to determine the way the handkerchief will look like by the given *n*. | The first line contains the single integer *n* (2<=≤<=*n*<=≤<=9). | Print a picture for the given *n*. You should strictly observe the number of spaces before the first digit on each line. Every two adjacent digits in the same line should be separated by exactly one space. There should be no spaces after the last digit at the end of each line. | [
"2\n",
"3\n"
] | [
"0\n 0 1 0\n0 1 2 1 0\n 0 1 0\n 0\n",
"0\n 0 1 0\n 0 1 2 1 0\n0 1 2 3 2 1 0\n 0 1 2 1 0\n 0 1 0\n 0\n"
] | none | [
{
"input": "2",
"output": " 0\n 0 1 0\n0 1 2 1 0\n 0 1 0\n 0"
},
{
"input": "3",
"output": " 0\n 0 1 0\n 0 1 2 1 0\n0 1 2 3 2 1 0\n 0 1 2 1 0\n 0 1 0\n 0"
},
{
"input": "4",
"output": " 0\n 0 1 0\n 0 1 2 1 0\n 0 1 2 3 2 1 0\n0 1 2 3 4 3 2 1 0... | 60 | 0 | 0 | 11,422 | |
101 | Buses | [
"binary search",
"data structures",
"dp"
] | B. Buses | 2 | 265 | Little boy Gerald studies at school which is quite far from his house. That's why he has to go there by bus every day. The way from home to school is represented by a segment of a straight line; the segment contains exactly *n*<=+<=1 bus stops. All of them are numbered with integers from 0 to *n* in the order in which they follow from Gerald's home. The bus stop by Gerald's home has number 0 and the bus stop by the school has number *n*.
There are *m* buses running between the house and the school: the *i*-th bus goes from stop *s**i* to *t**i* (*s**i*<=<<=*t**i*), visiting all the intermediate stops in the order in which they follow on the segment. Besides, Gerald's no idiot and he wouldn't get off the bus until it is still possible to ride on it closer to the school (obviously, getting off would be completely pointless). In other words, Gerald can get on the *i*-th bus on any stop numbered from *s**i* to *t**i*<=-<=1 inclusive, but he can get off the *i*-th bus only on the bus stop *t**i*.
Gerald can't walk between the bus stops and he also can't move in the direction from the school to the house.
Gerald wants to know how many ways he has to get from home to school. Tell him this number. Two ways are considered different if Gerald crosses some segment between the stops on different buses. As the number of ways can be too much, find the remainder of a division of this number by 1000000007 (109<=+<=7). | The first line contains two space-separated integers: *n* and *m* (1<=≤<=*n*<=≤<=109,<=0<=≤<=*m*<=≤<=105). Then follow *m* lines each containing two integers *s**i*,<=*t**i*. They are the numbers of starting stops and end stops of the buses (0<=≤<=*s**i*<=<<=*t**i*<=≤<=*n*). | Print the only number — the number of ways to get to the school modulo 1000000007 (109<=+<=7). | [
"2 2\n0 1\n1 2\n",
"3 2\n0 1\n1 2\n",
"5 5\n0 1\n0 2\n0 3\n0 4\n0 5\n"
] | [
"1\n",
"0\n",
"16\n"
] | The first test has the only variant to get to school: first on bus number one to the bus stop number one; then on bus number two to the bus stop number two.
In the second test no bus goes to the third bus stop, where the school is positioned. Thus, the correct answer is 0.
In the third test Gerald can either get or not on any of the first four buses to get closer to the school. Thus, the correct answer is 2<sup class="upper-index">4</sup> = 16. | [
{
"input": "2 2\n0 1\n1 2",
"output": "1"
},
{
"input": "3 2\n0 1\n1 2",
"output": "0"
},
{
"input": "5 5\n0 1\n0 2\n0 3\n0 4\n0 5",
"output": "16"
},
{
"input": "3 3\n1 2\n2 3\n1 3",
"output": "0"
},
{
"input": "10 10\n0 1\n0 2\n0 3\n0 4\n0 5\n0 6\n0 7\n0 8\n0 9\... | 2,000 | 31,027,200 | 0 | 11,427 |
1,005 | Tanya and Stairways | [
"implementation"
] | null | null | Little girl Tanya climbs the stairs inside a multi-storey building. Every time Tanya climbs a stairway, she starts counting steps from $1$ to the number of steps in this stairway. She speaks every number aloud. For example, if she climbs two stairways, the first of which contains $3$ steps, and the second contains $4$ steps, she will pronounce the numbers $1, 2, 3, 1, 2, 3, 4$.
You are given all the numbers pronounced by Tanya. How many stairways did she climb? Also, output the number of steps in each stairway.
The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways. | The first line contains $n$ ($1 \le n \le 1000$) — the total number of numbers pronounced by Tanya.
The second line contains integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) — all the numbers Tanya pronounced while climbing the stairs, in order from the first to the last pronounced number. Passing a stairway with $x$ steps, she will pronounce the numbers $1, 2, \dots, x$ in that order.
The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways. | In the first line, output $t$ — the number of stairways that Tanya climbed. In the second line, output $t$ numbers — the number of steps in each stairway she climbed. Write the numbers in the correct order of passage of the stairways. | [
"7\n1 2 3 1 2 3 4\n",
"4\n1 1 1 1\n",
"5\n1 2 3 4 5\n",
"5\n1 2 1 2 1\n"
] | [
"2\n3 4 ",
"4\n1 1 1 1 ",
"1\n5 ",
"3\n2 2 1 "
] | none | [
{
"input": "7\n1 2 3 1 2 3 4",
"output": "2\n3 4 "
},
{
"input": "4\n1 1 1 1",
"output": "4\n1 1 1 1 "
},
{
"input": "5\n1 2 3 4 5",
"output": "1\n5 "
},
{
"input": "5\n1 2 1 2 1",
"output": "3\n2 2 1 "
},
{
"input": "1\n1",
"output": "1\n1 "
},
{
"inp... | 46 | 0 | 0 | 11,485 | |
629 | Babaei and Birthday Cake | [
"data structures",
"dp"
] | null | null | As you know, every birthday party has a cake! This time, Babaei is going to prepare the very special birthday party's cake.
Simple cake is a cylinder of some radius and height. The volume of the simple cake is equal to the volume of corresponding cylinder. Babaei has *n* simple cakes and he is going to make a special cake placing some cylinders on each other.
However, there are some additional culinary restrictions. The cakes are numbered in such a way that the cake number *i* can be placed only on the table or on some cake number *j* where *j*<=<<=*i*. Moreover, in order to impress friends Babaei will put the cake *i* on top of the cake *j* only if the volume of the cake *i* is strictly greater than the volume of the cake *j*.
Babaei wants to prepare a birthday cake that has a maximum possible total volume. Help him find this value. | The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=100<=000) — the number of simple cakes Babaei has.
Each of the following *n* lines contains two integers *r**i* and *h**i* (1<=≤<=*r**i*,<=*h**i*<=≤<=10<=000), giving the radius and height of the *i*-th cake. | Print the maximum volume of the cake that Babaei can make. Your 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 . | [
"2\n100 30\n40 10\n",
"4\n1 1\n9 7\n1 4\n10 7\n"
] | [
"942477.796077000\n",
"3983.539484752\n"
] | In first sample, the optimal way is to choose the cake number 1.
In second sample, the way to get the maximum volume is to use cakes with indices 1, 2 and 4. | [
{
"input": "2\n100 30\n40 10",
"output": "942477.796077000"
},
{
"input": "4\n1 1\n9 7\n1 4\n10 7",
"output": "3983.539484752"
},
{
"input": "3\n2 2\n1 1\n3 3",
"output": "109.955742876"
},
{
"input": "3\n2 2\n3 3\n1 1",
"output": "109.955742876"
},
{
"input": "3\... | 0 | 0 | -1 | 11,499 | |
739 | Alyona and towers | [
"data structures"
] | null | null | Alyona has built *n* towers by putting small cubes some on the top of others. Each cube has size 1<=×<=1<=×<=1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row.
Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from *l**i* to *r**i* and adds *d**i* cubes on the top of them.
Let the sequence *a*1,<=*a*2,<=...,<=*a**n* be the heights of the towers from left to right. Let's call as a segment of towers *a**l*,<=*a**l*<=+<=1,<=...,<=*a**r* a hill if the following condition holds: there is integer *k* (*l*<=≤<=*k*<=≤<=*r*) such that *a**l*<=<<=*a**l*<=+<=1<=<<=*a**l*<=+<=2<=<<=...<=<<=*a**k*<=><=*a**k*<=+<=1<=><=*a**k*<=+<=2<=><=...<=><=*a**r*.
After each addition of *d**i* cubes on the top of the towers from *l**i* to *r**i*, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. | The first line contain single integer *n* (1<=≤<=*n*<=≤<=3·105) — the number of towers.
The second line contain *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109) — the number of cubes in each tower.
The third line contain single integer *m* (1<=≤<=*m*<=≤<=3·105) — the number of additions.
The next *m* lines contain 3 integers each. The *i*-th of these lines contains integers *l**i*, *r**i* and *d**i* (1<=≤<=*l*<=≤<=*r*<=≤<=*n*, 1<=≤<=*d**i*<=≤<=109), that mean that Alyona puts *d**i* cubes on the tio of each of the towers from *l**i* to *r**i*. | Print *m* lines. In *i*-th line print the maximum width of the hills after the *i*-th addition. | [
"5\n5 5 5 5 5\n3\n1 3 2\n2 2 1\n4 4 1\n"
] | [
"2\n4\n5\n"
] | The first sample is as follows:
After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2.
After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4.
After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5. | [] | 0 | 0 | -1 | 11,520 | |
364 | Ghd | [
"brute force",
"math",
"probabilities"
] | null | null | John Doe offered his sister Jane Doe find the gcd of some set of numbers *a*.
Gcd is a positive integer *g*, such that all number from the set are evenly divisible by *g* and there isn't such *g*' (*g*'<=><=*g*), that all numbers of the set are evenly divisible by *g*'.
Unfortunately Jane couldn't cope with the task and John offered her to find the ghd of the same subset of numbers.
Ghd is a positive integer *g*, such that at least half of numbers from the set are evenly divisible by *g* and there isn't such *g*' (*g*'<=><=*g*) that at least half of the numbers from the set are evenly divisible by *g*'.
Jane coped with the task for two hours. Please try it, too. | The first line contains an integer *n* (1<=≤<=*n*<=≤<=106) showing how many numbers are in set *a*. The second line contains space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=1012). Please note, that given set can contain equal numbers.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the %I64d specifier. | Print a single integer *g* — the Ghd of set *a*. | [
"6\n6 2 3 4 5 6\n",
"5\n5 5 6 10 15\n"
] | [
"3\n",
"5\n"
] | none | [
{
"input": "6\n6 2 3 4 5 6",
"output": "3"
},
{
"input": "5\n5 5 6 10 15",
"output": "5"
},
{
"input": "100\n32 40 7 3 7560 21 7560 7560 10 12 3 7560 7560 7560 7560 5 7560 7560 6 7560 7560 7560 35 7560 18 7560 7560 7560 7560 7560 48 2 7 25 7560 2 2 49 7560 7560 15 16 7560 7560 2 7560 27 ... | 4,000 | 114,585,600 | 0 | 11,533 | |
679 | Bear and Tower of Cubes | [
"binary search",
"dp",
"greedy"
] | null | null | Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length.
A block with side *a* has volume *a*3. A tower consisting of blocks with sides *a*1,<=*a*2,<=...,<=*a**k* has the total volume *a*13<=+<=*a*23<=+<=...<=+<=*a**k*3.
Limak is going to build a tower. First, he asks you to tell him a positive integer *X* — the required total volume of the tower. Then, Limak adds new blocks greedily, one by one. Each time he adds the biggest block such that the total volume doesn't exceed *X*.
Limak asks you to choose *X* not greater than *m*. Also, he wants to maximize the number of blocks in the tower at the end (however, he still behaves greedily). Secondarily, he wants to maximize *X*.
Can you help Limak? Find the maximum number of blocks his tower can have and the maximum *X*<=≤<=*m* that results this number of blocks. | The only line of the input contains one integer *m* (1<=≤<=*m*<=≤<=1015), meaning that Limak wants you to choose *X* between 1 and *m*, inclusive. | Print two integers — the maximum number of blocks in the tower and the maximum required total volume *X*, resulting in the maximum number of blocks. | [
"48\n",
"6\n"
] | [
"9 42\n",
"6 6\n"
] | In the first sample test, there will be 9 blocks if you choose *X* = 23 or *X* = 42. Limak wants to maximize *X* secondarily so you should choose 42.
In more detail, after choosing *X* = 42 the process of building a tower is:
- Limak takes a block with side 3 because it's the biggest block with volume not greater than 42. The remaining volume is 42 - 27 = 15. - The second added block has side 2, so the remaining volume is 15 - 8 = 7. - Finally, Limak adds 7 blocks with side 1, one by one.
So, there are 9 blocks in the tower. The total volume is is 3<sup class="upper-index">3</sup> + 2<sup class="upper-index">3</sup> + 7·1<sup class="upper-index">3</sup> = 27 + 8 + 7 = 42. | [
{
"input": "48",
"output": "9 42"
},
{
"input": "6",
"output": "6 6"
},
{
"input": "1",
"output": "1 1"
},
{
"input": "994",
"output": "12 941"
},
{
"input": "567000123",
"output": "16 566998782"
},
{
"input": "123830583943",
"output": "17 12383056... | 31 | 307,200 | -1 | 11,534 | |
0 | none | [
"none"
] | null | null | ZS the Coder is playing a game. There is a number displayed on the screen and there are two buttons, '<=+<=' (plus) and '' (square root). Initially, the number 2 is displayed on the screen. There are *n*<=+<=1 levels in the game and ZS the Coder start at the level 1.
When ZS the Coder is at level *k*, he can :
1. Press the '<=+<=' button. This increases the number on the screen by exactly *k*. So, if the number on the screen was *x*, it becomes *x*<=+<=*k*.1. Press the '' button. Let the number on the screen be *x*. After pressing this button, the number becomes . After that, ZS the Coder levels up, so his current level becomes *k*<=+<=1. This button can only be pressed when *x* is a perfect square, i.e. *x*<==<=*m*2 for some positive integer *m*.
Additionally, after each move, if ZS the Coder is at level *k*, and the number on the screen is *m*, then *m* must be a multiple of *k*. Note that this condition is only checked after performing the press. For example, if ZS the Coder is at level 4 and current number is 100, he presses the '' button and the number turns into 10. Note that at this moment, 10 is not divisible by 4, but this press is still valid, because after it, ZS the Coder is at level 5, and 10 is divisible by 5.
ZS the Coder needs your help in beating the game — he wants to reach level *n*<=+<=1. In other words, he needs to press the '' button *n* times. Help him determine the number of times he should press the '<=+<=' button before pressing the '' button at each level.
Please note that ZS the Coder wants to find just any sequence of presses allowing him to reach level *n*<=+<=1, but not necessarily a sequence minimizing the number of presses. | The first and only line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=100<=000), denoting that ZS the Coder wants to reach level *n*<=+<=1. | Print *n* non-negative integers, one per line. *i*-th of them should be equal to the number of times that ZS the Coder needs to press the '<=+<=' button before pressing the '' button at level *i*.
Each number in the output should not exceed 1018. However, the number on the screen can be greater than 1018.
It is guaranteed that at least one solution exists. If there are multiple solutions, print any of them. | [
"3\n",
"2\n",
"4\n"
] | [
"14\n16\n46\n",
"999999999999999998\n44500000000\n",
"2\n17\n46\n97\n"
] | In the first sample case:
On the first level, ZS the Coder pressed the ' + ' button 14 times (and the number on screen is initially 2), so the number became 2 + 14·1 = 16. Then, ZS the Coder pressed the '<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c77ded9b8209a8cb488cc2ec7b7fe1dae32a5309.png" style="max-width: 100.0%;max-height: 100.0%;"/>' button, and the number became <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c3d2663f5f74e9220fd5cbccbfaf4ca76ef7284f.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
After that, on the second level, ZS pressed the ' + ' button 16 times, so the number becomes 4 + 16·2 = 36. Then, ZS pressed the '<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c77ded9b8209a8cb488cc2ec7b7fe1dae32a5309.png" style="max-width: 100.0%;max-height: 100.0%;"/>' button, levelling up and changing the number into <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/49ab1d31f1435b7c7b96550d63a35be671d3d85a.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
After that, on the third level, ZS pressed the ' + ' button 46 times, so the number becomes 6 + 46·3 = 144. Then, ZS pressed the '<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c77ded9b8209a8cb488cc2ec7b7fe1dae32a5309.png" style="max-width: 100.0%;max-height: 100.0%;"/>' button, levelling up and changing the number into <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/499b57d4b7ba5e1e0957767cc182808ca48ef722.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
Note that 12 is indeed divisible by 4, so ZS the Coder can reach level 4.
Also, note that pressing the ' + ' button 10 times on the third level before levelling up does not work, because the number becomes 6 + 10·3 = 36, and when the '<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c77ded9b8209a8cb488cc2ec7b7fe1dae32a5309.png" style="max-width: 100.0%;max-height: 100.0%;"/>' button is pressed, the number becomes <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/49ab1d31f1435b7c7b96550d63a35be671d3d85a.png" style="max-width: 100.0%;max-height: 100.0%;"/> and ZS the Coder is at Level 4. However, 6 is not divisible by 4 now, so this is not a valid solution.
In the second sample case:
On the first level, ZS the Coder pressed the ' + ' button 999999999999999998 times (and the number on screen is initially 2), so the number became 2 + 999999999999999998·1 = 10<sup class="upper-index">18</sup>. Then, ZS the Coder pressed the '<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c77ded9b8209a8cb488cc2ec7b7fe1dae32a5309.png" style="max-width: 100.0%;max-height: 100.0%;"/>' button, and the number became <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/f07f2a60ab6cecbd2507861a0df57a16a015fd86.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
After that, on the second level, ZS pressed the ' + ' button 44500000000 times, so the number becomes 10<sup class="upper-index">9</sup> + 44500000000·2 = 9·10<sup class="upper-index">10</sup>. Then, ZS pressed the '<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c77ded9b8209a8cb488cc2ec7b7fe1dae32a5309.png" style="max-width: 100.0%;max-height: 100.0%;"/>' button, levelling up and changing the number into <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4c4d8829d987a7bcfd597cd1aa101327a66c0eca.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
Note that 300000 is a multiple of 3, so ZS the Coder can reach level 3. | [
{
"input": "3",
"output": "2\n17\n46"
},
{
"input": "2",
"output": "2\n17"
},
{
"input": "4",
"output": "2\n17\n46\n97"
},
{
"input": "1",
"output": "2"
},
{
"input": "100000",
"output": "2\n17\n46\n97\n176\n289\n442\n641\n892\n1201\n1574\n2017\n2536\n3137\n38... | 342 | 9,113,600 | 3 | 11,552 | |
343 | Water Tree | [
"data structures",
"dfs and similar",
"graphs",
"trees"
] | null | null | Mad scientist Mike has constructed a rooted tree, which consists of *n* vertices. Each vertex is a reservoir which can be either empty or filled with water.
The vertices of the tree are numbered from 1 to *n* with the root at vertex 1. For each vertex, the reservoirs of its children are located below the reservoir of this vertex, and the vertex is connected with each of the children by a pipe through which water can flow downwards.
Mike wants to do the following operations with the tree:
1. Fill vertex *v* with water. Then *v* and all its children are filled with water. 1. Empty vertex *v*. Then *v* and all its ancestors are emptied. 1. Determine whether vertex *v* is filled with water at the moment.
Mike has already compiled a full list of operations that he wants to perform in order. Before experimenting with the tree Mike decided to run the list through a simulation. Help Mike determine what results will he get after performing all the operations. | The first line of the input contains an integer *n* (1<=≤<=*n*<=≤<=500000) — the number of vertices in the tree. Each of the following *n*<=-<=1 lines contains two space-separated numbers *a**i*, *b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*, *a**i*<=≠<=*b**i*) — the edges of the tree.
The next line contains a number *q* (1<=≤<=*q*<=≤<=500000) — the number of operations to perform. Each of the following *q* lines contains two space-separated numbers *c**i* (1<=≤<=*c**i*<=≤<=3), *v**i* (1<=≤<=*v**i*<=≤<=*n*), where *c**i* is the operation type (according to the numbering given in the statement), and *v**i* is the vertex on which the operation is performed.
It is guaranteed that the given graph is a tree. | For each type 3 operation print 1 on a separate line if the vertex is full, and 0 if the vertex is empty. Print the answers to queries in the order in which the queries are given in the input. | [
"5\n1 2\n5 1\n2 3\n4 2\n12\n1 1\n2 3\n3 1\n3 2\n3 3\n3 4\n1 2\n2 4\n3 1\n3 3\n3 4\n3 5\n"
] | [
"0\n0\n0\n1\n0\n1\n0\n1\n"
] | none | [
{
"input": "5\n1 2\n5 1\n2 3\n4 2\n12\n1 1\n2 3\n3 1\n3 2\n3 3\n3 4\n1 2\n2 4\n3 1\n3 3\n3 4\n3 5",
"output": "0\n0\n0\n1\n0\n1\n0\n1"
},
{
"input": "1\n1\n3 1",
"output": "0"
},
{
"input": "2\n1 2\n13\n1 1\n3 1\n3 2\n2 1\n3 1\n3 2\n2 2\n3 1\n3 2\n1 1\n2 2\n3 1\n3 2",
"output": "1\n1... | 0 | 0 | -1 | 11,670 | |
711 | Coloring Trees | [
"dp"
] | null | null | ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the park where *n* trees grow. They decided to be naughty and color the trees in the park. The trees are numbered with integers from 1 to *n* from left to right.
Initially, tree *i* has color *c**i*. ZS the Coder and Chris the Baboon recognizes only *m* different colors, so 0<=≤<=*c**i*<=≤<=*m*, where *c**i*<==<=0 means that tree *i* is uncolored.
ZS the Coder and Chris the Baboon decides to color only the uncolored trees, i.e. the trees with *c**i*<==<=0. They can color each of them them in any of the *m* colors from 1 to *m*. Coloring the *i*-th tree with color *j* requires exactly *p**i*,<=*j* litres of paint.
The two friends define the beauty of a coloring of the trees as the minimum number of contiguous groups (each group contains some subsegment of trees) you can split all the *n* trees into so that each group contains trees of the same color. For example, if the colors of the trees from left to right are 2,<=1,<=1,<=1,<=3,<=2,<=2,<=3,<=1,<=3, the beauty of the coloring is 7, since we can partition the trees into 7 contiguous groups of the same color : {2},<={1,<=1,<=1},<={3},<={2,<=2},<={3},<={1},<={3}.
ZS the Coder and Chris the Baboon wants to color all uncolored trees so that the beauty of the coloring is exactly *k*. They need your help to determine the minimum amount of paint (in litres) needed to finish the job.
Please note that the friends can't color the trees that are already colored. | The first line contains three integers, *n*, *m* and *k* (1<=≤<=*k*<=≤<=*n*<=≤<=100, 1<=≤<=*m*<=≤<=100) — the number of trees, number of colors and beauty of the resulting coloring respectively.
The second line contains *n* integers *c*1,<=*c*2,<=...,<=*c**n* (0<=≤<=*c**i*<=≤<=*m*), the initial colors of the trees. *c**i* equals to 0 if the tree number *i* is uncolored, otherwise the *i*-th tree has color *c**i*.
Then *n* lines follow. Each of them contains *m* integers. The *j*-th number on the *i*-th of them line denotes *p**i*,<=*j* (1<=≤<=*p**i*,<=*j*<=≤<=109) — the amount of litres the friends need to color *i*-th tree with color *j*. *p**i*,<=*j*'s are specified even for the initially colored trees, but such trees still can't be colored. | Print a single integer, the minimum amount of paint needed to color the trees. If there are no valid tree colorings of beauty *k*, print <=-<=1. | [
"3 2 2\n0 0 0\n1 2\n3 4\n5 6\n",
"3 2 2\n2 1 2\n1 3\n2 4\n3 5\n",
"3 2 2\n2 0 0\n1 3\n2 4\n3 5\n",
"3 2 3\n2 1 2\n1 3\n2 4\n3 5\n"
] | [
"10",
"-1",
"5",
"0"
] | In the first sample case, coloring the trees with colors 2, 1, 1 minimizes the amount of paint used, which equals to 2 + 3 + 5 = 10. Note that 1, 1, 1 would not be valid because the beauty of such coloring equals to 1 ({1, 1, 1} is a way to group the trees into a single group of the same color).
In the second sample case, all the trees are colored, but the beauty of the coloring is 3, so there is no valid coloring, and the answer is - 1.
In the last sample case, all the trees are colored and the beauty of the coloring matches *k*, so no paint is used and the answer is 0. | [
{
"input": "3 2 2\n0 0 0\n1 2\n3 4\n5 6",
"output": "10"
},
{
"input": "3 2 2\n2 1 2\n1 3\n2 4\n3 5",
"output": "-1"
},
{
"input": "3 2 2\n2 0 0\n1 3\n2 4\n3 5",
"output": "5"
},
{
"input": "3 2 3\n2 1 2\n1 3\n2 4\n3 5",
"output": "0"
},
{
"input": "3 2 3\n0 0 0\n... | 1,606 | 57,651,200 | 3 | 11,699 | |
913 | Too Easy Problems | [
"binary search",
"brute force",
"data structures",
"greedy",
"sortings"
] | null | null | You are preparing for an exam on scheduling theory. The exam will last for exactly *T* milliseconds and will consist of *n* problems. You can either solve problem *i* in exactly *t**i* milliseconds or ignore it and spend no time. You don't need time to rest after solving a problem, either.
Unfortunately, your teacher considers some of the problems too easy for you. Thus, he assigned an integer *a**i* to every problem *i* meaning that the problem *i* can bring you a point to the final score only in case you have solved no more than *a**i* problems overall (including problem *i*).
Formally, suppose you solve problems *p*1,<=*p*2,<=...,<=*p**k* during the exam. Then, your final score *s* will be equal to the number of values of *j* between 1 and *k* such that *k*<=≤<=*a**p**j*.
You have guessed that the real first problem of the exam is already in front of you. Therefore, you want to choose a set of problems to solve during the exam maximizing your final score in advance. Don't forget that the exam is limited in time, and you must have enough time to solve all chosen problems. If there exist different sets of problems leading to the maximum final score, any of them will do. | The first line contains two integers *n* and *T* (1<=≤<=*n*<=≤<=2·105; 1<=≤<=*T*<=≤<=109) — the number of problems in the exam and the length of the exam in milliseconds, respectively.
Each of the next *n* lines contains two integers *a**i* and *t**i* (1<=≤<=*a**i*<=≤<=*n*; 1<=≤<=*t**i*<=≤<=104). The problems are numbered from 1 to *n*. | In the first line, output a single integer *s* — your maximum possible final score.
In the second line, output a single integer *k* (0<=≤<=*k*<=≤<=*n*) — the number of problems you should solve.
In the third line, output *k* distinct integers *p*1,<=*p*2,<=...,<=*p**k* (1<=≤<=*p**i*<=≤<=*n*) — the indexes of problems you should solve, in any order.
If there are several optimal sets of problems, you may output any of them. | [
"5 300\n3 100\n4 150\n4 80\n2 90\n2 300\n",
"2 100\n1 787\n2 788\n",
"2 100\n2 42\n2 58\n"
] | [
"2\n3\n3 1 4\n",
"0\n0\n\n",
"2\n2\n1 2\n"
] | In the first example, you should solve problems 3, 1, and 4. In this case you'll spend 80 + 100 + 90 = 270 milliseconds, falling within the length of the exam, 300 milliseconds (and even leaving yourself 30 milliseconds to have a rest). Problems 3 and 1 will bring you a point each, while problem 4 won't. You'll score two points.
In the second example, the length of the exam is catastrophically not enough to solve even a single problem.
In the third example, you have just enough time to solve both problems in 42 + 58 = 100 milliseconds and hand your solutions to the teacher with a smile. | [
{
"input": "5 300\n3 100\n4 150\n4 80\n2 90\n2 300",
"output": "2\n2\n3 4"
},
{
"input": "2 100\n1 787\n2 788",
"output": "0\n0"
},
{
"input": "2 100\n2 42\n2 58",
"output": "2\n2\n1 2"
},
{
"input": "1 1\n1 1",
"output": "1\n1\n1"
},
{
"input": "10 481\n4 25\n3 8... | 1,730 | 39,321,600 | 3 | 11,766 | |
799 | Cut the pie | [
"binary search",
"data structures",
"geometry"
] | null | null | Arkady reached the *n*-th level in Township game, so Masha decided to bake a pie for him! Of course, the pie has a shape of convex *n*-gon, i.e. a polygon with *n* vertices.
Arkady decided to cut the pie in two equal in area parts by cutting it by a straight line, so that he can eat one of them and give the other to Masha. There is a difficulty because Arkady has already put a knife at some point of the pie, so he now has to cut the pie by a straight line passing trough this point.
Help Arkady: find a line that passes through the point Arkady has put a knife into and cuts the pie into two parts of equal area, or determine that it's impossible. Your program has to quickly answer many queries with the same pie, but different points in which Arkady puts a knife. | The first line contains two integers *n* and *q* (3<=≤<=*n*<=≤<=104, 1<=≤<=*q*<=≤<=105) — the number of vertices in the pie and the number of queries.
*n* line follow describing the polygon vertices in clockwise order. The *i*-th of these line contains two integers *x**i* and *y**i* (<=-<=106<=≤<=*x**i*,<=*y**i*<=≤<=106) — the coordinates of the *i*-th vertex. It is guaranteed that the polygon is strictly convex, in particular, no three vertices line on the same line.
An empty line follows.
*q* lines follow describing the query points. The *i*-th of these lines contain two integers *x**i* and *y**i* (<=-<=106<=≤<=*x**i*,<=*y**i*<=≤<=106) — the coordinates of the point in which Arkady puts the knife in the *i*-th query. In is guaranteed that in each query the given point is strictly inside the polygon, in particular, is not on its edges. | For each query print single integer — the polar angle of the line that is the answer for the corresponding query, in radians. The angle should be in the segment [0;π], the angles are measured from the direction of *OX* axis in counter-clockwise order. For example, the polar angle of the *OY* axis is . If there is no answer in that query, print -1.
If there are several answers, print any of them. Your answer is considered correct if the difference between the areas of the parts divided by the total area of the polygon doesn't exceed 10<=-<=4 by absolute value. In other words, if *a* and *b* are the areas of the parts after the cut, then your answer is correct if and only of . | [
"3 1\n0 0\n0 3\n3 0\n\n1 1\n",
"5 3\n6 5\n6 3\n5 0\n0 0\n0 5\n\n5 4\n3 3\n5 2\n"
] | [
"2.67794504460098710000\n",
"0.60228734612690049000\n1.27933953226473580000\n2.85805511179015910000\n"
] | none | [] | 46 | 0 | 0 | 11,773 | |
461 | Appleman and a Sheet of Paper | [
"data structures",
"implementation"
] | null | null | Appleman has a very big sheet of paper. This sheet has a form of rectangle with dimensions 1<=×<=*n*. Your task is help Appleman with folding of such a sheet. Actually, you need to perform *q* queries. Each query will have one of the following types:
1. Fold the sheet of paper at position *p**i*. After this query the leftmost part of the paper with dimensions 1<=×<=*p**i* must be above the rightmost part of the paper with dimensions 1<=×<=([*current* *width* *of* *sheet*]<=-<=*p**i*). 1. Count what is the total width of the paper pieces, if we will make two described later cuts and consider only the pieces between the cuts. We will make one cut at distance *l**i* from the left border of the current sheet of paper and the other at distance *r**i* from the left border of the current sheet of paper.
Please look at the explanation of the first test example for better understanding of the problem. | The first line contains two integers: *n* and *q* (1<=<=≤<=*n*<=≤<=105; 1<=≤<=*q*<=≤<=105) — the width of the paper and the number of queries.
Each of the following *q* lines contains one of the described queries in the following format:
- "1 *p**i*" (1<=≤<=*p**i*<=<<=[*current* *width* *of* *sheet*]) — the first type query. - "2 *l**i* *r**i*" (0<=≤<=*l**i*<=<<=*r**i*<=≤<=[*current* *width* *of* *sheet*]) — the second type query. | For each query of the second type, output the answer. | [
"7 4\n1 3\n1 2\n2 0 1\n2 1 2\n",
"10 9\n2 2 9\n1 1\n2 0 1\n1 8\n2 0 8\n1 2\n2 1 3\n1 4\n2 2 4\n"
] | [
"4\n3\n",
"7\n2\n10\n4\n5\n"
] | The pictures below show the shapes of the paper during the queries of the first example:
After the first fold operation the sheet has width equal to 4, after the second one the width of the sheet equals to 2. | [
{
"input": "7 4\n1 3\n1 2\n2 0 1\n2 1 2",
"output": "4\n3"
},
{
"input": "10 9\n2 2 9\n1 1\n2 0 1\n1 8\n2 0 8\n1 2\n2 1 3\n1 4\n2 2 4",
"output": "7\n2\n10\n4\n5"
},
{
"input": "10 5\n2 1 9\n2 4 10\n1 1\n2 0 1\n2 0 1",
"output": "8\n6\n2\n2"
},
{
"input": "10 10\n2 5 9\n2 2 9... | 46 | 0 | 0 | 11,778 | |
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 ... | 1,000 | 10,035,200 | 0 | 11,789 |
0 | none | [
"none"
] | null | null | Levko loves array *a*1,<=*a*2,<=... ,<=*a**n*, consisting of integers, very much. That is why Levko is playing with array *a*, performing all sorts of operations with it. Each operation Levko performs is of one of two types:
1. Increase all elements from *l**i* to *r**i* by *d**i*. In other words, perform assignments *a**j*<==<=*a**j*<=+<=*d**i* for all *j* that meet the inequation *l**i*<=≤<=*j*<=≤<=*r**i*. 1. Find the maximum of elements from *l**i* to *r**i*. That is, calculate the value .
Sadly, Levko has recently lost his array. Fortunately, Levko has records of all operations he has performed on array *a*. Help Levko, given the operation records, find at least one suitable array. The results of all operations for the given array must coincide with the record results. Levko clearly remembers that all numbers in his array didn't exceed 109 in their absolute value, so he asks you to find such an array. | The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=5000) — the size of the array and the number of operations in Levko's records, correspondingly.
Next *m* lines describe the operations, the *i*-th line describes the *i*-th operation. The first integer in the *i*-th line is integer *t**i* (1<=≤<=*t**i*<=≤<=2) that describes the operation type. If *t**i*<==<=1, then it is followed by three integers *l**i*, *r**i* and *d**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*, <=-<=104<=≤<=*d**i*<=≤<=104) — the description of the operation of the first type. If *t**i*<==<=2, then it is followed by three integers *l**i*, *r**i* and *m**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*, <=-<=5·107<=≤<=*m**i*<=≤<=5·107) — the description of the operation of the second type.
The operations are given in the order Levko performed them on his array. | In the first line print "YES" (without the quotes), if the solution exists and "NO" (without the quotes) otherwise.
If the solution exists, then on the second line print *n* integers *a*1,<=*a*2,<=... ,<=*a**n* (|*a**i*|<=≤<=109) — the recovered array. | [
"4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 8\n",
"4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 13\n"
] | [
"YES\n4 7 4 7",
"NO\n"
] | none | [
{
"input": "4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 8",
"output": "YES\n8 7 4 7 "
},
{
"input": "4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 13",
"output": "NO"
},
{
"input": "97 29\n2 78 82 356152\n2 14 29 430177\n1 59 84 3680\n1 49 89 -2247\n1 92 96 3701\n2 54 89 377271\n1 62... | 46 | 0 | 0 | 11,799 | |
845 | Driving Test | [
"data structures",
"dp",
"greedy"
] | null | null | Polycarp has just attempted to pass the driving test. He ran over the straight road with the signs of four types.
- speed limit: this sign comes with a positive integer number — maximal speed of the car after the sign (cancel the action of the previous sign of this type); - overtake is allowed: this sign means that after some car meets it, it can overtake any other car; - no speed limit: this sign cancels speed limit if any (car can move with arbitrary speed after this sign); - no overtake allowed: some car can't overtake any other car after this sign.
Polycarp goes past the signs consequentially, each new sign cancels the action of all the previous signs of it's kind (speed limit/overtake). It is possible that two or more "no overtake allowed" signs go one after another with zero "overtake is allowed" signs between them. It works with "no speed limit" and "overtake is allowed" signs as well.
In the beginning of the ride overtake is allowed and there is no speed limit.
You are given the sequence of events in chronological order — events which happened to Polycarp during the ride. There are events of following types:
1. Polycarp changes the speed of his car to specified (this event comes with a positive integer number); 1. Polycarp's car overtakes the other car; 1. Polycarp's car goes past the "speed limit" sign (this sign comes with a positive integer); 1. Polycarp's car goes past the "overtake is allowed" sign; 1. Polycarp's car goes past the "no speed limit"; 1. Polycarp's car goes past the "no overtake allowed";
It is guaranteed that the first event in chronological order is the event of type 1 (Polycarp changed the speed of his car to specified).
After the exam Polycarp can justify his rule violations by telling the driving instructor that he just didn't notice some of the signs. What is the minimal number of signs Polycarp should say he didn't notice, so that he would make no rule violations from his point of view? | The first line contains one integer number *n* (1<=≤<=*n*<=≤<=2·105) — number of events.
Each of the next *n* lines starts with integer *t* (1<=≤<=*t*<=≤<=6) — the type of the event.
An integer *s* (1<=≤<=*s*<=≤<=300) follows in the query of the first and the third type (if it is the query of first type, then it's new speed of Polycarp's car, if it is the query of third type, then it's new speed limit).
It is guaranteed that the first event in chronological order is the event of type 1 (Polycarp changed the speed of his car to specified). | Print the minimal number of road signs Polycarp should say he didn't notice, so that he would make no rule violations from his point of view. | [
"11\n1 100\n3 70\n4\n2\n3 120\n5\n3 120\n6\n1 150\n4\n3 300\n",
"5\n1 100\n3 200\n2\n4\n5\n",
"7\n1 20\n2\n6\n4\n6\n6\n2\n"
] | [
"2\n",
"0\n",
"2\n"
] | In the first example Polycarp should say he didn't notice the "speed limit" sign with the limit of 70 and the second "speed limit" sign with the limit of 120.
In the second example Polycarp didn't make any rule violation.
In the third example Polycarp should say he didn't notice both "no overtake allowed" that came after "overtake is allowed" sign. | [
{
"input": "11\n1 100\n3 70\n4\n2\n3 120\n5\n3 120\n6\n1 150\n4\n3 300",
"output": "2"
},
{
"input": "5\n1 100\n3 200\n2\n4\n5",
"output": "0"
},
{
"input": "7\n1 20\n2\n6\n4\n6\n6\n2",
"output": "2"
},
{
"input": "1\n1 100",
"output": "0"
},
{
"input": "2\n1 100\... | 608 | 1,536,000 | 3 | 11,812 | |
0 | none | [
"none"
] | null | null | You are an experienced Codeforces user. Today you found out that during your activity on Codeforces you have made *y* submissions, out of which *x* have been successful. Thus, your current success rate on Codeforces is equal to *x*<=/<=*y*.
Your favorite rational number in the [0;1] range is *p*<=/<=*q*. Now you wonder: what is the smallest number of submissions you have to make if you want your success rate to be *p*<=/<=*q*? | The first line contains a single integer *t* (1<=≤<=*t*<=≤<=1000) — the number of test cases.
Each of the next *t* lines contains four integers *x*, *y*, *p* and *q* (0<=≤<=*x*<=≤<=*y*<=≤<=109; 0<=≤<=*p*<=≤<=*q*<=≤<=109; *y*<=><=0; *q*<=><=0).
It is guaranteed that *p*<=/<=*q* is an irreducible fraction.
Hacks. For hacks, an additional constraint of *t*<=≤<=5 must be met. | For each test case, output a single integer equal to the smallest number of submissions you have to make if you want your success rate to be equal to your favorite rational number, or -1 if this is impossible to achieve. | [
"4\n3 10 1 2\n7 14 3 8\n20 70 2 7\n5 6 1 1\n"
] | [
"4\n10\n0\n-1\n"
] | In the first example, you have to make 4 successful submissions. Your success rate will be equal to 7 / 14, or 1 / 2.
In the second example, you have to make 2 successful and 8 unsuccessful submissions. Your success rate will be equal to 9 / 24, or 3 / 8.
In the third example, there is no need to make any new submissions. Your success rate is already equal to 20 / 70, or 2 / 7.
In the fourth example, the only unsuccessful submission breaks your hopes of having the success rate equal to 1. | [
{
"input": "4\n3 10 1 2\n7 14 3 8\n20 70 2 7\n5 6 1 1",
"output": "4\n10\n0\n-1"
},
{
"input": "8\n0 1 0 1\n0 2 1 2\n0 3 1 1\n1 2 0 1\n1 2 1 1\n2 2 0 1\n3 3 1 2\n4 4 1 1",
"output": "0\n2\n-1\n-1\n-1\n-1\n3\n0"
},
{
"input": "5\n1 1000000000 1 2\n1 1000000000 1 2\n1 1000000000 1 2\n1 100... | 124 | 7,065,600 | -1 | 11,818 | |
0 | none | [
"none"
] | null | null | Vasya's telephone contains *n* photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo *n*. Similarly, by swiping right from the last photo you reach photo 1. It takes *a* seconds to swipe from photo to adjacent.
For each photo it is known which orientation is intended for it — horizontal or vertical. Phone is in the vertical orientation and can't be rotated. It takes *b* second to change orientation of the photo.
Vasya has *T* seconds to watch photos. He want to watch as many photos as possible. If Vasya opens the photo for the first time, he spends 1 second to notice all details in it. If photo is in the wrong orientation, he spends *b* seconds on rotating it before watching it. If Vasya has already opened the photo, he just skips it (so he doesn't spend any time for watching it or for changing its orientation). It is not allowed to skip unseen photos.
Help Vasya find the maximum number of photos he is able to watch during *T* seconds. | The first line of the input contains 4 integers *n*,<=*a*,<=*b*,<=*T* (1<=≤<=*n*<=≤<=5·105, 1<=≤<=*a*,<=*b*<=≤<=1000, 1<=≤<=*T*<=≤<=109) — the number of photos, time to move from a photo to adjacent, time to change orientation of a photo and time Vasya can spend for watching photo.
Second line of the input contains a string of length *n* containing symbols 'w' and 'h'.
If the *i*-th position of a string contains 'w', then the photo *i* should be seen in the horizontal orientation.
If the *i*-th position of a string contains 'h', then the photo *i* should be seen in vertical orientation. | Output the only integer, the maximum number of photos Vasya is able to watch during those *T* seconds. | [
"4 2 3 10\nwwhw\n",
"5 2 4 13\nhhwhh\n",
"5 2 4 1000\nhhwhh\n",
"3 1 100 10\nwhw\n"
] | [
"2\n",
"4\n",
"5\n",
"0\n"
] | In the first sample test you can rotate the first photo (3 seconds), watch the first photo (1 seconds), move left (2 second), rotate fourth photo (3 seconds), watch fourth photo (1 second). The whole process takes exactly 10 seconds.
Note that in the last sample test the time is not enough even to watch the first photo, also you can't skip it. | [
{
"input": "4 2 3 10\nwwhw",
"output": "2"
},
{
"input": "5 2 4 13\nhhwhh",
"output": "4"
},
{
"input": "5 2 4 1000\nhhwhh",
"output": "5"
},
{
"input": "3 1 100 10\nwhw",
"output": "0"
},
{
"input": "10 2 3 32\nhhwwhwhwwh",
"output": "7"
},
{
"input":... | 0 | 0 | -1 | 11,821 | |
463 | Caisa and Sugar | [
"brute force",
"implementation"
] | null | null | Caisa is going to have a party and he needs to buy the ingredients for a big chocolate cake. For that he is going to the biggest supermarket in town.
Unfortunately, he has just *s* dollars for sugar. But that's not a reason to be sad, because there are *n* types of sugar in the supermarket, maybe he able to buy one. But that's not all. The supermarket has very unusual exchange politics: instead of cents the sellers give sweets to a buyer as a change. Of course, the number of given sweets always doesn't exceed 99, because each seller maximizes the number of dollars in the change (100 cents can be replaced with a dollar).
Caisa wants to buy only one type of sugar, also he wants to maximize the number of sweets in the change. What is the maximum number of sweets he can get? Note, that Caisa doesn't want to minimize the cost of the sugar, he only wants to get maximum number of sweets as change. | The first line contains two space-separated integers *n*,<=*s* (1<=≤<=*n*,<=*s*<=≤<=100).
The *i*-th of the next *n* lines contains two integers *x**i*, *y**i* (1<=≤<=*x**i*<=≤<=100; 0<=≤<=*y**i*<=<<=100), where *x**i* represents the number of dollars and *y**i* the number of cents needed in order to buy the *i*-th type of sugar. | Print a single integer representing the maximum number of sweets he can buy, or -1 if he can't buy any type of sugar. | [
"5 10\n3 90\n12 0\n9 70\n5 50\n7 0\n",
"5 5\n10 10\n20 20\n30 30\n40 40\n50 50\n"
] | [
"50\n",
"-1\n"
] | In the first test sample Caisa can buy the fourth type of sugar, in such a case he will take 50 sweets as a change. | [
{
"input": "5 10\n3 90\n12 0\n9 70\n5 50\n7 0",
"output": "50"
},
{
"input": "5 5\n10 10\n20 20\n30 30\n40 40\n50 50",
"output": "-1"
},
{
"input": "1 2\n1 0",
"output": "0"
},
{
"input": "2 10\n20 99\n30 99",
"output": "-1"
},
{
"input": "15 21\n16 51\n33 44\n32 ... | 30 | 0 | 0 | 11,825 | |
22 | Bargaining Table | [
"brute force",
"dp"
] | B. Bargaining Table | 2 | 256 | Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room *n*<=×<=*m* meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office. | The first line contains 2 space-separated numbers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=25) — the office room dimensions. Then there follow *n* lines with *m* characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free. | Output one number — the maximum possible perimeter of a bargaining table for Bob's office room. | [
"3 3\n000\n010\n000\n",
"5 4\n1100\n0000\n0000\n0000\n0000\n"
] | [
"8\n",
"16\n"
] | none | [
{
"input": "3 3\n000\n010\n000",
"output": "8"
},
{
"input": "5 4\n1100\n0000\n0000\n0000\n0000",
"output": "16"
},
{
"input": "3 3\n000\n110\n000",
"output": "8"
},
{
"input": "4 2\n00\n10\n11\n00",
"output": "6"
},
{
"input": "3 5\n00001\n00000\n10100",
"out... | 342 | 26,009,600 | 3.866053 | 11,829 |
272 | Dima and Sequence | [
"implementation",
"math"
] | null | null | Dima got into number sequences. Now he's got sequence *a*1,<=*a*2,<=...,<=*a**n*, consisting of *n* positive integers. Also, Dima has got a function *f*(*x*), which can be defined with the following recurrence:
- *f*(0)<==<=0; - *f*(2·*x*)<==<=*f*(*x*); - *f*(2·*x*<=+<=1)<==<=*f*(*x*)<=+<=1.
Dima wonders, how many pairs of indexes (*i*,<=*j*) (1<=≤<=*i*<=<<=*j*<=≤<=*n*) are there, such that *f*(*a**i*)<==<=*f*(*a**j*). Help him, count the number of such pairs. | The first line contains integer *n* (1<=≤<=*n*<=≤<=105). The second line contains *n* positive integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109).
The numbers in the lines are separated by single spaces. | In a single line print the answer to the problem.
Please, don't use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"3\n1 2 4\n",
"3\n5 3 1\n"
] | [
"3\n",
"1\n"
] | In the first sample any pair (*i*, *j*) will do, so the answer is 3.
In the second sample only pair (1, 2) will do. | [
{
"input": "3\n1 2 4",
"output": "3"
},
{
"input": "3\n5 3 1",
"output": "1"
},
{
"input": "2\n469264357 996569493",
"output": "0"
},
{
"input": "6\n396640239 62005863 473635171 329666981 510631133 207643327",
"output": "2"
},
{
"input": "8\n851991424 32517099 310... | 154 | 0 | 0 | 11,835 | |
659 | New Reform | [
"data structures",
"dfs and similar",
"dsu",
"graphs",
"greedy"
] | null | null | Berland has *n* cities connected by *m* bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform. | The first line of the input contains two positive integers, *n* and *m* — the number of the cities and the number of roads in Berland (2<=≤<=*n*<=≤<=100<=000, 1<=≤<=*m*<=≤<=100<=000).
Next *m* lines contain the descriptions of the roads: the *i*-th road is determined by two distinct integers *x**i*,<=*y**i* (1<=≤<=*x**i*,<=*y**i*<=≤<=*n*, *x**i*<=≠<=*y**i*), where *x**i* and *y**i* are the numbers of the cities connected by the *i*-th road.
It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads. | Print a single integer — the minimum number of separated cities after the reform. | [
"4 3\n2 1\n1 3\n4 3\n",
"5 5\n2 1\n1 3\n2 3\n2 5\n4 3\n",
"6 5\n1 2\n2 3\n4 5\n4 6\n5 6\n"
] | [
"1\n",
"0\n",
"1\n"
] | In the first sample the following road orientation is allowed: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e5b18c46402af724bd3841d549d5d6f52fc16253.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/04481aced8a9d501ae5d785ab654c542ff5497a1.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/0d220a75a508edc0d540dbaec5e198345049b66f.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
The second sample: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e5b18c46402af724bd3841d549d5d6f52fc16253.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/030fc9181b578c2d906254d38dc56da5554323eb.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/29f71c065c3536e88b54429c734103ad3604f68b.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/a6011a68d6de05246ad6ba8aa24a5c5c71cd450a.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/0d220a75a508edc0d540dbaec5e198345049b66f.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
The third sample: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e5b18c46402af724bd3841d549d5d6f52fc16253.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/29f71c065c3536e88b54429c734103ad3604f68b.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7113d83514735a488d7b85262585381d26986195.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/98300067e46d036076e429b208db829296ebca9d.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/146f3f3d208399fbab4d1d8c2800f07bf7b463e5.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [
{
"input": "4 3\n2 1\n1 3\n4 3",
"output": "1"
},
{
"input": "5 5\n2 1\n1 3\n2 3\n2 5\n4 3",
"output": "0"
},
{
"input": "6 5\n1 2\n2 3\n4 5\n4 6\n5 6",
"output": "1"
},
{
"input": "4 4\n1 2\n2 3\n3 4\n4 1",
"output": "0"
},
{
"input": "10 45\n3 5\n2 3\n4 8\n2 5\n... | 998 | 43,417,600 | 3 | 11,837 | |
280 | Game on Tree | [
"implementation",
"math",
"probabilities",
"trees"
] | null | null | Momiji has got a rooted tree, consisting of *n* nodes. The tree nodes are numbered by integers from 1 to *n*. The root has number 1. Momiji decided to play a game on this tree.
The game consists of several steps. On each step, Momiji chooses one of the remaining tree nodes (let's denote it by *v*) and removes all the subtree nodes with the root in node *v* from the tree. Node *v* gets deleted as well. The game finishes when the tree has no nodes left. In other words, the game finishes after the step that chooses the node number 1.
Each time Momiji chooses a new node uniformly among all the remaining nodes. Your task is to find the expectation of the number of steps in the described game. | The first line contains integer *n* (1<=≤<=*n*<=≤<=105) — the number of nodes in the tree. The next *n*<=-<=1 lines contain the tree edges. The *i*-th line contains integers *a**i*, *b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*; *a**i*<=≠<=*b**i*) — the numbers of the nodes that are connected by the *i*-th edge.
It is guaranteed that the given graph is a tree. | Print a single real number — the expectation of the number of steps in the described game.
The answer will be considered correct if the absolute or relative error doesn't exceed 10<=-<=6. | [
"2\n1 2\n",
"3\n1 2\n1 3\n"
] | [
"1.50000000000000000000\n",
"2.00000000000000000000\n"
] | In the first sample, there are two cases. One is directly remove the root and another is remove the root after one step. Thus the expected steps are:
In the second sample, things get more complex. There are two cases that reduce to the first sample, and one case cleaned at once. Thus the expected steps are: | [
{
"input": "2\n1 2",
"output": "1.50000000000000000000"
},
{
"input": "3\n1 2\n1 3",
"output": "2.00000000000000000000"
},
{
"input": "10\n1 2\n2 3\n3 4\n1 5\n2 6\n6 7\n4 8\n6 9\n9 10",
"output": "3.81666666666666690000"
},
{
"input": "6\n1 3\n2 4\n5 6\n3 6\n5 4",
"output... | 1,000 | 10,342,400 | 0 | 11,840 | |
150 | Smart Cheater | [
"data structures",
"math",
"probabilities"
] | null | null | I guess there's not much point in reminding you that Nvodsk winters aren't exactly hot. That increased the popularity of the public transport dramatically. The route of bus 62 has exactly *n* stops (stop 1 goes first on its way and stop *n* goes last). The stops are positioned on a straight line and their coordinates are 0<==<=*x*1<=<<=*x*2<=<<=...<=<<=*x**n*.
Each day exactly *m* people use bus 62. For each person we know the number of the stop where he gets on the bus and the number of the stop where he gets off the bus. A ticket from stop *a* to stop *b* (*a*<=<<=*b*) costs *x**b*<=-<=*x**a* rubles. However, the conductor can choose no more than one segment NOT TO SELL a ticket for. We mean that conductor should choose C and D (С <= D) and sell a ticket for the segments [*A*, *C*] and [*D*, *B*], or not sell the ticket at all. The conductor and the passenger divide the saved money between themselves equally. The conductor's "untaxed income" is sometimes interrupted by inspections that take place as the bus drives on some segment of the route located between two consecutive stops. The inspector fines the conductor by *c* rubles for each passenger who doesn't have the ticket for this route's segment.
You know the coordinated of all stops *x**i*; the numbers of stops where the *i*-th passenger gets on and off, *a**i* and *b**i* (*a**i*<=<<=*b**i*); the fine *c*; and also *p**i* — the probability of inspection on segment between the *i*-th and the *i*<=+<=1-th stop. The conductor asked you to help him make a plan of selling tickets that maximizes the mathematical expectation of his profit. | The first line contains three integers *n*, *m* and *c* (2<=≤<=*n*<=≤<=150<=000, 1<=≤<=*m*<=≤<=300<=000, 1<=≤<=*c*<=≤<=10<=000).
The next line contains *n* integers *x**i* (0<=≤<=*x**i*<=≤<=109, *x*1<==<=0, *x**i*<=<<=*x**i*<=+<=1) — the coordinates of the stops on the bus's route.
The third line contains *n*<=-<=1 integer *p**i* (0<=≤<=*p**i*<=≤<=100) — the probability of inspection in percents on the segment between stop *i* and stop *i*<=+<=1.
Then follow *m* lines that describe the bus's passengers. Each line contains exactly two integers *a**i* and *b**i* (1<=≤<=*a**i*<=<<=*b**i*<=≤<=*n*) — the numbers of stops where the *i*-th passenger gets on and off. | Print the single real number — the maximum expectation of the conductor's profit. Your 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 3 10\n0 10 100\n100 0\n1 2\n2 3\n1 3\n",
"10 8 187\n0 10 30 70 150 310 630 1270 2550 51100\n13 87 65 0 100 44 67 3 4\n1 10\n2 9\n3 8\n1 5\n6 10\n2 7\n4 10\n4 5\n"
] | [
"90.000000000\n",
"76859.990000000\n"
] | A comment to the first sample:
The first and third passengers get tickets from stop 1 to stop 2. The second passenger doesn't get a ticket. There always is inspection on the segment 1-2 but both passengers have the ticket for it. There never is an inspection on the segment 2-3, that's why the second passenger gets away with the cheating. Our total profit is (0 + 90 / 2 + 90 / 2) = 90. | [] | 46 | 0 | 0 | 11,868 | |
691 | Xor-sequences | [
"matrices"
] | null | null | You are given *n* integers *a*1,<=<=*a*2,<=<=...,<=<=*a**n*.
A sequence of integers *x*1,<=<=*x*2,<=<=...,<=<=*x**k* is called a "xor-sequence" if for every 1<=<=≤<=<=*i*<=<=≤<=<=*k*<=-<=1 the number of ones in the binary representation of the number *x**i* *x**i*<=<=+<=<=1's is a multiple of 3 and for all 1<=≤<=*i*<=≤<=*k*. The symbol is used for the binary exclusive or operation.
How many "xor-sequences" of length *k* exist? Output the answer modulo 109<=+<=7.
Note if *a*<==<=[1,<=1] and *k*<==<=1 then the answer is 2, because you should consider the ones from *a* as different. | The first line contains two integers *n* and *k* (1<=≤<=*n*<=≤<=100, 1<=≤<=*k*<=≤<=1018) — the number of given integers and the length of the "xor-sequences".
The second line contains *n* integers *a**i* (0<=≤<=*a**i*<=≤<=1018). | Print the only integer *c* — the number of "xor-sequences" of length *k* modulo 109<=+<=7. | [
"5 2\n15 1 2 4 8\n",
"5 1\n15 1 2 4 8\n"
] | [
"13\n",
"5\n"
] | none | [
{
"input": "5 2\n15 1 2 4 8",
"output": "13"
},
{
"input": "5 1\n15 1 2 4 8",
"output": "5"
},
{
"input": "10 1\n44 65 23 44 100 19 19 23 19 40",
"output": "10"
},
{
"input": "10 2\n93 93 85 48 44 98 93 100 98 98",
"output": "52"
},
{
"input": "10 100\n22 0 41 63 ... | 3,000 | 29,491,200 | 0 | 11,877 | |
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"
}
] | 61 | 0 | 0 | 11,901 | |
929 | Места в самолёте | [
"*special",
"implementation"
] | null | null | В самолёте есть *n* рядов мест. Если смотреть на ряды сверху, то в каждом ряду есть 3 места слева, затем проход между рядами, затем 4 центральных места, затем ещё один проход между рядами, а затем ещё 3 места справа.
Известно, что некоторые места уже заняты пассажирами. Всего есть два вида пассажиров — статусные (те, которые часто летают) и обычные.
Перед вами стоит задача рассадить ещё *k* обычных пассажиров так, чтобы суммарное число соседей у статусных пассажиров было минимально возможным. Два пассажира считаются соседями, если они сидят в одном ряду и между ними нет других мест и прохода между рядами. Если пассажир является соседним пассажиром для двух статусных пассажиров, то его следует учитывать в сумме соседей дважды. | В первой строке следуют два целых числа *n* и *k* (1<=≤<=*n*<=≤<=100, 1<=≤<=*k*<=≤<=10·*n*) — количество рядов мест в самолёте и количество пассажиров, которых нужно рассадить.
Далее следует описание рядов мест самолёта по одному ряду в строке. Если очередной символ равен '-', то это проход между рядами. Если очередной символ равен '.', то это свободное место. Если очередной символ равен 'S', то на текущем месте будет сидеть статусный пассажир. Если очередной символ равен 'P', то на текущем месте будет сидеть обычный пассажир.
Гарантируется, что количество свободных мест не меньше *k*. Гарантируется, что все ряды удовлетворяют описанному в условии формату. | В первую строку выведите минимальное суммарное число соседей у статусных пассажиров.
Далее выведите план рассадки пассажиров, который минимизирует суммарное количество соседей у статусных пассажиров, в том же формате, что и во входных данных. Если в свободное место нужно посадить одного из *k* пассажиров, выведите строчную букву 'x' вместо символа '.'. | [
"1 2\nSP.-SS.S-S.S\n",
"4 9\nPP.-PPPS-S.S\nPSP-PPSP-.S.\n.S.-S..P-SS.\nP.S-P.PP-PSP\n"
] | [
"5\nSPx-SSxS-S.S\n",
"15\nPPx-PPPS-S.S\nPSP-PPSP-xSx\nxSx-SxxP-SSx\nP.S-PxPP-PSP\n"
] | В первом примере нужно посадить ещё двух обычных пассажиров. Для минимизации соседей у статусных пассажиров, нужно посадить первого из них на третье слева место, а второго на любое из оставшихся двух мест, так как независимо от выбора места он станет соседом двух статусных пассажиров.
Изначально, у статусного пассажира, который сидит на самом левом месте уже есть сосед. Также на четвёртом и пятом местах слева сидят статусные пассажиры, являющиеся соседями друг для друга (что добавляет к сумме 2).
Таким образом, после посадки ещё двух обычных пассажиров, итоговое суммарное количество соседей у статусных пассажиров станет равно пяти. | [
{
"input": "1 2\nSP.-SS.S-S.S",
"output": "5\nSPx-SSxS-S.S"
},
{
"input": "4 9\nPP.-PPPS-S.S\nPSP-PPSP-.S.\n.S.-S..P-SS.\nP.S-P.PP-PSP",
"output": "15\nPPx-PPPS-S.S\nPSP-PPSP-xSx\nxSx-SxxP-SSx\nP.S-PxPP-PSP"
},
{
"input": "3 7\n.S.-SSSP-..S\nS..-.SPP-S.P\n.S.-PPPP-PSP",
"output": "13... | 62 | 5,734,400 | 3 | 11,924 | |
908 | New Year and Curling | [
"brute force",
"geometry",
"implementation",
"math"
] | null | null | Carol is currently curling.
She has *n* disks each with radius *r* on the 2D plane.
Initially she has all these disks above the line *y*<==<=10100.
She then will slide the disks towards the line *y*<==<=0 one by one in order from 1 to *n*.
When she slides the *i*-th disk, she will place its center at the point (*x**i*,<=10100). She will then push it so the disk’s *y* coordinate continuously decreases, and *x* coordinate stays constant. The disk stops once it touches the line *y*<==<=0 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk.
Compute the *y*-coordinates of centers of all the disks after all disks have been pushed. | The first line will contain two integers *n* and *r* (1<=≤<=*n*,<=*r*<=≤<=1<=000), the number of disks, and the radius of the disks, respectively.
The next line will contain *n* integers *x*1,<=*x*2,<=...,<=*x**n* (1<=≤<=*x**i*<=≤<=1<=000) — the *x*-coordinates of the disks. | Print a single line with *n* numbers. The *i*-th number denotes the *y*-coordinate of the center of the *i*-th disk. The output will be accepted if it has absolute or relative error at most 10<=-<=6.
Namely, let's assume that your answer for a particular value of a coordinate is *a* and the answer of the jury is *b*. The checker program will consider your answer correct if for all coordinates. | [
"6 2\n5 5 6 8 3 12\n"
] | [
"2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613\n"
] | The final positions of the disks will look as follows:
In particular, note the position of the last disk. | [
{
"input": "6 2\n5 5 6 8 3 12",
"output": "2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613"
},
{
"input": "1 1\n5",
"output": "1"
},
{
"input": "5 300\n939 465 129 611 532",
"output": "300 667.864105343 1164.9596696 1522.27745533 2117.05388391"
},
{
"input": "5 ... | 46 | 5,529,600 | 0 | 11,926 | |
30 | Shooting Gallery | [
"dp",
"probabilities"
] | C. Shooting Gallery | 2 | 256 | One warm and sunny day king Copa decided to visit the shooting gallery, located at the Central Park, and try to win the main prize — big pink plush panda. The king is not good at shooting, so he invited you to help him.
The shooting gallery is an infinite vertical plane with Cartesian coordinate system on it. The targets are points on this plane. Each target is described by it's coordinates *x**i*, and *y**i*, by the time of it's appearance *t**i* and by the number *p**i*, which gives the probability that Copa hits this target if he aims at it.
A target appears and disappears instantly, so Copa can hit the target only if at the moment *t**i* his gun sight aimed at (*x**i*,<=*y**i*). Speed of movement of the gun sight on the plane is equal to 1. Copa knows all the information about the targets beforehand (remember, he is a king!). He wants to play in the optimal way, which maximizes the expected value of the amount of hit targets. He can aim at any target at the moment 0. | The first line contains integer *n* (1<=≤<=*n*<=≤<=1000) — amount of targets in the shooting gallery. Then *n* lines follow, each describing one target. Each description consists of four numbers *x**i*, *y**i*, *t**i*, *p**i* (where *x**i*, *y**i*, *t**i* — integers, <=-<=1000<=≤<=*x**i*,<=*y**i*<=≤<=1000,<=0<=≤<=*t**i*<=≤<=109, real number *p**i* is given with no more than 6 digits after the decimal point, 0<=≤<=*p**i*<=≤<=1). No two targets may be at the same point. | Output the maximum expected value of the amount of targets that was shot by the king. Your answer will be accepted if it differs from the correct answer by not more than 10<=-<=6. | [
"1\n0 0 0 0.5\n",
"2\n0 0 0 0.6\n5 0 5 0.7\n"
] | [
"0.5000000000\n",
"1.3000000000\n"
] | none | [
{
"input": "1\n0 0 0 0.5",
"output": "0.5000000000"
},
{
"input": "2\n0 0 0 0.6\n5 0 5 0.7",
"output": "1.3000000000"
},
{
"input": "1\n-5 2 3 0.886986",
"output": "0.8869860000"
},
{
"input": "4\n10 -7 14 0.926305\n-7 -8 12 0.121809\n-7 7 14 0.413446\n3 -8 6 0.859061",
"... | 216 | 204,800 | -1 | 11,957 |
132 | Turing Tape | [
"implementation"
] | null | null | INTERCAL is the oldest of esoteric programming languages. One of its many weird features is the method of character-based output, known as Turing Tape method. It converts an array of unsigned 8-bit integers into a sequence of characters to print, using the following method.
The integers of the array are processed one by one, starting from the first. Processing *i*-th element of the array is done in three steps:
1. The 8-bit binary notation of the ASCII-code of the previous printed character is reversed. When the first element of the array is processed, the result of this step is considered to be 0.
2. The *i*-th element of the array is subtracted from the result of the previous step modulo 256.
3. The binary notation of the result of the previous step is reversed again to produce ASCII-code of the *i*-th character to be printed.
You are given the text printed using this method. Restore the array used to produce this text. | The input will consist of a single line *text* which contains the message printed using the described method. String *text* will contain between 1 and 100 characters, inclusive. ASCII-code of each character of *text* will be between 32 (space) and 126 (tilde), inclusive. | Output the initial array, which was used to produce *text*, one integer per line. | [
"Hello, World!\n"
] | [
"238\n108\n112\n0\n64\n194\n48\n26\n244\n168\n24\n16\n162\n"
] | Let's have a closer look at the beginning of the example. The first character is "H" with ASCII-code 72 = 01001000<sub class="lower-index">2</sub>. Its reverse is 00010010<sub class="lower-index">2</sub> = 18, and this number should become the result of the second step of processing. The result of the first step is considered to be 0, so the first element of the array has to be (0 - 18) mod 256 = 238, where *a* mod *b* is the remainder of division of *a* by *b*. | [
{
"input": "Hello, World!",
"output": "238\n108\n112\n0\n64\n194\n48\n26\n244\n168\n24\n16\n162"
},
{
"input": "N",
"output": "142"
},
{
"input": "!{|aPUBKs[k\"HE;>O&(Nf}N4,*#g*<3sQXFJ'?Z/H9L[xx Rc5\"8~v}84+wv]w[oO0e':MaNy&6*]jRkYomz[o?=13Y?!fzA3eC\\",
"output": "124\n166\n160\n184\n... | 154 | 2,150,400 | -1 | 12,000 | |
630 | Moore's Law | [
"math"
] | null | null | The city administration of IT City decided to fix up a symbol of scientific and technical progress in the city's main square, namely an indicator board that shows the effect of Moore's law in real time.
Moore's law is the observation that the number of transistors in a dense integrated circuit doubles approximately every 24 months. The implication of Moore's law is that computer performance as function of time increases exponentially as well.
You are to prepare information that will change every second to display on the indicator board. Let's assume that every second the number of transistors increases exactly 1.000000011 times. | The only line of the input contains a pair of integers *n* (1000<=≤<=*n*<=≤<=10 000) and *t* (0<=≤<=*t*<=≤<=2 000 000 000) — the number of transistors in the initial time and the number of seconds passed since the initial time. | Output one number — the estimate of the number of transistors in a dence integrated circuit in *t* seconds since the initial time. The relative error of your answer should not be greater than 10<=-<=6. | [
"1000 1000000\n"
] | [
"1011.060722383550382782399454922040\n"
] | none | [
{
"input": "1000 1000000",
"output": "1011.060722383550382782399454922040"
},
{
"input": "1000 0",
"output": "1000"
},
{
"input": "1000 1",
"output": "1000.000011000"
},
{
"input": "1000 2",
"output": "1000.000022000000121000"
},
{
"input": "10000 3",
"output"... | 31 | 0 | 3 | 12,055 | |
734 | Anton and Digits | [
"brute force",
"greedy",
"implementation",
"math"
] | null | null | Recently Anton found a box with digits in his room. There are *k*2 digits 2, *k*3 digits 3, *k*5 digits 5 and *k*6 digits 6.
Anton's favorite integers are 32 and 256. He decided to compose this integers from digits he has. He wants to make the sum of these integers as large as possible. Help him solve this task!
Each digit can be used no more than once, i.e. the composed integers should contain no more than *k*2 digits 2, *k*3 digits 3 and so on. Of course, unused digits are not counted in the sum. | The only line of the input contains four integers *k*2, *k*3, *k*5 and *k*6 — the number of digits 2, 3, 5 and 6 respectively (0<=≤<=*k*2,<=*k*3,<=*k*5,<=*k*6<=≤<=5·106). | Print one integer — maximum possible sum of Anton's favorite integers that can be composed using digits from the box. | [
"5 1 3 4\n",
"1 1 1 1\n"
] | [
"800\n",
"256\n"
] | In the first sample, there are five digits 2, one digit 3, three digits 5 and four digits 6. Anton can compose three integers 256 and one integer 32 to achieve the value 256 + 256 + 256 + 32 = 800. Note, that there is one unused integer 2 and one unused integer 6. They are not counted in the answer.
In the second sample, the optimal answer is to create on integer 256, thus the answer is 256. | [
{
"input": "5 1 3 4",
"output": "800"
},
{
"input": "1 1 1 1",
"output": "256"
},
{
"input": "10 2 1 5",
"output": "320"
},
{
"input": "4 2 7 2",
"output": "576"
},
{
"input": "489 292 127 263",
"output": "41856"
},
{
"input": "9557 5242 1190 7734",
... | 46 | 0 | 3 | 12,059 | |
105 | Lift and Throw | [
"brute force"
] | E. Lift and Throw | 1 | 256 | You are given a straight half-line divided into segments of unit length, which we will call positions. The positions are numbered by positive integers that start with 1 from the end of half-line, i. e. 1, 2, 3 and so on. The distance between the positions is the absolute difference between the respective numbers.
Laharl, Etna and Flonne occupy some positions on the half-line and they want to get to the position with the largest possible number. They are originally placed in different positions.
Each of the characters can perform each of the following actions no more than once:
- Move a certain distance. - Grab another character and lift him above the head. - Throw the lifted character a certain distance.
Each character has a movement range parameter. They can only move to free positions, assuming that distance between those positions doesn't exceed the movement range.
One character can lift another character if the distance between the two characters equals 1, and no one already holds that another character. We can assume that the lifted character moves to the same position as the person who has lifted him, and the position in which he stood before becomes free. A lifted character cannot perform any actions and the character that holds him cannot walk.
Also, each character has a throwing range parameter. It is the distance at which this character can throw the one lifted above his head. He can only throw a character to a free position, and only when there is a lifted character.
We accept the situation when one person grabs another one who in his turn has the third character in his hands. This forms a "column" of three characters. For example, Laharl can hold Etna while Etna holds Flonne. In this case, Etna and the Flonne cannot perform any actions, and Laharl can only throw Etna (together with Flonne) at some distance.
Laharl, Etna and Flonne perform actions in any order. They perform actions in turns, that is no two of them can do actions at the same time.
Determine the maximum number of position at least one of the characters can reach. That is, such maximal number *x* so that one of the characters can reach position *x*. | The first line contains three integers: Laharl's position, his movement range and throwing range. The second and the third lines describe Etna's and Flonne's parameters correspondingly in the similar form. It is guaranteed that the three characters occupy distinct positions. All numbers in the input are between 1 and 10, inclusive. | Print a single number — the maximum ordinal number of position which either Laharl, Etna or Flonne can reach. | [
"9 3 3\n4 3 1\n2 3 3\n"
] | [
"15"
] | Let us explain how to reach position 15 in the sample.
Initially Laharl occupies position 9, Etna — position 4 and Flonne — position 2.
First Laharl moves to position 6.
Then Flonne moves to position 5 and grabs Etna.
Laharl grabs Flonne and throws to position 9.
Flonne throws Etna to position 12.
Etna moves to position 15. | [] | 46 | 0 | 0 | 12,074 |
141 | Hopscotch | [
"geometry",
"math"
] | null | null | So nearly half of the winter is over and Maria is dreaming about summer. She's fed up with skates and sleds, she was dreaming about Hopscotch all night long. It's a very popular children's game. The game field, the court, looks as is shown in the figure (all blocks are square and are numbered from bottom to top, blocks in the same row are numbered from left to right). Let us describe the hopscotch with numbers that denote the number of squares in the row, staring from the lowest one: 1-1-2-1-2-1-2-(1-2)..., where then the period is repeated (1-2).
The coordinate system is defined as shown in the figure. Side of all the squares are equal and have length *a*.
Maria is a very smart and clever girl, and she is concerned with quite serious issues: if she throws a stone into a point with coordinates (*x*,<=*y*), then will she hit some square? If the answer is positive, you are also required to determine the number of the square.
It is believed that the stone has fallen into the square if it is located strictly inside it. In other words a stone that has fallen on the square border is not considered a to hit a square. | The only input line contains three integers: *a*, *x*, *y*, where *a* (1<=≤<=*a*<=≤<=100) is the side of the square, *x* and *y* (<=-<=106<=≤<=*x*<=≤<=106,<=0<=≤<=*y*<=≤<=106) are coordinates of the stone. | Print the number of the square, inside which the stone fell. If the stone is on a border of some stone or outside the court, print "-1" without the quotes. | [
"1 0 0\n",
"3 1 1\n",
"3 0 10\n",
"3 0 7\n",
"3 4 0\n"
] | [
"-1\n",
"1\n",
"5\n",
"-1\n",
"-1\n"
] | none | [
{
"input": "1 0 0",
"output": "-1"
},
{
"input": "3 1 1",
"output": "1"
},
{
"input": "3 0 10",
"output": "5"
},
{
"input": "3 0 7",
"output": "-1"
},
{
"input": "3 4 0",
"output": "-1"
},
{
"input": "9 3 2",
"output": "1"
},
{
"input": "10... | 248 | 8,908,800 | 3 | 12,076 | |
0 | none | [
"none"
] | null | null | Students went into a class to write a test and sat in some way. The teacher thought: "Probably they sat in this order to copy works of each other. I need to rearrange them in such a way that students that were neighbors are not neighbors in a new seating."
The class can be represented as a matrix with *n* rows and *m* columns with a student in each cell. Two students are neighbors if cells in which they sit have a common side.
Let's enumerate students from 1 to *n*·*m* in order of rows. So a student who initially sits in the cell in row *i* and column *j* has a number (*i*<=-<=1)·*m*<=+<=*j*. You have to find a matrix with *n* rows and *m* columns in which all numbers from 1 to *n*·*m* appear exactly once and adjacent numbers in the original matrix are not adjacent in it, or determine that there is no such matrix. | The only line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=105; *n*·*m*<=≤<=105) — the number of rows and the number of columns in the required matrix. | If there is no such matrix, output "NO" (without quotes).
Otherwise in the first line output "YES" (without quotes), and in the next *n* lines output *m* integers which form the required matrix. | [
"2 4\n",
"2 1\n"
] | [
"YES\n5 4 7 2 \n3 6 1 8 \n",
"NO\n"
] | In the first test case the matrix initially looks like this:
It's easy to see that there are no two students that are adjacent in both matrices.
In the second test case there are only two possible seatings and in both of them students with numbers 1 and 2 are neighbors. | [
{
"input": "2 4",
"output": "YES\n5 4 7 2 \n3 6 1 8 "
},
{
"input": "2 1",
"output": "NO"
},
{
"input": "1 1",
"output": "YES\n1"
},
{
"input": "1 2",
"output": "NO"
},
{
"input": "1 3",
"output": "NO"
},
{
"input": "2 2",
"output": "NO"
},
{
... | 0 | 0 | -1 | 12,095 | |
142 | Help Caretaker | [
"brute force",
"dp"
] | null | null | Autumn came late to the kingdom of Far Far Away. The harvest was exuberant and it is now time to get ready for the winter. As most people celebrate the Harvest festival, Simon the Caretaker tries to solve a very non-trivial task of how to find place for the agricultural equipment in the warehouse.
He's got problems with some particularly large piece of equipment, which is, of course, turboplows. The problem is that when a turboplow is stored, it takes up not some simply rectangular space. It takes up a T-shaped space like on one of the four pictures below (here character "#" stands for the space occupied by the turboplow and character "." stands for the free space):
Simon faced a quite natural challenge: placing in the given *n*<=×<=*m* cells warehouse the maximum number of turboplows. As one stores the turboplows, he can rotate them in any manner (so that they take up the space like on one of the four pictures above). However, two turboplows cannot "overlap", that is, they cannot share the same cell in the warehouse.
Simon feels that he alone cannot find the optimal way of positioning the plugs in the warehouse that would maximize their quantity. Can you help him? | The only line contains two space-separated integers *n* and *m* — the sizes of the warehouse (1<=≤<=*n*,<=*m*<=≤<=9). | In the first line print the maximum number of turboplows that can be positioned in the warehouse. In each of the next *n* lines print *m* characters. Use "." (dot) to mark empty space and use successive capital Latin letters ("A" for the first turboplow, "B" for the second one and so on until you reach the number of turboplows in your scheme) to mark place for the corresponding turboplows considering that they are positioned in the optimal manner in the warehouse. The order in which you number places for the turboplows does not matter. If there are several optimal solutions for a warehouse of the given size, print any of them. | [
"3 3\n",
"5 6\n",
"2 2\n"
] | [
"1\nAAA\n.A.\n.A.\n",
"4\nA..C..\nAAAC..\nABCCCD\n.B.DDD\nBBB..D\n",
"0\n..\n..\n"
] | none | [
{
"input": "3 3",
"output": "1\nAAA\n.A.\n.A."
},
{
"input": "5 6",
"output": "4\nA..C..\nAAAC..\nABCCCD\n.B.DDD\nBBB..D"
},
{
"input": "2 2",
"output": "0\n..\n.."
},
{
"input": "4 2",
"output": "0\n..\n..\n..\n.."
},
{
"input": "3 4",
"output": "1\nA...\nAAA... | 0 | 0 | -1 | 12,098 | |
476 | Dreamoon and Strings | [
"dp",
"strings"
] | null | null | Dreamoon has a string *s* and a pattern string *p*. He first removes exactly *x* characters from *s* obtaining string *s*' as a result. Then he calculates that is defined as the maximal number of non-overlapping substrings equal to *p* that can be found in *s*'. He wants to make this number as big as possible.
More formally, let's define as maximum value of over all *s*' that can be obtained by removing exactly *x* characters from *s*. Dreamoon wants to know for all *x* from 0 to |*s*| where |*s*| denotes the length of string *s*. | The first line of the input contains the string *s* (1<=≤<=|*s*|<=≤<=2<=000).
The second line of the input contains the string *p* (1<=≤<=|*p*|<=≤<=500).
Both strings will only consist of lower case English letters. | Print |*s*|<=+<=1 space-separated integers in a single line representing the for all *x* from 0 to |*s*|. | [
"aaaaa\naa\n",
"axbaxxb\nab\n"
] | [
"2 2 1 1 0 0\n",
"0 1 1 2 1 1 0 0\n"
] | For the first sample, the corresponding optimal values of *s*' after removal 0 through |*s*| = 5 characters from *s* are {"aaaaa", "aaaa", "aaa", "aa", "a", ""}.
For the second sample, possible corresponding optimal values of *s*' are {"axbaxxb", "abaxxb", "axbab", "abab", "aba", "ab", "a", ""}. | [
{
"input": "aaaaa\naa",
"output": "2 2 1 1 0 0"
},
{
"input": "axbaxxb\nab",
"output": "0 1 1 2 1 1 0 0"
},
{
"input": "aabb\nab",
"output": "1 1 1 0 0"
},
{
"input": "aaaaaaaaaaaaaaa\na",
"output": "15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0"
},
{
"input": "aaaaaaaaaa... | 171 | 20,172,800 | 3 | 12,100 | |
962 | Merge Equals | [
"data structures",
"implementation"
] | null | null | You are given an array of positive integers. While there are at least two equal elements, we will perform the following operation. We choose the smallest value $x$ that occurs in the array $2$ or more times. Take the first two occurrences of $x$ in this array (the two leftmost occurrences). Remove the left of these two occurrences, and the right one is replaced by the sum of this two values (that is, $2 \cdot x$).
Determine how the array will look after described operations are performed.
For example, consider the given array looks like $[3, 4, 1, 2, 2, 1, 1]$. It will be changed in the following way: $[3, 4, 1, 2, 2, 1, 1]~\rightarrow~[3, 4, 2, 2, 2, 1]~\rightarrow~[3, 4, 4, 2, 1]~\rightarrow~[3, 8, 2, 1]$.
If the given array is look like $[1, 1, 3, 1, 1]$ it will be changed in the following way: $[1, 1, 3, 1, 1]~\rightarrow~[2, 3, 1, 1]~\rightarrow~[2, 3, 2]~\rightarrow~[3, 4]$. | The first line contains a single integer $n$ ($2 \le n \le 150\,000$) — the number of elements in the array.
The second line contains a sequence from $n$ elements $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^{9}$) — the elements of the array. | In the first line print an integer $k$ — the number of elements in the array after all the performed operations. In the second line print $k$ integers — the elements of the array after all the performed operations. | [
"7\n3 4 1 2 2 1 1\n",
"5\n1 1 3 1 1\n",
"5\n10 40 20 50 30\n"
] | [
"4\n3 8 2 1 \n",
"2\n3 4 \n",
"5\n10 40 20 50 30 \n"
] | The first two examples were considered in the statement.
In the third example all integers in the given array are distinct, so it will not change. | [
{
"input": "7\n3 4 1 2 2 1 1",
"output": "4\n3 8 2 1 "
},
{
"input": "5\n1 1 3 1 1",
"output": "2\n3 4 "
},
{
"input": "5\n10 40 20 50 30",
"output": "5\n10 40 20 50 30 "
},
{
"input": "100\n10 10 15 12 15 13 15 12 10 10 15 11 13 14 13 14 10 13 12 10 14 12 13 11 14 15 12 11 1... | 30 | 0 | 0 | 12,118 | |
523 | Name Quest | [
"*special",
"greedy"
] | null | null | A Martian boy is named *s* — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, if *s*=«aba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.
However rather than being happy once, he loves twice as much being happy twice! So, when he got string *t* as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.
Help *s* determine the number of distinct ways to cut the given string *t* into two parts in the required manner. | The first line contains string *s*, consisting of lowercase English letters. The length of string *s* is from 1 to 1000 letters.
The second line contains string *t*, that also consists of lowercase English letters. The length of string *t* is from 1 to 106 letters. | Print the sought number of ways to cut string *t* in two so that each part made *s* happy. | [
"aba\nbaobababbah\n",
"mars\nsunvenusearthmarsjupitersaturnuranusneptune\n"
] | [
"2\n",
"0\n"
] | none | [
{
"input": "aba\nbaobababbah",
"output": "2"
},
{
"input": "mars\nsunvenusearthmarsjupitersaturnuranusneptune",
"output": "0"
},
{
"input": "rry\nsorrymercuryismissedabove",
"output": "3"
},
{
"input": "a\naaaaaaaaaaaaaa",
"output": "13"
},
{
"input": "abcaba\nabc... | 265 | 268,390,400 | 0 | 12,128 | |
690 | Photographs (I) | [] | null | null | The Human-Cow Confederation (*HC*2), led by Heidi, has built a base where people and cows can hide, guarded from zombie attacks. The entrance to the base is protected by an automated gate which performs a kind of a Turing test: it shows the entering creature a photograph and asks them whether the top and bottom halves of this photograph have been swapped or not. A person (or even a cow) will have no problem answering such questions; on the other hand, a zombie would just randomly smash one of the two buttons.
The creature is asked a series of such questions. If at least 75% of them are answered correctly, the gate is unlocked; otherwise, a side door opens, beneath which a huge fan is spinning...
Heidi is now building a robot army to fight the zombies, and she wants the robots to also be able to enter the base. You are tasked with programming them to distinguish the images.
The first two images from the test set. The first picture has been rearranged, but not the second. | The first line of the input contains the number *q* of questions (1<=≤<=*q*<=≤<=220). After that, *q* questions follow, each of which in the format described below.
The first line of every question contains two space-separated integers *h* and *w* (1<=≤<=*h*,<=*w*<=≤<=600) – the height (number of rows) and width (number of columns) of the photograph. (Most photographs are roughly 200<=×<=300.) After this, *h* lines follow, each describing a single row of the picture. The picture is monochrome (in shades of grey). Its *i*-th row is described by *w* space-separated integers *a**ij* (*j*<==<=1,<=...,<=*w*), where *a**ij* is the brightness of the corresponding pixel (0<=≤<=*a**ij*<=<<=256, where 0 is black and 255 is white).
Each picture will be either a real-life photograph, or a real-life photograph which has been broken up into two pieces and rearranged. More precisely, in the latter case, the topmost rows have been moved to the bottom of the picture. It is guaranteed that *h* is even.
There is only a single input file to be processed, called all.in, and it is downloadable from the online judge. You are also a given another input file, called sample.in, which contains the first 20 pictures from all.in; you are provided the correct answers for sample.in in sample.out. You are also given a directory easy_bmp, which contains the first 50 input photographs in the form of .bmp image files, as well as a directory easy_sample_original_bmp, which contains the first 20 images before rearrangement. Check the notes for the download links. | Your program should print *q* lines. The *i*-th line should contain your answer for the *i*-th question: YES if the photograph has been rearranged and NO otherwise. Your answers will be accepted if they all conform to this format and if at least 75% of them are correct.
Because the input is rather huge, feel free to process it locally and submit just your precomputed answers (i.e., a program which just prints your output for the input file all.in). | [] | [] | The link to download all necessary files is http://assets.codeforces.com/files/690/easy_contestant_package.zip | [] | 46 | 4,915,200 | 3 | 12,144 | |
825 | String Compression | [
"dp",
"hashing",
"string suffix structures",
"strings"
] | null | null | Ivan wants to write a letter to his friend. The letter is a string *s* consisting of lowercase Latin letters.
Unfortunately, when Ivan started writing the letter, he realised that it is very long and writing the whole letter may take extremely long time. So he wants to write the compressed version of string *s* instead of the string itself.
The compressed version of string *s* is a sequence of strings *c*1,<=*s*1,<=*c*2,<=*s*2,<=...,<=*c**k*,<=*s**k*, where *c**i* is the decimal representation of number *a**i* (without any leading zeroes) and *s**i* is some string consisting of lowercase Latin letters. If Ivan writes string *s*1 exactly *a*1 times, then string *s*2 exactly *a*2 times, and so on, the result will be string *s*.
The length of a compressed version is |*c*1|<=+<=|*s*1|<=+<=|*c*2|<=+<=|*s*2|... |*c**k*|<=+<=|*s**k*|. Among all compressed versions Ivan wants to choose a version such that its length is minimum possible. Help Ivan to determine minimum possible length. | The only line of input contains one string *s* consisting of lowercase Latin letters (1<=≤<=|*s*|<=≤<=8000). | Output one integer number — the minimum possible length of a compressed version of *s*. | [
"aaaaaaaaaa\n",
"abcab\n",
"cczabababab\n"
] | [
"3\n",
"6\n",
"7\n"
] | In the first example Ivan will choose this compressed version: *c*<sub class="lower-index">1</sub> is 10, *s*<sub class="lower-index">1</sub> is a.
In the second example Ivan will choose this compressed version: *c*<sub class="lower-index">1</sub> is 1, *s*<sub class="lower-index">1</sub> is abcab.
In the third example Ivan will choose this compressed version: *c*<sub class="lower-index">1</sub> is 2, *s*<sub class="lower-index">1</sub> is c, *c*<sub class="lower-index">2</sub> is 1, *s*<sub class="lower-index">2</sub> is z, *c*<sub class="lower-index">3</sub> is 4, *s*<sub class="lower-index">3</sub> is ab. | [
{
"input": "aaaaaaaaaa",
"output": "3"
},
{
"input": "abcab",
"output": "6"
},
{
"input": "cczabababab",
"output": "7"
},
{
"input": "kbyjorwqjk",
"output": "11"
},
{
"input": "baaabbbaba",
"output": "9"
},
{
"input": "aaaaaaaaaa",
"output": "3"
... | 2,000 | 450,150,400 | 0 | 12,232 | |
38 | Queue | [
"data structures"
] | G. Queue | 2 | 256 | On a cold winter evening our hero Vasya stood in a railway queue to buy a ticket for Codeforces championship final. As it usually happens, the cashier said he was going to be away for 5 minutes and left for an hour. Then Vasya, not to get bored, started to analyze such a mechanism as a queue. The findings astonished Vasya.
Every man is characterized by two numbers: *a**i*, which is the importance of his current task (the greater the number is, the more important the task is) and number *c**i*, which is a picture of his conscience. Numbers *a**i* form the permutation of numbers from 1 to *n*.
Let the queue consist of *n*<=-<=1 people at the moment. Let's look at the way the person who came number *n* behaves. First, he stands at the end of the queue and the does the following: if importance of the task *a**i* of the man in front of him is less than *a**n*, they swap their places (it looks like this: the man number *n* asks the one before him: "Erm... Excuse me please but it's very important for me... could you please let me move up the queue?"), then he again poses the question to the man in front of him and so on. But in case when *a**i* is greater than *a**n*, moving up the queue stops. However, the man number *n* can perform the operation no more than *c**n* times.
In our task let us suppose that by the moment when the man number *n* joins the queue, the process of swaps between *n*<=-<=1 will have stopped. If the swap is possible it necessarily takes place.
Your task is to help Vasya model the described process and find the order in which the people will stand in queue when all the swaps stops. | The first input line contains an integer *n* which is the number of people who has joined the queue (1<=≤<=*n*<=≤<=105). In the next *n* lines descriptions of the people are given in order of their coming — space-separated integers *a**i* and *c**i* (1<=≤<=*a**i*<=≤<=*n*, 0<=≤<=*c**i*<=≤<=*n*). Every description is located on s single line. All the *a**i*'s are different. | Output the permutation of numbers from 1 to *n*, which signifies the queue formed according to the above described rules, starting from the beginning to the end. In this succession the *i*-th number stands for the number of a person who will stand in line on the place number *i* after the swaps ends. People are numbered starting with 1 in the order in which they were given in the input. Separate numbers by a space. | [
"2\n1 0\n2 1\n",
"3\n1 3\n2 3\n3 3\n",
"5\n2 3\n1 4\n4 3\n3 1\n5 2\n"
] | [
"2 1 ",
"3 2 1 ",
"3 1 5 4 2 "
] | none | [] | 61 | 0 | 0 | 12,288 |
605 | Lazy Student | [
"constructive algorithms",
"data structures",
"graphs"
] | null | null | Student Vladislav came to his programming exam completely unprepared as usual. He got a question about some strange algorithm on a graph — something that will definitely never be useful in real life. He asked a girl sitting next to him to lend him some cheat papers for this questions and found there the following definition:
The minimum spanning tree *T* of graph *G* is such a tree that it contains all the vertices of the original graph *G*, and the sum of the weights of its edges is the minimum possible among all such trees.
Vladislav drew a graph with *n* vertices and *m* edges containing no loops and multiple edges. He found one of its minimum spanning trees and then wrote for each edge its weight and whether it is included in the found tree or not. Unfortunately, the piece of paper where the graph was painted is gone and the teacher is getting very angry and demands to see the original graph. Help Vladislav come up with a graph so that the information about the minimum spanning tree remains correct. | The first line of the input contains two integers *n* and *m* () — the number of vertices and the number of edges in the graph.
Each of the next *m* lines describes an edge of the graph and consists of two integers *a**j* and *b**j* (1<=≤<=*a**j*<=≤<=109,<=*b**j*<==<={0,<=1}). The first of these numbers is the weight of the edge and the second number is equal to 1 if this edge was included in the minimum spanning tree found by Vladislav, or 0 if it was not.
It is guaranteed that exactly *n*<=-<=1 number {*b**j*} are equal to one and exactly *m*<=-<=*n*<=+<=1 of them are equal to zero. | If Vladislav has made a mistake and such graph doesn't exist, print <=-<=1.
Otherwise print *m* lines. On the *j*-th line print a pair of vertices (*u**j*,<=*v**j*) (1<=≤<=*u**j*,<=*v**j*<=≤<=*n*,<=*u**j*<=≠<=*v**j*), that should be connected by the *j*-th edge. The edges are numbered in the same order as in the input. The graph, determined by these edges, must be connected, contain no loops or multiple edges and its edges with *b**j*<==<=1 must define the minimum spanning tree. In case there are multiple possible solutions, print any of them. | [
"4 5\n2 1\n3 1\n4 0\n1 1\n5 0\n",
"3 3\n1 0\n2 1\n3 1\n"
] | [
"2 4\n1 4\n3 4\n3 1\n3 2\n",
"-1\n"
] | none | [
{
"input": "4 5\n2 1\n3 1\n4 0\n1 1\n5 0",
"output": "2 4\n1 4\n3 4\n3 1\n3 2"
},
{
"input": "3 3\n1 0\n2 1\n3 1",
"output": "-1"
},
{
"input": "2 1\n7 1",
"output": "1 2"
},
{
"input": "3 2\n8 1\n9 1",
"output": "1 2\n1 3"
},
{
"input": "3 3\n4 1\n5 0\n7 1",
... | 109 | 307,200 | 0 | 12,345 | |
933 | A Determined Cleanup | [
"math"
] | null | null | In order to put away old things and welcome a fresh new year, a thorough cleaning of the house is a must.
Little Tommy finds an old polynomial and cleaned it up by taking it modulo another. But now he regrets doing this...
Given two integers *p* and *k*, find a polynomial *f*(*x*) with non-negative integer coefficients strictly less than *k*, whose remainder is *p* when divided by (*x*<=+<=*k*). That is, *f*(*x*)<==<=*q*(*x*)·(*x*<=+<=*k*)<=+<=*p*, where *q*(*x*) is a polynomial (not necessarily with integer coefficients). | The only line of input contains two space-separated integers *p* and *k* (1<=≤<=*p*<=≤<=1018, 2<=≤<=*k*<=≤<=2<=000). | If the polynomial does not exist, print a single integer -1, or output two lines otherwise.
In the first line print a non-negative integer *d* — the number of coefficients in the polynomial.
In the second line print *d* space-separated integers *a*0,<=*a*1,<=...,<=*a**d*<=-<=1, describing a polynomial fulfilling the given requirements. Your output should satisfy 0<=≤<=*a**i*<=<<=*k* for all 0<=≤<=*i*<=≤<=*d*<=-<=1, and *a**d*<=-<=1<=≠<=0.
If there are many possible solutions, print any of them. | [
"46 2\n",
"2018 214\n"
] | [
"7\n0 1 0 0 1 1 1\n",
"3\n92 205 1\n"
] | In the first example, *f*(*x*) = *x*<sup class="upper-index">6</sup> + *x*<sup class="upper-index">5</sup> + *x*<sup class="upper-index">4</sup> + *x* = (*x*<sup class="upper-index">5</sup> - *x*<sup class="upper-index">4</sup> + 3*x*<sup class="upper-index">3</sup> - 6*x*<sup class="upper-index">2</sup> + 12*x* - 23)·(*x* + 2) + 46.
In the second example, *f*(*x*) = *x*<sup class="upper-index">2</sup> + 205*x* + 92 = (*x* - 9)·(*x* + 214) + 2018. | [
{
"input": "46 2",
"output": "7\n0 1 0 0 1 1 1"
},
{
"input": "2018 214",
"output": "3\n92 205 1"
},
{
"input": "4 2",
"output": "3\n0 0 1"
},
{
"input": "5 2",
"output": "3\n1 0 1"
},
{
"input": "10 3",
"output": "3\n1 0 1"
},
{
"input": "250 1958",
... | 77 | 19,968,000 | 3 | 12,353 | |
32 | Flea | [
"math"
] | C. Flea | 2 | 256 | It is known that fleas in Berland can jump only vertically and horizontally, and the length of the jump is always equal to *s* centimeters. A flea has found herself at the center of some cell of the checked board of the size *n*<=×<=*m* centimeters (each cell is 1<=×<=1 centimeters). She can jump as she wishes for an arbitrary number of times, she can even visit a cell more than once. The only restriction is that she cannot jump out of the board.
The flea can count the amount of cells that she can reach from the starting position (*x*,<=*y*). Let's denote this amount by *d**x*,<=*y*. Your task is to find the number of such starting positions (*x*,<=*y*), which have the maximum possible value of *d**x*,<=*y*. | The first line contains three integers *n*, *m*, *s* (1<=≤<=*n*,<=*m*,<=*s*<=≤<=106) — length of the board, width of the board and length of the flea's jump. | Output the only integer — the number of the required starting positions of the flea. | [
"2 3 1000000\n",
"3 3 2\n"
] | [
"6\n",
"4\n"
] | none | [
{
"input": "2 3 1000000",
"output": "6"
},
{
"input": "3 3 2",
"output": "4"
},
{
"input": "1 2 3",
"output": "2"
},
{
"input": "4 5 6",
"output": "20"
},
{
"input": "9 8 7",
"output": "8"
},
{
"input": "1000 1000 1000",
"output": "1000000"
},
... | 186 | 0 | 0 | 12,356 |
750 | New Year and Rating | [
"binary search",
"greedy",
"math"
] | null | null | Every Codeforces user has rating, described with one integer, possibly negative or zero. Users are divided into two divisions. The first division is for users with rating 1900 or higher. Those with rating 1899 or lower belong to the second division. In every contest, according to one's performance, his or her rating changes by some value, possibly negative or zero.
Limak competed in *n* contests in the year 2016. He remembers that in the *i*-th contest he competed in the division *d**i* (i.e. he belonged to this division just before the start of this contest) and his rating changed by *c**i* just after the contest. Note that negative *c**i* denotes the loss of rating.
What is the maximum possible rating Limak can have right now, after all *n* contests? If his rating may be arbitrarily big, print "Infinity". If there is no scenario matching the given information, print "Impossible". | The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=200<=000).
The *i*-th of next *n* lines contains two integers *c**i* and *d**i* (<=-<=100<=≤<=*c**i*<=≤<=100, 1<=≤<=*d**i*<=≤<=2), describing Limak's rating change after the *i*-th contest and his division during the *i*-th contest contest. | If Limak's current rating can be arbitrarily big, print "Infinity" (without quotes). If the situation is impossible, print "Impossible" (without quotes). Otherwise print one integer, denoting the maximum possible value of Limak's current rating, i.e. rating after the *n* contests. | [
"3\n-7 1\n5 2\n8 2\n",
"2\n57 1\n22 2\n",
"1\n-5 1\n",
"4\n27 2\n13 1\n-50 1\n8 2\n"
] | [
"1907\n",
"Impossible\n",
"Infinity\n",
"1897\n"
] | In the first sample, the following scenario matches all information Limak remembers and has maximum possible final rating:
- Limak has rating 1901 and belongs to the division 1 in the first contest. His rating decreases by 7. - With rating 1894 Limak is in the division 2. His rating increases by 5. - Limak has rating 1899 and is still in the division 2. In the last contest of the year he gets + 8 and ends the year with rating 1907.
In the second sample, it's impossible that Limak is in the division 1, his rating increases by 57 and after that Limak is in the division 2 in the second contest. | [
{
"input": "3\n-7 1\n5 2\n8 2",
"output": "1907"
},
{
"input": "2\n57 1\n22 2",
"output": "Impossible"
},
{
"input": "1\n-5 1",
"output": "Infinity"
},
{
"input": "4\n27 2\n13 1\n-50 1\n8 2",
"output": "1897"
},
{
"input": "6\n8 1\n-22 1\n9 2\n-7 2\n85 2\n77 1",
... | 1,138 | 9,932,800 | 3 | 12,359 | |
335 | Palindrome | [
"constructive algorithms",
"dp"
] | null | null | Given a string *s*, determine if it contains any palindrome of length exactly 100 as a subsequence. If it has any, print any one of them. If it doesn't have any, print a palindrome that is a subsequence of *s* and is as long as possible. | The only line of the input contains one string *s* of length *n* (1<=≤<=*n*<=≤<=5·104) containing only lowercase English letters. | If *s* contains a palindrome of length exactly 100 as a subsequence, print any palindrome of length 100 which is a subsequence of *s*. If *s* doesn't contain any palindromes of length exactly 100, print a palindrome that is a subsequence of *s* and is as long as possible.
If there exists multiple answers, you are allowed to print any of them. | [
"bbbabcbbb\n",
"rquwmzexectvnbanemsmdufrg\n"
] | [
"bbbcbbb\n",
"rumenanemur\n"
] | A subsequence of a string is a string that can be derived from it by deleting some characters without changing the order of the remaining characters. A palindrome is a string that reads the same forward or backward. | [
{
"input": "bbbabcbbb",
"output": "bbbcbbb"
},
{
"input": "rquwmzexectvnbanemsmdufrg",
"output": "rumenanemur"
},
{
"input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa... | 62 | 0 | 0 | 12,362 | |
875 | Delivery Club | [
"binary search",
"data structures",
"dp"
] | null | null | Petya and Vasya got employed as couriers. During the working day they are to deliver packages to *n* different points on the line. According to the company's internal rules, the delivery of packages must be carried out strictly in a certain order. Initially, Petya is at the point with the coordinate *s*1, Vasya is at the point with the coordinate *s*2, and the clients are at the points *x*1,<=*x*2,<=...,<=*x**n* in the order of the required visit.
The guys agree in advance who of them will deliver the package to which of the customers, and then they act as follows. When the package for the *i*-th client is delivered, the one who delivers the package to the (*i*<=+<=1)-st client is sent to the path (it can be the same person who went to the point *x**i*, or the other). The friend who is not busy in delivering the current package, is standing still.
To communicate with each other, the guys have got walkie-talkies. The walkie-talkies work rather poorly at great distances, so Petya and Vasya want to distribute the orders so that the maximum distance between them during the day is as low as possible. Help Petya and Vasya to minimize the maximum distance between them, observing all delivery rules. | The first line contains three integers *n*, *s*1, *s*2 (1<=≤<=*n*<=≤<=100<=000, 0<=≤<=*s*1,<=*s*2<=≤<=109) — number of points of delivery and starting positions of Petya and Vasya.
The second line contains *n* integers *x*1,<=*x*2,<=...,<=*x**n* — customers coordinates (0<=≤<=*x**i*<=≤<=109), in the order to make a delivery.
It is guaranteed, that among the numbers *s*1,<=*s*2,<=*x*1,<=...,<=*x**n* there are no two equal. | Output the only integer, minimum possible maximal distance between couriers during delivery. | [
"2 0 10\n5 6\n",
"3 2 1\n3 4 5\n",
"1 4 5\n2\n"
] | [
"10\n",
"1\n",
"2\n"
] | In the first test case the initial distance between the couriers is 10. This value will be the answer, for example, Petya can perform both deliveries, and Vasya will remain at the starting point.
In the second test case you can optimally act, for example, like this: Vasya delivers the package to the first customer, Petya to the second and, finally, Vasya delivers the package to the third client. With this order of delivery, the distance between the couriers will never exceed 1.
In the third test case only two variants are possible: if the delivery of a single package is carried out by Petya, the maximum distance between them will be 5 - 2 = 3. If Vasya will deliver the package, the maximum distance is 4 - 2 = 2. The latter method is optimal. | [] | 30 | 0 | 0 | 12,388 | |
199 | Special Olympics | [
"geometry"
] | null | null | A renowned abstract artist Sasha, drawing inspiration from nowhere, decided to paint a picture entitled "Special Olympics". He justly thought that, if the regular Olympic games have five rings, then the Special ones will do with exactly two rings just fine.
Let us remind you that a ring is a region located between two concentric circles with radii *r* and *R* (*r*<=<<=*R*). These radii are called internal and external, respectively. Concentric circles are circles with centers located at the same point.
Soon a white canvas, which can be considered as an infinite Cartesian plane, had two perfect rings, painted with solid black paint. As Sasha is very impulsive, the rings could have different radii and sizes, they intersect and overlap with each other in any way. We know only one thing for sure: the centers of the pair of rings are not the same.
When Sasha got tired and fell into a deep sleep, a girl called Ilona came into the room and wanted to cut a circle for the sake of good memories. To make the circle beautiful, she decided to cut along the contour.
We'll consider a contour to be a continuous closed line through which there is transition from one color to another (see notes for clarification). If the contour takes the form of a circle, then the result will be cutting out a circle, which Iona wants.
But the girl's inquisitive mathematical mind does not rest: how many ways are there to cut a circle out of the canvas? | The input contains two lines.
Each line has four space-separated integers *x**i*, *y**i*, *r**i*, *R**i*, that describe the *i*-th ring; *x**i* and *y**i* are coordinates of the ring's center, *r**i* and *R**i* are the internal and external radii of the ring correspondingly (<=-<=100<=≤<=*x**i*,<=*y**i*<=≤<=100; 1<=≤<=*r**i*<=<<=*R**i*<=≤<=100).
It is guaranteed that the centers of the rings do not coinside. | A single integer — the number of ways to cut out a circle from the canvas. | [
"60 60 45 55\n80 80 8 32\n",
"60 60 45 55\n80 60 15 25\n",
"50 50 35 45\n90 50 35 45\n"
] | [
"1",
"4",
"0"
] | Figures for test samples are given below. The possible cuts are marked with red dotted line. | [
{
"input": "60 60 45 55\n80 80 8 32",
"output": "1"
},
{
"input": "60 60 45 55\n80 60 15 25",
"output": "4"
},
{
"input": "50 50 35 45\n90 50 35 45",
"output": "0"
},
{
"input": "0 0 50 70\n1 0 60 80",
"output": "2"
},
{
"input": "0 0 1 2\n10 0 2 20",
"output"... | 218 | 6,963,200 | 3 | 12,389 | |
883 | Photo Processing | [
"binary search",
"dp"
] | null | null | Evlampiy has found one more cool application to process photos. However the application has certain limitations.
Each photo *i* has a contrast *v**i*. In order for the processing to be truly of high quality, the application must receive at least *k* photos with contrasts which differ as little as possible.
Evlampiy already knows the contrast *v**i* for each of his *n* photos. Now he wants to split the photos into groups, so that each group contains at least *k* photos. As a result, each photo must belong to exactly one group.
He considers a processing time of the *j*-th group to be the difference between the maximum and minimum values of *v**i* in the group. Because of multithreading the processing time of a division into groups is the maximum processing time among all groups.
Split *n* photos into groups in a such way that the processing time of the division is the minimum possible, i.e. that the the maximum processing time over all groups as least as possible. | The first line contains two integers *n* and *k* (1<=≤<=*k*<=≤<=*n*<=≤<=3·105) — number of photos and minimum size of a group.
The second line contains *n* integers *v*1,<=*v*2,<=...,<=*v**n* (1<=≤<=*v**i*<=≤<=109), where *v**i* is the contrast of the *i*-th photo. | Print the minimal processing time of the division into groups. | [
"5 2\n50 110 130 40 120\n",
"4 1\n2 3 4 1\n"
] | [
"20\n",
"0\n"
] | In the first example the photos should be split into 2 groups: [40, 50] and [110, 120, 130]. The processing time of the first group is 10, and the processing time of the second group is 20. Maximum among 10 and 20 is 20. It is impossible to split the photos into groups in a such way that the processing time of division is less than 20.
In the second example the photos should be split into four groups, each containing one photo. So the minimal possible processing time of a division is 0. | [
{
"input": "5 2\n50 110 130 40 120",
"output": "20"
},
{
"input": "4 1\n2 3 4 1",
"output": "0"
},
{
"input": "1 1\n4",
"output": "0"
},
{
"input": "2 2\n7 5",
"output": "2"
},
{
"input": "3 2\n34 3 75",
"output": "72"
},
{
"input": "5 2\n932 328 886 9... | 3,000 | 25,088,000 | 0 | 12,397 | |
58 | Trees | [
"brute force"
] | C. Trees | 2 | 256 | On Bertown's main street *n* trees are growing, the tree number *i* has the height of *a**i* meters (1<=≤<=*i*<=≤<=*n*). By the arrival of the President of Berland these trees were decided to be changed so that their heights formed a beautiful sequence. This means that the heights of trees on ends (the 1st one and the *n*-th one) should be equal to each other, the heights of the 2-nd and the (*n*<=-<=1)-th tree must also be equal to each other, at that the height of the 2-nd tree should be larger than the height of the first tree by 1, and so on. In other words, the heights of the trees, standing at equal distance from the edge (of one end of the sequence) must be equal to each other, and with the increasing of the distance from the edge by 1 the tree height must also increase by 1. For example, the sequences "2 3 4 5 5 4 3 2" and "1 2 3 2 1" are beautiful, and '1 3 3 1" and "1 2 3 1" are not.
Changing the height of a tree is a very expensive operation, using advanced technologies invented by Berland scientists. In one operation you can choose any tree and change its height to any number, either increase or decrease. Note that even after the change the height should remain a positive integer, i. e, it can't be less than or equal to zero. Identify the smallest number of changes of the trees' height needed for the sequence of their heights to become beautiful. | The first line contains integer *n* (1<=≤<=*n*<=≤<=105) which is the number of trees. The second line contains integers *a**i* (1<=≤<=*a**i*<=≤<=105) which are the heights of the trees. | Print a single number which is the minimal number of trees whose heights will have to be changed for the sequence to become beautiful. | [
"3\n2 2 2\n",
"4\n1 2 2 1\n"
] | [
"1\n",
"0\n"
] | none | [
{
"input": "3\n2 2 2",
"output": "1"
},
{
"input": "4\n1 2 2 1",
"output": "0"
},
{
"input": "3\n61452 50974 73849",
"output": "2"
},
{
"input": "4\n86002 1199 86003 86002",
"output": "1"
},
{
"input": "5\n92605 92606 41969 98774 92605",
"output": "2"
},
{... | 248 | 22,220,800 | -1 | 12,444 |
176 | Trading Business | [
"greedy",
"sortings"
] | null | null | To get money for a new aeonic blaster, ranger Qwerty decided to engage in trade for a while. He wants to buy some number of items (or probably not to buy anything at all) on one of the planets, and then sell the bought items on another planet. Note that this operation is not repeated, that is, the buying and the selling are made only once. To carry out his plan, Qwerty is going to take a bank loan that covers all expenses and to return the loaned money at the end of the operation (the money is returned without the interest). At the same time, Querty wants to get as much profit as possible.
The system has *n* planets in total. On each of them Qwerty can buy or sell items of *m* types (such as food, medicine, weapons, alcohol, and so on). For each planet *i* and each type of items *j* Qwerty knows the following:
- *a**ij* — the cost of buying an item; - *b**ij* — the cost of selling an item; - *c**ij* — the number of remaining items.
It is not allowed to buy more than *c**ij* items of type *j* on planet *i*, but it is allowed to sell any number of items of any kind.
Knowing that the hold of Qwerty's ship has room for no more than *k* items, determine the maximum profit which Qwerty can get. | The first line contains three space-separated integers *n*, *m* and *k* (2<=≤<=*n*<=≤<=10, 1<=≤<=*m*,<=*k*<=≤<=100) — the number of planets, the number of question types and the capacity of Qwerty's ship hold, correspondingly.
Then follow *n* blocks describing each planet.
The first line of the *i*-th block has the planet's name as a string with length from 1 to 10 Latin letters. The first letter of the name is uppercase, the rest are lowercase. Then in the *i*-th block follow *m* lines, the *j*-th of them contains three integers *a**ij*, *b**ij* and *c**ij* (1<=≤<=*b**ij*<=<<=*a**ij*<=≤<=1000, 0<=≤<=*c**ij*<=≤<=100) — the numbers that describe money operations with the *j*-th item on the *i*-th planet. The numbers in the lines are separated by spaces.
It is guaranteed that the names of all planets are different. | Print a single number — the maximum profit Qwerty can get. | [
"3 3 10\nVenus\n6 5 3\n7 6 5\n8 6 10\nEarth\n10 9 0\n8 6 4\n10 9 3\nMars\n4 3 0\n8 4 12\n7 2 5\n"
] | [
"16"
] | In the first test case you should fly to planet Venus, take a loan on 74 units of money and buy three items of the first type and 7 items of the third type (3·6 + 7·8 = 74). Then the ranger should fly to planet Earth and sell there all the items he has bought. He gets 3·9 + 7·9 = 90 units of money for the items, he should give 74 of them for the loan. The resulting profit equals 16 units of money. We cannot get more profit in this case. | [
{
"input": "3 3 10\nVenus\n6 5 3\n7 6 5\n8 6 10\nEarth\n10 9 0\n8 6 4\n10 9 3\nMars\n4 3 0\n8 4 12\n7 2 5",
"output": "16"
},
{
"input": "2 1 5\nA\n6 5 5\nB\n10 9 0",
"output": "15"
},
{
"input": "2 2 5\nAbcdefghij\n20 15 20\n10 5 13\nKlmopqrstu\n19 16 20\n12 7 14",
"output": "0"
}... | 186 | 307,200 | 0 | 12,487 | |
0 | none | [
"none"
] | null | null | You are running for a governor in a small city in Russia. You ran some polls and did some research, and for every person in the city you know whom he will vote for, and how much it will cost to bribe that person to vote for you instead of whomever he wants to vote for right now. You are curious, what is the smallest amount of money you need to spend on bribing to win the elections. To win elections you need to have strictly more votes than any other candidate. | First line contains one integer *n* (1<=≤<=*n*<=≤<=105) — number of voters in the city. Each of the next *n* lines describes one voter and contains two integers *a**i* and *b**i* (0<=≤<=*a**i*<=≤<=105; 0<=≤<=*b**i*<=≤<=104) — number of the candidate that voter is going to vote for and amount of money you need to pay him to change his mind. You are the candidate 0 (so if a voter wants to vote for you, *a**i* is equal to zero, in which case *b**i* will also be equal to zero). | Print one integer — smallest amount of money you need to spend to win the elections. | [
"5\n1 2\n1 2\n1 2\n2 1\n0 0\n",
"4\n1 2\n1 2\n2 1\n0 0\n",
"1\n100000 0\n"
] | [
"3\n",
"2\n",
"0\n"
] | none | [] | 31 | 0 | 0 | 12,492 | |
550 | Preparing Olympiad | [
"bitmasks",
"brute force"
] | null | null | You have *n* problems. You have estimated the difficulty of the *i*-th one as integer *c**i*. Now you want to prepare a problemset for a contest, using some of the problems you've made.
A problemset for the contest must consist of at least two problems. You think that the total difficulty of the problems of the contest must be at least *l* and at most *r*. Also, you think that the difference between difficulties of the easiest and the hardest of the chosen problems must be at least *x*.
Find the number of ways to choose a problemset for the contest. | The first line contains four integers *n*, *l*, *r*, *x* (1<=≤<=*n*<=≤<=15, 1<=≤<=*l*<=≤<=*r*<=≤<=109, 1<=≤<=*x*<=≤<=106) — the number of problems you have, the minimum and maximum value of total difficulty of the problemset and the minimum difference in difficulty between the hardest problem in the pack and the easiest one, respectively.
The second line contains *n* integers *c*1,<=*c*2,<=...,<=*c**n* (1<=≤<=*c**i*<=≤<=106) — the difficulty of each problem. | Print the number of ways to choose a suitable problemset for the contest. | [
"3 5 6 1\n1 2 3\n",
"4 40 50 10\n10 20 30 25\n",
"5 25 35 10\n10 10 20 10 20\n"
] | [
"2\n",
"2\n",
"6\n"
] | In the first example two sets are suitable, one consisting of the second and third problem, another one consisting of all three problems.
In the second example, two sets of problems are suitable — the set of problems with difficulties 10 and 30 as well as the set of problems with difficulties 20 and 30.
In the third example any set consisting of one problem of difficulty 10 and one problem of difficulty 20 is suitable. | [
{
"input": "3 5 6 1\n1 2 3",
"output": "2"
},
{
"input": "4 40 50 10\n10 20 30 25",
"output": "2"
},
{
"input": "5 25 35 10\n10 10 20 10 20",
"output": "6"
},
{
"input": "4 15 60 10\n10 20 30 25",
"output": "6"
},
{
"input": "1 10 20 1\n15",
"output": "0"
},... | 389 | 11,366,400 | 3 | 12,525 | |
785 | Anton and School - 2 | [
"combinatorics",
"dp",
"math",
"number theory"
] | null | null | As you probably know, Anton goes to school. One of the school subjects that Anton studies is Bracketology. On the Bracketology lessons students usually learn different sequences that consist of round brackets (characters "(" and ")" (without quotes)).
On the last lesson Anton learned about the regular simple bracket sequences (RSBS). A bracket sequence *s* of length *n* is an RSBS if the following conditions are met:
- It is not empty (that is *n*<=≠<=0). - The length of the sequence is even. - First charactes of the sequence are equal to "(". - Last charactes of the sequence are equal to ")".
For example, the sequence "((()))" is an RSBS but the sequences "((())" and "(()())" are not RSBS.
Elena Ivanovna, Anton's teacher, gave him the following task as a homework. Given a bracket sequence *s*. Find the number of its distinct subsequences such that they are RSBS. Note that a subsequence of *s* is a string that can be obtained from *s* by deleting some of its elements. Two subsequences are considered distinct if distinct sets of positions are deleted.
Because the answer can be very big and Anton's teacher doesn't like big numbers, she asks Anton to find the answer modulo 109<=+<=7.
Anton thought of this task for a very long time, but he still doesn't know how to solve it. Help Anton to solve this task and write a program that finds the answer for it! | The only line of the input contains a string *s* — the bracket sequence given in Anton's homework. The string consists only of characters "(" and ")" (without quotes). It's guaranteed that the string is not empty and its length doesn't exceed 200<=000. | Output one number — the answer for the task modulo 109<=+<=7. | [
")(()()\n",
"()()()\n",
")))\n"
] | [
"6\n",
"7\n",
"0\n"
] | In the first sample the following subsequences are possible:
- If we delete characters at the positions 1 and 5 (numbering starts with one), we will get the subsequence "(())". - If we delete characters at the positions 1, 2, 3 and 4, we will get the subsequence "()". - If we delete characters at the positions 1, 2, 4 and 5, we will get the subsequence "()". - If we delete characters at the positions 1, 2, 5 and 6, we will get the subsequence "()". - If we delete characters at the positions 1, 3, 4 and 5, we will get the subsequence "()". - If we delete characters at the positions 1, 3, 5 and 6, we will get the subsequence "()".
The rest of the subsequnces are not RSBS. So we got 6 distinct subsequences that are RSBS, so the answer is 6. | [
{
"input": ")(()()",
"output": "6"
},
{
"input": "()()()",
"output": "7"
},
{
"input": ")))",
"output": "0"
},
{
"input": ")())((((((()))()((()))()((()()()((()()()))",
"output": "4344426"
},
{
"input": "(",
"output": "0"
},
{
"input": ")",
"output"... | 30 | 0 | 0 | 12,537 | |
570 | Simple Game | [
"constructive algorithms",
"games",
"greedy",
"implementation",
"math"
] | null | null | One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to *n*. Let's assume that Misha chose number *m*, and Andrew chose number *a*.
Then, by using a random generator they choose a random integer *c* in the range between 1 and *n* (any integer from 1 to *n* is chosen with the same probability), after which the winner is the player, whose number was closer to *c*. The boys agreed that if *m* and *a* are located on the same distance from *c*, Misha wins.
Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number *n*. You need to determine which value of *a* Andrew must choose, so that the probability of his victory is the highest possible.
More formally, you need to find such integer *a* (1<=≤<=*a*<=≤<=*n*), that the probability that is maximal, where *c* is the equiprobably chosen integer from 1 to *n* (inclusive). | The first line contains two integers *n* and *m* (1<=≤<=*m*<=≤<=*n*<=≤<=109) — the range of numbers in the game, and the number selected by Misha respectively. | Print a single number — such value *a*, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them. | [
"3 1\n",
"4 3\n"
] | [
"2",
"2"
] | In the first sample test: Andrew wins if *c* is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses *a* = 3, the probability of winning will be 1 / 3. If *a* = 1, the probability of winning is 0.
In the second sample test: Andrew wins if *c* is equal to 1 and 2. The probability that Andrew wins is 1 / 2. For other choices of *a* the probability of winning is less. | [
{
"input": "3 1",
"output": "2"
},
{
"input": "4 3",
"output": "2"
},
{
"input": "5 5",
"output": "4"
},
{
"input": "10 5",
"output": "6"
},
{
"input": "20 13",
"output": "12"
},
{
"input": "51 1",
"output": "2"
},
{
"input": "100 50",
... | 61 | 6,758,400 | 0 | 12,548 | |
0 | none | [
"none"
] | null | null | Bear Limak has *n* colored balls, arranged in one long row. Balls are numbered 1 through *n*, from left to right. There are *n* possible colors, also numbered 1 through *n*. The *i*-th ball has color *t**i*.
For a fixed interval (set of consecutive elements) of balls we can define a dominant color. It's a color occurring the biggest number of times in the interval. In case of a tie between some colors, the one with the smallest number (index) is chosen as dominant.
There are non-empty intervals in total. For each color, your task is to count the number of intervals in which this color is dominant. | The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=5000) — the number of balls.
The second line contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (1<=≤<=*t**i*<=≤<=*n*) where *t**i* is the color of the *i*-th ball. | Print *n* integers. The *i*-th of them should be equal to the number of intervals where *i* is a dominant color. | [
"4\n1 2 1 2\n",
"3\n1 1 1\n"
] | [
"7 3 0 0 \n",
"6 0 0 \n"
] | In the first sample, color 2 is dominant in three intervals:
- An interval [2, 2] contains one ball. This ball's color is 2 so it's clearly a dominant color. - An interval [4, 4] contains one ball, with color 2 again. - An interval [2, 4] contains two balls of color 2 and one ball of color 1.
There are 7 more intervals and color 1 is dominant in all of them. | [
{
"input": "4\n1 2 1 2",
"output": "7 3 0 0 "
},
{
"input": "3\n1 1 1",
"output": "6 0 0 "
},
{
"input": "10\n9 1 5 2 9 2 9 2 1 1",
"output": "18 30 0 0 1 0 0 0 6 0 "
},
{
"input": "50\n17 13 19 19 19 34 32 24 24 13 34 17 19 19 7 32 19 13 13 30 19 34 34 28 41 24 24 47 22 34 2... | 2,000 | 4,915,200 | 0 | 12,595 | |
983 | NN country | [
"binary search",
"data structures",
"trees"
] | null | null | In the NN country, there are $n$ cities, numbered from $1$ to $n$, and $n - 1$ roads, connecting them. There is a roads path between any two cities.
There are $m$ bidirectional bus routes between cities. Buses drive between two cities taking the shortest path with stops in every city they drive through. Travelling by bus, you can travel from any stop on the route to any other. You can travel between cities only by bus.
You are interested in $q$ questions: is it possible to get from one city to another and what is the minimum number of buses you need to use for it? | The first line contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of cities.
The second line contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \le p_i < i$), where $p_i$ means that cities $p_i$ and $i$ are connected by road.
The third line contains a single integer $m$ ($1 \le m \le 2 \cdot 10^5$) — the number of bus routes.
Each of the next $m$ lines contains $2$ integers $a$ and $b$ ($1 \le a, b \le n$, $a \neq b$), meaning that there is a bus route between cities $a$ and $b$. It is possible that there is more than one route between two cities.
The next line contains a single integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of questions you are interested in.
Each of the next $q$ lines contains $2$ integers $v$ and $u$ ($1 \le v, u \le n$, $v \neq u$), meaning that you are interested if it is possible to get from city $v$ to city $u$ and what is the minimum number of buses you need to use for it. | Print the answer for each question on a separate line. If there is no way to get from one city to another, print $-1$. Otherwise print the minimum number of buses you have to use. | [
"7\n1 1 1 4 5 6\n4\n4 2\n5 4\n1 3\n6 7\n6\n4 5\n3 5\n7 2\n4 5\n3 2\n5 3\n",
"7\n1 1 2 3 4 1\n4\n4 7\n3 5\n7 6\n7 6\n6\n4 6\n3 1\n3 2\n2 7\n6 3\n5 3\n"
] | [
"1\n3\n-1\n1\n2\n3\n",
"1\n-1\n-1\n1\n-1\n1\n"
] | [] | 46 | 0 | 0 | 12,611 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.