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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
301 | Yaroslav and Time | [
"binary search",
"graphs",
"shortest paths"
] | null | null | Yaroslav is playing a game called "Time". The game has a timer showing the lifespan he's got left. As soon as the timer shows 0, Yaroslav's character dies and the game ends. Also, the game has *n* clock stations, station number *i* is at point (*x**i*,<=*y**i*) of the plane. As the player visits station number *i*, he increases the current time on his timer by *a**i*. The stations are for one-time use only, so if the player visits some station another time, the time on his timer won't grow.
A player spends *d*Β·*dist* time units to move between stations, where *dist* is the distance the player has covered and *d* is some constant. The distance between stations *i* and *j* is determined as |*x**i*<=-<=*x**j*|<=+<=|*y**i*<=-<=*y**j*|.
Initially, the player is at station number 1, and the player has strictly more than zero and strictly less than one units of time. At station number 1 one unit of money can increase the time on the timer by one time unit (you can buy only integer number of time units).
Now Yaroslav is wondering, how much money he needs to get to station *n*. Help Yaroslav. Consider the time to buy and to increase the timer value negligibly small. | The first line contains integers *n* and *d* (3<=β€<=*n*<=β€<=100,<=103<=β€<=*d*<=β€<=105) β the number of stations and the constant from the statement.
The second line contains *n*<=-<=2 integers: *a*2,<=*a*3,<=...,<=*a**n*<=-<=1 (1<=β€<=*a**i*<=β€<=103). The next *n* lines contain the coordinates of the stations. The *i*-th of them contains two integers *x**i*, *y**i* (-100<=β€<=*x**i*,<=*y**i*<=β€<=100).
It is guaranteed that no two stations are located at the same point. | In a single line print an integer β the answer to the problem. | [
"3 1000\n1000\n0 0\n0 1\n0 3\n",
"3 1000\n1000\n1 0\n1 1\n1 2\n"
] | [
"2000\n",
"1000\n"
] | none | [
{
"input": "3 1000\n1000\n0 0\n0 1\n0 3",
"output": "2000"
},
{
"input": "3 1000\n1000\n1 0\n1 1\n1 2",
"output": "1000"
},
{
"input": "5 1421\n896 448 727\n-19 -40\n-87 40\n69 51\n-55 61\n-7 67",
"output": "169099"
},
{
"input": "6 1000\n142 712 254 869\n7 0\n95 38\n96 -20\n... | 248 | 307,200 | 3 | 32,456 | |
960 | Pathwalks | [
"data structures",
"dp",
"graphs"
] | null | null | You are given a directed graph with *n* nodes and *m* edges, with all edges having a certain weight.
There might be multiple edges and self loops, and the graph can also be disconnected.
You need to choose a path (possibly passing through same vertices multiple times) in the graph such that the weights of the edges are in strictly increasing order, and these edges come in the order of input. Among all such paths, you need to find the the path that has the maximum possible number of edges, and report this value.
Please note that the edges picked don't have to be consecutive in the input. | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=100000,1<=β€<=*m*<=β€<=100000)Β β the number of vertices and edges in the graph, respectively.
*m* lines follows.
The *i*-th of these lines contains three space separated integers *a**i*, *b**i* and *w**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*, 0<=β€<=*w**i*<=β€<=100000), denoting an edge from vertex *a**i* to vertex *b**i* having weight *w**i* | Print one integer in a single line β the maximum number of edges in the path. | [
"3 3\n3 1 3\n1 2 1\n2 3 2\n",
"5 5\n1 3 2\n3 2 3\n3 4 5\n5 4 0\n4 5 8\n"
] | [
"2",
"3"
] | The answer for the first sample input is 2: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/609340f155794c4e9eebcd9cdfa23c73cf982f28.png" style="max-width: 100.0%;max-height: 100.0%;"/>. Note that you cannot traverse <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/9b1d1f66686c43090329870c208942499764a73b.png" style="max-width: 100.0%;max-height: 100.0%;"/> because edge <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/030fc9181b578c2d906254d38dc56da5554323eb.png" style="max-width: 100.0%;max-height: 100.0%;"/> appears earlier in the input than the other two edges and hence cannot be picked/traversed after either of the other two edges.
In the second sample, it's optimal to pick 1-st, 3-rd and 5-th edges to get the optimal answer: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/bd608e0d11c1e7d39405f62afde4e5a2d18cecf6.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [
{
"input": "3 3\n3 1 3\n1 2 1\n2 3 2",
"output": "2"
},
{
"input": "5 5\n1 3 2\n3 2 3\n3 4 5\n5 4 0\n4 5 8",
"output": "3"
},
{
"input": "5 10\n3 4 8366\n5 1 6059\n2 1 72369\n2 2 35472\n5 3 50268\n2 4 98054\n5 1 26220\n2 3 24841\n1 3 42450\n3 1 59590",
"output": "3"
},
{
"inp... | 1,000 | 10,956,800 | 0 | 32,501 | |
0 | none | [
"none"
] | null | null | Little Chris is a huge fan of linear algebra. This time he has been given a homework about the unusual square of a square matrix.
The dot product of two integer number vectors *x* and *y* of size *n* is the sum of the products of the corresponding components of the vectors. The unusual square of an *n*<=Γ<=*n* square matrix *A* is defined as the sum of *n* dot products. The *i*-th of them is the dot product of the *i*-th row vector and the *i*-th column vector in the matrix *A*.
Fortunately for Chris, he has to work only in *GF*(2)! This means that all operations (addition, multiplication) are calculated modulo 2. In fact, the matrix *A* is binary: each element of *A* is either 0 or 1. For example, consider the following matrix *A*:
The unusual square of *A* is equal to (1Β·1<=+<=1Β·0<=+<=1Β·1)<=+<=(0Β·1<=+<=1Β·1<=+<=1Β·0)<=+<=(1Β·1<=+<=0Β·1<=+<=0Β·0)<==<=0<=+<=1<=+<=1<==<=0.
However, there is much more to the homework. Chris has to process *q* queries; each query can be one of the following:
1. given a row index *i*, flip all the values in the *i*-th row in *A*; 1. given a column index *i*, flip all the values in the *i*-th column in *A*; 1. find the unusual square of *A*.
To flip a bit value *w* means to change it to 1<=-<=*w*, i.e., 1 changes to 0 and 0 changes to 1.
Given the initial matrix *A*, output the answers for each query of the third type! Can you solve Chris's homework? | The first line of input contains an integer *n* (1<=β€<=*n*<=β€<=1000), the number of rows and the number of columns in the matrix *A*. The next *n* lines describe the matrix: the *i*-th line contains *n* space-separated bits and describes the *i*-th row of *A*. The *j*-th number of the *i*-th line *a**ij* (0<=β€<=*a**ij*<=β€<=1) is the element on the intersection of the *i*-th row and the *j*-th column of *A*.
The next line of input contains an integer *q* (1<=β€<=*q*<=β€<=106), the number of queries. Each of the next *q* lines describes a single query, which can be one of the following:
- 1 *i* β flip the values of the *i*-th row; - 2 *i* β flip the values of the *i*-th column; - 3 β output the unusual square of *A*.
Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++. | Let the number of the 3rd type queries in the input be *m*. Output a single string *s* of length *m*, where the *i*-th symbol of *s* is the value of the unusual square of *A* for the *i*-th query of the 3rd type as it appears in the input. | [
"3\n1 1 1\n0 1 1\n1 0 0\n12\n3\n2 3\n3\n2 2\n2 2\n1 3\n3\n3\n1 2\n2 1\n1 1\n3\n"
] | [
"01001\n"
] | none | [
{
"input": "3\n1 1 1\n0 1 1\n1 0 0\n12\n3\n2 3\n3\n2 2\n2 2\n1 3\n3\n3\n1 2\n2 1\n1 1\n3",
"output": "01001"
},
{
"input": "1\n1\n9\n1 1\n3\n1 1\n1 1\n3\n1 1\n3\n1 1\n3",
"output": "0010"
},
{
"input": "3\n1 0 1\n0 1 1\n1 0 1\n4\n3\n3\n3\n3",
"output": "1111"
},
{
"input": "1... | 951 | 73,318,400 | 3 | 32,577 | |
144 | Meeting | [
"implementation"
] | null | null | The Super Duper Secret Meeting of the Super Duper Secret Military Squad takes place in a Super Duper Secret Place. The place is an infinite plane with introduced Cartesian coordinate system. The meeting table is represented as a rectangle whose sides are parallel to the coordinate axes and whose vertexes are located at the integer points of the plane. At each integer point which belongs to the table perimeter there is a chair in which a general sits.
Some points on the plane contain radiators for the generals not to freeze in winter. Each radiator is characterized by the number *r**i* β the radius of the area this radiator can heat. That is, if the distance between some general and the given radiator is less than or equal to *r**i*, than the general feels comfortable and warm. Here distance is defined as Euclidean distance, so the distance between points (*x*1,<=*y*1) and (*x*2,<=*y*2) is
Each general who is located outside the radiators' heating area can get sick. Thus, you should bring him a warm blanket. Your task is to count the number of warm blankets you should bring to the Super Duper Secret Place.
The generals who are already comfortable do not need a blanket. Also the generals never overheat, ever if they are located in the heating area of several radiators. The radiators can be located at any integer points on the plane, even inside the rectangle (under the table) or on the perimeter (directly under some general). Even in this case their radius does not change. | The first input line contains coordinates of two opposite table corners *x**a*, *y**a*, *x**b*, *y**b* (*x**a*<=β <=*x**b*,<=*y**a*<=β <=*y**b*). The second line contains integer *n* β the number of radiators (1<=β€<=*n*<=β€<=103). Then *n* lines contain the heaters' coordinates as "*x**i* *y**i* *r**i*", the numbers are separated by spaces. All input data numbers are integers. The absolute value of all coordinates does not exceed 1000, 1<=β€<=*r**i*<=β€<=1000. Several radiators can be located at the same point. | Print the only number β the number of blankets you should bring. | [
"2 5 4 2\n3\n3 1 2\n5 3 1\n1 3 2\n",
"5 2 6 3\n2\n6 2 2\n6 5 3\n"
] | [
"4\n",
"0\n"
] | In the first sample the generals are sitting at points: (2,β2), (2,β3), (2,β4), (2,β5), (3,β2), (3,β5), (4,β2), (4,β3), (4,β4), (4,β5). Among them, 4 generals are located outside the heating range. They are the generals at points: (2,β5), (3,β5), (4,β4), (4,β5).
In the second sample the generals are sitting at points: (5,β2), (5,β3), (6,β2), (6,β3). All of them are located inside the heating range. | [
{
"input": "2 5 4 2\n3\n3 1 2\n5 3 1\n1 3 2",
"output": "4"
},
{
"input": "5 2 6 3\n2\n6 2 2\n6 5 3",
"output": "0"
},
{
"input": "-705 595 -702 600\n1\n-589 365 261",
"output": "4"
},
{
"input": "-555 674 -553 774\n5\n-656 128 631\n597 -220 999\n-399 793 155\n-293 -363 1000\... | 2,000 | 614,400 | 0 | 32,602 | |
425 | Sereja and Squares | [
"binary search",
"data structures",
"hashing"
] | null | null | Sereja has painted *n* distinct points on the plane. The coordinates of each point are integers. Now he is wondering: how many squares are there with sides parallel to the coordinate axes and with points painted in all its four vertexes? Help him, calculate this number. | The first line contains integer *n* (1<=β€<=*n*<=β€<=105). Each of the next *n* lines contains two integers *x**i*,<=*y**i* (0<=β€<=*x**i*,<=*y**i*<=β€<=105), the integers represent the coordinates of the *i*-th point. It is guaranteed that all the given points are distinct. | In a single line print the required number of squares. | [
"5\n0 0\n0 2\n2 0\n2 2\n1 1\n",
"9\n0 0\n1 1\n2 2\n0 1\n1 0\n0 2\n2 0\n1 2\n2 1\n"
] | [
"1\n",
"5\n"
] | none | [
{
"input": "5\n0 0\n0 2\n2 0\n2 2\n1 1",
"output": "1"
},
{
"input": "9\n0 0\n1 1\n2 2\n0 1\n1 0\n0 2\n2 0\n1 2\n2 1",
"output": "5"
},
{
"input": "54\n0 8\n3 2\n9 3\n7 2\n8 2\n2 8\n10 10\n7 6\n1 1\n9 7\n4 0\n6 10\n10 1\n10 8\n5 1\n0 4\n7 10\n3 6\n0 5\n4 3\n3 0\n5 10\n6 9\n5 4\n6 6\n8 5\... | 2,000 | 3,481,600 | 0 | 32,604 | |
0 | none | [
"none"
] | null | null | Π ΠΎΠ΄ΠΈΡΠ΅Π»ΠΈ ΠΠ°ΡΠΈ Ρ
ΠΎΡΡΡ, ΡΡΠΎΠ±Ρ ΠΎΠ½ ΠΊΠ°ΠΊ ΠΌΠΎΠΆΠ½ΠΎ Π»ΡΡΡΠ΅ ΡΡΠΈΠ»ΡΡ. ΠΠΎΡΡΠΎΠΌΡ Π΅ΡΠ»ΠΈ ΠΎΠ½ ΠΏΠΎΠ»ΡΡΠ°Π΅Ρ ΠΏΠΎΠ΄ΡΡΠ΄ ΡΡΠΈ ΠΏΠΎΠ»ΠΎΠΆΠΈΡΠ΅Π»ΡΠ½ΡΠ΅ ΠΎΡΠ΅Π½ΠΊΠΈ (Β«ΡΠ΅ΡΠ²ΡΡΠΊΠΈΒ» ΠΈΠ»ΠΈ Β«ΠΏΡΡΡΡΠΊΠΈΒ»), ΠΎΠ½ΠΈ Π΄Π°ΡΡΡ Π΅ΠΌΡ ΠΏΠΎΠ΄Π°ΡΠΎΠΊ. Π‘ΠΎΠΎΡΠ²Π΅ΡΡΡΠ²Π΅Π½Π½ΠΎ, ΠΎΡΠ΅Π½ΠΊΠΈ Β«Π΅Π΄ΠΈΠ½ΠΈΡΠ°Β», Β«Π΄Π²ΠΎΠΉΠΊΠ°Β» ΠΈ Β«ΡΡΠΎΠΉΠΊΠ°Β» ΡΠΎΠ΄ΠΈΡΠ΅Π»ΠΈ ΠΠ°ΡΠΈ ΡΡΠΈΡΠ°ΡΡ ΠΏΠ»ΠΎΡ
ΠΈΠΌΠΈ. ΠΠΎΠ³Π΄Π° ΠΠ°ΡΡ ΠΏΠΎΠ»ΡΡΠ°Π΅Ρ ΠΏΠΎΠ΄ΡΡΠ΄ ΡΡΠΈ Ρ
ΠΎΡΠΎΡΠΈΠ΅ ΠΎΡΠ΅Π½ΠΊΠΈ, Π΅ΠΌΡ ΡΡΠ°Π·Ρ Π²ΡΡΡΠ°ΡΡ ΠΏΠΎΠ΄Π°ΡΠΎΠΊ, Π½ΠΎ Π΄Π»Ρ ΡΠΎΠ³ΠΎ, ΡΡΠΎΠ±Ρ ΠΏΠΎΠ»ΡΡΠΈΡΡ Π΅ΡΡ ΠΎΠ΄ΠΈΠ½ ΠΏΠΎΠ΄Π°ΡΠΎΠΊ, Π΅ΠΌΡ Π²Π½ΠΎΠ²Ρ Π½Π°Π΄ΠΎ ΠΏΠΎΠ»ΡΡΠΈΡΡ ΠΏΠΎΠ΄ΡΡΠ΄ Π΅ΡΡ ΡΡΠΈ Ρ
ΠΎΡΠΎΡΠΈΠ΅ ΠΎΡΠ΅Π½ΠΊΠΈ.
ΠΠ°ΠΏΡΠΈΠΌΠ΅Ρ, Π΅ΡΠ»ΠΈ ΠΠ°ΡΡ ΠΏΠΎΠ»ΡΡΠΈΡ ΠΏΠΎΠ΄ΡΡΠ΄ ΠΏΡΡΡ Β«ΡΠ΅ΡΠ²ΡΡΠΎΠΊΒ» ΠΎΡΠ΅Π½ΠΎΠΊ, Π° ΠΏΠΎΡΠΎΠΌ Β«Π΄Π²ΠΎΠΉΠΊΡΒ», ΡΠΎ Π΅ΠΌΡ Π΄Π°Π΄ΡΡ ΡΠΎΠ»ΡΠΊΠΎ ΠΎΠ΄ΠΈΠ½ ΠΏΠΎΠ΄Π°ΡΠΎΠΊ, Π° Π²ΠΎΡ Π΅ΡΠ»ΠΈ Π±Ρ Β«ΡΠ΅ΡΠ²ΡΡΠΎΠΊΒ» Π±ΡΠ»ΠΎ ΡΠΆΠ΅ ΡΠ΅ΡΡΡ, ΡΠΎ ΠΏΠΎΠ΄Π°ΡΠΊΠΎΠ² Π±ΡΠ»ΠΎ Π±Ρ Π΄Π²Π°.
ΠΠ° ΠΌΠ΅ΡΡΡ ΠΠ°ΡΡ ΠΏΠΎΠ»ΡΡΠΈΠ» *n* ΠΎΡΠ΅Π½ΠΎΠΊ. ΠΠ°ΠΌ ΠΏΡΠ΅Π΄ΡΡΠΎΠΈΡ ΠΏΠΎΡΡΠΈΡΠ°ΡΡ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΏΠΎΠ΄Π°ΡΠΊΠΎΠ², ΠΊΠΎΡΠΎΡΡΠ΅ ΠΏΠΎΠ»ΡΡΠΈΠ» ΠΠ°ΡΡ. ΠΡΠ΅Π½ΠΊΠΈ Π±ΡΠ΄ΡΡ Π΄Π°Π½Ρ ΠΈΠΌΠ΅Π½Π½ΠΎ Π² ΡΠΎΠΌ ΠΏΠΎΡΡΠ΄ΠΊΠ΅, Π² ΠΊΠΎΡΠΎΡΠΎΠΌ ΠΠ°ΡΡ ΠΈΡ
ΠΏΠΎΠ»ΡΡΠ°Π». | Π ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
ΡΠ»Π΅Π΄ΡΠ΅Ρ ΡΠ΅Π»ΠΎΠ΅ ΠΏΠΎΠ»ΠΎΠΆΠΈΡΠ΅Π»ΡΠ½ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ *n* (3<=β€<=*n*<=β€<=1000)Β β ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΎΡΠ΅Π½ΠΎΠΊ, ΠΏΠΎΠ»ΡΡΠ΅Π½Π½ΡΡ
ΠΠ°ΡΠ΅ΠΉ.
ΠΠΎ Π²ΡΠΎΡΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
ΡΠ»Π΅Π΄ΡΠ΅Ρ ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°ΡΠ΅Π»ΡΠ½ΠΎΡΡΡ ΠΈΠ· *n* ΡΠΈΡΠ΅Π» *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=5)Β β ΠΎΡΠ΅Π½ΠΊΠΈ, ΠΏΠΎΠ»ΡΡΠ΅Π½Π½ΡΠ΅ ΠΠ°ΡΠ΅ΠΉ. ΠΡΠ΅Π½ΠΊΠΈ Π·Π°Π΄Π°Π½Ρ Π² ΡΠΎΠΌ ΠΏΠΎΡΡΠ΄ΠΊΠ΅, Π² ΠΊΠΎΡΠΎΡΠΎΠΌ ΠΠ°ΡΡ ΠΈΡ
ΠΏΠΎΠ»ΡΡΠΈΠ». | ΠΡΠ²Π΅Π΄ΠΈΡΠ΅ ΠΎΠ΄Π½ΠΎ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎΒ β ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΏΠΎΠ΄Π°ΡΠΊΠΎΠ², ΠΏΠΎΠ»ΡΡΠ΅Π½Π½ΡΡ
ΠΠ°ΡΠ΅ΠΉ. | [
"6\n4 5 4 5 4 4\n",
"14\n1 5 4 5 2 4 4 5 5 4 3 4 5 5\n"
] | [
"2\n",
"3\n"
] | Π ΠΏΠ΅ΡΠ²ΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ ΠΠ°ΡΡ ΠΏΠΎΠ»ΡΡΠΈΡ Π΄Π²Π° ΠΏΠΎΠ΄Π°ΡΠΊΠ°Β β Π·Π° ΠΏΠ΅ΡΠ²ΡΠ΅ ΡΡΠΈ ΠΏΠΎΠ»ΠΎΠΆΠΈΡΠ΅Π»ΡΠ½ΡΠ΅ ΠΎΡΠ΅Π½ΠΊΠΈ ΠΈ Π·Π° ΡΠ»Π΅Π΄ΡΡΡΡΡ ΡΡΠΎΠΉΠΊΡ ΠΏΠΎΠ»ΠΎΠΆΠΈΡΠ΅Π»ΡΠ½ΡΡ
ΠΎΡΠ΅Π½ΠΎΠΊ ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²Π΅Π½Π½ΠΎ. | [
{
"input": "6\n4 5 4 5 4 4",
"output": "2"
},
{
"input": "14\n1 5 4 5 2 4 4 5 5 4 3 4 5 5",
"output": "3"
},
{
"input": "3\n4 5 4",
"output": "1"
},
{
"input": "3\n4 5 1",
"output": "0"
},
{
"input": "4\n5 4 3 5",
"output": "0"
},
{
"input": "10\n4 4 5... | 77 | 4,608,000 | 3 | 32,703 | |
753 | Interactive Bulls and Cows (Easy) | [
"brute force",
"constructive algorithms",
"implementation"
] | null | null | This problem is a little bit unusual. Here you are to implement an interaction with a testing system. That means that you can make queries and get responses in the online mode. Please be sure to use the stream flushing operation after each query's output in order not to leave part of your output in some buffer. For example, in C++ you've got to use the fflush(stdout) function, in Java β call System.out.flush(), and in Pascal β flush(output).
Bulls and Cows (also known as Cows and Bulls or Pigs and Bulls or Bulls and Cleots) is an old code-breaking paper and pencil game for two players, predating the similar commercially marketed board game Mastermind.
On a sheet of paper, the first player thinks a secret string. This string consists only of digits and has the length 4. The digits in the string must be all different, no two or more equal digits are allowed.
Then the second player tries to guess his opponent's string. For every guess the first player gives the number of matches. If the matching digits are on their right positions, they are "bulls", if on different positions, they are "cows". Thus a response is a pair of numbers β the number of "bulls" and the number of "cows". A try can contain equal digits.
More formally, let's the secret string is *s* and the second player are trying to guess it with a string *x*. The number of "bulls" is a number of such positions *i* (1<=β€<=*i*<=β€<=4) where *s*[*i*]<==<=*x*[*i*]. The number of "cows" is a number of such digits *c* that *s* contains *c* in the position *i* (i.e. *s*[*i*]<==<=*c*), *x* contains *c*, but *x*[*i*]<=β <=*c*.
For example, the secret string is "0427", the opponent's try is "0724", then the answer is 2 bulls and 2 cows (the bulls are "0" and "2", the cows are "4" and "7"). If the secret string is "0123", the opponent's try is "0330", then the answer is 1 bull and 1 cow.
In this problem you are to guess the string *s* that the system has chosen. You only know that the chosen string consists of 4 distinct digits.
You can make queries to the testing system, each query is the output of a single 4-digit string. The answer to the query is the number of bulls and number of cows. If the system's response equals "4 0", that means the interaction with your problem is over and the program must terminate. That is possible for two reasons β the program either guessed the number *x* or made an invalid action (for example, printed letters instead of digits).
Your program is allowed to do at most 50 queries.
You can hack solutions of other participants providing a 4-digit string containing distinct digits β the secret string. | To read answers to the queries, the program must use the standard input.
The program will receive pairs of non-negative integers in the input, one pair per line. The first number in a pair is a number of bulls and the second one is a number of cows of the string *s* and the string *x**i* printed by your program. If the system response equals "4 0", then your solution should terminate.
The testing system will let your program read the *i*-th pair of integers from the input only after your program displays the corresponding system query in the output: prints value *x**i* in a single line and executes operation flush. | The program must use the standard output to print queries.
Your program must output requests β 4-digit strings *x*1,<=*x*2,<=..., one per line. After the output of each line the program must execute flush operation. The program should read the answer to the query from the standard input.
Your program is allowed to do at most 50 queries. | [
"0 1\n2 0\n1 1\n0 4\n2 1\n4 0\n"
] | [
"8000\n0179\n3159\n3210\n0112\n0123"
] | The secret string *s* in the example is "0123". | [
{
"input": "0123",
"output": "20"
},
{
"input": "1234",
"output": "20"
},
{
"input": "9876",
"output": "20"
},
{
"input": "7158",
"output": "20"
},
{
"input": "7590",
"output": "20"
},
{
"input": "7325",
"output": "20"
},
{
"input": "7524",... | 46 | 4,608,000 | -1 | 32,707 | |
744 | Hongcow Draws a Circle | [
"geometry"
] | null | null | Hongcow really likes the color red. Hongcow doesn't like the color blue.
Hongcow is standing in an infinite field where there are *n* red points and *m* blue points.
Hongcow wants to draw a circle in the field such that this circle contains at least one red point, and no blue points. Points that line exactly on the boundary of the circle can be counted as either inside or outside.
Compute the radius of the largest circle that satisfies this condition. If this circle can have arbitrarily large size, print <=-<=1. Otherwise, your answer will be accepted if it has relative or absolute error at most 10<=-<=4. | The first line of the input will contain two integers *n*,<=*m* (1<=β€<=*n*,<=*m*<=β€<=1,<=000).
The next *n* lines will contain two integers *x**i*,<=*y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=104). This denotes the coordinates of a red point.
The next *m* lines will contain two integers *x**i*,<=*y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=104). This denotes the coordinates of a blue point.
No two points will have the same coordinates. | Print <=-<=1 if the circle can have arbitrary size. Otherwise, print a floating point number representing the largest radius circle that satisfies the conditions. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=4.
Namely, let's assume that your answer is *a* and the answer of the jury is *b*. The checker program will consider your answer correct if . | [
"2 5\n2 3\n3 4\n1 1\n1 4\n4 2\n4 7\n2 5\n",
"1 6\n3 3\n1 5\n5 4\n2 1\n3 4\n4 2\n1 3\n",
"2 2\n2 2\n3 3\n1 1\n4 4\n"
] | [
"3.5355338827\n",
"1.5811388195\n",
"-1\n"
] | This is a picture of the first sample
This is a picture of the second sample | [] | 30 | 0 | 0 | 32,808 | |
451 | Devu and Flowers | [
"bitmasks",
"combinatorics",
"number theory"
] | null | null | Devu wants to decorate his garden with flowers. He has purchased *n* boxes, where the *i*-th box contains *f**i* flowers. All flowers in a single box are of the same color (hence they are indistinguishable). Also, no two boxes have flowers of the same color.
Now Devu wants to select exactly *s* flowers from the boxes to decorate his garden. Devu would like to know, in how many different ways can he select the flowers from each box? Since this number may be very large, he asks you to find the number modulo (109<=+<=7).
Devu considers two ways different if there is at least one box from which different number of flowers are selected in these two ways. | The first line of input contains two space-separated integers *n* and *s* (1<=β€<=*n*<=β€<=20, 0<=β€<=*s*<=β€<=1014).
The second line contains *n* space-separated integers *f*1,<=*f*2,<=... *f**n* (0<=β€<=*f**i*<=β€<=1012). | Output a single integer β the number of ways in which Devu can select the flowers modulo (109<=+<=7). | [
"2 3\n1 3\n",
"2 4\n2 2\n",
"3 5\n1 3 2\n"
] | [
"2\n",
"1\n",
"3\n"
] | Sample 1. There are two ways of selecting 3 flowers: {1,β2} and {0,β3}.
Sample 2. There is only one way of selecting 4 flowers: {2,β2}.
Sample 3. There are three ways of selecting 5 flowers: {1,β2,β2}, {0,β3,β2}, and {1,β3,β1}. | [
{
"input": "2 3\n1 3",
"output": "2"
},
{
"input": "2 4\n2 2",
"output": "1"
},
{
"input": "3 5\n1 3 2",
"output": "3"
},
{
"input": "2 270030023747\n891135146290 437305641972",
"output": "30021858"
},
{
"input": "20 4385085334307\n273634411136 208521328637 450482... | 46 | 0 | 0 | 32,823 | |
78 | Evacuation | [
"flows",
"graphs",
"shortest paths"
] | E. Evacuation | 1 | 256 | They've screwed something up yet again... In one nuclear reactor of a research station an uncontrolled reaction is in progress and explosion which will destroy the whole station will happen soon.
The station is represented by a square *n*<=Γ<=*n* divided into 1<=Γ<=1 blocks. Each block is either a reactor or a laboratory. There can be several reactors and exactly one of them will explode soon. The reactors can be considered impassable blocks, but one can move through laboratories. Between any two laboratories, which are in adjacent blocks, there is a corridor. Blocks are considered adjacent if they have a common edge.
In each laboratory there is some number of scientists and some number of rescue capsules. Once the scientist climbs into a capsule, he is considered to be saved. Each capsule has room for not more than one scientist.
The reactor, which is about to explode, is damaged and a toxic coolant trickles from it into the neighboring blocks. The block, which contains the reactor, is considered infected. Every minute the coolant spreads over the laboratories through corridors. If at some moment one of the blocks is infected, then the next minute all the neighboring laboratories also become infected. Once a lab is infected, all the scientists there that are not in rescue capsules die. The coolant does not spread through reactor blocks.
There are exactly *t* minutes to the explosion. Any scientist in a minute can move down the corridor to the next lab, if it is not infected. On any corridor an unlimited number of scientists can simultaneously move in both directions. It is believed that the scientists inside a lab moves without consuming time. Moreover, any scientist could get into the rescue capsule instantly. It is also believed that any scientist at any given moment always has the time to perform their actions (move from the given laboratory into the next one, or climb into the rescue capsule) before the laboratory will be infected.
Find the maximum number of scientists who will be able to escape. | The first line contains two integers *n* and *t* (2<=β€<=*n*<=β€<=10, 1<=β€<=*t*<=β€<=60). Each of the next *n* lines contains *n* characters. These lines describe the scientists' locations. Then exactly one empty line follows. Each of the next *n* more lines contains *n* characters. These lines describe the rescue capsules' locations.
In the description of the scientists' and the rescue capsules' locations the character "Y" stands for a properly functioning reactor, "Z" stands for the malfunctioning reactor. The reactors' positions in both descriptions coincide. There is exactly one malfunctioning reactor on the station. The digits "0" - "9" stand for the laboratories. In the description of the scientists' locations those numbers stand for the number of scientists in the corresponding laboratories. In the rescue capsules' descriptions they stand for the number of such capsules in each laboratory. | Print a single number β the maximum number of scientists who will manage to save themselves. | [
"3 3\n1YZ\n1YY\n100\n\n0YZ\n0YY\n003\n",
"4 4\nY110\n1Y1Z\n1Y0Y\n0100\n\nY001\n0Y0Z\n0Y0Y\n0005\n"
] | [
"2",
"3"
] | In the second sample the events could take place as follows: | [] | 46 | 0 | 0 | 33,062 |
77 | Martian Food | [
"geometry"
] | E. Martian Food | 1 | 256 | Have you ever tasted Martian food? Well, you should.
Their signature dish is served on a completely black plate with the radius of *R*, flat as a pancake.
First, they put a perfectly circular portion of the Golden Honduras on the plate. It has the radius of *r* and is located as close to the edge of the plate as possible staying entirely within the plate. I. e. Golden Honduras touches the edge of the plate from the inside. It is believed that the proximity of the portion of the Golden Honduras to the edge of a plate demonstrates the neatness and exactness of the Martians.
Then a perfectly round portion of Pink Guadeloupe is put on the plate. The Guadeloupe should not overlap with Honduras, should not go beyond the border of the plate, but should have the maximum radius. I. e. Pink Guadeloupe should touch the edge of the plate from the inside, and touch Golden Honduras from the outside. For it is the size of the Rose Guadeloupe that shows the generosity and the hospitality of the Martians.
Further, the first portion (of the same perfectly round shape) of Green Bull Terrier is put on the plate. It should come in contact with Honduras and Guadeloupe, should not go beyond the border of the plate and should have maximum radius.
Each of the following portions of the Green Bull Terrier must necessarily touch the Golden Honduras, the previous portion of the Green Bull Terrier and touch the edge of a plate, but should not go beyond the border.
To determine whether a stranger is worthy to touch the food, the Martians ask him to find the radius of the *k*-th portion of the Green Bull Terrier knowing the radii of a plate and a portion of the Golden Honduras. And are you worthy? | The first line contains integer *t* (1<=β€<=*t*<=β€<=104) β amount of testcases.
Each of the following *t* lines contain three positive integers: the radii of the plate and a portion of the Golden Honduras *R* and *r* (1<=β€<=*r*<=<<=*R*<=β€<=104) and the number *k* (1<=β€<=*k*<=β€<=104).
In the pretests 1<=β€<=*k*<=β€<=2. | Print *t* lines β the radius of the *k*-th portion of the Green Bull Terrier for each test. The absolute or relative error of the answer should not exceed 10<=-<=6. | [
"2\n4 3 1\n4 2 2\n"
] | [
"0.9230769231\n0.6666666667\n"
] | Dish from the first sample looks like this:
<img class="tex-graphics" src="https://espresso.codeforces.com/e15f6caf257ceae8305cd82507b96dfac1b579ee.png" style="max-width: 100.0%;max-height: 100.0%;"/>
Dish from the second sample looks like this:
<img class="tex-graphics" src="https://espresso.codeforces.com/625a74ccb42332c9df05a22f8e17f81f086476d5.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "2\n4 3 1\n4 2 2",
"output": "0.9230769231\n0.6666666667"
},
{
"input": "1\n4 2 2",
"output": "0.6666666667"
},
{
"input": "1\n7 2 1",
"output": "1.7948717949"
},
{
"input": "1\n8 7 2",
"output": "0.9333333333"
},
{
"input": "1\n2 1 1",
"output": "0... | 249 | 7,782,400 | 3.861004 | 33,214 |
34 | Road Map | [
"dfs and similar",
"graphs"
] | D. Road Map | 2 | 256 | There are *n* cities in Berland. Each city has its index β an integer number from 1 to *n*. The capital has index *r*1. All the roads in Berland are two-way. The road system is such that there is exactly one path from the capital to each city, i.e. the road map looks like a tree. In Berland's chronicles the road map is kept in the following way: for each city *i*, different from the capital, there is kept number *p**i* β index of the last city on the way from the capital to *i*.
Once the king of Berland Berl XXXIV decided to move the capital from city *r*1 to city *r*2. Naturally, after this the old representation of the road map in Berland's chronicles became incorrect. Please, help the king find out a new representation of the road map in the way described above. | The first line contains three space-separated integers *n*, *r*1, *r*2 (2<=β€<=*n*<=β€<=5Β·104,<=1<=β€<=*r*1<=β <=*r*2<=β€<=*n*) β amount of cities in Berland, index of the old capital and index of the new one, correspondingly.
The following line contains *n*<=-<=1 space-separated integers β the old representation of the road map. For each city, apart from *r*1, there is given integer *p**i* β index of the last city on the way from the capital to city *i*. All the cities are described in order of increasing indexes. | Output *n*<=-<=1 numbers β new representation of the road map in the same format. | [
"3 2 3\n2 2\n",
"6 2 4\n6 1 2 4 2\n"
] | [
"2 3 ",
"6 4 1 4 2 "
] | none | [
{
"input": "3 2 3\n2 2",
"output": "2 3 "
},
{
"input": "6 2 4\n6 1 2 4 2",
"output": "6 4 1 4 2 "
},
{
"input": "7 7 6\n7 7 5 5 7 7",
"output": "7 7 5 5 7 6 "
},
{
"input": "4 2 3\n2 1 3",
"output": "3 1 3 "
},
{
"input": "5 5 4\n5 4 1 5",
"output": "5 4 1 4 ... | 155 | 102,400 | 0 | 33,229 |
855 | Nagini | [
"binary search",
"data structures"
] | null | null | Nagini, being a horcrux You-know-who created with the murder of Bertha Jorkins, has accumulated its army of snakes and is launching an attack on Hogwarts school.
Hogwarts' entrance can be imagined as a straight line (x-axis) from 1 to 105. Nagini is launching various snakes at the Hogwarts entrance. Each snake lands parallel to the entrance, covering a segment at a distance *k* from *x*<==<=*l* to *x*<==<=*r*. Formally, each snake can be imagined as being a line segment between points (*l*,<=*k*) and (*r*,<=*k*). Note that *k* can be both positive and negative, but not 0.
Let, at some *x*-coordinate *x*<==<=*i*, there be snakes at point (*i*,<=*y*1) and point (*i*,<=*y*2), such that *y*1<=><=0 and *y*2<=<<=0. Then, if for any point (*i*,<=*y*3) containing a snake such that *y*3<=><=0, *y*1<=β€<=*y*3 holds and for any point (*i*,<=*y*4) containing a snake such that *y*4<=<<=0, |*y*2|<=β€<=|*y*4| holds, then the danger value at coordinate *x*<==<=*i* is *y*1<=+<=|*y*2|. If no such *y*1 and *y*2 exist, danger value is 0.
Harry wants to calculate the danger value of various segments of the Hogwarts entrance. Danger value for a segment [*l*,<=*r*) of the entrance can be calculated by taking the sum of danger values for each integer *x*-coordinate present in the segment.
Formally, you have to implement two types of queries:
- 1 l r k: a snake is added parallel to entrance from *x*<==<=*l* to *x*<==<=*r* at y-coordinate *y*<==<=*k* (*l* inclusive, *r* exclusive). - 2 l r: you have to calculate the danger value of segment *l* to *r* (*l* inclusive, *r* exclusive). | First line of input contains a single integer *q* (1<=β€<=*q*<=β€<=5Β·104) denoting the number of queries.
Next *q* lines each describe a query. Each query description first contains the query type *type**i* (1<=β€<=*type**i*<=β€<=2). This is followed by further description of the query. In case of the type being 1, it is followed by integers *l**i*,<=*r**i* and *k**i* (, <=-<=109<=β€<=*k**i*<=β€<=109, *k*<=β <=0). Otherwise, it just contains two integers, *l**i* and *r**i* (1<=β€<=*l**i*<=<<=*r**i*<=β€<=105). | Output the answer for each query of type 2 in a separate line. | [
"3\n1 1 10 10\n1 2 4 -7\n2 1 10\n",
"7\n1 2 3 5\n1 1 10 10\n1 4 5 -5\n2 4 8\n1 1 10 -10\n2 4 8\n2 1 10\n"
] | [
"34\n",
"15\n75\n170\n"
] | In the first sample case, the danger value for *x*-coordinates 1 is 0 as there is no *y*<sub class="lower-index">2</sub> satisfying the above condition for *x*β=β1.
Danger values for *x*-coordinates 2 and 3 is 10β+β|β-β7|β=β17.
Danger values for *x*-coordinates 4 to 9 is again 0 as there is no *y*<sub class="lower-index">2</sub> satisfying the above condition for these coordinates.
Thus, total danger value is 17β+β17β=β34. | [] | 30 | 0 | 0 | 33,263 | |
360 | Levko and Array | [
"binary search",
"dp"
] | null | null | Levko has an array that consists of integers: *a*1,<=*a*2,<=... ,<=*a**n*. But he doesnβt like this array at all.
Levko thinks that the beauty of the array *a* directly depends on value *c*(*a*), which can be calculated by the formula:
Itβs time to change the world and Levko is going to change his array for the better. To be exact, Levko wants to change the values of at most *k* array elements (it is allowed to replace the values by any integers). Of course, the changes should make the array as beautiful as possible.
Help Levko and calculate what minimum number *c*(*a*) he can reach. | The first line contains two integers *n* and *k* (1<=β€<=*k*<=β€<=*n*<=β€<=2000). The second line contains space-separated integers *a*1,<=*a*2,<=... ,<=*a**n* (<=-<=109<=β€<=*a**i*<=β€<=109). | A single number β the minimum value of *c*(*a*) Levko can get. | [
"5 2\n4 7 4 7 4\n",
"3 1\n-100 0 100\n",
"6 3\n1 2 3 7 8 9\n"
] | [
"0\n",
"100\n",
"1\n"
] | In the first sample Levko can change the second and fourth elements and get array: 4, 4, 4, 4, 4.
In the third sample he can get array: 1, 2, 3, 4, 5, 6. | [
{
"input": "5 2\n4 7 4 7 4",
"output": "0"
},
{
"input": "3 1\n-100 0 100",
"output": "100"
},
{
"input": "6 3\n1 2 3 7 8 9",
"output": "1"
},
{
"input": "4 1\n-1000000000 -1000000000 1000000000 1000000000",
"output": "1000000000"
},
{
"input": "10 1\n-6 5 -7 -7 -... | 62 | 1,638,400 | 0 | 33,344 | |
630 | Hexagons! | [
"math"
] | null | null | After a probationary period in the game development company of IT City Petya was included in a group of the programmers that develops a new turn-based strategy game resembling the well known "Heroes of Might & Magic". A part of the game is turn-based fights of big squadrons of enemies on infinite fields where every cell is in form of a hexagon.
Some of magic effects are able to affect several field cells at once, cells that are situated not farther than *n* cells away from the cell in which the effect was applied. The distance between cells is the minimum number of cell border crosses on a path from one cell to another.
It is easy to see that the number of cells affected by a magic effect grows rapidly when *n* increases, so it can adversely affect the game performance. That's why Petya decided to write a program that can, given *n*, determine the number of cells that should be repainted after effect application, so that game designers can balance scale of the effects and the game performance. Help him to do it. Find the number of hexagons situated not farther than *n* cells away from a given cell. | The only line of the input contains one integer *n* (0<=β€<=*n*<=β€<=109). | Output one integer β the number of hexagons situated not farther than *n* cells away from a given cell. | [
"2\n"
] | [
"19"
] | none | [
{
"input": "2",
"output": "19"
},
{
"input": "0",
"output": "1"
},
{
"input": "1",
"output": "7"
},
{
"input": "3",
"output": "37"
},
{
"input": "749431",
"output": "1684942719577"
},
{
"input": "748629743",
"output": "1681339478558627377"
},
{... | 31 | 0 | 3 | 33,591 | |
13 | Triangles | [
"dp",
"geometry"
] | D. Triangles | 2 | 64 | Little Petya likes to draw. He drew *N* red and *M* blue points on the plane in such a way that no three points lie on the same line. Now he wonders what is the number of distinct triangles with vertices in red points which do not contain any blue point inside. | The first line contains two non-negative integer numbers *N* and *M* (0<=β€<=*N*<=β€<=500, 0<=β€<=*M*<=β€<=500) β the number of red and blue points respectively. The following *N* lines contain two integer numbers each β coordinates of red points. The following *M* lines contain two integer numbers each β coordinates of blue points. All coordinates do not exceed 109 by absolute value. | Output one integer β the number of distinct triangles with vertices in red points which do not contain any blue point inside. | [
"4 1\n0 0\n10 0\n10 10\n5 4\n2 1\n",
"5 5\n5 10\n6 1\n8 6\n-6 -7\n7 -1\n5 -1\n10 -4\n-10 -8\n-10 5\n-2 -8\n"
] | [
"2\n",
"7\n"
] | none | [] | 30 | 0 | 0 | 33,637 |
529 | Group Photo 2 (online mirror version) | [
"brute force",
"greedy",
"sortings"
] | null | null | Many years have passed, and *n* friends met at a party again. Technologies have leaped forward since the last meeting, cameras with timer appeared and now it is not obligatory for one of the friends to stand with a camera, and, thus, being absent on the photo.
Simply speaking, the process of photographing can be described as follows. Each friend occupies a rectangle of pixels on the photo: the *i*-th of them in a standing state occupies a *w**i* pixels wide and a *h**i* pixels high rectangle. But also, each person can lie down for the photo, and then he will occupy a *h**i* pixels wide and a *w**i* pixels high rectangle.
The total photo will have size *W*<=Γ<=*H*, where *W* is the total width of all the people rectangles, and *H* is the maximum of the heights. The friends want to determine what minimum area the group photo can they obtain if no more than *n*<=/<=2 of them can lie on the ground (it would be strange if more than *n*<=/<=2 gentlemen lie on the ground together, isn't it?..)
Help them to achieve this goal. | The first line contains integer *n* (1<=β€<=*n*<=β€<=1000) β the number of friends.
The next *n* lines have two integers *w**i*,<=*h**i* (1<=β€<=*w**i*,<=*h**i*<=β€<=1000) each, representing the size of the rectangle, corresponding to the *i*-th friend. | Print a single integer equal to the minimum possible area of the photo containing all friends if no more than *n*<=/<=2 of them can lie on the ground. | [
"3\n10 1\n20 2\n30 3\n",
"3\n3 1\n2 2\n4 3\n",
"1\n5 10\n"
] | [
"180\n",
"21\n",
"50\n"
] | none | [
{
"input": "3\n10 1\n20 2\n30 3",
"output": "180"
},
{
"input": "3\n3 1\n2 2\n4 3",
"output": "21"
},
{
"input": "1\n5 10",
"output": "50"
},
{
"input": "1\n1 1",
"output": "1"
},
{
"input": "1\n1000 1000",
"output": "1000000"
},
{
"input": "1\n1 1000"... | 61 | 5,632,000 | 0 | 33,639 | |
369 | Valera and Queries | [
"binary search",
"data structures"
] | null | null | Valera loves segments. He has recently come up with one interesting problem.
The *Ox* axis of coordinates has *n* segments, the *i*-th segment starts in position *l**i* and ends in position *r**i* (we will mark it as [*l**i*,<=*r**i*]). Your task is to process *m* queries, each consists of number *cnt**i* and a set of *cnt**i* coordinates of points located on the *Ox* axis. The answer to the query is the number of segments, such that each of them contains at least one point from the query. Segment [*l*,<=*r*] contains point *q*, if *l*<=β€<=*q*<=β€<=*r*.
Valera found the solution of this problem too difficult. So he asked you to help him. Help Valera. | The first line contains two integers *n*, *m* (1<=β€<=*n*,<=*m*<=β€<=3Β·105) β the number of segments on the axis of coordinates and the number of queries.
Next *n* lines contain the descriptions of the segments. The *i*-th line contains two positive integers *l**i*, *r**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=106) β the borders of the *i*-th segment.
Next *m* lines contain the description of the queries, one per line. Each line starts from integer *cnt**i* (1<=β€<=*cnt**i*<=β€<=3Β·105) β the number of points in the *i*-th query. Then the line contains *cnt**i* distinct positive integers *p*1,<=*p*2,<=...,<=*p**cnt**i* (1<=β€<=*p*1<=<<=*p*2<=<<=...<=<<=*p**cnt**i*<=β€<=106) β the coordinates of points in the *i*-th query.
It is guaranteed that the total number of points in all queries doesn't exceed 3Β·105. | Print *m* non-negative integers, where the *i*-th number is the response to the *i*-th query. | [
"3 3\n1 3\n4 5\n6 7\n3 1 4 7\n2 4 5\n1 8\n"
] | [
"3\n1\n0\n"
] | none | [
{
"input": "3 3\n1 3\n4 5\n6 7\n3 1 4 7\n2 4 5\n1 8",
"output": "3\n1\n0"
},
{
"input": "1 1\n172921 894619\n1 14141",
"output": "0"
},
{
"input": "3 1\n439010 864662\n377278 743032\n771051 955458\n1 568232",
"output": "2"
},
{
"input": "3 3\n328789 478281\n248154 348247\n820... | 46 | 0 | 0 | 33,762 | |
444 | DZY Loves Physics | [
"greedy",
"math"
] | null | null | DZY loves Physics, and he enjoys calculating density.
Almost everything has density, even a graph. We define the density of a non-directed graph (nodes and edges of the graph have some values) as follows:
Once DZY got a graph *G*, now he wants to find a connected induced subgraph *G*' of the graph, such that the density of *G*' is as large as possible.
An induced subgraph *G*'(*V*',<=*E*') of a graph *G*(*V*,<=*E*) is a graph that satisfies:
- ; - edge if and only if , and edge ; - the value of an edge in *G*' is the same as the value of the corresponding edge in *G*, so as the value of a node.
Help DZY to find the induced subgraph with maximum density. Note that the induced subgraph you choose must be connected. | The first line contains two space-separated integers *n*Β (1<=β€<=*n*<=β€<=500), . Integer *n* represents the number of nodes of the graph *G*, *m* represents the number of edges.
The second line contains *n* space-separated integers *x**i*Β (1<=β€<=*x**i*<=β€<=106), where *x**i* represents the value of the *i*-th node. Consider the graph nodes are numbered from 1 to *n*.
Each of the next *m* lines contains three space-separated integers *a**i*,<=*b**i*,<=*c**i*Β (1<=β€<=*a**i*<=<<=*b**i*<=β€<=*n*;Β 1<=β€<=*c**i*<=β€<=103), denoting an edge between node *a**i* and *b**i* with value *c**i*. The graph won't contain multiple edges. | Output a real number denoting the answer, with an absolute or relative error of at most 10<=-<=9. | [
"1 0\n1\n",
"2 1\n1 2\n1 2 1\n",
"5 6\n13 56 73 98 17\n1 2 56\n1 3 29\n1 4 42\n2 3 95\n2 4 88\n3 4 63\n"
] | [
"0.000000000000000\n",
"3.000000000000000\n",
"2.965517241379311\n"
] | In the first sample, you can only choose an empty subgraph, or the subgraph containing only node 1.
In the second sample, choosing the whole graph is optimal. | [
{
"input": "1 0\n1",
"output": "0.000000000000000"
},
{
"input": "2 1\n1 2\n1 2 1",
"output": "3.000000000000000"
},
{
"input": "5 6\n13 56 73 98 17\n1 2 56\n1 3 29\n1 4 42\n2 3 95\n2 4 88\n3 4 63",
"output": "2.965517241379311"
},
{
"input": "1 0\n734135",
"output": "0.0... | 0 | 0 | -1 | 33,820 | |
42 | Game of chess unfinished | [
"implementation"
] | B. Game of chess unfinished | 2 | 256 | Once Volodya was at the museum and saw a regular chessboard as a museum piece. And there were only four chess pieces on it: two white rooks, a white king and a black king. "Aha, blacks certainly didn't win!", β Volodya said and was right for sure. And your task is to say whether whites had won or not.
Pieces on the chessboard are guaranteed to represent a correct position (every piece occupies one cell, no two pieces occupy the same cell and kings cannot take each other). Thus, your task is only to decide whether whites mate blacks. We would remind you that it means that the black king can be taken by one of the opponent's pieces at the moment and also it cannot move to an unbeaten position. A rook moves vertically or horizontally by any number of free cells (assuming there are no other pieces on its path), a king β to the adjacent cells (either by corner or by side). Certainly, pieces cannot leave the board. The black king might be able to take opponent's rooks at his turn (see sample 3). | The input contains 4 space-separated piece positions: positions of the two rooks, the white king and the black king. Each position on 8<=Γ<=8 chessboard is denoted by two symbols β ('a' - 'h') and ('1' - '8') β which stand for horizontal and vertical coordinates of the cell occupied by the piece. It is guaranteed, that no two pieces occupy the same cell, and kings cannot take each other. | Output should contain one word: "CHECKMATE" if whites mate blacks, and "OTHER" otherwise. | [
"a6 b4 c8 a8\n",
"a6 c4 b6 b8\n",
"a2 b1 a3 a1\n"
] | [
"CHECKMATE\n",
"OTHER\n",
"OTHER\n"
] | none | [
{
"input": "a6 b4 c8 a8",
"output": "CHECKMATE"
},
{
"input": "a6 c4 b6 b8",
"output": "OTHER"
},
{
"input": "a2 b1 a3 a1",
"output": "OTHER"
},
{
"input": "a5 c5 c2 a1",
"output": "CHECKMATE"
},
{
"input": "a5 c5 c3 a1",
"output": "OTHER"
},
{
"input"... | 312 | 6,553,600 | 0 | 33,855 |
713 | Searching Rectangles | [
"binary search",
"constructive algorithms",
"interactive"
] | null | null | Filya just learned new geometry objectΒ β rectangle. He is given a field consisting of *n*<=Γ<=*n* unit cells. Rows are numbered from bottom to top with integer from 1 to *n*. Columns are numbered from left to right with integers from 1 to *n*. Cell, located at the intersection of the row *r* and column *c* is denoted as (*r*,<=*c*). Filya has painted two rectangles, such that their sides are parallel to coordinate axes and each cell lies fully inside or fully outside each of them. Moreover, no cell lies in both rectangles.
Later, hedgehog Filya became interested in the location of his rectangles but was unable to find the sheet of paper they were painted on. They were taken by Sonya and now she wants to play a little game with Filya. He tells her a query rectangle and she replies with the number of initial rectangles that lie fully inside the given query rectangle. The query rectangle should match the same conditions as initial rectangles. Rectangle lies fully inside the query if each o its cells lies inside the query.
Filya knows Sonya really well, so is sure that if he asks more than 200 questions she will stop to reply. | The first line of the input contains an integer *n* (2<=β€<=*n*<=β€<=216)Β β size of the field.
For each query an integer between 0 and 2 is returnedΒ β the number of initial rectangles that lie fully inside the query rectangle. | To make a query you have to print "? *x*1 *y*1 *x*2 *y*2" (without quotes) (1<=β€<=*x*1<=β€<=*x*2<=β€<=*n*, 1<=β€<=*y*1<=β€<=*y*2<=β€<=*n*), where (*x*1,<=*y*1) stands for the position of the bottom left cell of the query and (*x*2,<=*y*2) stands for the up right cell of the query. You are allowed to ask no more than 200 queries. After each query you should perform "flush" operation and read the answer.
In case you suppose you've already determined the location of two rectangles (or run out of queries) you should print "! *x*11 *y*11 *x*12 *y*12 *x*21 *y*21 *x*22 *y*22" (without quotes), where first four integers describe the bottom left and up right cells of the first rectangle, and following four describe the corresponding cells of the second rectangle. You can print the rectangles in an arbitrary order. After you have printed the answer, print the end of the line and perform "flush". Your program should terminate immediately after it print the answer. | [
"5\n2\n1\n0\n1\n1\n1\n0\n1\n"
] | [
"? 1 1 5 5\n? 1 1 3 3\n? 1 1 3 1\n? 2 2 2 2\n? 3 3 5 5\n? 3 3 3 5\n? 3 3 3 4\n? 3 4 3 5\n! 2 2 2 2 3 4 3 5\n"
] | none | [
{
"input": "5\n2 2 2 2\n3 4 3 5",
"output": "17\n2 2 2 2 3 4 3 5"
},
{
"input": "10\n1 2 4 2\n1 6 4 9",
"output": "21\n1 2 4 2 1 6 4 9"
},
{
"input": "10\n2 2 4 4\n7 10 9 10",
"output": "23\n2 2 4 4 7 10 9 10"
},
{
"input": "10\n1 1 10 1\n5 5 5 10",
"output": "23\n5 5 5 1... | 218 | 22,323,200 | 0 | 33,916 | |
0 | none | [
"none"
] | null | null | Arseny likes to organize parties and invite people to it. However, not only friends come to his parties, but friends of his friends, friends of friends of his friends and so on. That's why some of Arseny's guests can be unknown to him. He decided to fix this issue using the following procedure.
At each step he selects one of his guests *A*, who pairwise introduces all of his friends to each other. After this action any two friends of *A* become friends. This process is run until all pairs of guests are friends.
Arseny doesn't want to spend much time doing it, so he wants to finish this process using the minimum number of steps. Help Arseny to do it. | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=22; )Β β the number of guests at the party (including Arseny) and the number of pairs of people which are friends.
Each of the next *m* lines contains two integers *u* and *v* (1<=β€<=*u*,<=*v*<=β€<=*n*; *u*<=β <=*v*), which means that people with numbers *u* and *v* are friends initially. It's guaranteed that each pair of friends is described not more than once and the graph of friendship is connected. | In the first line print the minimum number of steps required to make all pairs of guests friends.
In the second line print the ids of guests, who are selected at each step.
If there are multiple solutions, you can output any of them. | [
"5 6\n1 2\n1 3\n2 3\n2 5\n3 4\n4 5\n",
"4 4\n1 2\n1 3\n1 4\n3 4\n"
] | [
"2\n2 3 ",
"1\n1 "
] | In the first test case there is no guest who is friend of all other guests, so at least two steps are required to perform the task. After second guest pairwise introduces all his friends, only pairs of guests (4,β1) and (4,β2) are not friends. Guest 3 or 5 can introduce them.
In the second test case guest number 1 is a friend of all guests, so he can pairwise introduce all guests in one step. | [] | 46 | 5,529,600 | 0 | 33,920 | |
85 | Guard Towers | [
"binary search",
"dsu",
"geometry",
"graphs",
"sortings"
] | E. Guard Towers | 1 | 256 | In a far away kingdom lives a very greedy king. To defend his land, he built *n* guard towers. Apart from the towers the kingdom has two armies, each headed by a tyrannical and narcissistic general. The generals can't stand each other, specifically, they will never let soldiers of two armies be present in one tower.
During defence operations to manage a guard tower a general has to send part of his army to that tower. Each general asks some fee from the king for managing towers. As they live in a really far away kingdom, each general evaluates his fee in the following weird manner: he finds two remotest (the most distant) towers, where the soldiers of his army are situated and asks for the fee equal to the distance. Each tower is represented by a point on the plane with coordinates (*x*,<=*y*), and the distance between two points with coordinates (*x*1,<=*y*1) and (*x*2,<=*y*2) is determined in this kingdom as |*x*1<=-<=*x*2|<=+<=|*y*1<=-<=*y*2|.
The greedy king was not exactly satisfied with such a requirement from the generals, that's why he only agreed to pay one fee for two generals, equal to the maximum of two demanded fees. However, the king is still green with greed, and among all the ways to arrange towers between armies, he wants to find the cheapest one. Each tower should be occupied by soldiers of exactly one army.
He hired you for that. You should find the minimum amount of money that will be enough to pay the fees. And as the king is also very scrupulous, you should also count the number of arrangements that will cost the same amount of money. As their number can be quite large, it is enough for the king to know it as a remainder from dividing by 109<=+<=7.
Two arrangements are distinct if the sets of towers occupied by soldiers of the first general are distinct. | The first line contains an integer *n* (2<=β€<=*n*<=β€<=5000), *n* is the number of guard towers. Then follow *n* lines, each of which contains two integers *x*,<=*y* β the coordinates of the *i*-th tower (0<=β€<=*x*,<=*y*<=β€<=5000). No two towers are present at one point.
Pretest 6 is one of the maximal tests for this problem. | Print on the first line the smallest possible amount of money that will be enough to pay fees to the generals.
Print on the second line the number of arrangements that can be carried out using the smallest possible fee. This number should be calculated modulo 1000000007 (109<=+<=7). | [
"2\n0 0\n1 1\n",
"4\n0 0\n0 1\n1 0\n1 1\n",
"3\n0 0\n1000 1000\n5000 5000\n"
] | [
"0\n2\n",
"1\n4\n",
"2000\n2\n"
] | In the first example there are only two towers, the distance between which is equal to 2. If we give both towers to one general, then we well have to pay 2 units of money. If each general receives a tower to manage, to fee will be equal to 0. That is the smallest possible fee. As you can easily see, we can obtain it in two ways. | [] | 46 | 0 | 0 | 34,008 |
965 | Single-use Stones | [
"binary search",
"flows",
"greedy",
"two pointers"
] | null | null | A lot of frogs want to cross a river. A river is $w$ units width, but frogs can only jump $l$ units long, where $l < w$. Frogs can also jump on lengths shorter than $l$. but can't jump longer. Hopefully, there are some stones in the river to help them.
The stones are located at integer distances from the banks. There are $a_i$ stones at the distance of $i$ units from the bank the frogs are currently at. Each stone can only be used once by one frog, after that it drowns in the water.
What is the maximum number of frogs that can cross the river, given that then can only jump on the stones? | The first line contains two integers $w$ and $l$ ($1 \le l < w \le 10^5$)Β β the width of the river and the maximum length of a frog's jump.
The second line contains $w - 1$ integers $a_1, a_2, \ldots, a_{w-1}$ ($0 \le a_i \le 10^4$), where $a_i$ is the number of stones at the distance $i$ from the bank the frogs are currently at. | Print a single integerΒ β the maximum number of frogs that can cross the river. | [
"10 5\n0 0 1 0 2 0 0 1 0\n",
"10 3\n1 1 1 1 2 1 1 1 1\n"
] | [
"3\n",
"3\n"
] | In the first sample two frogs can use the different stones at the distance $5$, and one frog can use the stones at the distances $3$ and then $8$.
In the second sample although there are two stones at the distance $5$, that does not help. The three paths are: $0 \to 3 \to 6 \to 9 \to 10$, $0 \to 2 \to 5 \to 8 \to 10$, $0 \to 1 \to 4 \to 7 \to 10$. | [
{
"input": "10 5\n0 0 1 0 2 0 0 1 0",
"output": "3"
},
{
"input": "10 3\n1 1 1 1 2 1 1 1 1",
"output": "3"
},
{
"input": "2 1\n0",
"output": "0"
},
{
"input": "2 1\n5",
"output": "5"
},
{
"input": "10 4\n0 0 6 2 7 1 6 4 0",
"output": "8"
},
{
"input": ... | 31 | 0 | 0 | 34,014 | |
63 | Bulls and Cows | [
"brute force",
"implementation"
] | C. Bulls and Cows | 2 | 256 | The "Bulls and Cows" game needs two people to play. The thinker thinks of a number and the guesser tries to guess it.
The thinker thinks of a four-digit number in the decimal system. All the digits in the number are different and the number may have a leading zero. It can't have more than one leading zero, because all it's digits should be different. The guesser tries to guess the number. He makes a series of guesses, trying experimental numbers and receives answers from the first person in the format "*x* bulls *y* cows". *x* represents the number of digits in the experimental number that occupy the same positions as in the sought number. *y* represents the number of digits of the experimental number that present in the sought number, but occupy different positions. Naturally, the experimental numbers, as well as the sought number, are represented by four-digit numbers where all digits are different and a leading zero can be present.
For example, let's suppose that the thinker thought of the number 0123. Then the guessers' experimental number 1263 will receive a reply "1 bull 2 cows" (3 occupies the same positions in both numbers and 1 and 2 are present in both numbers but they occupy different positions). Also, the answer to number 8103 will be "2 bulls 1 cow" (analogically, 1 and 3 occupy the same positions and 0 occupies a different one).
When the guesser is answered "4 bulls 0 cows", the game is over.
Now the guesser has already made several guesses and wants to know whether his next guess can possibly be the last one. | The first input line contains an integer *n* (1<=β€<=*n*<=β€<=10) which represents the number of already made guesses. Then follow *n* lines in the form of "*a**i* *b**i* *c**i*", where *a**i* is the *i*-th experimental number, *b**i* is the number of bulls, *c**i* is the number of cows (1<=β€<=*i*<=β€<=*n*, 0<=β€<=*b**i*,<=*c**i*,<=*b**i*<=+<=*c**i*<=β€<=4). The experimental numbers are correct, i.e., each of them contains exactly four digits, in each of them all the four digits are different, and there can be a leading zero. All the experimental numbers are different. As the guesser hasn't guessed the number yet, the answer "4 bulls 0 cows" is not present. | If the input data is enough to determine the sought number, print the number with four digits on a single line. If it has less than four digits, add leading zero. If the data is not enough, print "Need more data" without the quotes. If the thinker happens to have made a mistake in his replies, print "Incorrect data" without the quotes. | [
"2\n1263 1 2\n8103 2 1\n",
"2\n1234 2 2\n1256 0 2\n",
"2\n0123 1 1\n4567 1 2\n"
] | [
"Need more data",
"2134",
"Incorrect data"
] | none | [
{
"input": "2\n1263 1 2\n8103 2 1",
"output": "Need more data"
},
{
"input": "2\n1234 2 2\n1256 0 2",
"output": "2134"
},
{
"input": "2\n0123 1 1\n4567 1 2",
"output": "Incorrect data"
},
{
"input": "1\n1234 0 0",
"output": "Need more data"
},
{
"input": "4\n4789 ... | 248 | 512,000 | 3.937046 | 34,103 |
698 | Fix a Tree | [
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs",
"trees"
] | null | null | A tree is an undirected connected graph without cycles.
Let's consider a rooted undirected tree with *n* vertices, numbered 1 through *n*. There are many ways to represent such a tree. One way is to create an array with *n* integers *p*1,<=*p*2,<=...,<=*p**n*, where *p**i* denotes a parent of vertex *i* (here, for convenience a root is considered its own parent).
Given a sequence *p*1,<=*p*2,<=...,<=*p**n*, one is able to restore a tree:
1. There must be exactly one index *r* that *p**r*<==<=*r*. A vertex *r* is a root of the tree. 1. For all other *n*<=-<=1 vertices *i*, there is an edge between vertex *i* and vertex *p**i*.
A sequence *p*1,<=*p*2,<=...,<=*p**n* is called valid if the described procedure generates some (any) rooted tree. For example, for *n*<==<=3 sequences (1,2,2), (2,3,1) and (2,1,3) are not valid.
You are given a sequence *a*1,<=*a*2,<=...,<=*a**n*, not necessarily valid. Your task is to change the minimum number of elements, in order to get a valid sequence. Print the minimum number of changes and an example of a valid sequence after that number of changes. If there are many valid sequences achievable in the minimum number of changes, print any of them. | The first line of the input contains an integer *n* (2<=β€<=*n*<=β€<=200<=000)Β β the number of vertices in the tree.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=*n*). | In the first line print the minimum number of elements to change, in order to get a valid sequence.
In the second line, print any valid sequence possible to get from (*a*1,<=*a*2,<=...,<=*a**n*) in the minimum number of changes. If there are many such sequences, any of them will be accepted. | [
"4\n2 3 3 4\n",
"5\n3 2 2 5 3\n",
"8\n2 3 5 4 1 6 6 7\n"
] | [
"1\n2 3 4 4 \n",
"0\n3 2 2 5 3 \n",
"2\n2 3 7 8 1 6 6 7\n"
] | In the first sample, it's enough to change one element. In the provided output, a sequence represents a tree rooted in a vertex 4 (because *p*<sub class="lower-index">4</sub>β=β4), which you can see on the left drawing below. One of other correct solutions would be a sequence 2 3 3 2, representing a tree rooted in vertex 3 (right drawing below). On both drawings, roots are painted red.
In the second sample, the given sequence is already valid. | [
{
"input": "4\n2 3 3 4",
"output": "1\n2 3 4 4 "
},
{
"input": "5\n3 2 2 5 3",
"output": "0\n3 2 2 5 3 "
},
{
"input": "8\n2 3 5 4 1 6 6 7",
"output": "2\n2 3 7 8 1 6 6 7"
},
{
"input": "2\n1 2",
"output": "1\n2 2 "
},
{
"input": "7\n4 3 2 6 3 5 2",
"output": ... | 467 | 42,188,800 | 0 | 34,150 | |
690 | Brain Network (hard) | [
"trees"
] | null | null | Breaking news from zombie neurology! It turns out that β contrary to previous beliefs β every zombie is born with a single brain, and only later it evolves into a complicated brain structure. In fact, whenever a zombie consumes a brain, a new brain appears in its nervous system and gets immediately connected to one of the already existing brains using a single brain connector. Researchers are now interested in monitoring the brain latency of a zombie. Your task is to write a program which, given a history of evolution of a zombie's nervous system, computes its brain latency at every stage. | The first line of the input contains one number *n* β the number of brains in the final nervous system (2<=β€<=*n*<=β€<=200000). In the second line a history of zombie's nervous system evolution is given. For convenience, we number all the brains by 1,<=2,<=...,<=*n* in the same order as they appear in the nervous system (the zombie is born with a single brain, number 1, and subsequently brains 2,<=3,<=...,<=*n* are added). The second line contains *n*<=-<=1 space-separated numbers *p*2,<=*p*3,<=...,<=*p**n*, meaning that after a new brain *k* is added to the system, it gets connected to a parent-brain . | Output *n*<=-<=1 space-separated numbers β the brain latencies after the brain number *k* is added, for *k*<==<=2,<=3,<=...,<=*n*. | [
"6\n1\n2\n2\n1\n5\n"
] | [
"1 2 2 3 4 "
] | none | [
{
"input": "2\n1",
"output": "1 "
},
{
"input": "3\n1\n2",
"output": "1 2 "
},
{
"input": "10\n1\n1\n1\n1\n3\n3\n7\n5\n5",
"output": "1 2 2 2 3 3 4 5 5 "
},
{
"input": "120\n1\n1\n2\n2\n3\n3\n4\n4\n5\n5\n6\n6\n7\n7\n8\n8\n9\n9\n10\n10\n11\n11\n12\n12\n13\n13\n14\n14\n15\n15\n... | 2,000 | 62,976,000 | 0 | 34,181 | |
49 | Disposition | [
"constructive algorithms",
"math"
] | C. Disposition | 2 | 256 | Vasya bought the collected works of a well-known Berland poet Petya in *n* volumes. The volumes are numbered from 1 to *n*. He thinks that it does not do to arrange the book simply according to their order. Vasya wants to minimize the number of the dispositionβs divisors β the positive integers *i* such that for at least one *j* (1<=β€<=*j*<=β€<=*n*) is true both: *j* *mod* *i*<==<=0 and at the same time *p*(*j*) *mod* *i*<==<=0, where *p*(*j*) is the number of the tome that stands on the *j*-th place and *mod* is the operation of taking the division remainder. Naturally, one volume can occupy exactly one place and in one place can stand exactly one volume.
Help Vasya β find the volume disposition with the minimum number of divisors. | The first line contains number *n* (1<=β€<=*n*<=β€<=100000) which represents the number of volumes and free places. | Print *n* numbers β the sought disposition with the minimum divisor number. The *j*-th number (1<=β€<=*j*<=β€<=*n*) should be equal to *p*(*j*) β the number of tome that stands on the *j*-th place. If there are several solutions, print any of them. | [
"2\n",
"3\n"
] | [
"2 1 \n",
"1 3 2 \n"
] | none | [
{
"input": "2",
"output": "2 1 "
},
{
"input": "3",
"output": "1 3 2 "
},
{
"input": "4",
"output": "2 1 4 3 "
},
{
"input": "5",
"output": "1 3 2 5 4 "
},
{
"input": "6",
"output": "2 1 4 3 6 5 "
},
{
"input": "1",
"output": "1 "
},
{
"inp... | 310 | 7,987,200 | 3.907623 | 34,273 |
883 | Packmen Strike Back | [
"binary search",
"dp",
"math"
] | null | null | Game field is represented by a line of *n* square cells. In some cells there are packmen, in some cells there are asterisks and the rest of the cells are empty. Packmen eat asterisks.
Before the game starts you can choose a movement direction, left or right, for each packman. Once the game begins all the packmen simultaneously start moving according their directions. A packman can't change the given direction.
Once a packman enters a cell containing an asterisk, packman immediately eats the asterisk. Once the packman leaves the cell it becomes empty. Each packman moves at speed 1 cell per second. If a packman enters a border cell, the packman stops. Packmen do not interfere with the movement of other packmen; in one cell there can be any number of packmen moving in any directions.
Your task is to assign a direction to each packman so that they eat the maximal number of asterisks. If there are multiple ways to assign directions to eat the maximal number of asterisks, you should choose the way which minimizes the time to do that. | The first line contains integer number *n* (2<=β€<=*n*<=β€<=1<=000<=000) β the number of cells in the game field.
The second line contains *n* characters. If the *i*-th character is '.', the *i*-th cell is empty. If the *i*-th character is '*', the *i*-th cell contains an asterisk. If the *i*-th character is 'P', the *i*-th cell contains a packman.
The field contains at least one asterisk and at least one packman. | Print two integer numbers β the maximal number of asterisks packmen can eat and the minimal time to do it. | [
"6\n*.P*P*\n",
"8\n*...P..*\n"
] | [
"3 4\n",
"1 3\n"
] | In the first example the leftmost packman should move to the right, the rightmost packman should move to the left. All the asterisks will be eaten, the last asterisk will be eaten after 4 seconds. | [] | 2,245 | 268,390,400 | 0 | 34,330 | |
13 | Holes | [
"data structures",
"dsu"
] | E. Holes | 1 | 64 | Little Petya likes to play a lot. Most of all he likes to play a game Β«HolesΒ». This is a game for one person with following rules:
There are *N* holes located in a single row and numbered from left to right with numbers from 1 to *N*. Each hole has it's own power (hole number *i* has the power *a**i*). If you throw a ball into hole *i* it will immediately jump to hole *i*<=+<=*a**i*, then it will jump out of it and so on. If there is no hole with such number, the ball will just jump out of the row. On each of the *M* moves the player can perform one of two actions:
- Set the power of the hole *a* to value *b*. - Throw a ball into the hole *a* and count the number of jumps of a ball before it jump out of the row and also write down the number of the hole from which it jumped out just before leaving the row.
Petya is not good at math, so, as you have already guessed, you are to perform all computations. | The first line contains two integers *N* and *M* (1<=β€<=*N*<=β€<=105, 1<=β€<=*M*<=β€<=105) β the number of holes in a row and the number of moves. The second line contains *N* positive integers not exceeding *N* β initial values of holes power. The following *M* lines describe moves made by Petya. Each of these line can be one of the two types:
- 0 *a* *b* - 1 *a* | For each move of the type 1 output two space-separated numbers on a separate line β the number of the last hole the ball visited before leaving the row and the number of jumps it made. | [
"8 5\n1 1 1 1 1 2 8 2\n1 1\n0 1 3\n1 1\n0 3 4\n1 2\n"
] | [
"8 7\n8 5\n7 3\n"
] | none | [
{
"input": "8 5\n1 1 1 1 1 2 8 2\n1 1\n0 1 3\n1 1\n0 3 4\n1 2",
"output": "8 7\n8 5\n7 3"
},
{
"input": "10 10\n5 1 2 4 1 7 3 8 10 8\n0 5 6\n1 8\n1 1\n0 10 3\n1 5\n1 3\n1 2\n0 6 1\n1 9\n1 1",
"output": "8 1\n6 2\n5 1\n5 2\n5 3\n9 1\n10 4"
}
] | 1,000 | 102,400 | 0 | 34,335 |
128 | Birthday | [
"geometry",
"math"
] | null | null | Anna's got a birthday today. She invited many guests and cooked a huge (nearly infinite) birthday cake decorated by *n* banana circles of different sizes. Maria's birthday is about to start in 7 minutes too, and while Anna is older, she decided to play the boss a little. She told Maria to cut the cake by *k* straight-line cuts (the cutting lines can intersect) to divide banana circles into banana pieces.
Anna has many guests and she wants everyone to get at least one banana piece. That's why she told Maria to make the total number of banana pieces maximum. It's not a problem if some banana pieces end up on the same cake piece β the key is to make the maximum number of banana pieces. Determine what result Maria will achieve. | The first line contains two integers *n* and *k* β the number of banana circles and the number of cuts Maria should perform (1<=β€<=*n*<=β€<=1000, 1<=β€<=*k*<=β€<=105). Next *n* lines contain the positions and sizes of the banana circles (all banana circles are round). On the cake the Cartesian coordinate system is defined. Each line contains three integers *x*, *y* and *r* β the coordinates of the center of the corresponding banana piece and its radius (<=-<=1000<=β€<=*x*,<=*y*<=β€<=1000, 1<=β€<=*r*<=β€<=1000).
It is guaranteed that the banana circles do not intersect, do not touch each other and do not overlap with each other.
Pretest 10 is big test with *n*<==<=*k*<==<=1000. | Print the only integer β the largest number of banana pieces that Maria can get after she performs the *k* straight-line cuts.
Please do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specificator. | [
"1 1\n0 0 1\n",
"3 1\n0 0 1\n3 0 1\n6 0 1\n",
"1 3\n0 0 1\n"
] | [
"2\n",
"6\n",
"7\n"
] | none | [] | 92 | 0 | 0 | 34,368 | |
0 | none | [
"none"
] | null | null | Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are *n* flights that must depart today, the *i*-th of them is planned to depart at the *i*-th minute of the day.
Metropolis airport is the main transport hub of Metropolia, so it is difficult to keep the schedule intact. This is exactly the case today: because of technical issues, no flights were able to depart during the first *k* minutes of the day, so now the new departure schedule must be created.
All *n* scheduled flights must now depart at different minutes between (*k*<=+<=1)-th and (*k*<=+<=*n*)-th, inclusive. However, it's not mandatory for the flights to depart in the same order they were initially scheduled to do soΒ β their order in the new schedule can be different. There is only one restriction: no flight is allowed to depart earlier than it was supposed to depart in the initial schedule.
Helen knows that each minute of delay of the *i*-th flight costs airport *c**i* burles. Help her find the order for flights to depart in the new schedule that minimizes the total cost for the airport. | The first line contains two integers *n* and *k* (1<=β€<=*k*<=β€<=*n*<=β€<=300<=000), here *n* is the number of flights, and *k* is the number of minutes in the beginning of the day that the flights did not depart.
The second line contains *n* integers *c*1,<=*c*2,<=...,<=*c**n* (1<=β€<=*c**i*<=β€<=107), here *c**i* is the cost of delaying the *i*-th flight for one minute. | The first line must contain the minimum possible total cost of delaying the flights.
The second line must contain *n* different integers *t*1,<=*t*2,<=...,<=*t**n* (*k*<=+<=1<=β€<=*t**i*<=β€<=*k*<=+<=*n*), here *t**i* is the minute when the *i*-th flight must depart. If there are several optimal schedules, print any of them. | [
"5 2\n4 2 1 10 2\n"
] | [
"20\n3 6 7 4 5 \n"
] | Let us consider sample test. If Helen just moves all flights 2 minutes later preserving the order, the total cost of delaying the flights would be (3β-β1)Β·4β+β(4β-β2)Β·2β+β(5β-β3)Β·1β+β(6β-β4)Β·10β+β(7β-β5)Β·2β=β38 burles.
However, the better schedule is shown in the sample answer, its cost is (3β-β1)Β·4β+β(6β-β2)Β·2β+β(7β-β3)Β·1β+β(4β-β4)Β·10β+β(5β-β5)Β·2β=β20 burles. | [
{
"input": "5 2\n4 2 1 10 2",
"output": "20\n3 6 7 4 5 "
},
{
"input": "3 2\n3 1 2",
"output": "11\n3 5 4 "
},
{
"input": "5 5\n5 5 9 100 3",
"output": "321\n9 8 7 6 10 "
},
{
"input": "1 1\n1",
"output": "1\n2 "
},
{
"input": "1 1\n10000000",
"output": "10000... | 1,000 | 58,060,800 | 0 | 34,407 | |
898 | Alarm Clock | [
"greedy"
] | null | null | Every evening Vitalya sets *n* alarm clocks to wake up tomorrow. Every alarm clock rings during exactly one minute and is characterized by one integer *a**i*Β β number of minute after midnight in which it rings. Every alarm clock begins ringing at the beginning of the minute and rings during whole minute.
Vitalya will definitely wake up if during some *m* consecutive minutes at least *k* alarm clocks will begin ringing. Pay attention that Vitalya considers only alarm clocks which begin ringing during given period of time. He doesn't consider alarm clocks which started ringing before given period of time and continues ringing during given period of time.
Vitalya is so tired that he wants to sleep all day long and not to wake up. Find out minimal number of alarm clocks Vitalya should turn off to sleep all next day. Now all alarm clocks are turned on. | First line contains three integers *n*, *m* and *k* (1<=β€<=*k*<=β€<=*n*<=β€<=2Β·105, 1<=β€<=*m*<=β€<=106)Β β number of alarm clocks, and conditions of Vitalya's waking up.
Second line contains sequence of distinct integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=106) in which *a**i* equals minute on which *i*-th alarm clock will ring. Numbers are given in arbitrary order. Vitalya lives in a Berland in which day lasts for 106 minutes. | Output minimal number of alarm clocks that Vitalya should turn off to sleep all next day long. | [
"3 3 2\n3 5 1\n",
"5 10 3\n12 8 18 25 1\n",
"7 7 2\n7 3 4 1 6 5 2\n",
"2 2 2\n1 3\n"
] | [
"1\n",
"0\n",
"6\n",
"0\n"
] | In first example Vitalya should turn off first alarm clock which rings at minute 3.
In second example Vitalya shouldn't turn off any alarm clock because there are no interval of 10 consequence minutes in which 3 alarm clocks will ring.
In third example Vitalya should turn off any 6 alarm clocks. | [
{
"input": "3 3 2\n3 5 1",
"output": "1"
},
{
"input": "5 10 3\n12 8 18 25 1",
"output": "0"
},
{
"input": "7 7 2\n7 3 4 1 6 5 2",
"output": "6"
},
{
"input": "2 2 2\n1 3",
"output": "0"
},
{
"input": "1 4 1\n1",
"output": "1"
},
{
"input": "2 3 1\n1 2... | 623 | 14,950,400 | 3 | 34,464 | |
350 | Looking for Owls | [
"binary search",
"data structures",
"geometry",
"hashing",
"sortings"
] | null | null | Emperor Palpatine loves owls very much. The emperor has some blueprints with the new Death Star, the blueprints contain *n* distinct segments and *m* distinct circles. We will consider the segments indexed from 1 to *n* in some way and the circles β indexed from 1 to *m* in some way.
Palpatine defines an owl as a set of a pair of distinct circles (*i*,<=*j*) (*i*<=<<=*j*) and one segment *k*, such that:
1. circles *i* and *j* are symmetrical relatively to the straight line containing segment *k*; 1. circles *i* and *j* don't have any common points; 1. circles *i* and *j* have the same radius; 1. segment *k* intersects the segment that connects the centers of circles *i* and *j*.
Help Palpatine, count the number of distinct owls on the picture. | The first line contains two integers β *n* and *m* (1<=β€<=*n*<=β€<=3Β·105, 2<=β€<=*m*<=β€<=1500).
The next *n* lines contain four integers each, *x*1, *y*1, *x*2, *y*2 β the coordinates of the two endpoints of the segment. It's guaranteed that each segment has positive length.
The next *m* lines contain three integers each, *x**i*, *y**i*, *r**i* β the coordinates of the center and the radius of the *i*-th circle. All coordinates are integers of at most 104 in their absolute value. The radius is a positive integer of at most 104.
It is guaranteed that all segments and all circles are dictinct. | Print a single number β the answer to the problem.
Please, do not use the %lld specifier to output 64-bit integers is Π‘++. It is preferred to use the cout stream or the %I64d specifier. | [
"1 2\n3 2 3 -2\n0 0 2\n6 0 2\n",
"3 2\n0 0 0 1\n0 -1 0 1\n0 -1 0 0\n2 0 1\n-2 0 1\n",
"1 2\n-1 0 1 0\n-100 0 1\n100 0 1\n"
] | [
"1\n",
"3\n",
"0\n"
] | Here's an owl from the first sample. The owl is sitting and waiting for you to count it. | [] | 92 | 0 | 0 | 34,470 | |
0 | none | [
"none"
] | null | null | Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game.
Initially, Bob chooses integer *m*, bit depth of the game, which means that all numbers in the game will consist of *m* bits. Then he asks Peter to choose some *m*-bit number. After that, Bob computes the values of *n* variables. Each variable is assigned either a constant *m*-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values.
Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose. | The first line contains two integers *n* and *m*, the number of variables and bit depth, respectively (1<=β€<=*n*<=β€<=5000; 1<=β€<=*m*<=β€<=1000).
The following *n* lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of:
1. Binary number of exactly *m* bits. 1. The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter.
Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different. | In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as *m*-bit binary numbers. | [
"3 3\na := 101\nb := 011\nc := ? XOR b\n",
"5 1\na := 1\nbb := 0\ncx := ? OR a\nd := ? XOR ?\ne := d AND bb\n"
] | [
"011\n100\n",
"0\n0\n"
] | In the first sample if Peter chooses a number 011<sub class="lower-index">2</sub>, then *a*β=β101<sub class="lower-index">2</sub>,β*b*β=β011<sub class="lower-index">2</sub>,β*c*β=β000<sub class="lower-index">2</sub>, the sum of their values is 8. If he chooses the number 100<sub class="lower-index">2</sub>, then *a*β=β101<sub class="lower-index">2</sub>,β*b*β=β011<sub class="lower-index">2</sub>,β*c*β=β111<sub class="lower-index">2</sub>, the sum of their values is 15.
For the second test, the minimum and maximum sum of variables *a*, *bb*, *cx*, *d* and *e* is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0. | [
{
"input": "3 3\na := 101\nb := 011\nc := ? XOR b",
"output": "011\n100"
},
{
"input": "5 1\na := 1\nbb := 0\ncx := ? OR a\nd := ? XOR ?\ne := d AND bb",
"output": "0\n0"
},
{
"input": "2 10\nb := 0100101101\na := ? XOR b",
"output": "0100101101\n1011010010"
},
{
"input": "1 ... | 3,000 | 6,758,400 | 0 | 34,487 | |
631 | Product Sum | [
"data structures",
"dp",
"geometry"
] | null | null | Blake is the boss of Kris, however, this doesn't spoil their friendship. They often gather at the bar to talk about intriguing problems about maximising some values. This time the problem is really special.
You are given an array *a* of length *n*. The characteristic of this array is the value Β β the sum of the products of the values *a**i* by *i*. One may perform the following operation exactly once: pick some element of the array and move to any position. In particular, it's allowed to move the element to the beginning or to the end of the array. Also, it's allowed to put it back to the initial position. The goal is to get the array with the maximum possible value of characteristic. | The first line of the input contains a single integer *n* (2<=β€<=*n*<=β€<=200<=000)Β β the size of the array *a*.
The second line contains *n* integers *a**i* (1<=β€<=*i*<=β€<=*n*, |*a**i*|<=β€<=1<=000<=000)Β β the elements of the array *a*. | Print a single integer β the maximum possible value of characteristic of *a* that can be obtained by performing no more than one move. | [
"4\n4 3 2 5\n",
"5\n1 1 2 7 1\n",
"3\n1 1 2\n"
] | [
"39",
"49",
"9"
] | In the first sample, one may pick the first element and place it before the third (before 5). Thus, the answer will be 3Β·1β+β2Β·2β+β4Β·3β+β5Β·4β=β39.
In the second sample, one may pick the fifth element of the array and place it before the third. The answer will be 1Β·1β+β1Β·2β+β1Β·3β+β2Β·4β+β7Β·5β=β49. | [
{
"input": "4\n4 3 2 5",
"output": "39"
},
{
"input": "5\n1 1 2 7 1",
"output": "49"
},
{
"input": "3\n1 1 2",
"output": "9"
},
{
"input": "5\n1 2 3 4 5",
"output": "55"
},
{
"input": "5\n-1 -2 -3 -4 -5",
"output": "-45"
},
{
"input": "4\n0 0 0 0",
... | 577 | 46,080,000 | 0 | 34,520 | |
76 | Gift | [
"dsu",
"graphs",
"sortings",
"trees"
] | A. Gift | 2 | 256 | The kingdom of Olympia consists of *N* cities and *M* bidirectional roads. Each road connects exactly two cities and two cities can be connected with more than one road. Also it possible that some roads connect city with itself making a loop.
All roads are constantly plundered with bandits. After a while bandits became bored of wasting time in road robberies, so they suggested the king of Olympia to pay off. According to the offer, bandits want to get a gift consisted of gold and silver coins. Offer also contains a list of restrictions: for each road it is known *g**i* β the smallest amount of gold and *s**i* β the smallest amount of silver coins that should be in the gift to stop robberies on the road. That is, if the gift contains *a* gold and *b* silver coins, then bandits will stop robberies on all the roads that *g**i*<=β€<=*a* and *s**i*<=β€<=*b*.
Unfortunately kingdom treasury doesn't contain neither gold nor silver coins, but there are Olympian tugriks in it. The cost of one gold coin in tugriks is *G*, and the cost of one silver coin in tugriks is *S*. King really wants to send bandits such gift that for any two cities there will exist a safe path between them. Your task is to find the minimal cost in Olympian tugriks of the required gift. | The first line of the input contains two integers *N* and *M* (2<=β€<=*N*<=β€<=200, 1<=β€<=*M*<=β€<=50<=000) β the number of cities and the number of roads, respectively. The second line contains two integers *G* and *S* (1<=β€<=*G*,<=*S*<=β€<=109) β the prices of gold and silver coins in tugriks. The following *M* lines contain information about the offer. Each of the records in list is given as four integers *x**i*,<=*y**i*,<=*g**i*,<=*s**i*, where *x**i* and *y**i* are the numbers of cities that the road connects and *g**i*, *s**i* are minimal gold and silver coins requirements for the *i*-th road (1<=β€<=*x**i*,<=*y**i*<=β€<=*N*, 1<=β€<=*g**i*,<=*s**i*<=β€<=109). Cities are numbered from 1 to *N*. It is possible that there are more than one road between a pair of cities. It is possible that a road connects the city with itself. | The output should contain the minimal cost of the gift in Olympian tugriks. If there is no gift that satisfies the given requirements output . | [
"3 3\n2 1\n1 2 10 15\n1 2 4 20\n1 3 5 1\n"
] | [
"30\n"
] | none | [
{
"input": "3 3\n2 1\n1 2 10 15\n1 2 4 20\n1 3 5 1",
"output": "30"
}
] | 0 | 0 | -1 | 34,523 |
213 | Stars | [
"constructive algorithms",
"geometry"
] | null | null | Furik loves painting stars. A star is a shape that results if we take a regular pentagon and paint all diagonals in it.
Recently he decided to teach Rubik to paint stars. After many years of training Rubik could paint stars easily. But now Furik decided to test Rubik and complicated the task. Rubik must paint *n* stars, observing the following rules:
- all stars must be painted in a single move (i.e. it is forbidden to take the pen away from the paper); - it is forbidden to paint the same segment of non-zero length more than once; - the stars can intersect only in their vertexes; - the length of a side of the regular pentagon, in which Rubik paints each star, must equal 10.
Help Rubik to cope with this hard task. | A single line contains an integer (1<=β€<=*n*<=β€<=100) β the number of stars to paint. | On the first line print an integer *m* (1<=β€<=*m*<=β€<=5Β·*n*). On the next *m* lines print coordinates of *m* distinct points with accuracy of at least 9 and at most 100 digits after decimal point. All coordinates should not exceed 5000 in their absolute value. On each of the next *n* lines print 5 integers β the indexes of the points that form the given star in the clockwise or counterclockwise order. On the next line print 5Β·*n*<=+<=1 integers β the numbers of points in the order, in which Rubik paints stars. That is, if number with index *i* is *a**i*, and number with index *i*<=+<=1 is *a**i*<=+<=1, then points with indexes *a**i* and *a**i*<=+<=1 will have a segment painted between them.
You can consider all *m* printed points indexed from 1 to *m* in the order, in which they occur in the output. Separate the numbers on the lines with whitespaces.
Note that the answer has an imprecise validation. Try to obtain as accurate a solution as possible. The validator performs all calculations considering that the absolute error of a participant's answer is not more than 10<=-<=8. | [
"1\n"
] | [
"5\n3.830127018922193 3.366025403784439\n-3.601321235851749 10.057331467373021\n0.466045194906253 19.192786043799030\n10.411264148588986 18.147501411122495\n12.490381056766580 8.366025403784439\n1 2 3 4 5\n1 3 5 2 4 1\n"
] | The initial position of points in the sample is:
The order in which Rubik can paint segments is: | [] | 92 | 0 | 0 | 34,610 | |
0 | none | [
"none"
] | null | null | Appleman has a tree with *n* vertices. Some of the vertices (at least one) are colored black and other vertices are colored white.
Consider a set consisting of *k* (0<=β€<=*k*<=<<=*n*) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (*k*<=+<=1) parts. Note, that each part will be a tree with colored vertices.
Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109<=+<=7). | The first line contains an integer *n* (2<=<=β€<=*n*<=β€<=105) β the number of tree vertices.
The second line contains the description of the tree: *n*<=-<=1 integers *p*0,<=*p*1,<=...,<=*p**n*<=-<=2 (0<=β€<=*p**i*<=β€<=*i*). Where *p**i* means that there is an edge connecting vertex (*i*<=+<=1) of the tree and vertex *p**i*. Consider tree vertices are numbered from 0 to *n*<=-<=1.
The third line contains the description of the colors of the vertices: *n* integers *x*0,<=*x*1,<=...,<=*x**n*<=-<=1 (*x**i* is either 0 or 1). If *x**i* is equal to 1, vertex *i* is colored black. Otherwise, vertex *i* is colored white. | Output a single integer β the number of ways to split the tree modulo 1000000007 (109<=+<=7). | [
"3\n0 0\n0 1 1\n",
"6\n0 1 1 0 4\n1 1 0 0 1 0\n",
"10\n0 1 2 1 4 4 4 0 8\n0 0 0 1 0 1 1 0 0 1\n"
] | [
"2\n",
"1\n",
"27\n"
] | none | [
{
"input": "3\n0 0\n0 1 1",
"output": "2"
},
{
"input": "6\n0 1 1 0 4\n1 1 0 0 1 0",
"output": "1"
},
{
"input": "10\n0 1 2 1 4 4 4 0 8\n0 0 0 1 0 1 1 0 0 1",
"output": "27"
},
{
"input": "5\n0 1 1 3\n0 0 0 1 1",
"output": "1"
},
{
"input": "10\n0 1 1 2 4 3 3 3 2\... | 46 | 0 | 0 | 34,623 | |
913 | Party Lemonade | [
"bitmasks",
"dp",
"greedy"
] | null | null | A New Year party is not a New Year party without lemonade! As usual, you are expecting a lot of guests, and buying lemonade has already become a pleasant necessity.
Your favorite store sells lemonade in bottles of *n* different volumes at different costs. A single bottle of type *i* has volume 2*i*<=-<=1 liters and costs *c**i* roubles. The number of bottles of each type in the store can be considered infinite.
You want to buy at least *L* liters of lemonade. How many roubles do you have to spend? | The first line contains two integers *n* and *L* (1<=β€<=*n*<=β€<=30; 1<=β€<=*L*<=β€<=109)Β β the number of types of bottles in the store and the required amount of lemonade in liters, respectively.
The second line contains *n* integers *c*1,<=*c*2,<=...,<=*c**n* (1<=β€<=*c**i*<=β€<=109)Β β the costs of bottles of different types. | Output a single integerΒ β the smallest number of roubles you have to pay in order to buy at least *L* liters of lemonade. | [
"4 12\n20 30 70 90\n",
"4 3\n10000 1000 100 10\n",
"4 3\n10 100 1000 10000\n",
"5 787787787\n123456789 234567890 345678901 456789012 987654321\n"
] | [
"150\n",
"10\n",
"30\n",
"44981600785557577\n"
] | In the first example you should buy one 8-liter bottle for 90 roubles and two 2-liter bottles for 30 roubles each. In total you'll get 12 liters of lemonade for just 150 roubles.
In the second example, even though you need only 3 liters, it's cheaper to buy a single 8-liter bottle for 10 roubles.
In the third example it's best to buy three 1-liter bottles for 10 roubles each, getting three liters for 30 roubles. | [
{
"input": "4 12\n20 30 70 90",
"output": "150"
},
{
"input": "4 3\n10000 1000 100 10",
"output": "10"
},
{
"input": "4 3\n10 100 1000 10000",
"output": "30"
},
{
"input": "5 787787787\n123456789 234567890 345678901 456789012 987654321",
"output": "44981600785557577"
},... | 124 | 307,200 | 3 | 34,666 | |
74 | Shift It! | [
"constructive algorithms"
] | E. Shift It! | 2 | 256 | There is a square box 6<=Γ<=6 in size. It contains 36 chips 1<=Γ<=1 in size. Those chips contain 36 different characters β "0"-"9" and "A"-"Z". There is exactly one chip with each character.
You are allowed to make the following operations: you may choose one of 6 rows or one of 6 columns and cyclically shift the chips there to one position to the left or to the right (for the row) or upwards or downwards (for the column). Those operations are allowed to perform several times.
To solve the puzzle is to shift the chips using the above described operations so that they were written in the increasing order (exactly equal to the right picture). An example of solving the puzzle is shown on a picture below.
Write a program that finds the sequence of operations that solves the puzzle. That sequence should not necessarily be shortest, but you should not exceed the limit of 10000 operations. It is guaranteed that the solution always exists. | The input data are represented by 6 lines containing 6 characters each. They are the puzzle's initial position. Those lines contain each character from the string "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" exactly once. | On the first line print number *n*, which is the number of operations. On the next *n* lines print the sequence of operations one per line. An operation is described by a word consisting of two characters. The first character shows the direction where the row or the column will be shifted. The possible directions are "L", "R" (to the left, to the right correspondingly, we shift a row), "U", "D" (upwards, downwards correspondingly, we shift a column). The second character is the number of the row (or the column), it is an integer from "1" to "6". The rows are numbered from the top to the bottom, the columns are numbered from the left to the right.
The number of operations should not exceed 104. If there are several solutions, print any of them. | [
"01W345\n729AB6\nCD8FGH\nIJELMN\nOPKRST\nUVQXYZ\n"
] | [
"2\nR2\nU3\n"
] | none | [
{
"input": "01W345\n729AB6\nCD8FGH\nIJELMN\nOPKRST\nUVQXYZ",
"output": "260\nD2\nL2\nD2\nR2\nD2\nL2\nD2\nR2\nD2\nL2\nD2\nR2\nD2\nR1\nU3\nR1\nD3\nR1\nU3\nR1\nD3\nR1\nU3\nR1\nD3\nR1\nD5\nL2\nD5\nR2\nD5\nL2\nD5\nR2\nD5\nL2\nD5\nR2\nD5\nD4\nL2\nD4\nR2\nD4\nL2\nD4\nR2\nD4\nL2\nD4\nR2\nD4\nD3\nL2\nD3\nR2\nD3\nL2\... | 404 | 3,891,200 | 3.891752 | 34,689 |
37 | Computer Game | [
"greedy",
"implementation"
] | B. Computer Game | 1 | 256 | Vasyaβs elder brother Petya loves playing computer games. In one of his favourite computer games Petya reached the final level where a fight with the boss take place.
While playing the game Petya found spell scrolls and now he is about to use them. Letβs describe the way fighting goes on this level:
1) The boss has two parameters: *max* β the initial amount of health and *reg* β regeneration rate per second.
2) Every scroll also has two parameters: *pow**i* β spell power measured in percents β the maximal amount of health counted off the initial one, which allows to use the scroll (i.e. if the boss has more than *pow**i* percent of health the scroll cannot be used); and *dmg**i* the damage per second inflicted upon the boss if the scroll is used. As soon as a scroll is used it disappears and another spell is cast upon the boss that inflicts *dmg**i* of damage per second upon him until the end of the game.
During the battle the actions per second are performed in the following order: first the boss gets the damage from all the spells cast upon him, then he regenerates *reg* of health (at the same time he canβt have more than *max* of health), then the player may use another scroll (no more than one per second).
The boss is considered to be defeated if at the end of a second he has nonpositive (<=β€<=0) amount of health.
Help Petya to determine whether he can win with the set of scrolls available to him and if he can, determine the minimal number of seconds he needs to do it. | The first line contains three integers *N*, *max* and *reg* (1<=β€<=*N*,<=*max*,<=*reg*<=β€<=1000) ββ the amount of scrolls and the parameters of the boss. The next *N* lines contain two integers *pow**i* and *dmg**i* each β the parameters of the *i*-th scroll (0<=β€<=*pow**i*<=β€<=100, 1<=β€<=*dmg**i*<=β€<=2000). | In case Petya canβt complete this level, output in the single line NO.
Otherwise, output on the first line YES. On the second line output the minimal time after which the boss can be defeated and the number of used scrolls. In the next lines for each used scroll output space-separated number of seconds passed from the start of the battle to the moment the scroll was used and the number of the scroll. Scrolls are numbered starting from 1 in the input order. The first scroll is considered to be available to be used after 0 seconds.
Output scrolls in the order they were used. It is not allowed to use scrolls after the boss is defeated. | [
"2 10 3\n100 3\n99 1\n",
"2 100 10\n100 11\n90 9\n"
] | [
"NO\n",
"YES\n19 2\n0 1\n10 2\n"
] | none | [
{
"input": "2 10 3\n100 3\n99 1",
"output": "NO"
},
{
"input": "2 100 10\n100 11\n90 9",
"output": "YES\n19 2\n0 1\n10 2"
},
{
"input": "10 100 5\n61 3\n55 2\n12 6\n39 5\n21 10\n39 7\n16 1\n10 1\n70 5\n100 7",
"output": "YES\n21 6\n0 10\n15 9\n17 1\n18 2\n19 6\n20 5"
},
{
"in... | 46 | 0 | 0 | 34,733 |
581 | Luxurious Houses | [
"implementation",
"math"
] | null | null | The capital of Berland has *n* multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in *n* questions, *i*-th of them is about the following: "how many floors should be added to the *i*-th house to make it luxurious?" (for all *i* from 1 to *n*, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house *i* does not affect other answers (i.e., the floors to the houses are not actually added). | The first line of the input contains a single number *n* (1<=β€<=*n*<=β€<=105) β the number of houses in the capital of Berland.
The second line contains *n* space-separated positive integers *h**i* (1<=β€<=*h**i*<=β€<=109), where *h**i* equals the number of floors in the *i*-th house. | Print *n* integers *a*1,<=*a*2,<=...,<=*a**n*, where number *a**i* is the number of floors that need to be added to the house number *i* to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then *a**i* should be equal to zero.
All houses are numbered from left to right, starting from one. | [
"5\n1 2 3 1 2\n",
"4\n3 2 1 4\n"
] | [
"3 2 0 2 0 ",
"2 3 4 0 "
] | none | [
{
"input": "5\n1 2 3 1 2",
"output": "3 2 0 2 0 "
},
{
"input": "4\n3 2 1 4",
"output": "2 3 4 0 "
},
{
"input": "1\n2",
"output": "0 "
},
{
"input": "2\n5 4",
"output": "0 0 "
},
{
"input": "5\n10 18 36 33 20",
"output": "27 19 0 0 0 "
},
{
"input": "... | 1,000 | 12,902,400 | 0 | 34,739 | |
95 | Volleyball | [
"shortest paths"
] | C. Volleyball | 2 | 256 | Petya loves volleyball very much. One day he was running late for a volleyball match. Petya hasn't bought his own car yet, that's why he had to take a taxi. The city has *n* junctions, some of which are connected by two-way roads. The length of each road is defined by some positive integer number of meters; the roads can have different lengths.
Initially each junction has exactly one taxi standing there. The taxi driver from the *i*-th junction agrees to drive Petya (perhaps through several intermediate junctions) to some other junction if the travel distance is not more than *t**i* meters. Also, the cost of the ride doesn't depend on the distance and is equal to *c**i* bourles. Taxis can't stop in the middle of a road. Each taxi can be used no more than once. Petya can catch taxi only in the junction, where it stands initially.
At the moment Petya is located on the junction *x* and the volleyball stadium is on the junction *y*. Determine the minimum amount of money Petya will need to drive to the stadium. | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=1000,<=0<=β€<=*m*<=β€<=1000). They are the number of junctions and roads in the city correspondingly. The junctions are numbered from 1 to *n*, inclusive. The next line contains two integers *x* and *y* (1<=β€<=*x*,<=*y*<=β€<=*n*). They are the numbers of the initial and final junctions correspondingly. Next *m* lines contain the roads' description. Each road is described by a group of three integers *u**i*, *v**i*, *w**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*,<=1<=β€<=*w**i*<=β€<=109) β they are the numbers of the junctions connected by the road and the length of the road, correspondingly. The next *n* lines contain *n* pairs of integers *t**i* and *c**i* (1<=β€<=*t**i*,<=*c**i*<=β€<=109), which describe the taxi driver that waits at the *i*-th junction β the maximum distance he can drive and the drive's cost. The road can't connect the junction with itself, but between a pair of junctions there can be more than one road. All consecutive numbers in each line are separated by exactly one space character. | If taxis can't drive Petya to the destination point, print "-1" (without the quotes). Otherwise, print the drive's minimum cost.
Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator. | [
"4 4\n1 3\n1 2 3\n1 4 1\n2 4 1\n2 3 5\n2 7\n7 2\n1 2\n7 7\n"
] | [
"9\n"
] | An optimal way β ride from the junction 1 to 2 (via junction 4), then from 2 to 3. It costs 7+2=9 bourles. | [
{
"input": "4 4\n1 3\n1 2 3\n1 4 1\n2 4 1\n2 3 5\n2 7\n7 2\n1 2\n7 7",
"output": "9"
},
{
"input": "3 3\n1 3\n1 2 2\n1 3 3\n3 2 1\n2 7\n2 7\n3 6",
"output": "14"
},
{
"input": "3 1\n1 3\n1 2 2\n2 7\n2 7\n3 6",
"output": "-1"
},
{
"input": "3 2\n3 3\n1 2 2\n1 3 3\n2 7\n2 7\n3 ... | 966 | 28,057,600 | 3.706239 | 34,742 |
301 | Yaroslav and Algorithm | [
"constructive algorithms"
] | null | null | Yaroslav likes algorithms. We'll describe one of his favorite algorithms.
1. The algorithm receives a string as the input. We denote this input string as *a*. 1. The algorithm consists of some number of command. Π‘ommand number *i* looks either as *s**i* >> *w**i*, or as *s**i* <> *w**i*, where *s**i* and *w**i* are some possibly empty strings of length at most 7, consisting of digits and characters "?". 1. At each iteration, the algorithm looks for a command with the minimum index *i*, such that *s**i* occurs in *a* as a substring. If this command is not found the algorithm terminates. 1. Let's denote the number of the found command as *k*. In string *a* the first occurrence of the string *s**k* is replaced by string *w**k*. If the found command at that had form *s**k* >> *w**k*, then the algorithm continues its execution and proceeds to the next iteration. Otherwise, the algorithm terminates. 1. The value of string *a* after algorithm termination is considered to be the output of the algorithm.
Yaroslav has a set of *n* positive integers, he needs to come up with his favorite algorithm that will increase each of the given numbers by one. More formally, if we consider each number as a string representing the decimal representation of the number, then being run on each of these strings separately, the algorithm should receive the output string that is a recording of the corresponding number increased by one.
Help Yaroslav. | The first line contains integer *n* (1<=β€<=*n*<=β€<=100) β the number of elements in the set. The next *n* lines contains one positive integer each. All the given numbers are less than 1025. | Print the algorithm which can individually increase each number of the set. In the *i*-th line print the command number *i* without spaces.
Your algorithm will be launched for each of these numbers. The answer will be considered correct if: Β
- Each line will a correct algorithm command (see the description in the problem statement). - The number of commands should not exceed 50. - The algorithm will increase each of the given numbers by one. - To get a respond, the algorithm will perform no more than 200 iterations for each number. | [
"2\n10\n79\n"
] | [
"10<>11\n79<>80\n"
] | none | [
{
"input": "2\n10\n79",
"output": "10<>11\n79<>80"
},
{
"input": "5\n9\n99\n999\n9999\n99999",
"output": "0??<>1\n1??<>2\n2??<>3\n3??<>4\n4??<>5\n5??<>6\n6??<>7\n7??<>8\n8??<>9\n9??>>??0\n??<>1\n?0>>0?\n?1>>1?\n?2>>2?\n?3>>3?\n?4>>4?\n?5>>5?\n?6>>6?\n?7>>7?\n?8>>8?\n?9>>9?\n?>>??\n>>?"
},
{
... | 154 | 20,172,800 | 0 | 34,864 | |
986 | AND Graph | [
"bitmasks",
"dfs and similar",
"dsu",
"graphs"
] | null | null | You are given a set of size $m$ with integer elements between $0$ and $2^{n}-1$ inclusive. Let's build an undirected graph on these integers in the following way: connect two integers $x$ and $y$ with an edge if and only if $x \& y = 0$. Here $\&$ is the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). Count the number of connected components in that graph. | In the first line of input there are two integers $n$ and $m$ ($0 \le n \le 22$, $1 \le m \le 2^{n}$).
In the second line there are $m$ integers $a_1, a_2, \ldots, a_m$ ($0 \le a_{i} < 2^{n}$)Β β the elements of the set. All $a_{i}$ are distinct. | Print the number of connected components. | [
"2 3\n1 2 3\n",
"5 5\n5 19 10 20 12\n"
] | [
"2\n",
"2\n"
] | Graph from first sample:
<img class="tex-graphics" src="https://espresso.codeforces.com/2ae9959ec95b178e60e27e28f5d62c1099bb9ef1.png" style="max-width: 100.0%;max-height: 100.0%;"/>
Graph from second sample:
<img class="tex-graphics" src="https://espresso.codeforces.com/9f9eee8f2409bbd78cce726875915c405af8a07e.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "2 3\n1 2 3",
"output": "2"
},
{
"input": "5 5\n5 19 10 20 12",
"output": "2"
},
{
"input": "3 5\n3 5 0 6 7",
"output": "1"
},
{
"input": "0 1\n0",
"output": "1"
},
{
"input": "1 1\n1",
"output": "1"
},
{
"input": "1 1\n0",
"output": "1"... | 2,277 | 268,390,400 | 0 | 34,885 | |
549 | Degenerate Matrix | [
"binary search",
"math"
] | null | null | The determinant of a matrix 2<=Γ<=2 is defined as follows:
A matrix is called degenerate if its determinant is equal to zero.
The norm ||*A*|| of a matrix *A* is defined as a maximum of absolute values of its elements.
You are given a matrix . Consider any degenerate matrix *B* such that norm ||*A*<=-<=*B*|| is minimum possible. Determine ||*A*<=-<=*B*||. | The first line contains two integers *a* and *b* (|*a*|,<=|*b*|<=β€<=109), the elements of the first row of matrix *A*.
The second line contains two integers *c* and *d* (|*c*|,<=|*d*|<=β€<=109) the elements of the second row of matrix *A*. | Output a single real number, the minimum possible value of ||*A*<=-<=*B*||. Your answer is considered to be correct if its absolute or relative error does not exceed 10<=-<=9. | [
"1 2\n3 4\n",
"1 0\n0 1\n"
] | [
"0.2000000000\n",
"0.5000000000\n"
] | In the first sample matrix *B* is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ce214ad27bde5d77f87492eedd74d34c745f72a1.png" style="max-width: 100.0%;max-height: 100.0%;"/>
In the second sample matrix *B* is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/a07c34fbc6e9328bcb519d3f780eea6e02e5dc87.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "1 2\n3 4",
"output": "0.2000000000"
},
{
"input": "1 0\n0 1",
"output": "0.5000000000"
},
{
"input": "1000000000 0\n0 1000000000",
"output": "500000000.0000000000"
},
{
"input": "8205 9482\n11 -63",
"output": "35.0198432832"
},
{
"input": "0 0\n0 0",
... | 46 | 0 | -1 | 34,937 | |
884 | Binary Matrix | [
"dsu"
] | null | null | You are given a matrix of size *n*<=Γ<=*m*. Each element of the matrix is either 1 or 0. You have to determine the number of connected components consisting of 1's. Two cells belong to the same component if they have a common border, and both elements in these cells are 1's.
Note that the memory limit is unusual! | The first line contains two numbers *n* and *m* (1<=β€<=*n*<=β€<=212, 4<=β€<=*m*<=β€<=214) β the number of rows and columns, respectively. It is guaranteed that *m* is divisible by 4.
Then the representation of matrix follows. Each of *n* next lines contains one-digit hexadecimal numbers (that is, these numbers can be represented either as digits from 0 to 9 or as uppercase Latin letters from *A* to *F*). Binary representation of each of these numbers denotes next 4 elements of the matrix in the corresponding row. For example, if the number *B* is given, then the corresponding elements are 1011, and if the number is 5, then the corresponding elements are 0101.
Elements are not separated by whitespaces. | Print the number of connected components consisting of 1's. | [
"3 4\n1\nA\n8\n",
"2 8\n5F\nE3\n",
"1 4\n0\n"
] | [
"3\n",
"2\n",
"0\n"
] | In the first example the matrix is:
It is clear that it has three components.
The second example:
It is clear that the number of components is 2.
There are no 1's in the third example, so the answer is 0. | [
{
"input": "3 4\n1\nA\n8",
"output": "3"
},
{
"input": "2 8\n5F\nE3",
"output": "2"
},
{
"input": "1 4\n0",
"output": "0"
},
{
"input": "1 4\nD",
"output": "2"
},
{
"input": "10 120\n4100B3BC23752433106B89343D9BA9\nD0412141283A93738E2805121044D9\nB111606365A975606... | 108 | 716,800 | 0 | 35,009 | |
323 | Black-and-White Cube | [
"combinatorics",
"constructive algorithms"
] | null | null | You are given a cube of size *k*<=Γ<=*k*<=Γ<=*k*, which consists of unit cubes. Two unit cubes are considered neighbouring, if they have common face.
Your task is to paint each of *k*3 unit cubes one of two colours (black or white), so that the following conditions must be satisfied:
- each white cube has exactly 2 neighbouring cubes of white color; - each black cube has exactly 2 neighbouring cubes of black color. | The first line contains integer *k* (1<=β€<=*k*<=β€<=100), which is size of the cube. | Print -1 if there is no solution. Otherwise, print the required painting of the cube consequently by layers. Print a *k*<=Γ<=*k* matrix in the first *k* lines, showing how the first layer of the cube should be painted. In the following *k* lines print a *k*<=Γ<=*k* matrix β the way the second layer should be painted. And so on to the last *k*-th layer. Note that orientation of the cube in the space does not matter.
Mark a white unit cube with symbol "w" and a black one with "b". Use the format of output data, given in the test samples. You may print extra empty lines, they will be ignored. | [
"1\n",
"2\n"
] | [
"-1\n",
"bb\nww\n\nbb\nww\n"
] | none | [
{
"input": "1",
"output": "-1"
},
{
"input": "2",
"output": "bb\nww\n\nbb\nww"
},
{
"input": "3",
"output": "-1"
},
{
"input": "4",
"output": "bbbb\nbwwb\nbwwb\nbbbb\n\nwwww\nwbbw\nwbbw\nwwww\n\nbbbb\nbwwb\nbwwb\nbbbb\n\nwwww\nwbbw\nwbbw\nwwww"
},
{
"input": "5",
... | 30 | 0 | 0 | 35,020 | |
929 | ΠΠΎΠ³ΡΠ°Π½ΠΈΡΠ½ΡΠ΅ Π²ΡΠ°ΡΠ° | [] | null | null | ΠΠ΅ΡΠΎΠΉ ΠΡΠΊΠ°Π΄ΠΈΠΉ Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡ Π½Π° ΡΠ·ΠΊΠΎΠΉ ΠΏΠΎΠ»ΠΎΡΠΊΠ΅ Π·Π΅ΠΌΠ»ΠΈ, ΡΠ°Π·Π΄Π΅Π»Π΅Π½Π½ΠΎΠΉ Π½Π° *n* Π·ΠΎΠ½, ΠΏΡΠΎΠ½ΡΠΌΠ΅ΡΠΎΠ²Π°Π½Π½ΡΡ
ΠΎΡ 1 Π΄ΠΎ *n*. ΠΠ· *i*-ΠΉ Π·ΠΎΠ½Ρ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡΠΎΠΉΡΠΈ Π»ΠΈΡΡ Π² (*i*<=-<=1)-Ρ Π·ΠΎΠ½Ρ ΠΈ Π² (*i*<=+<=1)-Ρ Π·ΠΎΠ½Ρ, Π΅ΡΠ»ΠΈ ΠΎΠ½ΠΈ ΡΡΡΠ΅ΡΡΠ²ΡΡΡ. ΠΡΠΈ ΡΡΠΎΠΌ ΠΌΠ΅ΠΆΠ΄Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΏΠ°ΡΠΎΠΉ ΡΠΎΡΠ΅Π΄Π½ΠΈΡ
Π·ΠΎΠ½ Π½Π°Ρ
ΠΎΠ΄ΡΡΡΡ ΠΏΠΎΠ³ΡΠ°Π½ΠΈΡΠ½ΡΠ΅ Π²ΡΠ°ΡΠ°, ΠΊΠΎΡΠΎΡΡΠ΅ ΠΌΠΎΠ³ΡΡ Π±ΡΡΡ ΡΠ°Π·Π½ΡΡ
ΡΠ²Π΅ΡΠΎΠ², ΡΠ²Π΅Ρ Π²ΡΠ°Ρ ΠΌΠ΅ΠΆΠ΄Ρ *i*-ΠΉ ΠΈ (*i*<=+<=1)-ΠΉ Π·ΠΎΠ½ΠΎΠΉ ΡΠ°Π²Π΅Π½ *g**i*.
ΠΡΠΊΠ°Π΄ΠΈΠΉ ΠΌΠΎΠΆΠ΅Ρ ΠΏΡΠΎΠΉΡΠΈ ΠΏΠΎΠ³ΡΠ°Π½ΠΈΡΠ½ΡΠ΅ Π²ΡΠ°ΡΠ° Π½Π΅ΠΊΠΎΡΠΎΡΠΎΠ³ΠΎ ΡΠ²Π΅ΡΠ°, ΡΠΎΠ»ΡΠΊΠΎ Π΅ΡΠ»ΠΈ ΠΎΠ½ ΠΏΠ΅ΡΠ΅Π΄ ΡΡΠΈΠΌ ΠΏΠΎΠ±ΡΠ²Π°Π» Π² ΠΎΠ΄Π½ΠΎΠΌ ΠΈΠ· ΡΠ°ΡΡΠΎΠ² Ρ
ΡΠ°Π½ΠΈΡΠ΅Π»Π΅ΠΉ ΠΊΠ»ΡΡΠ΅ΠΉ ΡΡΠΎΠ³ΠΎ ΡΠ²Π΅ΡΠ° ΠΈ Π²Π·ΡΠ» ΠΊΠ»ΡΡ. Π ΠΊΠ°ΠΆΠ΄ΠΎΠΉ Π·ΠΎΠ½Π΅ Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡ ΡΠΎΠ²Π½ΠΎ ΠΎΠ΄ΠΈΠ½ ΡΠ°ΡΠ΅Ρ Ρ
ΡΠ°Π½ΠΈΡΠ΅Π»Ρ ΠΊΠ»ΡΡΠ΅ΠΉ Π½Π΅ΠΊΠΎΡΠΎΡΠΎΠ³ΠΎ ΡΠ²Π΅ΡΠ°, ΡΠ²Π΅Ρ ΡΠ°ΡΡΠ° Π² *i*-ΠΉ Π·ΠΎΠ½Π΅ ΡΠ°Π²Π΅Π½ *k**i*. ΠΠΎΡΠ»Π΅ ΠΏΠΎΡΠ΅ΡΠ΅Π½ΠΈΡ ΡΠ°ΡΡΠ° ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½Π½ΠΎΠ³ΠΎ ΡΠ²Π΅ΡΠ° ΠΡΠΊΠ°Π΄ΠΈΠΉ ΠΌΠΎΠΆΠ΅Ρ Π½Π΅ΠΎΠ³ΡΠ°Π½ΠΈΡΠ΅Π½Π½ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ ΡΠ°Π· ΠΏΡΠΎΡ
ΠΎΠ΄ΠΈΡΡ ΡΠ΅ΡΠ΅Π· Π»ΡΠ±ΡΠ΅ Π²ΡΠ°ΡΠ° ΡΡΠΎΠ³ΠΎ ΡΠ²Π΅ΡΠ°.
ΠΠ° ΠΏΡΠΎΡ
ΠΎΠ΄ ΡΠ΅ΡΠ΅Π· ΠΎΠ΄Π½ΠΈ Π²ΡΠ°ΡΠ° ΠΡΠΊΠ°Π΄ΠΈΠΉ ΡΡΠ°ΡΠΈΡ ΠΎΠ΄ΠΈΠ½ Ρ
ΠΎΠ΄, Π½Π° ΠΏΠΎΡΠ΅ΡΠ΅Π½ΠΈΠ΅ ΡΠ°ΡΡΠ° ΠΈ Π΄ΡΡΠ³ΠΈΠ΅ ΠΏΠ΅ΡΠ΅ΠΌΠ΅ΡΠ΅Π½ΠΈΡ Ρ
ΠΎΠ΄Ρ Π½Π΅ ΡΡΠ΅Π±ΡΡΡΡΡ. ΠΠ° ΠΊΠ°ΠΊΠΎΠ΅ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ Ρ
ΠΎΠ΄ΠΎΠ² ΠΡΠΊΠ°Π΄ΠΈΠΉ ΠΌΠΎΠΆΠ΅Ρ ΠΏΠΎΠΏΠ°ΡΡΡ ΠΈΠ· Π·ΠΎΠ½Ρ *a* Π² Π·ΠΎΠ½Ρ *b*, Π΅ΡΠ»ΠΈ ΠΈΠ·Π½Π°ΡΠ°Π»ΡΠ½ΠΎ Ρ Π½Π΅Π³ΠΎ Π½Π΅Ρ Π½ΠΈΠΊΠ°ΠΊΠΈΡ
ΠΊΠ»ΡΡΠ΅ΠΉ? | ΠΠ΅ΡΠ²Π°Ρ ΡΡΡΠΎΠΊΠ° ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ ΡΡΠΈ ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ»Π° *n*, *a*, *b* (2<=β€<=*n*<=β€<=100<=000, 1<=β€<=*a*,<=*b*<=β€<=*n*, *a*<=β <=*b*)Β β ΡΠΈΡΠ»ΠΎ Π·ΠΎΠ½, Π½ΠΎΠΌΠ΅Ρ Π½Π°ΡΠ°Π»ΡΠ½ΠΎΠΉ Π·ΠΎΠ½Ρ ΠΈ Π½ΠΎΠΌΠ΅Ρ ΠΊΠΎΠ½Π΅ΡΠ½ΠΎΠΉ Π·ΠΎΠ½Ρ, ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²Π΅Π½Π½ΠΎ.
ΠΡΠΎΡΠ°Ρ ΡΡΡΠΎΠΊΠ° ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ *n*<=-<=1 ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ *g*1,<=*g*2,<=...,<=*g**n*<=-<=1 (1<=β€<=*g**i*<=β€<=100<=000), Π³Π΄Π΅ *g**i* ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ ΡΠ²Π΅Ρ ΠΏΠΎΠ³ΡΠ°Π½ΠΈΡΠ½ΡΡ
Π²ΡΠ°Ρ ΠΌΠ΅ΠΆΠ΄Ρ Π·ΠΎΠ½Π°ΠΌΠΈ *i* ΠΈ *i*<=+<=1.
Π’ΡΠ΅ΡΡΡ ΡΡΡΠΎΠΊΠ° ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ *n* ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π» *k*1,<=*k*2,<=...,<=*k**n* (1<=β€<=*k**i*<=β€<=100<=000), Π³Π΄Π΅ *k**i* ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ ΡΠ²Π΅Ρ ΡΠ°ΡΡΠ° Ρ
ΡΠ°Π½ΠΈΡΠ΅Π»Ρ ΠΊΠ»ΡΡΠ΅ΠΉ Π² *i*-ΠΉ Π·ΠΎΠ½Π΅. | ΠΡΠ»ΠΈ ΠΡΠΊΠ°Π΄ΠΈΠΉ Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ ΠΏΠΎΠΏΠ°ΡΡΡ ΠΈΠ· Π·ΠΎΠ½Ρ *a* Π² Π·ΠΎΠ½Ρ *b*, Π½Π΅ ΠΈΠΌΠ΅Ρ ΠΈΠ·Π½Π°ΡΠ°Π»ΡΠ½ΠΎ ΠΊΠ»ΡΡΠ΅ΠΉ, Π²ΡΠ²Π΅Π΄ΠΈΡΠ΅ -1.
ΠΠ½Π°ΡΠ΅ Π²ΡΠ²Π΅Π΄ΠΈΡΠ΅ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Ρ
ΠΎΠ΄ΠΎΠ², ΠΊΠΎΡΠΎΡΠΎΠ΅ ΠΏΠΎΡΡΠ΅Π±ΡΠ΅ΡΡΡ ΠΡΠΊΠ°Π΄ΠΈΡ. | [
"5 4 1\n3 1 1 2\n7 1 2 1 3\n",
"5 1 5\n4 3 2 1\n4 3 2 5 5\n"
] | [
"7\n",
"-1\n"
] | Π ΠΏΠ΅ΡΠ²ΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅, ΡΡΠΎΠ±Ρ ΠΏΠΎΠΏΠ°ΡΡΡ ΠΈΠ· Π·ΠΎΠ½Ρ 4 Π² Π·ΠΎΠ½Ρ 1, ΠΡΠΊΠ°Π΄ΠΈΡ Π½ΡΠΆΠ½ΠΎ ΡΠ½Π°ΡΠ°Π»Π° Π²Π·ΡΡΡ ΠΊΠ»ΡΡ ΡΠ²Π΅ΡΠ° 1, ΠΏΡΠΎΠΉΡΠΈ Π² Π·ΠΎΠ½Ρ 3, ΡΠ°ΠΌ Π²Π·ΡΡΡ ΠΊΠ»ΡΡ ΡΠ²Π΅ΡΠ° 2 ΠΈ ΠΏΡΠΎΠΉΡΠΈ ΠΎΠ±ΡΠ°ΡΠ½ΠΎ Π² Π·ΠΎΠ½Ρ 4 ΠΈ Π·Π°ΡΠ΅ΠΌ Π² Π·ΠΎΠ½Ρ 5, Π²Π·ΡΡΡ ΡΠ°ΠΌ ΠΊΠ»ΡΡ ΡΠ²Π΅ΡΠ° 3 ΠΈ Π΄ΠΎΠΉΡΠΈ Π΄ΠΎ Π·ΠΎΠ½Ρ 1 Π·Π° ΡΠ΅ΡΡΡΠ΅ Ρ
ΠΎΠ΄Π°.
ΠΠΎ Π²ΡΠΎΡΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ ΠΡΠΊΠ°Π΄ΠΈΠΉ ΠΌΠΎΠΆΠ΅Ρ Π΄ΠΎΠΉΡΠΈ Π»ΠΈΡΡ Π΄ΠΎ ΡΠ΅ΡΠ²Π΅ΡΡΠΎΠΉ Π·ΠΎΠ½Ρ, ΡΠ°ΠΊ ΠΊΠ°ΠΊ ΡΠ°ΡΡΠΎΠ² Ρ
ΡΠ°Π½ΠΈΡΠ΅Π»Π΅ΠΉ ΠΊΠ»ΡΡΠ΅ΠΉ ΡΠ²Π΅ΡΠ° 1 Π½Π΅Ρ ΡΠΎΠ²ΡΠ΅ΠΌ. | [] | 46 | 5,632,000 | 0 | 35,062 | |
344 | Simple Molecules | [
"brute force",
"graphs",
"math"
] | null | null | Mad scientist Mike is busy carrying out experiments in chemistry. Today he will attempt to join three atoms into one molecule.
A molecule consists of atoms, with some pairs of atoms connected by atomic bonds. Each atom has a valence number β the number of bonds the atom must form with other atoms. An atom can form one or multiple bonds with any other atom, but it cannot form a bond with itself. The number of bonds of an atom in the molecule must be equal to its valence number.
Mike knows valence numbers of the three atoms. Find a molecule that can be built from these atoms according to the stated rules, or determine that it is impossible. | The single line of the input contains three space-separated integers *a*, *b* and *c* (1<=β€<=*a*,<=*b*,<=*c*<=β€<=106) β the valence numbers of the given atoms. | If such a molecule can be built, print three space-separated integers β the number of bonds between the 1-st and the 2-nd, the 2-nd and the 3-rd, the 3-rd and the 1-st atoms, correspondingly. If there are multiple solutions, output any of them. If there is no solution, print "Impossible" (without the quotes). | [
"1 1 2\n",
"3 4 5\n",
"4 1 1\n"
] | [
"0 1 1\n",
"1 3 2\n",
"Impossible\n"
] | The first sample corresponds to the first figure. There are no bonds between atoms 1 and 2 in this case.
The second sample corresponds to the second figure. There is one or more bonds between each pair of atoms.
The third sample corresponds to the third figure. There is no solution, because an atom cannot form bonds with itself.
The configuration in the fourth figure is impossible as each atom must have at least one atomic bond. | [
{
"input": "1 1 2",
"output": "0 1 1"
},
{
"input": "3 4 5",
"output": "1 3 2"
},
{
"input": "4 1 1",
"output": "Impossible"
},
{
"input": "1 1 1",
"output": "Impossible"
},
{
"input": "1000000 1000000 1000000",
"output": "500000 500000 500000"
},
{
"i... | 186 | 0 | 3 | 35,067 | |
0 | none | [
"none"
] | null | null | Christmas celebrations are coming to Whoville. Cindy Lou Who and her parents Lou Lou Who and Betty Lou Who decided to give sweets to all people in their street. They decided to give the residents of each house on the street, one kilogram of sweets. So they need as many kilos of sweets as there are homes on their street.
The street, where the Lou Who family lives can be represented as *n* consecutive sections of equal length. You can go from any section to a neighbouring one in one unit of time. Each of the sections is one of three types: an empty piece of land, a house or a shop. Cindy Lou and her family can buy sweets in a shop, but no more than one kilogram of sweets in one shop (the vendors care about the residents of Whoville not to overeat on sweets).
After the Lou Who family leave their home, they will be on the first section of the road. To get to this section of the road, they also require one unit of time. We can assume that Cindy and her mom and dad can carry an unlimited number of kilograms of sweets. Every time they are on a house section, they can give a kilogram of sweets to the inhabitants of the house, or they can simply move to another section. If the family have already given sweets to the residents of a house, they can't do it again. Similarly, if they are on the shop section, they can either buy a kilo of sweets in it or skip this shop. If they've bought a kilo of sweets in a shop, the seller of the shop remembered them and the won't sell them a single candy if they come again. The time to buy and give sweets can be neglected. The Lou Whos do not want the people of any house to remain without food.
The Lou Whos want to spend no more than *t* time units of time to give out sweets, as they really want to have enough time to prepare for the Christmas celebration. In order to have time to give all the sweets, they may have to initially bring additional *k* kilos of sweets.
Cindy Lou wants to know the minimum number of *k* kilos of sweets they need to take with them, to have time to give sweets to the residents of each house in their street.
Your task is to write a program that will determine the minimum possible value of *k*. | The first line of the input contains two space-separated integers *n* and *t* (2<=β€<=*n*<=β€<=5Β·105,<=1<=β€<=*t*<=β€<=109). The second line of the input contains *n* characters, the *i*-th of them equals "H" (if the *i*-th segment contains a house), "S" (if the *i*-th segment contains a shop) or "." (if the *i*-th segment doesn't contain a house or a shop).
It is guaranteed that there is at least one segment with a house. | If there isn't a single value of *k* that makes it possible to give sweets to everybody in at most *t* units of time, print in a single line "-1" (without the quotes). Otherwise, print on a single line the minimum possible value of *k*. | [
"6 6\nHSHSHS\n",
"14 100\n...HHHSSS...SH\n",
"23 50\nHHSS.......SSHHHHHHHHHH\n"
] | [
"1\n",
"0\n",
"8\n"
] | In the first example, there are as many stores, as houses. If the family do not take a single kilo of sweets from home, in order to treat the inhabitants of the first house, they will need to make at least one step back, and they have absolutely no time for it. If they take one kilogram of sweets, they won't need to go back.
In the second example, the number of shops is equal to the number of houses and plenty of time. Available at all stores passing out candy in one direction and give them when passing in the opposite direction.
In the third example, the shops on the street are fewer than houses. The Lou Whos have to take the missing number of kilograms of sweets with them from home. | [] | 124 | 0 | 0 | 35,163 | |
368 | Sereja and Coat Rack | [
"implementation"
] | null | null | Sereja owns a restaurant for *n* people. The restaurant hall has a coat rack with *n* hooks. Each restaurant visitor can use a hook to hang his clothes on it. Using the *i*-th hook costs *a**i* rubles. Only one person can hang clothes on one hook.
Tonight Sereja expects *m* guests in the restaurant. Naturally, each guest wants to hang his clothes on an available hook with minimum price (if there are multiple such hooks, he chooses any of them). However if the moment a guest arrives the rack has no available hooks, Sereja must pay a *d* ruble fine to the guest.
Help Sereja find out the profit in rubles (possibly negative) that he will get tonight. You can assume that before the guests arrive, all hooks on the rack are available, all guests come at different time, nobody besides the *m* guests is visiting Sereja's restaurant tonight. | The first line contains two integers *n* and *d* (1<=β€<=*n*,<=*d*<=β€<=100). The next line contains integers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=100). The third line contains integer *m* (1<=β€<=*m*<=β€<=100). | In a single line print a single integer β the answer to the problem. | [
"2 1\n2 1\n2\n",
"2 1\n2 1\n10\n"
] | [
"3\n",
"-5\n"
] | In the first test both hooks will be used, so Sereja gets 1β+β2β=β3 rubles.
In the second test both hooks will be used but Sereja pays a fine 8 times, so the answer is 3β-β8β=ββ-β5. | [
{
"input": "2 1\n2 1\n2",
"output": "3"
},
{
"input": "2 1\n2 1\n10",
"output": "-5"
},
{
"input": "1 1\n1\n2",
"output": "0"
},
{
"input": "3 96\n83 22 17\n19",
"output": "-1414"
},
{
"input": "8 4\n27 72 39 70 13 68 100 36\n95",
"output": "77"
},
{
"... | 46 | 0 | 0 | 35,187 | |
912 | Fishes | [
"data structures",
"graphs",
"greedy",
"probabilities",
"shortest paths"
] | null | null | While Grisha was celebrating New Year with Ded Moroz, Misha gifted Sasha a small rectangular pond of size *n*<=Γ<=*m*, divided into cells of size 1<=Γ<=1, inhabited by tiny evil fishes (no more than one fish per cell, otherwise they'll strife!).
The gift bundle also includes a square scoop of size *r*<=Γ<=*r*, designed for fishing. If the lower-left corner of the scoop-net is located at cell (*x*,<=*y*), all fishes inside the square (*x*,<=*y*)...(*x*<=+<=*r*<=-<=1,<=*y*<=+<=*r*<=-<=1) get caught. Note that the scoop-net should lie completely inside the pond when used.
Unfortunately, Sasha is not that skilled in fishing and hence throws the scoop randomly. In order to not frustrate Sasha, Misha decided to release *k* fishes into the empty pond in such a way that the expected value of the number of caught fishes is as high as possible. Help Misha! In other words, put *k* fishes in the pond into distinct cells in such a way that when the scoop-net is placed into a random position among (*n*<=-<=*r*<=+<=1)Β·(*m*<=-<=*r*<=+<=1) possible positions, the average number of caught fishes is as high as possible. | The only line contains four integers *n*,<=*m*,<=*r*,<=*k* (1<=β€<=*n*,<=*m*<=β€<=105, 1<=β€<=*r*<=β€<=*min*(*n*,<=*m*), 1<=β€<=*k*<=β€<=*min*(*n*Β·*m*,<=105)). | Print a single numberΒ β the maximum possible expected number of caught fishes.
You answer is considered correct, is its absolute or relative error does not exceed 10<=-<=9. Namely, let your answer be *a*, and the jury's answer be *b*. Your answer is considered correct, if . | [
"3 3 2 3\n",
"12 17 9 40\n"
] | [
"2.0000000000\n",
"32.8333333333\n"
] | In the first example you can put the fishes in cells (2,β1), (2,β2), (2,β3). In this case, for any of four possible positions of the scoop-net (highlighted with light green), the number of fishes inside is equal to two, and so is the expected value. | [
{
"input": "3 3 2 3",
"output": "2.0000000000"
},
{
"input": "12 17 9 40",
"output": "32.8333333333"
},
{
"input": "1 1 1 1",
"output": "1.0000000000"
},
{
"input": "10 10 5 100",
"output": "25.0000000000"
},
{
"input": "7 1 1 4",
"output": "0.5714285714"
},... | 1,000 | 30,515,200 | 0 | 35,221 | |
303 | Rectangle Puzzle II | [
"implementation",
"math"
] | null | null | You are given a rectangle grid. That grid's size is *n*<=Γ<=*m*. Let's denote the coordinate system on the grid. So, each point on the grid will have coordinates β a pair of integers (*x*,<=*y*) (0<=β€<=*x*<=β€<=*n*,<=0<=β€<=*y*<=β€<=*m*).
Your task is to find a maximum sub-rectangle on the grid (*x*1,<=*y*1,<=*x*2,<=*y*2) so that it contains the given point (*x*,<=*y*), and its length-width ratio is exactly (*a*,<=*b*). In other words the following conditions must hold: 0<=β€<=*x*1<=β€<=*x*<=β€<=*x*2<=β€<=*n*, 0<=β€<=*y*1<=β€<=*y*<=β€<=*y*2<=β€<=*m*, .
The sides of this sub-rectangle should be parallel to the axes. And values *x*1,<=*y*1,<=*x*2,<=*y*2 should be integers.
If there are multiple solutions, find the rectangle which is closest to (*x*,<=*y*). Here "closest" means the Euclid distance between (*x*,<=*y*) and the center of the rectangle is as small as possible. If there are still multiple solutions, find the lexicographically minimum one. Here "lexicographically minimum" means that we should consider the sub-rectangle as sequence of integers (*x*1,<=*y*1,<=*x*2,<=*y*2), so we can choose the lexicographically minimum one. | The first line contains six integers *n*,<=*m*,<=*x*,<=*y*,<=*a*,<=*b* (1<=β€<=*n*,<=*m*<=β€<=109,<=0<=β€<=*x*<=β€<=*n*,<=0<=β€<=*y*<=β€<=*m*,<=1<=β€<=*a*<=β€<=*n*,<=1<=β€<=*b*<=β€<=*m*). | Print four integers *x*1,<=*y*1,<=*x*2,<=*y*2, which represent the founded sub-rectangle whose left-bottom point is (*x*1,<=*y*1) and right-up point is (*x*2,<=*y*2). | [
"9 9 5 5 2 1\n",
"100 100 52 50 46 56\n"
] | [
"1 3 9 7\n",
"17 8 86 92\n"
] | none | [
{
"input": "9 9 5 5 2 1",
"output": "1 3 9 7"
},
{
"input": "100 100 52 50 46 56",
"output": "17 8 86 92"
},
{
"input": "100 100 16 60 42 75",
"output": "0 0 56 100"
},
{
"input": "100 100 28 22 47 50",
"output": "0 0 94 100"
},
{
"input": "100 100 44 36 96 21",
... | 109 | 0 | 0 | 35,280 | |
653 | Paper task | [
"data structures",
"string suffix structures",
"strings"
] | null | null | Alex was programming while Valentina (his toddler daughter) got there and started asking many questions about the round brackets (or parenthesis) in the code. He explained her a bit and when she got it he gave her a task in order to finish his code on time.
For the purpose of this problem we consider only strings consisting of opening and closing round brackets, that is characters '(' and ')'.
The sequence of brackets is called correct if:
1. it's empty; 1. it's a correct sequence of brackets, enclosed in a pair of opening and closing brackets; 1. it's a concatenation of two correct sequences of brackets.
For example, the sequences "()()" and "((()))(())" are correct, while ")(()", "(((((" and "())" are not.
Alex took a piece of paper, wrote a string *s* consisting of brackets and asked Valentina to count the number of distinct non-empty substrings of *s* that are correct sequences of brackets. In other words, her task is to count the number of non-empty correct sequences of brackets that occur in a string *s* as a substring (don't mix up with subsequences).
When Valentina finished the task, Alex noticed he doesn't know the answer. Help him don't loose face in front of Valentina and solve the problem! | The first line of the input contains an integer *n* (1<=β€<=*n*<=β€<=500<=000)Β β the length of the string *s*.
The second line contains a string *s* of length *n* consisting of only '(' and ')'. | Print the number of distinct non-empty correct sequences that occur in *s* as substring. | [
"10\n()()()()()\n",
"7\n)(())()\n"
] | [
"5\n",
"3\n"
] | In the first sample, there are 5 distinct substrings we should count: "()", "()()", "()()()", "()()()()" and "()()()()()".
In the second sample, there are 3 distinct substrings we should count: "()", "(())" and "(())()". | [
{
"input": "10\n()()()()()",
"output": "5"
},
{
"input": "7\n)(())()",
"output": "3"
},
{
"input": "1\n(",
"output": "0"
},
{
"input": "2\n))",
"output": "0"
},
{
"input": "15\n(())(()())(()()",
"output": "5"
},
{
"input": "30\n()(())(())(())()(())()()... | 124 | 0 | 0 | 35,375 | |
314 | Sereja and Subsequences | [
"data structures",
"dp"
] | null | null | Sereja has a sequence that consists of *n* positive integers, *a*1,<=*a*2,<=...,<=*a**n*.
First Sereja took a piece of squared paper and wrote all distinct non-empty non-decreasing subsequences of sequence *a*. Then for each sequence written on the squared paper, Sereja wrote on a piece of lines paper all sequences that do not exceed it.
A sequence of positive integers *x*<==<=*x*1,<=*x*2,<=...,<=*x**r* doesn't exceed a sequence of positive integers *y*<==<=*y*1,<=*y*2,<=...,<=*y**r*, if the following inequation holds: *x*1<=β€<=*y*1,<=*x*2<=β€<=*y*2,<=...,<=*x**r*<=β€<=*y**r*.
Now Sereja wonders, how many sequences are written on the lines piece of paper. Help Sereja, find the required quantity modulo 1000000007 (109<=+<=7). | The first line contains integer *n* (1<=β€<=*n*<=β€<=105). The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=106). | In the single line print the answer to the problem modulo 1000000007 (109<=+<=7). | [
"1\n42\n",
"3\n1 2 2\n",
"5\n1 2 3 4 5\n"
] | [
"42\n",
"13\n",
"719\n"
] | none | [
{
"input": "1\n42",
"output": "42"
},
{
"input": "3\n1 2 2",
"output": "13"
},
{
"input": "5\n1 2 3 4 5",
"output": "719"
},
{
"input": "4\n11479 29359 26963 24465",
"output": "927446239"
},
{
"input": "5\n5706 28146 23282 16828 9962",
"output": "446395832"
... | 62 | 0 | 0 | 35,403 | |
584 | Marina and Vasya | [
"constructive algorithms",
"greedy",
"strings"
] | null | null | Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly *t* characters. Help Vasya find at least one such string.
More formally, you are given two strings *s*1, *s*2 of length *n* and number *t*. Let's denote as *f*(*a*,<=*b*) the number of characters in which strings *a* and *b* are different. Then your task will be to find any string *s*3 of length *n*, such that *f*(*s*1,<=*s*3)<==<=*f*(*s*2,<=*s*3)<==<=*t*. If there is no such string, print <=-<=1. | The first line contains two integers *n* and *t* (1<=β€<=*n*<=β€<=105, 0<=β€<=*t*<=β€<=*n*).
The second line contains string *s*1 of length *n*, consisting of lowercase English letters.
The third line contain string *s*2 of length *n*, consisting of lowercase English letters. | Print a string of length *n*, differing from string *s*1 and from *s*2 in exactly *t* characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1. | [
"3 2\nabc\nxyc\n",
"1 0\nc\nb\n"
] | [
"ayd",
"-1\n"
] | none | [
{
"input": "3 2\nabc\nxyc",
"output": "bac"
},
{
"input": "1 0\nc\nb",
"output": "-1"
},
{
"input": "1 1\na\na",
"output": "b"
},
{
"input": "2 1\naa\naa",
"output": "ab"
},
{
"input": "3 1\nbcb\nbca",
"output": "bcc"
},
{
"input": "4 3\nccbb\ncaab",
... | 748 | 11,059,200 | 3 | 35,444 | |
390 | Inna, Dima and Song | [
"implementation"
] | null | null | Inna is a great piano player and Dima is a modest guitar player. Dima has recently written a song and they want to play it together. Of course, Sereja wants to listen to the song very much.
A song is a sequence of notes. Dima and Inna want to play each note at the same time. At that, they can play the *i*-th note at volume *v* (1<=β€<=*v*<=β€<=*a**i*; *v* is an integer) both on the piano and the guitar. They should retain harmony, so the total volume with which the *i*-th note was played on the guitar and the piano must equal *b**i*. If Dima and Inna cannot play a note by the described rules, they skip it and Sereja's joy drops by 1. But if Inna and Dima play the *i*-th note at volumes *x**i* and *y**i* (*x**i*<=+<=*y**i*<==<=*b**i*) correspondingly, Sereja's joy rises by *x**i*Β·*y**i*.
Sereja has just returned home from the university and his current joy is 0. Help Dima and Inna play the song so as to maximize Sereja's total joy after listening to the whole song! | The first line of the input contains integer *n* (1<=β€<=*n*<=β€<=105) β the number of notes in the song. The second line contains *n* integers *a**i* (1<=β€<=*a**i*<=β€<=106). The third line contains *n* integers *b**i* (1<=β€<=*b**i*<=β€<=106). | In a single line print an integer β the maximum possible joy Sereja feels after he listens to a song. | [
"3\n1 1 2\n2 2 3\n",
"1\n2\n5\n"
] | [
"4\n",
"-1\n"
] | In the first sample, Dima and Inna play the first two notes at volume 1 (1β+β1β=β2, the condition holds), they should play the last note at volumes 1 and 2. Sereja's total joy equals: 1Β·1β+β1Β·1β+β1Β·2β=β4.
In the second sample, there is no such pair (*x*,β*y*), that 1ββ€β*x*,β*y*ββ€β2, *x*β+β*y*β=β5, so Dima and Inna skip a note. Sereja's total joy equals -1. | [
{
"input": "3\n1 1 2\n2 2 3",
"output": "4"
},
{
"input": "1\n2\n5",
"output": "-1"
},
{
"input": "10\n2 2 3 4 5 6 7 8 9 10\n2 2 3 4 5 6 7 8 9 10",
"output": "96"
},
{
"input": "10\n1 2 3 4 5 6 7 8 9 10\n1 2 3 4 5 6 7 8 9 10",
"output": "94"
},
{
"input": "3\n1000... | 140 | 0 | 0 | 35,459 | |
241 | Mirror Box | [
"geometry",
"implementation"
] | null | null | Mirror Box is a name of a popular game in the Iranian National Amusement Park (INAP). There is a wooden box, 105 cm long and 100 cm high in this game. Some parts of the box's ceiling and floor are covered by mirrors. There are two negligibly small holes in the opposite sides of the box at heights *h**l* and *h**r* centimeters above the floor. The picture below shows what the box looks like.
In the game, you will be given a laser gun to shoot once. The laser beam must enter from one hole and exit from the other one. Each mirror has a preset number *v**i*, which shows the number of points players gain if their laser beam hits that mirror. Also β to make things even funnier β the beam must not hit any mirror more than once.
Given the information about the box, your task is to find the maximum score a player may gain. Please note that the reflection obeys the law "the angle of incidence equals the angle of reflection". | The first line of the input contains three space-separated integers *h**l*,<=*h**r*,<=*n* (0<=<<=*h**l*,<=*h**r*<=<<=100, 0<=β€<=*n*<=β€<=100) β the heights of the holes and the number of the mirrors.
Next *n* lines contain the descriptions of the mirrors. The *i*-th line contains space-separated *v**i*,<=*c**i*,<=*a**i*,<=*b**i*; the integer *v**i* (1<=β€<=*v**i*<=β€<=1000) is the score for the *i*-th mirror; the character *c**i* denotes *i*-th mirror's position β the mirror is on the ceiling if *c**i* equals "T" and on the floor if *c**i* equals "F"; integers *a**i* and *b**i* (0<=β€<=*a**i*<=<<=*b**i*<=β€<=105) represent the *x*-coordinates of the beginning and the end of the mirror.
No two mirrors will share a common point. Consider that the *x* coordinate increases in the direction from left to right, so the border with the hole at height *h**l* has the *x* coordinate equal to 0 and the border with the hole at height *h**r* has the *x* coordinate equal to 105. | The only line of output should contain a single integer β the maximum possible score a player could gain. | [
"50 50 7\n10 F 1 80000\n20 T 1 80000\n30 T 81000 82000\n40 T 83000 84000\n50 T 85000 86000\n60 T 87000 88000\n70 F 81000 89000\n",
"80 72 9\n15 T 8210 15679\n10 F 11940 22399\n50 T 30600 44789\n50 F 32090 36579\n5 F 45520 48519\n120 F 49250 55229\n8 F 59700 80609\n35 T 61940 64939\n2 T 92540 97769\n"
] | [
"100\n",
"120\n"
] | The second sample is depicted above. The red beam gets 10β+β50β+β5β+β35β+β8β+β2β=β110 points and the blue one gets 120.
The red beam on the picture given in the statement shows how the laser beam can go approximately, this is just illustration how the laser beam can gain score. So for the second sample there is no such beam that gain score 110. | [] | 60 | 0 | 0 | 35,614 | |
671 | Robin Hood | [
"binary search",
"greedy"
] | null | null | We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.
There are *n* citizens in Kekoland, each person has *c**i* coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in *k* days. He decided to spend these last days with helping poor people.
After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too.
Your task is to find the difference between richest and poorest persons wealth after *k* days. Note that the choosing at random among richest and poorest doesn't affect the answer. | The first line of the input contains two integers *n* and *k* (1<=β€<=*n*<=β€<=500<=000,<=0<=β€<=*k*<=β€<=109)Β β the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.
The second line contains *n* integers, the *i*-th of them is *c**i* (1<=β€<=*c**i*<=β€<=109)Β β initial wealth of the *i*-th person. | Print a single line containing the difference between richest and poorest peoples wealth. | [
"4 1\n1 1 4 2\n",
"3 1\n2 2 2\n"
] | [
"2\n",
"0\n"
] | Lets look at how wealth changes through day in the first sample.
1. [1,β1,β4,β2] 1. [2,β1,β3,β2] or [1,β2,β3,β2]
So the answer is 3β-β1β=β2
In second sample wealth will remain the same for each person. | [
{
"input": "4 1\n1 1 4 2",
"output": "2"
},
{
"input": "3 1\n2 2 2",
"output": "0"
},
{
"input": "10 20\n6 4 7 10 4 5 5 3 7 10",
"output": "1"
},
{
"input": "30 7\n3 3 2 2 2 2 3 4 4 5 2 1 1 5 5 3 4 3 2 1 3 4 3 2 2 5 2 5 1 2",
"output": "2"
},
{
"input": "2 0\n182 ... | 202 | 5,120,000 | 0 | 35,651 | |
750 | New Year and North Pole | [
"geometry",
"implementation"
] | null | null | In this problem we assume the Earth to be a completely round ball and its surface a perfect sphere. The length of the equator and any meridian is considered to be exactly 40<=000 kilometers. Thus, travelling from North Pole to South Pole or vice versa takes exactly 20<=000 kilometers.
Limak, a polar bear, lives on the North Pole. Close to the New Year, he helps somebody with delivering packages all around the world. Instead of coordinates of places to visit, Limak got a description how he should move, assuming that he starts from the North Pole. The description consists of *n* parts. In the *i*-th part of his journey, Limak should move *t**i* kilometers in the direction represented by a string *dir**i* that is one of: "North", "South", "West", "East".
Limak isnβt sure whether the description is valid. You must help him to check the following conditions:
- If at any moment of time (before any of the instructions or while performing one of them) Limak is on the North Pole, he can move only to the South. - If at any moment of time (before any of the instructions or while performing one of them) Limak is on the South Pole, he can move only to the North. - The journey must end on the North Pole.
Check if the above conditions are satisfied and print "YES" or "NO" on a single line. | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=50).
The *i*-th of next *n* lines contains an integer *t**i* and a string *dir**i* (1<=β€<=*t**i*<=β€<=106, )Β β the length and the direction of the *i*-th part of the journey, according to the description Limak got. | Print "YES" if the description satisfies the three conditions, otherwise print "NO", both without the quotes. | [
"5\n7500 South\n10000 East\n3500 North\n4444 West\n4000 North\n",
"2\n15000 South\n4000 East\n",
"5\n20000 South\n1000 North\n1000000 West\n9000 North\n10000 North\n",
"3\n20000 South\n10 East\n20000 North\n",
"2\n1000 North\n1000 South\n",
"4\n50 South\n50 North\n15000 South\n15000 North\n"
] | [
"YES\n",
"NO\n",
"YES\n",
"NO\n",
"NO\n",
"YES\n"
] | Drawings below show how Limak's journey would look like in first two samples. In the second sample the answer is "NO" because he doesn't end on the North Pole. | [
{
"input": "5\n7500 South\n10000 East\n3500 North\n4444 West\n4000 North",
"output": "YES"
},
{
"input": "2\n15000 South\n4000 East",
"output": "NO"
},
{
"input": "5\n20000 South\n1000 North\n1000000 West\n9000 North\n10000 North",
"output": "YES"
},
{
"input": "3\n20000 Sout... | 62 | 0 | 0 | 35,698 | |
0 | none | [
"none"
] | null | null | Alice and Bob decided to eat some fruit. In the kitchen they found a large bag of oranges and apples. Alice immediately took an orange for herself, Bob took an apple. To make the process of sharing the remaining fruit more fun, the friends decided to play a game. They put multiple cards and on each one they wrote a letter, either 'A', or the letter 'B'. Then they began to remove the cards one by one from left to right, every time they removed a card with the letter 'A', Alice gave Bob all the fruits she had at that moment and took out of the bag as many apples and as many oranges as she had before. Thus the number of oranges and apples Alice had, did not change. If the card had written letter 'B', then Bob did the same, that is, he gave Alice all the fruit that he had, and took from the bag the same set of fruit. After the last card way removed, all the fruit in the bag were over.
You know how many oranges and apples was in the bag at first. Your task is to find any sequence of cards that Alice and Bob could have played with. | The first line of the input contains two integers, *x*,<=*y* (1<=β€<=*x*,<=*y*<=β€<=1018,<=*xy*<=><=1) β the number of oranges and apples that were initially in the bag. | Print any sequence of cards that would meet the problem conditions as a compressed string of characters 'A' and 'B. That means that you need to replace the segments of identical consecutive characters by the number of repetitions of the characters and the actual character. For example, string AAABAABBB should be replaced by string 3A1B2A3B, but cannot be replaced by 2A1A1B2A3B or by 3AB2A3B. See the samples for clarifications of the output format. The string that you print should consist of at most 106 characters. It is guaranteed that if the answer exists, its compressed representation exists, consisting of at most 106 characters. If there are several possible answers, you are allowed to print any of them.
If the sequence of cards that meet the problem statement does not not exist, print a single word Impossible. | [
"1 4\n",
"2 2\n",
"3 2\n"
] | [
"3B\n",
"Impossible\n",
"1A1B\n"
] | In the first sample, if the row contained three cards with letter 'B', then Bob should give one apple to Alice three times. So, in the end of the game Alice has one orange and three apples, and Bob has one apple, in total it is one orange and four apples.
In second sample, there is no answer since one card is not enough for game to finish, and two cards will produce at least three apples or three oranges.
In the third sample, cards contain letters 'AB', so after removing the first card Bob has one orange and one apple, and after removal of second card Alice has two oranges and one apple. So, in total it is three oranges and two apples. | [
{
"input": "1 4",
"output": "3B"
},
{
"input": "2 2",
"output": "Impossible"
},
{
"input": "3 2",
"output": "1A1B"
},
{
"input": "2 1",
"output": "1A"
},
{
"input": "5 3",
"output": "1A1B1A"
},
{
"input": "5 2",
"output": "2A1B"
},
{
"input... | 62 | 0 | 3 | 35,705 | |
555 | Case of a Top Secret | [
"binary search",
"implementation",
"math"
] | null | null | Andrewid the Android is a galaxy-famous detective. Now he is busy with a top secret case, the details of which are not subject to disclosure.
However, he needs help conducting one of the investigative experiment. There are *n* pegs put on a plane, they are numbered from 1 to *n*, the coordinates of the *i*-th of them are (*x**i*,<=0). Then, we tie to the bottom of one of the pegs a weight on a tight rope of length *l* (thus, its coordinates will be equal to (*x**i*,<=<=-<=*l*), where *i* is the number of the used peg). Then the weight is pushed to the right, so that it starts to rotate counterclockwise. At the same time, if the weight during rotation touches some of the other pegs, it then begins to rotate around that peg. Suppose that each peg itself is very thin and does not affect the rope length while weight is rotating around it.
More formally, if at some moment the segment of the rope contains one or more pegs in addition to the peg around which the weight is rotating, the weight will then rotate around the farthermost one of them on a shorter segment of a rope. In particular, if the segment of the rope touches some peg by its endpoint, it is considered that the weight starts to rotate around that peg on a segment of the rope of length 0.
At some moment the weight will begin to rotate around some peg, without affecting the rest of the pegs. Andrewid interested in determining the number of this peg.
Andrewid prepared *m* queries containing initial conditions for pushing the weight, help him to determine for each of them, around what peg the weight will eventually rotate. | The first line contains integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=2Β·105) β the number of pegs and queries.
The next line contains *n* integers *x*1,<=*x*2,<=...,<=*x**n* (<=-<=109<=β€<=*x**i*<=β€<=109) β the coordinates of the pegs. It is guaranteed that the coordinates of all the pegs are distinct integers.
Next *m* lines contain the descriptions of the queries of pushing the weight, each consists of two integers *a**i* (1<=β€<=*a**i*<=β€<=*n*) and *l**i* (1<=β€<=*l**i*<=β€<=109) β the number of the starting peg and the length of the rope. | Print *m* lines, the *i*-th line should contain the number of the peg around which the weight will eventually rotate after the *i*-th push. | [
"3 2\n0 3 5\n2 3\n1 8\n",
"4 4\n1 5 7 15\n1 4\n2 15\n3 16\n1 28\n"
] | [
"3\n2\n",
"2\n4\n3\n1\n"
] | Picture to the first sample test:
<img class="tex-graphics" src="https://espresso.codeforces.com/8bc7392d3f6441884836fdead4a4afbac2a19f49.png" style="max-width: 100.0%;max-height: 100.0%;"/>
Picture to the second sample test:
<img class="tex-graphics" src="https://espresso.codeforces.com/abef521ff4b500a39098df2ced95cb992c1845ed.png" style="max-width: 100.0%;max-height: 100.0%;"/>
Note that in the last query weight starts to rotate around the peg 1 attached to a rope segment of length 0. | [] | 2,000 | 0 | 0 | 35,718 | |
209 | Pixels | [
"constructive algorithms",
"math"
] | null | null | Flatland is inhabited by pixels of three colors: red, green and blue. We know that if two pixels of different colors meet in a violent fight, only one of them survives the fight (that is, the total number of pixels decreases by one). Besides, if pixels of colors *x* and *y* (*x*<=β <=*y*) meet in a violent fight, then the pixel that survives the fight immediately changes its color to *z* (*z*<=β <=*x*;Β *z*<=β <=*y*). Pixels of the same color are friends, so they don't fight.
The King of Flatland knows that his land will be peaceful and prosperous when the pixels are of the same color. For each of the three colors you know the number of pixels of this color that inhabit Flatland. Help the king and determine whether fights can bring peace and prosperity to the country and if it is possible, find the minimum number of fights needed to make the land peaceful and prosperous. | The first line contains three space-separated integers *a*, *b* and *c* (0<=β€<=*a*,<=*b*,<=*c*<=β€<=231;Β *a*<=+<=*b*<=+<=*c*<=><=0) β the number of red, green and blue pixels, correspondingly. | Print a single number β the minimum number of pixel fights before the country becomes peaceful and prosperous. If making the country peaceful and prosperous is impossible, print -1. | [
"1 1 1\n",
"3 1 0\n"
] | [
"1\n",
"3\n"
] | In the first test sample the country needs only one fight to achieve peace and prosperity. Besides, it can be any fight whatsoever. For example, let's assume that the green and the blue pixels fight, then the surviving pixel will be red. As a result, after the fight there are two red pixels. There won't be other pixels.
In the second sample the following sequence of fights is possible: red and blue, green and red, red and blue. As a result, after all fights there is one green pixel left. | [
{
"input": "1 1 1",
"output": "1"
},
{
"input": "3 1 0",
"output": "3"
},
{
"input": "1 4 4",
"output": "4"
},
{
"input": "5 10 6",
"output": "10"
},
{
"input": "6 8 10",
"output": "8"
},
{
"input": "1 10 2",
"output": "10"
},
{
"input": "1... | 92 | 0 | 0 | 35,773 | |
1,003 | Tree Constructing | [
"constructive algorithms",
"graphs"
] | null | null | You are given three integers $n$, $d$ and $k$.
Your task is to construct an undirected tree on $n$ vertices with diameter $d$ and degree of each vertex at most $k$, or say that it is impossible.
An undirected tree is a connected undirected graph with $n - 1$ edges.
Diameter of a tree is the maximum length of a simple path (a path in which each vertex appears at most once) between all pairs of vertices of this tree.
Degree of a vertex is the number of edges incident to this vertex (i.e. for a vertex $u$ it is the number of edges $(u, v)$ that belong to the tree, where $v$ is any other vertex of a tree). | The first line of the input contains three integers $n$, $d$ and $k$ ($1 \le n, d, k \le 4 \cdot 10^5$). | If there is no tree satisfying the conditions above, print only one word "NO" (without quotes).
Otherwise in the first line print "YES" (without quotes), and then print $n - 1$ lines describing edges of a tree satisfying the conditions above. Vertices of the tree must be numbered from $1$ to $n$. You can print edges and vertices connected by an edge in any order. If there are multiple answers, print any of them.1 | [
"6 3 3\n",
"6 2 3\n",
"10 4 3\n",
"8 5 3\n"
] | [
"YES\n3 1\n4 1\n1 2\n5 2\n2 6\n",
"NO\n",
"YES\n2 9\n2 10\n10 3\n3 1\n6 10\n8 2\n4 3\n5 6\n6 7\n",
"YES\n2 5\n7 2\n3 7\n3 1\n1 6\n8 7\n4 3\n"
] | none | [
{
"input": "6 3 3",
"output": "YES\n2 5\n4 2\n3 4\n2 1\n4 6"
},
{
"input": "6 2 3",
"output": "NO"
},
{
"input": "10 4 3",
"output": "YES\n2 9\n2 10\n10 3\n3 1\n6 10\n8 2\n4 3\n5 6\n6 7"
},
{
"input": "8 5 3",
"output": "YES\n2 5\n7 2\n3 7\n3 1\n1 6\n8 7\n4 3"
},
{
... | 202 | 2,048,000 | -1 | 35,867 | |
305 | Olya and Graph | [
"combinatorics",
"math"
] | null | null | Olya has got a directed non-weighted graph, consisting of *n* vertexes and *m* edges. We will consider that the graph vertexes are indexed from 1 to *n* in some manner. Then for any graph edge that goes from vertex *v* to vertex *u* the following inequation holds: *v*<=<<=*u*.
Now Olya wonders, how many ways there are to add an arbitrary (possibly zero) number of edges to the graph so as the following conditions were met:
1. You can reach vertexes number *i*<=+<=1,<=*i*<=+<=2,<=...,<=*n* from any vertex number *i* (*i*<=<<=*n*). 1. For any graph edge going from vertex *v* to vertex *u* the following inequation fulfills: *v*<=<<=*u*. 1. There is at most one edge between any two vertexes. 1. The shortest distance between the pair of vertexes *i*,<=*j* (*i*<=<<=*j*), for which *j*<=-<=*i*<=β€<=*k* holds, equals *j*<=-<=*i* edges. 1. The shortest distance between the pair of vertexes *i*,<=*j* (*i*<=<<=*j*), for which *j*<=-<=*i*<=><=*k* holds, equals either *j*<=-<=*i* or *j*<=-<=*i*<=-<=*k* edges.
We will consider two ways distinct, if there is the pair of vertexes *i*,<=*j* (*i*<=<<=*j*), such that first resulting graph has an edge from *i* to *j* and the second one doesn't have it.
Help Olya. As the required number of ways can be rather large, print it modulo 1000000007 (109<=+<=7). | The first line contains three space-separated integers *n*,<=*m*,<=*k* (2<=β€<=*n*<=β€<=106,<=0<=β€<=*m*<=β€<=105,<=1<=β€<=*k*<=β€<=106).
The next *m* lines contain the description of the edges of the initial graph. The *i*-th line contains a pair of space-separated integers *u**i*,<=*v**i* (1<=β€<=*u**i*<=<<=*v**i*<=β€<=*n*) β the numbers of vertexes that have a directed edge from *u**i* to *v**i* between them.
It is guaranteed that any pair of vertexes *u**i*,<=*v**i* has at most one edge between them. It also is guaranteed that the graph edges are given in the order of non-decreasing *u**i*. If there are multiple edges going from vertex *u**i*, then it is guaranteed that these edges are given in the order of increasing *v**i*. | Print a single integer β the answer to the problem modulo 1000000007 (109<=+<=7). | [
"7 8 2\n1 2\n2 3\n3 4\n3 6\n4 5\n4 7\n5 6\n6 7\n",
"7 0 2\n",
"7 2 1\n1 3\n3 5\n"
] | [
"2\n",
"12\n",
"0\n"
] | In the first sample there are two ways: the first way is not to add anything, the second way is to add a single edge from vertex 2 to vertex 5. | [] | 60 | 0 | 0 | 35,874 | |
827 | Rusty String | [
"fft",
"math",
"strings"
] | null | null | Grigory loves strings. Recently he found a metal strip on a loft. The strip had length *n* and consisted of letters "V" and "K". Unfortunately, rust has eaten some of the letters so that it's now impossible to understand which letter was written.
Grigory couldn't understand for a long time what these letters remind him of, so he became interested in the following question: if we put a letter "V" or "K" on each unreadable position, which values can the period of the resulting string be equal to?
A period of a string is such an integer *d* from 1 to the length of the string that if we put the string shifted by *d* positions to the right on itself, then all overlapping letters coincide. For example, 3 and 5 are periods of "VKKVK". | There are several (at least one) test cases in the input. The first line contains single integerΒ β the number of test cases.
There is an empty line before each test case. Each test case is described in two lines: the first line contains single integer *n* (1<=β€<=*n*<=β€<=5Β·105)Β β the length of the string, the second line contains the string of length *n*, consisting of letters "V", "K" and characters "?". The latter means the letter on its position is unreadable.
It is guaranteed that the sum of lengths among all test cases doesn't exceed 5Β·105.
For hacks you can only use tests with one test case. | For each test case print two lines. In the first line print the number of possible periods after we replace each unreadable letter with "V" or "K". In the next line print all these values in increasing order. | [
"3\nΒ \n5\nV??VK\nΒ \n6\n??????\nΒ \n4\n?VK?\n"
] | [
"2\n3 5\n6\n1 2 3 4 5 6\n3\n2 3 4\n"
] | In the first test case from example we can obtain, for example, "VKKVK", which has periods 3 and 5.
In the second test case we can obtain "VVVVVV" which has all periods from 1 to 6.
In the third test case string "KVKV" has periods 2 and 4, and string "KVKK" has periods 3 and 4. | [] | 15 | 0 | -1 | 35,901 | |
113 | Double Happiness | [
"brute force",
"math",
"number theory"
] | C. Double Happiness | 3 | 128 | On the math lesson a teacher asked each pupil to come up with his own lucky numbers. As a fan of number theory Peter chose prime numbers. Bob was more original. He said that number *t* is his lucky number, if it can be represented as:
Now, the boys decided to find out how many days of the interval [*l*,<=*r*] (*l*<=β€<=*r*) are suitable for pair programming. They decided that the day *i* (*l*<=β€<=*i*<=β€<=*r*) is suitable for pair programming if and only if the number *i* is lucky for Peter and lucky for Bob at the same time. Help the boys to find the number of such days. | The first line of the input contains integer numbers *l*,<=*r* (1<=β€<=*l*,<=*r*<=β€<=3Β·108). | In the only line print the number of days on the segment [*l*,<=*r*], which are lucky for Peter and Bob at the same time. | [
"3 5\n",
"6 66\n"
] | [
"1\n",
"7\n"
] | none | [] | 2,635 | 6,963,200 | 0 | 35,978 |
803 | Periodic RMQ Problem | [
"data structures"
] | null | null | You are given an array *a* consisting of positive integers and *q* queries to this array. There are two types of queries:
- 1 *l* *r* *x* β for each index *i* such that *l*<=β€<=*i*<=β€<=*r* set *a**i*<==<=*x*. - 2 *l* *r* β find the minimum among such *a**i* that *l*<=β€<=*i*<=β€<=*r*.
We decided that this problem is too easy. So the array *a* is given in a compressed form: there is an array *b* consisting of *n* elements and a number *k* in the input, and before all queries *a* is equal to the concatenation of *k* arrays *b* (so the size of *a* is *n*Β·*k*). | The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=105, 1<=β€<=*k*<=β€<=104).
The second line contains *n* integers β elements of the array *b* (1<=β€<=*b**i*<=β€<=109).
The third line contains one integer *q* (1<=β€<=*q*<=β€<=105).
Then *q* lines follow, each representing a query. Each query is given either as 1 *l* *r* *x* β set all elements in the segment from *l* till *r* (including borders) to *x* (1<=β€<=*l*<=β€<=*r*<=β€<=*n*Β·*k*, 1<=β€<=*x*<=β€<=109) or as 2 *l* *r* β find the minimum among all elements in the segment from *l* till *r* (1<=β€<=*l*<=β€<=*r*<=β€<=*n*Β·*k*). | For each query of type 2 print the answer to this query β the minimum on the corresponding segment. | [
"3 1\n1 2 3\n3\n2 1 3\n1 1 2 4\n2 1 3\n",
"3 2\n1 2 3\n5\n2 4 4\n1 4 4 5\n2 4 4\n1 1 6 1\n2 6 6\n"
] | [
"1\n3\n",
"1\n5\n1\n"
] | none | [
{
"input": "3 1\n1 2 3\n3\n2 1 3\n1 1 2 4\n2 1 3",
"output": "1\n3"
},
{
"input": "3 2\n1 2 3\n5\n2 4 4\n1 4 4 5\n2 4 4\n1 1 6 1\n2 6 6",
"output": "1\n5\n1"
},
{
"input": "10 10\n10 8 10 9 2 2 4 6 10 1\n10\n1 17 87 5\n2 31 94\n1 5 56 8\n1 56 90 10\n1 25 93 6\n1 11 32 4\n2 20 49\n1 46 87... | 1,013 | 1,331,200 | 0 | 36,054 | |
792 | Mages and Monsters | [
"data structures",
"geometry"
] | null | null | Vova plays a computer game known as Mages and Monsters. Vova's character is a mage. Though as he has just started, his character knows no spells.
Vova's character can learn new spells during the game. Every spell is characterized by two values *x**i* and *y**i* β damage per second and mana cost per second, respectively. Vova doesn't have to use a spell for an integer amount of seconds. More formally, if he uses a spell with damage *x* and mana cost *y* for *z* seconds, then he will deal *x*Β·*z* damage and spend *y*Β·*z* mana (no rounding). If there is no mana left (mana amount is set in the start of the game and it remains the same at the beginning of every fight), then character won't be able to use any spells. It is prohibited to use multiple spells simultaneously.
Also Vova can fight monsters. Every monster is characterized by two values *t**j* and *h**j* β monster kills Vova's character in *t**j* seconds and has *h**j* health points. Mana refills after every fight (or Vova's character revives with full mana reserve), so previous fights have no influence on further ones.
Vova's character kills a monster, if he deals *h**j* damage to it in no more than *t**j* seconds using his spells (it is allowed to use more than one spell in a fight) and spending no more mana than he had at the beginning of the fight. If monster's health becomes zero exactly in *t**j* seconds (it means that the monster and Vova's character kill each other at the same time), then Vova wins the fight.
You have to write a program which can answer two types of queries:
- 1 *x* *y* β Vova's character learns new spell which deals *x* damage per second and costs *y* mana per second. - 2 *t* *h* β Vova fights the monster which kills his character in *t* seconds and has *h* health points.
Note that queries are given in a different form. Also remember that Vova's character knows no spells at the beginning of the game.
For every query of second type you have to determine if Vova is able to win the fight with corresponding monster. | The first line contains two integer numbers *q* and *m* (2<=β€<=*q*<=β€<=105,<=1<=β€<=*m*<=β€<=1012) β the number of queries and the amount of mana at the beginning of every fight.
*i*-th of each next *q* lines contains three numbers *k**i*, *a**i* and *b**i* (1<=β€<=*k**i*<=β€<=2,<=1<=β€<=*a**i*,<=*b**i*<=β€<=106).
Using them you can restore queries this way: let *j* be the index of the last query of second type with positive answer (*j*<==<=0 if there were none of these).
- If *k**i*<==<=1, then character learns spell with *x*<==<=(*a**i*<=+<=*j*) *mod* 106<=+<=1, *y*<==<=(*b**i*<=+<=*j*) *mod* 106<=+<=1. - If *k**i*<==<=2, then you have to determine if Vova is able to win the fight against monster with *t*<==<=(*a**i*<=+<=*j*) *mod* 106<=+<=1, *h*<==<=(*b**i*<=+<=*j*) *mod* 106<=+<=1. | For every query of second type print YES if Vova is able to win the fight with corresponding monster and NO otherwise. | [
"3 100\n1 4 9\n2 19 49\n2 19 49\n"
] | [
"YES\nNO\n"
] | In first example Vova's character at first learns the spell with 5 damage and 10 mana cost per second. Next query is a fight with monster which can kill character in 20 seconds and has 50 health points. Vova kills it in 10 seconds (spending 100 mana). Next monster has 52 health, so Vova can't deal that much damage with only 100 mana. | [
{
"input": "3 100\n1 4 9\n2 19 49\n2 19 49",
"output": "YES\nNO"
},
{
"input": "10 442006988299\n2 10 47\n1 9 83\n1 15 24\n2 19 47\n2 75 99\n2 85 23\n2 8 33\n2 9 82\n1 86 49\n2 71 49",
"output": "NO\nYES\nYES\nYES\nYES\nYES\nYES"
},
{
"input": "2 424978864039\n2 7 3\n2 10 8",
"output... | 0 | 0 | -1 | 36,172 | |
645 | Cowslip Collections | [
"combinatorics",
"math",
"number theory"
] | null | null | In an attempt to make peace with the Mischievious Mess Makers, Bessie and Farmer John are planning to plant some flower gardens to complement the lush, grassy fields of Bovinia. As any good horticulturist knows, each garden they plant must have the exact same arrangement of flowers. Initially, Farmer John has *n* different species of flowers he can plant, with *a**i* flowers of the *i*-th species.
On each of the next *q* days, Farmer John will receive a batch of flowers of a new species. On day *j*, he will receive *c**j* flowers of the same species, but of a different species from those Farmer John already has.
Farmer John, knowing the right balance between extravagance and minimalism, wants exactly *k* species of flowers to be used. Furthermore, to reduce waste, each flower of the *k* species Farmer John chooses must be planted in some garden. And each of the gardens must be identical; that is to say that each of the *k* chosen species should have an equal number of flowers in each garden. As Farmer John is a proponent of national equality, he would like to create the greatest number of gardens possible.
After receiving flowers on each of these *q* days, Farmer John would like to know the sum, over all possible choices of *k* species, of the maximum number of gardens he could create. Since this could be a large number, you should output your result modulo 109<=+<=7. | The first line of the input contains three integers *n*, *k* and *q* (1<=β€<=*k*<=β€<=*n*<=β€<=100<=000, 1<=β€<=*q*<=β€<=100<=000).
The *i*-th (1<=β€<=*i*<=β€<=*n*) of the next *n* lines of the input contains an integer *a**i* (1<=β€<=*a**i*<=β€<=1<=000<=000), the number of flowers of species *i* Farmer John has initially.
The *j*-th (1<=β€<=*j*<=β€<=*q*) of the next *q* lines of the input contains an integer *c**j* (1<=β€<=*c**j*<=β€<=1<=000<=000), the number of flowers of a new species Farmer John receives on day *j*. | After each of the *q* days, output the sum of the maximum possible number of gardens, where the sum is taken over all possible choices of *k* species, modulo 109<=+<=7. | [
"3 3 2\n4\n6\n9\n8\n6\n",
"4 1 2\n6\n5\n4\n3\n2\n1\n"
] | [
"5\n16\n",
"20\n21\n"
] | In the first sample case, after the first day Farmer John has (4,β6,β9,β8) of each type of flower, and *k*β=β3.
Choosing (4,β6,β8) lets him make 2 gardens, each with (2,β3,β4) of each flower, respectively. Choosing (4,β6,β9), (4,β9,β8) and (6,β9,β8) each only let him make one garden, since there is no number of gardens that each species can be evenly split into. So the sum over all choices of *k*β=β3 flowers is 2β+β1β+β1β+β1β=β5.
After the second day, Farmer John has (4,β6,β9,β8,β6) of each flower. The sum over all choices is 1β+β2β+β2β+β1β+β1β+β2β+β2β+β3β+β1β+β1β=β16.
In the second sample case, *k*β=β1. With *x* flowers Farmer John can make *x* gardens. So the answers to the queries are 6β+β5β+β4β+β3β+β2β=β20 and 6β+β5β+β4β+β3β+β2β+β1β=β21. | [
{
"input": "3 3 2\n4\n6\n9\n8\n6",
"output": "5\n16"
},
{
"input": "4 1 2\n6\n5\n4\n3\n2\n1",
"output": "20\n21"
},
{
"input": "3 3 3\n6\n8\n10\n12\n14\n16",
"output": "8\n20\n42"
},
{
"input": "1 1 1\n1\n1",
"output": "2"
},
{
"input": "10 10 10\n10\n10\n10\n10\n... | 46 | 0 | 0 | 36,251 | |
418 | Tricky Password | [
"data structures"
] | null | null | In order to ensure confidentiality, the access to the "Russian Code Cup" problems is password protected during the problem development process.
To select a password, the jury can generate a special table that contains *n* columns and the infinite number of rows. To construct a table, the first row is fixed, and all the others are obtained by the following rule:
In the row *i* at position *p* there is a number equal to the number of times *a*[*i*<=-<=1][*p*] occurs on the prefix *a*[*i*<=-<=1][1... *p*].
To ensure the required level of confidentiality, the jury must be able to perform the following operations:
- Replace number *a*[1][*p*] by *v* and rebuild the table. - Find the number *a*[*x*][*y*], which will be the new password.
Doing all these steps manually is very tedious, so the jury asks you to help him. Write a program that responds to the request of the jury. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=100000) β the number of columns. The second line contains the description of the first row of the table, that is, *n* integers, which are not less than 1 and do not exceed 109.
The third line of the input contains an integer *m* (1<=β€<=*m*<=β€<=100000) β the number of requests.
Next, each row contains a description of the request, which consists of three integers:
- If the first number is equal to 1, then the remaining two numbers are *v*, *p* (1<=β€<=*v*<=β€<=109; 1<=β€<=*p*<=β€<=*n*). So, you should put value *v* in the position *p* in the first row. - If the first number is equal to 2, then the remaining two numbers are *x*, *y* (1<=β€<=*x*<=β€<=105; 1<=β€<=*y*<=β€<=*n*) β the row and column of the table cell from which you want to get value. | Print an answer for each request of the second type in the order you receive them. | [
"6\n1 2 2 2 3 1\n3\n2 2 3\n1 3 3\n2 3 4\n"
] | [
"2\n1\n"
] | none | [] | 15 | 0 | 0 | 36,266 | |
838 | Diverging Directions | [
"data structures",
"dfs and similar",
"trees"
] | null | null | You are given a directed weighted graph with *n* nodes and 2*n*<=-<=2 edges. The nodes are labeled from 1 to *n*, while the edges are labeled from 1 to 2*n*<=-<=2. The graph's edges can be split into two parts.
- The first *n*<=-<=1 edges will form a rooted spanning tree, with node 1 as the root. All these edges will point away from the root. - The last *n*<=-<=1 edges will be from node *i* to node 1, for all 2<=β€<=*i*<=β€<=*n*.
You are given *q* queries. There are two types of queries
- 1 *i* *w*: Change the weight of the *i*-th edge to *w* - 2 *u* *v*: Print the length of the shortest path between nodes *u* to *v*
Given these queries, print the shortest path lengths. | The first line of input will contain two integers *n*,<=*q* (2<=β€<=*n*,<=*q*<=β€<=200<=000), the number of nodes, and the number of queries, respectively.
The next 2*n*<=-<=2 integers will contain 3 integers *a**i*,<=*b**i*,<=*c**i*, denoting a directed edge from node *a**i* to node *b**i* with weight *c**i*.
The first *n*<=-<=1 of these lines will describe a rooted spanning tree pointing away from node 1, while the last *n*<=-<=1 of these lines will have *b**i*<==<=1.
More specifically,
- The edges (*a*1,<=*b*1),<=(*a*2,<=*b*2),<=... (*a**n*<=-<=1,<=*b**n*<=-<=1) will describe a rooted spanning tree pointing away from node 1. - *b**j*<==<=1 for *n*<=β€<=*j*<=β€<=2*n*<=-<=2. - *a**n*,<=*a**n*<=+<=1,<=...,<=*a*2*n*<=-<=2 will be distinct and between 2 and *n*.
The next *q* lines will contain 3 integers, describing a query in the format described in the statement.
All edge weights will be between 1 and 106. | For each type 2 query, print the length of the shortest path in its own line. | [
"5 9\n1 3 1\n3 2 2\n1 4 3\n3 5 4\n5 1 5\n3 1 6\n2 1 7\n4 1 8\n2 1 1\n2 1 3\n2 3 5\n2 5 2\n1 1 100\n2 1 3\n1 8 30\n2 4 2\n2 2 4\n"
] | [
"0\n1\n4\n8\n100\n132\n10\n"
] | none | [] | 46 | 0 | 0 | 36,369 | |
455 | Function | [
"data structures"
] | null | null | Serega and Fedor play with functions. One day they came across a very interesting function. It looks like that:
- *f*(1,<=*j*)<==<=*a*[*j*], 1<=β€<=*j*<=β€<=*n*. - *f*(*i*,<=*j*)<==<=*min*(*f*(*i*<=-<=1,<=*j*),<=*f*(*i*<=-<=1,<=*j*<=-<=1))<=+<=*a*[*j*], 2<=β€<=*i*<=β€<=*n*, *i*<=β€<=*j*<=β€<=*n*.
Here *a* is an integer array of length *n*.
Serega and Fedya want to know what values this function takes at some points. But they don't want to calculate the values manually. So they ask you to help them. | The first line contains integer *n* (1<=β€<=*n*<=β€<=105) β the length of array *a*. The next line contains *n* integers: *a*[1],<=*a*[2],<=...,<=*a*[*n*] (0<=β€<=*a*[*i*]<=β€<=104).
The next line contains integer *m* (1<=β€<=*m*<=β€<=105) β the number of queries. Each of the next *m* lines contains two integers: *x**i*, *y**i* (1<=β€<=*x**i*<=β€<=*y**i*<=β€<=*n*). Each line means that Fedor and Serega want to know the value of *f*(*x**i*,<=*y**i*). | Print *m* lines β the answers to the guys' queries. | [
"6\n2 2 3 4 3 4\n4\n4 5\n3 4\n3 4\n2 3\n",
"7\n1 3 2 3 4 0 2\n4\n4 5\n2 3\n1 4\n4 6\n"
] | [
"12\n9\n9\n5\n",
"11\n4\n3\n0\n"
] | none | [] | 15 | 0 | 0 | 36,427 | |
632 | Thief in a Shop | [
"divide and conquer",
"dp",
"fft",
"math"
] | null | null | A thief made his way to a shop.
As usual he has his lucky knapsack with him. The knapsack can contain *k* objects. There are *n* kinds of products in the shop and an infinite number of products of each kind. The cost of one product of kind *i* is *a**i*.
The thief is greedy, so he will take exactly *k* products (it's possible for some kinds to take several products of that kind).
Find all the possible total costs of products the thief can nick into his knapsack. | The first line contains two integers *n* and *k* (1<=β€<=*n*,<=*k*<=β€<=1000) β the number of kinds of products and the number of products the thief will take.
The second line contains *n* integers *a**i* (1<=β€<=*a**i*<=β€<=1000) β the costs of products for kinds from 1 to *n*. | Print the only line with all the possible total costs of stolen products, separated by a space. The numbers should be printed in the ascending order. | [
"3 2\n1 2 3\n",
"5 5\n1 1 1 1 1\n",
"3 3\n3 5 11\n"
] | [
"2 3 4 5 6\n",
"5\n",
"9 11 13 15 17 19 21 25 27 33\n"
] | none | [
{
"input": "3 2\n1 2 3",
"output": "2 3 4 5 6"
},
{
"input": "5 5\n1 1 1 1 1",
"output": "5"
},
{
"input": "3 3\n3 5 11",
"output": "9 11 13 15 17 19 21 25 27 33"
},
{
"input": "10 3\n3 4 12 5 7 13 5 6 1 6",
"output": "3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23... | 5,000 | 9,523,200 | 0 | 36,500 | |
258 | Little Elephant and Elections | [
"brute force",
"combinatorics",
"dp"
] | null | null | There have recently been elections in the zoo. Overall there were 7 main political parties: one of them is the Little Elephant Political Party, 6 other parties have less catchy names.
Political parties find their number in the ballot highly important. Overall there are *m* possible numbers: 1,<=2,<=...,<=*m*. Each of these 7 parties is going to be assigned in some way to exactly one number, at that, two distinct parties cannot receive the same number.
The Little Elephant Political Party members believe in the lucky digits 4 and 7. They want to evaluate their chances in the elections. For that, they need to find out, how many correct assignments are there, such that the number of lucky digits in the Little Elephant Political Party ballot number is strictly larger than the total number of lucky digits in the ballot numbers of 6 other parties.
Help the Little Elephant Political Party, calculate this number. As the answer can be rather large, print the remainder from dividing it by 1000000007 (109<=+<=7). | A single line contains a single positive integer *m* (7<=β€<=*m*<=β€<=109) β the number of possible numbers in the ballot. | In a single line print a single integer β the answer to the problem modulo 1000000007 (109<=+<=7). | [
"7\n",
"8\n"
] | [
"0\n",
"1440\n"
] | none | [
{
"input": "7",
"output": "0"
},
{
"input": "8",
"output": "1440"
},
{
"input": "47",
"output": "907362803"
},
{
"input": "10",
"output": "40320"
},
{
"input": "9",
"output": "10080"
},
{
"input": "11",
"output": "120960"
},
{
"input": "25"... | 218 | 21,606,400 | 0 | 36,506 | |
364 | Matrix | [
"combinatorics",
"data structures",
"implementation"
] | null | null | You have a string of decimal digits *s*. Let's define *b**ij*<==<=*s**i*Β·*s**j*. Find in matrix *b* the number of such rectangles that the sum *b**ij* for all cells (*i*,<=*j*) that are the elements of the rectangle equals *a* in each rectangle.
A rectangle in a matrix is a group of four integers (*x*,<=*y*,<=*z*,<=*t*) (*x*<=β€<=*y*,<=*z*<=β€<=*t*). The elements of the rectangle are all cells (*i*,<=*j*) such that *x*<=β€<=*i*<=β€<=*y*,<=*z*<=β€<=*j*<=β€<=*t*. | The first line contains integer *a* (0<=β€<=*a*<=β€<=109), the second line contains a string of decimal integers *s* (1<=β€<=|*s*|<=β€<=4000). | Print a single integer β the answer to a problem.
Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"10\n12345\n",
"16\n439873893693495623498263984765\n"
] | [
"6\n",
"40\n"
] | none | [
{
"input": "10\n12345",
"output": "6"
},
{
"input": "16\n439873893693495623498263984765",
"output": "40"
},
{
"input": "0\n1230",
"output": "19"
},
{
"input": "8398\n67950927224842887617892831243606761170908507858527",
"output": "0"
},
{
"input": "2\n11",
"out... | 1,000 | 124,006,400 | 0 | 36,513 | |
44 | Hyperdrive | [
"math"
] | D. Hyperdrive | 2 | 256 | In a far away galaxy there are *n* inhabited planets, numbered with numbers from 1 to *n*. They are located at large distances from each other, that's why the communication between them was very difficult until on the planet number 1 a hyperdrive was invented. As soon as this significant event took place, *n*<=-<=1 spaceships were built on the planet number 1, and those ships were sent to other planets to inform about the revolutionary invention.
Paradoxical thought it may be, but the hyperspace is represented as simple three-dimensional Euclidean space. The inhabited planets may be considered fixed points in it, and no two points coincide and no three points lie on the same straight line. The movement of a ship with a hyperdrive between two planets is performed along a straight line at the constant speed, the same for all the ships. That's why the distance in the hyperspace are measured in hyperyears (a ship with a hyperdrive covers a distance of *s* hyperyears in *s* years).
When the ship reaches an inhabited planet, the inhabitants of the planet dissemble it, make *n*<=-<=2 identical to it ships with a hyperdrive and send them to other *n*<=-<=2 planets (except for the one from which the ship arrived). The time to make a new ship compared to the time in which they move from one planet to another is so small that it can be disregarded. New ships are absolutely identical to the ones sent initially: they move at the same constant speed along a straight line trajectory and, having reached a planet, perform the very same mission, i.e. are dissembled to build new *n*<=-<=2 ships and send them to all the planets except for the one from which the ship arrived. Thus, the process of spreading the important news around the galaxy continues.
However the hyperdrive creators hurried to spread the news about their invention so much that they didn't study completely what goes on when two ships collide in the hyperspace. If two moving ships find themselves at one point, they provoke an explosion of colossal power, leading to the destruction of the galaxy!
Your task is to find the time the galaxy will continue to exist from the moment of the ships' launch from the first planet. | The first line contains a number *n* (3<=β€<=*n*<=β€<=5000) β the number of inhabited planets in the galaxy. The next *n* lines contain integer coordinates of the planets in format "*x**i* *y**i* *z**i*" (<=-<=104<=β€<=*x**i*,<=*y**i*,<=*z**i*<=β€<=104). | Print the single number β the solution to the task with an absolute or relative error not exceeding 10<=-<=6. | [
"4\n0 0 0\n0 0 1\n0 1 0\n1 0 0\n"
] | [
"1.7071067812\n"
] | none | [
{
"input": "4\n0 0 0\n0 0 1\n0 1 0\n1 0 0",
"output": "1.7071067812"
},
{
"input": "3\n5 -5 4\n-5 -4 2\n-1 1 2",
"output": "12.6839364452"
},
{
"input": "3\n28 -69 72\n-36 9 -49\n94 83 95",
"output": "266.2401228107"
},
{
"input": "4\n-7 -72 93\n-40 42 49\n31 76 -36\n-56 12 -... | 124 | 0 | 0 | 36,585 |
121 | Lucky Array | [
"data structures"
] | null | null | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya has an array consisting of *n* numbers. He wants to perform *m* operations of two types:
- add *l* *r* *d* β add an integer *d* to all elements whose indexes belong to the interval from *l* to *r*, inclusive (1<=β€<=*l*<=β€<=*r*<=β€<=*n*,<=1<=β€<=*d*<=β€<=104); - count *l* *r* β find and print on the screen how many lucky numbers there are among elements with indexes that belong to the interval from *l* to *r* inclusive (1<=β€<=*l*<=β€<=*r*<=β€<=*n*). Each lucky number should be counted as many times as it appears in the interval.
Petya has a list of all operations. The operations are such that after all additions the array won't have numbers that would exceed 104. Help Petya write a program that would perform these operations. | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=105) β the number of numbers in the array and the number of operations correspondingly. The second line contains *n* positive integers, none of which exceeds 104 β those are the array numbers. Next *m* lines contain operations, one per line. They correspond to the description given in the statement.
It is guaranteed that after all operations are fulfilled each number in the array will not exceed 104. | For each operation of the second type print the single number on the single line β the number of lucky numbers in the corresponding interval. | [
"3 6\n2 3 4\ncount 1 3\ncount 1 2\nadd 1 3 2\ncount 1 3\nadd 2 3 3\ncount 1 3\n",
"4 5\n4 4 4 4\ncount 1 4\nadd 1 4 3\ncount 1 4\nadd 2 3 40\ncount 1 4\n"
] | [
"1\n0\n1\n1\n",
"4\n4\n4\n"
] | In the first sample after the first addition the array will look in the following manner:
4 5 6
After the second addition:
4 8 9
The second sample after the first addition:
7 7 7 7
After the second addition:
7 47 47 7 | [] | 154 | 4,198,400 | 0 | 36,837 | |
0 | none | [
"none"
] | null | null | Radewoosh is playing a computer game. There are *n* levels, numbered 1 through *n*. Levels are divided into *k* regions (groups). Each region contains some positive number of consecutive levels.
The game repeats the the following process:
1. If all regions are beaten then the game ends immediately. Otherwise, the system finds the first region with at least one non-beaten level. Let *X* denote this region.1. The system creates an empty bag for tokens. Each token will represent one level and there may be many tokens representing the same level. For each already beaten level *i* in the region *X*, the system adds *t**i* tokens to the bag (tokens representing the *i*-th level). 1. Let *j* denote the first non-beaten level in the region *X*. The system adds *t**j* tokens to the bag. 1. Finally, the system takes a uniformly random token from the bag and a player starts the level represented by the token. A player spends one hour and beats the level, even if he has already beaten it in the past.
Given *n*, *k* and values *t*1,<=*t*2,<=...,<=*t**n*, your task is to split levels into regions. Each level must belong to exactly one region, and each region must contain non-empty consecutive set of levels. What is the minimum possible expected number of hours required to finish the game? | The first line of the input contains two integers *n* and *k* (1<=β€<=*n*<=β€<=200<=000, 1<=β€<=*k*<=β€<=*min*(50,<=*n*))Β β the number of levels and the number of regions, respectively.
The second line contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (1<=β€<=*t**i*<=β€<=100<=000). | Print one real numberΒ β the minimum possible expected value of the number of hours spent to finish the game if levels are distributed between regions in the optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=4.
Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct if . | [
"4 2\n100 3 5 7\n",
"6 2\n1 2 4 8 16 32\n"
] | [
"5.7428571429\n",
"8.5000000000\n"
] | In the first sample, we are supposed to split 4 levels into 2 regions. It's optimal to create the first region with only one level (it must be the first level). Then, the second region must contain other three levels.
In the second sample, it's optimal to split levels into two regions with 3 levels each. | [] | 3,000 | 53,555,200 | 0 | 36,864 | |
65 | Harry Potter and the Golden Snitch | [
"binary search",
"geometry"
] | C. Harry Potter and the Golden Snitch | 2 | 256 | Brothers Fred and George Weasley once got into the sporting goods store and opened a box of Quidditch balls. After long and painful experiments they found out that the Golden Snitch is not enchanted at all. It is simply a programmed device. It always moves along the same trajectory, which is a polyline with vertices at the points (*x*0,<=*y*0,<=*z*0), (*x*1,<=*y*1,<=*z*1), ..., (*x**n*,<=*y**n*,<=*z**n*). At the beginning of the game the snitch is positioned at the point (*x*0,<=*y*0,<=*z*0), and then moves along the polyline at the constant speed *v**s*. The twins have not yet found out how the snitch behaves then. Nevertheless, they hope that the retrieved information will help Harry Potter and his team in the upcoming match against Slytherin. Harry Potter learned that at the beginning the game he will be at the point (*P**x*,<=*P**y*,<=*P**z*) and his super fast Nimbus 2011 broom allows him to move at the constant speed *v**p* in any direction or remain idle. *v**p* is not less than the speed of the snitch *v**s*. Harry Potter, of course, wants to catch the snitch as soon as possible. Or, if catching the snitch while it is moving along the polyline is impossible, he wants to hurry the Weasley brothers with their experiments. Harry Potter catches the snitch at the time when they are at the same point. Help Harry. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=10000). The following *n*<=+<=1 lines contain the coordinates *x**i*, *y**i*, *z**i*, separated by single spaces. The coordinates of any two consecutive points do not coincide. The next line contains the velocities *v**p* and *v**s*, the last line contains *P**x*, *P**y*, *P**z*, separated by single spaces. All the numbers in the input are integers, their absolute value does not exceed 104. The speeds are strictly positive. It is guaranteed that *v**s*<=β€<=*v**p*. | If Harry Potter can catch the snitch while it is moving along the polyline (including the end (*x**n*,<=*y**n*,<=*z**n*)), print "YES" in the first line (without the quotes). Print in the second line *t*, which is the earliest moment of time, when Harry will be able to catch the snitch. On the third line print three numbers *X*, *Y*, *Z*, the coordinates of the point at which this happens. The absolute or relative error in the answer should not exceed 10<=-<=6. If Harry is not able to catch the snitch during its moving along the described polyline, print "NO". | [
"4\n0 0 0\n0 10 0\n10 10 0\n10 0 0\n0 0 0\n1 1\n5 5 25\n",
"4\n0 0 0\n0 10 0\n10 10 0\n10 0 0\n0 0 0\n1 1\n5 5 50\n",
"1\n1 2 3\n4 5 6\n20 10\n1 2 3\n"
] | [
"YES\n25.5000000000\n10.0000000000 4.5000000000 0.0000000000\n",
"NO\n",
"YES\n0.0000000000\n1.0000000000 2.0000000000 3.0000000000\n"
] | none | [
{
"input": "4\n0 0 0\n0 10 0\n10 10 0\n10 0 0\n0 0 0\n1 1\n5 5 25",
"output": "YES\n25.5000000000\n10.0000000000 4.5000000000 0.0000000000"
},
{
"input": "4\n0 0 0\n0 10 0\n10 10 0\n10 0 0\n0 0 0\n1 1\n5 5 50",
"output": "NO"
},
{
"input": "1\n1 2 3\n4 5 6\n20 10\n1 2 3",
"output": "... | 248 | 3,891,200 | 3.930752 | 36,928 |
59 | Title | [
"expression parsing"
] | C. Title | 2 | 256 | Vasya has recently finished writing a book. Now he faces the problem of giving it the title. Vasya wants the title to be vague and mysterious for his book to be noticeable among others. That's why the title should be represented by a single word containing at least once each of the first *k* Latin letters and not containing any other ones. Also, the title should be a palindrome, that is it should be read similarly from the left to the right and from the right to the left.
Vasya has already composed the approximate variant of the title. You are given the title template *s* consisting of lowercase Latin letters and question marks. Your task is to replace all the question marks by lowercase Latin letters so that the resulting word satisfies the requirements, described above. Each question mark should be replaced by exactly one letter, it is not allowed to delete characters or add new ones to the template. If there are several suitable titles, choose the first in the alphabetical order, for Vasya's book to appear as early as possible in all the catalogues. | The first line contains an integer *k* (1<=β€<=*k*<=β€<=26) which is the number of allowed alphabet letters. The second line contains *s* which is the given template. In *s* only the first *k* lowercase letters of Latin alphabet and question marks can be present, the length of *s* is from 1 to 100 characters inclusively. | If there is no solution, print IMPOSSIBLE. Otherwise, a single line should contain the required title, satisfying the given template. The title should be a palindrome and it can only contain the first *k* letters of the Latin alphabet. At that, each of those *k* letters must be present at least once. If there are several suitable titles, print the lexicographically minimal one.
The lexicographical comparison is performed by the standard < operator in modern programming languages. The line *a* is lexicographically smaller than the line *b*, if exists such an *i* (1<=β€<=*i*<=β€<=|*s*|), that *a**i*<=<<=*b**i*, and for any *j* (1<=β€<=*j*<=<<=*i*) *a**j*<==<=*b**j*. |*s*| stands for the length of the given template. | [
"3\na?c\n",
"2\na??a\n",
"2\n?b?a\n"
] | [
"IMPOSSIBLE\n",
"abba\n",
"abba\n"
] | none | [
{
"input": "3\na?c",
"output": "IMPOSSIBLE"
},
{
"input": "2\na??a",
"output": "abba"
},
{
"input": "2\n?b?a",
"output": "abba"
},
{
"input": "3\n????",
"output": "IMPOSSIBLE"
},
{
"input": "2\n????",
"output": "abba"
},
{
"input": "1\n?",
"output"... | 248 | 20,172,800 | 3.900425 | 36,929 |
840 | On the Bench | [
"combinatorics",
"dp"
] | null | null | A year ago on the bench in public park Leha found an array of *n* numbers. Leha believes that permutation *p* is right if for all 1<=β€<=*i*<=<<=*n* condition, that *a**p**i*Β·*a**p**i*<=+<=1 is not perfect square, holds. Leha wants to find number of right permutations modulo 109<=+<=7. | First line of input data contains single integer *n* (1<=β€<=*n*<=β€<=300) β length of the array.
Next line contains *n* integers *a*1,<=*a*2,<=... ,<=*a**n* (1<=β€<=*a**i*<=β€<=109) β found array. | Output single integer β number of right permutations modulo 109<=+<=7. | [
"3\n1 2 4\n",
"7\n5 2 4 2 4 1 1\n"
] | [
"2\n",
"144\n"
] | For first example:
[1,β2,β4] β right permutation, because 2 and 8 are not perfect squares.
[1,β4,β2] β wrong permutation, because 4 is square of 2.
[2,β1,β4] β wrong permutation, because 4 is square of 2.
[2,β4,β1] β wrong permutation, because 4 is square of 2.
[4,β1,β2] β wrong permutation, because 4 is square of 2.
[4,β2,β1] β right permutation, because 8 and 2 are not perfect squares. | [
{
"input": "3\n1 2 4",
"output": "2"
},
{
"input": "7\n5 2 4 2 4 1 1",
"output": "144"
},
{
"input": "10\n3 1 1 2 1 3 4 4 1 4",
"output": "0"
},
{
"input": "50\n873 838 288 87 889 364 720 410 565 651 577 356 740 99 549 592 994 385 777 435 486 118 887 440 749 533 356 790 413 6... | 93 | 307,200 | -1 | 37,039 | |
288 | Polo the Penguin and Trees | [
"combinatorics",
"dfs and similar",
"trees"
] | null | null | Little penguin Polo has got a tree β a non-directed connected acyclic graph, containing *n* nodes and *n*<=-<=1 edges. We will consider the tree nodes numbered by integers from 1 to *n*.
Today Polo wonders, how to find the number of pairs of paths that don't have common nodes. More formally, he should find the number of groups of four integers *a*,<=*b*,<=*c* and *d* such that:
- 1<=β€<=*a*<=<<=*b*<=β€<=*n*; - 1<=β€<=*c*<=<<=*d*<=β€<=*n*; - there's no such node that lies on both the shortest path from node *a* to node *b* and from node *c* to node *d*.
The shortest path betweem two nodes is the path that is shortest in the number of edges.
Help Polo solve this problem. | The first line contains integer *n* (1<=β€<=*n*<=β€<=80000) β the number of tree nodes. Each of the following *n*<=-<=1 lines contains a pair of integers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*;Β *u**i*<=β <=*v**i*) β the *i*-th edge of the tree.
It is guaranteed that the given graph is a tree. | In a single line print a single integer β the answer to the problem.
Please do not use the %lld specificator to read or write 64-bit numbers in Π‘++. It is recommended to use the cin, cout streams or the %I64d specificator. | [
"4\n1 2\n2 3\n3 4\n"
] | [
"2\n"
] | none | [] | 1,340 | 58,060,800 | -1 | 37,062 | |
208 | Blood Cousins | [
"binary search",
"data structures",
"dfs and similar",
"trees"
] | null | null | Polycarpus got hold of a family relationship tree. The tree describes family relationships of *n* people, numbered 1 through *n*. Each person in the tree has no more than one parent.
Let's call person *a* a 1-ancestor of person *b*, if *a* is the parent of *b*.
Let's call person *a* a *k*-ancestor (*k*<=><=1) of person *b*, if person *b* has a 1-ancestor, and *a* is a (*k*<=-<=1)-ancestor of *b*'s 1-ancestor.
Family relationships don't form cycles in the found tree. In other words, there is no person who is his own ancestor, directly or indirectly (that is, who is an *x*-ancestor for himself, for some *x*, *x*<=><=0).
Let's call two people *x* and *y* (*x*<=β <=*y*) *p*-th cousins (*p*<=><=0), if there is person *z*, who is a *p*-ancestor of *x* and a *p*-ancestor of *y*.
Polycarpus wonders how many counsins and what kinds of them everybody has. He took a piece of paper and wrote *m* pairs of integers *v**i*, *p**i*. Help him to calculate the number of *p**i*-th cousins that person *v**i* has, for each pair *v**i*, *p**i*. | The first input line contains a single integer *n* (1<=β€<=*n*<=β€<=105) β the number of people in the tree. The next line contains *n* space-separated integers *r*1,<=*r*2,<=...,<=*r**n*, where *r**i* (1<=β€<=*r**i*<=β€<=*n*) is the number of person *i*'s parent or 0, if person *i* has no parent. It is guaranteed that family relationships don't form cycles.
The third line contains a single number *m* (1<=β€<=*m*<=β€<=105) β the number of family relationship queries Polycarus has. Next *m* lines contain pairs of space-separated integers. The *i*-th line contains numbers *v**i*, *p**i* (1<=β€<=*v**i*,<=*p**i*<=β€<=*n*). | Print *m* space-separated integers β the answers to Polycarpus' queries. Print the answers to the queries in the order, in which the queries occur in the input. | [
"6\n0 1 1 0 4 4\n7\n1 1\n1 2\n2 1\n2 2\n4 1\n5 1\n6 1\n"
] | [
"0 0 1 0 0 1 1 \n"
] | none | [
{
"input": "6\n0 1 1 0 4 4\n7\n1 1\n1 2\n2 1\n2 2\n4 1\n5 1\n6 1",
"output": "0 0 1 0 0 1 1 "
},
{
"input": "1\n0\n20\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1",
"output": "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "
},
{
"input": "... | 1,466 | 109,772,800 | 3 | 37,083 | |
81 | Polycarp's Picture Gallery | [
"constructive algorithms",
"greedy"
] | D. Polycarp's Picture Gallery | 2 | 256 | Polycarp loves not only to take pictures, but also to show his photos to friends. On his personal website he has recently installed a widget that can display *n* photos with the scroll option. At each moment of time the widget displays exactly one photograph with the option showing the previous/next one. From the first photo, you can switch to the second one or to the *n*-th one, from the second photo you can switch to the third one or to the first one, etc. Thus, navigation is performed in a cycle.
Polycarp's collection consists of *m* photo albums, the *i*-th album contains *a**i* photos. Polycarp wants to choose *n* photos and put them on a new widget. To make watching the photos interesting to the visitors, he is going to post pictures so that no two photos from one album were neighboring (each photo will have exactly two neighbors, the first photo's neighbors are the second and the *n*-th one).
Help Polycarp compile a photo gallery. Select *n* photos from his collection and put them in such order that no two photos from one album went one after the other. | The first line contains two integers *n* and *m* (3<=β€<=*n*<=β€<=1000, 1<=β€<=*m*<=β€<=40), where *n* is the number of photos on the widget, and *m* is the number of albums. The second line contains *m* integers *a*1,<=*a*2,<=...,<=*a**m* (1<=β€<=*a**i*<=β€<=1000), where *a**i* is the number of photos in the *i*-th album. | Print the single number -1 if there is no solution. Otherwise, print *n* numbers *t*1,<=*t*2,<=...,<=*t**n*, where *t**i* represents the number of the album of the *i*-th picture in the widget. The albums are numbered from 1 in the order of their appearance in the input. If there are several solutions, print any of them. | [
"4 3\n1 3 5\n",
"10 2\n5 5\n",
"10 3\n1 10 3\n"
] | [
"3 1 3 2\n",
"2 1 2 1 2 1 2 1 2 1\n",
"-1\n"
] | none | [] | 186 | 0 | 0 | 37,196 |
505 | Mr. Kitayuta's Technology | [
"dfs and similar"
] | null | null | Shuseki Kingdom is the world's leading nation for innovation and technology. There are *n* cities in the kingdom, numbered from 1 to *n*.
Thanks to Mr. Kitayuta's research, it has finally become possible to construct teleportation pipes between two cities. A teleportation pipe will connect two cities unidirectionally, that is, a teleportation pipe from city *x* to city *y* cannot be used to travel from city *y* to city *x*. The transportation within each city is extremely developed, therefore if a pipe from city *x* to city *y* and a pipe from city *y* to city *z* are both constructed, people will be able to travel from city *x* to city *z* instantly.
Mr. Kitayuta is also involved in national politics. He considers that the transportation between the *m* pairs of city (*a**i*,<=*b**i*) (1<=β€<=*i*<=β€<=*m*) is important. He is planning to construct teleportation pipes so that for each important pair (*a**i*,<=*b**i*), it will be possible to travel from city *a**i* to city *b**i* by using one or more teleportation pipes (but not necessarily from city *b**i* to city *a**i*). Find the minimum number of teleportation pipes that need to be constructed. So far, no teleportation pipe has been constructed, and there is no other effective transportation between cities. | The first line contains two space-separated integers *n* and *m* (2<=β€<=*n*<=β€<=105,<=1<=β€<=*m*<=β€<=105), denoting the number of the cities in Shuseki Kingdom and the number of the important pairs, respectively.
The following *m* lines describe the important pairs. The *i*-th of them (1<=β€<=*i*<=β€<=*m*) contains two space-separated integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*,<=*a**i*<=β <=*b**i*), denoting that it must be possible to travel from city *a**i* to city *b**i* by using one or more teleportation pipes (but not necessarily from city *b**i* to city *a**i*). It is guaranteed that all pairs (*a**i*,<=*b**i*) are distinct. | Print the minimum required number of teleportation pipes to fulfill Mr. Kitayuta's purpose. | [
"4 5\n1 2\n1 3\n1 4\n2 3\n2 4\n",
"4 6\n1 2\n1 4\n2 3\n2 4\n3 2\n3 4\n"
] | [
"3\n",
"4\n"
] | For the first sample, one of the optimal ways to construct pipes is shown in the image below:
For the second sample, one of the optimal ways is shown below: | [
{
"input": "4 5\n1 2\n1 3\n1 4\n2 3\n2 4",
"output": "3"
},
{
"input": "4 6\n1 2\n1 4\n2 3\n2 4\n3 2\n3 4",
"output": "4"
},
{
"input": "4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4",
"output": "3"
},
{
"input": "3 6\n1 2\n1 3\n2 1\n2 3\n3 1\n3 2",
"output": "3"
},
{
"input"... | 62 | 0 | 0 | 37,206 | |
573 | Bear and Bowling | [
"data structures",
"greedy"
] | null | null | Limak is an old brown bear. He often goes bowling with his friends. Today he feels really good and tries to beat his own record!
For rolling a ball one gets a score β an integer (maybe negative) number of points. Score for *i*-th roll is multiplied by *i* and scores are summed up. So, for *k* rolls with scores *s*1,<=*s*2,<=...,<=*s**k*, total score is . Total score is 0 if there were no rolls.
Limak made *n* rolls and got score *a**i* for *i*-th of them. He wants to maximize his total score and he came up with an interesting idea. He will cancel some rolls, saying that something distracted him or there was a strong wind.
Limak is able to cancel any number of rolls, maybe even all or none of them. Total score is calculated as if there were only non-canceled rolls. Look at the sample tests for clarification. What maximum total score can Limak get? | The first line contains single integer *n* (1<=β€<=*n*<=β€<=105).
The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (|*a**i*|<=β€<=107) - scores for Limak's rolls. | Print the maximum possible total score after choosing rolls to cancel. | [
"5\n-2 -8 0 5 -3\n",
"6\n-10 20 -30 40 -50 60\n"
] | [
"13\n",
"400\n"
] | In first sample Limak should cancel rolls with scores β-β8 and β-β3. Then he is left with three rolls with scores β-β2,β0,β5. Total score is 1Β·(β-β2)β+β2Β·0β+β3Β·5β=β13.
In second sample Limak should cancel roll with score β-β50. Total score is 1Β·(β-β10)β+β2Β·20β+β3Β·(β-β30)β+β4Β·40β+β5Β·60β=β400. | [] | 46 | 0 | -1 | 37,321 | |
79 | Beaver | [
"data structures",
"dp",
"greedy",
"hashing",
"strings",
"two pointers"
] | C. Beaver | 2 | 256 | After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string *s*, and she tried to remember the string *s* correctly.
However, Ciel feels *n* strings *b*1,<=*b*2,<=... ,<=*b**n* are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of *s*.
Determine the longest contiguous substring of *s* that does not contain any boring string, so that she can remember the longest part of Taro's response. | In the first line there is a string *s*. The length of *s* will be between 1 and 105, inclusive.
In the second line there is a single integer *n* (1<=β€<=*n*<=β€<=10). Next *n* lines, there is a string *b**i* (1<=β€<=*i*<=β€<=*n*). Each length of *b**i* will be between 1 and 10, inclusive.
Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive. | Output in the first line two space-separated integers *len* and *pos*: the length of the longest contiguous substring of *s* that does not contain any *b**i*, and the first position of the substring (0-indexed). The position *pos* must be between 0 and |*s*|<=-<=*len* inclusive, where |*s*| is the length of string *s*.
If there are several solutions, output any. | [
"Go_straight_along_this_street\n5\nstr\nlong\ntree\nbiginteger\nellipse\n",
"IhaveNoIdea\n9\nI\nh\na\nv\ne\nN\no\nI\nd\n",
"unagioisii\n2\nioi\nunagi\n"
] | [
"12 4\n",
"0 0\n",
"5 5\n"
] | In the first sample, the solution is traight_alon.
In the second sample, the solution is an empty string, so the output can be Β«0 0Β», Β«0 1Β», Β«0 2Β», and so on.
In the third sample, the solution is either nagio or oisii. | [
{
"input": "Go_straight_along_this_street\n5\nstr\nlong\ntree\nbiginteger\nellipse",
"output": "12 4"
},
{
"input": "IhaveNoIdea\n9\nI\nh\na\nv\ne\nN\no\nI\nd",
"output": "0 0"
},
{
"input": "unagioisii\n2\nioi\nunagi",
"output": "5 5"
},
{
"input": "abcabcabcabc\n3\nabcabca\... | 310 | 4,608,000 | 0 | 37,408 |
725 | Contest Balloons | [
"data structures",
"greedy"
] | null | null | One tradition of ACM-ICPC contests is that a team gets a balloon for every solved problem. We assume that the submission time doesn't matter and teams are sorted only by the number of balloons they have. It means that one's place is equal to the number of teams with more balloons, increased by 1. For example, if there are seven teams with more balloons, you get the eight place. Ties are allowed.
You should know that it's important to eat before a contest. If the number of balloons of a team is greater than the weight of this team, the team starts to float in the air together with their workstation. They eventually touch the ceiling, what is strictly forbidden by the rules. The team is then disqualified and isn't considered in the standings.
A contest has just finished. There are *n* teams, numbered 1 through *n*. The *i*-th team has *t**i* balloons and weight *w**i*. It's guaranteed that *t**i* doesn't exceed *w**i* so nobody floats initially.
Limak is a member of the first team. He doesn't like cheating and he would never steal balloons from other teams. Instead, he can give his balloons away to other teams, possibly making them float. Limak can give away zero or more balloons of his team. Obviously, he can't give away more balloons than his team initially has.
What is the best place Limak can get? | The first line of the standard input contains one integer *n* (2<=β€<=*n*<=β€<=300<=000)Β β the number of teams.
The *i*-th of *n* following lines contains two integers *t**i* and *w**i* (0<=β€<=*t**i*<=β€<=*w**i*<=β€<=1018)Β β respectively the number of balloons and the weight of the *i*-th team. Limak is a member of the first team. | Print one integer denoting the best place Limak can get. | [
"8\n20 1000\n32 37\n40 1000\n45 50\n16 16\n16 16\n14 1000\n2 1000\n",
"7\n4 4\n4 4\n4 4\n4 4\n4 4\n4 4\n5 5\n",
"7\n14000000003 1000000000000000000\n81000000000 88000000000\n5000000000 7000000000\n15000000000 39000000000\n46000000000 51000000000\n0 1000000000\n0 0\n"
] | [
"3\n",
"2\n",
"2\n"
] | In the first sample, Limak has 20 balloons initially. There are three teams with more balloons (32, 40 and 45 balloons), so Limak has the fourth place initially. One optimal strategy is:
1. Limak gives 6 balloons away to a team with 32 balloons and weight 37, which is just enough to make them fly. Unfortunately, Limak has only 14 balloons now and he would get the fifth place.1. Limak gives 6 balloons away to a team with 45 balloons. Now they have 51 balloons and weight 50 so they fly and get disqualified.1. Limak gives 1 balloon to each of two teams with 16 balloons initially.1. Limak has 20β-β6β-β6β-β1β-β1β=β6 balloons.1. There are three other teams left and their numbers of balloons are 40, 14 and 2.1. Limak gets the third place because there are two teams with more balloons.
In the second sample, Limak has the second place and he can't improve it.
In the third sample, Limak has just enough balloons to get rid of teams 2, 3 and 5 (the teams with 81β000β000β000, 5β000β000β000 and 46β000β000β000 balloons respectively). With zero balloons left, he will get the second place (ex-aequo with team 6 and team 7). | [
{
"input": "8\n20 1000\n32 37\n40 1000\n45 50\n16 16\n16 16\n14 1000\n2 1000",
"output": "3"
},
{
"input": "7\n4 4\n4 4\n4 4\n4 4\n4 4\n4 4\n5 5",
"output": "2"
},
{
"input": "7\n14000000003 1000000000000000000\n81000000000 88000000000\n5000000000 7000000000\n15000000000 39000000000\n460... | 3,000 | 57,446,400 | 0 | 37,473 | |
42 | Strange town | [
"constructive algorithms",
"math"
] | D. Strange town | 2 | 256 | Volodya has recently visited a very odd town. There are *N* tourist attractions in the town and every two of them are connected by a bidirectional road. Each road has some travel price (natural number) assigned to it and all prices are distinct. But the most striking thing about this town is that each city sightseeing tour has the same total price! That is, if we choose any city sightseeing tour β a cycle which visits every attraction exactly once β the sum of the costs of the tour roads is independent of the tour. Volodya is curious if you can find such price system with all road prices not greater than 1000. | Input contains just one natural number (3<=β€<=*N*<=β€<=20) β the number of town attractions. | Output should contain *N* rows containing *N* positive integer numbers each β the adjacency matrix of the prices graph (thus, *j*-th number in *i*-th row should be equal to the price of the road between the *j*-th and the *i*-th attraction). Diagonal numbers should be equal to zero. All numbers should not be greater than 1000. All prices should be positive and pairwise distinct. If there are several solutions, output any of them. | [
"3\n"
] | [
"0 3 4 \n3 0 5 \n4 5 0 \n"
] | none | [
{
"input": "3",
"output": "0 3 4 \n3 0 5 \n4 5 0 "
},
{
"input": "4",
"output": "0 3 4 6 \n3 0 5 7 \n4 5 0 8 \n6 7 8 0 "
},
{
"input": "5",
"output": "0 3 4 6 9 \n3 0 5 7 10 \n4 5 0 8 11 \n6 7 8 0 13 \n9 10 11 13 0 "
},
{
"input": "6",
"output": "0 3 4 6 9 14 \n3 0 5 7 10... | 30 | 0 | 0 | 37,511 |
286 | Shifting | [
"implementation"
] | null | null | John Doe has found the beautiful permutation formula.
Let's take permutation *p*<==<=*p*1,<=*p*2,<=...,<=*p**n*. Let's define transformation *f* of this permutation:
where *k* (*k*<=><=1) is an integer, the transformation parameter, *r* is such maximum integer that *rk*<=β€<=*n*. If *rk*<==<=*n*, then elements *p**rk*<=+<=1,<=*p**rk*<=+<=2 and so on are omitted. In other words, the described transformation of permutation *p* cyclically shifts to the left each consecutive block of length *k* and the last block with the length equal to the remainder after dividing *n* by *k*.
John Doe thinks that permutation *f*(*f*(Β ...Β *f*(*p*<==<=[1,<=2,<=...,<=*n*],<=2)Β ...Β ,<=*n*<=-<=1),<=*n*) is beautiful. Unfortunately, he cannot quickly find the beautiful permutation he's interested in. That's why he asked you to help him.
Your task is to find a beautiful permutation for the given *n*. For clarifications, see the notes to the third sample. | A single line contains integer *n* (2<=β€<=*n*<=β€<=106). | Print *n* distinct space-separated integers from 1 to *n* β a beautiful permutation of size *n*. | [
"2\n",
"3\n",
"4\n"
] | [
"2 1 \n",
"1 3 2 \n",
"4 2 3 1 \n"
] | A note to the third test sample:
- *f*([1,β2,β3,β4],β2)β=β[2,β1,β4,β3] - *f*([2,β1,β4,β3],β3)β=β[1,β4,β2,β3] - *f*([1,β4,β2,β3],β4)β=β[4,β2,β3,β1] | [] | 62 | 0 | 0 | 37,602 | |
884 | Bertown Subway | [
"dfs and similar",
"greedy",
"math"
] | null | null | The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself.
There are *n* stations in the subway. It was built according to the Bertown Transport Law:
1. For each station *i* there exists exactly one train that goes from this station. Its destination station is *p**i*, possibly *p**i*<==<=*i*; 1. For each station *i* there exists exactly one station *j* such that *p**j*<==<=*i*.
The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (*x*,<=*y*) such that person can start at station *x* and, after taking some subway trains (possibly zero), arrive at station *y* (1<=β€<=*x*,<=*y*<=β€<=*n*).
The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of *p**i* for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes.
The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get! | The first line contains one integer number *n* (1<=β€<=*n*<=β€<=100000) β the number of stations.
The second line contains *n* integer numbers *p*1, *p*2, ..., *p**n* (1<=β€<=*p**i*<=β€<=*n*) β the current structure of the subway. All these numbers are distinct. | Print one number β the maximum possible value of convenience. | [
"3\n2 1 3\n",
"5\n1 5 4 3 2\n"
] | [
"9\n",
"17\n"
] | In the first example the mayor can change *p*<sub class="lower-index">2</sub> to 3 and *p*<sub class="lower-index">3</sub> to 1, so there will be 9 pairs: (1,β1), (1,β2), (1,β3), (2,β1), (2,β2), (2,β3), (3,β1), (3,β2), (3,β3).
In the second example the mayor can change *p*<sub class="lower-index">2</sub> to 4 and *p*<sub class="lower-index">3</sub> to 5. | [
{
"input": "3\n2 1 3",
"output": "9"
},
{
"input": "5\n1 5 4 3 2",
"output": "17"
},
{
"input": "1\n1",
"output": "1"
},
{
"input": "2\n1 2",
"output": "4"
},
{
"input": "2\n2 1",
"output": "4"
},
{
"input": "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 ... | 46 | 716,800 | -1 | 37,669 | |
105 | Dark Assembly | [
"brute force",
"probabilities"
] | B. Dark Assembly | 2 | 256 | Dark Assembly is a governing body in the Netherworld. Here sit the senators who take the most important decisions for the player. For example, to expand the range of the shop or to improve certain characteristics of the character the Dark Assembly's approval is needed.
The Dark Assembly consists of *n* senators. Each of them is characterized by his level and loyalty to the player. The level is a positive integer which reflects a senator's strength. Loyalty is the probability of a positive decision in the voting, which is measured as a percentage with precision of up to 10%.
Senators make decisions by voting. Each of them makes a positive or negative decision in accordance with their loyalty. If strictly more than half of the senators take a positive decision, the player's proposal is approved.
If the player's proposal is not approved after the voting, then the player may appeal against the decision of the Dark Assembly. To do that, player needs to kill all the senators that voted against (there's nothing wrong in killing senators, they will resurrect later and will treat the player even worse). The probability that a player will be able to kill a certain group of senators is equal to *A*<=/<=(*A*<=+<=*B*), where *A* is the sum of levels of all player's characters and *B* is the sum of levels of all senators in this group. If the player kills all undesired senators, then his proposal is approved.
Senators are very fond of sweets. They can be bribed by giving them candies. For each received candy a senator increases his loyalty to the player by 10%. It's worth to mention that loyalty cannot exceed 100%. The player can take no more than *k* sweets to the courtroom. Candies should be given to the senators before the start of voting.
Determine the probability that the Dark Assembly approves the player's proposal if the candies are distributed among the senators in the optimal way. | The first line contains three integers *n*, *k* and *A* (1<=β€<=*n*,<=*k*<=β€<=8, 1<=β€<=*A*<=β€<=9999).
Then *n* lines follow. The *i*-th of them contains two numbers β *b**i* and *l**i* β the *i*-th senator's level and his loyalty.
The levels of all senators are integers in range from 1 to 9999 (inclusive). The loyalties of all senators are integers in range from 0 to 100 (inclusive) and all of them are divisible by 10. | Print one real number with precision 10<=-<=6 β the maximal possible probability that the Dark Assembly approves the player's proposal for the best possible distribution of candies among the senators. | [
"5 6 100\n11 80\n14 90\n23 70\n80 30\n153 70\n",
"5 3 100\n11 80\n14 90\n23 70\n80 30\n153 70\n",
"1 3 20\n20 20\n"
] | [
"1.0000000000\n",
"0.9628442962\n",
"0.7500000000\n"
] | In the first sample the best way of candies' distribution is giving them to first three of the senators. It ensures most of votes.
It the second sample player should give all three candies to the fifth senator. | [
{
"input": "5 6 100\n11 80\n14 90\n23 70\n80 30\n153 70",
"output": "1.0000000000"
},
{
"input": "5 3 100\n11 80\n14 90\n23 70\n80 30\n153 70",
"output": "0.9628442962"
},
{
"input": "1 3 20\n20 20",
"output": "0.7500000000"
},
{
"input": "4 3 40\n10 40\n11 50\n10 50\n9 50",
... | 2,000 | 4,915,200 | 0 | 37,731 |
41 | Pawn | [
"dp"
] | D. Pawn | 2 | 256 | On some square in the lowest row of a chessboard a stands a pawn. It has only two variants of moving: upwards and leftwards or upwards and rightwards. The pawn can choose from which square of the lowest row it can start its journey. On each square lay from 0 to 9 peas. The pawn wants to reach the uppermost row having collected as many peas as possible. As there it will have to divide the peas between itself and its *k* brothers, the number of peas must be divisible by *k*<=+<=1. Find the maximal number of peas it will be able to collect and which moves it should make to do it.
The pawn cannot throw peas away or leave the board. When a pawn appears in some square of the board (including the first and last square of the way), it necessarily takes all the peas. | The first line contains three integers *n*, *m*, *k* (2<=β€<=*n*,<=*m*<=β€<=100,<=0<=β€<=*k*<=β€<=10) β the number of rows and columns on the chessboard, the number of the pawn's brothers. Then follow *n* lines containing each *m* numbers from 0 to 9 without spaces β the chessboard's description. Each square is described by one number β the number of peas in it. The first line corresponds to the uppermost row and the last line β to the lowest row. | If it is impossible to reach the highest row having collected the number of peas divisible by *k*<=+<=1, print -1.
Otherwise, the first line must contain a single number β the maximal number of peas the pawn can collect given that the number must be divisible by *k*<=+<=1. The second line must contain a single number β the number of the square's column in the lowest row, from which the pawn must start its journey. The columns are numbered from the left to the right with integral numbers starting from 1. The third line must contain a line consisting of *n*<=-<=1 symbols β the description of the pawn's moves. If the pawn must move upwards and leftwards, print L, if it must move upwards and rightwards, print R. If there are several solutions to that problem, print any of them. | [
"3 3 1\n123\n456\n789\n",
"3 3 0\n123\n456\n789\n",
"2 2 10\n98\n75\n"
] | [
"16\n2\nRL\n",
"17\n3\nLR\n",
"-1\n"
] | none | [
{
"input": "3 3 1\n123\n456\n789",
"output": "16\n2\nRL"
},
{
"input": "3 3 0\n123\n456\n789",
"output": "17\n3\nLR"
},
{
"input": "2 2 10\n98\n75",
"output": "-1"
},
{
"input": "3 4 2\n8244\n4768\n4474",
"output": "18\n3\nLR"
},
{
"input": "4 3 10\n194\n707\n733\... | 122 | 5,836,800 | 0 | 37,762 |
855 | Harry Vs Voldemort | [
"dfs and similar",
"dp",
"graphs",
"trees"
] | null | null | After destroying all of Voldemort's Horcruxes, Harry and Voldemort are up for the final battle. They each cast spells from their wands and the spells collide.
The battle scene is Hogwarts, which can be represented in the form of a tree. There are, in total, *n* places in Hogwarts joined using *n*<=-<=1 undirected roads.
Ron, who was viewing this battle between Harry and Voldemort, wondered how many triplets of places (*u*,<=*v*,<=*w*) are there such that if Harry is standing at place *u* and Voldemort is standing at place *v*, their spells collide at a place *w*. This is possible for a triplet only when *u*, *v* and *w* are distinct, and there exist paths from *u* to *w* and from *v* to *w* which do not pass through the same roads.
Now, due to the battle havoc, new paths are being added all the time. You have to tell Ron the answer after each addition.
Formally, you are given a tree with *n* vertices and *n*<=-<=1 edges. *q* new edges are being added between the nodes of the tree. After each addition you need to tell the number of triplets (*u*,<=*v*,<=*w*) such that *u*, *v* and *w* are distinct and there exist two paths, one between *u* and *w*, another between *v* and *w* such that these paths do not have an edge in common. | First line contains an integer *n* (1<=β€<=*n*<=β€<=105), the number of places in Hogwarts.
Each of the next *n*<=-<=1 lines contains two space separated integers *u* and *v* (1<=β€<=*u*,<=*v*<=β€<=*n*) indicating a road between places *u* and *v*. It is guaranteed that the given roads form a connected tree.
Next line contains a single integer *q* (1<=β€<=*q*<=β€<=105), the number of new edges being added.
Each of the next *q* lines contains two space separated integers *u* and *v* (1<=β€<=*u*,<=*v*<=β€<=*n*) representing the new road being added.
Note that it is possible that a newly added road connects places that were connected by a road before. Also, a newly added road may connect a place to itself. | In the first line print the value for the number of triplets before any changes occurred.
After that print *q* lines, a single integer *ans**i* in each line containing the value for the number of triplets after *i*-th edge addition. | [
"3\n1 2\n2 3\n1\n2 3\n",
"4\n1 2\n2 3\n2 4\n2\n1 4\n3 4\n",
"5\n1 2\n2 3\n3 4\n4 5\n1\n1 5\n"
] | [
"2\n4\n",
"6\n18\n24\n",
"20\n60\n"
] | In the first sample case, for the initial tree, we have (1,β3,β2) and (3,β1,β2) as the only possible triplets (*u*,β*v*,β*w*).
After addition of edge from 2 to 3, we have (1,β3,β2), (3,β1,β2), (1,β2,β3) and (2,β1,β3) as the possible triplets. | [] | 31 | 0 | 0 | 37,790 | |
241 | Race | [
"brute force",
"implementation"
] | null | null | The Old City is a rectangular city represented as an *m*<=Γ<=*n* grid of blocks. This city contains many buildings, straight two-way streets and junctions. Each junction and each building is exactly one block. All the streets have width of one block and are either vertical or horizontal. There is a junction on both sides of each street. We call two blocks adjacent if and only if they share a common side. No two blocks of different streets are adjacent and no two junctions are adjacent.
There is an annual festival and as a part of it, The Old Peykan follows a special path in the city. This path starts from a block in a street, continues with many junctions and ends in a block of some street. For each street block, we know how much time it takes for the Old Peykan to go from this block to an adjacent block. Also the Old Peykan can go from each junction to its adjacent street blocks in one minute. Of course Old Peykan can't go to building blocks.
We know the initial position of the Old Peykan and the sequence of junctions that it passes to reach its destination. After passing all the junctions and reaching the destination, it will stay there forever. Your task is to find out where will the Old Peykan be *k* minutes after it starts moving. Consider that The Old Peykan always follows the shortest path that passes through the given sequence of junctions and reaches the destination.
Note that the Old Peykan may visit some blocks more than once. | The first line of input contains three integers *m*, *n* and *k* (3<=β€<=*m*,<=*n*<=β€<=100,<=1<=β€<=*k*<=β€<=100000). Next *m* lines are representing the city's map. Each of them containts *n* characters, each character is a block:
- Character "#" represents a building. - Digits "1", "2", ..., "9" represent a block of an street and this digit means the number of minutes it takes for the Old Peykan to pass this block. - Characters "a", "b", ..., "z" means that this block is a junction and this character is it's name. All the junction names are unique.
Consider that all blocks have the coordinates: the *j*-th in the *i*-th line have coordinates (*i*,<=*j*) (1<=β€<=*i*<=β€<=*m*,<=1<=β€<=*j*<=β€<=*n*).
The (*m*<=+<=2)th line contains two integers *r**s* and *c**s* (1<=β€<=*r**s*<=β€<=*m*,<=1<=β€<=*c**s*<=β€<=*n*), string *s* and another two integers *r**e* and *c**e* (1<=β€<=*r**e*<=β€<=*m*,<=1<=β€<=*c**e*<=β€<=*n*). The path starts from block (*r**s*,<=*c**s*), continues through junctions in the order that is specified by *s* and will end in block (*r**e*,<=*c**e*). Length of *s* is between 1 and 1000.
It's guaranteed that string *s* denotes a correct path from the start position to the end position and string *s* doesn't contain two consecutive equal letters. Also start position (*r**s*,<=*c**s*) and the end position (*r**e*,<=*c**e*) are street blocks. | In a single line print two integers *r**f* and *c**f* β (*r**f*,<=*c**f*) being the position of the Old Peykan after exactly *k* minutes. | [
"3 10 12\n##########\n#z1a1111b#\n##########\n2 3 ab 2 8\n",
"10 3 5\n###\n#w#\n#1#\n#a#\n#1#\n#1#\n#1#\n#1#\n#b#\n###\n3 2 abababababababab 6 2\n",
"3 10 6\n##########\n#z1a1311b#\n##########\n2 3 ab 2 8\n"
] | [
"2 8\n",
"8 2\n",
"2 7\n"
] | none | [
{
"input": "3 10 12\n##########\n#z1a1111b#\n##########\n2 3 ab 2 8",
"output": "2 8"
},
{
"input": "10 3 5\n###\n#w#\n#1#\n#a#\n#1#\n#1#\n#1#\n#1#\n#b#\n###\n3 2 abababababababab 6 2",
"output": "8 2"
},
{
"input": "3 10 6\n##########\n#z1a1311b#\n##########\n2 3 ab 2 8",
"output": ... | 154 | 2,150,400 | 3 | 37,832 | |
526 | Pudding Monsters | [
"data structures",
"divide and conquer"
] | null | null | In this problem you will meet the simplified model of game Pudding Monsters.
An important process in developing any game is creating levels. A game field in Pudding Monsters is an *n*<=Γ<=*n* rectangular grid, *n* of its cells contain monsters and some other cells contain game objects. The gameplay is about moving the monsters around the field. When two monsters are touching each other, they glue together into a single big one (as they are from pudding, remember?).
Statistics showed that the most interesting maps appear if initially each row and each column contains exactly one monster and the rest of map specifics is set up by the correct positioning of the other game objects.
A technique that's widely used to make the development process more efficient is reusing the available resources. For example, if there is a large *n*<=Γ<=*n* map, you can choose in it a smaller *k*<=Γ<=*k* square part, containing exactly *k* monsters and suggest it as a simplified version of the original map.
You wonder how many ways there are to choose in the initial map a *k*<=Γ<=*k* (1<=β€<=*k*<=β€<=*n*) square fragment, containing exactly *k* pudding monsters. Calculate this number. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=3<=Γ<=105) β the size of the initial field.
Next *n* lines contain the coordinates of the cells initially containing monsters. The *i*-th of the next lines contains two numbers *r**i*,<=*c**i* (1<=β€<=*r**i*,<=*c**i*<=β€<=*n*) β the row number and the column number of the cell that initially contains the *i*-th monster.
It is guaranteed that all *r**i* are distinct numbers and all *c**i* are distinct numbers. | Print the number of distinct square fragments of the original field that can form a new map. | [
"5\n1 1\n4 3\n3 2\n2 4\n5 5\n"
] | [
"10\n"
] | none | [] | 30 | 0 | 0 | 37,873 | |
282 | Painting Eggs | [
"greedy",
"math"
] | null | null | The Bitlandians are quite weird people. They have very peculiar customs.
As is customary, Uncle J. wants to have *n* eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work.
The kids are excited because just as is customary, they're going to be paid for the job!
Overall uncle J. has got *n* eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000.
Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500.
Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible. | The first line contains integer *n* (1<=β€<=*n*<=β€<=106) β the number of eggs.
Next *n* lines contain two integers *a**i* and *g**i* each (0<=β€<=*a**i*,<=*g**i*<=β€<=1000;Β *a**i*<=+<=*g**i*<==<=1000): *a**i* is the price said by A. for the *i*-th egg and *g**i* is the price said by G. for the *i*-th egg. | If it is impossible to assign the painting, print "-1" (without quotes).
Otherwise print a string, consisting of *n* letters "G" and "A". The *i*-th letter of this string should represent the child who will get the *i*-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as *S**a*, and the money Uncle J. must pay G. for the painting as *S**g*, then this inequality must hold: |*S**a*<=<=-<=<=*S**g*|<=<=β€<=<=500.
If there are several solutions, you are allowed to print any of them. | [
"2\n1 999\n999 1\n",
"3\n400 600\n400 600\n400 600\n"
] | [
"AG\n",
"AGA\n"
] | none | [
{
"input": "2\n1 999\n999 1",
"output": "AG"
},
{
"input": "3\n400 600\n400 600\n400 600",
"output": "AGA"
},
{
"input": "2\n500 500\n500 500",
"output": "AG"
},
{
"input": "1\n1 999",
"output": "A"
},
{
"input": "10\n1 999\n1 999\n1 999\n1 999\n1 999\n1 999\n1 99... | 3,898 | 156,057,600 | 3 | 37,986 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.