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 ... | 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*-... | 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... | 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*<=β€<=... | 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.c... | [
{
"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 ... | 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*... | 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 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 s... | 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:... | [
{
"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* (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 exa... | 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 th... | 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... | [
"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 b... | 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*<... | 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 answ... | [
"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 ... | 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 laborat... | 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 capsule... | 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 poss... | 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/625a74cc... | [
{
"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... | 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 ro... | 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 ... | 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 ... | 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-in... | [] | 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 bett... | 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... | 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 bl... | 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 descr... | 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... | 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 border... | 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 den... | 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. Con... | 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 ch... | 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... | 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 a... | 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 que... | [
"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... | 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*... | 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 ... | [] | 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.
D... | 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 p... | 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... | [] | 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. The... | 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 ar... | 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$,... | [
{
"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... | 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*,<... | 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" wi... | [
"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 con... | 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 vert... | [
{
"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 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... | 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 lea... | 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 simul... | 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... | 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 ... | 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 b... | 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-l... | 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... | 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. Thi... | 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... | 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 ... | [
"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β... | [
{
"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... | 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... | 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... | 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 e... | 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 ... | 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, ... | 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 numb... | [
"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*β=β... | [
{
"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 produc... | 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 becam... | 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 con... | 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* sta... | 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 indexe... | [
"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 ... | 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... | 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 co... | 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 t... | 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... | [
{
"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 chip... | 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 "... | [
"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 t... | 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... | [
"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 a... | 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... | [
"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 c... | 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 ... | 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... | 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 ... | [
"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/Bit... | 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" ... | [
{
"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 min... | 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/a0... | [
{
"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 re... | 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 ... | 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. A... | [
"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)-Ρ Π·ΠΎΠ½Ρ, Π΅ΡΠ»ΠΈ ΠΎΠ½ΠΈ ΡΡΡΠ΅ΡΡΠ²ΡΡΡ. ΠΡΠΈ ΡΡΠΎΠΌ ΠΌΠ΅ΠΆΠ΄Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΏΠ°ΡΠΎΠΉ ΡΠΎΡΠ΅Π΄Π½ΠΈΡ
Π·ΠΎΠ½ Π½Π°Ρ
ΠΎΠ΄ΡΡΡΡ ΠΏΠΎΠ³ΡΠ°Π½ΠΈΡΠ½ΡΠ΅ Π²ΡΠ°ΡΠ°, ΠΊΠΎΡΠΎΡΡΠ΅ ΠΌΠΎΠ³ΡΡ Π±ΡΡΡ ΡΠ°Π·Π½ΡΡ
ΡΠ²Π΅ΡΠΎΠ², ΡΠ²Π΅Ρ Π²ΡΠ°Ρ ΠΌΠ΅ΠΆΠ΄Ρ *... | ΠΠ΅ΡΠ²Π°Ρ ΡΡΡΠΎΠΊΠ° ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ ΡΡΠΈ ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ»Π° *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* ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ ΡΠ²Π΅... | ΠΡΠ»ΠΈ ΠΡΠΊΠ°Π΄ΠΈΠΉ Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ ΠΏΠΎΠΏΠ°ΡΡΡ ΠΈΠ· Π·ΠΎΠ½Ρ *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 Π·Π° ΡΠ΅ΡΡΡΠ΅ Ρ
ΠΎΠ΄Π°.
ΠΠΎ Π²ΡΠΎΡΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ ΠΡΠΊΠ°Π΄ΠΈΠΉ ΠΌΠΎΠΆΠ΅Ρ Π΄ΠΎΠΉΡΠΈ Π»ΠΈΡΡ Π΄ΠΎ ΡΠ΅ΡΠ²Π΅ΡΡΠΎΠΉ Π·ΠΎΠ½Ρ, ΡΠ°ΠΊ ΠΊΠ°ΠΊ ΡΠ°ΡΡΠΎΠ² Ρ
ΡΠ°Π½ΠΈΡΠ΅... | [] | 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... | 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 ... | [
{
"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 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... | 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... | [] | 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 gu... | 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*, designe... | 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... | 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 cons... | 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 t... | 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 whi... | 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 ... | 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 ski... | [
{
"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* cent... | 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*,<... | 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 suc... | [] | 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 i... | 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... | 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... | 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 let... | 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 replac... | [
"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 enou... | [
{
"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 ... | 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* l... | 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/abef521ff4b500a39098df2ced9... | [] | 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 t... | 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... | [
{
"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 simpl... | 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 a... | [
"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 ... | 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*<=β€<=*... | 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 hi... | 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 li... | 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*<=β€... | 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... | 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 ... | 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, respectivel... | 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 t... | 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... | [
{
"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* diffe... | 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.
Th... | 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 garden... | [
{
"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 th... | 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 ... | 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... | 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 firs... | 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 a... | 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... | 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... | 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 ... | 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... | 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 spa... | 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:
- ... | 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 cor... | 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 sys... | 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 ... | [
"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 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*... | 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 nu... | [
"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 conta... | 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 sever... | [
"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... | [
{
"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 ... | 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 p... | 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 fam... | 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... | 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 the... | [
"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,... | 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 s... | 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,<=... | 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... | 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 st... | 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*.... | [
"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 ... | 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 fi... | 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... | [
{
"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 ... | 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 th... | [
"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*... | 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... | 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*<... | [
{
"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 ... | 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 loyalti... | 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 c... | 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 b... | 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... | [
"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 road... | 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 co... | 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 sid... | 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 ... | 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... | 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 nu... | 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.... | 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 den... | [
"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.