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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
959 | Mahmoud and Ehab and the xor-MST | [
"bitmasks",
"dp",
"graphs",
"implementation",
"math"
] | null | null | Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of *n* vertices numbered from 0 to *n*<=-<=1. For all 0<=≤<=*u*<=<<=*v*<=<<=*n*, vertex *u* and vertex *v* are connected with an undirected edge that has weight... | The only line contains an integer *n* (2<=≤<=*n*<=≤<=1012), the number of vertices in the graph. | The only line contains an integer *x*, the weight of the graph's minimum spanning tree. | [
"4\n"
] | [
"4"
] | In the first sample: <img class="tex-graphics" src="https://espresso.codeforces.com/20e1655a0ec8e8d788bd2f5af92f93c968c65f3c.png" style="max-width: 100.0%;max-height: 100.0%;"/> The weight of the minimum spanning tree is 1+2+1=4. | [
{
"input": "4",
"output": "4"
},
{
"input": "10",
"output": "21"
},
{
"input": "2",
"output": "1"
},
{
"input": "1000000000000",
"output": "20140978692096"
},
{
"input": "999999999999",
"output": "20140978692095"
},
{
"input": "23131234",
"output":... | 61 | 0 | 0 | 20,537 | |
400 | Dima and Bacteria | [
"dsu",
"graphs",
"shortest paths"
] | null | null | Dima took up the biology of bacteria, as a result of his experiments, he invented *k* types of bacteria. Overall, there are *n* bacteria at his laboratory right now, and the number of bacteria of type *i* equals *c**i*. For convenience, we will assume that all the bacteria are numbered from 1 to *n*. The bacteria of ty... | The first line contains three integers *n*,<=*m*,<=*k* (1<=≤<=*n*<=≤<=105; 0<=≤<=*m*<=≤<=105; 1<=≤<=*k*<=≤<=500). The next line contains *k* integers *c*1,<=*c*2,<=...,<=*c**k* (1<=≤<=*c**i*<=≤<=*n*). Each of the next *m* lines contains three integers *u**i*,<=*v**i*,<=*x**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=105; 0<=≤<=*x**i... | If Dima's type-distribution is correct, print string «Yes», and then *k* lines: in the *i*-th line print integers *d*[*i*][1],<=*d*[*i*][2],<=...,<=*d*[*i*][*k*] (*d*[*i*][*i*]<==<=0). If there is no way to move energy from bacteria *i* to bacteria *j* appropriate *d*[*i*][*j*] must equal to -1. If the type-distributio... | [
"4 4 2\n1 3\n2 3 0\n3 4 0\n2 4 1\n2 1 2\n",
"3 1 2\n2 1\n1 2 0\n",
"3 2 2\n2 1\n1 2 0\n2 3 1\n",
"3 0 2\n1 2\n"
] | [
"Yes\n0 2\n2 0\n",
"Yes\n0 -1\n-1 0\n",
"Yes\n0 1\n1 0\n",
"No\n"
] | none | [
{
"input": "4 4 2\n1 3\n2 3 0\n3 4 0\n2 4 1\n2 1 2",
"output": "Yes\n0 2\n2 0"
},
{
"input": "3 1 2\n2 1\n1 2 0",
"output": "Yes\n0 -1\n-1 0"
},
{
"input": "3 2 2\n2 1\n1 2 0\n2 3 1",
"output": "Yes\n0 1\n1 0"
},
{
"input": "3 0 2\n1 2",
"output": "No"
},
{
"input... | 1,700 | 75,161,600 | 0 | 20,561 | |
978 | Mentors | [
"binary search",
"data structures",
"implementation"
] | null | null | In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$.
A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel.
You are... | The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel.
The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{... | Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. | [
"4 2\n10 4 10 15\n1 2\n4 3\n",
"10 4\n5 4 1 5 4 3 7 1 2 5\n4 6\n2 1\n10 8\n3 5\n"
] | [
"0 0 1 2 \n",
"5 4 0 5 3 3 9 0 2 5 \n"
] | In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can b... | [
{
"input": "4 2\n10 4 10 15\n1 2\n4 3",
"output": "0 0 1 2 "
},
{
"input": "10 4\n5 4 1 5 4 3 7 1 2 5\n4 6\n2 1\n10 8\n3 5",
"output": "5 4 0 5 3 3 9 0 2 5 "
},
{
"input": "2 0\n3 1",
"output": "1 0 "
},
{
"input": "2 0\n1 1",
"output": "0 0 "
},
{
"input": "10 35... | 872 | 56,934,400 | 3 | 20,562 | |
725 | Hidden Word | [
"brute force",
"constructive algorithms",
"implementation",
"strings"
] | null | null | Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid:
We say that two tiles are adjacent if they share a side or a corn... | The only line of the input contains the string *s*, consisting of 27 upper-case English letters. Each English letter occurs at least once in *s*. | Output two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print "Impossible". | [
"ABCDEFGHIJKLMNOPQRSGTUVWXYZ\n",
"BUVTYZFQSNRIWOXXGJLKACPEMDH\n"
] | [
"YXWVUTGHIJKLM\nZABCDEFSRQPON\n",
"Impossible\n"
] | none | [
{
"input": "ABCDEFGHIJKLMNOPQRSGTUVWXYZ",
"output": "YXWVUTGHIJKLM\nZABCDEFSRQPON"
},
{
"input": "BUVTYZFQSNRIWOXXGJLKACPEMDH",
"output": "Impossible"
},
{
"input": "DYCEUXXKMGZOINVPHWQSRTABLJF",
"output": "Impossible"
},
{
"input": "UTEDBZRVWLOFUASHCYIPXGJMKNQ",
"output"... | 46 | 307,200 | -1 | 20,692 | |
356 | Bags and Coins | [
"bitmasks",
"constructive algorithms",
"dp",
"greedy"
] | null | null | When you were a child you must have been told a puzzle of bags and coins. Anyway, here's one of its versions:
A horse has three bags. The first bag has one coin, the second bag has one coin and the third bag has three coins. In total, the horse has three coins in the bags. How is that possible?
The answer is quite ... | The first line contains two integers *n* and *s* (1<=≤<=*n*,<=*s*<=≤<=70000) — the number of bags and the total number of coins. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=70000), where *a**i* shows the number of coins in the *i*-th bag. | If the answer doesn't exist, print -1.
Otherwise, print *n* lines, on the *i*-th line print the contents of the *i*-th bag. The first number in the line, *c**i* (0<=≤<=*c**i*<=≤<=*a**i*), must represent the number of coins lying directly in the *i*-th bag (the coins in the bags that are in the *i*-th bag are not take... | [
"3 3\n1 3 1\n",
"3 3\n1 3 1\n",
"1 2\n1\n",
"8 10\n2 7 3 4 1 3 1 2\n"
] | [
"1 0\n1 2 3 1\n1 0\n",
"1 0\n2 1 3\n0 1 1\n",
"-1\n",
"2 0\n1 2 1 4\n0 2 7 8\n0 2 5 6\n1 0\n3 0\n1 0\n2 0\n"
] | The pictures below show two possible ways to solve one test case from the statement. The left picture corresponds to the first test case, the right picture corresponds to the second one. | [] | 30 | 0 | 0 | 20,705 | |
645 | Intellectual Inquiry | [
"dp",
"greedy",
"strings"
] | null | null | After getting kicked out of her reporting job for not knowing the alphabet, Bessie has decided to attend school at the Fillet and Eggs Eater Academy. She has been making good progress with her studies and now knows the first *k* English letters.
Each morning, Bessie travels to school along a sidewalk consisting of *m*... | The first line of the input contains two integers *n* and *k* (0<=≤<=*n*<=≤<=1<=000<=000, 1<=≤<=*k*<=≤<=26).
The second line contains a string *t* (|*t*|<==<=*m*,<=1<=≤<=*m*<=≤<=1<=000<=000) consisting of only first *k* lowercase English letters. | Determine the maximum number of distinct subsequences Bessie can form after labeling the last *n* sidewalk tiles each with one of the first *k* lowercase English letters. Since this number can be rather large, you should print it modulo 109<=+<=7.
Please note, that you are not asked to maximize the remainder modulo 10... | [
"1 3\nac\n",
"0 2\naaba\n"
] | [
"8\n",
"10\n"
] | In the first sample, the optimal labeling gives 8 different subsequences: "" (the empty string), "a", "c", "b", "ac", "ab", "cb", and "acb".
In the second sample, the entire sidewalk is already labeled. The are 10 possible different subsequences: "" (the empty string), "a", "b", "aa", "ab", "ba", "aaa", "aab", "aba", ... | [] | 2,000 | 3,072,000 | 0 | 20,708 | |
0 | none | [
"none"
] | null | null | Dr. Moriarty is about to send a message to Sherlock Holmes. He has a string *s*.
String *p* is called a substring of string *s* if you can read it starting from some position in the string *s*. For example, string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba".
Dr. Moriarty plans to take string *s* and c... | The first line contains a non-empty string *s*, consisting of lowercase Latin letters. The second line contains a non-empty string *u*, consisting of lowercase Latin letters. The lengths of both strings are in the range from 1 to 2000, inclusive. | Print the only integer — the minimum number of changes that Dr. Moriarty has to make with the string that you choose. | [
"aaaaa\naaa\n",
"abcabc\nbcd\n",
"abcdef\nklmnopq\n"
] | [
"0\n",
"1\n",
"7\n"
] | In the first sample Moriarty can take any substring of length 3, and it will be equal to the required message *u*, so Moriarty won't have to make any changes.
In the second sample you should take a substring consisting of characters from second to fourth ("bca") or from fifth to sixth ("bc"). Then you will only have t... | [
{
"input": "aaaaa\naaa",
"output": "0"
},
{
"input": "abcabc\nbcd",
"output": "1"
},
{
"input": "abcdef\nklmnopq",
"output": "7"
},
{
"input": "aaabbbaaa\naba",
"output": "1"
},
{
"input": "a\na",
"output": "0"
},
{
"input": "z\nz",
"output": "0"
... | 2,000 | 4,608,000 | 0 | 20,716 | |
729 | Road to Cinema | [
"binary search",
"greedy",
"sortings"
] | null | null | Vasya is currently at a car rental service, and he wants to reach cinema. The film he has bought a ticket for starts in *t* minutes. There is a straight road of length *s* from the service to the cinema. Let's introduce a coordinate system so that the car rental service is at the point 0, and the cinema is at the point... | The first line contains four positive integers *n*, *k*, *s* and *t* (1<=≤<=*n*<=≤<=2·105, 1<=≤<=*k*<=≤<=2·105, 2<=≤<=*s*<=≤<=109, 1<=≤<=*t*<=≤<=2·109) — the number of cars at the car rental service, the number of gas stations along the road, the length of the road and the time in which the film starts.
Each of the n... | Print the minimum rent price of an appropriate car, i.e. such car that Vasya will be able to reach the cinema before the film starts (not later than in *t* minutes). If there is no appropriate car, print -1. | [
"3 1 8 10\n10 8\n5 7\n11 9\n3\n",
"2 2 10 18\n10 4\n20 6\n5 3\n"
] | [
"10\n",
"20\n"
] | In the first sample, Vasya can reach the cinema in time using the first or the third cars, but it would be cheaper to choose the first one. Its price is equal to 10, and the capacity of its fuel tank is 8. Then Vasya can drive to the first gas station in the accelerated mode in 3 minutes, spending 6 liters of fuel. Aft... | [
{
"input": "3 1 8 10\n10 8\n5 7\n11 9\n3",
"output": "10"
},
{
"input": "2 2 10 18\n10 4\n20 6\n5 3",
"output": "20"
},
{
"input": "2 1 1000000000 2000000000\n111 1000000000\n101 1000000000\n5",
"output": "101"
},
{
"input": "2 1 1000000000 2000000000\n111 999999998\n101 9999... | 46 | 204,800 | 0 | 20,732 | |
125 | MST Company | [
"binary search",
"graphs"
] | null | null | The MST (Meaningless State Team) company won another tender for an important state reform in Berland.
There are *n* cities in Berland, some pairs of the cities are connected by roads. Each road has its price. One can move along any road in any direction. The MST team should carry out the repair works on some set of ro... | The first input line contains three integers *n*,<=*m*,<=*k* (1<=≤<=*n*<=≤<=5000;0<=≤<=*m*<=≤<=105;0<=≤<=*k*<=<<=5000), where *n* is the number of cities in the country, *m* is the number of roads in the country, *k* is the number of capital roads in the required set. Then *m* lines enumerate the roads in question. ... | In the first line print the number of roads in the required set. The second line should contain the numbers of roads included in the sought set. If the sought set does not exist, print -1. | [
"4 5 2\n1 2 1\n2 3 1\n3 4 1\n1 3 3\n1 4 2\n"
] | [
"3\n1 5 2 "
] | none | [] | 62 | 0 | -1 | 20,746 | |
58 | Expression | [
"dp"
] | E. Expression | 2 | 256 | One day Vasya was solving arithmetical problems. He wrote down an expression *a*<=+<=*b*<==<=*c* in his notebook. When the teacher checked Vasya's work it turned out that Vasya had solved the problem incorrectly. Now Vasya tries to find excuses. He says that he simply forgot to write down several digits in numbers *a*,... | The first and only input line contains the expression *a*<=+<=*b*<==<=*c* (1<=≤<=*a*,<=*b*,<=*c*<=≤<=106, *a*, *b* and *c* don't contain leading zeroes) which is the expression Vasya wrote down. | Print the correct expression *x*<=+<=*y*<==<=*z* (*x*, *y* and *z* are non-negative numbers without leading zeroes). The expression *a*<=+<=*b*<==<=*c* must be met in *x*<=+<=*y*<==<=*z* as a subsequence. The printed solution should have the minimal possible number of characters. If there are several such solutions, yo... | [
"2+4=5\n",
"1+1=3\n",
"1+1=2\n"
] | [
"21+4=25\n",
"1+31=32\n",
"1+1=2\n"
] | none | [
{
"input": "2+4=5",
"output": "21+4=25"
},
{
"input": "1+1=3",
"output": "1+31=32"
},
{
"input": "1+1=2",
"output": "1+1=2"
},
{
"input": "323+123=287",
"output": "323+12553=12876"
},
{
"input": "878347+637216=542650",
"output": "847834370+6372160=854206530"
... | 278 | 8,294,400 | 0 | 20,773 |
309 | Sheep | [
"binary search",
"greedy"
] | null | null | Information technologies are developing and are increasingly penetrating into all spheres of human activity. Incredible as it is, the most modern technology are used in farming!
A large farm has a meadow with grazing sheep. Overall there are *n* sheep and each of them contains a unique number from 1 to *n* — because t... | The first input line contains one integer *n* (1<=≤<=*n*<=≤<=2000). Each of the following *n* lines contains two integers *l**i* and *r**i* (1<=≤<=*l**i*,<=*r**i*<=≤<=109; *l**i*<=≤<=*r**i*). | In the single output line print *n* space-separated numbers — the sought arrangement of the sheep. The *i*-th value in the line must represent the number of the sheep that took the *i*-th place from left in the optimal arrangement line.
If there are multiple optimal arrangements, print any of them. | [
"3\n1 3\n5 7\n2 4\n",
"5\n1 5\n2 4\n3 6\n1 7\n2 6\n",
"4\n1 3\n4 6\n5 7\n2 3\n"
] | [
"1 3 2",
"2 1 3 5 4",
"1 4 2 3"
] | none | [] | 31 | 6,656,000 | 0 | 20,785 | |
543 | Road Improvement | [
"dp",
"trees"
] | null | null | The country has *n* cities and *n*<=-<=1 bidirectional roads, it is possible to get from every city to any other one if you move only along the roads. The cities are numbered with integers from 1 to *n* inclusive.
All the roads are initially bad, but the government wants to improve the state of some roads. We will ass... | The first line of the input contains a single integer *n* (2<=≤<=*n*<=≤<=2·105) — the number of cities in the country. Next line contains *n*<=-<=1 positive integers *p*2,<=*p*3,<=*p*4,<=...,<=*p**n* (1<=≤<=*p**i*<=≤<=*i*<=-<=1) — the description of the roads in the country. Number *p**i* means that the country has a r... | Print *n* integers *a*1,<=*a*2,<=...,<=*a**n*, where *a**i* is the sought number of ways to improve the quality of the roads modulo 1<=000<=000<=007 (109<=+<=7), if the capital of the country is at city number *i*. | [
"3\n1 1\n",
"5\n1 2 3 4\n"
] | [
"4 3 3",
"5 8 9 8 5"
] | none | [
{
"input": "3\n1 1",
"output": "4 3 3"
},
{
"input": "5\n1 2 3 4",
"output": "5 8 9 8 5"
},
{
"input": "31\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
"output": "73741817 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 5368... | 452 | 21,913,600 | 3 | 20,900 | |
68 | Contact | [
"geometry"
] | E. Contact | 3 | 256 | Little Petya is preparing for the first contact with aliens. He knows that alien spaceships have shapes of non-degenerate triangles and there will be exactly 4 ships. Landing platform for a ship can be made of 3 special columns located at some points of a Cartesian plane such that these 3 points form a triangle equal t... | Each of 4 lines will contain 6 integers *x*1 *y*1 *x*2 *y*2 *x*3 *y*3 (0<=≤<=*x*1,<=*y*1,<=*x*2,<=*y*2,<=*x*3,<=*y*3<=≤<=20), representing 3 points that describe the shape of each of 4 ships. It is guaranteed that 3 points in each line will represent a non-degenerate triangle. | First line should contain minimum number of columns enough to land all spaceships. | [
"0 0 1 0 1 2\n0 0 0 2 2 2\n0 0 3 0 1 2\n0 0 3 0 2 2\n",
"0 0 0 1 1 1\n0 0 0 2 2 2\n0 0 0 5 5 5\n0 0 0 17 17 17\n"
] | [
"4\n",
"9\n"
] | In the first test case columns can be put in these points: (0, 0), (1, 0), (3, 0), (1, 2). Note that the second ship can land using last 3 columns.
In the second test case following points can be chosen: (0, 0), (0, 1), (1, 0), (0, 2), (2, 0), (0, 5), (5, 0), (0, 17), (17, 0). It is impossible to use less than 9 colum... | [] | 62 | 0 | 0 | 20,949 |
955 | Scissors | [
"brute force",
"strings"
] | null | null | Jenya has recently acquired quite a useful tool — *k*-scissors for cutting strings. They are generally used for cutting out two non-intersecting substrings of length *k* from an arbitrary string *s* (its length should be at least 2·*k* in order to perform this operation) and concatenating them afterwards (preserving th... | The first line contains three integers *n*, *m*, *k* (2<=≤<=*m*<=≤<=2·*k*<=≤<=*n*<=≤<=5·105) — length of *s*, length of *t* and the aforementioned scissors' parameter correspondingly.
The next two lines feature *s* and *t* consisting of lowercase latin letters. | If there is no answer, print «No».
Otherwise print «Yes» and two integers *L* and *R* denoting the indexes where cutted substrings start (1-indexed). If there are several possible answers, output any. | [
"7 4 3\nbaabaab\naaaa\n",
"6 3 2\ncbcbcb\nbcc\n",
"7 5 3\naabbaaa\naaaaa\n"
] | [
"Yes\n1 5\n",
"Yes\n2 5\n",
"No\n"
] | In the first sample case you can cut out two substrings starting at 1 and 5. The resulting string baaaab contains aaaa as a substring.
In the second sample case the resulting string is bccb. | [
{
"input": "7 4 3\nbaabaab\naaaa",
"output": "Yes\n1 5"
},
{
"input": "6 3 2\ncbcbcb\nbcc",
"output": "Yes\n2 5"
},
{
"input": "7 5 3\naabbaaa\naaaaa",
"output": "No"
},
{
"input": "16 6 4\nabcdcadbbbcacdca\ndcadbc",
"output": "Yes\n4 10"
},
{
"input": "81 5 3\nac... | 62 | 7,065,600 | 0 | 20,960 | |
500 | New Year Permutation | [
"dfs and similar",
"dsu",
"graphs",
"greedy",
"math",
"sortings"
] | null | null | User ainta has a permutation *p*1,<=*p*2,<=...,<=*p**n*. As the New Year is coming, he wants to make his permutation as pretty as possible.
Permutation *a*1,<=*a*2,<=...,<=*a**n* is prettier than permutation *b*1,<=*b*2,<=...,<=*b**n*, if and only if there exists an integer *k* (1<=≤<=*k*<=≤<=*n*) where *a*1<==<=*b*1,... | The first line contains an integer *n* (1<=≤<=*n*<=≤<=300) — the size of the permutation *p*.
The second line contains *n* space-separated integers *p*1,<=*p*2,<=...,<=*p**n* — the permutation *p* that user ainta has. Each integer between 1 and *n* occurs exactly once in the given permutation.
Next *n* lines describe... | In the first and only line, print *n* space-separated integers, describing the prettiest permutation that can be obtained. | [
"7\n5 2 4 3 6 7 1\n0001001\n0000000\n0000010\n1000001\n0000000\n0010000\n1001000\n",
"5\n4 2 1 5 3\n00100\n00011\n10010\n01101\n01010\n"
] | [
"1 2 4 3 6 7 5\n",
"1 2 3 4 5\n"
] | In the first sample, the swap needed to obtain the prettiest permutation is: (*p*<sub class="lower-index">1</sub>, *p*<sub class="lower-index">7</sub>).
In the second sample, the swaps needed to obtain the prettiest permutation is (*p*<sub class="lower-index">1</sub>, *p*<sub class="lower-index">3</sub>), (*p*<sub cla... | [
{
"input": "7\n5 2 4 3 6 7 1\n0001001\n0000000\n0000010\n1000001\n0000000\n0010000\n1001000",
"output": "1 2 4 3 6 7 5"
},
{
"input": "5\n4 2 1 5 3\n00100\n00011\n10010\n01101\n01010",
"output": "1 2 3 4 5"
},
{
"input": "7\n1 7 6 4 2 3 5\n0000100\n0000010\n0000001\n0000000\n1000000\n010... | 46 | 0 | 0 | 20,986 | |
235 | Let's Play Osu! | [
"dp",
"math",
"probabilities"
] | null | null | You're playing a game called Osu! Here's a simplified version of it. There are *n* clicks in a game. For each click there are two outcomes: correct or bad. Let us denote correct as "O", bad as "X", then the whole play can be encoded as a sequence of *n* characters "O" and "X".
Using the play sequence you can calculate... | The first line contains an integer *n* (1<=≤<=*n*<=≤<=105) — the number of clicks. The second line contains *n* space-separated real numbers *p*1,<=*p*2,<=...,<=*p**n* (0<=≤<=*p**i*<=≤<=1).
There will be at most six digits after the decimal point in the given *p**i*. | Print a single real number — the expected score for your play. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. | [
"3\n0.5 0.5 0.5\n",
"4\n0.7 0.2 0.1 0.9\n",
"5\n1 1 1 1 1\n"
] | [
"2.750000000000000\n",
"2.489200000000000\n",
"25.000000000000000\n"
] | For the first example. There are 8 possible outcomes. Each has a probability of 0.125.
- "OOO" → 3<sup class="upper-index">2</sup> = 9; - "OOX" → 2<sup class="upper-index">2</sup> = 4; - "OXO" → 1<sup class="upper-index">2</sup> + 1<sup class="upper-index">2</sup> = 2; - "OXX" → 1<sup class="upper-index">2... | [
{
"input": "3\n0.5 0.5 0.5",
"output": "2.750000000000000"
},
{
"input": "4\n0.7 0.2 0.1 0.9",
"output": "2.489200000000000"
},
{
"input": "5\n1 1 1 1 1",
"output": "25.000000000000000"
},
{
"input": "10\n0.684846 0.156794 0.153696 0.714526 0.281868 0.628256 0.745339 0.123854... | 342 | 11,776,000 | 3 | 21,000 | |
132 | Constants in the language of Shakespeare | [
"constructive algorithms",
"dp",
"greedy"
] | null | null | Shakespeare is a widely known esoteric programming language in which programs look like plays by Shakespeare, and numbers are given by combinations of ornate epithets. In this problem we will have a closer look at the way the numbers are described in Shakespeare.
Each constant in Shakespeare is created from non-negati... | The only line of input contains a positive integer *n*, written as its binary notation. The length of the notation is at most 106. The first digit of the notation is guaranteed to be 1. | Output the required minimal *m*. After it output *m* lines. Each line has to be formatted as "+2^x" or "-2^x", where *x* is the power coefficient of the corresponding term. The order of the lines doesn't matter. | [
"1111\n",
"1010011\n"
] | [
"2\n+2^4\n-2^0\n",
"4\n+2^0\n+2^1\n+2^4\n+2^6\n"
] | none | [
{
"input": "1111",
"output": "2\n+2^4\n-2^0"
},
{
"input": "1010011",
"output": "4\n+2^0\n+2^1\n+2^4\n+2^6"
},
{
"input": "1",
"output": "1\n+2^0"
},
{
"input": "10110111",
"output": "4\n+2^8\n-2^6\n-2^3\n-2^0"
},
{
"input": "10101110",
"output": "4\n+2^8\n-2^... | 46 | 0 | 0 | 21,040 | |
0 | none | [
"none"
] | null | null | На тренировку по подготовке к соревнованиям по программированию пришли *n* команд. Тренер для каждой команды подобрал тренировку, комплект задач для *i*-й команды занимает *a**i* страниц. В распоряжении тренера есть *x* листов бумаги, у которых обе стороны чистые, и *y* листов, у которых только одна сторона чистая. При... | В первой строке входных данных следуют три целых числа *n*, *x* и *y* (1<=≤<=*n*<=≤<=200<=000, 0<=≤<=*x*,<=*y*<=≤<=109) — количество команд, количество листов бумаги с двумя чистыми сторонами и количество листов бумаги с одной чистой стороной.
Во второй строке входных данных следует последовательность из *n* целых чис... | Выведите единственное целое число — максимальное количество команд, которым тренер сможет напечатать комплекты задач целиком. | [
"2 3 5\n4 6\n",
"2 3 5\n4 7\n",
"6 3 5\n12 11 12 11 12 11\n"
] | [
"2\n",
"2\n",
"1\n"
] | В первом тестовом примере можно напечатать оба комплекта задач. Один из возможных ответов — напечатать весь первый комплект задач на листах с одной чистой стороной (после этого останется 3 листа с двумя чистыми сторонами и 1 лист с одной чистой стороной), а второй комплект напечатать на трех листах с двумя чистыми стор... | [
{
"input": "2 3 5\n4 6",
"output": "2"
},
{
"input": "2 3 5\n4 7",
"output": "2"
},
{
"input": "6 3 5\n12 11 12 11 12 11",
"output": "1"
},
{
"input": "1 4 3\n12",
"output": "0"
},
{
"input": "10 1 17\n3 1 1 2 1 3 4 4 1 4",
"output": "8"
},
{
"input": ... | 61 | 4,608,000 | 0 | 21,044 | |
236 | Easy Number Challenge | [
"implementation",
"number theory"
] | null | null | Let's denote *d*(*n*) as the number of divisors of a positive integer *n*. You are given three integers *a*, *b* and *c*. Your task is to calculate the following sum:
Find the sum modulo 1073741824 (230). | The first line contains three space-separated integers *a*, *b* and *c* (1<=≤<=*a*,<=*b*,<=*c*<=≤<=100). | Print a single integer — the required sum modulo 1073741824 (230). | [
"2 2 2\n",
"5 6 7\n"
] | [
"20\n",
"1520\n"
] | For the first example.
- *d*(1·1·1) = *d*(1) = 1; - *d*(1·1·2) = *d*(2) = 2; - *d*(1·2·1) = *d*(2) = 2; - *d*(1·2·2) = *d*(4) = 3; - *d*(2·1·1) = *d*(2) = 2; - *d*(2·1·2) = *d*(4) = 3; - *d*(2·2·1) = *d*(4) = 3; - *d*(2·2·2) = *d*(8) = 4.
So the result is 1 + 2 + 2 + 3 + 2 + 3 + 3 + 4 = 20. | [
{
"input": "2 2 2",
"output": "20"
},
{
"input": "5 6 7",
"output": "1520"
},
{
"input": "91 42 25",
"output": "3076687"
},
{
"input": "38 47 5",
"output": "160665"
},
{
"input": "82 29 45",
"output": "3504808"
},
{
"input": "40 15 33",
"output": "... | 2,000 | 0 | 0 | 21,139 | |
183 | Candy Shop | [
"greedy"
] | null | null | The prestigious Codeforces kindergarten consists of *n* kids, numbered 1 through *n*. Each of them are given allowance in rubles by their parents.
Today, they are going to the most famous candy shop in the town. The shop sells candies in packages: for all *i* between 1 and *m*, inclusive, it sells a package containing... | The first line contains two space-separated integers *n* and *m* (2<=≤<=*n*<=≤<=2·105,<=2<=≤<=*m*<=≤<=5·106,<=*n*<=≤<=*m*), denoting the number of kids and the maximum number of candies in a package sold by the candy shop, respectively.
Then *n* lines follow, each line will contain a single positive integer not exceed... | Print a single integer denoting the maximum number of candies that can be sold by the candy shop. | [
"2 5\n5\n10\n",
"3 8\n8\n16\n13\n",
"2 5000000\n12500002500000\n12500002500000\n"
] | [
"13\n",
"32\n",
"12500002500000\n"
] | For the first example, one of the scenarios that will result in 13 purchased candies is as follows.
- Turn 1. Kid 1 purchases 1 candy. - Turn 2. Kid 2 purchases 3 candies. - Turn 3. Kid 1 purchases 4 candies. - Turn 4. Kid 2 purchases 5 candies. | [] | 92 | 0 | 0 | 21,218 | |
375 | Tree and Queries | [
"data structures",
"dfs and similar",
"trees"
] | null | null | You have a rooted tree consisting of *n* vertices. Each vertex of the tree has some color. We will assume that the tree vertices are numbered by integers from 1 to *n*. Then we represent the color of vertex *v* as *c**v*. The tree root is a vertex with number 1.
In this problem you need to answer to *m* queries. Each ... | The first line contains two integers *n* and *m* (2<=≤<=*n*<=≤<=105; 1<=≤<=*m*<=≤<=105). The next line contains a sequence of integers *c*1,<=*c*2,<=...,<=*c**n* (1<=≤<=*c**i*<=≤<=105). The next *n*<=-<=1 lines contain the edges of the tree. The *i*-th line contains the numbers *a**i*,<=*b**i* (1<=≤<=*a**i*,<=*b**i*<=≤... | Print *m* integers — the answers to the queries in the order the queries appear in the input. | [
"8 5\n1 2 2 3 3 2 3 3\n1 2\n1 5\n2 3\n2 4\n5 6\n5 7\n5 8\n1 2\n1 3\n1 4\n2 3\n5 3\n",
"4 1\n1 2 3 4\n1 2\n2 3\n3 4\n1 1\n"
] | [
"2\n2\n1\n0\n1\n",
"4\n"
] | A subtree of vertex *v* in a rooted tree with root *r* is a set of vertices {*u* : *dist*(*r*, *v*) + *dist*(*v*, *u*) = *dist*(*r*, *u*)}. Where *dist*(*x*, *y*) is the length (in edges) of the shortest path between vertices *x* and *y*. | [
{
"input": "8 5\n1 2 2 3 3 2 3 3\n1 2\n1 5\n2 3\n2 4\n5 6\n5 7\n5 8\n1 2\n1 3\n1 4\n2 3\n5 3",
"output": "2\n2\n1\n0\n1"
},
{
"input": "4 1\n1 2 3 4\n1 2\n2 3\n3 4\n1 1",
"output": "4"
}
] | 1,000 | 102,400 | 0 | 21,223 | |
838 | Binary Blocks | [
"brute force"
] | null | null | You are given an image, that can be represented with a 2-d *n* by *m* grid of pixels. Each pixel of the image is either on or off, denoted by the characters "0" or "1", respectively. You would like to compress this image. You want to choose an integer *k*<=><=1 and split the image into *k* by *k* blocks. If *n* and ... | The first line of input will contain two integers *n*,<=*m* (2<=≤<=*n*,<=*m*<=≤<=2<=500), the dimensions of the image.
The next *n* lines of input will contain a binary string with exactly *m* characters, representing the image. | Print a single integer, the minimum number of pixels needed to toggle to make the image compressible. | [
"3 5\n00100\n10110\n11001\n"
] | [
"5\n"
] | We first choose *k* = 2.
The image is padded as follows:
We can toggle the image to look as follows:
We can see that this image is compressible for *k* = 2. | [
{
"input": "3 5\n00100\n10110\n11001",
"output": "5"
}
] | 2,000 | 29,491,200 | 0 | 21,235 | |
0 | none | [
"none"
] | null | null | Leha plays a computer game, where is on each level is given a connected graph with *n* vertices and *m* edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer *d**i*, which can be equal to 0, 1 or <=-<=1. To pass the level, he needs to find a «good» subset of edges of the gr... | The first line contains two integers *n*, *m* (1<=≤<=*n*<=≤<=3·105, *n*<=-<=1<=≤<=*m*<=≤<=3·105) — number of vertices and edges.
The second line contains *n* integers *d*1,<=*d*2,<=...,<=*d**n* (<=-<=1<=≤<=*d**i*<=≤<=1) — numbers on the vertices.
Each of the next *m* lines contains two integers *u* and *v* (1<=≤<=*u*... | Print <=-<=1 in a single line, if solution doesn't exist. Otherwise in the first line *k* — number of edges in a subset. In the next *k* lines indexes of edges. Edges are numerated in order as they are given in the input, starting from 1. | [
"1 0\n1\n",
"4 5\n0 0 0 -1\n1 2\n2 3\n3 4\n1 4\n2 4\n",
"2 1\n1 1\n1 2\n",
"3 3\n0 -1 1\n1 2\n2 3\n1 3\n"
] | [
"-1\n",
"0\n",
"1\n1\n",
"1\n2\n"
] | In the first sample we have single vertex without edges. It's degree is 0 and we can not get 1. | [
{
"input": "1 0\n1",
"output": "-1"
},
{
"input": "4 5\n0 0 0 -1\n1 2\n2 3\n3 4\n1 4\n2 4",
"output": "0"
},
{
"input": "2 1\n1 1\n1 2",
"output": "1\n1"
},
{
"input": "3 3\n0 -1 1\n1 2\n2 3\n1 3",
"output": "1\n2"
},
{
"input": "10 10\n-1 -1 -1 -1 -1 -1 -1 -1 -1 ... | 280 | 15,872,000 | 0 | 21,283 | |
632 | Alice, Bob, Two Teams | [
"brute force",
"constructive algorithms"
] | null | null | Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are *n* pieces, and the *i*-th piece has a strength *p**i*.
The way to split up game pieces is split into several steps:
1. First, Alice will split the pieces into two different groups *A* and *B*. This can be seen as ... | The first line contains integer *n* (1<=≤<=*n*<=≤<=5·105) — the number of game pieces.
The second line contains *n* integers *p**i* (1<=≤<=*p**i*<=≤<=109) — the strength of the *i*-th piece.
The third line contains *n* characters *A* or *B* — the assignment of teams after the first step (after Alice's step). | Print the only integer *a* — the maximum strength Bob can achieve. | [
"5\n1 2 3 4 5\nABABA\n",
"5\n1 2 3 4 5\nAAAAA\n",
"1\n1\nB\n"
] | [
"11\n",
"15\n",
"1\n"
] | In the first sample Bob should flip the suffix of length one.
In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5.
In the third sample Bob should do nothing. | [
{
"input": "5\n1 2 3 4 5\nABABA",
"output": "11"
},
{
"input": "5\n1 2 3 4 5\nAAAAA",
"output": "15"
},
{
"input": "1\n1\nB",
"output": "1"
},
{
"input": "10\n1 9 7 6 2 4 7 8 1 3\nABBABAABBB",
"output": "33"
},
{
"input": "100\n591 417 888 251 792 847 685 3 182 46... | 1,356 | 43,315,200 | 3 | 21,305 | |
39 | C*++ Calculations | [
"expression parsing",
"greedy"
] | A. C*++ Calculations | 2 | 64 | C*++ language is quite similar to C++. The similarity manifests itself in the fact that the programs written in C*++ sometimes behave unpredictably and lead to absolutely unexpected effects. For example, let's imagine an arithmetic expression in C*++ that looks like this (*expression* is the main term):
- *expression... | The first input line contains an integer *a* (<=-<=1000<=≤<=*a*<=≤<=1000) — the initial value of the variable "a". The next line contains an expression in C*++ language of the described type. The number of the summands in the expression does not exceed 1000. It is guaranteed that the line describing the expression cont... | Output a single number — the maximal possible value of the expression. | [
"1\n5*a++-3*++a+a++\n",
"3\na+++++a\n"
] | [
"11\n",
"8\n"
] | Consider the second example. Initially *a* = 3. Suppose that at first the first summand is calculated, and then the second one is. The first summand gets equal to 3, and the value of *a* is increased by 1. At the calculation of the second summand *a* is increased once more (gets equal to 5). The value of the second sum... | [
{
"input": "1\n5*a++-3*++a+a++",
"output": "11"
},
{
"input": "3\na+++++a",
"output": "8"
},
{
"input": "-668\n820*a+++402*++a-482*++a",
"output": "-492358"
},
{
"input": "902\n600*++a+411*a+++20*a++-340*++a-306*++a+485*a++-776*a+++417*a+++70*a++-703*a++",
"output": "-972... | 0 | 0 | -1 | 21,456 |
743 | Chloe and pleasant prizes | [
"dfs and similar",
"dp",
"graphs",
"trees"
] | null | null | Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took *n* prizes for the contestants and wrote on each of them a unique id (integer... | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=2·105) — the number of gifts.
The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=≤<=*a**i*<=≤<=109) — the pleasantness of the gifts.
The next (*n*<=-<=1) lines contain two numbers each. The *i*-th of these lines contains integers *u**i... | If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer — the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible. | [
"8\n0 5 -1 4 3 2 6 5\n1 2\n2 4\n2 5\n1 3\n3 6\n6 7\n6 8\n",
"4\n1 -5 1 1\n1 2\n1 4\n2 3\n",
"1\n-1\n"
] | [
"25",
"2",
"Impossible"
] | none | [
{
"input": "8\n0 5 -1 4 3 2 6 5\n1 2\n2 4\n2 5\n1 3\n3 6\n6 7\n6 8",
"output": "25"
},
{
"input": "4\n1 -5 1 1\n1 2\n1 4\n2 3",
"output": "2"
},
{
"input": "1\n-1",
"output": "Impossible"
},
{
"input": "10\n3 2 -4 2 0 3 3 3 3 4\n10 8\n4 2\n4 9\n3 5\n5 2\n7 4\n2 6\n1 8\n10 9",... | 31 | 0 | 0 | 21,461 | |
49 | Game | [
"brute force",
"dp",
"implementation"
] | D. Game | 2 | 256 | Vasya and Petya have invented a new game. Vasya takes a stripe consisting of 1<=×<=*n* square and paints the squares black and white. After that Petya can start moves — during a move he may choose any two neighboring squares of one color and repaint these two squares any way he wants, perhaps in different colors. Petya... | The first line contains number *n* (1<=≤<=*n*<=≤<=1000) which represents the stripe’s length. The second line contains exactly *n* symbols — the line’s initial coloring. 0 corresponds to a white square, 1 corresponds to a black one. | If Petya cannot win with such an initial coloring, print -1. Otherwise print the minimum number of moves Petya needs to win. | [
"6\n111010\n",
"5\n10001\n",
"7\n1100010\n",
"5\n00100\n"
] | [
"1\n",
"1\n",
"2\n",
"2\n"
] | In the first sample Petya can take squares 1 and 2. He repaints square 1 to black and square 2 to white.
In the second sample Petya can take squares 2 and 3. He repaints square 2 to white and square 3 to black. | [
{
"input": "6\n111010",
"output": "1"
},
{
"input": "5\n10001",
"output": "1"
},
{
"input": "7\n1100010",
"output": "2"
},
{
"input": "5\n00100",
"output": "2"
},
{
"input": "3\n101",
"output": "0"
},
{
"input": "6\n111111",
"output": "3"
},
{
... | 124 | 4,608,000 | 3.960417 | 21,619 |
592 | BCPC | [
"binary search",
"geometry",
"two pointers"
] | null | null | BCPC stands for Byteforces Collegiate Programming Contest, and is the most famous competition in Byteforces.
BCPC is a team competition. Each team is composed by a coach and three contestants. Blenda is the coach of the Bit State University(BSU), and she is very strict selecting the members of her team.
In BSU there ... | In the first line of the input three integers *n*, *c* and *d* (3<=≤<=*n*<=≤<=345678,<=1<=≤<=*c*,<=*d*<=≤<=109) are written. They denote the number of students Blenda can use to form teams, the value subtracted from all reading speeds and the value subtracted from all writing speeds respectively.
Each of the next *n* ... | Print the number of different teams in BSU, that are good according to Blenda's definition. | [
"5 2 2\n1 1\n4 1\n2 3\n3 2\n3 4\n",
"7 6 6\n3 2\n1 7\n5 7\n3 7\n6 4\n8 9\n8 5\n"
] | [
"4\n",
"11\n"
] | In the first sample the following teams are good: (*i* = 1, *j* = 2, *k* = 3), (*i* = 2, *j* = 5, *k* = 1), (*i* = 1, *j* = 4, *k* = 3), (*i* = 5, *j* = 1, *k* = 4).
Note, that for example the team (*i* = 3, *j* = 1, *k* = 2) is also good, but is considered to be the same as the team (*i* = 1, *j* = 2, *k* = 3). | [] | 30 | 204,800 | -1 | 21,646 | |
0 | none | [
"none"
] | null | null | All Berland residents are waiting for an unprecedented tour of wizard in his Blue Helicopter over the cities of Berland!
It is well-known that there are *n* cities in Berland, some pairs of which are connected by bidirectional roads. Each pair of cities is connected by no more than one road. It is not guaranteed that ... | The first line contains two integers *n*, *m* (1<=≤<=*n*<=≤<=2·105, 0<=≤<=*m*<=≤<=2·105) — the number of cities and the number of roads in Berland, respectively.
The roads description follow, one in each line. Each description is a pair of two integers *a**i*,<=*b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*, *a**i*<=≠<=*b**i*)... | In the first line print *w* — the maximum possible number of episodes. The next *w* lines should contain the episodes in format *x*, *y*, *z* — the three integers denoting the ids of the cities in the order of the wizard's visits. | [
"4 5\n1 2\n3 2\n2 4\n3 4\n4 1\n",
"5 8\n5 3\n1 2\n4 5\n5 1\n2 5\n4 3\n1 4\n3 2\n"
] | [
"2\n1 4 2\n4 3 2\n",
"4\n1 4 5\n2 3 4\n1 5 3\n5 2 1\n"
] | none | [
{
"input": "4 5\n1 2\n3 2\n2 4\n3 4\n4 1",
"output": "2\n1 4 2\n4 3 2"
},
{
"input": "5 8\n5 3\n1 2\n4 5\n5 1\n2 5\n4 3\n1 4\n3 2",
"output": "4\n1 4 5\n2 3 4\n1 5 3\n5 2 1"
},
{
"input": "1 0",
"output": "0"
},
{
"input": "2 1\n1 2",
"output": "0"
},
{
"input": "... | 1,684 | 133,632,000 | -1 | 21,706 | |
370 | Summer Reading | [
"dp",
"greedy"
] | null | null | At school Vasya got an impressive list of summer reading books. Unlike other modern schoolchildren, Vasya loves reading, so he read some book each day of the summer.
As Vasya was reading books, he was making notes in the Reader's Diary. Each day he wrote the orderal number of the book he was reading. The books in the ... | The first line contains integer *n* — the number of summer days (2<=≤<=*n*<=≤<=2·105). The second line contains *n* integers *a*1,<=*a*2,<=... *a**n* — the records in the diary in the order they were written (0<=≤<=*a**i*<=≤<=105). If Vasya forgot to write the number of the book on the *i*-th day, then *a**i* equals 0. | If it is impossible to correctly fill the blanks in the diary (the diary may contain mistakes initially), print "-1".
Otherwise, print in the first line the maximum number of books Vasya could have read in the summer if we stick to the diary. In the second line print *n* integers — the diary with correctly inserted r... | [
"7\n0 1 0 0 0 3 0\n",
"8\n0 0 0 0 0 0 0 0\n",
"4\n0 0 1 0\n",
"4\n0 0 0 3\n"
] | [
"3\n1 1 2 2 3 3 3 \n",
"4\n1 1 2 2 3 3 4 4 \n",
"1\n1 1 1 1 \n",
"-1\n"
] | none | [] | 31 | 0 | 0 | 21,731 | |
0 | none | [
"none"
] | null | null | Old MacDonald has a farm and a large potato field, (1010<=+<=1)<=×<=(1010<=+<=1) square meters in size. The field is divided into square garden beds, each bed takes up one square meter.
Old McDonald knows that the Colorado potato beetle is about to invade his farm and can destroy the entire harvest. To fight the insec... | The first line contains an integer *n* (1<=≤<=*n*<=≤<=1000) — the number of Old McDonald's movements.
Next *n* lines contain the description of Old McDonald's movements. The *i*-th of these lines describes the *i*-th movement. Each movement is given in the format "*d**i* *x**i*", where *d**i* is the character that det... | Print a single integer — the number of beds that won't be infected by the Colorado potato beetle.
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. | [
"5\nR 8\nU 9\nL 9\nD 8\nL 2\n",
"7\nR 10\nD 2\nL 7\nU 9\nD 2\nR 3\nD 10\n"
] | [
"101",
"52"
] | none | [] | 60 | 0 | 0 | 21,735 | |
335 | Buy One, Get One Free | [
"dp",
"greedy"
] | null | null | A nearby pie shop is having a special sale. For each pie you pay full price for, you may select one pie of a strictly lesser value to get for free. Given the prices of all the pies you wish to acquire, determine the minimum total amount you must pay for all of the pies. | Input will begin with an integer *n* (1<=≤<=*n*<=≤<=500000), the number of pies you wish to acquire. Following this is a line with *n* integers, each indicating the cost of a pie. All costs are positive integers not exceeding 109. | Print the minimum cost to acquire all the pies.
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. | [
"6\n3 4 5 3 4 5\n",
"5\n5 5 5 5 5\n",
"4\n309999 6000 2080 2080\n"
] | [
"14\n",
"25\n",
"314159\n"
] | In the first test case you can pay for a pie with cost 5 and get a pie with cost 4 for free, then pay for a pie with cost 5 and get a pie with cost 3 for free, then pay for a pie with cost 4 and get a pie with cost 3 for free.
In the second test case you have to pay full price for every pie. | [
{
"input": "6\n3 4 5 3 4 5",
"output": "14"
},
{
"input": "5\n5 5 5 5 5",
"output": "25"
},
{
"input": "4\n309999 6000 2080 2080",
"output": "314159"
},
{
"input": "10\n1 1 1 1 1 2 3 4 5 6",
"output": "16"
},
{
"input": "1\n1",
"output": "1"
},
{
"inpu... | 124 | 307,200 | 0 | 21,822 | |
507 | Guess Your Way Out! | [
"implementation",
"math",
"trees"
] | null | null | Amr bought a new video game "Guess Your Way Out!". The goal of the game is to find an exit from the maze that looks like a perfect binary tree of height *h*. The player is initially standing at the root of the tree and the exit from the tree is located at some leaf node.
Let's index all the leaf nodes from the left t... | Input consists of two integers *h*,<=*n* (1<=≤<=*h*<=≤<=50, 1<=≤<=*n*<=≤<=2*h*). | Output a single integer representing the number of nodes (excluding the exit node) Amr is going to visit before reaching the exit by following this algorithm. | [
"1 2\n",
"2 3\n",
"3 6\n",
"10 1024\n"
] | [
"2",
"5",
"10",
"2046"
] | A perfect binary tree of height *h* is a binary tree consisting of *h* + 1 levels. Level 0 consists of a single node called root, level *h* consists of 2<sup class="upper-index">*h*</sup> nodes called leaves. Each node that is not a leaf has exactly two children, left and right one.
Following picture illustrates the ... | [
{
"input": "1 2",
"output": "2"
},
{
"input": "2 3",
"output": "5"
},
{
"input": "3 6",
"output": "10"
},
{
"input": "10 1024",
"output": "2046"
},
{
"input": "10 577",
"output": "1345"
},
{
"input": "11 550",
"output": "408"
},
{
"input": ... | 1,000 | 1,228,800 | 0 | 21,837 | |
922 | Robot Vacuum Cleaner | [
"greedy",
"sortings"
] | null | null | Pushok the dog has been chasing Imp for a few hours already.
Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner.
While moving, the robot generates a string *t* consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string *t* as the number of occurrences of string "sh... | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=105) — the number of strings in robot's memory.
Next *n* lines contain the strings *t*1,<=*t*2,<=...,<=*t**n*, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 105. | Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings. | [
"4\nssh\nhs\ns\nhhhs\n",
"2\nh\ns\n"
] | [
"18\n",
"1\n"
] | The optimal concatenation in the first sample is *ssshhshhhs*. | [
{
"input": "4\nssh\nhs\ns\nhhhs",
"output": "18"
},
{
"input": "2\nh\ns",
"output": "1"
},
{
"input": "6\nh\ns\nhhh\nh\nssssss\ns",
"output": "40"
},
{
"input": "1\ns",
"output": "0"
},
{
"input": "10\nsshshss\nhssssssssh\nhhhhhh\nhhhs\nhshhh\nhhhhshsh\nhh\nh\nshs... | 46 | 0 | 0 | 21,859 | |
763 | Timofey and remoduling | [
"brute force",
"implementation",
"math",
"number theory"
] | null | null | Little Timofey likes integers a lot. Unfortunately, he is very young and can't work with very big integers, so he does all the operations modulo his favorite prime *m*. Also, Timofey likes to look for arithmetical progressions everywhere.
One of his birthday presents was a sequence of distinct integers *a*1,<=*a*2,<=.... | The first line contains two integers *m* and *n* (2<=≤<=*m*<=≤<=109<=+<=7, 1<=≤<=*n*<=≤<=105, *m* is prime) — Timofey's favorite prime module and the length of the sequence.
The second line contains *n* distinct integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=<<=*m*) — the elements of the sequence. | Print -1 if it is not possible to rearrange the elements of the sequence so that is will be an arithmetical progression modulo *m*.
Otherwise, print two integers — the first element of the obtained progression *x* (0<=≤<=*x*<=<<=*m*) and its difference *d* (0<=≤<=*d*<=<<=*m*).
If there are multiple answers, pri... | [
"17 5\n0 2 4 13 15\n",
"17 5\n0 2 4 13 14\n",
"5 3\n1 2 3\n"
] | [
"13 2\n",
"-1\n",
"3 4\n"
] | none | [
{
"input": "17 5\n0 2 4 13 15",
"output": "13 2"
},
{
"input": "17 5\n0 2 4 13 14",
"output": "-1"
},
{
"input": "5 3\n1 2 3",
"output": "3 4"
},
{
"input": "17 8\n7 9 11 13 15 1 3 5",
"output": "1 2"
},
{
"input": "13 2\n0 11",
"output": "0 11"
},
{
"... | 31 | 0 | 0 | 21,897 | |
754 | Fedor and coupons | [
"binary search",
"data structures",
"greedy",
"sortings"
] | null | null | All our characters have hobbies. The same is true for Fedor. He enjoys shopping in the neighboring supermarket.
The goods in the supermarket have unique integer ids. Also, for every integer there is a product with id equal to this integer. Fedor has *n* discount coupons, the *i*-th of them can be used with products w... | The first line contains two integers *n* and *k* (1<=≤<=*k*<=≤<=*n*<=≤<=3·105) — the number of coupons Fedor has, and the number of coupons he wants to choose.
Each of the next *n* lines contains two integers *l**i* and *r**i* (<=-<=109<=≤<=*l**i*<=≤<=*r**i*<=≤<=109) — the description of the *i*-th coupon. The coupons... | In the first line print single integer — the maximum number of products with which all the chosen coupons can be used. The products with which at least one coupon cannot be used shouldn't be counted.
In the second line print *k* distinct integers *p*1,<=*p*2,<=...,<=*p**k* (1<=≤<=*p**i*<=≤<=*n*) — the ids of the coupo... | [
"4 2\n1 100\n40 70\n120 130\n125 180\n",
"3 2\n1 12\n15 20\n25 30\n",
"5 2\n1 10\n5 15\n14 50\n30 70\n99 100\n"
] | [
"31\n1 2 \n",
"0\n1 2 \n",
"21\n3 4 \n"
] | In the first example if we take the first two coupons then all the products with ids in range [40, 70] can be bought with both coupons. There are 31 products in total.
In the second example, no product can be bought with two coupons, that is why the answer is 0. Fedor can choose any two coupons in this example. | [
{
"input": "4 2\n1 100\n40 70\n120 130\n125 180",
"output": "31\n1 2 "
},
{
"input": "3 2\n1 12\n15 20\n25 30",
"output": "0\n1 2 "
},
{
"input": "5 2\n1 10\n5 15\n14 50\n30 70\n99 100",
"output": "21\n3 4 "
},
{
"input": "7 6\n-8 6\n7 9\n-10 -5\n-6 10\n-7 -3\n5 8\n4 10",
... | 46 | 102,400 | 0 | 21,909 | |
150 | Quantity of Strings | [
"combinatorics",
"dfs and similar",
"graphs",
"math"
] | null | null | Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one gets funny thoughts. For example, let's say there are strings with the length exactly *n*, based on the alphabet of size *m*. Any its substring with length equal to *k* is a palindrome. How many such strings exist? Your task ... | The first and only line contains three integers: *n*, *m* and *k* (1<=≤<=*n*,<=*m*,<=*k*<=≤<=2000). | Print a single integer — the number of strings of the described type modulo 1000000007 (109<=+<=7). | [
"1 1 1\n",
"5 2 4\n"
] | [
"1\n",
"2\n"
] | In the first sample only one string is valid: "a" (let's denote the only letter of our alphabet as "a").
In the second sample (if we denote the alphabet letters as "a" and "b") the following strings are valid: "aaaaa" and "bbbbb". | [
{
"input": "1 1 1",
"output": "1"
},
{
"input": "5 2 4",
"output": "2"
},
{
"input": "7 4 20",
"output": "16384"
},
{
"input": "8 13 9",
"output": "815730721"
},
{
"input": "10 23 9",
"output": "529"
},
{
"input": "10 25 8",
"output": "25"
},
{... | 15 | 0 | 0 | 21,913 | |
515 | Drazil and His Happy Friends | [
"brute force",
"dsu",
"meet-in-the-middle",
"number theory"
] | null | null | Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.
There are *n* boys and *m* girls among his friends. Let's number them from 0 to *n*<=-<=1 and 0 to *m*<=-<=1 separately. In *i*-th day, Drazil invites -th ... | The first line contains two integer *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=100).
The second line contains integer *b* (0<=≤<=*b*<=≤<=*n*), denoting the number of happy boys among friends of Drazil, and then follow *b* distinct integers *x*1,<=*x*2,<=...,<=*x**b* (0<=≤<=*x**i*<=<<=*n*), denoting the list of indices of hap... | If Drazil can make all his friends become happy by this plan, print "Yes". Otherwise, print "No". | [
"2 3\n0\n1 0\n",
"2 4\n1 0\n1 2\n",
"2 3\n1 0\n1 1\n"
] | [
"Yes\n",
"No\n",
"Yes\n"
] | By <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/169ade208e6eb4f9263c57aaff716529d59c3288.png" style="max-width: 100.0%;max-height: 100.0%;"/> we define the remainder of integer division of *i* by *k*.
In first sample case:
- On the 0-th day, Drazil invites 0-th boy and 0-th girl. Bec... | [
{
"input": "2 3\n0\n1 0",
"output": "Yes"
},
{
"input": "2 4\n1 0\n1 2",
"output": "No"
},
{
"input": "2 3\n1 0\n1 1",
"output": "Yes"
},
{
"input": "16 88\n6 5 14 2 0 12 7\n30 21 64 35 79 74 39 63 44 81 73 0 27 33 69 12 86 46 20 25 55 52 7 58 23 5 60 32 41 50 82",
"outpu... | 77 | 6,963,200 | 0 | 21,922 | |
251 | Two Sets | [
"bitmasks",
"math"
] | null | null | Little Petya likes numbers a lot. Recently his mother has presented him a collection of *n* non-negative integers. There's only one thing Petya likes more than numbers: playing with little Masha. He immediately decided to give a part of his new collection to her. To make the game even more interesting, Petya decided to... | The first line contains integer *n* (1<=≤<=*n*<=≤<=105), showing how many numbers Petya's mother gave him. The second line contains the actual space-separated numbers. They are all integer, non-negative and do not exceed 1018. | Print *n* space-separated integers, the *i*-th of them should equal either 1, if Petya keeps the number that follows *i*-th in his collection, or it should equal 2, if Petya gives the corresponding number to Masha. The numbers are indexed in the order in which they are given in the input. | [
"6\n1 2 3 4 5 6\n",
"3\n1000000000000 1000000000000 1000000000000\n",
"8\n1 1 2 2 3 3 4 4\n"
] | [
"2 2 2 2 2 2\n",
"2 2 2\n",
"1 2 1 2 2 2 1 2\n"
] | none | [] | 30 | 0 | 0 | 22,073 | |
852 | Casinos and travel | [
"dp"
] | null | null | John has just bought a new car and is planning a journey around the country. Country has *N* cities, some of which are connected by bidirectional roads. There are *N*<=-<=1 roads and every city is reachable from any other city. Cities are labeled from 1 to *N*.
John first has to select from which city he will start hi... | In the first line, a positive integer *N* (1<=≤<=*N*<=≤<=100000), the number of cities.
In the next *N*<=-<=1 lines, two numbers *a*,<= *b* (1<=≤<=*a*,<=*b*<=≤<=*N*) separated by a single space meaning that cities *a* and *b* are connected by a bidirectional road. | Output one number, the answer to the problem modulo 109<=+<=7. | [
"2\n1 2\n",
"3\n1 2\n2 3\n"
] | [
"4\n",
"10\n"
] | Example 1: If Jack selects city 1 as John's starting city, he can either build 0 casinos, so John will be happy all the time, or build a casino in both cities, so John would visit a casino in city 1, become unhappy, then go to city 2, visit a casino there and become happy and his journey ends there because he can't go ... | [
{
"input": "2\n1 2",
"output": "4"
},
{
"input": "3\n1 2\n2 3",
"output": "10"
},
{
"input": "4\n1 2\n2 3\n3 4",
"output": "24"
}
] | 78 | 0 | 0 | 22,217 | |
995 | Number Clicker | [
"divide and conquer",
"graphs",
"meet-in-the-middle",
"number theory"
] | null | null | Allen is playing Number Clicker on his phone.
He starts with an integer $u$ on the screen. Every second, he can press one of 3 buttons.
1. Turn $u \to u+1 \pmod{p}$. 1. Turn $u \to u+p-1 \pmod{p}$. 1. Turn $u \to u^{p-2} \pmod{p}$.
Allen wants to press at most 200 buttons and end up with $v$ on the screen. Help h... | The first line of the input contains 3 positive integers: $u, v, p$ ($0 \le u, v \le p-1$, $3 \le p \le 10^9 + 9$). $p$ is guaranteed to be prime. | On the first line, print a single integer $\ell$, the number of button presses. On the second line, print integers $c_1, \dots, c_\ell$, the button presses. For $1 \le i \le \ell$, $1 \le c_i \le 3$.
We can show that the answer always exists. | [
"1 3 5\n",
"3 2 5\n"
] | [
"2\n1 1\n",
"1\n3\n"
] | In the first example the integer on the screen changes as $1 \to 2 \to 3$.
In the second example the integer on the screen changes as $3 \to 2$. | [
{
"input": "1 3 5",
"output": "2\n1 1 "
},
{
"input": "3 2 5",
"output": "1\n2 "
},
{
"input": "9382509 19872987 1000000007",
"output": "38\n3 2 2 3 2 2 2 3 2 2 2 2 2 2 3 2 2 2 2 2 2 3 2 3 2 3 2 2 2 3 2 2 2 3 1 1 1 1 "
},
{
"input": "1 1 3",
"output": "0"
},
{
"in... | 92 | 0 | 0 | 22,231 | |
22 | Segments | [
"greedy",
"sortings"
] | D. Segments | 1 | 256 | You are given *n* segments on the Ox-axis. You can drive a nail in any integer point on the Ox-axis line nail so, that all segments containing this point, are considered nailed down. If the nail passes through endpoint of some segment, this segment is considered to be nailed too. What is the smallest number of nails ne... | The first line of the input contains single integer number *n* (1<=≤<=*n*<=≤<=1000) — amount of segments. Following *n* lines contain descriptions of the segments. Each description is a pair of integer numbers — endpoints coordinates. All the coordinates don't exceed 10000 by absolute value. Segments can degenarate to ... | The first line should contain one integer number — the smallest number of nails needed to nail all the segments down. The second line should contain coordinates of driven nails separated by space in any order. If the answer is not unique, output any. | [
"2\n0 2\n2 5\n",
"5\n0 3\n4 2\n4 8\n8 10\n7 7\n"
] | [
"1\n2 ",
"3\n7 10 3\n"
] | none | [
{
"input": "2\n0 2\n2 5",
"output": "1\n2 "
},
{
"input": "5\n0 3\n4 2\n4 8\n8 10\n7 7",
"output": "3\n3 7 10 "
},
{
"input": "3\n40 -83\n52 -80\n-21 -4",
"output": "1\n-4 "
},
{
"input": "4\n67 -88\n37 -62\n-26 91\n-99 -50",
"output": "2\n-50 91 "
},
{
"input": "... | 155 | 1,638,400 | 3.919448 | 22,239 |
367 | Sereja and Algorithm | [
"data structures",
"implementation"
] | null | null | Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as *q*<==<=*q*1*q*2... *q**k*. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string *q... | The first line contains non-empty string *s*, its length (*n*) doesn't exceed 105. It is guaranteed that string *s* only contains characters: 'x', 'y', 'z'.
The second line contains integer *m* (1<=≤<=*m*<=≤<=105) — the number of tests. Next *m* lines contain the tests. The *i*-th line contains a pair of integers *l**... | For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. | [
"zyxxxxxxyyz\n5\n5 5\n1 3\n1 11\n1 4\n3 6\n"
] | [
"YES\nYES\nNO\nYES\nNO\n"
] | In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. | [
{
"input": "zyxxxxxxyyz\n5\n5 5\n1 3\n1 11\n1 4\n3 6",
"output": "YES\nYES\nNO\nYES\nNO"
},
{
"input": "yxzyzxzzxyyzzxxxzyyzzyzxxzxyzyyzxyzxyxxyzxyxzyzxyzxyyxzzzyzxyyxyzxxy\n10\n17 67\n6 35\n12 45\n56 56\n14 30\n25 54\n1 1\n46 54\n3 33\n19 40",
"output": "NO\nNO\nNO\nYES\nYES\nNO\nYES\nNO\nNO\nY... | 93 | 0 | -1 | 22,291 | |
958 | Encryption (easy) | [
"brute force"
] | null | null | Rebel spy Heidi has just obtained the plans for the Death Star from the Empire and, now on her way to safety, she is trying to break the encryption of the plans (of course they are encrypted – the Empire may be evil, but it is not stupid!). The encryption has several levels of security, and here is how the first one lo... | The first line of the input contains two space-separated integer *N* and *p* (2<=≤<=*N*<=≤<=100<=000, 2<=≤<=*p*<=≤<=10<=000) – the number of elements in *A*, and the modulo for computing scores, respectively.
The second line contains *N* space-separated integers which are the elements of *A*. Each integer is from the ... | Output the number *S* as described in the problem statement. | [
"4 10\n3 4 7 2\n",
"10 12\n16 3 24 13 9 8 7 5 12 12\n"
] | [
"16\n",
"13\n"
] | In the first example, the score is maximized if the input sequence is split into two parts as (3, 4), (7, 2). It gives the total score of <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/fc1f9aecea625f3be9a56917d3ba448d669ff278.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the se... | [
{
"input": "4 10\n3 4 7 2",
"output": "16"
},
{
"input": "10 12\n16 3 24 13 9 8 7 5 12 12",
"output": "13"
},
{
"input": "2 2\n9 9",
"output": "2"
},
{
"input": "2 2\n8 8",
"output": "0"
},
{
"input": "5 50\n1 1 1 1 1",
"output": "5"
},
{
"input": "5 5... | 46 | 3,379,200 | -1 | 22,300 | |
960 | Alternating Tree | [
"combinatorics",
"dfs and similar",
"divide and conquer",
"dp",
"probabilities",
"trees"
] | null | null | Given a tree with $n$ nodes numbered from $1$ to $n$. Each node $i$ has an associated value $V_i$.
If the simple path from $u_1$ to $u_m$ consists of $m$ nodes namely $u_1 \rightarrow u_2 \rightarrow u_3 \rightarrow \dots u_{m-1} \rightarrow u_{m}$, then its alternating function $A(u_{1},u_{m})$ is defined as $A(u_{1}... | The first line contains an integer $n$ $(2 \leq n \leq 2\cdot10^{5} )$ — the number of vertices in the tree.
The second line contains $n$ space-separated integers $V_1, V_2, \ldots, V_n$ ($-10^9\leq V_i \leq 10^9$) — values of the nodes.
The next $n-1$ lines each contain two space-separated integers $u$ and $v$ $(1\l... | Print the total sum of alternating functions of all unique simple paths modulo $10^{9}+7$. | [
"4\n-4 1 5 -2\n1 2\n1 3\n1 4\n",
"8\n-2 6 -4 -4 -9 -3 -7 23\n8 2\n2 3\n1 4\n6 5\n7 6\n4 7\n5 8\n"
] | [
"40\n",
"4\n"
] | Consider the first example.
A simple path from node $1$ to node $2$: $1 \rightarrow 2$ has alternating function equal to $A(1,2) = 1 \cdot (-4)+(-1) \cdot 1 = -5$.
A simple path from node $1$ to node $3$: $1 \rightarrow 3$ has alternating function equal to $A(1,3) = 1 \cdot (-4)+(-1) \cdot 5 = -9$.
A simple path fro... | [
{
"input": "4\n-4 1 5 -2\n1 2\n1 3\n1 4",
"output": "40"
},
{
"input": "8\n-2 6 -4 -4 -9 -3 -7 23\n8 2\n2 3\n1 4\n6 5\n7 6\n4 7\n5 8",
"output": "4"
},
{
"input": "15\n-4 6 1 6 -9 -8 -10 9 -2 -9 7 2 -3 1 13\n11 2\n10 3\n15 4\n7 5\n2 6\n9 7\n6 8\n12 9\n4 10\n1 11\n3 12\n8 13\n5 14\n13 15"... | 421 | 23,961,600 | 0 | 22,319 | |
115 | Lawnmower | [
"greedy",
"sortings"
] | null | null | You have a garden consisting entirely of grass and weeds. Your garden is described by an *n*<=×<=*m* grid, with rows numbered 1 to *n* from top to bottom, and columns 1 to *m* from left to right. Each cell is identified by a pair (*r*,<=*c*) which means that the cell is located at row *r* and column *c*. Each cell may ... | The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=150) — the number of rows and columns respectively. Then follow *n* lines containing *m* characters each — the content of the grid. "G" means that this cell contains grass. "W" means that this cell contains weeds.
It is guaranteed that the top-left... | Print a single number — the minimum number of moves required to mow all the weeds. | [
"4 5\nGWGGW\nGGWGG\nGWGGG\nWGGGG\n",
"3 3\nGWW\nWWW\nWWG\n",
"1 1\nG\n"
] | [
"11\n",
"7\n",
"0\n"
] | For the first example, this is the picture of the initial state of the grid:
A possible solution is by mowing the weeds as illustrated below: | [
{
"input": "4 5\nGWGGW\nGGWGG\nGWGGG\nWGGGG",
"output": "11"
},
{
"input": "3 3\nGWW\nWWW\nWWG",
"output": "7"
},
{
"input": "1 1\nG",
"output": "0"
},
{
"input": "4 3\nGWW\nWWW\nWWW\nWWG",
"output": "11"
},
{
"input": "6 5\nGWWWW\nWWWWW\nWWWWW\nWWWWW\nWWWWW\nWWWW... | 31 | 0 | 0 | 22,354 | |
367 | Sereja ans Anagrams | [
"binary search",
"data structures"
] | null | null | Sereja has two sequences *a* and *b* and number *p*. Sequence *a* consists of *n* integers *a*1,<=*a*2,<=...,<=*a**n*. Similarly, sequence *b* consists of *m* integers *b*1,<=*b*2,<=...,<=*b**m*. As usual, Sereja studies the sequences he has. Today he wants to find the number of positions *q* (*q*<=+<=(*m*<=-<=1)·*p*<=... | The first line contains three integers *n*, *m* and *p* (1<=≤<=*n*,<=*m*<=≤<=2·105,<=1<=≤<=*p*<=≤<=2·105). The next line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=≤<=*a**i*<=≤<=109). The next line contains *m* integers *b*1, *b*2, ..., *b**m* (1<=≤<=*b**i*<=≤<=109). | In the first line print the number of valid *q*s. In the second line, print the valid values in the increasing order. | [
"5 3 1\n1 2 3 2 1\n1 2 3\n",
"6 3 2\n1 3 2 2 3 1\n1 2 3\n"
] | [
"2\n1 3\n",
"2\n1 2\n"
] | none | [
{
"input": "5 3 1\n1 2 3 2 1\n1 2 3",
"output": "2\n1 3"
},
{
"input": "6 3 2\n1 3 2 2 3 1\n1 2 3",
"output": "2\n1 2"
},
{
"input": "68 16 3\n5 3 4 3 3 3 2 2 2 3 2 4 2 2 2 2 4 3 5 1 1 2 2 2 3 1 5 1 2 2 1 5 1 5 3 2 3 5 2 1 1 4 2 3 4 3 4 3 3 1 3 4 1 5 2 5 3 4 4 1 4 5 5 1 1 2 2 2\n5 4 4 3 ... | 61 | 2,867,200 | -1 | 22,390 | |
919 | Seat Arrangements | [
"brute force",
"implementation"
] | null | null | Suppose that you are in a campus and have to go for classes day by day. As you may see, when you hurry to a classroom, you surprisingly find that many seats there are already occupied. Today you and your friends went for class, and found out that some of the seats were occupied.
The classroom contains $n$ rows of seat... | The first line contains three positive integers $n,m,k$ ($1 \leq n, m, k \leq 2\,000$), where $n,m$ represent the sizes of the classroom and $k$ is the number of consecutive seats you need to find.
Each of the next $n$ lines contains $m$ characters '.' or '*'. They form a matrix representing the classroom, '.' denotes... | A single number, denoting the number of ways to find $k$ empty seats in the same row or column. | [
"2 3 2\n**.\n...\n",
"1 2 2\n..\n",
"3 3 4\n.*.\n*.*\n.*.\n"
] | [
"3\n",
"1\n",
"0\n"
] | In the first sample, there are three ways to arrange those seats. You can take the following seats for your arrangement.
- $(1,3)$, $(2,3)$ - $(2,2)$, $(2,3)$ - $(2,1)$, $(2,2)$ | [
{
"input": "1 2 2\n..",
"output": "1"
},
{
"input": "3 3 4\n.*.\n*.*\n.*.",
"output": "0"
},
{
"input": "1 1 1\n.",
"output": "1"
},
{
"input": "1 1 1\n*",
"output": "0"
},
{
"input": "2 2 2\n.*\n*.",
"output": "0"
},
{
"input": "1 1 1000\n.",
"out... | 1,000 | 9,728,000 | 0 | 22,423 | |
0 | none | [
"none"
] | null | null | You are given a rectangular parallelepiped with sides of positive integer lengths $A$, $B$ and $C$.
Find the number of different groups of three integers ($a$, $b$, $c$) such that $1\leq a\leq b\leq c$ and parallelepiped $A\times B\times C$ can be paved with parallelepipeds $a\times b\times c$. Note, that all small p... | The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) — the number of test cases.
Each of the next $t$ lines contains three integers $A$, $B$ and $C$ ($1 \leq A, B, C \leq 10^5$) — the sizes of the parallelepiped. | For each test case, print the number of different groups of three points that satisfy all given conditions. | [
"4\n1 1 1\n1 6 1\n2 2 2\n100 100 100\n"
] | [
"1\n4\n4\n165\n"
] | In the first test case, rectangular parallelepiped $(1, 1, 1)$ can be only divided into rectangular parallelepiped with sizes $(1, 1, 1)$.
In the second test case, rectangular parallelepiped $(1, 6, 1)$ can be divided into rectangular parallelepipeds with sizes $(1, 1, 1)$, $(1, 1, 2)$, $(1, 1, 3)$ and $(1, 1, 6)$.
I... | [
{
"input": "4\n1 1 1\n1 6 1\n2 2 2\n100 100 100",
"output": "1\n4\n4\n165"
},
{
"input": "10\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1",
"output": "1\n1\n1\n1\n1\n1\n1\n1\n1\n1"
},
{
"input": "10\n9 6 8\n5 5 2\n8 9 2\n2 7 9\n6 4 10\n1 1 8\n2 8 1\n10 6 3\n7 5 2... | 46 | 0 | 0 | 22,429 | |
379 | New Year Present | [
"constructive algorithms",
"implementation"
] | null | null | The New Year is coming! That's why many people today are busy preparing New Year presents. Vasily the Programmer is no exception.
Vasily knows that the best present is (no, it's not a contest) money. He's put *n* empty wallets from left to right in a row and decided how much money to put in what wallet. Vasily decided... | The first line contains integer *n* (2<=≤<=*n*<=≤<=300) — the number of wallets. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=300).
It is guaranteed that at least one *a**i* is positive. | Print the sequence that consists of *k* (1<=≤<=*k*<=≤<=106) characters, each of them equals: "L", "R" or "P". Each character of the sequence is an instruction to the robot. Character "L" orders to move to the left, character "R" orders to move to the right, character "P" orders the robot to put a coin in the wallet. Th... | [
"2\n1 2\n",
"4\n0 2 0 2\n"
] | [
"PRPLRP",
"RPRRPLLPLRRRP"
] | none | [
{
"input": "2\n1 2",
"output": "PRPLRP"
},
{
"input": "4\n0 2 0 2",
"output": "RPRRPLLPLRRRP"
},
{
"input": "10\n2 3 4 0 0 1 1 3 4 2",
"output": "PRPRPRRRPRPRPRPRPLPLPLLLLLPLPLPRPRPRRRRRPRPRPLPLLLLLLPLL"
},
{
"input": "10\n0 0 0 0 0 0 0 0 1 0",
"output": "RRRRRRRRPR"
},... | 77 | 3,072,000 | -1 | 22,525 | |
0 | none | [
"none"
] | null | null | One day Polycarpus got hold of two non-empty strings *s* and *t*, consisting of lowercase Latin letters. Polycarpus is quite good with strings, so he immediately wondered, how many different pairs of "*x* *y*" are there, such that *x* is a substring of string *s*, *y* is a subsequence of string *t*, and the content of ... | The input consists of two lines. The first of them contains *s* (1<=≤<=|*s*|<=≤<=5000), and the second one contains *t* (1<=≤<=|*t*|<=≤<=5000). Both strings consist of lowercase Latin letters. | Print a single number — the number of different pairs "*x* *y*" such that *x* is a substring of string *s*, *y* is a subsequence of string *t*, and the content of *x* and *y* is the same. As the answer can be rather large, print it modulo 1000000007 (109<=+<=7). | [
"aa\naa\n",
"codeforces\nforceofcode\n"
] | [
"5\n",
"60\n"
] | Let's write down all pairs "*x* *y*" that form the answer in the first sample: "*s*[1...1] *t*[1]", "*s*[2...2] *t*[1]", "*s*[1...1] *t*[2]","*s*[2...2] *t*[2]", "*s*[1...2] *t*[1 2]". | [] | 62 | 0 | 0 | 22,539 | |
332 | Theft of Blueprints | [
"graphs",
"math"
] | null | null | Insurgents accidentally got hold of the plan of a top secret research polygon created on a distant planet for the needs of the Galaxy Empire. The insurgents suppose that this polygon is developing new deadly weapon. The polygon consists of *n* missile silos connected by bidirectional underground passages. The passages ... | The first line contains two integers *n* and *k* (2<=≤<=*n*<=≤<=2000, 1<=≤<=*k*<=≤<=*n*<=-<=1) — the number of silos and the number of scout groups, correspondingly. The next *n*<=-<=1 lines describe the polygon plan: the *i*-th of these lines contains *n*<=-<=*i* integers *c**i*,<=*i*<=+<=1,<=*c**i*,<=*i*<=+<=2,<=...,... | Print the average danger of the scouting operation, rounded down to an integer. Note that at the given limits the answer to the problem always fits into the standard integer 64-bit data type.
Please do not use the %lld specifier to write 64-bit integers in С++. It is preferred to use the cout stream or the %I64d speci... | [
"6 1\n-1 -1 -1 8 -1\n-1 5 -1 -1\n-1 -1 3\n-1 -1\n-1\n",
"3 2\n10 0\n11\n"
] | [
"5\n",
"14\n"
] | In the first sample there are 6 one-element sets of silos. For sets {1}, {5} the operation danger will equal 8, for sets {3}, {6} — 3, for sets {2}, {4} — 5. The mathematical average equals <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/3bc833659595c25e73150ed7f23907011961ceca.png" style="... | [
{
"input": "6 1\n-1 -1 -1 8 -1\n-1 5 -1 -1\n-1 -1 3\n-1 -1\n-1",
"output": "5"
},
{
"input": "3 2\n10 0\n11",
"output": "14"
},
{
"input": "4 3\n15 1 3\n5 8\n9",
"output": "20"
},
{
"input": "5 2\n-1 -1 14 3\n19 -1 1\n-1 6\n0",
"output": "10"
},
{
"input": "10 9\n... | 92 | 0 | 0 | 22,540 | |
14 | Camels | [
"dp"
] | E. Camels | 2 | 64 | Bob likes to draw camels: with a single hump, two humps, three humps, etc. He draws a camel by connecting points on a coordinate plane. Now he's drawing camels with *t* humps, representing them as polylines in the plane. Each polyline consists of *n* vertices with coordinates (*x*1,<=*y*1), (*x*2,<=*y*2), ..., (*x**n*,... | The first line contains a pair of integers *n* and *t* (3<=≤<=*n*<=≤<=20, 1<=≤<=*t*<=≤<=10). | Output the required amount of camels with *t* humps. | [
"6 1\n",
"4 2\n"
] | [
"6\n",
"0\n"
] | In the first sample test sequences of *y*-coordinates for six camels are: 123421, 123431, 123432, 124321, 134321 и 234321 (each digit corresponds to one value of *y*<sub class="lower-index">*i*</sub>). | [
{
"input": "6 1",
"output": "6"
},
{
"input": "4 2",
"output": "0"
},
{
"input": "3 1",
"output": "14"
},
{
"input": "3 2",
"output": "0"
},
{
"input": "3 3",
"output": "0"
},
{
"input": "3 10",
"output": "0"
},
{
"input": "4 1",
"outpu... | 60 | 0 | 0 | 22,548 |
311 | Cats Transport | [
"data structures",
"dp"
] | null | null | Zxr960115 is owner of a large farm. He feeds *m* cute cats and employs *p* feeders. There's a straight road across the farm and *n* hills along the road, numbered from 1 to *n* from left to right. The distance between hill *i* and (*i*<=-<=1) is *d**i* meters. The feeders live in hill 1.
One day, the cats went out to ... | The first line of the input contains three integers *n*,<=*m*,<=*p* (2<=≤<=*n*<=≤<=105,<=1<=≤<=*m*<=≤<=105,<=1<=≤<=*p*<=≤<=100).
The second line contains *n*<=-<=1 positive integers *d*2,<=*d*3,<=...,<=*d**n* (1<=≤<=*d**i*<=<<=104).
Each of the next *m* lines contains two integers *h**i* and *t**i* (1<=≤<=*h**i*<=... | Output an integer, the minimum sum of waiting time of all cats.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"4 6 2\n1 3 5\n1 0\n2 1\n4 9\n1 10\n2 10\n3 12\n"
] | [
"3\n"
] | none | [
{
"input": "4 6 2\n1 3 5\n1 0\n2 1\n4 9\n1 10\n2 10\n3 12",
"output": "3"
}
] | 78 | 409,600 | 0 | 22,582 | |
796 | Police Stations | [
"constructive algorithms",
"dfs and similar",
"dp",
"graphs",
"shortest paths",
"trees"
] | null | null | Inzane finally found Zane with a lot of money to spare, so they together decided to establish a country of their own.
Ruling a country is not an easy job. Thieves and terrorists are always ready to ruin the country's peace. To fight back, Zane and Inzane have enacted a very effective law: from each city it must be pos... | The first line contains three integers *n*, *k*, and *d* (2<=≤<=*n*<=≤<=3·105, 1<=≤<=*k*<=≤<=3·105, 0<=≤<=*d*<=≤<=*n*<=-<=1) — the number of cities, the number of police stations, and the distance limitation in kilometers, respectively.
The second line contains *k* integers *p*1,<=*p*2,<=...,<=*p**k* (1<=≤<=*p**i*<=≤<... | In the first line, print one integer *s* that denotes the maximum number of roads that can be shut down.
In the second line, print *s* distinct integers, the indices of such roads, in any order.
If there are multiple answers, print any of them. | [
"6 2 4\n1 6\n1 2\n2 3\n3 4\n4 5\n5 6\n",
"6 3 2\n1 5 6\n1 2\n1 3\n1 4\n1 5\n5 6\n"
] | [
"1\n5\n",
"2\n4 5 "
] | In the first sample, if you shut down road 5, all cities can still reach a police station within *k* = 4 kilometers.
In the second sample, although this is the only largest valid set of roads that can be shut down, you can print either 4 5 or 5 4 in the second line. | [
{
"input": "6 2 4\n1 6\n1 2\n2 3\n3 4\n4 5\n5 6",
"output": "1\n3 "
},
{
"input": "6 3 2\n1 5 6\n1 2\n1 3\n1 4\n1 5\n5 6",
"output": "2\n4 5 "
},
{
"input": "10 1 5\n5\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10",
"output": "0"
},
{
"input": "11 1 5\n6\n1 2\n2 3\n3 4\n4 5\n... | 2,000 | 105,472,000 | 0 | 22,587 | |
0 | none | [
"none"
] | null | null | Bob is about to take a hot bath.
There are two taps to fill the bath: a hot water tap and a cold water tap. The cold water's temperature is *t*1, and the hot water's temperature is *t*2. The cold water tap can transmit any integer number of water units per second from 0 to *x*1, inclusive. Similarly, the hot water ta... | You are given five integers *t*1, *t*2, *x*1, *x*2 and *t*0 (1<=≤<=*t*1<=≤<=*t*0<=≤<=*t*2<=≤<=106, 1<=≤<=*x*1,<=*x*2<=≤<=106). | Print two space-separated integers *y*1 and *y*2 (0<=≤<=*y*1<=≤<=*x*1, 0<=≤<=*y*2<=≤<=*x*2). | [
"10 70 100 100 25\n",
"300 500 1000 1000 300\n",
"143 456 110 117 273\n"
] | [
"99 33",
"1000 0",
"76 54"
] | In the second sample the hot water tap shouldn't be opened, but the cold water tap should be opened at full capacity in order to fill the bath in the quickest way possible. | [
{
"input": "10 70 100 100 25",
"output": "99 33"
},
{
"input": "300 500 1000 1000 300",
"output": "1000 0"
},
{
"input": "143 456 110 117 273",
"output": "76 54"
},
{
"input": "10 20 5 5 13",
"output": "4 2"
},
{
"input": "1 3 1999 3444 2",
"output": "1999 199... | 216 | 2,867,200 | 0 | 22,609 | |
4 | Mysterious Present | [
"dp",
"sortings"
] | D. Mysterious Present | 1 | 64 | Peter decided to wish happy birthday to his friend from Australia and send him a card. To make his present more mysterious, he decided to make a chain. Chain here is such a sequence of envelopes *A*<==<={*a*1,<=<=*a*2,<=<=...,<=<=*a**n*}, where the width and the height of the *i*-th envelope is strictly higher than the... | The first line contains integers *n*, *w*, *h* (1<=<=≤<=*n*<=≤<=5000, 1<=≤<=*w*,<=<=*h*<=<=≤<=106) — amount of envelopes Peter has, the card width and height respectively. Then there follow *n* lines, each of them contains two integer numbers *w**i* and *h**i* — width and height of the *i*-th envelope (1<=≤<=*w**i*,<=<... | In the first line print the maximum chain size. In the second line print the numbers of the envelopes (separated by space), forming the required chain, starting with the number of the smallest envelope. Remember, please, that the card should fit into the smallest envelope. If the chain of maximum size is not unique, pr... | [
"2 1 1\n2 2\n2 2\n",
"3 3 3\n5 4\n12 11\n9 8\n"
] | [
"1\n1 \n",
"3\n1 3 2 \n"
] | none | [
{
"input": "2 1 1\n2 2\n2 2",
"output": "1\n1 "
},
{
"input": "3 3 3\n5 4\n12 11\n9 8",
"output": "3\n1 3 2 "
},
{
"input": "5 10 10\n22 23\n17 19\n13 17\n8 12\n2 6",
"output": "3\n3 2 1 "
},
{
"input": "5 13 13\n4 4\n10 10\n7 7\n1 1\n13 13",
"output": "0"
},
{
"i... | 46 | 0 | 0 | 22,624 |
118 | Caesar's Legions | [
"dp"
] | null | null | Gaius Julius Caesar, a famous general, loved to line up his soldiers. Overall the army had *n*1 footmen and *n*2 horsemen. Caesar thought that an arrangement is not beautiful if somewhere in the line there are strictly more that *k*1 footmen standing successively one after another, or there are strictly more than *k*2 ... | The only line contains four space-separated integers *n*1, *n*2, *k*1, *k*2 (1<=≤<=*n*1,<=*n*2<=≤<=100,<=1<=≤<=*k*1,<=*k*2<=≤<=10) which represent how many footmen and horsemen there are and the largest acceptable number of footmen and horsemen standing in succession, correspondingly. | Print the number of beautiful arrangements of the army modulo 100000000 (108). That is, print the number of such ways to line up the soldiers, that no more than *k*1 footmen stand successively, and no more than *k*2 horsemen stand successively. | [
"2 1 1 10\n",
"2 3 1 2\n",
"2 4 1 1\n"
] | [
"1\n",
"5\n",
"0\n"
] | Let's mark a footman as 1, and a horseman as 2.
In the first sample the only beautiful line-up is: 121
In the second sample 5 beautiful line-ups exist: 12122, 12212, 21212, 21221, 22121 | [
{
"input": "2 1 1 10",
"output": "1"
},
{
"input": "2 3 1 2",
"output": "5"
},
{
"input": "2 4 1 1",
"output": "0"
},
{
"input": "10 10 5 7",
"output": "173349"
},
{
"input": "12 15 7 2",
"output": "171106"
},
{
"input": "20 8 4 8",
"output": "1625... | 154 | 2,252,800 | -1 | 22,643 | |
756 | Nikita and stack | [
"data structures"
] | null | null | Nikita has a stack. A stack in this problem is a data structure that supports two operations. Operation push(x) puts an integer *x* on the top of the stack, and operation pop() deletes the top integer from the stack, i. e. the last added. If the stack is empty, then the operation pop() does nothing.
Nikita made *m* op... | The first line contains the integer *m* (1<=≤<=*m*<=≤<=105) — the number of operations Nikita made.
The next *m* lines contain the operations Nikita remembers. The *i*-th line starts with two integers *p**i* and *t**i* (1<=≤<=*p**i*<=≤<=*m*, *t**i*<==<=0 or *t**i*<==<=1) — the index of operation he remembers on the st... | Print *m* integers. The integer *i* should equal the number on the top of the stack after performing all the operations Nikita remembered on the steps from 1 to *i*. If the stack is empty after performing all these operations, print -1. | [
"2\n2 1 2\n1 0\n",
"3\n1 1 2\n2 1 3\n3 0\n",
"5\n5 0\n4 0\n3 1 1\n2 1 1\n1 1 2\n"
] | [
"2\n2\n",
"2\n3\n2\n",
"-1\n-1\n-1\n-1\n2\n"
] | In the first example, after Nikita remembers the operation on the first step, the operation push(2) is the only operation, so the answer is 2. After he remembers the operation pop() which was done before push(2), answer stays the same.
In the second example, the operations are push(2), push(3) and pop(). Nikita rememb... | [] | 46 | 0 | 0 | 22,667 | |
600 | Make Palindrome | [
"constructive algorithms",
"greedy",
"strings"
] | null | null | A string is called palindrome if it reads the same from left to right and from right to left. For example "kazak", "oo", "r" and "mikhailrubinchikkihcniburliahkim" are palindroms, but strings "abb" and "ij" are not.
You are given string *s* consisting of lowercase Latin letters. At once you can choose any position in ... | The only line contains string *s* (1<=≤<=|*s*|<=≤<=2·105) consisting of only lowercase Latin letters. | Print the lexicographically smallest palindrome that can be obtained with the minimal number of changes. | [
"aabc\n",
"aabcd\n"
] | [
"abba\n",
"abcba\n"
] | none | [
{
"input": "aabc",
"output": "abba"
},
{
"input": "aabcd",
"output": "abcba"
},
{
"input": "u",
"output": "u"
},
{
"input": "ttttt",
"output": "ttttt"
},
{
"input": "xxxvvvxxvv",
"output": "vvvxxxxvvv"
},
{
"input": "wrwrwfrrfrffrrwwwffffwrfrrwfrrfrwwf... | 31 | 307,200 | 0 | 22,680 | |
453 | Little Pony and Harmony Chest | [
"bitmasks",
"brute force",
"dp"
] | null | null | Princess Twilight went to Celestia and Luna's old castle to research the chest from the Elements of Harmony.
A sequence of positive integers *b**i* is harmony if and only if for every two elements of the sequence their greatest common divisor equals 1. According to an ancient book, the key of the chest is a harmony se... | The first line contains an integer *n* (1<=≤<=*n*<=≤<=100) — the number of elements of the sequences *a* and *b*. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=30). | Output the key — sequence *b**i* that minimizes the sum described above. If there are multiple optimal sequences, you can output any of them. | [
"5\n1 1 1 1 1\n",
"5\n1 6 4 2 8\n"
] | [
"1 1 1 1 1 ",
"1 5 3 1 8 "
] | none | [
{
"input": "5\n1 1 1 1 1",
"output": "1 1 1 1 1 "
},
{
"input": "5\n1 6 4 2 8",
"output": "1 5 3 1 8 "
},
{
"input": "10\n16 3 16 10 12 5 14 14 15 27",
"output": "19 1 17 7 11 1 16 13 15 29 "
},
{
"input": "10\n8 7 11 5 17 24 28 18 7 8",
"output": "9 7 11 5 17 23 29 19 1 ... | 202 | 6,656,000 | 0 | 22,724 | |
522 | Chicken or Fish? | [
"greedy"
] | null | null | Polycarp is flying in the airplane. Finally, it is his favorite time — the lunchtime. The BerAvia company stewardess is giving food consecutively to all the passengers from the 1-th one to the last one. Polycarp is sitting on seat *m*, that means, he will be the *m*-th person to get food.
The flight menu has *k* dishe... | Each test in this problem consists of one or more input sets. First goes a string that contains a single integer *t* (1<=≤<=*t*<=≤<=100<=000) — the number of input data sets in the test. Then the sets follow, each set is preceded by an empty line.
The first line of each set of the input contains integers *m*, *k* (2<=... | For each input set print the answer as a single line. Print a string of *k* letters "Y" or "N". Letter "Y" in position *i* should be printed if they could have run out of the *i*-th dish by the time the stewardess started serving Polycarp. | [
"2\n\n3 4\n2 3 2 1\n1 0\n0 0\n\n5 5\n1 2 1 3 1\n3 0\n0 0\n2 1\n4 0\n"
] | [
"YNNY\nYYYNY\n"
] | In the first input set depending on the choice of the second passenger the situation could develop in different ways:
- If he chose the first dish, then by the moment the stewardess reaches Polycarp, they will have run out of the first dish; - If he chose the fourth dish, then by the moment the stewardess reaches Po... | [
{
"input": "2\n\n3 4\n2 3 2 1\n1 0\n0 0\n\n5 5\n1 2 1 3 1\n3 0\n0 0\n2 1\n4 0",
"output": "YNNY\nYYYNY"
},
{
"input": "4\n\n2 1\n42\n0 0\n\n2 1\n2\n0 0\n\n2 1\n42\n1 0\n\n2 1\n2\n1 0",
"output": "N\nN\nN\nN"
},
{
"input": "5\n\n3 3\n1 1 1\n0 0\n0 1\n\n3 3\n1 1 1\n1 0\n2 1\n\n3 3\n1 1 1\n... | 46 | 0 | -1 | 22,765 | |
0 | none | [
"none"
] | null | null | Innokenty is a president of a new football league in Byteland. The first task he should do is to assign short names to all clubs to be shown on TV next to the score. Of course, the short names should be distinct, and Innokenty wants that all short names consist of three letters.
Each club's full name consist of two wo... | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=1000) — the number of clubs in the league.
Each of the next *n* lines contains two words — the team's name and the hometown's name for some club. Both team's name and hometown's name consist of uppercase English letters and have length at least 3 and at most ... | It it is not possible to choose short names and satisfy all constraints, print a single line "NO".
Otherwise, in the first line print "YES". Then print *n* lines, in each line print the chosen short name for the corresponding club. Print the clubs in the same order as they appeared in input.
If there are multiple ans... | [
"2\nDINAMO BYTECITY\nFOOTBALL MOSCOW\n",
"2\nDINAMO BYTECITY\nDINAMO BITECITY\n",
"3\nPLAYFOOTBALL MOSCOW\nPLAYVOLLEYBALL SPB\nGOGO TECHNOCUP\n",
"3\nABC DEF\nABC EFG\nABD OOO\n"
] | [
"YES\nDIN\nFOO\n",
"NO\n",
"YES\nPLM\nPLS\nGOG\n",
"YES\nABD\nABE\nABO\n"
] | In the first sample Innokenty can choose first option for both clubs.
In the second example it is not possible to choose short names, because it is not possible that one club has first option, and the other has second option if the first options are equal for both clubs.
In the third example Innokenty can choose the ... | [
{
"input": "2\nDINAMO BYTECITY\nFOOTBALL MOSCOW",
"output": "YES\nDIN\nFOO"
},
{
"input": "2\nDINAMO BYTECITY\nDINAMO BITECITY",
"output": "NO"
},
{
"input": "3\nPLAYFOOTBALL MOSCOW\nPLAYVOLLEYBALL SPB\nGOGO TECHNOCUP",
"output": "YES\nPLM\nPLS\nGOG"
},
{
"input": "3\nABC DEF... | 171 | 23,654,400 | 0 | 22,815 | |
755 | PolandBall and Gifts | [
"bitmasks",
"dp",
"greedy"
] | null | null | It's Christmas time! PolandBall and his friends will be giving themselves gifts. There are *n* Balls overall. Each Ball has someone for whom he should bring a present according to some permutation *p*, *p**i*<=≠<=*i* for all *i*.
Unfortunately, Balls are quite clumsy. We know earlier that exactly *k* of them will forg... | The first line of input contains two integers *n* and *k* (2<=≤<=*n*<=≤<=106, 0<=≤<=*k*<=≤<=*n*), representing the number of Balls and the number of Balls who will forget to bring their presents.
The second line contains the permutation *p* of integers from 1 to *n*, where *p**i* is the index of Ball who should get a... | You should output two values — minimum and maximum possible number of Balls who will not get their presents, in that order. | [
"5 2\n3 4 1 5 2\n",
"10 1\n2 3 4 5 6 7 8 9 10 1\n"
] | [
"2 4",
"2 2"
] | In the first sample, if the third and the first balls will forget to bring their presents, they will be th only balls not getting a present. Thus the minimum answer is 2. However, if the first ans the second balls will forget to bring their presents, then only the fifth ball will get a present. So, the maximum answer i... | [
{
"input": "5 2\n3 4 1 5 2",
"output": "2 4"
},
{
"input": "10 1\n2 3 4 5 6 7 8 9 10 1",
"output": "2 2"
},
{
"input": "5 4\n3 1 4 5 2",
"output": "5 5"
},
{
"input": "3 0\n2 3 1",
"output": "0 0"
},
{
"input": "4 3\n2 3 4 1",
"output": "4 4"
},
{
"inp... | 0 | 0 | -1 | 22,887 | |
55 | Pie or die | [
"games"
] | C. Pie or die | 2 | 256 | Volodya and Vlad play the following game. There are *k* pies at the cells of *n*<=<=×<=<=*m* board. Each turn Volodya moves one pie to the neighbouring (by side) cell. If the pie lies at the border of the board then Volodya can move it outside the board, get the pie and win. After Volodya's move, Vlad bans some edge at... | First line contains 3 integers, separated by space: 1<=≤<=*n*,<=*m*<=≤<=100 — dimensions of the board and 0<=≤<=*k*<=≤<=100 — the number of pies. Each of the next *k* lines contains 2 integers, separated by space: 1<=≤<=*x*<=≤<=*n*, 1<=≤<=*y*<=≤<=*m* — coordinates of the corresponding pie. There could be more than one ... | Output only one word: "YES" — if Volodya wins, "NO" — otherwise. | [
"2 2 1\n1 2\n",
"3 4 0\n",
"100 50 2\n50 25\n50 25\n"
] | [
"YES",
"NO",
"NO"
] | none | [
{
"input": "2 2 1\n1 2",
"output": "YES"
},
{
"input": "3 4 0",
"output": "NO"
},
{
"input": "100 50 2\n50 25\n50 25",
"output": "NO"
},
{
"input": "20 20 4\n10 10\n10 10\n10 10\n10 10",
"output": "NO"
},
{
"input": "15 15 1\n8 8",
"output": "NO"
},
{
... | 122 | 0 | 3.9695 | 22,899 |
383 | Propagating tree | [
"data structures",
"dfs and similar",
"trees"
] | null | null | Iahub likes trees very much. Recently he discovered an interesting tree named propagating tree. The tree consists of *n* nodes numbered from 1 to *n*, each node *i* having an initial value *a**i*. The root of the tree is node 1.
This tree has a special property: when a value *val* is added to a value of node *i*, the ... | The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=200000). The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=≤<=*a**i*<=≤<=1000). Each of the next *n*–1 lines contains two integers *v**i* and *u**i* (1<=≤<=*v**i*,<=*u**i*<=≤<=*n*), meaning that there is an edge between nodes *v**i* a... | For each query of type two (print the value of node *x*) you must print the answer to the query on a separate line. The queries must be answered in the order given in the input. | [
"5 5\n1 2 1 1 2\n1 2\n1 3\n2 4\n2 5\n1 2 3\n1 1 2\n2 1\n2 2\n2 4\n"
] | [
"3\n3\n0\n"
] | The values of the nodes are [1, 2, 1, 1, 2] at the beginning.
Then value 3 is added to node 2. It propagates and value -3 is added to it's sons, node 4 and node 5. Then it cannot propagate any more. So the values of the nodes are [1, 5, 1, - 2, - 1].
Then value 2 is added to node 1. It propagates and value -2 is ad... | [
{
"input": "5 5\n1 2 1 1 2\n1 2\n1 3\n2 4\n2 5\n1 2 3\n1 1 2\n2 1\n2 2\n2 4",
"output": "3\n3\n0"
},
{
"input": "10 10\n137 197 856 768 825 894 86 174 218 326\n7 8\n4 7\n8 9\n7 10\n1 2\n2 4\n3 6\n3 5\n2 3\n1 9 624\n2 1\n2 4\n1 6 505\n1 8 467\n1 3 643\n2 1\n1 8 631\n2 4\n1 7 244",
"output": "137\... | 1,107 | 130,252,800 | 3 | 22,952 | |
868 | El Toll Caves | [
"math"
] | null | null | The prehistoric caves of El Toll are located in Moià (Barcelona). You have heard that there is a treasure hidden in one of *n* possible spots in the caves. You assume that each of the spots has probability 1<=/<=*n* to contain a treasure.
You cannot get into the caves yourself, so you have constructed a robot that can... | The first line contains the number of test cases *T* (1<=≤<=*T*<=≤<=1000).
Each of the next *T* lines contains two integers *n* and *k* (1<=≤<=*k*<=≤<=*n*<=≤<=5·108). | For each test case output the answer in a separate line. | [
"3\n1 1\n2 1\n3 2\n"
] | [
"2\n500000007\n777777786\n"
] | In the first case the robot will repeatedly search in the only spot. The expected number of days in this case is 2. Note that in spite of the fact that we know the treasure spot from the start, the robot still has to search there until he succesfully recovers the treasure.
In the second case the answer can be shown to... | [] | 2,000 | 7,065,600 | 0 | 23,010 | |
776 | The Door Problem | [
"2-sat",
"dfs and similar",
"dsu",
"graphs"
] | null | null | Moriarty has trapped *n* people in *n* distinct rooms in a hotel. Some rooms are locked, others are unlocked. But, there is a condition that the people in the hotel can only escape when all the doors are unlocked at the same time. There are *m* switches. Each switch control doors of some rooms, but each door is control... | First line of input contains two integers *n* and *m* (2<=≤<=*n*<=≤<=105, 2<=≤<=*m*<=≤<=105) — the number of rooms and the number of switches.
Next line contains *n* space-separated integers *r*1,<=*r*2,<=...,<=*r**n* (0<=≤<=*r**i*<=≤<=1) which tell the status of room doors. The *i*-th room is locked if *r**i*<==<=0, ... | Output "YES" without quotes, if it is possible to open all doors at the same time, otherwise output "NO" without quotes. | [
"3 3\n1 0 1\n2 1 3\n2 1 2\n2 2 3\n",
"3 3\n1 0 1\n3 1 2 3\n1 2\n2 1 3\n",
"3 3\n1 0 1\n3 1 2 3\n2 1 2\n1 3\n"
] | [
"NO",
"YES",
"NO"
] | In the second example input, the initial statuses of the doors are [1, 0, 1] (0 means locked, 1 — unlocked).
After toggling switch 3, we get [0, 0, 0] that means all doors are locked.
Then, after toggling switch 1, we get [1, 1, 1] that means all doors are unlocked.
It can be seen that for the first and for the thir... | [
{
"input": "3 3\n1 0 1\n2 1 3\n2 1 2\n2 2 3",
"output": "NO"
},
{
"input": "3 3\n1 0 1\n3 1 2 3\n1 2\n2 1 3",
"output": "YES"
},
{
"input": "3 3\n1 0 1\n3 1 2 3\n2 1 2\n1 3",
"output": "NO"
},
{
"input": "11 10\n0 0 1 0 0 0 0 1 1 0 1\n3 2 3 11\n1 3\n2 6 7\n1 5\n1 11\n1 10\n5 ... | 733 | 38,195,200 | -1 | 23,029 | |
825 | Five-In-a-Row | [
"brute force",
"implementation"
] | null | null | Alice and Bob play 5-in-a-row game. They have a playing field of size 10<=×<=10. In turns they put either crosses or noughts, one at a time. Alice puts crosses and Bob puts noughts.
In current match they have made some turns and now it's Alice's turn. She wonders if she can put cross in such empty cell that she wins i... | You are given matrix 10<=×<=10 (10 lines of 10 characters each) with capital Latin letters 'X' being a cross, letters 'O' being a nought and '.' being an empty cell. The number of 'X' cells is equal to the number of 'O' cells and there is at least one of each type. There is at least one empty cell.
It is guaranteed th... | Print 'YES' if it's possible for Alice to win in one turn by putting cross in some empty cell. Otherwise print 'NO'. | [
"XX.XX.....\n.....OOOO.\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n",
"XXOXX.....\nOO.O......\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n"
] | [
"YES\n",
"NO\n"
] | none | [
{
"input": "O.......O.\n.....O.X..\n......O...\n....X.O...\n.O.O.....X\n.XO.....XX\n...X...X.O\n........O.\n........O.\n.X.X.....X",
"output": "NO"
},
{
"input": "....OX....\n..........\n.O..X...X.\nXXO..XO..O\nO.......X.\n...XX.....\n..O.O...OX\n.........X\n.....X..OO\n........O.",
"output": "N... | 124 | 0 | 0 | 23,074 | |
691 | Couple Cover | [
"brute force",
"dp",
"number theory"
] | null | null | Couple Cover, a wildly popular luck-based game, is about to begin! Two players must work together to construct a rectangle. A bag with *n* balls, each with an integer written on it, is placed on the table. The first player reaches in and grabs a ball randomly (all balls have equal probability of being chosen) — the num... | The input begins with a single positive integer *n* in its own line (1<=≤<=*n*<=≤<=106).
The second line contains *n* positive integers — the *i*-th number in this line is equal to *a**i* (1<=≤<=*a**i*<=≤<=3·106), the number written on the *i*-th ball.
The next line contains an integer *m* (1<=≤<=*m*<=≤<=106), the nu... | For each question, print the number of winning pairs of balls that exist for the given value of *p* in the separate line. | [
"5\n4 2 6 1 3\n4\n1 3 5 8\n",
"2\n5 6\n2\n30 31\n"
] | [
"20\n18\n14\n10\n",
"2\n0\n"
] | none | [
{
"input": "5\n4 2 6 1 3\n4\n1 3 5 8",
"output": "20\n18\n14\n10"
},
{
"input": "2\n5 6\n2\n30 31",
"output": "2\n0"
},
{
"input": "2\n2000000 2000000\n1\n2000000",
"output": "2"
},
{
"input": "1\n1\n1\n5",
"output": "0"
},
{
"input": "10\n18 34 3 49 40 50 53 30 2... | 46 | 0 | 0 | 23,089 | |
707 | Bakery | [
"graphs"
] | null | null | Masha wants to open her own bakery and bake muffins in one of the *n* cities numbered from 1 to *n*. There are *m* bidirectional roads, each of whose connects some pair of cities.
To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only *k* storages, located in different c... | The first line of the input contains three integers *n*, *m* and *k* (1<=≤<=*n*,<=*m*<=≤<=105, 0<=≤<=*k*<=≤<=*n*) — the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively.
Then *m* lines follow. Each of them contains three integers *u*, *v* and *l... | Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line.
If the bakery can not be opened (while satisfying conditions) in any of the *n* cities, print <=-<=1 in the only line. | [
"5 4 2\n1 2 5\n1 2 3\n2 3 4\n1 4 10\n1 5\n",
"3 1 1\n1 2 3\n3\n"
] | [
"3",
"-1"
] | Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened. | [
{
"input": "5 4 2\n1 2 5\n1 2 3\n2 3 4\n1 4 10\n1 5",
"output": "3"
},
{
"input": "3 1 1\n1 2 3\n3",
"output": "-1"
},
{
"input": "2 3 1\n1 2 3\n1 2 18\n1 2 13\n2",
"output": "3"
},
{
"input": "3 7 0\n1 3 9\n1 2 5\n1 2 21\n1 2 12\n1 2 13\n2 3 19\n2 3 8",
"output": "-1"
... | 46 | 134,860,800 | 0 | 23,141 | |
69 | Dot | [
"dp",
"games"
] | D. Dot | 3 | 256 | Anton and Dasha like to play different games during breaks on checkered paper. By the 11th grade they managed to play all the games of this type and asked Vova the programmer to come up with a new game. Vova suggested to them to play a game under the code name "dot" with the following rules:
- On the checkered paper ... | The first line of the input file contains 4 integers *x*, *y*, *n*, *d* (<=-<=200<=≤<=*x*,<=*y*<=≤<=200,<=1<=≤<=*d*<=≤<=200,<=1<=≤<=*n*<=≤<=20) — the initial coordinates of the dot, the distance *d* and the number of vectors. It is guaranteed that the initial dot is at the distance less than *d* from the origin of the ... | You should print "Anton", if the winner is Anton in case of both players play the game optimally, and "Dasha" otherwise. | [
"0 0 2 3\n1 1\n1 2\n",
"0 0 2 4\n1 1\n1 2\n"
] | [
"Anton",
"Dasha"
] | In the first test, Anton goes to the vector (1;2), and Dasha loses. In the second test Dasha with her first move shifts the dot so that its coordinates are (2;3), and Anton loses, as he has the only possible move — to reflect relatively to the line *y* = *x*. Dasha will respond to it with the same move and return the d... | [
{
"input": "0 0 2 3\n1 1\n1 2",
"output": "Anton"
},
{
"input": "0 0 2 4\n1 1\n1 2",
"output": "Dasha"
},
{
"input": "0 0 5 100\n12 105\n15 59\n21 1\n27 6\n27 76",
"output": "Anton"
},
{
"input": "0 0 5 100\n16 24\n29 6\n44 24\n66 37\n102 19",
"output": "Anton"
},
{
... | 872 | 22,220,800 | 3.813277 | 23,160 |
0 | none | [
"none"
] | null | null | For each string *s* consisting of characters '0' and '1' one can define four integers *a*00, *a*01, *a*10 and *a*11, where *a**xy* is the number of subsequences of length 2 of the string *s* equal to the sequence {*x*,<=*y*}.
In these problem you are given four integers *a*00, *a*01, *a*10, *a*11 and have to find any... | The only line of the input contains four non-negative integers *a*00, *a*01, *a*10 and *a*11. Each of them doesn't exceed 109. | If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1<=000<=000. | [
"1 2 3 4\n",
"1 2 2 1\n"
] | [
"Impossible\n",
"0110\n"
] | none | [
{
"input": "1 2 3 4",
"output": "Impossible"
},
{
"input": "1 2 2 1",
"output": "0110"
},
{
"input": "10 7 28 21",
"output": "011111110000"
},
{
"input": "0 0 0 0",
"output": "0"
},
{
"input": "499928010 820999488 178951395 499991253",
"output": "0000000000000... | 31 | 0 | 0 | 23,176 | |
225 | Unsolvable | [
"math",
"number theory"
] | null | null | Consider the following equation:
Let's find all integer *z* (*z*<=><=0), for which this equation is unsolvable in positive integers. The phrase "unsolvable in positive integers" means that there are no such positive integers *x* and *y* (*x*,<=*y*<=><=0), for which the given above equation holds.
Let's write o... | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=40). | Print a single integer — the number *z**n* modulo 1000000007 (109<=+<=7). It is guaranteed that the answer exists. | [
"1\n",
"2\n",
"3\n"
] | [
"1",
"3",
"15"
] | none | [
{
"input": "1",
"output": "1"
},
{
"input": "2",
"output": "3"
},
{
"input": "3",
"output": "15"
},
{
"input": "4",
"output": "63"
},
{
"input": "5",
"output": "4095"
},
{
"input": "6",
"output": "65535"
},
{
"input": "7",
"output": "26... | 248 | 0 | 0 | 23,197 | |
732 | Cormen --- The Best Friend Of a Man | [
"dp",
"greedy"
] | null | null | Recently a dog was bought for Polycarp. The dog's name is Cormen. Now Polycarp has a lot of troubles. For example, Cormen likes going for a walk.
Empirically Polycarp learned that the dog needs at least *k* walks for any two consecutive days in order to feel good. For example, if *k*<==<=5 and yesterday Polycarp went... | The first line contains two integers *n* and *k* (1<=≤<=*n*,<=*k*<=≤<=500) — the number of days and the minimum number of walks with Cormen for any two consecutive days.
The second line contains integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=500) — the number of walks with Cormen on the *i*-th day which Polyca... | In the first line print the smallest number of additional walks that Polycarp should do during the next *n* days so that Cormen will feel good during all days.
In the second line print *n* integers *b*1,<=*b*2,<=...,<=*b**n*, where *b**i* — the total number of walks on the *i*-th day according to the found solutions ... | [
"3 5\n2 0 1\n",
"3 1\n0 0 0\n",
"4 6\n2 4 3 5\n"
] | [
"4\n2 3 2\n",
"1\n0 1 0\n",
"0\n2 4 3 5\n"
] | none | [
{
"input": "3 5\n2 0 1",
"output": "4\n2 3 2"
},
{
"input": "3 1\n0 0 0",
"output": "1\n0 1 0"
},
{
"input": "4 6\n2 4 3 5",
"output": "0\n2 4 3 5"
},
{
"input": "5 1\n0 0 0 0 1",
"output": "2\n0 1 0 1 1"
},
{
"input": "10 500\n164 44 238 205 373 249 87 30 239 90"... | 62 | 0 | 3 | 23,202 | |
413 | Jeopardy! | [
"greedy",
"math"
] | null | null | 'Jeopardy!' is an intellectual game where players answer questions and earn points. Company Q conducts a simplified 'Jeopardy!' tournament among the best IT companies. By a lucky coincidence, the old rivals made it to the finals: company R1 and company R2.
The finals will have *n* questions, *m* of them are auction q... | The first line contains two space-separated integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=100; *m*<=≤<=*min*(*n*,<=30)) — the total number of questions and the number of auction questions, correspondingly. The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=107) — the prices o... | In the single line, print the answer to the problem — the maximum points the R2 company can get if it plays optimally well. It is guaranteed that the answer fits into the integer 64-bit signed type. | [
"4 1\n1 3 7 5\n3\n",
"3 2\n10 3 8\n2 3\n",
"2 2\n100 200\n1 2\n"
] | [
"18\n",
"40\n",
"400\n"
] | none | [
{
"input": "4 1\n1 3 7 5\n3",
"output": "18"
},
{
"input": "3 2\n10 3 8\n2 3",
"output": "40"
},
{
"input": "2 2\n100 200\n1 2",
"output": "400"
},
{
"input": "1 1\n1\n1",
"output": "1"
},
{
"input": "2 2\n1 5\n1 2",
"output": "10"
},
{
"input": "5 3\n... | 62 | 0 | 0 | 23,214 | |
309 | Memory for Arrays | [
"binary search",
"bitmasks",
"greedy"
] | null | null | You get to work and turn on the computer. You start coding and give little thought to the RAM role in the whole process. In this problem your task is to solve one of the problems you encounter in your computer routine.
We'll consider the RAM as a sequence of cells that can contain data. Some cells already contain some... | The first line of the input contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=106). The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109). The next line contains *m* integers *b*1,<=*b*2,<=...,<=*b**m* (1<=≤<=2*b**i*<=≤<=109). | Print a single integer — the answer to the problem. | [
"5 3\n8 4 3 2 2\n3 2 2\n",
"10 6\n1 1 1 1 1 1 1 1 1 1\n0 0 0 0 0 0\n"
] | [
"2\n",
"6\n"
] | In the first example you are given memory clusters with sizes 8, 4, 3, 2, 2 and arrays with sizes 8, 4, 4. There are few ways to obtain an answer equals 2: you can locate array with size 8 to the cluster with size 8, and one of the arrays with size 4 to the cluster with size 4. Another way is to locate two arrays with ... | [
{
"input": "5 3\n8 4 3 2 2\n3 2 2",
"output": "2"
},
{
"input": "10 6\n1 1 1 1 1 1 1 1 1 1\n0 0 0 0 0 0",
"output": "6"
},
{
"input": "5 10\n4 4 3 3 3\n0 0 0 0 0 2 0 2 0 0",
"output": "10"
},
{
"input": "5 10\n3 4 5 4 3\n1 2 1 0 1 2 1 0 2 0",
"output": "9"
},
{
"i... | 1,154 | 108,236,800 | 0 | 23,282 | |
813 | Bipartite Checking | [
"data structures",
"dsu",
"graphs"
] | null | null | You are given an undirected graph consisting of *n* vertices. Initially there are no edges in the graph. Also you are given *q* queries, each query either adds one undirected edge to the graph or removes it. After each query you have to check if the resulting graph is bipartite (that is, you can paint all vertices of t... | The first line contains two integers *n* and *q* (2<=≤<=*n*,<=*q*<=≤<=100000).
Then *q* lines follow. *i*th line contains two numbers *x**i* and *y**i* (1<=≤<=*x**i*<=<<=*y**i*<=≤<=*n*). These numbers describe *i*th query: if there is an edge between vertices *x**i* and *y**i*, then remove it, otherwise add it. | Print *q* lines. *i*th line must contain YES if the graph is bipartite after *i*th query, and NO otherwise. | [
"3 5\n2 3\n1 3\n1 2\n1 2\n1 2\n"
] | [
"YES\nYES\nNO\nYES\nNO\n"
] | none | [
{
"input": "3 5\n2 3\n1 3\n1 2\n1 2\n1 2",
"output": "YES\nYES\nNO\nYES\nNO"
},
{
"input": "5 10\n1 5\n2 5\n2 4\n1 4\n4 5\n2 4\n2 5\n1 4\n2 3\n1 2",
"output": "YES\nYES\nYES\nYES\nNO\nNO\nNO\nYES\nYES\nYES"
},
{
"input": "10 20\n1 10\n5 7\n1 2\n3 5\n3 6\n4 9\n3 4\n6 9\n4 8\n6 9\n7 8\n3 8... | 46 | 204,800 | -1 | 23,292 | |
731 | Socks | [
"dfs and similar",
"dsu",
"graphs",
"greedy"
] | null | null | Arseniy is already grown-up and independent. His mother decided to leave him alone for *m* days and left on a vacation. She have prepared a lot of food, left some money and washed all Arseniy's clothes.
Ten minutes before her leave she realized that it would be also useful to prepare instruction of which particular c... | The first line of input contains three integers *n*, *m* and *k* (2<=≤<=*n*<=≤<=200<=000, 0<=≤<=*m*<=≤<=200<=000, 1<=≤<=*k*<=≤<=200<=000) — the number of socks, the number of days and the number of available colors respectively.
The second line contain *n* integers *c*1, *c*2, ..., *c**n* (1<=≤<=*c**i*<=≤<=*k*) — curr... | Print one integer — the minimum number of socks that should have their colors changed in order to be able to obey the instructions and not make people laugh from watching the socks of different colors. | [
"3 2 3\n1 2 3\n1 2\n2 3\n",
"3 2 2\n1 1 2\n1 2\n2 1\n"
] | [
"2\n",
"0\n"
] | In the first sample, Arseniy can repaint the first and the third socks to the second color.
In the second sample, there is no need to change any colors. | [
{
"input": "3 2 3\n1 2 3\n1 2\n2 3",
"output": "2"
},
{
"input": "3 2 2\n1 1 2\n1 2\n2 1",
"output": "0"
},
{
"input": "3 3 3\n1 2 3\n1 2\n2 3\n3 1",
"output": "2"
},
{
"input": "4 2 4\n1 2 3 4\n1 2\n3 4",
"output": "2"
},
{
"input": "10 3 2\n2 1 1 2 1 1 2 1 2 2\n... | 2,000 | 89,907,200 | 0 | 23,325 | |
183 | Zoo | [
"brute force",
"geometry"
] | null | null | The Zoo in the Grid Kingdom is represented by an infinite grid. The Zoo has *n* observation binoculars located at the *OX* axis. For each *i* between 1 and *n*, inclusive, there exists a single binocular located at the point with coordinates (*i*,<=0). There are *m* flamingos in the Zoo, located at points with positive... | The first line contains two space-separated integers *n* and *m* (1<=≤<=*n*<=≤<=106,<=1<=≤<=*m*<=≤<=250), denoting the number of binoculars and the number of flamingos, respectively.
Then *m* lines follow, the *i*-th line will contain two space-separated integers *x**i* and *y**i* (1<=≤<=*x**i*,<=*y**i*<=≤<=109), whic... | Print a single integer denoting the maximum total number of flamingos that can be seen by all the binoculars. | [
"5 5\n2 1\n4 1\n3 2\n4 3\n4 4\n"
] | [
"11\n"
] | This picture shows the answer to the example test case. | [
{
"input": "5 5\n2 1\n4 1\n3 2\n4 3\n4 4",
"output": "11"
},
{
"input": "3 3\n1 1\n2 10\n3 100",
"output": "3"
},
{
"input": "1 2\n450000001 500000000\n900000001 1000000000",
"output": "2"
},
{
"input": "3 6\n1 1\n1 2\n1 3\n2 1\n2 2\n3 1",
"output": "7"
},
{
"inpu... | 92 | 307,200 | 0 | 23,355 | |
859 | Desk Disorder | [
"combinatorics",
"dfs and similar",
"dsu",
"graphs",
"trees"
] | null | null | A new set of desks just arrived, and it's about time! Things were getting quite cramped in the office. You've been put in charge of creating a new seating chart for the engineers. The desks are numbered, and you sent out a survey to the engineering team asking each engineer the number of the desk they currently sit at,... | Input will begin with a line containing *N* (1<=≤<=*N*<=≤<=100000), the number of engineers.
*N* lines follow, each containing exactly two integers. The *i*-th line contains the number of the current desk of the *i*-th engineer and the number of the desk the *i*-th engineer wants to move to. Desks are numbered from 1... | Print the number of possible assignments, modulo 1000000007<==<=109<=+<=7. | [
"4\n1 5\n5 2\n3 7\n7 3\n",
"5\n1 10\n2 10\n3 10\n4 10\n5 5\n"
] | [
"6\n",
"5\n"
] | These are the possible assignments for the first example:
- 1 5 3 7 - 1 2 3 7 - 5 2 3 7 - 1 5 7 3 - 1 2 7 3 - 5 2 7 3 | [
{
"input": "4\n1 5\n5 2\n3 7\n7 3",
"output": "6"
},
{
"input": "5\n1 10\n2 10\n3 10\n4 10\n5 5",
"output": "5"
},
{
"input": "1\n1 2",
"output": "2"
},
{
"input": "30\n22 37\n12 37\n37 58\n29 57\n43 57\n57 58\n58 53\n45 4\n1 4\n4 51\n35 31\n21 31\n31 51\n51 53\n53 48\n60 55\... | 93 | 4,710,400 | 0 | 23,503 | |
986 | Fair | [
"graphs",
"greedy",
"number theory",
"shortest paths"
] | null | null | Some company is going to hold a fair in Byteland. There are $n$ towns in Byteland and $m$ two-way roads between towns. Of course, you can reach any town from any other town using roads.
There are $k$ types of goods produced in Byteland and every town produces only one type. To hold a fair you have to bring at least $s... | There are $4$ integers $n$, $m$, $k$, $s$ in the first line of input ($1 \le n \le 10^{5}$, $0 \le m \le 10^{5}$, $1 \le s \le k \le min(n, 100)$) — the number of towns, the number of roads, the number of different types of goods, the number of different types of goods necessary to hold a fair.
In the next line there ... | Print $n$ numbers, the $i$-th of them is the minimum number of coins you need to spend on travel expenses to hold a fair in town $i$. Separate numbers with spaces. | [
"5 5 4 3\n1 2 4 3 2\n1 2\n2 3\n3 4\n4 1\n4 5\n",
"7 6 3 2\n1 2 3 3 2 2 1\n1 2\n2 3\n3 4\n2 5\n5 6\n6 7\n"
] | [
"2 2 2 2 3 \n",
"1 1 1 2 2 1 1 \n"
] | Let's look at the first sample.
To hold a fair in town $1$ you can bring goods from towns $1$ ($0$ coins), $2$ ($1$ coin) and $4$ ($1$ coin). Total numbers of coins is $2$.
Town $2$: Goods from towns $2$ ($0$), $1$ ($1$), $3$ ($1$). Sum equals $2$.
Town $3$: Goods from towns $3$ ($0$), $2$ ($1$), $4$ ($1$). Sum equa... | [] | 2,000 | 93,900,800 | 0 | 23,512 | |
264 | Choosing Balls | [
"dp"
] | null | null | There are *n* balls. They are arranged in a row. Each ball has a color (for convenience an integer) and an integer value. The color of the *i*-th ball is *c**i* and the value of the *i*-th ball is *v**i*.
Squirrel Liss chooses some balls and makes a new sequence without changing the relative order of the balls. She wa... | The first line contains two integers *n* and *q* (1<=≤<=*n*<=≤<=105; 1<=≤<=*q*<=≤<=500). The second line contains *n* integers: *v*1,<=*v*2,<=...,<=*v**n* (|*v**i*|<=≤<=105). The third line contains *n* integers: *c*1,<=*c*2,<=...,<=*c**n* (1<=≤<=*c**i*<=≤<=*n*).
The following *q* lines contain the values of the const... | For each query, output a line containing an integer — the answer to the query. The *i*-th line contains the answer to the *i*-th query in the input order.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"6 3\n1 -2 3 4 0 -1\n1 2 1 2 1 1\n5 1\n-2 1\n1 0\n",
"4 1\n-3 6 -1 2\n1 2 3 1\n1 -1\n"
] | [
"20\n9\n4\n",
"5\n"
] | In the first example, to achieve the maximal value:
- In the first query, you should select 1st, 3rd, and 4th ball. - In the second query, you should select 3rd, 4th, 5th and 6th ball. - In the third query, you should select 2nd and 4th ball.
Note that there may be other ways to achieve the maximal value. | [] | 124 | 4,812,800 | 0 | 23,513 | |
10 | Digital Root | [
"number theory"
] | C. Digital Root | 2 | 256 | Not long ago Billy came across such a problem, where there were given three natural numbers *A*, *B* and *C* from the range [1,<=*N*], and it was asked to check whether the equation *AB*<==<=*C* is correct. Recently Billy studied the concept of a digital root of a number. We should remind you that a digital root *d*(*x... | The first line contains the only number *N* (1<=≤<=*N*<=≤<=106). | Output one number — the amount of required *A*, *B* and *C* from the range [1,<=*N*]. | [
"4\n",
"5\n"
] | [
"2\n",
"6\n"
] | For the first sample the required triples are (3, 4, 3) and (4, 3, 3). | [
{
"input": "4",
"output": "2"
},
{
"input": "5",
"output": "6"
},
{
"input": "6",
"output": "14"
},
{
"input": "7",
"output": "25"
},
{
"input": "1",
"output": "0"
},
{
"input": "8",
"output": "40"
},
{
"input": "10",
"output": "82"
}... | 218 | 1,433,600 | 3.94283 | 23,582 |
388 | Fox and Card Game | [
"games",
"greedy",
"sortings"
] | null | null | Fox Ciel is playing a card game with her friend Fox Jiro. There are *n* piles of cards on the table. And there is a positive integer on each card.
The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom... | The first line contain an integer *n* (1<=≤<=*n*<=≤<=100). Each of the next *n* lines contains a description of the pile: the first integer in the line is *s**i* (1<=≤<=*s**i*<=≤<=100) — the number of cards in the *i*-th pile; then follow *s**i* positive integers *c*1, *c*2, ..., *c**k*, ..., *c**s**i* (1<=≤<=*c**k*<=≤... | Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally. | [
"2\n1 100\n2 1 10\n",
"1\n9 2 8 6 5 9 4 7 1 3\n",
"3\n3 1 3 2\n3 5 4 6\n2 8 7\n",
"3\n3 1000 1000 1000\n6 1000 1000 1000 1000 1000 1000\n5 1000 1000 1000 1000 1000\n"
] | [
"101 10\n",
"30 15\n",
"18 18\n",
"7000 7000\n"
] | In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.
In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. | [
{
"input": "2\n1 100\n2 1 10",
"output": "101 10"
},
{
"input": "1\n9 2 8 6 5 9 4 7 1 3",
"output": "30 15"
},
{
"input": "3\n3 1 3 2\n3 5 4 6\n2 8 7",
"output": "18 18"
},
{
"input": "3\n3 1000 1000 1000\n6 1000 1000 1000 1000 1000 1000\n5 1000 1000 1000 1000 1000",
"out... | 0 | 0 | -1 | 23,588 | |
392 | Blocked Points | [
"math"
] | null | null | Imagine you have an infinite 2D plane with Cartesian coordinate system. Some of the integral points are blocked, and others are not. Two integral points *A* and *B* on the plane are 4-connected if and only if:
- the Euclidean distance between *A* and *B* is one unit and neither *A* nor *B* is blocked; - or there is ... | The first line contains an integer *n* (0<=≤<=*n*<=≤<=4·107). | Print a single integer — the minimum number of points that should be blocked. | [
"1\n",
"2\n",
"3\n"
] | [
"4\n",
"8\n",
"16\n"
] | none | [
{
"input": "1",
"output": "4"
},
{
"input": "2",
"output": "8"
},
{
"input": "3",
"output": "16"
},
{
"input": "4",
"output": "20"
},
{
"input": "0",
"output": "1"
},
{
"input": "30426905",
"output": "172120564"
},
{
"input": "38450759",
... | 109 | 0 | 0 | 23,613 | |
215 | Periodical Numbers | [
"combinatorics",
"dp",
"number theory"
] | null | null | A non-empty string *s* is called binary, if it consists only of characters "0" and "1". Let's number the characters of binary string *s* from 1 to the string's length and let's denote the *i*-th character in string *s* as *s**i*.
Binary string *s* with length *n* is periodical, if there is an integer 1<=≤<=*k*<=<<=... | The single input line contains two integers *l* and *r* (1<=≤<=*l*<=≤<=*r*<=≤<=1018). The numbers are separated by a space.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | Print a single integer, showing how many periodic numbers are in the interval from *l* to *r* (both ends are included). | [
"1 10\n",
"25 38\n"
] | [
"3\n",
"2\n"
] | In the first sample periodic numbers are 3, 7 and 10.
In the second sample periodic numbers are 31 and 36. | [
{
"input": "1 10",
"output": "3"
},
{
"input": "25 38",
"output": "2"
},
{
"input": "7 9",
"output": "1"
},
{
"input": "12 20",
"output": "1"
},
{
"input": "7 49",
"output": "7"
},
{
"input": "28 97",
"output": "6"
},
{
"input": "65 72",
... | 92 | 0 | -1 | 23,615 | |
814 | An overnight dance in discotheque | [
"dfs and similar",
"dp",
"geometry",
"greedy",
"trees"
] | null | null | The crowdedness of the discotheque would never stop our friends from having fun, but a bit more spaciousness won't hurt, will it?
The discotheque can be seen as an infinite *xy*-plane, in which there are a total of *n* dancers. Once someone starts moving around, they will move only inside their own movement range, whi... | The first line of input contains a positive integer *n* (1<=≤<=*n*<=≤<=1<=000) — the number of dancers.
The following *n* lines each describes a dancer: the *i*-th line among them contains three space-separated integers *x**i*, *y**i* and *r**i* (<=-<=106<=≤<=*x**i*,<=*y**i*<=≤<=106, 1<=≤<=*r**i*<=≤<=106), describing ... | Output one decimal number — the largest achievable sum of spaciousness over two halves of the night.
The output is considered correct if it has a relative or absolute error of at most 10<=-<=9. Formally, let your answer be *a*, and the jury's answer be *b*. Your answer is considered correct if . | [
"5\n2 1 6\n0 4 1\n2 -1 3\n1 -2 1\n4 -1 1\n",
"8\n0 0 1\n0 0 2\n0 0 3\n0 0 4\n0 0 5\n0 0 6\n0 0 7\n0 0 8\n"
] | [
"138.23007676\n",
"289.02652413\n"
] | The first sample corresponds to the illustrations in the legend. | [
{
"input": "5\n2 1 6\n0 4 1\n2 -1 3\n1 -2 1\n4 -1 1",
"output": "138.23007676"
},
{
"input": "8\n0 0 1\n0 0 2\n0 0 3\n0 0 4\n0 0 5\n0 0 6\n0 0 7\n0 0 8",
"output": "289.02652413"
},
{
"input": "4\n1000000 -1000000 2\n1000000 -1000000 3\n-1000000 1000000 2\n-1000000 1000000 1000000",
... | 920 | 6,144,000 | 3 | 23,684 | |
301 | Yaroslav and Divisors | [
"data structures"
] | null | null | Yaroslav has an array *p*<==<=*p*1,<=*p*2,<=...,<=*p**n* (1<=≤<=*p**i*<=≤<=*n*), consisting of *n* distinct integers. Also, he has *m* queries:
- Query number *i* is represented as a pair of integers *l**i*, *r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*). - The answer to the query *l**i*,<=*r**i* is the number of pairs of... | The first line contains the integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=2·105). The second line contains *n* distinct integers *p*1,<=*p*2,<=...,<=*p**n* (1<=≤<=*p**i*<=≤<=*n*). The following *m* lines contain Yaroslav's queries. The *i*-th line contains integers *l**i*,<=*r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*). | Print *m* integers — the answers to Yaroslav's queries in the order they appear in the input.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"1 1\n1\n1 1\n",
"10 9\n1 2 3 4 5 6 7 8 9 10\n1 10\n2 9\n3 8\n4 7\n5 6\n2 2\n9 10\n5 10\n4 10\n"
] | [
"1\n",
"27\n14\n8\n4\n2\n1\n2\n7\n9\n"
] | none | [] | 248 | 0 | 0 | 23,692 | |
190 | Counter Attack | [
"data structures",
"dsu",
"graphs",
"hashing",
"sortings"
] | null | null | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has *n* cities, numbered from 1 to *n*, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (... | The first line contains two space-separated integers *n* and *m* (1<=≤<=*n*<=≤<=5·105,<=0<=≤<=*m*<=≤<=106) — the number of cities and the number of roads marked on the flatland map, correspondingly.
Next *m* lines contain descriptions of the cities on the map. The *i*-th line contains two integers *a**i* and *b**i* (1... | On the first line print number *k* — the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following *k* lines first print *t**i* (1<=≤<... | [
"4 4\n1 2\n1 3\n4 2\n4 3\n",
"3 1\n1 2\n"
] | [
"2\n2 1 4 \n2 2 3 \n",
"1\n3 1 2 3 \n"
] | In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | [
{
"input": "4 4\n1 2\n1 3\n4 2\n4 3",
"output": "2\n2 1 4 \n2 2 3 "
},
{
"input": "3 1\n1 2",
"output": "1\n3 1 2 3 "
},
{
"input": "8 14\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n5 6\n6 7",
"output": "2\n2 1 2 \n6 3 4 5 6 7 8 "
},
{
"input": "6 9\n1 4\n1 5... | 3,000 | 56,934,400 | 0 | 23,697 | |
504 | Misha and XOR | [
"bitmasks"
] | null | null | After Misha's birthday he had many large numbers left, scattered across the room. Now it's time to clean up and Misha needs to put them in a basket. He ordered this task to his pet robot that agreed to complete the task at certain conditions. Before the robot puts a number *x* to the basket, Misha should answer the que... | The first line contains number *m* (1<=≤<=*m*<=≤<=2000), showing how many numbers are scattered around the room.
The next *m* lines contain the numbers in the order in which the robot puts them in the basket. Each number is a positive integer strictly less than 10600 that doesn't contain leading zeroes. | For each number either print a 0 on the corresponding line, if the number cannot be represented as a XOR sum of numbers that are in the basket, or print integer *k* showing how many numbers are in the representation and the indexes of these numbers. Separate the numbers by spaces. Each number can occur in the represent... | [
"7\n7\n6\n5\n4\n3\n2\n1\n",
"2\n5\n5\n"
] | [
"0\n0\n0\n3 0 1 2\n2 1 2\n2 0 2\n2 0 1\n",
"0\n1 0\n"
] | The XOR sum of numbers is the result of bitwise sum of numbers modulo 2. | [
{
"input": "7\n7\n6\n5\n4\n3\n2\n1",
"output": "0\n0\n0\n3 0 1 2\n2 1 2\n2 0 2\n2 0 1"
},
{
"input": "2\n5\n5",
"output": "0\n1 0"
},
{
"input": "10\n81\n97\n12\n2\n16\n96\n80\n99\n6\n83",
"output": "0\n0\n0\n0\n0\n0\n3 0 1 5\n2 1 3\n0\n2 0 3"
},
{
"input": "10\n15106\n13599\... | 31 | 0 | 0 | 23,759 | |
0 | none | [
"none"
] | null | null | You must have heard of the two brothers dreaming of ruling the world. With all their previous plans failed, this time they decided to cooperate with each other in order to rule the world.
As you know there are *n* countries in the world. These countries are connected by *n*<=-<=1 directed roads. If you don't consider... | The first line of input contains an integer *n* (1<=≤<=*n*<=≤<=3000). Each of the next *n*<=-<=1 lines contains two space-separated integers *a**i* and *b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*; *a**i*<=≠<=*b**i*) saying there is a road from country *a**i* to country *b**i*.
Consider that countries are numbered from 1 to ... | In the only line of output print the minimum number of roads that their direction should be changed so that the brothers will be able to rule the world. | [
"4\n2 1\n3 1\n4 1\n",
"5\n2 1\n2 3\n4 3\n4 5\n"
] | [
"1\n",
"0\n"
] | none | [] | 92 | 0 | 0 | 23,785 | |
138 | Literature Lesson | [
"implementation"
] | null | null | Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes.
Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels.
Two lines rhyme if their ... | The first line contains two integers *n* and *k* (1<=≤<=*n*<=≤<=2500, 1<=≤<=*k*<=≤<=5) — the number of quatrains in the poem and the vowel's number, correspondingly. Next 4*n* lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104.
If ... | Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. | [
"1 1\nday\nmay\nsun\nfun\n",
"1 1\nday\nmay\ngray\nway\n",
"2 1\na\na\na\na\na\na\ne\ne\n",
"2 1\nday\nmay\nsun\nfun\ntest\nhill\nfest\nthrill\n"
] | [
"aabb\n",
"aaaa\n",
"aabb\n",
"NO\n"
] | In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO". | [
{
"input": "1 1\nday\nmay\nsun\nfun",
"output": "aabb"
},
{
"input": "1 1\nday\nmay\ngray\nway",
"output": "aaaa"
},
{
"input": "2 1\na\na\na\na\na\na\ne\ne",
"output": "aabb"
},
{
"input": "2 1\nday\nmay\nsun\nfun\ntest\nhill\nfest\nthrill",
"output": "NO"
},
{
"... | 62 | 0 | 0 | 23,818 | |
0 | none | [
"none"
] | null | null | Let's assume that
- *v*(*n*) is the largest prime number, that does not exceed *n*;- *u*(*n*) is the smallest prime number strictly greater than *n*.
Find . | The first line contains integer *t* (1<=≤<=*t*<=≤<=500) — the number of testscases.
Each of the following *t* lines of the input contains integer *n* (2<=≤<=*n*<=≤<=109). | Print *t* lines: the *i*-th of them must contain the answer to the *i*-th test as an irreducible fraction "*p*/*q*", where *p*,<=*q* are integers, *q*<=><=0. | [
"2\n2\n3\n"
] | [
"1/6\n7/30\n"
] | none | [
{
"input": "2\n2\n3",
"output": "1/6\n7/30"
},
{
"input": "1\n1000000000",
"output": "999999941999999673/1999999887999999118"
},
{
"input": "5\n3\n6\n9\n10\n5",
"output": "7/30\n5/14\n61/154\n9/22\n23/70"
},
{
"input": "5\n5\n8\n18\n17\n17",
"output": "23/70\n59/154\n17/3... | 46 | 0 | 0 | 23,824 | |
201 | Guess That Car! | [
"math",
"ternary search"
] | null | null | A widely known among some people Belarusian sport programmer Yura possesses lots of information about cars. That is why he has been invited to participate in a game show called "Guess That Car!".
The game show takes place on a giant parking lot, which is 4*n* meters long from north to south and 4*m* meters wide from w... | The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=1000) — the sizes of the parking lot. Each of the next *n* lines contains *m* integers: the *j*-th number in the *i*-th line describes the "rarity" *c**ij* (0<=≤<=*c**ij*<=≤<=100000) of the car that is located in the square with coordinates (*i*,<=*j... | In the first line print the minimum total time Yura needs to guess all offered cars. In the second line print two numbers *l**i* and *l**j* (0<=≤<=*l**i*<=≤<=*n*,<=0<=≤<=*l**j*<=≤<=*m*) — the numbers of dividing lines that form a junction that Yura should choose to stand on at the beginning of the game show. If there a... | [
"2 3\n3 4 5\n3 9 1\n",
"3 4\n1 0 0 0\n0 0 3 0\n0 0 5 5\n"
] | [
"392\n1 1\n",
"240\n2 3\n"
] | In the first test case the total time of guessing all cars is equal to 3·8 + 3·8 + 4·8 + 9·8 + 5·40 + 1·40 = 392.
The coordinate system of the field: | [
{
"input": "2 3\n3 4 5\n3 9 1",
"output": "392\n1 1"
},
{
"input": "3 4\n1 0 0 0\n0 0 3 0\n0 0 5 5",
"output": "240\n2 3"
},
{
"input": "4 7\n91811 50656 97940 70982 88108 31856 80371\n94488 4672 15935 10209 22935 55803 57593\n79510 213 92494 7670 49727 97141 54221\n89004 67886 99994 222... | 124 | 0 | 0 | 23,839 | |
201 | Clear Symmetry | [
"constructive algorithms",
"dp",
"math"
] | null | null | Consider some square matrix *A* with side *n* consisting of zeros and ones. There are *n* rows numbered from 1 to *n* from top to bottom and *n* columns numbered from 1 to *n* from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the *i*-row and the *j*-th col... | The only line contains a single integer *x* (1<=≤<=*x*<=≤<=100) — the required sharpness of the matrix. | Print a single number — the sought value of *n*. | [
"4\n",
"9\n"
] | [
"3\n",
"5\n"
] | The figure below shows the matrices that correspond to the samples: | [
{
"input": "4",
"output": "3"
},
{
"input": "9",
"output": "5"
},
{
"input": "10",
"output": "5"
},
{
"input": "12",
"output": "5"
},
{
"input": "1",
"output": "1"
},
{
"input": "19",
"output": "7"
},
{
"input": "3",
"output": "5"
},
... | 248 | 0 | 0 | 23,880 | |
908 | New Year and Buggy Bot | [
"brute force",
"implementation"
] | null | null | Bob programmed a robot to navigate through a 2d maze.
The maze has some obstacles. Empty cells are denoted by the character '.', where obstacles are denoted by '#'.
There is a single robot in the maze. Its start position is denoted with the character 'S'. This position has no obstacle in it. There is also a single ex... | The first line of input will contain two integers *n* and *m* (2<=≤<=*n*,<=*m*<=≤<=50), denoting the dimensions of the maze.
The next *n* lines will contain exactly *m* characters each, denoting the maze.
Each character of the maze will be '.', '#', 'S', or 'E'.
There will be exactly one 'S' and exactly one 'E' in t... | Print a single integer, the number of mappings of digits to directions that will lead the robot to the exit. | [
"5 6\n.....#\nS....#\n.#....\n.#....\n...E..\n333300012\n",
"6 6\n......\n......\n..SE..\n......\n......\n......\n01232123212302123021\n",
"5 3\n...\n.S.\n###\n.E.\n...\n3\n"
] | [
"1\n",
"14\n",
"0\n"
] | For the first sample, the only valid mapping is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/87a55361bde12e4223a96f0e1d83b94428f26f02.png" style="max-width: 100.0%;max-height: 100.0%;"/>, where *D* is down, *L* is left, *U* is up, *R* is right. | [
{
"input": "5 6\n.....#\nS....#\n.#....\n.#....\n...E..\n333300012",
"output": "1"
},
{
"input": "6 6\n......\n......\n..SE..\n......\n......\n......\n01232123212302123021",
"output": "14"
},
{
"input": "5 3\n...\n.S.\n###\n.E.\n...\n3",
"output": "0"
},
{
"input": "10 10\n.#... | 46 | 0 | 3 | 23,922 | |
958 | Maximum Control (medium) | [
"data structures",
"dfs and similar",
"graphs",
"greedy",
"trees"
] | null | null | The Resistance is trying to take control over as many planets of a particular solar system as possible. Princess Heidi is in charge of the fleet, and she must send ships to some planets in order to maximize the number of controlled planets.
The Galaxy contains *N* planets, connected by bidirectional hyperspace tunnels... | The first line of the input contains an integer *N* (1<=≤<=*N*<=≤<=105) – the number of planets in the galaxy.
The next *N*<=-<=1 lines describe the hyperspace tunnels between the planets. Each of the *N*<=-<=1 lines contains two space-separated integers *u* and *v* (1<=≤<=*u*,<=*v*<=≤<=*N*) indicating that there is a... | On a single line, print *N* space-separated integers. The *K*-th number should correspond to the maximum number of planets that can be controlled by the Resistance using a fleet of *K* ships. | [
"3\n1 2\n2 3\n",
"4\n1 2\n3 2\n4 2\n"
] | [
"1 3 3 ",
"1 3 4 4 "
] | Consider the first example. If *K* = 1, then Heidi can only send one ship to some planet and control it. However, for *K* ≥ 2, sending ships to planets 1 and 3 will allow the Resistance to control all planets. | [
{
"input": "3\n1 2\n2 3",
"output": "1 3 3 "
},
{
"input": "4\n1 2\n3 2\n4 2",
"output": "1 3 4 4 "
},
{
"input": "19\n2 19\n7 15\n8 10\n16 1\n12 5\n11 5\n6 18\n12 14\n14 15\n2 6\n9 14\n4 17\n16 10\n4 2\n7 18\n3 2\n9 13\n11 10",
"output": "1 14 16 17 18 19 19 19 19 19 19 19 19 19 19 ... | 140 | 512,000 | 0 | 23,964 | |
825 | Minimal Labels | [
"data structures",
"dfs and similar",
"graphs",
"greedy"
] | null | null | You are given a directed acyclic graph with *n* vertices and *m* edges. There are no self-loops or multiple edges between any pair of vertices. Graph can be disconnected.
You should assign labels to all vertices in such a way that:
- Labels form a valid permutation of length *n* — an integer sequence such that each ... | The first line contains two integer numbers *n*, *m* (2<=≤<=*n*<=≤<=105,<=1<=≤<=*m*<=≤<=105).
Next *m* lines contain two integer numbers *v* and *u* (1<=≤<=*v*,<=*u*<=≤<=*n*,<=*v*<=≠<=*u*) — edges of the graph. Edges are directed, graph doesn't contain loops or multiple edges. | Print *n* numbers — lexicographically smallest correct permutation of labels of vertices. | [
"3 3\n1 2\n1 3\n3 2\n",
"4 5\n3 1\n4 1\n2 3\n3 4\n2 4\n",
"5 4\n3 1\n2 1\n2 3\n4 5\n"
] | [
"1 3 2 \n",
"4 1 2 3 \n",
"3 1 2 4 5 \n"
] | none | [
{
"input": "3 3\n1 2\n1 3\n3 2",
"output": "1 3 2 "
},
{
"input": "4 5\n3 1\n4 1\n2 3\n3 4\n2 4",
"output": "4 1 2 3 "
},
{
"input": "5 4\n3 1\n2 1\n2 3\n4 5",
"output": "3 1 2 4 5 "
},
{
"input": "2 1\n2 1",
"output": "2 1 "
},
{
"input": "5 10\n5 2\n4 1\n2 1\n3 ... | 390 | 30,924,800 | 3 | 24,040 | |
172 | BHTML+BCSS | [
"*special",
"dfs and similar",
"expression parsing"
] | null | null | This problem is about imaginary languages BHTML and BCSS, which slightly resemble HTML and CSS. Read the problem statement carefully as the resemblance is rather slight and the problem uses very simplified analogs.
You are given a BHTML document that resembles HTML but is much simpler. It is recorded as a sequence of ... | The first line of the input contains a BHTML-document. The document has length from 4 to 106 characters. The document has a correct structure, doesn't contain spaces or any other unnecessary characters. Tagnames consist of lowercase Latin letters, their lengths are from 1 to 10 characters.
The second line contains an ... | Print *m* lines, the *j*-th line should contain the number of elements of the document that correspond to the *j*-th BCSS-rule. If there are no such elements at all, print on the line 0. | [
"<a><b><b></b></b></a><a><b></b><b><v/></b></a><b></b>\n4\na\na b b\na b\nb a\n",
"<b><aa/></b><aa><b/><b/></aa>\n5\naa b\nb\naa\nb aa\na\n"
] | [
"2\n1\n4\n0\n",
"2\n3\n2\n1\n0\n"
] | none | [] | 46 | 0 | 0 | 24,057 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.