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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
487 | Fight the Monster | [
"binary search",
"brute force",
"implementation"
] | null | null | A monster is attacking the Cyberland!
Master Yang, a braver, is going to beat the monster. Yang and the monster each have 3 attributes: hitpoints (*HP*), offensive power (*ATK*) and defensive power (*DEF*).
During the battle, every second the monster's HP decrease by *max*(0,<=*ATK**Y*<=-<=*DEF**M*), while Yang's HP ... | The first line contains three integers *HP**Y*,<=*ATK**Y*,<=*DEF**Y*, separated by a space, denoting the initial *HP*, *ATK* and *DEF* of Master Yang.
The second line contains three integers *HP**M*,<=*ATK**M*,<=*DEF**M*, separated by a space, denoting the *HP*, *ATK* and *DEF* of the monster.
The third line contains... | The only output line should contain an integer, denoting the minimum bitcoins Master Yang should spend in order to win. | [
"1 2 1\n1 100 1\n1 100 100\n",
"100 100 100\n1 1 1\n1 1 1\n"
] | [
"99\n",
"0\n"
] | For the first sample, prices for *ATK* and *DEF* are extremely high. Master Yang can buy 99 HP, then he can beat the monster with 1 HP left.
For the second sample, Master Yang is strong enough to beat the monster, so he doesn't need to buy anything. | [
{
"input": "1 2 1\n1 100 1\n1 100 100",
"output": "99"
},
{
"input": "100 100 100\n1 1 1\n1 1 1",
"output": "0"
},
{
"input": "50 80 92\n41 51 56\n75 93 12",
"output": "0"
},
{
"input": "76 63 14\n89 87 35\n20 15 56",
"output": "915"
},
{
"input": "12 59 66\n43 15... | 62 | 204,800 | 0 | 44,888 | |
0 | none | [
"none"
] | null | null | Petya got interested in grammar on his third year in school. He invented his own language called Petya's. Petya wanted to create a maximally simple language that would be enough to chat with friends, that's why all the language's grammar can be described with the following set of rules:
- There are three parts of spee... | The first line contains one or more words consisting of lowercase Latin letters. The overall number of characters (including letters and spaces) does not exceed 105.
It is guaranteed that any two consecutive words are separated by exactly one space and the input data do not contain any other spaces. It is possible tha... | If some word of the given text does not belong to the Petya's language or if the text contains more that one sentence, print "NO" (without the quotes). Otherwise, print "YES" (without the quotes). | [
"petr\n",
"etis atis animatis etis atis amatis\n",
"nataliala kataliala vetra feinites\n"
] | [
"YES\n",
"NO\n",
"YES\n"
] | none | [
{
"input": "petr",
"output": "YES"
},
{
"input": "etis atis animatis etis atis amatis",
"output": "NO"
},
{
"input": "nataliala kataliala vetra feinites",
"output": "YES"
},
{
"input": "qweasbvflios",
"output": "YES"
},
{
"input": "lios lios petr initis qwe",
... | 124 | 6,963,200 | 0 | 45,004 | |
0 | none | [
"none"
] | null | null | Disclaimer: there are lots of untranslateable puns in the Russian version of the statement, so there is one more reason for you to learn Russian :)
Rick and Morty like to go to the ridge High Cry for crying loudlyΒ β there is an extraordinary echo. Recently they discovered an interesting acoustic characteristic of this... | The first line contains integer *n* (1<=β€<=*n*<=β€<=200<=000), the number of mountains in the ridge.
Second line contains *n* integers *a**i* (0<=β€<=*a**i*<=β€<=109), the heights of mountains in order they are located in the ridge. | Print the only integer, the number of ways to choose two different mountains. | [
"5\n3 2 1 6 5\n",
"4\n3 3 3 3\n"
] | [
"8\n",
"0\n"
] | In the first test case all the ways are pairs of mountains with the numbers (numbering from one):
In the second test case there are no such pairs because for any pair of mountains the height of cry from them is 3, and this height is equal to the height of any mountain. | [
{
"input": "5\n3 2 1 6 5",
"output": "8"
},
{
"input": "4\n3 3 3 3",
"output": "0"
},
{
"input": "1\n0",
"output": "0"
},
{
"input": "1\n1",
"output": "0"
},
{
"input": "1\n1000000000",
"output": "0"
},
{
"input": "1\n6",
"output": "0"
},
{
... | 30 | 0 | 0 | 45,043 | |
898 | Squares and not squares | [
"constructive algorithms",
"greedy"
] | null | null | Ann and Borya have *n* piles with candies and *n* is even number. There are *a**i* candies in pile with number *i*.
Ann likes numbers which are square of some integer and Borya doesn't like numbers which are square of any integer. During one move guys can select some pile with candies and add one candy to it (this can... | First line contains one even integer *n* (2<=β€<=*n*<=β€<=200<=000)Β β number of piles with candies.
Second line contains sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=109)Β β amounts of candies in each pile. | Output minimal number of steps required to make exactly *n*<=/<=2 piles contain number of candies that is a square of some integer and exactly *n*<=/<=2 piles contain number of candies that is not a square of any integer. If condition is already satisfied output 0. | [
"4\n12 14 30 4\n",
"6\n0 0 0 0 0 0\n",
"6\n120 110 23 34 25 45\n",
"10\n121 56 78 81 45 100 1 0 54 78\n"
] | [
"2\n",
"6\n",
"3\n",
"0\n"
] | In first example you can satisfy condition in two moves. During each move you should add one candy to second pile. After it size of second pile becomes 16. After that Borya and Ann will have two piles with number of candies which is a square of integer (second and fourth pile) and two piles with number of candies which... | [
{
"input": "4\n12 14 30 4",
"output": "2"
},
{
"input": "6\n0 0 0 0 0 0",
"output": "6"
},
{
"input": "6\n120 110 23 34 25 45",
"output": "3"
},
{
"input": "10\n121 56 78 81 45 100 1 0 54 78",
"output": "0"
},
{
"input": "10\n0 675178538 310440616 608075179 0 0 0 ... | 2,000 | 25,088,000 | 0 | 45,254 | |
961 | k-substrings | [
"binary search",
"hashing",
"string suffix structures"
] | null | null | You are given a string *s* consisting of *n* lowercase Latin letters.
Let's denote *k*-substring of *s* as a string *subs**k*<==<=*s**k**s**k*<=+<=1..*s**n*<=+<=1<=-<=*k*. Obviously, *subs*1<==<=*s*, and there are exactly such substrings.
Let's call some string *t* an odd proper suprefix of a string *T* iff the foll... | The first line contains one integer *n* (2<=β€<=*n*<=β€<=106) β the length *s*.
The second line contains the string *s* consisting of *n* lowercase Latin letters. | Print integers. *i*-th of them should be equal to maximum length of an odd proper suprefix of *i*-substring of *s* (or <=-<=1, if there is no such string that is an odd proper suprefix of *i*-substring). | [
"15\nbcabcabcabcabca\n",
"24\nabaaabaaaabaaabaaaabaaab\n",
"19\ncabcabbcabcabbcabca\n"
] | [
"9 7 5 3 1 -1 -1 -1\n",
"15 13 11 9 7 5 3 1 1 -1 -1 1\n",
"5 3 1 -1 -1 1 1 -1 -1 -1\n"
] | The answer for first sample test is folowing:
- 1-substring: bcabcabcabcabca - 2-substring: cabcabcabcabc - 3-substring: abcabcabcab - 4-substring: bcabcabca - 5-substring: cabcabc - 6-substring: abcab - 7-substring: bca - 8-substring: c | [
{
"input": "15\nbcabcabcabcabca",
"output": "9 7 5 3 1 -1 -1 -1"
},
{
"input": "24\nabaaabaaaabaaabaaaabaaab",
"output": "15 13 11 9 7 5 3 1 1 -1 -1 1"
},
{
"input": "19\ncabcabbcabcabbcabca",
"output": "5 3 1 -1 -1 1 1 -1 -1 -1"
},
{
"input": "2\nza",
"output": "-1"
},... | 31 | 0 | 0 | 45,445 | |
436 | Cardboard Box | [
"data structures",
"greedy"
] | null | null | Everyone who has played Cut the Rope knows full well how the gameplay is organized. All levels in the game are divided into boxes. Initially only one box with some levels is available. Player should complete levels to earn stars, collecting stars opens new box with levels.
Imagine that you are playing Cut the Rope for... | The first line contains two integers *n* and *w* (1<=β€<=*n*<=β€<=3Β·105;Β 1<=β€<=*w*<=β€<=2*n*) β the number of levels in the first box and the number of stars you need to open another box. Each of the following *n* lines contains two integers *a**i* and *b**i* (1<=β€<=*a**i*<=<<=*b**i*<=β€<=109) β the attributes of the *i... | In the first line print integer *t* β the minimum time you need to open the next box.
In the next line, print *n* digits without spaces β the description of the optimal scenario:
- if you need to pass the *i*-th level for one star, the *i*-th digit should equal 1; - if you need to pass the *i*-th level for two st... | [
"2 3\n1 2\n1 2\n",
"5 3\n10 20\n5 10\n10 20\n6 9\n25 30\n"
] | [
"3\n12\n",
"14\n01020\n"
] | In the first test sample, answer 21 is also assumed correct. | [] | 5,000 | 96,870,400 | 0 | 45,817 | |
0 | none | [
"none"
] | null | null | You're trying to set the record on your favorite video game. The game consists of *N* levels, which must be completed sequentially in order to beat the game. You usually complete each level as fast as possible, but sometimes finish a level slower. Specifically, you will complete the *i*-th level in either *F**i* second... | The first line of input contains integers *N* and *R* , the number of levels and number of seconds you want to complete the game in, respectively. *N* lines follow. The *i*th such line contains integers *F**i*,<=*S**i*,<=*P**i* (1<=β€<=*F**i*<=<<=*S**i*<=β€<=100,<=80<=β€<=*P**i*<=β€<=99), the fast time for level *i*, th... | Print the total expected time. Your answer must be correct within an absolute or relative error of 10<=-<=9.
Formally, let your answer be *a*, and the jury's answer be *b*. Your answer will be considered correct, if . | [
"1 8\n2 8 81\n",
"2 30\n20 30 80\n3 9 85\n",
"4 319\n63 79 89\n79 97 91\n75 87 88\n75 90 83\n"
] | [
"3.14\n",
"31.4\n",
"314.159265358\n"
] | In the first example, you never need to reset. There's an 81% chance of completing the level in 2 seconds and a 19% chance of needing 8 seconds, both of which are within the goal time. The expected time is 0.81Β·2β+β0.19Β·8β=β3.14.
In the second example, you should reset after the first level if you complete it slowly. ... | [] | 30 | 0 | 0 | 45,871 | |
332 | Binary Key | [
"dp",
"greedy",
"implementation"
] | null | null | Let's assume that *p* and *q* are strings of positive length, called the container and the key correspondingly, string *q* only consists of characters 0 and 1. Let's take a look at a simple algorithm that extracts message *s* from the given container *p*:
In the given pseudocode *i*, *j* are integer variables, *s* is ... | The first two lines of the input are non-empty strings *p* and *s* (1<=β€<=|*p*|<=β€<=106, 1<=β€<=|*s*|<=β€<=200), describing the container and the message, correspondingly. The strings can contain any characters with the ASCII codes from 32 to 126, inclusive.
The third line contains a single integer *k* (1<=β€<=*k*<=β€<=20... | Print the required key (string of length *k*, consisting only of characters 0 and 1). If the key doesn't exist, print the single character 0. | [
"abacaba\naba\n6\n",
"abacaba\naba\n3\n"
] | [
"100001\n",
"0\n"
] | String *x*β=β*x*<sub class="lower-index">1</sub>*x*<sub class="lower-index">2</sub>... *x*<sub class="lower-index">*p*</sub> is lexicographically smaller than string *y*β=β*y*<sub class="lower-index">1</sub>*y*<sub class="lower-index">2</sub>... *y*<sub class="lower-index">*q*</sub>, if either *p*β<β*q* and *x*<sub ... | [] | 93 | 0 | 0 | 45,918 | |
0 | none | [
"none"
] | null | null | You already know that Valery's favorite sport is biathlon. Due to your help, he learned to shoot without missing, and his skills are unmatched at the shooting range. But now a smaller task is to be performed, he should learn to complete the path fastest.
The track's map is represented by a rectangle *n*<=Γ<=*m* in siz... | The first input line contains three integers *n*, *m* and *k* (1<=β€<=*n*,<=*m*<=β€<=50,<=*n*Β·*m*<=β₯<=2,<=1<=β€<=*k*<=β€<=4). Then *n* lines contain the map. Each line has the length of exactly *m* characters and consists of lowercase Latin letters and characters *S* and *T*. It is guaranteed that the map contains exactly ... | If there is a path that satisfies the condition, print it as a sequence of letters β the plot types. Otherwise, print "-1" (without quotes). You shouldn't print the character *S* in the beginning and *T* in the end.
Note that this sequence may be empty. This case is present in pretests. You can just print nothing or p... | [
"5 3 2\nSba\nccc\naac\nccc\nabT\n",
"3 4 1\nSxyy\nyxxx\nyyyT\n",
"1 3 3\nTyS\n",
"1 4 1\nSxyT\n"
] | [
"bcccc\n",
"xxxx\n",
"y\n",
"-1\n"
] | none | [
{
"input": "5 3 2\nSba\nccc\naac\nccc\nabT",
"output": "bcccc"
},
{
"input": "3 4 1\nSxyy\nyxxx\nyyyT",
"output": "xxxx"
},
{
"input": "1 3 3\nTyS",
"output": "y"
},
{
"input": "1 4 1\nSxyT",
"output": "-1"
},
{
"input": "1 3 3\nSaT",
"output": "a"
},
{
... | 92 | 0 | 0 | 45,921 | |
144 | Missile Silos | [
"data structures",
"dfs and similar",
"graphs",
"shortest paths"
] | null | null | A country called Berland consists of *n* cities, numbered with integer numbers from 1 to *n*. Some of them are connected by bidirectional roads. Each road has some length. There is a path from each city to any other one by these roads. According to some Super Duper Documents, Berland is protected by the Super Duper Mis... | The first line contains three integers *n*, *m* and *s* (2<=β€<=*n*<=β€<=105, , 1<=β€<=*s*<=β€<=*n*) β the number of cities, the number of roads in the country and the number of the capital, correspondingly. Capital is the city no. *s*.
Then *m* lines contain the descriptions of roads. Each of them is described by three ... | Print the single number β the number of Super Duper Secret Missile Silos that are located in Berland. | [
"4 6 1\n1 2 1\n1 3 3\n2 3 1\n2 4 1\n3 4 1\n1 4 2\n2\n",
"5 6 3\n3 1 1\n3 2 1\n3 4 1\n3 5 1\n1 2 6\n4 5 8\n4\n"
] | [
"3\n",
"3\n"
] | In the first sample the silos are located in cities 3 and 4 and on road (1,β3) at a distance 2 from city 1 (correspondingly, at a distance 1 from city 3).
In the second sample one missile silo is located right in the middle of the road (1,β2). Two more silos are on the road (4,β5) at a distance 3 from city 4 in the di... | [] | 92 | 102,400 | 0 | 45,946 | |
276 | Little Girl and Problem on Trees | [
"data structures",
"graphs",
"trees"
] | null | null | A little girl loves problems on trees very much. Here's one of them.
A tree is an undirected connected graph, not containing cycles. The degree of node *x* in the tree is the number of nodes *y* of the tree, such that each of them is connected with node *x* by some edge of the tree.
Let's consider a tree that consis... | The first line contains integers *n* (2<=β€<=*n*<=β€<=105) and *q* (1<=β€<=*q*<=β€<=105) β the number of tree nodes and the number of requests, correspondingly.
Each of the next *n*<=<=-<=<=1 lines contains two integers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*, *u**i*<=β <=*v**i*), that show that there is an edge b... | For each request to print the node value print an integer β the reply to the request. | [
"3 6\n1 2\n1 3\n0 3 1 2\n0 2 3 1\n0 1 5 2\n1 1\n1 2\n1 3\n",
"6 11\n1 2\n2 5\n5 4\n1 6\n1 3\n0 3 1 3\n0 3 4 5\n0 2 1 4\n0 1 5 5\n0 4 6 2\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n"
] | [
"9\n9\n6\n",
"11\n17\n11\n16\n17\n11\n"
] | none | [
{
"input": "3 6\n1 2\n1 3\n0 3 1 2\n0 2 3 1\n0 1 5 2\n1 1\n1 2\n1 3",
"output": "9\n9\n6"
},
{
"input": "6 11\n1 2\n2 5\n5 4\n1 6\n1 3\n0 3 1 3\n0 3 4 5\n0 2 1 4\n0 1 5 5\n0 4 6 2\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6",
"output": "11\n17\n11\n16\n17\n11"
},
{
"input": "4 8\n2 1\n1 4\n2 3\n0 4 92... | 62 | 0 | 0 | 45,956 | |
0 | none | [
"none"
] | null | null | Π ΠΠ΅ΡΠ»ΡΠ½Π΄ΡΠΊΠΎΠΌ Π³ΠΎΡΡΠ΄Π°ΡΡΡΠ²Π΅Π½Π½ΠΎΠΌ ΡΠ½ΠΈΠ²Π΅ΡΡΠΈΡΠ΅ΡΠ΅ Π»ΠΎΠΊΠ°Π»ΡΠ½Π°Ρ ΡΠ΅ΡΡ ΠΌΠ΅ΠΆΠ΄Ρ ΡΠ΅ΡΠ²Π΅ΡΠ°ΠΌΠΈ Π½Π΅ Π²ΡΠ΅Π³Π΄Π° ΡΠ°Π±ΠΎΡΠ°Π΅Ρ Π±Π΅Π· ΠΎΡΠΈΠ±ΠΎΠΊ. ΠΡΠΈ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΠ΅ Π΄Π²ΡΡ
ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²ΡΡ
ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠΉ ΠΏΠΎΠ΄ΡΡΠ΄ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Π° ΠΎΡΠΈΠ±ΠΊΠ°, Π² ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ΅ ΠΊΠΎΡΠΎΡΠΎΠΉ ΡΡΠΈ Π΄Π²Π° ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ ΡΠ»ΠΈΠ²Π°ΡΡΡΡ Π² ΠΎΠ΄Π½ΠΎ. ΠΡΠΈ ΡΠ°ΠΊΠΎΠΌ ΡΠ»ΠΈΡΠ½ΠΈΠΈ ΠΊΠΎΠ½Π΅Ρ ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ ΡΠΎΠ²ΠΌΠ΅ΡΠ°Π΅ΡΡΡ Ρ Π½Π°ΡΠ°Π»ΠΎΠΌ Π²ΡΠΎΡΠΎΠ³ΠΎ. ΠΠΎΠ½Π΅ΡΠ½ΠΎ, ΡΠΎΠ²ΠΌΠ΅ΡΠ΅Π½ΠΈΠ΅ ΠΌΠΎ... | Π Π΅Π΄ΠΈΠ½ΡΡΠ²Π΅Π½Π½ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π²ΡΡ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
ΡΠ»Π΅Π΄ΡΠ΅Ρ Π½Π΅ΠΏΡΡΡΠ°Ρ ΡΡΡΠΎΠΊΠ° *t*, ΡΠΎΡΡΠΎΡΡΠ°Ρ ΠΈΠ· ΡΡΡΠΎΡΠ½ΡΡ
Π±ΡΠΊΠ² Π»Π°ΡΠΈΠ½ΡΠΊΠΎΠ³ΠΎ Π°Π»ΡΠ°Π²ΠΈΡΠ°. ΠΠ»ΠΈΠ½Π° ΡΡΡΠΎΠΊΠΈ *t* Π½Π΅ ΠΏΡΠ΅Π²ΠΎΡΡ
ΠΎΠ΄ΠΈΡ 100 ΡΠΈΠΌΠ²ΠΎΠ»ΠΎΠ². | ΠΡΠ»ΠΈ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ *t* Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ ΡΠΎΠ΄Π΅ΡΠΆΠ°ΡΡ ΠΎΡΠΈΠ±ΠΊΠΈ, Π²ΡΠ²Π΅Π΄ΠΈΡΠ΅ Β«NOΒ» (Π±Π΅Π· ΠΊΠ°Π²ΡΡΠ΅ΠΊ) Π² Π΅Π΄ΠΈΠ½ΡΡΠ²Π΅Π½Π½ΡΡ ΡΡΡΠΎΠΊΡ Π²ΡΡ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
.
Π ΠΏΡΠΎΡΠΈΠ²Π½ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ Π² ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π²ΡΠ²Π΅Π΄ΠΈΡΠ΅ Β«YESΒ» (Π±Π΅Π· ΠΊΠ°Π²ΡΡΠ΅ΠΊ), Π° Π² ΡΠ»Π΅Π΄ΡΡΡΠ΅ΠΉ ΡΡΡΠΎΠΊΠ΅ Π²ΡΠ²Π΅Π΄ΠΈΡΠ΅ ΡΡΡΠΎΠΊΡ *s*Β β Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΠ΅ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ ΠΌΠΎΠ³Π»ΠΎ ΠΏΡΠΈΠ²Π΅ΡΡΠΈ ΠΊ ΠΎΡΠΈΠ±ΠΊΠ΅. ΠΡΠ»ΠΈ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΡΡ
ΠΎΡΠ²Π΅ΡΠΎΠ² Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ, ΡΠ°Π·ΡΠ΅ΡΠ°Π΅ΡΡ... | [
"abrakadabrabrakadabra\n",
"acacacaca\n",
"abcabc\n",
"abababab\n",
"tatbt\n"
] | [
"YES\nabrakadabra\n",
"YES\nacaca\n",
"NO\n",
"YES\nababab\n",
"NO\n"
] | ΠΠΎ Π²ΡΠΎΡΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ ΠΏΠΎΠ΄Ρ
ΠΎΠ΄ΡΡΠΈΠΌ ΠΎΡΠ²Π΅ΡΠΎΠΌ ΡΠ°ΠΊΠΆΠ΅ ΡΠ²Π»ΡΠ΅ΡΡΡ ΡΡΡΠΎΠΊΠ° acacaca. | [
{
"input": "abrakadabrabrakadabra",
"output": "YES\nabrakadabra"
},
{
"input": "acacacaca",
"output": "YES\nacaca"
},
{
"input": "abcabc",
"output": "NO"
},
{
"input": "abababab",
"output": "YES\nababab"
},
{
"input": "tatbt",
"output": "NO"
},
{
"inpu... | 46 | 4,608,000 | 0 | 46,013 | |
437 | The Child and Zoo | [
"dsu",
"sortings"
] | null | null | Of course our child likes walking in a zoo. The zoo has *n* areas, that are numbered from 1 to *n*. The *i*-th area contains *a**i* animals in it. Also there are *m* roads in the zoo, and each road connects two distinct areas. Naturally the zoo is connected, so you can reach any area of the zoo from any other area usin... | The first line contains two integers *n* and *m* (2<=β€<=*n*<=β€<=105; 0<=β€<=*m*<=β€<=105). The second line contains *n* integers: *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=105). Then follow *m* lines, each line contains two integers *x**i* and *y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=*n*; *x**i*<=β <=*y**i*), denoting the roa... | Output a real number β the value of .
The answer will be considered correct if its relative or absolute error doesn't exceed 10<=-<=4. | [
"4 3\n10 20 30 40\n1 3\n2 3\n4 3\n",
"3 3\n10 20 30\n1 2\n2 3\n3 1\n",
"7 8\n40 20 10 30 20 50 40\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n1 4\n5 7\n"
] | [
"16.666667\n",
"13.333333\n",
"18.571429\n"
] | Consider the first sample. There are 12 possible situations:
- *p*β=β1,β*q*β=β3,β*f*(*p*,β*q*)β=β10. - *p*β=β2,β*q*β=β3,β*f*(*p*,β*q*)β=β20. - *p*β=β4,β*q*β=β3,β*f*(*p*,β*q*)β=β30. - *p*β=β1,β*q*β=β2,β*f*(*p*,β*q*)β=β10. - *p*β=β2,β*q*β=β4,β*f*(*p*,β*q*)β=β20. - *p*β=β4,β*q*β=β1,β*f*(*p*,β*q*)β=β10.
Another 6 c... | [
{
"input": "4 3\n10 20 30 40\n1 3\n2 3\n4 3",
"output": "16.666667"
},
{
"input": "3 3\n10 20 30\n1 2\n2 3\n3 1",
"output": "13.333333"
},
{
"input": "7 8\n40 20 10 30 20 50 40\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n1 4\n5 7",
"output": "18.571429"
},
{
"input": "10 14\n594 965 90 32... | 46 | 0 | 0 | 46,071 | |
768 | Jon and Orbs | [
"dp",
"math",
"probabilities"
] | null | null | Jon Snow is on the lookout for some orbs required to defeat the white walkers. There are *k* different types of orbs and he needs at least one of each. One orb spawns daily at the base of a Weirwood tree north of the wall. The probability of this orb being of any kind is equal. As the north of wall is full of dangers, ... | First line consists of two space separated integers *k*, *q* (1<=β€<=*k*,<=*q*<=β€<=1000) β number of different kinds of orbs and number of queries respectively.
Each of the next *q* lines contain a single integer *p**i* (1<=β€<=*p**i*<=β€<=1000) β *i*-th query. | Output *q* lines. On *i*-th of them output single integer β answer for *i*-th query. | [
"1 1\n1\n",
"2 2\n1\n2\n"
] | [
"1\n",
"2\n2\n"
] | none | [
{
"input": "1 1\n1",
"output": "1"
},
{
"input": "2 2\n1\n2",
"output": "2\n2"
},
{
"input": "3 5\n1\n4\n20\n50\n300",
"output": "3\n3\n3\n3\n3"
},
{
"input": "4 5\n2\n4\n30\n100\n1000",
"output": "4\n4\n4\n4\n7"
},
{
"input": "5 6\n1\n2\n3\n4\n5\n6",
"output"... | 2,000 | 248,627,200 | 0 | 46,093 | |
909 | Colorful Points | [
"data structures",
"greedy",
"implementation"
] | null | null | You are given a set of points on a straight line. Each point has a color assigned to it. For point *a*, its neighbors are the points which don't have any other points between them and *a*. Each point has at most two neighbors - one from the left and one from the right.
You perform a sequence of operations on this set ... | Input contains a single string of lowercase English letters 'a'-'z'. The letters give the points' colors in the order in which they are arranged on the line: the first letter gives the color of the leftmost point, the second gives the color of the second point from the left etc.
The number of the points is between 1 a... | Output one line containing an integer - the number of operations which can be performed on the given set of points until there are no more points to delete. | [
"aabb\n",
"aabcaa\n"
] | [
"2\n",
"1\n"
] | In the first test case, the first operation will delete two middle points and leave points "ab", which will be deleted with the second operation. There will be no points left to apply the third operation to.
In the second test case, the first operation will delete the four points in the middle, leaving points "aa". No... | [
{
"input": "aabb",
"output": "2"
},
{
"input": "aabcaa",
"output": "1"
},
{
"input": "abbcccbba",
"output": "1"
},
{
"input": "aaaaaaaaaaa",
"output": "0"
},
{
"input": "aaaaaaaaabbbbbaaaabaaaaaaaaaaaaaaaaabaaaaaabbbbbbbaaabbbbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaaaaaaa... | 2,000 | 13,414,400 | 0 | 46,143 | |
717 | Festival Organization | [
"combinatorics",
"math",
"number theory"
] | null | null | The Prodiggers are quite a cool band and for this reason, they have been the surprise guest at the ENTER festival for the past 80 years. At the beginning of their careers, they werenβt so successful, so they had to spend time digging channels to earn money; hence the name. Anyway, they like to tour a lot and have surpr... | The first line of the input contains three integers *k*, *l* and *r* (1<=β€<=*k*<=β€<=200, 1<=β€<=*l*<=β€<=*r*<=β€<=1018). | Output a single number: the number of ways to select *k* different tours of the same length, modulo 1<=000<=000<=007. | [
"1 1 2\n"
] | [
"5\n"
] | none | [] | 46 | 5,632,000 | -1 | 46,154 | |
884 | Anti-Palindromize | [
"flows",
"graphs",
"greedy"
] | null | null | A string *a* of length *m* is called antipalindromic iff *m* is even, and for each *i* (1<=β€<=*i*<=β€<=*m*) *a**i*<=β <=*a**m*<=-<=*i*<=+<=1.
Ivan has a string *s* consisting of *n* lowercase Latin letters; *n* is even. He wants to form some string *t* that will be an antipalindromic permutation of *s*. Also Ivan has de... | The first line contains one integer *n* (2<=β€<=*n*<=β€<=100, *n* is even) β the number of characters in *s*.
The second line contains the string *s* itself. It consists of only lowercase Latin letters, and it is guaranteed that its letters can be reordered to form an antipalindromic string.
The third line contains *n*... | Print one number β the maximum possible beauty of *t*. | [
"8\nabacabac\n1 1 1 1 1 1 1 1\n",
"8\nabaccaba\n1 2 3 4 5 6 7 8\n",
"8\nabacabca\n1 2 3 4 4 3 2 1\n"
] | [
"8\n",
"26\n",
"17\n"
] | none | [
{
"input": "8\nabacabac\n1 1 1 1 1 1 1 1",
"output": "8"
},
{
"input": "8\nabaccaba\n1 2 3 4 5 6 7 8",
"output": "26"
},
{
"input": "8\nabacabca\n1 2 3 4 4 3 2 1",
"output": "17"
},
{
"input": "100\nbaaacbccbccaccaccaaabcabcabccacaabcbccbccabbabcbcbbaacacbacacacaacccbcbbbbacc... | 108 | 307,200 | 0 | 46,264 | |
917 | Stranger Trees | [
"dp",
"math",
"matrices",
"trees"
] | null | null | Will shares a psychic connection with the Upside Down Monster, so everything the monster knows, Will knows. Suddenly, he started drawing, page after page, non-stop. Joyce, his mom, and Chief Hopper put the drawings together, and they realized, it's a labeled tree!
A tree is a connected acyclic graph. Will's tree has *... | The first line of input contains a single integer *n* (2<=β€<=*n*<=β€<=100)Β β the size of the tree.
The next *n*<=-<=1 lines contain the edges of Will's tree. Each line contains two integers *v* and *u* (1<=β€<=*v*,<=*u*<=β€<=*n*, *v*<=β <=*u*), endpoints of an edge. It is guaranteed that the given graph is a tree. | Print *n* integers in one line. *i*-th integer should be the number of the number of labeled trees with *n* vertices that share exactly *i*<=-<=1 edges with Will's tree, modulo 1000<=000<=007<==<=109<=+<=7. | [
"3\n1 2\n1 3\n",
"4\n1 2\n2 3\n3 4\n",
"4\n1 2\n1 3\n1 4\n"
] | [
"0 2 1 ",
"1 7 7 1 ",
"0 9 6 1 "
] | none | [] | 62 | 0 | 0 | 46,300 | |
413 | 2048 | [
"bitmasks",
"dp"
] | null | null | The programmers from the R2 company love playing 2048. One day, they decided to invent their own simplified version of this game β 2*k* on a stripe.
Imagine an infinite in one direction stripe, consisting of unit squares (the side of each square is equal to the height of the stripe). Each square can either be empty or... | The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=2000;Β 3<=β€<=*k*<=β€<=11). The next line contains sequence of *n* integers, each of them is either 0, or 2, or 4. | Print a single integer β the number of ways to replace zeroes by numbers 2 or 4 to get a winning sequence. As this number can be rather large, print it modulo 1000000007 (109<=+<=7). | [
"7 4\n2 2 4 2 2 2 2\n",
"1 3\n0\n",
"2 3\n0 4\n",
"5 4\n2 0 0 4 4\n"
] | [
"1\n",
"0\n",
"1\n",
"2\n"
] | Consider the first example. The beginning of the strip will look as follows:
2 βββ 4 βββ 8 βββ 8 2 βββ 8 4 βββ 8 4 2 βββ 16.
To better understand the game, you can see the original game on http://gabrielecirulli.github.io/2048/. Please note that the game that is described on the strip is slightly different from the ... | [] | 31 | 0 | 0 | 46,578 | |
461 | Appleman and a Game | [
"binary search",
"shortest paths",
"strings"
] | null | null | Appleman and Toastman like games. Today they play a game with strings with the following rules. Firstly Toastman tells Appleman two strings *s* and *t* both consisting only of letters 'A', 'B', 'C', 'D'. Then Appleman must build string *s* as quickly as possible. Initially he has empty string, and in one second he can ... | The first line contains an integer *n* (1<=β€<=*n*<=β€<=1018). The second line contains string *t* (1<=β€<=|*t*|<=β€<=105). String *t* consists of only letters 'A', 'B', 'C', 'D'. Each letter appears at least once in string *t*. | Print a single integer β the largest possible time Appleman needs. | [
"5\nABCCAD\n",
"5\nAAABACADBABBBCBDCACBCCCDDDBDCDD\n"
] | [
"5\n",
"4\n"
] | In the first example, Toastman can choose *s* equal to "AAAAA".
In the second example, Toastman can choose *s* equal to "DADDA". | [] | 30 | 0 | 0 | 46,669 | |
1,008 | Turn the Rectangles | [
"greedy",
"sortings"
] | null | null | There are $n$ rectangles in a row. You can either turn each rectangle by $90$ degrees or leave it as it is. If you turn a rectangle, its width will be height, and its height will be width. Notice that you can turn any number of rectangles, you also can turn all or none of them. You can not change the order of the recta... | The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β the number of rectangles.
Each of the next $n$ lines contains two integers $w_i$ and $h_i$ ($1 \leq w_i, h_i \leq 10^9$)Β β the width and the height of the $i$-th rectangle. | Print "YES" (without quotes) if there is a way to make the rectangles go in order of non-ascending height, otherwise print "NO".
You can print each letter in any case (upper or lower). | [
"3\n3 4\n4 6\n3 5\n",
"2\n3 4\n5 5\n"
] | [
"YES\n",
"NO\n"
] | In the first test, you can rotate the second and the third rectangles so that the heights will be [4, 4, 3].
In the second test, there is no way the second rectangle will be not higher than the first one. | [
{
"input": "3\n3 4\n4 6\n3 5",
"output": "YES"
},
{
"input": "2\n3 4\n5 5",
"output": "NO"
},
{
"input": "10\n4 3\n1 1\n6 5\n4 5\n2 4\n9 5\n7 9\n9 2\n4 10\n10 1",
"output": "NO"
},
{
"input": "10\n241724251 76314740\n80658193 177743680\n213953908 406274173\n485639518 85918805... | 249 | 0 | 0 | 46,819 | |
749 | Leaving Auction | [
"binary search",
"data structures"
] | null | null | There are *n* people taking part in auction today. The rules of auction are classical. There were *n* bids made, though it's not guaranteed they were from different people. It might happen that some people made no bids at all.
Each bid is define by two integers (*a**i*,<=*b**i*), where *a**i* is the index of the perso... | The first line of the input contains an integer *n* (1<=β€<=*n*<=β€<=200<=000)Β β the number of participants and bids.
Each of the following *n* lines contains two integers *a**i* and *b**i* (1<=β€<=*a**i*<=β€<=*n*,<=1<=β€<=*b**i*<=β€<=109,<=*b**i*<=<<=*b**i*<=+<=1)Β β the number of participant who made the *i*-th bid and ... | For each question print two integerΒ β the index of the winner and the size of the winning bid. If there is no winner (there are no remaining bids at all), print two zeroes. | [
"6\n1 10\n2 100\n3 1000\n1 10000\n2 100000\n3 1000000\n3\n1 3\n2 2 3\n2 1 2\n",
"3\n1 10\n2 100\n1 1000\n2\n2 1 2\n2 2 3\n"
] | [
"2 100000\n1 10\n3 1000\n",
"0 0\n1 10\n"
] | Consider the first sample:
- In the first question participant number 3 is absent so the sequence of bids looks as follows: <li> 1 10 - 2 100 - 1 10β000 - 2 100β000 Participant number 2 wins with the bid 100β000.</li>- In the second question participants 2 and 3 are absent, so the sequence of bids looks: <li>... | [
{
"input": "6\n1 10\n2 100\n3 1000\n1 10000\n2 100000\n3 1000000\n3\n1 3\n2 2 3\n2 1 2",
"output": "2 100000\n1 10\n3 1000"
},
{
"input": "3\n1 10\n2 100\n1 1000\n2\n2 1 2\n2 2 3",
"output": "0 0\n1 10"
},
{
"input": "1\n1 1\n1\n1 1",
"output": "0 0"
},
{
"input": "2\n1 1\n2 ... | 30 | 0 | 0 | 46,829 | |
321 | Ciel and Gondolas | [
"data structures",
"divide and conquer",
"dp"
] | null | null | Fox Ciel is in the Amusement Park. And now she is in a queue in front of the Ferris wheel. There are *n* people (or foxes more precisely) in the queue: we use first people to refer one at the head of the queue, and *n*-th people to refer the last one in the queue.
There will be *k* gondolas, and the way we allocate go... | The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=4000 and 1<=β€<=*k*<=β€<=*min*(*n*,<=800)) β the number of people in the queue and the number of gondolas. Each of the following *n* lines contains *n* integers β matrix *u*, (0<=β€<=*u**ij*<=β€<=9, *u**ij*<==<=*u**ji* and *u**ii*<==<=0).
Please, use fast inp... | Print an integer β the minimal possible total unfamiliar value. | [
"5 2\n0 0 1 1 1\n0 0 1 1 1\n1 1 0 0 0\n1 1 0 0 0\n1 1 0 0 0\n",
"8 3\n0 1 1 1 1 1 1 1\n1 0 1 1 1 1 1 1\n1 1 0 1 1 1 1 1\n1 1 1 0 1 1 1 1\n1 1 1 1 0 1 1 1\n1 1 1 1 1 0 1 1\n1 1 1 1 1 1 0 1\n1 1 1 1 1 1 1 0\n",
"3 2\n0 2 0\n2 0 3\n0 3 0\n"
] | [
"0\n",
"7\n",
"2\n"
] | In the first example, we can allocate people like this: {1, 2} goes into a gondolas, {3, 4, 5} goes into another gondolas.
In the second example, an optimal solution is : {1, 2, 3} | {4, 5, 6} | {7, 8}. | [
{
"input": "5 2\n0 0 1 1 1\n0 0 1 1 1\n1 1 0 0 0\n1 1 0 0 0\n1 1 0 0 0",
"output": "0"
},
{
"input": "8 3\n0 1 1 1 1 1 1 1\n1 0 1 1 1 1 1 1\n1 1 0 1 1 1 1 1\n1 1 1 0 1 1 1 1\n1 1 1 1 0 1 1 1\n1 1 1 1 1 0 1 1\n1 1 1 1 1 1 0 1\n1 1 1 1 1 1 1 0",
"output": "7"
},
{
"input": "3 2\n0 2 0\n2 0... | 0 | 0 | -1 | 46,848 | |
13 | Letter A | [
"geometry",
"implementation"
] | B. Letter A | 1 | 64 | Little Petya learns how to write. The teacher gave pupils the task to write the letter *A* on the sheet of paper. It is required to check whether Petya really had written the letter *A*.
You are given three segments on the plane. They form the letter *A* if the following conditions hold:
- Two segments have common e... | The first line contains one integer *t* (1<=β€<=*t*<=β€<=10000) β the number of test cases to solve. Each case consists of three lines. Each of these three lines contains four space-separated integers β coordinates of the endpoints of one of the segments. All coordinates do not exceed 108 by absolute value. All segments ... | Output one line for each test case. Print Β«YESΒ» (without quotes), if the segments form the letter *A* and Β«NOΒ» otherwise. | [
"3\n4 4 6 0\n4 1 5 2\n4 0 4 4\n0 0 0 6\n0 6 2 -4\n1 1 0 1\n0 0 0 5\n0 5 2 -1\n1 2 0 1\n"
] | [
"YES\nNO\nYES\n"
] | none | [
{
"input": "3\n4 4 6 0\n4 1 5 2\n4 0 4 4\n0 0 0 6\n0 6 2 -4\n1 1 0 1\n0 0 0 5\n0 5 2 -1\n1 2 0 1",
"output": "YES\nNO\nYES"
}
] | 0 | 0 | -1 | 46,870 |
316 | EKG | [
"dfs and similar",
"dp"
] | null | null | In the rush of modern life, people often forget how beautiful the world is. The time to enjoy those around them is so little that some even stand in queues to several rooms at the same time in the clinic, running from one queue to another.
(Cultural note: standing in huge and disorganized queues for hours is a native ... | The first line contains two integers *n* (1<=β€<=*n*<=β€<=103) and *x* (1<=β€<=*x*<=β€<=*n*) β the number of beavers that stand in the queue and the Smart Beaver's number, correspondingly. All willing to get to the doctor are numbered from 1 to *n*.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*... | Print all possible positions of the Smart Beaver in the line in the increasing order. | [
"6 1\n2 0 4 0 6 0\n",
"6 2\n2 3 0 5 6 0\n",
"4 1\n0 0 0 0\n",
"6 2\n0 0 1 0 4 5\n"
] | [
"2\n4\n6\n",
"2\n5\n",
"1\n2\n3\n4\n",
"1\n3\n4\n6\n"
] | [
{
"input": "6 1\n2 0 4 0 6 0",
"output": "2\n4\n6"
},
{
"input": "6 2\n2 3 0 5 6 0",
"output": "2\n5"
},
{
"input": "4 1\n0 0 0 0",
"output": "1\n2\n3\n4"
},
{
"input": "6 2\n0 0 1 0 4 5",
"output": "1\n3\n4\n6"
},
{
"input": "10 7\n10 8 6 5 0 0 0 4 3 9",
"out... | 500 | 512,000 | 0 | 46,871 | ||
467 | George and Job | [
"dp",
"implementation"
] | null | null | The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, he didn't have enough money, so George was going to work as a programmer. Now he faced the following problem at the work.
Given a sequence of *n* integers *p*1,<=*p*2,<=...,<=*p**n*. You are to choose *k* pairs of integers:... | The first line contains three integers *n*, *m* and *k* (1<=β€<=(*m*<=Γ<=*k*)<=β€<=*n*<=β€<=5000). The second line contains *n* integers *p*1,<=*p*2,<=...,<=*p**n* (0<=β€<=*p**i*<=β€<=109). | Print an integer in a single line β the maximum possible value of sum. | [
"5 2 1\n1 2 3 4 5\n",
"7 1 3\n2 10 7 18 5 33 0\n"
] | [
"9\n",
"61\n"
] | none | [
{
"input": "5 2 1\n1 2 3 4 5",
"output": "9"
},
{
"input": "7 1 3\n2 10 7 18 5 33 0",
"output": "61"
},
{
"input": "13 8 1\n73 7 47 91 54 74 99 11 67 35 84 18 19",
"output": "515"
},
{
"input": "8 3 1\n8 46 37 81 81 57 11 2",
"output": "219"
},
{
"input": "20 5 3\... | 124 | 9,932,800 | -1 | 46,895 | |
750 | New Year and Snowy Grid | [
"dfs and similar",
"dsu",
"graphs",
"interactive"
] | null | null | Pay attention to the output section below, where you will see the information about flushing the output.
Bearland is a grid with *h* rows and *w* columns. Rows are numbered 1 through *h* from top to bottom. Columns are numbered 1 through *w* from left to right. Every cell is either allowed (denoted by '.' in the input... | The first line of the input contains three integers *h*, *w* and *q* (2<=β€<=*h*,<=*w*<=β€<=1000, 1<=β€<=*q*<=β€<=10<=000)Β β the height and the width of the grid, and the number of days, respectively.
Next *h* lines describe which cells are allowed and which permanently blocked. The *i*-th line contains a string of length... | For each of *q* days print "YES" if that day is interesting, and otherwise print "NO", both without the quotes. After printing an answer, you have to both print the end-of-line character and flush the output. Then you can proceed to the next day. You can get Idleness Limit Exceeded if you don't print anything or if you... | [
"3 5 4\n.....\n.....\n.#...\n1\n1 4\n1\n1 5\n2\n2 4\n3 1\n2\n1 5\n3 3\n",
"9 31 5\n...............................\n...............................\n.###.###.#.###...###.###.#.###.\n...#.#.#.#.#.......#.#.#.#...#.\n.###.#.#.#.###...###.#.#.#...#.\n.#...#.#.#.#.#...#...#.#.#...#.\n.###.###.#.###...###.###.#...#.\n... | [
"NO\nYES\nYES\nNO\n",
"NO\nYES\nYES\nYES\nNO\n"
] | In the first sample, there are 4 days. Drawings below show how Limak could go to school and return to his home in the second and the third day (on the left and on the right respectively). A permanently blocked cell is painted red, while cells temporarily blocked by snow are painted orange. Black and green arrows should... | [] | 15 | 0 | 0 | 46,917 | |
732 | Sockets | [
"greedy",
"sortings"
] | null | null | The ICM ACPC World Finals is coming! Unfortunately, the organizers of the competition were so busy preparing tasks that totally missed an important technical point β the organization of electricity supplement for all the participants workstations.
There are *n* computers for participants, the *i*-th of which has power... | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=200<=000)Β β the number of computers and the number of sockets.
The second line contains *n* integers *p*1,<=*p*2,<=...,<=*p**n* (1<=β€<=*p**i*<=β€<=109)Β β the powers of the computers.
The third line contains *m* integers *s*1,<=*s*2,<=...,<=*s**m* (... | In the first line print two numbers *c* and *u*Β β the maximum number of computers which can at the same time be connected to electricity and the minimum number of adapters needed to connect *c* computers.
In the second line print *m* integers *a*1,<=*a*2,<=...,<=*a**m* (0<=β€<=*a**i*<=β€<=109), where *a**i* equals the n... | [
"2 2\n1 1\n2 2\n",
"2 1\n2 100\n99\n"
] | [
"2 2\n1 1\n1 2\n",
"1 6\n6\n1 0\n"
] | none | [
{
"input": "2 2\n1 1\n2 2",
"output": "2 2\n1 1\n1 2"
},
{
"input": "2 1\n2 100\n99",
"output": "1 6\n6\n1 0"
},
{
"input": "1 1\n1\n1",
"output": "1 0\n0\n1"
},
{
"input": "2 1\n2 570129\n753393670",
"output": "1 29\n29\n1 0"
},
{
"input": "1 2\n258\n270411237 42... | 343 | 57,958,400 | 0 | 47,070 | |
216 | Martian Luck | [
"math",
"number theory"
] | null | null | You know that the Martians use a number system with base *k*. Digit *b* (0<=β€<=*b*<=<<=*k*) is considered lucky, as the first contact between the Martians and the Earthlings occurred in year *b* (by Martian chronology).
A digital root *d*(*x*) of number *x* is a number that consists of a single digit, resulting aft... | The first line contains three integers *k*, *b* and *n* (2<=β€<=*k*<=β€<=109, 0<=β€<=*b*<=<<=*k*, 1<=β€<=*n*<=β€<=105).
The second line contains string *s* as a sequence of *n* integers, representing digits in the *k*-base notation: the *i*-th integer equals *a**i* (0<=β€<=*a**i*<=<<=*k*) β the *i*-th digit of string ... | Print a single integer β the number of substrings that are lucky numbers.
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. | [
"10 5 6\n3 2 0 5 6 1\n",
"7 6 4\n3 5 0 4\n",
"257 0 3\n0 0 256\n"
] | [
"5",
"1",
"3"
] | In the first sample the following substrings have the sought digital root: *s*[1... 2] = "3 2", *s*[1... 3] = "3 2 0", *s*[3... 4] = "0 5", *s*[4... 4] = "5" and *s*[2... 6] = "2 0 5 6 1". | [
{
"input": "10 5 6\n3 2 0 5 6 1",
"output": "5"
},
{
"input": "7 6 4\n3 5 0 4",
"output": "1"
},
{
"input": "257 0 3\n0 0 256",
"output": "3"
},
{
"input": "2 1 1\n0",
"output": "0"
},
{
"input": "2 0 20\n1 1 1 0 1 1 1 1 0 0 0 0 1 0 0 0 0 1 0 1",
"output": "22... | 124 | 0 | 0 | 47,122 | |
915 | Physical Education Lessons | [
"data structures",
"implementation",
"sortings"
] | null | null | This year Alex has finished school, and now he is a first-year student of Berland State University. For him it was a total surprise that even though he studies programming, he still has to attend physical education lessons. The end of the term is very soon, but, unfortunately, Alex still hasn't attended a single lesson... | The first line contains one integer *n*, and the second line β one integer *q* (1<=β€<=*n*<=β€<=109, 1<=β€<=*q*<=β€<=3Β·105) β the number of days left before the end of the term, and the number of orders, respectively.
Then *q* lines follow, *i*-th line containing three integers *l**i*, *r**i* and *k**i* representing *i*-t... | Print *q* integers. *i*-th of them must be equal to the number of working days left until the end of the term after the first *i* orders are published. | [
"4\n6\n1 2 1\n3 4 1\n2 3 2\n1 3 2\n2 4 1\n1 4 2\n"
] | [
"2\n0\n2\n3\n1\n4\n"
] | none | [
{
"input": "4\n6\n1 2 1\n3 4 1\n2 3 2\n1 3 2\n2 4 1\n1 4 2",
"output": "2\n0\n2\n3\n1\n4"
},
{
"input": "3\n8\n2 2 1\n3 3 2\n1 1 1\n1 3 2\n2 3 2\n3 3 1\n1 2 1\n2 2 2",
"output": "2\n2\n1\n3\n3\n2\n0\n1"
},
{
"input": "7\n10\n5 7 1\n5 6 2\n7 7 2\n6 7 2\n5 5 1\n3 6 2\n1 3 2\n5 6 1\n1 3 1\n... | 1,000 | 77,721,600 | 0 | 47,143 | |
0 | none | [
"none"
] | null | null | Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical.
Valera absolutel... | The first line of the input data contains an integer *n* (1<=β€<=*n*<=β€<=105). The second line contains an array of original integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=β€<=*a**i*<=β€<=109). | Print on the single line the answer to the problem: the amount of subarrays, which are magical.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator). | [
"4\n2 1 1 4\n",
"5\n-2 -2 -2 0 1\n"
] | [
"5\n",
"8\n"
] | Notes to sample tests:
Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end.
In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3].
In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3]. | [
{
"input": "4\n2 1 1 4",
"output": "5"
},
{
"input": "5\n-2 -2 -2 0 1",
"output": "8"
},
{
"input": "1\n10",
"output": "1"
},
{
"input": "2\n5 6",
"output": "2"
},
{
"input": "5\n5 5 4 5 5",
"output": "7"
},
{
"input": "8\n1 2 0 0 0 0 3 3",
"output... | 154 | 0 | 0 | 47,261 | |
762 | Radio stations | [
"binary search",
"data structures"
] | null | null | In the lattice points of the coordinate line there are *n* radio stations, the *i*-th of which is described by three integers:
- *x**i* β the coordinate of the *i*-th station on the line, - *r**i* β the broadcasting range of the *i*-th station, - *f**i* β the broadcasting frequency of the *i*-th station.
We will s... | The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=105, 0<=β€<=*k*<=β€<=10) β the number of radio stations and the maximum difference in the frequencies for the pair of stations that reach each other to be considered bad.
In the next *n* lines follow the descriptions of radio stations. Each line contains th... | Output the number of bad pairs of radio stations. | [
"3 2\n1 3 10\n3 2 5\n4 10 8\n",
"3 3\n1 3 10\n3 2 5\n4 10 8\n",
"5 1\n1 3 2\n2 2 4\n3 2 1\n4 2 1\n5 3 3\n",
"5 1\n1 5 2\n2 5 4\n3 5 1\n4 5 1\n5 5 3\n"
] | [
"1\n",
"2\n",
"2\n",
"5\n"
] | none | [
{
"input": "3 2\n1 3 10\n3 2 5\n4 10 8",
"output": "1"
},
{
"input": "3 3\n1 3 10\n3 2 5\n4 10 8",
"output": "2"
},
{
"input": "5 1\n1 3 2\n2 2 4\n3 2 1\n4 2 1\n5 3 3",
"output": "2"
},
{
"input": "5 1\n1 5 2\n2 5 4\n3 5 1\n4 5 1\n5 5 3",
"output": "5"
},
{
"input... | 2,000 | 5,529,600 | 0 | 47,429 | |
875 | National Property | [
"2-sat",
"dfs and similar",
"graphs",
"implementation"
] | null | null | You all know that the Library of Bookland is the largest library in the world. There are dozens of thousands of books in the library.
Some long and uninteresting story was removed...
The alphabet of Bookland is so large that its letters are denoted by positive integers. Each letter can be small or large, the large ve... | The first line contains two integers *n* and *m* (2<=β€<=*n*<=β€<=100<=000, 1<=β€<=*m*<=β€<=100<=000)Β β the number of words and the number of letters in Bookland's alphabet, respectively. The letters of Bookland's alphabet are denoted by integers from 1 to *m*.
Each of the next *n* lines contains a description of one word... | In the first line print "Yes" (without quotes), if it is possible to capitalize some set of letters in such a way that the sequence of words becomes lexicographically ordered. Otherwise, print "No" (without quotes).
If the required is possible, in the second line print *k*Β β the number of letters Denis has to capitali... | [
"4 3\n1 2\n1 1\n3 1 3 2\n2 1 1\n",
"6 5\n2 1 2\n2 1 2\n3 1 2 3\n2 1 5\n2 4 4\n2 4 4\n",
"4 3\n4 3 2 2 1\n3 1 1 3\n3 2 3 3\n2 3 1\n"
] | [
"Yes\n2\n2 3 ",
"Yes\n0\n",
"No\n"
] | In the first example after Denis makes letters 2 and 3 large, the sequence looks like the following:
- 2' - 1 - 1 3' 2' - 1 1
The condition 2'β<β1 holds, so the first word is not lexicographically larger than the second word. The second word is the prefix of the third word, so the are in lexicographical order.... | [
{
"input": "4 3\n1 2\n1 1\n3 1 3 2\n2 1 1",
"output": "Yes\n2\n2 3 "
},
{
"input": "6 5\n2 1 2\n2 1 2\n3 1 2 3\n2 1 5\n2 4 4\n2 4 4",
"output": "Yes\n0"
},
{
"input": "4 3\n4 3 2 2 1\n3 1 1 3\n3 2 3 3\n2 3 1",
"output": "No"
},
{
"input": "4 4\n3 3 4 1\n4 3 4 2 2\n4 2 1 2 3\n... | 30 | 0 | 0 | 47,485 | |
228 | Two Tables | [
"brute force",
"implementation"
] | null | null | You've got two rectangular tables with sizes *n**a*<=Γ<=*m**a* and *n**b*<=Γ<=*m**b* cells. The tables consist of zeroes and ones. We will consider the rows and columns of both tables indexed starting from 1. Then we will define the element of the first table, located at the intersection of the *i*-th row and the *j*-t... | The first line contains two space-separated integers *n**a*,<=*m**a* (1<=β€<=*n**a*,<=*m**a*<=β€<=50) β the number of rows and columns in the first table. Then *n**a* lines contain *m**a* characters each β the elements of the first table. Each character is either a "0", or a "1".
The next line contains two space-separat... | Print two space-separated integers *x*,<=*y* (|*x*|,<=|*y*|<=β€<=109) β a shift with maximum overlap factor. If there are multiple solutions, print any of them. | [
"3 2\n01\n10\n00\n2 3\n001\n111\n",
"3 3\n000\n010\n000\n1 1\n1\n"
] | [
"0 1\n",
"-1 -1\n"
] | none | [
{
"input": "3 2\n01\n10\n00\n2 3\n001\n111",
"output": "0 1"
},
{
"input": "3 3\n000\n010\n000\n1 1\n1",
"output": "-1 -1"
},
{
"input": "2 4\n1010\n0011\n5 5\n01100\n01110\n00111\n00110\n00110",
"output": "1 1"
},
{
"input": "3 1\n0\n1\n0\n2 2\n11\n00",
"output": "-1 1"
... | 654 | 6,656,000 | 3 | 47,628 | |
501 | Misha and Forest | [
"constructive algorithms",
"data structures",
"greedy",
"sortings",
"trees"
] | null | null | Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of *n* vertices. For each vertex *v* from 0 to *n*<=-<=1 he wrote down two integers, *degree**v* and *s**v*, were the first integer is the number of vertices adjacent to vertex ... | The first line contains integer *n* (1<=β€<=*n*<=β€<=216), the number of vertices in the graph.
The *i*-th of the next lines contains numbers *degree**i* and *s**i* (0<=β€<=*degree**i*<=β€<=*n*<=-<=1, 0<=β€<=*s**i*<=<<=216), separated by a space. | In the first line print number *m*, the number of edges of the graph.
Next print *m* lines, each containing two distinct numbers, *a* and *b* (0<=β€<=*a*<=β€<=*n*<=-<=1, 0<=β€<=*b*<=β€<=*n*<=-<=1), corresponding to edge (*a*,<=*b*).
Edges can be printed in any order; vertices of the edge can also be printed in any order. | [
"3\n2 3\n1 0\n1 0\n",
"2\n1 1\n1 0\n"
] | [
"2\n1 0\n2 0\n",
"1\n0 1\n"
] | The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as "^", and in Pascal β as "xor". | [
{
"input": "3\n2 3\n1 0\n1 0",
"output": "2\n1 0\n2 0"
},
{
"input": "2\n1 1\n1 0",
"output": "1\n0 1"
},
{
"input": "10\n3 13\n2 6\n1 5\n3 5\n1 3\n2 2\n2 6\n1 6\n1 3\n2 3",
"output": "9\n2 5\n4 3\n7 6\n8 3\n5 0\n6 1\n3 9\n1 0\n9 0"
},
{
"input": "10\n1 2\n1 7\n1 0\n1 8\n0 0\... | 374 | 11,878,400 | 3 | 47,629 | |
580 | Kefa and Dishes | [
"bitmasks",
"dp"
] | null | null | When Kefa came to the restaurant and sat at a table, the waiter immediately brought him the menu. There were *n* dishes. Kefa knows that he needs exactly *m* dishes. But at that, he doesn't want to order the same dish twice to taste as many dishes as possible.
Kefa knows that the *i*-th dish gives him *a**i* units of... | The first line of the input contains three space-separated numbers, *n*, *m* and *k* (1<=β€<=*m*<=β€<=*n*<=β€<=18, 0<=β€<=*k*<=β€<=*n*<=*<=(*n*<=-<=1)) β the number of dishes on the menu, the number of portions Kefa needs to eat to get full and the number of eating rules.
The second line contains *n* space-separated number... | In the single line of the output print the maximum satisfaction that Kefa can get from going to the restaurant. | [
"2 2 1\n1 1\n2 1 1\n",
"4 3 2\n1 2 3 4\n2 1 5\n3 4 2\n"
] | [
"3\n",
"12\n"
] | In the first sample it is best to first eat the second dish, then the first one. Then we get one unit of satisfaction for each dish and plus one more for the rule.
In the second test the fitting sequences of choice are 4 2 1 or 2 1 4. In both cases we get satisfaction 7 for dishes and also, if we fulfill rule 1, we ge... | [
{
"input": "2 2 1\n1 1\n2 1 1",
"output": "3"
},
{
"input": "4 3 2\n1 2 3 4\n2 1 5\n3 4 2",
"output": "12"
},
{
"input": "1 1 0\n1000000000",
"output": "1000000000"
},
{
"input": "4 2 1\n100 1 2 200\n2 3 400",
"output": "403"
},
{
"input": "7 4 3\n2 6 13 5 7 1 9\n... | 2,000 | 232,448,000 | 0 | 47,697 | |
366 | Dima and Trap Graph | [
"binary search",
"data structures",
"dfs and similar",
"dsu",
"shortest paths",
"two pointers"
] | null | null | Dima and Inna love spending time together. The problem is, Seryozha isn't too enthusiastic to leave his room for some reason. But Dima and Inna love each other so much that they decided to get criminal...
Dima constructed a trap graph. He shouted: "Hey Seryozha, have a look at my cool graph!" to get his roommate inter... | The first line of the input contains two integers *n* and *m* (2<=β€<=*n*<=β€<=103,<=0<=β€<=*m*<=β€<=3Β·103). Then follow *m* lines describing the edges. Each line contains four integers *a**k*, *b**k*, *l**k* and *r**k* (1<=β€<=*a**k*,<=*b**k*<=β€<=*n*,<=1<=β€<=*l**k*<=β€<=*r**k*<=β€<=106). The numbers mean that in the trap gra... | In a single line of the output print an integer β the maximum loyalty among all paths from the first node to the *n*-th one. If such paths do not exist or the maximum loyalty equals 0, print in a single line "Nice work, Dima!" without the quotes. | [
"4 4\n1 2 1 10\n2 4 3 5\n1 3 1 5\n2 4 2 7\n",
"5 6\n1 2 1 10\n2 5 11 20\n1 4 2 5\n1 3 10 11\n3 4 12 10000\n4 5 6 6\n"
] | [
"6\n",
"Nice work, Dima!\n"
] | Explanation of the first example.
Overall, we have 2 ways to get from node 1 to node 4: first you must go along the edge 1-2 with range [1-10], then along one of the two edges 2-4.
One of them contains range [3-5], that is, we can pass through with numbers 3, 4, 5. So the loyalty of such path is 3.
If we go along e... | [
{
"input": "4 4\n1 2 1 10\n2 4 3 5\n1 3 1 5\n2 4 2 7",
"output": "6"
},
{
"input": "5 6\n1 2 1 10\n2 5 11 20\n1 4 2 5\n1 3 10 11\n3 4 12 10000\n4 5 6 6",
"output": "Nice work, Dima!"
},
{
"input": "6 6\n1 2 1 10\n2 3 1 10\n3 6 1 1\n1 4 1 4\n4 5 1 3\n5 6 1 3",
"output": "3"
},
{
... | 62 | 1,228,800 | 0 | 47,707 | |
0 | none | [
"none"
] | null | null | Hamed has recently found a string *t* and suddenly became quite fond of it. He spent several days trying to find all occurrences of *t* in other strings he had. Finally he became tired and started thinking about the following problem. Given a string *s* how many ways are there to extract *k*<=β₯<=1 non-overlapping subst... | Input consists of two lines containing strings *s* and *t* (1<=β€<=|*s*|,<=|*t*|<=β€<=105). Each string consists of lowercase Latin letters. | Print the answer in a single line. | [
"ababa\naba\n",
"welcometoroundtwohundredandeightytwo\nd\n",
"ddd\nd\n"
] | [
"5\n",
"274201\n",
"12\n"
] | none | [
{
"input": "ababa\naba",
"output": "5"
},
{
"input": "welcometoroundtwohundredandeightytwo\nd",
"output": "274201"
},
{
"input": "ddd\nd",
"output": "12"
},
{
"input": "vnssnssnssnssnssnssnssnssnssnssnssnssnssnssnssnssn\nnssnssns",
"output": "943392"
},
{
"input":... | 312 | 7,475,200 | 3 | 47,743 | |
935 | Fafa and Ancient Alphabet | [
"math",
"probabilities"
] | null | null | Ancient Egyptians are known to have used a large set of symbols to write on the walls of the temples. Fafa and Fifa went to one of the temples and found two non-empty words *S*1 and *S*2 of equal lengths on the wall of temple written one below the other. Since this temple is very ancient, some symbols from the words w... | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=<=*m*<=β€<=105) β the length of each of the two words and the size of the alphabet , respectively.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=*m*) β the symbols of *S*1. If *a**i*<==<=0, then the symbol at position *i*... | Print the value , where *P* and *Q* are coprime and is the answer to the problem. | [
"1 2\n0\n1\n",
"1 2\n1\n0\n",
"7 26\n0 15 12 9 13 0 14\n11 1 0 13 15 12 0\n"
] | [
"500000004\n",
"0\n",
"230769233\n"
] | In the first sample, the first word can be converted into (1) or (2). The second option is the only one that will make it lexicographically larger than the second word. So, the answer to the problem will be <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/a762254bc6d3a2cc6ae07485c1de945962e7... | [
{
"input": "1 2\n0\n1",
"output": "500000004"
},
{
"input": "1 2\n1\n0",
"output": "0"
},
{
"input": "7 26\n0 15 12 9 13 0 14\n11 1 0 13 15 12 0",
"output": "230769233"
},
{
"input": "6 26\n14 5 19 18 9 14\n0 0 0 0 0 0",
"output": "182369325"
},
{
"input": "4 26\n... | 451 | 9,011,200 | 3 | 47,750 | |
0 | none | [
"none"
] | null | null | Tree is a connected acyclic graph. Suppose you are given a tree consisting of *n* vertices. The vertex of this tree is called centroid if the size of each connected component that appears if this vertex is removed from the tree doesn't exceed .
You are given a tree of size *n* and can perform no more than one edge rep... | The first line of the input contains an integer *n* (2<=β€<=*n*<=β€<=400<=000)Β β the number of vertices in the tree. Each of the next *n*<=-<=1 lines contains a pair of vertex indices *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*)Β β endpoints of the corresponding edge. | Print *n* integers. The *i*-th of them should be equal to 1 if the *i*-th vertex can be made centroid by replacing no more than one edge, and should be equal to 0 otherwise. | [
"3\n1 2\n2 3\n",
"5\n1 2\n1 3\n1 4\n1 5\n"
] | [
"1 1 1 \n",
"1 0 0 0 0 \n"
] | In the first sample each vertex can be made a centroid. For example, in order to turn vertex 1 to centroid one have to replace the edge (2,β3) with the edge (1,β3). | [] | 46 | 0 | 0 | 47,768 | |
0 | none | [
"none"
] | null | null | The game of bingo is played on a 5<=Γ<=5 square grid filled with distinct numbers between 1 and 75. In this problem you will consider a generalized version played on an *n*<=Γ<=*n* grid with distinct numbers between 1 and *m* (*m*<=β₯<=*n*2).
A player begins by selecting a randomly generated bingo grid (generated unif... | Input will consist of three integers *n*, *m*, *k* (1<=β€<=*n*<=β€<=300;Β *n*2<=β€<=*m*<=β€<=100000;Β *n*<=β€<=*k*<=β€<=*m*). | Print the smaller of 1099 and the expected score. Your answer must be correct within an absolute or relative error of 10<=-<=9. | [
"1 2 1\n",
"2 4 3\n",
"7 59164 40872\n"
] | [
"2.5\n",
"4\n",
"3.1415926538\n"
] | none | [] | 46 | 0 | 0 | 47,835 | |
217 | Blackboard Fibonacci | [
"brute force",
"math"
] | null | null | Fibonacci numbers are the sequence of integers: *f*0<==<=0, *f*1<==<=1, *f*2<==<=1, *f*3<==<=2, *f*4<==<=3, *f*5<==<=5, ..., *f**n*<==<=*f**n*<=-<=2<=+<=*f**n*<=-<=1. So every next number is the sum of the previous two.
Bajtek has developed a nice way to compute Fibonacci numbers on a blackboard. First, he writes a 0.... | The first line contains the integers *n* and *r* (1<=β€<=*n*,<=*r*<=β€<=106). | The first line of the output should contain one number β the minimum possible number of mistakes made by Bajtek. The second line should contain *n* characters, starting with "T", describing one possible sequence of operations with that number of mistakes. Each character must be either "T" or "B".
If the required seque... | [
"6 10\n",
"4 5\n",
"2 1\n"
] | [
"2\nTBBTTB\n",
"0\nTBTB\n",
"IMPOSSIBLE\n"
] | none | [
{
"input": "6 10",
"output": "2\nTBBTTB"
},
{
"input": "4 5",
"output": "0\nTBTB"
},
{
"input": "2 1",
"output": "IMPOSSIBLE"
},
{
"input": "15 682",
"output": "IMPOSSIBLE"
},
{
"input": "1145 997855",
"output": "IMPOSSIBLE"
},
{
"input": "16 458",
... | 46 | 0 | 0 | 47,881 | |
515 | Drazil and Park | [
"data structures"
] | null | null | Drazil is a monkey. He lives in a circular park. There are *n* trees around the park. The distance between the *i*-th tree and (*i*<=+<=1)-st trees is *d**i*, the distance between the *n*-th tree and the first tree is *d**n*. The height of the *i*-th tree is *h**i*.
Drazil starts each day with the morning run. The mor... | The first line contains two integer *n* and *m* (3<=β€<=*n*<=β€<=105, 1<=β€<=*m*<=β€<=105), denoting number of trees and number of days, respectively.
The second line contains *n* integers *d*1,<=*d*2,<=...,<=*d**n* (1<=β€<=*d**i*<=β€<=109), the distances between consecutive trees.
The third line contains *n* integers *h*... | For each day print the answer in a separate line. | [
"5 3\n2 2 2 2 2\n3 5 2 1 4\n1 3\n2 2\n4 5\n",
"3 3\n5 1 4\n5 1 4\n3 3\n2 2\n1 1\n"
] | [
"12\n16\n18\n",
"17\n22\n11\n"
] | none | [
{
"input": "5 3\n2 2 2 2 2\n3 5 2 1 4\n1 3\n2 2\n4 5",
"output": "12\n16\n18"
},
{
"input": "3 3\n5 1 4\n5 1 4\n3 3\n2 2\n1 1",
"output": "17\n22\n11"
},
{
"input": "10 10\n8477 33103 38654 6582 27496 1106 15985 3644 29818 8849\n88745 72099 87767 85285 73517 94562 87214 63194 83791 77619... | 46 | 0 | 0 | 47,886 | |
926 | Endless Roses Most Beautiful | [] | null | null | Arkady decided to buy roses for his girlfriend.
A flower shop has white, orange and red roses, and the total amount of them is *n*. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have t... | The first line contains two integers *n* and *k* (1<=β€<=*k*<=β€<=*n*<=β€<=200<=000) β the number of roses in the show and the number of roses Arkady wants to buy.
The second line contains a sequence of integers *b*1,<=*b*2,<=...,<=*b**n* (1<=β€<=*b**i*<=β€<=10<=000), where *b**i* equals the beauty of the *i*-th rose.
The... | Print the maximum possible total beauty of a bouquet of *k* roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. | [
"5 3\n4 3 4 1 6\nRROWW\n",
"5 2\n10 20 14 20 11\nRRRRR\n",
"11 5\n5 6 3 2 3 4 7 5 4 5 6\nRWOORWORROW\n"
] | [
"11\n",
"-1\n",
"28\n"
] | In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11.
In the second example Arkady can not buy a bouquet because all ... | [
{
"input": "5 3\n4 3 4 1 6\nRROWW",
"output": "11"
},
{
"input": "5 2\n10 20 14 20 11\nRRRRR",
"output": "-1"
},
{
"input": "11 5\n5 6 3 2 3 4 7 5 4 5 6\nRWOORWORROW",
"output": "28"
},
{
"input": "15 10\n8560 6244 9607 5137 7187 3217 5527 9919 282 8748 3529 6110 5767 521 339... | 1,000 | 63,283,200 | 0 | 47,959 | |
977 | Cyclic Components | [
"dfs and similar",
"dsu",
"graphs"
] | null | null | You are given an undirected graph consisting of $n$ vertices and $m$ edges. Your task is to find the number of connected components which are cycles.
Here are some definitions of graph theory.
An undirected graph consists of two sets: set of nodes (called vertices) and set of edges. Each edge connects a pair of verti... | The first line contains two integer numbers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $0 \le m \le 2 \cdot 10^5$) β number of vertices and edges.
The following $m$ lines contains edges: edge $i$ is given as a pair of vertices $v_i$, $u_i$ ($1 \le v_i, u_i \le n$, $u_i \ne v_i$). There is no multiple edges in the given ... | Print one integer β the number of connected components which are also cycles. | [
"5 4\n1 2\n3 4\n5 4\n3 5\n",
"17 15\n1 8\n1 12\n5 11\n11 9\n9 15\n15 5\n4 13\n3 13\n4 3\n10 16\n7 10\n16 7\n14 3\n14 4\n17 6\n"
] | [
"1\n",
"2\n"
] | In the first example only component $[3, 4, 5]$ is also a cycle.
The illustration above corresponds to the second example. | [
{
"input": "5 4\n1 2\n3 4\n5 4\n3 5",
"output": "1"
},
{
"input": "17 15\n1 8\n1 12\n5 11\n11 9\n9 15\n15 5\n4 13\n3 13\n4 3\n10 16\n7 10\n16 7\n14 3\n14 4\n17 6",
"output": "2"
},
{
"input": "4 4\n1 2\n2 3\n1 3\n1 4",
"output": "0"
},
{
"input": "5 5\n1 2\n2 3\n3 4\n4 1\n3 5... | 748 | 37,580,800 | -1 | 48,024 | |
959 | Mahmoud and Ehab and yet another xor task | [
"bitmasks",
"dp",
"math",
"matrices"
] | null | null | Ehab has an array *a* of *n* integers. He likes the [bitwise-xor operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) and he likes to bother Mahmoud so he came up with a problem. He gave Mahmoud *q* queries. In each of them, he gave Mahmoud 2 integers *l* and *x*, and asked him to find the number of subseque... | The first line contains integers *n* and *q* (1<=β€<=*n*,<=*q*<=β€<=105), the number of elements in the array and the number of queries.
The next line contains *n* integers *a*1, *a*2, ..., *a**n* (0<=β€<=*a**i*<=<<=220), the elements of the array.
The next *q* lines, each contains integers *l* and *x* (1<=β€<=*l*<=β€<... | For each query, output its answer modulo 109<=+<=7 in a newline. | [
"5 5\n0 1 2 3 4\n4 3\n2 0\n3 7\n5 7\n5 8\n",
"3 2\n1 1 1\n3 1\n2 0\n"
] | [
"4\n2\n0\n4\n0\n",
"4\n2\n"
] | The bitwise-xor sum of the empty set is 0 and the bitwise-xor sum of a set containing one element is that element itself. | [
{
"input": "5 5\n0 1 2 3 4\n4 3\n2 0\n3 7\n5 7\n5 8",
"output": "4\n2\n0\n4\n0"
},
{
"input": "3 2\n1 1 1\n3 1\n2 0",
"output": "4\n2"
},
{
"input": "20 20\n353123 353123 677328 353123 0 997043 677328 420884 968991 566539 420884 801220 1030642 937882 762558 599450 196420 299659 101896 60... | 1,000 | 7,168,000 | 0 | 48,027 | |
85 | Embassy Queue | [
"data structures",
"greedy"
] | B. Embassy Queue | 2 | 256 | In an embassy of a well-known kingdom an electronic queue is organised. Every person who comes to the embassy, needs to make the following three actions: show the ID, pay money to the cashier and be fingerprinted. Besides, the actions should be performed in the given order.
For each action several separate windows are... | The first line contains three space-separated integers *k*1, *k*2, *k*3 (1<=β€<=*k**i*<=β€<=109), they are the number of windows of the first, second and third type correspondingly.
The second line contains three space-separated integers *t*1, *t*2, *t*3 (1<=β€<=*t**i*<=β€<=105), they are the periods of time needed to ser... | Print the single number, the maximum time a person will spend in the embassy if the queue is organized optimally.
Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams (also you may use the %I64d specificator). | [
"1 1 1\n1 1 1\n5\n1 1 1 1 1\n",
"2 1 1\n5 1 1\n5\n1 2 3 3 5\n"
] | [
"7\n",
"13\n"
] | In the first test 5 people come simultaneously at the moment of time equal to 1. There is one window of every type, it takes 1 unit of time to be served at each window. That's why the maximal time a person spends in the embassy is the time needed to be served at the windows (3 units of time) plus the time the last pers... | [
{
"input": "1 1 1\n1 1 1\n5\n1 1 1 1 1",
"output": "7"
},
{
"input": "2 1 1\n5 1 1\n5\n1 2 3 3 5",
"output": "13"
},
{
"input": "1 1 1\n1 1 1\n5\n1 2 3 4 5",
"output": "3"
},
{
"input": "1 1 1\n1 1 1\n5\n1 2 3 3 4",
"output": "4"
},
{
"input": "9 5 6\n8 2 2\n1\n5"... | 496 | 16,793,600 | 3.844719 | 48,037 |
780 | Underground Lab | [
"constructive algorithms",
"dfs and similar",
"graphs"
] | null | null | The evil Bumbershoot corporation produces clones for gruesome experiments in a vast underground lab. On one occasion, the corp cloned a boy Andryusha who was smarter than his comrades. Immediately Andryusha understood that something fishy was going on there. He rallied fellow clones to go on a feud against the evil cor... | The first line contains three integers *n*, *m*, and *k* (1<=β€<=*n*<=β€<=2Β·105, *n*<=-<=1<=β€<=*m*<=β€<=2Β·105, 1<=β€<=*k*<=β€<=*n*)Β β the number of vertices and edges in the lab, and the number of clones.
Each of the next *m* lines contains two integers *x**i* and *y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=*n*)Β β indices of vertice... | You should print *k* lines. *i*-th of these lines must start with an integer *c**i* ()Β β the number of vertices visited by *i*-th clone, followed by *c**i* integersΒ β indices of vertices visited by this clone in the order of visiting. You have to print each vertex every time it is visited, regardless if it was visited ... | [
"3 2 1\n2 1\n3 1\n",
"5 4 2\n1 2\n1 3\n1 4\n1 5\n"
] | [
"3 2 1 3\n",
"3 2 1 3\n3 4 1 5"
] | In the first sample case there is only one clone who may visit vertices in order (2, 1, 3), which fits the constraint of 6 vertices per clone.
In the second sample case the two clones can visited vertices in order (2, 1, 3) and (4, 1, 5), which fits the constraint of 5 vertices per clone. | [] | 61 | 5,632,000 | -1 | 48,083 | |
993 | Nikita and Order Statistics | [
"chinese remainder theorem",
"fft",
"math"
] | null | null | Nikita likes tasks on order statistics, for example, he can easily find the $k$-th number in increasing order on a segment of an array. But now Nikita wonders how many segments of an array there are such that a given number $x$ is the $k$-th number in increasing order on this segment. In other words, you should find th... | The first line contains two integers $n$ and $x$ $(1 \le n \le 2 \cdot 10^5, -10^9 \le x \le 10^9)$.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ $(-10^9 \le a_i \le 10^9)$Β β the given array. | Print $n+1$ integers, where the $i$-th number is the answer for Nikita's question for $k=i-1$. | [
"5 3\n1 2 3 4 5\n",
"2 6\n-5 9\n",
"6 99\n-1 -1 -1 -1 -1 -1\n"
] | [
"6 5 4 0 0 0 ",
"1 2 0 ",
"0 6 5 4 3 2 1 "
] | none | [
{
"input": "5 3\n1 2 3 4 5",
"output": "6 5 4 0 0 0 "
},
{
"input": "2 6\n-5 9",
"output": "1 2 0 "
},
{
"input": "6 99\n-1 -1 -1 -1 -1 -1",
"output": "0 6 5 4 3 2 1 "
},
{
"input": "5 -2\n-1 -1 -4 -5 1",
"output": "4 5 6 0 0 0 "
},
{
"input": "5 -6\n-4 2 -7 -1 -5... | 2,000 | 185,548,800 | 0 | 48,150 | |
358 | Dima and Containers | [
"constructive algorithms",
"greedy",
"implementation"
] | null | null | Dima has a birthday soon! It's a big day! Saryozha's present to Dima is that Seryozha won't be in the room and won't disturb Dima and Inna as they celebrate the birthday. Inna's present to Dima is a stack, a queue and a deck.
Inna wants her present to show Dima how great a programmer he is. For that, she is going to g... | The first line contains integer *n* (1<=β€<=*n*<=β€<=105) β the number of Inna's commands. Then *n* lines follow, describing Inna's commands. Each line consists an integer:
1. Integer *a* (1<=β€<=*a*<=β€<=105) means that Inna gives Dima a command to add number *a* into one of containers. 1. Integer 0 shows that Inna ask... | Each command of the input must correspond to one line of the output β Dima's action.
For the command of the first type (adding) print one word that corresponds to Dima's choice:
- pushStack β add to the end of the stack; - pushQueue β add to the end of the queue; - pushFront β add to the beginning of the deck; - ... | [
"10\n0\n1\n0\n1\n2\n0\n1\n2\n3\n0\n",
"4\n1\n2\n3\n0\n"
] | [
"0\npushStack\n1 popStack\npushStack\npushQueue\n2 popStack popQueue\npushStack\npushQueue\npushFront\n3 popStack popQueue popFront\n",
"pushStack\npushQueue\npushFront\n3 popStack popQueue popFront\n"
] | none | [
{
"input": "10\n0\n1\n0\n1\n2\n0\n1\n2\n3\n0",
"output": "0\npushStack\n1 popStack\npushStack\npushQueue\n2 popStack popQueue\npushStack\npushQueue\npushFront\n3 popStack popQueue popFront"
},
{
"input": "4\n1\n2\n3\n0",
"output": "pushStack\npushQueue\npushFront\n3 popStack popQueue popFront"
... | 77 | 0 | 0 | 48,269 | |
463 | Caisa and Tree | [
"brute force",
"dfs and similar",
"math",
"number theory",
"trees"
] | null | null | Caisa is now at home and his son has a simple task for him.
Given a rooted tree with *n* vertices, numbered from 1 to *n* (vertex 1 is the root). Each vertex of the tree has a value. You should answer *q* queries. Each query is one of the following:
- Format of the query is "1 *v*". Let's write out the sequence of v... | The first line contains two space-separated integers *n*, *q* (1<=β€<=*n*,<=*q*<=β€<=105).
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=2Β·106), where *a**i* represent the value of node *i*.
Each of the next *n*<=-<=1 lines contains two integers *x**i* and *y**i* (1<=β€<=*x**i*,<=*y... | For each query of the first type output the result of the query. | [
"4 6\n10 8 4 3\n1 2\n2 3\n3 4\n1 1\n1 2\n1 3\n1 4\n2 1 9\n1 4\n"
] | [
"-1\n1\n2\n-1\n1\n"
] | *gcd*(*x*,β*y*) is greatest common divisor of two integers *x* and *y*. | [
{
"input": "4 6\n10 8 4 3\n1 2\n2 3\n3 4\n1 1\n1 2\n1 3\n1 4\n2 1 9\n1 4",
"output": "-1\n1\n2\n-1\n1"
},
{
"input": "4 46\n1826622 227609 1815373 691816\n4 1\n3 1\n1 2\n1 2\n1 3\n2 3 1226461\n1 2\n2 2 329857\n1 4\n1 2\n1 2\n1 3\n1 3\n1 1\n1 4\n1 1\n1 3\n1 3\n1 2\n1 4\n1 4\n1 3\n1 2\n1 2\n2 3 106992... | 233 | 268,390,400 | 0 | 48,370 | |
0 | none | [
"none"
] | null | null | Natasha travels around Mars in the Mars rover. But suddenly it broke down, namelyΒ β the logical scheme inside it. The scheme is an undirected tree (connected acyclic graph) with a root in the vertex $1$, in which every leaf (excluding root) is an input, and all other vertices are logical elements, including the root, w... | The first line contains a single integer $n$ ($2 \le n \le 10^6$)Β β the number of vertices in the graph (both inputs and elements).
The $i$-th of the next $n$ lines contains a description of $i$-th vertex: the first word "AND", "OR", "XOR", "NOT" or "IN" (means the input of the scheme) is the vertex type. If this vert... | Print a string of characters '0' and '1' (without quotes)Β β answers to the problem for each input in the ascending order of their vertex indices. | [
"10\nAND 9 4\nIN 1\nIN 1\nXOR 6 5\nAND 3 7\nIN 0\nNOT 10\nIN 1\nIN 1\nAND 2 8\n"
] | [
"10110"
] | The original scheme from the example (before the input is changed):
<img class="tex-graphics" src="https://espresso.codeforces.com/4507113582d40356e140857daa04318b91197f46.png" style="max-width: 100.0%;max-height: 100.0%;"/>
Green indicates bits '1', yellow indicates bits '0'.
If Natasha changes the input bit $2$ to... | [
{
"input": "10\nAND 9 4\nIN 1\nIN 1\nXOR 6 5\nAND 3 7\nIN 0\nNOT 10\nIN 1\nIN 1\nAND 2 8",
"output": "10110"
},
{
"input": "3\nAND 2 3\nIN 0\nIN 0",
"output": "00"
},
{
"input": "3\nAND 2 3\nIN 1\nIN 0",
"output": "01"
},
{
"input": "3\nAND 2 3\nIN 0\nIN 1",
"output": "10... | 62 | 0 | 0 | 48,390 | |
148 | Terse princess | [
"constructive algorithms",
"greedy"
] | null | null | Β«Next pleaseΒ», β the princess called and cast an estimating glance at the next groom.
The princess intends to choose the most worthy groom, this is, the richest one. Whenever she sees a groom who is more rich than each of the previous ones, she says a measured Β«Oh...Β». Whenever the groom is richer than all previous on... | The only line of input data contains three integer numbers *n*,<=*a* and *b* (1<=β€<=*n*<=β€<=100,<=0<=β€<=*a*,<=*b*<=β€<=15,<=*n*<=><=*a*<=+<=*b*), separated with single spaces. | Output any sequence of integers *t*1,<=*t*2,<=...,<=*t**n*, where *t**i* (1<=β€<=*t**i*<=β€<=50000) is the fortune of *i*-th groom, that satisfies the given constraints. If no sequence exists that would satisfy all the requirements, output a single number -1. | [
"10 2 3\n",
"5 0 0\n"
] | [
"5 1 3 6 16 35 46 4 200 99",
"10 10 6 6 5"
] | Let's have a closer look at the answer for the first sample test.
- The princess said Β«Oh...Β» (highlighted in bold): 5 1 3 6 16 35 46 4 200 99. - The princess exclaimed Β«Wow!Β» (highlighted in bold): 5 1 3 6 16 35 46 4 200 99. | [
{
"input": "10 2 3",
"output": "1 2 4 8 9 10 10 10 10 10 "
},
{
"input": "5 0 0",
"output": "1 1 1 1 1 "
},
{
"input": "5 2 2",
"output": "1 2 4 5 6 "
},
{
"input": "6 2 2",
"output": "1 2 4 5 6 6 "
},
{
"input": "10 9 0",
"output": "-1"
},
{
"input": ... | 62 | 0 | 0 | 48,512 | |
963 | Cutting Rectangle | [
"brute force",
"math",
"number theory"
] | null | null | A rectangle with sides $A$ and $B$ is cut into rectangles with cuts parallel to its sides. For example, if $p$ horizontal and $q$ vertical cuts were made, $(p + 1) \cdot (q + 1)$ rectangles were left after the cutting. After the cutting, rectangles were of $n$ different types. Two rectangles are different if at least o... | The first line consists of a single integer $n$ ($1 \leq n \leq 2 \cdot 10^{5}$)Β β amount of different types of rectangles left after cutting the initial rectangle.
The next $n$ lines each consist of three integers $w_{i}, h_{i}, c_{i}$ $(1 \leq w_{i}, h_{i}, c_{i} \leq 10^{12})$Β β the lengths of the sides of the rect... | Output one integerΒ β the answer to the problem. | [
"1\n1 1 9\n",
"2\n2 3 20\n2 4 40\n",
"2\n1 2 5\n2 3 5\n"
] | [
"3\n",
"6\n",
"0\n"
] | In the first sample there are three suitable pairs: $(1; 9)$, $(3; 3)$ and $(9; 1)$.
In the second sample case there are 6 suitable pairs: $(2; 220)$, $(4; 110)$, $(8; 55)$, $(10; 44)$, $(20; 22)$ and $(40; 11)$.
Here the sample of cut for $(20; 22)$.
The third sample has no suitable pairs. | [
{
"input": "1\n1 1 9",
"output": "3"
},
{
"input": "2\n2 3 20\n2 4 40",
"output": "6"
},
{
"input": "2\n1 2 5\n2 3 5",
"output": "0"
}
] | 93 | 0 | -1 | 48,529 | |
364 | Free Market | [
"dp",
"greedy"
] | null | null | John Doe has recently found a "Free Market" in his city β that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has *n* items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that e... | The first line contains two space-separated integers *n*, *d* (1<=β€<=*n*<=β€<=50, 1<=β€<=*d*<=β€<=104) β the number of items on the market and John's sense of justice value, correspondingly. The second line contains *n* space-separated integers *c**i* (1<=β€<=*c**i*<=β€<=104). | Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. | [
"3 2\n1 3 10\n",
"3 5\n1 2 3\n",
"10 10000\n10000 9999 1 10000 10000 10000 1 2 3 4\n"
] | [
"4 3\n",
"6 2\n",
"50010 6\n"
] | In the first sample John can act like this:
- Take the first item (1β-β0ββ€β2). - Exchange the first item for the second one (3β-β1ββ€β2). - Take the first item (1β-β0ββ€β2). | [] | 31 | 0 | 0 | 48,572 | |
616 | Expensive Strings | [
"string suffix structures",
"strings"
] | null | null | You are given *n* strings *t**i*. Each string has cost *c**i*.
Let's define the function of string , where *p**s*,<=*i* is the number of occurrences of *s* in *t**i*, |*s*| is the length of the string *s*. Find the maximal value of function *f*(*s*) over all strings.
Note that the string *s* is not necessarily some s... | The first line contains the only integer *n* (1<=β€<=*n*<=β€<=105) β the number of strings in *t*.
Each of the next *n* lines contains contains a non-empty string *t**i*. *t**i* contains only lowercase English letters.
It is guaranteed that the sum of lengths of all strings in *t* is not greater than 5Β·105.
The last l... | Print the only integer *a* β the maximal value of the function *f*(*s*) over all strings *s*. Note one more time that the string *s* is not necessarily from *t*. | [
"2\naa\nbb\n2 1\n",
"2\naa\nab\n2 1\n"
] | [
"4\n",
"5\n"
] | none | [
{
"input": "2\naa\nbb\n2 1",
"output": "4"
},
{
"input": "2\naa\nab\n2 1",
"output": "5"
},
{
"input": "1\naaa\n2",
"output": "8"
},
{
"input": "3\na\naa\naaa\n1 2 3",
"output": "16"
},
{
"input": "3\na\naaa\naa\n2 1 3",
"output": "11"
},
{
"input": "1... | 31 | 0 | 0 | 48,729 | |
598 | Chocolate Bar | [
"brute force",
"dp"
] | null | null | You have a rectangular chocolate bar consisting of *n*<=Γ<=*m* single squares. You want to eat exactly *k* squares, so you may need to break the chocolate bar.
In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or ve... | The first line of the input contains a single integer *t* (1<=β€<=*t*<=β€<=40910)Β β the number of values *n*, *m* and *k* to process.
Each of the next *t* lines contains three integers *n*, *m* and *k* (1<=β€<=*n*,<=*m*<=β€<=30,<=1<=β€<=*k*<=β€<=*min*(*n*Β·*m*,<=50))Β β the dimensions of the chocolate bar and the number of sq... | For each *n*, *m* and *k* print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly *k* squares. | [
"4\n2 2 1\n2 2 3\n2 2 2\n2 2 4\n"
] | [
"5\n5\n4\n0\n"
] | In the first query of the sample one needs to perform two breaks:
- to split 2βΓβ2 bar into two pieces of 2βΓβ1 (cost is 2<sup class="upper-index">2</sup>β=β4), - to split the resulting 2βΓβ1 into two 1βΓβ1 pieces (cost is 1<sup class="upper-index">2</sup>β=β1).
In the second query of the sample one wants to eat 3 ... | [
{
"input": "4\n2 2 1\n2 2 3\n2 2 2\n2 2 4",
"output": "5\n5\n4\n0"
}
] | 217 | 9,728,000 | 0 | 48,810 | |
814 | An unavoidable detour for home | [
"combinatorics",
"dp",
"graphs",
"shortest paths"
] | null | null | Those unwilling to return home from a long journey, will be affected by the oddity of the snail and lose their way. Mayoi, the oddity's carrier, wouldn't like this to happen, but there's nothing to do with this before a cure is figured out. For now, she would only like to know the enormous number of possibilities to be... | The first line of input contains a positive integer *n* (3<=β€<=*n*<=β€<=50) β the number of towns.
The second line contains *n* space-separated integers *d*1,<=*d*2,<=...,<=*d**n* (2<=β€<=*d**i*<=β€<=3) β the number of roads connected to towns 1,<=2,<=...,<=*n*, respectively. It is guaranteed that the sum of *d**i* over ... | Output one integer β the total number of different possible ways in which the towns are connected, modulo 109<=+<=7. | [
"4\n3 2 3 2\n",
"5\n2 3 3 2 2\n",
"5\n2 2 2 2 2\n",
"20\n2 2 2 2 3 2 3 2 2 2 2 2 2 2 2 2 2 3 3 2\n"
] | [
"1\n",
"2\n",
"2\n",
"82944\n"
] | In the first example, the following structure is the only one to satisfy the constraints, the distances from towns 2,β3,β4 to the capital are all 1.
In the second example, the following two structures satisfy the constraints. | [] | 3,000 | 307,200 | 0 | 48,860 | |
294 | Shaass the Great | [
"dp",
"trees"
] | null | null | The great Shaass is the new king of the Drakht empire. The empire has *n* cities which are connected by *n*<=-<=1 bidirectional roads. Each road has an specific length and connects a pair of cities. There's a unique simple path connecting each pair of cities.
His majesty the great Shaass has decided to tear down one o... | The first line of the input contains an integer *n* denoting the number of cities in the empire, (2<=β€<=*n*<=β€<=5000). The next *n*<=-<=1 lines each contains three integers *a**i*, *b**i* and *w**i* showing that two cities *a**i* and *b**i* are connected using a road of length *w**i*, (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*,<=*... | On the only line of the output print the minimum pairwise sum of distances between the cities.
Please do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specificator. | [
"3\n1 2 2\n1 3 4\n",
"6\n1 2 1\n2 3 1\n3 4 1\n4 5 1\n5 6 1\n",
"6\n1 3 1\n2 3 1\n3 4 100\n4 5 2\n4 6 1\n"
] | [
"12\n",
"29\n",
"825\n"
] | none | [] | 46 | 0 | 0 | 48,872 | |
263 | Squares | [
"greedy",
"implementation",
"sortings"
] | null | null | Vasya has found a piece of paper with a coordinate system written on it. There are *n* distinct squares drawn in this coordinate system. Let's number the squares with integers from 1 to *n*. It turned out that points with coordinates (0,<=0) and (*a**i*,<=*a**i*) are the opposite corners of the *i*-th square.
Vasya wa... | The first line contains two space-separated integers *n*, *k* (1<=β€<=*n*,<=*k*<=β€<=50). The second line contains space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109).
It is guaranteed that all given squares are distinct. | In a single line print two space-separated integers *x* and *y* (0<=β€<=*x*,<=*y*<=β€<=109) β the coordinates of the point that belongs to exactly *k* squares. If there are multiple answers, you are allowed to print any of them.
If there is no answer, print "-1" (without the quotes). | [
"4 3\n5 1 3 4\n",
"3 1\n2 4 1\n",
"4 50\n5 1 10 2\n"
] | [
"2 1\n",
"4 0\n",
"-1\n"
] | none | [
{
"input": "4 3\n5 1 3 4",
"output": "2 1"
},
{
"input": "3 1\n2 4 1",
"output": "4 0"
},
{
"input": "4 50\n5 1 10 2",
"output": "-1"
},
{
"input": "3 4\n5 1 4",
"output": "-1"
},
{
"input": "1 1\n2",
"output": "2 1"
},
{
"input": "1 2\n10",
"outpu... | 124 | 0 | 3 | 48,903 | |
166 | Shoe Store | [
"dp",
"graph matchings",
"greedy",
"sortings",
"two pointers"
] | null | null | The warehouse in your shop has *n* shoe pairs. Each pair is characterized by two integers: its price *c**i* and its size *s**i*. We know that on this very day all numbers *s**i* are different, that is, there is no more than one pair of each size.
The shop has *m* customers who came at the same time. The customer numbe... | The first input line contains the only integer *n* (1<=β€<=*n*<=β€<=105) β the number of shoe pairs in the warehouse. Then *n* lines contain the descriptions of pairs of shoes as two integers *c**i* and *s**i* (1<=β€<=*c**i*,<=*s**i*<=β€<=109), the numbers are separated by a space. It is guaranteed that all numbers *s**i* ... | In the first line print the only integer β the maximum profit you can get from selling shoes. In the second line print an integer *k* β the number of shoe pairs you will sell. In the following *k* lines print the descriptions of the sold pairs β two space-separated integers where the first number is the customer's numb... | [
"3\n10 1\n30 2\n20 3\n2\n20 1\n20 2\n",
"3\n10 4\n20 5\n30 6\n2\n70 4\n50 5\n"
] | [
"30\n2\n2 3\n1 1\n",
"50\n2\n2 3\n1 2\n"
] | none | [] | 92 | 0 | 0 | 48,955 | |
840 | Leha and Function | [
"combinatorics",
"greedy",
"math",
"number theory",
"sortings"
] | null | null | Leha like all kinds of strange things. Recently he liked the function *F*(*n*,<=*k*). Consider all possible *k*-element subsets of the set [1,<=2,<=...,<=*n*]. For subset find minimal element in it. *F*(*n*,<=*k*) β mathematical expectation of the minimal element among all *k*-element subsets.
But only function does n... | First line of input data contains single integer *m* (1<=β€<=*m*<=β€<=2Β·105) β length of arrays *A* and *B*.
Next line contains *m* integers *a*1,<=*a*2,<=...,<=*a**m* (1<=β€<=*a**i*<=β€<=109) β array *A*.
Next line contains *m* integers *b*1,<=*b*2,<=...,<=*b**m* (1<=β€<=*b**i*<=β€<=109) β array *B*. | Output *m* integers *a*'1,<=*a*'2,<=...,<=*a*'*m* β array *A*' which is permutation of the array *A*. | [
"5\n7 3 5 3 4\n2 1 3 2 3\n",
"7\n4 6 5 8 8 2 6\n2 1 2 2 1 1 2\n"
] | [
"4 7 3 5 3\n",
"2 6 4 5 8 8 6\n"
] | none | [
{
"input": "5\n7 3 5 3 4\n2 1 3 2 3",
"output": "4 7 3 5 3"
},
{
"input": "7\n4 6 5 8 8 2 6\n2 1 2 2 1 1 2",
"output": "2 6 4 5 8 8 6"
}
] | 1,232 | 31,436,800 | 3 | 49,072 | |
708 | Incorrect Flow | [
"flows"
] | null | null | At the entrance examination for the magistracy of the MSU Cyber-Mechanics Department Sasha got the question about Ford-Fulkerson algorithm. He knew the topic perfectly as he worked with it many times on programming competition. As the task for the question he was given a network with partially build flow that he had to... | The first line of the input contains two integers *n* and *m* (2<=β€<=*n*<=β€<=100, 0<=β€<=*m*<=β€<=100)Β β the number of nodes and edges in the graph respectively. Each of the following *m* lines contains the description of the edges, consisting of four integers *u**i*, *v**i*, *c**i* and *f**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=... | Print one integerΒ β the minimum total sum of changes that Sasha has to do in order to get the correct flow description. | [
"2 1\n1 2 2 1\n",
"2 1\n1 2 1 2\n",
"3 3\n1 2 1 1\n2 3 2 2\n1 3 3 3\n",
"4 2\n2 3 1 1\n3 2 1 1\n"
] | [
"0\n",
"1\n",
"1\n",
"0\n"
] | In the first sample, the flow is initially correct. Note, that the flow is not maximum, but this is not required.
In the second sample, the flow value of the only edge is greater than its capacity. There are two ways to fix this: either increase the capacity up to 2 or reduce the flow down to 1.
In the third sample, ... | [] | 30 | 0 | 0 | 49,126 | |
87 | Beautiful Road | [
"dfs and similar",
"dp",
"dsu",
"graphs",
"implementation",
"sortings",
"trees"
] | D. Beautiful Road | 1 | 256 | A long time ago in some country in Asia were civil wars.
Each of *n* cities wanted to seize power. That's why sometimes one city gathered an army and sent it to campaign against another city.
Road making was difficult, so the country had few roads, exactly *n*<=-<=1. Also you could reach any city from any other city ... | The first line contains an integer *n* (2<=β€<=*n*<=β€<=105), which represents the number of cities. Next *n*<=-<=1 lines contain three integers each: the numbers of cities *a**i*,<=*b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*), connected by the *i*-th road and the number of days *d**i* the army spends to go on it (1<=β€<=*d**i*... | Print on the first line two integers β the number of beautiful trees on the most beautiful road and the number of the most beautiful roads. Print on the second line the list of the most beautiful roads in the sorted order by the numbers' increasing. The roads are numbered from 1 to *n*<=-<=1 in the order in which they ... | [
"2\n2 1 5\n",
"6\n1 2 1\n1 3 5\n3 4 2\n3 5 3\n3 6 4\n"
] | [
"2 1\n1 \n",
"16 1\n2 \n"
] | none | [
{
"input": "2\n2 1 5",
"output": "2 1\n1 "
},
{
"input": "6\n1 2 1\n1 3 5\n3 4 2\n3 5 3\n3 6 4",
"output": "16 1\n2 "
},
{
"input": "10\n10 6 43981\n4 2 6730\n1 2 35174\n5 3 61951\n8 7 43981\n7 1 6730\n5 8 6730\n9 3 52479\n6 4 18138",
"output": "32 1\n4 "
},
{
"input": "9\n6 ... | 1,000 | 50,176,000 | 0 | 49,159 |
191 | Demonstration | [
"greedy"
] | null | null | In the capital city of Berland, Bertown, demonstrations are against the recent election of the King of Berland. Berland opposition, led by Mr. Ovalny, believes that the elections were not fair enough and wants to organize a demonstration at one of the squares.
Bertown has *n* squares, numbered from 1 to *n*, they are ... | The first line contains two integers *n* and *k* β the number of squares and days left before the meeting, correspondingly (1<=β€<=*k*<=<<=*n*<=β€<=105).
The second line contains a single integer *b* β the number of bourles the administration has (1<=β€<=*b*<=β€<=1018).
The third line contains *n* space-separated inte... | Print a single number β the minimum number of the square where the opposition can organize the demonstration. | [
"5 2\n8\n2 4 5 3 1\n",
"5 2\n8\n3 2 4 1 5\n",
"5 4\n1000000000000000\n5 4 3 2 1\n"
] | [
"2\n",
"5\n",
"5\n"
] | In the first sample the opposition can act like this. On day one it applies for square 3. The administration has to organize an event there and end up with 3 bourles. If on the second day the opposition applies for square 2, the administration won't have the money to intervene.
In the second sample the opposition has ... | [] | 62 | 0 | 0 | 49,228 | |
599 | Spongebob and Squares | [
"brute force",
"math"
] | null | null | Spongebob is already tired trying to reason his weird actions and calculations, so he simply asked you to find all pairs of n and m, such that there are exactly *x* distinct squares in the table consisting of *n* rows and *m* columns. For example, in a 3<=Γ<=5 table there are 15 squares with side one, 8 squares with si... | The first line of the input contains a single integer *x* (1<=β€<=*x*<=β€<=1018)Β β the number of squares inside the tables Spongebob is interested in. | First print a single integer *k*Β β the number of tables with exactly *x* distinct squares inside.
Then print *k* pairs of integers describing the tables. Print the pairs in the order of increasing *n*, and in case of equalityΒ β in the order of increasing *m*. | [
"26\n",
"2\n",
"8\n"
] | [
"6\n1 26\n2 9\n3 5\n5 3\n9 2\n26 1\n",
"2\n1 2\n2 1\n",
"4\n1 8\n2 3\n3 2\n8 1\n"
] | In a 1βΓβ2 table there are 2 1βΓβ1 squares. So, 2 distinct squares in total.
In a 2βΓβ3 table there are 6 1βΓβ1 squares and 2 2βΓβ2 squares. That is equal to 8 squares in total. | [
{
"input": "26",
"output": "6\n1 26\n2 9\n3 5\n5 3\n9 2\n26 1"
},
{
"input": "2",
"output": "2\n1 2\n2 1"
},
{
"input": "8",
"output": "4\n1 8\n2 3\n3 2\n8 1"
},
{
"input": "1",
"output": "1\n1 1"
},
{
"input": "5005",
"output": "12\n1 5005\n5 335\n6 240\n10 9... | 265 | 14,950,400 | 0 | 49,411 | |
630 | Forecast | [
"math"
] | null | null | The Department of economic development of IT City created a model of city development till year 2100.
To prepare report about growth perspectives it is required to get growth estimates from the model.
To get the growth estimates it is required to solve a quadratic equation. Since the Department of economic developmen... | The only line of the input contains three integers *a*,<=*b*,<=*c* (<=-<=1000<=β€<=*a*,<=*b*,<=*c*<=β€<=1000) β the coefficients of *ax*2<=+<=*bx*<=+<=*c*<==<=0 equation. | In the first line output the greater of the equation roots, in the second line output the smaller one. Absolute or relative error should not be greater than 10<=-<=6. | [
"1 30 200\n"
] | [
"-10.000000000000000\n-20.000000000000000"
] | none | [
{
"input": "1 30 200",
"output": "-10.000000000000000\n-20.000000000000000"
},
{
"input": "1 1 -1",
"output": "0.618033988749895\n-1.618033988749895"
},
{
"input": "-1 1 1",
"output": "1.618033988749895\n-0.618033988749895"
},
{
"input": "1000 1 -1",
"output": "0.03112672... | 62 | 0 | 3 | 49,450 | |
955 | Icicles | [] | null | null | Andrew's favourite Krakozyabra has recenly fled away and now he's eager to bring it back!
At the moment the refugee is inside an icy cave with *n* icicles dangling from the ceiling located in integer coordinates numbered from 1 to *n*. The distance between floor and the *i*-th icicle is equal to *a**i*.
Andrew is fre... | The first line contains the number of icicles *n* (2<=β€<=*n*<=β€<=105).
The next line contains *n* space-separated numbers *a**i* (1<=β€<=*a**i*<=β€<=105)Β β the distances from floor to icicles. | Print an only integer β the minimum time it takes to entrap Krakozyabra between two fallen icicles. If it is impossible, print <=-<=1. | [
"5\n1 4 3 5 1\n",
"4\n1 2 1 1\n",
"2\n2 1\n",
"2\n1 2\n"
] | [
"3\n",
"2\n",
"3\n",
"-1\n"
] | In sample case one it's optimal to launch the sound wave from point 3. Then in two seconds icicles 1 and 5 will start falling, and in one more seconds they will block the paths. Krakozyabra will be located at <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/36de0b98a41a7a3c26317908808c214e6d... | [] | 15 | 0 | 0 | 49,752 | |
926 | Mobile Communications | [] | null | null | A sum of *p* rubles is charged from Arkady's mobile phone account every day in the morning. Among the following *m* days, there are *n* days when Arkady will top up the account: in the day *d**i* he will deposit *t**i* rubles on his mobile phone account. Arkady will always top up the account before the daily payment wi... | The first line contains three integers *n*, *p* and *m* (1<=β€<=*n*<=β€<=100<=000, 1<=β€<=*p*<=β€<=109, 1<=β€<=*m*<=β€<=109, *n*<=β€<=*m*) β the number of days Arkady will top up the account, the amount of the daily payment, and the number of days you should check.
The *i*-th of the following *n* lines contains two integers ... | Print the number of days from the 1-st to the *m*-th such that the account will have a negative amount on it after the daily payment. | [
"3 6 7\n2 13\n4 20\n7 9\n",
"5 4 100\n10 70\n15 76\n21 12\n30 100\n67 85\n"
] | [
"3\n",
"26\n"
] | In the first example the balance will change as following (remember, initially the balance is zero):
1. in the first day 6 rubles will be charged, the balance in the evening will be equal to β-β6; 1. in the second day Arkady will deposit 13 rubles, then 6 rubles will be charged, the balance in the evening will be eq... | [
{
"input": "3 6 7\n2 13\n4 20\n7 9",
"output": "3"
},
{
"input": "5 4 100\n10 70\n15 76\n21 12\n30 100\n67 85",
"output": "26"
},
{
"input": "14 25 100\n1 209\n2 224\n3 58\n4 31\n5 135\n6 16\n7 130\n8 113\n9 230\n10 60\n11 209\n12 185\n13 118\n14 16",
"output": "31"
},
{
"inp... | 62 | 7,065,600 | 0 | 49,796 | |
0 | none | [
"none"
] | C. Heroes | 2 | 256 | The year of 2012 is coming...
According to an ancient choradrican legend in this very year, in 2012, Diablo and his brothers Mephisto and Baal will escape from hell, and innumerable hordes of demons will enslave the human world. But seven brave heroes have already gathered on the top of a mountain Arreat to protect us... | The first line contains a single non-negative integer *n* (0<=β€<=*n*<=β€<=42) β amount of liking between the heroes. Next *n* lines describe liking in the form "p likes q", meaning that the hero p likes the hero q (p <=β <= q). Every liking is described in the input exactly once, no hero likes himself.
In the last line ... | Print two integers β the minimal difference in the experience between two heroes who will receive the maximum and minimum number of experience points, and the maximal total amount of liking in teams (the number of friendships between heroes that end up in one team).
When calculating the second answer, the team divisio... | [
"3\nTroll likes Dracul\nDracul likes Anka\nSnowy likes Hexadecimal\n210 200 180\n",
"2\nAnka likes Chapay\nChapay likes Anka\n10000 50 50\n"
] | [
"30 3\n",
"1950 2\n"
] | A note to first example: it the first team should be Dracul, Troll and Anka, in the second one Hexadecimal and Snowy, and in the third Cleo ΠΈ Chapay. | [
{
"input": "3\nTroll likes Dracul\nDracul likes Anka\nSnowy likes Hexadecimal\n210 200 180",
"output": "30 3"
},
{
"input": "2\nAnka likes Chapay\nChapay likes Anka\n10000 50 50",
"output": "1950 2"
},
{
"input": "11\nSnowy likes Dracul\nAnka likes Dracul\nChapay likes Snowy\nHexadecimal... | 218 | 6,963,200 | 3.93253 | 49,847 |
484 | Kindergarten | [
"data structures",
"dp",
"greedy"
] | null | null | In a kindergarten, the children are being divided into groups. The teacher put the children in a line and associated each child with his or her integer charisma value. Each child should go to exactly one group. Each group should be a nonempty segment of consecutive children of a line. A group's sociability is the maxim... | The first line contains integer *n*Β β the number of children in the line (1<=β€<=*n*<=β€<=106).
The second line contains *n* integers *a**i*Β β the charisma of the *i*-th child (<=-<=109<=β€<=*a**i*<=β€<=109). | Print the maximum possible total sociability of all groups. | [
"5\n1 2 3 1 2\n",
"3\n3 3 3\n"
] | [
"3\n",
"0\n"
] | In the first test sample one of the possible variants of an division is following: the first three children form a group with sociability 2, and the two remaining children form a group with sociability 1.
In the second test sample any division leads to the same result, the sociability will be equal to 0 in each group. | [
{
"input": "5\n1 2 3 1 2",
"output": "3"
},
{
"input": "3\n3 3 3",
"output": "0"
},
{
"input": "1\n0",
"output": "0"
},
{
"input": "2\n-1000000000 1000000000",
"output": "2000000000"
},
{
"input": "4\n1 4 2 3",
"output": "4"
},
{
"input": "4\n23 5 7 1"... | 15 | 0 | -1 | 49,896 | |
0 | none | [
"none"
] | null | null | One day Petya got a birthday present from his mom: a book called "The Legends and Myths of Graph Theory". From this book Petya learned about a hydra graph.
A non-oriented graph is a hydra, if it has a structure, shown on the figure below. Namely, there are two nodes *u* and *v* connected by an edge, they are the hydra... | The first line contains four integers *n*, *m*, *h*, *t* (1<=β€<=*n*,<=*m*<=β€<=105, 1<=β€<=*h*,<=*t*<=β€<=100) β the number of nodes and edges in graph *G*, and the number of a hydra's heads and tails.
Next *m* lines contain the description of the edges of graph *G*. The *i*-th of these lines contains two integers *a**i*... | If graph *G* has no hydra, print "NO" (without the quotes).
Otherwise, in the first line print "YES" (without the quotes). In the second line print two integers β the numbers of nodes *u* and *v*. In the third line print *h* numbers β the numbers of the nodes that are the heads. In the fourth line print *t* numbers β ... | [
"9 12 2 3\n1 2\n2 3\n1 3\n1 4\n2 5\n4 5\n4 6\n6 5\n6 7\n7 5\n8 7\n9 1\n",
"7 10 3 3\n1 2\n2 3\n1 3\n1 4\n2 5\n4 5\n4 6\n6 5\n6 7\n7 5\n"
] | [
"YES\n4 1\n5 6 \n9 3 2 \n",
"NO\n"
] | The first sample is depicted on the picture below: | [] | 154 | 307,200 | 0 | 49,925 | |
1,000 | Two-Paths | [
"data structures",
"dp",
"trees"
] | null | null | You are given a weighted tree (undirected connected graph with no cycles, loops or multiple edges) with $n$ vertices. The edge $\{u_j, v_j\}$ has weight $w_j$. Also each vertex $i$ has its own value $a_i$ assigned to it.
Let's call a path starting in vertex $u$ and ending in vertex $v$, where each edge can appear no m... | The first line contains two integers $n$ and $q$ ($2 \le n \le 3 \cdot 10^5$, $1 \le q \le 4 \cdot 10^5$) β the number of vertices in the tree and the number of queries.
The second line contains $n$ space-separated integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^9)$ β the values of the vertices.
Next $n - 1$ lines... | For each query print one integer per line β maximal profit $\text{Pr}(p)$ of the some 2-path $p$ with the corresponding endpoints. | [
"7 6\n6 5 5 3 2 1 2\n1 2 2\n2 3 2\n2 4 1\n4 5 1\n6 4 2\n7 3 25\n1 1\n4 4\n5 6\n6 4\n3 4\n3 7\n"
] | [
"9\n9\n9\n8\n12\n-14\n"
] | Explanation of queries:
1. $(1, 1)$ β one of the optimal 2-paths is the following: $1 \rightarrow 2 \rightarrow 4 \rightarrow 5 \rightarrow 4 \rightarrow 2 \rightarrow 3 \rightarrow 2 \rightarrow 1$. $\text{Pr}(p) = (a_1 + a_2 + a_3 + a_4 + a_5) - (2 \cdot w(1,2) + 2 \cdot w(2,3) + 2 \cdot w(2,4) + 2 \cdot w(4,5)) =... | [
{
"input": "7 6\n6 5 5 3 2 1 2\n1 2 2\n2 3 2\n2 4 1\n4 5 1\n6 4 2\n7 3 25\n1 1\n4 4\n5 6\n6 4\n3 4\n3 7",
"output": "9\n9\n9\n8\n12\n-14"
},
{
"input": "2 4\n5 3\n1 2 2\n1 1\n1 2\n2 1\n2 2",
"output": "5\n6\n6\n4"
},
{
"input": "5 1\n1000000000 1000000000 1000000000 1000000000 1000000000... | 3,042 | 268,390,400 | 0 | 49,940 | |
533 | Encoding | [
"hashing",
"string suffix structures",
"strings"
] | null | null | Polycarp invented a new way to encode strings. Let's assume that we have string *T*, consisting of lowercase English letters. Let's choose several pairs of letters of the English alphabet in such a way that each letter occurs in at most one pair. Then let's replace each letter in *T* with its pair letter if there is a ... | The first line of the input contains two integers, |*S*| and |*T*| (1<=β€<=|*T*|<=β€<=|*S*|<=β€<=2Β·105) β the lengths of string *S* and string *T*, respectively.
The second and third line of the input contain strings *S* and *T*, respectively. Both strings consist only of lowercase English letters. | Print number *k* β the number of suitable positions in string *S*.
In the next line print *k* integers *m*1,<=*m*2,<=...,<=*m**k* β the numbers of the suitable positions in the increasing order. | [
"11 5\nabacabadaba\nacaba\n",
"21 13\nparaparallelogramgram\nqolorreraglom\n"
] | [
"3\n1 3 7\n",
"1\n5\n"
] | none | [] | 124 | 0 | 0 | 50,001 | |
725 | Too Much Money | [
"brute force",
"greedy"
] | null | null | Alfred wants to buy a toy moose that costs *c* dollars. The store doesnβt give change, so he must give the store exactly *c* dollars, no more and no less. He has *n* coins. To make *c* dollars from his coins, he follows the following algorithm: let *S* be the set of coins being used. *S* is initially empty. Alfred repe... | The first line contains *c* (1<=β€<=*c*<=β€<=200<=000)Β β the price Alfred wants to pay. The second line contains *n* (1<=β€<=*n*<=β€<=200<=000)Β β the number of coins Alfred initially has. Then *n* lines follow, each containing a single integer *x* (1<=β€<=*x*<=β€<=*c*) representing the value of one of Alfred's coins. | If there is a solution, print the minimum possible total value of the coins in a solution. Otherwise, print "Greed is good" (without quotes). | [
"12\n3\n5\n3\n4\n",
"50\n8\n1\n2\n4\n8\n16\n37\n37\n37\n"
] | [
"5\n",
"Greed is good\n"
] | In the first sample, Bob should give Alfred a single coin worth $5. This creates the situation described in the problem statement.
In the second sample, there is no set of coins that will cause Alfred's algorithm to fail. | [] | 2,000 | 3,686,400 | 0 | 50,276 | |
67 | Sequence of Balls | [
"dp"
] | C. Sequence of Balls | 1 | 256 | You are given a sequence of balls *A* by your teacher, each labeled with a lowercase Latin letter 'a'-'z'. You don't like the given sequence. You want to change it into a new sequence, *B* that suits you better. So, you allow yourself four operations:
- You can insert any ball with any label into the sequence at any ... | The first line contains four space-separated integers *t**i*,<=*t**d*,<=*t**r*,<=*t**e* (0<=<<=*t**i*,<=*t**d*,<=*t**r*,<=*t**e*<=β€<=100). The following two lines contain sequences *A* and *B* on separate lines. The length of each line is between 1 and 4000 characters inclusive. | Print a single integer representing minimum time to convert *A* into *B*. | [
"1 1 1 1\nyoushouldnot\nthoushaltnot\n",
"2 4 10 3\nab\nba\n",
"1 10 20 30\na\nza\n"
] | [
"5\n",
"3\n",
"1\n"
] | In the second sample, you could delete the ball labeled 'a' from the first position and then insert another 'a' at the new second position with total time 6. However exchanging the balls give total time 3. | [] | 1,000 | 18,329,600 | 0 | 50,327 |
527 | Data Center Drama | [
"dfs and similar",
"graphs"
] | null | null | The project of a data center of a Big Software Company consists of *n* computers connected by *m* cables. Simply speaking, each computer can be considered as a box with multiple cables going out of the box. Very Important Information is transmitted along each cable in one of the two directions. As the data center plan ... | The first line contains two numbers, *n* and *m* (1<=β€<=*n*<=β€<=100<=000, 1<=β€<=*m*<=β€<=200<=000) β the number of computers and the number of the already present cables, respectively.
Each of the next lines contains two numbers *a**i*,<=*b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*) β the indices of the computers connected by... | In the first line print a single number *p* (*p*<=β₯<=*m*) β the minimum number of cables in the final scheme.
In each of the next *p* lines print a pair of numbers *c**i*,<=*d**i* (1<=β€<=*c**i*,<=*d**i*<=β€<=*n*), describing another cable. Such entry means that information will go along a certain cable in direction fro... | [
"4 6\n1 2\n2 3\n3 4\n4 1\n1 3\n1 3\n",
"3 4\n1 2\n2 3\n1 1\n3 3\n"
] | [
"6\n1 2\n3 4\n1 4\n3 2\n1 3\n1 3",
"6\n2 1\n2 3\n1 1\n3 3\n3 1\n1 1\n"
] | Picture for the first sample test. The tied pairs of cables are shown going out from the same point.
Picture for the second test from the statement. The added cables are drawin in bold.
Alternative answer for the second sample test: | [
{
"input": "4 6\n1 2\n2 3\n3 4\n4 1\n1 3\n1 3",
"output": "6\n3 4\n1 4\n3 1\n2 1\n2 3\n1 3"
},
{
"input": "3 4\n1 2\n2 3\n1 1\n3 3",
"output": "6\n1 1\n3 3\n2 3\n2 1\n3 1\n1 1"
},
{
"input": "1 1\n1 1",
"output": "2\n1 1\n1 1"
},
{
"input": "1 2\n1 1\n1 1",
"output": "2\n... | 46 | 0 | 0 | 50,396 | |
875 | Royal Questions | [
"dsu",
"graphs",
"greedy"
] | null | null | In a medieval kingdom, the economic crisis is raging. Milk drops fall, Economic indicators are deteriorating every day, money from the treasury disappear. To remedy the situation, King Charles Sunnyface decided make his *n* sons-princes marry the brides with as big dowry as possible.
In search of candidates, the king ... | The first line contains two integers *n*, *m* (2<=β€<=*n*<=β€<=200<=000, 1<=β€<=*m*<=β€<=200<=000)Β β number of princes and princesses respectively.
Each of following *m* lines contains three integers *a**i*, *b**i*, *w**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*, *a**i*<=β <=*b**i*, 1<=β€<=*w**i*<=β€<=10<=000)Β β number of princes, wh... | Print the only integerΒ β the maximum number of gold coins that a king can get by playing the right weddings. | [
"2 3\n1 2 5\n1 2 1\n2 1 10\n",
"3 2\n1 2 10\n3 2 20\n"
] | [
"15",
"30"
] | none | [
{
"input": "2 3\n1 2 5\n1 2 1\n2 1 10",
"output": "15"
},
{
"input": "3 2\n1 2 10\n3 2 20",
"output": "30"
},
{
"input": "2 2\n1 2 1\n1 2 2",
"output": "3"
},
{
"input": "3 3\n1 3 4\n1 3 2\n1 3 8",
"output": "12"
},
{
"input": "2 1\n1 2 6",
"output": "6"
},
... | 46 | 5,529,600 | -1 | 50,423 | |
540 | Ice Cave | [
"dfs and similar"
] | null | null | You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice.
The level of the cave where you are is a rectangular square grid of *n* rows and *m* columns. Each cell consis... | The first line contains two integers, *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=500)Β β the number of rows and columns in the cave description.
Each of the next *n* lines describes the initial state of the level of the cave, each line consists of *m* characters "." (that is, intact ice) and "X" (cracked ice).
The next line con... | If you can reach the destination, print 'YES', otherwise print 'NO'. | [
"4 6\nX...XX\n...XX.\n.X..X.\n......\n1 6\n2 2\n",
"5 4\n.X..\n...X\nX.X.\n....\n.XX.\n5 3\n1 1\n",
"4 7\n..X.XX.\n.XX..X.\nX...X..\nX......\n2 2\n1 6\n"
] | [
"YES\n",
"NO\n",
"YES\n"
] | In the first sample test one possible path is:
<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c61f56de718beea14935ccdc85ae2c4ad45c1454.png" style="max-width: 100.0%;max-height: 100.0%;"/>
After the first visit of cell (2,β2) the ice on it cracks and when you step there for the second tim... | [
{
"input": "4 6\nX...XX\n...XX.\n.X..X.\n......\n1 6\n2 2",
"output": "YES"
},
{
"input": "5 4\n.X..\n...X\nX.X.\n....\n.XX.\n5 3\n1 1",
"output": "NO"
},
{
"input": "4 7\n..X.XX.\n.XX..X.\nX...X..\nX......\n2 2\n1 6",
"output": "YES"
},
{
"input": "5 3\n.XX\n...\n.X.\n.X.\n.... | 46 | 102,400 | 0 | 50,451 | |
936 | World of Tank | [
"dp",
"greedy"
] | null | null | Vitya loves programming and problem solving, but sometimes, to distract himself a little, he plays computer games. Once he found a new interesting game about tanks, and he liked it so much that he went through almost all levels in one day. Remained only the last level, which was too tricky. Then Vitya remembered that h... | The first line contains four integers *n*, *m*1, *m*2 and *t*, the length of the field, the number of obstacles in the first lane, the number of obstacles in the second lane and the number of tank steps before reloading, respectively (1<=β€<=*n*<=β€<=109; 0<=β€<=*m*1,<=*m*2<=β€<=*n*; 0<=β€<=*m*1<=+<=*m*2<=β€<=106; 1<=β€<=*t*<... | In the first line print Β«YesΒ», if it is possible to pass the level, or Β«NoΒ», otherwise.
If it is possible, then in the second line print the number of times the tank moves from one lane to another, and in the next line print the coordinates of the transitions, one number per transition: the coordinate *x* (0<=β€<=*x*<=... | [
"6 2 3 2\n2 6\n3 5 6\n",
"1 1 1 1\n1\n1\n",
"9 5 2 5\n1 2 7 8 9\n4 6\n"
] | [
"Yes\n2\n0 3 \n2\n2 2\n4 1\n",
"No\n",
"Yes\n4\n0 3 5 10 \n1\n5 2\n"
] | Picture for the first sample test. | [] | 78 | 307,200 | 0 | 50,569 | |
837 | Prefix Sums | [
"binary search",
"brute force",
"combinatorics",
"math",
"matrices"
] | null | null | Consider the function *p*(*x*), where *x* is an array of *m* integers, which returns an array *y* consisting of *m*<=+<=1 integers such that *y**i* is equal to the sum of first *i* elements of array *x* (0<=β€<=*i*<=β€<=*m*).
You have an infinite sequence of arrays *A*0,<=*A*1,<=*A*2..., where *A*0 is given in the input... | The first line contains two integers *n* and *k* (2<=β€<=*n*<=β€<=200000, 1<=β€<=*k*<=β€<=1018). *n* is the size of array *A*0.
The second line contains *n* integers *A*00,<=*A*01... *A*0*n*<=-<=1 β the elements of *A*0 (0<=β€<=*A*0*i*<=β€<=109). At least two elements of *A*0 are positive. | Print the minimum *i* such that *A**i* contains a number which is larger or equal than *k*. | [
"2 2\n1 1\n",
"3 6\n1 1 1\n",
"3 1\n1 0 1\n"
] | [
"1\n",
"2\n",
"0\n"
] | none | [
{
"input": "2 2\n1 1",
"output": "1"
},
{
"input": "3 6\n1 1 1",
"output": "2"
},
{
"input": "3 1\n1 0 1",
"output": "0"
},
{
"input": "3 1000000000000000000\n5 4 5",
"output": "632455531"
},
{
"input": "4 1000000000000000000\n0 4 4 5",
"output": "707106780"
... | 514 | 8,192,000 | 0 | 50,653 | |
524 | The Art of Dealing with ATM | [
"binary search",
"sortings"
] | null | null | ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most *k* bills from it, and the ... | The first line contains two integers *n*, *k* (1<=β€<=*n*<=β€<=5000, 1<=β€<=*k*<=β€<=20).
The next line contains *n* space-separated integers *a**i* (1<=β€<=*a**i*<=β€<=107) β the denominations of the bills that are used in the country. Numbers *a**i* follow in the strictly increasing order.
The next line contains integer ... | For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print <=-<=1, if it is impossible to get the corresponding sum. | [
"6 20\n10 50 100 500 1000 5000\n8\n4200\n100000\n95000\n96000\n99000\n10100\n2015\n9950\n",
"5 2\n1 2 3 5 8\n8\n1\n3\n5\n7\n9\n11\n13\n15\n"
] | [
"6\n20\n19\n20\n-1\n3\n-1\n-1\n",
"1\n1\n1\n2\n2\n2\n2\n-1\n"
] | none | [
{
"input": "6 20\n10 50 100 500 1000 5000\n8\n4200\n100000\n95000\n96000\n99000\n10100\n2015\n9950",
"output": "6\n20\n19\n20\n-1\n3\n-1\n-1"
},
{
"input": "5 2\n1 2 3 5 8\n8\n1\n3\n5\n7\n9\n11\n13\n15",
"output": "1\n1\n1\n2\n2\n2\n2\n-1"
},
{
"input": "5 5\n1 2 3 6 102\n10\n1\n4\n30\n1... | 265 | 0 | 0 | 50,706 | |
489 | Unbearable Controversy of Being | [
"brute force",
"combinatorics",
"dfs and similar",
"graphs"
] | null | null | Tomash keeps wandering off and getting lost while he is walking along the streets of Berland. It's no surprise! In his home town, for any pair of intersections there is exactly one way to walk from one intersection to the other one. The capital of Berland is very different!
Tomash has noticed that even simple cases of... | The first line of the input contains a pair of integers *n*, *m* (1<=β€<=*n*<=β€<=3000,<=0<=β€<=*m*<=β€<=30000) β the number of intersections and roads, respectively. Next *m* lines list the roads, one per line. Each of the roads is given by a pair of integers *a**i*,<=*b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*;*a**i*<=β <=*b**i... | Print the required number of "damn rhombi". | [
"5 4\n1 2\n2 3\n1 4\n4 3\n",
"4 12\n1 2\n1 3\n1 4\n2 1\n2 3\n2 4\n3 1\n3 2\n3 4\n4 1\n4 2\n4 3\n"
] | [
"1\n",
"12\n"
] | none | [
{
"input": "5 4\n1 2\n2 3\n1 4\n4 3",
"output": "1"
},
{
"input": "4 12\n1 2\n1 3\n1 4\n2 1\n2 3\n2 4\n3 1\n3 2\n3 4\n4 1\n4 2\n4 3",
"output": "12"
},
{
"input": "1 0",
"output": "0"
},
{
"input": "10 20\n6 10\n4 2\n1 5\n6 1\n8 9\n1 3\n2 6\n9 7\n4 5\n3 7\n9 2\n3 9\n4 8\n1 10... | 0 | 0 | -1 | 50,748 | |
0 | none | [
"none"
] | null | null | In the year 2500 the annual graduation ceremony in the German University in Cairo (GUC) has run smoothly for almost 500 years so far.
The most important part of the ceremony is related to the arrangement of the professors in the ceremonial hall.
Traditionally GUC has *n* professors. Each professor has his seniority l... | The first line contains three integers *n*, *y* and *m* (1<=β€<=*n*<=β€<=16,<=2001<=β€<=*y*<=β€<=1018,<=0<=β€<=*m*<=β€<=100) β the number of professors, the year for which the arrangement should be computed, and the number of pairs of seats for which the seniority relation should be kept, respectively.
The next *m* lines co... | Print the order in which the professors should be seated in the requested year.
If by this year the GUC would have ran out of arrangements, or the given "senior-junior" relation are contradictory, print "The times have changed" (without quotes). | [
"3 2001 2\n1 2\n2 3\n",
"7 2020 6\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"10 3630801 0\n",
"3 2001 3\n1 2\n2 3\n3 1\n"
] | [
"1 2 3\n",
"1 2 3 7 4 6 5\n",
"The times have changed\n",
"The times have changed\n"
] | In the first example the lexicographically first order of seating is 1 2 3.
In the third example the GUC will run out of arrangements after the year 3630800.
In the fourth example there are no valid arrangements for the seating.
The lexicographical comparison of arrangements is performed by the < operator in mode... | [] | 62 | 0 | 0 | 50,788 | |
630 | Area of a Star | [
"geometry"
] | null | null | It was decided in IT City to distinguish successes of local IT companies by awards in the form of stars covered with gold from one side. To order the stars it is necessary to estimate order cost that depends on the area of gold-plating. Write a program that can calculate the area of a star.
A "star" figure having *n*<... | The only line of the input contains two integers *n* (5<=β€<=*n*<=<<=109, *n* is prime) and *r* (1<=β€<=*r*<=β€<=109) β the number of the star corners and the radius of the circumcircle correspondingly. | Output one number β the star area. The relative error of your answer should not be greater than 10<=-<=7. | [
"7 10\n"
] | [
"108.395919545675"
] | none | [
{
"input": "7 10",
"output": "108.395919545675"
},
{
"input": "5 1",
"output": "1.122569941449"
},
{
"input": "7 1000",
"output": "1083959.195456745256"
},
{
"input": "11 1000000000",
"output": "1061689977712182980.125000000000"
},
{
"input": "999999937 1",
"o... | 0 | 0 | -1 | 50,794 | |
138 | Hellish Constraints | [
"brute force",
"dp",
"two pointers"
] | null | null | Katya recently started to invent programming tasks and prepare her own contests. What she does not like is boring and simple constraints. Katya is fed up with all those "*N* does not exceed a thousand" and "the sum of *a**i* does not exceed a million" and she decided to come up with something a little more complicated.... | The first line contains a non-empty string *s*, consisting of small Latin letters. The length of the string *s* does not exceed 105.
The second line contains three space-separated integers *k*, *L* and *R* (0<=β€<=*L*<=β€<=*R*<=β€<=*k*<=β€<=500).
Next *k* lines contain Katya's constrictions in the following form "*c**i* ... | Print a single number β the number of substrings that meet the constrictions.
Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cout stream or the %I64d specificator. | [
"codeforces\n2 0 0\no 1 2\ne 1 2\n",
"codeforces\n2 1 1\no 1 2\no 1 2\n"
] | [
"7\n",
"0\n"
] | In the first test we should count the number of strings that do not contain characters "e" and "o". All such strings are as follows (in the order of occurrence in the initial string from the left to the right): "c", "d"', "f", "r", "rc", "c", "s".
In the second test we cannot achieve fulfilling exactly one of the two ... | [] | 124 | 0 | 0 | 50,827 | |
727 | Games on a CD | [
"data structures",
"hashing",
"string suffix structures",
"strings"
] | null | null | Several years ago Tolya had *n* computer games and at some point of time he decided to burn them to CD. After that he wrote down the names of the games one after another in a circle on the CD in clockwise order. The names were distinct, the length of each name was equal to *k*. The names didn't overlap.
Thus, there is... | The first line of the input contains two positive integers *n* and *k* (1<=β€<=*n*<=β€<=105, 1<=β€<=*k*<=β€<=105)Β β the amount of games Tolya burned to the CD, and the length of each of the names.
The second line of the input contains one string consisting of lowercase English lettersΒ β the string Tolya wrote on the CD, s... | If there is no answer, print "NO" (without quotes).
Otherwise, print two lines. In the first line print "YES" (without quotes). In the second line, print *n* integersΒ β the games which names were written on the CD. You should print games in the order they could have been written on the CD, it means, in clockwise order... | [
"3 1\nabc\n4\nb\na\nc\nd\n",
"4 2\naabbccdd\n4\ndd\nab\nbc\ncd\n"
] | [
"YES\n2 1 3 \n",
"NO\n"
] | none | [
{
"input": "3 1\nabc\n4\nb\na\nc\nd",
"output": "YES\n2 1 3 "
},
{
"input": "4 2\naabbccdd\n4\ndd\nab\nbc\ncd",
"output": "NO"
},
{
"input": "2 2\nkdcj\n4\ncj\nkd\njk\ndc",
"output": "YES\n3 4 "
},
{
"input": "10 10\nlgfrjgityzwtmfyygmpsmokiwphewhpoelsvnctwxmpimqvblgrisozncsi... | 46 | 0 | 0 | 50,865 | |
961 | Tufurama | [
"data structures"
] | null | null | One day Polycarp decided to rewatch his absolute favourite episode of well-known TV series "Tufurama". He was pretty surprised when he got results only for season 7 episode 3 with his search query of "Watch Tufurama season 3 episode 7 online full hd free". This got Polycarp confused β what if he decides to rewatch the ... | The first line contains one integer *n* (1<=<=β€<=*n*<=<=β€<=<=2Β·105) β the number of seasons.
The second line contains *n* integers separated by space *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109) β number of episodes in each season. | Print one integer β the number of pairs *x* and *y* (*x*<=<<=*y*) such that there exist both season *x* episode *y* and season *y* episode *x*. | [
"5\n1 2 3 4 5\n",
"3\n8 12 7\n",
"3\n3 2 1\n"
] | [
"0\n",
"3\n",
"2\n"
] | Possible pairs in the second example:
1. *x*β=β1, *y*β=β2 (season 1 episode 2 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/8774ca35b6e628888a4670e4539d47857e5e5841.png" style="max-width: 100.0%;max-height: 100.0%;"/> season 2 episode 1); 1. *x*β=β2, *y*β=β3 (season 2 episode 3 <img al... | [
{
"input": "5\n1 2 3 4 5",
"output": "0"
},
{
"input": "3\n8 12 7",
"output": "3"
},
{
"input": "3\n3 2 1",
"output": "2"
},
{
"input": "5\n2 3 4 5 6",
"output": "4"
},
{
"input": "8\n7 2 6 6 5 1 4 9",
"output": "9"
},
{
"input": "10\n1000000000 100000... | 498 | 44,236,800 | 3 | 51,079 | |
54 | First Digit Law | [
"dp",
"math",
"probabilities"
] | C. First Digit Law | 2 | 256 | In the probability theory the following paradox called Benford's law is known: "In many lists of random numbers taken from real sources, numbers starting with digit 1 occur much more often than numbers starting with any other digit" (that's the simplest form of the law).
Having read about it on Codeforces, the Hedgeho... | The first line contains number *N* which is the number of random variables (1<=β€<=*N*<=β€<=1000). Then follow *N* lines containing pairs of numbers *L**i*,<=*R**i*, each of whom is a description of a random variable. It is guaranteed that 1<=β€<=*L**i*<=β€<=*R**i*<=β€<=1018.
The last line contains an integer *K* (0<=β€<=*K... | Print the required probability. Print the fractional number with such a precision that the relative or absolute error of the result won't exceed 10<=-<=9. | [
"1\n1 2\n50\n",
"2\n1 2\n9 11\n50\n"
] | [
"0.500000000000000",
"0.833333333333333"
] | none | [
{
"input": "1\n1 2\n50",
"output": "0.500000000000000"
},
{
"input": "2\n1 2\n9 11\n50",
"output": "0.833333333333333"
},
{
"input": "3\n2 9\n20 99\n5 5\n0",
"output": "1.000000000000000"
},
{
"input": "3\n2 9\n20 99\n5 5\n100",
"output": "0.000000000000000"
},
{
... | 748 | 409,600 | 0 | 51,162 |
913 | Don't Exceed | [
"math",
"probabilities"
] | null | null | You generate real numbers *s*1,<=*s*2,<=...,<=*s**n* as follows:
- *s*0<==<=0; - *s**i*<==<=*s**i*<=-<=1<=+<=*t**i*, where *t**i* is a real number chosen independently uniformly at random between 0 and 1, inclusive.
You are given real numbers *x*1,<=*x*2,<=...,<=*x**n*. You are interested in the probability that *... | The first line contains integer *n* (1<=β€<=*n*<=β€<=30).
The next *n* lines contain real numbers *x*1,<=*x*2,<=...,<=*x**n*, given with at most six digits after the decimal point (0<=<<=*x**i*<=β€<=*n*). | Print a single integer, the answer to the problem. | [
"4\n1.00\n2\n3.000000\n4.0\n",
"1\n0.50216\n",
"2\n0.5\n1.0\n",
"6\n0.77\n1.234567\n2.1\n1.890\n2.9999\n3.77\n"
] | [
"1\n",
"342677322\n",
"623902721\n",
"859831967\n"
] | In the first example, the sought probability is 1 since the sum of *i* real numbers which don't exceed 1 doesn't exceed *i*.
In the second example, the probability is *x*<sub class="lower-index">1</sub> itself.
In the third example, the sought probability is 3β/β8. | [] | 46 | 0 | 0 | 51,206 | |
45 | Prime Problem | [
"number theory"
] | G. Prime Problem | 1 | 256 | In Berland prime numbers are fashionable β the respectable citizens dwell only on the floors with numbers that are prime numbers. The numismatists value particularly high the coins with prime nominal values. All the prime days are announced holidays!
Yet even this is not enough to make the Berland people happy. On the... | The single input line contains an integer *n* (2<=β€<=*n*<=β€<=6000) β the number of houses on the main streets of the capital. | Print the sequence of *n* numbers, where the *i*-th number stands for the number of color for house number *i*. Number the colors consecutively starting from 1. Any painting order is allowed. If there are several solutions to that problem, print any of them. If there's no such way of painting print the single number -1... | [
"8"
] | [
"1 2 2 1 1 1 1 2"
] | none | [
{
"input": "8",
"output": "1 1 1 1 2 1 1 1 "
},
{
"input": "2",
"output": "1 1 "
},
{
"input": "3",
"output": "1 1 2 "
},
{
"input": "4",
"output": "1 1 2 1 "
},
{
"input": "5",
"output": "1 2 1 1 1 "
},
{
"input": "6",
"output": "1 2 1 1 1 1 "
}... | 154 | 11,468,800 | 0 | 51,354 |
215 | Crosses | [
"brute force",
"implementation"
] | null | null | There is a board with a grid consisting of *n* rows and *m* columns, the rows are numbered from 1 from top to bottom and the columns are numbered from 1 from left to right. In this grid we will denote the cell that lies on row number *i* and column number *j* as (*i*,<=*j*).
A group of six numbers (*a*,<=*b*,<=*c*,<=*... | The input consists of a single line containing three integers *n*, *m* and *s* (1<=β€<=*n*,<=*m*<=β€<=500, 1<=β€<=*s*<=β€<=*n*Β·*m*). The integers are separated by a space. | Print a single integer β the number of distinct groups of six integers that denote crosses with area *s* and that are fully placed on the *n*<=Γ<=*m* grid.
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. | [
"2 2 1\n",
"3 4 5\n"
] | [
"4\n",
"4\n"
] | In the first sample the sought groups of six numbers are: (0,β0,β0,β0,β1,β1), (0,β0,β0,β0,β1,β2), (0,β0,β0,β0,β2,β1), (0,β0,β0,β0,β2,β2).
In the second sample the sought groups of six numbers are: (0,β1,β1,β0,β2,β2), (0,β1,β1,β0,β2,β3), (1,β0,β0,β1,β2,β2), (1,β0,β0,β1,β2,β3). | [
{
"input": "2 2 1",
"output": "4"
},
{
"input": "3 4 5",
"output": "4"
},
{
"input": "2 2 3",
"output": "0"
},
{
"input": "5 1 3",
"output": "9"
},
{
"input": "9 7 55",
"output": "4"
},
{
"input": "5 10 25",
"output": "102"
},
{
"input": "2... | 122 | 0 | 0 | 51,366 | |
903 | Swapping Characters | [
"brute force",
"hashing",
"implementation",
"strings"
] | null | null | We had a string *s* consisting of *n* lowercase Latin letters. We made *k* copies of this string, thus obtaining *k* identical strings *s*1,<=*s*2,<=...,<=*s**k*. After that, in each of these strings we swapped exactly two characters (the characters we swapped could be identical, but they had different indices in the s... | The first line contains two integers *k* and *n* (1<=β€<=*k*<=β€<=2500,<=2<=β€<=*n*<=β€<=5000,<=*k*Β Β·Β *n*<=β€<=5000) β the number of strings we obtained, and the length of each of these strings.
Next *k* lines contain the strings *s*1,<=*s*2,<=...,<=*s**k*, each consisting of exactly *n* lowercase Latin letters. | Print any suitable string *s*, or -1 if such string doesn't exist. | [
"3 4\nabac\ncaab\nacba\n",
"3 4\nkbbu\nkbub\nubkb\n",
"5 4\nabcd\ndcba\nacbd\ndbca\nzzzz\n"
] | [
"acab\n",
"kbub\n",
"-1\n"
] | In the first example *s*<sub class="lower-index">1</sub> is obtained by swapping the second and the fourth character in acab, *s*<sub class="lower-index">2</sub> is obtained by swapping the first and the second character, and to get *s*<sub class="lower-index">3</sub>, we swap the third and the fourth character.
In th... | [
{
"input": "3 4\nabac\ncaab\nacba",
"output": "acab"
},
{
"input": "3 4\nkbbu\nkbub\nubkb",
"output": "kbub"
},
{
"input": "5 4\nabcd\ndcba\nacbd\ndbca\nzzzz",
"output": "-1"
},
{
"input": "3 2\nxh\nxh\nxh",
"output": "hx"
},
{
"input": "3 4\nkbub\nkbbu\nubkb",
... | 77 | 20,172,800 | -1 | 51,369 | |
19 | Fairy | [
"dfs and similar",
"divide and conquer",
"dsu"
] | E. Fairy | 1 | 256 | Once upon a time there lived a good fairy A. One day a fine young man B came to her and asked to predict his future. The fairy looked into her magic ball and said that soon the fine young man will meet the most beautiful princess ever and will marry her. Then she drew on a sheet of paper *n* points and joined some of t... | The first input line contains two integer numbers: *n* β amount of the drawn points and *m* β amount of the drawn segments (1<=β€<=*n*<=β€<=104,<=0<=β€<=*m*<=β€<=104). The following *m* lines contain the descriptions of the segments. Each description contains two different space-separated integer numbers *v*, *u* (1<=β€<=*v... | In the first line output number *k* β amount of the segments in the answer. In the second line output *k* space-separated numbers β indexes of these segments in ascending order. Each index should be output only once. Segments are numbered from 1 in the input order. | [
"4 4\n1 2\n1 3\n2 4\n3 4\n",
"4 5\n1 2\n2 3\n3 4\n4 1\n1 3\n"
] | [
"4\n1 2 3 4 ",
"1\n5 "
] | none | [] | 60 | 0 | 0 | 51,374 |
618 | Double Knapsack | [
"constructive algorithms",
"two pointers"
] | null | null | You are given two multisets *A* and *B*. Each multiset has exactly *n* integers each between 1 and *n* inclusive. Multisets may contain multiple copies of the same number.
You would like to find a nonempty subset of *A* and a nonempty subset of *B* such that the sum of elements in these subsets are equal. Subsets are ... | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=1<=000<=000)Β β the size of both multisets.
The second line contains *n* integers, denoting the elements of *A*. Each element will be between 1 and *n* inclusive.
The third line contains *n* integers, denoting the elements of *B*. Each element wi... | If there is no solution, print a single integer <=-<=1. Otherwise, your solution should be printed on four lines.
The first line should contain a single integer *k**a*, the size of the corresponding subset of *A*. The second line should contain *k**a* distinct integers, the indices of the subset of *A*.
The third lin... | [
"10\n10 10 10 10 10 10 10 10 10 10\n10 9 8 7 6 5 4 3 2 1\n",
"5\n4 4 3 3 3\n2 2 2 2 5\n"
] | [
"1\n2\n3\n5 8 10\n",
"2\n2 3\n2\n3 5\n"
] | none | [
{
"input": "10\n10 10 10 10 10 10 10 10 10 10\n10 9 8 7 6 5 4 3 2 1",
"output": "1\n2\n3\n5 8 10"
},
{
"input": "5\n4 4 3 3 3\n2 2 2 2 5",
"output": "2\n2 3\n2\n3 5"
}
] | 2,500 | 198,553,600 | 0 | 51,466 | |
135 | Zero-One | [
"constructive algorithms",
"games",
"greedy"
] | null | null | Little Petya very much likes playing with little Masha. Recently he has received a game called "Zero-One" as a gift from his mother. Petya immediately offered Masha to play the game with him.
Before the very beginning of the game several cards are lain out on a table in one line from the left to the right. Each card c... | The first line contains a sequence of characters each of which can either be a "0", a "1" or a "?". This sequence determines the initial arrangement of cards on the table from the left to the right. The characters "?" mean that the given card was spoiled before the game. The sequence's length ranges from 2 to 105, incl... | Print the set of outcomes for all possible initial digits arrangements. Print each possible outcome on a single line. Each outcome should be represented by two characters: the digits written on the cards that were left by the end of the game. The outcomes should be sorted lexicographically in ascending order (see the f... | [
"????\n",
"1010\n",
"1?1\n"
] | [
"00\n01\n10\n11\n",
"10\n",
"01\n11\n"
] | In the first sample all 16 variants of numbers arrangement are possible. For the variant 0000 the outcome is 00. For the variant 1111 the outcome is 11. For the variant 0011 the outcome is 01. For the variant 1100 the outcome is 10. Regardless of outcomes for all other variants the set which we are looking for will con... | [
{
"input": "????",
"output": "00\n01\n10\n11"
},
{
"input": "1010",
"output": "10"
},
{
"input": "1?1",
"output": "01\n11"
},
{
"input": "111?",
"output": "11"
},
{
"input": "000?",
"output": "00"
},
{
"input": "1??1?",
"output": "00\n01\n10\n11"
... | 186 | 0 | 0 | 51,477 | |
796 | Bank Hacking | [
"constructive algorithms",
"data structures",
"dp",
"trees"
] | null | null | Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To search for Zane, he would need a lot of money, of which he sadly has none. To deal with the problem, he has decided to hack the banks.
There are *n* banks, numbered from 1 to *n*. There are also *n*<=-<=1 wires connecting the b... | The first line contains one integer *n* (1<=β€<=*n*<=β€<=3Β·105)Β β the total number of banks.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=β€<=*a**i*<=β€<=109)Β β the strengths of the banks.
Each of the next *n*<=-<=1 lines contains two integers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n... | Print one integerΒ β the minimum strength of the computer Inzane needs to accomplish the goal. | [
"5\n1 2 3 4 5\n1 2\n2 3\n3 4\n4 5\n",
"7\n38 -29 87 93 39 28 -55\n1 2\n2 5\n3 2\n2 4\n1 7\n7 6\n",
"5\n1 2 7 6 7\n1 5\n5 3\n3 4\n2 4\n"
] | [
"5",
"93",
"8"
] | In the first sample, Inzane can hack all banks using a computer with strength 5. Here is how:
- Initially, strengths of the banks are [1,β2,β3,β4,β5]. - He hacks bank 5, then strengths of the banks become [1,β2,β4,β5,ββ-β]. - He hacks bank 4, then strengths of the banks become [1,β3,β5,ββ-β,ββ-β]. - He hacks bank ... | [
{
"input": "5\n1 2 3 4 5\n1 2\n2 3\n3 4\n4 5",
"output": "5"
},
{
"input": "7\n38 -29 87 93 39 28 -55\n1 2\n2 5\n3 2\n2 4\n1 7\n7 6",
"output": "93"
},
{
"input": "5\n1 2 7 6 7\n1 5\n5 3\n3 4\n2 4",
"output": "8"
},
{
"input": "3\n2 2 2\n3 2\n1 2",
"output": "3"
},
{
... | 62 | 0 | 0 | 51,632 | |
110 | Lucky String | [
"constructive algorithms",
"strings"
] | B. Lucky String | 2 | 256 | Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya recently learned to determine whether a string of lowercase Latin letters is lucky. For each i... | The single line contains a positive integer *n* (1<=β€<=*n*<=β€<=105) β the length of the sought string. | Print on the single line the lexicographically minimal lucky string whose length equals *n*. | [
"5\n",
"3\n"
] | [
"abcda\n",
"abc\n"
] | The lexical comparison of strings is performed by the < operator in modern programming languages. String *a* is lexicographically less than string *b* if exists such *i* (1ββ€β*i*ββ€β*n*), that *a*<sub class="lower-index">*i*</sub>β<β*b*<sub class="lower-index">*i*</sub>, and for any *j* (1ββ€β*j*β<β*i*) *a*<sub ... | [
{
"input": "5",
"output": "abcda"
},
{
"input": "3",
"output": "abc"
},
{
"input": "8",
"output": "abcdabcd"
},
{
"input": "10",
"output": "abcdabcdab"
},
{
"input": "16",
"output": "abcdabcdabcdabcd"
},
{
"input": "64",
"output": "abcdabcdabcdabcd... | 124 | 819,200 | 3.967474 | 51,715 |
97 | Leaders | [
"dfs and similar",
"dsu",
"graphs",
"trees"
] | E. Leaders | 2 | 256 | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly orde... | The first line contains integers *n* and *m* (1<=β€<=*n*<=β€<=105,<=0<=β€<=*m*<=β€<=105) β the number of vertices and edges in the relationship graph correspondingly. Next *m* lines contain pairs of integers *a* and *b* which mean that there is an edge between the *a*-th and the *b*-th vertices (the vertices are numbered s... | For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No". | [
"7 7\n1 3\n1 4\n2 3\n2 4\n5 6\n6 7\n7 5\n8\n1 2\n1 3\n1 4\n2 4\n1 5\n5 6\n5 7\n6 7\n"
] | [
"No\nYes\nYes\nYes\nNo\nYes\nYes\nYes\n"
] | Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected componen... | [] | 92 | 0 | 0 | 51,862 |
164 | Variable, or There and Back Again | [
"dfs and similar",
"graphs"
] | null | null | Life is not easy for the perfectly common variable named Vasya. Wherever it goes, it is either assigned a value, or simply ignored, or is being used!
Vasya's life goes in states of a program. In each state, Vasya can either be used (for example, to calculate the value of another variable), or be assigned a value, or i... | The first line contains two space-separated integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=105) β the numbers of states and transitions, correspondingly.
The second line contains space-separated *n* integers *f*1,<=*f*2,<=...,<=*f**n* (0<=β€<=*f**i*<=β€<=2), *f**i* described actions performed upon Vasya in state *i*: 0 repre... | Print *n* integers *r*1,<=*r*2,<=...,<=*r**n*, separated by spaces or new lines. Number *r**i* should equal 1, if Vasya's value in state *i* is interesting to the world and otherwise, it should equal 0. The states are numbered from 1 to *n* in the order, in which they are described in the input. | [
"4 3\n1 0 0 2\n1 2\n2 3\n3 4\n",
"3 1\n1 0 2\n1 3\n",
"3 1\n2 0 1\n1 3\n"
] | [
"1\n1\n1\n1\n",
"1\n0\n1\n",
"0\n0\n0\n"
] | In the first sample the program states can be used to make the only path in which the value of Vasya interests the world, 1 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> 2 <img align="middle" cla... | [
{
"input": "4 3\n1 0 0 2\n1 2\n2 3\n3 4",
"output": "1\n1\n1\n1"
},
{
"input": "3 1\n1 0 2\n1 3",
"output": "1\n0\n1"
},
{
"input": "3 1\n2 0 1\n1 3",
"output": "0\n0\n0"
},
{
"input": "4 4\n1 0 2 0\n1 2\n2 3\n3 4\n4 1",
"output": "1\n1\n1\n0"
},
{
"input": "2 1\n... | 248 | 614,400 | 0 | 51,874 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.