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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | none | [
"none"
] | null | null | Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0,<=1]. Next, *n* stones will fall and Liss will escape from the stones. The stones are numbered from 1 to *n* in order.
The stones always fall to the center of Liss's interval. When Liss occupies the interval [*k*<=-<=*d*,<=*k*<=+<=*d*] and a stone falls to *k*, she will escape to the left or to the right. If she escapes to the left, her new interval will be [*k*<=-<=*d*,<=*k*]. If she escapes to the right, her new interval will be [*k*,<=*k*<=+<=*d*].
You are given a string *s* of length *n*. If the *i*-th character of *s* is "l" or "r", when the *i*-th stone falls Liss will escape to the left or to the right, respectively. Find the sequence of stones' numbers from left to right after all the *n* stones falls. | The input consists of only one line. The only line contains the string *s* (1<=β€<=|*s*|<=β€<=106). Each character in *s* will be either "l" or "r". | Output *n* lines β on the *i*-th line you should print the *i*-th stone's number from the left. | [
"llrlr\n",
"rrlll\n",
"lrlrr\n"
] | [
"3\n5\n4\n2\n1\n",
"1\n2\n5\n4\n3\n",
"2\n4\n5\n3\n1\n"
] | In the first example, the positions of stones 1, 2, 3, 4, 5 will be <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/58fdb5684df807bfcb705a9da9ce175613362b7d.png" style="max-width: 100.0%;max-height: 100.0%;"/>, respectively. So you should print the sequence: 3, 5, 4, 2, 1. | [
{
"input": "llrlr",
"output": "3\n5\n4\n2\n1"
},
{
"input": "rrlll",
"output": "1\n2\n5\n4\n3"
},
{
"input": "lrlrr",
"output": "2\n4\n5\n3\n1"
},
{
"input": "lllrlrllrl",
"output": "4\n6\n9\n10\n8\n7\n5\n3\n2\n1"
},
{
"input": "llrlrrrlrr",
"output": "3\n5\n6... | 1,107 | 29,388,800 | 3 | 2,944 | |
375 | Maximum Submatrix 2 | [
"data structures",
"dp",
"implementation",
"sortings"
] | null | null | You are given a matrix consisting of digits zero and one, its size is *n*<=Γ<=*m*. You are allowed to rearrange its rows. What is the maximum area of the submatrix that only consists of ones and can be obtained in the given problem by the described operations?
Let's assume that the rows of matrix *a* are numbered from 1 to *n* from top to bottom and the columns are numbered from 1 to *m* from left to right. A matrix cell on the intersection of the *i*-th row and the *j*-th column can be represented as (*i*,<=*j*). Formally, a submatrix of matrix *a* is a group of four integers *d*,<=*u*,<=*l*,<=*r* (1<=β€<=*d*<=β€<=*u*<=β€<=*n*;Β 1<=β€<=*l*<=β€<=*r*<=β€<=*m*). We will assume that the submatrix contains cells (*i*,<=*j*) (*d*<=β€<=*i*<=β€<=*u*;Β *l*<=β€<=*j*<=β€<=*r*). The area of the submatrix is the number of cells it contains. | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=5000). Next *n* lines contain *m* characters each β matrix *a*. Matrix *a* only contains characters: "0" and "1". Note that the elements of the matrix follow without any spaces in the lines. | Print a single integer β the area of the maximum obtained submatrix. If we cannot obtain a matrix of numbers one, print 0. | [
"1 1\n1\n",
"2 2\n10\n11\n",
"4 3\n100\n011\n000\n101\n"
] | [
"1\n",
"2\n",
"2\n"
] | none | [
{
"input": "1 1\n1",
"output": "1"
},
{
"input": "2 2\n10\n11",
"output": "2"
},
{
"input": "4 3\n100\n011\n000\n101",
"output": "2"
},
{
"input": "11 16\n0111110101100011\n1000101100010000\n0010110110010101\n0110110010110010\n0011101101110000\n1001100011010111\n0010011111111... | 2,000 | 268,185,600 | 0 | 2,948 | |
651 | Joysticks | [
"dp",
"greedy",
"implementation",
"math"
] | null | null | Friends are going to play console. They have two joysticks and only one charger for them. Initially first joystick is charged at *a*1 percent and second one is charged at *a*2 percent. You can connect charger to a joystick only at the beginning of each minute. In one minute joystick either discharges by 2 percent (if not connected to a charger) or charges by 1 percent (if connected to a charger).
Game continues while both joysticks have a positive charge. Hence, if at the beginning of minute some joystick is charged by 1 percent, it has to be connected to a charger, otherwise the game stops. If some joystick completely discharges (its charge turns to 0), the game also stops.
Determine the maximum number of minutes that game can last. It is prohibited to pause the game, i. e. at each moment both joysticks should be enabled. It is allowed for joystick to be charged by more than 100 percent. | The first line of the input contains two positive integers *a*1 and *a*2 (1<=β€<=*a*1,<=*a*2<=β€<=100), the initial charge level of first and second joystick respectively. | Output the only integer, the maximum number of minutes that the game can last. Game continues until some joystick is discharged. | [
"3 5\n",
"4 4\n"
] | [
"6\n",
"5\n"
] | In the first sample game lasts for 6 minute by using the following algorithm:
- at the beginning of the first minute connect first joystick to the charger, by the end of this minute first joystick is at 4%, second is at 3%; - continue the game without changing charger, by the end of the second minute the first joystick is at 5%, second is at 1%; - at the beginning of the third minute connect second joystick to the charger, after this minute the first joystick is at 3%, the second one is at 2%; - continue the game without changing charger, by the end of the fourth minute first joystick is at 1%, second one is at 3%; - at the beginning of the fifth minute connect first joystick to the charger, after this minute the first joystick is at 2%, the second one is at 1%; - at the beginning of the sixth minute connect second joystick to the charger, after this minute the first joystick is at 0%, the second one is at 2%.
After that the first joystick is completely discharged and the game is stopped. | [
{
"input": "3 5",
"output": "6"
},
{
"input": "4 4",
"output": "5"
},
{
"input": "100 100",
"output": "197"
},
{
"input": "1 100",
"output": "98"
},
{
"input": "100 1",
"output": "98"
},
{
"input": "1 4",
"output": "2"
},
{
"input": "1 1",
... | 46 | 0 | 0 | 2,965 | |
592 | The Monster and the Squirrel | [
"math"
] | null | null | Ari the monster always wakes up very early with the first ray of the sun and the first thing she does is feeding her squirrel.
Ari draws a regular convex polygon on the floor and numbers it's vertices 1,<=2,<=...,<=*n* in clockwise order. Then starting from the vertex 1 she draws a ray in the direction of each other vertex. The ray stops when it reaches a vertex or intersects with another ray drawn before. Ari repeats this process for vertex 2,<=3,<=...,<=*n* (in this particular order). And then she puts a walnut in each region inside the polygon.
Ada the squirrel wants to collect all the walnuts, but she is not allowed to step on the lines drawn by Ari. That means Ada have to perform a small jump if she wants to go from one region to another. Ada can jump from one region P to another region Q if and only if P and Q share a side or a corner.
Assuming that Ada starts from outside of the picture, what is the minimum number of jumps she has to perform in order to collect all the walnuts? | The first and only line of the input contains a single integer *n* (3<=β€<=*n*<=β€<=54321) - the number of vertices of the regular polygon drawn by Ari. | Print the minimum number of jumps Ada should make to collect all the walnuts. Note, that she doesn't need to leave the polygon after. | [
"5\n",
"3\n"
] | [
"9\n",
"1\n"
] | One of the possible solutions for the first sample is shown on the picture above. | [
{
"input": "5",
"output": "9"
},
{
"input": "3",
"output": "1"
},
{
"input": "54321",
"output": "2950553761"
},
{
"input": "4",
"output": "4"
},
{
"input": "6",
"output": "16"
},
{
"input": "7",
"output": "25"
},
{
"input": "8",
"output... | 61 | 0 | 3 | 2,972 | |
217 | Ice Skating | [
"brute force",
"dfs and similar",
"dsu",
"graphs"
] | null | null | Bajtek is learning to skate on ice. He's a beginner, so his only mode of transportation is pushing off from a snow drift to the north, east, south or west and sliding until he lands in another snow drift. He has noticed that in this way it's impossible to get from some snow drifts to some other by any sequence of moves. He now wants to heap up some additional snow drifts, so that he can get from any snow drift to any other one. He asked you to find the minimal number of snow drifts that need to be created.
We assume that Bajtek can only heap up snow drifts at integer coordinates. | The first line of input contains a single integer *n* (1<=β€<=*n*<=β€<=100) β the number of snow drifts. Each of the following *n* lines contains two integers *x**i* and *y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=1000) β the coordinates of the *i*-th snow drift.
Note that the north direction coinΡides with the direction of *Oy* axis, so the east direction coinΡides with the direction of the *Ox* axis. All snow drift's locations are distinct. | Output the minimal number of snow drifts that need to be created in order for Bajtek to be able to reach any snow drift from any other one. | [
"2\n2 1\n1 2\n",
"2\n2 1\n4 1\n"
] | [
"1\n",
"0\n"
] | none | [
{
"input": "2\n2 1\n1 2",
"output": "1"
},
{
"input": "2\n2 1\n4 1",
"output": "0"
},
{
"input": "24\n171 35\n261 20\n4 206\n501 446\n961 912\n581 748\n946 978\n463 514\n841 889\n341 466\n842 967\n54 102\n235 261\n925 889\n682 672\n623 636\n268 94\n635 710\n474 510\n697 794\n586 663\n182... | 248 | 0 | 0 | 2,976 | |
0 | none | [
"none"
] | null | null | You are given an array *a* with *n* distinct integers. Construct an array *b* by permuting *a* such that for every non-empty subset of indices *S*<==<={*x*1,<=*x*2,<=...,<=*x**k*} (1<=β€<=*x**i*<=β€<=*n*, 0<=<<=*k*<=<<=*n*) the sums of elements on that positions in *a* and *b* are different, i.Β e. | The first line contains one integer *n* (1<=β€<=*n*<=β€<=22)Β β the size of the array.
The second line contains *n* space-separated distinct integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=109)Β β the elements of the array. | If there is no such array *b*, print -1.
Otherwise in the only line print *n* space-separated integers *b*1,<=*b*2,<=...,<=*b**n*. Note that *b* must be a permutation of *a*.
If there are multiple answers, print any of them. | [
"2\n1 2\n",
"4\n1000 100 10 1\n"
] | [
"2 1 \n",
"100 1 1000 10\n"
] | An array *x* is a permutation of *y*, if we can shuffle elements of *y* such that it will coincide with *x*.
Note that the empty subset and the subset containing all indices are not counted. | [
{
"input": "2\n1 2",
"output": "2 1 "
},
{
"input": "4\n1000 100 10 1",
"output": "100 1 1000 10"
},
{
"input": "5\n1 3 4 5 2",
"output": "5 2 3 4 1 "
},
{
"input": "1\n10000000",
"output": "10000000 "
},
{
"input": "4\n1 5 8 4",
"output": "8 4 5 1 "
},
{
... | 62 | 0 | -1 | 2,978 | |
12 | Correct Solution? | [
"implementation",
"sortings"
] | B. Correct Solution? | 2 | 256 | One cold winter evening Alice and her older brother Bob was sitting at home near the fireplace and giving each other interesting problems to solve. When it was Alice's turn, she told the number *n* to Bob and said:
βShuffle the digits in this number in order to obtain the smallest possible number without leading zeroes.
βNo problem! β said Bob and immediately gave her an answer.
Alice said a random number, so she doesn't know whether Bob's answer is correct. Help her to find this out, because impatient brother is waiting for the verdict. | The first line contains one integer *n* (0<=β€<=*n*<=β€<=109) without leading zeroes. The second lines contains one integer *m* (0<=β€<=*m*<=β€<=109) β Bob's answer, possibly with leading zeroes. | Print OK if Bob's answer is correct and WRONG_ANSWER otherwise. | [
"3310\n1033\n",
"4\n5\n"
] | [
"OK\n",
"WRONG_ANSWER\n"
] | none | [
{
"input": "3310\n1033",
"output": "OK"
},
{
"input": "4\n5",
"output": "WRONG_ANSWER"
},
{
"input": "40\n04",
"output": "WRONG_ANSWER"
},
{
"input": "12\n12",
"output": "OK"
},
{
"input": "432\n234",
"output": "OK"
},
{
"input": "17109\n01179",
"o... | 0 | 0 | -1 | 2,979 |
7 | Palindrome Degree | [
"hashing",
"strings"
] | D. Palindrome Degree | 1 | 256 | String *s* of length *n* is called *k*-palindrome, if it is a palindrome itself, and its prefix and suffix of length are (*k*<=-<=1)-palindromes. By definition, any string (even empty) is 0-palindrome.
Let's call the palindrome degree of string *s* such a maximum number *k*, for which *s* is *k*-palindrome. For example, "abaaba" has degree equals to 3.
You are given a string. Your task is to find the sum of the palindrome degrees of all its prefixes. | The first line of the input data contains a non-empty string, consisting of Latin letters and digits. The length of the string does not exceed 5Β·106. The string is case-sensitive. | Output the only number β the sum of the polindrome degrees of all the string's prefixes. | [
"a2A\n",
"abacaba\n"
] | [
"1",
"6"
] | none | [
{
"input": "a2A",
"output": "1"
},
{
"input": "abacaba",
"output": "6"
},
{
"input": "CCeCeCCCee",
"output": "4"
},
{
"input": "opooppppopppopoppopoooppopopooopopppooopppoppoppoppppoooppooooooopppoopoopooooppooooppppppppooopooop",
"output": "3"
},
{
"input": "odri... | 92 | 4,608,000 | 0 | 2,984 |
976 | Well played! | [
"greedy",
"sortings"
] | null | null | Recently Max has got himself into popular CCG "BrainStone". As "BrainStone" is a pretty intellectual game, Max has to solve numerous hard problems during the gameplay. Here is one of them:
Max owns *n* creatures, *i*-th of them can be described with two numbers β its health *hp**i* and its damage *dmg**i*. Max also has two types of spells in stock:
1. Doubles health of the creature (*hp**i* := *hp**i*Β·2); 1. Assigns value of health of the creature to its damage (*dmg**i* := *hp**i*).
Spell of first type can be used no more than *a* times in total, of the second type β no more than *b* times in total. Spell can be used on a certain creature multiple times. Spells can be used in arbitrary order. It isn't necessary to use all the spells.
Max is really busy preparing for his final exams, so he asks you to determine what is the maximal total damage of all creatures he can achieve if he uses spells in most optimal way. | The first line contains three integers *n*, *a*, *b* (1<=β€<=*n*<=β€<=2Β·105, 0<=β€<=*a*<=β€<=20, 0<=β€<=*b*<=β€<=2Β·105) β the number of creatures, spells of the first type and spells of the second type, respectively.
The *i*-th of the next *n* lines contain two number *hp**i* and *dmg**i* (1<=β€<=*hp**i*,<=*dmg**i*<=β€<=109) β description of the *i*-th creature. | Print single integer β maximum total damage creatures can deal. | [
"2 1 1\n10 15\n6 1\n",
"3 0 3\n10 8\n7 11\n5 2\n"
] | [
"27\n",
"26\n"
] | In the first example Max should use the spell of the first type on the second creature, then the spell of the second type on the same creature. Then total damage will be equal to 15β+β6Β·2β=β27.
In the second example Max should use the spell of the second type on the first creature, then the spell of the second type on the third creature. Total damage will be equal to 10β+β11β+β5β=β26. | [
{
"input": "2 1 1\n10 15\n6 1",
"output": "27"
},
{
"input": "3 0 3\n10 8\n7 11\n5 2",
"output": "26"
},
{
"input": "1 0 0\n2 1",
"output": "1"
},
{
"input": "1 0 200000\n1 2",
"output": "2"
},
{
"input": "7 5 7\n29 25\n84 28\n34 34\n14 76\n85 9\n40 57\n99 88",
... | 93 | 7,065,600 | -1 | 2,993 | |
814 | An express train to reveries | [
"constructive algorithms"
] | null | null | Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.
On that night, Sengoku constructed a permutation *p*1,<=*p*2,<=...,<=*p**n* of integers from 1 to *n* inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with *n* meteorids, colours of which being integer sequences *a*1,<=*a*2,<=...,<=*a**n* and *b*1,<=*b*2,<=...,<=*b**n* respectively. Meteoroids' colours were also between 1 and *n* inclusive, and the two sequences were not identical, that is, at least one *i* (1<=β€<=*i*<=β€<=*n*) exists, such that *a**i*<=β <=*b**i* holds.
Well, she almost had it all β each of the sequences *a* and *b* matched exactly *n*<=-<=1 elements in Sengoku's permutation. In other words, there is exactly one *i* (1<=β€<=*i*<=β€<=*n*) such that *a**i*<=β <=*p**i*, and exactly one *j* (1<=β€<=*j*<=β€<=*n*) such that *b**j*<=β <=*p**j*.
For now, Sengoku is able to recover the actual colour sequences *a* and *b* through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night. | The first line of input contains a positive integer *n* (2<=β€<=*n*<=β€<=1<=000) β the length of Sengoku's permutation, being the length of both meteor outbursts at the same time.
The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=*n*) β the sequence of colours in the first meteor outburst.
The third line contains *n* space-separated integers *b*1,<=*b*2,<=...,<=*b**n* (1<=β€<=*b**i*<=β€<=*n*) β the sequence of colours in the second meteor outburst. At least one *i* (1<=β€<=*i*<=β€<=*n*) exists, such that *a**i*<=β <=*b**i* holds. | Output *n* space-separated integers *p*1,<=*p*2,<=...,<=*p**n*, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them.
Input guarantees that such permutation exists. | [
"5\n1 2 3 4 3\n1 2 5 4 5\n",
"5\n4 4 2 3 1\n5 4 5 3 1\n",
"4\n1 1 3 4\n1 4 3 4\n"
] | [
"1 2 5 4 3\n",
"5 4 2 3 1\n",
"1 2 3 4\n"
] | In the first sample, both 1,β2,β5,β4,β3 and 1,β2,β3,β4,β5 are acceptable outputs.
In the second sample, 5,β4,β2,β3,β1 is the only permutation to satisfy the constraints. | [
{
"input": "5\n1 2 3 4 3\n1 2 5 4 5",
"output": "1 2 5 4 3"
},
{
"input": "5\n4 4 2 3 1\n5 4 5 3 1",
"output": "5 4 2 3 1"
},
{
"input": "4\n1 1 3 4\n1 4 3 4",
"output": "1 2 3 4"
},
{
"input": "10\n1 2 3 4 7 6 7 8 9 10\n1 2 3 4 5 6 5 8 9 10",
"output": "1 2 3 4 5 6 7 8 9... | 62 | 0 | 0 | 3,008 | |
816 | Karen and Coffee | [
"binary search",
"data structures",
"implementation"
] | null | null | To stay woke and attentive during classes, Karen needs some coffee!
Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe".
She knows *n* coffee recipes. The *i*-th recipe suggests that coffee should be brewed between *l**i* and *r**i* degrees, inclusive, to achieve the optimal taste.
Karen thinks that a temperature is admissible if at least *k* recipes recommend it.
Karen has a rather fickle mind, and so she asks *q* questions. In each question, given that she only wants to prepare coffee with a temperature between *a* and *b*, inclusive, can you tell her how many admissible integer temperatures fall within the range? | The first line of input contains three integers, *n*, *k* (1<=β€<=*k*<=β€<=*n*<=β€<=200000), and *q* (1<=β€<=*q*<=β€<=200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively.
The next *n* lines describe the recipes. Specifically, the *i*-th line among these contains two integers *l**i* and *r**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=200000), describing that the *i*-th recipe suggests that the coffee be brewed between *l**i* and *r**i* degrees, inclusive.
The next *q* lines describe the questions. Each of these lines contains *a* and *b*, (1<=β€<=*a*<=β€<=*b*<=β€<=200000), describing that she wants to know the number of admissible integer temperatures between *a* and *b* degrees, inclusive. | For each question, output a single integer on a line by itself, the number of admissible integer temperatures between *a* and *b* degrees, inclusive. | [
"3 2 4\n91 94\n92 97\n97 99\n92 94\n93 97\n95 96\n90 100\n",
"2 1 1\n1 1\n200000 200000\n90 100\n"
] | [
"3\n3\n0\n4\n",
"0\n"
] | In the first test case, Karen knows 3 recipes.
1. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive. 1. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive. 1. The third one recommends brewing the coffee between 97 and 99 degrees, inclusive.
A temperature is admissible if at least 2 recipes recommend it.
She asks 4 questions.
In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible.
In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible.
In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none.
In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible.
In the second test case, Karen knows 2 recipes.
1. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree. 1. The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees.
A temperature is admissible if at least 1 recipe recommends it.
In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none. | [
{
"input": "3 2 4\n91 94\n92 97\n97 99\n92 94\n93 97\n95 96\n90 100",
"output": "3\n3\n0\n4"
},
{
"input": "2 1 1\n1 1\n200000 200000\n90 100",
"output": "0"
},
{
"input": "1 1 1\n1 1\n1 1",
"output": "1"
},
{
"input": "1 1 1\n200000 200000\n200000 200000",
"output": "1"
... | 2,500 | 149,401,600 | 0 | 3,012 | |
357 | Group of Students | [
"brute force",
"greedy",
"implementation"
] | null | null | At the beginning of the school year Berland State University starts two city school programming groups, for beginners and for intermediate coders. The children were tested in order to sort them into groups. According to the results, each student got some score from 1 to *m* points. We know that *c*1 schoolchildren got 1 point, *c*2 children got 2 points, ..., *c**m* children got *m* points. Now you need to set the passing rate *k* (integer from 1 to *m*): all schoolchildren who got less than *k* points go to the beginner group and those who get at strictly least *k* points go to the intermediate group. We know that if the size of a group is more than *y*, then the university won't find a room for them. We also know that if a group has less than *x* schoolchildren, then it is too small and there's no point in having classes with it. So, you need to split all schoolchildren into two groups so that the size of each group was from *x* to *y*, inclusive.
Help the university pick the passing rate in a way that meets these requirements. | The first line contains integer *m* (2<=β€<=*m*<=β€<=100). The second line contains *m* integers *c*1, *c*2, ..., *c**m*, separated by single spaces (0<=β€<=*c**i*<=β€<=100). The third line contains two space-separated integers *x* and *y* (1<=β€<=*x*<=β€<=*y*<=β€<=10000). At least one *c**i* is greater than 0. | If it is impossible to pick a passing rate in a way that makes the size of each resulting groups at least *x* and at most *y*, print 0. Otherwise, print an integer from 1 to *m* β the passing rate you'd like to suggest. If there are multiple possible answers, print any of them. | [
"5\n3 4 3 2 1\n6 8\n",
"5\n0 3 3 4 2\n3 10\n",
"2\n2 5\n3 6\n"
] | [
"3\n",
"4\n",
"0\n"
] | In the first sample the beginner group has 7 students, the intermediate group has 6 of them.
In the second sample another correct answer is 3. | [
{
"input": "5\n3 4 3 2 1\n6 8",
"output": "3"
},
{
"input": "5\n0 3 3 4 2\n3 10",
"output": "4"
},
{
"input": "2\n2 5\n3 6",
"output": "0"
},
{
"input": "3\n0 1 0\n2 10",
"output": "0"
},
{
"input": "5\n2 2 2 2 2\n5 5",
"output": "0"
},
{
"input": "10\... | 109 | 307,200 | 3 | 3,019 | |
453 | Little Pony and Expected Maximum | [
"probabilities"
] | null | null | Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she kept losing. Having returned to the castle, Twilight Sparkle became interested in the dice that were used in the game.
The dice has *m* faces: the first face of the dice contains a dot, the second one contains two dots, and so on, the *m*-th face contains *m* dots. Twilight Sparkle is sure that when the dice is tossed, each face appears with probability . Also she knows that each toss is independent from others. Help her to calculate the expected maximum number of dots she could get after tossing the dice *n* times. | A single line contains two integers *m* and *n* (1<=β€<=*m*,<=*n*<=β€<=105). | Output a single real number corresponding to the expected maximum. The answer will be considered correct if its relative or absolute error doesn't exceed 10<=<=-<=4. | [
"6 1\n",
"6 3\n",
"2 2\n"
] | [
"3.500000000000\n",
"4.958333333333\n",
"1.750000000000\n"
] | Consider the third test example. If you've made two tosses:
1. You can get 1 in the first toss, and 2 in the second. Maximum equals to 2. 1. You can get 1 in the first toss, and 1 in the second. Maximum equals to 1. 1. You can get 2 in the first toss, and 1 in the second. Maximum equals to 2. 1. You can get 2 in the first toss, and 2 in the second. Maximum equals to 2.
The probability of each outcome is 0.25, that is expectation equals to:
You can read about expectation using the following link: http://en.wikipedia.org/wiki/Expected_value | [
{
"input": "6 1",
"output": "3.500000000000"
},
{
"input": "6 3",
"output": "4.958333333333"
},
{
"input": "2 2",
"output": "1.750000000000"
},
{
"input": "5 4",
"output": "4.433600000000"
},
{
"input": "5 8",
"output": "4.814773760000"
},
{
"input": "... | 187 | 0 | 3 | 3,023 | |
293 | Cube Problem | [
"brute force",
"math",
"number theory"
] | null | null | Yaroslav, Andrey and Roman love playing cubes. Sometimes they get together and play cubes for hours and hours!
Today they got together again and they are playing cubes. Yaroslav took unit cubes and composed them into an *a*<=Γ<=*a*<=Γ<=*a* cube, Andrey made a *b*<=Γ<=*b*<=Γ<=*b* cube and Roman made a *c*<=Γ<=*c*<=Γ<=*c* cube. After that the game was finished and the guys left. But later, Vitaly entered the room. He saw the cubes and wanted to make a cube as well. But what size should the cube be? Of course it should be a large cube with the side of length *a*<=+<=*b*<=+<=*c*. Besides, Vitaly decided to decompose the cubes built by Yaroslav, Andrey and Roman and compose his own large cube out of them. However, it turned out that the unit cubes he got from destroying the three cubes just weren't enough to make a large cube. We know that Vitaly was short of exactly *n* cubes. Vitaly got upset, demolished everything and left. As he was leaving, he met Petya and told him that there had been three cubes in the room and that he needed another *n* unit cubes to make his own large cube.
Petya entered the room and saw the messily scattered cubes. He wanted to make it neat and orderly again. But he only knows that there had been three cubes, made of small unit cubes and that Vitaly needed *n* more unit cubes to make a large one! Help Petya understand, how many ways of sizes *a*, *b*, *c* are there to restore Yaroslav's, Andrey's and Roman's cubes. | The single line of the input contains integer *n* (1<=β€<=*n*<=β€<=1014). We know that all numbers *a*, *b*, *c* are positive integers.
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. | In the single line print the required number of ways. If it turns out that there isn't a single way of suitable sizes of *a*, *b*, *c*, print 0. | [
"24\n",
"648\n",
"5\n",
"93163582512000\n"
] | [
"1\n",
"7\n",
"0\n",
"39090\n"
] | none | [
{
"input": "24",
"output": "1"
},
{
"input": "648",
"output": "7"
},
{
"input": "5",
"output": "0"
},
{
"input": "93163582512000",
"output": "39090"
},
{
"input": "1260",
"output": "0"
},
{
"input": "1680",
"output": "0"
},
{
"input": "2520... | 0 | 0 | -1 | 3,034 | |
388 | Fox and Minimal path | [
"bitmasks",
"constructive algorithms",
"graphs",
"implementation",
"math"
] | null | null | Fox Ciel wants to write a task for a programming contest. The task is: "You are given a simple undirected graph with *n* vertexes. Each its edge has unit length. You should calculate the number of shortest paths between vertex 1 and vertex 2."
Same with some writers, she wants to make an example with some certain output: for example, her birthday or the number of her boyfriend. Can you help her to make a test case with answer equal exactly to *k*? | The first line contains a single integer *k* (1<=β€<=*k*<=β€<=109). | You should output a graph *G* with *n* vertexes (2<=β€<=*n*<=β€<=1000). There must be exactly *k* shortest paths between vertex 1 and vertex 2 of the graph.
The first line must contain an integer *n*. Then adjacency matrix *G* with *n* rows and *n* columns must follow. Each element of the matrix must be 'N' or 'Y'. If *G**ij* is 'Y', then graph *G* has a edge connecting vertex *i* and vertex *j*. Consider the graph vertexes are numbered from 1 to *n*.
The graph must be undirected and simple: *G**ii* = 'N' and *G**ij*<==<=*G**ji* must hold. And there must be at least one path between vertex 1 and vertex 2. It's guaranteed that the answer exists. If there multiple correct answers, you can output any of them. | [
"2",
"9",
"1"
] | [
"4\nNNYY\nNNYY\nYYNN\nYYNN",
"8\nNNYYYNNN\nNNNNNYYY\nYNNNNYYY\nYNNNNYYY\nYNNNNYYY\nNYYYYNNN\nNYYYYNNN\nNYYYYNNN",
"2\nNY\nYN"
] | In first example, there are 2 shortest paths: 1-3-2 and 1-4-2.
In second example, there are 9 shortest paths: 1-3-6-2, 1-3-7-2, 1-3-8-2, 1-4-6-2, 1-4-7-2, 1-4-8-2, 1-5-6-2, 1-5-7-2, 1-5-8-2. | [
{
"input": "2",
"output": "498\nNNYYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN... | 62 | 0 | 0 | 3,035 | |
710 | Optimal Point on a Line | [
"brute force",
"sortings"
] | null | null | You are given *n* points on a line with their coordinates *x**i*. Find the point *x* so the sum of distances to the given points is minimal. | The first line contains integer *n* (1<=β€<=*n*<=β€<=3Β·105) β the number of points on the line.
The second line contains *n* integers *x**i* (<=-<=109<=β€<=*x**i*<=β€<=109) β the coordinates of the given *n* points. | Print the only integer *x* β the position of the optimal point on the line. If there are several optimal points print the position of the leftmost one. It is guaranteed that the answer is always the integer. | [
"4\n1 2 3 4\n"
] | [
"2\n"
] | none | [
{
"input": "4\n1 2 3 4",
"output": "2"
},
{
"input": "5\n-1 -10 2 6 7",
"output": "2"
},
{
"input": "10\n-68 10 87 22 30 89 82 -97 -52 25",
"output": "22"
},
{
"input": "100\n457 827 807 17 871 935 907 -415 536 170 551 -988 865 758 -457 -892 -875 -488 684 19 0 555 -807 -624 -... | 342 | 22,937,600 | 3 | 3,039 | |
767 | The Queue | [
"brute force",
"greedy"
] | null | null | Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow.
He knows that the receptionist starts working after *t**s* minutes have passed after midnight and closes after *t**f* minutes have passed after midnight (so that (*t**f*<=-<=1) is the last minute when the receptionist is still working). The receptionist spends exactly *t* minutes on each person in the queue. If the receptionist would stop working within *t* minutes, he stops serving visitors (other than the one he already serves).
Vasya also knows that exactly *n* visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately.
For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them.
Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him! | The first line contains three integers: the point of time when the receptionist begins to work *t**s*, the point of time when the receptionist stops working *t**f* and the time the receptionist spends on each visitor *t*. The second line contains one integer *n*Β β the amount of visitors (0<=β€<=*n*<=β€<=100<=000). The third line contains positive integers in non-decreasing orderΒ β the points of time when the visitors arrive to the passport office.
All times are set in minutes and do not exceed 1012; it is guaranteed that *t**s*<=<<=*t**f*. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist. | Print single non-negative integerΒ β the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them. | [
"10 15 2\n2\n10 13\n",
"8 17 3\n4\n3 4 5 8\n"
] | [
"12",
"2"
] | In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight.
In the second example, Vasya has to come before anyone else to be served. | [
{
"input": "10 15 2\n2\n10 13",
"output": "12"
},
{
"input": "8 17 3\n4\n3 4 5 8",
"output": "2"
},
{
"input": "7 14 3\n2\n1 2",
"output": "0"
},
{
"input": "30 70 10\n3\n30 32 35",
"output": "60"
},
{
"input": "21 56 7\n5\n1 2 3 4 5",
"output": "0"
},
{
... | 77 | 3,379,200 | -1 | 3,053 | |
158 | Next Round | [
"*special",
"implementation"
] | null | null | "Contestant who earns a score equal to or greater than the *k*-th place finisher's score will advance to the next round, as long as the contestant earns a positive score..." β an excerpt from contest rules.
A total of *n* participants took part in the contest (*n*<=β₯<=*k*), and you already know their scores. Calculate how many participants will advance to the next round. | The first line of the input contains two integers *n* and *k* (1<=β€<=*k*<=β€<=*n*<=β€<=50) separated by a single space.
The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=100), where *a**i* is the score earned by the participant who got the *i*-th place. The given sequence is non-increasing (that is, for all *i* from 1 to *n*<=-<=1 the following condition is fulfilled: *a**i*<=β₯<=*a**i*<=+<=1). | Output the number of participants who advance to the next round. | [
"8 5\n10 9 8 7 7 7 5 5\n",
"4 2\n0 0 0 0\n"
] | [
"6\n",
"0\n"
] | In the first example the participant on the 5th place earned 7 points. As the participant on the 6th place also earned 7 points, there are 6 advancers.
In the second example nobody got a positive score. | [
{
"input": "8 5\n10 9 8 7 7 7 5 5",
"output": "6"
},
{
"input": "4 2\n0 0 0 0",
"output": "0"
},
{
"input": "5 1\n1 1 1 1 1",
"output": "5"
},
{
"input": "5 5\n1 1 1 1 1",
"output": "5"
},
{
"input": "1 1\n10",
"output": "1"
},
{
"input": "17 14\n16 15... | 92 | 0 | 3 | 3,056 | |
545 | Woodcutters | [
"dp",
"greedy"
] | null | null | Little Susie listens to fairy tales before bed every day. Today's fairy tale was about wood cutters and the little girl immediately started imagining the choppers cutting wood. She imagined the situation that is described below.
There are *n* trees located along the road at points with coordinates *x*1,<=*x*2,<=...,<=*x**n*. Each tree has its height *h**i*. Woodcutters can cut down a tree and fell it to the left or to the right. After that it occupies one of the segments [*x**i*<=-<=*h**i*,<=*x**i*] or [*x**i*;*x**i*<=+<=*h**i*]. The tree that is not cut down occupies a single point with coordinate *x**i*. Woodcutters can fell a tree if the segment to be occupied by the fallen tree doesn't contain any occupied point. The woodcutters want to process as many trees as possible, so Susie wonders, what is the maximum number of trees to fell. | The first line contains integer *n* (1<=β€<=*n*<=β€<=105) β the number of trees.
Next *n* lines contain pairs of integers *x**i*,<=*h**i* (1<=β€<=*x**i*,<=*h**i*<=β€<=109) β the coordinate and the height of the *Ρ*-th tree.
The pairs are given in the order of ascending *x**i*. No two trees are located at the point with the same coordinate. | Print a single number β the maximum number of trees that you can cut down by the given rules. | [
"5\n1 2\n2 1\n5 10\n10 9\n19 1\n",
"5\n1 2\n2 1\n5 10\n10 9\n20 1\n"
] | [
"3\n",
"4\n"
] | In the first sample you can fell the trees like that:
- fell the 1-st tree to the left β now it occupies segment [β-β1;1] - fell the 2-nd tree to the right β now it occupies segment [2;3] - leave the 3-rd tree β it occupies point 5 - leave the 4-th tree β it occupies point 10 - fell the 5-th tree to the right β now it occupies segment [19;20]
In the second sample you can also fell 4-th tree to the right, after that it will occupy segment [10;19]. | [
{
"input": "5\n1 2\n2 1\n5 10\n10 9\n19 1",
"output": "3"
},
{
"input": "5\n1 2\n2 1\n5 10\n10 9\n20 1",
"output": "4"
},
{
"input": "4\n10 4\n15 1\n19 3\n20 1",
"output": "4"
},
{
"input": "35\n1 7\n3 11\n6 12\n7 6\n8 5\n9 11\n15 3\n16 10\n22 2\n23 3\n25 7\n27 3\n34 5\n35 10... | 217 | 9,011,200 | 0 | 3,059 | |
29 | Mail Stamps | [
"data structures",
"dfs and similar",
"graphs",
"implementation"
] | C. Mail Stamps | 2 | 256 | One day Bob got a letter in an envelope. Bob knows that when Berland's post officers send a letter directly from city Β«AΒ» to city Β«BΒ», they stamp it with Β«A BΒ», or Β«B AΒ». Unfortunately, often it is impossible to send a letter directly from the city of the sender to the city of the receiver, that's why the letter is sent via some intermediate cities. Post officers never send a letter in such a way that the route of this letter contains some city more than once. Bob is sure that the post officers stamp the letters accurately.
There are *n* stamps on the envelope of Bob's letter. He understands that the possible routes of this letter are only two. But the stamps are numerous, and Bob can't determine himself none of these routes. That's why he asks you to help him. Find one of the possible routes of the letter. | The first line contains integer *n* (1<=β€<=*n*<=β€<=105) β amount of mail stamps on the envelope. Then there follow *n* lines with two integers each β description of the stamps. Each stamp is described with indexes of the cities between which a letter is sent. The indexes of cities are integers from 1 to 109. Indexes of all the cities are different. Every time the letter is sent from one city to another, exactly one stamp is put on the envelope. It is guaranteed that the given stamps correspond to some valid route from some city to some other city. | Output *n*<=+<=1 numbers β indexes of cities in one of the two possible routes of the letter. | [
"2\n1 100\n100 2\n",
"3\n3 1\n100 2\n3 2\n"
] | [
"2 100 1 ",
"100 2 3 1 "
] | none | [
{
"input": "2\n1 100\n100 2",
"output": "2 100 1 "
},
{
"input": "3\n3 1\n100 2\n3 2",
"output": "100 2 3 1 "
},
{
"input": "3\n458744979 589655889\n248228386 824699605\n458744979 824699605",
"output": "589655889 458744979 824699605 248228386 "
},
{
"input": "4\n90104473 2210... | 1,154 | 20,992,000 | 3.672399 | 3,061 |
990 | Graph And Its Complement | [
"constructive algorithms",
"graphs",
"implementation"
] | null | null | Given three numbers $n, a, b$. You need to find an adjacency matrix of such an undirected graph that the number of components in it is equal to $a$, and the number of components in its complement is $b$. The matrix must be symmetric, and all digits on the main diagonal must be zeroes.
In an undirected graph loops (edges from a vertex to itself) are not allowed. It can be at most one edge between a pair of vertices.
The adjacency matrix of an undirected graph is a square matrix of size $n$ consisting only of "0" and "1", where $n$ is the number of vertices of the graph and the $i$-th row and the $i$-th column correspond to the $i$-th vertex of the graph. The cell $(i,j)$ of the adjacency matrix contains $1$ if and only if the $i$-th and $j$-th vertices in the graph are connected by an edge.
A connected component is a set of vertices $X$ such that for every two vertices from this set there exists at least one path in the graph connecting this pair of vertices, but adding any other vertex to $X$ violates this rule.
The complement or inverse of a graph $G$ is a graph $H$ on the same vertices such that two distinct vertices of $H$ are adjacent if and only if they are not adjacent in $G$. | In a single line, three numbers are given $n, a, b \,(1 \le n \le 1000, 1 \le a, b \le n)$: is the number of vertexes of the graph, the required number of connectivity components in it, and the required amount of the connectivity component in it's complement. | If there is no graph that satisfies these constraints on a single line, print "NO" (without quotes).
Otherwise, on the first line, print "YES"(without quotes). In each of the next $n$ lines, output $n$ digits such that $j$-th digit of $i$-th line must be $1$ if and only if there is an edge between vertices $i$ and $j$ in $G$ (and $0$ otherwise). Note that the matrix must be symmetric, and all digits on the main diagonal must be zeroes.
If there are several matrices that satisfy the conditions β output any of them. | [
"3 1 2\n",
"3 3 3\n"
] | [
"YES\n001\n001\n110\n",
"NO\n"
] | none | [
{
"input": "3 1 2",
"output": "YES\n001\n001\n110"
},
{
"input": "3 3 3",
"output": "NO"
},
{
"input": "5 1 1",
"output": "YES\n01000\n10100\n01010\n00101\n00010"
},
{
"input": "123 1 84",
"output": "YES\n0011111111111111111111111111111111111111111111111111111111111111111... | 0 | 0 | -1 | 3,062 | |
678 | Joty and Chocolate | [
"implementation",
"math",
"number theory"
] | null | null | Little Joty has got a task to do. She has a line of *n* tiles indexed from 1 to *n*. She has to paint them in a strange pattern.
An unpainted tile should be painted Red if it's index is divisible by *a* and an unpainted tile should be painted Blue if it's index is divisible by *b*. So the tile with the number divisible by *a* and *b* can be either painted Red or Blue.
After her painting is done, she will get *p* chocolates for each tile that is painted Red and *q* chocolates for each tile that is painted Blue.
Note that she can paint tiles in any order she wants.
Given the required information, find the maximumΒ number of chocolates Joty can get. | The only line contains five integers *n*, *a*, *b*, *p* and *q* (1<=β€<=*n*,<=*a*,<=*b*,<=*p*,<=*q*<=β€<=109). | Print the only integer *s* β the maximum number of chocolates Joty can get.
Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type. | [
"5 2 3 12 15\n",
"20 2 3 3 5\n"
] | [
"39\n",
"51\n"
] | none | [
{
"input": "5 2 3 12 15",
"output": "39"
},
{
"input": "20 2 3 3 5",
"output": "51"
},
{
"input": "1 1 1 1 1",
"output": "1"
},
{
"input": "1 2 2 2 2",
"output": "0"
},
{
"input": "2 1 3 3 3",
"output": "6"
},
{
"input": "3 1 1 3 3",
"output": "9"
... | 1,000 | 1,740,800 | 0 | 3,067 | |
257 | View Angle | [
"brute force",
"geometry",
"math"
] | null | null | Flatland has recently introduced a new type of an eye check for the driver's licence. The check goes like that: there is a plane with mannequins standing on it. You should tell the value of the minimum angle with the vertex at the origin of coordinates and with all mannequins standing inside or on the boarder of this angle.
As you spend lots of time "glued to the screen", your vision is impaired. So you have to write a program that will pass the check for you. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=105) β the number of mannequins.
Next *n* lines contain two space-separated integers each: *x**i*,<=*y**i* (|*x**i*|,<=|*y**i*|<=β€<=1000) β the coordinates of the *i*-th mannequin. It is guaranteed that the origin of the coordinates has no mannequin. It is guaranteed that no two mannequins are located in the same point on the plane. | Print a single real number β the value of the sought angle in degrees. The answer will be considered valid if the relative or absolute error doesn't exceed 10<=-<=6. | [
"2\n2 0\n0 2\n",
"3\n2 0\n0 2\n-2 2\n",
"4\n2 0\n0 2\n-2 0\n0 -2\n",
"2\n2 1\n1 2\n"
] | [
"90.0000000000\n",
"135.0000000000\n",
"270.0000000000\n",
"36.8698976458\n"
] | Solution for the first sample test is shown below:
Solution for the second sample test is shown below:
Solution for the third sample test is shown below:
Solution for the fourth sample test is shown below: | [
{
"input": "2\n2 0\n0 2",
"output": "90.0000000000"
},
{
"input": "3\n2 0\n0 2\n-2 2",
"output": "135.0000000000"
},
{
"input": "4\n2 0\n0 2\n-2 0\n0 -2",
"output": "270.0000000000"
},
{
"input": "2\n2 1\n1 2",
"output": "36.8698976458"
},
{
"input": "1\n1 1",
... | 1,714 | 30,003,200 | 0 | 3,069 | |
73 | FreeDiv | [
"dfs and similar",
"graphs",
"greedy"
] | D. FreeDiv | 5 | 256 | Vasya plays FreeDiv. In this game he manages a huge state, which has *n* cities and *m* two-way roads between them. Unfortunately, not from every city you can reach any other one moving along these roads. Therefore Vasya decided to divide the state into provinces so that in every province, one could reach from every city all the cities of the province, but there are no roads between provinces.
Unlike other turn-based strategies, in FreeDiv a player has the opportunity to build tunnels between cities. The tunnels are two-way roads along which one can move armies undetected by the enemy. However, no more than one tunnel can be connected to each city. As for Vasya, he wants to build a network of tunnels so that any pair of cities in his state were reachable by some path consisting of roads and a tunnels. But at that no more than *k* tunnels are connected to each province (otherwise, the province will be difficult to keep in case other provinces are captured by enemy armies).
Vasya discovered that maybe he will not be able to build such a network for the current condition of the state. Maybe he'll have first to build several roads between cities in different provinces to merge the provinces. Your task is to determine the minimum number of roads Vasya needs to build so that it was possible to build the required network of tunnels in the resulting state. | The first line contains three integers *n*, *m* and *k* (1<=β€<=*n*,<=*k*<=β€<=106,<=0<=β€<=*m*<=β€<=106). Each of the next *m* lines contains two integers. They are the numbers of cities connected by a corresponding road. No road connects city to itself and there is at most one road between each pair of cities. | Print a single number, the minimum number of additional roads. | [
"3 3 2\n1 2\n2 3\n3 1\n",
"4 2 2\n1 2\n3 4\n",
"4 0 2\n"
] | [
"0",
"0",
"1"
] | In the first example only one province exists, so it is not necessary to build any tunnels or roads.
In the second example two provinces exist. It is possible to merge the provinces by building a tunnel between cities 1 and 3.
In the third example at least one additional road is necessary. For example it is possible to build additional road between cities 1 and 2 and build two tunnels between cities 1 and 3, 2 and 4 after that. | [
{
"input": "3 3 2\n1 2\n2 3\n3 1",
"output": "0"
},
{
"input": "4 2 2\n1 2\n3 4",
"output": "0"
},
{
"input": "4 0 2",
"output": "1"
},
{
"input": "4 0 3",
"output": "1"
},
{
"input": "8 3 4\n1 4\n4 8\n8 1",
"output": "1"
},
{
"input": "8 3 2\n1 4\n4 8... | 92 | 0 | 0 | 3,076 |
771 | Bear and Different Names | [
"constructive algorithms",
"greedy"
] | null | null | In the army, it isn't easy to form a group of soldiers that will be effective on the battlefield. The communication is crucial and thus no two soldiers should share a name (what would happen if they got an order that Bob is a scouter, if there are two Bobs?).
A group of soldiers is effective if and only if their names are different. For example, a group (John, Bob, Limak) would be effective, while groups (Gary, Bob, Gary) and (Alice, Alice) wouldn't.
You are a spy in the enemy's camp. You noticed *n* soldiers standing in a row, numbered 1 through *n*. The general wants to choose a group of *k* consecutive soldiers. For every *k* consecutive soldiers, the general wrote down whether they would be an effective group or not.
You managed to steal the general's notes, with *n*<=-<=*k*<=+<=1 strings *s*1,<=*s*2,<=...,<=*s**n*<=-<=*k*<=+<=1, each either "YES" or "NO".
- The string *s*1 describes a group of soldiers 1 through *k* ("YES" if the group is effective, and "NO" otherwise). - The string *s*2 describes a group of soldiers 2 through *k*<=+<=1. - And so on, till the string *s**n*<=-<=*k*<=+<=1 that describes a group of soldiers *n*<=-<=*k*<=+<=1 through *n*.
Your task is to find possible names of *n* soldiers. Names should match the stolen notes. Each name should be a string that consists of between 1 and 10 English letters, inclusive. The first letter should be uppercase, and all other letters should be lowercase. Names don't have to be existing namesΒ β it's allowed to print "Xyzzzdj" or "T" for example.
Find and print any solution. It can be proved that there always exists at least one solution. | The first line of the input contains two integers *n* and *k* (2<=β€<=*k*<=β€<=*n*<=β€<=50)Β β the number of soldiers and the size of a group respectively.
The second line contains *n*<=-<=*k*<=+<=1 strings *s*1,<=*s*2,<=...,<=*s**n*<=-<=*k*<=+<=1. The string *s**i* is "YES" if the group of soldiers *i* through *i*<=+<=*k*<=-<=1 is effective, and "NO" otherwise. | Find any solution satisfying all given conditions. In one line print *n* space-separated strings, denoting possible names of soldiers in the order. The first letter of each name should be uppercase, while the other letters should be lowercase. Each name should contain English letters only and has length from 1 to 10.
If there are multiple valid solutions, print any of them. | [
"8 3\nNO NO YES YES YES NO\n",
"9 8\nYES NO\n",
"3 2\nNO NO\n"
] | [
"Adam Bob Bob Cpqepqwer Limak Adam Bob Adam",
"R Q Ccccccccc Ccocc Ccc So Strong Samples Ccc",
"Na Na Na"
] | In the first sample, there are 8 soldiers. For every 3 consecutive ones we know whether they would be an effective group. Let's analyze the provided sample output:
- First three soldiers (i.e. Adam, Bob, Bob) wouldn't be an effective group because there are two Bobs. Indeed, the string *s*<sub class="lower-index">1</sub> is "NO". - Soldiers 2 through 4 (Bob, Bob, Cpqepqwer) wouldn't be effective either, and the string *s*<sub class="lower-index">2</sub> is "NO". - Soldiers 3 through 5 (Bob, Cpqepqwer, Limak) would be effective, and the string *s*<sub class="lower-index">3</sub> is "YES". - ..., - Soldiers 6 through 8 (Adam, Bob, Adam) wouldn't be effective, and the string *s*<sub class="lower-index">6</sub> is "NO". | [
{
"input": "8 3\nNO NO YES YES YES NO",
"output": "Ab Ac Ab Ac Af Ag Ah Ag "
},
{
"input": "9 8\nYES NO",
"output": "Ab Ac Ad Ae Af Ag Ah Ai Ac "
},
{
"input": "3 2\nNO NO",
"output": "Ab Ab Ab "
},
{
"input": "2 2\nYES",
"output": "Ab Ac "
},
{
"input": "2 2\nNO"... | 62 | 4,608,000 | 3 | 3,085 | |
856 | Set Theory | [
"brute force",
"constructive algorithms"
] | null | null | Masha and Grisha like studying sets of positive integers.
One day Grisha has written a set *A* containing *n* different integers *a**i* on a blackboard. Now he asks Masha to create a set *B* containing *n* different integers *b**j* such that all *n*2 integers that can be obtained by summing up *a**i* and *b**j* for all possible pairs of *i* and *j* are different.
Both Masha and Grisha don't like big numbers, so all numbers in *A* are from 1 to 106, and all numbers in *B* must also be in the same range.
Help Masha to create the set *B* that satisfies Grisha's requirement. | Input data contains multiple test cases. The first line contains an integer *t*Β β the number of test cases (1<=β€<=*t*<=β€<=100).
Each test case is described in the following way: the first line of the description contains one integer *n*Β β the number of elements in *A* (1<=β€<=*n*<=β€<=100).
The second line contains *n* integers *a**i*Β β the elements of *A* (1<=β€<=*a**i*<=β€<=106). | For each test first print the answer:
- NO, if Masha's task is impossible to solve, there is no way to create the required set *B*. - YES, if there is the way to create the required set. In this case the second line must contain *n* different positive integers *b**j*Β β elements of *B* (1<=β€<=*b**j*<=β€<=106). If there are several possible sets, output any of them. | [
"3\n3\n1 10 100\n1\n1\n2\n2 4\n"
] | [
"YES\n1 2 3 \nYES\n1 \nYES\n1 2 \n"
] | none | [
{
"input": "3\n3\n1 10 100\n1\n1\n2\n2 4",
"output": "YES\n1 2 3 \nYES\n1 \nYES\n1 2 "
},
{
"input": "1\n100\n74 14 24 45 22 9 49 78 79 20 60 1 31 91 32 39 90 5 42 57 30 58 64 68 12 11 86 8 3 38 76 17 98 26 85 92 56 65 89 66 36 87 23 67 13 48 15 47 81 73 63 50 34 93 82 44 77 69 96 100 41 19 35 16 88... | 1,000 | 0 | 0 | 3,091 | |
865 | Save the problem! | [
"constructive algorithms"
] | null | null | Attention: we lost all the test cases for this problem, so instead of solving the problem, we need you to generate test cases. We're going to give you the answer, and you need to print a test case that produces the given answer. The original problem is in the following paragraph.
People don't use cash as often as they used to. Having a credit card solves some of the hassles of cash, such as having to receive change when you can't form the exact amount of money needed to purchase an item. Typically cashiers will give you as few coins as possible in change, but they don't have to. For example, if your change is 30 cents, a cashier could give you a 5 cent piece and a 25 cent piece, or they could give you three 10 cent pieces, or ten 1 cent pieces, two 5 cent pieces, and one 10 cent piece. Altogether there are 18 different ways to make 30 cents using only 1 cent pieces, 5 cent pieces, 10 cent pieces, and 25 cent pieces. Two ways are considered different if they contain a different number of at least one type of coin. Given the denominations of the coins and an amount of change to be made, how many different ways are there to make change?
As we mentioned before, we lost all the test cases for this problem, so we're actually going to give you the number of ways, and want you to produce a test case for which the number of ways is the given number. There could be many ways to achieve this (we guarantee there's always at least one), so you can print any, as long as it meets the constraints described below. | Input will consist of a single integer *A* (1<=β€<=*A*<=β€<=105), the desired number of ways. | In the first line print integers *N* and *M* (1<=β€<=*N*<=β€<=106,<=1<=β€<=*M*<=β€<=10), the amount of change to be made, and the number of denominations, respectively.
Then print *M* integers *D*1,<=*D*2,<=...,<=*D**M* (1<=β€<=*D**i*<=β€<=106), the denominations of the coins. All denominations must be distinct: for any *i*<=β <=*j* we must have *D**i*<=β <=*D**j*.
If there are multiple tests, print any of them. You can print denominations in atbitrary order. | [
"18\n",
"3\n",
"314\n"
] | [
"30 4\n1 5 10 25\n",
"20 2\n5 2\n",
"183 4\n6 5 2 139\n"
] | none | [
{
"input": "18",
"output": "30 4\n1 5 10 25"
},
{
"input": "3",
"output": "20 2\n5 2"
},
{
"input": "314",
"output": "183 4\n6 5 2 139"
},
{
"input": "1023",
"output": "2045 2\n1 2"
},
{
"input": "100000",
"output": "199999 2\n1 2"
},
{
"input": "1",
... | 109 | 0 | 3 | 3,094 | |
0 | none | [
"none"
] | null | null | Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built *n* towers in a row. The *i*-th tower is made of *h**i* identical blocks. For clarification see picture for the first sample.
Limak will repeat the following operation till everything is destroyed.
Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time.
Limak is ready to start. You task is to count how many operations will it take him to destroy all towers. | The first line contains single integer *n* (1<=β€<=*n*<=β€<=105).
The second line contains *n* space-separated integers *h*1,<=*h*2,<=...,<=*h**n* (1<=β€<=*h**i*<=β€<=109) β sizes of towers. | Print the number of operations needed to destroy all towers. | [
"6\n2 1 4 6 2 2\n",
"7\n3 3 3 1 3 3 3\n"
] | [
"3\n",
"2\n"
] | The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. | [
{
"input": "6\n2 1 4 6 2 2",
"output": "3"
},
{
"input": "7\n3 3 3 1 3 3 3",
"output": "2"
},
{
"input": "7\n5128 5672 5805 5452 5882 5567 5032",
"output": "4"
},
{
"input": "10\n1 2 2 3 5 5 5 4 2 1",
"output": "5"
},
{
"input": "14\n20 20 20 20 20 20 3 20 20 20 2... | 218 | 9,113,600 | 3 | 3,096 | |
161 | Dress'em in Vests! | [
"binary search",
"brute force",
"greedy",
"two pointers"
] | null | null | The Two-dimensional kingdom is going through hard times... This morning the Three-Dimensional kingdom declared war on the Two-dimensional one. This (possibly armed) conflict will determine the ultimate owner of the straight line.
The Two-dimensional kingdom has a regular army of *n* people. Each soldier registered himself and indicated the desired size of the bulletproof vest: the *i*-th soldier indicated size *a**i*. The soldiers are known to be unpretentious, so the command staff assumes that the soldiers are comfortable in any vests with sizes from *a**i*<=-<=*x* to *a**i*<=+<=*y*, inclusive (numbers *x*,<=*y*<=β₯<=0 are specified).
The Two-dimensional kingdom has *m* vests at its disposal, the *j*-th vest's size equals *b**j*. Help mobilize the Two-dimensional kingdom's army: equip with vests as many soldiers as possible. Each vest can be used only once. The *i*-th soldier can put on the *j*-th vest, if *a**i*<=-<=*x*<=β€<=*b**j*<=β€<=*a**i*<=+<=*y*. | The first input line contains four integers *n*, *m*, *x* and *y* (1<=β€<=*n*,<=*m*<=β€<=105, 0<=β€<=*x*,<=*y*<=β€<=109) β the number of soldiers, the number of vests and two numbers that specify the soldiers' unpretentiousness, correspondingly.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109) in non-decreasing order, separated by single spaces β the desired sizes of vests.
The third line contains *m* integers *b*1,<=*b*2,<=...,<=*b**m* (1<=β€<=*b**j*<=β€<=109) in non-decreasing order, separated by single spaces β the sizes of the available vests. | In the first line print a single integer *k* β the maximum number of soldiers equipped with bulletproof vests.
In the next *k* lines print *k* pairs, one pair per line, as "*u**i* *v**i*" (without the quotes). Pair (*u**i*, *v**i*) means that soldier number *u**i* must wear vest number *v**i*. Soldiers and vests are numbered starting from one in the order in which they are specified in the input. All numbers of soldiers in the pairs should be pairwise different, all numbers of vests in the pairs also should be pairwise different. You can print the pairs in any order.
If there are multiple optimal answers, you are allowed to print any of them. | [
"5 3 0 0\n1 2 3 3 4\n1 3 5\n",
"3 3 2 2\n1 5 9\n3 5 7\n"
] | [
"2\n1 1\n3 2\n",
"3\n1 1\n2 2\n3 3\n"
] | In the first sample you need the vests' sizes to match perfectly: the first soldier gets the first vest (size 1), the third soldier gets the second vest (size 3). This sample allows another answer, which gives the second vest to the fourth soldier instead of the third one.
In the second sample the vest size can differ from the desired size by at most 2 sizes, so all soldiers can be equipped. | [
{
"input": "5 3 0 0\n1 2 3 3 4\n1 3 5",
"output": "2\n1 1\n3 2"
},
{
"input": "3 3 2 2\n1 5 9\n3 5 7",
"output": "3\n1 1\n2 2\n3 3"
},
{
"input": "1 1 0 0\n1\n1",
"output": "1\n1 1"
},
{
"input": "1 1 0 0\n1\n2",
"output": "0"
},
{
"input": "2 3 1 4\n1 5\n1 2 2",
... | 1,028 | 15,155,200 | 3 | 3,107 | |
0 | none | [
"none"
] | null | null | Astronaut Natasha arrived on Mars. She knows that the Martians are very poor aliens. To ensure a better life for the Mars citizens, their emperor decided to take tax from every tourist who visited the planet. Natasha is the inhabitant of Earth, therefore she had to pay the tax to enter the territory of Mars.
There are $n$ banknote denominations on Mars: the value of $i$-th banknote is $a_i$. Natasha has an infinite number of banknotes of each denomination.
Martians have $k$ fingers on their hands, so they use a number system with base $k$. In addition, the Martians consider the digit $d$ (in the number system with base $k$) divine. Thus, if the last digit in Natasha's tax amount written in the number system with the base $k$ is $d$, the Martians will be happy. Unfortunately, Natasha does not know the Martians' divine digit yet.
Determine for which values $d$ Natasha can make the Martians happy.
Natasha can use only her banknotes. Martians don't give her change. | The first line contains two integers $n$ and $k$ ($1 \le n \le 100\,000$, $2 \le k \le 100\,000$)Β β the number of denominations of banknotes and the base of the number system on Mars.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$)Β β denominations of banknotes on Mars.
All numbers are given in decimal notation. | On the first line output the number of values $d$ for which Natasha can make the Martians happy.
In the second line, output all these values in increasing order.
Print all numbers in decimal notation. | [
"2 8\n12 20\n",
"3 10\n10 20 30\n"
] | [
"2\n0 4 ",
"1\n0 "
] | Consider the first test case. It uses the octal number system.
If you take one banknote with the value of $12$, you will get $14_8$ in octal system. The last digit is $4_8$.
If you take one banknote with the value of $12$ and one banknote with the value of $20$, the total value will be $32$. In the octal system, it is $40_8$. The last digit is $0_8$.
If you take two banknotes with the value of $20$, the total value will be $40$, this is $50_8$ in the octal system. The last digit is $0_8$.
No other digits other than $0_8$ and $4_8$ can be obtained. Digits $0_8$ and $4_8$ could also be obtained in other ways.
The second test case uses the decimal number system. The nominals of all banknotes end with zero, so Natasha can give the Martians only the amount whose decimal notation also ends with zero. | [
{
"input": "2 8\n12 20",
"output": "2\n0 4 "
},
{
"input": "3 10\n10 20 30",
"output": "1\n0 "
},
{
"input": "5 10\n20 16 4 16 2",
"output": "5\n0 2 4 6 8 "
},
{
"input": "10 5\n4 6 8 6 4 10 2 10 8 6",
"output": "5\n0 1 2 3 4 "
},
{
"input": "20 25\n15 10 5 20 10 ... | 202 | 10,547,200 | 0 | 3,114 | |
203 | Photographer | [
"greedy",
"sortings"
] | null | null | Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve.
The camera's memory is *d* megabytes. Valera's camera can take photos of high and low quality. One low quality photo takes *a* megabytes of memory, one high quality photo take *b* megabytes of memory. For unknown reasons, each client asks him to make several low quality photos and several high quality photos. More formally, the *i*-th client asks to make *x**i* low quality photos and *y**i* high quality photos.
Valera wants to serve as many clients per day as possible, provided that they will be pleased with his work. To please the *i*-th client, Valera needs to give him everything he wants, that is, to make *x**i* low quality photos and *y**i* high quality photos. To make one low quality photo, the camera must have at least *a* megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least *b* megabytes of free memory space. Initially the camera's memory is empty. Valera also does not delete photos from the camera so that the camera's memory gradually fills up.
Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients. | The first line contains two integers *n* and *d* (1<=β€<=*n*<=β€<=105,<=1<=β€<=*d*<=β€<=109) β the number of clients and the camera memory size, correspondingly. The second line contains two integers *a* and *b* (1<=β€<=*a*<=β€<=*b*<=β€<=104) β the size of one low quality photo and of one high quality photo, correspondingly.
Next *n* lines describe the clients. The *i*-th line contains two integers *x**i* and *y**i* (0<=β€<=*x**i*,<=*y**i*<=β€<=105) β the number of low quality photos and high quality photos the *i*-th client wants, correspondingly.
All numbers on all lines are separated by single spaces. | On the first line print the answer to the problem β the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data. | [
"3 10\n2 3\n1 4\n2 1\n1 0\n",
"3 6\n6 6\n1 1\n1 0\n1 0\n"
] | [
"2\n3 2 ",
"1\n2 "
] | none | [
{
"input": "3 10\n2 3\n1 4\n2 1\n1 0",
"output": "2\n3 2 "
},
{
"input": "3 6\n6 6\n1 1\n1 0\n1 0",
"output": "1\n2 "
},
{
"input": "4 5\n6 8\n1 2\n3 0\n10 2\n0 4",
"output": "0"
},
{
"input": "4 10\n6 6\n1 2\n2 2\n0 0\n0 0",
"output": "2\n3 4 "
},
{
"input": "10 ... | 2,000 | 15,462,400 | 0 | 3,120 | |
570 | Elections | [
"implementation"
] | null | null | The country of Byalechinsk is running elections involving *n* candidates. The country consists of *m* cities. We know how many people in each city voted for each candidate.
The electoral system in the country is pretty unusual. At the first stage of elections the votes are counted for each city: it is assumed that in each city won the candidate who got the highest number of votes in this city, and if several candidates got the maximum number of votes, then the winner is the one with a smaller index.
At the second stage of elections the winner is determined by the same principle over the cities: the winner of the elections is the candidate who won in the maximum number of cities, and among those who got the maximum number of cities the winner is the one with a smaller index.
Determine who will win the elections. | The first line of the input contains two integers *n*, *m* (1<=β€<=*n*,<=*m*<=β€<=100) β the number of candidates and of cities, respectively.
Each of the next *m* lines contains *n* non-negative integers, the *j*-th number in the *i*-th line *a**ij* (1<=β€<=*j*<=β€<=*n*, 1<=β€<=*i*<=β€<=*m*, 0<=β€<=*a**ij*<=β€<=109) denotes the number of votes for candidate *j* in city *i*.
It is guaranteed that the total number of people in all the cities does not exceed 109. | Print a single number β the index of the candidate who won the elections. The candidates are indexed starting from one. | [
"3 3\n1 2 3\n2 3 1\n1 2 1\n",
"3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7\n"
] | [
"2",
"1"
] | Note to the first sample test. At the first stage city 1 chosen candidate 3, city 2 chosen candidate 2, city 3 chosen candidate 2. The winner is candidate 2, he gained 2 votes.
Note to the second sample test. At the first stage in city 1 candidates 1 and 2 got the same maximum number of votes, but candidate 1 has a smaller index, so the city chose candidate 1. City 2 chosen candidate 3. City 3 chosen candidate 1, due to the fact that everyone has the same number of votes, and 1 has the smallest index. City 4 chosen the candidate 3. On the second stage the same number of cities chose candidates 1 and 3. The winner is candidate 1, the one with the smaller index. | [
{
"input": "3 3\n1 2 3\n2 3 1\n1 2 1",
"output": "2"
},
{
"input": "3 4\n10 10 3\n5 1 6\n2 2 2\n1 5 7",
"output": "1"
},
{
"input": "1 3\n5\n3\n2",
"output": "1"
},
{
"input": "3 1\n1 2 3",
"output": "3"
},
{
"input": "3 1\n100 100 100",
"output": "1"
},
{... | 31 | 0 | 0 | 3,122 | |
812 | Sagheer, the Hausmeister | [
"bitmasks",
"brute force",
"dp"
] | null | null | Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.
The building consists of *n* floors with stairs at the left and the right sides. Each floor has *m* rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with *n* rows and *m*<=+<=2 columns, where the first and the last columns represent the stairs, and the *m* columns in the middle represent rooms.
Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.
Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off. | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=15 and 1<=β€<=*m*<=β€<=100) β the number of floors and the number of rooms in each floor, respectively.
The next *n* lines contains the building description. Each line contains a binary string of length *m*<=+<=2 representing a floor (the left stairs, then *m* rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.
The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0. | Print a single integer β the minimum total time needed to turn off all the lights. | [
"2 2\n0010\n0100\n",
"3 4\n001000\n000010\n000010\n",
"4 3\n01110\n01110\n01110\n01110\n"
] | [
"5\n",
"12\n",
"18\n"
] | In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.
In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.
In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor. | [
{
"input": "2 2\n0010\n0100",
"output": "5"
},
{
"input": "3 4\n001000\n000010\n000010",
"output": "12"
},
{
"input": "4 3\n01110\n01110\n01110\n01110",
"output": "18"
},
{
"input": "3 2\n0000\n0100\n0100",
"output": "4"
},
{
"input": "1 89\n0000000000000000000000... | 155 | 1,228,800 | 3 | 3,126 | |
414 | Mashmokh and Numbers | [
"constructive algorithms",
"number theory"
] | null | null | It's holiday. Mashmokh and his boss, Bimokh, are playing a game invented by Mashmokh.
In this game Mashmokh writes sequence of *n* distinct integers on the board. Then Bimokh makes several (possibly zero) moves. On the first move he removes the first and the second integer from from the board, on the second move he removes the first and the second integer of the remaining sequence from the board, and so on. Bimokh stops when the board contains less than two numbers. When Bimokh removes numbers *x* and *y* from the board, he gets *gcd*(*x*,<=*y*) points. At the beginning of the game Bimokh has zero points.
Mashmokh wants to win in the game. For this reason he wants his boss to get exactly *k* points in total. But the guy doesn't know how choose the initial sequence in the right way.
Please, help him. Find *n* distinct integers *a*1,<=*a*2,<=...,<=*a**n* such that his boss will score exactly *k* points. Also Mashmokh can't memorize too huge numbers. Therefore each of these integers must be at most 109. | The first line of input contains two space-separated integers *n*,<=*k*Β (1<=β€<=*n*<=β€<=105;Β 0<=β€<=*k*<=β€<=108). | If such sequence doesn't exist output -1 otherwise output *n* distinct space-separated integers *a*1,<=*a*2,<=...,<=*a**n*Β (1<=β€<=*a**i*<=β€<=109). | [
"5 2\n",
"5 3",
"7 2\n"
] | [
"1 2 3 4 5\n",
"2 4 3 7 1",
"-1\n"
] | *gcd*(*x*,β*y*) is greatest common divisor of *x* and *y*. | [
{
"input": "5 2",
"output": "1 2 3 4 5"
},
{
"input": "5 3",
"output": "2 4 5 6 7"
},
{
"input": "7 2",
"output": "-1"
},
{
"input": "1 1",
"output": "-1"
},
{
"input": "2 0",
"output": "-1"
},
{
"input": "1 10",
"output": "-1"
},
{
"input"... | 343 | 102,400 | 3 | 3,141 | |
298 | Sail | [
"brute force",
"greedy",
"implementation"
] | null | null | The polar bears are going fishing. They plan to sail from (*s**x*,<=*s**y*) to (*e**x*,<=*e**y*). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (*x*,<=*y*).
- If the wind blows to the east, the boat will move to (*x*<=+<=1,<=*y*). - If the wind blows to the south, the boat will move to (*x*,<=*y*<=-<=1). - If the wind blows to the west, the boat will move to (*x*<=-<=1,<=*y*). - If the wind blows to the north, the boat will move to (*x*,<=*y*<=+<=1).
Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (*x*,<=*y*). Given the wind direction for *t* seconds, what is the earliest time they sail to (*e**x*,<=*e**y*)? | The first line contains five integers *t*,<=*s**x*,<=*s**y*,<=*e**x*,<=*e**y* (1<=β€<=*t*<=β€<=105,<=<=-<=109<=β€<=*s**x*,<=*s**y*,<=*e**x*,<=*e**y*<=β€<=109). The starting location and the ending location will be different.
The second line contains *t* characters, the *i*-th character is the wind blowing direction at the *i*-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north). | If they can reach (*e**x*,<=*e**y*) within *t* seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes). | [
"5 0 0 1 1\nSESNW\n",
"10 5 3 3 6\nNENSWESNEE\n"
] | [
"4\n",
"-1\n"
] | In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.
In the second sample, they cannot sail to the destination. | [
{
"input": "5 0 0 1 1\nSESNW",
"output": "4"
},
{
"input": "10 5 3 3 6\nNENSWESNEE",
"output": "-1"
},
{
"input": "19 -172106364 -468680119 -172106365 -468680119\nSSEEESSSESESWSEESSS",
"output": "13"
},
{
"input": "39 -1000000000 -1000000000 -999999997 -1000000000\nENEENWSWSS... | 310 | 22,630,400 | 3 | 3,144 | |
108 | Palindromic Times | [
"implementation",
"strings"
] | A. Palindromic Times | 2 | 256 | Tattah is asleep if and only if Tattah is attending a lecture. This is a well-known formula among Tattah's colleagues.
On a Wednesday afternoon, Tattah was attending Professor HH's lecture. At 12:21, right before falling asleep, he was staring at the digital watch around Saher's wrist. He noticed that the digits on the clock were the same when read from both directions i.e. a palindrome.
In his sleep, he started dreaming about such rare moments of the day when the time displayed on a digital clock is a palindrome. As soon as he woke up, he felt destined to write a program that finds the next such moment.
However, he still hasn't mastered the skill of programming while sleeping, so your task is to help him. | The first and only line of the input starts with a string with the format "HH:MM" where "HH" is from "00" to "23" and "MM" is from "00" to "59". Both "HH" and "MM" have exactly two digits. | Print the palindromic time of day that comes soonest after the time given in the input. If the input time is palindromic, output the soonest palindromic time after the input time. | [
"12:21\n",
"23:59\n"
] | [
"13:31\n",
"00:00\n"
] | none | [
{
"input": "12:21",
"output": "13:31"
},
{
"input": "23:59",
"output": "00:00"
},
{
"input": "15:51",
"output": "20:02"
},
{
"input": "10:44",
"output": "11:11"
},
{
"input": "04:02",
"output": "04:40"
},
{
"input": "02:11",
"output": "02:20"
},
... | 124 | 0 | 0 | 3,146 |
577 | Multiplication Table | [
"implementation",
"number theory"
] | null | null | Let's consider a table consisting of *n* rows and *n* columns. The cell located at the intersection of *i*-th row and *j*-th column contains number *i*<=Γ<=*j*. The rows and columns are numbered starting from 1.
You are given a positive integer *x*. Your task is to count the number of cells in a table that contain number *x*. | The single line contains numbers *n* and *x* (1<=β€<=*n*<=β€<=105, 1<=β€<=*x*<=β€<=109) β the size of the table and the number that we are looking for in the table. | Print a single number: the number of times *x* occurs in the table. | [
"10 5\n",
"6 12\n",
"5 13\n"
] | [
"2\n",
"4\n",
"0\n"
] | A table for the second sample test is given below. The occurrences of number 12 are marked bold. | [
{
"input": "10 5",
"output": "2"
},
{
"input": "6 12",
"output": "4"
},
{
"input": "5 13",
"output": "0"
},
{
"input": "1 1",
"output": "1"
},
{
"input": "2 1",
"output": "1"
},
{
"input": "100000 1",
"output": "1"
},
{
"input": "1 10000000... | 1,000 | 0 | 0 | 3,155 | |
916 | Jamie and Interesting Graph | [
"constructive algorithms",
"graphs",
"shortest paths"
] | null | null | Jamie has recently found undirected weighted graphs with the following properties very interesting:
- The graph is connected and contains exactly *n* vertices and *m* edges. - All edge weights are integers and are in range [1,<=109] inclusive. - The length of shortest path from 1 to *n* is a prime number. - The sum of edges' weights in the minimum spanning tree (MST) of the graph is a prime number. - The graph contains no loops or multi-edges.
If you are not familiar with some terms from the statement you can find definitions of them in notes section.
Help Jamie construct any graph with given number of vertices and edges that is interesting! | First line of input contains 2 integers *n*, *m* Β β the required number of vertices and edges. | In the first line output 2 integers *sp*, *mstw* (1<=β€<=*sp*,<=*mstw*<=β€<=1014)Β β the length of the shortest path and the sum of edges' weights in the minimum spanning tree.
In the next *m* lines output the edges of the graph. In each line output 3 integers *u*, *v*, *w* (1<=β€<=*u*,<=*v*<=β€<=*n*,<=1<=β€<=*w*<=β€<=109) describing the edge connecting *u* and *v* and having weight *w*. | [
"4 4\n",
"5 4\n"
] | [
"7 7\n1 2 3\n2 3 2\n3 4 2\n2 4 4\n",
"7 13\n1 2 2\n1 3 4\n1 4 3\n4 5 4\n"
] | The graph of sample 1: <img class="tex-graphics" src="https://espresso.codeforces.com/42f9750de41b0d9a6b21e8615170113cfe19b0f2.png" style="max-width: 100.0%;max-height: 100.0%;"/> Shortest path sequence: {1,β2,β3,β4}. MST edges are marked with an asterisk (*).
Definition of terms used in the problem statement:
A shortest path in an undirected graph is a sequence of vertices (*v*<sub class="lower-index">1</sub>,β*v*<sub class="lower-index">2</sub>,β... ,β*v*<sub class="lower-index">*k*</sub>) such that *v*<sub class="lower-index">*i*</sub> is adjacent to *v*<sub class="lower-index">*i*β+β1</sub> 1ββ€β*i*β<β*k* and the sum of weight <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e7f62cfd5c2c3b81dc80aaf2f512898495354f03.png" style="max-width: 100.0%;max-height: 100.0%;"/> is minimized where *w*(*i*,β*j*) is the edge weight between *i* and *j*. ([https://en.wikipedia.org/wiki/Shortest_path_problem](https://en.wikipedia.org/wiki/Shortest_path_problem))
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. ([https://en.wikipedia.org/wiki/Prime_number](https://en.wikipedia.org/wiki/Prime_number))
A minimum spanning tree (MST) is a subset of the edges of a connected, edge-weighted undirected graph that connects all the vertices together, without any cycles and with the minimum possible total edge weight. ([https://en.wikipedia.org/wiki/Minimum_spanning_tree](https://en.wikipedia.org/wiki/Minimum_spanning_tree))
[https://en.wikipedia.org/wiki/Multiple_edges](https://en.wikipedia.org/wiki/Multiple_edges) | [
{
"input": "4 4",
"output": "100003 100003\n1 2 100001\n2 3 1\n3 4 1\n1 3 1000000000"
},
{
"input": "5 4",
"output": "100003 100003\n1 2 100000\n2 3 1\n3 4 1\n4 5 1"
},
{
"input": "2 1",
"output": "100003 100003\n1 2 100003"
},
{
"input": "10 19",
"output": "100003 100003... | 841 | 7,270,400 | 3 | 3,159 | |
621 | Wet Shark and Bishops | [
"combinatorics",
"implementation"
] | null | null | Today, Wet Shark is given *n* bishops on a 1000 by 1000 grid. Both rows and columns of the grid are numbered from 1 to 1000. Rows are numbered from top to bottom, while columns are numbered from left to right.
Wet Shark thinks that two bishops attack each other if they share the same diagonal. Note, that this is the only criteria, so two bishops may attack each other (according to Wet Shark) even if there is another bishop located between them. Now Wet Shark wants to count the number of pairs of bishops that attack each other. | The first line of the input contains *n* (1<=β€<=*n*<=β€<=200<=000)Β β the number of bishops.
Each of next *n* lines contains two space separated integers *x**i* and *y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=1000)Β β the number of row and the number of column where *i*-th bishop is positioned. It's guaranteed that no two bishops share the same position. | Output one integerΒ β the number of pairs of bishops which attack each other. | [
"5\n1 1\n1 5\n3 3\n5 1\n5 5\n",
"3\n1 1\n2 3\n3 5\n"
] | [
"6\n",
"0\n"
] | In the first sample following pairs of bishops attack each other: (1,β3), (1,β5), (2,β3), (2,β4), (3,β4) and (3,β5). Pairs (1,β2), (1,β4), (2,β5) and (4,β5) do not attack each other because they do not share the same diagonal. | [
{
"input": "5\n1 1\n1 5\n3 3\n5 1\n5 5",
"output": "6"
},
{
"input": "3\n1 1\n2 3\n3 5",
"output": "0"
},
{
"input": "3\n859 96\n634 248\n808 72",
"output": "0"
},
{
"input": "3\n987 237\n891 429\n358 145",
"output": "0"
},
{
"input": "3\n411 81\n149 907\n611 114"... | 1,560 | 9,728,000 | 3 | 3,160 | |
585 | Lizard Era: Beginning | [
"meet-in-the-middle"
] | null | null | In the game Lizard Era: Beginning the protagonist will travel with three companions: Lynn, Meliana and Worrigan. Overall the game has *n* mandatory quests. To perform each of them, you need to take exactly two companions.
The attitude of each of the companions to the hero is an integer. Initially, the attitude of each of them to the hero of neutral and equal to 0. As the hero completes quests, he makes actions that change the attitude of the companions, whom he took to perform this task, in positive or negative direction.
Tell us what companions the hero needs to choose to make their attitude equal after completing all the quests. If this can be done in several ways, choose the one in which the value of resulting attitude is greatest possible. | The first line contains positive integer *n* (1<=β€<=*n*<=β€<=25) β the number of important tasks.
Next *n* lines contain the descriptions of the tasks β the *i*-th line contains three integers *l**i*,<=*m**i*,<=*w**i* β the values by which the attitude of Lynn, Meliana and Worrigan respectively will change towards the hero if the hero takes them on the *i*-th task. All the numbers in the input are integers and do not exceed 107 in absolute value. | If there is no solution, print in the first line "Impossible".
Otherwise, print *n* lines, two characters is each line β in the *i*-th line print the first letters of the companions' names that hero should take to complete the *i*-th task ('L' for Lynn, 'M' for Meliana, 'W' for Worrigan). Print the letters in any order, if there are multiple solutions, print any of them. | [
"3\n1 0 0\n0 1 0\n0 0 1\n",
"7\n0 8 9\n5 9 -2\n6 -8 -7\n9 4 5\n-4 -9 9\n-4 5 2\n-6 8 -7\n",
"2\n1 0 0\n1 1 0\n"
] | [
"LM\nMW\nMW\n",
"LM\nMW\nLM\nLW\nMW\nLM\nLW\n",
"Impossible\n"
] | none | [
{
"input": "3\n1 0 0\n0 1 0\n0 0 1",
"output": "LM\nMW\nMW"
},
{
"input": "7\n0 8 9\n5 9 -2\n6 -8 -7\n9 4 5\n-4 -9 9\n-4 5 2\n-6 8 -7",
"output": "LM\nMW\nLM\nLW\nMW\nLM\nLW"
},
{
"input": "2\n1 0 0\n1 1 0",
"output": "Impossible"
},
{
"input": "25\n26668 10412 12658\n25216 1... | 2,000 | 261,529,600 | 0 | 3,162 | |
819 | Mister B and PR Shifts | [
"data structures",
"implementation",
"math"
] | null | null | Some time ago Mister B detected a strange signal from the space, which he started to study.
After some transformation the signal turned out to be a permutation *p* of length *n* or its cyclic shift. For the further investigation Mister B need some basis, that's why he decided to choose cyclic shift of this permutation which has the minimum possible deviation.
Let's define the deviation of a permutation *p* as .
Find a cyclic shift of permutation *p* with minimum possible deviation. If there are multiple solutions, print any of them.
Let's denote id *k* (0<=β€<=*k*<=<<=*n*) of a cyclic shift of permutation *p* as the number of right shifts needed to reach this shift, for example:
- *k*<==<=0: shift *p*1,<=*p*2,<=... *p**n*, - *k*<==<=1: shift *p**n*,<=*p*1,<=... *p**n*<=-<=1, - ..., - *k*<==<=*n*<=-<=1: shift *p*2,<=*p*3,<=... *p**n*,<=*p*1. | First line contains single integer *n* (2<=β€<=*n*<=β€<=106) β the length of the permutation.
The second line contains *n* space-separated integers *p*1,<=*p*2,<=...,<=*p**n* (1<=β€<=*p**i*<=β€<=*n*)Β β the elements of the permutation. It is guaranteed that all elements are distinct. | Print two integers: the minimum deviation of cyclic shifts of permutation *p* and the id of such shift. If there are multiple solutions, print any of them. | [
"3\n1 2 3\n",
"3\n2 3 1\n",
"3\n3 2 1\n"
] | [
"0 0\n",
"0 1\n",
"2 1\n"
] | In the first sample test the given permutation *p* is the identity permutation, that's why its deviation equals to 0, the shift id equals to 0 as well.
In the second sample test the deviation of *p* equals to 4, the deviation of the 1-st cyclic shift (1,β2,β3) equals to 0, the deviation of the 2-nd cyclic shift (3,β1,β2) equals to 4, the optimal is the 1-st cyclic shift.
In the third sample test the deviation of *p* equals to 4, the deviation of the 1-st cyclic shift (1,β3,β2) equals to 2, the deviation of the 2-nd cyclic shift (2,β1,β3) also equals to 2, so the optimal are both 1-st and 2-nd cyclic shifts. | [
{
"input": "3\n1 2 3",
"output": "0 0"
},
{
"input": "3\n2 3 1",
"output": "0 1"
},
{
"input": "3\n3 2 1",
"output": "2 1"
},
{
"input": "2\n1 2",
"output": "0 0"
},
{
"input": "2\n2 1",
"output": "0 1"
},
{
"input": "10\n10 1 9 2 8 3 7 4 6 5",
"ou... | 46 | 0 | 0 | 3,166 | |
195 | After Training | [
"data structures",
"implementation",
"math"
] | null | null | After a team finished their training session on Euro football championship, Valeric was commissioned to gather the balls and sort them into baskets. Overall the stadium has *n* balls and *m* baskets. The baskets are positioned in a row from left to right and they are numbered with numbers from 1 to *m*, correspondingly. The balls are numbered with numbers from 1 to *n*.
Valeric decided to sort the balls in the order of increasing of their numbers by the following scheme. He will put each new ball in the basket with the least number of balls. And if he's got several variants, he chooses the basket which stands closer to the middle. That means that he chooses the basket for which is minimum, where *i* is the number of the basket. If in this case Valeric still has multiple variants, he chooses the basket with the minimum number.
For every ball print the number of the basket where it will go according to Valeric's scheme.
Note that the balls are sorted into baskets in the order of increasing numbers, that is, the first ball goes first, then goes the second ball and so on. | The first line contains two space-separated integers *n*, *m* (1<=β€<=*n*,<=*m*<=β€<=105) β the number of balls and baskets, correspondingly. | Print *n* numbers, one per line. The *i*-th line must contain the number of the basket for the *i*-th ball. | [
"4 3\n",
"3 1\n"
] | [
"2\n1\n3\n2\n",
"1\n1\n1\n"
] | none | [
{
"input": "4 3",
"output": "2\n1\n3\n2"
},
{
"input": "3 1",
"output": "1\n1\n1"
},
{
"input": "10 3",
"output": "2\n1\n3\n2\n1\n3\n2\n1\n3\n2"
},
{
"input": "6 5",
"output": "3\n2\n4\n1\n5\n3"
},
{
"input": "2 6",
"output": "3\n4"
},
{
"input": "5 2"... | 530 | 0 | 3 | 3,170 | |
304 | Calendar | [
"brute force",
"implementation"
] | null | null | Calendars in widespread use today include the Gregorian calendar, which is the de facto international standard, and is used almost everywhere in the world for civil purposes. The Gregorian reform modified the Julian calendar's scheme of leap years as follows:
Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100; the centurial years that are exactly divisible by 400 are still leap years. For example, the year 1900 is not a leap year; the year 2000 is a leap year.
In this problem, you have been given two dates and your task is to calculate how many days are between them. Note, that leap years have unusual number of days in February.
Look at the sample to understand what borders are included in the aswer. | The first two lines contain two dates, each date is in the format yyyy:mm:dd (1900<=β€<=*yyyy*<=β€<=2038 and yyyy:mm:dd is a legal date). | Print a single integer β the answer to the problem. | [
"1900:01:01\n2038:12:31\n",
"1996:03:09\n1991:11:12\n"
] | [
"50768\n",
"1579\n"
] | none | [
{
"input": "1900:01:01\n2038:12:31",
"output": "50768"
},
{
"input": "1996:03:09\n1991:11:12",
"output": "1579"
},
{
"input": "1999:12:31\n2000:02:29",
"output": "60"
},
{
"input": "1903:09:27\n1988:06:15",
"output": "30943"
},
{
"input": "1913:11:14\n1901:05:11",... | 404 | 1,126,400 | 3 | 3,171 | |
31 | Schedule | [
"implementation"
] | C. Schedule | 2 | 256 | At the beginning of the new semester there is new schedule in the Berland State University. According to this schedule, *n* groups have lessons at the room 31. For each group the starting time of the lesson and the finishing time of the lesson are known. It has turned out that it is impossible to hold all lessons, because for some groups periods of their lessons intersect. If at some moment of time one groups finishes it's lesson, and the other group starts the lesson, their lessons don't intersect.
The dean wants to cancel the lesson in one group so that no two time periods of lessons of the remaining groups intersect. You are to find all ways to do that. | The first line contains integer *n* (1<=β€<=*n*<=β€<=5000) β amount of groups, which have lessons in the room 31. Then *n* lines follow, each of them contains two integers *l**i* *r**i* (1<=β€<=*l**i*<=<<=*r**i*<=β€<=106) β starting and finishing times of lesson of the *i*-th group. It is possible that initially no two lessons intersect (see sample 1). | Output integer *k* β amount of ways to cancel the lesson in exactly one group so that no two time periods of lessons of the remaining groups intersect. In the second line output *k* numbers β indexes of groups, where it is possible to cancel the lesson. Groups are numbered starting from 1 in the order that they were given in the input. Output the numbers in increasing order. | [
"3\n3 10\n20 30\n1 3\n",
"4\n3 10\n20 30\n1 3\n1 39\n",
"3\n1 5\n2 6\n3 7\n"
] | [
"3\n1 2 3 ",
"1\n4 ",
"0\n"
] | none | [
{
"input": "3\n3 10\n20 30\n1 3",
"output": "3\n1 2 3 "
},
{
"input": "4\n3 10\n20 30\n1 3\n1 39",
"output": "1\n4 "
},
{
"input": "3\n1 5\n2 6\n3 7",
"output": "0"
},
{
"input": "4\n1 5\n5 7\n6 9\n9 10",
"output": "2\n2 3 "
},
{
"input": "11\n717170 795210\n86642... | 2,000 | 204,800 | 0 | 3,173 |
15 | Cottage Village | [
"implementation",
"sortings"
] | A. Cottage Village | 2 | 64 | A new cottage village called Β«FlatvilleΒ» is being built in Flatland. By now they have already built in Β«FlatvilleΒ» *n* square houses with the centres on the *Πx*-axis. The houses' sides are parallel to the coordinate axes. It's known that no two houses overlap, but they can touch each other.
The architect bureau, where Peter works, was commissioned to build a new house in Β«FlatvilleΒ». The customer wants his future house to be on the *Πx*-axis, to be square in shape, have a side *t*, and touch at least one of the already built houses. For sure, its sides should be parallel to the coordinate axes, its centre should be on the *Ox*-axis and it shouldn't overlap any of the houses in the village.
Peter was given a list of all the houses in Β«FlatvilleΒ». Would you help him find the amount of possible positions of the new house? | The first line of the input data contains numbers *n* and *t* (1<=β€<=*n*,<=*t*<=β€<=1000). Then there follow *n* lines, each of them contains two space-separated integer numbers: *x**i* *a**i*, where *x**i* β *x*-coordinate of the centre of the *i*-th house, and *a**i* β length of its side (<=-<=1000<=β€<=*x**i*<=β€<=1000, 1<=β€<=*a**i*<=β€<=1000). | Output the amount of possible positions of the new house. | [
"2 2\n0 4\n6 2\n",
"2 2\n0 4\n5 2\n",
"2 3\n0 4\n5 2\n"
] | [
"4\n",
"3\n",
"2\n"
] | It is possible for the *x*-coordinate of the new house to have non-integer value. | [
{
"input": "2 2\n0 4\n6 2",
"output": "4"
},
{
"input": "2 2\n0 4\n5 2",
"output": "3"
},
{
"input": "2 3\n0 4\n5 2",
"output": "2"
},
{
"input": "1 1\n1 1",
"output": "2"
},
{
"input": "1 2\n2 1",
"output": "2"
},
{
"input": "2 1\n2 1\n1 1",
"outp... | 154 | 2,355,200 | 3.943952 | 3,176 |
499 | Watching a movie | [
"greedy",
"implementation"
] | null | null | You have decided to watch the best moments of some movie. There are two buttons on your player:
1. Watch the current minute of the movie. By pressing this button, you watch the current minute of the movie and the player automatically proceeds to the next minute of the movie. 1. Skip exactly *x* minutes of the movie (*x* is some fixed positive integer). If the player is now at the *t*-th minute of the movie, then as a result of pressing this button, it proceeds to the minute (*t*<=+<=*x*).
Initially the movie is turned on in the player on the first minute, and you want to watch exactly *n* best moments of the movie, the *i*-th best moment starts at the *l**i*-th minute and ends at the *r**i*-th minute (more formally, the *i*-th best moment consists of minutes: *l**i*,<=*l**i*<=+<=1,<=...,<=*r**i*).
Determine, what is the minimum number of minutes of the movie you have to watch if you want to watch all the best moments? | The first line contains two space-separated integers *n*, *x* (1<=β€<=*n*<=β€<=50, 1<=β€<=*x*<=β€<=105) β the number of the best moments of the movie and the value of *x* for the second button.
The following *n* lines contain the descriptions of the best moments of the movie, the *i*-th line of the description contains two integers separated by a space *l**i*, *r**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=105).
It is guaranteed that for all integers *i* from 2 to *n* the following condition holds: *r**i*<=-<=1<=<<=*l**i*. | Output a single number β the answer to the problem. | [
"2 3\n5 6\n10 12\n",
"1 1\n1 100000\n"
] | [
"6\n",
"100000\n"
] | In the first sample, the player was initially standing on the first minute. As the minutes from the 1-st to the 4-th one don't contain interesting moments, we press the second button. Now we can not press the second button and skip 3 more minutes, because some of them contain interesting moments. Therefore, we watch the movie from the 4-th to the 6-th minute, after that the current time is 7. Similarly, we again skip 3 minutes and then watch from the 10-th to the 12-th minute of the movie. In total, we watch 6 minutes of the movie.
In the second sample, the movie is very interesting, so you'll have to watch all 100000 minutes of the movie. | [
{
"input": "2 3\n5 6\n10 12",
"output": "6"
},
{
"input": "1 1\n1 100000",
"output": "100000"
},
{
"input": "10 1\n2156 3497\n4784 7775\n14575 31932\n33447 35902\n36426 47202\n48772 60522\n63982 68417\n78537 79445\n90081 90629\n94325 95728",
"output": "53974"
},
{
"input": "1... | 93 | 204,800 | -1 | 3,179 | |
991 | Candies | [
"binary search",
"implementation"
] | null | null | After passing a test, Vasya got himself a box of $n$ candies. He decided to eat an equal amount of candies each morning until there are no more candies. However, Petya also noticed the box and decided to get some candies for himself.
This means the process of eating candies is the following: in the beginning Vasya chooses a single integer $k$, same for all days. After that, in the morning he eats $k$ candies from the box (if there are less than $k$ candies in the box, he eats them all), then in the evening Petya eats $10\%$ of the candies remaining in the box. If there are still candies left in the box, the process repeatsΒ β next day Vasya eats $k$ candies again, and PetyaΒ β $10\%$ of the candies left in a box, and so on.
If the amount of candies in the box is not divisible by $10$, Petya rounds the amount he takes from the box down. For example, if there were $97$ candies in the box, Petya would eat only $9$ of them. In particular, if there are less than $10$ candies in a box, Petya won't eat any at all.
Your task is to find out the minimal amount of $k$ that can be chosen by Vasya so that he would eat at least half of the $n$ candies he initially got. Note that the number $k$ must be integer. | The first line contains a single integer $n$ ($1 \leq n \leq 10^{18}$)Β β the initial amount of candies in the box. | Output a single integerΒ β the minimal amount of $k$ that would allow Vasya to eat at least half of candies he got. | [
"68\n"
] | [
"3\n"
] | In the sample, the amount of candies, with $k=3$, would change in the following way (Vasya eats first):
$68 \to 65 \to 59 \to 56 \to 51 \to 48 \to 44 \to 41 \\ \to 37 \to 34 \to 31 \to 28 \to 26 \to 23 \to 21 \to 18 \to 17 \to 14 \\ \to 13 \to 10 \to 9 \to 6 \to 6 \to 3 \to 3 \to 0$.
In total, Vasya would eat $39$ candies, while PetyaΒ β $29$. | [
{
"input": "68",
"output": "3"
},
{
"input": "1",
"output": "1"
},
{
"input": "2",
"output": "1"
},
{
"input": "42",
"output": "1"
},
{
"input": "43",
"output": "2"
},
{
"input": "756",
"output": "29"
},
{
"input": "999999972",
"output"... | 1,000 | 0 | 0 | 3,181 | |
987 | Three displays | [
"brute force",
"dp",
"implementation"
] | null | null | It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay. | The first line contains a single integer $n$ ($3 \le n \le 3\,000$)Β β the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$)Β β the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$)Β β the rent costs for each display. | If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integerΒ β the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$. | [
"5\n2 4 5 4 10\n40 30 20 10 40\n",
"3\n100 101 100\n2 4 5\n",
"10\n1 2 3 4 5 6 7 8 9 10\n10 13 11 14 15 12 13 13 18 13\n"
] | [
"90\n",
"-1\n",
"33\n"
] | In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | [
{
"input": "5\n2 4 5 4 10\n40 30 20 10 40",
"output": "90"
},
{
"input": "3\n100 101 100\n2 4 5",
"output": "-1"
},
{
"input": "10\n1 2 3 4 5 6 7 8 9 10\n10 13 11 14 15 12 13 13 18 13",
"output": "33"
},
{
"input": "3\n1 2 3\n100000000 100000000 100000000",
"output": "300... | 296 | 7,270,400 | -1 | 3,182 | |
255 | Almost Arithmetical Progression | [
"brute force",
"dp"
] | null | null | Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as:
- *a*1<==<=*p*, where *p* is some integer; - *a**i*<==<=*a**i*<=-<=1<=+<=(<=-<=1)*i*<=+<=1Β·*q* (*i*<=><=1), where *q* is some integer.
Right now Gena has a piece of paper with sequence *b*, consisting of *n* integers. Help Gena, find there the longest subsequence of integers that is an almost arithmetical progression.
Sequence *s*1,<=<=*s*2,<=<=...,<=<=*s**k* is a subsequence of sequence *b*1,<=<=*b*2,<=<=...,<=<=*b**n*, if there is such increasing sequence of indexes *i*1,<=*i*2,<=...,<=*i**k* (1<=<=β€<=<=*i*1<=<=<<=<=*i*2<=<=<<=... <=<=<<=<=*i**k*<=<=β€<=<=*n*), that *b**i**j*<=<==<=<=*s**j*. In other words, sequence *s* can be obtained from *b* by crossing out some elements. | The first line contains integer *n* (1<=β€<=*n*<=β€<=4000). The next line contains *n* integers *b*1,<=*b*2,<=...,<=*b**n* (1<=β€<=*b**i*<=β€<=106). | Print a single integer β the length of the required longest subsequence. | [
"2\n3 5\n",
"4\n10 20 10 30\n"
] | [
"2\n",
"3\n"
] | In the first test the sequence actually is the suitable subsequence.
In the second test the following subsequence fits: 10,β20,β10. | [
{
"input": "2\n3 5",
"output": "2"
},
{
"input": "4\n10 20 10 30",
"output": "3"
},
{
"input": "5\n4 4 3 5 1",
"output": "2"
},
{
"input": "6\n2 3 2 2 1 3",
"output": "4"
},
{
"input": "8\n2 2 5 3 4 3 3 2",
"output": "3"
},
{
"input": "2\n468 335",
... | 249 | 5,427,200 | 0 | 3,188 | |
999 | Reachability from the Capital | [
"dfs and similar",
"graphs",
"greedy"
] | null | null | There are $n$ cities and $m$ roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way.
What is the minimum number of new roads that need to be built to make all the cities reachable from the capital?
New roads will also be one-way. | The first line of input consists of three integers $n$, $m$ and $s$ ($1 \le n \le 5000, 0 \le m \le 5000, 1 \le s \le n$) β the number of cities, the number of roads and the index of the capital. Cities are indexed from $1$ to $n$.
The following $m$ lines contain roads: road $i$ is given as a pair of cities $u_i$, $v_i$ ($1 \le u_i, v_i \le n$, $u_i \ne v_i$). For each pair of cities $(u, v)$, there can be at most one road from $u$ to $v$. Roads in opposite directions between a pair of cities are allowed (i.e. from $u$ to $v$ and from $v$ to $u$). | Print one integer β the minimum number of extra roads needed to make all the cities reachable from city $s$. If all the cities are already reachable from $s$, print 0. | [
"9 9 1\n1 2\n1 3\n2 3\n1 5\n5 6\n6 1\n1 8\n9 8\n7 1\n",
"5 4 5\n1 2\n2 3\n3 4\n4 1\n"
] | [
"3\n",
"1\n"
] | The first example is illustrated by the following:
For example, you can add roads ($6, 4$), ($7, 9$), ($1, 7$) to make all the cities reachable from $s = 1$.
The second example is illustrated by the following:
In this example, you can add any one of the roads ($5, 1$), ($5, 2$), ($5, 3$), ($5, 4$) to make all the cities reachable from $s = 5$. | [
{
"input": "9 9 1\n1 2\n1 3\n2 3\n1 5\n5 6\n6 1\n1 8\n9 8\n7 1",
"output": "3"
},
{
"input": "5 4 5\n1 2\n2 3\n3 4\n4 1",
"output": "1"
},
{
"input": "5000 0 2956",
"output": "4999"
},
{
"input": "2 0 2",
"output": "1"
},
{
"input": "2 1 1\n1 2",
"output": "0"... | 514 | 11,468,800 | 0 | 3,191 | |
777 | Alyona and Spreadsheet | [
"binary search",
"data structures",
"dp",
"greedy",
"implementation",
"two pointers"
] | null | null | During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables.
Now she has a table filled with integers. The table consists of *n* rows and *m* columns. By *a**i*,<=*j* we will denote the integer located at the *i*-th row and the *j*-th column. We say that the table is sorted in non-decreasing order in the column *j* if *a**i*,<=*j*<=β€<=*a**i*<=+<=1,<=*j* for all *i* from 1 to *n*<=-<=1.
Teacher gave Alyona *k* tasks. For each of the tasks two integers *l* and *r* are given and Alyona has to answer the following question: if one keeps the rows from *l* to *r* inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such *j* that *a**i*,<=*j*<=β€<=*a**i*<=+<=1,<=*j* for all *i* from *l* to *r*<=-<=1 inclusive.
Alyona is too small to deal with this task and asks you to help! | The first line of the input contains two positive integers *n* and *m* (1<=β€<=*n*Β·*m*<=β€<=100<=000)Β β the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table.
Each of the following *n* lines contains *m* integers. The *j*-th integers in the *i* of these lines stands for *a**i*,<=*j* (1<=β€<=*a**i*,<=*j*<=β€<=109).
The next line of the input contains an integer *k* (1<=β€<=*k*<=β€<=100<=000)Β β the number of task that teacher gave to Alyona.
The *i*-th of the next *k* lines contains two integers *l**i* and *r**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*n*). | Print "Yes" to the *i*-th line of the output if the table consisting of rows from *l**i* to *r**i* inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". | [
"5 4\n1 2 3 5\n3 1 3 2\n4 5 2 3\n5 5 3 2\n4 4 3 4\n6\n1 1\n2 5\n4 5\n3 5\n1 3\n1 5\n"
] | [
"Yes\nNo\nYes\nYes\nYes\nNo\n"
] | In the sample, the whole table is not sorted in any column. However, rows 1β3 are sorted in column 1, while rows 4β5 are sorted in column 3. | [
{
"input": "5 4\n1 2 3 5\n3 1 3 2\n4 5 2 3\n5 5 3 2\n4 4 3 4\n6\n1 1\n2 5\n4 5\n3 5\n1 3\n1 5",
"output": "Yes\nNo\nYes\nYes\nYes\nNo"
},
{
"input": "1 1\n1\n1\n1 1",
"output": "Yes"
},
{
"input": "10 1\n523130301\n127101624\n15573616\n703140639\n628818570\n957494759\n161270109\n38686565... | 966 | 23,654,400 | 0 | 3,193 | |
543 | Writing Code | [
"dp"
] | null | null | Programmers working on a large project have just received a task to write exactly *m* lines of code. There are *n* programmers working on a project, the *i*-th of them makes exactly *a**i* bugs in every line of code that he writes.
Let's call a sequence of non-negative integers *v*1,<=*v*2,<=...,<=*v**n* a plan, if *v*1<=+<=*v*2<=+<=...<=+<=*v**n*<==<=*m*. The programmers follow the plan like that: in the beginning the first programmer writes the first *v*1 lines of the given task, then the second programmer writes *v*2 more lines of the given task, and so on. In the end, the last programmer writes the remaining lines of the code. Let's call a plan good, if all the written lines of the task contain at most *b* bugs in total.
Your task is to determine how many distinct good plans are there. As the number of plans can be large, print the remainder of this number modulo given positive integer *mod*. | The first line contains four integers *n*, *m*, *b*, *mod* (1<=β€<=*n*,<=*m*<=β€<=500, 0<=β€<=*b*<=β€<=500; 1<=β€<=*mod*<=β€<=109<=+<=7)Β β the number of programmers, the number of lines of code in the task, the maximum total number of bugs respectively and the modulo you should use when printing the answer.
The next line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=500)Β β the number of bugs per line for each programmer. | Print a single integer β the answer to the problem modulo *mod*. | [
"3 3 3 100\n1 1 1\n",
"3 6 5 1000000007\n1 2 3\n",
"3 5 6 11\n1 2 1\n"
] | [
"10\n",
"0\n",
"0\n"
] | none | [
{
"input": "3 3 3 100\n1 1 1",
"output": "10"
},
{
"input": "3 6 5 1000000007\n1 2 3",
"output": "0"
},
{
"input": "3 5 6 11\n1 2 1",
"output": "0"
},
{
"input": "2 3 3 1000\n1 2",
"output": "1"
},
{
"input": "3 10 10 150691913\n8 7 10",
"output": "0"
},
{... | 3,000 | 14,745,600 | 0 | 3,197 | |
0 | none | [
"none"
] | null | null | Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every *a* minutes, but a train goes to Masha's direction every *b* minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often. | The first line contains two integers *a* and *b* (*a*<=β <=*b*,<=1<=β€<=*a*,<=*b*<=β€<=106). | Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency. | [
"3 7\n",
"5 3\n",
"2 3\n"
] | [
"Dasha\n",
"Masha\n",
"Equal\n"
] | Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0,β6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often. | [
{
"input": "3 7",
"output": "Dasha"
},
{
"input": "5 3",
"output": "Masha"
},
{
"input": "2 3",
"output": "Equal"
},
{
"input": "31 88",
"output": "Dasha"
},
{
"input": "8 75",
"output": "Dasha"
},
{
"input": "32 99",
"output": "Dasha"
},
{
... | 92 | 512,000 | -1 | 3,203 | |
287 | IQ Test | [
"brute force",
"implementation"
] | null | null | In the city of Ultima Thule job applicants are often offered an IQ test.
The test is as follows: the person gets a piece of squared paper with a 4<=Γ<=4 square painted on it. Some of the square's cells are painted black and others are painted white. Your task is to repaint at most one cell the other color so that the picture has a 2<=Γ<=2 square, completely consisting of cells of the same color. If the initial picture already has such a square, the person should just say so and the test will be completed.
Your task is to write a program that determines whether it is possible to pass the test. You cannot pass the test if either repainting any cell or no action doesn't result in a 2<=Γ<=2 square, consisting of cells of the same color. | Four lines contain four characters each: the *j*-th character of the *i*-th line equals "." if the cell in the *i*-th row and the *j*-th column of the square is painted white, and "#", if the cell is black. | Print "YES" (without the quotes), if the test can be passed and "NO" (without the quotes) otherwise. | [
"####\n.#..\n####\n....\n",
"####\n....\n####\n....\n"
] | [
"YES\n",
"NO\n"
] | In the first test sample it is enough to repaint the first cell in the second row. After such repainting the required 2βΓβ2 square is on the intersection of the 1-st and 2-nd row with the 1-st and 2-nd column. | [
{
"input": "###.\n...#\n###.\n...#",
"output": "NO"
},
{
"input": ".##.\n#..#\n.##.\n#..#",
"output": "NO"
},
{
"input": ".#.#\n#.#.\n.#.#\n#.#.",
"output": "NO"
},
{
"input": "##..\n..##\n##..\n..##",
"output": "NO"
},
{
"input": "#.#.\n#.#.\n.#.#\n.#.#",
"ou... | 77 | 7,065,600 | 3 | 3,206 | |
980 | Posterized | [
"games",
"greedy"
] | null | null | Professor Ibrahim has prepared the final homework for his algorithmβs class. He asked his students to implement the Posterization Image Filter.
Their algorithm will be tested on an array of integers, where the $i$-th integer represents the color of the $i$-th pixel in the image. The image is in black and white, therefore the color of each pixel will be an integer between 0 and 255 (inclusive).
To implement the filter, students are required to divide the black and white color range [0, 255] into groups of consecutive colors, and select one color in each group to be the groupβs key. In order to preserve image details, the size of a group must not be greater than $k$, and each color should belong to exactly one group.
Finally, the students will replace the color of each pixel in the array with that colorβs assigned group key.
To better understand the effect, here is an image of a basking turtle where the Posterization Filter was applied with increasing $k$ to the right.
To make the process of checking the final answer easier, Professor Ibrahim wants students to divide the groups and assign the keys in a way that produces the lexicographically smallest possible array. | The first line of input contains two integers $n$ and $k$ ($1 \leq n \leq 10^5$, $1 \leq k \leq 256$), the number of pixels in the image, and the maximum size of a group, respectively.
The second line contains $n$ integers $p_1, p_2, \dots, p_n$ ($0 \leq p_i \leq 255$), where $p_i$ is the color of the $i$-th pixel. | Print $n$ space-separated integers; the lexicographically smallest possible array that represents the image after applying the Posterization filter. | [
"4 3\n2 14 3 4\n",
"5 2\n0 2 1 255 254\n"
] | [
"0 12 3 3\n",
"0 1 1 254 254\n"
] | One possible way to group colors and assign keys for the first sample:
Color $2$ belongs to the group $[0,2]$, with group key $0$.
Color $14$ belongs to the group $[12,14]$, with group key $12$.
Colors $3$ and $4$ belong to group $[3, 5]$, with group key $3$.
Other groups won't affect the result so they are not listed here. | [
{
"input": "4 3\n2 14 3 4",
"output": "0 12 3 3"
},
{
"input": "5 2\n0 2 1 255 254",
"output": "0 1 1 254 254"
},
{
"input": "10 3\n112 184 161 156 118 231 191 128 91 229",
"output": "110 182 159 154 116 229 189 126 89 229"
},
{
"input": "9 3\n174 149 118 124 166 146 219 233 ... | 218 | 2,048,000 | -1 | 3,207 | |
437 | The Child and Set | [
"bitmasks",
"greedy",
"implementation",
"sortings"
] | null | null | At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set *S*:
- its elements were distinct integers from 1 to *limit*; - the value of was equal to *sum*; here *lowbit*(*x*) equals 2*k* where *k* is the position of the first one in the binary representation of *x*. For example, *lowbit*(100102)<==<=102,<=*lowbit*(100012)<==<=12,<=*lowbit*(100002)<==<=100002 (binary representation).
Can you help Picks and find any set *S*, that satisfies all the above conditions? | The first line contains two integers: *sum*,<=*limit* (1<=β€<=*sum*,<=*limit*<=β€<=105). | In the first line print an integer *n* (1<=β€<=*n*<=β€<=105), denoting the size of *S*. Then print the elements of set *S* in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1. | [
"5 5\n",
"4 3\n",
"5 1\n"
] | [
"2\n4 5\n",
"3\n2 3 1\n",
"-1\n"
] | In sample test 1: *lowbit*(4)β=β4,β*lowbit*(5)β=β1,β4β+β1β=β5.
In sample test 2: *lowbit*(1)β=β1,β*lowbit*(2)β=β2,β*lowbit*(3)β=β1,β1β+β2β+β1β=β4. | [
{
"input": "5 5",
"output": "2\n4 5"
},
{
"input": "4 3",
"output": "3\n2 3 1"
},
{
"input": "5 1",
"output": "-1"
},
{
"input": "54321 12345",
"output": "7008\n8958 8925 11009 10808 8221 9771 11269 7017 6416 11723 10324 5654 6569 10454 9164 10754 6069 7913 12154 11111 73... | 61 | 0 | 0 | 3,217 | |
961 | Lecture Sleep | [
"data structures",
"dp",
"implementation",
"two pointers"
] | null | null | Your friend Mishka and you attend a calculus lecture. Lecture lasts *n* minutes. Lecturer tells *a**i* theorems during the *i*-th minute.
Mishka is really interested in calculus, though it is so hard to stay awake for all the time of lecture. You are given an array *t* of Mishka's behavior. If Mishka is asleep during the *i*-th minute of the lecture then *t**i* will be equal to 0, otherwise it will be equal to 1. When Mishka is awake he writes down all the theorems he is being told β *a**i* during the *i*-th minute. Otherwise he writes nothing.
You know some secret technique to keep Mishka awake for *k* minutes straight. However you can use it only once. You can start using it at the beginning of any minute between 1 and *n*<=-<=*k*<=+<=1. If you use it on some minute *i* then Mishka will be awake during minutes *j* such that and will write down all the theorems lecturer tells.
You task is to calculate the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up. | The first line of the input contains two integer numbers *n* and *k* (1<=β€<=*k*<=β€<=*n*<=β€<=105) β the duration of the lecture in minutes and the number of minutes you can keep Mishka awake.
The second line of the input contains *n* integer numbers *a*1,<=*a*2,<=... *a**n* (1<=β€<=*a**i*<=β€<=104) β the number of theorems lecturer tells during the *i*-th minute.
The third line of the input contains *n* integer numbers *t*1,<=*t*2,<=... *t**n* (0<=β€<=*t**i*<=β€<=1) β type of Mishka's behavior at the *i*-th minute of the lecture. | Print only one integer β the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up. | [
"6 3\n1 3 5 2 5 4\n1 1 0 1 0 0\n"
] | [
"16\n"
] | In the sample case the better way is to use the secret technique at the beginning of the third minute. Then the number of theorems Mishka will be able to write down will be equal to 16. | [
{
"input": "6 3\n1 3 5 2 5 4\n1 1 0 1 0 0",
"output": "16"
},
{
"input": "5 3\n1 9999 10000 10000 10000\n0 0 0 0 0",
"output": "30000"
},
{
"input": "3 3\n10 10 10\n1 1 0",
"output": "30"
},
{
"input": "1 1\n423\n0",
"output": "423"
},
{
"input": "6 6\n1 3 5 2 5 4... | 249 | 12,288,000 | 3 | 3,219 | |
95 | Lucky Numbers | [
"dp",
"greedy"
] | B. Lucky Numbers | 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. For example, numbers 47, 7744, 474477 are super lucky and 4, 744, 467 are not.
One day Petya came across a positive integer *n*. Help him to find the least super lucky number which is not less than *n*. | The only line contains a positive integer *n* (1<=β€<=*n*<=β€<=10100000). This number doesn't have leading zeroes. | Output the least super lucky number that is more than or equal to *n*. | [
"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",
... | 404 | 2,048,000 | -1 | 3,222 |
374 | Inna and Pink Pony | [
"greedy",
"implementation"
] | null | null | Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an *n*<=Γ<=*m* chessboard, a very tasty candy and two numbers *a* and *b*.
Dima put the chessboard in front of Inna and placed the candy in position (*i*,<=*j*) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types:
- move the candy from position (*x*,<=*y*) on the board to position (*x*<=-<=*a*,<=*y*<=-<=*b*); - move the candy from position (*x*,<=*y*) on the board to position (*x*<=+<=*a*,<=*y*<=-<=*b*); - move the candy from position (*x*,<=*y*) on the board to position (*x*<=-<=*a*,<=*y*<=+<=*b*); - move the candy from position (*x*,<=*y*) on the board to position (*x*<=+<=*a*,<=*y*<=+<=*b*).
Naturally, Dima doesn't allow to move the candy beyond the chessboard borders.
Inna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (*i*,<=*j*) to one of the chessboard corners. Help them cope with the task! | The first line of the input contains six integers *n*,<=*m*,<=*i*,<=*j*,<=*a*,<=*b* (1<=β€<=*n*,<=*m*<=β€<=106;Β 1<=β€<=*i*<=β€<=*n*;Β 1<=β€<=*j*<=β€<=*m*;Β 1<=β€<=*a*,<=*b*<=β€<=106).
You can assume that the chessboard rows are numbered from 1 to *n* from top to bottom and the columns are numbered from 1 to *m* from left to right. Position (*i*,<=*j*) in the statement is a chessboard cell on the intersection of the *i*-th row and the *j*-th column. You can consider that the corners are: (1,<=*m*), (*n*,<=1), (*n*,<=*m*), (1,<=1). | In a single line print a single integer β the minimum number of moves needed to get the candy.
If Inna and the pony cannot get the candy playing by Dima's rules, print on a single line "Poor Inna and pony!" without the quotes. | [
"5 7 1 3 2 2\n",
"5 5 2 3 1 1\n"
] | [
"2\n",
"Poor Inna and pony!\n"
] | Note to sample 1:
Inna and the pony can move the candy to position (1β+β2,β3β+β2)β=β(3,β5), from there they can move it to positions (3β-β2,β5β+β2)β=β(1,β7) and (3β+β2,β5β+β2)β=β(5,β7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two. | [
{
"input": "5 7 1 3 2 2",
"output": "2"
},
{
"input": "5 5 2 3 1 1",
"output": "Poor Inna and pony!"
},
{
"input": "1 1 1 1 1 1",
"output": "0"
},
{
"input": "23000 15500 100 333 9 1",
"output": "15167"
},
{
"input": "33999 99333 33000 99000 3 9",
"output": "3... | 77 | 2,048,000 | -1 | 3,225 | |
690 | Photographs (II) | [] | null | null | Zombies seem to have become much more intelligent lately β a few have somehow wandered into the base through the automatic gate. Heidi has had to beef up security, and a new gate has been installed. Unfortunately, now the questions being asked are more complicated, and even humans have trouble answering them. Can you still program the robot army to do this reliably?
The new questions are of the following form: a grayscale photograph has been divided into several horizontal pieces, which have been arbitrarily rearranged. The task is to assemble the original image back from these pieces (somewhat like in a jigsaw puzzle). To further delay the zombies, significant Gaussian-distributed noise has been added to the image. | The input format is the same as in the previous version, except that the first line of every question now contains three space-separated numbers *h*, *w* and *k* (1<=β€<=*h*,<=*w*<=β€<=600, 2<=β€<=*k*<=β€<=16) β the height (number of rows) and width (number of columns) of the photograph and the number of pieces, respectively. The number of pieces evenly divides the height, and each piece is of the same height *h*<=/<=*k*.
Again, there is only one input file to be processed, and the same resources are provided to you as in the previous version (except that now you are given all input images in .bmp format, rather than the first 50). | Your program should print *q* lines. The *i*-th line should contain your answer for the *i*-th question: a space-separated sequence of *k* numbers Ο1,<=Ο2,<=...,<=Ο*k* such that:
- Ο is a permutation of {1,<=2,<=...,<=*k*}, that is, each number from 1 to *k* appears exactly once in Ο, - for each *j*<==<=1,<=...,<=*k*, Ο*j* is the position (index), in the original image, of the piece which is at position *j* in the input image. (See the illustration below for clarity.)
The second image from the test set. If the three pieces in the original image are numbered 1, 2, 3 from top to bottom, then the numbering in the image on the right should be 2, 3, 1. The correct answer for this image is thus 2 3 1.
Again, your answers will be accepted if they conform to this format and if at least 75% of them are correct.
Again, you may process the input locally and submit just your precomputed answers (i.e., a program which just prints your output for the input file all.in). | [] | [] | The link to download all the necessary materials is http://assets.codeforces.com/files/690/medium_contestant_package.zip | [] | 46 | 0 | 3 | 3,227 | |
426 | Sereja and Mirroring | [
"implementation"
] | null | null | Let's assume that we are given a matrix *b* of size *x*<=Γ<=*y*, let's determine the operation of mirroring matrix *b*. The mirroring of matrix *b* is a 2*x*<=Γ<=*y* matrix *c* which has the following properties:
- the upper half of matrix *c* (rows with numbers from 1 to *x*) exactly matches *b*; - the lower half of matrix *c* (rows with numbers from *x*<=+<=1 to 2*x*) is symmetric to the upper one; the symmetry line is the line that separates two halves (the line that goes in the middle, between rows *x* and *x*<=+<=1).
Sereja has an *n*<=Γ<=*m* matrix *a*. He wants to find such matrix *b*, that it can be transformed into matrix *a*, if we'll perform on it several (possibly zero) mirrorings. What minimum number of rows can such matrix contain? | The first line contains two integers, *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=100). Each of the next *n* lines contains *m* integers β the elements of matrix *a*. The *i*-th line contains integers *a**i*1,<=*a**i*2,<=...,<=*a**im* (0<=β€<=*a**ij*<=β€<=1) β the *i*-th row of the matrix *a*. | In the single line, print the answer to the problem β the minimum number of rows of matrix *b*. | [
"4 3\n0 0 1\n1 1 0\n1 1 0\n0 0 1\n",
"3 3\n0 0 0\n0 0 0\n0 0 0\n",
"8 1\n0\n1\n1\n0\n0\n1\n1\n0\n"
] | [
"2\n",
"3\n",
"2\n"
] | In the first test sample the answer is a 2βΓβ3 matrix *b*:
If we perform a mirroring operation with this matrix, we get the matrix *a* that is given in the input: | [
{
"input": "4 3\n0 0 1\n1 1 0\n1 1 0\n0 0 1",
"output": "2"
},
{
"input": "3 3\n0 0 0\n0 0 0\n0 0 0",
"output": "3"
},
{
"input": "8 1\n0\n1\n1\n0\n0\n1\n1\n0",
"output": "2"
},
{
"input": "10 4\n0 0 1 0\n0 0 1 0\n1 1 0 1\n0 0 1 1\n1 0 1 0\n1 0 1 0\n0 0 1 1\n1 1 0 1\n0 0 1 0\... | 62 | 5,222,400 | 0 | 3,230 | |
792 | New Bus Route | [
"implementation",
"sortings"
] | null | null | There are *n* cities situated along the main road of Berland. Cities are represented by their coordinates β integer numbers *a*1,<=*a*2,<=...,<=*a**n*. All coordinates are pairwise distinct.
It is possible to get from one city to another only by bus. But all buses and roads are very old, so the Minister of Transport decided to build a new bus route. The Minister doesn't want to spend large amounts of money β he wants to choose two cities in such a way that the distance between them is minimal possible. The distance between two cities is equal to the absolute value of the difference between their coordinates.
It is possible that there are multiple pairs of cities with minimal possible distance, so the Minister wants to know the quantity of such pairs.
Your task is to write a program that will calculate the minimal possible distance between two pairs of cities and the quantity of pairs which have this distance. | The first line contains one integer number *n* (2<=β€<=*n*<=β€<=2Β·105).
The second line contains *n* integer numbers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=β€<=*a**i*<=β€<=109). All numbers *a**i* are pairwise distinct. | Print two integer numbers β the minimal distance and the quantity of pairs with this distance. | [
"4\n6 -3 0 4\n",
"3\n-2 0 2\n"
] | [
"2 1\n",
"2 2\n"
] | In the first example the distance between the first city and the fourth city is |4β-β6|β=β2, and it is the only pair with this distance. | [
{
"input": "4\n6 -3 0 4",
"output": "2 1"
},
{
"input": "3\n-2 0 2",
"output": "2 2"
},
{
"input": "2\n1 2",
"output": "1 1"
},
{
"input": "2\n1000000000 -1000000000",
"output": "2000000000 1"
},
{
"input": "5\n-979619606 -979619602 -979619604 -979619605 -97961960... | 1,000 | 12,902,400 | 0 | 3,235 | |
53 | Physical Education | [
"sortings"
] | D. Physical Education | 2 | 256 | Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: *a*1,<=*a*2,<=...,<=*a**n*, where *a**i* is the height of the *i*-th student in the line and *n* is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: *b*1,<=*b*2,<=...,<=*b**n*, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: *a*1,<=*a*2,<=...,<=*a**n*. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=300) which is the number of students. The second line contains *n* space-separated integers *a**i* (1<=β€<=*a**i*<=β€<=109) which represent the height of the student occupying the *i*-th place must possess. The third line contains *n* space-separated integers *b**i* (1<=β€<=*b**i*<=β€<=109) which represent the height of the student occupying the *i*-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. *a* and *b* coincide as multisets. | In the first line print an integer *k* (0<=β€<=*k*<=β€<=106) which is the number of moves. It is not required to minimize *k* but it must not exceed 106. Then print *k* lines each containing two space-separated integers. Line *p**i*, *p**i*<=+<=1 (1<=β€<=*p**i*<=β€<=*n*<=-<=1) means that Vasya should swap students occupying places *p**i* and *p**i*<=+<=1. | [
"4\n1 2 3 2\n3 2 1 2\n",
"2\n1 100500\n1 100500\n"
] | [
"4\n2 3\n1 2\n3 4\n2 3\n",
"0\n"
] | none | [
{
"input": "4\n1 2 3 2\n3 2 1 2",
"output": "4\n2 3\n1 2\n3 4\n2 3"
},
{
"input": "2\n1 100500\n1 100500",
"output": "0"
},
{
"input": "3\n652586118 652586118 652586118\n652586118 652586118 652586118",
"output": "3\n2 3\n1 2\n2 3"
},
{
"input": "4\n681106577 681106577 6750771... | 124 | 512,000 | 3.968046 | 3,249 |
276 | Lunch Rush | [
"implementation"
] | null | null | Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly *k* time units for the lunch break.
The Rabbits have a list of *n* restaurants to lunch in: the *i*-th restaurant is characterized by two integers *f**i* and *t**i*. Value *t**i* shows the time the Rabbits need to lunch in the *i*-th restaurant. If time *t**i* exceeds the time *k* that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal *f**i*<=-<=(*t**i*<=-<=*k*). Otherwise, the Rabbits get exactly *f**i* units of joy.
Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value. | The first line contains two space-separated integers β *n* (1<=β€<=*n*<=β€<=104) and *k* (1<=β€<=*k*<=β€<=109) β the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next *n* lines contains two space-separated integers β *f**i* (1<=β€<=*f**i*<=β€<=109) and *t**i* (1<=β€<=*t**i*<=β€<=109) β the characteristics of the *i*-th restaurant. | In a single line print a single integer β the maximum joy value that the Rabbits will get from the lunch. | [
"2 5\n3 3\n4 5\n",
"4 6\n5 8\n3 6\n2 3\n2 2\n",
"1 5\n1 7\n"
] | [
"4\n",
"3\n",
"-1\n"
] | none | [
{
"input": "2 5\n3 3\n4 5",
"output": "4"
},
{
"input": "4 6\n5 8\n3 6\n2 3\n2 2",
"output": "3"
},
{
"input": "1 5\n1 7",
"output": "-1"
},
{
"input": "4 9\n10 13\n4 18\n13 3\n10 6",
"output": "13"
},
{
"input": "1 1\n1 1000000000",
"output": "-999999998"
}... | 186 | 0 | 3 | 3,251 | |
289 | Polo the Penguin and Matrix | [
"brute force",
"dp",
"implementation",
"sortings",
"ternary search"
] | null | null | Little penguin Polo has an *n*<=Γ<=*m* matrix, consisting of integers. Let's index the matrix rows from 1 to *n* from top to bottom and let's index the columns from 1 to *m* from left to right. Let's represent the matrix element on the intersection of row *i* and column *j* as *a**ij*.
In one move the penguin can add or subtract number *d* from some matrix element. Find the minimum number of moves needed to make all matrix elements equal. If the described plan is impossible to carry out, say so. | The first line contains three integers *n*, *m* and *d* (1<=β€<=*n*,<=*m*<=β€<=100,<=1<=β€<=*d*<=β€<=104) β the matrix sizes and the *d* parameter. Next *n* lines contain the matrix: the *j*-th integer in the *i*-th row is the matrix element *a**ij* (1<=β€<=*a**ij*<=β€<=104). | In a single line print a single integer β the minimum number of moves the penguin needs to make all matrix elements equal. If that is impossible, print "-1" (without the quotes). | [
"2 2 2\n2 4\n6 8\n",
"1 2 7\n6 7\n"
] | [
"4\n",
"-1\n"
] | none | [
{
"input": "2 2 2\n2 4\n6 8",
"output": "4"
},
{
"input": "1 2 7\n6 7",
"output": "-1"
},
{
"input": "3 2 1\n5 7\n1 2\n5 100",
"output": "104"
},
{
"input": "3 3 3\n5 8 5\n11 11 17\n14 5 2",
"output": "12"
},
{
"input": "3 3 3\n5 8 5\n11 11 17\n14 5 3",
"outpu... | 404 | 2,150,400 | -1 | 3,258 | |
909 | Segments | [
"constructive algorithms",
"math"
] | null | null | You are given an integer *N*. Consider all possible segments on the coordinate axis with endpoints at integer points with coordinates between 0 and *N*, inclusive; there will be of them.
You want to draw these segments in several layers so that in each layer the segments don't overlap (they might touch at the endpoints though). You can not move the segments to a different location on the coordinate axis.
Find the minimal number of layers you have to use for the given *N*. | The only input line contains a single integer *N* (1<=β€<=*N*<=β€<=100). | Output a single integer - the minimal number of layers required to draw the segments for the given *N*. | [
"2\n",
"3\n",
"4\n"
] | [
"2\n",
"4\n",
"6\n"
] | As an example, here are the segments and their optimal arrangement into layers for *N*β=β4. | [
{
"input": "2",
"output": "2"
},
{
"input": "3",
"output": "4"
},
{
"input": "4",
"output": "6"
},
{
"input": "21",
"output": "121"
},
{
"input": "100",
"output": "2550"
},
{
"input": "1",
"output": "1"
},
{
"input": "5",
"output": "9"
... | 93 | 1,433,600 | 3 | 3,263 | |
53 | Little Frog | [
"constructive algorithms"
] | C. Little Frog | 2 | 256 | Once upon a time a little frog whose name was Vasya decided to travel around his home swamp. Overall there are *n* mounds on the swamp, located on one line. The distance between the neighboring mounds is one meter. Vasya wants to visit all the mounds in one day; besides, he wants to visit each one exactly once. For that he makes a route plan, to decide the order in which to jump on the mounds. Vasya can pick any mound as the first one. He thinks it boring to jump two times at the same distance. That's why he wants any two jumps on his route to have different lengths. Help Vasya the Frog and make the plan for him. | The single line contains a number *n* (1<=β€<=*n*<=β€<=104) which is the number of mounds. | Print *n* integers *p**i* (1<=β€<=*p**i*<=β€<=*n*) which are the frog's route plan.
- All the *p**i*'s should be mutually different. - All the |*p**i*β*p**i*<=+<=1|'s should be mutually different (1<=β€<=*i*<=β€<=*n*<=-<=1).
If there are several solutions, output any. | [
"2\n",
"3\n"
] | [
"1 2 ",
"1 3 2 "
] | none | [
{
"input": "2",
"output": "1 2 "
},
{
"input": "3",
"output": "1 3 2 "
},
{
"input": "4",
"output": "1 4 2 3 "
},
{
"input": "5",
"output": "1 5 2 4 3 "
},
{
"input": "6",
"output": "1 6 2 5 3 4 "
},
{
"input": "1",
"output": "1 "
},
{
"inp... | 92 | 0 | 3.977 | 3,272 |
794 | Cutting Carrot | [
"geometry",
"math"
] | null | null | Igor the analyst has adopted *n* little bunnies. As we all know, bunnies love carrots. Thus, Igor has bought a carrot to be shared between his bunnies. Igor wants to treat all the bunnies equally, and thus he wants to cut the carrot into *n* pieces of equal area.
Formally, the carrot can be viewed as an isosceles triangle with base length equal to 1 and height equal to *h*. Igor wants to make *n*<=-<=1 cuts parallel to the base to cut the carrot into *n* pieces. He wants to make sure that all *n* pieces have the same area. Can you help Igor determine where to cut the carrot so that each piece have equal area? | The first and only line of input contains two space-separated integers, *n* and *h* (2<=β€<=*n*<=β€<=1000, 1<=β€<=*h*<=β€<=105). | The output should contain *n*<=-<=1 real numbers *x*1,<=*x*2,<=...,<=*x**n*<=-<=1. The number *x**i* denotes that the *i*-th cut must be made *x**i* units away from the apex of the carrot. In addition, 0<=<<=*x*1<=<<=*x*2<=<<=...<=<<=*x**n*<=-<=1<=<<=*h* must hold.
Your output will be considered correct if absolute or relative error of every number in your output doesn't exceed 10<=-<=6.
Formally, let your answer be *a*, and the jury's answer be *b*. Your answer is considered correct if . | [
"3 2\n",
"2 100000\n"
] | [
"1.154700538379 1.632993161855\n",
"70710.678118654752\n"
] | Definition of isosceles triangle: [https://en.wikipedia.org/wiki/Isosceles_triangle](https://en.wikipedia.org/wiki/Isosceles_triangle). | [
{
"input": "3 2",
"output": "1.154700538379 1.632993161855"
},
{
"input": "2 100000",
"output": "70710.678118654752"
},
{
"input": "1000 100000",
"output": "3162.277660168379 4472.135954999579 5477.225575051661 6324.555320336759 7071.067811865475 7745.966692414834 8366.600265340755 8... | 30 | 0 | -1 | 3,281 | |
357 | Flag Day | [
"constructive algorithms",
"implementation"
] | null | null | In Berland, there is the national holiday coming β the Flag Day. In the honor of this event the president of the country decided to make a big dance party and asked your agency to organize it. He has several conditions:
- overall, there must be *m* dances;- exactly three people must take part in each dance;- each dance must have one dancer in white clothes, one dancer in red clothes and one dancer in blue clothes (these are the colors of the national flag of Berland).
The agency has *n* dancers, and their number can be less than 3*m*. That is, some dancers will probably have to dance in more than one dance. All of your dancers must dance on the party. However, if some dance has two or more dancers from a previous dance, then the current dance stops being spectacular. Your agency cannot allow that to happen, so each dance has at most one dancer who has danced in some previous dance.
You considered all the criteria and made the plan for the *m* dances: each dance had three dancers participating in it. Your task is to determine the clothes color for each of the *n* dancers so that the President's third condition fulfilled: each dance must have a dancer in white, a dancer in red and a dancer in blue. The dancers cannot change clothes between the dances. | The first line contains two space-separated integers *n* (3<=β€<=*n*<=β€<=105) and *m* (1<=β€<=*m*<=β€<=105) β the number of dancers and the number of dances, correspondingly. Then *m* lines follow, describing the dances in the order of dancing them. The *i*-th line contains three distinct integers β the numbers of the dancers that take part in the *i*-th dance. The dancers are numbered from 1 to *n*. Each dancer takes part in at least one dance. | Print *n* space-separated integers: the *i*-th number must represent the color of the *i*-th dancer's clothes (1 for white, 2 for red, 3 for blue). If there are multiple valid solutions, print any of them. It is guaranteed that at least one solution exists. | [
"7 3\n1 2 3\n1 4 5\n4 6 7\n",
"9 3\n3 6 9\n2 5 8\n1 4 7\n",
"5 2\n4 1 5\n3 1 2\n"
] | [
"1 2 3 3 2 2 1 \n",
"1 1 1 2 2 2 3 3 3 \n",
"2 3 1 1 3 \n"
] | none | [
{
"input": "7 3\n1 2 3\n1 4 5\n4 6 7",
"output": "1 2 3 3 2 2 1 "
},
{
"input": "9 3\n3 6 9\n2 5 8\n1 4 7",
"output": "1 1 1 2 2 2 3 3 3 "
},
{
"input": "5 2\n4 1 5\n3 1 2",
"output": "2 3 1 1 3 "
},
{
"input": "14 5\n1 5 3\n13 10 11\n6 3 8\n14 9 2\n7 4 12",
"output": "1 ... | 31 | 307,200 | 0 | 3,283 | |
724 | Batch Sort | [
"brute force",
"greedy",
"implementation",
"math"
] | null | null | You are given a table consisting of *n* rows and *m* columns.
Numbers in each row form a permutation of integers from 1 to *m*.
You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to *n*<=+<=1 actions in total. Operations can be performed in any order.
You have to check whether it's possible to obtain the identity permutation 1,<=2,<=...,<=*m* in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order. | The first line of the input contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=20)Β β the number of rows and the number of columns in the given table.
Each of next *n* lines contains *m* integersΒ β elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to *m*. | If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). | [
"2 4\n1 3 2 4\n1 3 4 2\n",
"4 4\n1 2 3 4\n2 3 4 1\n3 4 1 2\n4 1 2 3\n",
"3 6\n2 1 3 4 5 6\n1 2 4 3 5 6\n1 2 3 4 6 5\n"
] | [
"YES\n",
"NO\n",
"YES\n"
] | In the first sample, one can act in the following way:
1. Swap second and third columns. Now the table is <center class="tex-equation">1Β 2Β 3Β 4</center> <center class="tex-equation">1Β 4Β 3Β 2</center> 1. In the second row, swap the second and the fourth elements. Now the table is <center class="tex-equation">1Β 2Β 3Β 4</center> <center class="tex-equation">1Β 2Β 3Β 4</center> | [
{
"input": "2 4\n1 3 2 4\n1 3 4 2",
"output": "YES"
},
{
"input": "4 4\n1 2 3 4\n2 3 4 1\n3 4 1 2\n4 1 2 3",
"output": "NO"
},
{
"input": "3 6\n2 1 3 4 5 6\n1 2 4 3 5 6\n1 2 3 4 6 5",
"output": "YES"
},
{
"input": "3 10\n1 2 3 4 5 6 7 10 9 8\n5 2 3 4 1 6 7 8 9 10\n1 2 3 4 5 6... | 109 | 4,915,200 | 3 | 3,288 | |
977 | Less or Equal | [
"sortings"
] | null | null | You are given a sequence of integers of length $n$ and integer number $k$. You should print any integer number $x$ in the range of $[1; 10^9]$ (i.e. $1 \le x \le 10^9$) such that exactly $k$ elements of given sequence are less than or equal to $x$.
Note that the sequence can contain equal elements.
If there is no such $x$, print "-1" (without quotes). | The first line of the input contains integer numbers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $0 \le k \le n$). The second line of the input contains $n$ integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β the sequence itself. | Print any integer number $x$ from range $[1; 10^9]$ such that exactly $k$ elements of given sequence is less or equal to $x$.
If there is no such $x$, print "-1" (without quotes). | [
"7 4\n3 7 5 1 10 3 20\n",
"7 2\n3 7 5 1 10 3 20\n"
] | [
"6",
"-1\n"
] | In the first example $5$ is also a valid answer because the elements with indices $[1, 3, 4, 6]$ is less than or equal to $5$ and obviously less than or equal to $6$.
In the second example you cannot choose any number that only $2$ elements of the given sequence will be less than or equal to this number because $3$ elements of the given sequence will be also less than or equal to this number. | [
{
"input": "7 4\n3 7 5 1 10 3 20",
"output": "5"
},
{
"input": "7 2\n3 7 5 1 10 3 20",
"output": "-1"
},
{
"input": "1 0\n1",
"output": "-1"
},
{
"input": "1 0\n2",
"output": "1"
},
{
"input": "1 1\n1000000000",
"output": "1000000000"
},
{
"input": "3 ... | 46 | 0 | 0 | 3,290 | |
526 | Om Nom and Necklace | [
"hashing",
"string suffix structures",
"strings"
] | null | null | One day Om Nom found a thread with *n* beads of different colors. He decided to cut the first several beads from this thread to make a bead necklace and present it to his girlfriend Om Nelly.
Om Nom knows that his girlfriend loves beautiful patterns. That's why he wants the beads on the necklace to form a regular pattern. A sequence of beads *S* is regular if it can be represented as *S*<==<=*A*<=+<=*B*<=+<=*A*<=+<=*B*<=+<=*A*<=+<=...<=+<=*A*<=+<=*B*<=+<=*A*, where *A* and *B* are some bead sequences, "<=+<=" is the concatenation of sequences, there are exactly 2*k*<=+<=1 summands in this sum, among which there are *k*<=+<=1 "*A*" summands and *k* "*B*" summands that follow in alternating order. Om Nelly knows that her friend is an eager mathematician, so she doesn't mind if *A* or *B* is an empty sequence.
Help Om Nom determine in which ways he can cut off the first several beads from the found thread (at least one; probably, all) so that they form a regular pattern. When Om Nom cuts off the beads, he doesn't change their order. | The first line contains two integers *n*, *k* (1<=β€<=*n*,<=*k*<=β€<=1<=000<=000) β the number of beads on the thread that Om Nom found and number *k* from the definition of the regular sequence above.
The second line contains the sequence of *n* lowercase Latin letters that represent the colors of the beads. Each color corresponds to a single letter. | Print a string consisting of *n* zeroes and ones. Position *i* (1<=β€<=*i*<=β€<=*n*) must contain either number one if the first *i* beads on the thread form a regular sequence, or a zero otherwise. | [
"7 2\nbcabcab\n",
"21 2\nababaababaababaababaa\n"
] | [
"0000011",
"000110000111111000011"
] | In the first sample test a regular sequence is both a sequence of the first 6 beads (we can take *A*β=β"", *B*β=β"bca"), and a sequence of the first 7 beads (we can take *A*β=β"b", *B*β=β"ca").
In the second sample test, for example, a sequence of the first 13 beads is regular, if we take *A*β=β"aba", *B*β=β"ba". | [
{
"input": "7 2\nbcabcab",
"output": "0000011"
},
{
"input": "21 2\nababaababaababaababaa",
"output": "000110000111111000011"
},
{
"input": "321 2\nabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaab... | 140 | 22,528,000 | 3 | 3,291 | |
48 | Rock-paper-scissors | [
"implementation",
"schedules"
] | A. Rock-paper-scissors | 2 | 256 | Uncle Fyodor, Matroskin the Cat and Sharic the Dog live their simple but happy lives in Prostokvashino. Sometimes they receive parcels from Uncle Fyodorβs parents and sometimes from anonymous benefactors, in which case it is hard to determine to which one of them the package has been sent. A photographic rifle is obviously for Sharic who loves hunting and fish is for Matroskin, but for whom was a new video game console meant? Every one of the three friends claimed that the present is for him and nearly quarreled. Uncle Fyodor had an idea how to solve the problem justly: they should suppose that the console was sent to all three of them and play it in turns. Everybody got relieved but then yet another burning problem popped up β who will play first? This time Matroskin came up with a brilliant solution, suggesting the most fair way to find it out: play rock-paper-scissors together. The rules of the game are very simple. On the count of three every player shows a combination with his hand (or paw). The combination corresponds to one of three things: a rock, scissors or paper. Some of the gestures win over some other ones according to well-known rules: the rock breaks the scissors, the scissors cut the paper, and the paper gets wrapped over the stone. Usually there are two players. Yet there are three friends, thatβs why they decided to choose the winner like that: If someone shows the gesture that wins over the other two players, then that player wins. Otherwise, another game round is required. Write a program that will determine the winner by the gestures they have shown. | The first input line contains the name of the gesture that Uncle Fyodor showed, the second line shows which gesture Matroskin showed and the third line shows Sharicβs gesture. | Print "F" (without quotes) if Uncle Fyodor wins. Print "M" if Matroskin wins and "S" if Sharic wins. If it is impossible to find the winner, print "?". | [
"rock\nrock\nrock\n",
"paper\nrock\nrock\n",
"scissors\nrock\nrock\n",
"scissors\npaper\nrock\n"
] | [
"?\n",
"F\n",
"?\n",
"?\n"
] | none | [
{
"input": "rock\nrock\nrock",
"output": "?"
},
{
"input": "paper\nrock\nrock",
"output": "F"
},
{
"input": "scissors\nrock\nrock",
"output": "?"
},
{
"input": "scissors\npaper\nrock",
"output": "?"
},
{
"input": "paper\npaper\nrock",
"output": "?"
},
{
... | 92 | 4,505,600 | 3.968608 | 3,321 |
311 | The Closest Pair | [
"constructive algorithms",
"implementation"
] | null | null | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given *n* points in the plane, find a pair of points between which the distance is minimized. Distance between (*x*1,<=*y*1) and (*x*2,<=*y*2) is .
The pseudo code of the unexpected code is as follows:
Here, *tot* can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, *tot* should not be more than *k* in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? | A single line which contains two space-separated integers *n* and *k* (2<=β€<=*n*<=β€<=2000, 1<=β€<=*k*<=β€<=109). | If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print *n* lines, and the *i*-th line contains two integers *x**i*,<=*y**i* (|*x**i*|,<=|*y**i*|<=β€<=109) representing the coordinates of the *i*-th point.
The conditions below must be held:
- All the points must be distinct. - |*x**i*|,<=|*y**i*|<=β€<=109. - After running the given code, the value of *tot* should be larger than *k*. | [
"4 3\n",
"2 100\n"
] | [
"0 0\n0 1\n1 0\n1 1\n",
"no solution\n"
] | none | [
{
"input": "4 3",
"output": "0 0\n0 1\n1 0\n1 1"
},
{
"input": "2 100",
"output": "no solution"
},
{
"input": "5 6",
"output": "0 0\n0 1\n0 2\n0 3\n0 4"
},
{
"input": "8 20",
"output": "0 0\n0 1\n0 2\n0 3\n0 4\n0 5\n0 6\n0 7"
},
{
"input": "6 15",
"output": "n... | 109 | 0 | 0 | 3,329 | |
0 | none | [
"none"
] | null | null | Two participants are each given a pair of distinct numbers from 1 to 9 such that there's exactly one number that is present in both pairs. They want to figure out the number that matches by using a communication channel you have access to without revealing it to you.
Both participants communicated to each other a set of pairs of numbers, that includes the pair given to them. Each pair in the communicated sets comprises two different numbers.
Determine if you can with certainty deduce the common number, or if you can determine with certainty that both participants know the number but you do not. | The first line contains two integers $n$ and $m$ ($1 \le n, m \le 12$) β the number of pairs the first participant communicated to the second and vice versa.
The second line contains $n$ pairs of integers, each between $1$ and $9$, β pairs of numbers communicated from first participant to the second.
The third line contains $m$ pairs of integers, each between $1$ and $9$, β pairs of numbers communicated from the second participant to the first.
All pairs within each set are distinct (in particular, if there is a pair $(1,2)$, there will be no pair $(2,1)$ within the same set), and no pair contains the same number twice.
It is guaranteed that the two sets do not contradict the statements, in other words, there is pair from the first set and a pair from the second set that share exactly one number. | If you can deduce the shared number with certainty, print that number.
If you can with certainty deduce that both participants know the shared number, but you do not know it, print $0$.
Otherwise print $-1$. | [
"2 2\n1 2 3 4\n1 5 3 4\n",
"2 2\n1 2 3 4\n1 5 6 4\n",
"2 3\n1 2 4 5\n1 2 1 3 2 3\n"
] | [
"1\n",
"0\n",
"-1\n"
] | In the first example the first participant communicated pairs $(1,2)$ and $(3,4)$, and the second communicated $(1,5)$, $(3,4)$. Since we know that the actual pairs they received share exactly one number, it can't be that they both have $(3,4)$. Thus, the first participant has $(1,2)$ and the second has $(1,5)$, and at this point you already know the shared number is $1$.
In the second example either the first participant has $(1,2)$ and the second has $(1,5)$, or the first has $(3,4)$ and the second has $(6,4)$. In the first case both of them know the shared number is $1$, in the second case both of them know the shared number is $4$. You don't have enough information to tell $1$ and $4$ apart.
In the third case if the first participant was given $(1,2)$, they don't know what the shared number is, since from their perspective the second participant might have been given either $(1,3)$, in which case the shared number is $1$, or $(2,3)$, in which case the shared number is $2$. While the second participant does know the number with certainty, neither you nor the first participant do, so the output is $-1$. | [
{
"input": "2 2\n1 2 3 4\n1 5 3 4",
"output": "1"
},
{
"input": "2 2\n1 2 3 4\n1 5 6 4",
"output": "0"
},
{
"input": "2 3\n1 2 4 5\n1 2 1 3 2 3",
"output": "-1"
},
{
"input": "2 1\n1 2 1 3\n1 2",
"output": "1"
},
{
"input": "4 4\n1 2 3 4 5 6 7 8\n2 3 4 5 6 7 8 1",... | 77 | 0 | 0 | 3,331 | |
919 | Supermarket | [
"brute force",
"greedy",
"implementation"
] | null | null | We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that $a$ yuan for $b$ kilos (You don't need to care about what "yuan" is), the same as $a/b$ yuan for a kilo.
Now imagine you'd like to buy $m$ kilos of apples. You've asked $n$ supermarkets and got the prices. Find the minimum cost for those apples.
You can assume that there are enough apples in all supermarkets. | The first line contains two positive integers $n$ and $m$ ($1 \leq n \leq 5\,000$, $1 \leq m \leq 100$), denoting that there are $n$ supermarkets and you want to buy $m$ kilos of apples.
The following $n$ lines describe the information of the supermarkets. Each line contains two positive integers $a, b$ ($1 \leq a, b \leq 100$), denoting that in this supermarket, you are supposed to pay $a$ yuan for $b$ kilos of apples. | The only line, denoting the minimum cost for $m$ kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed $10^{-6}$.
Formally, let your answer be $x$, and the jury's answer be $y$. Your answer is considered correct if $\frac{|x - y|}{\max{(1, |y|)}} \le 10^{-6}$. | [
"3 5\n1 2\n3 4\n1 3\n",
"2 1\n99 100\n98 99\n"
] | [
"1.66666667\n",
"0.98989899\n"
] | In the first sample, you are supposed to buy $5$ kilos of apples in supermarket $3$. The cost is $5/3$ yuan.
In the second sample, you are supposed to buy $1$ kilo of apples in supermarket $2$. The cost is $98/99$ yuan. | [
{
"input": "3 5\n1 2\n3 4\n1 3",
"output": "1.66666667"
},
{
"input": "2 1\n99 100\n98 99",
"output": "0.98989899"
},
{
"input": "50 37\n78 49\n96 4\n86 62\n28 4\n19 2\n79 43\n79 92\n95 35\n33 60\n54 84\n90 25\n2 25\n53 21\n86 52\n72 25\n6 78\n41 46\n3 68\n42 89\n33 35\n57 43\n99 45\n1 8... | 46 | 0 | 3 | 3,334 | |
437 | The Child and Homework | [
"implementation"
] | null | null | Once upon a time a child got a test consisting of multiple-choice questions as homework. A multiple-choice question consists of four choices: A, B, C and D. Each choice has a description, and the child should find out the only one that is correct.
Fortunately the child knows how to solve such complicated test. The child will follow the algorithm:
- If there is some choice whose description at least twice shorter than all other descriptions, or at least twice longer than all other descriptions, then the child thinks the choice is great. - If there is exactly one great choice then the child chooses it. Otherwise the child chooses C (the child think it is the luckiest choice).
You are given a multiple-choice questions, can you predict child's choose? | The first line starts with "A." (without quotes), then followed the description of choice A. The next three lines contains the descriptions of the other choices in the same format. They are given in order: B, C, D. Please note, that the description goes after prefix "X.", so the prefix mustn't be counted in description's length.
Each description is non-empty and consists of at most 100 characters. Each character can be either uppercase English letter or lowercase English letter, or "_". | Print a single line with the child's choice: "A", "B", "C" or "D" (without quotes). | [
"A.VFleaKing_is_the_author_of_this_problem\nB.Picks_is_the_author_of_this_problem\nC.Picking_is_the_author_of_this_problem\nD.Ftiasch_is_cute\n",
"A.ab\nB.abcde\nC.ab\nD.abc\n",
"A.c\nB.cc\nC.c\nD.c\n"
] | [
"D\n",
"C\n",
"B\n"
] | In the first sample, the first choice has length 39, the second one has length 35, the third one has length 37, and the last one has length 15. The choice D (length 15) is twice shorter than all other choices', so it is great choice. There is no other great choices so the child will choose D.
In the second sample, no choice is great, so the child will choose the luckiest choice C.
In the third sample, the choice B (length 2) is twice longer than all other choices', so it is great choice. There is no other great choices so the child will choose B. | [
{
"input": "A.VFleaKing_is_the_author_of_this_problem\nB.Picks_is_the_author_of_this_problem\nC.Picking_is_the_author_of_this_problem\nD.Ftiasch_is_cute",
"output": "D"
},
{
"input": "A.ab\nB.abcde\nC.ab\nD.abc",
"output": "C"
},
{
"input": "A.c\nB.cc\nC.c\nD.c",
"output": "B"
},
... | 62 | 0 | 3 | 3,341 | |
894 | Marco and GCD Sequence | [
"constructive algorithms",
"math"
] | null | null | In a dream Marco met an elderly man with a pair of black glasses. The man told him the key to immortality and then disappeared with the wind of time.
When he woke up, he only remembered that the key was a sequence of positive integers of some length *n*, but forgot the exact sequence. Let the elements of the sequence be *a*1,<=*a*2,<=...,<=*a**n*. He remembered that he calculated *gcd*(*a**i*,<=*a**i*<=+<=1,<=...,<=*a**j*) for every 1<=β€<=*i*<=β€<=*j*<=β€<=*n* and put it into a set *S*. *gcd* here means the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor).
Note that even if a number is put into the set *S* twice or more, it only appears once in the set.
Now Marco gives you the set *S* and asks you to help him figure out the initial sequence. If there are many solutions, print any of them. It is also possible that there are no sequences that produce the set *S*, in this case print -1. | The first line contains a single integer *m* (1<=β€<=*m*<=β€<=1000)Β β the size of the set *S*.
The second line contains *m* integers *s*1,<=*s*2,<=...,<=*s**m* (1<=β€<=*s**i*<=β€<=106)Β β the elements of the set *S*. It's guaranteed that the elements of the set are given in strictly increasing order, that means *s*1<=<<=*s*2<=<<=...<=<<=*s**m*. | If there is no solution, print a single line containing -1.
Otherwise, in the first line print a single integer *n* denoting the length of the sequence, *n* should not exceed 4000.
In the second line print *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=106)Β β the sequence.
We can show that if a solution exists, then there is a solution with *n* not exceeding 4000 and *a**i* not exceeding 106.
If there are multiple solutions, print any of them. | [
"4\n2 4 6 12\n",
"2\n2 3\n"
] | [
"3\n4 6 12",
"-1\n"
] | In the first example 2β=β*gcd*(4,β6), the other elements from the set appear in the sequence, and we can show that there are no values different from 2, 4, 6 and 12 among *gcd*(*a*<sub class="lower-index">*i*</sub>,β*a*<sub class="lower-index">*i*β+β1</sub>,β...,β*a*<sub class="lower-index">*j*</sub>) for every 1ββ€β*i*ββ€β*j*ββ€β*n*. | [
{
"input": "4\n2 4 6 12",
"output": "7\n2 2 4 2 6 2 12"
},
{
"input": "2\n2 3",
"output": "-1"
},
{
"input": "2\n1 6",
"output": "3\n1 1 6"
},
{
"input": "3\n1 2 7",
"output": "5\n1 1 2 1 7"
},
{
"input": "1\n1",
"output": "1\n1"
},
{
"input": "2\n1 10... | 109 | 2,457,600 | 3 | 3,350 | |
622 | The Time | [
"implementation"
] | null | null | You are given the current time in 24-hour format hh:mm. Find and print the time after *a* minutes.
Note that you should find only the time after *a* minutes, see the examples to clarify the problem statement.
You can read more about 24-hour format here [https://en.wikipedia.org/wiki/24-hour_clock](https://en.wikipedia.org/wiki/24-hour_clock). | The first line contains the current time in the format hh:mm (0<=β€<=*hh*<=<<=24,<=0<=β€<=*mm*<=<<=60). The hours and the minutes are given with two digits (the hours or the minutes less than 10 are given with the leading zeroes).
The second line contains integer *a* (0<=β€<=*a*<=β€<=104) β the number of the minutes passed. | The only line should contain the time after *a* minutes in the format described in the input. Note that you should print exactly two digits for the hours and the minutes (add leading zeroes to the numbers if needed).
See the examples to check the input/output format. | [
"23:59\n10\n",
"20:20\n121\n",
"10:10\n0\n"
] | [
"00:09\n",
"22:21\n",
"10:10\n"
] | none | [
{
"input": "23:59\n10",
"output": "00:09"
},
{
"input": "20:20\n121",
"output": "22:21"
},
{
"input": "10:10\n0",
"output": "10:10"
},
{
"input": "12:34\n10000",
"output": "11:14"
},
{
"input": "00:00\n10000",
"output": "22:40"
},
{
"input": "00:00\n14... | 46 | 0 | 3 | 3,351 | |
70 | Cookies | [
"math"
] | A. Cookies | 1 | 256 | Fangy collects cookies. Once he decided to take a box and put cookies into it in some way. If we take a square *k*<=Γ<=*k* in size, divided into blocks 1<=Γ<=1 in size and paint there the main diagonal together with cells, which lie above it, then the painted area will be equal to the area occupied by one cookie *k* in size. Fangy also has a box with a square base 2*n*<=Γ<=2*n*, divided into blocks 1<=Γ<=1 in size. In a box the cookies should not overlap, and they should not be turned over or rotated. See cookies of sizes 2 and 4 respectively on the figure:
To stack the cookies the little walrus uses the following algorithm. He takes out of the repository the largest cookie which can fit in some place in the box and puts it there. Everything could be perfect but alas, in the repository the little walrus has infinitely many cookies of size 2 and larger, and there are no cookies of size 1, therefore, empty cells will remain in the box. Fangy wants to know how many empty cells will be left in the end. | The first line contains a single integer *n* (0<=β€<=*n*<=β€<=1000). | Print the single number, equal to the number of empty cells in the box. The answer should be printed modulo 106<=+<=3. | [
"3\n"
] | [
"9"
] | If the box possesses the base of 2<sup class="upper-index">3</sup>βΓβ2<sup class="upper-index">3</sup> (as in the example), then the cookies will be put there in the following manner: | [
{
"input": "3",
"output": "9"
},
{
"input": "1",
"output": "1"
},
{
"input": "2",
"output": "3"
},
{
"input": "4",
"output": "27"
},
{
"input": "6",
"output": "243"
},
{
"input": "11",
"output": "59049"
},
{
"input": "14",
"output": "59... | 62 | 5,529,600 | 3.9587 | 3,356 |
590 | Three States | [
"dfs and similar",
"graphs",
"shortest paths"
] | null | null | The famous global economic crisis is approaching rapidly, so the states of Berman, Berance and Bertaly formed an alliance and allowed the residents of all member states to freely pass through the territory of any of them. In addition, it was decided that a road between the states should be built to guarantee so that one could any point of any country can be reached from any point of any other State.
Since roads are always expensive, the governments of the states of the newly formed alliance asked you to help them assess the costs. To do this, you have been issued a map that can be represented as a rectangle table consisting of *n* rows and *m* columns. Any cell of the map either belongs to one of three states, or is an area where it is allowed to build a road, or is an area where the construction of the road is not allowed. A cell is called passable, if it belongs to one of the states, or the road was built in this cell. From any passable cells you can move up, down, right and left, if the cell that corresponds to the movement exists and is passable.
Your task is to construct a road inside a minimum number of cells, so that it would be possible to get from any cell of any state to any cell of any other state using only passable cells.
It is guaranteed that initially it is possible to reach any cell of any state from any cell of this state, moving only along its cells. It is also guaranteed that for any state there is at least one cell that belongs to it. | The first line of the input contains the dimensions of the map *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=1000)Β β the number of rows and columns respectively.
Each of the next *n* lines contain *m* characters, describing the rows of the map. Digits from 1 to 3 represent the accessory to the corresponding state. The character '.' corresponds to the cell where it is allowed to build a road and the character '#' means no construction is allowed in this cell. | Print a single integerΒ β the minimum number of cells you need to build a road inside in order to connect all the cells of all states. If such a goal is unachievable, print -1. | [
"4 5\n11..2\n#..22\n#.323\n.#333",
"1 5\n1#2#3\n"
] | [
"2",
"-1\n"
] | none | [
{
"input": "4 5\n11..2\n#..22\n#.323\n.#333",
"output": "2"
},
{
"input": "1 5\n1#2#3",
"output": "-1"
},
{
"input": "3 4\n.2..\n...3\n.1#.",
"output": "2"
},
{
"input": "10 10\n##.#..#.#2\n...###....\n#..#....##\n.....#....\n.#........\n.....#####\n...#..#...\n....###...\n##... | 30 | 0 | 0 | 3,363 | |
924 | Mystical Mosaic | [
"greedy",
"implementation"
] | null | null | There is a rectangular grid of *n* rows of *m* initially-white cells each.
Arkady performed a certain number (possibly zero) of operations on it. In the *i*-th operation, a non-empty subset of rows *R**i* and a non-empty subset of columns *C**i* are chosen. For each row *r* in *R**i* and each column *c* in *C**i*, the intersection of row *r* and column *c* is coloured black.
There's another constraint: a row or a column can only be chosen at most once among all operations. In other words, it means that no pair of (*i*,<=*j*) (*i*<=<<=*j*) exists such that or , where denotes intersection of sets, and denotes the empty set.
You are to determine whether a valid sequence of operations exists that produces a given final grid. | The first line contains two space-separated integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=50)Β β the number of rows and columns of the grid, respectively.
Each of the following *n* lines contains a string of *m* characters, each being either '.' (denoting a white cell) or '#' (denoting a black cell), representing the desired setup. | If the given grid can be achieved by any valid sequence of operations, output "Yes"; otherwise output "No" (both without quotes).
You can print each character in any case (upper or lower). | [
"5 8\n.#.#..#.\n.....#..\n.#.#..#.\n#.#....#\n.....#..\n",
"5 5\n..#..\n..#..\n#####\n..#..\n..#..\n",
"5 9\n........#\n#........\n..##.#...\n.......#.\n....#.#.#\n"
] | [
"Yes\n",
"No\n",
"No\n"
] | For the first example, the desired setup can be produced by 3 operations, as is shown below.
For the second example, the desired setup cannot be produced, since in order to colour the center row, the third row and all columns must be selected in one operation, but after that no column can be selected again, hence it won't be possible to colour the other cells in the center column. | [
{
"input": "5 8\n.#.#..#.\n.....#..\n.#.#..#.\n#.#....#\n.....#..",
"output": "Yes"
},
{
"input": "5 5\n..#..\n..#..\n#####\n..#..\n..#..",
"output": "No"
},
{
"input": "5 9\n........#\n#........\n..##.#...\n.......#.\n....#.#.#",
"output": "No"
},
{
"input": "1 1\n#",
"o... | 31 | 0 | 0 | 3,368 | |
626 | Robot Sequence | [
"brute force",
"implementation"
] | null | null | Calvin the robot lies in an infinite rectangular grid. Calvin's source code contains a list of *n* commands, each either 'U', 'R', 'D', or 'L'Β β instructions to move a single square up, right, down, or left, respectively. How many ways can Calvin execute a non-empty contiguous substrings of commands and return to the same square he starts in? Two substrings are considered different if they have different starting or ending indices. | The first line of the input contains a single positive integer, *n* (1<=β€<=*n*<=β€<=200)Β β the number of commands.
The next line contains *n* characters, each either 'U', 'R', 'D', or 'L'Β β Calvin's source code. | Print a single integerΒ β the number of contiguous substrings that Calvin can execute and return to his starting square. | [
"6\nURLLDR\n",
"4\nDLUU\n",
"7\nRLRLRLR\n"
] | [
"2\n",
"0\n",
"12\n"
] | In the first case, the entire source code works, as well as the "RL" substring in the second and third characters.
Note that, in the third case, the substring "LR" appears three times, and is therefore counted three times to the total result. | [
{
"input": "6\nURLLDR",
"output": "2"
},
{
"input": "4\nDLUU",
"output": "0"
},
{
"input": "7\nRLRLRLR",
"output": "12"
},
{
"input": "1\nR",
"output": "0"
},
{
"input": "100\nURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDL... | 140 | 0 | 3 | 3,372 | |
839 | Game of the Rows | [
"brute force",
"greedy",
"implementation"
] | null | null | Daenerys Targaryen has an army consisting of *k* groups of soldiers, the *i*-th group contains *a**i* soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has *n* rows, each of them has 8 seats. We call two seats neighbor, if they are in the same row and in seats {1,<=2}, {3,<=4}, {4,<=5}, {5,<=6} or {7,<=8}.
Daenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats.
Your task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied. | The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=10000, 1<=β€<=*k*<=β€<=100)Β β the number of rows and the number of groups of soldiers, respectively.
The second line contains *k* integers *a*1,<=*a*2,<=*a*3,<=...,<=*a**k* (1<=β€<=*a**i*<=β€<=10000), where *a**i* denotes the number of soldiers in the *i*-th group.
It is guaranteed that *a*1<=+<=*a*2<=+<=...<=+<=*a**k*<=β€<=8Β·*n*. | If we can place the soldiers in the airplane print "YES" (without quotes). Otherwise print "NO" (without quotes).
You can choose the case (lower or upper) for each letter arbitrary. | [
"2 2\n5 8\n",
"1 2\n7 1\n",
"1 2\n4 4\n",
"1 4\n2 2 1 2\n"
] | [
"YES\n",
"NO\n",
"YES\n",
"YES\n"
] | In the first sample, Daenerys can place the soldiers like in the figure below:
In the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group.
In the third example Daenerys can place the first group on seats (1,β2,β7,β8), and the second group an all the remaining seats.
In the fourth example she can place the first two groups on seats (1,β2) and (7,β8), the third group on seats (3), and the fourth group on seats (5,β6). | [
{
"input": "2 2\n5 8",
"output": "YES"
},
{
"input": "1 2\n7 1",
"output": "NO"
},
{
"input": "1 2\n4 4",
"output": "YES"
},
{
"input": "1 4\n2 2 1 2",
"output": "YES"
},
{
"input": "10000 100\n749 2244 949 2439 2703 44 2394 124 285 3694 3609 717 1413 155 974 1778... | 156 | 20,172,800 | 0 | 3,373 | |
915 | Permute Digits | [
"dp",
"greedy"
] | null | null | You are given two positive integer numbers *a* and *b*. Permute (change order) of the digits of *a* to construct maximal number not exceeding *b*. No number in input and/or output can start with the digit 0.
It is allowed to leave *a* as it is. | The first line contains integer *a* (1<=β€<=*a*<=β€<=1018). The second line contains integer *b* (1<=β€<=*b*<=β€<=1018). Numbers don't have leading zeroes. It is guaranteed that answer exists. | Print the maximum possible number that is a permutation of digits of *a* and is not greater than *b*. The answer can't have any leading zeroes. It is guaranteed that the answer exists.
The number in the output should have exactly the same length as number *a*. It should be a permutation of digits of *a*. | [
"123\n222\n",
"3921\n10000\n",
"4940\n5000\n"
] | [
"213\n",
"9321\n",
"4940\n"
] | none | [
{
"input": "123\n222",
"output": "213"
},
{
"input": "3921\n10000",
"output": "9321"
},
{
"input": "4940\n5000",
"output": "4940"
},
{
"input": "23923472834\n23589234723",
"output": "23498743322"
},
{
"input": "102391019\n491010301",
"output": "399211100"
},... | 62 | 0 | 3 | 3,374 | |
777 | Game of Credit Cards | [
"data structures",
"dp",
"greedy",
"sortings"
] | null | null | After the fourth season Sherlock and Moriary have realized the whole foolishness of the battle between them and decided to continue their competitions in peaceful game of Credit Cards.
Rules of this game are simple: each player bring his favourite *n*-digit credit card. Then both players name the digits written on their cards one by one. If two digits are not equal, then the player, whose digit is smaller gets a flick (knock in the forehead usually made with a forefinger) from the other player. For example, if *n*<==<=3, Sherlock's card is 123 and Moriarty's card has number 321, first Sherlock names 1 and Moriarty names 3 so Sherlock gets a flick. Then they both digit 2 so no one gets a flick. Finally, Sherlock names 3, while Moriarty names 1 and gets a flick.
Of course, Sherlock will play honestly naming digits one by one in the order they are given, while Moriary, as a true villain, plans to cheat. He is going to name his digits in some other order (however, he is not going to change the overall number of occurences of each digit). For example, in case above Moriarty could name 1, 2, 3 and get no flicks at all, or he can name 2, 3 and 1 to give Sherlock two flicks.
Your goal is to find out the minimum possible number of flicks Moriarty will get (no one likes flicks) and the maximum possible number of flicks Sherlock can get from Moriarty. Note, that these two goals are different and the optimal result may be obtained by using different strategies. | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=1000)Β β the number of digits in the cards Sherlock and Moriarty are going to use.
The second line contains *n* digitsΒ β Sherlock's credit card number.
The third line contains *n* digitsΒ β Moriarty's credit card number. | First print the minimum possible number of flicks Moriarty will get. Then print the maximum possible number of flicks that Sherlock can get from Moriarty. | [
"3\n123\n321\n",
"2\n88\n00\n"
] | [
"0\n2\n",
"2\n0\n"
] | First sample is elaborated in the problem statement. In the second sample, there is no way Moriarty can avoid getting two flicks. | [
{
"input": "3\n123\n321",
"output": "0\n2"
},
{
"input": "2\n88\n00",
"output": "2\n0"
},
{
"input": "1\n4\n5",
"output": "0\n1"
},
{
"input": "1\n8\n7",
"output": "1\n0"
},
{
"input": "2\n55\n55",
"output": "0\n0"
},
{
"input": "3\n534\n432",
"out... | 156 | 0 | 3 | 3,379 | |
439 | Devu, the Dumb Guy | [
"implementation",
"sortings"
] | null | null | Devu is a dumb guy, his learning curve is very slow. You are supposed to teach him *n* subjects, the *i**th* subject has *c**i* chapters. When you teach him, you are supposed to teach all the chapters of a subject continuously.
Let us say that his initial per chapter learning power of a subject is *x* hours. In other words he can learn a chapter of a particular subject in *x* hours.
Well Devu is not complete dumb, there is a good thing about him too. If you teach him a subject, then time required to teach any chapter of the next subject will require exactly 1 hour less than previously required (see the examples to understand it more clearly). Note that his per chapter learning power can not be less than 1 hour.
You can teach him the *n* subjects in any possible order. Find out minimum amount of time (in hours) Devu will take to understand all the subjects and you will be free to do some enjoying task rather than teaching a dumb guy.
Please be careful that answer might not fit in 32 bit data type. | The first line will contain two space separated integers *n*, *x* (1<=β€<=*n*,<=*x*<=β€<=105). The next line will contain *n* space separated integers: *c*1,<=*c*2,<=...,<=*c**n* (1<=β€<=*c**i*<=β€<=105). | Output a single integer representing the answer to the problem. | [
"2 3\n4 1\n",
"4 2\n5 1 2 1\n",
"3 3\n1 1 1\n"
] | [
"11\n",
"10\n",
"6\n"
] | Look at the first example. Consider the order of subjects: 1, 2. When you teach Devu the first subject, it will take him 3 hours per chapter, so it will take 12 hours to teach first subject. After teaching first subject, his per chapter learning time will be 2 hours. Now teaching him second subject will take 2βΓβ1β=β2 hours. Hence you will need to spend 12β+β2β=β14 hours.
Consider the order of subjects: 2, 1. When you teach Devu the second subject, then it will take him 3 hours per chapter, so it will take 3βΓβ1β=β3 hours to teach the second subject. After teaching the second subject, his per chapter learning time will be 2 hours. Now teaching him the first subject will take 2βΓβ4β=β8 hours. Hence you will need to spend 11 hours.
So overall, minimum of both the cases is 11 hours.
Look at the third example. The order in this example doesn't matter. When you teach Devu the first subject, it will take him 3 hours per chapter. When you teach Devu the second subject, it will take him 2 hours per chapter. When you teach Devu the third subject, it will take him 1 hours per chapter. In total it takes 6 hours. | [
{
"input": "2 3\n4 1",
"output": "11"
},
{
"input": "4 2\n5 1 2 1",
"output": "10"
},
{
"input": "3 3\n1 1 1",
"output": "6"
},
{
"input": "20 4\n1 1 3 5 5 1 3 4 2 5 2 4 3 1 3 3 3 3 4 3",
"output": "65"
},
{
"input": "20 10\n6 6 1 2 6 4 5 3 6 5 4 5 6 5 4 6 6 2 3 3... | 186 | 7,577,600 | 3 | 3,383 | |
86 | Reflection | [
"math"
] | A. Reflection | 2 | 256 | For each positive integer *n* consider the integer Ο(*n*) which is obtained from *n* by replacing every digit *a* in the decimal notation of *n* with the digit (9<=<=-<=<=*a*). We say that Ο(*n*) is the reflection of *n*. For example, reflection of 192 equals 807. Note that leading zeros (if any) should be omitted. So reflection of 9 equals 0, reflection of 91 equals 8.
Let us call the weight of the number the product of the number and its reflection. Thus, the weight of the number 10 is equal to 10Β·89<==<=890.
Your task is to find the maximum weight of the numbers in the given range [*l*,<=*r*] (boundaries are included). | Input contains two space-separated integers *l* and *r* (1<=β€<=*l*<=β€<=*r*<=β€<=109) β bounds of the range. | Output should contain single integer number: maximum value of the product *n*Β·Ο(*n*), where *l*<=β€<=*n*<=β€<=*r*.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preferred to use cout (also you may use %I64d). | [
"3 7\n",
"1 1\n",
"8 10\n"
] | [
"20",
"8",
"890"
] | In the third sample weight of 8 equals 8Β·1β=β8, weight of 9 equals 9Β·0β=β0, weight of 10 equals 890.
Thus, maximum value of the product is equal to 890. | [
{
"input": "3 7",
"output": "20"
},
{
"input": "1 1",
"output": "8"
},
{
"input": "8 10",
"output": "890"
},
{
"input": "4 6",
"output": "20"
},
{
"input": "10 100",
"output": "89900"
},
{
"input": "1 999",
"output": "249500"
},
{
"input": ... | 92 | 0 | 0 | 3,403 |
712 | Memory and De-Evolution | [
"greedy",
"math"
] | null | null | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length *x*, and he wishes to perform operations to obtain an equilateral triangle of side length *y*.
In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer.
What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length *y*? | The first and only line contains two integers *x* and *y* (3<=β€<=*y*<=<<=*x*<=β€<=100<=000)Β β the starting and ending equilateral triangle side lengths respectively. | Print a single integerΒ β the minimum number of seconds required for Memory to obtain the equilateral triangle of side length *y* if he starts with the equilateral triangle of side length *x*. | [
"6 3\n",
"8 5\n",
"22 4\n"
] | [
"4\n",
"3\n",
"6\n"
] | In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides *a*, *b*, and *c* as (*a*,β*b*,β*c*). Then, Memory can do <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/18af21f738bad490df83097a90e1f2879a4b21c6.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the second sample test, Memory can do <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/bcfd51d1b2d764a1cf5fbc255cc02e6f5aaed3b1.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the third sample test, Memory can do: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/0969b7d413854c1e7528991d926bef1f7ffba008.png" style="max-width: 100.0%;max-height: 100.0%;"/>
<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/63e9e66b882c03e4c73e93ad92204dc255329309.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [
{
"input": "6 3",
"output": "4"
},
{
"input": "8 5",
"output": "3"
},
{
"input": "22 4",
"output": "6"
},
{
"input": "4 3",
"output": "3"
},
{
"input": "57 27",
"output": "4"
},
{
"input": "61 3",
"output": "9"
},
{
"input": "5 4",
"out... | 77 | 0 | 3 | 3,406 | |
580 | Kefa and Park | [
"dfs and similar",
"graphs",
"trees"
] | null | null | Kefa decided to celebrate his first big salary by going to the restaurant.
He lives by an unusual park. The park is a rooted tree consisting of *n* vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.
The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than *m* consecutive vertices with cats.
Your task is to help Kefa count the number of restaurants where he can go. | The first line contains two integers, *n* and *m* (2<=β€<=*n*<=β€<=105, 1<=β€<=*m*<=β€<=*n*) β the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n*, where each *a**i* either equals to 0 (then vertex *i* has no cat), or equals to 1 (then vertex *i* has a cat).
Next *n*<=-<=1 lines contains the edges of the tree in the format "*x**i* *y**i*" (without the quotes) (1<=β€<=*x**i*,<=*y**i*<=β€<=*n*, *x**i*<=β <=*y**i*), where *x**i* and *y**i* are the vertices of the tree, connected by an edge.
It is guaranteed that the given set of edges specifies a tree. | A single integer β the number of distinct leaves of a tree the path to which from Kefa's home contains at most *m* consecutive vertices with cats. | [
"4 1\n1 1 0 0\n1 2\n1 3\n1 4\n",
"7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n"
] | [
"2\n",
"2\n"
] | Let us remind you that a tree is a connected graph on *n* vertices and *n*β-β1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.
Note to the first sample test: <img class="tex-graphics" src="https://espresso.codeforces.com/785114b4b3f5336f02078c25750f87c5a1d0b4be.png" style="max-width: 100.0%;max-height: 100.0%;"/> The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.
Note to the second sample test: <img class="tex-graphics" src="https://espresso.codeforces.com/e5c07640680c837aec99126d94287872e69aa09a.png" style="max-width: 100.0%;max-height: 100.0%;"/> The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7. | [
{
"input": "4 1\n1 1 0 0\n1 2\n1 3\n1 4",
"output": "2"
},
{
"input": "7 1\n1 0 1 1 0 0 0\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7",
"output": "2"
},
{
"input": "3 2\n1 1 1\n1 2\n2 3",
"output": "0"
},
{
"input": "5 2\n1 1 0 1 1\n1 2\n2 3\n3 4\n4 5",
"output": "1"
},
{
"inpu... | 31 | 307,200 | 0 | 3,414 | |
33 | String Problem | [
"shortest paths"
] | B. String Problem | 2 | 256 | Boy Valera likes strings. And even more he likes them, when they are identical. That's why in his spare time Valera plays the following game. He takes any two strings, consisting of lower case Latin letters, and tries to make them identical. According to the game rules, with each move Valera can change one arbitrary character *A**i* in one of the strings into arbitrary character *B**i*, but he has to pay for every move a particular sum of money, equal to *W**i*. He is allowed to make as many moves as he needs. Since Valera is a very economical boy and never wastes his money, he asked you, an experienced programmer, to help him answer the question: what minimum amount of money should Valera have to get identical strings. | The first input line contains two initial non-empty strings *s* and *t*, consisting of lower case Latin letters. The length of each string doesn't exceed 105. The following line contains integer *n* (0<=β€<=*n*<=β€<=500)Β β amount of possible changings. Then follow *n* lines, each containing characters *A**i* and *B**i* (lower case Latin letters) and integer *W**i* (0<=β€<=*W**i*<=β€<=100), saying that it's allowed to change character *A**i* into character *B**i* in any of the strings and spend sum of money *W**i*. | If the answer exists, output the answer to the problem, and the resulting string. Otherwise output -1 in the only line. If the answer is not unique, output any. | [
"uayd\nuxxd\n3\na x 8\nx y 13\nd c 3\n",
"a\nb\n3\na b 2\na b 3\nb a 5\n",
"abc\nab\n6\na b 4\na b 7\nb a 8\nc b 11\nc a 3\na c 0\n"
] | [
"21\nuxyd\n",
"2\nb\n",
"-1\n"
] | none | [
{
"input": "uayd\nuxxd\n3\na x 8\nx y 13\nd c 3",
"output": "21\nuxyd"
},
{
"input": "a\nb\n3\na b 2\na b 3\nb a 5",
"output": "2\nb"
},
{
"input": "abc\nab\n6\na b 4\na b 7\nb a 8\nc b 11\nc a 3\na c 0",
"output": "-1"
},
{
"input": "xhtuopq\nrtutbz\n10\nh x 10\nx d 3\nr u 4... | 1,964 | 22,732,800 | 3.466657 | 3,416 |
237 | Young Table | [
"implementation",
"sortings"
] | null | null | You've got table *a*, consisting of *n* rows, numbered from 1 to *n*. The *i*-th line of table *a* contains *c**i* cells, at that for all *i* (1<=<<=*i*<=β€<=*n*) holds *c**i*<=β€<=*c**i*<=-<=1.
Let's denote *s* as the total number of cells of table *a*, that is, . We know that each cell of the table contains a single integer from 1 to *s*, at that all written integers are distinct.
Let's assume that the cells of the *i*-th row of table *a* are numbered from 1 to *c**i*, then let's denote the number written in the *j*-th cell of the *i*-th row as *a**i*,<=*j*. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions:
1. for all *i*,<=*j* (1<=<<=*i*<=β€<=*n*;Β 1<=β€<=*j*<=β€<=*c**i*) holds *a**i*,<=*j*<=><=*a**i*<=-<=1,<=*j*; 1. for all *i*,<=*j* (1<=β€<=*i*<=β€<=*n*;Β 1<=<<=*j*<=β€<=*c**i*) holds *a**i*,<=*j*<=><=*a**i*,<=*j*<=-<=1.
In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap.
Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than *s*. You do not have to minimize the number of operations. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=50) that shows the number of rows in the table. The second line contains *n* space-separated integers *c**i* (1<=β€<=*c**i*<=β€<=50;Β *c**i*<=β€<=*c**i*<=-<=1) β the numbers of cells on the corresponding rows.
Next *n* lines contain table *Π°*. The *i*-th of them contains *c**i* space-separated integers: the *j*-th integer in this line represents *a**i*,<=*j*.
It is guaranteed that all the given numbers *a**i*,<=*j* are positive and do not exceed *s*. It is guaranteed that all *a**i*,<=*j* are distinct. | In the first line print a single integer *m* (0<=β€<=*m*<=β€<=*s*), representing the number of performed swaps.
In the next *m* lines print the description of these swap operations. In the *i*-th line print four space-separated integers *x**i*,<=*y**i*,<=*p**i*,<=*q**i* (1<=β€<=*x**i*,<=*p**i*<=β€<=*n*;Β 1<=β€<=*y**i*<=β€<=*c**x**i*;Β 1<=β€<=*q**i*<=β€<=*c**p**i*). The printed numbers denote swapping the contents of cells *a**x**i*,<=*y**i* and *a**p**i*,<=*q**i*. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed. | [
"3\n3 2 1\n4 3 5\n6 1\n2\n",
"1\n4\n4 3 2 1\n"
] | [
"2\n1 1 2 2\n2 1 3 1\n",
"2\n1 1 1 4\n1 2 1 3\n"
] | none | [
{
"input": "3\n3 2 1\n4 3 5\n6 1\n2",
"output": "2\n1 1 2 2\n2 1 3 1"
},
{
"input": "1\n4\n4 3 2 1",
"output": "2\n1 1 1 4\n1 2 1 3"
},
{
"input": "5\n4 4 3 3 1\n14 13 4 15\n11 1 2 5\n7 6 10\n8 9 3\n12",
"output": "13\n1 1 2 2\n1 2 2 3\n1 3 4 3\n1 4 4 3\n2 1 2 4\n2 2 3 2\n2 3 3 1\n2 ... | 62 | 0 | 0 | 3,417 | |
475 | CGCDSSQ | [
"brute force",
"data structures",
"math"
] | null | null | Given a sequence of integers *a*1,<=...,<=*a**n* and *q* queries *x*1,<=...,<=*x**q* on it. For each query *x**i* you have to count the number of pairs (*l*,<=*r*) such that 1<=β€<=*l*<=β€<=*r*<=β€<=*n* and *gcd*(*a**l*,<=*a**l*<=+<=1,<=...,<=*a**r*)<==<=*x**i*.
is a greatest common divisor of *v*1,<=*v*2,<=...,<=*v**n*, that is equal to a largest positive integer that divides all *v**i*. | The first line of the input contains integer *n*, (1<=β€<=*n*<=β€<=105), denoting the length of the sequence. The next line contains *n* space separated integers *a*1,<=...,<=*a**n*, (1<=β€<=*a**i*<=β€<=109).
The third line of the input contains integer *q*, (1<=β€<=*q*<=β€<=3<=Γ<=105), denoting the number of queries. Then follows *q* lines, each contain an integer *x**i*, (1<=β€<=*x**i*<=β€<=109). | For each query print the result in a separate line. | [
"3\n2 6 3\n5\n1\n2\n3\n4\n6\n",
"7\n10 20 3 15 1000 60 16\n10\n1\n2\n3\n4\n5\n6\n10\n20\n60\n1000\n"
] | [
"1\n2\n2\n0\n1\n",
"14\n0\n2\n2\n2\n0\n2\n2\n1\n1\n"
] | none | [
{
"input": "3\n2 6 3\n5\n1\n2\n3\n4\n6",
"output": "1\n2\n2\n0\n1"
},
{
"input": "7\n10 20 3 15 1000 60 16\n10\n1\n2\n3\n4\n5\n6\n10\n20\n60\n1000",
"output": "14\n0\n2\n2\n2\n0\n2\n2\n1\n1"
},
{
"input": "10\n2 2 4 3 2 4 4 2 4 2\n104\n3\n3\n1\n4\n1\n1\n4\n1\n1\n3\n1\n1\n4\n1\n1\n1\n4\n3... | 108 | 5,836,800 | 0 | 3,420 | |
491 | Up the hill | [
"constructive algorithms",
"implementation"
] | null | null | Hiking club "Up the hill" just returned from a walk. Now they are trying to remember which hills they've just walked through.
It is known that there were *N* stops, all on different integer heights between 1 and *N* kilometers (inclusive) above the sea level. On the first day they've traveled from the first stop to the second stop, on the second day they've traveled from the second to the third and so on, and on the last day they've traveled from the stop *N*<=-<=1 to the stop *N* and successfully finished their expedition.
They are trying to find out which heights were their stops located at. They have an entry in a travel journal specifying how many days did they travel up the hill, and how many days did they walk down the hill.
Help them by suggesting some possible stop heights satisfying numbers from the travel journal. | In the first line there is an integer non-negative number *A* denoting the number of days of climbing up the hill. Second line contains an integer non-negative number *B*Β β the number of days of walking down the hill (*A*<=+<=*B*<=+<=1<==<=*N*, 1<=β€<=*N*<=β€<=100<=000). | Output *N* space-separated distinct integers from 1 to *N* inclusive, denoting possible heights of the stops in order of visiting. | [
"0\n1\n",
"2\n1"
] | [
"2 1 \n",
"1 3 4 2"
] | none | [
{
"input": "0\n1",
"output": "2 1 "
},
{
"input": "2\n1",
"output": "2 3 4 1 "
},
{
"input": "0\n3",
"output": "4 3 2 1 "
},
{
"input": "1\n1",
"output": "2 3 1 "
},
{
"input": "3\n7",
"output": "8 9 10 11 7 6 5 4 3 2 1 "
},
{
"input": "700\n300",
... | 124 | 20,172,800 | 0 | 3,439 | |
166 | Median | [
"greedy",
"math",
"sortings"
] | null | null | A median in an array with the length of *n* is an element which occupies position number after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2,<=6,<=1,<=2,<=3) is the number 2, and a median of array (0,<=96,<=17,<=23) β the number 17.
We define an expression as the integer part of dividing number *a* by number *b*.
One day Vasya showed Petya an array consisting of *n* integers and suggested finding the array's median. Petya didn't even look at the array and said that it equals *x*. Petya is a very honest boy, so he decided to add several numbers to the given array so that the median of the resulting array would be equal to *x*.
Petya can add any integers from 1 to 105 to the array, including the same numbers. Of course, he can add nothing to the array. If a number is added multiple times, then we should consider it the number of times it occurs. It is not allowed to delete of change initial numbers of the array.
While Petya is busy distracting Vasya, your task is to find the minimum number of elements he will need. | The first input line contains two space-separated integers *n* and *x* (1<=β€<=*n*<=β€<=500, 1<=β€<=*x*<=β€<=105) β the initial array's length and the required median's value. The second line contains *n* space-separated numbers β the initial array. The elements of the array are integers from 1 to 105. The array elements are not necessarily different. | Print the only integer β the minimum number of elements Petya needs to add to the array so that its median equals *x*. | [
"3 10\n10 20 30\n",
"3 4\n1 2 3\n"
] | [
"1\n",
"4\n"
] | In the first sample we can add number 9 to array (10,β20,β30). The resulting array (9,β10,β20,β30) will have a median in position <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7dd92241318a531b780c7783dfa446a3e413115e.png" style="max-width: 100.0%;max-height: 100.0%;"/>, that is, 10.
In the second sample you should add numbers 4, 5, 5, 5. The resulting array has median equal to 4. | [
{
"input": "3 10\n10 20 30",
"output": "1"
},
{
"input": "3 4\n1 2 3",
"output": "4"
},
{
"input": "2 2\n3 2",
"output": "0"
},
{
"input": "5 1\n1 1 2 1 2",
"output": "0"
},
{
"input": "5 4\n5 5 4 3 5",
"output": "1"
},
{
"input": "10 2\n2 2 1 3 2 1 2 ... | 124 | 0 | 3 | 3,442 | |
478 | Initial Bet | [
"implementation"
] | null | null | There are five people playing a game called "Generosity". Each person gives some non-zero number of coins *b* as an initial bet. After all players make their bets of *b* coins, the following operation is repeated for several times: a coin is passed from one player to some other player.
Your task is to write a program that can, given the number of coins each player has at the end of the game, determine the size *b* of the initial bet or find out that such outcome of the game cannot be obtained for any positive number of coins *b* in the initial bet. | The input consists of a single line containing five integers *c*1,<=*c*2,<=*c*3,<=*c*4 and *c*5 β the number of coins that the first, second, third, fourth and fifth players respectively have at the end of the game (0<=β€<=*c*1,<=*c*2,<=*c*3,<=*c*4,<=*c*5<=β€<=100). | Print the only line containing a single positive integer *b* β the number of coins in the initial bet of each player. If there is no such value of *b*, then print the only value "-1" (quotes for clarity). | [
"2 5 4 0 4\n",
"4 5 9 2 1\n"
] | [
"3\n",
"-1\n"
] | In the first sample the following sequence of operations is possible:
1. One coin is passed from the fourth player to the second player; 1. One coin is passed from the fourth player to the fifth player; 1. One coin is passed from the first player to the third player; 1. One coin is passed from the fourth player to the second player. | [
{
"input": "2 5 4 0 4",
"output": "3"
},
{
"input": "4 5 9 2 1",
"output": "-1"
},
{
"input": "0 0 0 0 0",
"output": "-1"
},
{
"input": "1 2 1 2 3",
"output": "-1"
},
{
"input": "100 0 0 0 0",
"output": "20"
},
{
"input": "2 3 4 5 6",
"output": "4"... | 46 | 0 | 0 | 3,446 | |
277 | Learning Languages | [
"dfs and similar",
"dsu"
] | null | null | The "BerCorp" company has got *n* employees. These employees can use *m* approved official languages for the formal correspondence. The languages are numbered with integers from 1 to *m*. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.
Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating). | The first line contains two integers *n* and *m* (2<=β€<=*n*,<=*m*<=β€<=100) β the number of employees and the number of languages.
Then *n* lines follow β each employee's language list. At the beginning of the *i*-th line is integer *k**i* (0<=β€<=*k**i*<=β€<=*m*) β the number of languages the *i*-th employee knows. Next, the *i*-th line contains *k**i* integers β *a**ij* (1<=β€<=*a**ij*<=β€<=*m*) β the identifiers of languages the *i*-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages.
The numbers in the lines are separated by single spaces. | Print a single integer β the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating). | [
"5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5\n",
"8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1\n",
"2 2\n1 2\n0\n"
] | [
"0\n",
"2\n",
"1\n"
] | In the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.
In the third sample employee 2 must learn language 2. | [
{
"input": "5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5",
"output": "0"
},
{
"input": "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1",
"output": "2"
},
{
"input": "2 2\n1 2\n0",
"output": "1"
},
{
"input": "2 2\n0\n0",
"output": "2"
},
{
"input": "5 5\n1 3\n0\n0\n2 4... | 218 | 307,200 | 0 | 3,448 | |
0 | none | [
"none"
] | null | null | One day, Hongcow goes to the store and sees a brand new deck of *n* special cards. Each individual card is either red or blue. He decides he wants to buy them immediately. To do this, he needs to play a game with the owner of the store.
This game takes some number of turns to complete. On a turn, Hongcow may do one of two things:
- Collect tokens. Hongcow collects 1 red token and 1 blue token by choosing this option (thus, 2 tokens in total per one operation). - Buy a card. Hongcow chooses some card and spends tokens to purchase it as specified below.
The *i*-th card requires *r**i* red resources and *b**i* blue resources. Suppose Hongcow currently has *A* red cards and *B* blue cards. Then, the *i*-th card will require Hongcow to spend *max*(*r**i*<=-<=*A*,<=0) red tokens, and *max*(*b**i*<=-<=*B*,<=0) blue tokens. Note, only tokens disappear, but the cards stay with Hongcow forever. Each card can be bought only once.
Given a description of the cards and their costs determine the minimum number of turns Hongcow needs to purchase all cards. | The first line of input will contain a single integer *n* (1<=β€<=*n*<=β€<=16).
The next *n* lines of input will contain three tokens *c**i*, *r**i* and *b**i*. *c**i* will be 'R' or 'B', denoting the color of the card as red or blue. *r**i* will be an integer denoting the amount of red resources required to obtain the card, and *b**i* will be an integer denoting the amount of blue resources required to obtain the card (0<=β€<=*r**i*,<=*b**i*<=β€<=107). | Output a single integer, denoting the minimum number of turns needed to acquire all the cards. | [
"3\nR 0 1\nB 1 0\nR 1 1\n",
"3\nR 3 0\nR 2 0\nR 1 0\n"
] | [
"4\n",
"6\n"
] | For the first sample, Hongcow's four moves are as follows:
1. Collect tokens 1. Buy card 1 1. Buy card 2 1. Buy card 3
For the second sample, one optimal strategy is as follows:
1. Collect tokens 1. Collect tokens 1. Buy card 2 1. Collect tokens 1. Buy card 3 1. Buy card 1 | [] | 61 | 204,800 | 0 | 3,457 | |
920 | Tea Queue | [
"implementation"
] | null | null | Recently *n* students from city S moved to city P to attend a programming camp.
They moved there by train. In the evening, all students in the train decided that they want to drink some tea. Of course, no two people can use the same teapot simultaneously, so the students had to form a queue to get their tea.
*i*-th student comes to the end of the queue at the beginning of *l**i*-th second. If there are multiple students coming to the queue in the same moment, then the student with greater index comes after the student with lesser index. Students in the queue behave as follows: if there is nobody in the queue before the student, then he uses the teapot for exactly one second and leaves the queue with his tea; otherwise the student waits for the people before him to get their tea. If at the beginning of *r**i*-th second student *i* still cannot get his tea (there is someone before him in the queue), then he leaves the queue without getting any tea.
For each student determine the second he will use the teapot and get his tea (if he actually gets it). | The first line contains one integer *t* β the number of test cases to solve (1<=β€<=*t*<=β€<=1000).
Then *t* test cases follow. The first line of each test case contains one integer *n* (1<=β€<=*n*<=β€<=1000) β the number of students.
Then *n* lines follow. Each line contains two integer *l**i*, *r**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=5000) β the second *i*-th student comes to the end of the queue, and the second he leaves the queue if he still cannot get his tea.
It is guaranteed that for every condition *l**i*<=-<=1<=β€<=*l**i* holds.
The sum of *n* over all test cases doesn't exceed 1000.
Note that in hacks you have to set *t*<==<=1. | For each test case print *n* integers. *i*-th of them must be equal to the second when *i*-th student gets his tea, or 0 if he leaves without tea. | [
"2\n2\n1 3\n1 4\n3\n1 5\n1 1\n2 3\n"
] | [
"1 2 \n1 0 2 \n"
] | The example contains 2 tests:
1. During 1-st second, students 1 and 2 come to the queue, and student 1 gets his tea. Student 2 gets his tea during 2-nd second. 1. During 1-st second, students 1 and 2 come to the queue, student 1 gets his tea, and student 2 leaves without tea. During 2-nd second, student 3 comes and gets his tea. | [
{
"input": "2\n2\n1 3\n1 4\n3\n1 5\n1 1\n2 3",
"output": "1 2 \n1 0 2 "
},
{
"input": "19\n1\n1 1\n1\n1 2\n1\n1 1000\n1\n1 2000\n1\n2 2\n1\n2 3\n1\n2 1000\n1\n2 2000\n1\n1999 1999\n1\n1999 2000\n1\n2000 2000\n2\n1 1\n1 1\n2\n1 1\n1 2\n2\n1 2\n1 1\n2\n1 2000\n1 1\n2\n1 1\n1 2000\n2\n1 2000\n2 2\n2\n2... | 77 | 5,734,400 | 3 | 3,459 | |
526 | King of Thieves | [
"brute force",
"implementation"
] | null | null | In this problem you will meet the simplified model of game King of Thieves.
In a new ZeptoLab game called "King of Thieves" your aim is to reach a chest with gold by controlling your character, avoiding traps and obstacles on your way.
An interesting feature of the game is that you can design your own levels that will be available to other players. Let's consider the following simple design of a level.
A dungeon consists of *n* segments located at a same vertical level, each segment is either a platform that character can stand on, or a pit with a trap that makes player lose if he falls into it. All segments have the same length, platforms on the scheme of the level are represented as '*' and pits are represented as '.'.
One of things that affects speedrun characteristics of the level is a possibility to perform a series of consecutive jumps of the same length. More formally, when the character is on the platform number *i*1, he can make a sequence of jumps through the platforms *i*1<=<<=*i*2<=<<=...<=<<=*i**k*, if *i*2<=-<=*i*1<==<=*i*3<=-<=*i*2<==<=...<==<=*i**k*<=-<=*i**k*<=-<=1. Of course, all segments *i*1,<=*i*2,<=... *i**k* should be exactly the platforms, not pits.
Let's call a level to be good if you can perform a sequence of four jumps of the same length or in the other words there must be a sequence *i*1,<=*i*2,<=...,<=*i*5, consisting of five platforms so that the intervals between consecutive platforms are of the same length. Given the scheme of the level, check if it is good. | The first line contains integer *n* (1<=β€<=*n*<=β€<=100) β the number of segments on the level.
Next line contains the scheme of the level represented as a string of *n* characters '*' and '.'. | If the level is good, print the word "yes" (without the quotes), otherwise print the word "no" (without the quotes). | [
"16\n.**.*..*.***.**.\n",
"11\n.*.*...*.*.\n"
] | [
"yes",
"no"
] | In the first sample test you may perform a sequence of jumps through platforms 2,β5,β8,β11,β14. | [
{
"input": "16\n.**.*..*.***.**.",
"output": "yes"
},
{
"input": "11\n.*.*...*.*.",
"output": "no"
},
{
"input": "53\n*.*.****.*.*......**....**.***.*.*.**.*.*.***...*..*.",
"output": "yes"
},
{
"input": "71\n**.**..*****.*.*.*.********.....*****.****.*..***...*.*.*.**.****.*... | 61 | 0 | 0 | 3,460 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.