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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
437 | The Child and Polygon | [
"dp",
"geometry"
] | null | null | This time our child has a simple polygon. He has to find the number of ways to split the polygon into non-degenerate triangles, each way must satisfy the following requirements:
- each vertex of each triangle is one of the polygon vertex; - each side of the polygon must be the side of exactly one triangle; - the ar... | The first line contains one integer *n* (3<=β€<=*n*<=β€<=200) β the number of vertices of the polygon. Then follow *n* lines, each line containing two integers. The *i*-th line contains *x**i*,<=*y**i* (|*x**i*|,<=|*y**i*|<=β€<=107) β the *i*-th vertex of the polygon in clockwise or counterclockwise order.
It's guarantee... | Output the number of ways modulo 1000000007 (109<=<=+<=<=7). | [
"4\n0 0\n0 1\n1 1\n1 0\n",
"4\n0 0\n1 0\n0 1\n-1 0\n",
"5\n0 0\n1 0\n1 1\n0 1\n-2 -1\n"
] | [
"2\n",
"1\n",
"3\n"
] | In the first sample, there are two possible splittings:
In the second sample, there are only one possible splitting: | [] | 31 | 0 | 0 | 24,065 | |
55 | Beautiful numbers | [
"dp",
"number theory"
] | D. Beautiful numbers | 4 | 256 | Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful numbers in given ranges. | The first line of the input contains the number of cases *t* (1<=β€<=*t*<=β€<=10). Each of the next *t* lines contains two natural numbers *l**i* and *r**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=9<=Β·1018).
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %... | Output should contain *t* numbers β answers to the queries, one number per line β quantities of beautiful numbers in given intervals (from *l**i* to *r**i*, inclusively). | [
"1\n1 9\n",
"1\n12 15\n"
] | [
"9\n",
"2\n"
] | none | [
{
"input": "1\n1 9",
"output": "9"
},
{
"input": "1\n12 15",
"output": "2"
},
{
"input": "1\n25 53",
"output": "7"
},
{
"input": "1\n1 1000",
"output": "138"
},
{
"input": "1\n1 100000",
"output": "4578"
},
{
"input": "2\n234 59843\n46 3243",
"outp... | 1,902 | 45,568,000 | 3.677373 | 24,092 |
159 | Zebra Tower | [
"*special",
"data structures",
"greedy",
"sortings"
] | null | null | Little Janet likes playing with cubes. Actually, she likes to play with anything whatsoever, cubes or tesseracts, as long as they are multicolored. Each cube is described by two parameters β color *c**i* and size *s**i*. A Zebra Tower is a tower that consists of cubes of exactly two colors. Besides, the colors of the c... | The first line contains an integer *n* (2<=β€<=*n*<=β€<=105) β the number of cubes. Next *n* lines contain the descriptions of the cubes, one description per line. A cube description consists of two space-separated integers *c**i* and *s**i* (1<=β€<=*c**i*,<=*s**i*<=β€<=109) β the *i*-th cube's color and size, correspondin... | Print the description of the Zebra Tower of the maximum height in the following form. In the first line print the tower's height, in the second line print the number of cubes that form the tower, and in the third line print the space-separated indices of cubes in the order in which they follow in the tower from the bot... | [
"4\n1 2\n1 3\n2 4\n3 3\n",
"2\n1 1\n2 1\n"
] | [
"9\n3\n2 3 1 \n",
"2\n2\n2 1 \n"
] | none | [
{
"input": "4\n1 2\n1 3\n2 4\n3 3",
"output": "9\n3\n2 3 1 "
},
{
"input": "2\n1 1\n2 1",
"output": "2\n2\n2 1 "
},
{
"input": "3\n1 2\n2 2\n2 1",
"output": "5\n3\n2 1 3 "
},
{
"input": "4\n2 1\n2 1\n1 1\n1 2",
"output": "5\n4\n4 2 3 1 "
},
{
"input": "6\n1 1\n1 1... | 46 | 307,200 | 0 | 24,103 | |
0 | none | [
"none"
] | null | null | Arpa has found a list containing *n* numbers. He calls a list bad if and only if it is not empty and gcd (see notes section for more information) of numbers in the list is 1.
Arpa can perform two types of operations:
- Choose a number and delete it with cost *x*. - Choose a number and increase it by 1 with cost *y*... | First line contains three integers *n*, *x* and *y* (1<=β€<=*n*<=β€<=5Β·105, 1<=β€<=*x*,<=*y*<=β€<=109)Β β the number of elements in the list and the integers *x* and *y*.
Second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=106)Β β the elements of the list. | Print a single integer: the minimum possible cost to make the list good. | [
"4 23 17\n1 17 17 16\n",
"10 6 2\n100 49 71 73 66 96 8 60 41 63\n"
] | [
"40\n",
"10\n"
] | In example, number 1 must be deleted (with cost 23) and number 16 must increased by 1 (with cost 17).
A gcd (greatest common divisor) of a set of numbers is the maximum integer that divides all integers in the set. Read more about gcd [here](https://en.wikipedia.org/wiki/Greatest_common_divisor). | [] | 0 | 0 | -1 | 24,127 | |
0 | none | [
"none"
] | null | null | As a tradition, every year before IOI all the members of Natalia Fan Club are invited to Malek Dance Club to have a fun night together. Malek Dance Club has 2*n* members and coincidentally Natalia Fan Club also has 2*n* members. Each member of MDC is assigned a unique id *i* from 0 to 2*n*<=-<=1. The same holds for eac... | The first line of input contains a binary number *x* of lenght *n*, (1<=β€<=*n*<=β€<=100).
This number may contain leading zeros. | Print the complexity of the given dance assignent modulo 1000000007 (109<=+<=7). | [
"11\n",
"01\n",
"1\n"
] | [
"6\n",
"2\n",
"1\n"
] | none | [
{
"input": "11",
"output": "6"
},
{
"input": "01",
"output": "2"
},
{
"input": "1",
"output": "1"
},
{
"input": "1111111111111111111111111111111111",
"output": "68817500"
},
{
"input": "0000000000000000000000000000000000000",
"output": "0"
},
{
"input"... | 92 | 0 | 0 | 24,132 | |
497 | Subsequences Return | [
"dp",
"matrices"
] | null | null | Assume that *s**k*(*n*) equals the sum of digits of number *n* in the *k*-based notation. For example, *s*2(5)<==<=*s*2(1012)<==<=1<=+<=0<=+<=1<==<=2, *s*3(14)<==<=*s*3(1123)<==<=1<=+<=1<=+<=2<==<=4.
The sequence of integers *a*0,<=...,<=*a**n*<=-<=1 is defined as . Your task is to calculate the number of distinct sub... | The first line contains two space-separated numbers *n* and *k* (1<=β€<=*n*<=β€<=1018, 2<=β€<=*k*<=β€<=30). | In a single line print the answer to the problem modulo 109<=+<=7. | [
"4 2\n",
"7 7\n"
] | [
"11\n",
"128\n"
] | In the first sample the sequence *a*<sub class="lower-index">*i*</sub> looks as follows: (0,β1,β1,β0). All the possible subsequences are:
In the second sample the sequence *a*<sub class="lower-index">*i*</sub> looks as follows: (0,β1,β2,β3,β4,β5,β6). The subsequences of this sequence are exactly all increasing sequen... | [] | 30 | 0 | 0 | 24,137 | |
83 | Doctor | [
"binary search",
"math",
"sortings"
] | B. Doctor | 2 | 256 | There are *n* animals in the queue to Dr. Dolittle. When an animal comes into the office, the doctor examines him, gives prescriptions, appoints tests and may appoint extra examination. Doc knows all the forest animals perfectly well and therefore knows exactly that the animal number *i* in the queue will have to visit... | The first line of input data contains two space-separated integers *n* and *k* (1<=β€<=*n*<=β€<=105, 0<=β€<=*k*<=β€<=1014). In the second line are given space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109).
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommen... | If the doctor will overall carry out less than *k* examinations, print a single number "-1" (without quotes). Otherwise, print the sequence of numbers β number of animals in the order in which they stand in the queue.
Note that this sequence may be empty. This case is present in pretests. You can just print nothing o... | [
"3 3\n1 2 1\n",
"4 10\n3 3 2 1\n",
"7 10\n1 3 3 1 2 3 1\n"
] | [
"2 ",
"-1\n",
"6 2 3 "
] | In the first sample test:
- Before examination: {1,β2,β3} - After the first examination: {2,β3} - After the second examination: {3,β2} - After the third examination: {2}
In the second sample test:
- Before examination: {1,β2,β3,β4,β5,β6,β7} - After the first examination: {2,β3,β4,β5,β6,β7} - After the second ... | [
{
"input": "3 3\n1 2 1",
"output": "2 "
},
{
"input": "4 10\n3 3 2 1",
"output": "-1"
},
{
"input": "7 10\n1 3 3 1 2 3 1",
"output": "6 2 3 "
},
{
"input": "1 0\n1",
"output": "1 "
},
{
"input": "6 101\n9 78 54 62 2 91",
"output": "4 6 2 3 "
},
{
"inpu... | 122 | 307,200 | 0 | 24,200 |
5 | Bindian Signalizing | [
"data structures"
] | E. Bindian Signalizing | 4 | 256 | Everyone knows that long ago on the territory of present-day Berland there lived Bindian tribes. Their capital was surrounded by *n* hills, forming a circle. On each hill there was a watchman, who watched the neighbourhood day and night.
In case of any danger the watchman could make a fire on the hill. One watchman co... | The first line of the input data contains an integer number *n* (3<=β€<=*n*<=β€<=106), *n* β the amount of hills around the capital. The second line contains *n* numbers β heights of the hills in clockwise order. All height numbers are integer and lie between 1 and 109. | Print the required amount of pairs. | [
"5\n1 2 4 5 3\n"
] | [
"7\n"
] | none | [
{
"input": "5\n1 2 4 5 3",
"output": "7"
},
{
"input": "3\n2118 2118 2118",
"output": "3"
},
{
"input": "3\n2221 1976 2221",
"output": "3"
},
{
"input": "3\n140 989 2895",
"output": "3"
},
{
"input": "4\n2440 2440 2440 2440",
"output": "6"
},
{
"input"... | 2,308 | 103,731,200 | 3.518286 | 24,272 |
690 | Collective Mindsets (medium) | [] | null | null | Way to go! Heidi now knows how many brains there must be for her to get one. But throwing herself in the midst of a clutch of hungry zombies is quite a risky endeavor. Hence Heidi wonders: what is the smallest number of brains that must be in the chest for her to get out at all (possibly empty-handed, but alive)?
The ... | The only line of input contains one integer: *N*, the number of attendees (1<=β€<=*N*<=β€<=109). | Output one integer: the smallest number of brains in the chest which allows Heidi to merely survive. | [
"1\n",
"3\n",
"99\n"
] | [
"0\n",
"1\n",
"49\n"
] | none | [
{
"input": "1",
"output": "0"
},
{
"input": "3",
"output": "1"
},
{
"input": "2",
"output": "0"
},
{
"input": "4",
"output": "0"
},
{
"input": "5",
"output": "2"
},
{
"input": "6",
"output": "1"
},
{
"input": "7",
"output": "3"
},
{... | 61 | 0 | 0 | 24,274 | |
480 | Parking Lot | [
"data structures",
"divide and conquer"
] | null | null | Petya's been bored at work and he is killing the time by watching the parking lot at the office. The parking lot looks from above like an *n*<=Γ<=*m* table (a cell of the table corresponds to a single parking spot). Some spots in the parking lot are taken, others are empty.
Petya watches cars riding into the parking l... | The first line contains three integers *n*, *m* and *k* β the sizes of the parking lot and the number of arriving cars after Petya started his watch (1<=β€<=*n*,<=*m*,<=*k*<=β€<=2000). Each of the following *n* lines contains *m* characters 'X' and '.', where 'X' means a taken spot and '.' means an empty spot. Each of th... | Print *k* integers β the length of the side of the maximum square of empty spots after the corresponding car has entered the parking lot. | [
"7 8 4\n........\nX.....X.\n........\n........\n.X......\n........\n........\n1 5\n6 4\n3 5\n4 6\n"
] | [
"5\n4\n4\n3\n"
] | none | [] | 30 | 0 | 0 | 24,329 | |
809 | Hitchhiking in the Baltic States | [
"data structures",
"dp"
] | null | null | Leha and Noora decided to go on a trip in the Baltic States. As you know from the previous problem, Leha has lost his car on the parking of the restaurant. Unfortunately, requests to the watchman didn't helped hacker find the car, so friends decided to go hitchhiking.
In total, they intended to visit *n* towns. Howeve... | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=3Β·105) denoting the number of towns Leha and Noora intended to visit.
Each line *i* of the *n* subsequent lines contains two integers *l**i*, *r**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=109), denoting that sights in *i*-th town are open for visitors on any day . | Print a single integer denoting the maximal number of towns, that Leha and Noora can visit. | [
"5\n6 6\n1 2\n3 4\n2 2\n1 4\n"
] | [
"3\n"
] | Consider the first example.
Let's take this plan: let's visit the sight in the second town on the first day, in the third town on the third day and in the fifth town on the fourth. That's would be the optimal answer. | [] | 46 | 0 | 0 | 24,334 | |
706 | Hard problem | [
"dp",
"strings"
] | null | null | Vasiliy is fond of solving different tasks. Today he found one he wasn't able to solve himself, so he asks you to help.
Vasiliy is given *n* strings consisting of lowercase English letters. He wants them to be sorted in lexicographical order (as in the dictionary), but he is not allowed to swap any of them. The only o... | The first line of the input contains a single integer *n* (2<=β€<=*n*<=β€<=100<=000)Β β the number of strings.
The second line contains *n* integers *c**i* (0<=β€<=*c**i*<=β€<=109), the *i*-th of them is equal to the amount of energy Vasiliy has to spent in order to reverse the *i*-th string.
Then follow *n* lines, each ... | If it is impossible to reverse some of the strings such that they will be located in lexicographical order, print <=-<=1. Otherwise, print the minimum total amount of energy Vasiliy has to spent. | [
"2\n1 2\nba\nac\n",
"3\n1 3 1\naa\nba\nac\n",
"2\n5 5\nbbb\naaa\n",
"2\n3 3\naaa\naa\n"
] | [
"1\n",
"1\n",
"-1\n",
"-1\n"
] | In the second sample one has to reverse string 2 or string 3. To amount of energy required to reverse the string 3 is smaller.
In the third sample, both strings do not change after reverse and they go in the wrong order, so the answer is β-β1.
In the fourth sample, both strings consists of characters 'a' only, but in... | [
{
"input": "2\n1 2\nba\nac",
"output": "1"
},
{
"input": "3\n1 3 1\naa\nba\nac",
"output": "1"
},
{
"input": "2\n5 5\nbbb\naaa",
"output": "-1"
},
{
"input": "2\n3 3\naaa\naa",
"output": "-1"
},
{
"input": "4\n0 0 8 6\nbi\nqp\nbt\nya",
"output": "8"
},
{
... | 62 | 0 | 0 | 24,348 | |
774 | Stepan's Series | [
"*special",
"dp"
] | null | null | Well, the series which Stepan watched for a very long time, ended. In total, the series had *n* episodes. For each of them, Stepan remembers either that he definitely has watched it, or that he definitely hasn't watched it, or he is unsure, has he watched this episode or not.
Stepan's dissatisfaction is the maximum n... | The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=100, 0<=β€<=*k*<=β€<=*n*) β the number of episodes in the series and the dissatisfaction which should be checked.
The second line contains the sequence which consists of *n* symbols "Y", "N" and "?". If the *i*-th symbol equals "Y", Stepan remembers that h... | If Stepan's dissatisfaction can be exactly equal to *k*, then print "YES" (without qoutes). Otherwise print "NO" (without qoutes). | [
"5 2\nNYNNY\n",
"6 1\n????NN\n"
] | [
"YES\n",
"NO\n"
] | In the first test Stepan remembers about all the episodes whether he has watched them or not. His dissatisfaction is 2, because he hasn't watch two episodes in a row β the episode number 3 and the episode number 4. The answer is "YES", because *k*β=β2.
In the second test *k*β=β1, Stepan's dissatisfaction is greater th... | [
{
"input": "5 2\nNYNNY",
"output": "YES"
},
{
"input": "6 1\n????NN",
"output": "NO"
},
{
"input": "100 8\nNYNNY?YNNNNNN?NNNNNYNY?YYNYNN?NNNY??NNYNYNNNYNNNYNNNNNNNNY?NNNYNYN?NNNY?YY?NNYNN?NNNYNNYNNYN?NNYNYNN",
"output": "YES"
},
{
"input": "10 1\nNY???NY?Y?",
"output": "Y... | 93 | 307,200 | 0 | 24,358 | |
556 | Case of Fake Numbers | [
"brute force",
"implementation"
] | null | null | Andrewid the Android is a galaxy-famous detective. He is now investigating a case of frauds who make fake copies of the famous Stolp's gears, puzzles that are as famous as the Rubik's cube once was.
Its most important components are a button and a line of *n* similar gears. Each gear has *n* teeth containing all numbe... | The first line contains integer *n* (1<=β€<=*n*<=β€<=1000) β the number of gears.
The second line contains *n* digits *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=*n*<=-<=1) β the sequence of active teeth: the active tooth of the *i*-th gear contains number *a**i*. | In a single line print "Yes" (without the quotes), if the given Stolp's gears puzzle is real, and "No" (without the quotes) otherwise. | [
"3\n1 0 0\n",
"5\n4 2 1 4 3\n",
"4\n0 2 3 1\n"
] | [
"Yes\n",
"Yes\n",
"No\n"
] | In the first sample test when you push the button for the first time, the sequence of active teeth will be 2 2 1, when you push it for the second time, you get 0 1 2. | [
{
"input": "3\n1 0 0",
"output": "Yes"
},
{
"input": "5\n4 2 1 4 3",
"output": "Yes"
},
{
"input": "4\n0 2 3 1",
"output": "No"
},
{
"input": "1\n0",
"output": "Yes"
},
{
"input": "2\n1 0",
"output": "Yes"
},
{
"input": "5\n2 4 4 1 1",
"output": "Y... | 46 | 0 | 3 | 24,469 | |
69 | Game | [
"implementation"
] | C. Game | 2 | 256 | In one school with Vasya there is a student Kostya. Kostya does not like physics, he likes different online games. Every day, having come home, Kostya throws his bag in the farthest corner and sits down at his beloved computer. Kostya even eats glued to the game. A few days ago Kostya bought a new RPG game "HaresButtle... | The first line has 4 natural numbers: *k* (1<=β€<=*k*<=β€<=100) β the number of Kostya's allies, *n* (1<=β€<=*n*<=β€<=50) β the number of basic artifacts, *m* (0<=β€<=*m*<=β€<=50) β the number of composite artifacts, *q* (1<=β€<=*q*<=β€<=500) β the number of his friends' purchases. The following *n* lines contain the names of ... | The output file should consist of *k* blocks. The first line should contain number *b**i* β the number of different artifacts the *i*-th ally has. Then the block should contain *b**i* lines with the names of these artifacts and the number of these artifacts. At that the lines should be printed in accordance with the le... | [
"2 3 2 5\ndesolator\nrefresher\nperseverance\nvanguard: desolator 1, refresher 1\nmaelstorm: perseverance 2\n1 desolator\n2 perseverance\n1 refresher\n2 desolator\n2 perseverance\n"
] | [
"1\nvanguard 1\n2\ndesolator 1\nmaelstorm 1\n"
] | none | [
{
"input": "2 3 2 5\ndesolator\nrefresher\nperseverance\nvanguard: desolator 1, refresher 1\nmaelstorm: perseverance 2\n1 desolator\n2 perseverance\n1 refresher\n2 desolator\n2 perseverance",
"output": "1\nvanguard 1\n2\ndesolator 1\nmaelstorm 1"
},
{
"input": "2 3 2 5\na\nb\nc\nd: a 1, b 1\ne: c 2\... | 186 | 307,200 | 0 | 24,626 |
662 | International Olympiad | [
"constructive algorithms",
"greedy",
"implementation",
"math"
] | null | null | International Abbreviation Olympiad takes place annually starting from 1989. Each year the competition receives an abbreviation of form IAO'y, where *y* stands for some number of consequent last digits of the current year. Organizers always pick an abbreviation with non-empty string *y* that has never been used before.... | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=1000)Β β the number of abbreviations to process.
Then *n* lines follow, each containing a single abbreviation. It's guaranteed that each abbreviation contains at most nine digits. | For each abbreviation given in the input, find the year of the corresponding Olympiad. | [
"5\nIAO'15\nIAO'2015\nIAO'1\nIAO'9\nIAO'0\n",
"4\nIAO'9\nIAO'99\nIAO'999\nIAO'9999\n"
] | [
"2015\n12015\n1991\n1989\n1990\n",
"1989\n1999\n2999\n9999\n"
] | none | [
{
"input": "5\nIAO'15\nIAO'2015\nIAO'1\nIAO'9\nIAO'0",
"output": "2015\n12015\n1991\n1989\n1990"
},
{
"input": "4\nIAO'9\nIAO'99\nIAO'999\nIAO'9999",
"output": "1989\n1999\n2999\n9999"
},
{
"input": "1\nIAO'111110",
"output": "1111110"
},
{
"input": "2\nIAO'0\nIAO'00",
"o... | 124 | 2,252,800 | 0 | 24,628 | |
492 | Vanya and Field | [
"math"
] | null | null | Vanya decided to walk in the field of size *n*<=Γ<=*n* cells. The field contains *m* apple trees, the *i*-th apple tree is at the cell with coordinates (*x**i*,<=*y**i*). Vanya moves towards vector (*dx*,<=*dy*). That means that if Vanya is now at the cell (*x*,<=*y*), then in a second he will be at cell . The followin... | The first line contains integers *n*,<=*m*,<=*dx*,<=*dy*(1<=β€<=*n*<=β€<=106, 1<=β€<=*m*<=β€<=105, 1<=β€<=*dx*,<=*dy*<=β€<=*n*) β the size of the field, the number of apple trees and the vector of Vanya's movement. Next *m* lines contain integers *x**i*,<=*y**i* (0<=β€<=*x**i*,<=*y**i*<=β€<=*n*<=-<=1) β the coordinates of appl... | Print two space-separated numbers β the coordinates of the cell from which you should start your path. If there are several answers you are allowed to print any of them. | [
"5 5 2 3\n0 0\n1 2\n1 3\n2 4\n3 1\n",
"2 3 1 1\n0 0\n0 1\n1 1\n"
] | [
"1 3\n",
"0 0\n"
] | In the first sample Vanya's path will look like: (1,β3)β-β(3,β1)β-β(0,β4)β-β(2,β2)β-β(4,β0)β-β(1,β3)
In the second sample: (0,β0)β-β(1,β1)β-β(0,β0) | [
{
"input": "5 5 2 3\n0 0\n1 2\n1 3\n2 4\n3 1",
"output": "1 3"
},
{
"input": "2 3 1 1\n0 0\n0 1\n1 1",
"output": "0 0"
},
{
"input": "5 5 2 4\n0 0\n1 2\n1 3\n2 4\n3 1",
"output": "1 2"
},
{
"input": "6 6 5 5\n0 0\n0 1\n0 2\n0 3\n0 4\n0 5",
"output": "0 0"
},
{
"in... | 46 | 0 | 0 | 24,720 | |
858 | Polycarp's phone book | [
"data structures",
"implementation",
"sortings"
] | null | null | There are *n* phone numbers in Polycarp's contacts on his phone. Each number is a 9-digit integer, starting with a digit different from 0. All the numbers are distinct.
There is the latest version of Berdroid OS installed on Polycarp's phone. If some number is entered, is shows up all the numbers in the contacts for w... | The first line contains single integer *n* (1<=β€<=*n*<=β€<=70000) β the total number of phone contacts in Polycarp's contacts.
The phone numbers follow, one in each line. Each number is a positive 9-digit integer starting with a digit from 1 to 9. All the numbers are distinct. | Print exactly *n* lines: the *i*-th of them should contain the shortest non-empty sequence of digits, such that if Polycarp enters it, the Berdroid OS shows up only the *i*-th number from the contacts. If there are several such sequences, print any of them. | [
"3\n123456789\n100000000\n100123456\n",
"4\n123456789\n193456789\n134567819\n934567891\n"
] | [
"9\n000\n01\n",
"2\n193\n81\n91\n"
] | none | [
{
"input": "3\n123456789\n100000000\n100123456",
"output": "9\n000\n01"
},
{
"input": "4\n123456789\n193456789\n134567819\n934567891",
"output": "2\n193\n81\n91"
},
{
"input": "1\n167038488",
"output": "4"
},
{
"input": "5\n115830748\n403459907\n556271610\n430358099\n41396141... | 3,182 | 184,217,600 | 3 | 24,780 | |
105 | Transmigration | [
"implementation"
] | A. Transmigration | 2 | 256 | In Disgaea as in most role-playing games, characters have skills that determine the character's ability to use certain weapons or spells. If the character does not have the necessary skill, he cannot use it. The skill level is represented as an integer that increases when you use this skill. Different character classes... | The first line contains three numbers *n*, *m* and *k* β the number of skills the current character has, the number of skills specific for the class into which the character is going to transmigrate and the reducing coefficient respectively; *n* and *m* are integers, and *k* is a real number with exactly two digits aft... | Print on the first line number *z* β the number of skills the character will have after the transmigration. Then print *z* lines, on each of which print a skill's name and level, separated by a single space. The skills should be given in the lexicographical order. | [
"5 4 0.75\naxe 350\nimpaler 300\nionize 80\nmegafire 120\nmagicboost 220\nheal\nmegafire\nshield\nmagicboost\n"
] | [
"6\naxe 262\nheal 0\nimpaler 225\nmagicboost 165\nmegafire 0\nshield 0\n"
] | none | [
{
"input": "5 4 0.75\naxe 350\nimpaler 300\nionize 80\nmegafire 120\nmagicboost 220\nheal\nmegafire\nshield\nmagicboost",
"output": "6\naxe 262\nheal 0\nimpaler 225\nmagicboost 165\nmegafire 0\nshield 0"
},
{
"input": "1 1 0.50\nstaff 1005\nionize",
"output": "2\nionize 0\nstaff 502"
},
{
... | 622 | 14,336,000 | 0 | 24,814 |
23 | Oranges and Apples | [
"constructive algorithms",
"sortings"
] | C. Oranges and Apples | 1 | 256 | In 2*N*<=-<=1 boxes there are apples and oranges. Your task is to choose *N* boxes so, that they will contain not less than half of all the apples and not less than half of all the oranges. | The first input line contains one number *T* β amount of tests. The description of each test starts with a natural number *N* β amount of boxes. Each of the following 2*N*<=-<=1 lines contains numbers *a**i* and *o**i* β amount of apples and oranges in the *i*-th box (0<=β€<=*a**i*,<=*o**i*<=β€<=109). The sum of *N* in a... | For each test output two lines. In the first line output YES, if it's possible to choose *N* boxes, or NO otherwise. If the answer is positive output in the second line *N* numbers β indexes of the chosen boxes. Boxes are numbered from 1 in the input order. Otherwise leave the second line empty. Separate the numbers wi... | [
"2\n2\n10 15\n5 7\n20 18\n1\n0 0\n"
] | [
"YES\n1 3\nYES\n1\n"
] | none | [
{
"input": "2\n2\n10 15\n5 7\n20 18\n1\n0 0",
"output": "YES\n3 1\nYES\n1"
}
] | 1,294 | 24,371,200 | 3.307605 | 24,842 |
254 | Anagram | [
"greedy",
"strings"
] | null | null | String *x* is an anagram of string *y*, if we can rearrange the letters in string *x* and get exact string *y*. For example, strings "DOG" and "GOD" are anagrams, so are strings "BABA" and "AABB", but strings "ABBAC" and "CAABA" are not.
You are given two strings *s* and *t* of the same length, consisting of uppercase... | The input consists of two lines. The first line contains string *s*, the second line contains string *t*. The strings have the same length (from 1 to 105 characters) and consist of uppercase English letters. | In the first line print *z* β the minimum number of replacement operations, needed to get an anagram of string *t* from string *s*. In the second line print the lexicographically minimum anagram that could be obtained in *z* operations. | [
"ABA\nCBA\n",
"CDBABC\nADCABD\n"
] | [
"1\nABC\n",
"2\nADBADC\n"
] | The second sample has eight anagrams of string *t*, that can be obtained from string *s* by replacing exactly two letters: "ADBADC", "ADDABC", "CDAABD", "CDBAAD", "CDBADA", "CDDABA", "DDAABC", "DDBAAC". These anagrams are listed in the lexicographical order. The lexicographically minimum anagram is "ADBADC". | [
{
"input": "ABA\nCBA",
"output": "1\nABC"
},
{
"input": "CDBABC\nADCABD",
"output": "2\nADBADC"
},
{
"input": "AABAA\nBBAAA",
"output": "1\nAABAB"
},
{
"input": "OVGHK\nRPGUC",
"output": "4\nCPGRU"
},
{
"input": "CCAACBA\nBBBAACC",
"output": "2\nBCAACBB"
},
... | 60 | 0 | -1 | 24,864 | |
899 | Letters Removing | [
"data structures",
"strings"
] | null | null | Petya has a string of length *n* consisting of small and large English letters and digits.
He performs *m* operations. Each operation is described with two integers *l* and *r* and a character *c*: Petya removes from the string all characters *c* on positions between *l* and *r*, inclusive. It's obvious that the lengt... | The first string contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=2Β·105) β the length of the string and the number of operations.
The second line contains the string of length *n*, consisting of small and large English letters and digits. Positions in the string are enumerated from 1.
Each of the next *m* lines... | Print the string Petya will obtain after performing all *m* operations. If the strings becomes empty after all operations, print an empty line. | [
"4 2\nabac\n1 3 a\n2 2 c\n",
"3 2\nA0z\n1 3 0\n1 1 z\n",
"10 4\nagtFrgF4aF\n2 5 g\n4 9 F\n1 5 4\n1 7 a\n",
"9 5\naAAaBBccD\n1 4 a\n5 6 c\n2 3 B\n4 4 D\n2 3 A\n"
] | [
"b\n",
"Az\n",
"tFrg4\n",
"AB\n"
] | In the first example during the first operation both letters 'a' are removed, so the string becomes "bc". During the second operation the letter 'c' (on the second position) is removed, and the string becomes "b".
In the second example during the first operation Petya removes '0' from the second position. After that t... | [
{
"input": "4 2\nabac\n1 3 a\n2 2 c",
"output": "b"
},
{
"input": "3 2\nA0z\n1 3 0\n1 1 z",
"output": "Az"
},
{
"input": "10 4\nagtFrgF4aF\n2 5 g\n4 9 F\n1 5 4\n1 7 a",
"output": "tFrg4"
},
{
"input": "9 5\naAAaBBccD\n1 4 a\n5 6 c\n2 3 B\n4 4 D\n2 3 A",
"output": "AB"
}... | 31 | 5,529,600 | 0 | 24,915 | |
340 | Bubble Sort Graph | [
"binary search",
"data structures",
"dp"
] | null | null | Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with *n* elements *a*1, *a*2, ..., *a**n* in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it *G*) initially has *n* vertices and 0 edges. During Bubble Sort execution, ... | The first line of the input contains an integer *n* (2<=β€<=*n*<=β€<=105). The next line contains *n* distinct integers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=*n*). | Output a single integer β the answer to the problem. | [
"3\n3 1 2\n"
] | [
"2\n"
] | Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2]. | [
{
"input": "3\n3 1 2",
"output": "2"
},
{
"input": "5\n4 2 1 3 5",
"output": "3"
},
{
"input": "10\n1 9 8 10 2 3 4 6 5 7",
"output": "6"
},
{
"input": "50\n12 24 42 43 36 3 40 29 7 34 10 13 28 9 35 23 25 21 19 4 20 18 11 38 41 48 6 46 33 17 31 37 2 30 32 44 45 5 47 49 16 15 5... | 1,000 | 7,065,600 | 0 | 24,916 | |
38 | Let's Go Rolling! | [
"dp",
"sortings"
] | E. Let's Go Rolling! | 2 | 256 | On a number axis directed from the left rightwards, *n* marbles with coordinates *x*1,<=*x*2,<=...,<=*x**n* are situated. Let's assume that the sizes of the marbles are infinitely small, that is in this task each of them is assumed to be a material point. You can stick pins in some of them and the cost of sticking in t... | The first input line contains an integer *n* (1<=β€<=*n*<=β€<=3000) which is the number of marbles. The next *n* lines contain the descriptions of the marbles in pairs of integers *x**i*, *c**i* (<=-<=109<=β€<=*x**i*,<=*c**i*<=β€<=109). The numbers are space-separated. Each description is given on a separate line. No two m... | Output the single number β the least fine you will have to pay. | [
"3\n2 3\n3 4\n1 2\n",
"4\n1 7\n3 1\n5 10\n6 1\n"
] | [
"5\n",
"11\n"
] | none | [
{
"input": "3\n2 3\n3 4\n1 2",
"output": "5"
},
{
"input": "4\n1 7\n3 1\n5 10\n6 1",
"output": "11"
},
{
"input": "1\n-948943148 10",
"output": "10"
},
{
"input": "5\n864449017 54\n864449026 504\n864449027 308\n864449028 284\n864449030 249",
"output": "97"
},
{
"i... | 186 | 307,200 | 0 | 24,962 |
380 | Sereja and Prefixes | [
"binary search",
"brute force"
] | null | null | Sereja loves number sequences very much. That's why he decided to make himself a new one following a certain algorithm.
Sereja takes a blank piece of paper. Then he starts writing out the sequence in *m* stages. Each time he either adds a new number to the end of the sequence or takes *l* first elements of the current... | The first line contains integer *m* (1<=β€<=*m*<=β€<=105) β the number of stages to build a sequence.
Next *m* lines contain the description of the stages in the order they follow. The first number in the line is a type of stage (1 or 2). Type 1 means adding one number to the end of the sequence, in this case the line ... | Print the elements that Sereja is interested in, in the order in which their numbers occur in the input. | [
"6\n1 1\n1 2\n2 2 1\n1 3\n2 5 2\n1 4\n16\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16\n"
] | [
"1 2 1 2 3 1 2 1 2 3 1 2 1 2 3 4\n"
] | none | [
{
"input": "6\n1 1\n1 2\n2 2 1\n1 3\n2 5 2\n1 4\n16\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16",
"output": "1 2 1 2 3 1 2 1 2 3 1 2 1 2 3 4"
},
{
"input": "2\n1 33085\n1 44638\n2\n1 2",
"output": "33085 44638"
},
{
"input": "10\n1 57757\n1 45234\n1 80807\n1 38496\n1 27469\n1 42645\n1 72643\... | 982 | 50,380,800 | 3 | 24,966 | |
908 | New Year and Original Order | [
"dp",
"math"
] | null | null | Let *S*(*n*) denote the number that represents the digits of *n* in sorted order. For example, *S*(1)<==<=1,<=*S*(5)<==<=5,<=*S*(50394)<==<=3459,<=*S*(353535)<==<=333555.
Given a number *X*, compute modulo 109<=+<=7. | The first line of input will contain the integer *X* (1<=β€<=*X*<=β€<=10700). | Print a single integer, the answer to the question. | [
"21\n",
"345342\n"
] | [
"195\n",
"390548434\n"
] | The first few values of *S* are 1,β2,β3,β4,β5,β6,β7,β8,β9,β1,β11,β12,β13,β14,β15,β16,β17,β18,β19,β2,β12. The sum of these values is 195. | [
{
"input": "21",
"output": "195"
},
{
"input": "345342",
"output": "390548434"
},
{
"input": "9438174368",
"output": "419438859"
},
{
"input": "33340691714056185860211260984431382156326935244157",
"output": "683387308"
},
{
"input": "606593899524279654880666327437... | 997 | 0 | 0 | 24,983 | |
30 | King's Problem? | [
"geometry",
"greedy"
] | D. King's Problem? | 3 | 256 | Every true king during his life must conquer the world, hold the Codeforces world finals, win pink panda in the shooting gallery and travel all over his kingdom.
King Copa has already done the first three things. Now he just needs to travel all over the kingdom. The kingdom is an infinite plane with Cartesian coordina... | The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=105,<=1<=β€<=*k*<=β€<=*n*<=+<=1) β amount of cities and index of the starting city. The second line contains *n*<=+<=1 numbers *x**i*. The third line contains *y**n*<=+<=1. All coordinates are integers and do not exceed 106 by absolute value. No two cities c... | Output the minimum possible length of the journey. Your answer must have relative or absolute error less than 10<=-<=6. | [
"3 1\n0 1 2 1\n1\n",
"3 1\n1 0 2 1\n1\n",
"4 5\n0 5 -1 -5 2\n3\n"
] | [
"3.41421356237309490000",
"3.82842712474619030000",
"14.24264068711928400000"
] | none | [
{
"input": "3 1\n0 1 2 1\n1",
"output": "3.41421356237309490000"
},
{
"input": "3 1\n1 0 2 1\n1",
"output": "3.82842712474619030000"
},
{
"input": "4 5\n0 5 -1 -5 2\n3",
"output": "14.24264068711928400000"
},
{
"input": "4 1\n0 5 -1 -5 2\n3",
"output": "16.858413792983193... | 278 | 9,932,800 | 0 | 25,048 |
570 | Pig and Palindromes | [
"combinatorics",
"dp"
] | null | null | Peppa the Pig was walking and walked into the forest. What a strange coincidence! The forest has the shape of a rectangle, consisting of *n* rows and *m* columns. We enumerate the rows of the rectangle from top to bottom with numbers from 1 to *n*, and the columns β from left to right with numbers from 1 to *m*. Let's ... | The first line contains two integers *n*,<=*m* (1<=β€<=*n*,<=*m*<=β€<=500) β the height and width of the field.
Each of the following *n* lines contains *m* lowercase English letters identifying the types of cells of the forest. Identical cells are represented by identical letters, different cells are represented by dif... | Print a single integer β the number of beautiful paths modulo 109<=+<=7. | [
"3 4\naaab\nbaaa\nabba\n"
] | [
"3"
] | Picture illustrating possibilities for the sample test.
<img class="tex-graphics" src="https://espresso.codeforces.com/bf73568d1cf80d89f66d4e472a91ae0339af83a2.png" style="max-width: 100.0%;max-height: 100.0%;"/>
<img class="tex-graphics" src="https://espresso.codeforces.com/1e870880d976e642be85498efab6bbd10bae8a84... | [
{
"input": "3 4\naaab\nbaaa\nabba",
"output": "3"
},
{
"input": "2 2\nab\naa",
"output": "2"
},
{
"input": "3 5\nqqqrw\nwqqtw\newqqq",
"output": "3"
},
{
"input": "1 5\nabbba",
"output": "1"
},
{
"input": "1 5\nabbbb",
"output": "0"
},
{
"input": "1 4\... | 2,573 | 9,625,600 | 3 | 25,247 | |
685 | Travelling Through the Snow Queen's Kingdom | [
"bitmasks",
"brute force",
"divide and conquer",
"graphs"
] | null | null | Gerda is travelling to the palace of the Snow Queen.
The road network consists of *n* intersections and *m* bidirectional roads. Roads are numbered from 1 to *m*. Snow Queen put a powerful spell on the roads to change the weather conditions there. Now, if Gerda steps on the road *i* at the moment of time less or equal... | The first line of the input contains integers *n*, *m* and *q* (2<=β€<=*n*<=β€<=1000, 1<=β€<=*m*,<=*q*<=β€<=200<=000)Β β the number of intersections in the road network of Snow Queen's Kingdom, the number of roads and the number of queries you have to answer.
The *i*-th of the following *m* lines contains the description o... | For each query print "Yes" (without quotes) if Gerda can be at the Snow Queen palace on time (not later than *r**i*) or "No" (without quotes) otherwise. | [
"5 4 6\n1 2\n2 3\n3 4\n3 5\n1 3 1 4\n1 3 2 4\n1 4 4 5\n1 4 4 1\n2 3 1 4\n2 2 2 3\n"
] | [
"Yes\nYes\nYes\nNo\nNo\nYes\n"
] | none | [] | 30 | 0 | 0 | 25,402 | |
39 | Tram | [] | I. Tram | 2 | 64 | In a Berland city S*** there is a tram engine house and only one tram. Three people work in the house β the tram driver, the conductor and the head of the engine house. The tram used to leave the engine house every morning and drove along his loop route. The tram needed exactly *c* minutes to complete the route. The he... | The first line contains integers *n* and *m* (2<=β€<=*n*,<=*m*<=β€<=105) β the number of crossroads and tramlines in S*** respectively. The next *m* lines contain the descriptions of the tramlines in "*u* *v*" format, where *u* is the initial tramline crossroad and *v* is its final crossroad. The crossroads are numbered ... | In the first line output the value of *t*. In the next line output the value of *k* β the required number of the cameras. In the next line output space-separated numbers of the crossroads, where the cameras should be installed. Output the numbers in increasing order. | [
"4 5\n1 2\n2 3\n3 4\n4 1\n1 4\n"
] | [
"2\n2\n1 3\n"
] | none | [] | 0 | 0 | -1 | 25,442 |
405 | Graph Cutting | [
"dfs and similar",
"graphs"
] | null | null | Little Chris is participating in a graph cutting contest. He's a pro. The time has come to test his skills to the fullest.
Chris is given a simple undirected connected graph with *n* vertices (numbered from 1 to *n*) and *m* edges. The problem is to cut it into edge-distinct paths of length 2. Formally, Chris has to p... | The first line of input contains two space-separated integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=105), the number of vertices and the number of edges in the graph. The next *m* lines contain the description of the graph's edges. The *i*-th line contains two space-separated integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i... | If it is possible to cut the given graph into edge-distinct paths of length 2, output lines. In the *i*-th line print three space-separated integers *x**i*, *y**i* and *z**i*, the description of the *i*-th path. The graph should contain this path, i.e., the graph should contain edges (*x**i*,<=*y**i*) and (*y**i*,<=*z... | [
"8 12\n1 2\n2 3\n3 4\n4 1\n1 3\n2 4\n3 5\n3 6\n5 6\n6 7\n6 8\n7 8\n",
"3 3\n1 2\n2 3\n3 1\n",
"3 2\n1 2\n2 3\n"
] | [
"1 2 4\n1 3 2\n1 4 3\n5 3 6\n5 6 8\n6 7 8",
"No solution\n",
"1 2 3\n"
] | none | [] | 46 | 0 | 0 | 25,444 | |
804 | Expected diameter of a tree | [
"binary search",
"brute force",
"dfs and similar",
"dp",
"sortings",
"trees"
] | null | null | Pasha is a good student and one of MoJaK's best friends. He always have a problem to think about. Today they had a talk about the following problem.
We have a forest (acyclic undirected graph) with *n* vertices and *m* edges. There are *q* queries we should answer. In each query two vertices *v* and *u* are given. Let... | The first line contains three integers *n*, *m* and *q*(1<=β€<=*n*,<=*m*,<=*q*<=β€<=105)Β β the number of vertices, the number of edges in the graph and the number of queries.
Each of the next *m* lines contains two integers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*), that means there is an edge between vertices *... | For each query print the expected value of *d* as described in the problem statement.
Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. Let's assume that your answer is *a*, and the jury's answer is *b*. The checker program will consider your answer correct, if . | [
"3 1 2\n1 3\n3 1\n2 3\n",
"5 2 3\n2 4\n4 3\n4 2\n4 1\n2 5\n"
] | [
"-1\n2.0000000000\n",
"-1\n2.6666666667\n2.6666666667\n"
] | In the first example the vertices 1 and 3 are in the same component, so the answer for the first query is -1. For the second query there are two options to add the edge: one option is to add the edge 1β-β2, the other one is 2β-β3. In both ways the resulting diameter is 2, so the answer is 2.
In the second example the ... | [
{
"input": "3 1 2\n1 3\n3 1\n2 3",
"output": "-1\n2.0000000000"
},
{
"input": "5 2 3\n2 4\n4 3\n4 2\n4 1\n2 5",
"output": "-1\n2.6666666667\n2.6666666667"
},
{
"input": "17 15 13\n3 15\n3 1\n15 9\n16 6\n1 5\n1 8\n16 12\n15 7\n9 4\n6 11\n15 14\n9 10\n15 13\n1 17\n11 2\n7 3\n9 6\n9 7\n1 8\... | 0 | 0 | -1 | 25,456 | |
351 | Jeff and Rounding | [
"dp",
"greedy",
"implementation",
"math"
] | null | null | Jeff got 2*n* real numbers *a*1,<=*a*2,<=...,<=*a*2*n* as a birthday present. The boy hates non-integer numbers, so he decided to slightly "adjust" the numbers he's got. Namely, Jeff consecutively executes *n* operations, each of them goes as follows:
- choose indexes *i* and *j* (*i*<=β <=*j*) that haven't been chose... | The first line contains integer *n* (1<=β€<=*n*<=β€<=2000). The next line contains 2*n* real numbers *a*1, *a*2, ..., *a*2*n* (0<=β€<=*a**i*<=β€<=10000), given with exactly three digits after the decimal point. The numbers are separated by spaces. | In a single line print a single real number β the required difference with exactly three digits after the decimal point. | [
"3\n0.000 0.500 0.750 1.000 2.000 3.000\n",
"3\n4469.000 6526.000 4864.000 9356.383 7490.000 995.896\n"
] | [
"0.250\n",
"0.279\n"
] | In the first test case you need to perform the operations as follows: (*i*β=β1,β*j*β=β4), (*i*β=β2,β*j*β=β3), (*i*β=β5,β*j*β=β6). In this case, the difference will equal |(0β+β0.5β+β0.75β+β1β+β2β+β3)β-β(0β+β0β+β1β+β1β+β2β+β3)|β=β0.25. | [
{
"input": "3\n0.000 0.500 0.750 1.000 2.000 3.000",
"output": "0.250"
},
{
"input": "3\n4469.000 6526.000 4864.000 9356.383 7490.000 995.896",
"output": "0.279"
},
{
"input": "3\n673.674 9263.142 6780.000 9801.000 4640.000 8244.000",
"output": "0.184"
},
{
"input": "3\n6470.... | 124 | 5,632,000 | 0 | 25,482 | |
747 | Comments | [
"dfs and similar",
"expression parsing",
"implementation",
"strings"
] | null | null | A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed.
Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment ex... | The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas.
It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (... | Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input. | [
"hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0\n",
"a,5,A,0,a,0,A,0,a,0,A,0\n",
"A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0\n"
] | [
"3\nhello test one \nok bye two \na b \n",
"2\na \nA a A a A \n",
"4\nA K M \nB F H L N O \nC D G I P \nE J \n"
] | The first example is explained in the statements. | [
{
"input": "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0",
"output": "3\nhello test one \nok bye two \na b "
},
{
"input": "a,5,A,0,a,0,A,0,a,0,A,0",
"output": "2\na \nA a A a A "
},
{
"input": "A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0",
"output": "4\nA K M \n... | 0 | 0 | -1 | 25,528 | |
977 | Divide by three, multiply by two | [
"dfs and similar",
"math",
"sortings"
] | null | null | Polycarp likes to play with numbers. He takes some integer number $x$, writes it down on the board, and then performs with it $n - 1$ operations of the two kinds:
- divide the number $x$ by $3$ ($x$ must be divisible by $3$); - multiply the number $x$ by $2$.
After each operation, Polycarp writes down the result o... | The first line of the input contatins an integer number $n$ ($2 \le n \le 100$) β the number of the elements in the sequence. The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \cdot 10^{18}$) β rearranged (reordered) sequence that Polycarp can wrote down on the board. | Print $n$ integer numbers β rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists. | [
"6\n4 8 6 3 12 9\n",
"4\n42 28 84 126\n",
"2\n1000000000000000000 3000000000000000000\n"
] | [
"9 3 6 12 4 8 \n",
"126 42 84 28 \n",
"3000000000000000000 1000000000000000000 \n"
] | In the first example the given sequence can be rearranged in the following way: $[9, 3, 6, 12, 4, 8]$. It can match possible Polycarp's game which started with $x = 9$. | [
{
"input": "6\n4 8 6 3 12 9",
"output": "9 3 6 12 4 8 "
},
{
"input": "4\n42 28 84 126",
"output": "126 42 84 28 "
},
{
"input": "2\n1000000000000000000 3000000000000000000",
"output": "3000000000000000000 1000000000000000000 "
},
{
"input": "19\n46875000000000000 73242187500... | 124 | 4,198,400 | -1 | 25,550 | |
173 | Chamber of Secrets | [
"dfs and similar",
"shortest paths"
] | null | null | "The Chamber of Secrets has been opened again" β this news has spread all around Hogwarts and some of the students have been petrified due to seeing the basilisk. Dumbledore got fired and now Harry is trying to enter the Chamber of Secrets. These aren't good news for Lord Voldemort. The problem is, he doesn't want anyb... | The first line of the input contains two integer numbers *n* and *m* (2<=β€<=*n*,<=*m*<=β€<=1000). Each of the next *n* lines contains *m* characters. Each character is either "." or "#" and represents one cell of the Chamber grid. It's "." if the corresponding cell is empty and "#" if it's a regular column. | Print the minimum number of columns to make magic or -1 if it's impossible to do. | [
"3 3\n.#.\n...\n.#.\n",
"4 3\n##.\n...\n.#.\n.#.\n"
] | [
"2\n",
"2\n"
] | The figure above shows the first sample test. In the first sample we should make both columns magic. The dragon figure represents the basilisk and the binoculars represent the person who will enter the Chamber of secrets. The black star shows the place where the person will be petrified. Yellow lines represent basilisk... | [
{
"input": "3 3\n.#.\n...\n.#.",
"output": "2"
},
{
"input": "4 3\n##.\n...\n.#.\n.#.",
"output": "2"
},
{
"input": "3 3\n###\n###\n###",
"output": "2"
},
{
"input": "3 4\n..##\n....\n..#.",
"output": "2"
},
{
"input": "4 3\n#.#\n...\n...\n.##",
"output": "2"
... | 280 | 409,600 | 0 | 25,638 | |
325 | Stadium and Games | [
"binary search",
"math"
] | null | null | Daniel is organizing a football tournament. He has come up with the following tournament format:
1. In the first several (possibly zero) stages, while the number of teams is even, they split in pairs and play one game for each pair. At each stage the loser of each pair is eliminated (there are no draws). Such stages... | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=1018), the number of games that should be played.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. | Print all possible numbers of invited teams in ascending order, one per line. If exactly *n* games cannot be played, output one number: -1. | [
"3\n",
"25\n",
"2\n"
] | [
"3\n4\n",
"20\n",
"-1\n"
] | none | [
{
"input": "3",
"output": "3\n4"
},
{
"input": "25",
"output": "20"
},
{
"input": "2",
"output": "-1"
},
{
"input": "1",
"output": "2"
},
{
"input": "15",
"output": "10\n16"
},
{
"input": "314",
"output": "-1"
},
{
"input": "524800",
"o... | 216 | 0 | 0 | 25,639 | |
96 | Lucky Numbers (easy) | [
"binary search",
"bitmasks",
"brute force"
] | B. Lucky Numbers (easy) | 2 | 256 | Petya loves lucky numbers. Everybody knows that positive integers are lucky if their decimal representation doesn't contain digits other than 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Lucky number is super lucky if it's decimal representation contains equal amount of digits 4 and 7. Fo... | The only line contains a positive integer *n* (1<=β€<=*n*<=β€<=109). This number doesn't have leading zeroes. | Output the least super lucky number that is more than or equal to *n*.
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. | [
"4500\n",
"47\n"
] | [
"4747\n",
"47\n"
] | none | [
{
"input": "4500",
"output": "4747"
},
{
"input": "47",
"output": "47"
},
{
"input": "1",
"output": "47"
},
{
"input": "12",
"output": "47"
},
{
"input": "4587",
"output": "4747"
},
{
"input": "100",
"output": "4477"
},
{
"input": "1007",
... | 154 | 5,120,000 | 0 | 25,643 |
547 | Mike and Fish | [
"constructive algorithms",
"dfs and similar",
"graphs"
] | null | null | As everyone knows, bears love fish. But Mike is a strange bear; He hates fish! The even more strange thing about him is he has an infinite number of blue and red fish.
He has marked *n* distinct points in the plane. *i*-th point is point (*x**i*,<=*y**i*). He wants to put exactly one fish in each of these points such... | The first line of input contains integer *n* (1<=β€<=*n*<=β€<=2<=Γ<=105).
The next *n* lines contain the information about the points, *i*-th line contains two integers *x**i* and *y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=2<=Γ<=105), the *i*-th point coordinates.
It is guaranteed that there is at least one valid answer. | Print the answer as a sequence of *n* characters 'r' (for red) or 'b' (for blue) where *i*-th character denotes the color of the fish in the *i*-th point. | [
"4\n1 1\n1 2\n2 1\n2 2\n",
"3\n1 1\n1 2\n2 1\n"
] | [
"brrb\n",
"brr\n"
] | none | [
{
"input": "4\n1 1\n1 2\n2 1\n2 2",
"output": "brrb"
},
{
"input": "3\n1 1\n1 2\n2 1",
"output": "brr"
},
{
"input": "3\n157210 22861\n175396 39466\n40933 17093",
"output": "rrr"
},
{
"input": "5\n55599 84144\n169207 98421\n1909 186625\n31525 147710\n7781 82078",
"output"... | 1,263 | 268,390,400 | 0 | 25,659 | |
916 | Jamie and Tree | [
"data structures",
"trees"
] | null | null | To your surprise, Jamie is the final boss! Ehehehe.
Jamie has given you a tree with *n* vertices, numbered from 1 to *n*. Initially, the root of the tree is the vertex with number 1. Also, each vertex has a value on it.
Jamie also gives you three types of queries on the tree:
1 *v*Β β Change the tree's root to vertex... | The first line of input contains two space-separated integers *n* and *q* (1<=β€<=*n*<=β€<=105,<=1<=β€<=*q*<=β€<=105)Β β the number of vertices in the tree and the number of queries to process respectively.
The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=108<=β€<=*a**i*<=β€<=108)Β β init... | For each query of the third type, output the required answer. It is guaranteed that at least one query of the third type is given by Jamie. | [
"6 7\n1 4 2 8 5 7\n1 2\n3 1\n4 3\n4 5\n3 6\n3 1\n2 4 6 3\n3 4\n1 6\n2 2 4 -5\n1 4\n3 3\n",
"4 6\n4 3 5 6\n1 2\n2 3\n3 4\n3 1\n1 3\n2 2 4 3\n1 1\n2 2 4 -3\n3 1\n"
] | [
"27\n19\n5\n",
"18\n21\n"
] | The following picture shows how the tree varies after the queries in the first sample. | [] | 30 | 0 | 0 | 25,677 | |
513 | Inversions problem | [
"dp"
] | null | null | You are given a permutation of *n* numbers *p*1,<=*p*2,<=...,<=*p**n*. We perform *k* operations of the following type: choose uniformly at random two indices *l* and *r* (*l*<=β€<=*r*) and reverse the order of the elements *p**l*,<=*p**l*<=+<=1,<=...,<=*p**r*. Your task is to find the expected value of the number of in... | The first line of input contains two integers *n* and *k* (1<=β€<=*n*<=β€<=100, 1<=β€<=*k*<=β€<=109). The next line contains *n* integers *p*1,<=*p*2,<=...,<=*p**n* β the given permutation. All *p**i* are different and in range from 1 to *n*.
The problem consists of three subproblems. The subproblems have different constr... | Output the answer with absolute or relative error no more than 1*e*<=-<=9. | [
"3 1\n1 2 3\n",
"3 4\n1 3 2\n"
] | [
"0.833333333333333\n",
"1.458333333333334\n"
] | Consider the first sample test. We will randomly pick an interval of the permutation (1,β2,β3) (which has no inversions) and reverse the order of its elements. With probability <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/eb946338365d9781f7d2e9ec692c26702d0ae3a7.png" style="max-width: 10... | [
{
"input": "3 1\n1 2 3",
"output": "0.833333333333333"
},
{
"input": "3 4\n1 3 2",
"output": "1.458333333333334"
},
{
"input": "6 1\n4 2 5 1 3 6",
"output": "6.380952380952381"
},
{
"input": "6 2\n1 4 6 5 2 3",
"output": "6.954648526077097"
},
{
"input": "4 4\n2 3... | 46 | 0 | 0 | 25,698 | |
630 | Indivisibility | [
"math",
"number theory"
] | null | null | IT City company developing computer games decided to upgrade its way to reward its employees. Now it looks the following way. After a new game release users start buying it actively, and the company tracks the number of sales with precision to each transaction. Every time when the next number of sales is not divisible ... | The only line of the input contains one integer *n* (1<=β€<=*n*<=β€<=1018) β the prediction on the number of people who will buy the game. | Output one integer showing how many numbers from 1 to *n* are not divisible by any number from 2 to 10. | [
"12\n"
] | [
"2"
] | none | [
{
"input": "12",
"output": "2"
},
{
"input": "2519",
"output": "576"
},
{
"input": "2521",
"output": "577"
},
{
"input": "1",
"output": "1"
},
{
"input": "314159265",
"output": "71807832"
},
{
"input": "718281828459045235",
"output": "1641787036477... | 46 | 0 | 3 | 25,704 | |
689 | Friends and Subsequences | [
"binary search",
"data structures"
] | null | null | Mike and !Mike are old childhood rivals, they are opposite in everything they do, except programming. Today they have a problem they cannot solve on their own, but together (with you)Β β who knows?
Every one of them has an integer sequences *a* and *b* of length *n*. Being given a query of the form of pair of integers... | The first line contains only integer *n* (1<=β€<=*n*<=β€<=200<=000).
The second line contains *n* integer numbers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=β€<=*a**i*<=β€<=109)Β β the sequence *a*.
The third line contains *n* integer numbers *b*1,<=*b*2,<=...,<=*b**n* (<=-<=109<=β€<=*b**i*<=β€<=109)Β β the sequence *b*. | Print the only integer numberΒ β the number of occasions the robot will count, thus for how many pairs is satisfied. | [
"6\n1 2 3 2 1 4\n6 7 1 2 3 2\n",
"3\n3 3 3\n1 1 1\n"
] | [
"2\n",
"0\n"
] | The occasions in the first sample case are:
1.*l*β=β4,*r*β=β4 since *max*{2}β=β*min*{2}.
2.*l*β=β4,*r*β=β5 since *max*{2,β1}β=β*min*{2,β3}.
There are no occasions in the second sample case since Mike will answer 3 to any query pair, but !Mike will always answer 1. | [
{
"input": "6\n1 2 3 2 1 4\n6 7 1 2 3 2",
"output": "2"
},
{
"input": "3\n3 3 3\n1 1 1",
"output": "0"
},
{
"input": "17\n714413739 -959271262 714413739 -745891378 926207665 -404845105 -404845105 -959271262 -189641729 -670860364 714413739 -189641729 192457837 -745891378 -670860364 536388... | 982 | 456,908,800 | 3 | 25,713 | |
333 | Chips | [
"greedy"
] | null | null | Gerald plays the following game. He has a checkered field of size *n*<=Γ<=*n* cells, where *m* various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for *n*<=-<=1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from ... | The first line contains two space-separated integers *n* and *m* (2<=β€<=*n*<=β€<=1000, 0<=β€<=*m*<=β€<=105) β the size of the field and the number of banned cells. Next *m* lines each contain two space-separated integers. Specifically, the *i*-th of these lines contains numbers *x**i* and *y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<... | Print a single integer β the maximum points Gerald can earn in this game. | [
"3 1\n2 2\n",
"3 0\n",
"4 3\n3 1\n3 2\n3 3\n"
] | [
"0\n",
"1\n",
"1\n"
] | In the first test the answer equals zero as we can't put chips into the corner cells.
In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips.
In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4). | [
{
"input": "3 1\n2 2",
"output": "0"
},
{
"input": "3 0",
"output": "1"
},
{
"input": "4 3\n3 1\n3 2\n3 3",
"output": "1"
},
{
"input": "2 1\n1 1",
"output": "0"
},
{
"input": "2 3\n1 2\n2 1\n2 2",
"output": "0"
},
{
"input": "5 1\n3 2",
"output": ... | 1,000 | 28,672,000 | 0 | 25,725 | |
171 | ucyhf | [
"*special",
"brute force",
"implementation",
"number theory"
] | null | null | qd ucyhf yi q fhycu dkcruh mxeiu huluhiu yi q tyvvuhudj fhycu dkcruh. oekh jqia yi je vydt jxu djx ucyhf. | jxu ydfkj sediyiji ev q iydwbu ydjuwuh *d* (1<=β€<=*d*<=β€<=11184) β jxu edu-rqiut ydtun ev jxu ucyhf je vydt. | ekjfkj q iydwbu dkcruh. | [
"1\n"
] | [
"13\n"
] | none | [
{
"input": "1",
"output": "13"
},
{
"input": "2",
"output": "17"
},
{
"input": "3",
"output": "31"
},
{
"input": "4",
"output": "37"
},
{
"input": "5",
"output": "71"
},
{
"input": "6",
"output": "73"
},
{
"input": "7",
"output": "79"
... | 746 | 1,228,800 | -1 | 25,741 | |
549 | Happy Line | [
"constructive algorithms",
"greedy",
"sortings"
] | null | null | Do you like summer? Residents of Berland do. They especially love eating ice cream in the hot summer. So this summer day a large queue of *n* Berland residents lined up in front of the ice cream stall. We know that each of them has a certain amount of berland dollars with them. The residents of Berland are nice people,... | The first line contains integer *n* (1<=β€<=*n*<=β€<=200<=000) β the number of residents who stand in the line.
The second line contains *n* space-separated integers *a**i* (0<=β€<=*a**i*<=β€<=109), where *a**i* is the number of Berland dollars of a man standing on the *i*-th position in the line. The positions are number... | If it is impossible to make all the residents happy, print ":(" without the quotes. Otherwise, print in the single line *n* space-separated integers, the *i*-th of them must be equal to the number of money of the person on position *i* in the new line. If there are multiple answers, print any of them. | [
"2\n11 8\n",
"5\n10 9 7 10 6\n",
"3\n12 3 3\n"
] | [
"9 10 ",
":(\n",
"4 4 10 "
] | In the first sample two residents should swap places, after that the first resident has 10 dollars and he is at the head of the line and the second resident will have 9 coins and he will be at the end of the line.
In the second sample it is impossible to achieve the desired result.
In the third sample the first pers... | [
{
"input": "2\n11 8",
"output": "9 10 "
},
{
"input": "5\n10 9 7 10 6",
"output": ":("
},
{
"input": "3\n12 3 3",
"output": "4 4 10 "
},
{
"input": "4\n7 3 9 10",
"output": "4 6 9 10 "
},
{
"input": "1\n1",
"output": "1 "
},
{
"input": "5\n15 5 8 6 3",... | 1,000 | 14,233,600 | 0 | 25,755 | |
888 | Xor-MST | [
"bitmasks",
"constructive algorithms",
"data structures"
] | null | null | You are given a complete undirected graph with *n* vertices. A number *a**i* is assigned to each vertex, and the weight of an edge between vertices *i* and *j* is equal to *a**i*<=*xor*<=*a**j*.
Calculate the weight of the minimum spanning tree in this graph. | The first line contains *n* (1<=β€<=*n*<=β€<=200000) β the number of vertices in the graph.
The second line contains *n* integers *a*1, *a*2, ..., *a**n* (0<=β€<=*a**i*<=<<=230) β the numbers assigned to the vertices. | Print one number β the weight of the minimum spanning tree in the graph. | [
"5\n1 2 3 4 5\n",
"4\n1 2 3 4\n"
] | [
"8\n",
"8\n"
] | none | [
{
"input": "5\n1 2 3 4 5",
"output": "8"
},
{
"input": "4\n1 2 3 4",
"output": "8"
},
{
"input": "1\n1",
"output": "0"
}
] | 46 | 0 | 0 | 25,815 | |
730 | Toda 2 | [
"greedy",
"implementation"
] | null | null | A group of *n* friends enjoys playing popular video game Toda 2. There is a rating system describing skill level of each player, initially the rating of the *i*-th friend is *r**i*.
The friends decided to take part in the championship as a team. But they should have equal ratings to be allowed to compose a single team... | The first line contains a single integer *n* (2<=β€<=*n*<=β€<=100) β the number of friends.
The second line contains *n* non-negative integers *r*1,<=*r*2,<=...,<=*r**n* (0<=β€<=*r**i*<=β€<=100), where *r**i* is the initial rating of the *i*-th friend. | In the first line, print a single integer *R* β the final rating of each of the friends.
In the second line, print integer *t* β the number of matches the friends have to play. Each of the following *t* lines should contain *n* characters '0' or '1', where the *j*-th character of the *i*-th line is equal to:
- '0', ... | [
"5\n4 5 1 7 4\n",
"2\n1 2\n",
"3\n1 1 1\n"
] | [
"1\n8\n01010\n00011\n01010\n10010\n00011\n11000\n00011\n11000\n",
"0\n2\n11\n11\n",
"1\n0\n"
] | none | [
{
"input": "5\n4 5 1 7 4",
"output": "1\n8\n01010\n00011\n01010\n10010\n00011\n11000\n00011\n11000"
},
{
"input": "2\n1 2",
"output": "0\n2\n11\n11"
},
{
"input": "3\n1 1 1",
"output": "1\n0"
},
{
"input": "10\n6 8 7 6 8 7 6 7 8 7",
"output": "6\n4\n0100100010\n0000000111... | 140 | 307,200 | 0 | 25,869 | |
222 | Reducing Fractions | [
"implementation",
"math",
"number theory",
"sortings"
] | null | null | To confuse the opponents, the Galactic Empire represents fractions in an unusual format. The fractions are represented as two sets of integers. The product of numbers from the first set gives the fraction numerator, the product of numbers from the second set gives the fraction denominator. However, it turned out that t... | The first input line contains two space-separated integers *n*, *m* (1<=β€<=*n*,<=*m*<=β€<=105) that show how many numbers the first set (the numerator) and the second set (the denominator) contain, correspondingly.
The second line contains *n* space-separated integers: *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=107) ... | Print the answer to the problem in the form, similar to the form of the input data. The number of values in the sets you print *n**out*,<=*m**out* must satisfy the inequality 1<=β€<=*n**out*,<=*m**out*<=β€<=105, and the actual values in the sets *a**out*,<=*i* and *b**out*,<=*i* must satisfy the inequality 1<=β€<=*a**out*... | [
"3 2\n100 5 2\n50 10\n",
"4 3\n2 5 10 20\n100 1 3\n"
] | [
"2 3\n2 1\n1 1 1\n",
"1 1\n20\n3\n"
] | In the first test sample the numerator equals 1000, the denominator equals 500. If we reduce fraction 1000/500 by the greatest common divisor of the numerator and the denominator (by 500), we obtain fraction 2/1.
In the second test sample the numerator equals 2000, the denominator equals 300. If we reduce fraction 200... | [
{
"input": "3 2\n100 5 2\n50 10",
"output": "2 3\n2 1\n1 1 1"
},
{
"input": "4 3\n2 5 10 20\n100 1 3",
"output": "1 1\n20\n3"
},
{
"input": "2 3\n50 10\n100 5 2",
"output": "2 3\n1 1 \n2 1 1 "
},
{
"input": "1 1\n1\n1",
"output": "1 1\n1 \n1 "
},
{
"input": "3 2\n... | 1,840 | 201,216,000 | 3 | 25,873 | |
177 | Party | [
"dfs and similar",
"dsu",
"graphs"
] | null | null | To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friends... | The first line of input contains an integer *n* β the number of the Beaver's acquaintances.
The second line contains an integer *k* β the number of pairs of friends. Next *k* lines contain space-separated pairs of integers *u**i*,<=*v**i* β indices of people who form the *i*-th pair of friends.
The next line conta... | Output a single number β the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0. | [
"9\n8\n1 2\n1 3\n2 3\n4 5\n6 7\n7 8\n8 9\n9 6\n2\n1 6\n7 9\n"
] | [
"3"
] | Let's have a look at the example.
Two groups of people can be invited: {1,β2,β3} and {4,β5}, thus the answer will be the size of the largest of these groups. Group {6,β7,β8,β9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1,β2,β3,β4,β5} also doesn't fit, because not all of its members ... | [
{
"input": "9\n8\n1 2\n1 3\n2 3\n4 5\n6 7\n7 8\n8 9\n9 6\n2\n1 6\n7 9",
"output": "3"
},
{
"input": "2\n1\n1 2\n0",
"output": "2"
},
{
"input": "2\n0\n1\n1 2",
"output": "1"
},
{
"input": "3\n2\n1 2\n1 3\n1\n2 3",
"output": "0"
},
{
"input": "3\n3\n1 3\n2 1\n2 3\n... | 154 | 0 | 0 | 25,952 | |
653 | Bear and Up-Down | [
"brute force",
"implementation"
] | null | null | The life goes up and down, just like nice sequences. Sequence *t*1,<=*t*2,<=...,<=*t**n* is called nice if the following two conditions are satisfied:
- *t**i*<=<<=*t**i*<=+<=1 for each odd *i*<=<<=*n*; - *t**i*<=><=*t**i*<=+<=1 for each even *i*<=<<=*n*.
For example, sequences (2,<=8), (1,<=5,<=1) and... | The first line of the input contains one integer *n* (2<=β€<=*n*<=β€<=150<=000)Β β the length of the sequence.
The second line contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (1<=β€<=*t**i*<=β€<=150<=000) β the initial sequence. It's guaranteed that the given sequence is not nice. | Print the number of ways to swap two elements exactly once in order to get a nice sequence. | [
"5\n2 8 4 7 7\n",
"4\n200 150 100 50\n",
"10\n3 2 1 4 1 4 1 4 1 4\n",
"9\n1 2 3 4 5 6 7 8 9\n"
] | [
"2\n",
"1\n",
"8\n",
"0\n"
] | In the first sample, there are two ways to get a nice sequence with one swap:
1. Swap *t*<sub class="lower-index">2</sub>β=β8 with *t*<sub class="lower-index">4</sub>β=β7. 1. Swap *t*<sub class="lower-index">1</sub>β=β2 with *t*<sub class="lower-index">5</sub>β=β7.
In the second sample, there is only one wayΒ β Lim... | [
{
"input": "5\n2 8 4 7 7",
"output": "2"
},
{
"input": "4\n200 150 100 50",
"output": "1"
},
{
"input": "10\n3 2 1 4 1 4 1 4 1 4",
"output": "8"
},
{
"input": "9\n1 2 3 4 5 6 7 8 9",
"output": "0"
},
{
"input": "5\n1 1 1 4 3",
"output": "1"
},
{
"input... | 1,076 | 15,667,200 | 3 | 25,993 | |
645 | Robot Rapping Results Report | [
"binary search",
"dp",
"graphs"
] | null | null | While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some alternative jobs. In her new gig as a reporter, Bessie needs to know about programming competition results as quickly as possible. When she covers the 2016 Robot Rap Battle Tournament, she notices that all of the robots o... | The first line of the input consists of two integers, the number of robots *n* (2<=β€<=*n*<=β€<=100<=000) and the number of rap battles *m* ().
The next *m* lines describe the results of the rap battles in the order they took place. Each consists of two integers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*, *u**i*<=... | Print the minimum *k* such that the ordering of the robots by skill level is uniquely defined by the first *k* rap battles. If there exists more than one ordering that satisfies all *m* relations, output -1. | [
"4 5\n2 1\n1 3\n2 3\n4 2\n4 3\n",
"3 2\n1 2\n3 2\n"
] | [
"4\n",
"-1\n"
] | In the first sample, the robots from strongest to weakest must be (4,β2,β1,β3), which Bessie can deduce after knowing the results of the first four rap battles.
In the second sample, both (1,β3,β2) and (3,β1,β2) are possible orderings of the robots from strongest to weakest after both rap battles. | [
{
"input": "4 5\n2 1\n1 3\n2 3\n4 2\n4 3",
"output": "4"
},
{
"input": "3 2\n1 2\n3 2",
"output": "-1"
},
{
"input": "2 1\n1 2",
"output": "1"
},
{
"input": "2 1\n2 1",
"output": "1"
},
{
"input": "5 10\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4\n3 5\n4 5",
"outp... | 1,403 | 42,905,600 | 3 | 26,028 | |
407 | k-d-sequence | [
"data structures"
] | null | null | We'll call a sequence of integers a good *k*-*d* sequence if we can add to it at most *k* numbers in such a way that after the sorting the sequence will be an arithmetic progression with difference *d*.
You got hold of some sequence *a*, consisting of *n* integers. Your task is to find its longest contiguous subsegmen... | The first line contains three space-separated integers *n*,<=*k*,<=*d* (1<=β€<=*n*<=β€<=2Β·105;Β 0<=β€<=*k*<=β€<=2Β·105;Β 0<=β€<=*d*<=β€<=109). The second line contains *n* space-separated integers: *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=β€<=*a**i*<=β€<=109)Β β the actual sequence. | Print two space-separated integers *l*,<=*r* (1<=β€<=*l*<=β€<=*r*<=β€<=*n*) show that sequence *a**l*,<=*a**l*<=+<=1,<=...,<=*a**r* is the longest subsegment that is a good *k*-*d* sequence.
If there are multiple optimal answers, print the one with the minimum value of *l*. | [
"6 1 2\n4 3 2 8 6 2\n"
] | [
"3 5\n"
] | In the first test sample the answer is the subsegment consisting of numbers 2, 8, 6Β β after adding number 4 and sorting it becomes sequence 2, 4, 6, 8Β β the arithmetic progression with difference 2. | [] | 15 | 0 | -1 | 26,124 | |
784 | Touchy-Feely Palindromes | [
"*special",
"implementation"
] | null | null | The only line of the input contains a string of digits. The length of the string is between 1 and 10, inclusive.
Output "Yes" or "No". | The only line of the input contains a string of digits. The length of the string is between 1 and 10, inclusive. | Output "Yes" or "No". | [
"373\n",
"121\n",
"436\n"
] | [
"Yes\n",
"No\n",
"Yes\n"
] | none | [
{
"input": "373",
"output": "Yes"
},
{
"input": "121",
"output": "No"
},
{
"input": "436",
"output": "Yes"
},
{
"input": "7",
"output": "Yes"
},
{
"input": "8",
"output": "No"
},
{
"input": "4357087936",
"output": "Yes"
},
{
"input": "80697... | 46 | 0 | 3 | 26,199 | |
744 | Hongcow's Game | [
"bitmasks",
"divide and conquer",
"interactive"
] | null | null | This is an interactive problem. In the interaction section below you will see the information about flushing the output.
In this problem, you will be playing a game with Hongcow. How lucky of you!
Hongcow has a hidden *n* by *n* matrix *M*. Let *M**i*,<=*j* denote the entry *i*-th row and *j*-th column of the matrix.... | The first line of input will contain a single integer *n* (2<=β€<=*n*<=β€<=1,<=000). | To print the final answer, print out the string -1 on its own line. Then, the next line should contain *n* integers. The *i*-th integer should be the minimum value of the *i*-th row of the matrix, excluding elements on the diagonal. Do not forget to flush your answer! | [
"3\n0 0 0\n2 7 0\n0 0 4\n3 0 8\n0 5 4",
"2\n0 0\n0 0"
] | [
"3\n1 2 3\n1\n3\n2\n1 2\n1\n2\n1\n1\n-1\n2 5 4\n",
"1\n2\n1\n1\n-1\n0 0"
] | In the first sample, Hongcow has the hidden matrix
Here is a more readable version demonstrating the interaction. The column on the left represents Hongcow, while the column on the right represents the contestant.
For the second sample, it is possible for off-diagonal elements of the matrix to be zero. | [] | 62 | 614,400 | 3 | 26,339 | |
185 | Mushroom Scientists | [
"math",
"ternary search"
] | null | null | As you very well know, the whole Universe traditionally uses three-dimensional Cartesian system of coordinates. In this system each point corresponds to three real coordinates (*x*,<=*y*,<=*z*). In this coordinate system, the distance between the center of the Universe and the point is calculated by the following formu... | The first line contains a single integer *S* (1<=β€<=*S*<=β€<=103) β the maximum sum of coordinates of the sought point.
The second line contains three space-separated integers *a*, *b*, *c* (0<=β€<=*a*,<=*b*,<=*c*<=β€<=103) β the numbers that describe the metric of mushroom scientists. | Print three real numbers β the coordinates of the point that reaches maximum value in the metrics of mushroom scientists. If there are multiple answers, print any of them that meets the limitations.
A natural logarithm of distance from the center of the Universe to the given point in the metric of mushroom scientists ... | [
"3\n1 1 1\n",
"3\n2 0 0\n"
] | [
"1.0 1.0 1.0\n",
"3.0 0.0 0.0\n"
] | none | [
{
"input": "3\n1 1 1",
"output": "1.0 1.0 1.0"
},
{
"input": "3\n2 0 0",
"output": "3.0 0.0 0.0"
},
{
"input": "10\n1 6 3",
"output": "1.0 6.0 3.0"
},
{
"input": "9\n8 2 0",
"output": "7.2 1.8 0.0"
},
{
"input": "1\n0 9 2",
"output": "0.0 0.8181818181818182 0.... | 92 | 0 | 0 | 26,350 | |
847 | Load Testing | [
"greedy"
] | null | null | Polycarp plans to conduct a load testing of its new project Fakebook. He already agreed with his friends that at certain points in time they will send requests to Fakebook. The load testing will last *n* minutes and in the *i*-th minute friends will send *a**i* requests.
Polycarp plans to test Fakebook under a special... | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=100<=000) β the duration of the load testing.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109), where *a**i* is the number of requests from friends in the *i*-th minute of the load testing. | Print the minimum number of additional requests from Polycarp that would make the load strictly increasing in the beginning and then strictly decreasing afterwards. | [
"5\n1 4 3 2 5\n",
"5\n1 2 2 2 1\n",
"7\n10 20 40 50 70 90 30\n"
] | [
"6\n",
"1\n",
"0\n"
] | In the first example Polycarp must make two additional requests in the third minute and four additional requests in the fourth minute. So the resulting load will look like: [1, 4, 5, 6, 5]. In total, Polycarp will make 6 additional requests.
In the second example it is enough to make one additional request in the thir... | [
{
"input": "5\n1 4 3 2 5",
"output": "6"
},
{
"input": "5\n1 2 2 2 1",
"output": "1"
},
{
"input": "7\n10 20 40 50 70 90 30",
"output": "0"
},
{
"input": "1\n1",
"output": "0"
},
{
"input": "2\n1 15",
"output": "0"
},
{
"input": "4\n36 54 55 9",
"o... | 296 | 14,131,200 | 3 | 26,456 | |
28 | pSort | [
"dfs and similar",
"dsu",
"graphs"
] | B. pSort | 2 | 256 | One day *n* cells of some array decided to play the following game. Initially each cell contains a number which is equal to it's ordinal number (starting from 1). Also each cell determined it's favourite number. On it's move *i*-th cell can exchange it's value with the value of some other *j*-th cell, if |*i*<=-<=*j*|<... | The first line contains positive integer *n* (1<=β€<=*n*<=β€<=100) β the number of cells in the array. The second line contains *n* distinct integers from 1 to *n* β permutation. The last line contains *n* integers from 1 to *n* β favourite numbers of the cells. | If the given state is reachable in the described game, output YES, otherwise NO. | [
"5\n5 4 3 2 1\n1 1 1 1 1\n",
"7\n4 3 5 1 2 7 6\n4 6 6 1 6 6 1\n",
"7\n4 2 5 1 3 7 6\n4 6 6 1 6 6 1\n"
] | [
"YES\n",
"NO\n",
"YES\n"
] | none | [
{
"input": "5\n5 4 3 2 1\n1 1 1 1 1",
"output": "YES"
},
{
"input": "7\n4 3 5 1 2 7 6\n4 6 6 1 6 6 1",
"output": "NO"
},
{
"input": "7\n4 2 5 1 3 7 6\n4 6 6 1 6 6 1",
"output": "YES"
},
{
"input": "6\n3 2 1 4 6 5\n3 6 1 6 6 1",
"output": "YES"
},
{
"input": "6\n3 ... | 280 | 20,172,800 | 3.892425 | 26,463 |
474 | Ant colony | [
"data structures",
"math",
"number theory"
] | null | null | Mole is hungry again. He found one ant colony, consisting of *n* ants, ordered in a row. Each ant *i* (1<=β€<=*i*<=β€<=*n*) has a strength *s**i*.
In order to make his dinner more interesting, Mole organizes a version of Β«Hunger GamesΒ» for the ants. He chooses two numbers *l* and *r* (1<=β€<=*l*<=β€<=*r*<=β€<=*n*) and each... | The first line contains one integer *n* (1<=β€<=*n*<=β€<=105), the size of the ant colony.
The second line contains *n* integers *s*1,<=*s*2,<=...,<=*s**n* (1<=β€<=*s**i*<=β€<=109), the strengths of the ants.
The third line contains one integer *t* (1<=β€<=*t*<=β€<=105), the number of test cases.
Each of the next *t* l... | Print to the standard output *t* lines. The *i*-th line contains number of ants that Mole eats from the segment [*l**i*,<=*r**i*]. | [
"5\n1 3 2 4 2\n4\n1 5\n2 5\n3 5\n4 5\n"
] | [
"4\n4\n1\n1\n"
] | In the first test battle points for each ant are *v*β=β[4,β0,β2,β0,β2], so ant number 1 is freed. Mole eats the ants 2, 3, 4, 5.
In the second test case battle points are *v*β=β[0,β2,β0,β2], so no ant is freed and all of them are eaten by Mole.
In the third test case battle points are *v*β=β[2,β0,β2], so ants number ... | [
{
"input": "5\n1 3 2 4 2\n4\n1 5\n2 5\n3 5\n4 5",
"output": "4\n4\n1\n1"
}
] | 31 | 0 | 0 | 26,482 | |
161 | Discounts | [
"constructive algorithms",
"greedy",
"sortings"
] | null | null | One day Polycarpus stopped by a supermarket on his way home. It turns out that the supermarket is having a special offer for stools. The offer is as follows: if a customer's shopping cart contains at least one stool, the customer gets a 50% discount on the cheapest item in the cart (that is, it becomes two times cheape... | The first input line contains two integers *n* and *k* (1<=β€<=*k*<=β€<=*n*<=β€<=103) β the number of items in the supermarket and the number of carts, correspondingly. Next *n* lines describe the items as "*c**i* *t**i*" (without the quotes), where *c**i* (1<=β€<=*c**i*<=β€<=109) is an integer denoting the price of the *i*... | In the first line print a single real number with exactly one decimal place β the minimum total price of the items, including the discounts.
In the following *k* lines print the descriptions of the items in the carts. In the *i*-th line print the description of the *i*-th cart as "*t* *b*1 *b*2 ... *b**t*" (without t... | [
"3 2\n2 1\n3 2\n3 1\n",
"4 3\n4 1\n1 2\n2 2\n3 2\n"
] | [
"5.5\n2 1 2\n1 3\n",
"8.0\n1 1\n2 4 2\n1 3\n"
] | In the first sample case the first cart should contain the 1st and 2nd items, and the second cart should contain the 3rd item. This way each cart has a stool and each cart has a 50% discount for the cheapest item. The total price of all items will be: 2Β·0.5β+β(3β+β3Β·0.5)β=β1β+β4.5β=β5.5. | [
{
"input": "3 2\n2 1\n3 2\n3 1",
"output": "5.5\n2 1 2\n1 3"
},
{
"input": "4 3\n4 1\n1 2\n2 2\n3 2",
"output": "8.0\n1 1\n1 2\n2 3 4"
},
{
"input": "1 1\n1 1",
"output": "0.5\n1 1"
},
{
"input": "1 1\n1 2",
"output": "1.0\n1 1"
},
{
"input": "10 1\n1 1\n2 2\n1 1\... | 280 | 20,172,800 | 0 | 26,528 | |
705 | Spider Man | [
"games",
"math"
] | null | null | Peter Parker wants to play a game with Dr. Octopus. The game is about cycles. Cycle is a sequence of vertices, such that first one is connected with the second, second is connected with third and so on, while the last one is connected with the first one again. Cycle may consist of a single isolated vertex.
Initially t... | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=100<=000)Β β the number of tests Peter is about to make.
The second line contains *n* space separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109), *i*-th of them stands for the number of vertices in the cycle added before the *i*-th... | Print the result of all tests in order they are performed. Print 1 if the player who moves first wins or 2 otherwise. | [
"3\n1 2 3\n",
"5\n1 1 5 1 1\n"
] | [
"2\n1\n1\n",
"2\n2\n2\n2\n2\n"
] | In the first sample test:
In Peter's first test, there's only one cycle with 1 vertex. First player cannot make a move and loses.
In his second test, there's one cycle with 1 vertex and one with 2. No one can make a move on the cycle with 1 vertex. First player can replace the second cycle with two cycles of 1 vertex... | [
{
"input": "3\n1 2 3",
"output": "2\n1\n1"
},
{
"input": "5\n1 1 5 1 1",
"output": "2\n2\n2\n2\n2"
},
{
"input": "1\n167959139",
"output": "2"
},
{
"input": "10\n802030518 598196518 640274071 983359971 71550121 96204862 799843967 446173607 796619138 402690754",
"output": ... | 139 | 14,643,200 | 3 | 26,582 | |
720 | Cactusophobia | [
"dfs and similar",
"flows"
] | null | null | Tree is a connected undirected graph that has no cycles. Edge cactus is a connected undirected graph without loops and parallel edges, such that each edge belongs to at most one cycle.
Vasya has an edge cactus, each edge of this graph has some color.
Vasya would like to remove the minimal number of edges in such way ... | The first line contains two integers: *n*, *m* (2<=β€<=*n*<=β€<=10<=000)Β β the number of vertices and the number of edges in Vasya's graph, respectively.
The following *m* lines contain three integers each: *u*, *v*, *c* (1<=β€<=*u*,<=*v*<=β€<=*n*, *u*<=β <=*v*, 1<=β€<=*c*<=β€<=*m*)Β β the numbers of vertices connected by the... | Output one integer: the maximal number of different colors that the resulting tree can have. | [
"4 4\n1 2 4\n2 3 1\n3 4 2\n4 2 3\n",
"7 9\n1 2 1\n2 3 4\n3 1 5\n1 4 5\n4 5 2\n5 1 6\n1 6 4\n6 7 6\n7 1 3\n"
] | [
"3\n",
"6\n"
] | none | [
{
"input": "4 4\n1 2 4\n2 3 1\n3 4 2\n4 2 3",
"output": "3"
},
{
"input": "7 9\n1 2 1\n2 3 4\n3 1 5\n1 4 5\n4 5 2\n5 1 6\n1 6 4\n6 7 6\n7 1 3",
"output": "6"
},
{
"input": "5 6\n4 1 2\n4 2 3\n5 1 5\n1 3 1\n1 2 1\n5 3 4",
"output": "4"
},
{
"input": "9 10\n8 7 2\n8 2 10\n8 4 2... | 46 | 0 | 0 | 26,605 | |
750 | New Year and Fireworks | [
"brute force",
"data structures",
"dfs and similar",
"dp",
"implementation"
] | null | null | One tradition of welcoming the New Year is launching fireworks into the sky. Usually a launched firework flies vertically upward for some period of time, then explodes, splitting into several parts flying in different directions. Sometimes those parts also explode after some period of time, splitting into even more par... | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=30)Β β the total depth of the recursion.
The second line contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (1<=β€<=*t**i*<=β€<=5). On the *i*-th level each of 2*i*<=-<=1 parts will cover *t**i* cells before exploding. | Print one integer, denoting the number of cells which will be visited at least once by any part of the firework. | [
"4\n4 2 2 3\n",
"6\n1 1 1 1 1 3\n",
"1\n3\n"
] | [
"39\n",
"85\n",
"3\n"
] | For the first sample, the drawings below show the situation after each level of recursion. Limak launched the firework from the bottom-most red cell. It covered *t*<sub class="lower-index">1</sub>β=β4 cells (marked red), exploded and divided into two parts (their further movement is marked green). All explosions are ma... | [] | 1,746 | 268,390,400 | 0 | 26,668 | |
282 | XOR and OR | [
"constructive algorithms",
"implementation",
"math"
] | null | null | The Bitlandians are quite weird people. They do everything differently. They have a different alphabet so they have a different definition for a string.
A Bitlandish string is a string made only of characters "0" and "1".
BitHaval (the mayor of Bitland) loves to play with Bitlandish strings. He takes some Bitlandish ... | The first line contains Bitlandish string *a*, the second line contains Bitlandish string *b*. The strings can have different lengths.
It is guaranteed that the given strings only consist of characters "0" and "1". The strings are not empty, their length doesn't exceed 106. | Print "YES" if *a* can be transformed into *b*, otherwise print "NO". Please do not print the quotes. | [
"11\n10\n",
"1\n01\n",
"000\n101\n"
] | [
"YES\n",
"NO\n",
"NO\n"
] | none | [
{
"input": "11\n10",
"output": "YES"
},
{
"input": "1\n01",
"output": "NO"
},
{
"input": "000\n101",
"output": "NO"
},
{
"input": "1101\n1111",
"output": "YES"
},
{
"input": "11000001\n00000001",
"output": "YES"
},
{
"input": "01\n10",
"output": "Y... | 62 | 0 | 0 | 26,716 | |
42 | Guilty --- to the kitchen! | [
"greedy",
"implementation"
] | A. Guilty β to the kitchen! | 2 | 256 | It's a very unfortunate day for Volodya today. He got bad mark in algebra and was therefore forced to do some work in the kitchen, namely to cook borscht (traditional Russian soup). This should also improve his algebra skills.
According to the borscht recipe it consists of *n* ingredients that have to be mixed in prop... | The first line of the input contains two space-separated integers *n* and *V* (1<=β€<=*n*<=β€<=20,<=1<=β€<=*V*<=β€<=10000). The next line contains *n* space-separated integers *a**i* (1<=β€<=*a**i*<=β€<=100). Finally, the last line contains *n* space-separated integers *b**i* (0<=β€<=*b**i*<=β€<=100). | Your program should output just one real number β the volume of soup that Volodya will cook. Your answer must have a relative or absolute error less than 10<=-<=4. | [
"1 100\n1\n40\n",
"2 100\n1 1\n25 30\n",
"2 100\n1 1\n60 60\n"
] | [
"40.0\n",
"50.0\n",
"100.0\n"
] | none | [
{
"input": "1 100\n1\n40",
"output": "40.0"
},
{
"input": "2 100\n1 1\n25 30",
"output": "50.0"
},
{
"input": "2 100\n1 1\n60 60",
"output": "100.0"
},
{
"input": "2 100\n1 1\n50 50",
"output": "100.0"
},
{
"input": "2 100\n1 2\n33 66",
"output": "99.0"
},
... | 92 | 0 | 3.977 | 26,770 |
856 | Eleventh Birthday | [
"combinatorics",
"dp",
"math"
] | null | null | It is Borya's eleventh birthday, and he has got a great present: *n* cards with numbers. The *i*-th card has the number *a**i* written on it. Borya wants to put his cards in a row to get one greater number. For example, if Borya has cards with numbers 1, 31, and 12, and he puts them in a row in this order, he would get... | Input data contains multiple test cases. The first line of the input data contains an integer *t*Β β the number of test cases (1<=β€<=*t*<=β€<=100). The descriptions of test cases follow.
Each test is described by two lines.
The first line contains an integer *n* (1<=β€<=*n*<=β€<=2000)Β β the number of cards in Borya's pre... | For each test case output one line: the number of ways to put the cards to the table so that the resulting big number was divisible by 11, print the number modulo 998244353. | [
"4\n2\n1 1\n3\n1 31 12\n3\n12345 67 84\n9\n1 2 3 4 5 6 7 8 9\n"
] | [
"2\n2\n2\n31680\n"
] | none | [] | 46 | 0 | 0 | 26,788 | |
457 | Distributed Join | [
"greedy"
] | null | null | Piegirl was asked to implement two table join operation for distributed database system, minimizing the network traffic.
Suppose she wants to join two tables, *A* and *B*. Each of them has certain number of rows which are distributed on different number of partitions. Table *A* is distributed on the first cluster cons... | First line contains two integer numbers, *m* and *n* (1<=β€<=*m*,<=*n*<=β€<=105). Second line contains description of the first cluster with *m* space separated integers, *a**i* (1<=β€<=*a**i*<=β€<=109). Similarly, third line describes second cluster with *n* space separated integers, *b**i* (1<=β€<=*b**i*<=β€<=109). | Print one integer β minimal number of copy operations. | [
"2 2\n2 6\n3 100\n",
"2 3\n10 10\n1 1 1\n"
] | [
"11\n",
"6\n"
] | In the first example it makes sense to move all the rows to the second partition of the second cluster which is achieved in 2β+β6β+β3β=β11 operations
In the second example Piegirl can copy each row from *B* to the both partitions of the first cluster which needs 2Β·3β=β6 copy operations. | [
{
"input": "2 2\n2 6\n3 100",
"output": "11"
},
{
"input": "2 3\n10 10\n1 1 1",
"output": "6"
},
{
"input": "2 2\n888381664 866366630\n170399907 404233949",
"output": "1149267712"
},
{
"input": "3 4\n337369924 278848730 654933675\n866361693 732544605 890800310 350303294",
... | 0 | 0 | -1 | 26,835 | |
103 | Buying Sets | [
"flows",
"graph matchings"
] | E. Buying Sets | 2 | 256 | The Hexadecimal virus loves playing with number sets β intersecting them, uniting them. One beautiful day she was surprised to find out that Scuzzy, her spherical pet cat, united all sets in one and ate the result! Something had to be done quickly and Hexadecimal rushed to the market.
The market has *n* sets of number... | The first line contains the only number *n* (1<=β€<=*n*<=β€<=300) β the number of sets available in the market.
Next *n* lines describe the goods: first we are given *m**i* (1<=β€<=*m**i*<=β€<=*n*) β the number of distinct numbers in the *i*-th set, then follow *m**i* numbers β the set's elements. We know that the set's e... | Print a single number β the minimum price the virus will have to pay for such a collection of *k* sets that union of the collection's sets would have exactly *k* distinct numbers (). | [
"3\n1 1\n2 2 3\n1 3\n10 20 -3\n",
"5\n2 1 2\n2 2 3\n2 3 4\n2 4 5\n2 5 1\n1 -1 1 -1 1\n",
"5\n2 1 2\n2 2 3\n2 3 4\n2 4 5\n2 5 1\n-1 1 -1 1 -1\n"
] | [
"-3\n",
"0\n",
"-1\n"
] | none | [] | 92 | 0 | 0 | 26,963 |
161 | Abracadabra | [
"divide and conquer"
] | null | null | Polycarpus analyzes a string called abracadabra. This string is constructed using the following algorithm:
- On the first step the string consists of a single character "a". - On the *k*-th step Polycarpus concatenates two copies of the string obtained on the (*k*<=-<=1)-th step, while inserting the *k*-th characte... | The input consists of a single line containing four integers *l*1, *r*1, *l*2, *r*2 (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=109, *i*<==<=1,<=2). The numbers are separated by single spaces. *l**i* and *r**i* give the indices of the first and the last characters of the *i*-th chosen substring, correspondingly (*i*<==<=1,<=2). The c... | Print a single number β the length of the longest common substring of the given strings. If there are no common substrings, print 0. | [
"3 6 1 4\n",
"1 1 4 4\n"
] | [
"2\n",
"0\n"
] | In the first sample the first substring is "acab", the second one is "abac". These two substrings have two longest common substrings "ac" and "ab", but we are only interested in their length β 2.
In the second sample the first substring is "a", the second one is "c". These two substrings don't have any common characte... | [
{
"input": "3 6 1 4",
"output": "2"
},
{
"input": "1 1 4 4",
"output": "0"
},
{
"input": "2 2 6 6",
"output": "1"
},
{
"input": "73426655 594361930 343984155 989446962",
"output": "379149396"
},
{
"input": "169720415 312105195 670978284 671296539",
"output": "... | 15 | 0 | -1 | 26,989 | |
436 | Pudding Monsters | [
"dp"
] | null | null | Have you ever played Pudding Monsters? In this task, a simplified one-dimensional model of this game is used.
Imagine an infinite checkered stripe, the cells of which are numbered sequentially with integers. Some cells of the strip have monsters, other cells of the strip are empty. All monsters are made of pudding, so... | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=105;Β 1<=β€<=*m*<=β€<=2000) β the number of monsters on the strip and the number of special cells.
The second line contains *n* distinct integers β the numbers of the cells with monsters, then the third line contains *m* distinct integers β the numbers of th... | Print a single integer β the maximum number of special cells that will contain monsters in the optimal game. | [
"3 2\n1 3 5\n2 4\n",
"4 2\n1 3 4 6\n2 5\n",
"4 2\n1 8 4 5\n7 2\n"
] | [
"2\n",
"2\n",
"1\n"
] | none | [] | 46 | 0 | 0 | 27,047 | |
370 | Mittens | [
"constructive algorithms",
"greedy",
"sortings"
] | null | null | A Christmas party in city S. had *n* children. All children came in mittens. The mittens can be of different colors, but each child had the left and the right mitten of the same color. Let's say that the colors of the mittens are numbered with integers from 1 to *m*, and the children are numbered from 1 to *n*. Then th... | The first line contains two integers *n* and *m* β the number of the children and the number of possible mitten colors (1<=β€<=*n*<=β€<=5000, 1<=β€<=*m*<=β€<=100). The second line contains *n* integers *c*1,<=*c*2,<=... *c**n*, where *c**i* is the color of the mittens of the *i*-th child (1<=β€<=*c**i*<=β€<=*m*). | In the first line, print the maximum number of children who can end up with a distinct-colored pair of mittens. In the next *n* lines print the way the mittens can be distributed in this case. On the *i*-th of these lines print two space-separated integers: the color of the left and the color of the right mitten the *i... | [
"6 3\n1 3 2 2 1 1\n",
"4 2\n1 2 1 1\n"
] | [
"6\n2 1\n1 2\n2 1\n1 3\n1 2\n3 1\n",
"2\n1 2\n1 1\n2 1\n1 1\n"
] | none | [
{
"input": "6 3\n1 3 2 2 1 1",
"output": "6\n2 1\n1 2\n2 1\n1 3\n1 2\n3 1"
},
{
"input": "4 2\n1 2 1 1",
"output": "2\n1 2\n1 1\n2 1\n1 1"
},
{
"input": "1 1\n1",
"output": "0\n1 1"
},
{
"input": "4 1\n1 1 1 1",
"output": "0\n1 1\n1 1\n1 1\n1 1"
},
{
"input": "2 2... | 46 | 0 | 0 | 27,065 | |
620 | Professor GukiZ and Two Arrays | [
"binary search",
"two pointers"
] | null | null | Professor GukiZ has two arrays of integers, a and b. Professor wants to make the sum of the elements in the array a *s**a* as close as possible to the sum of the elements in the array b *s**b*. So he wants to minimize the value *v*<==<=|*s**a*<=-<=*s**b*|.
In one operation professor can swap some element from the arra... | The first line contains integer *n* (1<=β€<=*n*<=β€<=2000) β the number of elements in the array a.
The second line contains *n* integers *a**i* (<=-<=109<=β€<=*a**i*<=β€<=109) β the elements of the array a.
The third line contains integer *m* (1<=β€<=*m*<=β€<=2000) β the number of elements in the array b.
The fourth line... | In the first line print the minimal value *v*<==<=|*s**a*<=-<=*s**b*| that can be got with no more than two swaps.
The second line should contain the number of swaps *k* (0<=β€<=*k*<=β€<=2).
Each of the next *k* lines should contain two integers *x**p*,<=*y**p* (1<=β€<=*x**p*<=β€<=*n*,<=1<=β€<=*y**p*<=β€<=*m*) β the index ... | [
"5\n5 4 3 2 1\n4\n1 1 1 1\n",
"5\n1 2 3 4 5\n1\n15\n",
"5\n1 2 3 4 5\n4\n1 2 3 4\n"
] | [
"1\n2\n1 1\n4 2\n",
"0\n0\n",
"1\n1\n3 1\n"
] | none | [
{
"input": "5\n5 4 3 2 1\n4\n1 1 1 1",
"output": "1\n2\n1 1\n4 2"
},
{
"input": "5\n1 2 3 4 5\n1\n15",
"output": "0\n0"
},
{
"input": "5\n1 2 3 4 5\n4\n1 2 3 4",
"output": "1\n1\n3 1"
},
{
"input": "1\n-42\n1\n-86",
"output": "44\n0"
},
{
"input": "1\n-21\n10\n-43... | 1,840 | 72,601,600 | 0 | 27,123 | |
111 | Petya and Spiders | [
"bitmasks",
"dp",
"dsu"
] | C. Petya and Spiders | 2 | 256 | Little Petya loves training spiders. Petya has a board *n*<=Γ<=*m* in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform its commands. There are 5 possible commands: to stay idle or to move from current cell ... | The first line contains two space-separated integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=40,<=*n*Β·*m*<=β€<=40) β the board sizes. | In the first line print the maximum number of cells without spiders. | [
"1 1\n",
"2 3\n"
] | [
"0\n",
"4\n"
] | In the first sample the only possible answer is:
s
In the second sample one of the possible solutions is:
s denotes command "stay idle", l, r, d, u denote commands "crawl left", "crawl right", "crawl down", "crawl up", correspondingly. | [
{
"input": "1 1",
"output": "0"
},
{
"input": "2 3",
"output": "4"
},
{
"input": "4 1",
"output": "2"
},
{
"input": "4 2",
"output": "5"
},
{
"input": "4 3",
"output": "8"
},
{
"input": "4 4",
"output": "12"
},
{
"input": "1 40",
"outpu... | 31 | 0 | 0 | 27,172 |
729 | Subordinates | [
"constructive algorithms",
"data structures",
"graphs",
"greedy",
"sortings"
] | null | null | There are *n* workers in a company, each of them has a unique id from 1 to *n*. Exaclty one of them is a chief, his id is *s*. Each worker except the chief has exactly one immediate superior.
There was a request to each of the workers to tell how how many superiors (not only immediate). Worker's superiors are his imme... | The first line contains two positive integers *n* and *s* (1<=β€<=*n*<=β€<=2Β·105, 1<=β€<=*s*<=β€<=*n*)Β β the number of workers and the id of the chief.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=*n*<=-<=1), where *a**i* is the number of superiors (not only immediate) the worker with... | Print the minimum number of workers that could make a mistake. | [
"3 2\n2 0 2\n",
"5 3\n1 0 0 4 1\n"
] | [
"1\n",
"2\n"
] | In the first example it is possible that only the first worker made a mistake. Then:
- the immediate superior of the first worker is the second worker, - the immediate superior of the third worker is the first worker, - the second worker is the chief. | [
{
"input": "3 2\n2 0 2",
"output": "1"
},
{
"input": "5 3\n1 0 0 4 1",
"output": "2"
},
{
"input": "1 1\n0",
"output": "0"
},
{
"input": "2 1\n0 0",
"output": "1"
},
{
"input": "2 1\n0 1",
"output": "0"
},
{
"input": "2 1\n1 0",
"output": "2"
},
... | 61 | 2,764,800 | -1 | 27,203 | |
176 | Archaeology | [
"data structures",
"dfs and similar",
"trees"
] | null | null | This time you should help a team of researchers on an island in the Pacific Ocean. They research the culture of the ancient tribes that used to inhabit the island many years ago.
Overall they've dug out *n* villages. Some pairs of villages were connected by roads. People could go on the roads in both directions. Overa... | The first line contains an integer *n* (1<=β€<=*n*<=β€<=105) β the number of villages. The next *n*<=-<=1 lines describe the roads. The *i*-th of these lines contains three integers *a**i*, *b**i* and *c**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*, *a**i*<=β <=*b**i*, 1<=β€<=*c**i*<=β€<=109, 1<=β€<=*i*<=<<=*n*) β the numbers of vi... | For each query of type "?" print the total length of used roads on a single line. You should print the answers to the queries in the order, in which they are given in the input.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier... | [
"6\n1 2 1\n1 3 5\n4 1 7\n4 5 3\n6 4 2\n10\n+ 3\n+ 1\n?\n+ 6\n?\n+ 5\n?\n- 6\n- 3\n?\n"
] | [
"5\n14\n17\n10\n"
] | none | [] | 92 | 0 | 0 | 27,219 | |
37 | Old Berland Language | [
"data structures",
"greedy",
"trees"
] | C. Old Berland Language | 2 | 256 | Berland scientists know that the Old Berland language had exactly *n* words. Those words had lengths of *l*1,<=*l*2,<=...,<=*l**n* letters. Every word consisted of two letters, 0 and 1. Ancient Berland people spoke quickly and didnβt make pauses between the words, but at the same time they could always understand each ... | The first line contains one integer *N* (1<=β€<=*N*<=β€<=1000) β the number of words in Old Berland language. The second line contains *N* space-separated integers β the lengths of these words. All the lengths are natural numbers not exceeding 1000. | If thereβs no such set of words, in the single line output NO. Otherwise, in the first line output YES, and in the next *N* lines output the words themselves in the order their lengths were given in the input file. If the answer is not unique, output any. | [
"3\n1 2 3\n",
"3\n1 1 1\n"
] | [
"YES\n0\n10\n110\n",
"NO\n"
] | none | [
{
"input": "3\n1 2 3",
"output": "YES\n0\n10\n110"
},
{
"input": "3\n1 1 1",
"output": "NO"
},
{
"input": "10\n4 4 4 4 4 4 4 4 4 4",
"output": "YES\n0000\n0001\n0010\n0011\n0100\n0101\n0110\n0111\n1000\n1001"
},
{
"input": "20\n6 7 7 7 7 6 7 7 7 7 7 7 7 7 7 7 7 7 6 7",
"o... | 310 | 3,174,400 | 3.916587 | 27,273 |
543 | Listening to Music | [
"constructive algorithms",
"data structures"
] | null | null | Please note that the memory limit differs from the standard.
You really love to listen to music. During the each of next *s* days you will listen to exactly *m* songs from the playlist that consists of exactly *n* songs. Let's number the songs from the playlist with numbers from 1 to *n*, inclusive. The quality of son... | The first line contains two positive integers *n*, *m* (1<=β€<=*m*<=β€<=*n*<=β€<=2Β·105). The second line contains *n* positive integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=<<=230) β the description of songs from the playlist.
The next line contains a single number *s* (1<=β€<=*s*<=β€<=2Β·105) β the number of days ... | Print exactly *s* integers *ans*1,<=*ans*2,<=...,<=*ans**s*, where *ans**i* is the minimum displeasure that you can get on day *i*. | [
"5 3\n1 2 1 2 3\n5\n1 1 2\n1 3 2\n1 3 3\n1 3 5\n1 3 1\n"
] | [
"2\n0\n2\n3\n1\n"
] | none | [] | 46 | 0 | 0 | 27,405 | |
141 | Take-off Ramps | [
"graphs",
"shortest paths"
] | null | null | Vasya participates in a ski race along the *X* axis. The start is at point 0, and the finish is at *L*, that is, at a distance *L* meters from the start in the positive direction of the axis. Vasya has been training so hard that he can run one meter in exactly one second.
Besides, there are *n* take-off ramps on the t... | The first line contains two integers *n* and *L* (0<=β€<=*n*<=β€<=105, 1<=β€<=*L*<=β€<=109). Then *n* lines contain the descriptions of the ramps, each description is on a single line. Each description is a group of four non-negative integers *x**i*, *d**i*, *t**i*, *p**i* (0<=β€<=*x**i*<=β€<=*L*, 1<=β€<=*d**i*,<=*t**i*,<=*p*... | Print in the first line the minimum time in seconds Vasya needs to complete the track. Print in the second line *k* β the number of take-off ramps that Vasya needs to use, and print on the third line of output *k* numbers the number the take-off ramps Vasya used in the order in which he used them. Print each number exa... | [
"2 20\n5 10 5 5\n4 16 1 7\n",
"2 20\n9 8 12 6\n15 5 1 1\n"
] | [
"15\n1\n1 ",
"16\n1\n2 "
] | In the first sample, Vasya cannot use ramp 2, because then he will need to gather speed starting from point -3, which is not permitted by the statement. The optimal option is using ramp 1, the resulting time is: moving to the point of gathering speed + gathering speed until reaching the takeoff ramp + flight time + mov... | [
{
"input": "2 20\n5 10 5 5\n4 16 1 7",
"output": "15\n1\n1 "
},
{
"input": "2 20\n9 8 12 6\n15 5 1 1",
"output": "16\n1\n2 "
},
{
"input": "0 10",
"output": "10\n0"
},
{
"input": "1 20\n2 1 10 1",
"output": "20\n0"
},
{
"input": "3 50\n44 3 4 47\n2 40 33 2\n28 16 ... | 124 | 614,400 | 0 | 27,448 | |
8 | Two Friends | [
"binary search",
"geometry"
] | D. Two Friends | 1 | 64 | Two neighbours, Alan and Bob, live in the city, where there are three buildings only: a cinema, a shop and the house, where they live. The rest is a big asphalt square.
Once they went to the cinema, and the film impressed them so deeply, that when they left the cinema, they did not want to stop discussing it.
Bob wa... | The first line contains two integers: *t*1,<=*t*2 (0<=β€<=*t*1,<=*t*2<=β€<=100). The second line contains the cinema's coordinates, the third one β the house's, and the last line β the shop's.
All the coordinates are given in meters, are integer, and do not exceed 100 in absolute magnitude. No two given places are in t... | In the only line output one number β the maximum distance that Alan and Bob will cover together, discussing the film. Output the answer accurate to not less than 4 decimal places. | [
"0 2\n0 0\n4 0\n-3 0\n",
"0 0\n0 0\n2 0\n1 0\n"
] | [
"1.0000000000\n",
"2.0000000000\n"
] | none | [
{
"input": "0 2\n0 0\n4 0\n-3 0",
"output": "1.0000000000"
},
{
"input": "0 0\n0 0\n2 0\n1 0",
"output": "2.0000000000"
},
{
"input": "0 2\n0 0\n40 0\n-31 1",
"output": "1.0002538218"
},
{
"input": "100 2\n0 0\n4 0\n-3 0",
"output": "6.0000000000"
},
{
"input": "2... | 62 | 0 | 0 | 27,550 |
755 | PolandBall and White-Red graph | [
"constructive algorithms",
"graphs",
"shortest paths"
] | null | null | PolandBall has an undirected simple graph consisting of *n* vertices. Unfortunately, it has no edges. The graph is very sad because of that. PolandBall wanted to make it happier, adding some red edges. Then, he will add white edges in every remaining place. Therefore, the final graph will be a clique in two colors: whi... | The only one input line contains two integers *n* and *k* (2<=β€<=*n*<=β€<=1000, 1<=β€<=*k*<=β€<=1000), representing graph's size and sought colorfulness. | If it's impossible to find a suitable graph, print -1.
Otherwise, you can output any graph which fulfills PolandBall's requirements. First, output *m*Β β the number of red edges in your graph. Then, you should output *m* lines, each containing two integers *a**i* and *b**i*, (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*, *a**i*<=β <=*... | [
"4 1\n",
"5 2\n"
] | [
"-1\n",
"4\n1 2\n2 3\n3 4\n4 5\n"
] | In the first sample case, no graph can fulfill PolandBall's requirements.
In the second sample case, red graph is a path from 1 to 5. Its diameter is 4. However, white graph has diameter 2, because it consists of edges 1-3, 1-4, 1-5, 2-4, 2-5, 3-5. | [
{
"input": "4 1",
"output": "-1"
},
{
"input": "5 2",
"output": "4\n1 2\n2 3\n3 4\n4 5"
},
{
"input": "500 3",
"output": "123755\n1 2\n499 500\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 2... | 46 | 0 | 0 | 27,561 | |
0 | none | [
"none"
] | null | null | Two best friends Serozha and Gena play a game.
Initially there is one pile consisting of *n* stones on the table. During one move one pile should be taken and divided into an arbitrary number of piles consisting of *a*1<=><=*a*2<=><=...<=><=*a**k*<=><=0 stones. The piles should meet the condition *a*1<=-<=... | The single line contains a single integer *n* (1<=β€<=*n*<=β€<=105). | If Serozha wins, print *k*, which represents the minimal number of piles into which he can split the initial one during the first move in order to win the game.
If Gena wins, print "-1" (without the quotes). | [
"3\n",
"6\n",
"100\n"
] | [
"2\n",
"-1\n",
"8\n"
] | none | [] | 60 | 0 | 0 | 27,573 | |
125 | Hobbits' Party | [
"constructive algorithms",
"greedy"
] | null | null | Everyone knows that hobbits love to organize all sorts of parties and celebrations. There are *n* hobbits living in the Shire. They decided to organize the Greatest Party (GP) that would last for several days. Next day the hobbits wrote a guest list, some non-empty set containing all the inhabitants of the Shire. To en... | The first line contains an integer *n* (3<=β€<=*n*<=β€<=10000), representing the number of hobbits. | In the first output line print a number *k* β the maximum duration of GP in days. Then on *k* lines print the guest lists, (the guests should be separated by spaces). Print each guest list on the single line. Each list can contain an arbitrary positive number of hobbits. The hobbits are numbered with integers from 1 to... | [
"4\n",
"5\n"
] | [
"3\n1 2 \n1 3 \n2 3 \n",
"3\n1 2 \n1 3 \n2 3 \n"
] | none | [
{
"input": "4",
"output": "3\n1 2 \n1 3 \n2 3 "
},
{
"input": "5",
"output": "3\n1 2 \n1 3 \n2 3 "
},
{
"input": "6",
"output": "4\n1 2 3 \n1 4 5 \n2 4 6 \n3 5 6 "
},
{
"input": "7",
"output": "4\n1 2 3 \n1 4 5 \n2 4 6 \n3 5 6 "
},
{
"input": "8",
"output": "4... | 92 | 0 | 0 | 27,590 | |
442 | Borya and Hanabi | [
"bitmasks",
"brute force",
"implementation"
] | null | null | Have you ever played Hanabi? If not, then you've got to try it out! This problem deals with a simplified version of the game.
Overall, the game has 25 types of cards (5 distinct colors and 5 distinct values). Borya is holding *n* cards. The game is somewhat complicated by the fact that everybody sees Borya's cards exc... | The first line contains integer *n* (1<=β€<=*n*<=β€<=100) β the number of Borya's cards. The next line contains the descriptions of *n* cards. The description of each card consists of exactly two characters. The first character shows the color (overall this position can contain five distinct letters β R, G, B, Y, W). The... | Print a single integer β the minimum number of hints that the other players should make. | [
"2\nG3 G3\n",
"4\nG4 R4 R3 B3\n",
"5\nB1 Y1 W1 G1 R1\n"
] | [
"0\n",
"2\n",
"4\n"
] | In the first sample Borya already knows for each card that it is a green three.
In the second sample we can show all fours and all red cards.
In the third sample you need to make hints about any four colors. | [
{
"input": "2\nG3 G3",
"output": "0"
},
{
"input": "4\nG4 R4 R3 B3",
"output": "2"
},
{
"input": "5\nB1 Y1 W1 G1 R1",
"output": "4"
},
{
"input": "10\nY4 B1 R3 G5 R5 W3 W5 W2 R1 Y1",
"output": "6"
},
{
"input": "3\nG4 G3 B4",
"output": "2"
},
{
"input"... | 62 | 0 | 0 | 27,601 | |
362 | Fools and Foolproof Roads | [
"data structures",
"dfs and similar",
"dsu",
"graphs",
"greedy"
] | null | null | You must have heard all about the Foolland on your Geography lessons. Specifically, you must know that federal structure of this country has been the same for many centuries. The country consists of *n* cities, some pairs of cities are connected by bidirectional roads, each road is described by its length *l**i*.
The ... | The first line contains four integers *n* (1<=β€<=*n*<=β€<=105), *m* (0<=β€<=*m*<=β€<=105), *p* (0<=β€<=*p*<=β€<=105), *q* (1<=β€<=*q*<=β€<=*n*) β the number of cities in the Foolland, the number of existing roads, the number of roads that are planned to construct and the required number of regions.
Next *m* lines describe th... | If constructing the roads in the required way is impossible, print a single string "NO" (without the quotes). Otherwise, in the first line print word "YES" (without the quotes), and in the next *p* lines print the road construction plan. Each line of the plan must consist of two distinct integers, giving the numbers of... | [
"9 6 2 2\n1 2 2\n3 2 1\n4 6 20\n1 3 8\n7 8 3\n5 7 2\n",
"2 0 1 2\n",
"2 0 0 2\n"
] | [
"YES\n9 5\n1 9\n",
"NO\n",
"YES\n"
] | Consider the first sample. Before the reform the Foolland consists of four regions. The first region includes cities 1, 2, 3, the second region has cities 4 and 6, the third region has cities 5, 7, 8, the fourth region has city 9. The total length of the roads in these cities is 11, 20, 5 and 0, correspondingly. Accord... | [] | 61 | 819,200 | 0 | 27,615 | |
518 | Arthur and Questions | [
"greedy",
"implementation",
"math",
"ternary search"
] | null | null | After bracket sequences Arthur took up number theory. He has got a new favorite sequence of length *n* (*a*1,<=*a*2,<=...,<=*a**n*), consisting of integers and integer *k*, not exceeding *n*.
This sequence had the following property: if you write out the sums of all its segments consisting of *k* consecutive elements ... | The first line contains two integers *n* and *k* (1<=β€<=*k*<=β€<=*n*<=β€<=105), showing how many numbers are in Arthur's sequence and the lengths of segments respectively.
The next line contains *n* space-separated elements *a**i* (1<=β€<=*i*<=β€<=*n*).
If *a**i*Β <==<=Β ?, then the *i*-th element of Arthur's sequence was ... | If Arthur is wrong at some point and there is no sequence that could fit the given information, print a single string "Incorrect sequence" (without the quotes).
Otherwise, print *n* integers β Arthur's favorite sequence. If there are multiple such sequences, print the sequence with the minimum sum |*a**i*|, where |*a*... | [
"3 2\n? 1 2\n",
"5 1\n-10 -9 ? -7 -6\n",
"5 3\n4 6 7 2 9\n"
] | [
"0 1 2 \n",
"-10 -9 -8 -7 -6 \n",
"Incorrect sequence\n"
] | none | [
{
"input": "3 2\n? 1 2",
"output": "0 1 2 "
},
{
"input": "5 1\n-10 -9 ? -7 -6",
"output": "-10 -9 -8 -7 -6 "
},
{
"input": "5 3\n4 6 7 2 9",
"output": "Incorrect sequence"
},
{
"input": "9 3\n? ? ? ? ? ? ? ? ?",
"output": "-1 -1 -1 0 0 0 1 1 1 "
},
{
"input": "5 ... | 405 | 21,401,600 | 3 | 27,616 | |
0 | none | [
"none"
] | null | null | Drazil created a following problem about putting 1<=Γ<=2 tiles into an *n*<=Γ<=*m* grid:
"There is a grid with some cells that are empty and some cells that are occupied. You should use 1<=Γ<=2 tiles to cover all empty cells and no two tiles should cover each other. And you should print a solution about how to do it."... | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=2000).
The following *n* lines describe the grid rows. Character '.' denotes an empty cell, and the character '*' denotes a cell that is occupied. | If there is no solution or the solution is not unique, you should print the string "Not unique".
Otherwise you should print how to cover all empty cells with 1<=Γ<=2 tiles. Use characters "<>" to denote horizontal tiles and characters "^v" to denote vertical tiles. Refer to the sample test for the output format ... | [
"3 3\n...\n.*.\n...\n",
"4 4\n..**\n*...\n*.**\n....\n",
"2 4\n*..*\n....\n",
"1 1\n.\n",
"1 1\n*\n"
] | [
"Not unique\n",
"<>**\n*^<>\n*v**\n<><>\n",
"*<>*\n<><>\n",
"Not unique\n",
"*\n"
] | In the first case, there are indeed two solutions:
and
so the answer is "Not unique". | [] | 0 | 0 | -1 | 27,632 | |
914 | Palindromes in a Tree | [
"bitmasks",
"data structures",
"divide and conquer",
"trees"
] | null | null | You are given a tree (a connected acyclic undirected graph) of *n* vertices. Vertices are numbered from 1 to *n* and each vertex is assigned a character from a to t.
A path in the tree is said to be palindromic if at least one permutation of the labels in the path is a palindrome.
For each vertex, output the number o... | The first line contains an integer *n* (2<=β€<=*n*<=β€<=2Β·105) Β β the number of vertices in the tree.
The next *n*<=-<=1 lines each contain two integers *u* and *v* (1<=<=β€<=<=*u*,<=*v*<=<=β€<=<=*n*,<=*u*<=β <=*v*) denoting an edge connecting vertex *u* and vertex *v*. It is guaranteed that the given graph is a tree.
The... | Print *n* integers in a single line, the *i*-th of which is the number of palindromic paths passing through vertex *i* in the tree. | [
"5\n1 2\n2 3\n3 4\n3 5\nabcbb\n",
"7\n6 2\n4 3\n3 7\n5 2\n7 2\n1 4\nafefdfs\n"
] | [
"1 3 4 3 3 \n",
"1 4 1 1 2 4 2 \n"
] | In the first sample case, the following paths are palindromic:
2β-β3β-β4
2β-β3β-β5
4β-β3β-β5
Additionally, all paths containing only one vertex are palindromic. Listed below are a few paths in the first sample that are not palindromic:
1β-β2β-β3
1β-β2β-β3β-β4
1β-β2β-β3β-β5 | [
{
"input": "5\n1 2\n2 3\n3 4\n3 5\nabcbb",
"output": "1 3 4 3 3 "
},
{
"input": "7\n6 2\n4 3\n3 7\n5 2\n7 2\n1 4\nafefdfs",
"output": "1 4 1 1 2 4 2 "
},
{
"input": "5\n3 1\n3 5\n5 4\n5 2\nticdm",
"output": "1 1 1 1 1 "
},
{
"input": "10\n10 8\n3 2\n9 7\n1 5\n5 3\n7 6\n8 4\n1... | 46 | 0 | 0 | 27,679 | |
762 | Maximum path | [
"dp",
"greedy",
"implementation"
] | null | null | You are given a rectangular table 3<=Γ<=*n*. Each cell contains an integer. You can move from one cell to another if they share a side.
Find such path from the upper left cell to the bottom right cell of the table that doesn't visit any of the cells twice, and the sum of numbers written in the cells of this path is ma... | The first line contains an integer *n* (1<=β€<=*n*<=β€<=105) Β β the number of columns in the table.
Next three lines contain *n* integers each Β β the description of the table. The *j*-th number in the *i*-th line corresponds to the cell *a**ij* (<=-<=109<=β€<=*a**ij*<=β€<=109) of the table. | Output the maximum sum of numbers on a path from the upper left cell to the bottom right cell of the table, that doesn't visit any of the cells twice. | [
"3\n1 1 1\n1 -1 1\n1 1 1\n",
"5\n10 10 10 -1 -1\n-1 10 10 10 10\n-1 10 10 10 10\n"
] | [
"7\n",
"110\n"
] | The path for the first example:
The path for the second example: | [
{
"input": "3\n1 1 1\n1 -1 1\n1 1 1",
"output": "7"
},
{
"input": "5\n10 10 10 -1 -1\n-1 10 10 10 10\n-1 10 10 10 10",
"output": "110"
},
{
"input": "15\n-87 -91 31 63 91 35 -14 51 20 20 -20 -94 -59 77 76\n11 81 22 -29 91 -26 -10 -12 46 10 100 88 14 64 41\n26 -31 99 -39 -30 30 28 74 -7 2... | 1,000 | 24,371,200 | 0 | 27,691 | |
76 | Mice | [
"greedy",
"two pointers"
] | B. Mice | 0 | 256 | Modern researches has shown that a flock of hungry mice searching for a piece of cheese acts as follows: if there are several pieces of cheese then each mouse chooses the closest one. After that all mice start moving towards the chosen piece of cheese. When a mouse or several mice achieve the destination point and ther... | The first line of the input contains four integer numbers *N* (1<=β€<=*N*<=β€<=105), *M* (0<=β€<=*M*<=β€<=105), *Y*0 (0<=β€<=*Y*0<=β€<=107), *Y*1 (0<=β€<=*Y*1<=β€<=107, *Y*0<=β <=*Y*1). The second line contains a strictly increasing sequence of *N* numbers β *x* coordinates of mice. Third line contains a strictly increasing seq... | The only line of output should contain one number β the minimal number of mice which will remain without cheese. | [
"3 2 0 2\n0 1 3\n2 5\n"
] | [
"1\n"
] | All the three mice will choose the first piece of cheese. Second and third mice will eat this piece. The first one will remain hungry, because it was running towards the same piece, but it was late. The second piece of cheese will remain uneaten. | [
{
"input": "3 2 0 2\n0 1 3\n2 5",
"output": "1"
},
{
"input": "7 11 10 20\n6 18 32 63 66 68 87\n6 8 15 23 25 41 53 59 60 75 90",
"output": "1"
},
{
"input": "13 17 14 1\n6 9 10 12 17 25 91 100 118 136 145 163 172\n0 1 2 3 4 10 12 13 16 17 19 22 26 27 28 109 154",
"output": "4"
},
... | 311 | 14,745,600 | 3 | 27,728 |
401 | Roman and Numbers | [
"bitmasks",
"brute force",
"combinatorics",
"dp",
"number theory"
] | null | null | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number *n*, modulo *m*.
Number *x* is considered close to number... | The first line contains two integers: *n* (1<=β€<=*n*<=<<=1018) and *m* (1<=β€<=*m*<=β€<=100). | In a single line print a single integer β the number of numbers close to number *n* modulo *m*. | [
"104 2\n",
"223 4\n",
"7067678 8\n"
] | [
"3\n",
"1\n",
"47\n"
] | In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | [
{
"input": "104 2",
"output": "3"
},
{
"input": "223 4",
"output": "1"
},
{
"input": "7067678 8",
"output": "47"
},
{
"input": "202 10",
"output": "1"
},
{
"input": "1306432 9",
"output": "0"
},
{
"input": "9653092 9",
"output": "0"
},
{
"i... | 61 | 11,673,600 | -1 | 27,821 | |
557 | Ann and Half-Palindrome | [
"data structures",
"dp",
"graphs",
"string suffix structures",
"strings",
"trees"
] | null | null | Tomorrow Ann takes the hardest exam of programming where she should get an excellent mark.
On the last theoretical class the teacher introduced the notion of a half-palindrome.
String *t* is a half-palindrome, if for all the odd positions *i* () the following condition is held: *t**i*<==<=*t*|*t*|<=-<=*i*<=+<=1, wh... | The first line of the input contains string *s* (1<=β€<=|*s*|<=β€<=5000), consisting only of characters 'a' and 'b', where |*s*| is the length of string *s*.
The second line contains a positive integer *k*Β βΒ the lexicographical number of the requested string among all the half-palindrome substrings of the given string ... | Print a substring of the given string that is the *k*-th in the lexicographical order of all substrings of the given string that are half-palindromes. | [
"abbabaab\n7\n",
"aaaaa\n10\n",
"bbaabb\n13\n"
] | [
"abaa\n",
"aaa\n",
"bbaabb\n"
] | By definition, string *a*β=β*a*<sub class="lower-index">1</sub>*a*<sub class="lower-index">2</sub>... *a*<sub class="lower-index">*n*</sub> is lexicographically less than string *b*β=β*b*<sub class="lower-index">1</sub>*b*<sub class="lower-index">2</sub>... *b*<sub class="lower-index">*m*</sub>, if either *a* is a pref... | [
{
"input": "abbabaab\n7",
"output": "abaa"
},
{
"input": "aaaaa\n10",
"output": "aaa"
},
{
"input": "bbaabb\n13",
"output": "bbaabb"
},
{
"input": "a\n1",
"output": "a"
},
{
"input": "b\n1",
"output": "b"
},
{
"input": "ababbbbbbbaaaaaaaabbbbbb\n23",
... | 1,044 | 158,720,000 | 3 | 27,835 | |
774 | Pens And Days Of Week | [
"*special",
"binary search",
"number theory"
] | null | null | Stepan has *n* pens. Every day he uses them, and on the *i*-th day he uses the pen number *i*. On the (*n*<=+<=1)-th day again he uses the pen number 1, on the (*n*<=+<=2)-th β he uses the pen number 2 and so on.
On every working day (from Monday to Saturday, inclusive) Stepan spends exactly 1 milliliter of ink of the... | The first line contains the integer *n* (1<=β€<=*n*<=β€<=50<=000) β the number of pens Stepan has.
The second line contains the sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109), where *a**i* is equal to the number of milliliters of ink which the pen number *i* currently has. | Print the index of the pen which will run out of ink before all (it means that there will be no ink left in it), if Stepan will use pens according to the conditions described above.
Pens are numbered in the order they are given in input data. The numeration begins from one.
Note that the answer is always unambiguou... | [
"3\n3 3 3\n",
"5\n5 4 5 4 4\n"
] | [
"2\n",
"5\n"
] | In the first test Stepan uses ink of pens as follows:
1. on the day number 1 (Monday) Stepan will use the pen number 1, after that there will be 2 milliliters of ink in it; 1. on the day number 2 (Tuesday) Stepan will use the pen number 2, after that there will be 2 milliliters of ink in it; 1. on the day number 3... | [
{
"input": "3\n3 3 3",
"output": "2"
},
{
"input": "5\n5 4 5 4 4",
"output": "5"
},
{
"input": "28\n2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033",
"output": "1"
},
{
"input": "7\n10... | 0 | 0 | -1 | 27,843 | |
196 | Lexicographically Maximum Subsequence | [
"greedy",
"strings"
] | null | null | You've got string *s*, consisting of only lowercase English letters. Find its lexicographically maximum subsequence.
We'll call a non-empty string *s*[*p*1*p*2... *p**k*]<==<=*s**p*1*s**p*2... *s**p**k*(1<=β€<=*p*1<=<<=*p*2<=<<=...<=<<=*p**k*<=β€<=|*s*|) a subsequence of string *s*<==<=*s*1*s*2... *s*|*s*|.
St... | The single line contains a non-empty string *s*, consisting only of lowercase English letters. The string's length doesn't exceed 105. | Print the lexicographically maximum subsequence of string *s*. | [
"ababba\n",
"abbcbccacbbcbaaba\n"
] | [
"bbba\n",
"cccccbba\n"
] | Let's look at samples and see what the sought subsequences look like (they are marked with uppercase bold letters).
The first sample: aBaBBA
The second sample: abbCbCCaCbbCBaaBA | [
{
"input": "ababba",
"output": "bbba"
},
{
"input": "abbcbccacbbcbaaba",
"output": "cccccbba"
},
{
"input": "thankstosamarasauteddybearsforthiscontest",
"output": "yttt"
},
{
"input": "cantouristsolveitlessthaninoneminute",
"output": "vute"
},
{
"input": "areprete... | 2,000 | 6,041,600 | 0 | 27,892 | |
196 | The Next Good String | [
"data structures",
"greedy",
"hashing",
"strings"
] | null | null | In problems on strings one often has to find a string with some particular properties. The problem authors were reluctant to waste time on thinking of a name for some string so they called it good. A string is good if it doesn't have palindrome substrings longer than or equal to *d*.
You are given string *s*, consist... | The first line contains integer *d* (1<=β€<=*d*<=β€<=|*s*|).
The second line contains a non-empty string *s*, its length is no more than 4Β·105 characters. The string consists of lowercase English letters. | Print the good string that lexicographically follows *s*, has the same length and consists of only lowercase English letters. If such string does not exist, print "Impossible" (without the quotes). | [
"3\naaaaaaa\n",
"3\nzzyzzzz\n",
"4\nabbabbbabbb\n"
] | [
"aabbcaa\n",
"Impossible\n",
"abbbcaaabab\n"
] | none | [] | 92 | 0 | 0 | 27,929 | |
916 | Jamie and To-do List | [
"data structures",
"interactive",
"trees"
] | null | null | Why I have to finish so many assignments???
Jamie is getting very busy with his school life. He starts to forget the assignments that he has to do. He decided to write the things down on a to-do list. He assigns a value priority for each of his assignment (lower value means more important) so he can decide which he ne... | The first line consists of a single integer *q* (1<=β€<=*q*<=β€<=105)Β β the number of operations.
The following *q* lines consists of the description of the operations. The *i*-th line consists of the operation that Jamie has done in the *i*-th day. The query has the following format:
The first word in the line indicat... | For each query operation, output a single integerΒ β the number of assignments that have a priority lower than assignment *a**i*, or <=-<=1 if *a**i* is not in the to-do list. | [
"8\nset chemlabreport 1\nset physicsexercise 2\nset chinesemockexam 3\nquery physicsexercise\nquery chinesemockexam\nremove physicsexercise\nquery physicsexercise\nquery chinesemockexam\n",
"8\nset physicsexercise 2\nset chinesemockexam 3\nset physicsexercise 1\nquery physicsexercise\nquery chinesemockexam\nundo ... | [
"1\n2\n-1\n1\n",
"0\n1\n0\n-1\n",
"-1\n-1\n-1\n",
"-1\n"
] | none | [] | 2,000 | 207,257,600 | 0 | 27,953 | |
712 | Memory and Scores | [
"combinatorics",
"dp",
"math"
] | null | null | Memory and his friend Lexa are competing to get higher score in one popular computer game. Memory starts with score *a* and Lexa starts with score *b*. In a single turn, both Memory and Lexa get some integer in the range [<=-<=*k*;*k*] (i.e. one integer among <=-<=*k*,<=<=-<=*k*<=+<=1,<=<=-<=*k*<=+<=2,<=...,<=<=-<=2,<=... | The first and only line of input contains the four integers *a*, *b*, *k*, and *t* (1<=β€<=*a*,<=*b*<=β€<=100, 1<=β€<=*k*<=β€<=1000, 1<=β€<=*t*<=β€<=100)Β β the amount Memory and Lexa start with, the number *k*, and the number of turns respectively. | Print the number of possible games satisfying the conditions modulo 1<=000<=000<=007 (109<=+<=7) in one line. | [
"1 2 2 1\n",
"1 1 1 2\n",
"2 12 3 1\n"
] | [
"6\n",
"31\n",
"0\n"
] | In the first sample test, Memory starts with 1 and Lexa starts with 2. If Lexa picks β-β2, Memory can pick 0, 1, or 2 to win. If Lexa picks β-β1, Memory can pick 1 or 2 to win. If Lexa picks 0, Memory can pick 2 to win. If Lexa picks 1 or 2, Memory cannot win. Thus, there are 3β+β2β+β1β=β6 possible games in which Memor... | [
{
"input": "1 2 2 1",
"output": "6"
},
{
"input": "1 1 1 2",
"output": "31"
},
{
"input": "2 12 3 1",
"output": "0"
},
{
"input": "4 6 2 1",
"output": "3"
},
{
"input": "4 6 2 2",
"output": "122"
},
{
"input": "6 4 2 2",
"output": "435"
},
{
... | 2,000 | 9,420,800 | 0 | 28,005 | |
205 | Little Elephant and Sorting | [
"brute force",
"greedy"
] | null | null | The Little Elephant loves sortings.
He has an array *a* consisting of *n* integers. Let's number the array elements from 1 to *n*, then the *i*-th element will be denoted as *a**i*. The Little Elephant can make one move to choose an arbitrary pair of integers *l* and *r* (1<=β€<=*l*<=β€<=*r*<=β€<=*n*) and increase *a**i*... | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=105) β the size of array *a*. The next line contains *n* integers, separated by single spaces β array *a* (1<=β€<=*a**i*<=β€<=109). The array elements are listed in the line in the order of their index's increasing. | In a single line print a single integer β the answer to the problem.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"3\n1 2 3\n",
"3\n3 2 1\n",
"4\n7 4 1 47\n"
] | [
"0\n",
"2\n",
"6\n"
] | In the first sample the array is already sorted in the non-decreasing order, so the answer is 0.
In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]).
In ... | [
{
"input": "3\n1 2 3",
"output": "0"
},
{
"input": "3\n3 2 1",
"output": "2"
},
{
"input": "4\n7 4 1 47",
"output": "6"
},
{
"input": "10\n1 2 3 4 5 6 7 8 9 1000000000",
"output": "0"
},
{
"input": "10\n1000000000 1 1000000000 1 1000000000 1 1000000000 1 100000000... | 170 | 9,113,600 | 3 | 28,045 | |
401 | Sereja and Contests | [
"greedy",
"implementation",
"math"
] | null | null | Sereja is a coder and he likes to take part in Codesorfes rounds. However, Uzhland doesn't have good internet connection, so Sereja sometimes skips rounds.
Codesorfes has rounds of two types: *Div*1 (for advanced coders) and *Div*2 (for beginner coders). Two rounds, *Div*1 and *Div*2, can go simultaneously, (*Div*1 ro... | The first line contains two integers: *x* (1<=β€<=*x*<=β€<=4000) β the round Sereja is taking part in today, and *k* (0<=β€<=*k*<=<<=4000) β the number of rounds he took part in.
Next *k* lines contain the descriptions of the rounds that Sereja took part in before. If Sereja took part in one of two simultaneous rounds... | Print in a single line two integers β the minimum and the maximum number of rounds that Sereja could have missed. | [
"3 2\n2 1\n2 2\n",
"9 3\n1 2 3\n2 8\n1 4 5\n",
"10 0\n"
] | [
"0 0",
"2 3",
"5 9"
] | In the second sample we have unused identifiers of rounds 1, 6, 7. The minimum number of rounds Sereja could have missed equals to 2. In this case, the round with the identifier 1 will be a usual *Div*2 round and the round with identifier 6 will be synchronous with the *Div*1 round.
The maximum number of rounds equal... | [
{
"input": "3 2\n2 1\n2 2",
"output": "0 0"
},
{
"input": "9 3\n1 2 3\n2 8\n1 4 5",
"output": "2 3"
},
{
"input": "10 0",
"output": "5 9"
},
{
"input": "10 2\n1 1 2\n1 8 9",
"output": "3 5"
},
{
"input": "9 3\n1 4 5\n1 1 2\n1 6 7",
"output": "2 2"
},
{
... | 499 | 5,427,200 | 3 | 28,068 | |
988 | Equal Sums | [
"implementation",
"sortings"
] | null | null | You are given $k$ sequences of integers. The length of the $i$-th sequence equals to $n_i$.
You have to choose exactly two sequences $i$ and $j$ ($i \ne j$) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence $i$ (its length will be equal to $n_i - 1$) equals... | The first line contains an integer $k$ ($2 \le k \le 2 \cdot 10^5$) β the number of sequences.
Then $k$ pairs of lines follow, each pair containing a sequence.
The first line in the $i$-th pair contains one integer $n_i$ ($1 \le n_i < 2 \cdot 10^5$) β the length of the $i$-th sequence. The second line of the $i$-t... | If it is impossible to choose two sequences such that they satisfy given conditions, print "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), in the second line β two integers $i$, $x$ ($1 \le i \le k, 1 \le x \le n_i$), in the third line β two integers $j$, $y$ ($1 \le j \le k, 1 \le y \l... | [
"2\n5\n2 3 1 3 2\n6\n1 1 2 2 2 1\n",
"3\n1\n5\n5\n1 1 1 1 1\n2\n2 3\n",
"4\n6\n2 2 2 2 2 2\n5\n2 2 2 2 2\n3\n2 2 2\n5\n2 2 2 2 2\n"
] | [
"YES\n2 6\n1 2\n",
"NO\n",
"YES\n2 2\n4 1\n"
] | In the first example there are two sequences $[2, 3, 1, 3, 2]$ and $[1, 1, 2, 2, 2, 1]$. You can remove the second element from the first sequence to get $[2, 1, 3, 2]$ and you can remove the sixth element from the second sequence to get $[1, 1, 2, 2, 2]$. The sums of the both resulting sequences equal to $8$, i.e. the... | [
{
"input": "2\n5\n2 3 1 3 2\n6\n1 1 2 2 2 1",
"output": "YES\n2 1\n1 4"
},
{
"input": "3\n1\n5\n5\n1 1 1 1 1\n2\n2 3",
"output": "NO"
},
{
"input": "4\n6\n2 2 2 2 2 2\n5\n2 2 2 2 2\n3\n2 2 2\n5\n2 2 2 2 2",
"output": "YES\n4 1\n2 5"
},
{
"input": "2\n2\n0 -10000\n2\n10000 0",... | 31 | 0 | 0 | 28,094 | |
670 | Correct Bracket Sequence Editor | [
"data structures",
"dsu",
"strings"
] | null | null | Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS).
Note that a bracket sequence is correct if it is possible to get a correct mathematical expression by adding "+"-s and "1"-s to it. For example, sequences "(())()", "()" and "(()(()))" are correct, ... | The first line contains three positive integers *n*, *m* and *p* (2<=β€<=*n*<=β€<=500<=000, 1<=β€<=*m*<=β€<=500<=000, 1<=β€<=*p*<=β€<=*n*)Β β the number of brackets in the correct bracket sequence, the number of operations and the initial position of cursor. Positions in the sequence are numbered from left to right, starting ... | Print the correct bracket sequence, obtained as a result of applying all operations to the initial sequence. | [
"8 4 5\n(())()()\nRDLD\n",
"12 5 3\n((()())(()))\nRRDLD\n",
"8 8 8\n(())()()\nLLLLLLDD\n"
] | [
"()\n",
"(()(()))\n",
"()()\n"
] | In the first sample the cursor is initially at position 5. Consider actions of the editor:
1. command "R"Β β the cursor moves to the position 6 on the right; 1. command "D"Β β the deletion of brackets from the position 5 to the position 6. After that CBS takes the form (())(), the cursor is at the position 5; 1. comm... | [
{
"input": "8 4 5\n(())()()\nRDLD",
"output": "()"
},
{
"input": "12 5 3\n((()())(()))\nRRDLD",
"output": "(()(()))"
},
{
"input": "8 8 8\n(())()()\nLLLLLLDD",
"output": "()()"
},
{
"input": "4 2 2\n()()\nLD",
"output": "()"
},
{
"input": "6 4 1\n()()()\nDRRD",
... | 779 | 36,044,800 | -1 | 28,098 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.