contestId int64 0 1.01k | name stringlengths 2 58 | tags listlengths 0 11 | title stringclasses 523
values | time-limit stringclasses 8
values | memory-limit stringclasses 8
values | problem-description stringlengths 0 7.15k | input-specification stringlengths 0 2.05k | output-specification stringlengths 0 1.5k | demo-input listlengths 0 7 | demo-output listlengths 0 7 | note stringlengths 0 5.24k | test_cases listlengths 0 402 | timeConsumedMillis int64 0 8k | memoryConsumedBytes int64 0 537M | score float64 -1 3.99 | __index_level_0__ int64 0 621k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | none | [
"none"
] | null | null | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya dreamt of a lexicographically *k*-th permutation of integers from 1 to *n*. Determine ... | The first line contains two integers *n* and *k* (1<=β€<=*n*,<=*k*<=β€<=109) β the number of elements in the permutation and the lexicographical number of the permutation. | If the *k*-th permutation of numbers from 1 to *n* does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes *i*, that *i* and *a**i* are both lucky numbers. | [
"7 4\n",
"4 7\n"
] | [
"1\n",
"1\n"
] | A permutation is an ordered set of *n* elements, where each integer from 1 to *n* occurs exactly once. The element of permutation in position with index *i* is denoted as *a*<sub class="lower-index">*i*</sub> (1ββ€β*i*ββ€β*n*). Permutation *a* is lexicographically smaller that permutation *b* if there is such a *i* (1ββ€β... | [
{
"input": "7 4",
"output": "1"
},
{
"input": "4 7",
"output": "1"
},
{
"input": "7 1",
"output": "2"
},
{
"input": "7 5040",
"output": "1"
},
{
"input": "10 1023",
"output": "0"
},
{
"input": "7 7477",
"output": "-1"
},
{
"input": "10 1000... | 60 | 0 | 0 | 5,743 | |
416 | Art Union | [
"brute force",
"dp",
"implementation"
] | null | null | A well-known art union called "Kalevich is Alive!" manufactures objects d'art (pictures). The union consists of *n* painters who decided to organize their work as follows.
Each painter uses only the color that was assigned to him. The colors are distinct for all painters. Let's assume that the first painter uses color... | The first line of the input contains integers *m*,<=*n* (1<=β€<=*m*<=β€<=50000,<=1<=β€<=*n*<=β€<=5), where *m* is the number of pictures and *n* is the number of painters. Then follow the descriptions of the pictures, one per line. Each line contains *n* integers *t**i*1,<=*t**i*2,<=...,<=*t**in* (1<=β€<=*t**ij*<=β€<=1000), ... | Print the sequence of *m* integers *r*1,<=*r*2,<=...,<=*r**m*, where *r**i* is the moment when the *n*-th painter stopped working on the *i*-th picture. | [
"5 1\n1\n2\n3\n4\n5\n",
"4 2\n2 5\n3 1\n5 3\n10 1\n"
] | [
"1 3 6 10 15 ",
"7 8 13 21 "
] | none | [
{
"input": "5 1\n1\n2\n3\n4\n5",
"output": "1 3 6 10 15 "
},
{
"input": "4 2\n2 5\n3 1\n5 3\n10 1",
"output": "7 8 13 21 "
},
{
"input": "1 1\n66",
"output": "66 "
},
{
"input": "2 2\n1 1\n1 1",
"output": "2 3 "
},
{
"input": "2 2\n10 1\n10 1",
"output": "11 2... | 732 | 9,625,600 | 0 | 5,746 | |
660 | Seating On Bus | [
"implementation"
] | null | null | Consider 2*n* rows of the seats in a bus. *n* rows of the seats on the left and *n* rows of the seats on the right. Each row can be filled by two people. So the total capacity of the bus is 4*n*.
Consider that *m* (*m*<=β€<=4*n*) people occupy the seats in the bus. The passengers entering the bus are numbered from 1 to... | The only line contains two integers, *n* and *m* (1<=β€<=*n*<=β€<=100,<=1<=β€<=*m*<=β€<=4*n*) β the number of pairs of rows and the number of passengers. | Print *m* distinct integers from 1 to *m* β the order in which the passengers will get off the bus. | [
"2 7\n",
"9 36\n"
] | [
"5 1 6 2 7 3 4\n",
"19 1 20 2 21 3 22 4 23 5 24 6 25 7 26 8 27 9 28 10 29 11 30 12 31 13 32 14 33 15 34 16 35 17 36 18\n"
] | none | [
{
"input": "2 7",
"output": "5 1 6 2 7 3 4"
},
{
"input": "9 36",
"output": "19 1 20 2 21 3 22 4 23 5 24 6 25 7 26 8 27 9 28 10 29 11 30 12 31 13 32 14 33 15 34 16 35 17 36 18"
},
{
"input": "1 1",
"output": "1"
},
{
"input": "1 4",
"output": "3 1 4 2"
},
{
"input... | 109 | 0 | 0 | 5,753 | |
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* ... | 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, fin... | [
{
"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"
},
{
... | 109 | 0 | 0 | 5,776 | |
262 | Roma and Changing Signs | [
"greedy"
] | null | null | Roma works in a company that sells TVs. Now he has to prepare a report for the last year.
Roma has got a list of the company's incomes. The list is a sequence that consists of *n* integers. The total income of the company is the sum of all integers in sequence. Roma decided to perform exactly *k* changes of signs of s... | The first line contains two integers *n* and *k* (1<=β€<=*n*,<=*k*<=β€<=105), showing, how many numbers are in the sequence and how many swaps are to be made.
The second line contains a non-decreasing sequence, consisting of *n* integers *a**i* (|*a**i*|<=β€<=104).
The numbers in the lines are separated by single spaces... | In the single line print the answer to the problem β the maximum total income that we can obtain after exactly *k* changes. | [
"3 2\n-1 -1 1\n",
"3 1\n-1 -1 1\n"
] | [
"3\n",
"1\n"
] | In the first sample we can get sequence [1, 1, 1], thus the total income equals 3.
In the second test, the optimal strategy is to get sequence [-1, 1, 1], thus the total income equals 1. | [
{
"input": "3 2\n-1 -1 1",
"output": "3"
},
{
"input": "3 1\n-1 -1 1",
"output": "1"
},
{
"input": "17 27\n257 320 676 1136 2068 2505 2639 4225 4951 5786 7677 7697 7851 8337 8429 8469 9343",
"output": "81852"
},
{
"input": "69 28\n-9822 -9264 -9253 -9221 -9139 -9126 -9096 -89... | 280 | 7,372,800 | 0 | 5,796 | |
995 | Tesla | [
"constructive algorithms",
"implementation"
] | null | null | Allen dreams of one day owning a enormous fleet of electric cars, the car of the future! He knows that this will give him a big status boost. As Allen is planning out all of the different types of cars he will own and how he will arrange them, he realizes that he has a problem.
Allen's future parking lot can be repre... | The first line of the input contains two space-separated integers $n$ and $k$ ($1 \le n \le 50$, $1 \le k \le 2n$), representing the number of columns and the number of cars, respectively.
The next four lines will contain $n$ integers each between $0$ and $k$ inclusive, representing the initial state of the parking lo... | If there is a sequence of moves that brings all of the cars to their parking spaces, with at most $20000$ car moves, then print $m$, the number of moves, on the first line. On the following $m$ lines, print the moves (one move per line) in the format $i$ $r$ $c$, which corresponds to Allen moving car $i$ to the neighbo... | [
"4 5\n1 2 0 4\n1 2 0 4\n5 0 0 3\n0 5 0 3\n",
"1 2\n1\n2\n1\n2\n",
"1 2\n1\n1\n2\n2\n"
] | [
"6\n1 1 1\n2 1 2\n4 1 4\n3 4 4\n5 3 2\n5 4 2\n",
"-1\n",
"2\n1 1 1\n2 4 1\n"
] | In the first sample test case, all cars are in front of their spots except car $5$, which is in front of the parking spot adjacent. The example shows the shortest possible sequence of moves, but any sequence of length at most $20000$ will be accepted.
In the second sample test case, there is only one column, and the c... | [
{
"input": "4 5\n1 2 0 4\n1 2 0 4\n5 0 0 3\n0 5 0 3",
"output": "6\n1 1 1\n2 1 2\n4 1 4\n3 4 4\n5 3 2\n5 4 2"
},
{
"input": "1 2\n1\n2\n1\n2",
"output": "-1"
},
{
"input": "1 2\n1\n1\n2\n2",
"output": "2\n1 1 1\n2 4 1"
},
{
"input": "2 2\n1 0\n0 2\n0 1\n0 2",
"output": "7... | 109 | 307,200 | -1 | 5,810 | |
863 | Almost Permutation | [
"flows"
] | null | null | Recently Ivan noticed an array *a* while debugging his code. Now Ivan can't remember this array, but the bug he was trying to fix didn't go away, so Ivan thinks that the data from this array might help him to reproduce the bug.
Ivan clearly remembers that there were *n* elements in the array, and each element was not ... | The first line contains two integer numbers *n* and *q* (1<=β€<=*n*<=β€<=50, 0<=β€<=*q*<=β€<=100).
Then *q* lines follow, each representing a fact about the array. *i*-th line contains the numbers *t**i*, *l**i*, *r**i* and *v**i* for *i*-th fact (1<=β€<=*t**i*<=β€<=2, 1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*n*, 1<=β€<=*v**i*<=β€<=*n*, ... | If the facts are controversial and there is no array that corresponds to them, print -1. Otherwise, print minimum possible *cost* of the array. | [
"3 0\n",
"3 1\n1 1 3 2\n",
"3 2\n1 1 3 2\n2 1 3 2\n",
"3 2\n1 1 3 2\n2 1 3 1\n"
] | [
"3\n",
"5\n",
"9\n",
"-1\n"
] | none | [
{
"input": "3 0",
"output": "3"
},
{
"input": "3 1\n1 1 3 2",
"output": "5"
},
{
"input": "3 2\n1 1 3 2\n2 1 3 2",
"output": "9"
},
{
"input": "3 2\n1 1 3 2\n2 1 3 1",
"output": "-1"
},
{
"input": "50 0",
"output": "50"
},
{
"input": "50 1\n2 31 38 25"... | 46 | 0 | 0 | 5,814 | |
56 | Domino Principle | [
"binary search",
"data structures",
"sortings"
] | E. Domino Principle | 2 | 256 | Vasya is interested in arranging dominoes. He is fed up with common dominoes and he uses the dominoes of different heights. He put *n* dominoes on the table along one axis, going from left to right. Every domino stands perpendicular to that axis so that the axis passes through the center of its base. The *i*-th domino ... | The first line contains integer *n* (1<=β€<=*n*<=β€<=105) which is the number of dominoes. Then follow *n* lines containing two integers *x**i* and *h**i* (<=-<=108<=β€<=*x**i*<=β€<=108,<=2<=β€<=*h**i*<=β€<=108) each, which are the coordinate and height of every domino. No two dominoes stand on one point. | Print *n* space-separated numbers *z**i* β the number of dominoes that will fall if Vasya pushes the *i*-th domino to the right (including the domino itself). | [
"4\n16 5\n20 5\n10 10\n18 2\n",
"4\n0 10\n1 5\n9 10\n15 10\n"
] | [
"3 1 4 1 ",
"4 1 2 1 "
] | none | [
{
"input": "4\n16 5\n20 5\n10 10\n18 2",
"output": "3 1 4 1 "
},
{
"input": "4\n0 10\n1 5\n9 10\n15 10",
"output": "4 1 2 1 "
},
{
"input": "3\n6 7\n2 9\n-6 10",
"output": "1 2 3 "
},
{
"input": "4\n-6 9\n9 5\n-7 4\n0 9",
"output": "2 1 3 1 "
},
{
"input": "5\n-47... | 2,000 | 21,606,400 | 0 | 5,817 |
143 | Help Kingdom of Far Far Away 2 | [
"implementation",
"strings"
] | null | null | For some time the program of rounding numbers that had been developed by the Codeforces participants during one of the previous rounds, helped the citizens of Far Far Away to convert numbers into a more easily readable format. However, as time went by, the economy of the Far Far Away developed and the scale of operatio... | The input contains a number that needs to be converted into financial format. The number's notation length does not exceed 100 characters, including (possible) signs "-" (minus) and "." (decimal point). The number's notation is correct, that is:
- The number's notation only contains characters from the set {"0" β "9... | Print the number given in the input in the financial format by the rules described in the problem statement. | [
"2012\n",
"0.000\n",
"-0.00987654321\n",
"-12345678.9\n"
] | [
"$2,012.00",
"$0.00",
"($0.00)",
"($12,345,678.90)"
] | Pay attention to the second and third sample tests. They show that the sign of a number in the financial format (and consequently, the presence or absence of brackets) is determined solely by the sign of the initial number. It does not depend on the sign of the number you got after translating the number to the financi... | [
{
"input": "2012",
"output": "$2,012.00"
},
{
"input": "0.000",
"output": "$0.00"
},
{
"input": "-0.00987654321",
"output": "($0.00)"
},
{
"input": "-12345678.9",
"output": "($12,345,678.90)"
},
{
"input": "0.99999999999999999999",
"output": "$0.99"
},
{
... | 92 | 512,000 | 0 | 5,824 | |
172 | Phone Code | [
"*special",
"brute force",
"implementation"
] | null | null | Polycarpus has *n* friends in Tarasov city. Polycarpus knows phone numbers of all his friends: they are strings *s*1,<=*s*2,<=...,<=*s**n*. All these strings consist only of digits and have the same length.
Once Polycarpus needed to figure out Tarasov city phone code. He assumed that the phone code of the city is the... | The first line of the input contains an integer *n* (2<=β€<=*n*<=β€<=3Β·104) β the number of Polycarpus's friends. The following *n* lines contain strings *s*1,<=*s*2,<=...,<=*s**n* β the phone numbers of Polycarpus's friends. It is guaranteed that all strings consist only of digits and have the same length from 1 to 20, ... | Print the number of digits in the city phone code. | [
"4\n00209\n00219\n00999\n00909\n",
"2\n1\n2\n",
"3\n77012345678999999999\n77012345678901234567\n77012345678998765432\n"
] | [
"2\n",
"0\n",
"12\n"
] | A prefix of string *t* is a string that is obtained by deleting zero or more digits from the end of string *t*. For example, string "00209" has 6 prefixes: "" (an empty prefix), "0", "00", "002", "0020", "00209".
In the first sample the city phone code is string "00".
In the second sample the city phone code is an em... | [
{
"input": "4\n00209\n00219\n00999\n00909",
"output": "2"
},
{
"input": "2\n1\n2",
"output": "0"
},
{
"input": "3\n77012345678999999999\n77012345678901234567\n77012345678998765432",
"output": "12"
},
{
"input": "5\n4491183345\n4491184811\n4491162340\n4491233399\n4491449214",
... | 248 | 2,969,600 | 3 | 5,833 | |
888 | Almost Identity Permutations | [
"combinatorics",
"dp",
"math"
] | null | null | A permutation *p* of size *n* is an array such that every integer from 1 to *n* occurs exactly once in this array.
Let's call a permutation an almost identity permutation iff there exist at least *n*<=-<=*k* indices *i* (1<=β€<=*i*<=β€<=*n*) such that *p**i*<==<=*i*.
Your task is to count the number of almost identity ... | The first line contains two integers *n* and *k* (4<=β€<=*n*<=β€<=1000, 1<=β€<=*k*<=β€<=4). | Print the number of almost identity permutations for given *n* and *k*. | [
"4 1\n",
"4 2\n",
"5 3\n",
"5 4\n"
] | [
"1\n",
"7\n",
"31\n",
"76\n"
] | none | [
{
"input": "4 1",
"output": "1"
},
{
"input": "4 2",
"output": "7"
},
{
"input": "5 3",
"output": "31"
},
{
"input": "5 4",
"output": "76"
},
{
"input": "200 1",
"output": "1"
},
{
"input": "200 2",
"output": "19901"
},
{
"input": "200 3",
... | 93 | 2,355,200 | 3 | 5,837 | |
48 | Permutations | [
"greedy"
] | D. Permutations | 1 | 256 | A permutation is a sequence of integers from 1 to *n* of length *n* containing each number exactly once. For example, (1), (4,<=3,<=5,<=1,<=2), (3,<=2,<=1) are permutations, and (1,<=1), (4,<=3,<=1), (2,<=3,<=4) are not.
There are many tasks on permutations. Today you are going to solve one of them. Letβs imagine tha... | The first line contains an integer *n* (1<=β€<=*n*<=β€<=105). The next line contains the mixed array of *n* integers, divided with a single space. The numbers in the array are from 1 to 105. | If this array can be split into several permutations so that every element of the array belongs to exactly one permutation, print in the first line the number of permutations. The second line should contain *n* numbers, corresponding to the elements of the given array. If the *i*-th element belongs to the first permuta... | [
"9\n1 2 3 1 2 1 4 2 5\n",
"4\n4 3 2 1\n",
"4\n1 2 2 3\n"
] | [
"3\n3 1 2 1 2 2 2 3 2\n",
"1\n1 1 1 1 ",
"-1\n"
] | In the first sample test the array is split into three permutations: (2,β1), (3,β2,β1,β4,β5), (1,β2). The first permutation is formed by the second and the fourth elements of the array, the second one β by the third, the fifth, the sixth, the seventh and the ninth elements, the third one β by the first and the eigth el... | [
{
"input": "9\n1 2 3 1 2 1 4 2 5",
"output": "3\n1 1 1 2 2 3 1 3 1 "
},
{
"input": "4\n4 3 2 1",
"output": "1\n1 1 1 1 "
},
{
"input": "4\n1 2 2 3",
"output": "-1"
},
{
"input": "1\n1",
"output": "1\n1 "
},
{
"input": "1\n2",
"output": "-1"
},
{
"input... | 93 | 0 | 0 | 5,838 |
923 | Producing Snow | [
"binary search",
"data structures"
] | null | null | Alice likes snow a lot! Unfortunately, this year's winter is already over, and she can't expect to have any more of it. Bob has thus bought her a giftΒ β a large snow maker. He plans to make some amount of snow every day. On day *i* he will make a pile of snow of volume *V**i* and put it in her garden.
Each day, every ... | The first line contains a single integer *N* (1<=β€<=*N*<=β€<=105)Β β the number of days.
The second line contains *N* integers *V*1,<=*V*2,<=...,<=*V**N* (0<=β€<=*V**i*<=β€<=109), where *V**i* is the initial size of a snow pile made on the day *i*.
The third line contains *N* integers *T*1,<=*T*2,<=...,<=*T**N* (0<=β€<=*... | Output a single line with *N* integers, where the *i*-th integer represents the total volume of snow melted on day *i*. | [
"3\n10 10 5\n5 7 2\n",
"5\n30 25 20 15 10\n9 10 12 4 13\n"
] | [
"5 12 4\n",
"9 20 35 11 25\n"
] | In the first sample, Bob first makes a snow pile of volume 10, which melts to the size of 5 on the same day. On the second day, he makes another pile of size 10. Since it is a bit warmer than the day before, the first pile disappears completely while the second pile shrinks to 3. At the end of the second day, he has on... | [
{
"input": "3\n10 10 5\n5 7 2",
"output": "5 12 4"
},
{
"input": "5\n30 25 20 15 10\n9 10 12 4 13",
"output": "9 20 35 11 25"
},
{
"input": "4\n0 0 0 0\n1 2 3 4",
"output": "0 0 0 0"
},
{
"input": "10\n11 39 16 34 25 3 12 11 31 16\n10 0 4 9 8 9 7 8 9 2",
"output": "10 0 9... | 686 | 37,068,800 | 3 | 5,839 | |
1,003 | Intense Heat | [
"brute force",
"implementation",
"math"
] | null | null | The heat during the last few days has been really intense. Scientists from all over the Berland study how the temperatures and weather change, and they claim that this summer is abnormally hot. But any scientific claim sounds a lot more reasonable if there are some numbers involved, so they have decided to actually cal... | The first line contains two integers $n$ and $k$ ($1 \le k \le n \le 5000$) β the number of days in the given period, and the minimum number of days in a segment we consider when calculating heat intensity value, respectively.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 5000$) β the ... | Print one real number β the heat intensity value, i. e., the maximum of average temperatures over all segments of not less than $k$ consecutive days.
Your answer will be considered correct if the following condition holds: $|res - res_0| < 10^{-6}$, where $res$ is your answer, and $res_0$ is the answer given by the... | [
"4 3\n3 4 1 2\n"
] | [
"2.666666666666667\n"
] | none | [
{
"input": "4 3\n3 4 1 2",
"output": "2.666666666666667"
},
{
"input": "5 1\n3 10 9 10 6",
"output": "10.000000000000000"
},
{
"input": "5 2\n7 3 3 1 8",
"output": "5.000000000000000"
},
{
"input": "5 3\n1 7 6 9 1",
"output": "7.333333333333333"
},
{
"input": "5 4... | 4,000 | 0 | 0 | 5,868 | |
850 | Five Dimensional Points | [
"brute force",
"geometry",
"math"
] | null | null | You are given set of *n* points in 5-dimensional space. The points are labeled from 1 to *n*. No two points coincide.
We will call point *a* bad if there are different points *b* and *c*, not equal to *a*, from the given set such that angle between vectors and is acute (i.e. strictly less than ). Otherwise, the poin... | The first line of input contains a single integer *n* (1<=β€<=*n*<=β€<=103)Β β the number of points.
The next *n* lines of input contain five integers *a**i*,<=*b**i*,<=*c**i*,<=*d**i*,<=*e**i* (|*a**i*|,<=|*b**i*|,<=|*c**i*|,<=|*d**i*|,<=|*e**i*|<=β€<=103) Β β the coordinates of the i-th point. All points are distinct. | First, print a single integer *k*Β β the number of good points.
Then, print *k* integers, each on their own lineΒ β the indices of the good points in ascending order. | [
"6\n0 0 0 0 0\n1 0 0 0 0\n0 1 0 0 0\n0 0 1 0 0\n0 0 0 1 0\n0 0 0 0 1\n",
"3\n0 0 1 2 0\n0 0 9 2 0\n0 0 5 9 0\n"
] | [
"1\n1\n",
"0\n"
] | In the first sample, the first point forms exactly a <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/362ac8c7a7496dffc06cd0c843287cd822be63c3.png" style="max-width: 100.0%;max-height: 100.0%;"/> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, ... | [
{
"input": "6\n0 0 0 0 0\n1 0 0 0 0\n0 1 0 0 0\n0 0 1 0 0\n0 0 0 1 0\n0 0 0 0 1",
"output": "1\n1"
},
{
"input": "3\n0 0 1 2 0\n0 0 9 2 0\n0 0 5 9 0",
"output": "0"
},
{
"input": "1\n0 0 0 0 0",
"output": "1\n1"
},
{
"input": "2\n0 1 2 3 4\n5 6 7 8 9",
"output": "2\n1\n2"... | 46 | 0 | 0 | 5,871 | |
863 | Yet Another Array Queries Problem | [
"data structures",
"implementation"
] | null | null | You are given an array *a* of size *n*, and *q* queries to it. There are queries of two types:
- 1 *l**i* *r**i* β perform a cyclic shift of the segment [*l**i*,<=*r**i*] to the right. That is, for every *x* such that *l**i*<=β€<=*x*<=<<=*r**i* new value of *a**x*<=+<=1 becomes equal to old value of *a**x*, and ne... | The first line contains three integer numbers *n*, *q* and *m* (1<=β€<=*n*,<=*q*<=β€<=2Β·105, 1<=β€<=*m*<=β€<=100).
The second line contains *n* integer numbers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=109).
Then *q* lines follow. *i*-th of them contains three integer numbers *t**i*, *l**i*, *r**i*, where *t**i* is the... | Print *m* numbers, *i*-th of which is equal to the number at index *b**i* after all queries are done. | [
"6 3 5\n1 2 3 4 5 6\n2 1 3\n2 3 6\n1 1 6\n2 2 1 5 3\n"
] | [
"3 3 1 5 2 \n"
] | none | [
{
"input": "6 3 5\n1 2 3 4 5 6\n2 1 3\n2 3 6\n1 1 6\n2 2 1 5 3",
"output": "3 3 1 5 2 "
},
{
"input": "5 2 5\n64 3 4 665 2\n1 1 3\n2 1 5\n1 2 3 4 5",
"output": "2 665 3 64 4 "
},
{
"input": "1 1 1\n474812122\n2 1 1\n1",
"output": "474812122 "
}
] | 156 | 102,400 | 0 | 5,882 | |
151 | Phone Numbers | [
"implementation",
"strings"
] | null | null | Winters are just damn freezing cold in Nvodsk! That's why a group of *n* friends prefers to take a taxi, order a pizza and call girls. The phone numbers in the city consist of three pairs of digits (for example, 12-34-56). Each friend has a phonebook of size *s**i* (that's the number of phone numbers). We know that tax... | The first line contains an integer *n* (1<=β€<=*n*<=β€<=100) β the number of friends.
Then follow *n* data blocks that describe each friend's phone books. Each block is presented in the following form: first goes the line that contains integer *s**i* and string *name**i* (0<=β€<=*s**i*<=β€<=100) β the number of phone num... | In the first line print the phrase "If you want to call a taxi, you should call: ". Then print names of all friends whose phone books contain maximal number of taxi phone numbers.
In the second line print the phrase "If you want to order a pizza, you should call: ". Then print names of all friends who have maximal nu... | [
"4\n2 Fedorov\n22-22-22\n98-76-54\n3 Melnikov\n75-19-09\n23-45-67\n99-99-98\n7 Rogulenko\n22-22-22\n11-11-11\n33-33-33\n44-44-44\n55-55-55\n66-66-66\n95-43-21\n3 Kaluzhin\n11-11-11\n99-99-99\n98-65-32\n",
"3\n5 Gleb\n66-66-66\n55-55-55\n01-01-01\n65-43-21\n12-34-56\n3 Serega\n55-55-55\n87-65-43\n65-55-21\n5 Melni... | [
"If you want to call a taxi, you should call: Rogulenko.\nIf you want to order a pizza, you should call: Fedorov, Rogulenko, Kaluzhin.\nIf you want to go to a cafe with a wonderful girl, you should call: Melnikov.\n",
"If you want to call a taxi, you should call: Gleb.\nIf you want to order a pizza, you should ca... | In the first sample you are given four friends. Fedorov's phone book contains one taxi number and one pizza delivery number, Melnikov's phone book only has 3 numbers of girls, Rogulenko's one has 6 taxi numbers and one pizza delivery number, Kaluzhin's one contains 2 taxi numbers and one pizza delivery number.
Thus, i... | [
{
"input": "4\n2 Fedorov\n22-22-22\n98-76-54\n3 Melnikov\n75-19-09\n23-45-67\n99-99-98\n7 Rogulenko\n22-22-22\n11-11-11\n33-33-33\n44-44-44\n55-55-55\n66-66-66\n95-43-21\n3 Kaluzhin\n11-11-11\n99-99-99\n98-65-32",
"output": "If you want to call a taxi, you should call: Rogulenko.\nIf you want to order a piz... | 46 | 0 | 0 | 5,883 | |
567 | Berland National Library | [
"implementation"
] | null | null | Berland National Library has recently been built in the capital of Berland. In addition, in the library you can take any of the collected works of Berland leaders, the library has a reading room.
Today was the pilot launch of an automated reading room visitors' accounting system! The scanner of the system is installed... | The first line contains a positive integer *n* (1<=β€<=*n*<=β€<=100) β the number of records in the system log. Next follow *n* events from the system journal in the order in which the were made. Each event was written on a single line and looks as "+ *r**i*" or "- *r**i*", where *r**i* is an integer from 1 to 106, the r... | Print a single integer β the minimum possible capacity of the reading room. | [
"6\n+ 12001\n- 12001\n- 1\n- 1200\n+ 1\n+ 7\n",
"2\n- 1\n- 2\n",
"2\n+ 1\n- 1\n"
] | [
"3",
"2",
"1"
] | In the first sample test, the system log will ensure that at some point in the reading room were visitors with registration numbers 1, 1200 and 12001. More people were not in the room at the same time based on the log. Therefore, the answer to the test is 3. | [
{
"input": "6\n+ 12001\n- 12001\n- 1\n- 1200\n+ 1\n+ 7",
"output": "3"
},
{
"input": "2\n- 1\n- 2",
"output": "2"
},
{
"input": "2\n+ 1\n- 1",
"output": "1"
},
{
"input": "5\n+ 1\n- 1\n+ 2\n+ 3\n- 4",
"output": "3"
},
{
"input": "3\n- 1\n- 2\n- 3",
"output": "... | 78 | 0 | 0 | 5,903 | |
552 | Vanya and Scales | [
"brute force",
"dp",
"greedy",
"math",
"meet-in-the-middle",
"number theory"
] | null | null | Vanya has a scales for weighing loads and weights of masses *w*0,<=*w*1,<=*w*2,<=...,<=*w*100 grams where *w* is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass *m* using the given weights, if the weights can be put on both pans of the scale... | The first line contains two integers *w*,<=*m* (2<=β€<=*w*<=β€<=109, 1<=β€<=*m*<=β€<=109) β the number defining the masses of the weights and the mass of the item. | Print word 'YES' if the item can be weighted and 'NO' if it cannot. | [
"3 7\n",
"100 99\n",
"100 50\n"
] | [
"YES\n",
"YES\n",
"NO\n"
] | Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7β+β3β=β9β+β1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can ha... | [
{
"input": "3 7",
"output": "YES"
},
{
"input": "100 99",
"output": "YES"
},
{
"input": "100 50",
"output": "NO"
},
{
"input": "1000000000 1",
"output": "YES"
},
{
"input": "100 10002",
"output": "NO"
},
{
"input": "4 7",
"output": "NO"
},
{
... | 108 | 0 | 0 | 5,924 | |
0 | none | [
"none"
] | null | null | From beginning till end, this message has been waiting to be conveyed.
For a given unordered multiset of *n* lowercase English letters ("multi" means that a letter may appear more than once), we treat all letters as strings of length 1, and repeat the following operation *n*<=-<=1 times:
- Remove any two elements *s... | The first and only line of input contains a non-negative integer *k* (0<=β€<=*k*<=β€<=100<=000) β the required minimum cost. | Output a non-empty string of no more than 100<=000 lowercase English letters β any multiset satisfying the requirements, concatenated to be a string.
Note that the printed string doesn't need to be the final concatenated string. It only needs to represent an unordered multiset of letters. | [
"12\n",
"3\n"
] | [
"abababab\n",
"codeforces\n"
] | For the multiset {'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b'}, one of the ways to complete the process is as follows:
- {"ab", "a", "b", "a", "b", "a", "b"}, with a cost of 0; - {"aba", "b", "a", "b", "a", "b"}, with a cost of 1; - {"abab", "a", "b", "a", "b"}, with a cost of 1; - {"abab", "ab", "a", "b"}, with a cost... | [
{
"input": "12",
"output": "abababab"
},
{
"input": "3",
"output": "codeforces"
},
{
"input": "0",
"output": "o"
},
{
"input": "2",
"output": "aabb"
},
{
"input": "5",
"output": "aaabbcc"
},
{
"input": "10",
"output": "aaaaa"
},
{
"input": ... | 46 | 0 | 0 | 5,935 | |
677 | Vanya and Balloons | [
"binary search",
"brute force",
"dp",
"implementation"
] | null | null | Vanya plays a game of balloons on the field of size *n*<=Γ<=*n*, where each cell contains a balloon with one of the values 0, 1, 2 or 3. The goal is to destroy a cross, such that the product of all values of balloons in the cross is maximum possible. There are two types of crosses: normal and rotated. For example:
or
... | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=1000)Β β the number of rows and columns in the table with balloons.
The each of the following *n* lines contains *n* characters '0', '1', '2' or '3'Β β the description of the values in balloons. | Print the maximum possible product modulo 109<=+<=7. Note, that you are not asked to maximize the remainder modulo 109<=+<=7, but to find the maximum value and print it this modulo. | [
"4\n1233\n0213\n2020\n0303\n",
"5\n00300\n00300\n33333\n00300\n00300\n",
"5\n00003\n02030\n00300\n03020\n30000\n",
"5\n21312\n10003\n10002\n10003\n23231\n",
"5\n12131\n12111\n12112\n21311\n21212\n"
] | [
"108\n",
"19683\n",
"108\n",
"3\n",
"24\n"
] | In the first sample, the maximum product is achieved for a rotated cross with a center in the cell (3,β3) and radius 1: 2Β·2Β·3Β·3Β·3β=β108. | [] | 30 | 0 | 0 | 5,947 | |
596 | Wilbur and Swimming Pool | [
"geometry",
"implementation"
] | null | null | After making bad dives into swimming pools, Wilbur wants to build a swimming pool in the shape of a rectangle in his backyard. He has set up coordinate axes, and he wants the sides of the rectangle to be parallel to them. Of course, the area of the rectangle must be positive. Wilbur had all four vertices of the planned... | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=4)Β β the number of vertices that were not erased by Wilbur's friend.
Each of the following *n* lines contains two integers *x**i* and *y**i* (<=-<=1000<=β€<=*x**i*,<=*y**i*<=β€<=1000)Β βthe coordinates of the *i*-th vertex that remains. Vertices are... | Print the area of the initial rectangle if it could be uniquely determined by the points remaining. Otherwise, print <=-<=1. | [
"2\n0 0\n1 1\n",
"1\n1 1\n"
] | [
"1\n",
"-1\n"
] | In the first sample, two opposite corners of the initial rectangle are given, and that gives enough information to say that the rectangle is actually a unit square.
In the second sample there is only one vertex left and this is definitely not enough to uniquely define the area. | [
{
"input": "2\n0 0\n1 1",
"output": "1"
},
{
"input": "1\n1 1",
"output": "-1"
},
{
"input": "1\n-188 17",
"output": "-1"
},
{
"input": "1\n71 -740",
"output": "-1"
},
{
"input": "4\n-56 -858\n-56 -174\n778 -858\n778 -174",
"output": "570456"
},
{
"inp... | 62 | 0 | 3 | 5,976 | |
386 | Fly, freebies, fly! | [
"binary search",
"brute force",
"implementation"
] | null | null | Everyone loves a freebie. Especially students.
It is well-known that if in the night before exam a student opens window, opens the student's record-book and shouts loudly three times "Fly, freebie, fly!" β then flown freebie helps him to pass the upcoming exam.
In the night before the exam on mathematical analysis *n... | The first line of the input contains integer *n* (1<=β€<=*n*<=β€<=100), where *n* β the number of students shouted "Fly, freebie, fly!" The second line contains *n* positive integers *t**i* (1<=β€<=*t**i*<=β€<=1000).
The last line contains integer *T* (1<=β€<=*T*<=β€<=1000) β the time interval during which the freebie was n... | Print a single integer β the largest number of people who will pass exam tomorrow because of the freebie visit. | [
"6\n4 1 7 8 3 8\n1\n"
] | [
"3\n"
] | none | [
{
"input": "6\n4 1 7 8 3 8\n1",
"output": "3"
},
{
"input": "4\n4 2 1 5\n2",
"output": "2"
},
{
"input": "10\n4 7 1 3 8 5 2 1 8 4\n3",
"output": "6"
},
{
"input": "8\n39 49 37 28 40 17 50 2\n10",
"output": "3"
},
{
"input": "2\n1 1\n1",
"output": "2"
},
{
... | 31 | 0 | 0 | 5,988 | |
257 | Playing Cubes | [
"games",
"greedy",
"implementation"
] | null | null | Petya and Vasya decided to play a little. They found *n* red cubes and *m* blue cubes. The game goes like that: the players take turns to choose a cube of some color (red or blue) and put it in a line from left to right (overall the line will have *n*<=+<=*m* cubes). Petya moves first. Petya's task is to get as many pa... | The only line contains two space-separated integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=105) β the number of red and blue cubes, correspondingly. | On a single line print two space-separated integers β the number of Petya's and Vasya's points correspondingly provided that both players play optimally well. | [
"3 1\n",
"2 4\n"
] | [
"2 1\n",
"3 2\n"
] | In the first test sample the optimal strategy for Petya is to put the blue cube in the line. After that there will be only red cubes left, so by the end of the game the line of cubes from left to right will look as [blue, red, red, red]. So, Petya gets 2 points and Vasya gets 1 point.
If Petya would choose the red cu... | [
{
"input": "3 1",
"output": "2 1"
},
{
"input": "2 4",
"output": "3 2"
},
{
"input": "1 1",
"output": "0 1"
},
{
"input": "2 1",
"output": "1 1"
},
{
"input": "4 4",
"output": "3 4"
},
{
"input": "10 7",
"output": "9 7"
},
{
"input": "5 13"... | 156 | 0 | -1 | 5,994 | |
60 | Where Are My Flakes? | [
"implementation",
"two pointers"
] | A. Where Are My Flakes? | 2 | 256 | One morning the Cereal Guy found out that all his cereal flakes were gone. He found a note instead of them. It turned out that his smart roommate hid the flakes in one of *n* boxes. The boxes stand in one row, they are numbered from 1 to *n* from the left to the right. The roommate left hints like "Hidden to the left o... | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=1000,<=0<=β€<=*m*<=β€<=1000) which represent the number of boxes and the number of hints correspondingly. Next *m* lines contain hints like "To the left of *i*" and "To the right of *i*", where *i* is integer (1<=β€<=*i*<=β€<=*n*). The hints may coincide. | The answer should contain exactly one integer β the number of boxes that should necessarily be checked or "-1" if the hints are contradictory. | [
"2 1\nTo the left of 2\n",
"3 2\nTo the right of 1\nTo the right of 2\n",
"3 1\nTo the left of 3\n",
"3 2\nTo the left of 2\nTo the right of 1\n"
] | [
"1\n",
"1\n",
"2\n",
"-1\n"
] | none | [
{
"input": "2 1\nTo the left of 2",
"output": "1"
},
{
"input": "3 2\nTo the right of 1\nTo the right of 2",
"output": "1"
},
{
"input": "3 1\nTo the left of 3",
"output": "2"
},
{
"input": "3 2\nTo the left of 2\nTo the right of 1",
"output": "-1"
},
{
"input": "... | 374 | 1,433,600 | 3.90383 | 5,995 |
221 | Little Elephant and Numbers | [
"implementation"
] | null | null | The Little Elephant loves numbers.
He has a positive integer *x*. The Little Elephant wants to find the number of positive integers *d*, such that *d* is the divisor of *x*, and *x* and *d* have at least one common (the same) digit in their decimal representations.
Help the Little Elephant to find the described num... | A single line contains a single integer *x* (1<=β€<=*x*<=β€<=109). | In a single line print an integer β the answer to the problem. | [
"1\n",
"10\n"
] | [
"1\n",
"2\n"
] | none | [
{
"input": "1",
"output": "1"
},
{
"input": "10",
"output": "2"
},
{
"input": "47",
"output": "1"
},
{
"input": "100",
"output": "5"
},
{
"input": "128",
"output": "6"
},
{
"input": "2",
"output": "1"
},
{
"input": "17",
"output": "2"
... | 124 | 0 | 0 | 6,003 | |
177 | Rectangular Game | [
"number theory"
] | null | null | The Smart Beaver from ABBYY decided to have a day off. But doing nothing the whole day turned out to be too boring, and he decided to play a game with pebbles. Initially, the Beaver has *n* pebbles. He arranges them in *a* equal rows, each row has *b* pebbles (*a*<=><=1). Note that the Beaver must use all the pebble... | The single line of the input contains a single integer *n* β the initial number of pebbles the Smart Beaver has.
The input limitations for getting 30 points are:
- 2<=β€<=*n*<=β€<=50
The input limitations for getting 100 points are:
- 2<=β€<=*n*<=β€<=109 | Print a single number β the maximum possible result of the game. | [
"10\n",
"8\n"
] | [
"16\n",
"15\n"
] | Consider the first example (*c*<sub class="lower-index">1</sub>β=β10). The possible options for the game development are:
- Arrange the pebbles in 10 rows, one pebble per row. Then *c*<sub class="lower-index">2</sub>β=β1, and the game ends after the first move with the result of 11. - Arrange the pebbles in 5 rows, ... | [
{
"input": "10",
"output": "16"
},
{
"input": "8",
"output": "15"
},
{
"input": "4",
"output": "7"
},
{
"input": "36",
"output": "67"
},
{
"input": "32",
"output": "63"
},
{
"input": "46",
"output": "70"
},
{
"input": "6",
"output": "10... | 186 | 20,172,800 | 3 | 6,005 | |
79 | Colorful Field | [
"implementation",
"sortings"
] | B. Colorful Field | 2 | 256 | Fox Ciel saw a large field while she was on a bus. The field was a *n*<=Γ<=*m* rectangle divided into 1<=Γ<=1 cells. Some cells were wasteland, and other each cell contained crop plants: either carrots or kiwis or grapes.
After seeing the field carefully, Ciel found that the crop plants of each cell were planted in f... | In the first line there are four positive integers *n*,<=*m*,<=*k*,<=*t* (1<=β€<=*n*<=β€<=4Β·104,<=1<=β€<=*m*<=β€<=4Β·104,<=1<=β€<=*k*<=β€<=103,<=1<=β€<=*t*<=β€<=103), each of which represents the height of the field, the width of the field, the number of waste cells and the number of queries that ask the kind of crop plants in ... | For each query, if the cell is waste, print Waste. Otherwise, print the name of crop plants in the cell: either Carrots or Kiwis or Grapes. | [
"4 5 5 6\n4 3\n1 3\n3 3\n2 5\n3 2\n1 3\n1 4\n2 3\n2 4\n1 1\n1 1\n"
] | [
"Waste\nGrapes\nCarrots\nKiwis\nCarrots\nCarrots\n"
] | The sample corresponds to the figure in the statement. | [
{
"input": "4 5 5 6\n4 3\n1 3\n3 3\n2 5\n3 2\n1 3\n1 4\n2 3\n2 4\n1 1\n1 1",
"output": "Waste\nGrapes\nCarrots\nKiwis\nCarrots\nCarrots"
},
{
"input": "2 3 2 2\n1 1\n2 2\n2 1\n2 2",
"output": "Grapes\nWaste"
},
{
"input": "31 31 31 4\n4 9\n16 27\n11 29\n8 28\n11 2\n10 7\n22 6\n1 25\n14 8... | 186 | 0 | 0 | 6,007 |
0 | none | [
"none"
] | null | null | It is nighttime and Joe the Elusive got into the country's main bank's safe. The safe has *n* cells positioned in a row, each of them contains some amount of diamonds. Let's make the problem more comfortable to work with and mark the cells with positive numbers from 1 to *n* from the left to the right.
Unfortunately, ... | The first line contains integers *n*, *m* and *k* (1<=β€<=*n*<=β€<=104, 1<=β€<=*m*,<=*k*<=β€<=109). The next line contains *n* numbers. The *i*-th number is equal to the amount of diamonds in the *i*-th cell β it is an integer from 0 to 105. | Print a single number β the maximum number of diamonds Joe can steal. | [
"2 3 1\n2 3\n",
"3 2 2\n4 1 3\n"
] | [
"0",
"2"
] | In the second sample Joe can act like this:
The diamonds' initial positions are 4 1 3.
During the first period of time Joe moves a diamond from the 1-th cell to the 2-th one and a diamond from the 3-th cell to his pocket.
By the end of the first period the diamonds' positions are 3 2 2. The check finds no difference... | [
{
"input": "2 3 1\n2 3",
"output": "0"
},
{
"input": "3 2 2\n4 1 3",
"output": "2"
},
{
"input": "5 10 10\n7 0 7 0 7",
"output": "7"
},
{
"input": "6 10 4\n1 2 3 4 5 6",
"output": "0"
},
{
"input": "7 5 2\n1 2 3 4 5 6 7",
"output": "1"
},
{
"input": "1... | 122 | 0 | 0 | 6,008 | |
816 | Karen and Morning | [
"brute force",
"implementation"
] | null | null | Karen is getting ready for a new school day!
It is currently hh:mm, given in a 24-hour format. As you know, Karen loves palindromes, and she believes that it is good luck to wake up when the time is a palindrome.
What is the minimum number of minutes she should sleep, such that, when she wakes up, the time is a palin... | The first and only line of input contains a single string in the format hh:mm (00<=β€<= hh <=β€<=23, 00<=β€<= mm <=β€<=59). | Output a single integer on a line by itself, the minimum number of minutes she should sleep, such that, when she wakes up, the time is a palindrome. | [
"05:39\n",
"13:31\n",
"23:59\n"
] | [
"11\n",
"0\n",
"1\n"
] | In the first test case, the minimum number of minutes Karen should sleep for is 11. She can wake up at 05:50, when the time is a palindrome.
In the second test case, Karen can wake up immediately, as the current time, 13:31, is already a palindrome.
In the third test case, the minimum number of minutes Karen should s... | [
{
"input": "05:39",
"output": "11"
},
{
"input": "13:31",
"output": "0"
},
{
"input": "23:59",
"output": "1"
},
{
"input": "13:32",
"output": "69"
},
{
"input": "14:40",
"output": "1"
},
{
"input": "14:00",
"output": "41"
},
{
"input": "05:... | 61 | 6,963,200 | 0 | 6,024 | |
879 | Table Tennis | [
"data structures",
"implementation"
] | null | null | *n* people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins *k* games in a row. This player becomes the winner.
For each of t... | The first line contains two integers: *n* and *k* (2<=β€<=*n*<=β€<=500, 2<=β€<=*k*<=β€<=1012)Β β the number of people and the number of wins.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=*n*) β powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all ... | Output a single integer β power of the winner. | [
"2 2\n1 2\n",
"4 2\n3 1 2 4\n",
"6 2\n6 5 3 1 2 4\n",
"2 10000000000\n2 1\n"
] | [
"2 ",
"3 ",
"6 ",
"2\n"
] | Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner. | [
{
"input": "2 2\n1 2",
"output": "2 "
},
{
"input": "4 2\n3 1 2 4",
"output": "3 "
},
{
"input": "6 2\n6 5 3 1 2 4",
"output": "6 "
},
{
"input": "2 10000000000\n2 1",
"output": "2"
},
{
"input": "4 4\n1 3 4 2",
"output": "4 "
},
{
"input": "2 21474836... | 61 | 0 | 0 | 6,045 | |
761 | Dasha and Password | [
"brute force",
"dp",
"implementation"
] | null | null | After overcoming the stairs Dasha came to classes. She needed to write a password to begin her classes. The password is a string of length *n* which satisfies the following requirements:
- There is at least one digit in the string, - There is at least one lowercase (small) letter of the Latin alphabet in the string,... | The first line contains two integers *n*, *m* (3<=β€<=*n*<=β€<=50,<=1<=β€<=*m*<=β€<=50) β the length of the password and the length of strings which are assigned to password symbols.
Each of the next *n* lines contains the string which is assigned to the *i*-th symbol of the password string. Its length is *m*, it consist... | Print one integer β the minimum number of operations which is necessary to make the string, which is displayed on the screen, a valid password. | [
"3 4\n1**2\na3*0\nc4**\n",
"5 5\n#*&#*\n*a1c&\n&q2w*\n#a3c#\n*&#*&\n"
] | [
"1\n",
"3\n"
] | In the first test it is necessary to move the pointer of the third string to one left to get the optimal answer.
In the second test one of possible algorithms will be:
- to move the pointer of the second symbol once to the right. - to move the pointer of the third symbol twice to the right. | [
{
"input": "3 4\n1**2\na3*0\nc4**",
"output": "1"
},
{
"input": "5 5\n#*&#*\n*a1c&\n&q2w*\n#a3c#\n*&#*&",
"output": "3"
},
{
"input": "5 2\n&l\n*0\n*9\n*#\n#o",
"output": "2"
},
{
"input": "25 16\nvza**ooxkmd#*ywa\ndip#*#&ef&z&&&pv\nwggob&&72#*&&nku\nrsb##*&jm&#u**te\nzif#lu#... | 685 | 30,003,200 | 3 | 6,049 | |
57 | Square Earth? | [
"dfs and similar",
"greedy",
"implementation"
] | A. Square Earth? | 2 | 256 | Meg the Rabbit decided to do something nice, specifically β to determine the shortest distance between two points on the surface of our planet. But Meg... what can you say, she wants everything simple. So, she already regards our planet as a two-dimensional circle. No, wait, it's even worse β as a square of side *n*. T... | The single line contains 5 space-separated integers: *n*,<=*x*1,<=*y*1,<=*x*2,<=*y*2 (1<=β€<=*n*<=β€<=1000,<=0<=β€<=*x*1,<=*y*1,<=*x*2,<=*y*2<=β€<=*n*) which correspondingly represent a side of the square, the coordinates of the first point and the coordinates of the second point. It is guaranteed that the points lie on th... | You must print on a single line the shortest distance between the points. | [
"2 0 0 1 0\n",
"2 0 1 2 1\n",
"100 0 0 100 100\n"
] | [
"1\n",
"4\n",
"200\n"
] | none | [
{
"input": "2 0 0 1 0",
"output": "1"
},
{
"input": "2 0 1 2 1",
"output": "4"
},
{
"input": "100 0 0 100 100",
"output": "200"
},
{
"input": "4 0 3 1 4",
"output": "2"
},
{
"input": "10 8 10 10 0",
"output": "12"
},
{
"input": "26 21 0 26 14",
"ou... | 218 | 3,379,200 | -1 | 6,062 |
427 | Checkposts | [
"dfs and similar",
"graphs",
"two pointers"
] | null | null | Your city has *n* junctions. There are *m* one-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions.
To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction *i* can protect junction *j... | In the first line, you will be given an integer *n*, number of junctions (1<=β€<=*n*<=β€<=105). In the next line, *n* space-separated integers will be given. The *i**th* integer is the cost of building checkpost at the *i**th* junction (costs will be non-negative and will not exceed 109).
The next line will contain an i... | Print two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo 1000000007 (109<=+<=7). | [
"3\n1 2 3\n3\n1 2\n2 3\n3 2\n",
"5\n2 8 0 6 0\n6\n1 4\n1 3\n2 4\n3 4\n4 5\n5 1\n",
"10\n1 3 2 2 1 3 1 4 10 10\n12\n1 2\n2 3\n3 1\n3 4\n4 5\n5 6\n5 7\n6 4\n7 3\n8 9\n9 10\n10 9\n",
"2\n7 91\n2\n1 2\n2 1\n"
] | [
"3 1\n",
"8 2\n",
"15 6\n",
"7 1\n"
] | none | [
{
"input": "3\n1 2 3\n3\n1 2\n2 3\n3 2",
"output": "3 1"
},
{
"input": "5\n2 8 0 6 0\n6\n1 4\n1 3\n2 4\n3 4\n4 5\n5 1",
"output": "8 2"
},
{
"input": "10\n1 3 2 2 1 3 1 4 10 10\n12\n1 2\n2 3\n3 1\n3 4\n4 5\n5 6\n5 7\n6 4\n7 3\n8 9\n9 10\n10 9",
"output": "15 6"
},
{
"input": ... | 810 | 22,630,400 | -1 | 6,098 | |
261 | Maxim and Restaurant | [
"dp",
"math",
"probabilities"
] | null | null | Maxim has opened his own restaurant! The restaurant has got a huge table, the table's length is *p* meters.
Maxim has got a dinner party tonight, *n* guests will come to him. Let's index the guests of Maxim's restaurant from 1 to *n*. Maxim knows the sizes of all guests that are going to come to him. The *i*-th guest'... | The first line contains integer *n* (1<=β€<=*n*<=β€<=50) β the number of guests in the restaurant. The next line contains integers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=50) β the guests' sizes in meters. The third line contains integer *p* (1<=β€<=*p*<=β€<=50) β the table's length in meters.
The numbers in the lines ... | In a single line print a real number β the answer to the problem. The answer will be considered correct, if the absolute or relative error doesn't exceed 10<=-<=4. | [
"3\n1 2 3\n3\n"
] | [
"1.3333333333\n"
] | In the first sample the people will come in the following orders:
- (1,β2,β3) β there will be two people in the restaurant; - (1,β3,β2) β there will be one person in the restaurant; - (2,β1,β3) β there will be two people in the restaurant; - (2,β3,β1) β there will be one person in the restaurant; - (3,β1,β2) β t... | [
{
"input": "3\n1 2 3\n3",
"output": "1.3333333333"
},
{
"input": "9\n2 2 2 2 2 2 2 1 2\n9",
"output": "4.5555555556"
},
{
"input": "7\n2 1 1 2 1 1 2\n2",
"output": "1.2857142857"
},
{
"input": "8\n3 1 5 6 1 5 4 4\n7",
"output": "1.6250000000"
},
{
"input": "2\n1 3... | 248 | 716,800 | -1 | 6,117 | |
981 | Bookshelves | [
"bitmasks",
"dp",
"greedy"
] | null | null | Mr Keks is a typical white-collar in Byteland.
He has a bookshelf in his office with some books on it, each book has an integer positive price.
Mr Keks defines the value of a shelf as the sum of books prices on it.
Miraculously, Mr Keks was promoted and now he is moving into a new office.
He learned that in the ne... | The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 50$)Β β the number of books and the number of shelves in the new office.
The second line contains $n$ integers $a_1, a_2, \ldots a_n$, ($0 < a_i < 2^{50}$)Β β the prices of the books in the order they stand on the old shelf. | Print the maximum possible beauty of $k$ shelves in the new office. | [
"10 4\n9 14 28 1 7 13 15 29 2 31\n",
"7 3\n3 14 15 92 65 35 89\n"
] | [
"24\n",
"64\n"
] | In the first example you can split the books as follows:
$$(9 + 14 + 28 + 1 + 7) \& (13 + 15) \& (29 + 2) \& (31) = 24.$$
In the second example you can split the books as follows:
$$(3 + 14 + 15 + 92) \& (65) \& (35 + 89) = 64.$$ | [
{
"input": "10 4\n9 14 28 1 7 13 15 29 2 31",
"output": "24"
},
{
"input": "7 3\n3 14 15 92 65 35 89",
"output": "64"
},
{
"input": "40 5\n6 18 24 5 14 16 31 9 15 5 25 2 18 12 19 27 10 23 23 18 22 14 1 14 6 14 17 28 11 21 8 23 10 30 21 5 17 11 26 16",
"output": "80"
},
{
"inp... | 655 | 50,995,200 | 3 | 6,122 | |
40 | Find Color | [
"constructive algorithms",
"geometry",
"implementation",
"math"
] | A. Find Color | 2 | 256 | Not so long ago as a result of combat operations the main Berland place of interest β the magic clock β was damaged. The cannon's balls made several holes in the clock, that's why the residents are concerned about the repair. The magic clock can be represented as an infinite Cartesian plane, where the origin correspond... | The first and single line contains two integers *x* and *y* β the coordinates of the hole made in the clock by the ball. Each of the numbers *x* and *y* has an absolute value that does not exceed 1000. | Find the required color.
All the points between which and the origin of coordinates the distance is integral-value are painted black. | [
"-2 1\n",
"2 1\n",
"4 3\n"
] | [
"white\n",
"black\n",
"black\n"
] | none | [
{
"input": "-2 1",
"output": "white"
},
{
"input": "2 1",
"output": "black"
},
{
"input": "4 3",
"output": "black"
},
{
"input": "3 3",
"output": "black"
},
{
"input": "4 4",
"output": "white"
},
{
"input": "-4 4",
"output": "black"
},
{
"i... | 154 | 6,758,400 | 3.948911 | 6,126 |
126 | Pills | [
"brute force",
"flows"
] | null | null | Doctor prescribed medicine to his patient. The medicine is represented by pills. Each pill consists of a shell and healing powder. The shell consists of two halves; each half has one of four colors β blue, red, white or yellow.
The doctor wants to put 28 pills in a rectangular box 7<=Γ<=8 in size. Besides, each pill o... | First 7 lines contain the doctor's picture. Each line contains 8 characters, each character can be "B", "R", "W" and "Y" that stands for blue, red, white and yellow colors correspondingly.
Next four lines contain 10 numbers that stand for, correspondingly, the number of pills painted:
"BY" "BW" "BR" "BB"
"RY" "RW" "... | Print on the first line the maximal number cells for which the colors match.
Then print 13 lines each containing 15 characters β the pills' position in the optimal arrangement. The intersections of odd lines and odd columns should contain characters "B", "R", "W" and "Y". All other positions should contain characters ... | [
"WWWBBWWW\nWWWBBWWW\nYYWBBWWW\nYYWBBWRR\nYYWBBWRR\nYYWBBWRR\nYYWBBWRR\n0 0 0 8\n0 1 5\n1 10\n5\n",
"WWWWWWWW\nWBYWRBBY\nBRYRBWYY\nWWBRYWBB\nBWWRWBYW\nRBWRBWYY\nWWWWWWWW\n0 0 0 1\n0 0 1\n0 1\n25\n"
] | [
"53\nW.W.W.B.B.W.W.W\n|.|.|.|.|.|.|.|\nW.W.W.B.B.W.W.W\n...............\nY.Y.W.B.B.W.W-W\n|.|.|.|.|.|....\nY.Y.W.B.B.W.R.R\n............|.|\nY.Y.W.B.B.R.R.R\n|.|.|.|.|.|....\nY.Y.W.B.B.W.R.R\n............|.|\nY-Y.B-B.B-B.R.R\n",
"15\nW.Y.Y-Y.Y-Y.Y-Y\n|.|............\nW.Y.Y.Y.Y.B-B.Y\n....|.|.|.....|\nY-Y.Y.Y.Y.Y-... | none | [] | 46 | 0 | 0 | 6,142 | |
229 | Planets | [
"binary search",
"data structures",
"graphs",
"shortest paths"
] | null | null | Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet.
Overall the galaxy has *... | The first line contains two space-separated integers: *n* (2<=β€<=*n*<=β€<=105), the number of planets in the galaxy, and *m* (0<=β€<=*m*<=β€<=105) β the number of pairs of planets between which Jack can travel using stargates. Then *m* lines follow, containing three integers each: the *i*-th line contains numbers of plane... | Print a single number β the least amount of time Jack needs to get from planet 1 to planet *n*. If Jack can't get to planet *n* in any amount of time, print number -1. | [
"4 6\n1 2 2\n1 3 3\n1 4 8\n2 3 4\n2 4 5\n3 4 3\n0\n1 3\n2 3 4\n0\n",
"3 1\n1 2 3\n0\n1 3\n0\n"
] | [
"7\n",
"-1\n"
] | In the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves t... | [
{
"input": "4 6\n1 2 2\n1 3 3\n1 4 8\n2 3 4\n2 4 5\n3 4 3\n0\n1 3\n2 3 4\n0",
"output": "7"
},
{
"input": "3 1\n1 2 3\n0\n1 3\n0",
"output": "-1"
},
{
"input": "2 1\n1 2 3\n0\n1 3",
"output": "3"
},
{
"input": "2 1\n1 2 3\n1 0\n0",
"output": "4"
},
{
"input": "3 3... | 92 | 0 | 0 | 6,151 | |
722 | Generating Sets | [
"binary search",
"data structures",
"dfs and similar",
"greedy",
"strings",
"trees"
] | null | null | You are given a set *Y* of *n* distinct positive integers *y*1,<=*y*2,<=...,<=*y**n*.
Set *X* of *n* distinct positive integers *x*1,<=*x*2,<=...,<=*x**n* is said to generate set *Y* if one can transform *X* to *Y* by applying some number of the following two operation to integers in *X*:
1. Take any integer *x**i* ... | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=50<=000)Β β the number of elements in *Y*.
The second line contains *n* integers *y*1,<=...,<=*y**n* (1<=β€<=*y**i*<=β€<=109), that are guaranteed to be distinct. | Print *n* integersΒ β set of distinct integers that generate *Y* and the maximum element of which is minimum possible. If there are several such sets, print any of them. | [
"5\n1 2 3 4 5\n",
"6\n15 14 3 13 1 12\n",
"6\n9 7 13 17 5 11\n"
] | [
"4 5 2 3 1 \n",
"12 13 14 7 3 1 \n",
"4 5 2 6 3 1 \n"
] | none | [
{
"input": "5\n1 2 3 4 5",
"output": "4 5 2 3 1 "
},
{
"input": "6\n15 14 3 13 1 12",
"output": "12 13 14 7 3 1 "
},
{
"input": "6\n9 7 13 17 5 11",
"output": "4 5 2 6 3 1 "
},
{
"input": "10\n18 14 19 17 11 7 20 10 4 12",
"output": "8 9 4 10 5 2 6 7 3 1 "
},
{
"i... | 46 | 0 | 0 | 6,178 | |
770 | Online Courses In BSU | [
"*special",
"dfs and similar",
"graphs",
"implementation"
] | null | null | Now you can take online courses in the Berland State University! Polycarp needs to pass *k* main online courses of his specialty to get a diploma. In total *n* courses are availiable for the passage.
The situation is complicated by the dependence of online courses, for each course there is a list of those that must be... | The first line contains *n* and *k* (1<=β€<=*k*<=β€<=*n*<=β€<=105) β the number of online-courses and the number of main courses of Polycarp's specialty.
The second line contains *k* distinct integers from 1 to *n* β numbers of main online-courses of Polycarp's specialty.
Then *n* lines follow, each of them describes ... | Print -1, if there is no the way to get a specialty.
Otherwise, in the first line print the integer *m* β the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print *m* distinct integers β numbers of courses which it is necessary to pass in the chronological order ... | [
"6 2\n5 3\n0\n0\n0\n2 2 1\n1 4\n1 5\n",
"9 3\n3 9 5\n0\n0\n3 9 4 5\n0\n0\n1 8\n1 6\n1 2\n2 1 2\n",
"3 3\n1 2 3\n1 2\n1 3\n1 1\n"
] | [
"5\n1 2 3 4 5 \n",
"6\n1 2 9 4 5 3 \n",
"-1\n"
] | In the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course. | [
{
"input": "6 2\n5 3\n0\n0\n0\n2 2 1\n1 4\n1 5",
"output": "5\n1 2 3 4 5 "
},
{
"input": "9 3\n3 9 5\n0\n0\n3 9 4 5\n0\n0\n1 8\n1 6\n1 2\n2 1 2",
"output": "6\n1 2 9 4 5 3 "
},
{
"input": "3 3\n1 2 3\n1 2\n1 3\n1 1",
"output": "-1"
},
{
"input": "5 3\n2 1 4\n0\n0\n1 5\n0\n0",... | 46 | 307,200 | 0 | 6,185 | |
744 | Hongcow Masters the Cyclic Shift | [
"strings",
"two pointers"
] | null | null | Hongcow's teacher heard that Hongcow had learned about the cyclic shift, and decided to set the following problem for him.
You are given a list of *n* strings *s*1,<=*s*2,<=...,<=*s**n* contained in the list *A*.
A list *X* of strings is called stable if the following condition holds.
First, a message is defined as ... | The first line of input will contain a single integer *n* (1<=β€<=*n*<=β€<=30), denoting the number of strings in the list.
The next *n* lines will each contain a string *s**i* (). | Print a single integer, the number of nonempty contiguous sublists that are stable. | [
"4\na\nab\nb\nbba\n",
"5\nhh\nee\nll\nll\noo\n",
"6\naab\nab\nbba\nb\nab\nc\n"
] | [
"7\n",
"0\n",
"13\n"
] | For the first sample, there are 10 sublists to consider. Sublists ["a", "ab", "b"], ["ab", "b", "bba"], and ["a", "ab", "b", "bba"] are not stable. The other seven sublists are stable.
For example, *X* = ["a", "ab", "b"] is not stable, since the message "ab" + "ab" = "abab" has four cyclic shifts ["abab", "baba", "aba... | [] | 46 | 4,608,000 | -1 | 6,221 | |
338 | GCD Table | [
"chinese remainder theorem",
"math",
"number theory"
] | null | null | Consider a table *G* of size *n*<=Γ<=*m* such that *G*(*i*,<=*j*)<==<=*GCD*(*i*,<=*j*) for all 1<=β€<=*i*<=β€<=*n*,<=1<=β€<=*j*<=β€<=*m*. *GCD*(*a*,<=*b*) is the greatest common divisor of numbers *a* and *b*.
You have a sequence of positive integer numbers *a*1,<=*a*2,<=...,<=*a**k*. We say that this sequence occurs in t... | The first line contains three space-separated integers *n*, *m* and *k* (1<=β€<=*n*,<=*m*<=β€<=1012; 1<=β€<=*k*<=β€<=10000). The second line contains *k* space-separated integers *a*1,<=*a*2,<=...,<=*a**k* (1<=β€<=*a**i*<=β€<=1012). | Print a single word "YES", if the given sequence occurs in table *G*, otherwise print "NO". | [
"100 100 5\n5 2 1 2 1\n",
"100 8 5\n5 2 1 2 1\n",
"100 100 7\n1 2 3 4 5 6 7\n"
] | [
"YES\n",
"NO\n",
"NO\n"
] | Sample 1. The tenth row of table *G* starts from sequence {1, 2, 1, 2, 5, 2, 1, 2, 1, 10}. As you can see, elements from fifth to ninth coincide with sequence *a*.
Sample 2. This time the width of table *G* equals 8. Sequence *a* doesn't occur there. | [
{
"input": "100 100 5\n5 2 1 2 1",
"output": "YES"
},
{
"input": "100 8 5\n5 2 1 2 1",
"output": "NO"
},
{
"input": "100 100 7\n1 2 3 4 5 6 7",
"output": "NO"
},
{
"input": "5 5 5\n1 1 1 1 1",
"output": "YES"
},
{
"input": "11 10 1\n11",
"output": "NO"
},
... | 186 | 921,600 | 3 | 6,250 | |
476 | Dreamoon and Sets | [
"constructive algorithms",
"greedy",
"math"
] | null | null | Dreamoon likes to play with sets, integers and . is defined as the largest positive integer that divides both *a* and *b*.
Let *S* be a set of exactly four distinct integers greater than 0. Define *S* to be of rank *k* if and only if for all pairs of distinct elements *s**i*, *s**j* from *S*, .
Given *k* and *n*, Dr... | The single line of the input contains two space separated integers *n*, *k* (1<=β€<=*n*<=β€<=10<=000,<=1<=β€<=*k*<=β€<=100). | On the first line print a single integer β the minimal possible *m*.
On each of the next *n* lines print four space separated integers representing the *i*-th set.
Neither the order of the sets nor the order of integers within a set is important. If there are multiple possible solutions with minimal *m*, print any o... | [
"1 1\n",
"2 2\n"
] | [
"5\n1 2 3 5\n",
"22\n2 4 6 22\n14 18 10 16\n"
] | For the first example it's easy to see that set {1,β2,β3,β4} isn't a valid set of rank 1 since <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e2af04e5e60e1fe79a4d74bf22dfa575f0b0f7bb.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [
{
"input": "1 1",
"output": "5\n1 3 4 5"
},
{
"input": "2 2",
"output": "22\n2 6 8 10\n14 18 20 22"
},
{
"input": "7 7",
"output": "287\n7 21 28 35\n49 63 70 77\n91 105 112 119\n133 147 154 161\n175 189 196 203\n217 231 238 245\n259 273 280 287"
},
{
"input": "13 7",
"out... | 31 | 0 | -1 | 6,270 | |
859 | Declined Finalists | [
"greedy",
"implementation"
] | null | null | This year, as in previous years, MemSQL is inviting the top 25 competitors from the Start[c]up qualification round to compete onsite for the final round. Not everyone who is eligible to compete onsite can afford to travel to the office, though. Initially the top 25 contestants are invited to come onsite. Each eligible ... | The first line of input contains *K* (1<=β€<=*K*<=β€<=25), the number of onsite finalists you know. The second line of input contains *r*1,<=*r*2,<=...,<=*r**K* (1<=β€<=*r**i*<=β€<=106), the qualifying ranks of the finalists you know. All these ranks are distinct. | Print the minimum possible number of contestants that declined the invitation to compete onsite. | [
"25\n2 3 4 5 6 7 8 9 10 11 12 14 15 16 17 18 19 20 21 22 23 24 25 26 28\n",
"5\n16 23 8 15 4\n",
"3\n14 15 92\n"
] | [
"3\n",
"0\n",
"67\n"
] | In the first example, you know all 25 onsite finalists. The contestants who ranked 1-st, 13-th, and 27-th must have declined, so the answer is 3. | [
{
"input": "25\n2 3 4 5 6 7 8 9 10 11 12 14 15 16 17 18 19 20 21 22 23 24 25 26 28",
"output": "3"
},
{
"input": "5\n16 23 8 15 4",
"output": "0"
},
{
"input": "3\n14 15 92",
"output": "67"
},
{
"input": "1\n1000000",
"output": "999975"
},
{
"input": "25\n1000000 ... | 46 | 0 | 3 | 6,278 | |
3 | Least Cost Bracket Sequence | [
"greedy"
] | D. Least Cost Bracket Sequence | 1 | 64 | This is yet another problem on regular bracket sequences.
A bracket sequence is called regular, if by inserting "+" and "1" into it we get a correct mathematical expression. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. You have a pattern of a bracket sequenc... | The first line contains a non-empty pattern of even length, consisting of characters "(", ")" and "?". Its length doesn't exceed 5Β·104. Then there follow *m* lines, where *m* is the number of characters "?" in the pattern. Each line contains two integer numbers *a**i* and *b**i* (1<=β€<=*a**i*,<=<=*b**i*<=β€<=106), where... | Print the cost of the optimal regular bracket sequence in the first line, and the required sequence in the second.
Print -1, if there is no answer. If the answer is not unique, print any of them. | [
"(??)\n1 2\n2 8\n"
] | [
"4\n()()\n"
] | none | [
{
"input": "(??)\n1 2\n2 8",
"output": "4\n()()"
},
{
"input": "??\n1 1\n1 1",
"output": "2\n()"
},
{
"input": "(???\n1 1\n1 1\n1 1",
"output": "3\n(())"
},
{
"input": "(??)\n2 1\n1 1",
"output": "2\n()()"
},
{
"input": "(???)?\n3 3\n3 1\n3 3\n2 3",
"output": ... | 404 | 2,048,000 | 3.782741 | 6,284 |
9 | Hexadecimal's Numbers | [
"brute force",
"implementation",
"math"
] | C. Hexadecimal's Numbers | 1 | 64 | One beautiful July morning a terrible thing happened in Mainframe: a mean virus Megabyte somehow got access to the memory of his not less mean sister Hexadecimal. He loaded there a huge amount of *n* different natural numbers from 1 to *n* to obtain total control over her energy.
But his plan failed. The reason for th... | Input data contains the only number *n* (1<=β€<=*n*<=β€<=109). | Output the only number β answer to the problem. | [
"10\n"
] | [
"2"
] | For *n* = 10 the answer includes numbers 1 and 10. | [
{
"input": "10",
"output": "2"
},
{
"input": "20",
"output": "3"
},
{
"input": "72",
"output": "3"
},
{
"input": "99",
"output": "3"
},
{
"input": "100",
"output": "4"
},
{
"input": "101",
"output": "5"
},
{
"input": "102",
"output": "5... | 1,090 | 67,072,000 | 0 | 6,304 |
0 | none | [
"none"
] | null | null | There are two small spaceship, surrounded by two groups of enemy larger spaceships. The space is a two-dimensional plane, and one group of the enemy spaceships is positioned in such a way that they all have integer $y$-coordinates, and their $x$-coordinate is equal to $-100$, while the second group is positioned in suc... | The first line contains two integers $n$ and $m$ ($1 \le n, m \le 60$), the number of enemy spaceships with $x = -100$ and the number of enemy spaceships with $x = 100$, respectively.
The second line contains $n$ integers $y_{1,1}, y_{1,2}, \ldots, y_{1,n}$ ($|y_{1,i}| \le 10\,000$) β the $y$-coordinates of the spaces... | Print a single integer β the largest number of enemy spaceships that can be destroyed. | [
"3 9\n1 2 3\n1 2 3 7 8 9 11 12 13\n",
"5 5\n1 2 3 4 5\n1 2 3 4 5\n"
] | [
"9\n",
"10\n"
] | In the first example the first spaceship can be positioned at $(0, 2)$, and the second β at $(0, 7)$. This way all the enemy spaceships in the first group and $6$ out of $9$ spaceships in the second group will be destroyed.
In the second example the first spaceship can be positioned at $(0, 3)$, and the second can be ... | [
{
"input": "3 9\n1 2 3\n1 2 3 7 8 9 11 12 13",
"output": "9"
},
{
"input": "5 5\n1 2 3 4 5\n1 2 3 4 5",
"output": "10"
},
{
"input": "50 50\n744 333 562 657 680 467 357 376 759 311 371 327 369 172 286 577 446 922 16 69 350 92 627 852 878 733 148 857 663 969 131 250 563 665 67 169 178 625... | 30 | 0 | 0 | 6,314 | |
802 | Heidi and Library (easy) | [
"greedy"
] | null | null | Your search for Heidi is over β you finally found her at a library, dressed up as a human. In fact, she has spent so much time there that she now runs the place! Her job is to buy books and keep them at the library so that people can borrow and read them. There are *n* different books, numbered 1 through *n*.
We will ... | The first line of input will contain two integers *n* and *k* (1<=β€<=*n*,<=*k*<=β€<=80). The second line will contain *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=*n*) β the sequence of book requests. | On a single line print the minimum cost of buying books at the store so as to satisfy all requests. | [
"4 80\n1 2 2 1\n",
"4 1\n1 2 2 1\n",
"4 2\n1 2 3 1\n"
] | [
"2\n",
"3\n",
"3\n"
] | In the first test case, Heidi is able to keep all books forever. Therefore, she only needs to buy the book 1 before the first day and the book 2 before the second day.
In the second test case, she can only keep one book at a time. Therefore she will need to buy new books on the first, second and fourth day.
In the th... | [
{
"input": "4 80\n1 2 2 1",
"output": "2"
},
{
"input": "4 1\n1 2 2 1",
"output": "3"
},
{
"input": "4 2\n1 2 3 1",
"output": "3"
},
{
"input": "11 1\n1 2 3 5 1 10 10 1 1 3 5",
"output": "9"
},
{
"input": "5 2\n1 2 3 1 2",
"output": "4"
},
{
"input": "... | 62 | 0 | 0 | 6,322 | |
66 | Petya and Post | [
"data structures",
"dp"
] | E. Petya and Post | 2 | 256 | Little Vasya's uncle is a postman. The post offices are located on one circular road. Besides, each post office has its own gas station located next to it. Petya's uncle works as follows: in the morning he should leave the house and go to some post office. In the office he receives a portion of letters and a car. Then ... | The first line contains integer *n* (1<=β€<=*n*<=β€<=105). The second line contains *n* integers *a**i* β amount of gasoline on the *i*-th station. The third line contains *n* integers *b*1,<=*b*2,<=...,<=*b**n*. They are the distances between the 1-st and the 2-nd gas stations, between the 2-nd and the 3-rd ones, ..., b... | Print on the first line the number *k* β the number of possible post offices, from which the car can drive one circle along a circular road. Print on the second line *k* numbers in the ascending order β the numbers of offices, from which the car can start. | [
"4\n1 7 2 3\n8 1 1 3\n",
"8\n1 2 1 2 1 2 1 2\n2 1 2 1 2 1 2 1\n"
] | [
"2\n2 4\n",
"8\n1 2 3 4 5 6 7 8\n"
] | none | [
{
"input": "4\n1 7 2 3\n8 1 1 3",
"output": "2\n2 4"
},
{
"input": "8\n1 2 1 2 1 2 1 2\n2 1 2 1 2 1 2 1",
"output": "8\n1 2 3 4 5 6 7 8"
},
{
"input": "20\n31 16 20 30 19 35 8 11 20 45 10 26 21 39 29 52 8 10 37 49\n16 33 41 32 43 24 35 48 19 37 28 26 7 10 23 48 18 2 1 25",
"output": ... | 92 | 0 | 0 | 6,330 |
327 | Hungry Sequence | [
"math"
] | null | null | Iahub and Iahubina went to a date at a luxury restaurant. Everything went fine until paying for the food. Instead of money, the waiter wants Iahub to write a Hungry sequence consisting of *n* integers.
A sequence *a*1, *a*2, ..., *a**n*, consisting of *n* integers, is Hungry if and only if:
- Its elements are in i... | The input contains a single integer: *n* (1<=β€<=*n*<=β€<=105). | Output a line that contains *n* space-separated integers *a*1 *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=107), representing a possible Hungry sequence. Note, that each *a**i* must not be greater than 10000000 (107) and less than 1.
If there are multiple solutions you can output any one. | [
"3\n",
"5\n"
] | [
"2 9 15\n",
"11 14 20 27 31\n"
] | none | [
{
"input": "3",
"output": "2 9 15"
},
{
"input": "5",
"output": "11 14 20 27 31"
},
{
"input": "1",
"output": "3"
},
{
"input": "1000",
"output": "3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 ... | 92 | 3,379,200 | -1 | 6,333 | |
932 | Permutation Cycle | [
"brute force",
"constructive algorithms"
] | null | null | For a permutation *P*[1... *N*] of integers from 1 to *N*, function *f* is defined as follows:
Let *g*(*i*) be the minimum positive integer *j* such that *f*(*i*,<=*j*)<==<=*i*. We can show such *j* always exists.
For given *N*,<=*A*,<=*B*, find a permutation *P* of integers from 1 to *N* such that for 1<=β€<=*i*<=β€<=... | The only line contains three integers *N*,<=*A*,<=*B* (1<=β€<=*N*<=β€<=106,<=1<=β€<=*A*,<=*B*<=β€<=*N*). | If no such permutation exists, output -1. Otherwise, output a permutation of integers from 1 to *N*. | [
"9 2 5\n",
"3 2 1\n"
] | [
"6 5 8 3 4 1 9 2 7",
"1 2 3 "
] | In the first example, *g*(1)β=β*g*(6)β=β*g*(7)β=β*g*(9)β=β2 and *g*(2)β=β*g*(3)β=β*g*(4)β=β*g*(5)β=β*g*(8)β=β5
In the second example, *g*(1)β=β*g*(2)β=β*g*(3)β=β1 | [
{
"input": "9 2 5",
"output": "2 1 4 3 6 7 8 9 5 "
},
{
"input": "3 2 1",
"output": "1 2 3 "
},
{
"input": "7 4 4",
"output": "-1"
},
{
"input": "1000000 999998 3",
"output": "-1"
},
{
"input": "1 1 1",
"output": "1 "
},
{
"input": "993012 997 1001",
... | 2,000 | 30,105,600 | 0 | 6,340 | |
0 | none | [
"none"
] | null | null | Vasya should paint a fence in front of his own cottage. The fence is a sequence of *n* wooden boards arranged in a single row. Each board is a 1 centimeter wide rectangle. Let's number the board fence using numbers 1,<=2,<=...,<=*n* from left to right. The height of the *i*-th board is *h**i* centimeters.
Vasya has a ... | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=200) β the number of boards in Vasya's fence.
The second line contains two integers *a* and *b* (0<=β€<=*a*,<=*b*<=β€<=4Β·104) β the area that can be painted red and the area that can be painted green, correspondingly.
The third line contains a sequence of *n* ... | Print a single number β the minimum unattractiveness value Vasya can get if he paints his fence completely. If it is impossible to do, print <=-<=1. | [
"4\n5 7\n3 3 4 1\n",
"3\n2 3\n1 3 1\n",
"3\n3 3\n2 2 2\n"
] | [
"3\n",
"2\n",
"-1\n"
] | none | [] | 92 | 0 | 0 | 6,343 | |
213 | Numbers | [
"combinatorics",
"dp"
] | null | null | Furik loves writing all sorts of problems, especially such that he can't solve himself. You've got one of his problems, the one Furik gave to Rubik. And Rubik asks you to solve it.
There is integer *n* and array *a*, consisting of ten integers, indexed by numbers from 0 to 9. Your task is to count the number of positi... | The first line contains integer *n* (1<=β€<=*n*<=β€<=100). The next line contains 10 integers *a*[0], *a*[1], ..., *a*[9] (0<=β€<=*a*[*i*]<=β€<=100) β elements of array *a*. The numbers are separated by spaces. | On a single line print the remainder of dividing the answer to the problem by 1000000007 (109<=+<=7). | [
"1\n0 0 0 0 0 0 0 0 0 1\n",
"2\n1 1 0 0 0 0 0 0 0 0\n",
"3\n1 1 0 0 0 0 0 0 0 0\n"
] | [
"1\n",
"1\n",
"36\n"
] | In the first sample number 9 meets the requirements.
In the second sample number 10 meets the requirements.
In the third sample numbers 10, 110, 210, 120, 103 meet the requirements. There are other suitable numbers, 36 in total. | [
{
"input": "1\n0 0 0 0 0 0 0 0 0 1",
"output": "1"
},
{
"input": "2\n1 1 0 0 0 0 0 0 0 0",
"output": "1"
},
{
"input": "3\n1 1 0 0 0 0 0 0 0 0",
"output": "36"
},
{
"input": "4\n0 1 0 1 2 0 0 0 0 0",
"output": "12"
},
{
"input": "5\n2 1 2 0 0 0 0 0 0 0",
"outp... | 92 | 0 | 0 | 6,350 | |
75 | Facetook Priority Wall | [
"expression parsing",
"implementation",
"strings"
] | B. Facetook Priority Wall | 2 | 256 | Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described).
This priority factor will be affected by three types of actions:
- 1. "*X* posted on *Y*'s wall... | The first line contains your name. The second line contains an integer *n*, which is the number of actions (1<=β€<=*n*<=β€<=100). Then *n* lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra s... | Print *m* lines, where *m* is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority fa... | [
"ahmed\n3\nahmed posted on fatma's wall\nfatma commented on ahmed's post\nmona likes ahmed's post\n",
"aba\n1\nlikes likes posted's post\n"
] | [
"fatma\nmona\n",
"likes\nposted\n"
] | none | [
{
"input": "ahmed\n3\nahmed posted on fatma's wall\nfatma commented on ahmed's post\nmona likes ahmed's post",
"output": "fatma\nmona"
},
{
"input": "aba\n1\nlikes likes posted's post",
"output": "likes\nposted"
},
{
"input": "nu\n5\ng commented on pwyndmh's post\nqv posted on g's wall\n... | 62 | 102,400 | 0 | 6,353 |
182 | Common Divisors | [
"brute force",
"hashing",
"implementation",
"math",
"strings"
] | null | null | Vasya has recently learned at school what a number's divisor is and decided to determine a string's divisor. Here is what he came up with.
String *a* is the divisor of string *b* if and only if there exists a positive integer *x* such that if we write out string *a* consecutively *x* times, we get string *b*. For exam... | The first input line contains a non-empty string *s*1.
The second input line contains a non-empty string *s*2.
Lengths of strings *s*1 and *s*2 are positive and do not exceed 105. The strings only consist of lowercase Latin letters. | Print the number of common divisors of strings *s*1 and *s*2. | [
"abcdabcd\nabcdabcdabcdabcd\n",
"aaa\naa\n"
] | [
"2\n",
"1\n"
] | In first sample the common divisors are strings "abcd" and "abcdabcd".
In the second sample the common divisor is a single string "a". String "aa" isn't included in the answer as it isn't a divisor of string "aaa". | [
{
"input": "abcdabcd\nabcdabcdabcdabcd",
"output": "2"
},
{
"input": "aaa\naa",
"output": "1"
},
{
"input": "aaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaa",
"output": "3"
},
{
"input": "aaaaaaaaaaaaaa\naaaaaaaaaaaaaa",
"output": "4"
},
{
"input": "a\nb",
... | 62 | 204,800 | 0 | 6,360 | |
514 | R2D2 and Droid Army | [
"binary search",
"data structures",
"two pointers"
] | null | null | An army of *n* droids is lined up in one row. Each droid is described by *m* integers *a*1,<=*a*2,<=...,<=*a**m*, where *a**i* is the number of details of the *i*-th type in this droid's mechanism. R2-D2 wants to destroy the sequence of consecutive droids of maximum length. He has *m* weapons, the *i*-th weapon can aff... | The first line contains three integers *n*,<=*m*,<=*k* (1<=β€<=*n*<=β€<=105, 1<=β€<=*m*<=β€<=5, 0<=β€<=*k*<=β€<=109) β the number of droids, the number of detail types and the number of available shots, respectively.
Next *n* lines follow describing the droids. Each line contains *m* integers *a*1,<=*a*2,<=...,<=*a**m* (0<=... | Print *m* space-separated integers, where the *i*-th number is the number of shots from the weapon of the *i*-th type that the robot should make to destroy the subsequence of consecutive droids of the maximum length.
If there are multiple optimal solutions, print any of them.
It is not necessary to make exactly *k* ... | [
"5 2 4\n4 0\n1 2\n2 1\n0 2\n1 3\n",
"3 2 4\n1 2\n1 3\n2 2\n"
] | [
"2 2\n",
"1 3\n"
] | In the first test the second, third and fourth droids will be destroyed.
In the second test the first and second droids will be destroyed. | [
{
"input": "5 2 4\n4 0\n1 2\n2 1\n0 2\n1 3",
"output": "2 2"
},
{
"input": "3 2 4\n1 2\n1 3\n2 2",
"output": "1 3"
},
{
"input": "1 1 0\n0",
"output": "0"
},
{
"input": "1 1 0\n1",
"output": "0"
},
{
"input": "1 1 1\n0",
"output": "0"
},
{
"input": "4 ... | 46 | 0 | 0 | 6,386 | |
353 | Queue | [
"constructive algorithms",
"dp"
] | null | null | There are *n* schoolchildren, boys and girls, lined up in the school canteen in front of the bun stall. The buns aren't ready yet and the line is undergoing some changes.
Each second all boys that stand right in front of girls, simultaneously swap places with the girls (so that the girls could go closer to the beginni... | The first line contains a sequence of letters without spaces *s*1*s*2... *s**n* (1<=β€<=*n*<=β€<=106), consisting of capital English letters M and F. If letter *s**i* equals M, that means that initially, the line had a boy on the *i*-th position. If letter *s**i* equals F, then initially the line had a girl on the *i*-th... | Print a single integer β the number of seconds needed to move all the girls in the line in front of the boys. If the line has only boys or only girls, print 0. | [
"MFM\n",
"MMFF\n",
"FFMMM\n"
] | [
"1\n",
"3\n",
"0\n"
] | In the first test case the sequence of changes looks as follows: MFM βββ FMM.
The second test sample corresponds to the sample from the statement. The sequence of changes is: MMFF βββ MFMF βββ FMFM βββ FFMM. | [
{
"input": "MFM",
"output": "1"
},
{
"input": "MMFF",
"output": "3"
},
{
"input": "FFMMM",
"output": "0"
},
{
"input": "MMFMMFFFFM",
"output": "7"
},
{
"input": "MFFFMMFMFMFMFFFMMMFFMMMMMMFMMFFMMMFMMFMFFFMMFMMMFFMMFFFFFMFMFFFMMMFFFMFMFMFMFFFMMMMFMMFMMFFMMMMMMFFM",... | 0 | 0 | -1 | 6,398 | |
986 | Perfect Encoding | [
"fft",
"math"
] | null | null | You are working as an analyst in a company working on a new system for big data storage. This system will store $n$ different objects. Each object should have a unique ID.
To create the system, you choose the parameters of the systemΒ β integers $m \ge 1$ and $b_{1}, b_{2}, \ldots, b_{m}$. With these parameters an ID o... | In the only line of input there is one positive integer $n$. The length of the decimal representation of $n$ is no greater than $1.5 \cdot 10^{6}$. The integer does not contain leading zeros. | Print one numberΒ β minimal value of $\sum_{i=1}^{m} b_{i}$. | [
"36\n",
"37\n",
"12345678901234567890123456789\n"
] | [
"10\n",
"11\n",
"177\n"
] | none | [
{
"input": "36",
"output": "10"
},
{
"input": "37",
"output": "11"
},
{
"input": "12345678901234567890123456789",
"output": "177"
},
{
"input": "1",
"output": "1"
},
{
"input": "2",
"output": "2"
},
{
"input": "3",
"output": "3"
},
{
"input... | 249 | 5,939,200 | 3 | 6,400 | |
343 | Rational Resistance | [
"math",
"number theory"
] | null | null | Mad scientist Mike is building a time machine in his spare time. To finish the work, he needs a resistor with a certain resistance value.
However, all Mike has is lots of identical resistors with unit resistance *R*0<==<=1. Elements with other resistance can be constructed from these resistors. In this problem, we wil... | The single input line contains two space-separated integers *a* and *b* (1<=β€<=*a*,<=*b*<=β€<=1018). It is guaranteed that the fraction is irreducible. It is guaranteed that a solution always exists. | Print a single number β the answer to the problem.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is recommended to use the cin, cout streams or the %I64d specifier. | [
"1 1\n",
"3 2\n",
"199 200\n"
] | [
"1\n",
"3\n",
"200\n"
] | In the first sample, one resistor is enough.
In the second sample one can connect the resistors in parallel, take the resulting element and connect it to a third resistor consecutively. Then, we get an element with resistance <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5305da389756aab6... | [
{
"input": "1 1",
"output": "1"
},
{
"input": "3 2",
"output": "3"
},
{
"input": "199 200",
"output": "200"
},
{
"input": "1 1000000000000000000",
"output": "1000000000000000000"
},
{
"input": "3 1",
"output": "3"
},
{
"input": "21 8",
"output": "7... | 122 | 0 | 0 | 6,403 | |
45 | Codecraft III | [
"implementation"
] | A. Codecraft III | 2 | 256 | Today Vasya visited a widely known site and learned that the continuation of his favourite game Codecraft II will appear after exactly *k* months. He looked at the calendar and learned that at the moment is the month number *s*. Vasya immediately got interested in what month Codecraft III will appear. Help him understa... | The first input line contains the name of the current month. It is guaranteed that it is a proper English name of one of twelve months. The first letter is uppercase, the rest are lowercase. The second line contains integer *k* (0<=β€<=*k*<=β€<=100) β the number of months left till the appearance of Codecraft III. | Print starting from an uppercase letter the name of the month in which the continuation of Codeforces II will appear. The printed name must be contained in the list January, February, March, April, May, June, July, August, September, October, November, December. | [
"November\n3\n",
"May\n24\n"
] | [
"February\n",
"May\n"
] | none | [
{
"input": "November\n3",
"output": "February"
},
{
"input": "May\n24",
"output": "May"
},
{
"input": "April\n0",
"output": "April"
},
{
"input": "September\n0",
"output": "September"
},
{
"input": "August\n0",
"output": "August"
},
{
"input": "June\n1... | 186 | 0 | 0 | 6,424 |
57 | Martian Architecture | [
"implementation"
] | B. Martian Architecture | 2 | 256 | Chris the Rabbit found the traces of an ancient Martian civilization. The brave astronomer managed to see through a small telescope an architecture masterpiece β "A Road to the Sun". The building stands on cubical stones of the same size. The foundation divides the entire "road" into cells, into which the cubical stone... | The first line contains three space-separated integers: *n*,<=*m*,<=*k* (1<=β€<=*n*,<=*m*<=β€<=105,<=1<=β€<=*k*<=β€<=*min*(*n*,<=100)) which is the number of cells, the number of "Roads to the Sun" and the number of cells in the query correspondingly. Each of the following *m* roads contain three space-separated integers: ... | You have to print a single number on a single line which is the sum of stones in all the cells Chris is interested in.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d). | [
"5 2 1\n1 5 1\n2 4 1\n3\n",
"3 2 1\n1 3 1\n1 3 1\n2\n",
"3 2 1\n1 3 1\n1 3 1\n3\n"
] | [
"5\n",
"4\n",
"6\n"
] | none | [
{
"input": "5 2 1\n1 5 1\n2 4 1\n3",
"output": "5"
},
{
"input": "3 2 1\n1 3 1\n1 3 1\n2",
"output": "4"
},
{
"input": "3 2 1\n1 3 1\n1 3 1\n3",
"output": "6"
},
{
"input": "10 10 3\n6 10 74\n7 9 35\n3 6 63\n2 4 80\n2 10 78\n10 10 13\n4 10 16\n1 2 13\n3 7 17\n4 6 67\n9 8 10",... | 2,000 | 8,806,400 | 0 | 6,466 |
958 | Hyperspace Jump (easy) | [
"expression parsing",
"math"
] | null | null | The Rebel fleet is on the run. It consists of *m* ships currently gathered around a single planet. Just a few seconds ago, the vastly more powerful Empire fleet has appeared in the same solar system, and the Rebels will need to escape into hyperspace. In order to spread the fleet, the captain of each ship has independe... | The first line of the input contains a single integer *m* (1<=β€<=*m*<=β€<=200<=000) β the number of ships. The next *m* lines describe one jump coordinate each, given as an arithmetic expression. An expression has the form (a+b)/c. Namely, it consists of: an opening parenthesis (, a positive integer *a* of up to two dec... | Print a single line consisting of *m* space-separated integers. The *i*-th integer should be equal to the number of ships whose coordinate is equal to that of the *i*-th ship (including the *i*-th ship itself). | [
"4\n(99+98)/97\n(26+4)/10\n(12+33)/15\n(5+1)/7\n"
] | [
"1 2 2 1 "
] | In the sample testcase, the second and the third ship will both end up at the coordinate 3.
Note that this problem has only two versions β easy and hard. | [
{
"input": "4\n(99+98)/97\n(26+4)/10\n(12+33)/15\n(5+1)/7",
"output": "1 2 2 1 "
},
{
"input": "10\n(44+98)/19\n(36+58)/47\n(62+74)/68\n(69+95)/82\n(26+32)/29\n(32+46)/39\n(32+24)/28\n(47+61)/54\n(39+13)/26\n(98+98)/98",
"output": "1 9 9 9 9 9 9 9 9 9 "
},
{
"input": "30\n(89+76)/87\n(81... | 608 | 19,763,200 | 3 | 6,477 | |
94 | Friends | [
"graphs",
"implementation",
"math"
] | B. Friends | 1 | 256 | One day Igor K. stopped programming and took up math. One late autumn evening he was sitting at a table reading a book and thinking about something.
The following statement caught his attention: "Among any six people there are either three pairwise acquainted people or three pairwise unacquainted people"
Igor just c... | The first line contains an integer *m* (0<=β€<=*m*<=β€<=10), which is the number of relations of acquaintances among the five friends of Igor's.
Each of the following *m* lines contains two integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=5;*a**i*<=β <=*b**i*), where (*a**i*,<=*b**i*) is a pair of acquainted people.... | Print "FAIL", if among those five people there are no either three pairwise acquainted or three pairwise unacquainted people. Otherwise print "WIN". | [
"4\n1 3\n2 3\n1 4\n5 3\n",
"5\n1 2\n2 3\n3 4\n4 5\n5 1\n"
] | [
"WIN\n",
"FAIL\n"
] | none | [
{
"input": "4\n1 3\n2 3\n1 4\n5 3",
"output": "WIN"
},
{
"input": "5\n1 2\n2 3\n3 4\n4 5\n5 1",
"output": "FAIL"
},
{
"input": "1\n4 3",
"output": "WIN"
},
{
"input": "6\n1 3\n2 3\n1 2\n5 3\n4 2\n4 5",
"output": "WIN"
},
{
"input": "2\n1 3\n2 5",
"output": "WI... | 61 | 0 | 0 | 6,507 |
735 | Urbanization | [
"greedy",
"number theory",
"sortings"
] | null | null | Local authorities have heard a lot about combinatorial abilities of Ostap Bender so they decided to ask his help in the question of urbanization. There are *n* people who plan to move to the cities. The wealth of the *i* of them is equal to *a**i*. Authorities plan to build two cities, first for *n*1 people and second ... | The first line of the input contains three integers *n*, *n*1 and *n*2 (1<=β€<=*n*,<=*n*1,<=*n*2<=β€<=100<=000, *n*1<=+<=*n*2<=β€<=*n*)Β β the number of candidates who want to move to the cities, the planned number of residents of the first city and the planned number of residents of the second city.
The second line conta... | Print one real valueΒ β the maximum possible sum of arithmetic means of wealth of cities' residents. You 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 ... | [
"2 1 1\n1 5\n",
"4 2 1\n1 4 2 3\n"
] | [
"6.00000000\n",
"6.50000000\n"
] | In the first sample, one of the optimal solutions is to move candidate 1 to the first city and candidate 2 to the second.
In the second sample, the optimal solution is to pick candidates 3 and 4 for the first city, and candidate 2 for the second one. Thus we obtain (*a*<sub class="lower-index">3</sub>β+β*a*<sub class=... | [
{
"input": "2 1 1\n1 5",
"output": "6.00000000"
},
{
"input": "4 2 1\n1 4 2 3",
"output": "6.50000000"
},
{
"input": "3 1 2\n1 2 3",
"output": "4.50000000"
},
{
"input": "10 4 6\n3 5 7 9 12 25 67 69 83 96",
"output": "88.91666667"
},
{
"input": "19 7 12\n1 2 4 8 1... | 202 | 30,720,000 | 3 | 6,519 | |
797 | Broken BST | [
"data structures",
"dfs and similar"
] | null | null | Let *T* be arbitrary binary tree β tree, every vertex of which has no more than two children. Given tree is rooted, so there exists only one vertex which doesn't have a parent β it's the root of a tree. Every vertex has an integer number written on it. Following algorithm is run on every value from the tree *T*:
1. S... | First line contains integer number *n* (1<=β€<=*n*<=β€<=105) β number of vertices in the tree.
Each of the next *n* lines contains 3 numbers *v*, *l*, *r* (0<=β€<=*v*<=β€<=109) β value on current vertex, index of the left child of the vertex and index of the right child of the vertex, respectively. If some child doesn't e... | Print number of times when search algorithm will fail. | [
"3\n15 -1 -1\n10 1 3\n5 -1 -1\n",
"8\n6 2 3\n3 4 5\n12 6 7\n1 -1 8\n4 -1 -1\n5 -1 -1\n14 -1 -1\n2 -1 -1\n"
] | [
"2\n",
"1\n"
] | In the example the root of the tree in vertex 2. Search of numbers 5 and 15 will return fail because on the first step algorithm will choose the subtree which doesn't contain numbers you are looking for. | [
{
"input": "3\n15 -1 -1\n10 1 3\n5 -1 -1",
"output": "2"
},
{
"input": "8\n6 2 3\n3 4 5\n12 6 7\n1 -1 8\n4 -1 -1\n5 -1 -1\n14 -1 -1\n2 -1 -1",
"output": "1"
},
{
"input": "1\n493041212 -1 -1",
"output": "0"
},
{
"input": "10\n921294733 5 9\n341281094 -1 -1\n35060484 10 -1\n36... | 888 | 31,232,000 | -1 | 6,520 | |
886 | Maximum Element | [
"combinatorics",
"dp",
"math"
] | null | null | One day Petya was solving a very interesting problem. But although he used many optimization techniques, his solution still got Time limit exceeded verdict. Petya conducted a thorough analysis of his program and found out that his function for finding maximum element in an array of *n* positive integers was too slow. D... | The only line contains two integers *n* and *k* (1<=β€<=*n*,<=*k*<=β€<=106), separated by a spaceΒ β the length of the permutations and the parameter *k*. | Output the answer to the problem modulo 109<=+<=7. | [
"5 2\n",
"5 3\n",
"6 3\n"
] | [
"22\n",
"6\n",
"84\n"
] | Permutations from second example:
[4,β1,β2,β3,β5], [4,β1,β3,β2,β5], [4,β2,β1,β3,β5], [4,β2,β3,β1,β5], [4,β3,β1,β2,β5], [4,β3,β2,β1,β5]. | [
{
"input": "5 2",
"output": "22"
},
{
"input": "5 3",
"output": "6"
},
{
"input": "6 3",
"output": "84"
},
{
"input": "5 10",
"output": "0"
},
{
"input": "1 1",
"output": "0"
},
{
"input": "2 1",
"output": "0"
},
{
"input": "2 2",
"outp... | 108 | 15,052,800 | 0 | 6,522 | |
903 | Almost Difference | [
"data structures",
"math"
] | null | null | Let's denote a function
You are given an array *a* consisting of *n* integers. You have to calculate the sum of *d*(*a**i*,<=*a**j*) over all pairs (*i*,<=*j*) such that 1<=β€<=*i*<=β€<=*j*<=β€<=*n*. | The first line contains one integer *n* (1<=β€<=*n*<=β€<=200000) β the number of elements in *a*.
The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=109) β elements of the array. | Print one integer β the sum of *d*(*a**i*,<=*a**j*) over all pairs (*i*,<=*j*) such that 1<=β€<=*i*<=β€<=*j*<=β€<=*n*. | [
"5\n1 2 3 1 3\n",
"4\n6 6 5 5\n",
"4\n6 6 4 4\n"
] | [
"4\n",
"0\n",
"-8\n"
] | In the first example:
1. *d*(*a*<sub class="lower-index">1</sub>,β*a*<sub class="lower-index">2</sub>)β=β0; 1. *d*(*a*<sub class="lower-index">1</sub>,β*a*<sub class="lower-index">3</sub>)β=β2; 1. *d*(*a*<sub class="lower-index">1</sub>,β*a*<sub class="lower-index">4</sub>)β=β0; 1. *d*(*a*<sub class="lower-index">... | [
{
"input": "5\n1 2 3 1 3",
"output": "4"
},
{
"input": "4\n6 6 5 5",
"output": "0"
},
{
"input": "4\n6 6 4 4",
"output": "-8"
},
{
"input": "1\n1",
"output": "0"
},
{
"input": "1\n1000000000",
"output": "0"
},
{
"input": "2\n1 1000000000",
"output"... | 296 | 35,328,000 | 3 | 6,534 | |
627 | XOR Equation | [
"dp",
"math"
] | null | null | Two positive integers *a* and *b* have a sum of *s* and a bitwise XOR of *x*. How many possible values are there for the ordered pair (*a*,<=*b*)? | The first line of the input contains two integers *s* and *x* (2<=β€<=*s*<=β€<=1012, 0<=β€<=*x*<=β€<=1012), the sum and bitwise xor of the pair of positive integers, respectively. | Print a single integer, the number of solutions to the given conditions. If no solutions exist, print 0. | [
"9 5\n",
"3 3\n",
"5 2\n"
] | [
"4\n",
"2\n",
"0\n"
] | In the first sample, we have the following solutions: (2,β7), (3,β6), (6,β3), (7,β2).
In the second sample, the only solutions are (1,β2) and (2,β1). | [
{
"input": "9 5",
"output": "4"
},
{
"input": "3 3",
"output": "2"
},
{
"input": "5 2",
"output": "0"
},
{
"input": "6 0",
"output": "1"
},
{
"input": "549755813887 549755813887",
"output": "549755813886"
},
{
"input": "2 0",
"output": "1"
},
{... | 46 | 0 | 0 | 6,538 | |
706 | Beru-taxi | [
"brute force",
"geometry",
"implementation"
] | null | null | Vasiliy lives at point (*a*,<=*b*) of the coordinate plane. He is hurrying up to work so he wants to get out of his house as soon as possible. New app suggested *n* available Beru-taxi nearby. The *i*-th taxi is located at point (*x**i*,<=*y**i*) and moves with a speed *v**i*.
Consider that each of *n* drivers will m... | The first line of the input contains two integers *a* and *b* (<=-<=100<=β€<=*a*,<=*b*<=β€<=100)Β β coordinates of Vasiliy's home.
The second line contains a single integer *n* (1<=β€<=*n*<=β€<=1000)Β β the number of available Beru-taxi cars nearby.
The *i*-th of the following *n* lines contains three integers *x**i*, *y*... | Print a single real valueΒ β the minimum time Vasiliy needs to get in any of the Beru-taxi cars. You 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 answ... | [
"0 0\n2\n2 0 1\n0 2 2\n",
"1 3\n3\n3 3 2\n-2 3 6\n-2 7 10\n"
] | [
"1.00000000000000000000",
"0.50000000000000000000"
] | In the first sample, first taxi will get to Vasiliy in time 2, and second will do this in time 1, therefore 1 is the answer.
In the second sample, cars 2 and 3 will arrive simultaneously. | [
{
"input": "0 0\n2\n2 0 1\n0 2 2",
"output": "1.00000000000000000000"
},
{
"input": "1 3\n3\n3 3 2\n-2 3 6\n-2 7 10",
"output": "0.50000000000000000000"
},
{
"input": "2 2\n10\n8 10 1\n14 18 5\n2 2 1\n4 2 2\n5 2 1\n0 2 1\n2 10 4\n10 2 4\n14 18 20\n14 18 10",
"output": "0.000000000000... | 77 | 1,638,400 | 3 | 6,540 | |
922 | Magic Forest | [
"brute force"
] | null | null | Imp is in a magic forest, where xorangles grow (wut?)
A xorangle of order *n* is such a non-degenerate triangle, that lengths of its sides are integers not exceeding *n*, and the xor-sum of the lengths is equal to zero. Imp has to count the number of distinct xorangles of order *n* to get out of the forest.
Formally... | The only line contains a single integer *n* (1<=β€<=*n*<=β€<=2500). | Print the number of xorangles of order *n*. | [
"6\n",
"10\n"
] | [
"1\n",
"2\n"
] | The only xorangle in the first sample is (3,β5,β6). | [
{
"input": "6",
"output": "1"
},
{
"input": "10",
"output": "2"
},
{
"input": "3",
"output": "0"
},
{
"input": "4",
"output": "0"
},
{
"input": "5",
"output": "0"
},
{
"input": "2500",
"output": "700393"
},
{
"input": "952",
"output": "... | 93 | 1,945,600 | 3 | 6,550 | |
483 | Friends and Presents | [
"binary search",
"math"
] | null | null | You have two friends. You want to present each of them several positive integers. You want to present *cnt*1 numbers to the first friend and *cnt*2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the... | The only line contains four positive integers *cnt*1, *cnt*2, *x*, *y* (1<=β€<=*cnt*1,<=*cnt*2<=<<=109; *cnt*1<=+<=*cnt*2<=β€<=109; 2<=β€<=*x*<=<<=*y*<=β€<=3Β·104)Β β the numbers that are described in the statement. It is guaranteed that numbers *x*, *y* are prime. | Print a single integer β the answer to the problem. | [
"3 1 2 3\n",
"1 3 2 3\n"
] | [
"5\n",
"4\n"
] | In the first sample you give the set of numbers {1,β3,β5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1,β3,β5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first... | [
{
"input": "3 1 2 3",
"output": "5"
},
{
"input": "1 3 2 3",
"output": "4"
},
{
"input": "916200 69682 2 3",
"output": "1832399"
},
{
"input": "808351 17767 433 509",
"output": "826121"
},
{
"input": "8851 901 20897 26183",
"output": "9752"
},
{
"input... | 62 | 0 | 3 | 6,566 | |
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... | 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... | 2,000 | 0 | 0 | 6,596 | |
49 | Sum | [
"math"
] | B. Sum | 2 | 256 | Vasya studies positional numeral systems. Unfortunately, he often forgets to write the base of notation in which the expression is written. Once he saw a note in his notebook saying *a*<=+<=*b*<==<=?, and that the base of the positional notation wasnβt written anywhere. Now Vasya has to choose a base *p* and regard the... | The first letter contains two space-separated numbers *a* and *b* (1<=β€<=*a*,<=*b*<=β€<=1000) which represent the given summands. | Print a single number β the length of the longest answer. | [
"78 87\n",
"1 1\n"
] | [
"3\n",
"2\n"
] | none | [
{
"input": "78 87",
"output": "3"
},
{
"input": "1 1",
"output": "2"
},
{
"input": "9 7",
"output": "2"
},
{
"input": "11 11",
"output": "3"
},
{
"input": "43 21",
"output": "3"
},
{
"input": "84 89",
"output": "3"
},
{
"input": "12 34",
... | 248 | 0 | 0 | 6,610 |
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... | 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... | 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 1... | [
{
"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"
},
... | 154 | 0 | 3 | 6,611 | |
526 | Om Nom and Dark Park | [
"dfs and similar",
"greedy",
"implementation"
] | null | null | Om Nom is the main character of a game "Cut the Rope". He is a bright little monster who likes visiting friends living at the other side of the park. However the dark old parks can scare even somebody as fearless as Om Nom, so he asks you to help him.
The park consists of 2*n*<=+<=1<=-<=1 squares connected by roads so... | The first line contains integer *n* (1<=β€<=*n*<=β€<=10) β the number of roads on the path from the entrance to any exit.
The next line contains 2*n*<=+<=1<=-<=2 numbers *a*2,<=*a*3,<=... *a*2*n*<=+<=1<=-<=1 β the initial numbers of street lights on each road of the park. Here *a**i* is the number of street lights on th... | Print the minimum number of street lights that we should add to the roads of the park to make Om Nom feel safe. | [
"2\n1 2 3 4 5 6\n"
] | [
"5\n"
] | Picture for the sample test. Green color denotes the additional street lights. | [
{
"input": "2\n1 2 3 4 5 6",
"output": "5"
},
{
"input": "2\n1 2 3 3 2 2",
"output": "0"
},
{
"input": "1\n39 52",
"output": "13"
},
{
"input": "2\n59 96 34 48 8 72",
"output": "139"
},
{
"input": "3\n87 37 91 29 58 45 51 74 70 71 47 38 91 89",
"output": "210"... | 31 | 0 | 0 | 6,627 | |
14 | Four Segments | [
"brute force",
"constructive algorithms",
"geometry",
"implementation",
"math"
] | C. Four Segments | 2 | 64 | Several months later Alex finally got his brother Bob's creation by post. And now, in his turn, Alex wants to boast about something to his brother. He thought for a while, and came to the conclusion that he has no ready creations, and decided to write a program for rectangles detection. According to his plan, the progr... | The input data contain four lines. Each of these lines contains four integers *x*1, *y*1, *x*2, *y*2 (<=-<=109<=β€<=*x*1,<=*y*1,<=*x*2,<=*y*2<=β€<=109) β coordinates of segment's beginning and end positions. The given segments can degenerate into points. | Output the word Β«YESΒ», if the given four segments form the required rectangle, otherwise output Β«NOΒ». | [
"1 1 6 1\n1 0 6 0\n6 0 6 1\n1 1 1 0\n",
"0 0 0 3\n2 0 0 0\n2 2 2 0\n0 2 2 2\n"
] | [
"YES\n",
"NO\n"
] | none | [
{
"input": "1 1 6 1\n1 0 6 0\n6 0 6 1\n1 1 1 0",
"output": "YES"
},
{
"input": "0 0 0 3\n2 0 0 0\n2 2 2 0\n0 2 2 2",
"output": "NO"
},
{
"input": "0 0 0 2\n2 0 0 0\n2 2 2 0\n0 2 2 2",
"output": "YES"
},
{
"input": "0 0 10 0\n0 0 10 0\n0 0 0 5\n0 0 0 -5",
"output": "NO"
... | 62 | 0 | 0 | 6,628 |
868 | Huge Strings | [
"bitmasks",
"brute force",
"dp",
"implementation",
"strings"
] | null | null | You are given *n* strings *s*1,<=*s*2,<=...,<=*s**n* consisting of characters 0 and 1. *m* operations are performed, on each of them you concatenate two existing strings into a new one. On the *i*-th operation the concatenation *s**a**i**s**b**i* is saved into a new string *s**n*<=+<=*i* (the operations are numbered st... | The first line contains single integer *n* (1<=β€<=*n*<=β€<=100)Β β the number of strings. The next *n* lines contain strings *s*1,<=*s*2,<=...,<=*s**n* (1<=β€<=|*s**i*|<=β€<=100), one per line. The total length of strings is not greater than 100.
The next line contains single integer *m* (1<=β€<=*m*<=β€<=100)Β β the number o... | Print *m* lines, each should contain one integerΒ β the answer to the question after the corresponding operation. | [
"5\n01\n10\n101\n11111\n0\n3\n1 2\n6 5\n4 4\n"
] | [
"1\n2\n0\n"
] | On the first operation, a new string "0110" is created. For *k*β=β1 the two possible binary strings of length *k* are "0" and "1", they are substrings of the new string. For *k*β=β2 and greater there exist strings of length *k* that do not appear in this string (for *k*β=β2 such string is "00"). So the answer is 1.
On... | [
{
"input": "5\n01\n10\n101\n11111\n0\n3\n1 2\n6 5\n4 4",
"output": "1\n2\n0"
},
{
"input": "5\n01\n1\n0011\n0\n01\n6\n5 5\n3 2\n4 2\n6 7\n5 1\n9 7",
"output": "1\n1\n1\n2\n1\n2"
},
{
"input": "5\n111101000111100011100110000100\n000111001\n01101000\n000011010010001001100100000001010010011... | 810 | 268,390,400 | 0 | 6,630 | |
999 | Cards and Joy | [
"dp"
] | null | null | There are $n$ players sitting at the card table. Each player has a favorite number. The favorite number of the $j$-th player is $f_j$.
There are $k \cdot n$ cards on the table. Each card contains a single integer: the $i$-th card contains number $c_i$. Also, you are given a sequence $h_1, h_2, \dots, h_k$. Its meaning... | The first line of input contains two integers $n$ and $k$ ($1 \le n \le 500, 1 \le k \le 10$) β the number of players and the number of cards each player will get.
The second line contains $k \cdot n$ integers $c_1, c_2, \dots, c_{k \cdot n}$ ($1 \le c_i \le 10^5$) β the numbers written on the cards.
The third line c... | Print one integer β the maximum possible total joy levels of the players among all possible card distributions. | [
"4 3\n1 3 2 8 5 5 8 2 2 8 5 2\n1 2 2 5\n2 6 7\n",
"3 3\n9 9 9 9 9 9 9 9 9\n1 2 3\n1 2 3\n"
] | [
"21\n",
"0\n"
] | In the first example, one possible optimal card distribution is the following:
- Player $1$ gets cards with numbers $[1, 3, 8]$; - Player $2$ gets cards with numbers $[2, 2, 8]$; - Player $3$ gets cards with numbers $[2, 2, 8]$; - Player $4$ gets cards with numbers $[5, 5, 5]$.
Thus, the answer is $2 + 6 + 6 + 7 ... | [
{
"input": "4 3\n1 3 2 8 5 5 8 2 2 8 5 2\n1 2 2 5\n2 6 7",
"output": "21"
},
{
"input": "3 3\n9 9 9 9 9 9 9 9 9\n1 2 3\n1 2 3",
"output": "0"
},
{
"input": "1 1\n1\n2\n1",
"output": "0"
},
{
"input": "1 1\n1\n1\n1",
"output": "1"
},
{
"input": "1 1\n1\n1\n100000",... | 2,000 | 63,385,600 | 0 | 6,635 | |
374 | Inna and Nine | [
"combinatorics",
"greedy"
] | null | null | Inna loves digit 9 very much. That's why she asked Dima to write a small number consisting of nines. But Dima must have misunderstood her and he wrote a very large number *a*, consisting of digits from 1 to 9.
Inna wants to slightly alter the number Dima wrote so that in the end the number contained as many digits nin... | The first line of the input contains integer *a* (1<=β€<=*a*<=β€<=10100000). Number *a* doesn't have any zeroes. | In a single line print a single integer β the answer to the problem. It is guaranteed that the answer to the problem doesn't exceed 263<=-<=1.
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. | [
"369727\n",
"123456789987654321\n",
"1\n"
] | [
"2\n",
"1\n",
"1\n"
] | Notes to the samples
In the first sample Inna can get the following numbers: 369727βββ99727βββ9997, 369727βββ99727βββ9979.
In the second sample, Inna can act like this: 123456789987654321βββ12396789987654321βββ1239678998769321. | [
{
"input": "369727",
"output": "2"
},
{
"input": "123456789987654321",
"output": "1"
},
{
"input": "1",
"output": "1"
},
{
"input": "3636363636363454545454543636363636454545452727272727218181818181999111777",
"output": "1512"
},
{
"input": "1188",
"output": "1... | 655 | 11,264,000 | 3 | 6,637 | |
877 | Slava and tanks | [
"constructive algorithms"
] | null | null | Slava plays his favorite game "Peace Lightning". Now he is flying a bomber on a very specific map.
Formally, map is a checkered field of size 1<=Γ<=*n*, the cells of which are numbered from 1 to *n*, in each cell there can be one or several tanks. Slava doesn't know the number of tanks and their positions, because he ... | The first line contains a single integer *n* (2<=β€<=*n*<=β€<=100<=000) β the size of the map. | In the first line print *m* β the minimum number of bombs Slava needs to destroy all tanks.
In the second line print *m* integers *k*1,<=*k*2,<=...,<=*k**m*. The number *k**i* means that the *i*-th bomb should be dropped at the cell *k**i*.
If there are multiple answers, you can print any of them. | [
"2\n",
"3\n"
] | [
"3\n2 1 2 ",
"4\n2 1 3 2 "
] | none | [
{
"input": "2",
"output": "3\n2 1 2 "
},
{
"input": "3",
"output": "4\n2 1 3 2 "
},
{
"input": "4",
"output": "6\n2 4 1 3 2 4 "
},
{
"input": "6",
"output": "9\n2 4 6 1 3 5 2 4 6 "
},
{
"input": "10",
"output": "15\n2 4 6 8 10 1 3 5 7 9 2 4 6 8 10 "
},
{
... | 62 | 0 | 0 | 6,638 | |
23 | Tree | [
"dp"
] | E. Tree | 2 | 256 | Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can... | The first input line contains integer number *n* (1<=β€<=*n*<=β€<=700) β amount of vertices in the tree. The following *n*<=-<=1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, *a**i*, *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*). It's guaranteed that the graph d... | Output the only number β the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. | [
"5\n1 2\n2 3\n3 4\n4 5\n",
"8\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n6 8\n",
"3\n1 2\n1 3\n"
] | [
"6",
"18",
"3"
] | none | [
{
"input": "5\n1 2\n2 3\n3 4\n4 5",
"output": "6"
},
{
"input": "8\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n6 8",
"output": "18"
},
{
"input": "3\n1 2\n1 3",
"output": "3"
},
{
"input": "5\n3 2\n1 5\n4 5\n5 3",
"output": "6"
},
{
"input": "5\n2 1\n3 4\n3 5\n5 2",
"outpu... | 0 | 0 | -1 | 6,651 |
746 | Green and Black Tea | [
"constructive algorithms",
"greedy",
"math"
] | null | null | Innokentiy likes tea very much and today he wants to drink exactly *n* cups of tea. He would be happy to drink more but he had exactly *n* tea bags, *a* of them are green and *b* are black.
Innokentiy doesn't like to drink the same tea (green or black) more than *k* times in a row. Your task is to determine the order ... | The first line contains four integers *n*, *k*, *a* and *b* (1<=β€<=*k*<=β€<=*n*<=β€<=105, 0<=β€<=*a*,<=*b*<=β€<=*n*)Β β the number of cups of tea Innokentiy wants to drink, the maximum number of cups of same tea he can drink in a row, the number of tea bags of green and black tea. It is guaranteed that *a*<=+<=*b*<==<=*n*. | If it is impossible to drink *n* cups of tea, print "NO" (without quotes).
Otherwise, print the string of the length *n*, which consists of characters 'G' and 'B'. If some character equals 'G', then the corresponding cup of tea should be green. If some character equals 'B', then the corresponding cup of tea should be ... | [
"5 1 3 2\n",
"7 2 2 5\n",
"4 3 4 0\n"
] | [
"GBGBG\n",
"BBGBGBB",
"NO\n"
] | none | [
{
"input": "5 1 3 2",
"output": "GBGBG"
},
{
"input": "7 2 2 5",
"output": "BBGBBGB"
},
{
"input": "4 3 4 0",
"output": "NO"
},
{
"input": "2 2 0 2",
"output": "BB"
},
{
"input": "3 2 0 3",
"output": "NO"
},
{
"input": "1 1 0 1",
"output": "B"
},... | 92 | 2,764,800 | 0 | 6,657 | |
228 | Zigzag | [
"data structures"
] | null | null | The court wizard Zigzag wants to become a famous mathematician. For that, he needs his own theorem, like the Cauchy theorem, or his sum, like the Minkowski sum. But most of all he wants to have his sequence, like the Fibonacci sequence, and his function, like the Euler's totient function.
The Zigag's sequence with the... | The first line contains integer *n* (1<=β€<=*n*<=β€<=105) β The number of elements in array *a*. The second line contains *n* space-separated integers: *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109) β the elements of the array.
The third line contains integer *m* (1<=β€<=*m*<=β€<=105) β the number of operations. Next ... | For each Zigzag operation print the calculated value of the Zigzag function on a single line. Print the values for Zigzag functions in the order, in which they are given in the input.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d sp... | [
"5\n2 3 1 5 5\n4\n2 2 3 2\n2 1 5 3\n1 3 5\n2 1 5 3\n"
] | [
"5\n26\n38\n"
] | Explanation of the sample test:
- Result of the first operation is *Z*(2,β3,β2)β=β3Β·1β+β1Β·2β=β5. - Result of the second operation is *Z*(1,β5,β3)β=β2Β·1β+β3Β·2β+β1Β·3β+β5Β·2β+β5Β·1β=β26. - After the third operation array *a* is equal to 2,β3,β5,β5,β5. - Result of the forth operation is *Z*(1,β5,β3)β=β2Β·1β+β3Β·2β+β5Β·3β+... | [] | 92 | 0 | 0 | 6,660 | |
761 | Dasha and Puzzle | [
"constructive algorithms",
"dfs and similar",
"graphs",
"greedy",
"trees"
] | null | null | Dasha decided to have a rest after solving the problem. She had been ready to start her favourite activity β origami, but remembered the puzzle that she could not solve.
The tree is a non-oriented connected graph without cycles. In particular, there always are *n*<=-<=1 edges in a tree with *n* vertices.
The puzzle ... | The first line contains single integer *n* (1<=β€<=*n*<=β€<=30) β the number of vertices in the tree.
Each of next *n*<=-<=1 lines contains two integers *u**i*, *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*) that mean that the *i*-th edge of the tree connects vertices *u**i* and *v**i*.
It is guaranteed that the described gra... | If the puzzle doesn't have a solution then in the only line print "NO".
Otherwise, the first line should contain "YES". The next *n* lines should contain the pair of integers *x**i*, *y**i* (|*x**i*|,<=|*y**i*|<=β€<=1018) β the coordinates of the point which corresponds to the *i*-th vertex of the tree.
If there are s... | [
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7",
"6\n1 2\n2 3\n2 4\n2 5\n2 6\n",
"4\n1 2\n2 3\n3 4"
] | [
"YES\n0 0\n1 0\n0 1\n2 0\n1 -1\n-1 1\n0 2",
"NO\n",
"YES\n3 3\n4 3\n5 3\n6 3"
] | In the first sample one of the possible positions of tree is: <img class="tex-graphics" src="https://espresso.codeforces.com/360c9c903e5177970a5a3b2711f6718c2163ffd8.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7",
"output": "YES\n0 0\n0 1073741824\n1073741824 0\n0 1610612736\n536870912 1073741824\n1073741824 536870912\n1610612736 0"
},
{
"input": "6\n1 2\n2 3\n2 4\n2 5\n2 6",
"output": "NO"
},
{
"input": "4\n1 2\n2 3\n3 4",
"output": "YES\n0 0\n0 1... | 155 | 409,600 | 0 | 6,664 | |
79 | Bus Game | [
"greedy"
] | A. Bus Game | 2 | 256 | After Fox Ciel won an onsite round of a programming contest, she took a bus to return to her castle. The fee of the bus was 220 yen. She met Rabbit Hanako in the bus. They decided to play the following game because they got bored in the bus.
- Initially, there is a pile that contains *x* 100-yen coins and *y* 10-yen ... | The first line contains two integers *x* (0<=β€<=*x*<=β€<=106) and *y* (0<=β€<=*y*<=β€<=106), separated by a single space. | If Ciel wins, print "Ciel". Otherwise, print "Hanako". | [
"2 2\n",
"3 22\n"
] | [
"Ciel\n",
"Hanako\n"
] | In the first turn (Ciel's turn), she will choose 2 100-yen coins and 2 10-yen coins. In the second turn (Hanako's turn), she will choose 1 100-yen coin and 12 10-yen coins. In the third turn (Ciel's turn), she can't pay exactly 220 yen, so Ciel will lose. | [
{
"input": "2 2",
"output": "Ciel"
},
{
"input": "3 22",
"output": "Hanako"
},
{
"input": "0 22",
"output": "Ciel"
},
{
"input": "1000 1000",
"output": "Ciel"
},
{
"input": "0 0",
"output": "Hanako"
},
{
"input": "0 21",
"output": "Hanako"
},
{... | 280 | 2,867,200 | 3.924659 | 6,668 |
981 | Businessmen Problems | [
"sortings"
] | null | null | Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies.
In order to avoid this representatives of both companies decided to make an agreement on the ... | The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) Β β the number of elements discovered by ChemForces.
The $i$-th of the next $n$ lines contains two integers $a_i$ and $x_i$ ($1 \leq a_i \leq 10^9$, $1 \leq x_i \leq 10^9$) Β β the index of the $i$-th element and the income of its usage on the exhibitio... | Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets. | [
"3\n1 2\n7 2\n3 10\n4\n1 4\n2 4\n3 4\n4 4\n",
"1\n1000000000 239\n3\n14 15\n92 65\n35 89\n"
] | [
"24\n",
"408\n"
] | In the first example ChemForces can choose the set ($3, 7$), while TopChemist can choose ($1, 2, 4$). This way the total income is $(10 + 2) + (4 + 4 + 4) = 24$.
In the second example ChemForces can choose the only element $10^9$, while TopChemist can choose ($14, 92, 35$). This way the total income is $(239) + (15 + ... | [
{
"input": "3\n1 2\n7 2\n3 10\n4\n1 4\n2 4\n3 4\n4 4",
"output": "24"
},
{
"input": "1\n1000000000 239\n3\n14 15\n92 65\n35 89",
"output": "408"
},
{
"input": "10\n598654597 488228616\n544064902 21923894\n329635457 980089248\n988262691 654502493\n967529230 543358150\n835120075 128123793\... | 1,263 | 22,016,000 | 3 | 6,675 | |
931 | Laboratory Work | [
"implementation",
"math"
] | null | null | Anya and Kirill are doing a physics laboratory work. In one of the tasks they have to measure some value *n* times, and then compute the average value to lower the error.
Kirill has already made his measurements, and has got the following integer values: *x*1, *x*2, ..., *x**n*. It is important that the values are clo... | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=100<=000) β the numeber of measurements made by Kirill.
The second line contains a sequence of integers *x*1,<=*x*2,<=...,<=*x**n* (<=-<=100<=000<=β€<=*x**i*<=β€<=100<=000) β the measurements made by Kirill. It is guaranteed that the difference between the maxi... | In the first line print the minimum possible number of equal measurements.
In the second line print *n* integers *y*1,<=*y*2,<=...,<=*y**n* β the values Anya should write. You can print the integers in arbitrary order. Keep in mind that the minimum value among Anya's values should be not less that the minimum among Ki... | [
"6\n-1 1 1 0 0 -1\n",
"3\n100 100 101\n",
"7\n-10 -9 -10 -8 -10 -9 -9\n"
] | [
"2\n0 0 0 0 0 0 \n",
"3\n101 100 100 \n",
"5\n-10 -10 -9 -9 -9 -9 -9 \n"
] | In the first example Anya can write zeros as here measurements results. The average value is then equal to the average value of Kirill's values, and there are only two equal measurements.
In the second example Anya should write two values 100 and one value 101 (in any order), because it is the only possibility to make... | [
{
"input": "6\n-1 1 1 0 0 -1",
"output": "2\n0 0 0 0 0 0 "
},
{
"input": "3\n100 100 101",
"output": "3\n101 100 100 "
},
{
"input": "7\n-10 -9 -10 -8 -10 -9 -9",
"output": "5\n-10 -10 -9 -9 -9 -9 -9 "
},
{
"input": "60\n-8536 -8536 -8536 -8535 -8536 -8536 -8536 -8536 -8536 -... | 295 | 12,697,600 | 0 | 6,681 | |
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 absolutel... | 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 | 0 | 6,683 |
0 | none | [
"none"
] | null | null | Bear Limak prepares problems for a programming competition. Of course, it would be unprofessional to mention the sponsor name in the statement. Limak takes it seriously and he is going to change some words. To make it still possible to read, he will try to modify each word as little as possible.
Limak has a string *s*... | The first line of the input contains an integer *n* (1<=β€<=*n*<=β€<=75)Β β the length of the string.
The second line contains a string *s*, consisting of uppercase English letters. The length of the string is equal to *n*. | Print one integer, denoting the minimum possible number of moves Limak can do, in order to obtain a string without a substring "VK". | [
"4\nVKVK\n",
"5\nBVVKV\n",
"7\nVVKEVKK\n",
"20\nVKVKVVVKVOVKVQKKKVVK\n",
"5\nLIMAK\n"
] | [
"3\n",
"2\n",
"3\n",
"8\n",
"0\n"
] | In the first sample, the initial string is "VKVK". The minimum possible number of moves is 3. One optimal sequence of moves is:
1. Swap two last letters. The string becomes "VKKV".1. Swap first two letters. The string becomes "KVKV".1. Swap the second and the third letter. The string becomes "KKVV". Indeed, this str... | [] | 46 | 0 | 0 | 6,691 | |
159 | Matchmaker | [
"*special",
"greedy",
"sortings"
] | null | null | Polycarpus has *n* markers and *m* marker caps. Each marker is described by two numbers: *x**i* is the color and *y**i* is the diameter. Correspondingly, each cap is described by two numbers: *a**j* is the color and *b**j* is the diameter. Cap (*a**j*,<=*b**j*) can close marker (*x**i*,<=*y**i*) only if their diameters... | The first input line contains two space-separated integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=105) β the number of markers and the number of caps, correspondingly.
Next *n* lines describe the markers. The *i*-th line contains two space-separated integers *x**i*, *y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=1000) β the *i*-th mark... | Print two space-separated integers *u*,<=*v*, where *u* is the number of closed markers and *v* is the number of beautifully closed markers in the sought optimal way. Remember that you have to find the way to close the maximum number of markers, and if there are several such ways, you should choose the one where the nu... | [
"3 4\n1 2\n3 4\n2 4\n5 4\n2 4\n1 1\n1 2\n",
"2 2\n1 2\n2 1\n3 4\n5 1\n"
] | [
"3 2\n",
"1 0\n"
] | In the first test sample the first marker should be closed by the fourth cap, the second marker should be closed by the first cap and the third marker should be closed by the second cap. Thus, three markers will be closed, and two of them will be beautifully closed β the first and the third markers. | [
{
"input": "3 4\n1 2\n3 4\n2 4\n5 4\n2 4\n1 1\n1 2",
"output": "3 2"
},
{
"input": "2 2\n1 2\n2 1\n3 4\n5 1",
"output": "1 0"
},
{
"input": "6 7\n2 1\n2 2\n2 1\n1 1\n2 1\n1 2\n2 2\n2 2\n2 2\n1 2\n2 2\n1 1\n1 2",
"output": "3 3"
},
{
"input": "6 7\n2 1\n1 1\n2 2\n1 2\n1 1\n1 2... | 3,000 | 10,444,800 | 0 | 6,696 | |
699 | One Bomb | [
"implementation"
] | null | null | You are given a description of a depot. It is a rectangular checkered field of *n*<=Γ<=*m* size. Each cell in a field can be empty (".") or it can be occupied by a wall ("*").
You have one bomb. If you lay the bomb at the cell (*x*,<=*y*), then after triggering it will wipe out all walls in the row *x* and all walls ... | The first line contains two positive integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=1000)Β β the number of rows and columns in the depot field.
The next *n* lines contain *m* symbols "." and "*" eachΒ β the description of the field. *j*-th symbol in *i*-th of them stands for cell (*i*,<=*j*). If the symbol is equal to ".", ... | If it is impossible to wipe out all walls by placing and triggering exactly one bomb, then print "NO" in the first line (without quotes).
Otherwise print "YES" (without quotes) in the first line and two integers in the second lineΒ β the coordinates of the cell at which the bomb should be laid. If there are multiple an... | [
"3 4\n.*..\n....\n.*..\n",
"3 3\n..*\n.*.\n*..\n",
"6 5\n..*..\n..*..\n*****\n..*..\n..*..\n..*..\n"
] | [
"YES\n1 2\n",
"NO\n",
"YES\n3 3\n"
] | none | [
{
"input": "3 4\n.*..\n....\n.*..",
"output": "YES\n1 2"
},
{
"input": "3 3\n..*\n.*.\n*..",
"output": "NO"
},
{
"input": "6 5\n..*..\n..*..\n*****\n..*..\n..*..\n..*..",
"output": "YES\n3 3"
},
{
"input": "1 10\n**********",
"output": "YES\n1 1"
},
{
"input": "10... | 46 | 0 | 0 | 6,701 | |
519 | A and B and Lecture Rooms | [
"binary search",
"data structures",
"dfs and similar",
"dp",
"trees"
] | null | null | A and B are preparing themselves for programming contests.
The University where A and B study is a set of rooms connected by corridors. Overall, the University has *n* rooms connected by *n*<=-<=1 corridors so that you can get from any room to any other one by moving along the corridors. The rooms are numbered from 1 ... | The first line contains integer *n* (1<=β€<=*n*<=β€<=105) β the number of rooms in the University.
The next *n*<=-<=1 lines describe the corridors. The *i*-th of these lines (1<=β€<=*i*<=β€<=*n*<=-<=1) contains two integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*), showing that the *i*-th corridor connects rooms ... | In the *i*-th (1<=β€<=*i*<=β€<=*m*) line print the number of rooms that are equidistant from the rooms where A and B write contest on the *i*-th day. | [
"4\n1 2\n1 3\n2 4\n1\n2 3\n",
"4\n1 2\n2 3\n2 4\n2\n1 2\n1 3\n"
] | [
"1\n",
"0\n2\n"
] | in the first sample there is only one room at the same distance from rooms number 2 and 3 β room number 1. | [
{
"input": "4\n1 2\n1 3\n2 4\n1\n2 3",
"output": "1"
},
{
"input": "4\n1 2\n2 3\n2 4\n2\n1 2\n1 3",
"output": "0\n2"
},
{
"input": "15\n1 2\n1 3\n1 4\n2 5\n2 6\n2 7\n5 8\n6 9\n9 14\n14 15\n7 10\n4 13\n3 11\n3 12\n6\n10 15\n13 12\n2 15\n8 4\n15 12\n6 13",
"output": "1\n10\n1\n7\n0\n4"... | 2,000 | 14,336,000 | 0 | 6,735 | |
272 | Dima and Two Sequences | [
"combinatorics",
"math",
"sortings"
] | null | null | Little Dima has two sequences of points with integer coordinates: sequence (*a*1,<=1),<=(*a*2,<=2),<=...,<=(*a**n*,<=*n*) and sequence (*b*1,<=1),<=(*b*2,<=2),<=...,<=(*b**n*,<=*n*).
Now Dima wants to count the number of distinct sequences of points of length 2Β·*n* that can be assembled from these sequences, such that... | The first line contains integer *n* (1<=β€<=*n*<=β€<=105). The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109). The third line contains *n* integers *b*1,<=*b*2,<=...,<=*b**n* (1<=β€<=*b**i*<=β€<=109). The numbers in the lines are separated by spaces.
The last line contains integer *m* ... | In the single line print the remainder after dividing the answer to the problem by number *m*. | [
"1\n1\n2\n7\n",
"2\n1 2\n2 3\n11\n"
] | [
"1\n",
"2\n"
] | In the first sample you can get only one sequence: (1,β1),β(2,β1).
In the second sample you can get such sequences : (1,β1),β(2,β2),β(2,β1),β(3,β2); (1,β1),β(2,β1),β(2,β2),β(3,β2). Thus, the answer is 2. | [
{
"input": "1\n1\n2\n7",
"output": "1"
},
{
"input": "2\n1 2\n2 3\n11",
"output": "2"
},
{
"input": "100\n1 8 10 6 5 3 2 3 4 2 3 7 1 1 5 1 4 1 8 1 5 5 6 5 3 7 4 5 5 3 8 7 8 6 8 9 10 7 8 5 8 9 1 3 7 2 6 1 7 7 2 8 1 5 4 2 10 4 9 8 1 10 1 5 9 8 1 9 5 1 5 7 1 6 7 8 8 2 2 3 3 7 2 10 6 3 6 3 5... | 778 | 47,718,400 | 3 | 6,749 | |
229 | Shifts | [
"brute force",
"two pointers"
] | null | null | You are given a table consisting of *n* rows and *m* columns. Each cell of the table contains a number, 0 or 1. In one move we can choose some row of the table and cyclically shift its values either one cell to the left, or one cell to the right.
To cyclically shift a table row one cell to the right means to move the ... | The first line contains two space-separated integers: *n* (1<=β€<=*n*<=β€<=100)Β β the number of rows in the table and *m* (1<=β€<=*m*<=β€<=104)Β β the number of columns in the table. Then *n* lines follow, each of them contains *m* characters "0" or "1": the *j*-th character of the *i*-th line describes the contents of the ... | Print a single number: the minimum number of moves needed to get only numbers 1 in some column of the table. If this is impossible, print -1. | [
"3 6\n101010\n000100\n100000\n",
"2 3\n111\n000\n"
] | [
"3\n",
"-1\n"
] | In the first sample one way to achieve the goal with the least number of moves is as follows: cyclically shift the second row to the right once, then shift the third row to the left twice. Then the table column before the last one will contain only 1s.
In the second sample one can't shift the rows to get a column cont... | [
{
"input": "3 6\n101010\n000100\n100000",
"output": "3"
},
{
"input": "2 3\n111\n000",
"output": "-1"
},
{
"input": "1 1\n1",
"output": "0"
},
{
"input": "1 1\n0",
"output": "-1"
},
{
"input": "3 1\n1\n1\n0",
"output": "-1"
},
{
"input": "6 2\n10\n11\n... | 218 | 0 | 0 | 6,754 | |
573 | Bear and Drawing | [
"constructive algorithms",
"dfs and similar",
"trees"
] | null | null | Limak is a little bear who learns to draw. People usually start with houses, fences and flowers but why would bears do it? Limak lives in the forest and he decides to draw a tree.
Recall that tree is a connected graph consisting of *n* vertices and *n*<=-<=1 edges.
Limak chose a tree with *n* vertices. He has infinit... | The first line contains single integer *n* (1<=β€<=*n*<=β€<=105).
Next *n*<=-<=1 lines contain description of a tree. *i*-th of them contains two space-separated integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*,<=*a**i*<=β <=*b**i*) denoting an edge between vertices *a**i* and *b**i*. It's guaranteed that given ... | Print "Yes" (without the quotes) if Limak can draw chosen tree. Otherwise, print "No" (without the quotes). | [
"8\n1 2\n1 3\n1 6\n6 4\n6 7\n6 5\n7 8\n",
"13\n1 2\n1 3\n1 4\n2 5\n2 6\n2 7\n3 8\n3 9\n3 10\n4 11\n4 12\n4 13\n"
] | [
"Yes\n",
"No\n"
] | none | [] | 30 | 0 | 0 | 6,755 | |
616 | Sum of Remainders | [
"implementation",
"math",
"number theory"
] | null | null | Calculate the value of the sum: *n* mod 1 + *n* mod 2 + *n* mod 3 + ... + *n* mod *m*. As the result can be very large, you should print the value modulo 109<=+<=7 (the remainder when divided by 109<=+<=7).
The modulo operator *a* mod *b* stands for the remainder after dividing *a* by *b*. For example 10 mod 3 = 1. | The only line contains two integers *n*,<=*m* (1<=β€<=*n*,<=*m*<=β€<=1013) β the parameters of the sum. | Print integer *s* β the value of the required sum modulo 109<=+<=7. | [
"3 4\n",
"4 4\n",
"1 1\n"
] | [
"4\n",
"1\n",
"0\n"
] | none | [
{
"input": "3 4",
"output": "4"
},
{
"input": "4 4",
"output": "1"
},
{
"input": "1 1",
"output": "0"
},
{
"input": "20000000 10000000",
"output": "176305083"
},
{
"input": "10000000000000 10000000000000",
"output": "869957328"
},
{
"input": "1 1000000... | 2,000 | 0 | 0 | 6,759 | |
797 | Minimal string | [
"data structures",
"greedy",
"strings"
] | null | null | Petya recieved a gift of a string *s* with length up to 105 characters for his birthday. He took two more empty strings *t* and *u* and decided to play a game. This game has two possible moves:
- Extract the first character of *s* and append *t* with this character. - Extract the last character of *t* and append *u*... | First line contains non-empty string *s* (1<=β€<=|*s*|<=β€<=105), consisting of lowercase English letters. | Print resulting string *u*. | [
"cab\n",
"acdb\n"
] | [
"abc\n",
"abdc\n"
] | none | [
{
"input": "cab",
"output": "abc"
},
{
"input": "acdb",
"output": "abdc"
},
{
"input": "a",
"output": "a"
},
{
"input": "ab",
"output": "ab"
},
{
"input": "ba",
"output": "ab"
},
{
"input": "dijee",
"output": "deeji"
},
{
"input": "bhrmc",
... | 108 | 307,200 | 0 | 6,760 | |
397 | On Segment's Own Points | [
"implementation"
] | null | null | Our old friend Alexey has finally entered the University of City N β the Berland capital. Alexey expected his father to get him a place to live in but his father said it was high time for Alexey to practice some financial independence. So, Alexey is living in a dorm.
The dorm has exactly one straight dryer β a 100 ce... | The first line contains a positive integer *n* (1<=β€<=*n*<=β€<=100). The (*i*<=+<=1)-th line contains integers *l**i* and *r**i* (0<=β€<=*l**i*<=<<=*r**i*<=β€<=100) βΒ the endpoints of the corresponding segment for the *i*-th student. | On a single line print a single number *k*, equal to the sum of lengths of the parts of the dryer which are inside Alexey's segment and are outside all other segments. | [
"3\n0 5\n2 8\n1 6\n",
"3\n0 10\n1 5\n7 15\n"
] | [
"1\n",
"3\n"
] | Note that it's not important are clothes drying on the touching segments (e.g. (0,β1) and (1,β2)) considered to be touching or not because you need to find the length of segments.
In the first test sample Alexey may use the only segment (0,β1). In such case his clothes will not touch clothes on the segments (1,β6) and... | [
{
"input": "3\n0 5\n2 8\n1 6",
"output": "1"
},
{
"input": "3\n0 10\n1 5\n7 15",
"output": "3"
},
{
"input": "1\n0 100",
"output": "100"
},
{
"input": "2\n1 9\n1 9",
"output": "0"
},
{
"input": "2\n1 9\n5 10",
"output": "4"
},
{
"input": "2\n1 9\n3 5",... | 93 | 6,144,000 | 0 | 6,764 | |
484 | Bits | [
"bitmasks",
"constructive algorithms"
] | null | null | Let's denote as the number of bits set ('1' bits) in the binary representation of the non-negative integer *x*.
You are given multiple queries consisting of pairs of integers *l* and *r*. For each query, find the *x*, such that *l*<=β€<=*x*<=β€<=*r*, and is maximum possible. If there are multiple such numbers find the... | The first line contains integer *n*Β β the number of queries (1<=β€<=*n*<=β€<=10000).
Each of the following *n* lines contain two integers *l**i*,<=*r**i*Β β the arguments for the corresponding query (0<=β€<=*l**i*<=β€<=*r**i*<=β€<=1018). | For each query print the answer in a separate line. | [
"3\n1 2\n2 4\n1 10\n"
] | [
"1\n3\n7\n"
] | The binary representations of numbers from 1 to 10 are listed below:
1<sub class="lower-index">10</sub>β=β1<sub class="lower-index">2</sub>
2<sub class="lower-index">10</sub>β=β10<sub class="lower-index">2</sub>
3<sub class="lower-index">10</sub>β=β11<sub class="lower-index">2</sub>
4<sub class="lower-index">10</su... | [
{
"input": "3\n1 2\n2 4\n1 10",
"output": "1\n3\n7"
},
{
"input": "55\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n2 2\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n3 3\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n4 4\n4 5\n4 6\n4 7\n4 8\n4 9\n4 10\n5 5\n5 6\n5 7\n5 8\n5 9\n5 10\n6 6\n6 7\n6 8\n6 9\n6 10... | 109 | 0 | 0 | 6,774 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.