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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
91 | Igloo Skyscraper | [
"data structures",
"geometry"
] | E. Igloo Skyscraper | 3 | 256 | Today the North Pole hosts an Olympiad in a sport called... toy igloo skyscrapers' building!
There are *n* walruses taking part in the contest. Each walrus is given a unique number from 1 to *n*. After start each walrus begins to build his own igloo skyscraper. Initially, at the moment of time equal to 0, the height o... | The first line contains numbers *n* and *q* (1<=β€<=*n*,<=*q*<=β€<=105). Next *n* lines contain pairs of numbers *a**i*, *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=109). Then follow *q* queries i the following format *l**i*, *r**i*, *t**i*, one per each line (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*n*, 0<=β€<=*t**i*<=β€<=106). All input numbe... | For each journalists' query print the number of the walrus *x* that meets the criteria, given in the statement. Print one number per line. | [
"5 4\n4 1\n3 5\n6 2\n3 5\n6 5\n1 5 2\n1 3 5\n1 1 0\n1 5 0\n",
"5 4\n6 1\n5 1\n2 5\n4 3\n6 1\n2 4 1\n3 4 5\n1 4 5\n1 2 0\n"
] | [
"5\n2\n1\n5\n",
"3\n3\n3\n1\n"
] | none | [] | 0 | 0 | -1 | 97,320 |
435 | Special Grid | [
"brute force",
"dp",
"greedy"
] | null | null | You are given an *n*<=Γ<=*m* grid, some of its nodes are black, the others are white. Moreover, it's not an ordinary grid β each unit square of the grid has painted diagonals.
The figure below is an example of such grid of size 3<=Γ<=5. Four nodes of this grid are black, the other 11 nodes are white.
Your task is to ... | The first line contains two integers *n* and *m* (2<=β€<=*n*,<=*m*<=β€<=400). Each of the following *n* lines contain *m* characters (zeros and ones) β the description of the grid. If the *j*-th character in the *i*-th line equals zero, then the node on the *i*-th horizontal line and on the *j*-th vertical line is painte... | Print a single integer β the number of required triangles. | [
"3 5\n10000\n10010\n00001\n",
"2 2\n00\n00\n",
"2 2\n11\n11\n"
] | [
"20\n",
"4\n",
"0\n"
] | The figure below shows red and blue triangles. They are the examples of the required triangles in the first sample. One of the invalid triangles is painted green. It is invalid because not all sides go along the grid lines. | [
{
"input": "3 5\n10000\n10010\n00001",
"output": "20"
},
{
"input": "2 2\n00\n00",
"output": "4"
},
{
"input": "2 2\n11\n11",
"output": "0"
},
{
"input": "10 10\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n000000... | 30 | 0 | 0 | 97,343 | |
593 | Happy Tree Party | [
"data structures",
"dfs and similar",
"graphs",
"math",
"trees"
] | null | null | Bogdan has a birthday today and mom gave him a tree consisting of *n* vertecies. For every edge of the tree *i*, some number *x**i* was written on it. In case you forget, a tree is a connected non-directed graph without cycles. After the present was granted, *m* guests consecutively come to Bogdan's party. When the *i*... | The first line of the input contains integers, *n* and *m* (2<=β€<=*n*<=β€<=200<=000, 1<=β€<=*m*<=β€<=200<=000)Β β the number of vertecies in the tree granted to Bogdan by his mom and the number of guests that came to the party respectively.
Next *n*<=-<=1 lines contain the description of the edges. The *i*-th of these lin... | For each guest who chooses the operation of the first type, print the result of processing the value *y**i* through the path from *a**i* to *b**i*. | [
"6 6\n1 2 1\n1 3 7\n1 4 4\n2 5 5\n2 6 2\n1 4 6 17\n2 3 2\n1 4 6 17\n1 5 5 20\n2 4 1\n1 5 1 3\n",
"5 4\n1 2 7\n1 3 3\n3 4 2\n3 5 5\n1 4 2 100\n1 5 4 1\n2 2 2\n1 1 3 4\n"
] | [
"2\n4\n20\n3\n",
"2\n0\n2\n"
] | Initially the tree looks like this:
The response to the first query is: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5d118e070291dffa2524a35d044ed59aa2446202.png" style="max-width: 100.0%;max-height: 100.0%;"/> = 2
After the third edge is changed, the tree looks like this:
The respo... | [
{
"input": "6 6\n1 2 1\n1 3 7\n1 4 4\n2 5 5\n2 6 2\n1 4 6 17\n2 3 2\n1 4 6 17\n1 5 5 20\n2 4 1\n1 5 1 3",
"output": "2\n4\n20\n3"
},
{
"input": "5 4\n1 2 7\n1 3 3\n3 4 2\n3 5 5\n1 4 2 100\n1 5 4 1\n2 2 2\n1 1 3 4",
"output": "2\n0\n2"
},
{
"input": "2 1\n1 2 1\n1 2 1 1",
"output": "1... | 3,000 | 134,041,600 | 0 | 97,352 | |
212 | Polycarpus is Looking for Good Substrings | [
"bitmasks",
"hashing",
"implementation"
] | null | null | We'll call string *s*[*a*,<=*b*]<==<=*s**a**s**a*<=+<=1... *s**b* (1<=β€<=*a*<=β€<=*b*<=β€<=|*s*|) a substring of string *s*<==<=*s*1*s*2... *s*|*s*|, where |*s*| is the length of string *s*.
The trace of a non-empty string *t* is a set of characters that the string consists of. For example, the trace of string "aab" equ... | The first line contains a non-empty string *s* (1<=β€<=|*s*|<=β€<=106).
The second line contains a single integer *m* (1<=β€<=*m*<=β€<=104). Next *m* lines contain descriptions of sets *C**i*. The *i*-th line contains string *c**i* such that its trace equals *C**i*. It is guaranteed that all characters of each string *c**... | Print *m* integers β the *i*-th integer must equal *r*(*C**i*,<=*s*). | [
"aaaaa\n2\na\na\n",
"abacaba\n3\nac\nba\na\n"
] | [
"1\n1\n",
"1\n2\n4\n"
] | none | [] | 528 | 268,390,400 | 0 | 97,456 | |
290 | HQ | [
"*special",
"constructive algorithms"
] | null | null | The famous joke programming language HQ9+ has only 4 commands. In this problem we will explore its subset β a language called HQ... | The only line of the input is a string between 1 and 106 characters long. | Output "Yes" or "No". | [
"HHHH\n",
"HQHQH\n",
"HHQHHQH\n",
"HHQQHHQQHH\n"
] | [
"Yes\n",
"No\n",
"No\n",
"Yes\n"
] | The rest of the problem statement was destroyed by a stray raccoon. We are terribly sorry for the inconvenience. | [
{
"input": "HHHH",
"output": "Yes"
},
{
"input": "HQHQH",
"output": "No"
},
{
"input": "HHQHHQH",
"output": "No"
},
{
"input": "HHQQHHQQHH",
"output": "Yes"
},
{
"input": "Q",
"output": "Yes"
},
{
"input": "HHHHHHHHHHQHHH",
"output": "No"
},
{
... | 60 | 204,800 | 0 | 97,517 | |
533 | Correcting Mistakes | [
"constructive algorithms",
"dp",
"greedy",
"hashing",
"strings",
"two pointers"
] | null | null | Analyzing the mistakes people make while typing search queries is a complex and an interesting work. As there is no guaranteed way to determine what the user originally meant by typing some query, we have to use different sorts of heuristics.
Polycarp needed to write a code that could, given two words, check whether t... | The first line contains integer *n* (1<=β€<=*n*<=β€<=100<=000) β the length of words *S* and *T*.
The second line contains word *S*.
The third line contains word *T*.
Words *S* and *T* consist of lowercase English letters. It is guaranteed that *S* and *T* are distinct words. | Print a single integer β the number of distinct words *W* that can be transformed to *S* and *T* due to a typo. | [
"7\nreading\ntrading\n",
"5\nsweet\nsheep\n",
"3\ntoy\ntry\n"
] | [
"1\n",
"0\n",
"2\n"
] | In the first sample test the two given words could be obtained only from word "treading" (the deleted letters are marked in bold).
In the second sample test the two given words couldn't be obtained from the same word by removing one letter.
In the third sample test the two given words could be obtained from either wo... | [
{
"input": "7\nreading\ntrading",
"output": "1"
},
{
"input": "5\nsweet\nsheep",
"output": "0"
},
{
"input": "3\ntoy\ntry",
"output": "2"
},
{
"input": "5\nspare\nspars",
"output": "2"
},
{
"input": "1\na\nb",
"output": "2"
},
{
"input": "1\nz\ny",
... | 108 | 16,793,600 | 3 | 97,601 | |
0 | none | [
"none"
] | null | null | How many specific orders do you know? Ascending order, descending order, order of ascending length, order of ascending polar angle... Let's have a look at another specific order: *d*-sorting. This sorting is applied to the strings of length at least *d*, where *d* is some positive integer. The characters of the string ... | The first line of the input contains a non-empty string *S* of length *n*, consisting of lowercase and uppercase English letters and digits from 0 to 9.
The second line of the input contains integer *m*Β β the number of shuffling operations (1<=β€<=*m*Β·*n*<=β€<=106).
Following *m* lines contain the descriptions of the... | After each operation print the current state of string *S*. | [
"qwerty\n3\n4 2\n6 3\n5 2\n"
] | [
"qertwy\nqtewry\nqetyrw\n"
] | Here is detailed explanation of the sample. The first modification is executed with arguments *k*β=β4, *d*β=β2. That means that you need to apply 2-sorting for each substring of length 4 one by one moving from the left to the right. The string will transform in the following manner:
qwerty βββ qewrty βββ qerwty βββ qe... | [] | 2,000 | 87,040,000 | 0 | 97,603 | |
490 | Chocolate | [
"brute force",
"dfs and similar",
"math",
"meet-in-the-middle",
"number theory"
] | null | null | Polycarpus likes giving presents to Paraskevi. He has bought two chocolate bars, each of them has the shape of a segmented rectangle. The first bar is *a*1<=Γ<=*b*1 segments large and the second one is *a*2<=Γ<=*b*2 segments large.
Polycarpus wants to give Paraskevi one of the bars at the lunch break and eat the other... | The first line of the input contains integers *a*1,<=*b*1 (1<=β€<=*a*1,<=*b*1<=β€<=109) β the initial sizes of the first chocolate bar. The second line of the input contains integers *a*2,<=*b*2 (1<=β€<=*a*2,<=*b*2<=β€<=109) β the initial sizes of the second bar.
You can use the data of type int64 (in Pascal), long long (... | In the first line print *m* β the sought minimum number of minutes. In the second and third line print the possible sizes of the bars after they are leveled in *m* minutes. Print the sizes using the format identical to the input format. Print the sizes (the numbers in the printed pairs) in any order. The second line mu... | [
"2 6\n2 3\n",
"36 5\n10 16\n",
"3 5\n2 1\n"
] | [
"1\n1 6\n2 3\n",
"3\n16 5\n5 16\n",
"-1\n"
] | none | [
{
"input": "2 6\n2 3",
"output": "1\n1 6\n2 3"
},
{
"input": "36 5\n10 16",
"output": "3\n16 5\n5 16"
},
{
"input": "3 5\n2 1",
"output": "-1"
},
{
"input": "36 5\n10 12",
"output": "1\n24 5\n10 12"
},
{
"input": "1 1\n1 1",
"output": "0\n1 1\n1 1"
},
{
... | 46 | 0 | 0 | 98,021 | |
901 | Weighting a Tree | [
"constructive algorithms",
"dfs and similar",
"graphs"
] | null | null | You are given a connected undirected graph with *n* vertices and *m* edges. The vertices are enumerated from 1 to *n*.
You are given *n* integers *c*1,<=*c*2,<=...,<=*c**n*, each of them is between <=-<=*n* and *n*, inclusive. It is also guaranteed that the parity of *c**v* equals the parity of degree of vertex *v*. ... | The first line contains two integers *n* and *m* (2<=β€<=*n*<=β€<=105, *n*<=-<=1<=β€<=*m*<=β€<=105)Β β the number of vertices and the number of edges.
The next line contains *n* integers *c*1,<=*c*2,<=...,<=*c**n* (<=-<=*n*<=β€<=*c**i*<=β€<=*n*), where *c**i* is the required sum of weights of edges connected to vertex *i*. I... | If there is no solution, print "NO".
Otherwise print "YES" and then *m* lines, the *i*-th of them is the weight of the *i*-th edge *w**i* (<=-<=2Β·*n*2<=β€<=*w**i*<=β€<=2Β·*n*2). | [
"3 3\n2 2 2\n1 2\n2 3\n1 3\n",
"4 3\n-1 0 2 1\n1 2\n2 3\n3 4\n",
"6 6\n3 5 5 5 1 5\n1 4\n3 2\n4 3\n4 5\n3 5\n5 6\n",
"4 4\n4 4 2 4\n1 2\n2 3\n3 4\n4 1\n"
] | [
"YES\n1\n1\n1\n",
"YES\n-1\n1\n1\n",
"YES\n3\n5\n3\n-1\n-3\n5\n",
"NO"
] | none | [
{
"input": "3 3\n2 2 2\n1 2\n2 3\n1 3",
"output": "YES\n1\n1\n1"
},
{
"input": "4 3\n-1 0 2 1\n1 2\n2 3\n3 4",
"output": "YES\n-1\n1\n1"
},
{
"input": "6 6\n3 5 5 5 1 5\n1 4\n3 2\n4 3\n4 5\n3 5\n5 6",
"output": "YES\n3\n5\n3\n-1\n-3\n5"
},
{
"input": "4 4\n4 4 2 4\n1 2\n2 3\n... | 0 | 0 | -1 | 98,196 | |
0 | none | [
"none"
] | null | null | Andrey's favourite number is *n*. Andrey's friends gave him two identical numbers *n* as a New Year present. He hung them on a wall and watched them adoringly.
Then Andrey got bored from looking at the same number and he started to swap digits first in one, then in the other number, then again in the first number and ... | The first line contains a positive integer *n* β the original number. The number of digits in this number does not exceed 105. The number is written without any leading zeroes. | Print two permutations of digits of number *n*, such that the sum of these numbers ends with the maximum number of zeroes. The permutations can have leading zeroes (if they are present, they all should be printed). The permutations do not have to be different. If there are several answers, print any of them. | [
"198\n",
"500\n"
] | [
"981\n819\n",
"500\n500\n"
] | none | [] | 30 | 0 | 0 | 98,352 | |
997 | Sky Full of Stars | [
"combinatorics",
"math"
] | null | null | On one of the planets of Solar system, in Atmosphere University, many students are fans of bingo game.
It is well known that one month on this planet consists of $n^2$ days, so calendars, represented as square matrix $n$ by $n$ are extremely popular.
Weather conditions are even more unusual. Due to the unique composi... | The first and only line of input contains a single integer $n$ ($1 \le n \le 1000\,000$)Β β the number of rows and columns in the calendar. | Print one numberΒ β number of lucky colorings of the calendar modulo $998244353$ | [
"1\n",
"2\n",
"3\n"
] | [
"3\n",
"63\n",
"9933\n"
] | In the first sample any coloring is lucky, since the only column contains cells of only one color.
In the second sample, there are a lot of lucky colorings, in particular, the following colorings are lucky:
While these colorings are not lucky: | [] | 358 | 716,800 | -1 | 98,386 | |
926 | Segments | [
"data structures"
] | null | null | There is a straight line colored in white. *n* black segments are added on it one by one.
After each segment is added, determine the number of connected components of black segments (i.Β e. the number of black segments in the union of the black segments).
In particular, if one segment ends in a point *x*, and another... | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=200<=000) β the number of segments.
The *i*-th of the next *n* lines contains two integers *l**i* and *r**i* (1<=β€<=*l**i*<=<<=*r**i*<=β€<=109) β the coordinates of the left and the right ends of the *i*-th segment. The segments are listed in the order they... | Print *n* integers β the number of connected components of black segments after each segment is added. | [
"3\n1 3\n4 5\n2 4\n",
"9\n10 20\n50 60\n30 40\n70 80\n90 100\n60 70\n10 40\n40 50\n80 90\n"
] | [
"1 2 1 \n",
"1 2 3 4 5 4 3 2 1 \n"
] | In the first example there are two components after the addition of the first two segments, because these segments do not intersect. The third added segment intersects the left segment and touches the right segment at the point 4 (these segments belong to the same component, according to the statements). Thus the numbe... | [] | 46 | 0 | 0 | 98,572 | |
449 | Jzzhu and Numbers | [
"bitmasks",
"combinatorics",
"dp"
] | null | null | Jzzhu have *n* non-negative integers *a*1,<=*a*2,<=...,<=*a**n*. We will call a sequence of indexes *i*1,<=*i*2,<=...,<=*i**k* (1<=β€<=*i*1<=<<=*i*2<=<<=...<=<<=*i**k*<=β€<=*n*) a group of size *k*.
Jzzhu wonders, how many groups exists such that *a**i*1 & *a**i*2 & ... & *a**i**k*<==<=0 (1<=β€<=*k*... | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=106). The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=106). | Output a single integer representing the number of required groups modulo 1000000007 (109<=+<=7). | [
"3\n2 3 3\n",
"4\n0 1 2 3\n",
"6\n5 2 0 5 2 1\n"
] | [
"0\n",
"10\n",
"53\n"
] | none | [
{
"input": "3\n2 3 3",
"output": "0"
},
{
"input": "4\n0 1 2 3",
"output": "10"
},
{
"input": "6\n5 2 0 5 2 1",
"output": "53"
},
{
"input": "16\n0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15",
"output": "64594"
},
{
"input": "10\n450661 128600 993228 725823 293549 33490 ... | 31 | 0 | -1 | 98,581 | |
0 | none | [
"none"
] | null | null | Recently, a wild Krakozyabra appeared at Jelly Castle. It is, truth to be said, always eager to have something for dinner.
Its favorite meal is natural numbers (typically served with honey sauce), or, to be more precise, the zeros in their corresponding decimal representations. As for other digits, Krakozyabra dislike... | In the first and only string, the numbers *L* and *R* are givenΒ β the boundaries of the range (1<=β€<=*L*<=β€<=*R*<=β€<=1018). | Output the sole numberΒ β the answer for the problem. | [
"1 10\n",
"40 57\n",
"157 165\n"
] | [
"9\n",
"17\n",
"9\n"
] | In the first sample case, the inedible tails are the numbers from 1 to 9. Note that 10 and 1 have the same inedible tailΒ β the number 1.
In the second sample case, each number has a unique inedible tail, except for the pair 45,β54. The answer to this sample case is going to be (57β-β40β+β1)β-β1β=β17. | [] | 31 | 0 | 0 | 98,628 | |
732 | Exams | [
"binary search",
"greedy",
"sortings"
] | null | null | Vasiliy has an exam period which will continue for *n* days. He has to pass exams on *m* subjects. Subjects are numbered from 1 to *m*.
About every day we know exam for which one of *m* subjects can be passed on that day. Perhaps, some day you can't pass any exam. It is not allowed to pass more than one exam on any da... | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=105)Β β the number of days in the exam period and the number of subjects.
The second line contains *n* integers *d*1,<=*d*2,<=...,<=*d**n* (0<=β€<=*d**i*<=β€<=*m*), where *d**i* is the number of subject, the exam of which can be passed on the day numb... | Print one integerΒ β the minimum number of days in which Vasiliy can pass all exams. If it is impossible, print -1. | [
"7 2\n0 1 0 2 1 0 2\n2 1\n",
"10 3\n0 0 1 2 3 0 2 0 1 2\n1 1 4\n",
"5 1\n1 1 1 1 1\n5\n"
] | [
"5\n",
"9\n",
"-1\n"
] | In the first example Vasiliy can behave as follows. On the first and the second day he can prepare for the exam number 1 and pass it on the fifth day, prepare for the exam number 2 on the third day and pass it on the fourth day.
In the second example Vasiliy should prepare for the exam number 3 during the first four d... | [
{
"input": "7 2\n0 1 0 2 1 0 2\n2 1",
"output": "5"
},
{
"input": "10 3\n0 0 1 2 3 0 2 0 1 2\n1 1 4",
"output": "9"
},
{
"input": "5 1\n1 1 1 1 1\n5",
"output": "-1"
},
{
"input": "100 10\n1 1 6 6 6 2 5 7 6 5 3 7 10 10 8 9 7 6 9 2 6 7 8 6 7 5 2 5 10 1 10 1 8 10 2 9 7 1 6 8 3 ... | 77 | 1,536,000 | 0 | 99,378 | |
936 | Sleepy Game | [
"dfs and similar",
"dp",
"games",
"graphs"
] | null | null | Petya and Vasya arranged a game. The game runs by the following rules. Players have a directed graph consisting of *n* vertices and *m* edges. One of the vertices contains a chip. Initially the chip is located at vertex *s*. Players take turns moving the chip along some edge of the graph. Petya goes first. Player who c... | The first line of input contain two integers *n* and *m*Β β the number of vertices and the number of edges in the graph (2<=β€<=*n*<=β€<=105, 0<=β€<=*m*<=β€<=2Β·105).
The next *n* lines contain the information about edges of the graph. *i*-th line (1<=β€<=*i*<=β€<=*n*) contains nonnegative integer *c**i*Β β number of vertices ... | If Petya can win print Β«WinΒ» in the first line. In the next line print numbers *v*1,<=*v*2,<=...,<=*v**k* (1<=β€<=*k*<=β€<=106)Β β the sequence of vertices Petya should visit for the winning. Vertex *v*1 should coincide with *s*. For *i*<==<=1... *k*<=-<=1 there should be an edge from *v**i* to *v**i*<=+<=1 in the graph. ... | [
"5 6\n2 2 3\n2 4 5\n1 4\n1 5\n0\n1\n",
"3 2\n1 3\n1 1\n0\n2\n",
"2 2\n1 2\n1 1\n1\n"
] | [
"Win\n1 2 4 5 \n",
"Lose\n",
"Draw\n"
] | In the first example the graph is the following:
Initially the chip is located at vertex 1. In the first move Petya moves the chip to vertex 2, after that he moves it to vertex 4 for Vasya. After that he moves to vertex 5. Now it is Vasya's turn and there is no possible move, so Petya wins.
In the second example the ... | [
{
"input": "5 6\n2 2 3\n2 4 5\n1 4\n1 5\n0\n1",
"output": "Win\n1 2 4 5 "
},
{
"input": "3 2\n1 3\n1 1\n0\n2",
"output": "Lose"
},
{
"input": "2 2\n1 2\n1 1\n1",
"output": "Draw"
},
{
"input": "92 69\n1 76\n1 14\n1 9\n0\n1 46\n1 80\n0\n0\n1 77\n0\n1 53\n1 81\n1 61\n1 40\n0\n1... | 62 | 0 | 0 | 99,527 | |
838 | Expected Earnings | [] | null | null | You are playing a game with a bag of red and black balls. Initially, you are told that the bag has *n* balls total. In addition, you are also told that the bag has probability *p**i*<=/<=106 of containing exactly *i* red balls.
You now would like to buy balls from this bag. You really like the color red, so red balls ... | The first line of input will contain two integers *n*,<=*X* (1<=β€<=*n*<=β€<=10<=000, 0<=β€<=*X*<=β€<=106).
The next line of input will contain *n*<=+<=1 integers *p*0,<=*p*1,<=... *p**n* (0<=β€<=*p**i*<=β€<=106, )
The value of *c* can be computed as . | Print a single floating point number representing the optimal expected value.
Your answer will be accepted if it has absolute or relative error at most 10<=-<=9. More specifically, if your answer is *a* and the jury answer is *b*, your answer will be accepted if . | [
"3 200000\n250000 250000 250000 250000\n"
] | [
"0.9000000000\n"
] | Here, there is equal probability for the bag to contain 0,1,2,3 red balls. Also, it costs 0.2 to draw a ball from the bag. | [] | 483 | 4,505,600 | 0 | 99,685 | |
390 | Inna and Candy Boxes | [
"data structures"
] | null | null | Inna loves sweets very much. She has *n* closed present boxes lines up in a row in front of her. Each of these boxes contains either a candy (Dima's work) or nothing (Sereja's work). Let's assume that the boxes are numbered from 1 to *n*, from left to right.
As the boxes are closed, Inna doesn't know which boxes conta... | The first line of the input contains three integers *n*, *k* and *w* (1<=β€<=*k*<=β€<=*min*(*n*,<=10),<=1<=β€<=*n*,<=*w*<=β€<=105). The second line contains *n* characters. If the *i*-th box contains a candy, the *i*-th character of the line equals 1, otherwise it equals 0.
Each of the following *w* lines contains two int... | For each question, print a single number on a single line β the minimum number of operations Dima needs to make the answer to the question positive. | [
"10 3 3\n1010100011\n1 3\n1 6\n4 9\n"
] | [
"1\n3\n2\n"
] | For the first question, you need to take a candy from the first box to make the answer positive. So the answer is 1.
For the second question, you need to take a candy from the first box, take a candy from the fifth box and put a candy to the sixth box. The answer is 3.
For the third question, you need to take a candy... | [] | 78 | 0 | 0 | 99,953 | |
741 | Arpaβs letter-marked tree and Mehrdadβs Dokhtar-kosh paths | [
"data structures",
"dfs and similar",
"trees"
] | null | null | Just in case somebody missed it: we have wonderful girls in Arpaβs land.
Arpa has a rooted tree (connected acyclic graph) consisting of *n* vertices. The vertices are numbered 1 through *n*, the vertex 1 is the root. There is a letter written on each edge of this tree. Mehrdad is a fan of Dokhtar-kosh things. He call ... | The first line contains integer *n* (1<=<=β€<=<=*n*<=<=β€<=<=5Β·105)Β β the number of vertices in the tree.
(*n*<=<=-<=<=1) lines follow, the *i*-th of them contain an integer *p**i*<=+<=1 and a letter *c**i*<=+<=1 (1<=<=β€<=<=*p**i*<=+<=1<=<=β€<=<=*i*, *c**i*<=+<=1 is lowercase English letter, between a and v, inclusively)... | Print *n* integers. The *i*-th of them should be the length of the longest simple path in subtree of the *i*-th vertex that form a Dokhtar-kosh string. | [
"4\n1 s\n2 a\n3 s\n",
"5\n1 a\n2 h\n1 a\n4 h\n"
] | [
"3 1 1 0 ",
"4 1 0 1 0 "
] | none | [] | 93 | 20,172,800 | 0 | 99,954 | |
808 | Card Game | [
"binary search",
"flows",
"graphs"
] | null | null | Digital collectible card games have become very popular recently. So Vova decided to try one of these.
Vova has *n* cards in his collection. Each of these cards is characterised by its power *p**i*, magic number *c**i* and level *l**i*. Vova wants to build a deck with total power not less than *k*, but magic numbers m... | The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=100, 1<=β€<=*k*<=β€<=100000).
Then *n* lines follow, each of these lines contains three numbers that represent the corresponding card: *p**i*, *c**i* and *l**i* (1<=β€<=*p**i*<=β€<=1000, 1<=β€<=*c**i*<=β€<=100000, 1<=β€<=*l**i*<=β€<=*n*). | If Vova won't be able to build a deck with required power, print <=-<=1. Otherwise print the minimum level Vova has to reach in order to build a deck. | [
"5 8\n5 5 1\n1 5 4\n4 6 3\n1 12 4\n3 12 1\n",
"3 7\n4 4 1\n5 8 2\n5 3 3\n"
] | [
"4\n",
"2\n"
] | none | [
{
"input": "5 8\n5 5 1\n1 5 4\n4 6 3\n1 12 4\n3 12 1",
"output": "4"
},
{
"input": "3 7\n4 4 1\n5 8 2\n5 3 3",
"output": "2"
},
{
"input": "10 10\n3 3 6\n5 10 4\n4 7 9\n4 9 7\n1 9 4\n1 6 10\n4 10 1\n4 4 6\n2 7 2\n1 5 4",
"output": "4"
},
{
"input": "10 20\n9 4 10\n2 8 9\n9 1 ... | 187 | 10,444,800 | 3 | 100,031 | |
995 | Cowmpany Cowmpensation | [
"combinatorics",
"dp",
"math",
"trees"
] | null | null | Allen, having graduated from the MOO Institute of Techcowlogy (MIT), has started a startup! Allen is the president of his startup. He also hires $n-1$ other employees, each of which is assigned a direct superior. If $u$ is a superior of $v$ and $v$ is a superior of $w$ then also $u$ is a superior of $w$. Additionally, ... | The first line of the input contains two integers $n$ and $D$ ($1 \le n \le 3000$, $1 \le D \le 10^9$).
The remaining $n-1$ lines each contain a single positive integer, where the $i$-th line contains the integer $p_i$ ($1 \le p_i \le i$). $p_i$ denotes the direct superior of employee $i+1$. | Output a single integer: the number of ways to assign salaries modulo $10^9 + 7$. | [
"3 2\n1\n1\n",
"3 3\n1\n2\n",
"2 5\n1\n"
] | [
"5\n",
"10\n",
"15\n"
] | In the first sample case, employee 2 and 3 report directly to Allen. The three salaries, in order, can be $(1,1,1)$, $(2,1,1)$, $(2,1,2)$, $(2,2,1)$ or $(2,2,2)$.
In the second sample case, employee 2 reports to Allen and employee 3 reports to employee 2. In order, the possible salaries are $(1,1,1)$, $(2,1,1)$, $(2,2... | [] | 2,000 | 256,819,200 | 0 | 100,086 | |
954 | Castle Defense | [
"binary search",
"data structures",
"greedy",
"two pointers"
] | null | null | Today you are going to lead a group of elven archers to defend the castle that is attacked by an army of angry orcs. Three sides of the castle are protected by impassable mountains and the remaining side is occupied by a long wall that is split into *n* sections. At this moment there are exactly *a**i* archers located ... | The first line of the input contains three integers *n*, *r* and *k* (1<=β€<=*n*<=β€<=500<=000, 0<=β€<=*r*<=β€<=*n*, 0<=β€<=*k*<=β€<=1018)Β β the number of sections of the wall, the maximum distance to other section archers can still shoot and the number of archers yet to be distributed along the wall. The second line contain... | Print one integerΒ β the maximum possible value of defense plan reliability, i.e. the maximum possible value of minimum defense level if we distribute *k* additional archers optimally. | [
"5 0 6\n5 4 3 4 9\n",
"4 2 0\n1 2 3 4\n",
"5 1 1\n2 1 2 1 2\n"
] | [
"5\n",
"6\n",
"3\n"
] | none | [
{
"input": "5 0 6\n5 4 3 4 9",
"output": "5"
},
{
"input": "4 2 0\n1 2 3 4",
"output": "6"
},
{
"input": "5 1 1\n2 1 2 1 2",
"output": "3"
},
{
"input": "1 0 0\n0",
"output": "0"
},
{
"input": "1 0 1000000000000000000\n1000000000",
"output": "10000000010000000... | 1,500 | 80,998,400 | 0 | 100,198 | |
178 | Magic Squares | [] | null | null | The Smart Beaver from ABBYY loves puzzles. One of his favorite puzzles is the magic square. He has recently had an idea to automate the solution of this puzzle. The Beaver decided to offer this challenge to the ABBYY Cup contestants.
The magic square is a matrix of size *n*<=Γ<=*n*. The elements of this matrix are int... | The first input line contains a single integer *n*. The next line contains *n*2 integers *a**i* (<=-<=108<=β€<=*a**i*<=β€<=108), separated by single spaces.
The input limitations for getting 20 points are:
- 1<=β€<=*n*<=β€<=3
The input limitations for getting 50 points are:
- 1<=β€<=*n*<=β€<=4 - It is guaranteed tha... | The first line of the output should contain a single integer *s*. In each of the following *n* lines print *n* integers, separated by spaces and describing the resulting magic square. In the resulting magic square the sums in the rows, columns and diagonals must be equal to *s*. If there are multiple solutions, you are... | [
"3\n1 2 3 4 5 6 7 8 9\n",
"3\n1 0 -1 0 2 -1 -2 0 1\n",
"2\n5 5 5 5\n"
] | [
"15\n2 7 6\n9 5 1\n4 3 8\n",
"0\n1 0 -1\n-2 0 2\n1 0 -1\n",
"10\n5 5\n5 5\n"
] | none | [
{
"input": "3\n1 2 3 4 5 6 7 8 9",
"output": "15\n2 7 6\n9 5 1\n4 3 8"
},
{
"input": "3\n1 0 -1 0 2 -1 -2 0 1",
"output": "0\n1 0 -1\n-2 0 2\n1 0 -1"
},
{
"input": "2\n5 5 5 5",
"output": "10\n5 5\n5 5"
},
{
"input": "2\n-1 -1 -1 -1",
"output": "-2\n-1 -1\n-1 -1"
},
{... | 1,090 | 13,516,800 | 3 | 100,317 | |
660 | Bear and Bowling 4 | [
"binary search",
"data structures",
"divide and conquer",
"geometry",
"ternary search"
] | null | null | Limak is an old brown bear. He often goes bowling with his friends. Today he feels really good and tries to beat his own record!
For rolling a ball one gets a score β an integer (maybe negative) number of points. Score for the *i*-th roll is multiplied by *i* and scores are summed up. So, for *k* rolls with scores *s*... | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=2Β·105) β the total number of rolls made by Limak.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (|*a**i*|<=β€<=107) β scores for Limak's rolls. | Print the maximum possible total score after cancelling rolls. | [
"6\n5 -1000 1 -3 7 -8\n",
"5\n1000 1000 1001 1000 1000\n",
"3\n-60 -70 -80\n"
] | [
"16\n",
"15003\n",
"0\n"
] | In the first sample test, Limak should cancel the first two rolls, and one last roll. He will be left with rolls 1,ββ-β3,β7 what gives him the total score 1Β·1β+β2Β·(β-β3)β+β3Β·7β=β1β-β6β+β21β=β16. | [
{
"input": "6\n5 -1000 1 -3 7 -8",
"output": "16"
},
{
"input": "5\n1000 1000 1001 1000 1000",
"output": "15003"
},
{
"input": "3\n-60 -70 -80",
"output": "0"
},
{
"input": "1\n-4",
"output": "0"
},
{
"input": "2\n-3 6",
"output": "9"
},
{
"input": "3\... | 2,000 | 5,529,600 | 0 | 100,523 | |
491 | New York Hotel | [
"greedy",
"math"
] | null | null | Think of New York as a rectangular grid consisting of *N* vertical avenues numerated from 1 to *N* and *M* horizontal streets numerated 1 to *M*. *C* friends are staying at *C* hotels located at some street-avenue crossings. They are going to celebrate birthday of one of them in the one of *H* restaurants also located ... | The first line contains two integers *N* ΠΈ *M*Β β size of the city (1<=β€<=*N*,<=*M*<=β€<=109). In the next line there is a single integer *C* (1<=β€<=*C*<=β€<=105)Β β the number of hotels friends stayed at. Following *C* lines contain descriptions of hotels, each consisting of two coordinates *x* and *y* (1<=β€<=*x*<=β€<=*N*,... | In the first line output the optimal distance. In the next line output index of a restaurant that produces this optimal distance. If there are several possibilities, you are allowed to output any of them. | [
"10 10\n2\n1 1\n3 3\n2\n1 10\n4 4\n"
] | [
"6\n2\n"
] | none | [
{
"input": "10 10\n2\n1 1\n3 3\n2\n1 10\n4 4",
"output": "6\n2"
},
{
"input": "100 100\n10\n53 20\n97 6\n12 74\n48 92\n97 13\n47 96\n75 32\n69 21\n95 75\n1 54\n10\n36 97\n41 1\n1 87\n39 23\n27 44\n73 97\n1 1\n6 26\n48 3\n5 69",
"output": "108\n4"
},
{
"input": "100 100\n10\n86 72\n25 73\... | 46 | 0 | 0 | 100,640 | |
607 | Power Tree | [
"data structures",
"trees"
] | null | null | Genos and Saitama went shopping for Christmas trees. However, a different type of tree caught their attention, the exalted Power Tree.
A Power Tree starts out as a single root vertex indexed 1. A Power Tree grows through a magical phenomenon known as an update. In an update, a single vertex is added to the tree as a ... | The first line of the input contains two space separated integers *v*1 and *q* (1<=β€<=*v*1<=<<=109, 1<=β€<=*q*<=β€<=200<=000) β the value of vertex 1 and the total number of updates and queries respectively.
The next *q* lines contain the updates and queries. Each of them has one of the following forms:
- 1Β *p**i*... | For each query, print out the power of the given vertex modulo 109<=+<=7. | [
"2 5\n1 1 3\n1 2 5\n1 3 7\n1 4 11\n2 1\n",
"5 5\n1 1 4\n1 2 3\n2 2\n1 2 7\n2 1\n"
] | [
"344\n",
"14\n94\n"
] | For the first sample case, after all the updates the graph will have vertices labelled in the following manner: 1 β 2 β 3 β 4 β 5
These vertices will have corresponding values: 2 β 3 β 5 β 7 β 11
And corresponding powers: 344 β 170 β 82 β 36 β 11 | [] | 31 | 0 | 0 | 100,908 | |
817 | MEX Queries | [
"binary search",
"data structures",
"trees"
] | null | null | You are given a set of integer numbers, initially it is empty. You should perform *n* queries.
There are three different types of queries:
- 1 *l* *r* β Add all missing numbers from the interval [*l*,<=*r*] - 2 *l* *r* β Remove all present numbers from the interval [*l*,<=*r*] - 3 *l* *r* β Invert the interval [*... | The first line contains one integer number *n* (1<=β€<=*n*<=β€<=105).
Next *n* lines contain three integer numbers *t*,<=*l*,<=*r* (1<=β€<=*t*<=β€<=3,<=1<=β€<=*l*<=β€<=*r*<=β€<=1018) β type of the query, left and right bounds. | Print MEX of the set after each query. | [
"3\n1 3 4\n3 1 6\n2 1 3\n",
"4\n1 1 3\n3 5 6\n2 4 4\n3 1 6\n"
] | [
"1\n3\n1\n",
"4\n4\n4\n1\n"
] | Here are contents of the set after each query in the first example:
1. {3,β4} β the interval [3,β4] is added 1. {1,β2,β5,β6} β numbers {3,β4} from the interval [1,β6] got deleted and all the others are added 1. {5,β6} β numbers {1,β2} got deleted | [
{
"input": "3\n1 3 4\n3 1 6\n2 1 3",
"output": "1\n3\n1"
},
{
"input": "4\n1 1 3\n3 5 6\n2 4 4\n3 1 6",
"output": "4\n4\n4\n1"
},
{
"input": "1\n1 1 89",
"output": "90"
},
{
"input": "10\n1 1 9\n2 2 10\n2 1 10\n1 8 10\n3 8 10\n2 1 8\n3 9 10\n1 7 10\n1 4 10\n3 10 10",
"out... | 1,481 | 138,137,600 | 0 | 101,338 | |
613 | Kingdom and its Cities | [
"dfs and similar",
"divide and conquer",
"dp",
"graphs",
"sortings",
"trees"
] | null | null | Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. However, in order not to lose face in front of the relatives, the King should first finish reforms in his kingdom. As the King can not wait for his daughter's marriage, reforms must be finished as soon as possible.
The kingdom curren... | The first line of the input contains integer *n* (1<=β€<=*n*<=β€<=100<=000)Β β the number of cities in the kingdom.
Each of the next *n*<=-<=1 lines contains two distinct integers *u**i*, *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*)Β β the indices of the cities connected by the *i*-th road. It is guaranteed that you can get fro... | For each plan print a single integer β the minimum number of cities that the barbarians need to capture, or print <=-<=1 if all the barbarians' attempts to isolate important cities will not be effective. | [
"4\n1 3\n2 3\n4 3\n4\n2 1 2\n3 2 3 4\n3 1 2 4\n4 1 2 3 4\n",
"7\n1 2\n2 3\n3 4\n1 5\n5 6\n5 7\n1\n4 2 4 6 7\n"
] | [
"1\n-1\n1\n-1\n",
"2\n"
] | In the first sample, in the first and the third King's plan barbarians can capture the city 3, and that will be enough. In the second and the fourth plans all their attempts will not be effective.
In the second sample the cities to capture are 3 and 5. | [
{
"input": "4\n1 3\n2 3\n4 3\n4\n2 1 2\n3 2 3 4\n3 1 2 4\n4 1 2 3 4",
"output": "1\n-1\n1\n-1"
},
{
"input": "7\n1 2\n2 3\n3 4\n1 5\n5 6\n5 7\n1\n4 2 4 6 7",
"output": "2"
},
{
"input": "7\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n5\n4 1 3 5 7\n3 2 4 6\n2 1 7\n2 3 4\n3 1 6 7",
"output": "3\n2\n... | 124 | 0 | 0 | 101,432 | |
746 | Tram | [
"constructive algorithms",
"implementation",
"math"
] | null | null | The tram in Berland goes along a straight line from the point 0 to the point *s* and back, passing 1 meter per *t*1 seconds in both directions. It means that the tram is always in the state of uniform rectilinear motion, instantly turning around at points *x*<==<=0 and *x*<==<=*s*.
Igor is at the point *x*1. He should... | The first line contains three integers *s*, *x*1 and *x*2 (2<=β€<=*s*<=β€<=1000, 0<=β€<=*x*1,<=*x*2<=β€<=*s*, *x*1<=β <=*x*2)Β β the maximum coordinate of the point to which the tram goes, the point Igor is at, and the point he should come to.
The second line contains two integers *t*1 and *t*2 (1<=β€<=*t*1,<=*t*2<=β€<=1000)Β ... | Print the minimum time in seconds which Igor needs to get from the point *x*1 to the point *x*2. | [
"4 2 4\n3 4\n1 1\n",
"5 4 0\n1 2\n3 1\n"
] | [
"8\n",
"7\n"
] | In the first example it is profitable for Igor to go by foot and not to wait the tram. Thus, he has to pass 2 meters and it takes 8 seconds in total, because he passes 1 meter per 4 seconds.
In the second example Igor can, for example, go towards the point *x*<sub class="lower-index">2</sub> and get to the point 1 in... | [
{
"input": "4 2 4\n3 4\n1 1",
"output": "8"
},
{
"input": "5 4 0\n1 2\n3 1",
"output": "7"
},
{
"input": "5 4 0\n5 14\n1 -1",
"output": "55"
},
{
"input": "10 7 2\n7 9\n9 -1",
"output": "45"
},
{
"input": "20 5 19\n163 174\n4 1",
"output": "2436"
},
{
... | 78 | 0 | 3 | 102,011 | |
940 | Cashback | [
"data structures",
"dp",
"greedy",
"math"
] | null | null | Since you are the best Wraith King, Nizhniy Magazin Β«MirΒ» at the centre of Vinnytsia is offering you a discount.
You are given an array *a* of length *n* and an integer *c*.
The value of some array *b* of length *k* is the sum of its elements except for the smallest. For example, the value of the array [3,<=1,<=6,<... | The first line contains integers *n* and *c* (1<=β€<=*n*,<=*c*<=β€<=100<=000).
The second line contains *n* integers *a**i* (1<=β€<=*a**i*<=β€<=109)Β β elements of *a*. | Output a single integer Β β the smallest possible sum of values of these subarrays of some partition of *a*. | [
"3 5\n1 2 3\n",
"12 10\n1 1 10 10 10 10 10 10 9 10 10 10\n",
"7 2\n2 3 6 4 5 7 1\n",
"8 4\n1 3 4 5 5 3 4 1\n"
] | [
"6\n",
"92\n",
"17\n",
"23\n"
] | In the first example any partition yields 6 as the sum.
In the second example one of the optimal partitions is [1,β1],β[10,β10,β10,β10,β10,β10,β9,β10,β10,β10] with the values 2 and 90 respectively.
In the third example one of the optimal partitions is [2,β3],β[6,β4,β5,β7],β[1] with the values 3, 13 and 1 respectively... | [
{
"input": "3 5\n1 2 3",
"output": "6"
},
{
"input": "12 10\n1 1 10 10 10 10 10 10 9 10 10 10",
"output": "92"
},
{
"input": "7 2\n2 3 6 4 5 7 1",
"output": "17"
},
{
"input": "8 4\n1 3 4 5 5 3 4 1",
"output": "23"
},
{
"input": "15 5\n11 15 16 24 24 28 36 40 49 4... | 0 | 0 | -1 | 102,252 | |
406 | Hill Climbing | [
"dfs and similar",
"geometry",
"trees"
] | null | null | This problem has nothing to do with Little Chris. It is about hill climbers instead (and Chris definitely isn't one).
There are *n* hills arranged on a line, each in the form of a vertical line segment with one endpoint on the ground. The hills are numbered with numbers from 1 to *n* from left to right. The *i*-th hil... | The first line of input contains a single integer *n* (1<=β€<=*n*<=β€<=105), the number of hills. The next *n* lines describe the hills. The *i*-th of them contains two space-separated integers *x**i*, *y**i* (1<=β€<=*x**i*<=β€<=107; 1<=β€<=*y**i*<=β€<=1011), the position and the height of the *i*-th hill. The hills are give... | In a single line output *m* space-separated integers, where the *i*-th integer is the number of the meeting hill for the members of the *i*-th team. | [
"6\n1 4\n2 1\n3 2\n4 3\n6 4\n7 4\n3\n3 1\n5 6\n2 3\n"
] | [
"5 6 3 \n"
] | none | [] | 1,000 | 25,190,400 | 0 | 102,486 | |
703 | Mishka and Divisors | [
"dp",
"number theory"
] | null | null | After playing with her beautiful array, Mishka decided to learn some math. After learning how to multiply, divide and what is divisibility, she is now interested in solving the following problem.
You are given integer *k* and array *a*1,<=*a*2,<=...,<=*a**n* of *n* integers. You are to find non-empty subsequence of ar... | The first line of the input contains two integers *n* and *k* (1<=β€<=*n*<=β€<=1<=000, 1<=β€<=*k*<=β€<=1012).
The second line of the input contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=1012)Β β array elements. | Print single positive integer *m* in the first lineΒ β the number of elements in desired sequence.
In the second line print *m* distinct integersΒ β the sequence of indices of given array elements, which should be taken into the desired sequence.
If there are more than one such subsequence (e.g. subsequence of minimum... | [
"5 60\n2 4 6 5 2\n"
] | [
"3\n4 3 1 "
] | none | [
{
"input": "5 60\n2 4 6 5 2",
"output": "3\n4 3 1 "
},
{
"input": "10 66\n93 799 19 143 1739 1403 19 1681 8513 1457",
"output": "-1"
},
{
"input": "10 78\n2473 7234 317 3793 6638 6 7789 753 1117 8243",
"output": "-1"
},
{
"input": "10 78\n19 1081 143 5 31 61 1363 319 39 589",... | 30 | 0 | 0 | 102,616 | |
809 | Find a car | [
"combinatorics",
"divide and conquer",
"dp"
] | null | null | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Form... | The first line contains one integer *q* (1<=β€<=*q*<=β€<=104)Β β the number of Leha's requests.
The next *q* lines contain five integers *x*1,<=*y*1,<=*x*2,<=*y*2,<=*k* (1<=β€<=*x*1<=β€<=*x*2<=β€<=109,<=1<=β€<=*y*1<=β€<=*y*2<=β€<=109,<=1<=β€<=*k*<=β€<=2Β·109)Β β parameters of Leha's requests. | Print exactly *q* linesΒ β in the first line print the answer to the first request, in the secondΒ β the answer to the second request and so on. | [
"4\n1 1 1 1 1\n3 2 5 4 5\n1 1 5 5 10000\n1 4 2 5 2\n"
] | [
"1\n13\n93\n0\n"
] | Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (*k*β=β1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<img class="tex-graphics" src="https://espresso.codeforces.com/76839e22308c8... | [
{
"input": "4\n1 1 1 1 1\n3 2 5 4 5\n1 1 5 5 10000\n1 4 2 5 2",
"output": "1\n13\n93\n0"
},
{
"input": "10\n3 7 4 10 7\n6 1 7 10 18\n9 6 10 8 3\n1 8 3 10 3\n10 4 10 5 19\n8 9 9 10 10\n10 1 10 5 4\n8 1 9 4 18\n6 3 9 5 1\n6 6 9 6 16",
"output": "22\n130\n0\n0\n25\n3\n0\n68\n0\n22"
},
{
"in... | 4,000 | 0 | 0 | 103,514 | |
442 | Artem and Array | [
"data structures",
"greedy"
] | null | null | Artem has an array of *n* positive integers. Artem decided to play with it. The game consists of *n* moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets *min*(*a*,<=*b*) points, where *a* and *b* are numbers that were adjacent with the removed number. If the number... | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=5Β·105) β the number of elements in the array. The next line contains *n* integers *a**i* (1<=β€<=*a**i*<=β€<=106) β the values of the array elements. | In a single line print a single integer β the maximum number of points Artem can get. | [
"5\n3 1 5 2 6\n",
"5\n1 2 3 4 5\n",
"5\n1 100 101 100 1\n"
] | [
"11\n",
"6\n",
"102\n"
] | none | [
{
"input": "5\n3 1 5 2 6",
"output": "11"
},
{
"input": "5\n1 2 3 4 5",
"output": "6"
},
{
"input": "5\n1 100 101 100 1",
"output": "102"
},
{
"input": "10\n96 66 8 18 30 48 34 11 37 42",
"output": "299"
},
{
"input": "1\n87",
"output": "0"
},
{
"input... | 93 | 0 | 0 | 103,563 | |
249 | Donkey and Stars | [
"data structures",
"dp",
"geometry",
"math",
"sortings"
] | null | null | In the evenings Donkey would join Shrek to look at the stars. They would sit on a log, sipping tea and they would watch the starry sky. The sky hung above the roof, right behind the chimney. Shrek's stars were to the right of the chimney and the Donkey's stars were to the left. Most days the Donkey would just count the... | The first line contains an integer *n* (1<=β€<=*n*<=β€<=105) β the number of stars. The second line contains simple fractions representing relationships "*a*/*b* *c*/*d*", such that and (0<=β€<=*a*,<=*b*,<=*c*,<=*d*<=β€<=105; ; ; ). The given numbers *a*, *b*, *c*, *d* are integers.
Next *n* lines contain pairs of integ... | In a single line print number *m* β the answer to the problem. | [
"15\n1/3 2/1\n3 1\n6 2\n4 2\n2 5\n4 5\n6 6\n3 4\n1 6\n2 1\n7 4\n9 3\n5 3\n1 3\n15 5\n12 4\n"
] | [
"4\n"
] | In the sample the longest chain the Donkey can build consists of four stars. Note that the Donkey can't choose the stars that lie on the rays he imagines. | [
{
"input": "15\n1/3 2/1\n3 1\n6 2\n4 2\n2 5\n4 5\n6 6\n3 4\n1 6\n2 1\n7 4\n9 3\n5 3\n1 3\n15 5\n12 4",
"output": "4"
},
{
"input": "15\n2/1 2/0\n3 1\n6 2\n9 3\n12 4\n15 5\n2 1\n4 2\n5 3\n7 4\n1 3\n3 4\n2 5\n4 5\n1 6\n6 6",
"output": "1"
},
{
"input": "15\n2/1 2/0\n3 1\n6 2\n9 3\n12 4\n15... | 2,000 | 4,710,400 | 0 | 103,637 | |
306 | White, Black and White Again | [
"combinatorics",
"number theory"
] | null | null | Polycarpus is sure that his life fits the description: "first there is a white stripe, then a black one, then a white one again". So, Polycarpus is sure that this rule is going to fulfill during the next *n* days. Polycarpus knows that he is in for *w* good events and *b* not-so-good events. At least one event is going... | The single line of the input contains integers *n*, *w* and *b* (3<=β€<=*n*<=β€<=4000, 2<=β€<=*w*<=β€<=4000, 1<=β€<=*b*<=β€<=4000) β the number of days, the number of good events and the number of not-so-good events. It is guaranteed that *w*<=+<=*b*<=β₯<=*n*. | Print the required number of ways modulo 1000000009 (109<=+<=9). | [
"3 2 1\n",
"4 2 2\n",
"3 2 2\n"
] | [
"2\n",
"4\n",
"4\n"
] | We'll represent the good events by numbers starting from 1 and the not-so-good events β by letters starting from 'a'. Vertical lines separate days.
In the first sample the possible ways are: "1|a|2" and "2|a|1". In the second sample the possible ways are: "1|a|b|2", "2|a|b|1", "1|b|a|2" and "2|b|a|1". In the third sam... | [
{
"input": "3 2 1",
"output": "2"
},
{
"input": "4 2 2",
"output": "4"
},
{
"input": "3 2 2",
"output": "4"
},
{
"input": "3 3 1",
"output": "12"
},
{
"input": "3 2 2",
"output": "4"
},
{
"input": "3 3 3",
"output": "72"
},
{
"input": "4 2 ... | 92 | 0 | 0 | 103,649 | |
240 | TorCoder | [
"data structures"
] | null | null | A boy named Leo doesn't miss a single TorCoder contest round. On the last TorCoder round number 100666 Leo stumbled over the following problem. He was given a string *s*, consisting of *n* lowercase English letters, and *m* queries. Each query is characterised by a pair of integers *l**i*,<=*r**i* (1<=β€<=*l**i*<=β€<=*r*... | The first input line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=105) β the string length and the number of the queries.
The second line contains string *s*, consisting of *n* lowercase Latin letters.
Each of the next *m* lines contains a pair of integers *l**i*,<=*r**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*n*) β... | In a single line print the result of applying *m* queries to string *s*. Print the queries in the order in which they are given in the input. | [
"7 2\naabcbaa\n1 3\n5 7\n",
"3 2\nabc\n1 2\n2 3\n"
] | [
"abacaba\n",
"abc\n"
] | A substring (*l*<sub class="lower-index">*i*</sub>,β*r*<sub class="lower-index">*i*</sub>) 1ββ€β*l*<sub class="lower-index">*i*</sub>ββ€β*r*<sub class="lower-index">*i*</sub>ββ€β*n*) of string *s*β=β*s*<sub class="lower-index">1</sub>*s*<sub class="lower-index">2</sub>... *s*<sub class="lower-index">*n*</sub> of length *n... | [] | 31 | 0 | -1 | 103,661 | |
200 | Football Championship | [
"brute force",
"implementation"
] | null | null | Any resemblance to any real championship and sport is accidental.
The Berland National team takes part in the local Football championship which now has a group stage. Let's describe the formal rules of the local championship:
- the team that kicked most balls in the enemy's goal area wins the game; - the victory g... | The input has five lines.
Each line describes a game as "*team*1 *team*2 *goals*1:*goals*2" (without the quotes), what means that team *team*1 played a game with team *team*2, besides, *team*1 scored *goals*1 goals and *team*2 scored *goals*2 goals. The names of teams *team*1 and *team*2 are non-empty strings, consist... | Print the required score in the last game as *X*:*Y*, where *X* is the number of goals Berland scored and *Y* is the number of goals the opponent scored. If the Berland team does not get the first or the second place in the group, whatever this game's score is, then print on a single line "IMPOSSIBLE" (without the quot... | [
"AERLAND DERLAND 2:1\nDERLAND CERLAND 0:3\nCERLAND AERLAND 0:1\nAERLAND BERLAND 2:0\nDERLAND BERLAND 4:0\n",
"AERLAND DERLAND 2:2\nDERLAND CERLAND 2:3\nCERLAND AERLAND 1:3\nAERLAND BERLAND 2:1\nDERLAND BERLAND 4:1\n"
] | [
"6:0\n",
"IMPOSSIBLE\n"
] | In the first sample "BERLAND" plays the last game with team "CERLAND". If Berland wins with score 6:0, the results' table looks like that in the end:
1. AERLAND (points: 9, the difference between scored and missed goals: 4, scored goals: 5) 1. BERLAND (points: 3, the difference between scored and missed goals: 0, s... | [] | 122 | 0 | 0 | 103,831 | |
727 | Polycarp's problems | [
"binary search",
"dp",
"greedy"
] | null | null | Polycarp is an experienced participant in Codehorses programming contests. Now he wants to become a problemsetter.
He sent to the coordinator a set of *n* problems. Each problem has it's quality, the quality of the *i*-th problem is *a**i* (*a**i* can be positive, negative or equal to zero). The problems are ordered b... | The first line of input contains two integers *n* and *m* (1<=β€<=*n*<=β€<=750, 1<=β€<=*m*<=β€<=200<=000)Β β the number of problems in the problemset and the number of guesses about the current coordinator's mood.
The second line of input contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=β€<=*a**i*<=β€<=109)Β β the ... | Print *m* lines, in *i*-th line print single integerΒ β the answer to the problem with *q*<==<=*b**i*. | [
"6 3\n8 -5 -4 1 -7 4\n0 7 3\n"
] | [
"2\n0\n1\n"
] | none | [
{
"input": "6 3\n8 -5 -4 1 -7 4\n0 7 3",
"output": "2\n0\n1"
},
{
"input": "10 5\n-1 -1 -1 -1 -1 -1 -1 -1 -1 -1\n0 1 2 3 4",
"output": "10\n9\n8\n7\n6"
},
{
"input": "10 5\n1 -1 1 -2 1 -1 1 -2 1 -1\n0 1 2 3 4",
"output": "1\n1\n0\n0\n0"
},
{
"input": "1 5\n7\n1 3 5 7 9",
... | 2,000 | 4,300,800 | 0 | 103,964 | |
730 | Minimum and Maximum | [
"constructive algorithms",
"interactive"
] | null | null | This is an interactive problem. You have to use flush operation right after printing each line. For example, in C++ you should use function fflush(stdout), in Java β System.out.flush(), in Pascal β flush(output) and in Python β sys.stdout.flush().
In this problem, you need to find maximal and minimal elements of an ar... | none | none | [
"2\n2\nΒ \n>\nΒ \n3\nΒ \n=\nΒ \n=\nΒ "
] | [
"? 1 2\nΒ \n! 2 1\nΒ \n? 3 1\nΒ \n? 2 1\nΒ \n! 2 3"
] | none | [
{
"input": "2\n2\n2 1\n3\n1 1 1",
"output": "1 out of 1\n3 out of 3\n2 queries processed [sumn=5]"
},
{
"input": "1\n4\n1 1 2 2",
"output": "4 out of 4\n1 queries processed [sumn=4]"
},
{
"input": "2\n5\n1 1 2 1 1\n3\n3 2 1",
"output": "6 out of 6\n3 out of 3\n2 queries processed [su... | 77 | 2,867,200 | 0 | 104,058 | |
581 | Three Logos | [
"bitmasks",
"brute force",
"constructive algorithms",
"geometry",
"implementation",
"math"
] | null | null | Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no e... | The first line of the input contains six positive integers *x*1,<=*y*1,<=*x*2,<=*y*2,<=*x*3,<=*y*3 (1<=β€<=*x*1,<=*y*1,<=*x*2,<=*y*2,<=*x*3,<=*y*3<=β€<=100), where *x**i* and *y**i* determine the length and width of the logo of the *i*-th company respectively. | If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square *n*, where you can place all the three logos. Each of the next *n* lines should contain *n* uppercase English letters "A", "B"... | [
"5 1 2 5 5 2\n",
"4 4 2 6 4 2\n"
] | [
"5\nAAAAA\nBBBBB\nBBBBB\nCCCCC\nCCCCC\n",
"6\nBBBBBB\nBBBBBB\nAAAACC\nAAAACC\nAAAACC\nAAAACC\n"
] | none | [
{
"input": "5 1 2 5 5 2",
"output": "5\nAAAAA\nBBBBB\nBBBBB\nCCCCC\nCCCCC"
},
{
"input": "4 4 2 6 4 2",
"output": "6\nBBBBBB\nBBBBBB\nAAAACC\nAAAACC\nAAAACC\nAAAACC"
},
{
"input": "1 3 1 3 3 1",
"output": "3\nAAA\nBBB\nCCC"
},
{
"input": "2 4 1 4 1 4",
"output": "4\nAAAA\... | 31 | 307,200 | 0 | 104,213 | |
0 | none | [
"none"
] | null | null | Pieguy and Piegirl are playing a game. They have a rooted binary tree, that has a property that each node is either a leaf or has exactly two children. Each leaf has a number associated with it.
On his/her turn a player can choose any two leafs that share their immediate parent, remove them, and associate either of th... | First line contains a single integer *t* (1<=β€<=*t*<=β€<=100) β number of test cases. Then *t* test cases follow. Each test case begins with an empty line, followed by a line with a single integer *n* (1<=β€<=*n*<=β€<=250), followed by *n* lines describing *n* nodes of the tree. Each of those *n* lines either contains a n... | For each test case print one line with one integer on it β the number that will be associated with the root when the game ends. | [
"4\n\n3\n-1 1 2\n10\n5\n\n5\n-1 1 2\n-1 3 4\n10\n5\n20\n\n7\n-1 1 2\n-1 3 4\n-1 5 6\n1\n2\n3\n4\n\n11\n-1 1 2\n-1 3 4\n-1 5 6\n-1 7 8\n15\n7\n-1 9 10\n7\n8\n9\n11\n"
] | [
"10\n10\n4\n8\n"
] | none | [] | 46 | 0 | 0 | 104,331 | |
57 | Chess | [
"math",
"shortest paths"
] | E. Chess | 2 | 256 | Brian the Rabbit adores chess. Not long ago he argued with Stewie the Rabbit that a knight is better than a king. To prove his point he tries to show that the knight is very fast but Stewie doesn't accept statements without evidence. He constructed an infinite chessboard for Brian, where he deleted several squares to a... | The first line contains two integers *k* and *n* (0<=β€<=*k*<=β€<=1018,<=0<=β€<=*n*<=β€<=440) which are correspondingly the maximal number of moves a knight can make and the number of deleted cells. Then follow *n* lines, each giving the coordinates of a deleted square in the form (*x**i*,<=*y**i*) (|*x**i*|<=β€<=10,<=|*y**... | You must print the answer on a single line. As it can be rather long, you should print it modulo 1000000007. | [
"1 0\n",
"2 7\n-1 2\n1 2\n2 1\n2 -1\n1 -2\n-1 -2\n-2 -1\n"
] | [
"9\n",
"9\n"
] | none | [] | 92 | 0 | 0 | 104,717 |
266 | Below the Diagonal | [
"constructive algorithms",
"greedy",
"math"
] | null | null | You are given a square matrix consisting of *n* rows and *n* columns. We assume that the rows are numbered from 1 to *n* from top to bottom and the columns are numbered from 1 to *n* from left to right. Some cells (*n*<=-<=1 cells in total) of the the matrix are filled with ones, the remaining cells are filled with zer... | The first line contains an integer *n* (2<=β€<=*n*<=β€<=1000) β the number of rows and columns. Then follow *n*<=-<=1 lines that contain one's positions, one per line. Each position is described by two integers *x**k*,<=*y**k* (1<=β€<=*x**k*,<=*y**k*<=β€<=*n*), separated by a space. A pair (*x**k*,<=*y**k*) means that the ... | Print the description of your actions. These actions should transform the matrix to the described special form.
In the first line you should print a non-negative integer *m* (*m*<=β€<=105) β the number of actions. In each of the next *m* lines print three space-separated integers *t*,<=*i*,<=*j* (1<=β€<=*t*<=β€<=2,<=1<=β€... | [
"2\n1 2\n",
"3\n3 1\n1 3\n",
"3\n2 1\n3 2\n"
] | [
"2\n2 1 2\n1 1 2\n",
"3\n2 2 3\n1 1 3\n1 1 2\n",
"0\n"
] | none | [] | 62 | 0 | 0 | 104,936 | |
0 | none | [
"none"
] | null | null | Copying large hexadecimal (base 16) strings by hand can be error prone, but that doesn't stop people from doing it. You've discovered a bug in the code that was likely caused by someone making a mistake when copying such a string. You suspect that whoever copied the string did not change any of the digits in the string... | Input will contain a hexadecimal string *S* consisting only of digits 0 to 9 and lowercase English letters from *a* to *f*, with length at most 14. At least one of the characters is non-zero. | If it is not possible, print "NO" (without quotes).
Otherwise, print the lowercase hexadecimal string corresponding to the smallest possible numerical value, including any necessary leading zeros for the length to be correct. | [
"f1e\n",
"0f1e\n",
"12d2c\n"
] | [
"NO\n",
"00f1\n",
"00314\n"
] | The numerical value of a hexadecimal string is computed by multiplying each digit by successive powers of 16, starting with the rightmost digit, which is multiplied by 16<sup class="upper-index">0</sup>. Hexadecimal digits representing values greater than 9 are represented by letters: *a*β=β10,β*b*β=β11,β*c*β=β12,β*d*β... | [] | 46 | 0 | 0 | 105,024 | |
852 | Bob and stages | [
"dp",
"geometry"
] | null | null | The citizens of BubbleLand are celebrating their 10th anniversary so they decided to organize a big music festival. Bob got a task to invite *N* famous singers who would sing on the fest. He was too busy placing stages for their performances that he totally forgot to write the invitation e-mails on time, and unfortunat... | The first line of input contains two integers *N* (3<=β€<=*N*<=β€<=200) and *K* (3<=β€<=*K*<=β€<=*min*(*N*,<=50)), separated with one empty space, representing number of stages and number of singers, respectively.
Each of the next *N* lines contains two integers *X**i* and *Y**i* (0<=β€<=*X**i*,<=*Y**i*<=β€<=106) representi... | Output contains only one line with one number, rounded to exactly two decimal places: the maximal festival area. Rounding is performed so that 0.5 and more rounds up and everything else rounds down. | [
"5 4\n0 0\n3 0\n2 1\n4 4\n1 5\n"
] | [
"10.00\n"
] | Example explanation: From all possible convex polygon with 4 vertices and no other vertex inside, the largest is one with points (0,β0), (2,β1), (4,β4) and (1,β5). | [] | 30 | 0 | 0 | 105,113 | |
91 | Ski Base | [
"combinatorics",
"dsu",
"graphs"
] | C. Ski Base | 2 | 256 | A ski base is planned to be built in Walrusland. Recently, however, the project is still in the constructing phase. A large land lot was chosen for the construction. It contains *n* ski junctions, numbered from 1 to *n*. Initially the junctions aren't connected in any way.
In the constructing process *m* bidirectional... | The first line contains two integers *n* and *m* (2<=β€<=*n*<=β€<=105,<=1<=β€<=*m*<=β€<=105). They represent the number of junctions and the number of roads correspondingly. Then on *m* lines follows the description of the roads in the order in which they were built. Each road is described by a pair of integers *a**i* and ... | Print *m* lines: the *i*-th line should represent the number of ways to build a ski base after the end of construction of the road number *i*. The numbers should be printed modulo 1000000009 (109<=+<=9). | [
"3 4\n1 3\n2 3\n1 2\n1 2\n"
] | [
"0\n0\n1\n3\n"
] | Let us have 3 junctions and 4 roads between the junctions have already been built (as after building all the roads in the sample): 1 and 3, 2 and 3, 2 roads between junctions 1 and 2. The land lot for the construction will look like this:
The land lot for the construction will look in the following way:
We can choose... | [
{
"input": "3 4\n1 3\n2 3\n1 2\n1 2",
"output": "0\n0\n1\n3"
},
{
"input": "15 29\n6 11\n14 3\n10 4\n14 7\n6 14\n7 15\n13 8\n10 13\n4 14\n15 8\n12 7\n3 5\n6 7\n8 1\n4 5\n11 5\n10 6\n11 3\n13 14\n7 10\n3 12\n7 14\n8 11\n7 15\n15 8\n12 7\n4 3\n9 4\n8 10",
"output": "0\n0\n0\n0\n0\n0\n0\n0\n0\n1\n1... | 46 | 0 | 0 | 105,192 |
359 | Neatness | [
"constructive algorithms",
"dfs and similar"
] | null | null | Simon loves neatness. So before he goes to bed, Simon wants to complete all chores in the house.
Simon's house looks like a rectangular table consisting of *n* rows and *n* columns from above. All rows of the table are numbered from 1 to *n* from top to bottom. All columns of the table are numbered from 1 to *n* from ... | The first line contains three positive integers *n*,<=*x*0,<=*y*0 (2<=β€<=*n*<=β€<=500,<=1<=β€<=*x*0,<=*y*0<=β€<=*n*).
Next *n* lines contain the description of rooms in the house. The *i*-th line contains *n* space-separated integers *a**i*1,<=*a**i*2,<=...,<=*a**in*. If number *a**ij* equals zero, then room (*i*,<=*j*) ... | If there is no desired sequence of actions, print "NO" (without the quotes). Otherwise, print "YES" (without the quotes) and the description of the required sequence of actions as a string. Note that you do not have to minimize the length of the sequence of actions but you shouldn't use more than 3Β·106 actions. | [
"3 1 1\n1 0 0\n0 1 0\n1 0 0\n",
"3 1 1\n1 0 0\n0 1 0\n0 0 1\n"
] | [
"YES\nD1R2L2D2UU2\n",
"NO\n"
] | none | [] | 46 | 0 | 0 | 105,232 | |
242 | Dispute | [
"dfs and similar",
"graphs",
"greedy"
] | null | null | Valera has *n* counters numbered from 1 to *n*. Some of them are connected by wires, and each of the counters has a special button.
Initially, all the counters contain number 0. When you press a button on a certain counter, the value it has increases by one. Also, the values recorded in all the counters, directly conn... | The first line contains two space-separated integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=105), that denote the number of counters Valera has and the number of pairs of counters connected by wires.
Each of the following *m* lines contains two space-separated integers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*,<=*u**... | If Valera can't win the dispute print in the first line -1.
Otherwise, print in the first line integer *k* (0<=β€<=*k*<=β€<=*n*). In the second line print *k* distinct space-separated integers β the numbers of the counters, where Valera should push buttons to win the dispute, in arbitrary order.
If there exists multipl... | [
"5 5\n2 3\n4 1\n1 5\n5 3\n2 1\n1 1 2 0 2\n",
"4 2\n1 2\n3 4\n0 0 0 0\n"
] | [
"2\n1 2\n",
"3\n1 3 4\n"
] | none | [
{
"input": "5 5\n2 3\n4 1\n1 5\n5 3\n2 1\n1 1 2 0 2",
"output": "2\n1 2"
},
{
"input": "4 2\n1 2\n3 4\n0 0 0 0",
"output": "3\n1 3 4"
},
{
"input": "3 3\n2 3\n1 3\n1 2\n1 1 3",
"output": "0"
},
{
"input": "4 6\n3 4\n2 3\n2 4\n1 2\n1 3\n4 1\n4 0 2 3",
"output": "1\n2 "
}... | 1,246 | 40,652,800 | 3 | 105,344 | |
898 | Restoring the Expression | [
"brute force",
"hashing",
"math"
] | null | null | A correct expression of the form a+b=c was written; *a*, *b* and *c* are non-negative integers without leading zeros. In this expression, the plus and equally signs were lost. The task is to restore the expression. In other words, one character '+' and one character '=' should be inserted into given sequence of digits ... | The first line contains a non-empty string consisting of digits. The length of the string does not exceed 106. | Output the restored expression. If there are several solutions, you can print any of them.
Note that the answer at first should contain two terms (divided with symbol '+'), and then the result of their addition, before which symbol'=' should be.
Do not separate numbers and operation signs with spaces. Strictly follo... | [
"12345168\n",
"099\n",
"199100\n",
"123123123456456456579579579\n"
] | [
"123+45=168\n",
"0+9=9\n",
"1+99=100\n",
"123123123+456456456=579579579\n"
] | none | [
{
"input": "12345168",
"output": "123+45=168"
},
{
"input": "099",
"output": "0+9=9"
},
{
"input": "199100",
"output": "1+99=100"
},
{
"input": "123123123456456456579579579",
"output": "123123123+456456456=579579579"
},
{
"input": "112",
"output": "1+1=2"
},... | 2,000 | 268,390,400 | 0 | 105,529 | |
979 | Kuro and Topological Parity | [
"dp"
] | null | null | Kuro has recently won the "Most intelligent cat ever" contest. The three friends then decided to go to Katie's home to celebrate Kuro's winning. After a big meal, they took a small break then started playing games.
Kuro challenged Katie to create a game with only a white paper, a pencil, a pair of scissors and a lot o... | The first line contains two integers $n$ and $p$ ($1 \leq n \leq 50$, $0 \leq p \leq 1$) β the number of pieces and Kuro's wanted parity.
The second line contains $n$ integers $c_{1}, c_{2}, ..., c_{n}$ ($-1 \leq c_{i} \leq 1$) β the colors of the pieces. | Print a single integer β the number of ways to put the arrows and choose colors so the number of valid paths of alternating colors has the parity of $p$. | [
"3 1\n-1 0 1\n",
"2 1\n1 0\n",
"1 1\n-1\n"
] | [
"6",
"1",
"2"
] | In the first example, there are $6$ ways to color the pieces and add the arrows, as are shown in the figure below. The scores are $3, 3, 5$ for the first row and $5, 3, 3$ for the second row, both from left to right. | [
{
"input": "3 1\n-1 0 1",
"output": "6"
},
{
"input": "2 1\n1 0",
"output": "1"
},
{
"input": "1 1\n-1",
"output": "2"
},
{
"input": "1 0\n-1",
"output": "0"
},
{
"input": "1 1\n0",
"output": "1"
},
{
"input": "5 1\n-1 -1 -1 -1 -1",
"output": "1651... | 46 | 0 | 0 | 105,595 | |
757 | Bash Plays with Functions | [
"brute force",
"combinatorics",
"dp",
"number theory"
] | null | null | Bash got tired on his journey to become the greatest Pokemon master. So he decides to take a break and play with functions.
Bash defines a function *f*0(*n*), which denotes the number of ways of factoring *n* into two factors *p* and *q* such that *gcd*(*p*,<=*q*)<==<=1. In other words, *f*0(*n*) is the number of orde... | The first line contains an integer *q* (1<=β€<=*q*<=β€<=106)Β β the number of values Bash wants to know.
Each of the next *q* lines contain two integers *r* and *n* (0<=β€<=*r*<=β€<=106, 1<=β€<=*n*<=β€<=106), which denote Bash wants to know the value *f**r*(*n*). | Print *q* integers. For each pair of *r* and *n* given, print *f**r*(*n*) modulo 109<=+<=7 on a separate line. | [
"5\n0 30\n1 25\n3 65\n2 5\n4 48\n"
] | [
"8\n5\n25\n4\n630\n"
] | none | [
{
"input": "5\n0 30\n1 25\n3 65\n2 5\n4 48",
"output": "8\n5\n25\n4\n630"
},
{
"input": "10\n788320 679319\n530483 595211\n563388 996394\n781948 542877\n651049 580717\n932449 999004\n869373 900380\n892578 818546\n786482 603289\n867938 792888",
"output": "788322\n414333258\n434446408\n445798223\n... | 2,932 | 268,390,400 | 0 | 105,673 | |
909 | AND-permutations | [
"constructive algorithms"
] | null | null | Given an integer *N*, find two permutations:
1. Permutation *p* of numbers from 1 to *N* such that *p**i*<=β <=*i* and *p**i*<=&<=*i*<==<=0 for all *i*<==<=1,<=2,<=...,<=*N*. 1. Permutation *q* of numbers from 1 to *N* such that *q**i*<=β <=*i* and *q**i*<=&<=*i*<=β <=0 for all *i*<==<=1,<=2,<=...,<=*N*.
&... | The input consists of one line containing a single integer *N* (1<=β€<=*N*<=β€<=105). | For each subtask, if the required permutation doesn't exist, output a single line containing the word "NO"; otherwise output the word "YES" in the first line and *N* elements of the permutation, separated by spaces, in the second line. If there are several possible permutations in a subtask, output any of them. | [
"3\n",
"6\n"
] | [
"NO\nNO\n",
"YES\n6 5 4 3 2 1 \nYES\n3 6 2 5 1 4\n"
] | none | [
{
"input": "3",
"output": "NO\nNO"
},
{
"input": "6",
"output": "YES\n6 5 4 3 2 1 \nYES\n3 6 2 5 1 4"
},
{
"input": "100000",
"output": "YES\n30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 32 31 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 7... | 483 | 268,390,400 | 0 | 105,709 | |
277 | Set of Points | [
"constructive algorithms",
"geometry"
] | null | null | Convexity of a set of points on the plane is the size of the largest subset of points that form a convex polygon. Your task is to build a set of *n* points with the convexity of exactly *m*. Your set of points should not contain three points that lie on a straight line. | The single line contains two integers *n* and *m* (3<=β€<=*m*<=β€<=100,<=*m*<=β€<=*n*<=β€<=2*m*). | If there is no solution, print "-1". Otherwise, print *n* pairs of integers β the coordinates of points of any set with the convexity of *m*. The coordinates shouldn't exceed 108 in their absolute value. | [
"4 3\n",
"6 3\n",
"6 6\n",
"7 4\n"
] | [
"0 0\n3 0\n0 3\n1 1\n",
"-1\n",
"10 0\n-10 0\n10 1\n9 1\n9 -1\n0 -2\n",
"176166 6377\n709276 539564\n654734 174109\n910147 434207\n790497 366519\n606663 21061\n859328 886001\n"
] | none | [
{
"input": "4 3",
"output": "0 0\n3 0\n0 3\n1 1"
},
{
"input": "6 3",
"output": "-1"
},
{
"input": "6 6",
"output": "10 0\n-10 0\n10 1\n9 1\n9 -1\n0 -2"
},
{
"input": "7 4",
"output": "176166 6377\n709276 539564\n654734 174109\n910147 434207\n790497 366519\n606663 21061\n... | 60 | 0 | 0 | 105,962 | |
38 | The Great Marathon | [
"dp"
] | H. The Great Marathon | 4 | 256 | On the Berland Dependence Day it was decided to organize a great marathon. Berland consists of *n* cities, some of which are linked by two-way roads. Each road has a certain length. The cities are numbered from 1 to *n*. It is known that one can get from any city to any other one by the roads.
*n* runners take part in... | The first input line contains given integers *n* and *m* (3<=β€<=*n*<=β€<=50, *n*<=-<=1<=β€<=*m*<=β€<=1000), where *n* is the number of Berland towns and *m* is the number of roads.
Next in *m* lines road descriptions are given as groups of three integers *v*, *u*, *c*, which are the numbers of linked towns and its length... | Print the single number β the number of ways to distribute the medals. It is guaranteed that the number fits in the standard 64-bit signed data type. | [
"3 2\n1 2 1\n2 3 1\n1 1 1 1\n",
"4 5\n1 2 2\n2 3 1\n3 4 2\n4 1 2\n1 3 3\n1 2 1 1\n",
"3 3\n1 2 2\n2 3 1\n3 1 2\n1 1 1 1\n"
] | [
"3\n",
"19\n",
"4\n"
] | none | [] | 154 | 2,969,600 | -1 | 105,990 |
44 | Triminoes | [
"constructive algorithms",
"greedy"
] | J. Triminoes | 2 | 256 | There are many interesting tasks on domino tilings. For example, an interesting fact is known. Let us take a standard chessboard (8<=Γ<=8) and cut exactly two squares out of it. It turns out that the resulting board can always be tiled using dominoes 1<=Γ<=2, if the two cut out squares are of the same color, otherwise ... | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=1000) β the board size. Next *n* lines contain *m* symbols each and represent the board description. If some position contains ".", then the square in this position has been cut out. Symbol "w" stands for a white square, "b" stands for a black square... | If at least one correct tiling exists, in the first line print "YES" (without quotes), and then β the tiling description. The description must contain *n* lines, *m* symbols in each. The cut out squares, as well as in the input data, are marked by ".". To denote triminoes symbols "a", "b", "c", "d" can be used, and all... | [
"6 10\n.w.wbw.wbw\nwbwbw.w.w.\nbw.wbwbwbw\nw.wbw.wbwb\n...wbw.w.w\n..wbw.wbw.\n",
"2 2\nwb\nbw\n",
"1 3\nwbw\n",
"1 3\n...\n"
] | [
"YES\n.a.aaa.ccc\nbaccc.c.a.\nba.dddcbab\nb.aaa.cbab\n...bbb.b.b\n..ccc.ddd.",
"NO\n",
"YES\nbbb\n",
"YES\n...\n"
] | none | [
{
"input": "6 10\n.w.wbw.wbw\nwbwbw.w.w.\nbw.wbwbwbw\nw.wbw.wbwb\n...wbw.w.w\n..wbw.wbw.",
"output": "YES\n.a.aaa.aaa\nbabbb.a.b.\nba.cccacba\nb.aaa.acba\n...bbb.c.a\n..aaa.aaa."
},
{
"input": "2 2\nwb\nbw",
"output": "NO"
},
{
"input": "1 3\nwbw",
"output": "YES\naaa"
},
{
"... | 92 | 0 | 0 | 106,041 |
195 | Building Forest | [
"data structures",
"dsu",
"graphs"
] | null | null | An oriented weighted forest is an acyclic weighted digraph in which from each vertex at most one edge goes.
The root of vertex *v* of an oriented weighted forest is a vertex from which no edge goes and which can be reached from vertex *v* moving along the edges of the weighted oriented forest. We denote the root of ve... | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=105) β the number of operations of adding a vertex.
Next *n* lines contain descriptions of the operations, the *i*-th line contains the description of the operation of adding the *i*-th vertex in the following format: the first number of a line is an integer ... | Print a single number β the sum of weights of all edges of the resulting graph modulo 1000000007 (109<=+<=7). | [
"6\n0\n0\n1 2 1\n2 1 5 2 2\n1 1 2\n1 3 4\n",
"5\n0\n1 1 5\n0\n0\n2 3 1 4 3\n"
] | [
"30\n",
"9\n"
] | Conside the first sample:
1. Vertex 1 is added. *k*β=β0, thus no edges are added.1. Vertex 2 is added. *k*β=β0, thus no edges are added.1. Vertex 3 is added. *k*β=β1. *v*<sub class="lower-index">1</sub>β=β2, *x*<sub class="lower-index">1</sub>β=β1. Edge from vertex *root*(2)β=β2 to vertex 3 with weight *depth*(2)β+β... | [] | 2,000 | 12,800,000 | 0 | 106,728 | |
979 | Kuro and GCD and XOR and SUM | [
"binary search",
"bitmasks",
"brute force",
"data structures",
"dp",
"dsu",
"greedy",
"math",
"number theory",
"strings",
"trees"
] | null | null | Kuro is currently playing an educational game about numbers. The game focuses on the greatest common divisor (GCD), the XOR value, and the sum of two numbers. Kuro loves the game so much that he solves levels by levels day by day.
Sadly, he's going on a vacation for a day, and he isn't able to continue his solving str... | The first line contains one integer $q$ ($2 \leq q \leq 10^{5}$) β the number of tasks the game wants you to perform.
$q$ lines follow, each line begins with an integer $t_i$ β the type of the task:
- If $t_i = 1$, an integer $u_i$ follow ($1 \leq u_i \leq 10^{5}$) β you have to add $u_i$ to the array $a$. - If $t... | For each task of type $2$, output on one line the desired number $v$, or -1 if no such numbers are found. | [
"5\n1 1\n1 2\n2 1 1 3\n2 1 1 2\n2 1 1 1\n",
"10\n1 9\n2 9 9 22\n2 3 3 18\n1 25\n2 9 9 20\n2 25 25 14\n1 20\n2 26 26 3\n1 14\n2 20 20 9\n"
] | [
"2\n1\n-1\n",
"9\n9\n9\n-1\n-1\n-1\n"
] | In the first example, there are 5 tasks:
- The first task requires you to add $1$ into $a$. $a$ is now $\left\{1\right\}$. - The second task requires you to add $2$ into $a$. $a$ is now $\left\{1, 2\right\}$. - The third task asks you a question with $x = 1$, $k = 1$ and $s = 3$. Taking both $1$ and $2$ as $v$ sat... | [] | 0 | 0 | -1 | 106,813 | |
332 | Students' Revenge | [
"data structures",
"greedy",
"sortings"
] | null | null | A student's life is fraught with complications. Some Berland University students know this only too well. Having studied for two years, they contracted strong antipathy towards the chairperson of some department. Indeed, the person in question wasn't the kindest of ladies to begin with: prone to reforming groups, banni... | The first line contains three integers *n* (1<=β€<=*n*<=β€<=105), *p* (1<=β€<=*p*<=β€<=*n*), *k* (1<=β€<=*k*<=β€<=*p*) β the number of orders the directors are going to discuss, the number of orders to pass and the number of orders to be obeyed by the chairperson, correspondingly. Each of the following *n* lines contains two... | Print in an arbitrary order *p* distinct integers β the numbers of the orders to accept so that the students could carry out the revenge. The orders are indexed from 1 to *n* in the order they occur in the input. If there are multiple solutions, you can print any of them. | [
"5 3 2\n5 6\n5 8\n1 3\n4 3\n4 11\n",
"5 3 3\n10 18\n18 17\n10 20\n20 18\n20 18\n"
] | [
"3 1 2 ",
"2 4 5 "
] | In the first sample one of optimal solutions is to pass orders 1, 2, 3. In this case the chairperson obeys orders number 1 and 2. She gets 10 new grey hairs in the head and the directors' displeasement will equal 3. Note that the same result can be achieved with order 4 instead of order 3.
In the second sample, the ch... | [
{
"input": "5 3 2\n5 6\n5 8\n1 3\n4 3\n4 11",
"output": "3 1 2 "
},
{
"input": "5 3 3\n10 18\n18 17\n10 20\n20 18\n20 18",
"output": "2 4 5 "
},
{
"input": "10 7 4\n4 3\n5 3\n5 5\n4 3\n4 5\n3 5\n4 5\n4 4\n3 5\n4 5",
"output": "1 4 8 3 5 7 10 "
},
{
"input": "20 15 10\n79 84\n... | 30 | 0 | 0 | 107,799 | |
0 | none | [
"none"
] | null | null | Limak is a little grizzly bear. He will once attack Deerland but now he can only destroy trees in role-playing games. Limak starts with a tree with one vertex. The only vertex has index 1 and is a root of the tree.
Sometimes, a game chooses a subtree and allows Limak to attack it. When a subtree is attacked then each ... | The first line of the input contains one integer *q* (1<=β€<=*q*<=β€<=500<=000)Β β the number of queries.
Then, *q* lines follow. The *i*-th of them contains two integers *type**i* and *v**i* (1<=β€<=*type**i*<=β€<=2). If *type**i*<==<=1 then *v**i* denotes a parent of a new vertex, while if *type**i*<==<=2 then you should... | For each query of the second type print one real numberΒ βthe expected value of the penalty if Limak attacks the given subtree. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6.
Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker ... | [
"7\n1 1\n1 1\n2 1\n1 2\n1 3\n2 2\n2 1\n",
"8\n2 1\n1 1\n1 2\n1 3\n1 4\n2 1\n1 4\n2 1\n"
] | [
"0.7500000000\n0.5000000000\n1.1875000000\n",
"0.0000000000\n0.9375000000\n0.9687500000\n"
] | Below, you can see the drawing for the first sample. Red circles denote queries of the second type. | [] | 30 | 0 | 0 | 108,460 | |
734 | Anton and Chess | [
"implementation"
] | null | null | Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead.
The first task he faced is to check whether the king is in check. Anton doesn't know how to implement thi... | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=500<=000)Β β the number of black pieces.
The second line contains two integers *x*0 and *y*0 (<=-<=109<=β€<=*x*0,<=*y*0<=β€<=109)Β β coordinates of the white king.
Then follow *n* lines, each of them contains a character and two integers *x**i* and ... | The only line of the output should contains "YES" (without quotes) if the white king is in check and "NO" (without quotes) otherwise. | [
"2\n4 2\nR 1 1\nB 1 5\n",
"2\n4 2\nR 3 3\nB 1 5\n"
] | [
"YES\n",
"NO\n"
] | Picture for the first sample:
Picture for the second sample: | [
{
"input": "2\n4 2\nR 1 1\nB 1 5",
"output": "YES"
},
{
"input": "2\n4 2\nR 3 3\nB 1 5",
"output": "NO"
},
{
"input": "5\n-1 1\nR -10 10\nQ -9 9\nQ -2 -8\nB -6 10\nB -10 1",
"output": "YES"
},
{
"input": "20\n-321 454\nQ 967 -89\nR -811 454\nQ -404 454\nR -734 454\nQ -804 454... | 1,278 | 0 | 3 | 108,466 | |
883 | Berland.Taxi | [
"data structures"
] | null | null | Berland.Taxi is a new taxi company with *k* cars which started operating in the capital of Berland just recently. The capital has *n* houses on a straight line numbered from 1 (leftmost) to *n* (rightmost), and the distance between any two neighboring houses is the same.
You have to help the company schedule all the t... | The first line of input contains integers *n*, *k* and *m* (2<=β€<=*n*<=β€<=2Β·105, 1<=β€<=*k*,<=*m*<=β€<=2Β·105) β number of houses, number of cars, and number of taxi ride requests. The second line contains integers *x*1,<=*x*2,<=...,<=*x**k* (1<=β€<=*x**i*<=β€<=*n*) β initial positions of cars. *x**i* is a house number at w... | Print *m* lines: the *j*-th line should contain two integer numbers, the answer for the *j*-th ride request β car number assigned by the operator and passenger wait time. | [
"10 1 2\n3\n5 2 8\n9 10 3\n",
"5 2 1\n1 5\n10 3 5\n",
"5 2 2\n1 5\n10 3 5\n20 4 1\n"
] | [
"1 1\n1 5\n",
"1 2\n",
"1 2\n2 1\n"
] | In the first sample test, a request comes in at time 5 and the car needs to get from house 3 to house 2 to pick up the passenger. Therefore wait time will be 1 and the ride will be completed at time 5β+β1β+β6β=β12. The second request comes in at time 9, so the passenger will have to wait for the car to become available... | [] | 30 | 0 | 0 | 108,486 | |
542 | Playing on Graph | [
"graphs",
"shortest paths"
] | null | null | Vova and Marina love offering puzzles to each other. Today Marina offered Vova to cope with the following task.
Vova has a non-directed graph consisting of *n* vertices and *m* edges without loops and multiple edges. Let's define the operation of contraction two vertices *a* and *b* that are not connected by an edge. ... | The first line contains two integers *n*,<=*m* (1<=β€<=*n*<=β€<=1000, 0<=β€<=*m*<=β€<=100<=000) β the number of vertices and the number of edges in the original graph.
Next *m* lines contain the descriptions of edges in the format *a**i*,<=*b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*, *a**i*<=β <=*b**i*), which means that there i... | If it is impossible to obtain a chain from the given graph, print <=-<=1. Otherwise, print the maximum possible number of edges in the resulting chain. | [
"5 4\n1 2\n2 3\n3 4\n3 5\n",
"4 6\n1 2\n2 3\n1 3\n3 4\n2 4\n1 4\n",
"4 2\n1 3\n2 4\n"
] | [
"3\n",
"-1\n",
"2\n"
] | In the first sample test you can contract vertices 4 and 5 and obtain a chain of length 3.
In the second sample test it is initially impossible to contract any pair of vertexes, so it is impossible to achieve the desired result.
In the third sample test you can contract vertices 1 and 2 and obtain a chain of length 2... | [
{
"input": "5 4\n1 2\n2 3\n3 4\n3 5",
"output": "3"
},
{
"input": "4 6\n1 2\n2 3\n1 3\n3 4\n2 4\n1 4",
"output": "-1"
},
{
"input": "4 2\n1 3\n2 4",
"output": "2"
},
{
"input": "1 0",
"output": "0"
},
{
"input": "1000 0",
"output": "0"
},
{
"input": "1... | 0 | 0 | -1 | 108,730 | |
56 | Corporation Mail | [
"data structures",
"expression parsing",
"implementation"
] | C. Corporation Mail | 2 | 256 | The Beroil corporation structure is hierarchical, that is it can be represented as a tree. Let's examine the presentation of this structure as follows:
- *employee* ::= *name*. | *name*:*employee*1,*employee*2, ... ,*employee**k*. - *name* ::= name of an employee
That is, the description of each employee consists o... | The first and single line contains the corporation structure which is a string of length from 1 to 1000 characters. It is guaranteed that the description is correct. Every name is a string consisting of capital Latin letters from 1 to 10 symbols in length. | Print a single number β the number of uncomfortable situations in the company. | [
"MIKE:MAX.,ARTEM:MIKE..,DMITRY:DMITRY.,DMITRY...\n",
"A:A..\n",
"A:C:C:C:C.....\n"
] | [
"3\n",
"1\n",
"6\n"
] | none | [
{
"input": "A:A..",
"output": "1"
},
{
"input": "CK:CK.,CK.,CK..",
"output": "3"
},
{
"input": "RHLGWEVBJ:KAWUINWEI:KAWUINWEI..,ZQATMW.,KAWUINWEI.,RSWN..",
"output": "1"
},
{
"input": "GIRRY.",
"output": "0"
},
{
"input": "XGB:QJNGARRAZV:DWGDCCU:ARDKJV:P:MXBLZKLPY... | 218 | 0 | 0 | 109,211 |
40 | Number Table | [
"combinatorics"
] | E. Number Table | 2 | 216 | As it has been found out recently, all the Berland's current economical state can be described using a simple table *n*<=Γ<=*m* in size. *n* β the number of days in each Berland month, *m* β the number of months. Thus, a table cell corresponds to a day and a month of the Berland's year. Each cell will contain either 1,... | The first line contains integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=1000). The second line contains the integer *k* (0<=β€<=*k*<=<<=*max*(*n*,<=*m*)) β the number of cells in which the data had been preserved. The next *k* lines contain the data on the state of the table in the preserved cells. Each line is of the form... | Print the number of different tables that could conform to the preserved data modulo *p*. | [
"2 2\n0\n100\n",
"2 2\n1\n1 1 -1\n100\n"
] | [
"2\n",
"1\n"
] | none | [
{
"input": "2 2\n0\n100",
"output": "2"
},
{
"input": "2 2\n1\n1 1 -1\n100",
"output": "1"
},
{
"input": "7 6\n4\n3 2 -1\n7 1 1\n2 1 1\n3 4 -1\n179460773",
"output": "0"
},
{
"input": "6 2\n3\n5 1 -1\n4 2 1\n1 2 -1\n958300150",
"output": "4"
},
{
"input": "6 9\n3\... | 92 | 0 | 0 | 109,296 |
933 | A Creative Cutout | [
"brute force",
"combinatorics",
"math"
] | null | null | Everything red frightens Nian the monster. So do red paper and... you, red on Codeforces, potential or real.
Big Banban has got a piece of paper with endless lattice points, where lattice points form squares with the same area. His most favorite closed shape is the circle because of its beauty and simplicity. Once he ... | The first line contains one integer *m* (1<=β€<=*m*<=β€<=1012). | In the first line print one integer representing . | [
"5\n",
"233\n"
] | [
"387\n",
"788243189\n"
] | A piece of paper with 5 circles is shown in the following.
There are 5 types of lattice points where the degree of beauty of each red point is 1β+β2β+β3β+β4β+β5β=β15, the degree of beauty of each orange point is 2β+β3β+β4β+β5β=β14, the degree of beauty of each green point is 4β+β5β=β9, the degree of beauty of each blu... | [] | 1,668 | 268,390,400 | 0 | 109,442 | |
990 | Flow Control | [
"dfs and similar",
"dp",
"greedy",
"trees"
] | null | null | You have to handle a very complex water distribution system. The system consists of $n$ junctions and $m$ pipes, $i$-th pipe connects junctions $x_i$ and $y_i$.
The only thing you can do is adjusting the pipes. You have to choose $m$ integer numbers $f_1$, $f_2$, ..., $f_m$ and use them as pipe settings. $i$-th pipe w... | The first line contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of junctions.
The second line contains $n$ integers $s_1, s_2, \dots, s_n$ ($-10^4 \le s_i \le 10^4$) β constraints for the junctions.
The third line contains an integer $m$ ($0 \le m \le 2 \cdot 10^5$) β the number of pipes.
$i$-th of ... | If you can choose such integer numbers $f_1, f_2, \dots, f_m$ in such a way that all requirements on incoming and outcoming flows are satisfied, then output "Possible" in the first line. Then output $m$ lines, $i$-th line should contain $f_i$ β the chosen setting numbers for the pipes. Pipes are numbered in order they ... | [
"4\n3 -10 6 1\n5\n1 2\n3 2\n2 4\n3 4\n3 1\n",
"4\n3 -10 6 4\n5\n1 2\n3 2\n2 4\n3 4\n3 1\n"
] | [
"Possible\n4\n-6\n8\n-7\n7\n",
"Impossible\n"
] | none | [
{
"input": "4\n3 -10 6 1\n5\n1 2\n3 2\n2 4\n3 4\n3 1",
"output": "Possible\n-3\n-6\n1\n0\n0"
},
{
"input": "4\n3 -10 6 4\n5\n1 2\n3 2\n2 4\n3 4\n3 1",
"output": "Impossible"
},
{
"input": "1\n0\n0",
"output": "Possible"
},
{
"input": "1\n123\n0",
"output": "Impossible"
... | 1,388 | 80,486,400 | 3 | 109,736 | |
765 | Souvenirs | [
"data structures"
] | null | null | Artsem is on vacation and wants to buy souvenirs for his two teammates. There are *n* souvenir shops along the street. In *i*-th shop Artsem can buy one souvenir for *a**i* dollars, and he cannot buy more than one souvenir in one shop. He doesn't want to introduce envy in his team, so he wants to buy two souvenirs with... | The first line contains an integer *n* (2<=β€<=*n*<=β€<=105).
The second line contains *n* space-separated integers *a*1, ..., *a**n* (0<=β€<=*a**i*<=β€<=109).
The third line contains the number of queries *m* (1<=β€<=*m*<=β€<=3Β·105).
Next *m* lines describe the queries. *i*-th of these lines contains two space-separated ... | Print the answer to each query in a separate line. | [
"8\n3 1 4 1 5 9 2 6\n4\n1 8\n1 3\n4 8\n5 7\n"
] | [
"0\n1\n1\n3\n"
] | none | [] | 3,000 | 921,600 | 0 | 109,861 | |
0 | none | [
"none"
] | null | null | Nanami is an expert at playing games. This day, Nanami's good friend Hajime invited her to watch a game of baseball. Unwilling as she was, she followed him to the stadium. But Nanami had no interest in the game, so she looked around to see if there was something that might interest her. That's when she saw the digital ... | The first line contains three space-separated integers *n*, *m* and *q*Β (1<=β€<=*n*,<=*m*,<=*q*<=β€<=1000) β the height and width of the digital board, and the number of operations.
Then follow *n* lines, each line containing *m* space-separated integers. The *j*-th integer of the *i*-th line is *a**i*,<=*j* β the initi... | For each query, print a single line containing one integer β the answer to Nanami's query. | [
"3 4 5\n0 1 1 0\n1 0 0 1\n0 1 1 0\n2 2 2\n2 1 2\n1 2 2\n1 2 3\n2 2 2\n",
"3 3 4\n1 1 1\n1 1 1\n1 1 1\n2 2 2\n1 2 2\n2 1 1\n2 2 1\n"
] | [
"0\n2\n6\n",
"6\n3\n3\n"
] | Consider the first sample.
The first query specifies pixel (2,β2), which is dark itself, so there are no valid light blocks, thus the answer is 0.
The second query specifies pixel (1,β2). The biggest light block is the block with (1,β2) as its upper-left vertex and (1,β3) as its lower-right vertex.
The last query sp... | [] | 77 | 0 | 0 | 109,904 | |
182 | Optimal Sum | [
"data structures",
"greedy"
] | null | null | And here goes another problem on arrays. You are given positive integer *len* and array *a* which consists of *n* integers *a*1, *a*2, ..., *a**n*. Let's introduce two characteristics for the given array.
- Let's consider an arbitrary interval of the array with length *len*, starting in position *i*. Value , is the m... | The first line contains two integers *n*, *len* (1<=β€<=*len*<=β€<=*n*<=β€<=105) β the number of elements in the array and the length of the chosen subinterval of the array, correspondingly.
The second line contains a sequence consisting of *n* integers *a*1, *a*2, ..., *a**n* (|*a**i*|<=β€<=109) β the original array.
... | In a single line print the maximum possible optimal sum after no more than *k* acceptable operations are fulfilled.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier. | [
"5 3\n0 -2 3 -5 1\n2\n",
"5 2\n1 -3 -10 4 1\n3\n",
"3 3\n-2 -5 4\n1\n"
] | [
"10\n",
"14\n",
"11\n"
] | none | [] | 0 | 0 | -1 | 110,069 | |
575 | Bribes | [
"dfs and similar",
"graphs",
"trees"
] | null | null | Ruritania is a country with a very badly maintained road network, which is not exactly good news for lorry drivers that constantly have to do deliveries. In fact, when roads are maintained, they become one-way. It turns out that it is sometimes impossible to get from one town to another in a legal way β however, we kno... | The first line contains *N*, the number of towns in Ruritania. The following *N*<=-<=1 lines contain information regarding individual roads between towns. A road is represented by a tuple of integers (*a*,*b*,*x*), which are separated with a single whitespace character. The numbers *a* and *b* represent the cities conn... | The output should contain a single number: the least amount of thousands of Ruritanian dinars Borna should allocate for bribes, modulo 109<=+<=7. | [
"5\n1 2 0\n2 3 0\n5 1 1\n3 4 1\n5\n5 4 5 2 2\n"
] | [
"4\n"
] | Borna first takes the route 1βββ5 and has to pay 1000 dinars. After that, he takes the route 5βββ1βββ2βββ3βββ4 and pays nothing this time. However, when he has to return via 4βββ3βββ2βββ1βββ5, he needs to prepare 3000 (1000+2000) dinars. Afterwards, getting to 2 via 5βββ1βββ2 will cost him nothing. Finally, he doesn't ... | [] | 62 | 0 | 0 | 110,072 | |
0 | none | [
"none"
] | null | null | You play the game with your friend. The description of this game is listed below.
Your friend creates *n* distinct strings of the same length *m* and tells you all the strings. Then he randomly chooses one of them. He chooses strings equiprobably, i.e. the probability of choosing each of the *n* strings equals . You ... | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=50)Β β the number of strings your friend came up with.
The next *n* lines contain the strings that your friend has created. It is guaranteed that all the strings are distinct and only consist of large and small English letters. Besides, the lengths of all stri... | Print the single number β the expected value. Your answer will be considered correct if its absolute or relative error doesn't exceed 10<=-<=9. | [
"2\naab\naac\n",
"3\naaA\naBa\nCaa\n",
"3\naca\nvac\nwqq\n"
] | [
"2.000000000000000\n",
"1.666666666666667\n",
"1.000000000000000\n"
] | In the first sample the strings only differ in the character in the third position. So only the following situations are possible:
- you guess the string in one question. The event's probability is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/adaad783a304f9e72218e6e79114c6551a112aad.p... | [] | 46 | 0 | 0 | 110,241 | |
34 | Collisions | [
"brute force",
"implementation",
"math"
] | E. Collisions | 2 | 256 | On a number line there are *n* balls. At time moment 0 for each ball the following data is known: its coordinate *x**i*, speed *v**i* (possibly, negative) and weight *m**i*. The radius of the balls can be ignored.
The balls collide elastically, i.e. if two balls weighing *m*1 and *m*2 and with speeds *v*1 and *v*2 col... | The first line contains two integers *n* and *t* (1<=β€<=*n*<=β€<=10,<=0<=β€<=*t*<=β€<=100) β amount of balls and duration of the process. Then follow *n* lines, each containing three integers: *x**i*, *v**i*, *m**i* (1<=β€<=|*v**i*|,<=*m**i*<=β€<=100,<=|*x**i*|<=β€<=100) β coordinate, speed and weight of the ball with index ... | Output *n* numbers β coordinates of the balls *t* seconds after. Output the numbers accurate to at least 4 digits after the decimal point. | [
"2 9\n3 4 5\n0 7 8\n",
"3 10\n1 2 3\n4 -5 6\n7 -8 9\n"
] | [
"68.538461538\n44.538461538\n",
"-93.666666667\n-74.666666667\n-15.666666667\n"
] | none | [
{
"input": "2 9\n3 4 5\n0 7 8",
"output": "68.538461538\n44.538461538"
},
{
"input": "3 10\n1 2 3\n4 -5 6\n7 -8 9",
"output": "-93.666666667\n-74.666666667\n-15.666666667"
},
{
"input": "6 76\n-35 -79 86\n-76 -20 64\n-91 79 10\n12 25 51\n62 6 30\n8 -14 65",
"output": "-980.877658851\... | 92 | 0 | 0 | 110,446 |
0 | none | [
"none"
] | null | null | Petya likes horse racing very much. Horses numbered from *l* to *r* take part in the races. Petya wants to evaluate the probability of victory; for some reason, to do that he needs to know the amount of nearly lucky horses' numbers. A nearly lucky number is an integer number that has at least two lucky digits the dista... | The first line contains two integers *t* and *k* (1<=β€<=*t*,<=*k*<=β€<=1000) β the number of segments and the distance between the numbers correspondingly. Next *t* lines contain pairs of integers *l**i* and *r**i* (1<=β€<=*l*<=β€<=*r*<=β€<=101000). All numbers are given without the leading zeroes. Numbers in each line are... | Output *t* lines. In each line print one integer β the answer for the corresponding segment modulo 1000000007 (109<=+<=7). | [
"1 2\n1 100\n",
"1 2\n70 77\n",
"2 1\n1 20\n80 100\n"
] | [
"4\n",
"2\n",
"0\n0\n"
] | In the first sample, the four nearly lucky numbers are 44, 47, 74, 77.
In the second sample, only 74 and 77 are in the given segment. | [] | 60 | 0 | 0 | 110,598 | |
303 | Minimum Modular | [
"brute force",
"graphs",
"math",
"number theory"
] | null | null | You have been given *n* distinct integers *a*1,<=*a*2,<=...,<=*a**n*. You can remove at most *k* of them. Find the minimum modular *m* (*m*<=><=0), so that for every pair of the remaining integers (*a**i*,<=*a**j*), the following unequality holds: . | The first line contains two integers *n* and *k* (1<=<=β€<=*n*<=<=β€<=5000,<=0<=β€<=*k*<=β€<=4), which we have mentioned above.
The second line contains *n* distinct integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=106). | Print a single positive integer β the minimum *m*. | [
"7 0\n0 2 3 6 7 12 18\n",
"7 1\n0 2 3 6 7 12 18\n"
] | [
"13\n",
"7\n"
] | none | [] | 2,000 | 67,072,000 | 0 | 110,805 | |
459 | Pashmak and Graph | [
"dp",
"sortings"
] | null | null | Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem.
You are given a weighted directed graph with *n* vertices and *m* edges. You need to find a path (... | The first line contains two integers *n*, *m* (2<=β€<=*n*<=β€<=3Β·105;Β 1<=β€<=*m*<=β€<=*min*(*n*Β·(*n*<=-<=1),<=3Β·105)). Then, *m* lines follows. The *i*-th line contains three space separated integers: *u**i*, *v**i*, *w**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*;Β 1<=β€<=*w**i*<=β€<=105) which indicates that there's a directed edge w... | Print a single integer β the answer to the problem. | [
"3 3\n1 2 1\n2 3 1\n3 1 1\n",
"3 3\n1 2 1\n2 3 2\n3 1 3\n",
"6 7\n1 2 1\n3 2 5\n2 4 2\n2 5 2\n2 6 9\n5 4 3\n4 3 4\n"
] | [
"1\n",
"3\n",
"6\n"
] | In the first sample the maximum trail can be any of this trails: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/1534088dd4d998a9bcc7e17dba96f7c213c54fc6.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the second sample the maximum trail is <img align="middle" class="tex-formula" ... | [
{
"input": "3 3\n1 2 1\n2 3 1\n3 1 1",
"output": "1"
},
{
"input": "3 3\n1 2 1\n2 3 2\n3 1 3",
"output": "3"
},
{
"input": "6 7\n1 2 1\n3 2 5\n2 4 2\n2 5 2\n2 6 9\n5 4 3\n4 3 4",
"output": "6"
},
{
"input": "2 2\n1 2 1\n2 1 2",
"output": "2"
},
{
"input": "4 3\n1 ... | 1,000 | 22,220,800 | 0 | 111,627 | |
486 | Valid Sets | [
"dfs and similar",
"dp",
"math",
"trees"
] | null | null | As you know, an undirected connected graph with *n* nodes and *n*<=-<=1 edges is called a tree. You are given an integer *d* and a tree consisting of *n* nodes. Each node *i* has a value *a**i* associated with it.
We call a set *S* of tree nodes valid if following conditions are satisfied:
1. *S* is non-empty.1. *S*... | The first line contains two space-separated integers *d* (0<=β€<=*d*<=β€<=2000) and *n* (1<=β€<=*n*<=β€<=2000).
The second line contains *n* space-separated positive integers *a*1,<=*a*2,<=...,<=*a**n*(1<=β€<=*a**i*<=β€<=2000).
Then the next *n*<=-<=1 line each contain pair of integers *u* and *v* (1<=β€<=*u*,<=*v*<=β€<=*n*)... | Print the number of valid sets modulo 1000000007. | [
"1 4\n2 1 3 2\n1 2\n1 3\n3 4\n",
"0 3\n1 2 3\n1 2\n2 3\n",
"4 8\n7 8 7 5 4 6 4 10\n1 6\n1 2\n5 8\n1 3\n3 5\n6 7\n3 4\n"
] | [
"8\n",
"3\n",
"41\n"
] | In the first sample, there are exactly 8 valid sets: {1},β{2},β{3},β{4},β{1,β2},β{1,β3},β{3,β4} and {1,β3,β4}. Set {1,β2,β3,β4} is not valid, because the third condition isn't satisfied. Set {1,β4} satisfies the third condition, but conflicts with the second condition. | [] | 30 | 0 | 0 | 111,843 | |
0 | none | [
"none"
] | null | null | In the country of Never, there are *n* cities and a well-developed road system. There is exactly one bidirectional road between every pair of cities, thus, there are as many as roads! No two roads intersect, and no road passes through intermediate cities. The art of building tunnels and bridges has been mastered by Ne... | The first line contains a single integer *n* (2<=β€<=*n*<=β€<=2000)Β β the number of cities in Never.
The following *n*<=-<=1 lines contain the description of the road network. The *i*-th of these lines contains *n*<=-<=*i* integers. The *j*-th integer in the *i*-th line denotes the perishability of the road between citi... | For each city in order from 1 to *n*, output the minimum possible sum of perishabilities of the routes to this city from all the other cities of Never if the signposts are set in a way which minimizes this sum. | [
"3\n1 2\n3\n",
"6\n2 9 9 6 6\n7 1 9 10\n9 2 5\n4 10\n8\n"
] | [
"2\n2\n3\n",
"6\n5\n7\n5\n7\n11\n"
] | The first example is explained by the picture below. From left to right, there is the initial road network and the optimal directions of the signposts in case the museum is built in city 1, 2 and 3, respectively. The museum city is represented by a blue circle, the directions of the signposts are represented by green a... | [] | 3,000 | 46,489,600 | 0 | 112,165 | |
756 | Long number | [
"expression parsing",
"math",
"number theory"
] | null | null | Consider the following grammar:
- <expression> ::= <term> | <expression> '+' <term>- <term> ::= <number> | <number> '-' <number> | <number> '(' <expression> ')'- <number> ::= <pos_digit> | <number> <digit>- <digit> ::= '0' | &... | The only line contains a non-empty string at most 105 characters long which is valid according to the given grammar. In particular, it means that in terms l-r *l*<=β€<=*r* holds. | Print single integerΒ β the number described by the expression modulo 109<=+<=7. | [
"8-11\n",
"2(2-4+1)+2(2(17))\n",
"1234-5678\n",
"1+2+3+4-5+6+7-9\n"
] | [
"891011\n",
"100783079\n",
"745428774\n",
"123456789\n"
] | none | [
{
"input": "8-11",
"output": "891011"
},
{
"input": "2(2-4+1)+2(2(17))",
"output": "100783079"
},
{
"input": "1234-5678",
"output": "745428774"
},
{
"input": "1+2+3+4-5+6+7-9",
"output": "123456789"
},
{
"input": "598777",
"output": "598777"
},
{
"inpu... | 2,000 | 13,414,400 | 0 | 112,175 | |
643 | Choosing Ads | [
"data structures"
] | null | null | One social network developer recently suggested a new algorithm of choosing ads for users.
There are *n* slots which advertisers can buy. It is possible to buy a segment of consecutive slots at once. The more slots you own, the bigger are the chances your ad will be shown to users.
Every time it is needed to choose a... | The first line of the input contains three integers *n*, *m* and *p* (1<=β€<=*n*,<=*m*<=β€<=150<=000,<=20<=β€<=*p*<=β€<=100)Β β the number of slots, the number of queries to your system and threshold for which display of the ad is guaranteed.
Next line contains *n* integers *a**i* (1<=β€<=*a**i*<=β€<=150<=000), where the *i*... | For each query of the second type answer should be printed in a separate line. First integer of the answer should be the number of advertisements that will be shown . Next *cnt* integers should be advertisers' ids.
It is allowed to print one advertiser more than once, but each advertiser that owns at least slots of ... | [
"5 9 33\n1 2 1 3 3\n2 1 5\n2 1 5\n2 1 3\n2 3 3\n1 2 4 5\n2 1 5\n2 3 5\n1 4 5 1\n2 1 5\n"
] | [
"3 1 2 3\n2 1 3\n2 2 1\n3 1 1000 1000\n1 5\n2 5 3\n2 1 5"
] | Samples demonstrate that you actually have quite a lot of freedom in choosing advertisers. | [] | 46 | 0 | 0 | 112,676 | |
598 | Cut Length | [
"geometry"
] | null | null | Given simple (without self-intersections) *n*-gon. It is not necessary convex. Also you are given *m* lines. For each line find the length of common part of the line and the *n*-gon.
The boundary of *n*-gon belongs to polygon. It is possible that *n*-gon contains 180-degree angles. | The first line contains integers *n* and *m* (3<=β€<=*n*<=β€<=1000;1<=β€<=*m*<=β€<=100). The following *n* lines contain coordinates of polygon vertices (in clockwise or counterclockwise direction). All vertices are distinct.
The following *m* lines contain line descriptions. Each of them contains two distict points of a ... | Print *m* lines, the *i*-th line should contain the length of common part of the given *n*-gon and the *i*-th line. The answer will be considered correct if the absolute or relative error doesn't exceed 10<=-<=6. | [
"4 3\n0 0\n1 0\n1 1\n0 1\n0 0 1 1\n0 0 0 1\n0 0 1 -1\n"
] | [
"1.41421356237309514547\n1.00000000000000000000\n0.00000000000000000000\n"
] | none | [
{
"input": "4 3\n0 0\n1 0\n1 1\n0 1\n0 0 1 1\n0 0 0 1\n0 0 1 -1",
"output": "1.41421356237309514547\n1.00000000000000000000\n0.00000000000000000000"
},
{
"input": "4 9\n0 0\n0 1\n1 1\n1 0\n0 0 1 1\n1 1 0 0\n0 0 1 0\n0 0 0.5 0\n0 0.5 1 0.5\n0 1 1 1\n1 1 1 0\n0.75 0.75 0.75 0.25\n0 0.25 1 0.75",
"... | 46 | 0 | 0 | 112,773 | |
0 | none | [
"none"
] | null | null | There are *n* bears in the inn and *p* places to sleep. Bears will party together for some number of nights (and days).
Bears love drinking juice. They don't like wine but they can't distinguish it from juice by taste or smell.
A bear doesn't sleep unless he drinks wine. A bear must go to sleep a few hours after drin... | The only line of the input contains three integers *n*, *p* and *q* (1<=β€<=*n*<=β€<=109, 1<=β€<=*p*<=β€<=130, 1<=β€<=*q*<=β€<=2<=000<=000)Β β the number of bears, the number of places to sleep and the number of scenarios, respectively. | Print one integer, equal to . | [
"5 1 3\n",
"1 100 4\n",
"3 2 1\n",
"100 100 100\n"
] | [
"32\n",
"4\n",
"7\n",
"381863924\n"
] | In the first sample, there are 5 bears and only 1 place to sleep. We have *R*<sub class="lower-index">1</sub>β=β6,β*R*<sub class="lower-index">2</sub>β=β11,β*R*<sub class="lower-index">3</sub>β=β16 so the answer is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/b3272ae710321a0a8ceffa5d8c9a... | [] | 5,000 | 4,198,400 | 0 | 112,807 | |
117 | Not Quick Transformation | [
"divide and conquer",
"math"
] | null | null | Let *a* be an array consisting of *n* numbers. The array's elements are numbered from 1 to *n*, *even* is an array consisting of the numerals whose numbers are even in *a* (*even**i*<==<=*a*2*i*, 1<=β€<=2*i*<=β€<=*n*), *odd* is an array consisting of the numberals whose numbers are odd in *Π°* (*odd**i*<==<=*a*2*i*<=-<=1,... | The first line contains three integers *n*, *m*, *mod* (1<=β€<=*n*<=β€<=1018,<=1<=β€<=*m*<=β€<=105,<=1<=β€<=*mod*<=β€<=109). Next *m* lines describe the queries. Each query is defined by four integers *l*, *r*, *u*, *v* (1<=β€<=*l*<=β€<=*r*<=β€<=*n*, 1<=β€<=*u*<=β€<=*v*<=β€<=1018).
Please do not use the %lld specificator to read ... | Print *m* lines each containing an integer β remainder modulo *mod* of the query result. | [
"4 5 10000\n2 3 4 5\n2 4 1 3\n1 2 2 4\n2 3 3 5\n1 3 3 4\n",
"2 5 10000\n1 2 2 2\n1 1 4 5\n1 1 2 5\n1 1 1 3\n1 2 5 5\n"
] | [
"0\n5\n3\n3\n3\n",
"2\n0\n0\n1\n0\n"
] | Let's consider the first example. First let's construct an array *b*β=β*F*(*a*)β=β*F*([1,β2,β3,β4]).
- Step 1. *F*([1,β2,β3,β4])β=β*F*([1,β3])β+β*F*([2,β4]) - Step 2. *F*([1,β3])β=β*F*([1])β+β*F*([3])β=β[1]β+β[3]β=β[1,β3] - Step 3. *F*([2,β4])β=β*F*([2])β+β*F*([4])β=β[2]β+β[4]β=β[2,β4] - Step 4. *b*β=β*F*([1,β2,β... | [] | 154 | 0 | -1 | 113,064 | |
45 | Goats and Wolves | [
"greedy"
] | F. Goats and Wolves | 2 | 256 | Once Vasya needed to transport *m* goats and *m* wolves from riverbank to the other as quickly as possible. The boat can hold *n* animals and Vasya, in addition, he is permitted to put less than *n* animals in the boat. If in one place (on one of the banks or in the boat) the wolves happen to strictly outnumber the goa... | The first line contains two space-separated numbers *m* and *n* (1<=β€<=*m*,<=*n*<=β€<=105) β the number of animals and the boat's capacity. | If it is impossible to transport all the animals so that no one got upset, and all the goats survived, print -1. Otherwise print the single number β how many times Vasya will have to cross the river. | [
"3 2\n",
"33 3\n"
] | [
"11\n",
"-1\n"
] | The first sample match to well-known problem for children. | [
{
"input": "3 2",
"output": "11"
},
{
"input": "33 3",
"output": "-1"
},
{
"input": "2 3",
"output": "3"
},
{
"input": "100000 100000",
"output": "5"
},
{
"input": "100000 4",
"output": "199997"
},
{
"input": "1 3",
"output": "1"
},
{
"inpu... | 62 | 0 | 0 | 113,597 |
60 | Mushroom Gnomes | [
"math",
"matrices"
] | E. Mushroom Gnomes | 3 | 256 | Once upon a time in the thicket of the mushroom forest lived mushroom gnomes. They were famous among their neighbors for their magic mushrooms. Their magic nature made it possible that between every two neighboring mushrooms every minute grew another mushroom with the weight equal to the sum of weights of two neighbori... | The first line contains four integers *n*, *x*, *y*, *p* (1<=β€<=*n*<=β€<=106,<=0<=β€<=*x*,<=*y*<=β€<=1018,<=*x*<=+<=*y*<=><=0,<=2<=β€<=*p*<=β€<=109) which represent the number of mushrooms, the number of minutes after the first replanting, the number of minutes after the second replanting and the module. The next line co... | The answer should contain a single number which is the total weights of the mushrooms modulo *p* in the end after *x*<=+<=*y* minutes. | [
"2 1 0 657276545\n1 2\n",
"2 1 1 888450282\n1 2\n",
"4 5 0 10000\n1 2 3 4\n"
] | [
"6\n",
"14\n",
"1825\n"
] | none | [
{
"input": "2 1 0 657276545\n1 2",
"output": "6"
},
{
"input": "2 1 1 888450282\n1 2",
"output": "14"
},
{
"input": "4 5 0 10000\n1 2 3 4",
"output": "1825"
},
{
"input": "4 0 8 78731972\n1 52 76 81",
"output": "1108850"
},
{
"input": "4 0 8 414790855\n1 88 97 99"... | 92 | 0 | 0 | 113,635 |
677 | Vanya and Treasure | [
"data structures",
"dp",
"graphs",
"shortest paths"
] | null | null | Vanya is in the palace that can be represented as a grid *n*<=Γ<=*m*. Each room contains a single chest, an the room located in the *i*-th row and *j*-th columns contains the chest of type *a**ij*. Each chest of type *x*<=β€<=*p*<=-<=1 contains a key that can open any chest of type *x*<=+<=1, and all chests of type 1 ar... | The first line of the input contains three integers *n*, *m* and *p* (1<=β€<=*n*,<=*m*<=β€<=300,<=1<=β€<=*p*<=β€<=*n*Β·*m*)Β β the number of rows and columns in the table representing the palace and the number of different types of the chests, respectively.
Each of the following *n* lines contains *m* integers *a**ij* (1<=β€... | Print one integerΒ β the minimum possible total distance Vanya has to walk in order to get the treasure from the chest of type *p*. | [
"3 4 3\n2 1 1 1\n1 1 1 1\n2 1 1 3\n",
"3 3 9\n1 3 5\n8 9 7\n4 6 2\n",
"3 4 12\n1 2 3 4\n8 7 6 5\n9 10 11 12\n"
] | [
"5\n",
"22\n",
"11\n"
] | none | [] | 30 | 0 | 0 | 114,100 | |
550 | Regular Bridge | [
"constructive algorithms",
"graphs",
"implementation"
] | null | null | An undirected graph is called *k*-regular, if the degrees of all its vertices are equal *k*. An edge of a connected graph is called a bridge, if after removing it the graph is being split into two connected components.
Build a connected undirected *k*-regular graph containing at least one bridge, or else state that su... | The single line of the input contains integer *k* (1<=β€<=*k*<=β€<=100) β the required degree of the vertices of the regular graph. | Print "NO" (without quotes), if such graph doesn't exist.
Otherwise, print "YES" in the first line and the description of any suitable graph in the next lines.
The description of the made graph must start with numbers *n* and *m* β the number of vertices and edges respectively.
Each of the next *m* lines must cont... | [
"1\n"
] | [
"YES\n2 1\n1 2\n"
] | In the sample from the statement there is a suitable graph consisting of two vertices, connected by a single edge. | [
{
"input": "1",
"output": "YES\n2 1\n1 2"
},
{
"input": "3",
"output": "YES\n10 15\n1 6\n1 2\n1 3\n2 4\n2 5\n3 4\n3 5\n4 5\n6 7\n6 8\n7 9\n7 10\n8 9\n8 10\n9 10"
},
{
"input": "11",
"output": "YES\n26 143\n1 14\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n2 12\n2 13\n2 4\n2 5... | 46 | 0 | 0 | 114,357 | |
847 | Berland SU Computer Network | [
"constructive algorithms",
"dfs and similar",
"graphs",
"hashing",
"trees"
] | null | null | In the computer network of the Berland State University there are *n* routers numbered from 1 to *n*. Some pairs of routers are connected by patch cords. Information can be transmitted over patch cords in both direction. The network is arranged in such a way that communication between any two routers (directly or throu... | The first line contains a single integer *n* (2<=β€<=*n*<=β€<=1000) β the number of routers in the network.
The *i*-th of the following *n* lines contains a description of the lists for the router *i*.
The description of each list begins with the number of routers in it. Then the symbol ':' follows, and after that the ... | Print -1 if no solution exists.
In the other case print to the first line *n*<=-<=1 β the total number of patch cords in the network. In each of the following *n*<=-<=1 lines print two integers β the routers which are directly connected by a patch cord. Information about each patch cord must be printed exactly once.
... | [
"3\n2:3,2\n1:1-1:3\n2:1,2\n",
"5\n4:2,5,3,4\n1:4-1:1-2:5,3\n4:4,5,2,1\n4:2,1,3,5\n1:3-3:4,2,1\n",
"3\n1:2-1:3\n1:1-1:3\n1:1-1:2\n"
] | [
"2\n2 1\n2 3\n",
"4\n2 1\n2 4\n5 2\n3 5\n",
"-1\n"
] | The first example is analyzed in the statement.
The answer to the second example is shown on the picture.
The first router has one list, which contains all other routers. The second router has three lists: the first β the single router 4, the second β the single router 1, the third β two routers 3 and 5. The third ro... | [
{
"input": "3\n2:3,2\n1:1-1:3\n2:1,2",
"output": "2\n2 1\n2 3"
},
{
"input": "5\n4:2,5,3,4\n1:4-1:1-2:5,3\n4:4,5,2,1\n4:2,1,3,5\n1:3-3:4,2,1",
"output": "4\n2 1\n2 4\n5 2\n3 5"
},
{
"input": "3\n1:2-1:3\n1:1-1:3\n1:1-1:2",
"output": "-1"
},
{
"input": "2\n1:2\n1:1",
"outp... | 46 | 0 | 0 | 114,453 | |
70 | Information Reform | [
"dp",
"implementation",
"trees"
] | E. Information Reform | 2 | 256 | Thought it is already the XXI century, the Mass Media isn't very popular in Walrusland. The cities get news from messengers who can only travel along roads. The network of roads in Walrusland is built so that it is possible to get to any city from any other one in exactly one way, and the roads' lengths are equal.
The... | The first line contains two given numbers *n* and *k* (1<=β€<=*n*<=β€<=180,<=1<=β€<=*k*<=β€<=105).
The second line contains *n*<=-<=1 integers *d**i*, numbered starting with 1 (*d**i*<=β€<=*d**i*<=+<=1,<=0<=β€<=*d**i*<=β€<=105).
Next *n*<=-<=1 lines contain the pairs of cities connected by a road. | On the first line print the minimum number of fishlars needed for a year's maintenance. On the second line print *n* numbers, where the *i*-th number will represent the number of the regional center, appointed to the *i*-th city. If the *i*-th city is a regional center itself, then you should print number *i*.
If ther... | [
"8 10\n2 5 9 11 15 19 20\n1 4\n1 3\n1 7\n4 6\n2 8\n2 3\n3 5\n"
] | [
"38\n3 3 3 4 3 4 3 3 "
] | none | [] | 92 | 0 | 0 | 114,461 |
335 | Counting Skyscrapers | [
"dp",
"math",
"probabilities"
] | null | null | A number of skyscrapers have been built in a line. The number of skyscrapers was chosen uniformly at random between 2 and 314! (314 factorial, a very large number). The height of each skyscraper was chosen randomly and independently, with height *i* having probability 2<=-<=*i* for all positive integers *i*. The floors... | The first line of input will be a name, either string "Alice" or "Bob". The second line of input contains two integers *n* and *h* (2<=β€<=*n*<=β€<=30000, 0<=β€<=*h*<=β€<=30). If the name is "Alice", then *n* represents the value of Alice's counter when she reaches the rightmost skyscraper, otherwise *n* represents the val... | Output a single real value giving the expected value of the Alice's counter if you were given Bob's counter, or Bob's counter if you were given Alice's counter.
You answer will be considered correct if its absolute or relative error doesn't exceed 10<=-<=9. | [
"Alice\n3 1\n",
"Bob\n2 30\n",
"Alice\n2572 10\n"
] | [
"3.500000000\n",
"2\n",
"3439.031415943\n"
] | In the first example, Bob's counter has a 62.5% chance of being 3, a 25% chance of being 4, and a 12.5% chance of being 5. | [] | 92 | 0 | 0 | 114,484 | |
203 | Transportation | [
"greedy",
"sortings",
"two pointers"
] | null | null | Valera came to Japan and bought many robots for his research. He's already at the airport, the plane will fly very soon and Valera urgently needs to bring all robots to the luggage compartment.
The robots are self-propelled (they can potentially move on their own), some of them even have compartments to carry other ro... | The first line contains three space-separated integers *n*,<=*d*,<=*S* (1<=β€<=*n*<=β€<=105,<=1<=β€<=*d*,<=*S*<=β€<=109). The first number represents the number of robots, the second one β the distance to the luggage compartment and the third one β the amount of available fuel.
Next *n* lines specify the robots. The *i*-t... | Print two space-separated integers β the maximum number of robots Valera can transport to the luggage compartment and the minimum amount of fuel he will need for that. If Valera won't manage to get any robots to the luggage compartment, print two zeroes. | [
"3 10 10\n0 12 10\n1 6 10\n0 1 1\n",
"2 7 10\n3 12 10\n5 16 8\n",
"4 8 10\n0 12 3\n1 1 0\n0 3 11\n1 6 9\n"
] | [
"2 6\n",
"0 0\n",
"4 9\n"
] | none | [] | 92 | 0 | 0 | 114,655 | |
131 | Yet Another Task with Queens | [
"sortings"
] | null | null | A queen is the strongest chess piece. In modern chess the queen can move any number of squares in any horizontal, vertical or diagonal direction (considering that there're no other pieces on its way). The queen combines the options given to the rook and the bishop.
There are *m* queens on a square *n*<=Γ<=*n* chessboa... | The first line of the input contains a pair of integers *n*,<=*m* (1<=β€<=*n*,<=*m*<=β€<=105), where *n* is the size of the board and *m* is the number of queens on the board. Then *m* following lines contain positions of the queens, one per line. Each line contains a pair of integers *r**i*,<=*c**i* (1<=β€<=*r**i*,<=*c**... | Print the required sequence *t*0,<=*t*1,<=...,<=*t*8, separating the numbers with spaces. | [
"8 4\n4 3\n4 8\n6 5\n1 6\n",
"10 3\n1 1\n1 2\n1 3\n"
] | [
"0 3 0 1 0 0 0 0 0 ",
"0 2 1 0 0 0 0 0 0 "
] | none | [] | 60 | 0 | 0 | 114,694 | |
542 | Quest | [
"dp",
"greedy"
] | null | null | Polycarp is making a quest for his friends. He has already made *n* tasks, for each task the boy evaluated how interesting it is as an integer *q**i*, and the time *t**i* in minutes needed to complete the task.
An interesting feature of his quest is: each participant should get the task that is best suited for him, d... | The first line contains two integers *n* and *T* (1<=β€<=*n*<=β€<=1000, 1<=β€<=*T*<=β€<=100) β the number of tasks made by Polycarp and the maximum time a quest player should fit into.
Next *n* lines contain two integers *t**i*,<=*q**i* (1<=β€<=*t**i*<=β€<=*T*, 1<=β€<=*q**i*<=β€<=1000) each β the time in minutes needed to com... | Print a single integer β the maximum possible total interest value of all the tasks in the quest. | [
"5 5\n1 1\n1 1\n2 2\n3 3\n4 4\n",
"5 5\n4 1\n4 2\n4 3\n4 4\n4 5\n",
"2 2\n1 1\n2 10\n"
] | [
"11\n",
"9\n",
"10\n"
] | In the first sample test all the five tasks can be complemented with four questions and joined into one quest.
In the second sample test it is impossible to use all the five tasks, but you can take two of them, the most interesting ones.
In the third sample test the optimal strategy is to include only the second task... | [
{
"input": "5 5\n1 1\n1 1\n2 2\n3 3\n4 4",
"output": "11"
},
{
"input": "5 5\n4 1\n4 2\n4 3\n4 4\n4 5",
"output": "9"
},
{
"input": "2 2\n1 1\n2 10",
"output": "10"
},
{
"input": "10 1\n1 732\n1 649\n1 821\n1 756\n1 377\n1 216\n1 733\n1 420\n1 857\n1 193",
"output": "857"... | 46 | 0 | 0 | 114,974 | |
0 | none | [
"none"
] | null | null | Vasilisa the Wise from a far away kingdom got a present from her friend Helga the Wise from a farther away kingdom. The present is a surprise box, yet Vasilisa the Wise doesn't know yet what the surprise actually is because she cannot open the box. She hopes that you can help her in that.
The box's lock is constructed... | The first line contains exactly 6 characters without spaces from the set {R, O, Y, G, B, V} β they are the colors of gems with which the box should be decorated. | Print the required number of different ways to decorate the box. | [
"YYYYYY\n",
"BOOOOB\n",
"ROYGBV\n"
] | [
"1\n",
"2\n",
"30\n"
] | none | [
{
"input": "YYYYYY",
"output": "1"
},
{
"input": "BOOOOB",
"output": "2"
},
{
"input": "ROYGBV",
"output": "30"
},
{
"input": "RRRRRR",
"output": "1"
},
{
"input": "BOOOOO",
"output": "1"
},
{
"input": "GOGGVG",
"output": "2"
},
{
"input": ... | 30 | 0 | 0 | 115,100 | |
0 | none | [
"none"
] | null | null | Recently a tournament in *k* kinds of sports has begun in Berland. Vasya wants to make money on the bets.
The scheme of the tournament is very mysterious and not fully disclosed. Competitions are held back to back, each of them involves two sportsmen who have not left the tournament yet. Each match can be held in any ... | The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=5Β·104, 1<=β€<=*k*<=β€<=10) β the number of tournaments and the number of kinds of sport, respectively.
Each of the next *n* lines contains *k* integers *s**i*1,<=*s**i*2,<=...,<=*s**ik* (1<=β€<=*s**ij*<=β€<=109), where *s**ij* is the power of the *i*-th sport... | For each of the *n* tournaments output the number of contenders who can win. | [
"3 2\n1 5\n5 1\n10 10\n",
"3 2\n2 2\n3 3\n1 10\n",
"3 2\n2 3\n1 1\n3 2\n"
] | [
"1\n2\n1\n",
"1\n1\n3\n",
"1\n1\n2\n"
] | In the first sample:
In the first tournament there is only one sportsman, and he is the winner.
In the second tournament, there are two sportsmen, and everyone can defeat another, depending on kind of sports.
In the third tournament, the third sportsman in the strongest in both kinds of sports, so he is the winner r... | [] | 46 | 0 | 0 | 115,177 | |
193 | Xor | [
"brute force"
] | null | null | John Doe has four arrays: *a*, *b*, *k*, and *p*. Each array consists of *n* integers. Elements of all arrays are indexed starting from 1. Array *p* is a permutation of integers 1 to *n*.
John invented a game for his friends and himself. Initially a player is given array *a*. The player must consecutively execute exac... | The first line contains space-separated integers *n*, *u* and *r* (1<=β€<=*n*,<=*u*<=β€<=30, 0<=β€<=*r*<=β€<=100) β the number of elements in each array, the number of operations and the number that describes one of the operations.
Each of the next four lines contains *n* space-separated integers β arrays *a*, *b*, *k*, ... | On a single line print number *s* β the maximum number of points that a player can win in John's game.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"3 2 1\n7 7 7\n8 8 8\n1 2 3\n1 3 2\n",
"2 1 0\n1 1\n1 1\n1 -1\n1 2\n"
] | [
"96\n",
"0\n"
] | In the first sample you should first apply the operation of the first type, then the operation of the second type. | [] | 46 | 4,505,600 | 0 | 115,229 | |
404 | Maze 1D | [
"binary search",
"greedy",
"implementation"
] | null | null | Valera has a strip infinite in both directions and consisting of cells. The cells are numbered by integers. The cell number 0 has a robot.
The robot has instructions β the sequence of moves that he must perform. In one move, the robot moves one cell to the left or one cell to the right, according to instructions. Befo... | The first line contains a sequence of characters without spaces *s*1*s*2... *s**n* (1<=β€<=*n*<=β€<=106), consisting only of letters "L" and "R". If character *s**i* equals "L", then the robot on the *i*-th move must try to move one cell to the left. If the *s**i*-th character equals "R", then the robot on the *i*-th mov... | Print a single integer β the required number of ways. It's guaranteed that this number fits into 64-bit signed integer type. | [
"RR\n",
"RRL\n"
] | [
"1\n",
"1\n"
] | In the first sample Valera mustn't add any obstacles and his finishing cell must be cell 2.
In the second sample, Valera must add an obstacle in cell number 1, and his finishing cell must be cell number β-β1. In this case robot skips the first two moves and on the third move he goes straight from the starting cell to ... | [
{
"input": "RR",
"output": "1"
},
{
"input": "RRL",
"output": "1"
},
{
"input": "LLLLLRRRRR",
"output": "5"
},
{
"input": "LLRLLLLRLRRRLRRLLRLL",
"output": "0"
},
{
"input": "LRRLRLLLRRLRRRLLLLLRLLLLLRLLRRRLRRLRRLRLRLRLRLLLRRLRLLLLRLRRLLRLRRRRRLRLLLLRRLLRLLLRLLRRL... | 46 | 0 | 0 | 115,410 | |
954 | Water Taps | [
"binary search",
"greedy",
"sortings"
] | null | null | Consider a system of *n* water taps all pouring water into the same container. The *i*-th water tap can be set to deliver any amount of water from 0 to *a**i* ml per second (this amount may be a real number). The water delivered by *i*-th tap has temperature *t**i*.
If for every you set *i*-th tap to deliver exactly ... | The first line contains two integers *n* and *T* (1<=β€<=*n*<=β€<=200000, 1<=β€<=*T*<=β€<=106) β the number of water taps and the desired temperature of water, respectively.
The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=106) where *a**i* is the maximum amount of water *i*-th tap can deliv... | Print the maximum possible amount of water with temperature exactly *T* you can get per second (if it is impossible to obtain water with such temperature, then the answer is considered to be 0).
Your answer is considered correct if its absolute or relative error doesn't exceed 10<=-<=6. | [
"2 100\n3 10\n50 150\n",
"3 9\n5 5 30\n6 6 10\n",
"2 12\n1 3\n10 15\n"
] | [
"6.000000000000000\n",
"40.000000000000000\n",
"1.666666666666667\n"
] | none | [
{
"input": "2 100\n3 10\n50 150",
"output": "6.000000000000000"
},
{
"input": "3 9\n5 5 30\n6 6 10",
"output": "40.000000000000000"
},
{
"input": "2 12\n1 3\n10 15",
"output": "1.666666666666667"
},
{
"input": "20 30\n70 97 14 31 83 22 83 56 19 87 59 7 7 89 24 82 34 40 6 24\n... | 155 | 39,526,400 | 3 | 115,424 | |
852 | Property | [
"greedy",
"sortings"
] | null | null | Bill is a famous mathematician in BubbleLand. Thanks to his revolutionary math discoveries he was able to make enough money to build a beautiful house. Unfortunately, for not paying property tax on time, court decided to punish Bill by making him lose a part of his property.
Billβs property can be observed as a convex... | The first line contains one integer number *n* (2<=β€<=*n*<=β€<=50000), representing number of edges of 2*n*-sided polygon.
The second line contains *n* distinct integer numbers *B*2*k* (0<=β€<=*B*2*k*<=β€<=*n*<=-<=1,<= *k*<==<=0... *n*<=-<=1) separated by a single space, representing points the court chose. If *B*2*k*<==... | Output contains *n* distinct integers separated by a single space representing points *B*1,<=*B*3,<=...,<=*B*2*n*<=-<=1 Bill should choose in order to maximize the property area. If there are multiple solutions that maximize the area, return any of them. | [
"3\n0 1 2\n"
] | [
"0 2 1\n"
] | To maximize area Bill should choose points: *B*<sub class="lower-index">1</sub>β=β*P*<sub class="lower-index">0</sub>, *B*<sub class="lower-index">3</sub>β=β*P*<sub class="lower-index">2</sub>, *B*<sub class="lower-index">5</sub>β=β*P*<sub class="lower-index">1</sub>
<img class="tex-graphics" src="https://espresso.cod... | [
{
"input": "3\n0 1 2",
"output": "0 2 1"
},
{
"input": "10\n0 1 2 3 4 5 6 7 8 9",
"output": "0 1 2 3 5 6 7 8 9 4"
},
{
"input": "10\n1 7 3 6 8 2 4 5 0 9",
"output": "2 6 5 9 7 1 4 0 3 8"
},
{
"input": "10\n4 9 7 2 3 5 6 1 8 0",
"output": "8 9 6 1 4 7 2 5 3 0"
},
{
... | 93 | 0 | 0 | 116,057 | |
135 | Weak Subsequence | [
"combinatorics"
] | null | null | Little Petya very much likes strings. Recently he has received a voucher to purchase a string as a gift from his mother. The string can be bought in the local shop. One can consider that the shop has all sorts of strings over the alphabet of fixed size. The size of the alphabet is equal to *k*. However, the voucher has... | The first line contains two integers *k* (1<=β€<=*k*<=β€<=106) and *w* (2<=β€<=*w*<=β€<=109) β the alphabet size and the required length of the maximum substring that also is the weak subsequence, correspondingly. | Print a single number β the number of strings Petya can buy using the voucher, modulo 1000000007 (109<=+<=7). If there are infinitely many such strings, print "-1" (without the quotes). | [
"2 2\n",
"3 5\n",
"2 139\n"
] | [
"10\n",
"1593\n",
"717248223\n"
] | In the first sample Petya can buy the following strings: aaa, aab, abab, abb, abba, baa, baab, baba, bba, bbb. | [
{
"input": "2 2",
"output": "10"
},
{
"input": "3 5",
"output": "1593"
},
{
"input": "2 139",
"output": "717248223"
},
{
"input": "5 6",
"output": "983725"
},
{
"input": "1000 1002",
"output": "9396758"
},
{
"input": "131 132",
"output": "757914194... | 46 | 0 | 0 | 116,244 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.