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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
348 | Turtles | [
"dp",
"matrices"
] | null | null | You've got a table of size *n*<=Γ<=*m*. We'll consider the table rows numbered from top to bottom 1 through *n*, and the columns numbered from left to right 1 through *m*. Then we'll denote the cell in row *x* and column *y* as (*x*,<=*y*).
Initially cell (1,<=1) contains two similar turtles. Both turtles want to get to cell (*n*,<=*m*). Some cells of the table have obstacles but it is guaranteed that there aren't any obstacles in the upper left and lower right corner. A turtle (one or the other) can go from cell (*x*,<=*y*) to one of two cells (*x*<=+<=1,<=*y*) and (*x*,<=*y*<=+<=1), as long as the required cell doesn't contain an obstacle. The turtles have had an argument so they don't want to have any chance of meeting each other along the way. Help them find the number of ways in which they can go from cell (1,<=1) to cell (*n*,<=*m*).
More formally, find the number of pairs of non-intersecting ways from cell (1,<=1) to cell (*n*,<=*m*) modulo 1000000007 (109<=+<=7). Two ways are called non-intersecting if they have exactly two common points β the starting point and the final point. | The first line contains two integers *n*,<=*m* (2<=β€<=*n*,<=*m*<=β€<=3000). Each of the following *n* lines contains *m* characters describing the table. The empty cells are marked by characters ".", the cells with obstacles are marked by "#".
It is guaranteed that the upper left and the lower right cells are empty. | In a single line print a single integer β the number of pairs of non-intersecting paths from cell (1,<=1) to cell (*n*,<=*m*) modulo 1000000007 (109<=+<=7). | [
"4 5\n.....\n.###.\n.###.\n.....\n",
"2 3\n...\n...\n"
] | [
"1\n",
"1\n"
] | none | [
{
"input": "10 10\n.#.#.#.#..\n#...##.###\n...#...##.\n..##......\n#.###..#.#\n.###..#.#.\n...#...##.\n.....#.##.\n.#.#....##\n#....###..",
"output": "0"
},
{
"input": "10 8\n.#######\n########\n########\n########\n########\n########\n########\n########\n########\n#######.",
"output": "0"
}
] | 0 | 0 | -1 | 2,375 | |
891 | Gluttony | [
"constructive algorithms",
"greedy"
] | 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 "
},
{
... | 46 | 6,963,200 | 0 | 2,376 | |
281 | Word Capitalization | [
"implementation",
"strings"
] | null | null | Capitalization is writing a word with its first letter as a capital letter. Your task is to capitalize the given word.
Note, that during capitalization all the letters except the first one remains unchanged. | A single line contains a non-empty word. This word consists of lowercase and uppercase English letters. The length of the word will not exceed 103. | Output the given word after capitalization. | [
"ApPLe\n",
"konjac\n"
] | [
"ApPLe\n",
"Konjac\n"
] | none | [
{
"input": "ApPLe",
"output": "ApPLe"
},
{
"input": "konjac",
"output": "Konjac"
},
{
"input": "a",
"output": "A"
},
{
"input": "A",
"output": "A"
},
{
"input": "z",
"output": "Z"
},
{
"input": "ABACABA",
"output": "ABACABA"
},
{
"input": "... | 92 | 0 | 3 | 2,378 | |
769 | News About Credit | [
"*special",
"greedy",
"two pointers"
] | null | null | Polycarp studies at the university in the group which consists of *n* students (including himself). All they are registrated in the social net "TheContacnt!".
Not all students are equally sociable. About each student you know the value *a**i* β the maximum number of messages which the *i*-th student is agree to send per day. The student can't send messages to himself.
In early morning Polycarp knew important news that the programming credit will be tomorrow. For this reason it is necessary to urgently inform all groupmates about this news using private messages.
Your task is to make a plan of using private messages, so that:
- the student *i* sends no more than *a**i* messages (for all *i* from 1 to *n*); - all students knew the news about the credit (initially only Polycarp knew it); - the student can inform the other student only if he knows it himself.
Let's consider that all students are numerated by distinct numbers from 1 to *n*, and Polycarp always has the number 1.
In that task you shouldn't minimize the number of messages, the moment of time, when all knew about credit or some other parameters. Find any way how to use private messages which satisfies requirements above. | The first line contains the positive integer *n* (2<=β€<=*n*<=β€<=100) β the number of students.
The second line contains the sequence *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=100), where *a**i* equals to the maximum number of messages which can the *i*-th student agree to send. Consider that Polycarp always has the number 1. | Print -1 to the first line if it is impossible to inform all students about credit.
Otherwise, in the first line print the integer *k* β the number of messages which will be sent. In each of the next *k* lines print two distinct integers *f* and *t*, meaning that the student number *f* sent the message with news to the student number *t*. All messages should be printed in chronological order. It means that the student, who is sending the message, must already know this news. It is assumed that students can receive repeated messages with news of the credit.
If there are several answers, it is acceptable to print any of them. | [
"4\n1 2 1 0\n",
"6\n2 0 1 3 2 0\n",
"3\n0 2 2\n"
] | [
"3\n1 2\n2 4\n2 3\n",
"6\n1 3\n3 4\n1 2\n4 5\n5 6\n4 6\n",
"-1\n"
] | In the first test Polycarp (the student number 1) can send the message to the student number 2, who after that can send the message to students number 3 and 4. Thus, all students knew about the credit. | [
{
"input": "4\n1 2 1 0",
"output": "3\n1 2\n2 3\n2 4"
},
{
"input": "6\n2 0 1 3 2 0",
"output": "5\n1 4\n1 5\n4 3\n4 2\n4 6"
},
{
"input": "3\n0 2 2",
"output": "-1"
},
{
"input": "2\n0 0",
"output": "-1"
},
{
"input": "2\n1 0",
"output": "1\n1 2"
},
{
... | 62 | 4,915,200 | 3 | 2,379 | |
76 | Points | [
"implementation",
"math"
] | E. Points | 1 | 256 | You are given *N* points on a plane. Write a program which will find the sum of squares of distances between all pairs of points. | The first line of input contains one integer number *N* (1<=β€<=*N*<=β€<=100<=000) β the number of points. Each of the following *N* lines contain two integer numbers *X* and *Y* (<=-<=10<=000<=β€<=*X*,<=*Y*<=β€<=10<=000) β the coordinates of points. Two or more points may coincide. | The only line of output should contain the required sum of squares of distances between all pairs of points. | [
"4\n1 1\n-1 -1\n1 -1\n-1 1\n"
] | [
"32\n"
] | none | [
{
"input": "4\n1 1\n-1 -1\n1 -1\n-1 1",
"output": "32"
},
{
"input": "1\n6 3",
"output": "0"
},
{
"input": "30\n-7 -12\n-2 5\n14 8\n9 17\n15 -18\n20 6\n20 8\n-13 12\n-4 -20\n-11 -16\n-6 16\n1 -9\n5 -12\n13 -17\n11 5\n8 -9\n-13 5\n19 -13\n-19 -8\n-14 10\n10 3\n-16 -8\n-17 16\n-14 -15\n5 1... | 686 | 6,963,200 | 0 | 2,390 |
852 | Digits | [
"brute force",
"implementation",
"math"
] | null | null | John gave Jack a very hard problem. He wrote a very big positive integer *A*0 on a piece of paper. The number is less than 10200000 . In each step, Jack is allowed to put '<=+<=' signs in between some of the digits (maybe none) of the current number and calculate the sum of the expression. He can perform the same procedure on that sum and so on. The resulting sums can be labeled respectively by *A*1, *A*2 etc. His task is to get to a single digit number.
The problem is that there is not much blank space on the paper. There are only three lines of space, so he can't perform more than three steps. Since he wants to fill up the paper completely, he will perform exactly three steps.
Jack must not add leading zeros to intermediate results, but he can put '<=+<=' signs in front of digit 0. For example, if the current number is 1000100, 10<=+<=001<=+<=00 is a valid step, resulting in number 11. | First line contains a positive integer *N* (1<=β€<=*N*<=β€<=200000), representing the number of digits of *A*0.
Second line contains a string of length *N* representing positive integer number *A*0. Each character is digit. There will be no leading zeros. | Output exactly three lines, the steps Jack needs to perform to solve the problem. You can output any sequence of steps which results in a single digit number (and is logically consistent).
Every step consists of digits and '<=+<=' signs. Steps should not contain several '<=+<=' signs in a row, whitespaces, or '<=+<=' signs as the first or last character. They also need to be arithmetically consistent.
Solution might not be unique. Output any of them in that case. | [
"1\n1\n",
"4\n5806\n"
] | [
"1\n1\n1\n",
"5+8+0+6\n1+9\n1+0\n"
] | In the first sample, Jack can't put 'β+β' signs anywhere, so he just writes 1 in each line and solves the problem. Here, solution is unique.
In the second sample, Jack first puts 'β+β' between every two consecutive digits, thus getting the result 5β+β8β+β0β+β6β=β19. He does the same on the second step, getting 1β+β9β=β10. Once more, he gets 1β+β0β=β1, so after three steps, the result is 1 and his solution is correct. | [] | 124 | 2,150,400 | 0 | 2,392 | |
168 | Wizards and Demonstration | [
"implementation",
"math"
] | null | null | Some country is populated by wizards. They want to organize a demonstration.
There are *n* people living in the city, *x* of them are the wizards who will surely go to the demonstration. Other city people (*n*<=-<=*x* people) do not support the wizards and aren't going to go to the demonstration. We know that the city administration will react only to the demonstration involving at least *y* percent of the city people. Having considered the matter, the wizards decided to create clone puppets which can substitute the city people on the demonstration.
So all in all, the demonstration will involve only the wizards and their puppets. The city administration cannot tell the difference between a puppet and a person, so, as they calculate the percentage, the administration will consider the city to be consisting of only *n* people and not containing any clone puppets.
Help the wizards and find the minimum number of clones to create to that the demonstration had no less than *y* percent of the city people. | The first line contains three space-separated integers, *n*, *x*, *y* (1<=β€<=*n*,<=*x*,<=*y*<=β€<=104,<=*x*<=β€<=*n*) β the number of citizens in the city, the number of wizards and the percentage the administration needs, correspondingly.
Please note that *y* can exceed 100 percent, that is, the administration wants to see on a demonstration more people that actually live in the city (<=><=*n*). | Print a single integer β the answer to the problem, the minimum number of clones to create, so that the demonstration involved no less than *y* percent of *n* (the real total city population). | [
"10 1 14\n",
"20 10 50\n",
"1000 352 146\n"
] | [
"1\n",
"0\n",
"1108\n"
] | In the first sample it is necessary that at least 14% of 10 people came to the demonstration. As the number of people should be integer, then at least two people should come. There is only one wizard living in the city and he is going to come. That isn't enough, so he needs to create one clone.
In the second sample 10 people should come to the demonstration. The city has 10 wizards. They will all come to the demonstration, so nobody has to create any clones. | [
{
"input": "10 1 14",
"output": "1"
},
{
"input": "20 10 50",
"output": "0"
},
{
"input": "1000 352 146",
"output": "1108"
},
{
"input": "68 65 20",
"output": "0"
},
{
"input": "78 28 27",
"output": "0"
},
{
"input": "78 73 58",
"output": "0"
},
... | 218 | 0 | 3 | 2,395 | |
192 | Funky Numbers | [
"binary search",
"brute force",
"implementation"
] | null | null | As you very well know, this year's funkiest numbers are so called triangular numbers (that is, integers that are representable as , where *k* is some positive integer), and the coolest numbers are those that are representable as a sum of two triangular numbers.
A well-known hipster Andrew adores everything funky and cool but unfortunately, he isn't good at maths. Given number *n*, help him define whether this number can be represented by a sum of two triangular numbers (not necessarily different)! | The first input line contains an integer *n* (1<=β€<=*n*<=β€<=109). | Print "YES" (without the quotes), if *n* can be represented as a sum of two triangular numbers, otherwise print "NO" (without the quotes). | [
"256\n",
"512\n"
] | [
"YES\n",
"NO\n"
] | In the first sample number <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/92095692c6ea93e9e3b837a0408ba7543549d5b2.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the second sample number 512 can not be represented as a sum of two triangular numbers. | [
{
"input": "256",
"output": "YES"
},
{
"input": "512",
"output": "NO"
},
{
"input": "80",
"output": "NO"
},
{
"input": "828",
"output": "YES"
},
{
"input": "6035",
"output": "NO"
},
{
"input": "39210",
"output": "YES"
},
{
"input": "79712",... | 342 | 7,577,600 | 3 | 2,402 | |
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,152 | 20,787,200 | 3 | 2,403 | |
0 | none | [
"none"
] | null | null | Firecrackers scare Nian the monster, but they're wayyyyy too noisy! Maybe fireworks make a nice complement.
Little Tommy is watching a firework show. As circular shapes spread across the sky, a splendid view unfolds on the night of Lunar New Year's eve.
A wonder strikes Tommy. How many regions are formed by the circles on the sky? We consider the sky as a flat plane. A region is a connected part of the plane with positive area, whose bound consists of parts of bounds of the circles and is a curve or several curves without self-intersections, and that does not contain any curve other than its boundaries. Note that exactly one of the regions extends infinitely. | The first line of input contains one integer *n* (1<=β€<=*n*<=β€<=3), denoting the number of circles.
The following *n* lines each contains three space-separated integers *x*, *y* and *r* (<=-<=10<=β€<=*x*,<=*y*<=β€<=10, 1<=β€<=*r*<=β€<=10), describing a circle whose center is (*x*,<=*y*) and the radius is *r*. No two circles have the same *x*, *y* and *r* at the same time. | Print a single integerΒ β the number of regions on the plane. | [
"3\n0 0 1\n2 0 1\n4 0 1\n",
"3\n0 0 2\n3 0 2\n6 0 2\n",
"3\n0 0 2\n2 0 2\n1 1 2\n"
] | [
"4\n",
"6\n",
"8\n"
] | For the first example,
For the second example,
For the third example, | [
{
"input": "3\n0 0 1\n2 0 1\n4 0 1",
"output": "4"
},
{
"input": "3\n0 0 2\n3 0 2\n6 0 2",
"output": "6"
},
{
"input": "3\n0 0 2\n2 0 2\n1 1 2",
"output": "8"
},
{
"input": "1\n0 0 10",
"output": "2"
},
{
"input": "2\n-10 10 1\n10 -10 1",
"output": "3"
},
... | 62 | 5,632,000 | -1 | 2,404 | |
747 | Mammoth's Genome Decoding | [
"implementation",
"strings"
] | null | null | The process of mammoth's genome decoding in Berland comes to its end!
One of the few remaining tasks is to restore unrecognized nucleotides in a found chain *s*. Each nucleotide is coded with a capital letter of English alphabet: 'A', 'C', 'G' or 'T'. Unrecognized nucleotides are coded by a question mark '?'. Thus, *s* is a string consisting of letters 'A', 'C', 'G', 'T' and characters '?'.
It is known that the number of nucleotides of each of the four types in the decoded genome of mammoth in Berland should be equal.
Your task is to decode the genome and replace each unrecognized nucleotide with one of the four types so that the number of nucleotides of each of the four types becomes equal. | The first line contains the integer *n* (4<=β€<=*n*<=β€<=255)Β β the length of the genome.
The second line contains the string *s* of length *n*Β β the coded genome. It consists of characters 'A', 'C', 'G', 'T' and '?'. | If it is possible to decode the genome, print it. If there are multiple answer, print any of them. If it is not possible, print three equals signs in a row: "===" (without quotes). | [
"8\nAG?C??CT\n",
"4\nAGCT\n",
"6\n????G?\n",
"4\nAA??\n"
] | [
"AGACGTCT\n",
"AGCT\n",
"===\n",
"===\n"
] | In the first example you can replace the first question mark with the letter 'A', the second question mark with the letter 'G', the third question mark with the letter 'T', then each nucleotide in the genome would be presented twice.
In the second example the genome is already decoded correctly and each nucleotide is exactly once in it.
In the third and the fourth examples it is impossible to decode the genom. | [
{
"input": "8\nAG?C??CT",
"output": "AGACGTCT"
},
{
"input": "4\nAGCT",
"output": "AGCT"
},
{
"input": "6\n????G?",
"output": "==="
},
{
"input": "4\nAA??",
"output": "==="
},
{
"input": "4\n????",
"output": "ACGT"
},
{
"input": "252\n???????GCG??T??TT... | 109 | 6,963,200 | 3 | 2,410 | |
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": "?"
},
{
... | 218 | 0 | 3.9455 | 2,416 |
171 | ucyhf | [
"*special",
"brute force",
"implementation",
"number theory"
] | null | null | qd ucyhf yi q fhycu dkcruh mxeiu huluhiu yi q tyvvuhudj fhycu dkcruh. oekh jqia yi je vydt jxu djx ucyhf. | jxu ydfkj sediyiji ev q iydwbu ydjuwuh *d* (1<=β€<=*d*<=β€<=11184) β jxu edu-rqiut ydtun ev jxu ucyhf je vydt. | ekjfkj q iydwbu dkcruh. | [
"1\n"
] | [
"13\n"
] | none | [
{
"input": "1",
"output": "13"
},
{
"input": "2",
"output": "17"
},
{
"input": "3",
"output": "31"
},
{
"input": "4",
"output": "37"
},
{
"input": "5",
"output": "71"
},
{
"input": "6",
"output": "73"
},
{
"input": "7",
"output": "79"
... | 310 | 19,763,200 | 3 | 2,421 | |
32 | Constellation | [
"implementation"
] | D. Constellation | 2 | 256 | A star map in Berland is a checked field *n*<=Γ<=*m* squares. In each square there is or there is not a star. The favourite constellation of all Berland's astronomers is the constellation of the Cross. This constellation can be formed by any 5 stars so, that for some integer *x* (radius of the constellation) the following is true:
- the 2nd is on the same vertical line as the 1st, but *x* squares up - the 3rd is on the same vertical line as the 1st, but *x* squares down - the 4th is on the same horizontal line as the 1st, but *x* squares left - the 5th is on the same horizontal line as the 1st, but *x* squares right
Such constellations can be very numerous, that's why they are numbered with integers from 1 on the following principle: when two constellations are compared, the one with a smaller radius gets a smaller index; if their radii are equal β the one, whose central star if higher than the central star of the other one; if their central stars are at the same level β the one, whose central star is to the left of the central star of the other one.
Your task is to find the constellation with index *k* by the given Berland's star map. | The first line contains three integers *n*, *m* and *k* (1<=β€<=*n*,<=*m*<=β€<=300,<=1<=β€<=*k*<=β€<=3Β·107) β height and width of the map and index of the required constellation respectively. The upper-left corner has coordinates (1,<=1), and the lower-right β (*n*,<=*m*). Then there follow *n* lines, *m* characters each β description of the map. *j*-th character in *i*-th line is Β«*Β», if there is a star in the corresponding square, and Β«.Β» if this square is empty. | If the number of the constellations is less than *k*, output -1. Otherwise output 5 lines, two integers each β coordinates of the required constellation. Output the stars in the following order: central, upper, lower, left, right. | [
"5 6 1\n....*.\n...***\n....*.\n..*...\n.***..\n",
"5 6 2\n....*.\n...***\n....*.\n..*...\n.***..\n",
"7 7 2\n...*...\n.......\n...*...\n*.***.*\n...*...\n.......\n...*...\n"
] | [
"2 5\n1 5\n3 5\n2 4\n2 6\n",
"-1\n",
"4 4\n1 4\n7 4\n4 1\n4 7\n"
] | none | [
{
"input": "5 6 1\n....*.\n...***\n....*.\n..*...\n.***..",
"output": "2 5\n1 5\n3 5\n2 4\n2 6"
},
{
"input": "5 6 2\n....*.\n...***\n....*.\n..*...\n.***..",
"output": "-1"
},
{
"input": "5 5 1\n.....\n.....\n.*..*\n*.*..\n....*",
"output": "-1"
},
{
"input": "5 5 3\n*.***\n... | 2,000 | 117,248,000 | 0 | 2,426 |
656 | Ace It! | [
"*special"
] | null | null | The only line of the input is a string of 7 characters. The first character is letter A, followed by 6 digits. The input is guaranteed to be valid (for certain definition of "valid").
Output a single integer. | The only line of the input is a string of 7 characters. The first character is letter A, followed by 6 digits. The input is guaranteed to be valid (for certain definition of "valid"). | Output a single integer. | [
"A221033\n",
"A223635\n",
"A232726\n"
] | [
"21\n",
"22\n",
"23\n"
] | none | [
{
"input": "A221033",
"output": "21"
},
{
"input": "A223635",
"output": "22"
},
{
"input": "A232726",
"output": "23"
},
{
"input": "A102210",
"output": "25"
},
{
"input": "A231010",
"output": "26"
},
{
"input": "A222222",
"output": "13"
},
{
... | 77 | 0 | 3 | 2,427 | |
699 | Launch of Collider | [
"implementation"
] | null | null | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. *n* particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, *x**i* is the coordinate of the *i*-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.
You know the direction of each particle movementΒ β it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.
Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. | The first line contains the positive integer *n* (1<=β€<=*n*<=β€<=200<=000)Β β the number of particles.
The second line contains *n* symbols "L" and "R". If the *i*-th symbol equals "L", then the *i*-th particle will move to the left, otherwise the *i*-th symbol equals "R" and the *i*-th particle will move to the right.
The third line contains the sequence of pairwise distinct even integers *x*1,<=*x*2,<=...,<=*x**n* (0<=β€<=*x**i*<=β€<=109)Β β the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. | In the first line print the only integerΒ β the first moment (in microseconds) when two particles are at the same point and there will be an explosion.
Print the only integer -1, if the collision of particles doesn't happen. | [
"4\nRLRL\n2 4 6 10\n",
"3\nLLR\n40 50 60\n"
] | [
"1\n",
"-1\n"
] | In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.
In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | [
{
"input": "4\nRLRL\n2 4 6 10",
"output": "1"
},
{
"input": "3\nLLR\n40 50 60",
"output": "-1"
},
{
"input": "4\nRLLR\n46 230 264 470",
"output": "92"
},
{
"input": "6\nLLRLLL\n446 492 650 844 930 970",
"output": "97"
},
{
"input": "8\nRRLLLLLL\n338 478 512 574 59... | 0 | 0 | -1 | 2,430 | |
676 | Nicholas and Permutation | [
"constructive algorithms",
"implementation"
] | null | null | Nicholas has an array *a* that contains *n* distinct integers from 1 to *n*. In other words, Nicholas has a permutation of size *n*.
Nicholas want the minimum element (integer 1) and the maximum element (integer *n*) to be as far as possible from each other. He wants to perform exactly one swap in order to maximize the distance between the minimum and the maximum elements. The distance between two elements is considered to be equal to the absolute difference between their positions. | The first line of the input contains a single integer *n* (2<=β€<=*n*<=β€<=100)Β β the size of the permutation.
The second line of the input contains *n* distinct integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=*n*), where *a**i* is equal to the element at the *i*-th position. | Print a single integerΒ β the maximum possible distance between the minimum and the maximum elements Nicholas can achieve by performing exactly one swap. | [
"5\n4 5 1 3 2\n",
"7\n1 6 5 3 4 7 2\n",
"6\n6 5 4 3 2 1\n"
] | [
"3\n",
"6\n",
"5\n"
] | In the first sample, one may obtain the optimal answer by swapping elements 1 and 2.
In the second sample, the minimum and the maximum elements will be located in the opposite ends of the array if we swap 7 and 2.
In the third sample, the distance between the minimum and the maximum elements is already maximum possible, so we just perform some unnecessary swap, for example, one can swap 5 and 2. | [
{
"input": "5\n4 5 1 3 2",
"output": "3"
},
{
"input": "7\n1 6 5 3 4 7 2",
"output": "6"
},
{
"input": "6\n6 5 4 3 2 1",
"output": "5"
},
{
"input": "2\n1 2",
"output": "1"
},
{
"input": "2\n2 1",
"output": "1"
},
{
"input": "3\n2 3 1",
"output": "... | 140 | 0 | 0 | 2,432 | |
992 | Nastya Studies Informatics | [
"math",
"number theory"
] | null | null | Today on Informatics class Nastya learned about GCD and LCM (see links below). Nastya is very intelligent, so she solved all the tasks momentarily and now suggests you to solve one of them as well.
We define a pair of integers (*a*,<=*b*) good, if *GCD*(*a*,<=*b*)<==<=*x* and *LCM*(*a*,<=*b*)<==<=*y*, where *GCD*(*a*,<=*b*) denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor) of *a* and *b*, and *LCM*(*a*,<=*b*) denotes the [least common multiple](https://en.wikipedia.org/wiki/Least_common_multiple) of *a* and *b*.
You are given two integers *x* and *y*. You are to find the number of good pairs of integers (*a*,<=*b*) such that *l*<=β€<=*a*,<=*b*<=β€<=*r*. Note that pairs (*a*,<=*b*) and (*b*,<=*a*) are considered different if *a*<=β <=*b*. | The only line contains four integers *l*,<=*r*,<=*x*,<=*y* (1<=β€<=*l*<=β€<=*r*<=β€<=109, 1<=β€<=*x*<=β€<=*y*<=β€<=109). | In the only line print the only integerΒ β the answer for the problem. | [
"1 2 1 2\n",
"1 12 1 12\n",
"50 100 3 30\n"
] | [
"2\n",
"4\n",
"0\n"
] | In the first example there are two suitable good pairs of integers (*a*,β*b*): (1,β2) and (2,β1).
In the second example there are four suitable good pairs of integers (*a*,β*b*): (1,β12), (12,β1), (3,β4) and (4,β3).
In the third example there are good pairs of integers, for example, (3,β30), but none of them fits the condition *l*ββ€β*a*,β*b*ββ€β*r*. | [
{
"input": "1 2 1 2",
"output": "2"
},
{
"input": "1 12 1 12",
"output": "4"
},
{
"input": "50 100 3 30",
"output": "0"
},
{
"input": "1 1000000000 1 1000000000",
"output": "4"
},
{
"input": "1 1000000000 158260522 200224287",
"output": "0"
},
{
"input... | 78 | 6,963,200 | 3 | 2,433 | |
30 | Accounting | [
"brute force",
"math"
] | A. Accounting | 2 | 256 | A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself.
The total income *A* of his kingdom during 0-th year is known, as well as the total income *B* during *n*-th year (these numbers can be negative β it means that there was a loss in the correspondent year).
King wants to show financial stability. To do this, he needs to find common coefficient *X* β the coefficient of income growth during one year. This coefficient should satisfy the equation:
Surely, the king is not going to do this job by himself, and demands you to find such number *X*.
It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient *X* must be integers. The number *X* may be zero or negative. | The input contains three integers *A*, *B*, *n* (|*A*|,<=|*B*|<=β€<=1000, 1<=β€<=*n*<=β€<=10). | Output the required integer coefficient *X*, or Β«No solutionΒ», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. | [
"2 18 2\n",
"-1 8 3\n",
"0 0 10\n",
"1 16 5\n"
] | [
"3",
"-2",
"5",
"No solution"
] | none | [
{
"input": "2 18 2",
"output": "3"
},
{
"input": "-1 8 3",
"output": "-2"
},
{
"input": "0 0 10",
"output": "5"
},
{
"input": "1 16 5",
"output": "No solution"
},
{
"input": "0 1 2",
"output": "No solution"
},
{
"input": "3 0 4",
"output": "0"
},... | 60 | 0 | 0 | 2,434 |
63 | Bulls and Cows | [
"brute force",
"implementation"
] | C. Bulls and Cows | 2 | 256 | The "Bulls and Cows" game needs two people to play. The thinker thinks of a number and the guesser tries to guess it.
The thinker thinks of a four-digit number in the decimal system. All the digits in the number are different and the number may have a leading zero. It can't have more than one leading zero, because all it's digits should be different. The guesser tries to guess the number. He makes a series of guesses, trying experimental numbers and receives answers from the first person in the format "*x* bulls *y* cows". *x* represents the number of digits in the experimental number that occupy the same positions as in the sought number. *y* represents the number of digits of the experimental number that present in the sought number, but occupy different positions. Naturally, the experimental numbers, as well as the sought number, are represented by four-digit numbers where all digits are different and a leading zero can be present.
For example, let's suppose that the thinker thought of the number 0123. Then the guessers' experimental number 1263 will receive a reply "1 bull 2 cows" (3 occupies the same positions in both numbers and 1 and 2 are present in both numbers but they occupy different positions). Also, the answer to number 8103 will be "2 bulls 1 cow" (analogically, 1 and 3 occupy the same positions and 0 occupies a different one).
When the guesser is answered "4 bulls 0 cows", the game is over.
Now the guesser has already made several guesses and wants to know whether his next guess can possibly be the last one. | The first input line contains an integer *n* (1<=β€<=*n*<=β€<=10) which represents the number of already made guesses. Then follow *n* lines in the form of "*a**i* *b**i* *c**i*", where *a**i* is the *i*-th experimental number, *b**i* is the number of bulls, *c**i* is the number of cows (1<=β€<=*i*<=β€<=*n*, 0<=β€<=*b**i*,<=*c**i*,<=*b**i*<=+<=*c**i*<=β€<=4). The experimental numbers are correct, i.e., each of them contains exactly four digits, in each of them all the four digits are different, and there can be a leading zero. All the experimental numbers are different. As the guesser hasn't guessed the number yet, the answer "4 bulls 0 cows" is not present. | If the input data is enough to determine the sought number, print the number with four digits on a single line. If it has less than four digits, add leading zero. If the data is not enough, print "Need more data" without the quotes. If the thinker happens to have made a mistake in his replies, print "Incorrect data" without the quotes. | [
"2\n1263 1 2\n8103 2 1\n",
"2\n1234 2 2\n1256 0 2\n",
"2\n0123 1 1\n4567 1 2\n"
] | [
"Need more data",
"2134",
"Incorrect data"
] | none | [
{
"input": "2\n1263 1 2\n8103 2 1",
"output": "Need more data"
},
{
"input": "2\n1234 2 2\n1256 0 2",
"output": "2134"
},
{
"input": "2\n0123 1 1\n4567 1 2",
"output": "Incorrect data"
},
{
"input": "1\n1234 0 0",
"output": "Need more data"
},
{
"input": "4\n4789 ... | 248 | 7,577,600 | 3.923886 | 2,440 |
0 | none | [
"none"
] | null | null | Iahub likes trees very much. Recently he discovered an interesting tree named propagating tree. The tree consists of *n* nodes numbered from 1 to *n*, each node *i* having an initial value *a**i*. The root of the tree is node 1.
This tree has a special property: when a value *val* is added to a value of node *i*, the value -*val* is added to values of all the children of node *i*. Note that when you add value -*val* to a child of node *i*, you also add -(-*val*) to all children of the child of node *i* and so on. Look an example explanation to understand better how it works.
This tree supports two types of queries:
- "1 *x* *val*" β *val* is added to the value of node *x*; - "2 *x*" β print the current value of node *x*.
In order to help Iahub understand the tree better, you must answer *m* queries of the preceding type. | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=200000). The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=1000). Each of the next *n*β1 lines contains two integers *v**i* and *u**i* (1<=β€<=*v**i*,<=*u**i*<=β€<=*n*), meaning that there is an edge between nodes *v**i* and *u**i*.
Each of the next *m* lines contains a query in the format described above. It is guaranteed that the following constraints hold for all queries: 1<=β€<=*x*<=β€<=*n*,<=1<=β€<=*val*<=β€<=1000. | For each query of type two (print the value of node *x*) you must print the answer to the query on a separate line. The queries must be answered in the order given in the input. | [
"5 5\n1 2 1 1 2\n1 2\n1 3\n2 4\n2 5\n1 2 3\n1 1 2\n2 1\n2 2\n2 4\n"
] | [
"3\n3\n0\n"
] | The values of the nodes are [1,β2,β1,β1,β2] at the beginning.
Then value 3 is added to node 2. It propagates and value -3 is added to it's sons, node 4 and node 5. Then it cannot propagate any more. So the values of the nodes are [1,β5,β1,ββ-β2,ββ-β1].
Then value 2 is added to node 1. It propagates and value -2 is added to it's sons, node 2 and node 3. From node 2 it propagates again, adding value 2 to it's sons, node 4 and node 5. Node 3 has no sons, so it cannot propagate from there. The values of the nodes are [3,β3,ββ-β1,β0,β1].
You can see all the definitions about the tree at the following link: http://en.wikipedia.org/wiki/Tree_(graph_theory) | [
{
"input": "5 5\n1 2 1 1 2\n1 2\n1 3\n2 4\n2 5\n1 2 3\n1 1 2\n2 1\n2 2\n2 4",
"output": "3\n3\n0"
},
{
"input": "10 10\n137 197 856 768 825 894 86 174 218 326\n7 8\n4 7\n8 9\n7 10\n1 2\n2 4\n3 6\n3 5\n2 3\n1 9 624\n2 1\n2 4\n1 6 505\n1 8 467\n1 3 643\n2 1\n1 8 631\n2 4\n1 7 244",
"output": "137\... | 46 | 0 | 0 | 2,444 | |
652 | z-sort | [
"sortings"
] | null | null | A student of *z*-school found a kind of sorting called *z*-sort. The array *a* with *n* elements are *z*-sorted if two conditions hold:
1. *a**i*<=β₯<=*a**i*<=-<=1 for all even *i*, 1. *a**i*<=β€<=*a**i*<=-<=1 for all odd *i*<=><=1.
For example the arrays [1,2,1,2] and [1,1,1,1] are *z*-sorted while the array [1,2,3,4] isnβt *z*-sorted.
Can you make the array *z*-sorted? | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=1000) β the number of elements in the array *a*.
The second line contains *n* integers *a**i* (1<=β€<=*a**i*<=β€<=109) β the elements of the array *a*. | If it's possible to make the array *a* *z*-sorted print *n* space separated integers *a**i* β the elements after *z*-sort. Otherwise print the only word "Impossible". | [
"4\n1 2 2 1\n",
"5\n1 3 2 2 5\n"
] | [
"1 2 1 2\n",
"1 5 2 3 2\n"
] | none | [
{
"input": "4\n1 2 2 1",
"output": "1 2 1 2"
},
{
"input": "5\n1 3 2 2 5",
"output": "1 5 2 3 2"
},
{
"input": "1\n1",
"output": "1"
},
{
"input": "10\n1 1 1 1 1 1 1 1 1 1",
"output": "1 1 1 1 1 1 1 1 1 1"
},
{
"input": "10\n1 9 7 6 2 4 7 8 1 3",
"output": "1 ... | 62 | 4,608,000 | 0 | 2,445 | |
864 | Bus | [
"greedy",
"implementation",
"math"
] | null | null | A bus moves along the coordinate line *Ox* from the point *x*<==<=0 to the point *x*<==<=*a*. After starting from the point *x*<==<=0, it reaches the point *x*<==<=*a*, immediately turns back and then moves to the point *x*<==<=0. After returning to the point *x*<==<=0 it immediately goes back to the point *x*<==<=*a* and so on. Thus, the bus moves from *x*<==<=0 to *x*<==<=*a* and back. Moving from the point *x*<==<=0 to *x*<==<=*a* or from the point *x*<==<=*a* to *x*<==<=0 is called a bus journey. In total, the bus must make *k* journeys.
The petrol tank of the bus can hold *b* liters of gasoline. To pass a single unit of distance the bus needs to spend exactly one liter of gasoline. The bus starts its first journey with a full petrol tank.
There is a gas station in point *x*<==<=*f*. This point is between points *x*<==<=0 and *x*<==<=*a*. There are no other gas stations on the bus route. While passing by a gas station in either direction the bus can stop and completely refuel its tank. Thus, after stopping to refuel the tank will contain *b* liters of gasoline.
What is the minimum number of times the bus needs to refuel at the point *x*<==<=*f* to make *k* journeys? The first journey starts in the point *x*<==<=0. | The first line contains four integers *a*, *b*, *f*, *k* (0<=<<=*f*<=<<=*a*<=β€<=106, 1<=β€<=*b*<=β€<=109, 1<=β€<=*k*<=β€<=104) β the endpoint of the first bus journey, the capacity of the fuel tank of the bus, the point where the gas station is located, and the required number of journeys. | Print the minimum number of times the bus needs to refuel to make *k* journeys. If it is impossible for the bus to make *k* journeys, print -1. | [
"6 9 2 4\n",
"6 10 2 4\n",
"6 5 4 3\n"
] | [
"4\n",
"2\n",
"-1\n"
] | In the first example the bus needs to refuel during each journey.
In the second example the bus can pass 10 units of distance without refueling. So the bus makes the whole first journey, passes 4 units of the distance of the second journey and arrives at the point with the gas station. Then it can refuel its tank, finish the second journey and pass 2 units of distance from the third journey. In this case, it will again arrive at the point with the gas station. Further, he can refill the tank up to 10 liters to finish the third journey and ride all the way of the fourth journey. At the end of the journey the tank will be empty.
In the third example the bus can not make all 3 journeys because if it refuels during the second journey, the tanks will contain only 5 liters of gasoline, but the bus needs to pass 8 units of distance until next refueling. | [
{
"input": "6 9 2 4",
"output": "4"
},
{
"input": "6 10 2 4",
"output": "2"
},
{
"input": "6 5 4 3",
"output": "-1"
},
{
"input": "2 2 1 1",
"output": "0"
},
{
"input": "10 4 6 10",
"output": "-1"
},
{
"input": "3 1 1 1",
"output": "-1"
},
{
... | 62 | 0 | 0 | 2,446 | |
979 | Kuro and Walking Route | [
"dfs and similar",
"trees"
] | null | null | Kuro is living in a country called Uberland, consisting of $n$ towns, numbered from $1$ to $n$, and $n - 1$ bidirectional roads connecting these towns. It is possible to reach each town from any other. Each road connects two towns $a$ and $b$. Kuro loves walking and he is planning to take a walking marathon, in which he will choose a pair of towns $(u, v)$ ($u \neq v$) and walk from $u$ using the shortest path to $v$ (note that $(u, v)$ is considered to be different from $(v, u)$).
Oddly, there are 2 special towns in Uberland named Flowrisa (denoted with the index $x$) and Beetopia (denoted with the index $y$). Flowrisa is a town where there are many strong-scent flowers, and Beetopia is another town where many bees live. In particular, Kuro will avoid any pair of towns $(u, v)$ if on the path from $u$ to $v$, he reaches Beetopia after he reached Flowrisa, since the bees will be attracted with the flower smell on Kuroβs body and sting him.
Kuro wants to know how many pair of city $(u, v)$ he can take as his route. Since heβs not really bright, he asked you to help him with this problem. | The first line contains three integers $n$, $x$ and $y$ ($1 \leq n \leq 3 \cdot 10^5$, $1 \leq x, y \leq n$, $x \ne y$) - the number of towns, index of the town Flowrisa and index of the town Beetopia, respectively.
$n - 1$ lines follow, each line contains two integers $a$ and $b$ ($1 \leq a, b \leq n$, $a \ne b$), describes a road connecting two towns $a$ and $b$.
It is guaranteed that from each town, we can reach every other town in the city using the given roads. That is, the given map of towns and roads is a tree. | A single integer resembles the number of pair of towns $(u, v)$ that Kuro can use as his walking route. | [
"3 1 3\n1 2\n2 3\n",
"3 1 3\n1 2\n1 3\n"
] | [
"5",
"4"
] | On the first example, Kuro can choose these pairs:
- $(1, 2)$: his route would be $1 \rightarrow 2$, - $(2, 3)$: his route would be $2 \rightarrow 3$, - $(3, 2)$: his route would be $3 \rightarrow 2$, - $(2, 1)$: his route would be $2 \rightarrow 1$, - $(3, 1)$: his route would be $3 \rightarrow 2 \rightarrow 1$.
Kuro can't choose pair $(1, 3)$ since his walking route would be $1 \rightarrow 2 \rightarrow 3$, in which Kuro visits town $1$ (Flowrisa) and then visits town $3$ (Beetopia), which is not allowed (note that pair $(3, 1)$ is still allowed because although Kuro visited Flowrisa and Beetopia, he did not visit them in that order).
On the second example, Kuro can choose the following pairs:
- $(1, 2)$: his route would be $1 \rightarrow 2$, - $(2, 1)$: his route would be $2 \rightarrow 1$, - $(3, 2)$: his route would be $3 \rightarrow 1 \rightarrow 2$, - $(3, 1)$: his route would be $3 \rightarrow 1$. | [
{
"input": "3 1 3\n1 2\n2 3",
"output": "5"
},
{
"input": "3 1 3\n1 2\n1 3",
"output": "4"
},
{
"input": "61 26 12\n33 38\n32 8\n27 59\n1 21\n61 57\n61 22\n35 18\n61 14\n39 56\n50 10\n1 42\n21 43\n61 41\n31 30\n35 9\n23 28\n39 34\n39 4\n39 25\n27 60\n45 51\n1 11\n35 26\n29 15\n23 44\n31 ... | 1,949 | 38,912,000 | 0 | 2,447 | |
932 | Team Work | [
"combinatorics",
"dp",
"math"
] | null | null | You have a team of *N* people. For a particular task, you can pick any non-empty subset of people. The cost of having *x* people for the task is *x**k*.
Output the sum of costs over all non-empty subsets of people. | Only line of input contains two integers *N* (1<=β€<=*N*<=β€<=109) representing total number of people and *k* (1<=β€<=*k*<=β€<=5000). | Output the sum of costs for all non empty subsets modulo 109<=+<=7. | [
"1 1\n",
"3 2\n"
] | [
"1\n",
"24\n"
] | In the first example, there is only one non-empty subset {1} with cost 1<sup class="upper-index">1</sup>β=β1.
In the second example, there are seven non-empty subsets.
- {1} with cost 1<sup class="upper-index">2</sup>β=β1
- {2} with cost 1<sup class="upper-index">2</sup>β=β1
- {1,β2} with cost 2<sup class="upper-index">2</sup>β=β4
- {3} with cost 1<sup class="upper-index">2</sup>β=β1
- {1,β3} with cost 2<sup class="upper-index">2</sup>β=β4
- {2,β3} with cost 2<sup class="upper-index">2</sup>β=β4
- {1,β2,β3} with cost 3<sup class="upper-index">2</sup>β=β9
The total cost is 1β+β1β+β4β+β1β+β4β+β4β+β9β=β24. | [
{
"input": "1 1",
"output": "1"
},
{
"input": "3 2",
"output": "24"
},
{
"input": "5 3",
"output": "800"
},
{
"input": "12 4",
"output": "8067072"
},
{
"input": "20 5",
"output": "87486873"
},
{
"input": "522 4575",
"output": "558982611"
},
{
... | 2,000 | 307,200 | 0 | 2,448 | |
682 | Alyona and Mex | [
"sortings"
] | null | null | Someone gave Alyona an array containing *n* positive integers *a*1,<=*a*2,<=...,<=*a**n*. In one operation, Alyona can choose any element of the array and decrease it, i.e. replace with any positive integer that is smaller than the current one. Alyona can repeat this operation as many times as she wants. In particular, she may not apply any operation to the array at all.
Formally, after applying some operations Alyona will get an array of *n* positive integers *b*1,<=*b*2,<=...,<=*b**n* such that 1<=β€<=*b**i*<=β€<=*a**i* for every 1<=β€<=*i*<=β€<=*n*. Your task is to determine the maximum possible value of mex of this array.
Mex of an array in this problem is the minimum positive integer that doesn't appear in this array. For example, mex of the array containing 1, 3 and 4 is equal to 2, while mex of the array containing 2, 3 and 2 is equal to 1. | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=100<=000)Β β the number of elements in the Alyona's array.
The second line of the input contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109)Β β the elements of the array. | Print one positive integerΒ β the maximum possible value of mex of the array after Alyona applies some (possibly none) operations. | [
"5\n1 3 3 3 6\n",
"2\n2 1\n"
] | [
"5\n",
"3\n"
] | In the first sample case if one will decrease the second element value to 2 and the fifth element value to 4 then the mex value of resulting array 1 2 3 3 4 will be equal to 5.
To reach the answer to the second sample case one must not decrease any of the array elements. | [
{
"input": "5\n1 3 3 3 6",
"output": "5"
},
{
"input": "2\n2 1",
"output": "3"
},
{
"input": "1\n1",
"output": "2"
},
{
"input": "1\n1000000000",
"output": "2"
},
{
"input": "1\n2",
"output": "2"
},
{
"input": "2\n1 1",
"output": "2"
},
{
"... | 171 | 8,806,400 | 3 | 2,452 | |
0 | none | [
"none"
] | null | null | You have an array *a* with length *n*, you can perform operations. Each operation is like this: choose two adjacent elements from *a*, say *x* and *y*, and replace one of them with *gcd*(*x*,<=*y*), where *gcd* denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor).
What is the minimum number of operations you need to make all of the elements equal to 1? | The first line of the input contains one integer *n* (1<=β€<=*n*<=β€<=2000) β the number of elements in the array.
The second line contains *n* space separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109)Β β the elements of the array. | Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. | [
"5\n2 2 3 4 6\n",
"4\n2 4 6 8\n",
"3\n2 6 9\n"
] | [
"5\n",
"-1\n",
"4\n"
] | In the first sample you can turn all numbers to 1 using the following 5 moves:
- [2,β2,β3,β4,β6]. - [2,β1,β3,β4,β6] - [2,β1,β3,β1,β6] - [2,β1,β1,β1,β6] - [1,β1,β1,β1,β6] - [1,β1,β1,β1,β1]
We can prove that in this case it is not possible to make all numbers one using less than 5 moves. | [
{
"input": "5\n2 2 3 4 6",
"output": "5"
},
{
"input": "4\n2 4 6 8",
"output": "-1"
},
{
"input": "3\n2 6 9",
"output": "4"
},
{
"input": "15\n10 10 10 10 10 10 21 21 21 21 21 21 21 21 21",
"output": "15"
},
{
"input": "12\n10 10 14 14 14 14 14 14 14 14 21 21",
... | 857 | 6,758,400 | 0 | 2,456 | |
842 | Gleb And Pizza | [
"geometry"
] | null | null | Gleb ordered pizza home. When the courier delivered the pizza, he was very upset, because several pieces of sausage lay on the crust, and he does not really like the crust.
The pizza is a circle of radius *r* and center at the origin. Pizza consists of the main part β circle of radius *r*<=-<=*d* with center at the origin, and crust around the main part of the width *d*. Pieces of sausage are also circles. The radius of the *i*Β -th piece of the sausage is *r**i*, and the center is given as a pair (*x**i*, *y**i*).
Gleb asks you to help determine the number of pieces of sausage caught on the crust. A piece of sausage got on the crust, if it completely lies on the crust. | First string contains two integer numbers *r* and *d* (0<=β€<=*d*<=<<=*r*<=β€<=500)Β β the radius of pizza and the width of crust.
Next line contains one integer number *n*Β β the number of pieces of sausage (1<=β€<=*n*<=β€<=105).
Each of next *n* lines contains three integer numbers *x**i*, *y**i* and *r**i* (<=-<=500<=β€<=*x**i*,<=*y**i*<=β€<=500, 0<=β€<=*r**i*<=β€<=500), where *x**i* and *y**i* are coordinates of the center of *i*-th peace of sausage, *r**i*Β β radius of *i*-th peace of sausage. | Output the number of pieces of sausage that lay on the crust. | [
"8 4\n7\n7 8 1\n-7 3 2\n0 2 1\n0 -2 2\n-3 -3 1\n0 6 2\n5 3 1\n",
"10 8\n4\n0 0 9\n0 0 10\n1 0 1\n1 0 2\n"
] | [
"2\n",
"0\n"
] | Below is a picture explaining the first example. Circles of green color denote pieces of sausage lying on the crust. | [
{
"input": "8 4\n7\n7 8 1\n-7 3 2\n0 2 1\n0 -2 2\n-3 -3 1\n0 6 2\n5 3 1",
"output": "2"
},
{
"input": "10 8\n4\n0 0 9\n0 0 10\n1 0 1\n1 0 2",
"output": "0"
},
{
"input": "1 0\n1\n1 1 0",
"output": "0"
},
{
"input": "3 0\n5\n3 0 0\n0 3 0\n-3 0 0\n0 -3 0\n3 0 1",
"output": ... | 46 | 0 | 0 | 2,457 | |
500 | New Year Permutation | [
"dfs and similar",
"dsu",
"graphs",
"greedy",
"math",
"sortings"
] | null | null | User ainta has a permutation *p*1,<=*p*2,<=...,<=*p**n*. As the New Year is coming, he wants to make his permutation as pretty as possible.
Permutation *a*1,<=*a*2,<=...,<=*a**n* is prettier than permutation *b*1,<=*b*2,<=...,<=*b**n*, if and only if there exists an integer *k* (1<=β€<=*k*<=β€<=*n*) where *a*1<==<=*b*1,<=*a*2<==<=*b*2,<=...,<=*a**k*<=-<=1<==<=*b**k*<=-<=1 and *a**k*<=<<=*b**k* all holds.
As known, permutation *p* is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an *n*<=Γ<=*n* binary matrix *A*, user ainta can swap the values of *p**i* and *p**j* (1<=β€<=*i*,<=*j*<=β€<=*n*, *i*<=β <=*j*) if and only if *A**i*,<=*j*<==<=1.
Given the permutation *p* and the matrix *A*, user ainta wants to know the prettiest permutation that he can obtain. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=300) β the size of the permutation *p*.
The second line contains *n* space-separated integers *p*1,<=*p*2,<=...,<=*p**n* β the permutation *p* that user ainta has. Each integer between 1 and *n* occurs exactly once in the given permutation.
Next *n* lines describe the matrix *A*. The *i*-th line contains *n* characters '0' or '1' and describes the *i*-th row of *A*. The *j*-th character of the *i*-th line *A**i*,<=*j* is the element on the intersection of the *i*-th row and the *j*-th column of A. It is guaranteed that, for all integers *i*,<=*j* where 1<=β€<=*i*<=<<=*j*<=β€<=*n*, *A**i*,<=*j*<==<=*A**j*,<=*i* holds. Also, for all integers *i* where 1<=β€<=*i*<=β€<=*n*, *A**i*,<=*i*<==<=0 holds. | In the first and only line, print *n* space-separated integers, describing the prettiest permutation that can be obtained. | [
"7\n5 2 4 3 6 7 1\n0001001\n0000000\n0000010\n1000001\n0000000\n0010000\n1001000\n",
"5\n4 2 1 5 3\n00100\n00011\n10010\n01101\n01010\n"
] | [
"1 2 4 3 6 7 5\n",
"1 2 3 4 5\n"
] | In the first sample, the swap needed to obtain the prettiest permutation is: (*p*<sub class="lower-index">1</sub>,β*p*<sub class="lower-index">7</sub>).
In the second sample, the swaps needed to obtain the prettiest permutation is (*p*<sub class="lower-index">1</sub>,β*p*<sub class="lower-index">3</sub>),β(*p*<sub class="lower-index">4</sub>,β*p*<sub class="lower-index">5</sub>),β(*p*<sub class="lower-index">3</sub>,β*p*<sub class="lower-index">4</sub>).
A permutation *p* is a sequence of integers *p*<sub class="lower-index">1</sub>,β*p*<sub class="lower-index">2</sub>,β...,β*p*<sub class="lower-index">*n*</sub>, consisting of *n* distinct positive integers, each of them doesn't exceed *n*. The *i*-th element of the permutation *p* is denoted as *p*<sub class="lower-index">*i*</sub>. The size of the permutation *p* is denoted as *n*. | [
{
"input": "7\n5 2 4 3 6 7 1\n0001001\n0000000\n0000010\n1000001\n0000000\n0010000\n1001000",
"output": "1 2 4 3 6 7 5"
},
{
"input": "5\n4 2 1 5 3\n00100\n00011\n10010\n01101\n01010",
"output": "1 2 3 4 5"
},
{
"input": "7\n1 7 6 4 2 3 5\n0000100\n0000010\n0000001\n0000000\n1000000\n010... | 61 | 1,433,600 | 3 | 2,478 | |
611 | New Year and Domino | [
"dp",
"implementation"
] | null | null | They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so.
Limak is a little polar bear who loves to play. He has recently got a rectangular grid with *h* rows and *w* columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through *h* from top to bottom. Columns are numbered 1 through *w* from left to right.
Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid.
Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle? | The first line of the input contains two integers *h* and *w* (1<=β€<=*h*,<=*w*<=β€<=500)Β β the number of rows and the number of columns, respectively.
The next *h* lines describe a grid. Each line contains a string of the length *w*. Each character is either '.' or '#'Β β denoting an empty or forbidden cell, respectively.
The next line contains a single integer *q* (1<=β€<=*q*<=β€<=100<=000)Β β the number of queries.
Each of the next *q* lines contains four integers *r*1*i*, *c*1*i*, *r*2*i*, *c*2*i* (1<=β€<=*r*1*i*<=β€<=*r*2*i*<=β€<=*h*,<=1<=β€<=*c*1*i*<=β€<=*c*2*i*<=β€<=*w*)Β β the *i*-th query. Numbers *r*1*i* and *c*1*i* denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers *r*2*i* and *c*2*i* denote the row and the column (respectively) of the bottom right cell of the rectangle. | Print *q* integers, *i*-th should be equal to the number of ways to put a single domino inside the *i*-th rectangle. | [
"5 8\n....#..#\n.#......\n##.#....\n##..#.##\n........\n4\n1 1 2 3\n4 1 4 1\n1 2 4 5\n2 5 5 8\n",
"7 39\n.......................................\n.###..###..#..###.....###..###..#..###.\n...#..#.#..#..#.........#..#.#..#..#...\n.###..#.#..#..###.....###..#.#..#..###.\n.#....#.#..#....#.....#....#.#..#..#.#.\n.###... | [
"4\n0\n10\n15\n",
"53\n89\n120\n23\n0\n2\n"
] | A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways. | [
{
"input": "5 8\n....#..#\n.#......\n##.#....\n##..#.##\n........\n4\n1 1 2 3\n4 1 4 1\n1 2 4 5\n2 5 5 8",
"output": "4\n0\n10\n15"
},
{
"input": "7 39\n.......................................\n.###..###..#..###.....###..###..#..###.\n...#..#.#..#..#.........#..#.#..#..#...\n.###..#.#..#..###.....##... | 1,294 | 38,809,600 | 3 | 2,482 | |
244 | Undoubtedly Lucky Numbers | [
"bitmasks",
"brute force",
"dfs and similar"
] | null | null | Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits *x* and *y*. For example, if *x*<==<=4, and *y*<==<=7, then numbers 47, 744, 4 are lucky.
Let's call a positive integer *a* undoubtedly lucky, if there are such digits *x* and *y* (0<=β€<=*x*,<=*y*<=β€<=9), that the decimal representation of number *a* (without leading zeroes) contains only digits *x* and *y*.
Polycarpus has integer *n*. He wants to know how many positive integers that do not exceed *n*, are undoubtedly lucky. Help him, count this number. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=109) β Polycarpus's number. | Print a single integer that says, how many positive integers that do not exceed *n* are undoubtedly lucky. | [
"10\n",
"123\n"
] | [
"10\n",
"113\n"
] | In the first test sample all numbers that do not exceed 10 are undoubtedly lucky.
In the second sample numbers 102, 103, 104, 105, 106, 107, 108, 109, 120, 123 are not undoubtedly lucky. | [
{
"input": "10",
"output": "10"
},
{
"input": "123",
"output": "113"
},
{
"input": "1",
"output": "1"
},
{
"input": "2",
"output": "2"
},
{
"input": "1000",
"output": "352"
},
{
"input": "1000000000",
"output": "40744"
},
{
"input": "999999... | 434 | 30,003,200 | 3 | 2,483 | |
248 | Cupboards | [
"implementation"
] | null | null | One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.
Karlsson's gaze immediately fell on *n* wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.
And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.
Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds *t*, in which he is able to bring all the cupboard doors in the required position.
Your task is to write a program that will determine the required number of seconds *t*. | The first input line contains a single integer *n* β the number of cupboards in the kitchen (2<=β€<=*n*<=β€<=104). Then follow *n* lines, each containing two integers *l**i* and *r**i* (0<=β€<=*l**i*,<=*r**i*<=β€<=1). Number *l**i* equals one, if the left door of the *i*-th cupboard is opened, otherwise number *l**i* equals zero. Similarly, number *r**i* equals one, if the right door of the *i*-th cupboard is opened, otherwise number *r**i* equals zero.
The numbers in the lines are separated by single spaces. | In the only output line print a single integer *t* β the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs. | [
"5\n0 1\n1 0\n0 1\n1 1\n0 1\n"
] | [
"3\n"
] | none | [
{
"input": "5\n0 1\n1 0\n0 1\n1 1\n0 1",
"output": "3"
},
{
"input": "2\n0 0\n0 0",
"output": "0"
},
{
"input": "3\n0 1\n1 1\n1 1",
"output": "1"
},
{
"input": "8\n0 1\n1 0\n0 1\n1 1\n0 1\n1 0\n0 1\n1 0",
"output": "7"
},
{
"input": "8\n1 0\n1 0\n1 0\n0 1\n0 1\n1 ... | 154 | 0 | 3 | 2,489 | |
908 | New Year and Rainbow Roads | [
"graphs",
"greedy",
"implementation"
] | null | null | Roy and Biv have a set of *n* points on the infinite number line.
Each point has one of 3 colors: red, green, or blue.
Roy and Biv would like to connect all the points with some edges. Edges can be drawn between any of the two of the given points. The cost of an edge is equal to the distance between the two points it connects.
They want to do this in such a way that they will both see that all the points are connected (either directly or indirectly).
However, there is a catch: Roy cannot see the color red and Biv cannot see the color blue.
Therefore, they have to choose the edges in such a way that if all the red points are removed, the remaining blue and green points are connected (and similarly, if all the blue points are removed, the remaining red and green points are connected).
Help them compute the minimum cost way to choose edges to satisfy the above constraints. | The first line will contain an integer *n* (1<=β€<=*n*<=β€<=300<=000), the number of points.
The next *n* lines will contain two tokens *p**i* and *c**i* (*p**i* is an integer, 1<=β€<=*p**i*<=β€<=109, *c**i* is a uppercase English letter 'R', 'G' or 'B'), denoting the position of the *i*-th point and the color of the *i*-th point. 'R' means red, 'G' denotes green, and 'B' means blue. The positions will be in strictly increasing order. | Print a single integer, the minimum cost way to solve the problem. | [
"4\n1 G\n5 R\n10 B\n15 G\n",
"4\n1 G\n2 R\n3 B\n10 G\n"
] | [
"23\n",
"12\n"
] | In the first sample, it is optimal to draw edges between the points (1,2), (1,4), (3,4). These have costs 4, 14, 5, respectively. | [
{
"input": "4\n1 G\n5 R\n10 B\n15 G",
"output": "23"
},
{
"input": "4\n1 G\n2 R\n3 B\n10 G",
"output": "12"
},
{
"input": "4\n1 G\n123123 R\n987987987 B\n1000000000 G",
"output": "1012135134"
},
{
"input": "1\n3 R",
"output": "0"
}
] | 108 | 3,481,600 | 0 | 2,495 | |
765 | Neverending competitions | [
"implementation",
"math"
] | null | null | There are literally dozens of snooker competitions held each year, and team Jinotega tries to attend them all (for some reason they prefer name "snookah")! When a competition takes place somewhere far from their hometown, Ivan, Artsem and Konstantin take a flight to the contest and back.
Jinotega's best friends, team Base have found a list of their itinerary receipts with information about departure and arrival airports. Now they wonder, where is Jinotega now: at home or at some competition far away? They know that:
- this list contains all Jinotega's flights in this year (in arbitrary order), - Jinotega has only flown from his hometown to a snooker contest and back, - after each competition Jinotega flies back home (though they may attend a competition in one place several times), - and finally, at the beginning of the year Jinotega was at home.
Please help them to determine Jinotega's location! | In the first line of input there is a single integer *n*: the number of Jinotega's flights (1<=β€<=*n*<=β€<=100). In the second line there is a string of 3 capital Latin letters: the name of Jinotega's home airport. In the next *n* lines there is flight information, one flight per line, in form "XXX->YYY", where "XXX" is the name of departure airport "YYY" is the name of arrival airport. Exactly one of these airports is Jinotega's home airport.
It is guaranteed that flights information is consistent with the knowledge of Jinotega's friends, which is described in the main part of the statement. | If Jinotega is now at home, print "home" (without quotes), otherwise print "contest". | [
"4\nSVO\nSVO->CDG\nLHR->SVO\nSVO->LHR\nCDG->SVO\n",
"3\nSVO\nSVO->HKT\nHKT->SVO\nSVO->RAP\n"
] | [
"home\n",
"contest\n"
] | In the first sample Jinotega might first fly from SVO to CDG and back, and then from SVO to LHR and back, so now they should be at home. In the second sample Jinotega must now be at RAP because a flight from RAP back to SVO is not on the list. | [
{
"input": "4\nSVO\nSVO->CDG\nLHR->SVO\nSVO->LHR\nCDG->SVO",
"output": "home"
},
{
"input": "3\nSVO\nSVO->HKT\nHKT->SVO\nSVO->RAP",
"output": "contest"
},
{
"input": "1\nESJ\nESJ->TSJ",
"output": "contest"
},
{
"input": "2\nXMR\nFAJ->XMR\nXMR->FAJ",
"output": "home"
},
... | 109 | 0 | 3 | 2,496 | |
580 | Kefa and Company | [
"binary search",
"sortings",
"two pointers"
] | null | null | Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company.
Kefa has *n* friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn't want any friend to feel poor compared to somebody else in the company (Kefa doesn't count). A friend feels poor if in the company there is someone who has at least *d* units of money more than he does. Also, Kefa wants the total friendship factor of the members of the company to be maximum. Help him invite an optimal company! | The first line of the input contains two space-separated integers, *n* and *d* (1<=β€<=*n*<=β€<=105, ) β the number of Kefa's friends and the minimum difference between the amount of money in order to feel poor, respectively.
Next *n* lines contain the descriptions of Kefa's friends, the (*i*<=+<=1)-th line contains the description of the *i*-th friend of type *m**i*, *s**i* (0<=β€<=*m**i*,<=*s**i*<=β€<=109) β the amount of money and the friendship factor, respectively. | Print the maximum total friendship factir that can be reached. | [
"4 5\n75 5\n0 100\n150 20\n75 1\n",
"5 100\n0 7\n11 32\n99 10\n46 8\n87 54\n"
] | [
"100\n",
"111\n"
] | In the first sample test the most profitable strategy is to form a company from only the second friend. At all other variants the total degree of friendship will be worse.
In the second sample test we can take all the friends. | [
{
"input": "4 5\n75 5\n0 100\n150 20\n75 1",
"output": "100"
},
{
"input": "5 100\n0 7\n11 32\n99 10\n46 8\n87 54",
"output": "111"
},
{
"input": "1 1000000000\n15 12",
"output": "12"
},
{
"input": "5 1\n5 9\n2 10\n8 5\n18 12\n1 1",
"output": "12"
},
{
"input": "3... | 31 | 0 | 0 | 2,498 | |
845 | Luba And The Ticket | [
"brute force",
"greedy",
"implementation"
] | null | null | Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky.
The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. | You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β this string denotes Luba's ticket. The ticket can start with the digit 0. | Print one number β the minimum possible number of digits Luba needs to replace to make the ticket lucky. | [
"000000\n",
"123456\n",
"111000\n"
] | [
"0\n",
"2\n",
"1\n"
] | In the first example the ticket is already lucky, so the answer is 0.
In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required.
In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required. | [
{
"input": "000000",
"output": "0"
},
{
"input": "123456",
"output": "2"
},
{
"input": "111000",
"output": "1"
},
{
"input": "120111",
"output": "0"
},
{
"input": "999999",
"output": "0"
},
{
"input": "199880",
"output": "1"
},
{
"input": "... | 108 | 0 | 0 | 2,500 | |
445 | DZY Loves Chessboard | [
"dfs and similar",
"implementation"
] | null | null | DZY loves chessboard, and he enjoys playing with it.
He has a chessboard of *n* rows and *m* columns. Some cells of the chessboard are bad, others are good. For every good cell, DZY wants to put a chessman on it. Each chessman is either white or black. After putting all chessmen, DZY wants that no two chessmen with the same color are on two adjacent cells. Two cells are adjacent if and only if they share a common edge.
You task is to find any suitable placement of chessmen on the given chessboard. | The first line contains two space-separated integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=100).
Each of the next *n* lines contains a string of *m* characters: the *j*-th character of the *i*-th string is either "." or "-". A "." means that the corresponding cell (in the *i*-th row and the *j*-th column) is good, while a "-" means it is bad. | Output must contain *n* lines, each line must contain a string of *m* characters. The *j*-th character of the *i*-th string should be either "W", "B" or "-". Character "W" means the chessman on the cell is white, "B" means it is black, "-" means the cell is a bad cell.
If multiple answers exist, print any of them. It is guaranteed that at least one answer exists. | [
"1 1\n.\n",
"2 2\n..\n..\n",
"3 3\n.-.\n---\n--."
] | [
"B\n",
"BW\nWB\n",
"B-B\n---\n--B"
] | In the first sample, DZY puts a single black chessman. Of course putting a white one is also OK.
In the second sample, all 4 cells are good. No two same chessmen share an edge in the sample output.
In the third sample, no good cells are adjacent. So you can just put 3 chessmen, no matter what their colors are. | [
{
"input": "1 1\n.",
"output": "B"
},
{
"input": "2 2\n..\n..",
"output": "BW\nWB"
},
{
"input": "3 3\n.-.\n---\n--.",
"output": "B-B\n---\n--B"
},
{
"input": "3 1\n-\n.\n.",
"output": "-\nW\nB"
},
{
"input": "11 11\n-----------\n-----------\n-----------\n--------... | 140 | 5,427,200 | 3 | 2,503 | |
238 | Not Wool Sequences | [
"constructive algorithms",
"math"
] | null | null | A sequence of non-negative integers *a*1,<=*a*2,<=...,<=*a**n* of length *n* is called a wool sequence if and only if there exists two integers *l* and *r* (1<=β€<=*l*<=β€<=*r*<=β€<=*n*) such that . In other words each wool sequence contains a subsequence of consecutive elements with xor equal to 0.
The expression means applying the operation of a bitwise xor to numbers *x* and *y*. The given operation exists in all modern programming languages, for example, in languages C++ and Java it is marked as "^", in Pascal β as "xor".
In this problem you are asked to compute the number of sequences made of *n* integers from 0 to 2*m*<=-<=1 that are not a wool sequence. You should print this number modulo 1000000009 (109<=+<=9). | The only line of input contains two space-separated integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=105). | Print the required number of sequences modulo 1000000009 (109<=+<=9) on the only line of output. | [
"3 2\n"
] | [
"6\n"
] | Sequences of length 3 made of integers 0, 1, 2 and 3 that are not a wool sequence are (1, 3, 1), (1, 2, 1), (2, 1, 2), (2, 3, 2), (3, 1, 3) and (3, 2, 3). | [
{
"input": "3 2",
"output": "6"
},
{
"input": "4 2",
"output": "0"
},
{
"input": "1 2",
"output": "3"
},
{
"input": "4 11",
"output": "433239206"
},
{
"input": "5 100",
"output": "345449482"
},
{
"input": "5444 31525",
"output": "637906839"
},
... | 0 | 0 | -1 | 2,511 | |
609 | Minimum spanning tree for each edge | [
"data structures",
"dfs and similar",
"dsu",
"graphs",
"trees"
] | null | null | Connected undirected weighted graph without self-loops and multiple edges is given. Graph contains *n* vertices and *m* edges.
For each edge (*u*,<=*v*) find the minimal possible weight of the spanning tree that contains the edge (*u*,<=*v*).
The weight of the spanning tree is the sum of weights of all edges included in spanning tree. | First line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=2Β·105,<=*n*<=-<=1<=β€<=*m*<=β€<=2Β·105) β the number of vertices and edges in graph.
Each of the next *m* lines contains three integers *u**i*,<=*v**i*,<=*w**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*,<=*u**i*<=β <=*v**i*,<=1<=β€<=*w**i*<=β€<=109) β the endpoints of the *i*-th edge and its weight. | Print *m* lines. *i*-th line should contain the minimal possible weight of the spanning tree that contains *i*-th edge.
The edges are numbered from 1 to *m* in order of their appearing in input. | [
"5 7\n1 2 3\n1 3 1\n1 4 5\n2 3 2\n2 5 3\n3 4 2\n4 5 4\n"
] | [
"9\n8\n11\n8\n8\n8\n9\n"
] | none | [
{
"input": "5 7\n1 2 3\n1 3 1\n1 4 5\n2 3 2\n2 5 3\n3 4 2\n4 5 4",
"output": "9\n8\n11\n8\n8\n8\n9"
},
{
"input": "2 1\n1 2 42",
"output": "42"
},
{
"input": "3 3\n1 2 10\n2 3 20\n3 1 40",
"output": "30\n30\n50"
},
{
"input": "4 6\n1 2 999999001\n1 3 999999003\n1 4 999999009\... | 2,000 | 35,123,200 | 0 | 2,517 | |
0 | none | [
"none"
] | null | null | The main road in Bytecity is a straight line from south to north. Conveniently, there are coordinates measured in meters from the southernmost building in north direction.
At some points on the road there are *n* friends, and *i*-th of them is standing at the point *x**i* meters and can move with any speed no greater than *v**i* meters per second in any of the two directions along the road: south or north.
You are to compute the minimum time needed to gather all the *n* friends at some point on the road. Note that the point they meet at doesn't need to have integer coordinate. | The first line contains single integer *n* (2<=β€<=*n*<=β€<=60<=000)Β β the number of friends.
The second line contains *n* integers *x*1,<=*x*2,<=...,<=*x**n* (1<=β€<=*x**i*<=β€<=109)Β β the current coordinates of the friends, in meters.
The third line contains *n* integers *v*1,<=*v*2,<=...,<=*v**n* (1<=β€<=*v**i*<=β€<=109)Β β the maximum speeds of the friends, in meters per second. | Print the minimum time (in seconds) needed for all the *n* friends to meet at some point on the road.
Your answer will be considered correct, if its absolute or relative error isn't greater than 10<=-<=6. Formally, let your answer be *a*, while jury's answer be *b*. Your answer will be considered correct if holds. | [
"3\n7 1 3\n1 2 1\n",
"4\n5 10 3 2\n2 3 2 4\n"
] | [
"2.000000000000\n",
"1.400000000000\n"
] | In the first sample, all friends can gather at the point 5 within 2 seconds. In order to achieve this, the first friend should go south all the time at his maximum speed, while the second and the third friends should go north at their maximum speeds. | [
{
"input": "3\n7 1 3\n1 2 1",
"output": "2.000000000000"
},
{
"input": "4\n5 10 3 2\n2 3 2 4",
"output": "1.400000000000"
},
{
"input": "3\n1 1000000000 2\n1 2 1000000000",
"output": "333333332.999999999971"
},
{
"input": "2\n4 5\n10 8",
"output": "0.055555555556"
},
... | 5,000 | 0 | 0 | 2,522 | |
755 | PolandBall and Hypothesis | [
"brute force",
"graphs",
"math",
"number theory"
] | null | null | PolandBall is a young, clever Ball. He is interested in prime numbers. He has stated a following hypothesis: "There exists such a positive integer *n* that for each positive integer *m* number *n*Β·*m*<=+<=1 is a prime number".
Unfortunately, PolandBall is not experienced yet and doesn't know that his hypothesis is incorrect. Could you prove it wrong? Write a program that finds a counterexample for any *n*. | The only number in the input is *n* (1<=β€<=*n*<=β€<=1000)Β β number from the PolandBall's hypothesis. | Output such *m* that *n*Β·*m*<=+<=1 is not a prime number. Your answer will be considered correct if you output any suitable *m* such that 1<=β€<=*m*<=β€<=103. It is guaranteed the the answer exists. | [
"3\n",
"4\n"
] | [
"1",
"2"
] | A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.
For the first sample testcase, 3Β·1β+β1β=β4. We can output 1.
In the second sample testcase, 4Β·1β+β1β=β5. We cannot output 1 because 5 is prime. However, *m*β=β2 is okay since 4Β·2β+β1β=β9, which is not a prime number. | [
{
"input": "3",
"output": "1"
},
{
"input": "4",
"output": "2"
},
{
"input": "10",
"output": "2"
},
{
"input": "153",
"output": "1"
},
{
"input": "1000",
"output": "1"
},
{
"input": "1",
"output": "3"
},
{
"input": "2",
"output": "4"
... | 124 | 0 | 0 | 2,528 | |
245 | Game with Coins | [
"greedy"
] | null | null | Two pirates Polycarpus and Vasily play a very interesting game. They have *n* chests with coins, the chests are numbered with integers from 1 to *n*. Chest number *i* has *a**i* coins.
Polycarpus and Vasily move in turns. Polycarpus moves first. During a move a player is allowed to choose a positive integer *x* (2Β·*x*<=+<=1<=β€<=*n*) and take a coin from each chest with numbers *x*, 2Β·*x*, 2Β·*x*<=+<=1. It may turn out that some chest has no coins, in this case the player doesn't take a coin from this chest. The game finishes when all chests get emptied.
Polycarpus isn't a greedy scrooge. Polycarpys is a lazy slob. So he wonders in what minimum number of moves the game can finish. Help Polycarpus, determine the minimum number of moves in which the game can finish. Note that Polycarpus counts not only his moves, he also counts Vasily's moves. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=100) β the number of chests with coins. The second line contains a sequence of space-separated integers: *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=1000), where *a**i* is the number of coins in the chest number *i* at the beginning of the game. | Print a single integer β the minimum number of moves needed to finish the game. If no sequence of turns leads to finishing the game, print -1. | [
"1\n1\n",
"3\n1 2 3\n"
] | [
"-1\n",
"3\n"
] | In the first test case there isn't a single move that can be made. That's why the players won't be able to empty the chests.
In the second sample there is only one possible move *x*β=β1. This move should be repeated at least 3 times to empty the third chest. | [
{
"input": "1\n1",
"output": "-1"
},
{
"input": "3\n1 2 3",
"output": "3"
},
{
"input": "100\n269 608 534 956 993 409 297 735 258 451 468 422 125 407 580 769 857 383 419 67 377 230 842 113 169 427 287 75 372 133 456 450 644 303 638 40 217 445 427 730 168 341 371 633 237 951 142 596 528 5... | 186 | 6,963,200 | 0 | 2,530 | |
284 | Cows and Poker Game | [
"brute force",
"implementation"
] | null | null | There are *n* cows playing poker at a table. For the current betting phase, each player's status is either "ALLIN", "IN", or "FOLDED", and does not change throughout the phase. To increase the suspense, a player whose current status is not "FOLDED" may show his/her hand to the table. However, so as not to affect any betting decisions, he/she may only do so if all other players have a status of either "ALLIN" or "FOLDED". The player's own status may be either "ALLIN" or "IN".
Find the number of cows that can currently show their hands without affecting any betting decisions. | The first line contains a single integer, *n* (2<=β€<=*n*<=β€<=2Β·105). The second line contains *n* characters, each either "A", "I", or "F". The *i*-th character is "A" if the *i*-th player's status is "ALLIN", "I" if the *i*-th player's status is "IN", or "F" if the *i*-th player's status is "FOLDED". | The first line should contain a single integer denoting the number of players that can currently show their hands. | [
"6\nAFFAAA\n",
"3\nAFI\n"
] | [
"4\n",
"1\n"
] | In the first sample, cows 1, 4, 5, and 6 can show their hands. In the second sample, only cow 3 can show her hand. | [
{
"input": "6\nAFFAAA",
"output": "4"
},
{
"input": "3\nAFI",
"output": "1"
},
{
"input": "3\nFFF",
"output": "0"
},
{
"input": "3\nFIF",
"output": "1"
},
{
"input": "3\nAAA",
"output": "3"
},
{
"input": "3\nIII",
"output": "0"
},
{
"input"... | 374 | 1,126,400 | 3 | 2,534 | |
447 | DZY Loves Hash | [
"implementation"
] | null | null | DZY has a hash table with *p* buckets, numbered from 0 to *p*<=-<=1. He wants to insert *n* numbers, in the order they are given, into the hash table. For the *i*-th number *x**i*, DZY will put it into the bucket numbered *h*(*x**i*), where *h*(*x*) is the hash function. In this problem we will assume, that *h*(*x*)<==<=*x*Β *mod*Β *p*. Operation *a*Β *mod*Β *b* denotes taking a remainder after division *a* by *b*.
However, each bucket can contain no more than one element. If DZY wants to insert an number into a bucket which is already filled, we say a "conflict" happens. Suppose the first conflict happens right after the *i*-th insertion, you should output *i*. If no conflict happens, just output -1. | The first line contains two integers, *p* and *n* (2<=β€<=*p*,<=*n*<=β€<=300). Then *n* lines follow. The *i*-th of them contains an integer *x**i* (0<=β€<=*x**i*<=β€<=109). | Output a single integer β the answer to the problem. | [
"10 5\n0\n21\n53\n41\n53\n",
"5 5\n0\n1\n2\n3\n4\n"
] | [
"4\n",
"-1\n"
] | none | [
{
"input": "10 5\n0\n21\n53\n41\n53",
"output": "4"
},
{
"input": "5 5\n0\n1\n2\n3\n4",
"output": "-1"
},
{
"input": "10 6\n811966798\n734823552\n790326404\n929189974\n414343256\n560346537",
"output": "4"
},
{
"input": "2 2\n788371161\n801743052",
"output": "-1"
},
{
... | 62 | 0 | 3 | 2,535 | |
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... | 108 | 1,945,600 | 3 | 2,536 | |
821 | Okabe and Boxes | [
"data structures",
"greedy",
"trees"
] | null | null | Okabe and Super Hacker Daru are stacking and removing boxes. There are *n* boxes numbered from 1 to *n*. Initially there are no boxes on the stack.
Okabe, being a control freak, gives Daru 2*n* commands: *n* of which are to add a box to the top of the stack, and *n* of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to *n*. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.
That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.
Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed. | The first line of input contains the integer *n* (1<=β€<=*n*<=β€<=3Β·105)Β β the number of boxes.
Each of the next 2*n* lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer *x* (1<=β€<=*x*<=β€<=*n*) follows, indicating that Daru should add the box with number *x* to the top of the stack.
It is guaranteed that exactly *n* lines contain "add" operations, all the boxes added are distinct, and *n* lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed. | Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands. | [
"3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove\n",
"7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove\n"
] | [
"1\n",
"2\n"
] | In the first sample, Daru should reorder the boxes after adding box 3 to the stack.
In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack. | [
{
"input": "3\nadd 1\nremove\nadd 2\nadd 3\nremove\nremove",
"output": "1"
},
{
"input": "7\nadd 3\nadd 2\nadd 1\nremove\nadd 4\nremove\nremove\nremove\nadd 6\nadd 7\nadd 5\nremove\nremove\nremove",
"output": "2"
},
{
"input": "4\nadd 1\nadd 3\nremove\nadd 4\nadd 2\nremove\nremove\nremov... | 3,000 | 30,822,400 | 0 | 2,539 | |
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... | 93 | 9,318,400 | 3 | 2,540 | |
963 | Alternating Sum | [
"math",
"number theory"
] | null | null | You are given two integers $a$ and $b$. Moreover, you are given a sequence $s_0, s_1, \dots, s_{n}$. All values in $s$ are integers $1$ or $-1$. It's known that sequence is $k$-periodic and $k$ divides $n+1$. In other words, for each $k \leq i \leq n$ it's satisfied that $s_{i} = s_{i - k}$.
Find out the non-negative remainder of division of $\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i}$ by $10^{9} + 9$.
Note that the modulo is unusual! | The first line contains four integers $n, a, b$ and $k$ $(1 \leq n \leq 10^{9}, 1 \leq a, b \leq 10^{9}, 1 \leq k \leq 10^{5})$.
The second line contains a sequence of length $k$ consisting of characters '+' and '-'.
If the $i$-th character (0-indexed) is '+', then $s_{i} = 1$, otherwise $s_{i} = -1$.
Note that only the first $k$ members of the sequence are given, the rest can be obtained using the periodicity property. | Output a single integerΒ β value of given expression modulo $10^{9} + 9$. | [
"2 2 3 3\n+-+\n",
"4 1 5 1\n-\n"
] | [
"7\n",
"999999228\n"
] | In the first example:
$(\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i})$ = $2^{2} 3^{0} - 2^{1} 3^{1} + 2^{0} 3^{2}$ = 7
In the second example:
$(\sum \limits_{i=0}^{n} s_{i} a^{n - i} b^{i}) = -1^{4} 5^{0} - 1^{3} 5^{1} - 1^{2} 5^{2} - 1^{1} 5^{3} - 1^{0} 5^{4} = -781 \equiv 999999228 \pmod{10^{9} + 9}$. | [
{
"input": "2 2 3 3\n+-+",
"output": "7"
},
{
"input": "4 1 5 1\n-",
"output": "999999228"
},
{
"input": "1 1 4 2\n-+",
"output": "3"
},
{
"input": "3 1 4 4\n+--+",
"output": "45"
},
{
"input": "5 1 1 6\n++---+",
"output": "0"
},
{
"input": "5 2 2 6\n+... | 1,000 | 0 | 0 | 2,545 | |
0 | none | [
"none"
] | null | null | One day, little Vasya found himself in a maze consisting of (*n*<=+<=1) rooms, numbered from 1 to (*n*<=+<=1). Initially, Vasya is at the first room and to get out of the maze, he needs to get to the (*n*<=+<=1)-th one.
The maze is organized as follows. Each room of the maze has two one-way portals. Let's consider room number *i* (1<=β€<=*i*<=β€<=*n*), someone can use the first portal to move from it to room number (*i*<=+<=1), also someone can use the second portal to move from it to room number *p**i*, where 1<=β€<=*p**i*<=β€<=*i*.
In order not to get lost, Vasya decided to act as follows.
- Each time Vasya enters some room, he paints a cross on its ceiling. Initially, Vasya paints a cross at the ceiling of room 1. - Let's assume that Vasya is in room *i* and has already painted a cross on its ceiling. Then, if the ceiling now contains an odd number of crosses, Vasya uses the second portal (it leads to room *p**i*), otherwise Vasya uses the first portal.
Help Vasya determine the number of times he needs to use portals to get to room (*n*<=+<=1) in the end. | The first line contains integer *n* (1<=β€<=*n*<=β€<=103)Β β the number of rooms. The second line contains *n* integers *p**i* (1<=β€<=*p**i*<=β€<=*i*). Each *p**i* denotes the number of the room, that someone can reach, if he will use the second portal in the *i*-th room. | Print a single number β the number of portal moves the boy needs to go out of the maze. As the number can be rather large, print it modulo 1000000007 (109<=+<=7). | [
"2\n1 2\n",
"4\n1 1 2 3\n",
"5\n1 1 1 1 1\n"
] | [
"4\n",
"20\n",
"62\n"
] | none | [
{
"input": "2\n1 2",
"output": "4"
},
{
"input": "4\n1 1 2 3",
"output": "20"
},
{
"input": "5\n1 1 1 1 1",
"output": "62"
},
{
"input": "7\n1 2 1 3 1 2 1",
"output": "154"
},
{
"input": "1\n1",
"output": "2"
},
{
"input": "3\n1 1 3",
"output": "8"... | 1,000 | 0 | 0 | 2,548 | |
319 | Malek Dance Club | [
"combinatorics",
"math"
] | null | null | As a tradition, every year before IOI all the members of Natalia Fan Club are invited to Malek Dance Club to have a fun night together. Malek Dance Club has 2*n* members and coincidentally Natalia Fan Club also has 2*n* members. Each member of MDC is assigned a unique id *i* from 0 to 2*n*<=-<=1. The same holds for each member of NFC.
One of the parts of this tradition is one by one dance, where each member of MDC dances with a member of NFC. A dance pair is a pair of numbers (*a*,<=*b*) such that member *a* from MDC dances with member *b* from NFC.
The complexity of a pairs' assignment is the number of pairs of dancing pairs (*a*,<=*b*) and (*c*,<=*d*) such that *a*<=<<=*c* and *b*<=><=*d*.
You are given a binary number of length *n* named *x*. We know that member *i* from MDC dances with member from NFC. Your task is to calculate the complexity of this assignment modulo 1000000007 (109<=+<=7).
Expression denotes applying Β«XORΒ» to numbers *x* and *y*. This operation exists in all modern programming languages, for example, in C++ and Java it denotes as Β«^Β», in Pascal β Β«xorΒ». | The first line of input contains a binary number *x* of lenght *n*, (1<=β€<=*n*<=β€<=100).
This number may contain leading zeros. | Print the complexity of the given dance assignent modulo 1000000007 (109<=+<=7). | [
"11\n",
"01\n",
"1\n"
] | [
"6\n",
"2\n",
"1\n"
] | none | [
{
"input": "11",
"output": "6"
},
{
"input": "01",
"output": "2"
},
{
"input": "1",
"output": "1"
},
{
"input": "1111111111111111111111111111111111",
"output": "68817500"
},
{
"input": "0000000000000000000000000000000000000",
"output": "0"
},
{
"input"... | 62 | 0 | 0 | 2,551 | |
845 | Chess Tourney | [
"implementation",
"sortings"
] | null | null | Berland annual chess tournament is coming!
Organizers have gathered 2Β·*n* chess players who should be divided into two teams with *n* people each. The first team is sponsored by BerOil and the second team is sponsored by BerMobile. Obviously, organizers should guarantee the win for the team of BerOil.
Thus, organizers should divide all 2Β·*n* players into two teams with *n* people each in such a way that the first team always wins.
Every chess player has its rating *r**i*. It is known that chess player with the greater rating always wins the player with the lower rating. If their ratings are equal then any of the players can win.
After teams assignment there will come a drawing to form *n* pairs of opponents: in each pair there is a player from the first team and a player from the second team. Every chess player should be in exactly one pair. Every pair plays once. The drawing is totally random.
Is it possible to divide all 2Β·*n* players into two teams with *n* people each so that the player from the first team in every pair wins regardless of the results of the drawing? | The first line contains one integer *n* (1<=β€<=*n*<=β€<=100).
The second line contains 2Β·*n* integers *a*1,<=*a*2,<=... *a*2*n* (1<=β€<=*a**i*<=β€<=1000). | If it's possible to divide all 2Β·*n* players into two teams with *n* people each so that the player from the first team in every pair wins regardless of the results of the drawing, then print "YES". Otherwise print "NO". | [
"2\n1 3 2 4\n",
"1\n3 3\n"
] | [
"YES\n",
"NO\n"
] | none | [
{
"input": "2\n1 3 2 4",
"output": "YES"
},
{
"input": "1\n3 3",
"output": "NO"
},
{
"input": "5\n1 1 1 1 2 2 3 3 3 3",
"output": "NO"
},
{
"input": "5\n1 1 1 1 1 2 2 2 2 2",
"output": "YES"
},
{
"input": "10\n1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000... | 62 | 5,632,000 | 3 | 2,552 | |
90 | African Crossword | [
"implementation",
"strings"
] | B. African Crossword | 2 | 256 | An African crossword is a rectangular table *n*<=Γ<=*m* in size. Each cell of the table contains exactly one letter. This table (it is also referred to as grid) contains some encrypted word that needs to be decoded.
To solve the crossword you should cross out all repeated letters in rows and columns. In other words, a letter should only be crossed out if and only if the corresponding column or row contains at least one more letter that is exactly the same. Besides, all such letters are crossed out simultaneously.
When all repeated letters have been crossed out, we should write the remaining letters in a string. The letters that occupy a higher position follow before the letters that occupy a lower position. If the letters are located in one row, then the letter to the left goes first. The resulting word is the answer to the problem.
You are suggested to solve an African crossword and print the word encrypted there. | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=100). Next *n* lines contain *m* lowercase Latin letters each. That is the crossword grid. | Print the encrypted word on a single line. It is guaranteed that the answer consists of at least one letter. | [
"3 3\ncba\nbcd\ncbc\n",
"5 5\nfcofd\nooedo\nafaoa\nrdcdf\neofsf\n"
] | [
"abcd",
"codeforces"
] | none | [
{
"input": "3 3\ncba\nbcd\ncbc",
"output": "abcd"
},
{
"input": "5 5\nfcofd\nooedo\nafaoa\nrdcdf\neofsf",
"output": "codeforces"
},
{
"input": "4 4\nusah\nusha\nhasu\nsuha",
"output": "ahhasusu"
},
{
"input": "7 5\naabcd\neffgh\niijkk\nlmnoo\npqqrs\nttuvw\nxxyyz",
"output... | 62 | 307,200 | -1 | 2,554 |
985 | Liebig's Barrels | [
"greedy"
] | null | null | You have *m*<==<=*n*Β·*k* wooden staves. The *i*-th stave has length *a**i*. You have to assemble *n* barrels consisting of *k* staves each, you can use any *k* staves to construct a barrel. Each stave must belong to exactly one barrel.
Let volume *v**j* of barrel *j* be equal to the length of the minimal stave in it.
You want to assemble exactly *n* barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed *l*, i.e. |*v**x*<=-<=*v**y*|<=β€<=*l* for any 1<=β€<=*x*<=β€<=*n* and 1<=β€<=*y*<=β€<=*n*.
Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above. | The first line contains three space-separated integers *n*, *k* and *l* (1<=β€<=*n*,<=*k*<=β€<=105, 1<=β€<=*n*Β·*k*<=β€<=105, 0<=β€<=*l*<=β€<=109).
The second line contains *m*<==<=*n*Β·*k* space-separated integers *a*1,<=*a*2,<=...,<=*a**m* (1<=β€<=*a**i*<=β€<=109) β lengths of staves. | Print single integer β maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly *n* barrels satisfying the condition |*v**x*<=-<=*v**y*|<=β€<=*l* for any 1<=β€<=*x*<=β€<=*n* and 1<=β€<=*y*<=β€<=*n*. | [
"4 2 1\n2 2 1 2 3 2 2 3\n",
"2 1 0\n10 10\n",
"1 2 1\n5 2\n",
"3 2 1\n1 2 3 4 5 6\n"
] | [
"7\n",
"20\n",
"2\n",
"0\n"
] | In the first example you can form the following barrels: [1,β2], [2,β2], [2,β3], [2,β3].
In the second example you can form the following barrels: [10], [10].
In the third example you can form the following barrels: [2,β5].
In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough. | [
{
"input": "4 2 1\n2 2 1 2 3 2 2 3",
"output": "7"
},
{
"input": "2 1 0\n10 10",
"output": "20"
},
{
"input": "1 2 1\n5 2",
"output": "2"
},
{
"input": "3 2 1\n1 2 3 4 5 6",
"output": "0"
},
{
"input": "10 3 189\n267 697 667 4 52 128 85 616 142 344 413 660 962 194... | 218 | 8,499,200 | 3 | 2,559 | |
704 | Ant Man | [
"dp",
"graphs",
"greedy"
] | null | null | Scott Lang is at war with Darren Cross. There are *n* chairs in a hall where they are, numbered with 1,<=2,<=...,<=*n* from left to right. The *i*-th chair is located at coordinate *x**i*. Scott is on chair number *s* and Cross is on chair number *e*. Scott can jump to all other chairs (not only neighboring chairs). He wants to start at his position (chair number *s*), visit each chair exactly once and end up on chair number *e* with Cross.
As we all know, Scott can shrink or grow big (grow big only to his normal size), so at any moment of time he can be either small or large (normal). The thing is, he can only shrink or grow big while being on a chair (not in the air while jumping to another chair). Jumping takes time, but shrinking and growing big takes no time. Jumping from chair number *i* to chair number *j* takes |*x**i*<=-<=*x**j*| seconds. Also, jumping off a chair and landing on a chair takes extra amount of time.
If Scott wants to jump to a chair on his left, he can only be small, and if he wants to jump to a chair on his right he should be large.
Jumping off the *i*-th chair takes:
- *c**i* extra seconds if he's small. - *d**i* extra seconds otherwise (he's large).
Also, landing on *i*-th chair takes:
- *b**i* extra seconds if he's small. - *a**i* extra seconds otherwise (he's large).
In simpler words, jumping from *i*-th chair to *j*-th chair takes exactly:
- |*x**i*<=-<=*x**j*|<=+<=*c**i*<=+<=*b**j* seconds if *j*<=<<=*i*. - |*x**i*<=-<=*x**j*|<=+<=*d**i*<=+<=*a**j* seconds otherwise (*j*<=><=*i*).
Given values of *x*, *a*, *b*, *c*, *d* find the minimum time Scott can get to Cross, assuming he wants to visit each chair exactly once. | The first line of the input contains three integers *n*,<=*s* and *e* (2<=β€<=*n*<=β€<=5000,<=1<=β€<=*s*,<=*e*<=β€<=*n*,<=*s*<=β <=*e*)Β β the total number of chairs, starting and ending positions of Scott.
The second line contains *n* integers *x*1,<=*x*2,<=...,<=*x**n* (1<=β€<=*x*1<=<<=*x*2<=<<=...<=<<=*x**n*<=β€<=109).
The third line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a*1,<=*a*2,<=...,<=*a**n*<=β€<=109).
The fourth line contains *n* integers *b*1,<=*b*2,<=...,<=*b**n* (1<=β€<=*b*1,<=*b*2,<=...,<=*b**n*<=β€<=109).
The fifth line contains *n* integers *c*1,<=*c*2,<=...,<=*c**n* (1<=β€<=*c*1,<=*c*2,<=...,<=*c**n*<=β€<=109).
The sixth line contains *n* integers *d*1,<=*d*2,<=...,<=*d**n* (1<=β€<=*d*1,<=*d*2,<=...,<=*d**n*<=β€<=109). | Print the minimum amount of time Scott needs to get to the Cross while visiting each chair exactly once. | [
"7 4 3\n8 11 12 16 17 18 20\n17 16 20 2 20 5 13\n17 8 8 16 12 15 13\n12 4 16 4 15 7 6\n8 14 2 11 17 12 8\n"
] | [
"139\n"
] | In the sample testcase, an optimal solution would be <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5bbd3e094ffa5a72e263dfaec7aeaff795bc22a3.png" style="max-width: 100.0%;max-height: 100.0%;"/>. Spent time would be 17β+β24β+β23β+β20β+β33β+β22β=β139. | [
{
"input": "7 4 3\n8 11 12 16 17 18 20\n17 16 20 2 20 5 13\n17 8 8 16 12 15 13\n12 4 16 4 15 7 6\n8 14 2 11 17 12 8",
"output": "139"
},
{
"input": "2 1 2\n75475634 804928248\n476927808 284875072\n503158867 627937890\n322595515 786026685\n645468307 669240390",
"output": "1659795993"
},
{
... | 62 | 204,800 | 0 | 2,562 | |
1,000 | Covered Points Count | [
"data structures",
"implementation",
"sortings"
] | null | null | You are given $n$ segments on a coordinate line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide.
Your task is the following: for every $k \in [1..n]$, calculate the number of points with integer coordinates such that the number of segments that cover these points equals $k$. A segment with endpoints $l_i$ and $r_i$ covers point $x$ if and only if $l_i \le x \le r_i$. | The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of segments.
The next $n$ lines contain segments. The $i$-th line contains a pair of integers $l_i, r_i$ ($0 \le l_i \le r_i \le 10^{18}$) β the endpoints of the $i$-th segment. | Print $n$ space separated integers $cnt_1, cnt_2, \dots, cnt_n$, where $cnt_i$ is equal to the number of points such that the number of segments that cover these points equals to $i$. | [
"3\n0 3\n1 3\n3 8\n",
"3\n1 3\n2 4\n5 7\n"
] | [
"6 2 1 \n",
"5 2 0 \n"
] | The picture describing the first example:
<img class="tex-graphics" src="https://espresso.codeforces.com/f76b3fe547bff6be5b14de76c8b78ba3efecc744.png" style="max-width: 100.0%;max-height: 100.0%;"/>
Points with coordinates $[0, 4, 5, 6, 7, 8]$ are covered by one segment, points $[1, 2]$ are covered by two segments and point $[3]$ is covered by three segments.
The picture describing the second example:
<img class="tex-graphics" src="https://espresso.codeforces.com/6e9332c303e1bc5d6cf34c2d6c5e2a19c9417289.png" style="max-width: 100.0%;max-height: 100.0%;"/>
Points $[1, 4, 5, 6, 7]$ are covered by one segment, points $[2, 3]$ are covered by two segments and there are no points covered by three segments. | [
{
"input": "3\n0 3\n1 3\n3 8",
"output": "6 2 1 "
},
{
"input": "3\n1 3\n2 4\n5 7",
"output": "5 2 0 "
},
{
"input": "1\n0 1000000000000000000",
"output": "1000000000000000001 "
}
] | 31 | 0 | 0 | 2,565 | |
603 | Alternative Thinking | [
"dp",
"greedy",
"math"
] | null | null | Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length *n*. Each character of Kevin's string represents Kevin's score on one of the *n* questions of the olympiadβ'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0,<=1,<=0,<=1}, {1,<=0,<=1}, and {1,<=0,<=1,<=0} are alternating sequences, while {1,<=0,<=0} and {0,<=1,<=0,<=1,<=1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substringβthat is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have. | The first line contains the number of questions on the olympiad *n* (1<=β€<=*n*<=β€<=100<=000).
The following line contains a binary string of length *n* representing Kevin's results on the USAICO. | Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring. | [
"8\n10000011\n",
"2\n01\n"
] | [
"5\n",
"2\n"
] | In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | [
{
"input": "8\n10000011",
"output": "5"
},
{
"input": "2\n01",
"output": "2"
},
{
"input": "5\n10101",
"output": "5"
},
{
"input": "75\n010101010101010101010101010101010101010101010101010101010101010101010101010",
"output": "75"
},
{
"input": "11\n00000000000",
... | 46 | 716,800 | -1 | 2,568 | |
83 | Magical Array | [
"math"
] | A. Magical Array | 2 | 256 | Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical.
Valera absolutely accidentally got a piece of ancient parchment on which an array of numbers was written. He immediately thought that the numbers in this array were not random. As a result of extensive research Valera worked out a wonderful property that a magical array should have: an array is defined as magic if its minimum and maximum coincide.
He decided to share this outstanding discovery with you, but he asks you for help in return. Despite the tremendous intelligence and wit, Valera counts very badly and so you will have to complete his work. All you have to do is count the number of magical subarrays of the original array of numbers, written on the parchment. Subarray is defined as non-empty sequence of consecutive elements. | The first line of the input data contains an integer *n* (1<=β€<=*n*<=β€<=105). The second line contains an array of original integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=β€<=*a**i*<=β€<=109). | Print on the single line the answer to the problem: the amount of subarrays, which are magical.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator). | [
"4\n2 1 1 4\n",
"5\n-2 -2 -2 0 1\n"
] | [
"5\n",
"8\n"
] | Notes to sample tests:
Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end.
In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3].
In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3]. | [
{
"input": "4\n2 1 1 4",
"output": "5"
},
{
"input": "5\n-2 -2 -2 0 1",
"output": "8"
},
{
"input": "1\n10",
"output": "1"
},
{
"input": "2\n5 6",
"output": "2"
},
{
"input": "5\n5 5 4 5 5",
"output": "7"
},
{
"input": "8\n1 2 0 0 0 0 3 3",
"output... | 92 | 0 | -1 | 2,572 |
332 | Students' Revenge | [
"data structures",
"greedy",
"sortings"
] | null | null | A student's life is fraught with complications. Some Berland University students know this only too well. Having studied for two years, they contracted strong antipathy towards the chairperson of some department. Indeed, the person in question wasn't the kindest of ladies to begin with: prone to reforming groups, banning automatic passes and other mean deeds. At last the students decided that she just can't get away with all this anymore...
The students pulled some strings on the higher levels and learned that the next University directors' meeting is going to discuss *n* orders about the chairperson and accept exactly *p* of them. There are two values assigned to each order: *a**i* is the number of the chairperson's hairs that turn grey if she obeys the order and *b**i* β the displeasement of the directors if the order isn't obeyed. The students may make the directors pass any *p* orders chosen by them. The students know that the chairperson will obey exactly *k* out of these *p* orders. She will pick the orders to obey in the way that minimizes first, the directors' displeasement and second, the number of hairs on her head that turn grey.
The students want to choose *p* orders in the way that maximizes the number of hairs on the chairperson's head that turn grey. If there are multiple ways to accept the orders, then the students are keen on maximizing the directors' displeasement with the chairperson's actions. Help them. | The first line contains three integers *n* (1<=β€<=*n*<=β€<=105), *p* (1<=β€<=*p*<=β€<=*n*), *k* (1<=β€<=*k*<=β€<=*p*) β the number of orders the directors are going to discuss, the number of orders to pass and the number of orders to be obeyed by the chairperson, correspondingly. Each of the following *n* lines contains two integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=109), describing the corresponding order. | Print in an arbitrary order *p* distinct integers β the numbers of the orders to accept so that the students could carry out the revenge. The orders are indexed from 1 to *n* in the order they occur in the input. If there are multiple solutions, you can print any of them. | [
"5 3 2\n5 6\n5 8\n1 3\n4 3\n4 11\n",
"5 3 3\n10 18\n18 17\n10 20\n20 18\n20 18\n"
] | [
"3 1 2 ",
"2 4 5 "
] | In the first sample one of optimal solutions is to pass orders 1, 2, 3. In this case the chairperson obeys orders number 1 and 2. She gets 10 new grey hairs in the head and the directors' displeasement will equal 3. Note that the same result can be achieved with order 4 instead of order 3.
In the second sample, the chairperson can obey all the orders, so the best strategy for the students is to pick the orders with the maximum sum of *a*<sub class="lower-index">*i*</sub> values. The chairperson gets 58 new gray hairs and the directors' displeasement will equal 0. | [
{
"input": "5 3 2\n5 6\n5 8\n1 3\n4 3\n4 11",
"output": "3 1 2 "
},
{
"input": "5 3 3\n10 18\n18 17\n10 20\n20 18\n20 18",
"output": "2 4 5 "
},
{
"input": "10 7 4\n4 3\n5 3\n5 5\n4 3\n4 5\n3 5\n4 5\n4 4\n3 5\n4 5",
"output": "1 4 8 3 5 7 10 "
},
{
"input": "20 15 10\n79 84\n... | 2,000 | 30,412,800 | 0 | 2,588 | |
551 | GukiZ and Binary Operations | [
"combinatorics",
"implementation",
"math",
"matrices",
"number theory"
] | null | null | We all know that GukiZ often plays with arrays.
Now he is thinking about this problem: how many arrays *a*, of length *n*, with non-negative elements strictly less then 2*l* meet the following condition: ? Here operation means bitwise AND (in Pascal it is equivalent to and, in C/C++/Java/Python it is equivalent to &), operation means bitwise OR (in Pascal it is equivalent to , in C/C++/Java/Python it is equivalent to |).
Because the answer can be quite large, calculate it modulo *m*. This time GukiZ hasn't come up with solution, and needs you to help him! | First and the only line of input contains four integers *n*, *k*, *l*, *m* (2<=β€<=*n*<=β€<=1018, 0<=β€<=*k*<=β€<=1018, 0<=β€<=*l*<=β€<=64, 1<=β€<=*m*<=β€<=109<=+<=7). | In the single line print the number of arrays satisfying the condition above modulo *m*. | [
"2 1 2 10\n",
"2 1 1 3\n",
"3 3 2 10\n"
] | [
"3\n",
"1\n",
"9\n"
] | In the first sample, satisfying arrays are {1,β1},β{3,β1},β{1,β3}.
In the second sample, only satisfying array is {1,β1}.
In the third sample, satisfying arrays are {0,β3,β3},β{1,β3,β2},β{1,β3,β3},β{2,β3,β1},β{2,β3,β3},β{3,β3,β0},β{3,β3,β1},β{3,β3,β2},β{3,β3,β3}. | [
{
"input": "2 1 2 10",
"output": "3"
},
{
"input": "2 1 1 3",
"output": "1"
},
{
"input": "3 3 2 10",
"output": "9"
},
{
"input": "5135 42542 15 4354",
"output": "0"
},
{
"input": "21 21 21 21",
"output": "1"
},
{
"input": "2 0 0 5",
"output": "1"
... | 93 | 0 | 0 | 2,593 | |
540 | School Marks | [
"greedy",
"implementation"
] | null | null | Little Vova studies programming in an elite school. Vova and his classmates are supposed to write *n* progress tests, for each test they will get a mark from 1 to *p*. Vova is very smart and he can write every test for any mark, but he doesn't want to stand out from the crowd too much. If the sum of his marks for all tests exceeds value *x*, then his classmates notice how smart he is and start distracting him asking to let them copy his homework. And if the median of his marks will be lower than *y* points (the definition of a median is given in the notes), then his mom will decide that he gets too many bad marks and forbid him to play computer games.
Vova has already wrote *k* tests and got marks *a*1,<=...,<=*a**k*. He doesn't want to get into the first or the second situation described above and now he needs to determine which marks he needs to get for the remaining tests. Help him do that. | The first line contains 5 space-separated integers: *n*, *k*, *p*, *x* and *y* (1<=β€<=*n*<=β€<=999, *n* is odd, 0<=β€<=*k*<=<<=*n*, 1<=β€<=*p*<=β€<=1000, *n*<=β€<=*x*<=β€<=*n*Β·*p*, 1<=β€<=*y*<=β€<=*p*). Here *n* is the number of tests that Vova is planned to write, *k* is the number of tests he has already written, *p* is the maximum possible mark for a test, *x* is the maximum total number of points so that the classmates don't yet disturb Vova, *y* is the minimum median point so that mom still lets him play computer games.
The second line contains *k* space-separated integers: *a*1,<=...,<=*a**k* (1<=β€<=*a**i*<=β€<=*p*)Β β the marks that Vova got for the tests he has already written. | If Vova cannot achieve the desired result, print "-1".
Otherwise, print *n*<=-<=*k* space-separated integersΒ β the marks that Vova should get for the remaining tests. If there are multiple possible solutions, print any of them. | [
"5 3 5 18 4\n3 5 4\n",
"5 3 5 16 4\n5 5 5\n"
] | [
"4 1\n",
"-1\n"
] | The median of sequence *a*<sub class="lower-index">1</sub>,Β ...,Β *a*<sub class="lower-index">*n*</sub> where *n* is odd (in this problem *n* is always odd) is the element staying on (*n*β+β1)β/β2 position in the sorted list of *a*<sub class="lower-index">*i*</sub>.
In the first sample the sum of marks equals 3 + 5 + 4 + 4 + 1 = 17, what doesn't exceed 18, that means that Vova won't be disturbed by his classmates. And the median point of the sequence {1, 3, 4, 4, 5} equals to 4, that isn't less than 4, so his mom lets him play computer games.
Please note that you do not have to maximize the sum of marks or the median mark. Any of the answers: "4Β 2", "2Β 4", "5Β 1", "1Β 5", "4Β 1", "1Β 4" for the first test is correct.
In the second sample Vova got three '5' marks, so even if he gets two '1' marks, the sum of marks will be 17, that is more than the required value of 16. So, the answer to this test is "-1". | [
{
"input": "5 3 5 18 4\n3 5 4",
"output": "4 1"
},
{
"input": "5 3 5 16 4\n5 5 5",
"output": "-1"
},
{
"input": "5 3 5 17 4\n5 5 5",
"output": "1 1"
},
{
"input": "5 3 5 12 1\n5 5 1",
"output": "-1"
},
{
"input": "5 3 5 13 1\n5 5 1",
"output": "1 1"
},
{
... | 31 | 5,632,000 | 0 | 2,596 | |
612 | Replace To Make Regular Bracket Sequence | [
"data structures",
"expression parsing",
"math"
] | null | null | You are given string *s* consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.
The following definition of a regular bracket sequence is well-known, so you can be familiar with it.
Let's define a regular bracket sequence (RBS). Empty string is RBS. Let *s*1 and *s*2 be a RBS then the strings <*s*1>*s*2, {*s*1}*s*2, [*s*1]*s*2, (*s*1)*s*2 are also RBS.
For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not.
Determine the least number of replaces to make the string *s* RBS. | The only line contains a non empty string *s*, consisting of only opening and closing brackets of four kinds. The length of *s* does not exceed 106. | If it's impossible to get RBS from *s* print Impossible.
Otherwise print the least number of replaces needed to get RBS from *s*. | [
"[<}){}\n",
"{()}[]\n",
"]]\n"
] | [
"2",
"0",
"Impossible"
] | none | [
{
"input": "[<}){}",
"output": "2"
},
{
"input": "{()}[]",
"output": "0"
},
{
"input": "]]",
"output": "Impossible"
},
{
"input": ">",
"output": "Impossible"
},
{
"input": "{}",
"output": "0"
},
{
"input": "{}",
"output": "0"
},
{
"input": ... | 61 | 307,200 | 0 | 2,601 | |
340 | Tourist Problem | [
"combinatorics",
"implementation",
"math"
] | null | null | Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are *n* destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The *n* destinations are described by a non-negative integers sequence *a*1, *a*2, ..., *a**n*. The number *a**k* represents that the *k*th destination is at distance *a**k* kilometers from the starting point. No two destinations are located in the same place.
Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination.
The distance between destination located at kilometer *x* and next destination, located at kilometer *y*, is |*x*<=-<=*y*| kilometers. We call a "route" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all *n* destinations and he doesn't visit a destination more than once.
Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him. | The first line contains integer *n* (2<=β€<=*n*<=β€<=105). Next line contains *n* distinct integers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=107). | Output two integers β the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible. | [
"3\n2 3 5\n"
] | [
"22 3"
] | Consider 6 possible routes:
- [2, 3, 5]: total distance traveled: |2 β 0| + |3 β 2| + |5 β 3| = 5; - [2, 5, 3]: |2 β 0| + |5 β 2| + |3 β 5| = 7; - [3, 2, 5]: |3 β 0| + |2 β 3| + |5 β 2| = 7; - [3, 5, 2]: |3 β 0| + |5 β 3| + |2 β 5| = 8; - [5, 2, 3]: |5 β 0| + |2 β 5| + |3 β 2| = 9; - [5, 3, 2]: |5 β 0| + |3 β 5| + |2 β 3| = 8.
The average travel distance is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/29119d3733c79f70eb2d77186ac1606bf938508a.png" style="max-width: 100.0%;max-height: 100.0%;"/> = <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ee9d5516ed2ca1d2b65ed21f8a64f58f94954c30.png" style="max-width: 100.0%;max-height: 100.0%;"/> = <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ed5cc8cb7dd43cfb27f2459586062538e44de7bd.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [
{
"input": "3\n2 3 5",
"output": "22 3"
},
{
"input": "4\n1 5 77 2",
"output": "547 4"
},
{
"input": "5\n3 3842 288 199 334",
"output": "35918 5"
},
{
"input": "7\n1 2 3 40 52 33 86",
"output": "255 1"
},
{
"input": "7\n1 10 100 1000 10000 1000000 10000000",
"... | 342 | 14,131,200 | 3 | 2,605 | |
801 | Valued Keys | [
"constructive algorithms",
"greedy",
"strings"
] | null | null | You found a mysterious function *f*. The function takes two strings *s*1 and *s*2. These strings must consist only of lowercase English letters, and must be the same length.
The output of the function *f* is another string of the same length. The *i*-th character of the output is equal to the minimum of the *i*-th character of *s*1 and the *i*-th character of *s*2.
For example, *f*("ab", "ba") = "aa", and *f*("nzwzl", "zizez") = "niwel".
You found two strings *x* and *y* of the same length and consisting of only lowercase English letters. Find any string *z* such that *f*(*x*,<=*z*)<==<=*y*, or print -1 if no such string *z* exists. | The first line of input contains the string *x*.
The second line of input contains the string *y*.
Both *x* and *y* consist only of lowercase English letters, *x* and *y* have same length and this length is between 1 and 100. | If there is no string *z* such that *f*(*x*,<=*z*)<==<=*y*, print -1.
Otherwise, print a string *z* such that *f*(*x*,<=*z*)<==<=*y*. If there are multiple possible answers, print any of them. The string *z* should be the same length as *x* and *y* and consist only of lowercase English letters. | [
"ab\naa\n",
"nzwzl\nniwel\n",
"ab\nba\n"
] | [
"ba\n",
"xiyez\n",
"-1\n"
] | The first case is from the statement.
Another solution for the second case is "zizez"
There is no solution for the third case. That is, there is no *z* such that *f*("ab", *z*)β=β "ba". | [
{
"input": "ab\naa",
"output": "ba"
},
{
"input": "nzwzl\nniwel",
"output": "xiyez"
},
{
"input": "ab\nba",
"output": "-1"
},
{
"input": "r\nl",
"output": "l"
},
{
"input": "d\ny",
"output": "-1"
},
{
"input": "yvowz\ncajav",
"output": "cajav"
},... | 93 | 0 | 0 | 2,606 | |
450 | Jzzhu and Children | [
"implementation"
] | null | null | There are *n* children in Jzzhu's school. Jzzhu is going to give some candies to them. Let's number all the children from 1 to *n*. The *i*-th child wants to get at least *a**i* candies.
Jzzhu asks children to line up. Initially, the *i*-th child stands at the *i*-th place of the line. Then Jzzhu start distribution of the candies. He follows the algorithm:
1. Give *m* candies to the first child of the line. 1. If this child still haven't got enough candies, then the child goes to the end of the line, else the child go home. 1. Repeat the first two steps while the line is not empty.
Consider all the children in the order they go home. Jzzhu wants to know, which child will be the last in this order? | The first line contains two integers *n*,<=*m* (1<=β€<=*n*<=β€<=100;Β 1<=β€<=*m*<=β€<=100). The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=100). | Output a single integer, representing the number of the last child. | [
"5 2\n1 3 1 4 2\n",
"6 4\n1 1 2 2 3 3\n"
] | [
"4\n",
"6\n"
] | Let's consider the first sample.
Firstly child 1 gets 2 candies and go home. Then child 2 gets 2 candies and go to the end of the line. Currently the line looks like [3, 4, 5, 2] (indices of the children in order of the line). Then child 3 gets 2 candies and go home, and then child 4 gets 2 candies and goes to the end of the line. Currently the line looks like [5, 2, 4]. Then child 5 gets 2 candies and goes home. Then child 2 gets two candies and goes home, and finally child 4 gets 2 candies and goes home.
Child 4 is the last one who goes home. | [
{
"input": "5 2\n1 3 1 4 2",
"output": "4"
},
{
"input": "6 4\n1 1 2 2 3 3",
"output": "6"
},
{
"input": "7 3\n6 1 5 4 2 3 1",
"output": "4"
},
{
"input": "10 5\n2 7 3 6 2 5 1 3 4 5",
"output": "4"
},
{
"input": "100 1\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18... | 77 | 1,638,400 | 3 | 2,610 | |
234 | Practice | [
"constructive algorithms",
"divide and conquer",
"implementation"
] | null | null | Little time is left before Berland annual football championship. Therefore the coach of team "Losewille Rangers" decided to resume the practice, that were indefinitely interrupted for uncertain reasons. Overall there are *n* players in "Losewille Rangers". Each player on the team has a number β a unique integer from 1 to *n*. To prepare for the championship, the coach Mr. Floppe decided to spend some number of practices.
Mr. Floppe spent some long nights of his holiday planning how to conduct the practices. He came to a very complex practice system. Each practice consists of one game, all *n* players of the team take part in the game. The players are sorted into two teams in some way. In this case, the teams may have different numbers of players, but each team must have at least one player.
The coach wants to be sure that after the series of the practice sessions each pair of players had at least one practice, when they played in different teams. As the players' energy is limited, the coach wants to achieve the goal in the least number of practices.
Help him to schedule the practices. | A single input line contains integer *n* (2<=β€<=*n*<=β€<=1000). | In the first line print *m* β the minimum number of practices the coach will have to schedule. Then print the descriptions of the practices in *m* lines.
In the *i*-th of those lines print *f**i* β the number of players in the first team during the *i*-th practice (1<=β€<=*f**i*<=<<=*n*), and *f**i* numbers from 1 to *n* β the numbers of players in the first team. The rest of the players will play in the second team during this practice. Separate numbers on a line with spaces. Print the numbers of the players in any order. If there are multiple optimal solutions, print any of them. | [
"2\n",
"3\n"
] | [
"1\n1 1\n",
"2\n2 1 2\n1 1\n"
] | none | [
{
"input": "2",
"output": "1\n1 1"
},
{
"input": "3",
"output": "2\n2 1 2\n1 1"
},
{
"input": "4",
"output": "2\n2 1 2\n2 1 3"
},
{
"input": "5",
"output": "3\n3 1 2 3\n3 1 2 4\n1 1"
},
{
"input": "6",
"output": "3\n3 1 2 3\n4 1 2 4 5\n2 1 4"
},
{
"inp... | 218 | 3,584,000 | 3 | 2,611 | |
897 | Scarborough Fair | [
"implementation"
] | null | null | Parsley, sage, rosemary and thyme.
Remember me to one who lives there.
He once was the true love of mine.
Willem is taking the girl to the highest building in island No.28, however, neither of them knows how to get there.
Willem asks his friend, Grick for directions, Grick helped them, and gave them a task.
Although the girl wants to help, Willem insists on doing it by himself.
Grick gave Willem a string of length *n*.
Willem needs to do *m* operations, each operation has four parameters *l*,<=*r*,<=*c*1,<=*c*2, which means that all symbols *c*1 in range [*l*,<=*r*] (from *l*-th to *r*-th, including *l* and *r*) are changed into *c*2. String is 1-indexed.
Grick wants to know the final string after all the *m* operations. | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=100).
The second line contains a string *s* of length *n*, consisting of lowercase English letters.
Each of the next *m* lines contains four parameters *l*,<=*r*,<=*c*1,<=*c*2 (1<=β€<=*l*<=β€<=*r*<=β€<=*n*, *c*1,<=*c*2 are lowercase English letters), separated by space. | Output string *s* after performing *m* operations described above. | [
"3 1\nioi\n1 1 i n\n",
"5 3\nwxhak\n3 3 h x\n1 5 x a\n1 3 w g\n"
] | [
"noi",
"gaaak"
] | For the second example:
After the first operation, the string is wxxak.
After the second operation, the string is waaak.
After the third operation, the string is gaaak. | [
{
"input": "3 1\nioi\n1 1 i n",
"output": "noi"
},
{
"input": "5 3\nwxhak\n3 3 h x\n1 5 x a\n1 3 w g",
"output": "gaaak"
},
{
"input": "9 51\nbhfbdcgff\n2 3 b b\n2 8 e f\n3 8 g f\n5 7 d a\n1 5 e b\n3 4 g b\n6 7 c d\n3 6 e g\n3 6 e h\n5 6 a e\n7 9 a c\n4 9 a h\n3 7 c b\n6 9 b g\n1 7 h b\n... | 124 | 0 | 3 | 2,616 | |
0 | none | [
"none"
] | null | null | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an *n*<=Γ<=*n* table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to *n* from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following *q* actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action. | The first line contains integers *n* (1<=β€<=*n*<=β€<=109) and *q* (1<=β€<=*q*<=β€<=2Β·105) β the size of the chocolate bar and the number of actions.
Next *q* lines contain the descriptions of the actions: the *i*-th of them contains numbers *x**i* and *y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=*n*, *x**i*<=+<=*y**i*<==<=*n*<=+<=1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up). | Print *q* lines, the *i*-th of them should contain the number of eaten pieces as a result of the *i*-th action. | [
"6 5\n3 4 U\n6 1 L\n2 5 L\n1 6 U\n4 3 U\n",
"10 6\n2 9 U\n10 1 U\n1 10 U\n8 3 L\n10 1 L\n6 5 U\n"
] | [
"4\n3\n2\n1\n2\n",
"9\n1\n10\n6\n0\n2\n"
] | Pictures to the sample tests:
<img class="tex-graphics" src="https://espresso.codeforces.com/2ce2eba5359eb520eb9b09725b638508b03473a8.png" style="max-width: 100.0%;max-height: 100.0%;"/>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | [] | 46 | 0 | 0 | 2,621 | |
245 | System Administrator | [
"implementation"
] | null | null | Polycarpus is a system administrator. There are two servers under his strict guidance β *a* and *b*. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers *x* and *y* (*x*<=+<=*y*<==<=10;Β *x*,<=*y*<=β₯<=0). These numbers mean that *x* packets successfully reached the corresponding server through the network and *y* packets were lost.
Today Polycarpus has performed overall *n* ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.
Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results. | The first line contains a single integer *n* (2<=β€<=*n*<=β€<=1000) β the number of commands Polycarpus has fulfilled. Each of the following *n* lines contains three integers β the description of the commands. The *i*-th of these lines contains three space-separated integers *t**i*, *x**i*, *y**i* (1<=β€<=*t**i*<=β€<=2;Β *x**i*,<=*y**i*<=β₯<=0;Β *x**i*<=+<=*y**i*<==<=10). If *t**i*<==<=1, then the *i*-th command is "ping a", otherwise the *i*-th command is "ping b". Numbers *x**i*, *y**i* represent the result of executing this command, that is, *x**i* packets reached the corresponding server successfully and *y**i* packets were lost.
It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command. | In the first line print string "LIVE" (without the quotes) if server *a* is "alive", otherwise print "DEAD" (without the quotes).
In the second line print the state of server *b* in the similar format. | [
"2\n1 5 5\n2 6 4\n",
"3\n1 0 10\n2 0 10\n1 10 0\n"
] | [
"LIVE\nLIVE\n",
"LIVE\nDEAD\n"
] | Consider the first test case. There 10 packets were sent to server *a*, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server *b*, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.
Consider the second test case. There were overall 20 packages sent to server *a*, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server *b*, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network. | [
{
"input": "2\n1 5 5\n2 6 4",
"output": "LIVE\nLIVE"
},
{
"input": "3\n1 0 10\n2 0 10\n1 10 0",
"output": "LIVE\nDEAD"
},
{
"input": "10\n1 3 7\n2 4 6\n1 2 8\n2 5 5\n2 10 0\n2 10 0\n1 8 2\n2 2 8\n2 10 0\n1 1 9",
"output": "DEAD\nLIVE"
},
{
"input": "11\n1 8 2\n1 6 4\n1 9 1\n1... | 92 | 0 | 0 | 2,624 | |
618 | Guess the Permutation | [
"constructive algorithms"
] | null | null | Bob has a permutation of integers from 1 to *n*. Denote this permutation as *p*. The *i*-th element of *p* will be denoted as *p**i*. For all pairs of distinct integers *i*,<=*j* between 1 and *n*, he wrote the number *a**i*,<=*j*<==<=*min*(*p**i*,<=*p**j*). He writes *a**i*,<=*i*<==<=0 for all integer *i* from 1 to *n*.
Bob gave you all the values of *a**i*,<=*j* that he wrote down. Your job is to reconstruct any permutation that could have generated these values. The input will be formed so that it is guaranteed that there is at least one solution that is consistent with the information given. | The first line of the input will contain a single integer *n* (2<=β€<=*n*<=β€<=50).
The next *n* lines will contain the values of *a**i*,<=*j*. The *j*-th number on the *i*-th line will represent *a**i*,<=*j*. The *i*-th number on the *i*-th line will be 0. It's guaranteed that *a**i*,<=*j*<==<=*a**j*,<=*i* and there is at least one solution consistent with the information given. | Print *n* space separated integers, which represents a permutation that could have generated these values. If there are multiple possible solutions, print any of them. | [
"2\n0 1\n1 0\n",
"5\n0 2 2 1 2\n2 0 4 1 3\n2 4 0 1 3\n1 1 1 0 1\n2 3 3 1 0\n"
] | [
"2 1\n",
"2 5 4 1 3\n"
] | In the first case, the answer can be {1,β2} or {2,β1}.
In the second case, another possible answer is {2,β4,β5,β1,β3}. | [
{
"input": "2\n0 1\n1 0",
"output": "2 1"
},
{
"input": "5\n0 2 2 1 2\n2 0 4 1 3\n2 4 0 1 3\n1 1 1 0 1\n2 3 3 1 0",
"output": "2 5 4 1 3"
},
{
"input": "10\n0 1 5 2 5 3 4 5 5 5\n1 0 1 1 1 1 1 1 1 1\n5 1 0 2 6 3 4 6 6 6\n2 1 2 0 2 2 2 2 2 2\n5 1 6 2 0 3 4 8 8 7\n3 1 3 2 3 0 3 3 3 3\n4 1 4... | 93 | 0 | 3 | 2,626 | |
864 | Fire | [
"dp",
"sortings"
] | null | null | Polycarp is in really serious trouble β his house is on fire! It's time to save the most valuable items. Polycarp estimated that it would take *t**i* seconds to save *i*-th item. In addition, for each item, he estimated the value of *d**i* β the moment after which the item *i* will be completely burned and will no longer be valuable for him at all. In particular, if *t**i*<=β₯<=*d**i*, then *i*-th item cannot be saved.
Given the values *p**i* for each of the items, find a set of items that Polycarp can save such that the total value of this items is maximum possible. Polycarp saves the items one after another. For example, if he takes item *a* first, and then item *b*, then the item *a* will be saved in *t**a* seconds, and the item *b* β in *t**a*<=+<=*t**b* seconds after fire started. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=100) β the number of items in Polycarp's house.
Each of the following *n* lines contains three integers *t**i*,<=*d**i*,<=*p**i* (1<=β€<=*t**i*<=β€<=20, 1<=β€<=*d**i*<=β€<=2<=000, 1<=β€<=*p**i*<=β€<=20) β the time needed to save the item *i*, the time after which the item *i* will burn completely and the value of item *i*. | In the first line print the maximum possible total value of the set of saved items. In the second line print one integer *m* β the number of items in the desired set. In the third line print *m* distinct integers β numbers of the saved items in the order Polycarp saves them. Items are 1-indexed in the same order in which they appear in the input. If there are several answers, print any of them. | [
"3\n3 7 4\n2 6 5\n3 7 6\n",
"2\n5 6 1\n3 3 5\n"
] | [
"11\n2\n2 3 \n",
"1\n1\n1 \n"
] | In the first example Polycarp will have time to save any two items, but in order to maximize the total value of the saved items, he must save the second and the third item. For example, he can firstly save the third item in 3 seconds, and then save the second item in another 2 seconds. Thus, the total value of the saved items will be 6β+β5β=β11.
In the second example Polycarp can save only the first item, since even if he immediately starts saving the second item, he can save it in 3 seconds, but this item will already be completely burned by this time. | [
{
"input": "3\n3 7 4\n2 6 5\n3 7 6",
"output": "11\n2\n2 3 "
},
{
"input": "2\n5 6 1\n3 3 5",
"output": "1\n1\n1 "
},
{
"input": "9\n13 18 14\n8 59 20\n9 51 2\n18 32 15\n1 70 18\n14 81 14\n10 88 16\n18 52 3\n1 50 6",
"output": "106\n8\n1 4 9 8 2 5 6 7 "
},
{
"input": "5\n12 4... | 233 | 2,252,800 | -1 | 2,629 | |
768 | Code For 1 | [
"constructive algorithms",
"dfs and similar",
"divide and conquer"
] | null | null | Jon fought bravely to rescue the wildlings who were attacked by the white-walkers at Hardhome. On his arrival, Sam tells him that he wants to go to Oldtown to train at the Citadel to become a maester, so he can return and take the deceased Aemon's place as maester of Castle Black. Jon agrees to Sam's proposal and Sam sets off his journey to the Citadel. However becoming a trainee at the Citadel is not a cakewalk and hence the maesters at the Citadel gave Sam a problem to test his eligibility.
Initially Sam has a list with a single element *n*. Then he has to perform certain operations on this list. In each operation Sam must remove any element *x*, such that *x*<=><=1, from the list and insert at the same position , , sequentially. He must continue with these operations until all the elements in the list are either 0 or 1.
Now the masters want the total number of 1s in the range *l* to *r* (1-indexed). Sam wants to become a maester but unfortunately he cannot solve this problem. Can you help Sam to pass the eligibility test? | The first line contains three integers *n*, *l*, *r* (0<=β€<=*n*<=<<=250, 0<=β€<=*r*<=-<=*l*<=β€<=105, *r*<=β₯<=1, *l*<=β₯<=1) β initial element and the range *l* to *r*.
It is guaranteed that *r* is not greater than the length of the final list. | Output the total number of 1s in the range *l* to *r* in the final sequence. | [
"7 2 5\n",
"10 3 10\n"
] | [
"4\n",
"5\n"
] | Consider first example:
<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/288fbb682a6fa1934a47b763d6851f9d32a06150.png" style="max-width: 100.0%;max-height: 100.0%;"/>
Elements on positions from 2-nd to 5-th in list is [1,β1,β1,β1]. The number of ones is 4.
For the second example:
<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/52e9bc51ef858cacc27fc274c7ba9419d5c1ded9.png" style="max-width: 100.0%;max-height: 100.0%;"/>
Elements on positions from 3-rd to 10-th in list is [1,β1,β1,β0,β1,β0,β1,β0]. The number of ones is 5. | [
{
"input": "7 2 5",
"output": "4"
},
{
"input": "10 3 10",
"output": "5"
},
{
"input": "56 18 40",
"output": "20"
},
{
"input": "203 40 124",
"output": "67"
},
{
"input": "903316762502 354723010040 354723105411",
"output": "78355"
},
{
"input": "335343... | 2,000 | 0 | 0 | 2,631 | |
624 | Save Luke | [
"math"
] | null | null | Luke Skywalker got locked up in a rubbish shredder between two presses. R2D2 is already working on his rescue, but Luke needs to stay alive as long as possible. For simplicity we will assume that everything happens on a straight line, the presses are initially at coordinates 0 and *L*, and they move towards each other with speed *v*1 and *v*2, respectively. Luke has width *d* and is able to choose any position between the presses. Luke dies as soon as the distance between the presses is less than his width. Your task is to determine for how long Luke can stay alive. | The first line of the input contains four integers *d*, *L*, *v*1, *v*2 (1<=β€<=*d*,<=*L*,<=*v*1,<=*v*2<=β€<=10<=000,<=*d*<=<<=*L*)Β β Luke's width, the initial position of the second press and the speed of the first and second presses, respectively. | Print a single real valueΒ β the maximum period of time Luke can stay alive for. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6.
Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct, if . | [
"2 6 2 2\n",
"1 9 1 2\n"
] | [
"1.00000000000000000000\n",
"2.66666666666666650000\n"
] | In the first sample Luke should stay exactly in the middle of the segment, that is at coordinates [2;4], as the presses move with the same speed.
In the second sample he needs to occupy the position <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/71395c777960eaded59a9fdc428a9625f152605b.png" style="max-width: 100.0%;max-height: 100.0%;"/>. In this case both presses move to his edges at the same time. | [
{
"input": "2 6 2 2",
"output": "1.00000000000000000000"
},
{
"input": "1 9 1 2",
"output": "2.66666666666666650000"
},
{
"input": "1 10000 1 1",
"output": "4999.50000000000000000000"
},
{
"input": "9999 10000 10000 10000",
"output": "0.00005000000000000000"
},
{
... | 62 | 0 | 3 | 2,649 | |
283 | Cows and Sequence | [
"constructive algorithms",
"data structures",
"implementation"
] | null | null | Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform *n* operations. Each operation is one of the following:
1. Add the integer *x**i* to the first *a**i* elements of the sequence. 1. Append an integer *k**i* to the end of the sequence. (And hence the size of the sequence increases by 1) 1. Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence.
After each operation, the cows would like to know the average of all the numbers in the sequence. Help them! | The first line contains a single integer *n*Β (1<=β€<=*n*<=β€<=2Β·105) β the number of operations. The next *n* lines describe the operations. Each line will start with an integer *t**i* (1<=β€<=*t**i*<=β€<=3), denoting the type of the operation (see above). If *t**i*<==<=1, it will be followed by two integers *a**i*,<=*x**i* (|*x**i*|<=β€<=103;Β 1<=β€<=*a**i*). If *t**i*<==<=2, it will be followed by a single integer *k**i* (|*k**i*|<=β€<=103). If *t**i*<==<=3, it will not be followed by anything.
It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence. | Output *n* lines each containing the average of the numbers in the sequence after the corresponding operation.
The answer will be considered correct if its absolute or relative error doesn't exceed 10<=-<=6. | [
"5\n2 1\n3\n2 3\n2 1\n3\n",
"6\n2 1\n1 2 20\n2 2\n1 2 -3\n3\n3\n"
] | [
"0.500000\n0.000000\n1.500000\n1.333333\n1.500000\n",
"0.500000\n20.500000\n14.333333\n12.333333\n17.500000\n17.000000\n"
] | In the second sample, the sequence becomes <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/fb5aaaa5dc516fe540cef52fd153768bfdb941c8.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "5\n2 1\n3\n2 3\n2 1\n3",
"output": "0.500000\n0.000000\n1.500000\n1.333333\n1.500000"
},
{
"input": "6\n2 1\n1 2 20\n2 2\n1 2 -3\n3\n3",
"output": "0.500000\n20.500000\n14.333333\n12.333333\n17.500000\n17.000000"
},
{
"input": "1\n1 1 1",
"output": "1.000000"
},
{
... | 1,500 | 14,438,400 | 0 | 2,652 | |
246 | Increase and Decrease | [
"greedy",
"math"
] | null | null | Polycarpus has an array, consisting of *n* integers *a*1,<=*a*2,<=...,<=*a**n*. Polycarpus likes it when numbers in an array match. That's why he wants the array to have as many equal numbers as possible. For that Polycarpus performs the following operation multiple times:
- he chooses two elements of the array *a**i*, *a**j* (*i*<=β <=*j*); - he simultaneously increases number *a**i* by 1 and decreases number *a**j* by 1, that is, executes *a**i*<==<=*a**i*<=+<=1 and *a**j*<==<=*a**j*<=-<=1.
The given operation changes exactly two distinct array elements. Polycarpus can apply the described operation an infinite number of times.
Now he wants to know what maximum number of equal array elements he can get if he performs an arbitrary number of such operation. Help Polycarpus. | The first line contains integer *n* (1<=β€<=*n*<=β€<=105) β the array size. The second line contains space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (|*a**i*|<=β€<=104) β the original array. | Print a single integer β the maximum number of equal array elements he can get if he performs an arbitrary number of the given operation. | [
"2\n2 1\n",
"3\n1 4 1\n"
] | [
"1\n",
"3\n"
] | none | [
{
"input": "2\n2 1",
"output": "1"
},
{
"input": "3\n1 4 1",
"output": "3"
},
{
"input": "4\n2 -7 -2 -6",
"output": "3"
},
{
"input": "4\n2 0 -2 -1",
"output": "3"
},
{
"input": "6\n-1 1 0 0 -1 -1",
"output": "5"
},
{
"input": "5\n0 0 0 0 0",
"outp... | 280 | 0 | 0 | 2,654 | |
600 | Make Palindrome | [
"constructive algorithms",
"greedy",
"strings"
] | null | null | A string is called palindrome if it reads the same from left to right and from right to left. For example "kazak", "oo", "r" and "mikhailrubinchikkihcniburliahkim" are palindroms, but strings "abb" and "ij" are not.
You are given string *s* consisting of lowercase Latin letters. At once you can choose any position in the string and change letter in that position to any other lowercase letter. So after each changing the length of the string doesn't change. At first you can change some letters in *s*. Then you can permute the order of letters as you want. Permutation doesn't count as changes.
You should obtain palindrome with the minimal number of changes. If there are several ways to do that you should get the lexicographically (alphabetically) smallest palindrome. So firstly you should minimize the number of changes and then minimize the palindrome lexicographically. | The only line contains string *s* (1<=β€<=|*s*|<=β€<=2Β·105) consisting of only lowercase Latin letters. | Print the lexicographically smallest palindrome that can be obtained with the minimal number of changes. | [
"aabc\n",
"aabcd\n"
] | [
"abba\n",
"abcba\n"
] | none | [
{
"input": "aabc",
"output": "abba"
},
{
"input": "aabcd",
"output": "abcba"
},
{
"input": "u",
"output": "u"
},
{
"input": "ttttt",
"output": "ttttt"
},
{
"input": "xxxvvvxxvv",
"output": "vvvxxxxvvv"
},
{
"input": "wrwrwfrrfrffrrwwwffffwrfrrwfrrfrwwf... | 93 | 4,505,600 | 3 | 2,655 | |
716 | Complete the Word | [
"greedy",
"two pointers"
] | null | null | ZS the Coder loves to read the dictionary. He thinks that a word is nice if there exists a substring (contiguous segment of letters) of it of length 26 where each letter of English alphabet appears exactly once. In particular, if the string has length strictly less than 26, no such substring exists and thus it is not nice.
Now, ZS the Coder tells you a word, where some of its letters are missing as he forgot them. He wants to determine if it is possible to fill in the missing letters so that the resulting word is nice. If it is possible, he needs you to find an example of such a word as well. Can you help him? | The first and only line of the input contains a single string *s* (1<=β€<=|*s*|<=β€<=50<=000), the word that ZS the Coder remembers. Each character of the string is the uppercase letter of English alphabet ('A'-'Z') or is a question mark ('?'), where the question marks denotes the letters that ZS the Coder can't remember. | If there is no way to replace all the question marks with uppercase letters such that the resulting word is nice, then print <=-<=1 in the only line.
Otherwise, print a string which denotes a possible nice word that ZS the Coder learned. This string should match the string from the input, except for the question marks replaced with uppercase English letters.
If there are multiple solutions, you may print any of them. | [
"ABC??FGHIJK???OPQR?TUVWXY?\n",
"WELCOMETOCODEFORCESROUNDTHREEHUNDREDANDSEVENTYTWO\n",
"??????????????????????????\n",
"AABCDEFGHIJKLMNOPQRSTUVW??M\n"
] | [
"ABCDEFGHIJKLMNOPQRZTUVWXYS",
"-1",
"MNBVCXZLKJHGFDSAQPWOEIRUYT",
"-1"
] | In the first sample case, ABCDEFGHIJKLMNOPQRZTUVWXYS is a valid answer beacuse it contains a substring of length 26 (the whole string in this case) which contains all the letters of the English alphabet exactly once. Note that there are many possible solutions, such as ABCDEFGHIJKLMNOPQRSTUVWXYZ or ABCEDFGHIJKLMNOPQRZTUVWXYS.
In the second sample case, there are no missing letters. In addition, the given string does not have a substring of length 26 that contains all the letters of the alphabet, so the answer is β-β1.
In the third sample case, any string of length 26 that contains all letters of the English alphabet fits as an answer. | [
{
"input": "ABC??FGHIJK???OPQR?TUVWXY?",
"output": "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
},
{
"input": "WELCOMETOCODEFORCESROUNDTHREEHUNDREDANDSEVENTYTWO",
"output": "-1"
},
{
"input": "??????????????????????????",
"output": "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
},
{
"input": "AABCDEFGHIJKLMNO... | 62 | 0 | 0 | 2,656 | |
195 | Let's Watch Football | [
"binary search",
"brute force",
"math"
] | null | null | Valeric and Valerko missed the last Euro football game, so they decided to watch the game's key moments on the Net. They want to start watching as soon as possible but the connection speed is too low. If they turn on the video right now, it will "hang up" as the size of data to watch per second will be more than the size of downloaded data per second.
The guys want to watch the whole video without any pauses, so they have to wait some integer number of seconds for a part of the video to download. After this number of seconds passes, they can start watching. Waiting for the whole video to download isn't necessary as the video can download after the guys started to watch.
Let's suppose that video's length is *c* seconds and Valeric and Valerko wait *t* seconds before the watching. Then for any moment of time *t*0, *t*<=β€<=*t*0<=β€<=*c*<=+<=*t*, the following condition must fulfill: the size of data received in *t*0 seconds is not less than the size of data needed to watch *t*0<=-<=*t* seconds of the video.
Of course, the guys want to wait as little as possible, so your task is to find the minimum integer number of seconds to wait before turning the video on. The guys must watch the video without pauses. | The first line contains three space-separated integers *a*, *b* and *c* (1<=β€<=*a*,<=*b*,<=*c*<=β€<=1000,<=*a*<=><=*b*). The first number (*a*) denotes the size of data needed to watch one second of the video. The second number (*b*) denotes the size of data Valeric and Valerko can download from the Net per second. The third number (*c*) denotes the video's length in seconds. | Print a single number β the minimum integer number of seconds that Valeric and Valerko must wait to watch football without pauses. | [
"4 1 1\n",
"10 3 2\n",
"13 12 1\n"
] | [
"3\n",
"5\n",
"1\n"
] | In the first sample video's length is 1 second and it is necessary 4 units of data for watching 1 second of video, so guys should download 4 Β· 1 = 4 units of data to watch the whole video. The most optimal way is to wait 3 seconds till 3 units of data will be downloaded and then start watching. While guys will be watching video 1 second, one unit of data will be downloaded and Valerik and Valerko will have 4 units of data by the end of watching. Also every moment till the end of video guys will have more data then necessary for watching.
In the second sample guys need 2 Β· 10 = 20 units of data, so they have to wait 5 seconds and after that they will have 20 units before the second second ends. However, if guys wait 4 seconds, they will be able to watch first second of video without pauses, but they will download 18 units of data by the end of second second and it is less then necessary. | [
{
"input": "4 1 1",
"output": "3"
},
{
"input": "10 3 2",
"output": "5"
},
{
"input": "13 12 1",
"output": "1"
},
{
"input": "2 1 3",
"output": "3"
},
{
"input": "6 2 4",
"output": "8"
},
{
"input": "5 2 1",
"output": "2"
},
{
"input": "2 1... | 92 | 0 | 3 | 2,661 | |
825 | Five-In-a-Row | [
"brute force",
"implementation"
] | null | null | Alice and Bob play 5-in-a-row game. They have a playing field of size 10<=Γ<=10. In turns they put either crosses or noughts, one at a time. Alice puts crosses and Bob puts noughts.
In current match they have made some turns and now it's Alice's turn. She wonders if she can put cross in such empty cell that she wins immediately.
Alice wins if some crosses in the field form line of length not smaller than 5. This line can be horizontal, vertical and diagonal. | You are given matrix 10<=Γ<=10 (10 lines of 10 characters each) with capital Latin letters 'X' being a cross, letters 'O' being a nought and '.' being an empty cell. The number of 'X' cells is equal to the number of 'O' cells and there is at least one of each type. There is at least one empty cell.
It is guaranteed that in the current arrangement nobody has still won. | Print 'YES' if it's possible for Alice to win in one turn by putting cross in some empty cell. Otherwise print 'NO'. | [
"XX.XX.....\n.....OOOO.\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n",
"XXOXX.....\nOO.O......\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n"
] | [
"YES\n",
"NO\n"
] | none | [
{
"input": "O.......O.\n.....O.X..\n......O...\n....X.O...\n.O.O.....X\n.XO.....XX\n...X...X.O\n........O.\n........O.\n.X.X.....X",
"output": "NO"
},
{
"input": "....OX....\n..........\n.O..X...X.\nXXO..XO..O\nO.......X.\n...XX.....\n..O.O...OX\n.........X\n.....X..OO\n........O.",
"output": "N... | 46 | 5,529,600 | 0 | 2,668 | |
203 | Two Problems | [
"brute force",
"implementation"
] | null | null | A boy Valera registered on site Codeforces as Valera, and wrote his first Codeforces Round #300. He boasted to a friend Arkady about winning as much as *x* points for his first contest. But Arkady did not believe his friend's words and decided to check whether Valera could have shown such a result.
He knows that the contest number 300 was unusual because there were only two problems. The contest lasted for *t* minutes, the minutes are numbered starting from zero. The first problem had the initial cost of *a* points, and every minute its cost reduced by *d**a* points. The second problem had the initial cost of *b* points, and every minute this cost reduced by *d**b* points. Thus, as soon as the zero minute of the contest is over, the first problem will cost *a*<=-<=*d**a* points, and the second problem will cost *b*<=-<=*d**b* points. It is guaranteed that at any moment of the contest each problem has a non-negative cost.
Arkady asks you to find out whether Valera could have got exactly *x* points for this contest. You should assume that Valera could have solved any number of the offered problems. You should also assume that for each problem Valera made no more than one attempt, besides, he could have submitted both problems at the same minute of the contest, starting with minute 0 and ending with minute number *t*<=-<=1. Please note that Valera can't submit a solution exactly *t* minutes after the start of the contest or later. | The single line of the input contains six integers *x*,<=*t*,<=*a*,<=*b*,<=*d**a*,<=*d**b* (0<=β€<=*x*<=β€<=600;Β 1<=β€<=*t*,<=*a*,<=*b*,<=*d**a*,<=*d**b*<=β€<=300) β Valera's result, the contest's duration, the initial cost of the first problem, the initial cost of the second problem, the number of points that the first and the second problem lose per minute, correspondingly.
It is guaranteed that at each minute of the contest each problem has a non-negative cost, that is, *a*<=-<=*i*Β·*d**a*<=β₯<=0 and *b*<=-<=*i*Β·*d**b*<=β₯<=0 for all 0<=β€<=*i*<=β€<=*t*<=-<=1. | If Valera could have earned exactly *x* points at a contest, print "YES", otherwise print "NO" (without the quotes). | [
"30 5 20 20 3 5\n",
"10 4 100 5 5 1\n"
] | [
"YES\n",
"NO\n"
] | In the first sample Valera could have acted like this: he could have submitted the first problem at minute 0 and the second problem β at minute 2. Then the first problem brings him 20 points and the second problem brings him 10 points, that in total gives the required 30 points. | [
{
"input": "30 5 20 20 3 5",
"output": "YES"
},
{
"input": "10 4 100 5 5 1",
"output": "NO"
},
{
"input": "0 7 30 50 3 4",
"output": "YES"
},
{
"input": "50 10 30 20 1 2",
"output": "YES"
},
{
"input": "40 1 40 5 11 2",
"output": "YES"
},
{
"input": "3... | 92 | 0 | 0 | 2,669 | |
76 | Mice | [
"greedy",
"two pointers"
] | B. Mice | 0 | 256 | Modern researches has shown that a flock of hungry mice searching for a piece of cheese acts as follows: if there are several pieces of cheese then each mouse chooses the closest one. After that all mice start moving towards the chosen piece of cheese. When a mouse or several mice achieve the destination point and there is still a piece of cheese in it, they eat it and become well-fed. Each mice that reaches this point after that remains hungry. Moving speeds of all mice are equal.
If there are several ways to choose closest pieces then mice will choose it in a way that would minimize the number of hungry mice. To check this theory scientists decided to conduct an experiment. They located *N* mice and *M* pieces of cheese on a cartesian plane where all mice are located on the line *y*<==<=*Y*0 and all pieces of cheese β on another line *y*<==<=*Y*1. To check the results of the experiment the scientists need a program which simulates the behavior of a flock of hungry mice.
Write a program that computes the minimal number of mice which will remain hungry, i.e. without cheese. | The first line of the input contains four integer numbers *N* (1<=β€<=*N*<=β€<=105), *M* (0<=β€<=*M*<=β€<=105), *Y*0 (0<=β€<=*Y*0<=β€<=107), *Y*1 (0<=β€<=*Y*1<=β€<=107, *Y*0<=β <=*Y*1). The second line contains a strictly increasing sequence of *N* numbers β *x* coordinates of mice. Third line contains a strictly increasing sequence of *M* numbers β *x* coordinates of cheese. All coordinates are integers and do not exceed 107 by absolute value. | The only line of output should contain one number β the minimal number of mice which will remain without cheese. | [
"3 2 0 2\n0 1 3\n2 5\n"
] | [
"1\n"
] | All the three mice will choose the first piece of cheese. Second and third mice will eat this piece. The first one will remain hungry, because it was running towards the same piece, but it was late. The second piece of cheese will remain uneaten. | [
{
"input": "3 2 0 2\n0 1 3\n2 5",
"output": "1"
},
{
"input": "7 11 10 20\n6 18 32 63 66 68 87\n6 8 15 23 25 41 53 59 60 75 90",
"output": "1"
},
{
"input": "13 17 14 1\n6 9 10 12 17 25 91 100 118 136 145 163 172\n0 1 2 3 4 10 12 13 16 17 19 22 26 27 28 109 154",
"output": "4"
},
... | 155 | 17,612,800 | 3 | 2,672 |
534 | Polycarpus' Dice | [
"math"
] | null | null | Polycarp has *n* dice *d*1,<=*d*2,<=...,<=*d**n*. The *i*-th dice shows numbers from 1 to *d**i*. Polycarp rolled all the dice and the sum of numbers they showed is *A*. Agrippina didn't see which dice showed what number, she knows only the sum *A* and the values *d*1,<=*d*2,<=...,<=*d**n*. However, she finds it enough to make a series of statements of the following type: dice *i* couldn't show number *r*. For example, if Polycarp had two six-faced dice and the total sum is *A*<==<=11, then Agrippina can state that each of the two dice couldn't show a value less than five (otherwise, the remaining dice must have a value of at least seven, which is impossible).
For each dice find the number of values for which it can be guaranteed that the dice couldn't show these values if the sum of the shown values is *A*. | The first line contains two integers *n*,<=*A* (1<=β€<=*n*<=β€<=2Β·105,<=*n*<=β€<=*A*<=β€<=*s*) β the number of dice and the sum of shown values where *s*<==<=*d*1<=+<=*d*2<=+<=...<=+<=*d**n*.
The second line contains *n* integers *d*1,<=*d*2,<=...,<=*d**n* (1<=β€<=*d**i*<=β€<=106), where *d**i* is the maximum value that the *i*-th dice can show. | Print *n* integers *b*1,<=*b*2,<=...,<=*b**n*, where *b**i* is the number of values for which it is guaranteed that the *i*-th dice couldn't show them. | [
"2 8\n4 4\n",
"1 3\n5\n",
"2 3\n2 3\n"
] | [
"3 3 ",
"4 ",
"0 1 "
] | In the first sample from the statement *A* equal to 8 could be obtained in the only case when both the first and the second dice show 4. Correspondingly, both dice couldn't show values 1, 2 or 3.
In the second sample from the statement *A* equal to 3 could be obtained when the single dice shows 3. Correspondingly, it couldn't show 1, 2, 4 or 5.
In the third sample from the statement *A* equal to 3 could be obtained when one dice shows 1 and the other dice shows 2. That's why the first dice doesn't have any values it couldn't show and the second dice couldn't show 3. | [
{
"input": "2 8\n4 4",
"output": "3 3 "
},
{
"input": "1 3\n5",
"output": "4 "
},
{
"input": "2 3\n2 3",
"output": "0 1 "
},
{
"input": "1 1\n3",
"output": "2 "
},
{
"input": "1 2\n3",
"output": "2 "
},
{
"input": "2 2\n2 3",
"output": "1 2 "
},
... | 514 | 21,913,600 | 3 | 2,674 | |
409 | Magnum Opus | [
"*special"
] | null | null | Salve, mi amice.
Et tu quidem de lapis philosophorum. Barba non facit philosophum. Labor omnia vincit. Non potest creatio ex nihilo. Necesse est partibus.
Rp:
Β Β Β Β I Aqua Fortis
Β Β Β Β I Aqua Regia
Β Β Β Β II Amalgama
Β Β Β Β VII Minium
Β Β Β Β IV Vitriol
Misce in vitro et Γ¦stus, et nil admirari. Festina lente, et nulla tenaci invia est via.
Fac et spera,
Vale,
Nicolas Flamel | The first line of input contains several space-separated integers *a**i* (0<=β€<=*a**i*<=β€<=100). | Print a single integer. | [
"2 4 6 8 10\n"
] | [
"1\n"
] | none | [
{
"input": "2 4 6 8 10",
"output": "1"
},
{
"input": "50 27 17 31 89",
"output": "4"
},
{
"input": "50 87 29 81 21",
"output": "5"
},
{
"input": "74 21 36 68 80",
"output": "9"
},
{
"input": "75 82 48 95 12",
"output": "3"
},
{
"input": "41 85 14 43 23... | 108 | 0 | 3 | 2,682 | |
598 | Chocolate Bar | [
"brute force",
"dp"
] | null | null | You have a rectangular chocolate bar consisting of *n*<=Γ<=*m* single squares. You want to eat exactly *k* squares, so you may need to break the chocolate bar.
In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length.
For example, if you have a chocolate bar consisting of 2<=Γ<=3 unit squares then you can break it horizontally and get two 1<=Γ<=3 pieces (the cost of such breaking is 32<==<=9), or you can break it vertically in two ways and get two pieces: 2<=Γ<=1 and 2<=Γ<=2 (the cost of such breaking is 22<==<=4).
For several given values *n*, *m* and *k* find the minimum total cost of breaking. You can eat exactly *k* squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to *k* squares. The remaining *n*Β·*m*<=-<=*k* squares are not necessarily form a single rectangular piece. | The first line of the input contains a single integer *t* (1<=β€<=*t*<=β€<=40910)Β β the number of values *n*, *m* and *k* to process.
Each of the next *t* lines contains three integers *n*, *m* and *k* (1<=β€<=*n*,<=*m*<=β€<=30,<=1<=β€<=*k*<=β€<=*min*(*n*Β·*m*,<=50))Β β the dimensions of the chocolate bar and the number of squares you want to eat respectively. | For each *n*, *m* and *k* print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly *k* squares. | [
"4\n2 2 1\n2 2 3\n2 2 2\n2 2 4\n"
] | [
"5\n5\n4\n0\n"
] | In the first query of the sample one needs to perform two breaks:
- to split 2βΓβ2 bar into two pieces of 2βΓβ1 (cost is 2<sup class="upper-index">2</sup>β=β4), - to split the resulting 2βΓβ1 into two 1βΓβ1 pieces (cost is 1<sup class="upper-index">2</sup>β=β1).
In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample. | [
{
"input": "4\n2 2 1\n2 2 3\n2 2 2\n2 2 4",
"output": "5\n5\n4\n0"
}
] | 2,000 | 10,444,800 | 0 | 2,685 | |
0 | none | [
"none"
] | null | null | Fox Ciel has *n* boxes in her room. They have the same size and weight, but they might have different strength. The *i*-th box can hold at most *x**i* boxes on its top (we'll call *x**i* the strength of the box).
Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile.
Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than *x**i* boxes on the top of *i*-th box. What is the minimal number of piles she needs to construct? | The first line contains an integer *n* (1<=β€<=*n*<=β€<=100). The next line contains *n* integers *x*1,<=*x*2,<=...,<=*x**n* (0<=β€<=*x**i*<=β€<=100). | Output a single integer β the minimal possible number of piles. | [
"3\n0 0 10\n",
"5\n0 1 2 3 4\n",
"4\n0 0 0 0\n",
"9\n0 1 0 2 0 1 1 2 10\n"
] | [
"2\n",
"1\n",
"4\n",
"3\n"
] | In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2.
In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). | [
{
"input": "3\n0 0 10",
"output": "2"
},
{
"input": "5\n0 1 2 3 4",
"output": "1"
},
{
"input": "4\n0 0 0 0",
"output": "4"
},
{
"input": "9\n0 1 0 2 0 1 1 2 10",
"output": "3"
},
{
"input": "1\n0",
"output": "1"
},
{
"input": "2\n0 0",
"output": "... | 46 | 0 | 0 | 2,687 | |
879 | Borya's Diagnosis | [
"implementation"
] | null | null | It seems that Borya is seriously sick. He is going visit *n* doctors to find out the exact diagnosis. Each of the doctors needs the information about all previous visits, so Borya has to visit them in the prescribed order (i.e. Borya should first visit doctor 1, then doctor 2, then doctor 3 and so on). Borya will get the information about his health from the last doctor.
Doctors have a strange working schedule. The doctor *i* goes to work on the *s**i*-th day and works every *d**i* day. So, he works on days *s**i*,<=*s**i*<=+<=*d**i*,<=*s**i*<=+<=2*d**i*,<=....
The doctor's appointment takes quite a long time, so Borya can not see more than one doctor per day. What is the minimum time he needs to visit all doctors? | First line contains an integer *n* β number of doctors (1<=β€<=*n*<=β€<=1000).
Next *n* lines contain two numbers *s**i* and *d**i* (1<=β€<=*s**i*,<=*d**i*<=β€<=1000). | Output a single integer β the minimum day at which Borya can visit the last doctor. | [
"3\n2 2\n1 2\n2 2\n",
"2\n10 1\n6 5\n"
] | [
"4\n",
"11\n"
] | In the first sample case, Borya can visit all doctors on days 2, 3 and 4.
In the second sample case, Borya can visit all doctors on days 10 and 11. | [
{
"input": "3\n2 2\n1 2\n2 2",
"output": "4"
},
{
"input": "2\n10 1\n6 5",
"output": "11"
},
{
"input": "3\n6 10\n3 3\n8 2",
"output": "10"
},
{
"input": "4\n4 8\n10 10\n4 2\n8 2",
"output": "14"
},
{
"input": "5\n7 1\n5 1\n6 1\n1 6\n6 8",
"output": "14"
},
... | 389 | 2,969,600 | 3 | 2,691 | |
864 | Fair Game | [
"implementation",
"sortings"
] | null | null | Petya and Vasya decided to play a game. They have *n* cards (*n* is an even number). A single integer is written on each card.
Before the game Petya will choose an integer and after that Vasya will choose another integer (different from the number that Petya chose). During the game each player takes all the cards with number he chose. For example, if Petya chose number 5 before the game he will take all cards on which 5 is written and if Vasya chose number 10 before the game he will take all cards on which 10 is written.
The game is considered fair if Petya and Vasya can take all *n* cards, and the number of cards each player gets is the same.
Determine whether Petya and Vasya can choose integer numbers before the game so that the game is fair. | The first line contains a single integer *n* (2<=β€<=*n*<=β€<=100) β number of cards. It is guaranteed that *n* is an even number.
The following *n* lines contain a sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (one integer per line, 1<=β€<=*a**i*<=β€<=100) β numbers written on the *n* cards. | If it is impossible for Petya and Vasya to choose numbers in such a way that the game will be fair, print "NO" (without quotes) in the first line. In this case you should not print anything more.
In the other case print "YES" (without quotes) in the first line. In the second line print two distinct integers β number that Petya should choose and the number that Vasya should choose to make the game fair. If there are several solutions, print any of them. | [
"4\n11\n27\n27\n11\n",
"2\n6\n6\n",
"6\n10\n20\n30\n20\n10\n20\n",
"6\n1\n1\n2\n2\n3\n3\n"
] | [
"YES\n11 27\n",
"NO\n",
"NO\n",
"NO\n"
] | In the first example the game will be fair if, for example, Petya chooses number 11, and Vasya chooses number 27. Then the will take all cards β Petya will take cards 1 and 4, and Vasya will take cards 2 and 3. Thus, each of them will take exactly two cards.
In the second example fair game is impossible because the numbers written on the cards are equal, but the numbers that Petya and Vasya should choose should be distinct.
In the third example it is impossible to take all cards. Petya and Vasya can take at most five cards β for example, Petya can choose number 10 and Vasya can choose number 20. But for the game to be fair it is necessary to take 6 cards. | [
{
"input": "4\n11\n27\n27\n11",
"output": "YES\n11 27"
},
{
"input": "2\n6\n6",
"output": "NO"
},
{
"input": "6\n10\n20\n30\n20\n10\n20",
"output": "NO"
},
{
"input": "6\n1\n1\n2\n2\n3\n3",
"output": "NO"
},
{
"input": "2\n1\n100",
"output": "YES\n1 100"
},
... | 93 | 0 | 0 | 2,697 | |
437 | The Child and Toy | [
"graphs",
"greedy",
"sortings"
] | null | null | On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy.
The toy consists of *n* parts and *m* ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part *i* as *v**i*. The child spend *v**f*1<=+<=*v**f*2<=+<=...<=+<=*v**f**k* energy for removing part *i* where *f*1,<=*f*2,<=...,<=*f**k* are the parts that are directly connected to the *i*-th and haven't been removed.
Help the child to find out, what is the minimum total energy he should spend to remove all *n* parts. | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=1000; 0<=β€<=*m*<=β€<=2000). The second line contains *n* integers: *v*1,<=*v*2,<=...,<=*v**n* (0<=β€<=*v**i*<=β€<=105). Then followed *m* lines, each line contains two integers *x**i* and *y**i*, representing a rope from part *x**i* to part *y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=*n*;Β *x**i*<=β <=*y**i*).
Consider all the parts are numbered from 1 to *n*. | Output the minimum total energy the child should spend to remove all *n* parts of the toy. | [
"4 3\n10 20 30 40\n1 4\n1 2\n2 3\n",
"4 4\n100 100 100 100\n1 2\n2 3\n2 4\n3 4\n",
"7 10\n40 10 20 10 20 80 40\n1 5\n4 7\n4 5\n5 2\n5 7\n6 4\n1 6\n1 3\n4 3\n1 4\n"
] | [
"40\n",
"400\n",
"160\n"
] | One of the optimal sequence of actions in the first sample is:
- First, remove part 3, cost of the action is 20. - Then, remove part 2, cost of the action is 10. - Next, remove part 4, cost of the action is 10. - At last, remove part 1, cost of the action is 0.
So the total energy the child paid is 20β+β10β+β10β+β0β=β40, which is the minimum.
In the second sample, the child will spend 400 no matter in what order he will remove the parts. | [
{
"input": "4 3\n10 20 30 40\n1 4\n1 2\n2 3",
"output": "40"
},
{
"input": "4 4\n100 100 100 100\n1 2\n2 3\n2 4\n3 4",
"output": "400"
},
{
"input": "7 10\n40 10 20 10 20 80 40\n1 5\n4 7\n4 5\n5 2\n5 7\n6 4\n1 6\n1 3\n4 3\n1 4",
"output": "160"
},
{
"input": "1 0\n23333",
... | 62 | 102,400 | 3 | 2,699 | |
322 | Ciel and Flowers | [
"combinatorics",
"math"
] | null | null | Fox Ciel has some flowers: *r* red flowers, *g* green flowers and *b* blue flowers. She wants to use these flowers to make several bouquets. There are 4 types of bouquets:
- To make a "red bouquet", it needs 3 red flowers. - To make a "green bouquet", it needs 3 green flowers. - To make a "blue bouquet", it needs 3 blue flowers. - To make a "mixing bouquet", it needs 1 red, 1 green and 1 blue flower.
Help Fox Ciel to find the maximal number of bouquets she can make. | The first line contains three integers *r*, *g* and *b* (0<=β€<=*r*,<=*g*,<=*b*<=β€<=109) β the number of red, green and blue flowers. | Print the maximal number of bouquets Fox Ciel can make. | [
"3 6 9\n",
"4 4 4\n",
"0 0 0\n"
] | [
"6\n",
"4\n",
"0\n"
] | In test case 1, we can make 1 red bouquet, 2 green bouquets and 3 blue bouquets.
In test case 2, we can make 1 red, 1 green, 1 blue and 1 mixing bouquet. | [
{
"input": "3 6 9",
"output": "6"
},
{
"input": "4 4 4",
"output": "4"
},
{
"input": "0 0 0",
"output": "0"
},
{
"input": "0 3 6",
"output": "3"
},
{
"input": "7 8 9",
"output": "7"
},
{
"input": "8 8 9",
"output": "8"
},
{
"input": "15 3 9... | 124 | 0 | 3 | 2,710 | |
0 | none | [
"none"
] | null | null | Valentin participates in a show called "Shockers". The rules are quite easy: jury selects one letter which Valentin doesn't know. He should make a small speech, but every time he pronounces a word that contains the selected letter, he receives an electric shock. He can make guesses which letter is selected, but for each incorrect guess he receives an electric shock too. The show ends when Valentin guesses the selected letter correctly.
Valentin can't keep in mind everything, so he could guess the selected letter much later than it can be uniquely determined and get excessive electric shocks. Excessive electric shocks are those which Valentin got after the moment the selected letter can be uniquely determined. You should find out the number of excessive electric shocks. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=105)Β β the number of actions Valentin did.
The next *n* lines contain descriptions of his actions, each line contains description of one action. Each action can be of one of three types:
1. Valentin pronounced some word and didn't get an electric shock. This action is described by the string ". w" (without quotes), in which "." is a dot (ASCII-code 46), and *w* is the word that Valentin said. 1. Valentin pronounced some word and got an electric shock. This action is described by the string "! w" (without quotes), in which "!" is an exclamation mark (ASCII-code 33), and *w* is the word that Valentin said. 1. Valentin made a guess about the selected letter. This action is described by the string "? s" (without quotes), in which "?" is a question mark (ASCII-code 63), and *s* is the guessΒ β a lowercase English letter.
All words consist only of lowercase English letters. The total length of all words does not exceed 105.
It is guaranteed that last action is a guess about the selected letter. Also, it is guaranteed that Valentin didn't make correct guesses about the selected letter before the last action. Moreover, it's guaranteed that if Valentin got an electric shock after pronouncing some word, then it contains the selected letter; and also if Valentin didn't get an electric shock after pronouncing some word, then it does not contain the selected letter. | Output a single integerΒ β the number of electric shocks that Valentin could have avoided if he had told the selected letter just after it became uniquely determined. | [
"5\n! abc\n. ad\n. b\n! cd\n? c\n",
"8\n! hello\n! codeforces\n? c\n. o\n? d\n? h\n. l\n? e\n",
"7\n! ababahalamaha\n? a\n? b\n? a\n? b\n? a\n? h\n"
] | [
"1\n",
"2\n",
"0\n"
] | In the first test case after the first action it becomes clear that the selected letter is one of the following: *a*,β*b*,β*c*. After the second action we can note that the selected letter is not *a*. Valentin tells word "b" and doesn't get a shock. After that it is clear that the selected letter is *c*, but Valentin pronounces the word *cd* and gets an excessive electric shock.
In the second test case after the first two electric shocks we understand that the selected letter is *e* or *o*. Valentin tries some words consisting of these letters and after the second word it's clear that the selected letter is *e*, but Valentin makes 3 more actions before he makes a correct hypothesis.
In the third example the selected letter can be uniquely determined only when Valentin guesses it, so he didn't get excessive electric shocks. | [
{
"input": "5\n! abc\n. ad\n. b\n! cd\n? c",
"output": "1"
},
{
"input": "8\n! hello\n! codeforces\n? c\n. o\n? d\n? h\n. l\n? e",
"output": "2"
},
{
"input": "7\n! ababahalamaha\n? a\n? b\n? a\n? b\n? a\n? h",
"output": "0"
},
{
"input": "4\n! abcd\n! cdef\n? d\n? c",
"o... | 249 | 6,656,000 | 3 | 2,711 | |
362 | Petya and Staircases | [
"implementation",
"sortings"
] | null | null | Little boy Petya loves stairs very much. But he is bored from simple going up and down them β he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.
Now Petya is on the first stair of the staircase, consisting of *n* stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number *n* without touching a dirty stair once.
One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only. | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=109, 0<=β€<=*m*<=β€<=3000) β the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains *m* different space-separated integers *d*1,<=*d*2,<=...,<=*d**m* (1<=β€<=*d**i*<=β€<=*n*) β the numbers of the dirty stairs (in an arbitrary order). | Print "YES" if Petya can reach stair number *n*, stepping only on the clean stairs. Otherwise print "NO". | [
"10 5\n2 4 8 3 6\n",
"10 5\n2 4 5 7 9\n"
] | [
"NO",
"YES"
] | none | [
{
"input": "10 5\n2 4 8 3 6",
"output": "NO"
},
{
"input": "10 5\n2 4 5 7 9",
"output": "YES"
},
{
"input": "10 9\n2 3 4 5 6 7 8 9 10",
"output": "NO"
},
{
"input": "5 2\n4 5",
"output": "NO"
},
{
"input": "123 13\n36 73 111 2 92 5 47 55 48 113 7 78 37",
"outp... | 78 | 7,065,600 | -1 | 2,716 | |
1,004 | Sonya and Robots | [
"constructive algorithms",
"implementation"
] | null | null | Since Sonya is interested in robotics too, she decided to construct robots that will read and recognize numbers.
Sonya has drawn $n$ numbers in a row, $a_i$ is located in the $i$-th position. She also has put a robot at each end of the row (to the left of the first number and to the right of the last number). Sonya will give a number to each robot (they can be either same or different) and run them. When a robot is running, it is moving toward to another robot, reading numbers in the row. When a robot is reading a number that is equal to the number that was given to that robot, it will turn off and stay in the same position.
Sonya does not want robots to break, so she will give such numbers that robots will stop before they meet. That is, the girl wants them to stop at different positions so that the first robot is to the left of the second one.
For example, if the numbers $[1, 5, 4, 1, 3]$ are written, and Sonya gives the number $1$ to the first robot and the number $4$ to the second one, the first robot will stop in the $1$-st position while the second one in the $3$-rd position. In that case, robots will not meet each other. As a result, robots will not be broken. But if Sonya gives the number $4$ to the first robot and the number $5$ to the second one, they will meet since the first robot will stop in the $3$-rd position while the second one is in the $2$-nd position.
Sonya understands that it does not make sense to give a number that is not written in the row because a robot will not find this number and will meet the other robot.
Sonya is now interested in finding the number of different pairs that she can give to robots so that they will not meet. In other words, she wants to know the number of pairs ($p$, $q$), where she will give $p$ to the first robot and $q$ to the second one. Pairs ($p_i$, $q_i$) and ($p_j$, $q_j$) are different if $p_i\neq p_j$ or $q_i\neq q_j$.
Unfortunately, Sonya is busy fixing robots that broke after a failed launch. That is why she is asking you to find the number of pairs that she can give to robots so that they will not meet. | The first line contains a single integer $n$ ($1\leq n\leq 10^5$)Β β the number of numbers in a row.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1\leq a_i\leq 10^5$)Β β the numbers in a row. | Print one numberΒ β the number of possible pairs that Sonya can give to robots so that they will not meet. | [
"5\n1 5 4 1 3\n",
"7\n1 2 1 1 1 3 2\n"
] | [
"9\n",
"7\n"
] | In the first example, Sonya can give pairs ($1$, $1$), ($1$, $3$), ($1$, $4$), ($1$, $5$), ($4$, $1$), ($4$, $3$), ($5$, $1$), ($5$, $3$), and ($5$, $4$).
In the second example, Sonya can give pairs ($1$, $1$), ($1$, $2$), ($1$, $3$), ($2$, $1$), ($2$, $2$), ($2$, $3$), and ($3$, $2$). | [
{
"input": "5\n1 5 4 1 3",
"output": "9"
},
{
"input": "7\n1 2 1 1 1 3 2",
"output": "7"
},
{
"input": "10\n2 2 4 4 3 1 1 2 3 2",
"output": "14"
},
{
"input": "15\n1 2 2 1 2 4 2 1 1 6 6 4 2 5 4",
"output": "20"
},
{
"input": "1\n1",
"output": "0"
}
] | 186 | 1,024,000 | -1 | 2,717 | |
858 | Which floor? | [
"brute force",
"implementation"
] | null | null | In a building where Polycarp lives there are equal number of flats on each floor. Unfortunately, Polycarp don't remember how many flats are on each floor, but he remembers that the flats are numbered from 1 from lower to upper floors. That is, the first several flats are on the first floor, the next several flats are on the second and so on. Polycarp don't remember the total number of flats in the building, so you can consider the building to be infinitely high (i.e. there are infinitely many floors). Note that the floors are numbered from 1.
Polycarp remembers on which floors several flats are located. It is guaranteed that this information is not self-contradictory. It means that there exists a building with equal number of flats on each floor so that the flats from Polycarp's memory have the floors Polycarp remembers.
Given this information, is it possible to restore the exact floor for flat *n*? | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=100, 0<=β€<=*m*<=β€<=100), where *n* is the number of the flat you need to restore floor for, and *m* is the number of flats in Polycarp's memory.
*m* lines follow, describing the Polycarp's memory: each of these lines contains a pair of integers *k**i*,<=*f**i* (1<=β€<=*k**i*<=β€<=100, 1<=β€<=*f**i*<=β€<=100), which means that the flat *k**i* is on the *f**i*-th floor. All values *k**i* are distinct.
It is guaranteed that the given information is not self-contradictory. | Print the number of the floor in which the *n*-th flat is located, if it is possible to determine it in a unique way. Print -1 if it is not possible to uniquely restore this floor. | [
"10 3\n6 2\n2 1\n7 3\n",
"8 4\n3 1\n6 2\n5 2\n2 1\n"
] | [
"4\n",
"-1\n"
] | In the first example the 6-th flat is on the 2-nd floor, while the 7-th flat is on the 3-rd, so, the 6-th flat is the last on its floor and there are 3 flats on each floor. Thus, the 10-th flat is on the 4-th floor.
In the second example there can be 3 or 4 flats on each floor, so we can't restore the floor for the 8-th flat. | [
{
"input": "10 3\n6 2\n2 1\n7 3",
"output": "4"
},
{
"input": "8 4\n3 1\n6 2\n5 2\n2 1",
"output": "-1"
},
{
"input": "8 3\n7 2\n6 2\n1 1",
"output": "2"
},
{
"input": "4 2\n8 3\n3 1",
"output": "2"
},
{
"input": "11 4\n16 4\n11 3\n10 3\n15 4",
"output": "3"
... | 46 | 0 | 0 | 2,728 | |
960 | Minimize the error | [
"data structures",
"greedy",
"sortings"
] | null | null | You are given two arrays *A* and *B*, each of size *n*. The error, *E*, between these two arrays is defined . You have to perform exactly *k*1 operations on array *A* and exactly *k*2 operations on array *B*. In one operation, you have to choose one element of the array and increase or decrease it by 1.
Output the minimum possible value of error after *k*1 operations on array *A* and *k*2 operations on array *B* have been performed. | The first line contains three space-separated integers *n* (1<=β€<=*n*<=β€<=103), *k*1 and *k*2 (0<=β€<=*k*1<=+<=*k*2<=β€<=103, *k*1 and *k*2 are non-negative) β size of arrays and number of operations to perform on *A* and *B* respectively.
Second line contains *n* space separated integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=106<=β€<=*a**i*<=β€<=106) β array *A*.
Third line contains *n* space separated integers *b*1,<=*b*2,<=...,<=*b**n* (<=-<=106<=β€<=*b**i*<=β€<=106)β array *B*. | Output a single integer β the minimum possible value of after doing exactly *k*1 operations on array *A* and exactly *k*2 operations on array *B*. | [
"2 0 0\n1 2\n2 3\n",
"2 1 0\n1 2\n2 2\n",
"2 5 7\n3 4\n14 4\n"
] | [
"2",
"0",
"1"
] | In the first sample case, we cannot perform any operations on *A* or *B*. Therefore the minimum possible error *E*β=β(1β-β2)<sup class="upper-index">2</sup>β+β(2β-β3)<sup class="upper-index">2</sup>β=β2.
In the second sample case, we are required to perform exactly one operation on *A*. In order to minimize error, we increment the first element of *A* by 1. Now, *A*β=β[2,β2]. The error is now *E*β=β(2β-β2)<sup class="upper-index">2</sup>β+β(2β-β2)<sup class="upper-index">2</sup>β=β0. This is the minimum possible error obtainable.
In the third sample case, we can increase the first element of *A* to 8, using the all of the 5 moves available to us. Also, the first element of *B* can be reduced to 8 using the 6 of the 7 available moves. Now *A*β=β[8,β4] and *B*β=β[8,β4]. The error is now *E*β=β(8β-β8)<sup class="upper-index">2</sup>β+β(4β-β4)<sup class="upper-index">2</sup>β=β0, but we are still left with 1 move for array *B*. Increasing the second element of *B* to 5 using the left move, we get *B*β=β[8,β5] and *E*β=β(8β-β8)<sup class="upper-index">2</sup>β+β(4β-β5)<sup class="upper-index">2</sup>β=β1. | [
{
"input": "2 0 0\n1 2\n2 3",
"output": "2"
},
{
"input": "2 1 0\n1 2\n2 2",
"output": "0"
},
{
"input": "2 5 7\n3 4\n14 4",
"output": "1"
},
{
"input": "2 0 1\n1 2\n2 2",
"output": "0"
},
{
"input": "2 1 1\n0 0\n1 1",
"output": "0"
},
{
"input": "5 5 ... | 78 | 7,065,600 | 0 | 2,739 | |
0 | none | [
"none"
] | null | null | Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures).
There are *n* members, numbered 1 through *n*. *m* pairs of members are friends. Of course, a member can't be a friend with themselves.
Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z.
For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well.
Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes. | The first line of the input contain two integers *n* and *m* (3<=β€<=*n*<=β€<=150<=000, )Β β the number of members and the number of pairs of members that are friends.
The *i*-th of the next *m* lines contains two distinct integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*,<=*a**i*<=β <=*b**i*). Members *a**i* and *b**i* are friends with each other. No pair of members will appear more than once in the input. | If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes). | [
"4 3\n1 3\n3 4\n1 4\n",
"4 4\n3 1\n2 3\n3 4\n1 2\n",
"10 4\n4 3\n5 10\n8 9\n1 2\n",
"3 2\n1 2\n2 3\n"
] | [
"YES\n",
"NO\n",
"YES\n",
"NO\n"
] | The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2,β3) are friends and members (3,β4) are friends, while members (2,β4) are not. | [
{
"input": "4 3\n1 3\n3 4\n1 4",
"output": "YES"
},
{
"input": "4 4\n3 1\n2 3\n3 4\n1 2",
"output": "NO"
},
{
"input": "10 4\n4 3\n5 10\n8 9\n1 2",
"output": "YES"
},
{
"input": "3 2\n1 2\n2 3",
"output": "NO"
},
{
"input": "3 0",
"output": "YES"
},
{
... | 1,000 | 77,414,400 | 0 | 2,740 | |
848 | Rooter's Song | [
"constructive algorithms",
"data structures",
"geometry",
"implementation",
"sortings",
"two pointers"
] | null | null | Wherever the destination is, whoever we meet, let's render this song together.
On a Cartesian coordinate plane lies a rectangular stage of size *w*<=Γ<=*h*, represented by a rectangle with corners (0,<=0), (*w*,<=0), (*w*,<=*h*) and (0,<=*h*). It can be seen that no collisions will happen before one enters the stage.
On the sides of the stage stand *n* dancers. The *i*-th of them falls into one of the following groups:
- Vertical: stands at (*x**i*,<=0), moves in positive *y* direction (upwards); - Horizontal: stands at (0,<=*y**i*), moves in positive *x* direction (rightwards).
According to choreography, the *i*-th dancer should stand still for the first *t**i* milliseconds, and then start moving in the specified direction at 1 unit per millisecond, until another border is reached. It is guaranteed that no two dancers have the same group, position and waiting time at the same time.
When two dancers collide (i.e. are on the same point at some time when both of them are moving), they immediately exchange their moving directions and go on.
Dancers stop when a border of the stage is reached. Find out every dancer's stopping position. | The first line of input contains three space-separated positive integers *n*, *w* and *h* (1<=β€<=*n*<=β€<=100<=000, 2<=β€<=*w*,<=*h*<=β€<=100<=000) β the number of dancers and the width and height of the stage, respectively.
The following *n* lines each describes a dancer: the *i*-th among them contains three space-separated integers *g**i*, *p**i*, and *t**i* (1<=β€<=*g**i*<=β€<=2, 1<=β€<=*p**i*<=β€<=99<=999, 0<=β€<=*t**i*<=β€<=100<=000), describing a dancer's group *g**i* (*g**i*<==<=1 β vertical, *g**i*<==<=2 β horizontal), position, and waiting time. If *g**i*<==<=1 then *p**i*<==<=*x**i*; otherwise *p**i*<==<=*y**i*. It's guaranteed that 1<=β€<=*x**i*<=β€<=*w*<=-<=1 and 1<=β€<=*y**i*<=β€<=*h*<=-<=1. It is guaranteed that no two dancers have the same group, position and waiting time at the same time. | Output *n* lines, the *i*-th of which contains two space-separated integers (*x**i*,<=*y**i*) β the stopping position of the *i*-th dancer in the input. | [
"8 10 8\n1 1 10\n1 4 13\n1 7 1\n1 8 2\n2 2 0\n2 5 14\n2 6 0\n2 6 1\n",
"3 2 3\n1 1 2\n2 1 1\n1 1 5\n"
] | [
"4 8\n10 5\n8 8\n10 6\n10 2\n1 8\n7 8\n10 6\n",
"1 3\n2 1\n1 3\n"
] | The first example corresponds to the initial setup in the legend, and the tracks of dancers are marked with different colours in the following figure.
In the second example, no dancers collide. | [
{
"input": "8 10 8\n1 1 10\n1 4 13\n1 7 1\n1 8 2\n2 2 0\n2 5 14\n2 6 0\n2 6 1",
"output": "4 8\n10 5\n8 8\n10 6\n10 2\n1 8\n7 8\n10 6"
},
{
"input": "3 2 3\n1 1 2\n2 1 1\n1 1 5",
"output": "1 3\n2 1\n1 3"
},
{
"input": "1 10 10\n1 8 1",
"output": "8 10"
},
{
"input": "3 4 5\n... | 46 | 0 | 0 | 2,742 | |
194 | Square | [
"math"
] | null | null | There is a square painted on a piece of paper, the square's side equals *n* meters. John Doe draws crosses on the square's perimeter. John paints the first cross in the lower left corner of the square. Then John moves along the square's perimeter in the clockwise direction (first upwards, then to the right, then downwards, then to the left and so on). Every time he walks (*n*<=+<=1) meters, he draws a cross (see picture for clarifications).
John Doe stops only when the lower left corner of the square has two crosses. How many crosses will John draw? | The first line contains integer *t* (1<=β€<=*t*<=β€<=104) β the number of test cases.
The second line contains *t* space-separated integers *n**i* (1<=β€<=*n**i*<=β€<=109) β the sides of the square for each test sample. | For each test sample print on a single line the answer to it, that is, the number of crosses John will draw as he will move along the square of the corresponding size. Print the answers to the samples in the order in which the samples are given in the input.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"3\n4 8 100\n"
] | [
"17\n33\n401\n"
] | none | [
{
"input": "3\n4 8 100",
"output": "17\n33\n401"
},
{
"input": "8\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 13",
"output": "4000000001\n4000000001\n4000000001\n4000000001\n4000000001\n4000000001\n4000000001\n27"
},
{
"input": "3\n13 17 21",
"output... | 187 | 4,710,400 | 3 | 2,745 | |
21 | Intersection | [
"implementation",
"math"
] | B. Intersection | 1 | 256 | You are given two set of points. The first set is determined by the equation *A*1*x*<=+<=*B*1*y*<=+<=*C*1<==<=0, and the second one is determined by the equation *A*2*x*<=+<=*B*2*y*<=+<=*C*2<==<=0.
Write the program which finds the number of points in the intersection of two given sets. | The first line of the input contains three integer numbers *A*1,<=*B*1,<=*C*1 separated by space. The second line contains three integer numbers *A*2,<=*B*2,<=*C*2 separated by space. All the numbers are between -100 and 100, inclusive. | Print the number of points in the intersection or -1 if there are infinite number of points. | [
"1 1 0\n2 2 0\n",
"1 1 0\n2 -2 0\n"
] | [
"-1\n",
"1\n"
] | none | [
{
"input": "1 1 0\n2 2 0",
"output": "-1"
},
{
"input": "1 1 0\n2 -2 0",
"output": "1"
},
{
"input": "0 0 0\n0 0 0",
"output": "-1"
},
{
"input": "1 1 1\n1 1 1",
"output": "-1"
},
{
"input": "8 3 -4\n-5 2 7",
"output": "1"
},
{
"input": "-1 -1 0\n0 -1 ... | 62 | 4,608,000 | 0 | 2,746 |
645 | Robot Rapping Results Report | [
"binary search",
"dp",
"graphs"
] | null | null | While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some alternative jobs. In her new gig as a reporter, Bessie needs to know about programming competition results as quickly as possible. When she covers the 2016 Robot Rap Battle Tournament, she notices that all of the robots operate under deterministic algorithms. In particular, robot *i* will beat robot *j* if and only if robot *i* has a higher skill level than robot *j*. And if robot *i* beats robot *j* and robot *j* beats robot *k*, then robot *i* will beat robot *k*. Since rapping is such a subtle art, two robots can never have the same skill level.
Given the results of the rap battles in the order in which they were played, determine the minimum number of first rap battles that needed to take place before Bessie could order all of the robots by skill level. | The first line of the input consists of two integers, the number of robots *n* (2<=β€<=*n*<=β€<=100<=000) and the number of rap battles *m* ().
The next *m* lines describe the results of the rap battles in the order they took place. Each consists of two integers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*, *u**i*<=β <=*v**i*), indicating that robot *u**i* beat robot *v**i* in the *i*-th rap battle. No two rap battles involve the same pair of robots.
It is guaranteed that at least one ordering of the robots satisfies all *m* relations. | Print the minimum *k* such that the ordering of the robots by skill level is uniquely defined by the first *k* rap battles. If there exists more than one ordering that satisfies all *m* relations, output -1. | [
"4 5\n2 1\n1 3\n2 3\n4 2\n4 3\n",
"3 2\n1 2\n3 2\n"
] | [
"4\n",
"-1\n"
] | In the first sample, the robots from strongest to weakest must be (4,β2,β1,β3), which Bessie can deduce after knowing the results of the first four rap battles.
In the second sample, both (1,β3,β2) and (3,β1,β2) are possible orderings of the robots from strongest to weakest after both rap battles. | [
{
"input": "4 5\n2 1\n1 3\n2 3\n4 2\n4 3",
"output": "4"
},
{
"input": "3 2\n1 2\n3 2",
"output": "-1"
},
{
"input": "2 1\n1 2",
"output": "1"
},
{
"input": "2 1\n2 1",
"output": "1"
},
{
"input": "5 10\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4\n3 5\n4 5",
"outp... | 62 | 0 | 0 | 2,750 | |
558 | Lala Land and Apple Trees | [
"brute force",
"implementation",
"sortings"
] | null | null | Amr lives in Lala Land. Lala Land is a very beautiful country that is located on a coordinate line. Lala Land is famous with its apple trees growing everywhere.
Lala Land has exactly *n* apple trees. Tree number *i* is located in a position *x**i* and has *a**i* apples growing on it. Amr wants to collect apples from the apple trees. Amr currently stands in *x*<==<=0 position. At the beginning, he can choose whether to go right or left. He'll continue in his direction until he meets an apple tree he didn't visit before. He'll take all of its apples and then reverse his direction, continue walking in this direction until he meets another apple tree he didn't visit before and so on. In the other words, Amr reverses his direction when visiting each new apple tree. Amr will stop collecting apples when there are no more trees he didn't visit in the direction he is facing.
What is the maximum number of apples he can collect? | The first line contains one number *n* (1<=β€<=*n*<=β€<=100), the number of apple trees in Lala Land.
The following *n* lines contains two integers each *x**i*, *a**i* (<=-<=105<=β€<=*x**i*<=β€<=105, *x**i*<=β <=0, 1<=β€<=*a**i*<=β€<=105), representing the position of the *i*-th tree and number of apples on it.
It's guaranteed that there is at most one apple tree at each coordinate. It's guaranteed that no tree grows in point 0. | Output the maximum number of apples Amr can collect. | [
"2\n-1 5\n1 5\n",
"3\n-2 2\n1 4\n-1 3\n",
"3\n1 9\n3 5\n7 10\n"
] | [
"10",
"9",
"9"
] | In the first sample test it doesn't matter if Amr chose at first to go left or right. In both cases he'll get all the apples.
In the second sample test the optimal solution is to go left to *x*β=ββ-β1, collect apples from there, then the direction will be reversed, Amr has to go to *x*β=β1, collect apples from there, then the direction will be reversed and Amr goes to the final tree *x*β=ββ-β2.
In the third sample test the optimal solution is to go right to *x*β=β1, collect apples from there, then the direction will be reversed and Amr will not be able to collect anymore apples because there are no apple trees to his left. | [
{
"input": "2\n-1 5\n1 5",
"output": "10"
},
{
"input": "3\n-2 2\n1 4\n-1 3",
"output": "9"
},
{
"input": "3\n1 9\n3 5\n7 10",
"output": "9"
},
{
"input": "1\n1 1",
"output": "1"
},
{
"input": "4\n10000 100000\n-1000 100000\n-2 100000\n-1 100000",
"output": "3... | 46 | 0 | 0 | 2,751 | |
0 | none | [
"none"
] | null | null | Permutation *p* is an ordered set of integers *p*1,<=<=<=*p*2,<=<=<=...,<=<=<=*p**n*, consisting of *n* distinct positive integers not larger than *n*. We'll denote as *n* the length of permutation *p*1,<=<=<=*p*2,<=<=<=...,<=<=<=*p**n*.
Your task is to find such permutation *p* of length *n*, that the group of numbers |*p*1<=-<=*p*2|,<=|*p*2<=-<=*p*3|,<=...,<=|*p**n*<=-<=1<=-<=*p**n*| has exactly *k* distinct elements. | The single line of the input contains two space-separated positive integers *n*, *k* (1<=β€<=*k*<=<<=*n*<=β€<=105). | Print *n* integers forming the permutation. If there are multiple answers, print any of them. | [
"3 2\n",
"3 1\n",
"5 2\n"
] | [
"1 3 2\n",
"1 2 3\n",
"1 3 2 4 5\n"
] | By |*x*| we denote the absolute value of number *x*. | [
{
"input": "3 2",
"output": "1 3 2"
},
{
"input": "3 1",
"output": "1 2 3"
},
{
"input": "5 2",
"output": "1 3 2 4 5"
},
{
"input": "5 4",
"output": "1 5 2 4 3"
},
{
"input": "10 4",
"output": "1 10 2 9 8 7 6 5 4 3"
},
{
"input": "10 3",
"output": ... | 514 | 3,174,400 | 3 | 2,755 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.