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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
863 | Turn Off The TV | [
"data structures",
"sortings"
] | null | null | Luba needs your help again! Luba has *n* TV sets. She knows that *i*-th TV set will be working from moment of time *l**i* till moment *r**i*, inclusive.
Luba wants to switch off one of TV sets in order to free the socket. Let's call some TV set redundant if after switching it off the number of integer moments of time when at least one of TV sets is working won't decrease. Luba will be very upset if she has to switch off a non-redundant TV set.
Help Luba by telling her the index of some redundant TV set. If there is no any, print -1. | The first line contains one integer number *n* (1<=≤<=*n*<=≤<=2·105) — the number of TV sets.
Then *n* lines follow, each of them containing two integer numbers *l**i*,<=*r**i* (0<=≤<=*l**i*<=≤<=*r**i*<=≤<=109) denoting the working time of *i*-th TV set. | If there is no any redundant TV set, print -1. Otherwise print the index of any redundant TV set (TV sets are indexed from 1 to *n*).
If there are multiple answers, print any of them. | [
"3\n1 3\n4 6\n1 7\n",
"2\n0 10\n0 10\n",
"3\n1 2\n3 4\n6 8\n",
"3\n1 2\n2 3\n3 4\n"
] | [
"1\n",
"1\n",
"-1\n",
"2\n"
] | Consider the first sample. Initially all integer moments of time such that at least one TV set is working are from the segment [1;7]. It's easy to see that this segment won't change if we switch off the first TV set (or the second one).
Note that in the fourth sample you can switch off the second TV set, since even without it all integer moments such that any of the TV sets is working denote the segment [1;4]. | [
{
"input": "3\n1 3\n4 6\n1 7",
"output": "1"
},
{
"input": "2\n0 10\n0 10",
"output": "1"
},
{
"input": "3\n1 2\n3 4\n6 8",
"output": "-1"
},
{
"input": "3\n1 2\n2 3\n3 4",
"output": "2"
},
{
"input": "3\n0 500000000\n500000001 1000000000\n0 1000000000",
"outp... | 2,000 | 59,801,600 | 0 | 7,710 | |
0 | none | [
"none"
] | null | null | You've got array *A*, consisting of *n* integers and a positive integer *k*. Array *A* is indexed by integers from 1 to *n*.
You need to permute the array elements so that value | The first line contains two integers *n*,<=*k* (2<=≤<=*n*<=≤<=3·105, 1<=≤<=*k*<=≤<=*min*(5000,<=*n*<=-<=1)).
The second line contains *n* integers *A*[1],<=*A*[2],<=...,<=*A*[*n*] (<=-<=109<=≤<=*A*[*i*]<=≤<=109), separate by spaces — elements of the array *A*. | Print the minimum possible value of the sum described in the statement. | [
"3 2\n1 2 4\n",
"5 2\n3 -5 3 -5 3\n",
"6 3\n4 3 4 3 2 5\n"
] | [
"1\n",
"0\n",
"3\n"
] | In the first test one of the optimal permutations is 1 4 2.
In the second test the initial order is optimal.
In the third test one of the optimal permutations is 2 3 4 4 3 5. | [] | 46 | 0 | 0 | 7,723 | |
653 | Bear and Forgotten Tree 2 | [
"dfs and similar",
"dsu",
"graphs",
"trees"
] | null | null | A tree is a connected undirected graph consisting of *n* vertices and *n*<=<=-<=<=1 edges. Vertices are numbered 1 through *n*.
Limak is a little polar bear. He once had a tree with *n* vertices but he lost it. He still remembers something about the lost tree though.
You are given *m* pairs of vertices (*a*1,<=*b*1),<=(*a*2,<=*b*2),<=...,<=(*a**m*,<=*b**m*). Limak remembers that for each *i* there was no edge between *a**i* and *b**i*. He also remembers that vertex 1 was incident to exactly *k* edges (its degree was equal to *k*).
Is it possible that Limak remembers everything correctly? Check whether there exists a tree satisfying the given conditions. | The first line of the input contains three integers *n*, *m* and *k* () — the number of vertices in Limak's tree, the number of forbidden pairs of vertices, and the degree of vertex 1, respectively.
The *i*-th of next *m* lines contains two distinct integers *a**i* and *b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*,<=*a**i*<=≠<=*b**i*) — the *i*-th pair that is forbidden. It's guaranteed that each pair of vertices will appear at most once in the input. | Print "possible" (without quotes) if there exists at least one tree satisfying the given conditions. Otherwise, print "impossible" (without quotes). | [
"5 4 2\n1 2\n2 3\n4 2\n4 1\n",
"6 5 3\n1 2\n1 3\n1 4\n1 5\n1 6\n"
] | [
"possible\n",
"impossible\n"
] | In the first sample, there are *n* = 5 vertices. The degree of vertex 1 should be *k* = 2. All conditions are satisfied for a tree with edges 1 - 5, 5 - 2, 1 - 3 and 3 - 4.
In the second sample, Limak remembers that none of the following edges existed: 1 - 2, 1 - 3, 1 - 4, 1 - 5 and 1 - 6. Hence, vertex 1 couldn't be connected to any other vertex and it implies that there is no suitable tree. | [
{
"input": "5 4 2\n1 2\n2 3\n4 2\n4 1",
"output": "possible"
},
{
"input": "6 5 3\n1 2\n1 3\n1 4\n1 5\n1 6",
"output": "impossible"
},
{
"input": "4 3 2\n2 3\n2 4\n3 4",
"output": "impossible"
},
{
"input": "4 2 2\n1 2\n1 3",
"output": "impossible"
},
{
"input": "... | 2,000 | 11,776,000 | 0 | 7,739 | |
1,000 | Codehorses T-shirts | [
"greedy",
"implementation"
] | null | null | Codehorses has just hosted the second Codehorses Cup. This year, the same as the previous one, organizers are giving T-shirts for the winners.
The valid sizes of T-shirts are either "M" or from $0$ to $3$ "X" followed by "S" or "L". For example, sizes "M", "XXS", "L", "XXXL" are valid and "XM", "Z", "XXXXL" are not.
There are $n$ winners to the cup for both the previous year and the current year. Ksenia has a list with the T-shirt sizes printed for the last year cup and is yet to send the new list to the printing office.
Organizers want to distribute the prizes as soon as possible, so now Ksenia is required not to write the whole list from the scratch but just make some changes to the list of the previous year. In one second she can choose arbitrary position in any word and replace its character with some uppercase Latin letter. Ksenia can't remove or add letters in any of the words.
What is the minimal number of seconds Ksenia is required to spend to change the last year list to the current one?
The lists are unordered. That means, two lists are considered equal if and only if the number of occurrences of any string is the same in both lists. | The first line contains one integer $n$ ($1 \le n \le 100$) — the number of T-shirts.
The $i$-th of the next $n$ lines contains $a_i$ — the size of the $i$-th T-shirt of the list for the previous year.
The $i$-th of the next $n$ lines contains $b_i$ — the size of the $i$-th T-shirt of the list for the current year.
It is guaranteed that all the sizes in the input are valid. It is also guaranteed that Ksenia can produce list $b$ from the list $a$. | Print the minimal number of seconds Ksenia is required to spend to change the last year list to the current one. If the lists are already equal, print 0. | [
"3\nXS\nXS\nM\nXL\nS\nXS\n",
"2\nXXXL\nXXL\nXXL\nXXXS\n",
"2\nM\nXS\nXS\nM\n"
] | [
"2\n",
"1\n",
"0\n"
] | In the first example Ksenia can replace "M" with "S" and "S" in one of the occurrences of "XS" with "L".
In the second example Ksenia should replace "L" in "XXXL" with "S".
In the third example lists are equal. | [
{
"input": "3\nXS\nXS\nM\nXL\nS\nXS",
"output": "2"
},
{
"input": "2\nXXXL\nXXL\nXXL\nXXXS",
"output": "1"
},
{
"input": "2\nM\nXS\nXS\nM",
"output": "0"
},
{
"input": "1\nXXXL\nXXXL",
"output": "0"
},
{
"input": "1\nM\nM",
"output": "0"
},
{
"input": ... | 92 | 0 | 3 | 7,759 | |
18 | Triangle | [
"brute force",
"geometry"
] | A. Triangle | 2 | 64 | At a geometry lesson Bob learnt that a triangle is called right-angled if it is nondegenerate and one of its angles is right. Bob decided to draw such a triangle immediately: on a sheet of paper he drew three points with integer coordinates, and joined them with segments of straight lines, then he showed the triangle to Peter. Peter said that Bob's triangle is not right-angled, but is almost right-angled: the triangle itself is not right-angled, but it is possible to move one of the points exactly by distance 1 so, that all the coordinates remain integer, and the triangle become right-angled. Bob asks you to help him and find out if Peter tricks him. By the given coordinates of the triangle you should find out if it is right-angled, almost right-angled, or neither of these. | The first input line contains 6 space-separated integers *x*1,<=*y*1,<=*x*2,<=*y*2,<=*x*3,<=*y*3 — coordinates of the triangle's vertices. All the coordinates are integer and don't exceed 100 in absolute value. It's guaranteed that the triangle is nondegenerate, i.e. its total area is not zero. | If the given triangle is right-angled, output RIGHT, if it is almost right-angled, output ALMOST, and if it is neither of these, output NEITHER. | [
"0 0 2 0 0 1\n",
"2 3 4 5 6 6\n",
"-1 0 2 0 0 1\n"
] | [
"RIGHT\n",
"NEITHER\n",
"ALMOST\n"
] | none | [
{
"input": "0 0 2 0 0 1",
"output": "RIGHT"
},
{
"input": "2 3 4 5 6 6",
"output": "NEITHER"
},
{
"input": "-1 0 2 0 0 1",
"output": "ALMOST"
},
{
"input": "27 74 85 23 100 99",
"output": "NEITHER"
},
{
"input": "-97 -19 17 62 30 -76",
"output": "NEITHER"
},... | 216 | 0 | 0 | 7,766 |
921 | Labyrinth-5 | [] | null | null | See the problem statement here: [http://codeforces.com/contest/921/problem/01](//codeforces.com/contest/921/problem/01). | none | none | [] | [] | none | [] | 30 | 5,632,000 | 2 | 7,768 | |
526 | Om Nom and Candies | [
"brute force",
"greedy",
"math"
] | null | null | A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place?
One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs *W**r* grams and each blue candy weighs *W**b* grams. Eating a single red candy gives Om Nom *H**r* joy units and eating a single blue candy gives Om Nom *H**b* joy units.
Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than *C* grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat. | The single line contains five integers *C*,<=*H**r*,<=*H**b*,<=*W**r*,<=*W**b* (1<=≤<=*C*,<=*H**r*,<=*H**b*,<=*W**r*,<=*W**b*<=≤<=109). | Print a single integer — the maximum number of joy units that Om Nom can get. | [
"10 3 5 2 3\n"
] | [
"16\n"
] | In the sample test Om Nom can eat two candies of each type and thus get 16 joy units. | [
{
"input": "10 3 5 2 3",
"output": "16"
},
{
"input": "5 3 1 6 7",
"output": "0"
},
{
"input": "982068341 55 57 106 109",
"output": "513558662"
},
{
"input": "930064129 32726326 25428197 83013449 64501049",
"output": "363523396"
},
{
"input": "927155987 21197 1599... | 312 | 2,457,600 | 3 | 7,773 | |
48 | Land Lot | [
"brute force",
"implementation"
] | B. Land Lot | 2 | 256 | Vasya has a beautiful garden where wonderful fruit trees grow and yield fantastic harvest every year. But lately thieves started to sneak into the garden at nights and steal the fruit too often. Vasya can’t spend the nights in the garden and guard the fruit because there’s no house in the garden! Vasya had been saving in for some time and finally he decided to build the house. The rest is simple: he should choose in which part of the garden to build the house. In the evening he sat at his table and drew the garden’s plan. On the plan the garden is represented as a rectangular checkered field *n*<=×<=*m* in size divided into squares whose side length is 1. In some squares Vasya marked the trees growing there (one shouldn’t plant the trees too close to each other that’s why one square contains no more than one tree). Vasya wants to find a rectangular land lot *a*<=×<=*b* squares in size to build a house on, at that the land lot border should go along the lines of the grid that separates the squares. All the trees that grow on the building lot will have to be chopped off. Vasya loves his garden very much, so help him choose the building land lot location so that the number of chopped trees would be as little as possible. | The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=50) which represent the garden location. The next *n* lines contain *m* numbers 0 or 1, which describe the garden on the scheme. The zero means that a tree doesn’t grow on this square and the 1 means that there is a growing tree. The last line contains two integers *a* and *b* (1<=≤<=*a*,<=*b*<=≤<=50). Note that Vasya can choose for building an *a*<=×<=*b* rectangle as well a *b*<=×<=*a* one, i.e. the side of the lot with the length of *a* can be located as parallel to the garden side with the length of *n*, as well as parallel to the garden side with the length of *m*. | Print the minimum number of trees that needs to be chopped off to select a land lot *a*<=×<=*b* in size to build a house on. It is guaranteed that at least one lot location can always be found, i. e. either *a*<=≤<=*n* and *b*<=≤<=*m*, or *a*<=≤<=*m* и *b*<=≤<=*n*. | [
"2 2\n1 0\n1 1\n1 1\n",
"4 5\n0 0 1 0 1\n0 1 1 1 0\n1 0 1 0 1\n1 1 1 1 1\n2 3\n"
] | [
"0\n",
"2\n"
] | In the second example the upper left square is (1,1) and the lower right is (3,2). | [
{
"input": "2 2\n1 0\n1 1\n1 1",
"output": "0"
},
{
"input": "4 5\n0 0 1 0 1\n0 1 1 1 0\n1 0 1 0 1\n1 1 1 1 1\n2 3",
"output": "2"
},
{
"input": "3 3\n0 0 0\n0 0 0\n0 0 0\n1 2",
"output": "0"
},
{
"input": "3 3\n1 1 1\n1 1 1\n1 1 1\n2 1",
"output": "2"
},
{
"input... | 310 | 0 | 3.9225 | 7,785 |
940 | Points on the line | [
"brute force",
"greedy",
"sortings"
] | null | null | We've got no test cases. A big olympiad is coming up. But the problemsetters' number one priority should be adding another problem to the round.
The diameter of a multiset of points on the line is the largest distance between two points from this set. For example, the diameter of the multiset {1,<=3,<=2,<=1} is 2.
Diameter of multiset consisting of one point is 0.
You are given *n* points on the line. What is the minimum number of points you have to remove, so that the diameter of the multiset of the remaining points will not exceed *d*? | The first line contains two integers *n* and *d* (1<=≤<=*n*<=≤<=100,<=0<=≤<=*d*<=≤<=100) — the amount of points and the maximum allowed diameter respectively.
The second line contains *n* space separated integers (1<=≤<=*x**i*<=≤<=100) — the coordinates of the points. | Output a single integer — the minimum number of points you have to remove. | [
"3 1\n2 1 4\n",
"3 0\n7 7 7\n",
"6 3\n1 3 4 6 9 10\n"
] | [
"1\n",
"0\n",
"3\n"
] | In the first test case the optimal strategy is to remove the point with coordinate 4. The remaining points will have coordinates 1 and 2, so the diameter will be equal to 2 - 1 = 1.
In the second test case the diameter is equal to 0, so its is unnecessary to remove any points.
In the third test case the optimal strategy is to remove points with coordinates 1, 9 and 10. The remaining points will have coordinates 3, 4 and 6, so the diameter will be equal to 6 - 3 = 3. | [
{
"input": "3 1\n2 1 4",
"output": "1"
},
{
"input": "3 0\n7 7 7",
"output": "0"
},
{
"input": "6 3\n1 3 4 6 9 10",
"output": "3"
},
{
"input": "11 5\n10 11 12 13 14 15 16 17 18 19 20",
"output": "5"
},
{
"input": "1 100\n1",
"output": "0"
},
{
"input"... | 77 | 0 | 3 | 7,788 | |
288 | Polo the Penguin and XOR operation | [
"implementation",
"math"
] | null | null | Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to *n*, inclusive.
For permutation *p*<==<=*p*0,<=*p*1,<=...,<=*p**n*, Polo has defined its beauty — number .
Expression means applying the operation of bitwise excluding "OR" to numbers *x* and *y*. This operation exists in all modern programming languages, for example, in language C++ and Java it is represented as "^" and in Pascal — as "xor".
Help him find among all permutations of integers from 0 to *n* the permutation with the maximum beauty. | The single line contains a positive integer *n* (1<=≤<=*n*<=≤<=106). | In the first line print integer *m* the maximum possible beauty. In the second line print any permutation of integers from 0 to *n* with the beauty equal to *m*.
If there are several suitable permutations, you are allowed to print any of them. | [
"4\n"
] | [
"20\n0 2 1 4 3\n"
] | none | [
{
"input": "4",
"output": "20\n0 2 1 4 3"
},
{
"input": "7",
"output": "56\n7 6 5 4 3 2 1 0"
},
{
"input": "1",
"output": "2\n1 0"
},
{
"input": "2",
"output": "6\n0 2 1"
},
{
"input": "3",
"output": "12\n3 2 1 0"
},
{
"input": "8",
"output": "72\n... | 2,000 | 0 | 0 | 7,815 | |
505 | Mr. Kitayuta's Colorful Graph | [
"dfs and similar",
"dp",
"dsu",
"graphs"
] | null | null | Mr. Kitayuta has just bought an undirected graph consisting of *n* vertices and *m* edges. The vertices of the graph are numbered from 1 to *n*. Each edge, namely edge *i*, has a color *c**i*, connecting vertex *a**i* and *b**i*.
Mr. Kitayuta wants you to process the following *q* queries.
In the *i*-th query, he gives you two integers — *u**i* and *v**i*.
Find the number of the colors that satisfy the following condition: the edges of that color connect vertex *u**i* and vertex *v**i* directly or indirectly. | The first line of the input contains space-separated two integers — *n* and *m* (2<=≤<=*n*<=≤<=100,<=1<=≤<=*m*<=≤<=100), denoting the number of the vertices and the number of the edges, respectively.
The next *m* lines contain space-separated three integers — *a**i*, *b**i* (1<=≤<=*a**i*<=<<=*b**i*<=≤<=*n*) and *c**i* (1<=≤<=*c**i*<=≤<=*m*). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if *i*<=≠<=*j*, (*a**i*,<=*b**i*,<=*c**i*)<=≠<=(*a**j*,<=*b**j*,<=*c**j*).
The next line contains a integer — *q* (1<=≤<=*q*<=≤<=100), denoting the number of the queries.
Then follows *q* lines, containing space-separated two integers — *u**i* and *v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*). It is guaranteed that *u**i*<=≠<=*v**i*. | For each query, print the answer in a separate line. | [
"4 5\n1 2 1\n1 2 2\n2 3 1\n2 3 3\n2 4 3\n3\n1 2\n3 4\n1 4\n",
"5 7\n1 5 1\n2 5 1\n3 5 1\n4 5 1\n1 2 2\n2 3 2\n3 4 2\n5\n1 5\n5 1\n2 5\n1 5\n1 4\n"
] | [
"2\n1\n0\n",
"1\n1\n1\n1\n2\n"
] | Let's consider the first sample.
- Vertex 1 and vertex 2 are connected by color 1 and 2. - Vertex 3 and vertex 4 are connected by color 3. - Vertex 1 and vertex 4 are not connected by any single color. | [
{
"input": "4 5\n1 2 1\n1 2 2\n2 3 1\n2 3 3\n2 4 3\n3\n1 2\n3 4\n1 4",
"output": "2\n1\n0"
},
{
"input": "5 7\n1 5 1\n2 5 1\n3 5 1\n4 5 1\n1 2 2\n2 3 2\n3 4 2\n5\n1 5\n5 1\n2 5\n1 5\n1 4",
"output": "1\n1\n1\n1\n2"
},
{
"input": "2 1\n1 2 1\n1\n1 2",
"output": "1"
},
{
"input... | 170 | 2,355,200 | 3 | 7,829 | |
3 | Lorry | [
"greedy",
"sortings"
] | B. Lorry | 2 | 64 | A group of tourists is going to kayak and catamaran tour. A rented lorry has arrived to the boat depot to take kayaks and catamarans to the point of departure. It's known that all kayaks are of the same size (and each of them occupies the space of 1 cubic metre), and all catamarans are of the same size, but two times bigger than kayaks (and occupy the space of 2 cubic metres).
Each waterborne vehicle has a particular carrying capacity, and it should be noted that waterborne vehicles that look the same can have different carrying capacities. Knowing the truck body volume and the list of waterborne vehicles in the boat depot (for each one its type and carrying capacity are known), find out such set of vehicles that can be taken in the lorry, and that has the maximum total carrying capacity. The truck body volume of the lorry can be used effectively, that is to say you can always put into the lorry a waterborne vehicle that occupies the space not exceeding the free space left in the truck body. | The first line contains a pair of integer numbers *n* and *v* (1<=≤<=*n*<=≤<=105; 1<=≤<=*v*<=≤<=109), where *n* is the number of waterborne vehicles in the boat depot, and *v* is the truck body volume of the lorry in cubic metres. The following *n* lines contain the information about the waterborne vehicles, that is a pair of numbers *t**i*,<=*p**i* (1<=≤<=*t**i*<=≤<=2; 1<=≤<=*p**i*<=≤<=104), where *t**i* is the vehicle type (1 – a kayak, 2 – a catamaran), and *p**i* is its carrying capacity. The waterborne vehicles are enumerated in order of their appearance in the input file. | In the first line print the maximum possible carrying capacity of the set. In the second line print a string consisting of the numbers of the vehicles that make the optimal set. If the answer is not unique, print any of them. | [
"3 2\n1 2\n2 7\n1 3\n"
] | [
"7\n2\n"
] | none | [
{
"input": "3 2\n1 2\n2 7\n1 3",
"output": "7\n2"
},
{
"input": "5 3\n1 9\n2 9\n1 9\n2 10\n1 6",
"output": "24\n3 1 5"
},
{
"input": "10 10\n1 14\n2 15\n2 11\n2 12\n2 9\n1 14\n2 15\n1 9\n2 11\n2 6",
"output": "81\n6 1 7 2 4 9"
},
{
"input": "20 19\n2 47\n1 37\n1 48\n2 42\n2 4... | 60 | 0 | 0 | 7,883 |
0 | none | [
"none"
] | null | null | Вася купил стол, у которого *n* ножек. Каждая ножка состоит из двух частей, которые соединяются друг с другом. Каждая часть может быть произвольной положительной длины, но гарантируется, что из всех 2*n* частей возможно составить *n* ножек одинаковой длины. При составлении ножки любые две части могут быть соединены друг с другом. Изначально все ножки стола разобраны, а вам заданы длины 2*n* частей в произвольном порядке.
Помогите Васе собрать все ножки стола так, чтобы все они были одинаковой длины, разбив заданные 2*n* части на пары правильным образом. Каждая ножка обязательно должна быть составлена ровно из двух частей, не разрешается использовать как ножку только одну часть. | В первой строке задано число *n* (1<=≤<=*n*<=≤<=1000) — количество ножек у стола, купленного Васей.
Во второй строке следует последовательность из 2*n* целых положительных чисел *a*1,<=*a*2,<=...,<=*a*2*n* (1<=≤<=*a**i*<=≤<=100<=000) — длины частей ножек стола в произвольном порядке. | Выведите *n* строк по два целых числа в каждой — длины частей ножек, которые надо соединить друг с другом. Гарантируется, что всегда возможно собрать *n* ножек одинаковой длины. Если ответов несколько, разрешается вывести любой из них. | [
"3\n1 3 2 4 5 3\n",
"3\n1 1 1 2 2 2\n"
] | [
"1 5\n2 4\n3 3\n",
"1 2\n2 1\n1 2\n"
] | none | [
{
"input": "3\n1 3 2 4 5 3",
"output": "1 5\n2 4\n3 3"
},
{
"input": "3\n1 1 1 2 2 2",
"output": "1 2\n1 2\n1 2"
},
{
"input": "1\n3 7",
"output": "3 7"
},
{
"input": "10\n9 13 18 7 18 13 2 2 5 16 3 17 5 4 18 2 15 11 7 15",
"output": "2 18\n2 18\n2 18\n3 17\n4 16\n5 15\n5... | 608 | 5,529,600 | 3 | 7,886 | |
582 | Once Again... | [
"constructive algorithms",
"dp",
"matrices"
] | null | null | You are given an array of positive integers *a*1,<=*a*2,<=...,<=*a**n*<=×<=*T* of length *n*<=×<=*T*. We know that for any *i*<=><=*n* it is true that *a**i*<==<=*a**i*<=-<=*n*. Find the length of the longest non-decreasing sequence of the given array. | The first line contains two space-separated integers: *n*, *T* (1<=≤<=*n*<=≤<=100, 1<=≤<=*T*<=≤<=107). The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=300). | Print a single number — the length of a sought sequence. | [
"4 3\n3 1 4 2\n"
] | [
"5\n"
] | The array given in the sample looks like that: 3, 1, 4, 2, 3, 1, 4, 2, 3, 1, 4, 2. The elements in bold form the largest non-decreasing subsequence. | [
{
"input": "4 3\n3 1 4 2",
"output": "5"
},
{
"input": "1 1000\n42",
"output": "1000"
},
{
"input": "31 3767\n16 192 152 78 224 202 186 52 118 19 13 38 199 196 35 295 100 64 205 37 166 124 169 214 66 243 134 192 253 270 92",
"output": "7546"
},
{
"input": "15 12226\n18 125 21... | 0 | 0 | -1 | 7,894 | |
696 | Puzzles | [
"dfs and similar",
"math",
"probabilities",
"trees"
] | null | null | Barney lives in country USC (United States of Charzeh). USC has *n* cities numbered from 1 through *n* and *n*<=-<=1 roads between them. Cities and roads of USC form a rooted tree (Barney's not sure why it is rooted). Root of the tree is the city number 1. Thus if one will start his journey from city 1, he can visit any city he wants by following roads.
Some girl has stolen Barney's heart, and Barney wants to find her. He starts looking for in the root of the tree and (since he is Barney Stinson not a random guy), he uses a random DFS to search in the cities. A pseudo code of this algorithm is as follows:
As told before, Barney will start his journey in the root of the tree (equivalent to call dfs(1)).
Now Barney needs to pack a backpack and so he wants to know more about his upcoming journey: for every city *i*, Barney wants to know the expected value of starting_time[i]. He's a friend of Jon Snow and knows nothing, that's why he asked for your help. | The first line of input contains a single integer *n* (1<=≤<=*n*<=≤<=105) — the number of cities in USC.
The second line contains *n*<=-<=1 integers *p*2,<=*p*3,<=...,<=*p**n* (1<=≤<=*p**i*<=<<=*i*), where *p**i* is the number of the parent city of city number *i* in the tree, meaning there is a road between cities numbered *p**i* and *i* in USC. | In the first and only line of output print *n* numbers, where *i*-th number is the expected value of starting_time[i].
Your answer for each city will be considered correct if its absolute or relative error does not exceed 10<=-<=6. | [
"7\n1 2 1 1 4 4\n",
"12\n1 1 2 2 4 4 3 3 1 10 8\n"
] | [
"1.0 4.0 5.0 3.5 4.5 5.0 5.0 \n",
"1.0 5.0 5.5 6.5 7.5 8.0 8.0 7.0 7.5 6.5 7.5 8.0 \n"
] | none | [
{
"input": "7\n1 2 1 1 4 4",
"output": "1.0 4.0 5.0 3.5 4.5 5.0 5.0 "
},
{
"input": "12\n1 1 2 2 4 4 3 3 1 10 8",
"output": "1.0 5.0 5.5 6.5 7.5 8.0 8.0 7.0 7.5 6.5 7.5 8.0 "
},
{
"input": "3\n1 2",
"output": "1.0 2.0 3.0 "
},
{
"input": "8\n1 1 2 2 3 6 1",
"output": "1.0... | 1,000 | 5,939,200 | 0 | 7,896 | |
702 | Cellular Network | [
"binary search",
"implementation",
"two pointers"
] | null | null | You are given *n* points on the straight line — the positions (*x*-coordinates) of the cities and *m* points on the same line — the positions (*x*-coordinates) of the cellular towers. All towers work in the same way — they provide cellular network for all cities, which are located at the distance which is no more than *r* from this tower.
Your task is to find minimal *r* that each city has been provided by cellular network, i.e. for each city there is at least one cellular tower at the distance which is no more than *r*.
If *r*<==<=0 then a tower provides cellular network only for the point where it is located. One tower can provide cellular network for any number of cities, but all these cities must be at the distance which is no more than *r* from this tower. | The first line contains two positive integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=105) — the number of cities and the number of cellular towers.
The second line contains a sequence of *n* integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=≤<=*a**i*<=≤<=109) — the coordinates of cities. It is allowed that there are any number of cities in the same point. All coordinates *a**i* are given in non-decreasing order.
The third line contains a sequence of *m* integers *b*1,<=*b*2,<=...,<=*b**m* (<=-<=109<=≤<=*b**j*<=≤<=109) — the coordinates of cellular towers. It is allowed that there are any number of towers in the same point. All coordinates *b**j* are given in non-decreasing order. | Print minimal *r* so that each city will be covered by cellular network. | [
"3 2\n-2 2 4\n-3 0\n",
"5 3\n1 5 10 14 17\n4 11 15\n"
] | [
"4\n",
"3\n"
] | none | [
{
"input": "3 2\n-2 2 4\n-3 0",
"output": "4"
},
{
"input": "5 3\n1 5 10 14 17\n4 11 15",
"output": "3"
},
{
"input": "1 1\n-1000000000\n1000000000",
"output": "2000000000"
},
{
"input": "1 1\n1000000000\n-1000000000",
"output": "2000000000"
},
{
"input": "10 10\n... | 92 | 2,048,000 | -1 | 7,916 | |
154 | Hometask | [
"greedy"
] | null | null | Sergey attends lessons of the *N*-ish language. Each lesson he receives a hometask. This time the task is to translate some sentence to the *N*-ish language. Sentences of the *N*-ish language can be represented as strings consisting of lowercase Latin letters without spaces or punctuation marks.
Sergey totally forgot about the task until half an hour before the next lesson and hastily scribbled something down. But then he recollected that in the last lesson he learned the grammar of *N*-ish. The spelling rules state that *N*-ish contains some "forbidden" pairs of letters: such letters can never occur in a sentence next to each other. Also, the order of the letters doesn't matter (for example, if the pair of letters "ab" is forbidden, then any occurrences of substrings "ab" and "ba" are also forbidden). Also, each pair has different letters and each letter occurs in no more than one forbidden pair.
Now Sergey wants to correct his sentence so that it doesn't contain any "forbidden" pairs of letters that stand next to each other. However, he is running out of time, so he decided to simply cross out some letters from the sentence. What smallest number of letters will he have to cross out? When a letter is crossed out, it is "removed" so that the letters to its left and right (if they existed), become neighboring. For example, if we cross out the first letter from the string "aba", we get the string "ba", and if we cross out the second letter, we get "aa". | The first line contains a non-empty string *s*, consisting of lowercase Latin letters — that's the initial sentence in *N*-ish, written by Sergey. The length of string *s* doesn't exceed 105.
The next line contains integer *k* (0<=≤<=*k*<=≤<=13) — the number of forbidden pairs of letters.
Next *k* lines contain descriptions of forbidden pairs of letters. Each line contains exactly two different lowercase Latin letters without separators that represent the forbidden pairs. It is guaranteed that each letter is included in no more than one pair. | Print the single number — the smallest number of letters that need to be removed to get a string without any forbidden pairs of neighboring letters. Please note that the answer always exists as it is always possible to remove all letters. | [
"ababa\n1\nab\n",
"codeforces\n2\ndo\ncs\n"
] | [
"2\n",
"1\n"
] | In the first sample you should remove two letters b.
In the second sample you should remove the second or the third letter. The second restriction doesn't influence the solution. | [
{
"input": "ababa\n1\nab",
"output": "2"
},
{
"input": "codeforces\n2\ndo\ncs",
"output": "1"
},
{
"input": "nllnrlrnll\n1\nrl",
"output": "1"
},
{
"input": "aludfbjtwnkgnfl\n1\noy",
"output": "0"
},
{
"input": "pgpgppgggpbbnnn\n2\npg\nnb",
"output": "7"
},
... | 216 | 2,662,400 | 3 | 7,949 | |
896 | Ithea Plays With Chtholly | [
"binary search",
"constructive algorithms",
"games",
"greedy",
"interactive"
] | null | null | This is an interactive problem. Refer to the Interaction section below for better understanding.
Ithea and Chtholly want to play a game in order to determine who can use the kitchen tonight.
Initially, Ithea puts *n* clear sheets of paper in a line. They are numbered from 1 to *n* from left to right.
This game will go on for *m* rounds. In each round, Ithea will give Chtholly an integer between 1 and *c*, and Chtholly needs to choose one of the sheets to write down this number (if there is already a number before, she will erase the original one and replace it with the new one).
Chtholly wins if, at any time, all the sheets are filled with a number and the *n* numbers are in non-decreasing order looking from left to right from sheet 1 to sheet *n*, and if after *m* rounds she still doesn't win, she loses the game.
Chtholly really wants to win the game as she wants to cook something for Willem. But she doesn't know how to win the game. So Chtholly finds you, and your task is to write a program to receive numbers that Ithea gives Chtholly and help her make the decision on which sheet of paper write this number. | The first line contains 3 integers *n*,<=*m* and *c* (, means rounded up) — the number of sheets, the number of rounds and the largest possible number Ithea can give to Chtholly respectively. The remaining parts of input are given throughout the interaction process. | none | [
"2 4 4\n2\n1\n3\n"
] | [
"1\n2\n2\n"
] | In the example, Chtholly initially knew there were 2 sheets, 4 rounds and each number was between 1 and 4. She then received a 2 and decided to write it in the 1st sheet. Then she received a 1 and wrote it in the 2nd sheet. At last, she received a 3 and replaced 1 with 3 in the 2nd sheet. At this time all the sheets were filled with a number and they were non-decreasing, so she won the game.
Note that it is required that your program terminate immediately after Chtholly wins and do not read numbers from the input for the remaining rounds. If not, undefined behaviour may arise and it won't be sure whether your program will be accepted or rejected. Also because of this, please be careful when hacking others' codes. In the sample, Chtholly won the game after the 3rd round, so it is required that your program doesn't read the number of the remaining 4th round.
The input format for hacking:
- The first line contains 3 integers *n*, *m* and *c*; - The following *m* lines each contains an integer between 1 and *c*, indicating the number given to Chtholly in each round. | [
{
"input": "2 4 4\n2\n1\n3\n4",
"output": "3"
},
{
"input": "2 2 2\n1\n2",
"output": "2"
},
{
"input": "3 6 3\n1\n2\n1\n3\n1\n3",
"output": "3"
},
{
"input": "4 8 4\n4\n4\n4\n4\n4\n4\n4\n4",
"output": "4"
},
{
"input": "10 120 15\n6\n11\n9\n11\n3\n12\n11\n12\n2\n8... | 109 | 4,608,000 | 0 | 7,960 | |
779 | Weird Rounding | [
"brute force",
"greedy"
] | null | null | Polycarp is crazy about round numbers. He especially likes the numbers divisible by 10*k*.
In the given number of *n* Polycarp wants to remove the least number of digits to get a number that is divisible by 10*k*. For example, if *k*<==<=3, in the number 30020 it is enough to delete a single digit (2). In this case, the result is 3000 that is divisible by 103<==<=1000.
Write a program that prints the minimum number of digits to be deleted from the given integer number *n*, so that the result is divisible by 10*k*. The result should not start with the unnecessary leading zero (i.e., zero can start only the number 0, which is required to be written as exactly one digit).
It is guaranteed that the answer exists. | The only line of the input contains two integer numbers *n* and *k* (0<=≤<=*n*<=≤<=2<=000<=000<=000, 1<=≤<=*k*<=≤<=9).
It is guaranteed that the answer exists. All numbers in the input are written in traditional notation of integers, that is, without any extra leading zeros. | Print *w* — the required minimal number of digits to erase. After removing the appropriate *w* digits from the number *n*, the result should have a value that is divisible by 10*k*. The result can start with digit 0 in the single case (the result is zero and written by exactly the only digit 0). | [
"30020 3\n",
"100 9\n",
"10203049 2\n"
] | [
"1\n",
"2\n",
"3\n"
] | In the example 2 you can remove two digits: 1 and any 0. The result is number 0 which is divisible by any number. | [
{
"input": "30020 3",
"output": "1"
},
{
"input": "100 9",
"output": "2"
},
{
"input": "10203049 2",
"output": "3"
},
{
"input": "0 1",
"output": "0"
},
{
"input": "0 9",
"output": "0"
},
{
"input": "100 2",
"output": "0"
},
{
"input": "102... | 62 | 4,608,000 | 3 | 7,964 | |
45 | Planting Trees | [
"constructive algorithms"
] | J. Planting Trees | 2 | 256 | Vasya is a Greencode wildlife preservation society proponent. One day he found an empty field nobody owned, divided it into *n*<=×<=*m* squares and decided to plant a forest there. Vasya will plant *nm* trees of all different heights from 1 to *nm*. For his forest to look more natural he wants any two trees growing in the side neighbouring squares to have the absolute value of difference in heights to be strictly more than 1. Help Vasya: make the plan of the forest planting for which this condition is fulfilled. | The first line contains two space-separated integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=100) — the number of rows and columns on Vasya's field | If there's no solution, print -1. Otherwise, print *n* lines containing *m* numbers each — the trees' planting plan. In every square of the plan the height of a tree that should be planted on this square should be written. If there are several solutions to that problem, print any of them. | [
"2 3\n",
"2 1\n"
] | [
"3 6 2\n5 1 4\n",
"-1\n"
] | none | [
{
"input": "2 3",
"output": "4 1 5 \n2 6 3 "
},
{
"input": "2 1",
"output": "-1"
},
{
"input": "1 1",
"output": "1 "
},
{
"input": "1 2",
"output": "-1"
},
{
"input": "1 3",
"output": "-1"
},
{
"input": "1 4",
"output": "3 1 4 2 "
},
{
"inp... | 124 | 7,065,600 | -1 | 7,966 |
746 | New Roads | [
"constructive algorithms",
"graphs",
"trees"
] | null | null | There are *n* cities in Berland, each of them has a unique id — an integer from 1 to *n*, the capital is the one with id 1. Now there is a serious problem in Berland with roads — there are no roads.
That is why there was a decision to build *n*<=-<=1 roads so that there will be exactly one simple path between each pair of cities.
In the construction plan *t* integers *a*1,<=*a*2,<=...,<=*a**t* were stated, where *t* equals to the distance from the capital to the most distant city, concerning new roads. *a**i* equals the number of cities which should be at the distance *i* from the capital. The distance between two cities is the number of roads one has to pass on the way from one city to another.
Also, it was decided that among all the cities except the capital there should be exactly *k* cities with exactly one road going from each of them. Such cities are dead-ends and can't be economically attractive. In calculation of these cities the capital is not taken into consideration regardless of the number of roads from it.
Your task is to offer a plan of road's construction which satisfies all the described conditions or to inform that it is impossible. | The first line contains three positive numbers *n*, *t* and *k* (2<=≤<=*n*<=≤<=2·105, 1<=≤<=*t*,<=*k*<=<<=*n*) — the distance to the most distant city from the capital and the number of cities which should be dead-ends (the capital in this number is not taken into consideration).
The second line contains a sequence of *t* integers *a*1,<=*a*2,<=...,<=*a**t* (1<=≤<=*a**i*<=<<=*n*), the *i*-th number is the number of cities which should be at the distance *i* from the capital. It is guaranteed that the sum of all the values *a**i* equals *n*<=-<=1. | If it is impossible to built roads which satisfy all conditions, print -1.
Otherwise, in the first line print one integer *n* — the number of cities in Berland. In the each of the next *n*<=-<=1 line print two integers — the ids of cities that are connected by a road. Each road should be printed exactly once. You can print the roads and the cities connected by a road in any order.
If there are multiple answers, print any of them. Remember that the capital has id 1. | [
"7 3 3\n2 3 1\n",
"14 5 6\n4 4 2 2 1\n",
"3 1 1\n2\n"
] | [
"7\n1 3\n2 1\n2 6\n2 4\n7 4\n3 5\n",
"14\n3 1\n1 4\n11 6\n1 2\n10 13\n6 10\n10 12\n14 12\n8 4\n5 1\n3 7\n2 6\n5 9\n",
"-1\n"
] | none | [
{
"input": "7 3 3\n2 3 1",
"output": "7\n1 2\n2 6\n5 3\n2 4\n1 3\n7 4"
},
{
"input": "14 5 6\n4 4 2 2 1",
"output": "14\n12 14\n7 3\n6 10\n5 1\n13 10\n1 3\n8 4\n9 5\n4 1\n6 2\n12 10\n6 11\n2 1"
},
{
"input": "3 1 1\n2",
"output": "-1"
},
{
"input": "6 3 3\n1 2 2",
"output... | 46 | 2,764,800 | 0 | 7,972 | |
66 | Petya and Java | [
"implementation",
"strings"
] | A. Petya and Java | 2 | 256 | Little Petya has recently started attending a programming club. Naturally he is facing the problem of choosing a programming language. After long considerations he realized that Java is the best choice. The main argument in favor of choosing Java was that it has a very large integer data type, called BigInteger.
But having attended several classes of the club, Petya realized that not all tasks require using the BigInteger type. It turned out that in some tasks it is much easier to use small data types. That's why a question arises: "Which integer type to use if one wants to store a positive integer *n*?"
Petya knows only 5 integer types:
1) byte occupies 1 byte and allows you to store numbers from <=-<=128 to 127
2) short occupies 2 bytes and allows you to store numbers from <=-<=32768 to 32767
3) int occupies 4 bytes and allows you to store numbers from <=-<=2147483648 to 2147483647
4) long occupies 8 bytes and allows you to store numbers from <=-<=9223372036854775808 to 9223372036854775807
5) BigInteger can store any integer number, but at that it is not a primitive type, and operations with it are much slower.
For all the types given above the boundary values are included in the value range.
From this list, Petya wants to choose the smallest type that can store a positive integer *n*. Since BigInteger works much slower, Peter regards it last. Help him. | The first line contains a positive number *n*. It consists of no more than 100 digits and doesn't contain any leading zeros. The number *n* can't be represented as an empty string.
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). | Print the first type from the list "byte, short, int, long, BigInteger", that can store the natural number *n*, in accordance with the data given above. | [
"127\n",
"130\n",
"123456789101112131415161718192021222324\n"
] | [
"byte\n",
"short\n",
"BigInteger\n"
] | none | [
{
"input": "127",
"output": "byte"
},
{
"input": "130",
"output": "short"
},
{
"input": "123456789101112131415161718192021222324",
"output": "BigInteger"
},
{
"input": "6",
"output": "byte"
},
{
"input": "16",
"output": "byte"
},
{
"input": "126",
... | 62 | 0 | 0 | 7,980 |
250 | Building Bridge | [
"geometry",
"ternary search",
"two pointers"
] | null | null | Two villages are separated by a river that flows from the north to the south. The villagers want to build a bridge across the river to make it easier to move across the villages.
The river banks can be assumed to be vertical straight lines *x*<==<=*a* and *x*<==<=*b* (0<=<<=*a*<=<<=*b*).
The west village lies in a steppe at point *O*<==<=(0,<=0). There are *n* pathways leading from the village to the river, they end at points *A**i*<==<=(*a*,<=*y**i*). The villagers there are plain and simple, so their pathways are straight segments as well.
The east village has reserved and cunning people. Their village is in the forest on the east bank of the river, but its exact position is not clear. There are *m* twisted paths leading from this village to the river and ending at points *B**i*<==<=(*b*,<=*y*'*i*). The lengths of all these paths are known, the length of the path that leads from the eastern village to point *B**i*, equals *l**i*.
The villagers want to choose exactly one point on the left bank of river *A**i*, exactly one point on the right bank *B**j* and connect them by a straight-line bridge so as to make the total distance between the villages (the sum of |*OA**i*|<=+<=|*A**i**B**j*|<=+<=*l**j*, where |*XY*| is the Euclidean distance between points *X* and *Y*) were minimum. The Euclidean distance between points (*x*1,<=*y*1) and (*x*2,<=*y*2) equals .
Help them and find the required pair of points. | The first line contains integers *n*, *m*, *a*, *b* (1<=≤<=*n*,<=*m*<=≤<=105, 0<=<<=*a*<=<<=*b*<=<<=106).
The second line contains *n* integers in the ascending order: the *i*-th integer determines the coordinate of point *A**i* and equals *y**i* (|*y**i*|<=≤<=106).
The third line contains *m* integers in the ascending order: the *i*-th integer determines the coordinate of point *B**i* and equals *y*'*i* (|*y*'*i*|<=≤<=106).
The fourth line contains *m* more integers: the *i*-th of them determines the length of the path that connects the eastern village and point *B**i*, and equals *l**i* (1<=≤<=*l**i*<=≤<=106).
It is guaranteed, that there is such a point *C* with abscissa at least *b*, that |*B**i**C*|<=≤<=*l**i* for all *i* (1<=≤<=*i*<=≤<=*m*). It is guaranteed that no two points *A**i* coincide. It is guaranteed that no two points *B**i* coincide. | Print two integers — the numbers of points on the left (west) and right (east) banks, respectively, between which you need to build a bridge. You can assume that the points on the west bank are numbered from 1 to *n*, in the order in which they are given in the input. Similarly, the points on the east bank are numbered from 1 to *m* in the order in which they are given in the input.
If there are multiple solutions, print any of them. The solution will be accepted if the final length of the path will differ from the answer of the jury by no more than 10<=-<=6 in absolute or relative value. | [
"3 2 3 5\n-2 -1 4\n-1 2\n7 3\n"
] | [
"2 2"
] | none | [
{
"input": "3 2 3 5\n-2 -1 4\n-1 2\n7 3",
"output": "2 2"
},
{
"input": "1 1 10 20\n5\n-5\n1",
"output": "1 1"
},
{
"input": "2 2 1 2\n-1 10\n8 9\n3 7",
"output": "1 1"
},
{
"input": "10 20 50 60\n-96 -75 32 37 42 43 44 57 61 65\n-95 -90 -86 -79 -65 -62 -47 -11 -8 -6 1 8 23 2... | 1,000 | 11,571,200 | 0 | 7,982 | |
243 | The Brand New Function | [
"bitmasks"
] | null | null | Polycarpus has a sequence, consisting of *n* non-negative integers: *a*1,<=*a*2,<=...,<=*a**n*.
Let's define function *f*(*l*,<=*r*) (*l*,<=*r* are integer, 1<=≤<=*l*<=≤<=*r*<=≤<=*n*) for sequence *a* as an operation of bitwise OR of all the sequence elements with indexes from *l* to *r*. Formally: *f*(*l*,<=*r*)<==<=*a**l* | *a**l*<=+<=1 | ... | *a**r*.
Polycarpus took a piece of paper and wrote out the values of function *f*(*l*,<=*r*) for all *l*,<=*r* (*l*,<=*r* are integer, 1<=≤<=*l*<=≤<=*r*<=≤<=*n*). Now he wants to know, how many distinct values he's got in the end.
Help Polycarpus, count the number of distinct values of function *f*(*l*,<=*r*) for the given sequence *a*.
Expression *x* | *y* means applying the operation of bitwise OR to numbers *x* and *y*. This operation exists in all modern programming languages, for example, in language C++ and Java it is marked as "|", in Pascal — as "or". | The first line contains integer *n* (1<=≤<=*n*<=≤<=105) — the number of elements of sequence *a*. The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=106) — the elements of sequence *a*. | Print a single integer — the number of distinct values of function *f*(*l*,<=*r*) for the given sequence *a*.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier. | [
"3\n1 2 0\n",
"10\n1 2 3 4 5 6 1 2 9 10\n"
] | [
"4",
"11"
] | In the first test case Polycarpus will have 6 numbers written on the paper: *f*(1, 1) = 1, *f*(1, 2) = 3, *f*(1, 3) = 3, *f*(2, 2) = 2, *f*(2, 3) = 2, *f*(3, 3) = 0. There are exactly 4 distinct numbers among them: 0, 1, 2, 3. | [
{
"input": "3\n1 2 0",
"output": "4"
},
{
"input": "10\n1 2 3 4 5 6 1 2 9 10",
"output": "11"
},
{
"input": "1\n123",
"output": "1"
},
{
"input": "10\n6 8 4 5 1 9 10 2 3 7",
"output": "15"
},
{
"input": "7\n1 2 4 8 16 32 64",
"output": "28"
},
{
"input... | 2,000 | 10,547,200 | 0 | 8,011 | |
555 | Case of Matryoshkas | [
"implementation"
] | null | null | Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.
The main exhibit is a construction of *n* matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to *n*. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1<=→<=2<=→<=4<=→<=5.
In one second, you can perform one of the two following operations:
- Having a matryoshka *a* that isn't nested in any other matryoshka and a matryoshka *b*, such that *b* doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put *a* in *b*; - Having a matryoshka *a* directly contained in matryoshka *b*, such that *b* is not nested in any other matryoshka, you may get *a* out of *b*.
According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1<=→<=2<=→<=...<=→<=*n*). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action. | The first line contains integers *n* (1<=≤<=*n*<=≤<=105) and *k* (1<=≤<=*k*<=≤<=105) — the number of matryoshkas and matryoshka chains in the initial configuration.
The next *k* lines contain the descriptions of the chains: the *i*-th line first contains number *m**i* (1<=≤<=*m**i*<=≤<=*n*), and then *m**i* numbers *a**i*1,<=*a**i*2,<=...,<=*a**im**i* — the numbers of matryoshkas in the chain (matryoshka *a**i*1 is nested into matryoshka *a**i*2, that is nested into matryoshka *a**i*3, and so on till the matryoshka *a**im**i* that isn't nested into any other matryoshka).
It is guaranteed that *m*1<=+<=*m*2<=+<=...<=+<=*m**k*<==<=*n*, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order. | In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration. | [
"3 2\n2 1 2\n1 3\n",
"7 3\n3 1 3 7\n2 2 5\n2 4 6\n"
] | [
"1\n",
"10\n"
] | In the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get 1 → 2 → 3.
In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds. | [
{
"input": "3 2\n2 1 2\n1 3",
"output": "1"
},
{
"input": "7 3\n3 1 3 7\n2 2 5\n2 4 6",
"output": "10"
},
{
"input": "1 1\n1 1",
"output": "0"
},
{
"input": "3 2\n1 2\n2 1 3",
"output": "3"
},
{
"input": "5 3\n1 4\n3 1 2 3\n1 5",
"output": "2"
},
{
"in... | 358 | 8,294,400 | 3 | 8,030 | |
923 | Primal Sport | [
"math",
"number theory"
] | null | null | Alice and Bob begin their day with a quick game. They first choose a starting number *X*0<=≥<=3 and try to reach one million by the process described below.
Alice goes first and then they take alternating turns. In the *i*-th turn, the player whose turn it is selects a prime number smaller than the current number, and announces the smallest multiple of this prime number that is not smaller than the current number.
Formally, he or she selects a prime *p*<=<<=*X**i*<=-<=1 and then finds the minimum *X**i*<=≥<=*X**i*<=-<=1 such that *p* divides *X**i*. Note that if the selected prime *p* already divides *X**i*<=-<=1, then the number does not change.
Eve has witnessed the state of the game after two turns. Given *X*2, help her determine what is the smallest possible starting number *X*0. Note that the players don't necessarily play optimally. You should consider all possible game evolutions. | The input contains a single integer *X*2 (4<=≤<=*X*2<=≤<=106). It is guaranteed that the integer *X*2 is composite, that is, is not prime. | Output a single integer — the minimum possible *X*0. | [
"14\n",
"20\n",
"8192\n"
] | [
"6\n",
"15\n",
"8191\n"
] | In the first test, the smallest possible starting number is *X*<sub class="lower-index">0</sub> = 6. One possible course of the game is as follows:
- Alice picks prime 5 and announces *X*<sub class="lower-index">1</sub> = 10 - Bob picks prime 7 and announces *X*<sub class="lower-index">2</sub> = 14.
In the second case, let *X*<sub class="lower-index">0</sub> = 15.
- Alice picks prime 2 and announces *X*<sub class="lower-index">1</sub> = 16 - Bob picks prime 5 and announces *X*<sub class="lower-index">2</sub> = 20. | [
{
"input": "14",
"output": "6"
},
{
"input": "20",
"output": "15"
},
{
"input": "8192",
"output": "8191"
},
{
"input": "1000000",
"output": "998677"
},
{
"input": "959806",
"output": "239958"
},
{
"input": "1452",
"output": "1206"
},
{
"inp... | 982 | 9,113,600 | 3 | 8,051 | |
0 | none | [
"none"
] | null | null | Arkady decides to observe a river for *n* consecutive days. The river's water level on each day is equal to some real value.
Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the *i*-th day this value is equal to *m**i*.
Define *d**i* as the number of marks strictly under the water level on the *i*-th day. You are to find out the minimum possible sum of *d**i* over all days. There are no marks on the channel before the first day. | The first line contains a single positive integer *n* (1<=≤<=*n*<=≤<=105) — the number of days.
The second line contains *n* space-separated integers *m*1,<=*m*2,<=...,<=*m**n* (0<=≤<=*m**i*<=<<=*i*) — the number of marks strictly above the water on each day. | Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days. | [
"6\n0 1 0 3 0 2\n",
"5\n0 1 2 1 2\n",
"5\n0 1 1 2 2\n"
] | [
"6\n",
"1\n",
"0\n"
] | In the first example, the following figure shows an optimal case.
Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6.
In the second example, the following figure shows an optimal case. | [
{
"input": "6\n0 1 0 3 0 2",
"output": "6"
},
{
"input": "5\n0 1 2 1 2",
"output": "1"
},
{
"input": "5\n0 1 1 2 2",
"output": "0"
},
{
"input": "1\n0",
"output": "0"
},
{
"input": "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15... | 233 | 14,848,000 | 3 | 8,061 | |
990 | Post Lamps | [
"brute force",
"greedy"
] | null | null | Adilbek's house is located on a street which can be represented as the OX axis. This street is really dark, so Adilbek wants to install some post lamps to illuminate it. Street has $n$ positions to install lamps, they correspond to the integer numbers from $0$ to $n - 1$ on the OX axis. However, some positions are blocked and no post lamp can be placed there.
There are post lamps of different types which differ only by their power. When placed in position $x$, post lamp of power $l$ illuminates the segment $[x; x + l]$. The power of each post lamp is always a positive integer number.
The post lamp shop provides an infinite amount of lamps of each type from power $1$ to power $k$. Though each customer is only allowed to order post lamps of exactly one type. Post lamps of power $l$ cost $a_l$ each.
What is the minimal total cost of the post lamps of exactly one type Adilbek can buy to illuminate the entire segment $[0; n]$ of the street? If some lamps illuminate any other segment of the street, Adilbek does not care, so, for example, he may place a lamp of power $3$ in position $n - 1$ (even though its illumination zone doesn't completely belong to segment $[0; n]$). | The first line contains three integer numbers $n$, $m$ and $k$ ($1 \le k \le n \le 10^6$, $0 \le m \le n$) — the length of the segment of the street Adilbek wants to illuminate, the number of the blocked positions and the maximum power of the post lamp available.
The second line contains $m$ integer numbers $s_1, s_2, \dots, s_m$ ($0 \le s_1 < s_2 < \dots s_m < n$) — the blocked positions.
The third line contains $k$ integer numbers $a_1, a_2, \dots, a_k$ ($1 \le a_i \le 10^6$) — the costs of the post lamps. | Print the minimal total cost of the post lamps of exactly one type Adilbek can buy to illuminate the entire segment $[0; n]$ of the street.
If illumintaing the entire segment $[0; n]$ is impossible, print -1. | [
"6 2 3\n1 3\n1 2 3\n",
"4 3 4\n1 2 3\n1 10 100 1000\n",
"5 1 5\n0\n3 3 3 3 3\n",
"7 4 3\n2 4 5 6\n3 14 15\n"
] | [
"6\n",
"1000\n",
"-1\n",
"-1\n"
] | none | [
{
"input": "6 2 3\n1 3\n1 2 3",
"output": "6"
},
{
"input": "4 3 4\n1 2 3\n1 10 100 1000",
"output": "1000"
},
{
"input": "5 1 5\n0\n3 3 3 3 3",
"output": "-1"
},
{
"input": "7 4 3\n2 4 5 6\n3 14 15",
"output": "-1"
},
{
"input": "1 0 1\n\n1000000",
"output": ... | 1,606 | 88,064,000 | 0 | 8,095 | |
711 | Chris and Magic Square | [
"constructive algorithms",
"implementation"
] | null | null | ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a *n*<=×<=*n* magic grid on the entrance which is filled with integers. Chris noticed that exactly one of the cells in the grid is empty, and to enter Udayland, they need to fill a positive integer into the empty cell.
Chris tried filling in random numbers but it didn't work. ZS the Coder realizes that they need to fill in a positive integer such that the numbers in the grid form a magic square. This means that he has to fill in a positive integer so that the sum of the numbers in each row of the grid (), each column of the grid (), and the two long diagonals of the grid (the main diagonal — and the secondary diagonal — ) are equal.
Chris doesn't know what number to fill in. Can you help Chris find the correct positive integer to fill in or determine that it is impossible? | The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=500) — the number of rows and columns of the magic grid.
*n* lines follow, each of them contains *n* integers. The *j*-th number in the *i*-th of them denotes *a**i*,<=*j* (1<=≤<=*a**i*,<=*j*<=≤<=109 or *a**i*,<=*j*<==<=0), the number in the *i*-th row and *j*-th column of the magic grid. If the corresponding cell is empty, *a**i*,<=*j* will be equal to 0. Otherwise, *a**i*,<=*j* is positive.
It is guaranteed that there is exactly one pair of integers *i*,<=*j* (1<=≤<=*i*,<=*j*<=≤<=*n*) such that *a**i*,<=*j*<==<=0. | Output a single integer, the positive integer *x* (1<=≤<=*x*<=≤<=1018) that should be filled in the empty cell so that the whole grid becomes a magic square. If such positive integer *x* does not exist, output <=-<=1 instead.
If there are multiple solutions, you may print any of them. | [
"3\n4 0 2\n3 5 7\n8 1 6\n",
"4\n1 1 1 1\n1 1 0 1\n1 1 1 1\n1 1 1 1\n",
"4\n1 1 1 1\n1 1 0 1\n1 1 2 1\n1 1 1 1\n"
] | [
"9\n",
"1\n",
"-1\n"
] | In the first sample case, we can fill in 9 into the empty cell to make the resulting grid a magic square. Indeed,
The sum of numbers in each row is:
4 + 9 + 2 = 3 + 5 + 7 = 8 + 1 + 6 = 15.
The sum of numbers in each column is:
4 + 3 + 8 = 9 + 5 + 1 = 2 + 7 + 6 = 15.
The sum of numbers in the two diagonals is:
4 + 5 + 6 = 2 + 5 + 8 = 15.
In the third sample case, it is impossible to fill a number in the empty square such that the resulting grid is a magic square. | [
{
"input": "3\n4 0 2\n3 5 7\n8 1 6",
"output": "9"
},
{
"input": "4\n1 1 1 1\n1 1 0 1\n1 1 1 1\n1 1 1 1",
"output": "1"
},
{
"input": "4\n1 1 1 1\n1 1 0 1\n1 1 2 1\n1 1 1 1",
"output": "-1"
},
{
"input": "1\n0",
"output": "1"
},
{
"input": "10\n92 67 99 74 1 51 8 ... | 0 | 0 | -1 | 8,108 | |
518 | Anya and Smartphone | [
"constructive algorithms",
"data structures",
"implementation"
] | null | null | Anya has bought a new smartphone that uses Berdroid operating system. The smartphone menu has exactly *n* applications, each application has its own icon. The icons are located on different screens, one screen contains *k* icons. The icons from the first to the *k*-th one are located on the first screen, from the (*k*<=+<=1)-th to the 2*k*-th ones are on the second screen and so on (the last screen may be partially empty).
Initially the smartphone menu is showing the screen number 1. To launch the application with the icon located on the screen *t*, Anya needs to make the following gestures: first she scrolls to the required screen number *t*, by making *t*<=-<=1 gestures (if the icon is on the screen *t*), and then make another gesture — press the icon of the required application exactly once to launch it.
After the application is launched, the menu returns to the first screen. That is, to launch the next application you need to scroll through the menu again starting from the screen number 1.
All applications are numbered from 1 to *n*. We know a certain order in which the icons of the applications are located in the menu at the beginning, but it changes as long as you use the operating system. Berdroid is intelligent system, so it changes the order of the icons by moving the more frequently used icons to the beginning of the list. Formally, right after an application is launched, Berdroid swaps the application icon and the icon of a preceding application (that is, the icon of an application on the position that is smaller by one in the order of menu). The preceding icon may possibly be located on the adjacent screen. The only exception is when the icon of the launched application already occupies the first place, in this case the icon arrangement doesn't change.
Anya has planned the order in which she will launch applications. How many gestures should Anya make to launch the applications in the planned order?
Note that one application may be launched multiple times. | The first line of the input contains three numbers *n*,<=*m*,<=*k* (1<=≤<=*n*,<=*m*,<=*k*<=≤<=105) — the number of applications that Anya has on her smartphone, the number of applications that will be launched and the number of icons that are located on the same screen.
The next line contains *n* integers, permutation *a*1,<=*a*2,<=...,<=*a**n* — the initial order of icons from left to right in the menu (from the first to the last one), *a**i* — is the id of the application, whose icon goes *i*-th in the menu. Each integer from 1 to *n* occurs exactly once among *a**i*.
The third line contains *m* integers *b*1,<=*b*2,<=...,<=*b**m*(1<=≤<=*b**i*<=≤<=*n*) — the ids of the launched applications in the planned order. One application may be launched multiple times. | Print a single number — the number of gestures that Anya needs to make to launch all the applications in the desired order. | [
"8 3 3\n1 2 3 4 5 6 7 8\n7 8 1\n",
"5 4 2\n3 1 5 2 4\n4 4 4 4\n"
] | [
"7\n",
"8\n"
] | In the first test the initial configuration looks like (123)(456)(78), that is, the first screen contains icons of applications 1, 2, 3, the second screen contains icons 4, 5, 6, the third screen contains icons 7, 8.
After application 7 is launched, we get the new arrangement of the icons — (123)(457)(68). To launch it Anya makes 3 gestures.
After application 8 is launched, we get configuration (123)(457)(86). To launch it Anya makes 3 gestures.
After application 1 is launched, the arrangement of icons in the menu doesn't change. To launch it Anya makes 1 gesture.
In total, Anya makes 7 gestures. | [
{
"input": "8 3 3\n1 2 3 4 5 6 7 8\n7 8 1",
"output": "7"
},
{
"input": "5 4 2\n3 1 5 2 4\n4 4 4 4",
"output": "8"
},
{
"input": "10 10 3\n1 2 3 4 5 6 7 8 9 10\n2 3 4 5 6 7 8 9 10 1",
"output": "25"
},
{
"input": "10 12 3\n6 1 2 9 3 10 8 5 7 4\n3 9 9 4 8 2 3 8 10 8 3 4",
... | 327 | 15,769,600 | 3 | 8,109 | |
883 | Automatic Door | [
"implementation"
] | null | null | There is an automatic door at the entrance of a factory. The door works in the following way:
- when one or several people come to the door and it is closed, the door immediately opens automatically and all people immediately come inside, - when one or several people come to the door and it is open, all people immediately come inside, - opened door immediately closes in *d* seconds after its opening, - if the door is closing and one or several people are coming to the door at the same moment, then all of them will have enough time to enter and only after that the door will close.
For example, if *d*<==<=3 and four people are coming at four different moments of time *t*1<==<=4, *t*2<==<=7, *t*3<==<=9 and *t*4<==<=13 then the door will open three times: at moments 4, 9 and 13. It will close at moments 7 and 12.
It is known that *n* employees will enter at moments *a*,<=2·*a*,<=3·*a*,<=...,<=*n*·*a* (the value *a* is positive integer). Also *m* clients will enter at moments *t*1,<=*t*2,<=...,<=*t**m*.
Write program to find the number of times the automatic door will open. Assume that the door is initially closed. | The first line contains four integers *n*, *m*, *a* and *d* (1<=≤<=*n*,<=*a*<=≤<=109, 1<=≤<=*m*<=≤<=105, 1<=≤<=*d*<=≤<=1018) — the number of the employees, the number of the clients, the moment of time when the first employee will come and the period of time in which the door closes.
The second line contains integer sequence *t*1,<=*t*2,<=...,<=*t**m* (1<=≤<=*t**i*<=≤<=1018) — moments of time when clients will come. The values *t**i* are given in non-decreasing order. | Print the number of times the door will open. | [
"1 1 3 4\n7\n",
"4 3 4 2\n7 9 11\n"
] | [
"1\n",
"4\n"
] | In the first example the only employee will come at moment 3. At this moment the door will open and will stay open until the moment 7. At the same moment of time the client will come, so at first he will enter and only after it the door will close. Thus the door will open one time. | [
{
"input": "1 1 3 4\n7",
"output": "1"
},
{
"input": "4 3 4 2\n7 9 11",
"output": "4"
},
{
"input": "10 10 51 69\n154 170 170 183 251 337 412 426 445 452",
"output": "6"
},
{
"input": "70 10 26 17\n361 371 579 585 629 872 944 1017 1048 1541",
"output": "70"
},
{
"... | 46 | 0 | 0 | 8,131 | |
0 | none | [
"none"
] | null | null | PMP is getting a warrior. He is practicing a lot, but the results are not acceptable yet. This time instead of programming contests, he decided to compete in a car racing to increase the spirit of victory. He decides to choose a competition that also exhibits algorithmic features.
AlgoRace is a special league of car racing where different teams compete in a country of *n* cities. Cities are numbered 1 through *n*. Every two distinct cities in the country are connected with one bidirectional road. Each competing team should introduce one driver and a set of cars.
The competition is held in *r* rounds. In *i*-th round, drivers will start at city *s**i* and finish at city *t**i*. Drivers are allowed to change their cars at most *k**i* times. Changing cars can take place in any city in no time. One car can be used multiple times in one round, but total number of changes should not exceed *k**i*. Drivers can freely choose their path to destination.
PMP has prepared *m* type of purpose-built cars. Beside for PMP’s driving skills, depending on properties of the car and the road, a car traverses each road in each direction in different times.
PMP Warriors wants to devise best strategies of choosing car and roads in each round to maximize the chance of winning the cup. For each round they want to find the minimum time required to finish it. | The first line contains three space-separated integers *n*,<=*m*,<=*r* (2<=≤<=*n*<=≤<=60,<=1<=≤<=*m*<=≤<=60,<=1<=≤<=*r*<=≤<=105) — the number of cities, the number of different types of cars and the number of rounds in the competition, correspondingly.
Next *m* sets of *n*<=×<=*n* matrices of integers between 0 to 106 (inclusive) will follow — describing the time one car requires to traverse different roads. The *k*-th integer in *j*-th line of the *i*-th set is the time that *i*-th car requires to traverse the road from *j*-th city to *k*-th city. These matrices are not necessarily symmetric, but their diagonal is always zero.
Next *r* lines contain description of the rounds. The *i*-th of these lines contains space-separated integers *s**i*,<=*t**i*,<=*k**i* (1<=≤<=*s**i*,<=*t**i*<=≤<=*n*,<=*s**i*<=≠<=*t**i*,<=0<=≤<=*k**i*<=≤<=1000) — the number of starting city, finishing city and the number of possible car changes in *i*-th round, correspondingly. | For each round you should print the minimum required time to complete the round in a single line. | [
"4 2 3\n0 1 5 6\n2 0 3 6\n1 3 0 1\n6 6 7 0\n0 3 5 6\n2 0 1 6\n1 3 0 2\n6 6 7 0\n1 4 2\n1 4 1\n1 4 3\n",
"4 2 3\n0 7 3 3\n8 0 10 5\n1 1 0 4\n8 9 2 0\n0 3 3 9\n7 0 4 9\n3 8 0 4\n4 8 9 0\n2 3 3\n2 1 3\n1 2 2\n"
] | [
"3\n4\n3\n",
"4\n5\n3\n"
] | In the first sample, in all rounds PMP goes from city #1 to city #2, then city #3 and finally city #4. But the sequences of types of the cars he uses are (1, 2, 1) in the first round and (1, 2, 2) in the second round. In the third round, although he can change his car three times, he uses the same strategy as the first round which only needs two car changes. | [] | 62 | 0 | 0 | 8,132 | |
771 | Bear and Rectangle Strips | [
"dp",
"greedy"
] | null | null | Limak has a grid that consists of 2 rows and *n* columns. The *j*-th cell in the *i*-th row contains an integer *t**i*,<=*j* which can be positive, negative or zero.
A non-empty rectangle of cells is called nice if and only if the sum of numbers in its cells is equal to 0.
Limak wants to choose some nice rectangles and give them to his friends, as gifts. No two chosen rectangles should share a cell. What is the maximum possible number of nice rectangles Limak can choose? | The first line of the input contains an integer *n* (1<=≤<=*n*<=≤<=300<=000) — the number of columns in the grid.
The next two lines contain numbers in the grid. The *i*-th of those two lines contains *n* integers *t**i*,<=1,<=*t**i*,<=2,<=...,<=*t**i*,<=*n* (<=-<=109<=≤<=*t**i*,<=*j*<=≤<=109). | Print one integer, denoting the maximum possible number of cell-disjoint nice rectangles. | [
"6\n70 70 70 70 70 -15\n90 -60 -30 30 -30 15\n",
"4\n0 -1 0 0\n0 0 1 0\n",
"3\n1000000000 999999999 -1000000000\n999999999 -1000000000 -999999998\n"
] | [
"3\n",
"6\n",
"1\n"
] | In the first sample, there are four nice rectangles:
Limak can't choose all of them because they are not disjoint. He should take three nice rectangles: those denoted as blue frames on the drawings.
In the second sample, it's optimal to choose six nice rectangles, each consisting of one cell with a number 0.
In the third sample, the only nice rectangle is the whole grid — the sum of all numbers is 0. Clearly, Limak can choose at most one nice rectangle, so the answer is 1. | [] | 0 | 0 | -1 | 8,136 | |
978 | Bus Video System | [
"combinatorics",
"math"
] | null | null | The busses in Berland are equipped with a video surveillance system. The system records information about changes in the number of passengers in a bus after stops.
If $x$ is the number of passengers in a bus just before the current bus stop and $y$ is the number of passengers in the bus just after current bus stop, the system records the number $y-x$. So the system records show how number of passengers changed.
The test run was made for single bus and $n$ bus stops. Thus, the system recorded the sequence of integers $a_1, a_2, \dots, a_n$ (exactly one number for each bus stop), where $a_i$ is the record for the bus stop $i$. The bus stops are numbered from $1$ to $n$ in chronological order.
Determine the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to $w$ (that is, at any time in the bus there should be from $0$ to $w$ passengers inclusive). | The first line contains two integers $n$ and $w$ $(1 \le n \le 1\,000, 1 \le w \le 10^{9})$ — the number of bus stops and the capacity of the bus.
The second line contains a sequence $a_1, a_2, \dots, a_n$ $(-10^{6} \le a_i \le 10^{6})$, where $a_i$ equals to the number, which has been recorded by the video system after the $i$-th bus stop. | Print the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to $w$. If the situation is contradictory (i.e. for any initial number of passengers there will be a contradiction), print 0. | [
"3 5\n2 1 -3\n",
"2 4\n-1 1\n",
"4 10\n2 4 1 2\n"
] | [
"3\n",
"4\n",
"2\n"
] | In the first example initially in the bus could be $0$, $1$ or $2$ passengers.
In the second example initially in the bus could be $1$, $2$, $3$ or $4$ passengers.
In the third example initially in the bus could be $0$ or $1$ passenger. | [
{
"input": "3 5\n2 1 -3",
"output": "3"
},
{
"input": "2 4\n-1 1",
"output": "4"
},
{
"input": "4 10\n2 4 1 2",
"output": "2"
},
{
"input": "2 10\n-1 2",
"output": "9"
},
{
"input": "3 4\n-3 -4 4",
"output": "0"
},
{
"input": "10 1\n-1 -1 3 -4 2 3 0 -3... | 46 | 0 | 3 | 8,137 | |
76 | Plus and xor | [
"dp",
"greedy",
"math"
] | D. Plus and xor | 0 | 256 | Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.
For example, if *X*<==<=10910<==<=11011012, *Y*<==<=4110<==<=1010012, then:
Write a program, which takes two non-negative integers *A* and *B* as an input and finds two non-negative integers *X* and *Y*, which satisfy the following conditions:
- *A*<==<=*X*<=+<=*Y* - *B* <==<= *X* xor *Y*, where xor is bitwise exclusive or. - *X* is the smallest number among all numbers for which the first two conditions are true. | The first line contains integer number *A* and the second line contains integer number *B* (0<=≤<=*A*,<=*B*<=≤<=264<=-<=1). | The only output line should contain two integer non-negative numbers *X* and *Y*. Print the only number -1 if there is no answer. | [
"142\n76\n"
] | [
"33 109\n"
] | none | [
{
"input": "142\n76",
"output": "33 109"
},
{
"input": "638\n126",
"output": "256 382"
},
{
"input": "1639\n1176",
"output": "-1"
},
{
"input": "12608\n0",
"output": "6304 6304"
},
{
"input": "104066\n104066",
"output": "0 104066"
},
{
"input": "102499... | 92 | 0 | 3 | 8,141 |
220 | Little Elephant and Shifts | [
"data structures"
] | null | null | The Little Elephant has two permutations *a* and *b* of length *n*, consisting of numbers from 1 to *n*, inclusive. Let's denote the *i*-th (1<=≤<=*i*<=≤<=*n*) element of the permutation *a* as *a**i*, the *j*-th (1<=≤<=*j*<=≤<=*n*) element of the permutation *b* — as *b**j*.
The distance between permutations *a* and *b* is the minimum absolute value of the difference between the positions of the occurrences of some number in *a* and in *b*. More formally, it's such minimum |*i*<=-<=*j*|, that *a**i*<==<=*b**j*.
A cyclic shift number *i* (1<=≤<=*i*<=≤<=*n*) of permutation *b* consisting from *n* elements is a permutation *b**i**b**i*<=+<=1... *b**n**b*1*b*2... *b**i*<=-<=1. Overall a permutation has *n* cyclic shifts.
The Little Elephant wonders, for all cyclic shifts of permutation *b*, what is the distance between the cyclic shift and permutation *a*? | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=105) — the size of the permutations. The second line contains permutation *a* as *n* distinct numbers from 1 to *n*, inclusive. The numbers are separated with single spaces. The third line contains permutation *b* in the same format. | In *n* lines print *n* integers — the answers for cyclic shifts. Print the answers to the shifts in the order of the shifts' numeration in permutation *b*, that is, first for the 1-st cyclic shift, then for the 2-nd, and so on. | [
"2\n1 2\n2 1\n",
"4\n2 1 3 4\n3 4 2 1\n"
] | [
"1\n0\n",
"2\n1\n0\n1\n"
] | none | [] | 92 | 0 | 0 | 8,162 | |
547 | Mike and Feet | [
"binary search",
"data structures",
"dp",
"dsu"
] | null | null | Mike is the president of country What-The-Fatherland. There are *n* bears living in this country besides Mike. All of them are standing in a line and they are numbered from 1 to *n* from left to right. *i*-th bear is exactly *a**i* feet high.
A group of bears is a non-empty contiguous segment of the line. The size of a group is the number of bears in that group. The strength of a group is the minimum height of the bear in that group.
Mike is a curious to know for each *x* such that 1<=≤<=*x*<=≤<=*n* the maximum strength among all groups of size *x*. | The first line of input contains integer *n* (1<=≤<=*n*<=≤<=2<=×<=105), the number of bears.
The second line contains *n* integers separated by space, *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109), heights of bears. | Print *n* integers in one line. For each *x* from 1 to *n*, print the maximum strength among all groups of size *x*. | [
"10\n1 2 3 4 5 4 3 2 1 6\n"
] | [
"6 4 4 3 3 2 2 1 1 1 \n"
] | none | [
{
"input": "10\n1 2 3 4 5 4 3 2 1 6",
"output": "6 4 4 3 3 2 2 1 1 1 "
},
{
"input": "3\n524125987 923264237 374288891",
"output": "923264237 524125987 374288891 "
},
{
"input": "5\n585325539 365329221 412106895 291882089 564718673",
"output": "585325539 365329221 365329221 291882089... | 0 | 0 | -1 | 8,181 | |
631 | Report | [
"data structures",
"sortings"
] | null | null | Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are *n* commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of *m* managers. Each of them may reorder the elements in some order. Namely, the *i*-th manager either sorts first *r**i* numbers in non-descending or non-ascending order and then passes the report to the manager *i*<=+<=1, or directly to Blake (if this manager has number *i*<==<=*m*).
Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence *a**i* of length *n* and the description of each manager, that is value *r**i* and his favourite order. You are asked to speed up the process and determine how the final report will look like. | The first line of the input contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=200<=000) — the number of commodities in the report and the number of managers, respectively.
The second line contains *n* integers *a**i* (|*a**i*|<=≤<=109) — the initial report before it gets to the first manager.
Then follow *m* lines with the descriptions of the operations managers are going to perform. The *i*-th of these lines contains two integers *t**i* and *r**i* (, 1<=≤<=*r**i*<=≤<=*n*), meaning that the *i*-th manager sorts the first *r**i* numbers either in the non-descending (if *t**i*<==<=1) or non-ascending (if *t**i*<==<=2) order. | Print *n* integers — the final report, which will be passed to Blake by manager number *m*. | [
"3 1\n1 2 3\n2 2\n",
"4 2\n1 2 4 3\n2 3\n1 2\n"
] | [
"2 1 3 ",
"2 4 1 3 "
] | In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.
In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake. | [
{
"input": "3 1\n1 2 3\n2 2",
"output": "2 1 3 "
},
{
"input": "4 2\n1 2 4 3\n2 3\n1 2",
"output": "2 4 1 3 "
},
{
"input": "4 1\n4 3 2 1\n1 4",
"output": "1 2 3 4 "
},
{
"input": "5 1\n1 2 3 4 5\n2 5",
"output": "5 4 3 2 1 "
},
{
"input": "6 2\n3 1 2 6 4 5\n1 6\n... | 2,000 | 14,643,200 | 0 | 8,201 | |
821 | Okabe and Boxes | [
"data structures",
"greedy",
"trees"
] | null | null | Okabe and Super Hacker Daru are stacking and removing boxes. There are *n* boxes numbered from 1 to *n*. Initially there are no boxes on the stack.
Okabe, being a control freak, gives Daru 2*n* commands: *n* of which are to add a box to the top of the stack, and *n* of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to *n*. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.
That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.
Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed. | The first line of input contains the integer *n* (1<=≤<=*n*<=≤<=3·105) — the number of boxes.
Each of the next 2*n* lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer *x* (1<=≤<=*x*<=≤<=*n*) follows, indicating that Daru should add the box with number *x* to the top of the stack.
It is guaranteed that exactly *n* lines contain "add" operations, all the boxes added are distinct, and *n* lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed. | Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands. | [
"3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove\n",
"7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove\n"
] | [
"1\n",
"2\n"
] | In the first sample, Daru should reorder the boxes after adding box 3 to the stack.
In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack. | [
{
"input": "3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove",
"output": "1"
},
{
"input": "7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove",
"output": "2"
},
{
"input": "4\nadd 1\nadd 3\nremove\nadd 4\nadd 2\nremove\nremove\nremov... | 93 | 23,142,400 | -1 | 8,204 | |
10 | Greedy Change | [
"constructive algorithms"
] | E. Greedy Change | 2 | 256 | Billy investigates the question of applying greedy algorithm to different spheres of life. At the moment he is studying the application of greedy algorithm to the problem about change. There is an amount of *n* coins of different face values, and the coins of each value are not limited in number. The task is to collect the sum *x* with the minimum amount of coins. Greedy algorithm with each its step takes the coin of the highest face value, not exceeding *x*. Obviously, if among the coins' face values exists the face value 1, any sum *x* can be collected with the help of greedy algorithm. However, greedy algorithm does not always give the optimal representation of the sum, i.e. the representation with the minimum amount of coins. For example, if there are face values {1,<=3,<=4} and it is asked to collect the sum 6, greedy algorithm will represent the sum as 4<=+<=1<=+<=1, while the optimal representation is 3<=+<=3, containing one coin less. By the given set of face values find out if there exist such a sum *x* that greedy algorithm will collect in a non-optimal way. If such a sum exists, find out the smallest of these sums. | The first line contains an integer *n* (1<=≤<=*n*<=≤<=400) — the amount of the coins' face values. The second line contains *n* integers *a**i* (1<=≤<=*a**i*<=≤<=109), describing the face values. It is guaranteed that *a*1<=><=*a*2<=><=...<=><=*a**n* and *a**n*<==<=1. | If greedy algorithm collects any sum in an optimal way, output -1. Otherwise output the smallest sum that greedy algorithm collects in a non-optimal way. | [
"5\n25 10 5 2 1\n",
"3\n4 3 1\n"
] | [
"-1\n",
"6\n"
] | none | [
{
"input": "5\n25 10 5 2 1",
"output": "-1"
},
{
"input": "3\n4 3 1",
"output": "6"
},
{
"input": "5\n9 8 5 2 1",
"output": "13"
},
{
"input": "5\n18 17 10 2 1",
"output": "27"
},
{
"input": "4\n73 70 33 1",
"output": "99"
},
{
"input": "4\n25 10 5 1",... | 62 | 0 | 0 | 8,208 |
837 | Two Seals | [
"brute force",
"implementation"
] | null | null | One very important person has a piece of paper in the form of a rectangle *a*<=×<=*b*.
Also, he has *n* seals. Each seal leaves an impression on the paper in the form of a rectangle of the size *x**i*<=×<=*y**i*. Each impression must be parallel to the sides of the piece of paper (but seal can be rotated by 90 degrees).
A very important person wants to choose two different seals and put them two impressions. Each of the selected seals puts exactly one impression. Impressions should not overlap (but they can touch sides), and the total area occupied by them should be the largest possible. What is the largest area that can be occupied by two seals? | The first line contains three integer numbers *n*, *a* and *b* (1<=≤<=*n*,<=*a*,<=*b*<=≤<=100).
Each of the next *n* lines contain two numbers *x**i*, *y**i* (1<=≤<=*x**i*,<=*y**i*<=≤<=100). | Print the largest total area that can be occupied by two seals. If you can not select two seals, print 0. | [
"2 2 2\n1 2\n2 1\n",
"4 10 9\n2 3\n1 1\n5 10\n9 11\n",
"3 10 10\n6 6\n7 7\n20 5\n"
] | [
"4\n",
"56\n",
"0\n"
] | In the first example you can rotate the second seal by 90 degrees. Then put impression of it right under the impression of the first seal. This will occupy all the piece of paper.
In the second example you can't choose the last seal because it doesn't fit. By choosing the first and the third seals you occupy the largest area.
In the third example there is no such pair of seals that they both can fit on a piece of paper. | [
{
"input": "2 2 2\n1 2\n2 1",
"output": "4"
},
{
"input": "4 10 9\n2 3\n1 1\n5 10\n9 11",
"output": "56"
},
{
"input": "3 10 10\n6 6\n7 7\n20 5",
"output": "0"
},
{
"input": "2 1 1\n1 1\n1 1",
"output": "0"
},
{
"input": "2 1 2\n1 1\n1 1",
"output": "2"
},
... | 233 | 9,216,000 | 3 | 8,210 | |
387 | George and Number | [
"greedy",
"implementation"
] | null | null | George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers *b*. During the game, George modifies the array by using special changes. Let's mark George's current array as *b*1,<=*b*2,<=...,<=*b*|*b*| (record |*b*| denotes the current length of the array). Then one change is a sequence of actions:
- Choose two distinct indexes *i* and *j* (1<=≤<=*i*,<=*j*<=≤<=|*b*|; *i*<=≠<=*j*), such that *b**i*<=≥<=*b**j*. - Get number *v*<==<=*concat*(*b**i*,<=*b**j*), where *concat*(*x*,<=*y*) is a number obtained by adding number *y* to the end of the decimal record of number *x*. For example, *concat*(500,<=10)<==<=50010, *concat*(2,<=2)<==<=22. - Add number *v* to the end of the array. The length of the array will increase by one. - Remove from the array numbers with indexes *i* and *j*. The length of the array will decrease by two, and elements of the array will become re-numbered from 1 to current length of the array.
George played for a long time with his array *b* and received from array *b* an array consisting of exactly one number *p*. Now George wants to know: what is the maximum number of elements array *b* could contain originally? Help him find this number. Note that originally the array could contain only positive integers. | The first line of the input contains a single integer *p* (1<=≤<=*p*<=<<=10100000). It is guaranteed that number *p* doesn't contain any leading zeroes. | Print an integer — the maximum number of elements array *b* could contain originally. | [
"9555\n",
"10000000005\n",
"800101\n",
"45\n",
"1000000000000001223300003342220044555\n",
"19992000\n",
"310200\n"
] | [
"4",
"2",
"3",
"1",
"17",
"1",
"2"
] | Let's consider the test examples:
- Originally array *b* can be equal to {5, 9, 5, 5}. The sequence of George's changes could have been: {5, 9, 5, 5} → {5, 5, 95} → {95, 55} → {9555}. - Originally array *b* could be equal to {1000000000, 5}. Please note that the array *b* cannot contain zeros. - Originally array *b* could be equal to {800, 10, 1}. - Originally array *b* could be equal to {45}. It cannot be equal to {4, 5}, because George can get only array {54} from this array in one operation.
Note that the numbers can be very large. | [
{
"input": "9555",
"output": "4"
},
{
"input": "10000000005",
"output": "2"
},
{
"input": "800101",
"output": "3"
},
{
"input": "45",
"output": "1"
},
{
"input": "1000000000000001223300003342220044555",
"output": "17"
},
{
"input": "19992000",
"out... | 795 | 10,649,600 | -1 | 8,226 | |
0 | none | [
"none"
] | null | null | Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections *i* and 2*i* and another road between *i* and 2*i*<=+<=1 for every positive integer *i*. You can clearly see that there exists a unique shortest path between any two intersections.
Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will *q* consecutive events happen soon. There are two types of events:
1. Government makes a new rule. A rule can be denoted by integers *v*, *u* and *w*. As the result of this action, the passing fee of all roads on the shortest path from *u* to *v* increases by *w* dollars.
2. Barney starts moving from some intersection *v* and goes to intersection *u* where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections.
Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes). | The first line of input contains a single integer *q* (1<=≤<=*q*<=≤<=1<=000).
The next *q* lines contain the information about the events in chronological order. Each event is described in form 1 *v* *u* *w* if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from *u* to *v* by *w* dollars, or in form 2 *v* *u* if it's an event when Barnie goes to cuddle from the intersection *v* to the intersection *u*.
1<=≤<=*v*,<=*u*<=≤<=1018,<=*v*<=≠<=*u*,<=1<=≤<=*w*<=≤<=109 states for every description line. | For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events. | [
"7\n1 3 4 30\n1 4 1 2\n1 3 6 8\n2 4 3\n1 6 1 40\n2 3 7\n2 2 4\n"
] | [
"94\n0\n32\n"
] | In the example testcase:
Here are the intersections used:
1. Intersections on the path are 3, 1, 2 and 4. 1. Intersections on the path are 4, 2 and 1. 1. Intersections on the path are only 3 and 6. 1. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. 1. Intersections on the path are 6, 3 and 1. 1. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. 1. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second). | [
{
"input": "7\n1 3 4 30\n1 4 1 2\n1 3 6 8\n2 4 3\n1 6 1 40\n2 3 7\n2 2 4",
"output": "94\n0\n32"
},
{
"input": "1\n2 666077344481199252 881371880336470888",
"output": "0"
},
{
"input": "10\n1 1 63669439577744021 396980128\n1 2582240553355225 63669439577744021 997926286\n1 258224055335522... | 93 | 3,993,600 | 3 | 8,227 | |
56 | Spoilt Permutation | [
"implementation"
] | B. Spoilt Permutation | 2 | 256 | Vasya collects coins: he has exactly one coin for every year from 1 to *n*. Naturally, Vasya keeps all the coins in his collection in the order in which they were released. Once Vasya's younger brother made a change — he took all the coins whose release year dated from *l* to *r* inclusively and put them in the reverse order. That is, he took a certain segment [*l*,<=*r*] and reversed it. At that the segment's endpoints did not coincide. For example, if *n*<==<=8, then initially Vasya's coins were kept in the order 1 2 3 4 5 6 7 8. If Vasya's younger brother chose the segment [2,<=6], then after the reversal the coin order will change to 1 6 5 4 3 2 7 8. Vasya suspects that someone else could have spoilt the permutation after his brother. Help him to find that out. Check if the given permutation can be obtained from the permutation 1 2 ... *n* using exactly one segment reversal. If it is possible, find the segment itself. | The first line contains an integer *n* (1<=≤<=*n*<=≤<=1000) which is the number of coins in Vasya's collection. The second line contains space-separated *n* integers which are the spoilt sequence of coins. It is guaranteed that the given sequence is a permutation, i.e. it contains only integers from 1 to *n*, and every number is used exactly 1 time. | If it is impossible to obtain the given permutation from the original one in exactly one action, print 0 0. Otherwise, print two numbers *l* *r* (1<=≤<=*l*<=<<=*r*<=≤<=*n*) which are the endpoints of the segment that needs to be reversed to obtain from permutation 1 2 ... *n* the given one. | [
"8\n1 6 5 4 3 2 7 8\n",
"4\n2 3 4 1\n",
"4\n1 2 3 4\n"
] | [
"2 6\n",
"0 0\n",
"0 0\n"
] | none | [
{
"input": "8\n1 6 5 4 3 2 7 8",
"output": "2 6"
},
{
"input": "4\n2 3 4 1",
"output": "0 0"
},
{
"input": "4\n1 2 3 4",
"output": "0 0"
},
{
"input": "8\n1 3 2 4 6 5 7 8",
"output": "0 0"
},
{
"input": "8\n1 3 4 2 6 5 7 8",
"output": "0 0"
},
{
"input... | 186 | 307,200 | 0 | 8,247 |
146 | Lucky Mask | [
"brute force",
"implementation"
] | null | null | Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya calls a mask of a positive integer *n* the number that is obtained after successive writing of all lucky digits of number *n* from the left to the right. For example, the mask of number 72174994 is number 7744, the mask of 7 is 7, the mask of 9999047 is 47. Obviously, mask of any number is always a lucky number.
Petya has two numbers — an arbitrary integer *a* and a lucky number *b*. Help him find the minimum number *c* (*c*<=><=*a*) such that the mask of number *c* equals *b*. | The only line contains two integers *a* and *b* (1<=≤<=*a*,<=*b*<=≤<=105). It is guaranteed that number *b* is lucky. | In the only line print a single number — the number *c* that is sought by Petya. | [
"1 7\n",
"100 47\n"
] | [
"7\n",
"147\n"
] | none | [
{
"input": "1 7",
"output": "7"
},
{
"input": "100 47",
"output": "147"
},
{
"input": "458 47",
"output": "467"
},
{
"input": "7 7",
"output": "17"
},
{
"input": "547 47",
"output": "647"
},
{
"input": "77 77",
"output": "177"
},
{
"input":... | 278 | 3,174,400 | 3 | 8,249 | |
255 | Mr. Bender and Square | [
"binary search",
"implementation",
"math"
] | null | null | Mr. Bender has a digital table of size *n*<=×<=*n*, each cell can be switched on or off. He wants the field to have at least *c* switched on squares. When this condition is fulfilled, Mr Bender will be happy.
We'll consider the table rows numbered from top to bottom from 1 to *n*, and the columns — numbered from left to right from 1 to *n*. Initially there is exactly one switched on cell with coordinates (*x*,<=*y*) (*x* is the row number, *y* is the column number), and all other cells are switched off. Then each second we switch on the cells that are off but have the side-adjacent cells that are on.
For a cell with coordinates (*x*,<=*y*) the side-adjacent cells are cells with coordinates (*x*<=-<=1,<=*y*), (*x*<=+<=1,<=*y*), (*x*,<=*y*<=-<=1), (*x*,<=*y*<=+<=1).
In how many seconds will Mr. Bender get happy? | The first line contains four space-separated integers *n*,<=*x*,<=*y*,<=*c* (1<=≤<=*n*,<=*c*<=≤<=109; 1<=≤<=*x*,<=*y*<=≤<=*n*; *c*<=≤<=*n*2). | In a single line print a single integer — the answer to the problem. | [
"6 4 3 1\n",
"9 3 8 10\n"
] | [
"0\n",
"2\n"
] | Initially the first test has one painted cell, so the answer is 0. In the second test all events will go as is shown on the figure. <img class="tex-graphics" src="https://espresso.codeforces.com/51bd695513bdc59c6ded01f0d34daa5361285209.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [
{
"input": "6 4 3 1",
"output": "0"
},
{
"input": "9 3 8 10",
"output": "2"
},
{
"input": "9 4 3 10",
"output": "2"
},
{
"input": "9 8 2 10",
"output": "2"
},
{
"input": "1 1 1 1",
"output": "0"
},
{
"input": "10 7 2 7",
"output": "2"
},
{
... | 30 | 0 | 0 | 8,273 | |
51 | Geometrical problem | [
"implementation"
] | D. Geometrical problem | 1 | 256 | Polycarp loves geometric progressions — he collects them. However, as such progressions occur very rarely, he also loves the sequences of numbers where it is enough to delete a single element to get a geometric progression.
In this task we shall define geometric progressions as finite sequences of numbers *a*1,<=*a*2,<=...,<=*a**k*, where *a**i*<==<=*c*·*b**i*<=-<=1 for some real numbers *c* and *b*. For example, the sequences [2, -4, 8], [0, 0, 0, 0], [199] are geometric progressions and [0, 1, 2, 3] is not.
Recently Polycarp has found a sequence and he can't classify it. Help him to do it. Determine whether it is a geometric progression. If it is not, check if it can become a geometric progression if an element is deleted from it. | The first line contains an integer *n* (1<=≤<=*n*<=≤<=105) — the number of elements in the given sequence. The second line contains the given sequence. The numbers are space-separated. All the elements of the given sequence are integers and their absolute value does not exceed 104. | Print 0, if the given sequence is a geometric progression. Otherwise, check if it is possible to make the sequence a geometric progression by deleting a single element. If it is possible, print 1. If it is impossible, print 2. | [
"4\n3 6 12 24\n",
"4\n-8 -16 24 -32\n",
"4\n0 1 2 3\n"
] | [
"0\n",
"1\n",
"2\n"
] | none | [
{
"input": "4\n3 6 12 24",
"output": "0"
},
{
"input": "4\n-8 -16 24 -32",
"output": "1"
},
{
"input": "4\n0 1 2 3",
"output": "2"
},
{
"input": "5\n1 1 1 1 2",
"output": "1"
},
{
"input": "4\n1 -1 1 -1",
"output": "0"
},
{
"input": "8\n1 2 4 8 16 32 -... | 154 | 0 | 0 | 8,294 |
490 | Hacking Cypher | [
"brute force",
"math",
"number theory",
"strings"
] | null | null | Polycarpus participates in a competition for hacking into a new secure messenger. He's almost won.
Having carefully studied the interaction protocol, Polycarpus came to the conclusion that the secret key can be obtained if he properly cuts the public key of the application into two parts. The public key is a long integer which may consist of even a million digits!
Polycarpus needs to find such a way to cut the public key into two nonempty parts, that the first (left) part is divisible by *a* as a separate number, and the second (right) part is divisible by *b* as a separate number. Both parts should be positive integers that have no leading zeros. Polycarpus knows values *a* and *b*.
Help Polycarpus and find any suitable method to cut the public key. | The first line of the input contains the public key of the messenger — an integer without leading zeroes, its length is in range from 1 to 106 digits. The second line contains a pair of space-separated positive integers *a*, *b* (1<=≤<=*a*,<=*b*<=≤<=108). | In the first line print "YES" (without the quotes), if the method satisfying conditions above exists. In this case, next print two lines — the left and right parts after the cut. These two parts, being concatenated, must be exactly identical to the public key. The left part must be divisible by *a*, and the right part must be divisible by *b*. The two parts must be positive integers having no leading zeros. If there are several answers, print any of them.
If there is no answer, print in a single line "NO" (without the quotes). | [
"116401024\n97 1024\n",
"284254589153928171911281811000\n1009 1000\n",
"120\n12 1\n"
] | [
"YES\n11640\n1024\n",
"YES\n2842545891539\n28171911281811000\n",
"NO\n"
] | none | [
{
"input": "116401024\n97 1024",
"output": "YES\n11640\n1024"
},
{
"input": "284254589153928171911281811000\n1009 1000",
"output": "YES\n2842545891539\n28171911281811000"
},
{
"input": "120\n12 1",
"output": "NO"
},
{
"input": "604\n6 4",
"output": "YES\n60\n4"
},
{
... | 1,000 | 10,956,800 | 0 | 8,302 | |
919 | Congruence Equation | [
"chinese remainder theorem",
"math",
"number theory"
] | null | null | Given an integer $x$. Your task is to find out how many positive integers $n$ ($1 \leq n \leq x$) satisfy $$n \cdot a^n \equiv b \quad (\textrm{mod}\;p),$$ where $a, b, p$ are all known constants. | The only line contains four integers $a,b,p,x$ ($2 \leq p \leq 10^6+3$, $1 \leq a,b < p$, $1 \leq x \leq 10^{12}$). It is guaranteed that $p$ is a prime. | Print a single integer: the number of possible answers $n$. | [
"2 3 5 8\n",
"4 6 7 13\n",
"233 233 10007 1\n"
] | [
"2\n",
"1\n",
"1\n"
] | In the first sample, we can see that $n=2$ and $n=8$ are possible answers. | [
{
"input": "2 3 5 8",
"output": "2"
},
{
"input": "4 6 7 13",
"output": "1"
},
{
"input": "233 233 10007 1",
"output": "1"
},
{
"input": "338792 190248 339821 152634074578",
"output": "449263"
},
{
"input": "629260 663548 739463 321804928248",
"output": "43481... | 1,356 | 69,632,000 | 0 | 8,319 | |
675 | Money Transfers | [
"constructive algorithms",
"data structures",
"greedy",
"sortings"
] | null | null | There are *n* banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank *n* are neighbours if *n*<=><=1. No bank is a neighbour of itself.
Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank.
There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.
Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero. | The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=100<=000) — the number of banks.
The second line contains *n* integers *a**i* (<=-<=109<=≤<=*a**i*<=≤<=109), the *i*-th of them is equal to the initial balance of the account in the *i*-th bank. It's guaranteed that the sum of all *a**i* is equal to 0. | Print the minimum number of operations required to change balance in each bank to zero. | [
"3\n5 0 -5\n",
"4\n-1 0 1 0\n",
"4\n1 2 3 -6\n"
] | [
"1\n",
"2\n",
"3\n"
] | In the first sample, Vasya may transfer 5 from the first bank to the third.
In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first.
In the third sample, the following sequence provides the optimal answer:
1. transfer 1 from the first bank to the second bank; 1. transfer 3 from the second bank to the third; 1. transfer 6 from the third bank to the fourth. | [
{
"input": "3\n5 0 -5",
"output": "1"
},
{
"input": "4\n-1 0 1 0",
"output": "2"
},
{
"input": "4\n1 2 3 -6",
"output": "3"
},
{
"input": "1\n0",
"output": "0"
},
{
"input": "50\n108431864 128274949 -554057370 -384620666 -202862975 -803855410 -482167063 -55139054 ... | 218 | 16,179,200 | 3 | 8,325 | |
28 | Bender Problem | [
"implementation"
] | A. Bender Problem | 2 | 256 | Robot Bender decided to make Fray a birthday present. He drove *n* nails and numbered them from 1 to *n* in some order. Bender decided to make a picture using metal rods. The picture is a closed polyline, which vertices should be nails (in the given order). The segments of the polyline should be parallel to the coordinate axes. Polyline is allowed to have self-intersections. Bender can take a rod and fold it exactly once in any place to form an angle of 90 degrees. Then he can attach the place of the fold to some unoccupied nail and attach two ends of this rod to adjacent nails. A nail is considered unoccupied if there is no rod attached to it (neither by it's end nor the by the fold place). No rod could be used twice. It is not required to use all the rods.
Help Bender to solve this difficult task. | The first line contains two positive integers *n* and *m* (4<=≤<=*n*<=≤<=500,<=2<=≤<=*m*<=≤<=500, *n* is even) — the amount of nails and the amount of rods. *i*-th of the following *n* lines contains a pair of integers, denoting the coordinates of the *i*-th nail. Nails should be connected in the same order as they are given in the input. The last line contains *m* integers — the lenghts of the rods. All coordinates do not exceed 104 by absolute value. Lengths of the rods are between 1 and 200<=000. No rod can be used twice. It is guaranteed that all segments of the given polyline are parallel to coordinate axes. No three consecutive nails lie on the same line. | If it is impossible to solve Bender's problem, output NO. Otherwise, output YES in the first line, and in the second line output *n* numbers — *i*-th of them should be the number of rod, which fold place is attached to the *i*-th nail, or -1, if there is no such rod.
If there are multiple solutions, print any of them. | [
"4 2\n0 0\n0 2\n2 2\n2 0\n4 4\n",
"6 3\n0 0\n1 0\n1 1\n2 1\n2 2\n0 2\n3 2 3\n",
"6 3\n0 0\n1 0\n1 1\n2 1\n2 2\n0 2\n2 2 3\n"
] | [
"YES\n1 -1 2 -1 \n",
"YES\n1 -1 2 -1 3 -1 \n",
"NO\n"
] | none | [
{
"input": "4 2\n0 0\n0 2\n2 2\n2 0\n4 4",
"output": "YES\n1 -1 2 -1 "
},
{
"input": "6 3\n0 0\n1 0\n1 1\n2 1\n2 2\n0 2\n3 2 3",
"output": "YES\n1 -1 2 -1 3 -1 "
},
{
"input": "6 3\n0 0\n1 0\n1 1\n2 1\n2 2\n0 2\n2 2 3",
"output": "NO"
},
{
"input": "4 4\n0 0\n0 1\n1 1\n1 0\n1... | 124 | 307,200 | 0 | 8,369 |
163 | Lemmings | [
"binary search"
] | null | null | As you know, lemmings like jumping. For the next spectacular group jump *n* lemmings gathered near a high rock with *k* comfortable ledges on it. The first ledge is situated at the height of *h* meters, the second one is at the height of 2*h* meters, and so on (the *i*-th ledge is at the height of *i*·*h* meters). The lemmings are going to jump at sunset, and there's not much time left.
Each lemming is characterized by its climbing speed of *v**i* meters per minute and its weight *m**i*. This means that the *i*-th lemming can climb to the *j*-th ledge in minutes.
To make the jump beautiful, heavier lemmings should jump from higher ledges: if a lemming of weight *m**i* jumps from ledge *i*, and a lemming of weight *m**j* jumps from ledge *j* (for *i*<=<<=*j*), then the inequation *m**i*<=≤<=*m**j* should be fulfilled.
Since there are *n* lemmings and only *k* ledges (*k*<=≤<=*n*), the *k* lemmings that will take part in the jump need to be chosen. The chosen lemmings should be distributed on the ledges from 1 to *k*, one lemming per ledge. The lemmings are to be arranged in the order of non-decreasing weight with the increasing height of the ledge. In addition, each lemming should have enough time to get to his ledge, that is, the time of his climb should not exceed *t* minutes. The lemmings climb to their ledges all at the same time and they do not interfere with each other.
Find the way to arrange the lemmings' jump so that time *t* is minimized. | The first line contains space-separated integers *n*, *k* and *h* (1<=≤<=*k*<=≤<=*n*<=≤<=105, 1<=≤<=*h*<=≤<=104) — the total number of lemmings, the number of ledges and the distance between adjacent ledges.
The second line contains *n* space-separated integers *m*1,<=*m*2,<=...,<=*m**n* (1<=≤<=*m**i*<=≤<=109), where *m**i* is the weight of *i*-th lemming.
The third line contains *n* space-separated integers *v*1,<=*v*2,<=...,<=*v**n* (1<=≤<=*v**i*<=≤<=109), where *v**i* is the speed of *i*-th lemming. | Print *k* different numbers from 1 to *n* — the numbers of the lemmings who go to ledges at heights *h*,<=2*h*,<=...,<=*kh*, correspondingly, if the jump is organized in an optimal way. If there are multiple ways to select the lemmings, pick any of them. | [
"5 3 2\n1 2 3 2 1\n1 2 1 2 10\n",
"5 3 10\n3 4 3 2 1\n5 4 3 2 1\n"
] | [
"5 2 4\n",
"4 3 1\n"
] | Let's consider the first sample case. The fifth lemming (speed 10) gets to the ledge at height 2 in <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/215e7035e1b836a262740867b9bbd824fd3c66fe.png" style="max-width: 100.0%;max-height: 100.0%;"/> minutes; the second lemming (speed 2) gets to the ledge at height 4 in 2 minutes; the fourth lemming (speed 2) gets to the ledge at height 6 in 3 minutes. All lemmings manage to occupy their positions in 3 minutes. | [] | 920 | 53,145,600 | 0 | 8,379 | |
409 | The Great Game | [
"*special"
] | null | null | Two teams meet in The Game World Championship. Some scientists consider this game to be the most intellectually challenging game in the world. You are given two strings describing the teams' actions in the final battle. Figure out who became the champion. | The input contains two strings of equal length (between 2 and 20 characters, inclusive). Each line describes the actions of one team. | Output "TEAM 1 WINS" if the first team won, "TEAM 2 WINS" if the second team won, and "TIE" if there was a tie. | [
"[]()[]8<\n8<[]()8<\n",
"8<8<()\n[]8<[]\n"
] | [
"TEAM 2 WINS\n",
"TIE\n"
] | none | [
{
"input": "[]()[]8<\n8<[]()8<",
"output": "TEAM 2 WINS"
},
{
"input": "8<8<()\n[]8<[]",
"output": "TIE"
},
{
"input": "()\n[]",
"output": "TEAM 2 WINS"
},
{
"input": "()\n8<",
"output": "TEAM 1 WINS"
},
{
"input": "8<\n[]",
"output": "TEAM 1 WINS"
},
{
... | 140 | 0 | 0 | 8,388 | |
376 | I.O.U. | [
"implementation"
] | null | null | Imagine that there is a group of three friends: A, B and С. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The debts still mean the same but the total sum of the debts now equals 20 rubles.
This task is a generalisation of a described example. Imagine that your group of friends has *n* people and you know the debts between the people. Optimize the given debts without changing their meaning. In other words, finally for each friend the difference between the total money he should give and the total money he should take must be the same. Print the minimum sum of all debts in the optimal rearrangement of the debts. See the notes to the test samples to better understand the problem. | The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=100; 0<=≤<=*m*<=≤<=104). The next *m* lines contain the debts. The *i*-th line contains three integers *a**i*,<=*b**i*,<=*c**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*; *a**i*<=≠<=*b**i*; 1<=≤<=*c**i*<=≤<=100), which mean that person *a**i* owes person *b**i* *c**i* rubles.
Assume that the people are numbered by integers from 1 to *n*.
It is guaranteed that the same pair of people occurs at most once in the input. The input doesn't simultaneously contain pair of people (*x*,<=*y*) and pair of people (*y*,<=*x*). | Print a single integer — the minimum sum of debts in the optimal rearrangement. | [
"5 3\n1 2 10\n2 3 1\n2 4 1\n",
"3 0\n",
"4 3\n1 2 1\n2 3 1\n3 1 1\n"
] | [
"10\n",
"0\n",
"0\n"
] | In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.
In the second sample, there are no debts.
In the third sample, you can annul all the debts. | [
{
"input": "5 3\n1 2 10\n2 3 1\n2 4 1",
"output": "10"
},
{
"input": "3 0",
"output": "0"
},
{
"input": "4 3\n1 2 1\n2 3 1\n3 1 1",
"output": "0"
},
{
"input": "20 28\n1 5 6\n1 12 7\n1 13 4\n1 15 7\n1 20 3\n2 4 1\n2 15 6\n3 5 3\n3 8 10\n3 13 8\n3 20 6\n4 6 10\n4 12 8\n4 19 5\... | 62 | 0 | 3 | 8,403 | |
32 | Hide-and-Seek | [
"geometry",
"implementation"
] | E. Hide-and-Seek | 2 | 256 | Victor and Peter are playing hide-and-seek. Peter has hidden, and Victor is to find him. In the room where they are playing, there is only one non-transparent wall and one double-sided mirror. Victor and Peter are points with coordinates (*x**v*,<=*y**v*) and (*x**p*,<=*y**p*) respectively. The wall is a segment joining points with coordinates (*x**w*,<=1,<=*y**w*,<=1) and (*x**w*,<=2,<=*y**w*,<=2), the mirror — a segment joining points (*x**m*,<=1,<=*y**m*,<=1) and (*x**m*,<=2,<=*y**m*,<=2).
If an obstacle has a common point with a line of vision, it's considered, that the boys can't see each other with this line of vision. If the mirror has a common point with the line of vision, it's considered, that the boys can see each other in the mirror, i.e. reflection takes place. The reflection process is governed by laws of physics — the angle of incidence is equal to the angle of reflection. The incident ray is in the same half-plane as the reflected ray, relative to the mirror. I.e. to see each other Victor and Peter should be to the same side of the line, containing the mirror (see example 1). If the line of vision is parallel to the mirror, reflection doesn't take place, and the mirror isn't regarded as an obstacle (see example 4).
Victor got interested if he can see Peter, while standing at the same spot. Help him solve this problem. | The first line contains two numbers *x**v* and *y**v* — coordinates of Victor.
The second line contains two numbers *x**p* and *y**p* — coordinates of Peter.
The third line contains 4 numbers *x**w*,<=1, *y**w*,<=1, *x**w*,<=2, *y**w*,<=2 — coordinates of the wall.
The forth line contains 4 numbers *x**m*,<=1, *y**m*,<=1, *x**m*,<=2, *y**m*,<=2 — coordinates of the mirror.
All the coordinates are integer numbers, and don't exceed 104 in absolute value. It's guaranteed, that the segments don't have common points, Victor and Peter are not on any of the segments, coordinates of Victor and Peter aren't the same, the segments don't degenerate into points. | Output YES, if Victor can see Peter without leaving the initial spot. Otherwise output NO. | [
"-1 3\n1 3\n0 2 0 4\n0 0 0 1\n",
"0 0\n1 1\n0 1 1 0\n-100 -100 -101 -101\n",
"0 0\n1 1\n0 1 1 0\n-1 1 1 3\n",
"0 0\n10 0\n100 100 101 101\n1 0 3 0\n"
] | [
"NO\n",
"NO\n",
"YES\n",
"YES\n"
] | none | [
{
"input": "-1 3\n1 3\n0 2 0 4\n0 0 0 1",
"output": "NO"
},
{
"input": "0 0\n1 1\n0 1 1 0\n-100 -100 -101 -101",
"output": "NO"
},
{
"input": "0 0\n1 1\n0 1 1 0\n-1 1 1 3",
"output": "YES"
},
{
"input": "0 0\n10 0\n100 100 101 101\n1 0 3 0",
"output": "YES"
},
{
"... | 186 | 0 | 3.9535 | 8,408 |
0 | none | [
"none"
] | null | null | We'll call a set of positive integers *a* beautiful if the following condition fulfills: for any prime *p*, if , then . In other words, if one number from the set is divisible by prime *p*, then at least half of numbers from the set is divisible by *p*.
Your task is to find any beautiful set, where the number of elements is equal to *k* and each element doesn't exceed 2*k*2. | The first line contains integer *k* (10<=≤<=*k*<=≤<=5000) that shows how many numbers the required beautiful set should have. | In the first line print *k* space-separated integers that are a beautiful set. If there are multiple such sets, you are allowed to print any of them. | [
"10\n"
] | [
"16 18 24 27 36 48 54 72 108 144 \n"
] | none | [] | 46 | 0 | 0 | 8,441 | |
975 | Ghosts | [
"geometry",
"math"
] | null | null | Ghosts live in harmony and peace, they travel the space without any purpose other than scare whoever stands in their way.
There are $n$ ghosts in the universe, they move in the $OXY$ plane, each one of them has its own velocity that does not change in time: $\overrightarrow{V} = V_{x}\overrightarrow{i} + V_{y}\overrightarrow{j}$ where $V_{x}$ is its speed on the $x$-axis and $V_{y}$ is on the $y$-axis.
A ghost $i$ has experience value $EX_i$, which represent how many ghosts tried to scare him in his past. Two ghosts scare each other if they were in the same cartesian point at a moment of time.
As the ghosts move with constant speed, after some moment of time there will be no further scaring (what a relief!) and the experience of ghost kind $GX = \sum_{i=1}^{n} EX_i$ will never increase.
Tameem is a red giant, he took a picture of the cartesian plane at a certain moment of time $T$, and magically all the ghosts were aligned on a line of the form $y = a \cdot x + b$. You have to compute what will be the experience index of the ghost kind $GX$ in the indefinite future, this is your task for today.
Note that when Tameem took the picture, $GX$ may already be greater than $0$, because many ghosts may have scared one another at any moment between $[-\infty, T]$. | The first line contains three integers $n$, $a$ and $b$ ($1 \leq n \leq 200000$, $1 \leq |a| \leq 10^9$, $0 \le |b| \le 10^9$) — the number of ghosts in the universe and the parameters of the straight line.
Each of the next $n$ lines contains three integers $x_i$, $V_{xi}$, $V_{yi}$ ($-10^9 \leq x_i \leq 10^9$, $-10^9 \leq V_{x i}, V_{y i} \leq 10^9$), where $x_i$ is the current $x$-coordinate of the $i$-th ghost (and $y_i = a \cdot x_i + b$).
It is guaranteed that no two ghosts share the same initial position, in other words, it is guaranteed that for all $(i,j)$ $x_i \neq x_j$ for $i \ne j$. | Output one line: experience index of the ghost kind $GX$ in the indefinite future. | [
"4 1 1\n1 -1 -1\n2 1 1\n3 1 1\n4 -1 -1\n",
"3 1 0\n-1 1 0\n0 0 -1\n1 -1 -2\n",
"3 1 0\n0 0 0\n1 0 0\n2 0 0\n"
] | [
"8\n",
"6\n",
"0\n"
] | There are four collisions $(1,2,T-0.5)$, $(1,3,T-1)$, $(2,4,T+1)$, $(3,4,T+0.5)$, where $(u,v,t)$ means a collision happened between ghosts $u$ and $v$ at moment $t$. At each collision, each ghost gained one experience point, this means that $GX = 4 \cdot 2 = 8$.
In the second test, all points will collide when $t = T + 1$.
The red arrow represents the 1-st ghost velocity, orange represents the 2-nd ghost velocity, and blue represents the 3-rd ghost velocity. | [
{
"input": "4 1 1\n1 -1 -1\n2 1 1\n3 1 1\n4 -1 -1",
"output": "8"
},
{
"input": "3 1 0\n-1 1 0\n0 0 -1\n1 -1 -2",
"output": "6"
},
{
"input": "3 1 0\n0 0 0\n1 0 0\n2 0 0",
"output": "0"
},
{
"input": "10 7 -626288749\n795312099 49439844 266151109\n-842143911 23740808 62497340... | 904 | 43,008,000 | 3 | 8,456 | |
769 | News About Credit | [
"*special",
"greedy",
"two pointers"
] | null | null | Polycarp studies at the university in the group which consists of *n* students (including himself). All they are registrated in the social net "TheContacnt!".
Not all students are equally sociable. About each student you know the value *a**i* — the maximum number of messages which the *i*-th student is agree to send per day. The student can't send messages to himself.
In early morning Polycarp knew important news that the programming credit will be tomorrow. For this reason it is necessary to urgently inform all groupmates about this news using private messages.
Your task is to make a plan of using private messages, so that:
- the student *i* sends no more than *a**i* messages (for all *i* from 1 to *n*); - all students knew the news about the credit (initially only Polycarp knew it); - the student can inform the other student only if he knows it himself.
Let's consider that all students are numerated by distinct numbers from 1 to *n*, and Polycarp always has the number 1.
In that task you shouldn't minimize the number of messages, the moment of time, when all knew about credit or some other parameters. Find any way how to use private messages which satisfies requirements above. | The first line contains the positive integer *n* (2<=≤<=*n*<=≤<=100) — the number of students.
The second line contains the sequence *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=100), where *a**i* equals to the maximum number of messages which can the *i*-th student agree to send. Consider that Polycarp always has the number 1. | Print -1 to the first line if it is impossible to inform all students about credit.
Otherwise, in the first line print the integer *k* — the number of messages which will be sent. In each of the next *k* lines print two distinct integers *f* and *t*, meaning that the student number *f* sent the message with news to the student number *t*. All messages should be printed in chronological order. It means that the student, who is sending the message, must already know this news. It is assumed that students can receive repeated messages with news of the credit.
If there are several answers, it is acceptable to print any of them. | [
"4\n1 2 1 0\n",
"6\n2 0 1 3 2 0\n",
"3\n0 2 2\n"
] | [
"3\n1 2\n2 4\n2 3\n",
"6\n1 3\n3 4\n1 2\n4 5\n5 6\n4 6\n",
"-1\n"
] | In the first test Polycarp (the student number 1) can send the message to the student number 2, who after that can send the message to students number 3 and 4. Thus, all students knew about the credit. | [
{
"input": "4\n1 2 1 0",
"output": "3\n1 2\n2 3\n2 4"
},
{
"input": "6\n2 0 1 3 2 0",
"output": "5\n1 4\n1 5\n4 3\n4 2\n4 6"
},
{
"input": "3\n0 2 2",
"output": "-1"
},
{
"input": "2\n0 0",
"output": "-1"
},
{
"input": "2\n1 0",
"output": "1\n1 2"
},
{
... | 62 | 4,915,200 | 3 | 8,460 | |
27 | Number With The Given Amount Of Divisors | [
"brute force",
"dp",
"number theory"
] | E. Number With The Given Amount Of Divisors | 2 | 256 | Given the number *n*, find the smallest positive integer which has exactly *n* divisors. It is guaranteed that for the given *n* the answer will not exceed 1018. | The first line of the input contains integer *n* (1<=≤<=*n*<=≤<=1000). | Output the smallest positive integer with exactly *n* divisors. | [
"4\n",
"6\n"
] | [
"6\n",
"12\n"
] | none | [
{
"input": "1",
"output": "1"
},
{
"input": "7",
"output": "64"
},
{
"input": "8",
"output": "24"
},
{
"input": "9",
"output": "36"
},
{
"input": "10",
"output": "48"
},
{
"input": "15",
"output": "144"
},
{
"input": "20",
"output": "24... | 92 | 0 | 3.977 | 8,477 |
774 | Big Number and Remainder | [
"*special",
"math",
"number theory"
] | null | null | Stepan has a very big positive integer.
Let's consider all cyclic shifts of Stepan's integer (if we look at his integer like at a string) which are also integers (i.e. they do not have leading zeros). Let's call such shifts as good shifts. For example, for the integer 10203 the good shifts are the integer itself 10203 and integers 20310 and 31020.
Stepan wants to know the minimum remainder of the division by the given number *m* among all good shifts. Your task is to determine the minimum remainder of the division by *m*. | The first line contains the integer which Stepan has. The length of Stepan's integer is between 2 and 200<=000 digits, inclusive. It is guaranteed that Stepan's integer does not contain leading zeros.
The second line contains the integer *m* (2<=≤<=*m*<=≤<=108) — the number by which Stepan divides good shifts of his integer. | Print the minimum remainder which Stepan can get if he divides all good shifts of his integer by the given number *m*. | [
"521\n3\n",
"1001\n5\n",
"5678901234567890123456789\n10000\n"
] | [
"2\n",
"0\n",
"123\n"
] | In the first example all good shifts of the integer 521 (good shifts are equal to 521, 215 and 152) has same remainder 2 when dividing by 3.
In the second example there are only two good shifts: the Stepan's integer itself and the shift by one position to the right. The integer itself is 1001 and the remainder after dividing it by 5 equals 1. The shift by one position to the right equals to 1100 and the remainder after dividing it by 5 equals 0, which is the minimum possible remainder. | [
{
"input": "521\n3",
"output": "2"
},
{
"input": "1001\n5",
"output": "0"
},
{
"input": "5678901234567890123456789\n10000",
"output": "123"
},
{
"input": "552352155\n13",
"output": "2"
},
{
"input": "11533077525260\n193983",
"output": "22331"
},
{
"inp... | 3,000 | 5,529,600 | 0 | 8,484 | |
0 | none | [
"none"
] | null | null | One day Natalia was walking in the woods when she met a little mushroom gnome. The gnome told her the following story:
Everybody knows that the mushroom gnomes' power lies in the magic mushrooms that grow in the native woods of the gnomes. There are *n* trees and *m* magic mushrooms in the woods: the *i*-th tree grows at a point on a straight line with coordinates *a**i* and has the height of *h**i*, the *j*-th mushroom grows at the point with coordinates *b**j* and has magical powers *z**j*.
But one day wild mushroommunchers, the sworn enemies of mushroom gnomes unleashed a terrible storm on their home forest. As a result, some of the trees began to fall and crush the magic mushrooms. The supreme oracle of mushroom gnomes calculated in advance the probability for each tree that it will fall to the left, to the right or will stand on. If the tree with the coordinate *x* and height *h* falls to the left, then all the mushrooms that belong to the right-open interval [*x*<=-<=*h*,<=*x*), are destroyed. If a tree falls to the right, then the mushrooms that belong to the left-open interval (*x*,<=*x*<=+<=*h*] are destroyed. Only those mushrooms that are not hit by a single tree survive.
Knowing that all the trees fall independently of each other (i.e., all the events are mutually independent, and besides, the trees do not interfere with other trees falling in an arbitrary direction), the supreme oracle was also able to quickly calculate what would be the expectation of the total power of the mushrooms which survived after the storm. His calculations ultimately saved the mushroom gnomes from imminent death.
Natalia, as a good Olympiad programmer, got interested in this story, and she decided to come up with a way to quickly calculate the expectation of the sum of the surviving mushrooms' power. | The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=105, 1<=≤<=*m*<=≤<=104) — the number of trees and mushrooms, respectively.
Each of the next *n* lines contain four integers — *a**i*, *h**i*, *l**i*, *r**i* (|*a**i*|<=≤<=109, 1<=≤<=*h**i*<=≤<=109, 0<=≤<=*l**i*,<=*r**i*,<=*l**i*<=+<=*r**i*<=≤<=100) which represent the coordinate of the *i*-th tree, its height, the percentage of the probabilities that the tree falls to the left and to the right, respectively (the remaining percentage is the probability that the tree will stand on).
Each of next *m* lines contain two integers *b**j*, *z**j* (|*b**j*|<=≤<=109, 1<=≤<=*z**j*<=≤<=103) which represent the coordinate and the magical power of the *j*-th mushroom, respectively.
An arbitrary number of trees and mushrooms can grow in one point. | Print a real number — the expectation of the total magical power of the surviving mushrooms. The result is accepted with relative or absolute accuracy 10<=-<=4. | [
"1 1\n2 2 50 50\n1 1\n",
"2 1\n2 2 50 50\n4 2 50 50\n3 1\n"
] | [
"0.5000000000\n",
"0.2500000000\n"
] | It is believed that the mushroom with the coordinate *x* belongs to the right-open interval [*l*, *r*) if and only if *l* ≤ *x* < *r*. Similarly, the mushroom with the coordinate *x* belongs to the left-open interval (*l*, *r*] if and only if *l* < *x* ≤ *r*.
In the first test the mushroom survives with the probability of 50%, depending on where the single tree falls.
In the second test the mushroom survives only if neither of the two trees falls on it. It occurs with the probability of 50% × 50% = 25%.
Pretest №12 is the large test with 10<sup class="upper-index">5</sup> trees and one mushroom. | [] | 31 | 0 | 0 | 8,498 | |
727 | Bill Total Value | [
"expression parsing",
"implementation",
"strings"
] | null | null | Vasily exited from a store and now he wants to recheck the total price of all purchases in his bill. The bill is a string in which the names of the purchases and their prices are printed in a row without any spaces. Check has the format "*name*1*price*1*name*2*price*2...*name**n**price**n*", where *name**i* (name of the *i*-th purchase) is a non-empty string of length not more than 10, consisting of lowercase English letters, and *price**i* (the price of the *i*-th purchase) is a non-empty string, consisting of digits and dots (decimal points). It is possible that purchases with equal names have different prices.
The price of each purchase is written in the following format. If the price is an integer number of dollars then cents are not written.
Otherwise, after the number of dollars a dot (decimal point) is written followed by cents in a two-digit format (if number of cents is between 1 and 9 inclusively, there is a leading zero).
Also, every three digits (from less significant to the most) in dollars are separated by dot (decimal point). No extra leading zeroes are allowed. The price always starts with a digit and ends with a digit.
For example:
- "234", "1.544", "149.431.10", "0.99" and "123.05" are valid prices, - ".333", "3.33.11", "12.00", ".33", "0.1234" and "1.2" are not valid.
Write a program that will find the total price of all purchases in the given bill. | The only line of the input contains a non-empty string *s* with length not greater than 1000 — the content of the bill.
It is guaranteed that the bill meets the format described above. It is guaranteed that each price in the bill is not less than one cent and not greater than 106 dollars. | Print the total price exactly in the same format as prices given in the input. | [
"chipsy48.32televizor12.390\n",
"a1b2c3.38\n",
"aa0.01t0.03\n"
] | [
"12.438.32\n",
"6.38\n",
"0.04\n"
] | none | [
{
"input": "chipsy48.32televizor12.390",
"output": "12.438.32"
},
{
"input": "a1b2c3.38",
"output": "6.38"
},
{
"input": "aa0.01t0.03",
"output": "0.04"
},
{
"input": "test0.50test0.50",
"output": "1"
},
{
"input": "a500b500",
"output": "1.000"
},
{
"i... | 93 | 6,963,200 | 0 | 8,508 | |
803 | Coprime Subsequences | [
"bitmasks",
"combinatorics",
"number theory"
] | null | null | Let's call a non-empty sequence of positive integers *a*1,<=*a*2... *a**k* coprime if the greatest common divisor of all elements of this sequence is equal to 1.
Given an array *a* consisting of *n* positive integers, find the number of its coprime subsequences. Since the answer may be very large, print it modulo 109<=+<=7.
Note that two subsequences are considered different if chosen indices are different. For example, in the array [1,<=1] there are 3 different subsequences: [1], [1] and [1,<=1]. | The first line contains one integer number *n* (1<=≤<=*n*<=≤<=100000).
The second line contains *n* integer numbers *a*1,<=*a*2... *a**n* (1<=≤<=*a**i*<=≤<=100000). | Print the number of coprime subsequences of *a* modulo 109<=+<=7. | [
"3\n1 2 3\n",
"4\n1 1 1 1\n",
"7\n1 3 5 15 3 105 35\n"
] | [
"5\n",
"15\n",
"100\n"
] | In the first example coprime subsequences are:
1. 1 1. 1, 2 1. 1, 3 1. 1, 2, 3 1. 2, 3
In the second example all subsequences are coprime. | [
{
"input": "3\n1 2 3",
"output": "5"
},
{
"input": "4\n1 1 1 1",
"output": "15"
},
{
"input": "7\n1 3 5 15 3 105 35",
"output": "100"
},
{
"input": "1\n1",
"output": "1"
},
{
"input": "1\n100000",
"output": "0"
},
{
"input": "5\n10 8 6 4 6",
"outpu... | 982 | 26,521,600 | 3 | 8,516 | |
545 | Toy Cars | [
"implementation"
] | null | null | Little Susie, thanks to her older brother, likes to play with cars. Today she decided to set up a tournament between them. The process of a tournament is described in the next paragraph.
There are *n* toy cars. Each pair collides. The result of a collision can be one of the following: no car turned over, one car turned over, both cars turned over. A car is good if it turned over in no collision. The results of the collisions are determined by an *n*<=×<=*n* matrix *А*: there is a number on the intersection of the *і*-th row and *j*-th column that describes the result of the collision of the *і*-th and the *j*-th car:
- <=-<=1: if this pair of cars never collided. <=-<=1 occurs only on the main diagonal of the matrix. - 0: if no car turned over during the collision. - 1: if only the *i*-th car turned over during the collision. - 2: if only the *j*-th car turned over during the collision. - 3: if both cars turned over during the collision.
Susie wants to find all the good cars. She quickly determined which cars are good. Can you cope with the task? | The first line contains integer *n* (1<=≤<=*n*<=≤<=100) — the number of cars.
Each of the next *n* lines contains *n* space-separated integers that determine matrix *A*.
It is guaranteed that on the main diagonal there are <=-<=1, and <=-<=1 doesn't appear anywhere else in the matrix.
It is guaranteed that the input is correct, that is, if *A**ij*<==<=1, then *A**ji*<==<=2, if *A**ij*<==<=3, then *A**ji*<==<=3, and if *A**ij*<==<=0, then *A**ji*<==<=0. | Print the number of good cars and in the next line print their space-separated indices in the increasing order. | [
"3\n-1 0 0\n0 -1 1\n0 2 -1\n",
"4\n-1 3 3 3\n3 -1 3 3\n3 3 -1 3\n3 3 3 -1\n"
] | [
"2\n1 3 ",
"0\n"
] | none | [
{
"input": "3\n-1 0 0\n0 -1 1\n0 2 -1",
"output": "2\n1 3 "
},
{
"input": "4\n-1 3 3 3\n3 -1 3 3\n3 3 -1 3\n3 3 3 -1",
"output": "0"
},
{
"input": "1\n-1",
"output": "1\n1 "
},
{
"input": "2\n-1 0\n0 -1",
"output": "2\n1 2 "
},
{
"input": "2\n-1 1\n2 -1",
"out... | 46 | 0 | 3 | 8,541 | |
618 | Constellation | [
"geometry",
"implementation"
] | null | null | Cat Noku has obtained a map of the night sky. On this map, he found a constellation with *n* stars numbered from 1 to *n*. For each *i*, the *i*-th star is located at coordinates (*x**i*,<=*y**i*). No two stars are located at the same position.
In the evening Noku is going to take a look at the night sky. He would like to find three distinct stars and form a triangle. The triangle must have positive area. In addition, all other stars must lie strictly outside of this triangle. He is having trouble finding the answer and would like your help. Your job is to find the indices of three stars that would form a triangle that satisfies all the conditions.
It is guaranteed that there is no line such that all stars lie on that line. It can be proven that if the previous condition is satisfied, there exists a solution to this problem. | The first line of the input contains a single integer *n* (3<=≤<=*n*<=≤<=100<=000).
Each of the next *n* lines contains two integers *x**i* and *y**i* (<=-<=109<=≤<=*x**i*,<=*y**i*<=≤<=109).
It is guaranteed that no two stars lie at the same point, and there does not exist a line such that all stars lie on that line. | Print three distinct integers on a single line — the indices of the three points that form a triangle that satisfies the conditions stated in the problem.
If there are multiple possible answers, you may print any of them. | [
"3\n0 1\n1 0\n1 1\n",
"5\n0 0\n0 2\n2 0\n2 2\n1 1\n"
] | [
"1 2 3\n",
"1 3 5\n"
] | In the first sample, we can print the three indices in any order.
In the second sample, we have the following picture.
Note that the triangle formed by starts 1, 4 and 3 doesn't satisfy the conditions stated in the problem, as point 5 is not strictly outside of this triangle (it lies on it's border). | [
{
"input": "3\n0 1\n1 0\n1 1",
"output": "1 2 3"
},
{
"input": "5\n0 0\n0 2\n2 0\n2 2\n1 1",
"output": "1 3 5"
},
{
"input": "3\n819934317 939682125\n487662889 8614219\n-557136619 382982369",
"output": "1 3 2"
},
{
"input": "10\n25280705 121178189\n219147240 -570920213\n-8298... | 717 | 307,200 | 0 | 8,544 | |
260 | Balls and Boxes | [
"constructive algorithms",
"greedy",
"implementation"
] | null | null | Little Vasya had *n* boxes with balls in the room. The boxes stood in a row and were numbered with numbers from 1 to *n* from left to right.
Once Vasya chose one of the boxes, let's assume that its number is *i*, took all balls out from it (it is guaranteed that this box originally had at least one ball), and began putting balls (one at a time) to the boxes with numbers *i*<=+<=1, *i*<=+<=2, *i*<=+<=3 and so on. If Vasya puts a ball into the box number *n*, then the next ball goes to box 1, the next one goes to box 2 and so on. He did it until he had no balls left in his hands. It is possible that Vasya puts multiple balls to the same box, and it is also possible that one or more balls will go to the box number *i*. If *i*<==<=*n*, Vasya puts the first ball into the box number 1, then the next ball goes to box 2 and so on.
For example, let's suppose that initially Vasya had four boxes, and the first box had 3 balls, the second one had 2, the third one had 5 and the fourth one had 4 balls. Then, if *i*<==<=3, then Vasya will take all five balls out of the third box and put them in the boxes with numbers: 4,<=1,<=2,<=3,<=4. After all Vasya's actions the balls will lie in the boxes as follows: in the first box there are 4 balls, 3 in the second one, 1 in the third one and 6 in the fourth one.
At this point Vasya has completely forgotten the original arrangement of the balls in the boxes, but he knows how they are arranged now, and the number *x* — the number of the box, where he put the last of the taken out balls.
He asks you to help to find the initial arrangement of the balls in the boxes. | The first line of the input contains two integers *n* and *x* (2<=≤<=*n*<=≤<=105, 1<=≤<=*x*<=≤<=*n*), that represent the number of the boxes and the index of the box that got the last ball from Vasya, correspondingly. The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n*, where integer *a**i* (0<=≤<=*a**i*<=≤<=109, *a**x*<=≠<=0) represents the number of balls in the box with index *i* after Vasya completes all the actions.
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. | Print *n* integers, where the *i*-th one represents the number of balls in the box number *i* before Vasya starts acting. Separate the numbers in the output by spaces. If there are multiple correct solutions, you are allowed to print any of them. | [
"4 4\n4 3 1 6\n",
"5 2\n3 2 0 2 7\n",
"3 3\n2 3 1\n"
] | [
"3 2 5 4 ",
"2 1 4 1 6 ",
"1 2 3 "
] | none | [
{
"input": "4 4\n4 3 1 6",
"output": "3 2 5 4 "
},
{
"input": "5 2\n3 2 0 2 7",
"output": "2 1 4 1 6 "
},
{
"input": "3 3\n2 3 1",
"output": "1 2 3 "
},
{
"input": "10 3\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 100000... | 93 | 0 | 0 | 8,565 | |
961 | Pair Of Lines | [
"geometry"
] | null | null | You are given *n* points on Cartesian plane. Every point is a lattice point (i.<=e. both of its coordinates are integers), and all points are distinct.
You may draw two straight lines (not necessarily distinct). Is it possible to do this in such a way that every point lies on at least one of these lines? | The first line contains one integer *n* (1<=≤<=*n*<=≤<=105) — the number of points you are given.
Then *n* lines follow, each line containing two integers *x**i* and *y**i* (|*x**i*|,<=|*y**i*|<=≤<=109)— coordinates of *i*-th point. All *n* points are distinct. | If it is possible to draw two straight lines in such a way that each of given points belongs to at least one of these lines, print YES. Otherwise, print NO. | [
"5\n0 0\n0 1\n1 1\n1 -1\n2 2\n",
"5\n0 0\n1 0\n2 1\n1 1\n2 3\n"
] | [
"YES\n",
"NO\n"
] | In the first example it is possible to draw two lines, the one containing the points 1, 3 and 5, and another one containing two remaining points. | [
{
"input": "5\n0 0\n0 1\n1 1\n1 -1\n2 2",
"output": "YES"
},
{
"input": "5\n0 0\n1 0\n2 1\n1 1\n2 3",
"output": "NO"
},
{
"input": "1\n-1000000000 1000000000",
"output": "YES"
},
{
"input": "5\n2 -1\n-4 1\n0 -9\n5 -9\n9 -10",
"output": "NO"
},
{
"input": "5\n6 1\n... | 326 | 34,816,000 | 3 | 8,575 | |
106 | Space Rescuers | [
"geometry",
"ternary search"
] | E. Space Rescuers | 2 | 256 | The Galaxy contains *n* planets, there are many different living creatures inhabiting each planet. And each creature can get into troubles! Space rescuers know it perfectly well and they are always ready to help anyone who really needs help. All you need to do is call for them.
Now the space rescuers plan to build the largest in the history of the Galaxy rescue station; however, the rescue station's location is yet to be determined. As some cases are real emergencies, the rescuers want to find such a point in the Galaxy from which it would be possible to get to the remotest planet in the minimum possible time. In other words, the rescuers need such point in the space that the distance between it and the planet remotest from it was minimal (if we compare this point with all other possible points in the space). Unfortunately, the rescuers can't sole this problem.
As the planets are quite remote from each other, they can be considered as points in Euclidean three-dimensional space. The distance between points (*x**i*,<=*y**i*,<=*z**i*) and (*x**j*,<=*y**j*,<=*z**j*) can be calculated by the formula . The rescue station can be positioned in any point in the space. It can also coincide with some planet.
Galaxy is in danger! Save the space rescuers and find the required point for them. | The first line of the input file contains integer *n* — the number of planets (1<=≤<=*N*<=≤<=100). Each of the following *n* lines contains information about the planets. The *i*-th line contains three integers *x**i*,<=*y**i*,<=*z**i* — the coordinates of the *i*-th planet (<=-<=104<=≤<=*x**i*,<=*y**i*,<=*z**i*<=≤<=104, 1<=≤<=*i*<=≤<=*n*). No two planets coincide. | Print on the first line of the output file three space-separated real numbers *x*0,<=*y*0,<=*z*0 — the coordinates for the future base. If there are several solutions, you are allowed to print any of them. The answer will be accepted if the distance from this point to the remotest planet will differ from the juries' variant in no more than 10<=-<=6 in absolute or relative value. | [
"5\n5 0 0\n-5 0 0\n0 3 4\n4 -3 0\n2 2 -2\n"
] | [
"0.000 0.000 0.000\n"
] | none | [
{
"input": "5\n5 0 0\n-5 0 0\n0 3 4\n4 -3 0\n2 2 -2",
"output": "-0.0000000017 -0.0000000319 0.0000000473"
},
{
"input": "4\n-2 -9 1\n10 4 0\n-1 1 0\n3 -10 -4",
"output": "4.0000068501 -2.5000015036 0.5000626514"
},
{
"input": "5\n6 0 -4\n8 1 5\n-8 5 -6\n-2 -4 -3\n8 -2 1",
"output": ... | 2,000 | 10,342,400 | 0 | 8,580 |
365 | The Fibonacci Segment | [
"implementation"
] | null | null | You have array *a*1,<=*a*2,<=...,<=*a**n*. Segment [*l*,<=*r*] (1<=≤<=*l*<=≤<=*r*<=≤<=*n*) is good if *a**i*<==<=*a**i*<=-<=1<=+<=*a**i*<=-<=2, for all *i* (*l*<=+<=2<=≤<=*i*<=≤<=*r*).
Let's define *len*([*l*,<=*r*])<==<=*r*<=-<=*l*<=+<=1, *len*([*l*,<=*r*]) is the length of the segment [*l*,<=*r*]. Segment [*l*1,<=*r*1], is longer than segment [*l*2,<=*r*2], if *len*([*l*1,<=*r*1])<=><=*len*([*l*2,<=*r*2]).
Your task is to find a good segment of the maximum length in array *a*. Note that a segment of length 1 or 2 is always good. | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=105) — the number of elements in the array. The second line contains integers: *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=109). | Print the length of the longest good segment in array *a*. | [
"10\n1 2 3 5 8 13 21 34 55 89\n",
"5\n1 1 1 1 1\n"
] | [
"10\n",
"2\n"
] | none | [
{
"input": "10\n1 2 3 5 8 13 21 34 55 89",
"output": "10"
},
{
"input": "5\n1 1 1 1 1",
"output": "2"
},
{
"input": "1\n1000",
"output": "1"
},
{
"input": "51\n1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0",
"output"... | 93 | 1,331,200 | 0 | 8,590 | |
245 | Internet Address | [
"implementation",
"strings"
] | null | null | Vasya is an active Internet user. One day he came across an Internet resource he liked, so he wrote its address in the notebook. We know that the address of the written resource has format:
where:
- <protocol> can equal either "http" (without the quotes) or "ftp" (without the quotes), - <domain> is a non-empty string, consisting of lowercase English letters, - the /<context> part may not be present. If it is present, then <context> is a non-empty string, consisting of lowercase English letters.
If string <context> isn't present in the address, then the additional character "/" isn't written. Thus, the address has either two characters "/" (the ones that go before the domain), or three (an extra one in front of the context).
When the boy came home, he found out that the address he wrote in his notebook had no punctuation marks. Vasya must have been in a lot of hurry and didn't write characters ":", "/", ".".
Help Vasya to restore the possible address of the recorded Internet resource. | The first line contains a non-empty string that Vasya wrote out in his notebook. This line consists of lowercase English letters only.
It is guaranteed that the given string contains at most 50 letters. It is guaranteed that the given string can be obtained from some correct Internet resource address, described above. | Print a single line — the address of the Internet resource that Vasya liked. If there are several addresses that meet the problem limitations, you are allowed to print any of them. | [
"httpsunrux\n",
"ftphttprururu\n"
] | [
"http://sun.ru/x\n",
"ftp://http.ru/ruru\n"
] | In the second sample there are two more possible answers: "ftp://httpruru.ru" and "ftp://httpru.ru/ru". | [
{
"input": "httpsunrux",
"output": "http://sun.ru/x"
},
{
"input": "ftphttprururu",
"output": "ftp://http.ru/ruru"
},
{
"input": "httpuururrururruruurururrrrrurrurrurruruuruuu",
"output": "http://uu.ru/rrururruruurururrrrrurrurrurruruuruuu"
},
{
"input": "httpabuaruauabbaruru... | 124 | 307,200 | 3 | 8,627 | |
718 | Efim and Strange Grade | [
"dp",
"implementation",
"math"
] | null | null | Efim just received his grade for the last test. He studies in a special school and his grade can be equal to any positive decimal fraction. First he got disappointed, as he expected a way more pleasant result. Then, he developed a tricky plan. Each second, he can ask his teacher to round the grade at any place after the decimal point (also, he can ask to round to the nearest integer).
There are *t* seconds left till the end of the break, so Efim has to act fast. Help him find what is the maximum grade he can get in no more than *t* seconds. Note, that he can choose to not use all *t* seconds. Moreover, he can even choose to not round the grade at all.
In this problem, classic rounding rules are used: while rounding number to the *n*-th digit one has to take a look at the digit *n*<=+<=1. If it is less than 5 than the *n*-th digit remain unchanged while all subsequent digits are replaced with 0. Otherwise, if the *n*<=+<=1 digit is greater or equal to 5, the digit at the position *n* is increased by 1 (this might also change some other digits, if this one was equal to 9) and all subsequent digits are replaced with 0. At the end, all trailing zeroes are thrown away.
For example, if the number 1.14 is rounded to the first decimal place, the result is 1.1, while if we round 1.5 to the nearest integer, the result is 2. Rounding number 1.299996121 in the fifth decimal place will result in number 1.3. | The first line of the input contains two integers *n* and *t* (1<=≤<=*n*<=≤<=200<=000, 1<=≤<=*t*<=≤<=109) — the length of Efim's grade and the number of seconds till the end of the break respectively.
The second line contains the grade itself. It's guaranteed that the grade is a positive number, containing at least one digit after the decimal points, and it's representation doesn't finish with 0. | Print the maximum grade that Efim can get in *t* seconds. Do not print trailing zeroes. | [
"6 1\n10.245\n",
"6 2\n10.245\n",
"3 100\n9.2\n"
] | [
"10.25\n",
"10.3\n",
"9.2\n"
] | In the first two samples Efim initially has grade 10.245.
During the first second Efim can obtain grade 10.25, and then 10.3 during the next second. Note, that the answer 10.30 will be considered incorrect.
In the third sample the optimal strategy is to not perform any rounding at all. | [
{
"input": "6 1\n10.245",
"output": "10.25"
},
{
"input": "6 2\n10.245",
"output": "10.3"
},
{
"input": "3 100\n9.2",
"output": "9.2"
},
{
"input": "12 5\n872.04488525",
"output": "872.1"
},
{
"input": "35 8\n984227318.2031144444444444494637612",
"output": "98... | 46 | 0 | -1 | 8,629 | |
962 | Students in Railway Carriage | [
"constructive algorithms",
"greedy",
"implementation"
] | null | null | There are $n$ consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger.
The university team for the Olympiad consists of $a$ student-programmers and $b$ student-athletes. Determine the largest number of students from all $a+b$ students, which you can put in the railway carriage so that:
- no student-programmer is sitting next to the student-programmer; - and no student-athlete is sitting next to the student-athlete.
In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting.
Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all). | The first line contain three integers $n$, $a$ and $b$ ($1 \le n \le 2\cdot10^{5}$, $0 \le a, b \le 2\cdot10^{5}$, $a + b > 0$) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes.
The second line contains a string with length $n$, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member. | Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete. | [
"5 1 1\n*...*\n",
"6 2 3\n*...*.\n",
"11 3 10\n.*....**.*.\n",
"3 2 3\n***\n"
] | [
"2\n",
"4\n",
"7\n",
"0\n"
] | In the first example you can put all student, for example, in the following way: *.AB*
In the second example you can put four students, for example, in the following way: *BAB*B
In the third example you can put seven students, for example, in the following way: B*ABAB**A*B
The letter A means a student-programmer, and the letter B — student-athlete. | [
{
"input": "5 1 1\n*...*",
"output": "2"
},
{
"input": "6 2 3\n*...*.",
"output": "4"
},
{
"input": "11 3 10\n.*....**.*.",
"output": "7"
},
{
"input": "3 2 3\n***",
"output": "0"
},
{
"input": "9 5 3\n*...*...*",
"output": "6"
},
{
"input": "9 2 4\n*.... | 265 | 8,396,800 | 0 | 8,633 | |
0 | none | [
"none"
] | null | null | Fox Ciel is participating in a party in Prime Kingdom. There are *n* foxes there (include Fox Ciel). The i-th fox is *a**i* years old.
They will have dinner around some round tables. You want to distribute foxes such that:
1. Each fox is sitting at some table. 1. Each table has at least 3 foxes sitting around it. 1. The sum of ages of any two adjacent foxes around each table should be a prime number.
If *k* foxes *f*1, *f*2, ..., *f**k* are sitting around table in clockwise order, then for 1<=≤<=*i*<=≤<=*k*<=-<=1: *f**i* and *f**i*<=+<=1 are adjacent, and *f*1 and *f**k* are also adjacent.
If it is possible to distribute the foxes in the desired manner, find out a way to do that. | The first line contains single integer *n* (3<=≤<=*n*<=≤<=200): the number of foxes in this party.
The second line contains *n* integers *a**i* (2<=≤<=*a**i*<=≤<=104). | If it is impossible to do this, output "Impossible".
Otherwise, in the first line output an integer *m* (): the number of tables.
Then output *m* lines, each line should start with an integer *k* -=– the number of foxes around that table, and then *k* numbers — indices of fox sitting around that table in clockwise order.
If there are several possible arrangements, output any of them. | [
"4\n3 4 8 9\n",
"5\n2 2 2 2 2\n",
"12\n2 3 4 5 6 7 8 9 10 11 12 13\n",
"24\n2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25\n"
] | [
"1\n4 1 2 4 3\n",
"Impossible\n",
"1\n12 1 2 3 6 5 12 9 8 7 10 11 4\n",
"3\n6 1 2 3 6 5 4\n10 7 8 9 12 15 14 13 16 11 10\n8 17 18 23 22 19 20 21 24\n"
] | In example 1, they can sit around one table, their ages are: 3-8-9-4, adjacent sums are: 11, 17, 13 and 7, all those integers are primes.
In example 2, it is not possible: the sum of 2+2 = 4 is not a prime number. | [
{
"input": "4\n3 4 8 9",
"output": "1\n4 1 2 4 3"
},
{
"input": "5\n2 2 2 2 2",
"output": "Impossible"
},
{
"input": "12\n2 3 4 5 6 7 8 9 10 11 12 13",
"output": "1\n12 1 2 3 6 5 12 9 8 7 10 11 4"
},
{
"input": "24\n2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24... | 46 | 0 | 0 | 8,634 | |
958 | Maximum Control (easy) | [
"implementation"
] | null | null | The Resistance is trying to take control over all planets in a particular solar system. This solar system is shaped like a tree. More precisely, some planets are connected by bidirectional hyperspace tunnels in such a way that there is a path between every pair of the planets, but removing any tunnel would disconnect some of them.
The Resistance already has measures in place that will, when the time is right, enable them to control every planet that is not remote. A planet is considered to be remote if it is connected to the rest of the planets only via a single hyperspace tunnel.
How much work is there left to be done: that is, how many remote planets are there? | The first line of the input contains an integer *N* (2<=≤<=*N*<=≤<=1000) – the number of planets in the galaxy.
The next *N*<=-<=1 lines describe the hyperspace tunnels between the planets. Each of the *N*<=-<=1 lines contains two space-separated integers *u* and *v* (1<=≤<=*u*,<=*v*<=≤<=*N*) indicating that there is a bidirectional hyperspace tunnel between the planets *u* and *v*. It is guaranteed that every two planets are connected by a path of tunnels, and that each tunnel connects a different pair of planets. | A single integer denoting the number of remote planets. | [
"5\n4 1\n4 2\n1 3\n1 5\n",
"4\n1 2\n4 3\n1 4\n"
] | [
"3\n",
"2\n"
] | In the first example, only planets 2, 3 and 5 are connected by a single tunnel.
In the second example, the remote planets are 2 and 3.
Note that this problem has only two versions – easy and medium. | [
{
"input": "5\n4 1\n4 2\n1 3\n1 5",
"output": "3"
},
{
"input": "4\n1 2\n4 3\n1 4",
"output": "2"
},
{
"input": "10\n4 3\n2 6\n10 1\n5 7\n5 8\n10 6\n5 9\n9 3\n2 9",
"output": "4"
}
] | 0 | 0 | -1 | 8,646 | |
376 | Lever | [
"implementation",
"math"
] | null | null | You have a description of a lever as string *s*. We'll represent the string length as record |*s*|, then the lever looks as a horizontal bar with weights of length |*s*|<=-<=1 with exactly one pivot. We will assume that the bar is a segment on the *Ox* axis between points 0 and |*s*|<=-<=1.
The decoding of the lever description is given below.
- If the *i*-th character of the string equals "^", that means that at coordinate *i* there is the pivot under the bar. - If the *i*-th character of the string equals "=", that means that at coordinate *i* there is nothing lying on the bar. - If the *i*-th character of the string equals digit *c* (1-9), that means that at coordinate *i* there is a weight of mass *c* on the bar.
Your task is, given the lever description, print if it will be in balance or not. Assume that the bar doesn't weight anything. Assume that the bar initially is in balance then all weights are simultaneously put on it. After that the bar either tilts to the left, or tilts to the right, or is in balance. | The first line contains the lever description as a non-empty string *s* (3<=≤<=|*s*|<=≤<=106), consisting of digits (1-9) and characters "^" and "=". It is guaranteed that the line contains exactly one character "^". It is guaranteed that the pivot of the lever isn't located in any end of the lever bar.
To solve the problem you may need 64-bit integer numbers. Please, do not forget to use them in your programs. | Print "left" if the given lever tilts to the left, "right" if it tilts to the right and "balance", if it is in balance. | [
"=^==\n",
"9===^==1\n",
"2==^7==\n",
"41^52==\n"
] | [
"balance\n",
"left\n",
"right\n",
"balance\n"
] | As you solve the problem, you may find the following link useful to better understand how a lever functions: http://en.wikipedia.org/wiki/Lever.
The pictures to the examples: | [
{
"input": "=^==",
"output": "balance"
},
{
"input": "9===^==1",
"output": "left"
},
{
"input": "2==^7==",
"output": "right"
},
{
"input": "41^52==",
"output": "balance"
},
{
"input": "=^2=4=1===1=",
"output": "right"
},
{
"input": "9=6===5==3=9=1=1^7=... | 249 | 18,124,800 | 3 | 8,649 | |
283 | Cows and Sequence | [
"constructive algorithms",
"data structures",
"implementation"
] | null | null | Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform *n* operations. Each operation is one of the following:
1. Add the integer *x**i* to the first *a**i* elements of the sequence. 1. Append an integer *k**i* to the end of the sequence. (And hence the size of the sequence increases by 1) 1. Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence.
After each operation, the cows would like to know the average of all the numbers in the sequence. Help them! | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=2·105) — the number of operations. The next *n* lines describe the operations. Each line will start with an integer *t**i* (1<=≤<=*t**i*<=≤<=3), denoting the type of the operation (see above). If *t**i*<==<=1, it will be followed by two integers *a**i*,<=*x**i* (|*x**i*|<=≤<=103; 1<=≤<=*a**i*). If *t**i*<==<=2, it will be followed by a single integer *k**i* (|*k**i*|<=≤<=103). If *t**i*<==<=3, it will not be followed by anything.
It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence. | Output *n* lines each containing the average of the numbers in the sequence after the corresponding operation.
The answer will be considered correct if its absolute or relative error doesn't exceed 10<=-<=6. | [
"5\n2 1\n3\n2 3\n2 1\n3\n",
"6\n2 1\n1 2 20\n2 2\n1 2 -3\n3\n3\n"
] | [
"0.500000\n0.000000\n1.500000\n1.333333\n1.500000\n",
"0.500000\n20.500000\n14.333333\n12.333333\n17.500000\n17.000000\n"
] | In the second sample, the sequence becomes <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/fb5aaaa5dc516fe540cef52fd153768bfdb941c8.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "5\n2 1\n3\n2 3\n2 1\n3",
"output": "0.500000\n0.000000\n1.500000\n1.333333\n1.500000"
},
{
"input": "6\n2 1\n1 2 20\n2 2\n1 2 -3\n3\n3",
"output": "0.500000\n20.500000\n14.333333\n12.333333\n17.500000\n17.000000"
},
{
"input": "1\n1 1 1",
"output": "1.000000"
},
{
... | 1,500 | 10,547,200 | 0 | 8,660 | |
730 | Ber Patio | [] | null | null | Polycarp is a regular customer at the restaurant "Ber Patio". He likes having lunches there.
"Ber Patio" has special discount program for regular customers. A customer can collect bonuses and partially cover expenses in the restaurant.
Let's assume a customer currently has *b* bonuses and she has to pay *r* burles for a lunch. In this case the customer can use bonuses (1 bonus = 1 burle) to reduce the payment. She can cover at most half of the payment using bonuses. However, 1 bonus will be added to the customer's bonus balance per each 10 burles she paid.
Formally:
1. a customer can choose any number *x* of bonuses to use ()), 1. the customer's bonus balance is reduced by *x*, 1. the customer pays *r*<=-<=*x* burles, 1. the customer's bonus balance is increased by ⌊(*r*<=-<=*x*)<=/<=10⌋ (i.e. integer division rounded down is used).
Initially, there are *b* bonuses on Polycarp's account. Polycarp is going to have a lunch in "Ber Patio" for the next *n* days. He estimated the values *a*1,<=*a*2,<=...,<=*a**n*, where *a**i* is the number of burles in a receipt for the *i*-th day. The sum over all receipts doesn't exceed 105 burles.
Write a program to find the minimum number of burles Polycarp has to spend and an optimal strategy to use bonuses. | The first line contains two integer numbers *n* and *b* (1<=≤<=*n*<=≤<=5000, 0<=≤<=*b*<=≤<=105) — number of days and initial number of bonuses Polycarp has.
The second line contains the integer sequence *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=1000), where *a**i* is the amount of burles in the *i*-th day's receipt.
It is guaranteed that the sum of all receipts does not exceed 105 burles. | On the first line, print the expected minimal number of burles to pay for all *n* receipts.
On the second line, print the sequence of integer numbers *b*1,<=*b*2,<=...,<=*b**n*, where *b**i* is the number of bonuses to use on the *i*-th day. If there are multiple solutions, print any of them. | [
"3 21\n12 75 52\n",
"3 39\n58 64 33\n"
] | [
"110\n2 5 22 \n",
"107\n28 4 16 \n"
] | none | [] | 31 | 0 | 0 | 8,662 | |
587 | Duff in Mafia | [
"2-sat",
"binary search"
] | null | null | Duff is one if the heads of Mafia in her country, Andarz Gu. Andarz Gu has *n* cities (numbered from 1 to *n*) connected by *m* bidirectional roads (numbered by 1 to *m*).
Each road has a destructing time, and a color. *i*-th road connects cities *v**i* and *u**i* and its color is *c**i* and its destructing time is *t**i*.
Mafia wants to destruct a matching in Andarz Gu. A matching is a subset of roads such that no two roads in this subset has common endpoint. They can destruct these roads in parallel, i. e. the total destruction time is a maximum over destruction times of all selected roads.
They want two conditions to be satisfied:
1. The remaining roads form a proper coloring. 1. Destructing time of this matching is minimized.
The remaining roads after destructing this matching form a proper coloring if and only if no two roads of the same color have same endpoint, or, in the other words, edges of each color should form a matching.
There is no programmer in Mafia. That's why Duff asked for your help. Please help her and determine which matching to destruct in order to satisfied those conditions (or state that this is not possible). | The first line of input contains two integers *n* and *m* (2<=≤<=*n*<=≤<=5<=×<=104 and 1<=≤<=*m*<=≤<=5<=×<=104), number of cities and number of roads in the country.
The next *m* lines contain the the roads. *i*<=-<=*th* of them contains four integers *v**i*,<=*u**i*,<=*c**i* and *t**i* (1<=≤<=*v**i*,<=*u**i*<=≤<=*n*, *v**i*<=≠<=*u**i* and 1<=≤<=*c**i*,<=*t**i*<=≤<=109 for each 1<=≤<=*i*<=≤<=*m*). | In the first line of input, print "Yes" (without quotes) if satisfying the first condition is possible and "No" (without quotes) otherwise.
If it is possible, then you have to print two integers *t* and *k* in the second line, the minimum destructing time and the number of roads in the matching ().
In the third line print *k* distinct integers separated by spaces, indices of the roads in the matching in any order. Roads are numbered starting from one in order of their appearance in the input.
If there's more than one solution, print any of them. | [
"5 7\n2 1 3 7\n3 1 1 6\n5 4 1 8\n4 5 1 1\n3 2 2 3\n4 5 2 5\n2 3 2 4\n",
"3 5\n3 2 1 3\n1 3 1 1\n3 2 1 4\n1 3 2 2\n1 3 2 10\n"
] | [
"Yes\n3 2\n4 5\n",
"No\n"
] | Graph of Andarz Gu in the first sample case is as follows:
A solution would be to destruct the roads with crosses.
Graph of Andarz Gu in the second sample case is as follows: | [] | 93 | 2,252,800 | -1 | 8,669 | |
900 | Remove Extra One | [
"brute force",
"data structures",
"math"
] | null | null | You are given a permutation *p* of length *n*. Remove one element from permutation to make the number of records the maximum possible.
We remind that in a sequence of numbers *a*1,<=*a*2,<=...,<=*a**k* the element *a**i* is a record if for every integer *j* (1<=≤<=*j*<=<<=*i*) the following holds: *a**j*<=<<=*a**i*. | The first line contains the only integer *n* (1<=≤<=*n*<=≤<=105) — the length of the permutation.
The second line contains *n* integers *p*1,<=*p*2,<=...,<=*p**n* (1<=≤<=*p**i*<=≤<=*n*) — the permutation. All the integers are distinct. | Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one. | [
"1\n1\n",
"5\n5 1 2 3 4\n"
] | [
"1\n",
"5\n"
] | In the first example the only element can be removed. | [
{
"input": "1\n1",
"output": "1"
},
{
"input": "5\n5 1 2 3 4",
"output": "5"
},
{
"input": "5\n4 3 5 1 2",
"output": "1"
},
{
"input": "9\n9 5 8 6 3 2 4 1 7",
"output": "9"
},
{
"input": "3\n3 2 1",
"output": "1"
},
{
"input": "7\n1 6 7 4 2 5 3",
"... | 186 | 2,560,000 | -1 | 8,679 | |
0 | none | [
"none"
] | null | null | You are given a string *s*, consisting of small Latin letters. Let's denote the length of the string as |*s*|. The characters in the string are numbered starting from 1.
Your task is to find out if it is possible to rearrange characters in string *s* so that for any prime number *p*<=≤<=|*s*| and for any integer *i* ranging from 1 to |*s*|<=/<=*p* (inclusive) the following condition was fulfilled *s**p*<==<=*s**p*<=×<=*i*. If the answer is positive, find one way to rearrange the characters. | The only line contains the initial string *s*, consisting of small Latin letters (1<=≤<=|*s*|<=≤<=1000). | If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". | [
"abc\n",
"abcd\n",
"xxxyxxx\n"
] | [
"YES\nabc\n",
"NO\n",
"YES\nxxxxxxy\n"
] | In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba".
In the second sample no letter permutation will satisfy the condition at *p* = 2 (*s*<sub class="lower-index">2</sub> = *s*<sub class="lower-index">4</sub>).
In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. | [
{
"input": "abc",
"output": "YES\nabc"
},
{
"input": "abcd",
"output": "NO"
},
{
"input": "xxxyxxx",
"output": "YES\nxxxxxxy"
},
{
"input": "xxxjddyxduquybxdxx",
"output": "NO"
},
{
"input": "jjjjjjjjjjzjjjjjjjjjjjjjjjj",
"output": "YES\njjjjjjjjjjjjjjjjjjjjjj... | 216 | 20,275,200 | 0 | 8,690 | |
1,004 | Sonya and Matrix | [
"brute force",
"constructive algorithms",
"implementation"
] | null | null | Since Sonya has just learned the basics of matrices, she decided to play with them a little bit.
Sonya imagined a new type of matrices that she called rhombic matrices. These matrices have exactly one zero, while all other cells have the Manhattan distance to the cell containing the zero. The cells with equal numbers have the form of a rhombus, that is why Sonya called this type so.
The Manhattan distance between two cells ($x_1$, $y_1$) and ($x_2$, $y_2$) is defined as $|x_1 - x_2| + |y_1 - y_2|$. For example, the Manhattan distance between the cells $(5, 2)$ and $(7, 1)$ equals to $|5-7|+|2-1|=3$.
Note that rhombic matrices are uniquely defined by $n$, $m$, and the coordinates of the cell containing the zero.
She drew a $n\times m$ rhombic matrix. She believes that you can not recreate the matrix if she gives you only the elements of this matrix in some arbitrary order (i.e., the sequence of $n\cdot m$ numbers). Note that Sonya will not give you $n$ and $m$, so only the sequence of numbers in this matrix will be at your disposal.
Write a program that finds such an $n\times m$ rhombic matrix whose elements are the same as the elements in the sequence in some order. | The first line contains a single integer $t$ ($1\leq t\leq 10^6$) — the number of cells in the matrix.
The second line contains $t$ integers $a_1, a_2, \ldots, a_t$ ($0\leq a_i< t$) — the values in the cells in arbitrary order. | In the first line, print two positive integers $n$ and $m$ ($n \times m = t$) — the size of the matrix.
In the second line, print two integers $x$ and $y$ ($1\leq x\leq n$, $1\leq y\leq m$) — the row number and the column number where the cell with $0$ is located.
If there are multiple possible answers, print any of them. If there is no solution, print the single integer $-1$. | [
"20\n1 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4\n",
"18\n2 2 3 2 4 3 3 3 0 2 4 2 1 3 2 1 1 1\n",
"6\n2 1 0 2 1 2\n"
] | [
"4 5\n2 2\n",
"3 6\n2 3\n",
"-1\n"
] | You can see the solution to the first example in the legend. You also can choose the cell $(2, 2)$ for the cell where $0$ is located. You also can choose a $5\times 4$ matrix with zero at $(4, 2)$.
In the second example, there is a $3\times 6$ matrix, where the zero is located at $(2, 3)$ there.
In the third example, a solution does not exist. | [
{
"input": "20\n1 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4",
"output": "4 5\n2 2"
},
{
"input": "18\n2 2 3 2 4 3 3 3 0 2 4 2 1 3 2 1 1 1",
"output": "3 6\n2 3"
},
{
"input": "6\n2 1 0 2 1 2",
"output": "-1"
},
{
"input": "1\n0",
"output": "1 1\n1 1"
},
{
"input": "7\... | 155 | 8,396,800 | -1 | 8,720 | |
128 | Games with Rectangle | [
"combinatorics",
"dp"
] | null | null | In this task Anna and Maria play the following game. Initially they have a checkered piece of paper with a painted *n*<=×<=*m* rectangle (only the border, no filling). Anna and Maria move in turns and Anna starts. During each move one should paint inside the last-painted rectangle a new lesser rectangle (along the grid lines). The new rectangle should have no common points with the previous one. Note that when we paint a rectangle, we always paint only the border, the rectangles aren't filled.
Nobody wins the game — Anna and Maria simply play until they have done *k* moves in total. Count the number of different ways to play this game. | The first and only line contains three integers: *n*,<=*m*,<=*k* (1<=≤<=*n*,<=*m*,<=*k*<=≤<=1000). | Print the single number — the number of the ways to play the game. As this number can be very big, print the value modulo 1000000007 (109<=+<=7). | [
"3 3 1\n",
"4 4 1\n",
"6 7 2\n"
] | [
"1\n",
"9\n",
"75\n"
] | Two ways to play the game are considered different if the final pictures are different. In other words, if one way contains a rectangle that is not contained in the other way.
In the first sample Anna, who performs her first and only move, has only one possible action plan — insert a 1 × 1 square inside the given 3 × 3 square.
In the second sample Anna has as much as 9 variants: 4 ways to paint a 1 × 1 square, 2 ways to insert a 1 × 2 rectangle vertically, 2 more ways to insert it horizontally and one more way is to insert a 2 × 2 square. | [
{
"input": "3 3 1",
"output": "1"
},
{
"input": "4 4 1",
"output": "9"
},
{
"input": "6 7 2",
"output": "75"
},
{
"input": "5 5 3",
"output": "0"
},
{
"input": "2 2 1",
"output": "0"
},
{
"input": "999 999 499",
"output": "1"
},
{
"input": ... | 482 | 114,176,000 | 3 | 8,750 | |
696 | Legen... | [
"data structures",
"dp",
"matrices",
"strings"
] | null | null | Barney was hanging out with Nora for a while and now he thinks he may have feelings for her. Barney wants to send her a cheesy text message and wants to make her as happy as possible.
Initially, happiness level of Nora is 0. Nora loves some pickup lines like "I'm falling for you" and stuff. Totally, she knows *n* pickup lines, each consisting only of lowercase English letters, also some of them may be equal (in writing, but different in pronouncing or meaning though). Every time Nora sees *i*-th pickup line as a consecutive subsequence of Barney's text message her happiness level increases by *a**i*. These substrings may overlap, for example, Nora will see the pickup line aa twice and the pickup line ab once in text message aaab.
Due to texting app limits, Barney's text may have up to *l* characters.
Barney asked you to help him make Nora as much happy as possible, it's gonna be legen... | The first line of input contains two integers *n* and *l* (1<=≤<=*n*<=≤<=200,<=1<=≤<=*l*<=≤<=1014) — the number of pickup lines and the maximum length of Barney's text.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=100), meaning that Nora's happiness level increases by *a**i* after every time seeing *i*-th pickup line.
The next *n* lines contain the pickup lines. *i*-th of them contains a single string *s**i* consisting of only English lowercase letter. Summary length of all pickup lines does not exceed 200.
All strings are not empty. | Print the only integer — the maximum possible value of Nora's happiness level after reading Barney's text. | [
"3 6\n3 2 1\nheart\nearth\nart\n",
"3 6\n3 2 8\nheart\nearth\nart\n"
] | [
"6\n",
"16\n"
] | An optimal answer for the first sample case is hearth containing each pickup line exactly once.
An optimal answer for the second sample case is artart. | [] | 30 | 0 | 0 | 8,805 | |
475 | Bayan Bus | [
"implementation"
] | null | null | The final round of Bayan Programming Contest will be held in Tehran, and the participants will be carried around with a yellow bus. The bus has 34 passenger seats: 4 seats in the last row and 3 seats in remaining rows.
The event coordinator has a list of *k* participants who should be picked up at the airport. When a participant gets on the bus, he will sit in the last row with an empty seat. If there is more than one empty seat in that row, he will take the leftmost one.
In order to keep track of the people who are on the bus, the event coordinator needs a figure showing which seats are going to be taken by *k* participants. Your task is to draw the figure representing occupied seats. | The only line of input contains integer *k*, (0<=≤<=*k*<=≤<=34), denoting the number of participants. | Print the figure of a bus with *k* passengers as described in sample tests. Character '#' denotes an empty seat, while 'O' denotes a taken seat. 'D' is the bus driver and other characters in the output are for the purpose of beautifying the figure. Strictly follow the sample test cases output format. Print exactly six lines. Do not output extra space or other characters. | [
"9\n",
"20\n"
] | [
"+------------------------+\n|O.O.O.#.#.#.#.#.#.#.#.|D|)\n|O.O.O.#.#.#.#.#.#.#.#.|.|\n|O.......................|\n|O.O.#.#.#.#.#.#.#.#.#.|.|)\n+------------------------+\n",
"+------------------------+\n|O.O.O.O.O.O.O.#.#.#.#.|D|)\n|O.O.O.O.O.O.#.#.#.#.#.|.|\n|O.......................|\n|O.O.O.O.O.O.#.#.#.#.#.|.|... | none | [
{
"input": "9",
"output": "+------------------------+\n|O.O.O.#.#.#.#.#.#.#.#.|D|)\n|O.O.O.#.#.#.#.#.#.#.#.|.|\n|O.......................|\n|O.O.#.#.#.#.#.#.#.#.#.|.|)\n+------------------------+"
},
{
"input": "20",
"output": "+------------------------+\n|O.O.O.O.O.O.O.#.#.#.#.|D|)\n|O.O.O.O.O.... | 62 | 0 | 3 | 8,816 | |
135 | Cycle | [
"brute force",
"dfs and similar",
"implementation"
] | null | null | Little Petya very much likes rectangular tables that consist of characters "0" and "1". Recently he has received one such table as a gift from his mother. The table contained *n* rows and *m* columns. The rows are numbered from top to bottom from 1 to *n*, the columns are numbered from the left to the right from 1 to *m*. Petya immediately decided to find the longest cool cycle whatever it takes.
A cycle is a sequence of pairwise distinct cells where each two consecutive cells have a common side; besides, the first cell has a common side with the last cell. A cycle is called cool if it fulfills all the following conditions simultaneously:
- The cycle entirely consists of the cells that contain "1". - Each cell that belongs to the cycle, has a common side with exactly two other cells that belong to the cycle. - Each cell of the table that contains "1" either belongs to the cycle or is positioned outside of it (see definition below).
To define the notion of "outside" formally, let's draw a cycle on a plane. Let each cell of the cycle (*i*,<=*j*) (*i* is the row number, *j* is the column number) correspond to the point (*i*,<=*j*) on the coordinate plane. Let a straight line segment join each pair of points that correspond to the cells belonging to the cycle and sharing a side. Thus, we will get a closed polyline that has no self-intersections and self-touches. The polyline divides the plane into two connected parts: the part of an infinite area and the part of a finite area. It is considered that cell (*r*,<=*c*) lies outside of the cycle if it does not belong to the cycle and the corresponding point on the plane with coordinates (*r*,<=*c*) lies in the part with the infinite area.
Help Petya to find the length of the longest cool cycle in the table. The cycle length is defined as the number of cells that belong to the cycle. | The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=1000) — the number of rows and columns in the table, respectively. Each of the following *n* lines contains *m* characters. Each character can be either "0" or "1". | Print a single number — the length of the longest cool cycle in the table. If such cycles do not exist, print 0. | [
"3 3\n111\n101\n111\n",
"5 5\n01010\n10101\n01010\n10101\n01010\n",
"7 7\n1111111\n1000101\n1000101\n1000101\n1000111\n1000001\n1111111\n",
"5 5\n11111\n10001\n10101\n10001\n11111\n"
] | [
"8\n",
"0\n",
"24\n",
"0\n"
] | In the first example there's only one cycle and it is cool.
In the second sample there's no cycle at all.
In the third sample there are two cool cycles: their lengths are 12 and 24.
In the fourth sample there also is only one cycle but it isn't cool as there's a cell containing "1" inside this cycle. | [] | 60 | 0 | 0 | 8,828 | |
645 | Enduring Exodus | [
"binary search",
"two pointers"
] | null | null | In an attempt to escape the Mischievous Mess Makers' antics, Farmer John has abandoned his farm and is traveling to the other side of Bovinia. During the journey, he and his *k* cows have decided to stay at the luxurious Grand Moo-dapest Hotel. The hotel consists of *n* rooms located in a row, some of which are occupied.
Farmer John wants to book a set of *k*<=+<=1 currently unoccupied rooms for him and his cows. He wants his cows to stay as safe as possible, so he wishes to minimize the maximum distance from his room to the room of his cow. The distance between rooms *i* and *j* is defined as |*j*<=-<=*i*|. Help Farmer John protect his cows by calculating this minimum possible distance. | The first line of the input contains two integers *n* and *k* (1<=≤<=*k*<=<<=*n*<=≤<=100<=000) — the number of rooms in the hotel and the number of cows travelling with Farmer John.
The second line contains a string of length *n* describing the rooms. The *i*-th character of the string will be '0' if the *i*-th room is free, and '1' if the *i*-th room is occupied. It is guaranteed that at least *k*<=+<=1 characters of this string are '0', so there exists at least one possible choice of *k*<=+<=1 rooms for Farmer John and his cows to stay in. | Print the minimum possible distance between Farmer John's room and his farthest cow. | [
"7 2\n0100100\n",
"5 1\n01010\n",
"3 2\n000\n"
] | [
"2\n",
"2\n",
"1\n"
] | In the first sample, Farmer John can book room 3 for himself, and rooms 1 and 4 for his cows. The distance to the farthest cow is 2. Note that it is impossible to make this distance 1, as there is no block of three consecutive unoccupied rooms.
In the second sample, Farmer John can book room 1 for himself and room 3 for his single cow. The distance between him and his cow is 2.
In the third sample, Farmer John books all three available rooms, taking the middle room for himself so that both cows are next to him. His distance from the farthest cow is 1. | [
{
"input": "7 2\n0100100",
"output": "2"
},
{
"input": "5 1\n01010",
"output": "2"
},
{
"input": "3 2\n000",
"output": "1"
},
{
"input": "10 1\n1101111101",
"output": "6"
},
{
"input": "2 1\n00",
"output": "1"
},
{
"input": "3 1\n010",
"output": "2... | 390 | 307,200 | 3 | 8,831 | |
32 | Constellation | [
"implementation"
] | D. Constellation | 2 | 256 | A star map in Berland is a checked field *n*<=×<=*m* squares. In each square there is or there is not a star. The favourite constellation of all Berland's astronomers is the constellation of the Cross. This constellation can be formed by any 5 stars so, that for some integer *x* (radius of the constellation) the following is true:
- the 2nd is on the same vertical line as the 1st, but *x* squares up - the 3rd is on the same vertical line as the 1st, but *x* squares down - the 4th is on the same horizontal line as the 1st, but *x* squares left - the 5th is on the same horizontal line as the 1st, but *x* squares right
Such constellations can be very numerous, that's why they are numbered with integers from 1 on the following principle: when two constellations are compared, the one with a smaller radius gets a smaller index; if their radii are equal — the one, whose central star if higher than the central star of the other one; if their central stars are at the same level — the one, whose central star is to the left of the central star of the other one.
Your task is to find the constellation with index *k* by the given Berland's star map. | The first line contains three integers *n*, *m* and *k* (1<=≤<=*n*,<=*m*<=≤<=300,<=1<=≤<=*k*<=≤<=3·107) — height and width of the map and index of the required constellation respectively. The upper-left corner has coordinates (1,<=1), and the lower-right — (*n*,<=*m*). Then there follow *n* lines, *m* characters each — description of the map. *j*-th character in *i*-th line is «*», if there is a star in the corresponding square, and «.» if this square is empty. | If the number of the constellations is less than *k*, output -1. Otherwise output 5 lines, two integers each — coordinates of the required constellation. Output the stars in the following order: central, upper, lower, left, right. | [
"5 6 1\n....*.\n...***\n....*.\n..*...\n.***..\n",
"5 6 2\n....*.\n...***\n....*.\n..*...\n.***..\n",
"7 7 2\n...*...\n.......\n...*...\n*.***.*\n...*...\n.......\n...*...\n"
] | [
"2 5\n1 5\n3 5\n2 4\n2 6\n",
"-1\n",
"4 4\n1 4\n7 4\n4 1\n4 7\n"
] | none | [
{
"input": "5 6 1\n....*.\n...***\n....*.\n..*...\n.***..",
"output": "2 5\n1 5\n3 5\n2 4\n2 6"
},
{
"input": "5 6 2\n....*.\n...***\n....*.\n..*...\n.***..",
"output": "-1"
},
{
"input": "5 5 1\n.....\n.....\n.*..*\n*.*..\n....*",
"output": "-1"
},
{
"input": "5 5 3\n*.***\n... | 2,000 | 95,027,200 | 0 | 8,935 |
0 | none | [
"none"
] | null | null | There are *n* points on a straight line, and the *i*-th point among them is located at *x**i*. All these coordinates are distinct.
Determine the number *m* — the smallest number of points you should add on the line to make the distances between all neighboring points equal. | The first line contains a single integer *n* (3<=≤<=*n*<=≤<=100<=000) — the number of points.
The second line contains a sequence of integers *x*1,<=*x*2,<=...,<=*x**n* (<=-<=109<=≤<=*x**i*<=≤<=109) — the coordinates of the points. All these coordinates are distinct. The points can be given in an arbitrary order. | Print a single integer *m* — the smallest number of points you should add on the line to make the distances between all neighboring points equal. | [
"3\n-5 10 5\n",
"6\n100 200 400 300 600 500\n",
"4\n10 9 0 -1\n"
] | [
"1\n",
"0\n",
"8\n"
] | In the first example you can add one point with coordinate 0.
In the second example the distances between all neighboring points are already equal, so you shouldn't add anything. | [
{
"input": "3\n-5 10 5",
"output": "1"
},
{
"input": "6\n100 200 400 300 600 500",
"output": "0"
},
{
"input": "4\n10 9 0 -1",
"output": "8"
},
{
"input": "3\n1 4 7",
"output": "0"
},
{
"input": "3\n1 4 6",
"output": "3"
},
{
"input": "3\n1 2 6",
"... | 62 | 7,065,600 | 0 | 8,938 | |
86 | Powerful array | [
"data structures",
"implementation",
"math",
"two pointers"
] | D. Powerful array | 5 | 256 | An array of positive integers *a*1,<=*a*2,<=...,<=*a**n* is given. Let us consider its arbitrary subarray *a**l*,<=*a**l*<=+<=1...,<=*a**r*, where 1<=≤<=*l*<=≤<=*r*<=≤<=*n*. For every positive integer *s* denote by *K**s* the number of occurrences of *s* into the subarray. We call the power of the subarray the sum of products *K**s*·*K**s*·*s* for every positive integer *s*. The sum contains only finite number of nonzero summands as the number of different values in the array is indeed finite.
You should calculate the power of *t* given subarrays. | First line contains two integers *n* and *t* (1<=≤<=*n*,<=*t*<=≤<=200000) — the array length and the number of queries correspondingly.
Second line contains *n* positive integers *a**i* (1<=≤<=*a**i*<=≤<=106) — the elements of the array.
Next *t* lines contain two positive integers *l*, *r* (1<=≤<=*l*<=≤<=*r*<=≤<=*n*) each — the indices of the left and the right ends of the corresponding subarray. | Output *t* lines, the *i*-th line of the output should contain single positive integer — the power of the *i*-th query subarray.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preferred to use cout stream (also you may use %I64d). | [
"3 2\n1 2 1\n1 2\n1 3\n",
"8 3\n1 1 2 2 1 3 1 1\n2 7\n1 6\n2 7\n"
] | [
"3\n6\n",
"20\n20\n20\n"
] | Consider the following array (see the second sample) and its [2, 7] subarray (elements of the subarray are colored): | [
{
"input": "3 2\n1 2 1\n1 2\n1 3",
"output": "3\n6"
},
{
"input": "8 3\n1 1 2 2 1 3 1 1\n2 7\n1 6\n2 7",
"output": "20\n20\n20"
},
{
"input": "20 8\n1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2\n4 15\n1 2\n2 20\n7 7\n13 18\n7 7\n3 19\n3 8",
"output": "108\n3\n281\n1\n27\n1\n209\n27"
},
... | 186 | 0 | -1 | 8,995 |
915 | Almost Acyclic Graph | [
"dfs and similar",
"graphs"
] | null | null | You are given a [directed graph](https://en.wikipedia.org/wiki/Directed_graph) consisting of *n* vertices and *m* edges (each edge is directed, so it can be traversed in only one direction). You are allowed to remove at most one edge from it.
Can you make this graph [acyclic](https://en.wikipedia.org/wiki/Directed_acyclic_graph) by removing at most one edge from it? A directed graph is called acyclic iff it doesn't contain any cycle (a non-empty path that starts and ends in the same vertex). | The first line contains two integers *n* and *m* (2<=≤<=*n*<=≤<=500, 1<=≤<=*m*<=≤<=*min*(*n*(*n*<=-<=1),<=100000)) — the number of vertices and the number of edges, respectively.
Then *m* lines follow. Each line contains two integers *u* and *v* denoting a directed edge going from vertex *u* to vertex *v* (1<=≤<=*u*,<=*v*<=≤<=*n*, *u*<=≠<=*v*). Each ordered pair (*u*,<=*v*) is listed at most once (there is at most one directed edge from *u* to *v*). | If it is possible to make this graph acyclic by removing at most one edge, print YES. Otherwise, print NO. | [
"3 4\n1 2\n2 3\n3 2\n3 1\n",
"5 6\n1 2\n2 3\n3 2\n3 1\n2 1\n4 5\n"
] | [
"YES\n",
"NO\n"
] | In the first example you can remove edge <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/29f71c065c3536e88b54429c734103ad3604f68b.png" style="max-width: 100.0%;max-height: 100.0%;"/>, and the graph becomes acyclic.
In the second example you have to remove at least two edges (for example, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/420322fe5fba4eb3e3eba6886a2edb31f15762ce.png" style="max-width: 100.0%;max-height: 100.0%;"/> and <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/29f71c065c3536e88b54429c734103ad3604f68b.png" style="max-width: 100.0%;max-height: 100.0%;"/>) in order to make the graph acyclic. | [
{
"input": "3 4\n1 2\n2 3\n3 2\n3 1",
"output": "YES"
},
{
"input": "5 6\n1 2\n2 3\n3 2\n3 1\n2 1\n4 5",
"output": "NO"
},
{
"input": "2 2\n1 2\n2 1",
"output": "YES"
},
{
"input": "7 7\n1 3\n3 6\n3 7\n5 3\n6 2\n6 7\n7 2",
"output": "YES"
},
{
"input": "500 50\n39... | 1,000 | 21,299,200 | 0 | 9,002 | |
691 | Exponential notation | [
"implementation",
"strings"
] | null | null | You are given a positive decimal number *x*.
Your task is to convert it to the "simple exponential notation".
Let *x*<==<=*a*·10*b*, where 1<=≤<=*a*<=<<=10, then in general case the "simple exponential notation" looks like "aEb". If *b* equals to zero, the part "Eb" should be skipped. If *a* is an integer, it should be written without decimal point. Also there should not be extra zeroes in *a* and *b*. | The only line contains the positive decimal number *x*. The length of the line will not exceed 106. Note that you are given too large number, so you can't use standard built-in data types "float", "double" and other. | Print the only line — the "simple exponential notation" of the given number *x*. | [
"16\n",
"01.23400\n",
".100\n",
"100.\n"
] | [
"1.6E1\n",
"1.234\n",
"1E-1\n",
"1E2\n"
] | none | [
{
"input": "16",
"output": "1.6E1"
},
{
"input": "01.23400",
"output": "1.234"
},
{
"input": ".100",
"output": "1E-1"
},
{
"input": "100.",
"output": "1E2"
},
{
"input": "9000",
"output": "9E3"
},
{
"input": "0.0012",
"output": "1.2E-3"
},
{
... | 312 | 10,752,000 | 3 | 9,005 | |
799 | T-shirt buying | [
"data structures",
"implementation"
] | null | null | A new pack of *n* t-shirts came to a shop. Each of the t-shirts is characterized by three integers *p**i*, *a**i* and *b**i*, where *p**i* is the price of the *i*-th t-shirt, *a**i* is front color of the *i*-th t-shirt and *b**i* is back color of the *i*-th t-shirt. All values *p**i* are distinct, and values *a**i* and *b**i* are integers from 1 to 3.
*m* buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the *j*-th buyer we know his favorite color *c**j*.
A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.
You are to compute the prices each buyer will pay for t-shirts. | The first line contains single integer *n* (1<=≤<=*n*<=≤<=200<=000) — the number of t-shirts.
The following line contains sequence of integers *p*1,<=*p*2,<=...,<=*p**n* (1<=≤<=*p**i*<=≤<=1<=000<=000<=000), where *p**i* equals to the price of the *i*-th t-shirt.
The following line contains sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=3), where *a**i* equals to the front color of the *i*-th t-shirt.
The following line contains sequence of integers *b*1,<=*b*2,<=...,<=*b**n* (1<=≤<=*b**i*<=≤<=3), where *b**i* equals to the back color of the *i*-th t-shirt.
The next line contains single integer *m* (1<=≤<=*m*<=≤<=200<=000) — the number of buyers.
The following line contains sequence *c*1,<=*c*2,<=...,<=*c**m* (1<=≤<=*c**j*<=≤<=3), where *c**j* equals to the favorite color of the *j*-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served. | Print to the first line *m* integers — the *j*-th integer should be equal to the price of the t-shirt which the *j*-th buyer will buy. If the *j*-th buyer won't buy anything, print -1. | [
"5\n300 200 400 500 911\n1 2 1 2 3\n2 1 3 2 1\n6\n2 3 1 2 1 1\n",
"2\n1000000000 1\n1 1\n1 2\n2\n2 1\n"
] | [
"200 400 300 500 911 -1 \n",
"1 1000000000 \n"
] | none | [
{
"input": "5\n300 200 400 500 911\n1 2 1 2 3\n2 1 3 2 1\n6\n2 3 1 2 1 1",
"output": "200 400 300 500 911 -1 "
},
{
"input": "2\n1000000000 1\n1 1\n1 2\n2\n2 1",
"output": "1 1000000000 "
},
{
"input": "10\n251034796 163562337 995167403 531046374 341924810 828969071 971837553 183763940 8... | 3,000 | 24,576,000 | 0 | 9,041 | |
837 | Vasya's Function | [
"binary search",
"implementation",
"math"
] | null | null | Vasya is studying number theory. He has denoted a function *f*(*a*,<=*b*) such that:
- *f*(*a*,<=0)<==<=0; - *f*(*a*,<=*b*)<==<=1<=+<=*f*(*a*,<=*b*<=-<=*gcd*(*a*,<=*b*)), where *gcd*(*a*,<=*b*) is the greatest common divisor of *a* and *b*.
Vasya has two numbers *x* and *y*, and he wants to calculate *f*(*x*,<=*y*). He tried to do it by himself, but found out that calculating this function the way he wants to do that might take very long time. So he decided to ask you to implement a program that will calculate this function swiftly. | The first line contains two integer numbers *x* and *y* (1<=≤<=*x*,<=*y*<=≤<=1012). | Print *f*(*x*,<=*y*). | [
"3 5\n",
"6 3\n"
] | [
"3\n",
"1\n"
] | none | [
{
"input": "3 5",
"output": "3"
},
{
"input": "6 3",
"output": "1"
},
{
"input": "1000000009 1000000008",
"output": "1000000008"
},
{
"input": "1000000007 1000000006",
"output": "1000000006"
},
{
"input": "2000000018 2000000017",
"output": "1000000009"
},
... | 77 | 5,734,400 | 0 | 9,043 | |
0 | none | [
"none"
] | null | null | Alyona has a tree with *n* vertices. The root of the tree is the vertex 1. In each vertex Alyona wrote an positive integer, in the vertex *i* she wrote *a**i*. Moreover, the girl wrote a positive integer to every edge of the tree (possibly, different integers on different edges).
Let's define *dist*(*v*,<=*u*) as the sum of the integers written on the edges of the simple path from *v* to *u*.
The vertex *v* controls the vertex *u* (*v*<=≠<=*u*) if and only if *u* is in the subtree of *v* and *dist*(*v*,<=*u*)<=≤<=*a**u*.
Alyona wants to settle in some vertex. In order to do this, she wants to know for each vertex *v* what is the number of vertices *u* such that *v* controls *u*. | The first line contains single integer *n* (1<=≤<=*n*<=≤<=2·105).
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109) — the integers written in the vertices.
The next (*n*<=-<=1) lines contain two integers each. The *i*-th of these lines contains integers *p**i* and *w**i* (1<=≤<=*p**i*<=≤<=*n*, 1<=≤<=*w**i*<=≤<=109) — the parent of the (*i*<=+<=1)-th vertex in the tree and the number written on the edge between *p**i* and (*i*<=+<=1).
It is guaranteed that the given graph is a tree. | Print *n* integers — the *i*-th of these numbers should be equal to the number of vertices that the *i*-th vertex controls. | [
"5\n2 5 1 4 6\n1 7\n1 1\n3 5\n3 6\n",
"5\n9 7 8 6 5\n1 1\n2 1\n3 1\n4 1\n"
] | [
"1 0 1 0 0\n",
"4 3 2 1 0\n"
] | In the example test case the vertex 1 controls the vertex 3, the vertex 3 controls the vertex 5 (note that is doesn't mean the vertex 1 controls the vertex 5). | [
{
"input": "5\n2 5 1 4 6\n1 7\n1 1\n3 5\n3 6",
"output": "1 0 1 0 0"
},
{
"input": "5\n9 7 8 6 5\n1 1\n2 1\n3 1\n4 1",
"output": "4 3 2 1 0"
},
{
"input": "1\n1",
"output": "0"
},
{
"input": "2\n1 1\n1 1",
"output": "1 0"
},
{
"input": "10\n40 77 65 14 86 16 2 51 ... | 46 | 0 | -1 | 9,048 | |
316 | Special Task | [
"math"
] | null | null | Special Agent Smart Beaver works in a secret research department of ABBYY. He's been working there for a long time and is satisfied with his job, as it allows him to eat out in the best restaurants and order the most expensive and exotic wood types there.
The content special agent has got an important task: to get the latest research by British scientists on the English Language. These developments are encoded and stored in a large safe. The Beaver's teeth are strong enough, so the authorities assured that upon arriving at the place the beaver won't have any problems with opening the safe.
And he finishes his aspen sprig and leaves for this important task. Of course, the Beaver arrived at the location without any problems, but alas. He can't open the safe with his strong and big teeth. At this point, the Smart Beaver get a call from the headquarters and learns that opening the safe with the teeth is not necessary, as a reliable source has sent the following information: the safe code consists of digits and has no leading zeroes. There also is a special hint, which can be used to open the safe. The hint is string *s* with the following structure:
- if *s**i* = "?", then the digit that goes *i*-th in the safe code can be anything (between 0 to 9, inclusively); - if *s**i* is a digit (between 0 to 9, inclusively), then it means that there is digit *s**i* on position *i* in code; - if the string contains letters from "A" to "J", then all positions with the same letters must contain the same digits and the positions with distinct letters must contain distinct digits. - The length of the safe code coincides with the length of the hint.
For example, hint "?JGJ9" has such matching safe code variants: "51919", "55959", "12329", "93539" and so on, and has wrong variants such as: "56669", "00111", "03539" and "13666".
After receiving such information, the authorities change the plan and ask the special agents to work quietly and gently and not to try to open the safe by mechanical means, and try to find the password using the given hint.
At a special agent school the Smart Beaver was the fastest in his platoon finding codes for such safes, but now he is not in that shape: the years take their toll ... Help him to determine the number of possible variants of the code to the safe, matching the given hint. After receiving this information, and knowing his own speed of entering codes, the Smart Beaver will be able to determine whether he will have time for tonight's show "Beavers are on the trail" on his favorite TV channel, or he should work for a sleepless night... | The first line contains string *s* — the hint to the safe code. String *s* consists of the following characters: ?, 0-9, A-J. It is guaranteed that the first character of string *s* doesn't equal to character 0.
The input limits for scoring 30 points are (subproblem A1):
- 1<=≤<=|*s*|<=≤<=5.
The input limits for scoring 100 points are (subproblems A1+A2):
- 1<=≤<=|*s*|<=≤<=105.
Here |*s*| means the length of string *s*. | Print the number of codes that match the given hint. | [
"AJ\n",
"1?AA\n"
] | [
"81\n",
"100\n"
] | none | [
{
"input": "AJ",
"output": "81"
},
{
"input": "1?AA",
"output": "100"
},
{
"input": "?",
"output": "9"
},
{
"input": "7",
"output": "1"
},
{
"input": "A",
"output": "9"
},
{
"input": "BBB?",
"output": "90"
},
{
"input": "BC??",
"output"... | 372 | 819,200 | -1 | 9,049 | |
923 | Perfect Security | [
"data structures",
"greedy",
"strings",
"trees"
] | null | null | Alice has a very important message *M* consisting of some non-negative integers that she wants to keep secret from Eve. Alice knows that the only theoretically secure cipher is one-time pad. Alice generates a random key *K* of the length equal to the message's length. Alice computes the bitwise xor of each element of the message and the key (, where denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)) and stores this encrypted message *A*. Alice is smart. Be like Alice.
For example, Alice may have wanted to store a message *M*<==<=(0,<=15,<=9,<=18). She generated a key *K*<==<=(16,<=7,<=6,<=3). The encrypted message is thus *A*<==<=(16,<=8,<=15,<=17).
Alice realised that she cannot store the key with the encrypted message. Alice sent her key *K* to Bob and deleted her own copy. Alice is smart. Really, be like Alice.
Bob realised that the encrypted message is only secure as long as the key is secret. Bob thus randomly permuted the key before storing it. Bob thinks that this way, even if Eve gets both the encrypted message and the key, she will not be able to read the message. Bob is not smart. Don't be like Bob.
In the above example, Bob may have, for instance, selected a permutation (3,<=4,<=1,<=2) and stored the permuted key *P*<==<=(6,<=3,<=16,<=7).
One year has passed and Alice wants to decrypt her message. Only now Bob has realised that this is impossible. As he has permuted the key randomly, the message is lost forever. Did we mention that Bob isn't smart?
Bob wants to salvage at least some information from the message. Since he is not so smart, he asks for your help. You know the encrypted message *A* and the permuted key *P*. What is the lexicographically smallest message that could have resulted in the given encrypted text?
More precisely, for given *A* and *P*, find the lexicographically smallest message *O*, for which there exists a permutation π such that for every *i*.
Note that the sequence *S* is lexicographically smaller than the sequence *T*, if there is an index *i* such that *S**i*<=<<=*T**i* and for all *j*<=<<=*i* the condition *S**j*<==<=*T**j* holds. | The first line contains a single integer *N* (1<=≤<=*N*<=≤<=300000), the length of the message.
The second line contains *N* integers *A*1,<=*A*2,<=...,<=*A**N* (0<=≤<=*A**i*<=<<=230) representing the encrypted message.
The third line contains *N* integers *P*1,<=*P*2,<=...,<=*P**N* (0<=≤<=*P**i*<=<<=230) representing the permuted encryption key. | Output a single line with *N* integers, the lexicographically smallest possible message *O*. Note that all its elements should be non-negative. | [
"3\n8 4 13\n17 2 7\n",
"5\n12 7 87 22 11\n18 39 9 12 16\n",
"10\n331415699 278745619 998190004 423175621 42983144 166555524 843586353 802130100 337889448 685310951\n226011312 266003835 342809544 504667531 529814910 684873393 817026985 844010788 993949858 1031395667\n"
] | [
"10 3 28\n",
"0 14 69 6 44\n",
"128965467 243912600 4281110 112029883 223689619 76924724 429589 119397893 613490433 362863284\n"
] | In the first case, the solution is (10, 3, 28), since <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/a896b30a69636d1bfbfa981eae10650f5fee843c.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e383e4333ea37c4652ce2ac1ccfc2cfcf96e0896.png" style="max-width: 100.0%;max-height: 100.0%;"/> and <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c24ed3c6f88805eb3710487b3fe07ff64034151a.png" style="max-width: 100.0%;max-height: 100.0%;"/>. Other possible permutations of key yield messages (25, 6, 10), (25, 3, 15), (10, 21, 10), (15, 21, 15) and (15, 6, 28), which are all lexicographically larger than the solution. | [
{
"input": "3\n8 4 13\n17 2 7",
"output": "10 3 28"
},
{
"input": "5\n12 7 87 22 11\n18 39 9 12 16",
"output": "0 14 69 6 44"
},
{
"input": "10\n331415699 278745619 998190004 423175621 42983144 166555524 843586353 802130100 337889448 685310951\n226011312 266003835 342809544 504667531 529... | 3,500 | 161,996,800 | 0 | 9,058 | |
383 | Vowels | [
"combinatorics",
"divide and conquer",
"dp"
] | null | null | Iahubina is tired of so many complicated languages, so she decided to invent a new, simple language. She already made a dictionary consisting of *n* 3-words. A 3-word is a sequence of exactly 3 lowercase letters of the first 24 letters of the English alphabet (*a* to *x*). She decided that some of the letters are vowels, and all the others are consonants. The whole language is based on a simple rule: any word that contains at least one vowel is correct.
Iahubina forgot which letters are the vowels, and wants to find some possible correct sets of vowels. She asks Iahub questions. In each question, she will give Iahub a set of letters considered vowels (in this question). For each question she wants to know how many words of the dictionary are correct, considering the given set of vowels.
Iahubina wants to know the *xor* of the squared answers to all the possible questions. There are 224 different questions, they are all subsets of the set of the first 24 letters of the English alphabet. Help Iahub find that number. | The first line contains one integer, *n* (1<=≤<=*n*<=≤<=104). Each of the next *n* lines contains a 3-word consisting of 3 lowercase letters. There will be no two identical 3-words. | Print one number, the *xor* of the squared answers to the queries. | [
"5\nabc\naaa\nada\nbcd\ndef\n"
] | [
"0\n"
] | none | [
{
"input": "5\nabc\naaa\nada\nbcd\ndef",
"output": "0"
},
{
"input": "100\namd\namj\natr\nbcp\nbjm\ncna\ncpj\ncse\ndij\ndjp\ndlv\nebk\nedf\nelw\nfbr\nfcl\nfhs\nflo\nfmj\ngcg\ngen\nghg\ngvb\ngxx\nhbe\nhbf\nhgu\nhlv\nhqa\nibg\nifp\nima\nitt\nivl\nixu\njle\njli\nket\nkit\nkws\nlep\nles\nleu\nmbp\nmci\n... | 30 | 0 | 0 | 9,117 | |
342 | Xenia and Spies | [
"brute force",
"greedy",
"implementation"
] | null | null | Xenia the vigorous detective faced *n* (*n*<=≥<=2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to *n* from left to right.
Spy *s* has an important note. He has to pass the note to spy *f*. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is *x*, he can pass the note to another spy, either *x*<=-<=1 or *x*<=+<=1 (if *x*<==<=1 or *x*<==<=*n*, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During *m* steps Xenia watches some spies attentively. Specifically, during step *t**i* (steps are numbered from 1) Xenia watches spies numbers *l**i*,<=*l**i*<=+<=1,<=*l**i*<=+<=2,<=...,<=*r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got *s* and *f*. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy *s* to spy *f* as quickly as possible (in the minimum number of steps). | The first line contains four integers *n*, *m*, *s* and *f* (1<=≤<=*n*,<=*m*<=≤<=105; 1<=≤<=*s*,<=*f*<=≤<=*n*; *s*<=≠<=*f*; *n*<=≥<=2). Each of the following *m* lines contains three integers *t**i*,<=*l**i*,<=*r**i* (1<=≤<=*t**i*<=≤<=109,<=1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*). It is guaranteed that *t*1<=<<=*t*2<=<<=*t*3<=<<=...<=<<=*t**m*. | Print *k* characters in a line: the *i*-th character in the line must represent the spies' actions on step *i*. If on step *i* the spy with the note must pass the note to the spy with a lesser number, the *i*-th character should equal "L". If on step *i* the spy with the note must pass it to the spy with a larger number, the *i*-th character must equal "R". If the spy must keep the note at the *i*-th step, the *i*-th character must equal "X".
As a result of applying the printed sequence of actions spy *s* must pass the note to spy *f*. The number of printed characters *k* must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. | [
"3 5 1 3\n1 1 2\n2 2 3\n3 3 3\n4 1 1\n10 1 3\n"
] | [
"XXRR\n"
] | none | [
{
"input": "3 5 1 3\n1 1 2\n2 2 3\n3 3 3\n4 1 1\n10 1 3",
"output": "XXRR"
},
{
"input": "2 3 2 1\n1 1 2\n2 1 2\n4 1 2",
"output": "XXL"
},
{
"input": "5 11 1 5\n1 1 5\n2 2 2\n3 1 1\n4 3 3\n5 3 3\n6 1 1\n7 4 4\n8 4 5\n10 1 3\n11 5 5\n13 1 5",
"output": "XXXRXRXXRR"
},
{
"inpu... | 62 | 0 | 0 | 9,124 | |
958 | Death Stars (medium) | [
"hashing",
"strings"
] | null | null | The stardate is 1983, and Princess Heidi is getting better at detecting the Death Stars. This time, two Rebel spies have yet again given Heidi two maps with the possible locations of the Death Star. Since she got rid of all double agents last time, she knows that both maps are correct, and indeed show the map of the solar system that contains the Death Star. However, this time the Empire has hidden the Death Star very well, and Heidi needs to find a place that appears on both maps in order to detect the Death Star.
The first map is an *N*<=×<=*M* grid, each cell of which shows some type of cosmic object that is present in the corresponding quadrant of space. The second map is an *M*<=×<=*N* grid. Heidi needs to align those two maps in such a way that they overlap over some *M*<=×<=*M* section in which all cosmic objects are identical. Help Heidi by identifying where such an *M*<=×<=*M* section lies within both maps. | The first line of the input contains two space-separated integers *N* and *M* (1<=≤<=*N*<=≤<=2000, 1<=≤<=*M*<=≤<=200, *M*<=≤<=*N*). The next *N* lines each contain *M* lower-case Latin characters (a-z), denoting the first map. Different characters correspond to different cosmic object types. The next *M* lines each contain *N* characters, describing the second map in the same format. | The only line of the output should contain two space-separated integers *i* and *j*, denoting that the section of size *M*<=×<=*M* in the first map that starts at the *i*-th row is equal to the section of the second map that starts at the *j*-th column. Rows and columns are numbered starting from 1.
If there are several possible ways to align the maps, Heidi will be satisfied with any of those. It is guaranteed that a solution exists. | [
"10 5\nsomer\nandom\nnoise\nmayth\neforc\nebewi\nthyou\nhctwo\nagain\nnoise\nsomermayth\nandomeforc\nnoiseebewi\nagainthyou\nnoisehctwo\n"
] | [
"4 6\n"
] | The 5-by-5 grid for the first test case looks like this: | [
{
"input": "10 5\nsomer\nandom\nnoise\nmayth\neforc\nebewi\nthyou\nhctwo\nagain\nnoise\nsomermayth\nandomeforc\nnoiseebewi\nagainthyou\nnoisehctwo",
"output": "4 6"
},
{
"input": "1 1\ng\ng",
"output": "1 1"
}
] | 1,715 | 154,214,400 | 3 | 9,141 | |
0 | none | [
"none"
] | null | null | Santa Claus likes palindromes very much. There was his birthday recently. *k* of his friends came to him to congratulate him, and each of them presented to him a string *s**i* having the same length *n*. We denote the beauty of the *i*-th string by *a**i*. It can happen that *a**i* is negative — that means that Santa doesn't find this string beautiful at all.
Santa Claus is crazy about palindromes. He is thinking about the following question: what is the maximum possible total beauty of a palindrome which can be obtained by concatenating some (possibly all) of the strings he has? Each present can be used at most once. Note that all strings have the same length *n*.
Recall that a palindrome is a string that doesn't change after one reverses it.
Since the empty string is a palindrome too, the answer can't be negative. Even if all *a**i*'s are negative, Santa can obtain the empty string. | The first line contains two positive integers *k* and *n* divided by space and denoting the number of Santa friends and the length of every string they've presented, respectively (1<=≤<=*k*,<=*n*<=≤<=100<=000; *n*·*k* <=≤<=100<=000).
*k* lines follow. The *i*-th of them contains the string *s**i* and its beauty *a**i* (<=-<=10<=000<=≤<=*a**i*<=≤<=10<=000). The string consists of *n* lowercase English letters, and its beauty is integer. Some of strings may coincide. Also, equal strings can have different beauties. | In the only line print the required maximum possible beauty. | [
"7 3\nabb 2\naaa -3\nbba -1\nzyz -4\nabb 5\naaa 7\nxyx 4\n",
"3 1\na 1\na 2\na 3\n",
"2 5\nabcde 10000\nabcde 10000\n"
] | [
"12\n",
"6\n",
"0\n"
] | In the first example Santa can obtain abbaaaxyxaaabba by concatenating strings 5, 2, 7, 6 and 3 (in this order). | [
{
"input": "7 3\nabb 2\naaa -3\nbba -1\nzyz -4\nabb 5\naaa 7\nxyx 4",
"output": "12"
},
{
"input": "3 1\na 1\na 2\na 3",
"output": "6"
},
{
"input": "2 5\nabcde 10000\nabcde 10000",
"output": "0"
},
{
"input": "10 10\nnjxbzflaka -1\nfelbvvtkja 6\ngxiuztqkcw 5\naomvscmtti 6\nj... | 46 | 0 | -1 | 9,146 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.