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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
768 | The Winds of Winter | [
"binary search",
"data structures"
] | null | null | Given a rooted tree with *n* nodes. The Night King removes exactly one node from the tree and all the edges associated with it. Doing this splits the tree and forms a forest. The node which is removed is not a part of the forest.
The root of a tree in the forest is the node in that tree which does not have a parent. W... | The first line of the input contains an integer *n* (1<=<=β€<=<=*n*<=<=β€<=<=105) β the number of vertices in the tree. Each of the next *n* lines contains a pair of vertex indices *u**i* and *v**i* (1<=<=β€<=<=*u**i*,<=<=*v**i*<=<=β€<=<=*n*) where *u**i* is the parent of *v**i*. If *u**i*<==<=0 then *v**i* is the root. | Print *n* line each containing a single integer. The *i*-th of them should be equal to minimum value of strength of forest formed when *i*-th node is removed and Jon Snow performs the operation described above at most once. | [
"10\n0 1\n1 2\n1 3\n1 4\n2 5\n2 6\n3 7\n4 8\n4 9\n5 10\n",
"2\n2 1\n0 2\n"
] | [
"3\n4\n5\n5\n5\n9\n9\n9\n9\n9\n",
"1\n1\n"
] | The tree for first test case is depicted below. <img class="tex-graphics" src="https://espresso.codeforces.com/11c39aaab54dce5e508a594b4f57109d5cc3b317.png" style="max-width: 100.0%;max-height: 100.0%;"/> When you remove the first node, the tree splits to form the following forest. The strength of this forest is 4. <im... | [] | 30 | 0 | 0 | 215,175 | |
163 | Large Refrigerator | [
"brute force"
] | null | null | Vasya wants to buy a new refrigerator. He believes that a refrigerator should be a rectangular parallelepiped with integer edge lengths. Vasya calculated that for daily use he will need a refrigerator with volume of at least *V*. Moreover, Vasya is a minimalist by nature, so the volume should be no more than *V*, eithe... | The first line contains a single integer *t* (1<=β€<=*t*<=β€<=500) β the number of data sets.
The description of *t* data sets follows. Each set consists of a single integer *V* (2<=β€<=*V*<=β€<=1018), given by its factorization as follows.
Let *V* = *p*1*a*1*p*2*a*2... *p**k**a**k*, where *p**i* are different prime numb... | Print *t* lines, on the *i*-th line print the answer to the *i*-th data set as four space-separated integers: the minimum possible surface area *S* and the corresponding edge lengths *a*, *b*, *c*. If there are multiple variants of the lengths of edges that give the minimum area, you are allowed to print any of them. Y... | [
"3\n1\n2 3\n1\n17 1\n3\n3 1\n2 3\n5 1\n"
] | [
"24 2 2 2\n70 1 1 17\n148 4 6 5\n"
] | In the first data set of the sample the fridge's volume *V*β=β2<sup class="upper-index">3</sup>β=β8, and the minimum surface area will be produced by the edges of equal length.
In the second data set the volume *V*β=β17, and it can be produced by only one set of integer lengths. | [] | 92 | 0 | 0 | 215,273 | |
243 | Matrix | [
"data structures"
] | null | null | Let's consider an *n*<=Γ<=*n* square matrix, consisting of digits one and zero.
We'll consider a matrix good, if it meets the following condition: in each row of the matrix all ones go in one group. That is, each row of the matrix looks like that 00...0011...1100...00 (or simply consists of zeroes if it has no ones).
... | The first line contains integer *n* (1<=β€<=*n*<=β€<=500) β the size of matrix *a*.
Each of *n* following lines contains *n* characters "0" and "1" β matrix *a*. Note that the characters are written without separators. | Print "YES" in the first line, if you can rearrange the matrix columns so as to get a good matrix *b*. In the next *n* lines print the good matrix *b*. If there are multiple answers, you are allowed to print any of them.
If it is impossible to get a good matrix, print "NO". | [
"6\n100010\n110110\n011001\n010010\n000100\n011001\n",
"3\n110\n101\n011\n"
] | [
"YES\n011000\n111100\n000111\n001100\n100000\n000111\n",
"NO\n"
] | none | [] | 62 | 0 | 0 | 215,871 | |
633 | The Chocolate Spree | [
"dfs and similar",
"dp",
"graphs",
"trees"
] | null | null | Alice and Bob have a tree (undirected acyclic connected graph). There are *a**i* chocolates waiting to be picked up in the *i*-th vertex of the tree. First, they choose two different vertices as their starting positions (Alice chooses first) and take all the chocolates contained in them.
Then, they alternate their mov... | The first line of the input contains the single integer *n* (2<=β€<=n<=β€<=100<=000)Β β the number of vertices in the tree.
The second line contains *n* integers *a**i* (1<=β€<=*a**i*<=β€<=109), *i*-th of these numbers stands for the number of chocolates stored at the node *i*.
Then follow *n*<=-<=1 lines that describe th... | Print the number of chocolates Alice and Bob can collect together if they behave optimally. | [
"9\n1 2 3 4 5 6 7 8 9\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n",
"2\n20 10\n1 2\n"
] | [
"25\n",
"30\n"
] | In the first sample, Alice may start at the vertex 9 and Bob at vertex 8. Alice will select vertex 1 and Bob has no options now. Alice selects the vertex 7 and they both stop.
In the second sample, both of them will pick either of the nodes alternately. | [] | 61 | 0 | 0 | 216,436 | |
766 | Mahmoud and a Dictionary | [
"data structures",
"dfs and similar",
"dp",
"dsu",
"graphs"
] | null | null | Mahmoud wants to write a new dictionary that contains *n* words and relations between them. There are two types of relations: synonymy (i.Β e. the two words mean the same) and antonymy (i.Β e. the two words mean the opposite). From time to time he discovers a new relation between two words.
He know that if two words hav... | The first line of input contains three integers *n*, *m* and *q* (2<=β€<=*n*<=β€<=105, 1<=β€<=*m*,<=*q*<=β€<=105) where *n* is the number of words in the dictionary, *m* is the number of relations Mahmoud figured out and *q* is the number of questions Mahmoud asked after telling all relations.
The second line contains *n*... | First, print *m* lines, one per each relation. If some relation is wrong (makes two words opposite and have the same meaning at the same time) you should print "NO" (without quotes) and ignore it, otherwise print "YES" (without quotes).
After that print *q* lines, one per each question. If the two words have the same ... | [
"3 3 4\nhate love like\n1 love like\n2 love hate\n1 hate like\nlove like\nlove hate\nlike hate\nhate like\n",
"8 6 5\nhi welcome hello ihateyou goaway dog cat rat\n1 hi welcome\n1 ihateyou goaway\n2 hello ihateyou\n2 hi goaway\n2 hi hello\n1 hi hello\ndog cat\ndog hi\nhi hello\nihateyou goaway\nwelcome ihateyou\n... | [
"YES\nYES\nNO\n1\n2\n2\n2\n",
"YES\nYES\nYES\nYES\nNO\nYES\n3\n3\n1\n1\n2\n"
] | none | [
{
"input": "3 3 4\nhate love like\n1 love like\n2 love hate\n1 hate like\nlove like\nlove hate\nlike hate\nhate like",
"output": "YES\nYES\nNO\n1\n2\n2\n2"
},
{
"input": "8 6 5\nhi welcome hello ihateyou goaway dog cat rat\n1 hi welcome\n1 ihateyou goaway\n2 hello ihateyou\n2 hi goaway\n2 hi hello\n... | 951 | 50,278,400 | 3 | 216,496 | |
231 | Magic, Wizardry and Wonders | [
"constructive algorithms",
"greedy"
] | null | null | Vasya the Great Magician and Conjurer loves all kinds of miracles and wizardry. In one wave of a magic wand he can turn an object into something else. But, as you all know, there is no better magic in the Universe than the magic of numbers. That's why Vasya adores math and spends a lot of time turning some numbers into... | The single line contains three space-separated integers: *n* (2<=β€<=*n*<=β€<=100) β the initial number of cards on the table, *d* (|*d*|<=β€<=104) β the number on the card that was left on the table after all the magical actions, and *l* (1<=β€<=*l*<=β€<=100) β the limits for the initial integers. | If Vasya is mistaken, that is, if there doesn't exist a set that meets the requirements given in the statement, then print a single number -1, otherwise print the sought set containing *n* integers from 1 to *l*. Separate the integers by spaces. Print the integers in the order, in which they were written on the cards f... | [
"3 3 2\n",
"5 -4 3\n",
"5 -4 4\n"
] | [
"2 1 2 ",
"-1\n",
"2 4 1 4 1 "
] | none | [
{
"input": "3 3 2",
"output": "2 1 2 "
},
{
"input": "5 -4 3",
"output": "-1"
},
{
"input": "5 -4 4",
"output": "2 4 1 4 1 "
},
{
"input": "2 0 10",
"output": "10 10 "
},
{
"input": "5 2 2",
"output": "2 2 2 1 1 "
},
{
"input": "7 -24 10",
"output"... | 186 | 0 | 3 | 216,773 | |
788 | New task | [
"data structures"
] | null | null | On the 228-th international Uzhlyandian Wars strategic game tournament teams from each country are called. The teams should consist of 5 participants.
The team of Uzhlyandia will consist of soldiers, because there are no gamers.
Masha is a new minister of defense and gaming. The prime duty of the minister is to calcu... | The first line contains single integer *n* (1<=β€<=*n*<=β€<=105)Β β the number of soldiers in Uzhlyandia.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109)Β β the soldiers' skills.
The third line contains single integer *m* (1<=β€<=*m*<=β€<=105)Β β the number of changes.
The next *m* l... | Print *m* integersΒ β the number of distinct teams after each change.
Print the answers modulo 1000000007 (109<=+<=7). | [
"6\n1 1 1 1 1 1\n2\n1 3\n2 3\n",
"8\n3 4 4 2 4 5 4 1\n3\n1 5\n2 5\n1 2\n"
] | [
"1\n6\n",
"1\n6\n2\n"
] | In the first example, after the first change the only team consists of soldiers [1,β2,β4,β5,β6]. After the second change any five soldiers can form a team.
In the first example after the first change the only team is soldiers [1,β2,β3,β7,β8]. After the second change the possible teams are: [1,β2,β3,β5,β7], [1,β2,β3,β5... | [] | 46 | 0 | 0 | 216,876 | |
802 | Marmots (medium) | [
"math"
] | null | null | Good job! Now that Heidi is able to distinguish between Poisson and uniform distributions, she is in a good position to actually estimate the populations.
Can you help Heidi estimate each village's population? | Same as the easy version. | Output one line per village, in the same order as provided in the input, containing your (integer) population estimate.
Your answer is considered correct if it is an integer that falls into the interval , where *P* is the real population of the village, used to create the distribution (either Poisson or uniform) from ... | [] | [] | none | [] | 92 | 1,638,400 | 0 | 217,639 | |
888 | Connecting Vertices | [
"dp",
"graphs"
] | null | null | There are *n* points marked on the plane. The points are situated in such a way that they form a regular polygon (marked points are its vertices, and they are numbered in counter-clockwise order). You can draw *n*<=-<=1 segments, each connecting any two marked points, in such a way that all points have to be connected ... | The first line contains one number *n* (3<=β€<=*n*<=β€<=500) β the number of marked points.
Then *n* lines follow, each containing *n* elements. *a**i*,<=*j* (*j*-th element of line *i*) is equal to 1 iff you can connect points *i* and *j* directly (otherwise *a**i*,<=*j*<==<=0). It is guaranteed that for any pair of po... | Print the number of ways to connect points modulo 109<=+<=7. | [
"3\n0 0 1\n0 0 1\n1 1 0\n",
"4\n0 1 1 1\n1 0 1 1\n1 1 0 1\n1 1 1 0\n",
"3\n0 0 0\n0 0 1\n0 1 0\n"
] | [
"1\n",
"12\n",
"0\n"
] | none | [
{
"input": "3\n0 0 1\n0 0 1\n1 1 0",
"output": "1"
},
{
"input": "4\n0 1 1 1\n1 0 1 1\n1 1 0 1\n1 1 1 0",
"output": "12"
},
{
"input": "3\n0 0 0\n0 0 1\n0 1 0",
"output": "0"
},
{
"input": "4\n0 0 0 1\n0 0 0 0\n0 0 0 1\n1 0 1 0",
"output": "0"
},
{
"input": "4\n0 ... | 46 | 0 | 0 | 218,079 | |
335 | More Reclamation | [
"games"
] | null | null | In a far away land, there are two cities near a river. One day, the cities decide that they have too little space and would like to reclaim some of the river area into land.
The river area can be represented by a grid with *r* rows and exactly two columns β each cell represents a rectangular area. The rows are numbere... | The first line consists of two integers *r* and *n* (1<=β€<=*r*<=β€<=100,<=0<=β€<=*n*<=β€<=*r*). Then *n* lines follow, describing the cells that were already reclaimed. Each line consists of two integers: *r**i* and *c**i* (1<=β€<=*r**i*<=β€<=*r*,<=1<=β€<=*c**i*<=β€<=2), which represent the cell located at row *r**i* and colu... | Output "WIN" if the city whose turn it is to choose a cell can guarantee that they will be the last to choose a cell. Otherwise print "LOSE". | [
"3 1\n1 1\n",
"12 2\n4 1\n8 1\n",
"1 1\n1 2\n"
] | [
"WIN\n",
"WIN\n",
"LOSE\n"
] | In the first example, there are 3 possible cells for the first city to reclaim: (2,β1), (3,β1), or (3,β2). The first two possibilities both lose, as they leave exactly one cell for the other city.
However, reclaiming the cell at (3,β2) leaves no more cells that can be reclaimed, and therefore the first city wins.
In ... | [
{
"input": "3 1\n1 1",
"output": "WIN"
},
{
"input": "12 2\n4 1\n8 1",
"output": "WIN"
},
{
"input": "1 1\n1 2",
"output": "LOSE"
},
{
"input": "4 0",
"output": "LOSE"
},
{
"input": "100 44\n41 1\n13 1\n52 1\n83 1\n64 2\n86 2\n12 1\n77 1\n100 2\n97 2\n58 1\n33 2\n... | 62 | 0 | 0 | 218,080 | |
734 | Anton and Making Potions | [
"binary search",
"dp",
"greedy",
"two pointers"
] | null | null | Anton is playing a very interesting computer game, but now he is stuck at one of the levels. To pass to the next level he has to prepare *n* potions.
Anton has a special kettle, that can prepare one potions in *x* seconds. Also, he knows spells of two types that can faster the process of preparing potions.
1. Spells... | The first line of the input contains three integers *n*, *m*, *k* (1<=β€<=*n*<=β€<=2Β·109,<=1<=β€<=*m*,<=*k*<=β€<=2Β·105)Β β the number of potions, Anton has to make, the number of spells of the first type and the number of spells of the second type.
The second line of the input contains two integers *x* and *s* (2<=β€<=*x*<=... | Print one integerΒ β the minimum time one has to spent in order to prepare *n* potions. | [
"20 3 2\n10 99\n2 4 3\n20 10 40\n4 15\n10 80\n",
"20 3 2\n10 99\n2 4 3\n200 100 400\n4 15\n100 800\n"
] | [
"20\n",
"200\n"
] | In the first sample, the optimum answer is to use the second spell of the first type that costs 10 manapoints. Thus, the preparation time of each potion changes to 4 seconds. Also, Anton should use the second spell of the second type to instantly prepare 15 potions spending 80 manapoints. The total number of manapoints... | [
{
"input": "20 3 2\n10 99\n2 4 3\n20 10 40\n4 15\n10 80",
"output": "20"
},
{
"input": "20 3 2\n10 99\n2 4 3\n200 100 400\n4 15\n100 800",
"output": "200"
},
{
"input": "10 3 3\n10 33\n1 7 6\n17 25 68\n2 9 10\n78 89 125",
"output": "10"
},
{
"input": "94 1 1\n26 324\n7\n236\n... | 46 | 0 | 0 | 218,255 | |
241 | Challenging Balloons | [
"constructive algorithms"
] | null | null | Martha β as a professional problemsetter β proposed a problem for a world-class contest. This is the problem statement:
Tomorrow is Nadia's birthday, and Bardia (her brother) is assigned to make the balloons ready!
There are *n* balloons (initially empty) that are tied to a straight line on certain positions *x*1,<=*... | Please pay attention! No input will be given to your program for this problem. So you do not have to read from the input anything. | You should output the generated small testcase (which Artha's solution doesn't get it right). It should be in the following format:
- First line must contain the only number *n* (1<=β€<=*n*<=β€<=500). - The *i*-th of the next *n* lines should contain the description of the *i*-th balloon β two space-separated integers... | [] | [] | The testcase depicted in the figure above (just showing how output should be formatted): | [] | 62 | 0 | 0 | 218,363 | |
0 | none | [
"none"
] | null | null | Bearland is a dangerous place. Limak canβt travel on foot. Instead, he has *k* magic teleportation stones. Each stone can be used at most once. The *i*-th stone allows to teleport to a point (*ax**i*,<=*ay**i*). Limak can use stones in any order.
There are *n* monsters in Bearland. The *i*-th of them stands at (*mx**i... | The first line of the input contains two integers *k* and *n* (1<=β€<=*k*<=β€<=7, 1<=β€<=*n*<=β€<=1000)Β β the number of stones and the number of monsters.
The *i*-th of following *k* lines contains two integers *ax**i* and *ay**i* (<=-<=109<=β€<=*ax**i*,<=*ay**i*<=β€<=109)Β β coordinates to which Limak can teleport using the... | Print the number of monsters which should be afraid of Limak. | [
"2 4\n-2 -1\n4 5\n4 2\n2 1\n4 -1\n1 -1\n",
"3 8\n10 20\n0 0\n20 40\n300 600\n30 60\n170 340\n50 100\n28 56\n90 180\n-4 -8\n-1 -2\n"
] | [
"3\n",
"5\n"
] | In the first sample, there are two stones and four monsters. Stones allow to teleport to points (β-β2,ββ-β1) and (4,β5), marked blue in the drawing below. Monsters are at (4,β2), (2,β1), (4,ββ-β1) and (1,ββ-β1), marked red. A monster at (4,ββ-β1) shouldn't be afraid because it's impossible that Limak will hit it with a... | [] | 62 | 0 | 0 | 219,536 | |
0 | none | [
"none"
] | null | null | There are $n$ startups. Startups can be active or acquired. If a startup is acquired, then that means it has exactly one active startup that it is following. An active startup can have arbitrarily many acquired startups that are following it. An active startup cannot follow any other startup.
The following steps happe... | The first line contains a single integer $n$ ($2 \leq n \leq 500$), the number of startups.
The next line will contain $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($a_i = -1$ or $1 \leq a_i \leq n$). If $a_i = -1$, then that means startup $i$ is active. Otherwise, if $1 \leq a_i \leq n$, then startup $i$ is a... | Print a single integer, the expected number of days needed for the process to end with exactly one active startup, modulo $10^9+7$. | [
"3\n-1 -1 -1\n",
"2\n2 -1\n",
"40\n3 3 -1 -1 4 4 -1 -1 -1 -1 -1 10 10 10 10 10 10 4 20 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 3 3 3 3 3 3 3 3\n"
] | [
"3\n",
"0\n",
"755808950\n"
] | In the first sample, there are three active startups labeled $1$, $2$ and $3$, and zero acquired startups. Here's an example of how one scenario can happen
1. Startup $1$ acquires startup $2$ (This state can be represented by the array $[-1, 1, -1]$) 1. Startup $3$ acquires startup $1$ (This state can be represented... | [
{
"input": "3\n-1 -1 -1",
"output": "3"
},
{
"input": "2\n2 -1",
"output": "0"
},
{
"input": "40\n3 3 -1 -1 4 4 -1 -1 -1 -1 -1 10 10 10 10 10 10 4 20 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 3 3 3 3 3 3 3 3",
"output": "755808950"
},
{
"input": "8\n-1 3 -1 -1 -1 3 -1 -1",
"... | 124 | 0 | 3 | 220,626 | |
269 | String Theory | [
"geometry",
"math",
"strings"
] | null | null | Emuskald is an innovative musician and always tries to push the boundaries of music production. Now he has come up with an idea for a revolutionary musical instrument β a rectangular harp.
A rectangular harp is a rectangle *n*<=Γ<=*m* consisting of *n* rows and *m* columns. The rows are numbered 1 to *n* from top to b... | The first line of input contains two space-separated integers numbers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=105), the height and width of the harp in units. Each of the following *n*<=+<=*m* lines contains 4 space-separated tokens, describing a single string: two symbols *a**i*, *b**i* and two integer numbers *p**i*, *q**i*... | If it is possible to rearrange the rows and columns to fix the harp, on the first line output *n* space-separated integers β the old numbers of rows now placed from top to bottom in the fixed harp. On the second line, output *m* space-separated integers β the old numbers of columns now placed from left to right in the ... | [
"3 4\nL T 1 3\nL B 2 2\nL B 3 3\nT R 1 2\nT B 2 1\nT R 4 1\nB R 4 3\n",
"3 3\nL T 1 1\nT R 3 1\nR B 3 3\nB L 1 3\nL R 2 2\nT B 2 2\n"
] | [
"1 2 3 \n3 2 1 4 \n",
"No solution\n"
] | none | [] | 92 | 0 | 0 | 220,914 | |
734 | Anton and Tree | [
"dfs and similar",
"dp",
"trees"
] | null | null | Anton is growing a tree in his garden. In case you forgot, the tree is a connected acyclic undirected graph.
There are *n* vertices in the tree, each of them is painted black or white. Anton doesn't like multicolored trees, so he wants to change the tree such that all vertices have the same color (black or white).
To... | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=200<=000)Β β the number of vertices in the tree.
The second line contains *n* integers *color**i* (0<=β€<=*color**i*<=β€<=1)Β β colors of the vertices. *color**i*<==<=0 means that the *i*-th vertex is initially painted white, while *color**i*<==<=1 m... | Print one integerΒ β the minimum number of operations Anton has to apply in order to make all vertices of the tree black or all vertices of the tree white. | [
"11\n0 0 0 1 1 0 1 0 0 1 1\n1 2\n1 3\n2 4\n2 5\n5 6\n5 7\n3 8\n3 9\n3 10\n9 11\n",
"4\n0 0 0 0\n1 2\n2 3\n3 4\n"
] | [
"2\n",
"0\n"
] | In the first sample, the tree is the same as on the picture. If we first apply operation *paint*(3) and then apply *paint*(6), the tree will become completely black, so the answer is 2.
In the second sample, the tree is already white, so there is no need to apply any operations and the answer is 0. | [
{
"input": "11\n0 0 0 1 1 0 1 0 0 1 1\n1 2\n1 3\n2 4\n2 5\n5 6\n5 7\n3 8\n3 9\n3 10\n9 11",
"output": "2"
},
{
"input": "4\n0 0 0 0\n1 2\n2 3\n3 4",
"output": "0"
},
{
"input": "15\n0 1 0 0 1 1 0 1 1 1 1 1 0 1 0\n10 7\n10 3\n10 8\n5 7\n13 14\n8 13\n15 4\n15 13\n5 2\n9 3\n11 15\n13 6\n1 1... | 0 | 0 | -1 | 222,405 | |
377 | Preparing for the Contest | [
"binary search",
"data structures",
"greedy",
"sortings"
] | null | null | Soon there will be held the world's largest programming contest, but the testing system still has *m* bugs. The contest organizer, a well-known university, has no choice but to attract university students to fix all the bugs. The university has *n* students able to perform such work. The students realize that they are ... | The first line contains three space-separated integers: *n*, *m* and *s* (1<=β€<=*n*,<=*m*<=β€<=105, 0<=β€<=*s*<=β€<=109)Β β the number of students, the number of bugs in the system and the maximum number of passes the university is ready to give the students.
The next line contains *m* space-separated integers *a*1, *a*2,... | If the university can't correct all bugs print "NO".
Otherwise, on the first line print "YES", and on the next line print *m* space-separated integers: the *i*-th of these numbers should equal the number of the student who corrects the *i*-th bug in the optimal answer. The bugs should be corrected as quickly as possib... | [
"3 4 9\n1 3 1 2\n2 1 3\n4 3 6\n",
"3 4 10\n2 3 1 2\n2 1 3\n4 3 6\n",
"3 4 9\n2 3 1 2\n2 1 3\n4 3 6\n",
"3 4 5\n1 3 1 2\n2 1 3\n5 3 6\n"
] | [
"YES\n2 3 2 3\n",
"YES\n1 3 1 3\n",
"YES\n3 3 2 3\n",
"NO\n"
] | Consider the first sample.
The third student (with level 3) must fix the 2nd and 4th bugs (complexities 3 and 2 correspondingly) and the second student (with level 1) must fix the 1st and 3rd bugs (their complexity also equals 1). Fixing each bug takes one day for each student, so it takes 2 days to fix all bugs (the ... | [
{
"input": "3 4 9\n1 3 1 2\n2 1 3\n4 3 6",
"output": "YES\n2 3 2 3"
},
{
"input": "3 4 10\n2 3 1 2\n2 1 3\n4 3 6",
"output": "YES\n1 3 1 3"
},
{
"input": "3 4 9\n2 3 1 2\n2 1 3\n4 3 6",
"output": "YES\n3 3 2 3"
},
{
"input": "3 4 5\n1 3 1 2\n2 1 3\n5 3 6",
"output": "NO"
... | 2,000 | 24,166,400 | 0 | 222,840 | |
258 | Little Elephant and Broken Sorting | [
"dp",
"math",
"probabilities"
] | null | null | The Little Elephant loves permutations of integers from 1 to *n* very much. But most of all he loves sorting them. To sort a permutation, the Little Elephant repeatedly swaps some elements. As a result, he must receive a permutation 1,<=2,<=3,<=...,<=*n*.
This time the Little Elephant has permutation *p*1,<=*p*2,<=...... | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=1000,<=*n*<=><=1) β the permutation size and the number of moves. The second line contains *n* distinct integers, not exceeding *n* β the initial permutation. Next *m* lines each contain two integers: the *i*-th line contains integers *a**i* and *... | In the only line print a single real number β the answer to the problem. The answer will be considered correct if its relative or absolute error does not exceed 10<=-<=6. | [
"2 1\n1 2\n1 2\n",
"4 3\n1 3 2 4\n1 2\n2 3\n1 4\n"
] | [
"0.500000000\n",
"3.000000000\n"
] | none | [
{
"input": "2 1\n1 2\n1 2",
"output": "0.500000000"
},
{
"input": "4 3\n1 3 2 4\n1 2\n2 3\n1 4",
"output": "3.000000000"
},
{
"input": "7 4\n7 6 4 2 1 5 3\n1 3\n2 1\n7 2\n3 5",
"output": "11.250000000"
},
{
"input": "10 1\n1 2 3 4 5 6 7 8 9 10\n1 10",
"output": "8.5000000... | 218 | 307,200 | 0 | 222,865 | |
534 | Simplified Nonogram | [
"bitmasks",
"dp",
"hashing",
"meet-in-the-middle"
] | null | null | In this task you have to write a program dealing with nonograms on fields no larger than 5<=Γ<=20.
Simplified nonogram is a task where you have to build such field (each cell is either white or black) that satisfies the given information about rows and columns. For each row and each column the number of contiguous bla... | In the first line there follow two integers *n*, *m* (1<=β€<=*n*<=β€<=5,<=1<=β€<=*m*<=β€<=20) β number of rows and number of columns respectively.
Second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* where *a**i* is the number of contiguous black segments in *i*-th row of the field.
Similarly, third line contain... | Output any possible solution. Output should consist of *n* lines each containing *m* characters. Denote white cell as "." and black cell as "*". | [
"3 5\n2 3 2\n1 0 1 2 1\n",
"3 3\n2 1 2\n2 1 2\n",
"3 3\n1 0 1\n2 2 2\n"
] | [
"*.**.\n*.*.*\n*..**",
"*.*\n.*.\n*.*\n",
"***\n...\n***\n"
] | none | [
{
"input": "3 5\n2 3 2\n1 0 1 2 1",
"output": "*..**\n*.*.*\n*..**"
},
{
"input": "3 3\n2 1 2\n2 1 2",
"output": "*.*\n.*.\n*.*"
},
{
"input": "3 3\n1 0 1\n2 2 2",
"output": "***\n...\n***"
},
{
"input": "1 1\n1\n1",
"output": "*"
},
{
"input": "1 1\n0\n0",
"o... | 46 | 0 | 0 | 222,876 | |
761 | Dasha and Photos | [
"brute force",
"data structures",
"dp",
"implementation"
] | null | null | Dasha decided to have a rest after solving the problem *D* and began to look photos from previous competitions.
Let's call photos as the matrix with the size *n*<=Γ<=*m*, which consists of lowercase English letters.
Some *k* photos especially interested her, because they can be received from photo-template by paintin... | The first line contains three integers *n*, *m*, *k* (1<=β€<=*n*,<=*m*<=β€<=103,<=1<=β€<=*k*<=β€<=3Β·105) β the number of strings in the photo-template, the number of columns and the number of special photos which are interesting for Dasha.
The next *n* lines contains the string with *m* length which consists of little La... | In the only line print the minimum total distance from the found special photo to all other special photos. | [
"3 3 2\naaa\naaa\naaa\n1 1 2 2 b\n2 2 3 3 c\n",
"5 5 3\nabcde\neabcd\ndeabc\ncdeab\nbcdea\n1 1 3 4 f\n1 2 3 3 e\n1 3 3 4 i\n"
] | [
"10\n",
"59\n"
] | In the first example the photos are following:
The distance between them is 10. | [] | 46 | 0 | 0 | 223,555 | |
848 | Shake It! | [
"combinatorics",
"dp",
"flows",
"graphs"
] | null | null | A never-ending, fast-changing and dream-like world unfolds, as the secret door opens.
A world is an unordered graph *G*, in whose vertex set *V*(*G*) there are two special vertices *s*(*G*) and *t*(*G*). An initial world has vertex set {*s*(*G*),<=*t*(*G*)} and an edge between them.
A total of *n* changes took place ... | The first and only line of input contains two space-separated integers *n*, *m* (1<=β€<=*n*,<=*m*<=β€<=50) β the number of operations performed and the minimum cut, respectively. | Output one integer β the number of non-similar worlds that can be built, modulo 109<=+<=7. | [
"3 2\n",
"4 4\n",
"7 3\n",
"31 8\n"
] | [
"6\n",
"3\n",
"1196\n",
"64921457\n"
] | In the first example, the following 6 worlds are pairwise non-similar and satisfy the constraints, with *s*(*G*) marked in green, *t*(*G*) marked in blue, and one of their minimum cuts in light blue.
In the second example, the following 3 worlds satisfy the constraints. | [
{
"input": "3 2",
"output": "6"
},
{
"input": "4 4",
"output": "3"
},
{
"input": "7 3",
"output": "1196"
},
{
"input": "31 8",
"output": "64921457"
},
{
"input": "1 1",
"output": "0"
},
{
"input": "10 2",
"output": "141356"
},
{
"input": "3... | 545 | 8,908,800 | 3 | 223,667 | |
720 | Closing ceremony | [
"greedy"
] | null | null | The closing ceremony of Squanch Code Cup is held in the big hall with *n*<=Γ<=*m* seats, arranged in *n* rows, *m* seats in a row. Each seat has two coordinates (*x*,<=*y*) (1<=β€<=*x*<=β€<=*n*, 1<=β€<=*y*<=β€<=*m*).
There are two queues of people waiting to enter the hall: *k* people are standing at (0,<=0) and *n*Β·*m*<... | The first line of input contains two integers *n* and *m* (1<=β€<=*n*Β·*m*<=β€<=104)Β β the size of the hall.
The second line contains several integers. The first integer *k* (0<=β€<=*k*<=β€<=*n*Β·*m*)Β β the number of people at (0,<=0). The following *k* integers indicate stamina of each person there.
The third line also co... | If it is possible to distribute tickets between people in the described manner print "YES", otherwise print "NO". | [
"2 2\n3 3 3 2\n1 3\n",
"2 2\n3 2 3 3\n1 2\n"
] | [
"YES\n",
"NO\n"
] | none | [
{
"input": "2 2\n3 3 3 2\n1 3",
"output": "YES"
},
{
"input": "2 2\n3 2 3 3\n1 2",
"output": "NO"
},
{
"input": "1 2\n2 2 3\n0",
"output": "YES"
},
{
"input": "1 1\n1 2\n0",
"output": "YES"
},
{
"input": "1 1\n0\n1 1",
"output": "NO"
}
] | 124 | 23,552,000 | 0 | 225,076 | |
575 | Bulbo | [
"dp",
"greedy"
] | null | null | Bananistan is a beautiful banana republic. Beautiful women in beautiful dresses. Beautiful statues of beautiful warlords. Beautiful stars in beautiful nights.
In Bananistan people play this crazy game β Bulbo. Thereβs an array of bulbs and player at the position, which represents one of the bulbs. The distance between... | The first line contains number of turns *n* and initial position *x*. Next *n* lines contain two numbers *l**start* and *l**end*, which represent that all bulbs from interval [*l**start*,<=*l**end*] are shining this turn.
- 1<=β€<=*n*<=β€<=5000 - 1<=β€<=*x*<=β€<=109 - 1<=β€<=*l**start*<=β€<=*l**end*<=β€<=109 | Output should contain a single number which represents the best result (minimum cost) that could be obtained by playing this Bulbo game. | [
"5 4\n2 7\n9 16\n8 10\n9 17\n1 6\n"
] | [
"8\n"
] | Before 1. turn move to position 5
Before 2. turn move to position 9
Before 5. turn move to position 8 | [] | 31 | 0 | 0 | 225,132 | |
546 | Soldier and Cards | [
"brute force",
"dfs and similar",
"games"
] | null | null | Two bored soldiers are playing card war. Their card deck consists of exactly *n* cards, numbered from 1 to *n*, all values are different. They divide cards between them in some manner, it's possible that they have different number of cards. Then they play a "war"-like card game.
The rules are following. On each turn ... | First line contains a single integer *n* (2<=β€<=*n*<=β€<=10), the number of cards.
Second line contains integer *k*1 (1<=β€<=*k*1<=β€<=*n*<=-<=1), the number of the first soldier's cards. Then follow *k*1 integers that are the values on the first soldier's cards, from top to bottom of his stack.
Third line contains inte... | If somebody wins in this game, print 2 integers where the first one stands for the number of fights before end of game and the second one is 1 or 2 showing which player has won.
If the game won't end and will continue forever output <=-<=1. | [
"4\n2 1 3\n2 4 2\n",
"3\n1 2\n2 1 3\n"
] | [
"6 2",
"-1"
] | First sample:
Second sample: | [
{
"input": "4\n2 1 3\n2 4 2",
"output": "6 2"
},
{
"input": "3\n1 2\n2 1 3",
"output": "-1"
},
{
"input": "5\n4 5 3 2 4\n1 1",
"output": "1 1"
},
{
"input": "6\n2 6 5\n4 1 2 3 4",
"output": "6 1"
},
{
"input": "7\n6 6 5 2 7 4 1\n1 3",
"output": "1 1"
},
{
... | 77 | 2,048,000 | 3 | 225,667 | |
140 | New Year Snowflake | [
"geometry",
"sortings"
] | null | null | As Gerald ..., in other words, on a New Year Eve Constantine prepared an unusual present for the Beautiful Lady. The present is the magic New Year snowflake that can make any dream come true.
The New Year snowflake consists of tiny ice crystals, which can be approximately regarded as points on the plane. The beauty of... | The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=200<=000, 0<=β€<=*k*<=β€<=10) β the number of the surviving snowflake crystals and the maximum number of melted crystals, correspondingly. Next *n* lines contain the coordinates of the crystals that are left in the following form: "*x**i* *y**i*". The coordi... | The first line contains an integer *c* β the number of possible symmetry centers. Next *c* lines should contain the centers' descriptions. Each symmetry center is described by a couple of coordinates "*x* *y*", separated by a space. Print the coordinates with absolute error not exceeding 10<=-<=6. You are allowed to pr... | [
"4 0\n0 0\n0 1\n1 0\n1 1\n",
"4 2\n0 0\n0 1\n1 0\n1 1\n",
"4 4\n0 0\n0 1\n1 0\n1 1\n"
] | [
"1\n0.5 0.5\n",
"5\n0.0 0.5\n0.5 0.0\n0.5 0.5\n0.5 1.0\n1.0 0.5\n",
"-1\n"
] | none | [] | 30 | 0 | 0 | 226,028 | |
444 | DZY Loves FFT | [
"probabilities"
] | null | null | DZY loves Fast Fourier Transformation, and he enjoys using it.
Fast Fourier Transformation is an algorithm used to calculate convolution. Specifically, if *a*, *b* and *c* are sequences with length *n*, which are indexed from 0 to *n*<=-<=1, and
We can calculate *c* fast using Fast Fourier Transformation.
DZY made a... | The only line of input contains three space-separated integers *n*,<=*d*,<=*x*Β (1<=β€<=*d*<=β€<=*n*<=β€<=100000;Β 0<=β€<=*x*<=β€<=1000000006). Because DZY is naughty, *x* can't be equal to 27777500. | Output *n* lines, the *i*-th line should contain an integer *c**i*<=-<=1. | [
"3 1 1\n",
"5 4 2\n",
"5 4 3\n"
] | [
"1\n3\n2\n",
"2\n2\n4\n5\n5\n",
"5\n5\n5\n5\n4\n"
] | In the first sample, *a* is [1 3 2], *b* is [1 0 0], so *c*<sub class="lower-index">0</sub>β=β*max*(1Β·1)β=β1, *c*<sub class="lower-index">1</sub>β=β*max*(1Β·0,β3Β·1)β=β3, *c*<sub class="lower-index">2</sub>β=β*max*(1Β·0,β3Β·0,β2Β·1)β=β2.
In the second sample, *a* is [2 1 4 5 3], *b* is [1 1 1 0 1].
In the third sample, *a... | [] | 30 | 0 | 0 | 226,283 | |
46 | Emperor's Problem | [
"geometry"
] | G. Emperor's Problem | 2 | 256 | It happened at the times of the Great Berland Empire. Once the Emperor dreamt that the Messenger from the gods ordered to build a temple whose base would be a convex polygon with *n* angles. Next morning the Emperor gave the command to build a temple whose base was a regular polygon with *n* angles. The temple was buil... | The first line contains the single number *n* (3<=β€<=*n*<=β€<=10000). | Print "YES" (without quotes) in the first line if it is possible to build a polygon possessing the needed qualities. In the next *n* lines print integer coordinates of the polygon vertices in the order in which they would be passed counter clockwise. The absolute value of the coordinates shouldn't exceed 109. No two ve... | [
"3\n",
"3\n"
] | [
"YES\n0 0\n1 0\n0 2\n",
"YES\n0 1\n-1 0\n-1 -1\n"
] | none | [] | 108 | 0 | 0 | 226,325 |
698 | Cron | [] | null | null | Sometime the classic solution are not powerful enough and we have to design our own. For the purpose of this problem you have to implement the part of the system of task scheduling.
Each task should be executed at some particular moments of time. In our system you may set the exact value for the second, minute, hour, ... | The first line of the input contains six integers *s*, *m*, *h*, *day*, *date* and *month* (0<=β€<=*s*,<=*m*<=β€<=59, 0<=β€<=*h*<=β€<=23, 1<=β€<=*day*<=β€<=7, 1<=β€<=*date*<=β€<=31, 1<=β€<=*month*<=β€<=12). Each of the number can also be equal to <=-<=1. It's guaranteed, that there are infinitely many moments of time when this t... | Print *n* lines, the *i*-th of them should contain the first moment of time strictly greater than *t**i*, when the task should be executed. | [
"-1 59 23 -1 -1 -1\n6\n1467372658\n1467417540\n1467417541\n1467417598\n1467417599\n1467417600\n",
"0 0 12 6 3 7\n3\n1467372658\n1467460810\n1467547200\n"
] | [
"1467417540\n1467417541\n1467417542\n1467417599\n1467503940\n1467503940\n",
"1467460800\n1467547200\n1468065600\n"
] | The moment of time 1467372658 after the midnight of January 1st, 1970 is 11:30:58 July 1st, 2016. | [
{
"input": "-1 59 23 -1 -1 -1\n6\n1467372658\n1467417540\n1467417541\n1467417598\n1467417599\n1467417600",
"output": "1467417540\n1467417541\n1467417542\n1467417599\n1467503940\n1467503940"
},
{
"input": "0 0 12 6 3 7\n3\n1467372658\n1467460810\n1467547200",
"output": "1467460800\n1467547200\n14... | 249 | 11,673,600 | -1 | 227,032 | |
362 | Insertion Sort | [
"data structures",
"dp",
"implementation",
"math"
] | null | null | Petya is a beginner programmer. He has already mastered the basics of the C++ language and moved on to learning algorithms. The first algorithm he encountered was insertion sort. Petya has already written the code that implements this algorithm and sorts the given integer zero-indexed array *a* of size *n* in the non-d... | The first line contains a single integer *n* (2<=β€<=*n*<=β€<=5000) β the length of the permutation. The second line contains *n* different integers from 0 to *n*<=-<=1, inclusive β the actual permutation. | Print two integers: the minimum number of times the swap function is executed and the number of such pairs (*i*,<=*j*) that swapping the elements of the input permutation with indexes *i* and *j* leads to the minimum number of the executions. | [
"5\n4 0 3 1 2\n",
"5\n1 2 3 4 0\n"
] | [
"3 2\n",
"3 4\n"
] | In the first sample the appropriate pairs are (0,β3) and (0,β4).
In the second sample the appropriate pairs are (0,β4), (1,β4), (2,β4) and (3,β4). | [
{
"input": "5\n4 0 3 1 2",
"output": "3 2"
},
{
"input": "5\n1 2 3 4 0",
"output": "3 4"
},
{
"input": "5\n1 3 4 0 2",
"output": "4 5"
},
{
"input": "10\n9 8 7 6 5 4 3 2 1 0",
"output": "28 1"
},
{
"input": "5\n0 4 1 3 2",
"output": "1 1"
},
{
"input":... | 1,434 | 10,444,800 | 3 | 227,267 | |
145 | Lucky Pair | [
"combinatorics",
"data structures",
"implementation"
] | null | null | Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya has an array *a* of *n* integers. The numbers in the array are numbered starting from 1. Unf... | The first line contains an integer *n* (2<=β€<=*n*<=β€<=105) β the size of the array *a*. The second line contains *n* space-separated integers *a**i* (1<=β€<=*a**i*<=β€<=109) β array *a*. It is guaranteed that no more than 1000 elements in the array *a* are lucky numbers. | On the single line print the only number β the answer to the problem.
Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator. | [
"4\n1 4 2 4\n",
"2\n4 7\n",
"4\n4 4 7 7\n"
] | [
"9\n",
"1\n",
"9\n"
] | The subarray *a*[*l*..*r*] is an array that consists of elements *a*<sub class="lower-index">*l*</sub>, *a*<sub class="lower-index">*l*β+β1</sub>, ..., *a*<sub class="lower-index">*r*</sub>.
In the first sample there are 9 possible pairs that satisfy the condition: [1,β1] and [2,β2], [1,β1] and [2,β3], [1,β1] and [2,β... | [] | 62 | 0 | 0 | 227,580 | |
249 | Endless Matrix | [
"math"
] | null | null | A Russian space traveller Alisa Selezneva, like any other schoolgirl of the late 21 century, is interested in science. She has recently visited the MIT (Moscow Institute of Time), where its chairman and the co-inventor of the time machine academician Petrov told her about the construction of a time machine.
During the... | The first input line contains a single integer *t* (1<=β€<=*t*<=β€<=105) β the number of test sets for which you should solve the problem.
Each of the next *t* lines contains the description of a test β four positive integers *x*1,<=*y*1,<=*x*2 and *y*2 (1<=β€<=*x*1<=β€<=*x*2<=β€<=109,<=1<=β€<=*y*1<=β€<=*y*2<=β€<=109), separ... | For each query print the meaning of the expression if it contains at most 10 characters. Otherwise, print three characters "." (without the quotes), and then ten last digits of the time expression. Print the answer to each query on a single line. Follow the format, given in the sample as closely as possible. | [
"5\n1 1 1 1\n2 2 3 3\n2 3 5 6\n100 87 288 2002\n4 2 5 4\n"
] | [
"1\n24\n300\n...5679392764\n111\n"
] | none | [] | 60 | 0 | 0 | 227,701 | |
0 | none | [
"none"
] | null | null | Little Elephant loves Furik and Rubik, who he met in a small city Kremenchug.
The Little Elephant has two strings of equal length *a* and *b*, consisting only of uppercase English letters. The Little Elephant selects a pair of substrings of equal length β the first one from string *a*, the second one from string *b*. ... | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=2Β·105) β the length of strings *a* and *b*. The second line contains string *a*, the third line contains string *b*. The strings consist of uppercase English letters only. The length of both strings equals *n*. | On a single line print a real number β the answer to the problem. The answer will be considered correct if its relative or absolute error does not exceed 10<=-<=6. | [
"2\nAB\nBA\n",
"3\nAAB\nCAA\n"
] | [
"0.400000000\n",
"0.642857143\n"
] | Let's assume that we are given string *a*β=β*a*<sub class="lower-index">1</sub>*a*<sub class="lower-index">2</sub>... *a*<sub class="lower-index">|*a*|</sub>, then let's denote the string's length as |*a*|, and its *i*-th character β as *a*<sub class="lower-index">*i*</sub>.
A substring *a*[*l*... *r*] (1ββ€β*l*ββ€β*r*β... | [
{
"input": "2\nAB\nBA",
"output": "0.400000000"
},
{
"input": "3\nAAB\nCAA",
"output": "0.642857143"
},
{
"input": "7\nAAAAAAA\nBBBBBBB",
"output": "0.000000000"
},
{
"input": "4\nAAAA\nAAAB",
"output": "1.333333333"
},
{
"input": "10\nSATYFFJYBA\nBGFOBFBVAV",
... | 311 | 27,136,000 | 3 | 228,121 | |
117 | Tree or not Tree | [
"data structures",
"divide and conquer",
"implementation",
"trees"
] | null | null | You are given an undirected connected graph *G* consisting of *n* vertexes and *n* edges. *G* contains no self-loops or multiple edges. Let each edge has two states: on and off. Initially all edges are switched off.
You are also given *m* queries represented as (*v*,<=*u*) β change the state of all edges on the shorte... | The first line contains two integers *n* and *m* (3<=β€<=*n*<=β€<=105, 1<=β€<=*m*<=β€<=105). Then *n* lines describe the graph edges as *a* *b* (1<=β€<=*a*,<=*b*<=β€<=*n*). Next *m* lines contain the queries as *v* *u* (1<=β€<=*v*,<=*u*<=β€<=*n*).
It is guaranteed that the graph is connected, does not have any self-loops or ... | Print *m* lines, each containing one integer β the query results. | [
"5 2\n2 1\n4 3\n2 4\n2 5\n4 1\n5 4\n1 5\n",
"6 2\n4 6\n4 3\n1 2\n6 5\n1 5\n1 4\n2 5\n2 6\n"
] | [
"3\n3\n",
"4\n3\n"
] | Let's consider the first sample. We'll highlight the switched on edges blue on the image.
- The graph before applying any operations. No graph edges are switched on, that's why there initially are 5 connected components. <center> <img class="tex-graphics" src="https://espresso.codeforces.com/2c7aa638136542ed6824b69da... | [] | 62 | 0 | 0 | 230,840 | |
796 | Sequence Recovery | [
"bitmasks",
"data structures",
"greedy"
] | null | null | Zane once had a good sequence *a* consisting of *n* integers *a*1,<=*a*2,<=...,<=*a**n*Β β but he has lost it.
A sequence is said to be good if and only if all of its integers are non-negative and do not exceed 109 in value.
However, Zane remembers having played around with his sequence by applying *m* operations to i... | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=3Β·105)Β β the number of integers in Zane's original sequence and the number of operations that have been applied to the sequence, respectively.
The *i*-th of the following *m* lines starts with one integer *t**i* ()Β β the type of the *i*-th operation... | If there does not exist a valid good sequence, print "NO" (without quotation marks) in the first line.
Otherwise, print "YES" (without quotation marks) in the first line, and print *n* space-separated integers *b*1,<=*b*2,<=...,<=*b**n* (0<=β€<=*b**i*<=β€<=109) in the second line.
If there are multiple answers, print a... | [
"5 4\n1 1 5 19\n1 2 5 1\n2 5 100\n1 1 5 100\n",
"5 2\n1 1 5 0\n1 1 5 100\n"
] | [
"YES\n19 0 0 0 1\n",
"NO"
] | In the first sample, it is easy to verify that this good sequence is valid. In particular, its cuteness is 19 OR 0 OR 0 OR 0 OR 1 β=β 19.
In the second sample, the two operations clearly contradict, so there is no such good sequence. | [] | 46 | 0 | 0 | 231,429 | |
0 | none | [
"none"
] | null | null | A very unusual citizen lives in a far away kingdom β Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg β he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery an... | The first line contains three space-separated integers *a*, *b* and *l* from the problem's statement (1<=β€<=*a*,<=*b*,<=*l*<=β€<=104). | Print the maximally possible width of a coffin with absolute or relative error no more than 10<=-<=7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answ... | [
"2 2 1\n",
"2 2 2\n",
"2 2 3\n",
"2 2 6\n"
] | [
"1.0000000\n",
"2.0000000",
"1.3284271\n",
"My poor head =(\n"
] | In the first example the answer is restricted by the coffin's length (remember β coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall... | [] | 92 | 0 | 0 | 232,335 | |
611 | New Year and Cake | [
"geometry",
"two pointers"
] | null | null | Limak is a little polar bear. According to some old traditions, his bear family prepared a New Year cake. And Limak likes cakes.
As you may know, a New Year cake is a strictly convex polygon with *n* vertices.
Parents won't allow Limak to eat more than half of a cake because he would get sick. After some thinking the... | The first line of the input contains a single integer *n* (4<=β€<=*n*<=β€<=500<=000)Β β the number of vertices in the polygon denoting the cake.
Each of the next *n* lines contains two integers *x**i* and *y**i* (|*x**i*|,<=|*y**i*|<=β€<=109)Β β coordinates of the *i*-th point.
It's guaranteed that all points are distinct... | Print the sum of values of Limak's disappointment over all possible scenarios modulo 109<=+<=7. | [
"5\n2 4\n2 7\n5 7\n5 4\n3 -2\n",
"4\n-1000000000 -5000000\n0 1234567\n1 1\n-5 -100000000\n",
"8\n-10 0\n-6 6\n0 10\n6 6\n10 0\n6 -6\n0 -10\n-6 -6\n"
] | [
"90\n",
"525185196\n",
"5216\n"
] | In the first sample possible values of Limak's disappointment are 0,β18,β18,β24,β30. | [] | 46 | 0 | 0 | 233,834 | |
339 | Xenia and Weights | [
"constructive algorithms",
"dfs and similar",
"dp",
"graphs",
"greedy",
"shortest paths"
] | null | null | Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes ... | The first line contains a string consisting of exactly ten zeroes and ones: the *i*-th (*i*<=β₯<=1) character in the line equals "1" if Xenia has *i* kilo weights, otherwise the character equals "0". The second line contains integer *m* (1<=β€<=*m*<=β€<=1000). | In the first line print "YES", if there is a way to put *m* weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put *m* weights on the scales, then print in the next line *m* integers β the weights' weights in the order you put them on the scales.
If there are multiple solutions, yo... | [
"0000000101\n3\n",
"1000000000\n2\n"
] | [
"YES\n8 10 8\n",
"NO\n"
] | none | [
{
"input": "0000000101\n3",
"output": "YES\n8 10 8"
},
{
"input": "1000000000\n2",
"output": "NO"
},
{
"input": "1000000000\n1",
"output": "YES\n1"
},
{
"input": "1111111111\n1000",
"output": "YES\n9 10 9 10 9 10 9 10 9 10 9 10 9 10 9 10 9 8 10 9 10 9 10 9 10 9 10 9 10 9 ... | 77 | 3,276,800 | 0 | 234,373 | |
579 | Finding Team Member | [
"brute force",
"implementation",
"sortings"
] | null | null | There is a programing contest named SnakeUp, 2*n* people want to compete for it. In order to attend this contest, people need to form teams of exactly two people. You are given the strength of each possible combination of two people. All the values of the strengths are distinct.
Every contestant hopes that he can find... | There are 2*n* lines in the input.
The first line contains an integer *n* (1<=β€<=*n*<=β€<=400) β the number of teams to be formed.
The *i*-th line (*i*<=><=1) contains *i*<=-<=1 numbers *a**i*1, *a**i*2, ... , *a**i*(*i*<=-<=1). Here *a**ij* (1<=β€<=*a**ij*<=β€<=106, all *a**ij* are distinct) denotes the strength of... | Output a line containing 2*n* numbers. The *i*-th number should represent the number of teammate of *i*-th person. | [
"2\n6\n1 2\n3 4 5\n",
"3\n487060\n3831 161856\n845957 794650 976977\n83847 50566 691206 498447\n698377 156232 59015 382455 626960\n"
] | [
"2 1 4 3\n",
"6 5 4 3 2 1\n"
] | In the first sample, contestant 1 and 2 will be teammates and so do contestant 3 and 4, so the teammate of contestant 1, 2, 3, 4 will be 2, 1, 4, 3 respectively. | [
{
"input": "2\n6\n1 2\n3 4 5",
"output": "2 1 4 3"
},
{
"input": "3\n487060\n3831 161856\n845957 794650 976977\n83847 50566 691206 498447\n698377 156232 59015 382455 626960",
"output": "6 5 4 3 2 1"
},
{
"input": "3\n8\n1 6\n14 13 15\n4 2 11 9\n12 5 3 7 10",
"output": "6 5 4 3 2 1"
... | 2,000 | 28,979,200 | 0 | 234,665 | |
696 | ...Dary! | [
"binary search",
"geometry",
"two pointers"
] | null | null | Barney has finally found the one, a beautiful young lady named Lyanna. The problem is, Lyanna and Barney are trapped in Lord Loss' castle. This castle has shape of a convex polygon of *n* points. Like most of castles in Demonata worlds, this castle has no ceiling.
Barney and Lyanna have an escape plan, but it requires... | The first line of input contains a single integer *n* (3<=β€<=*n*<=β€<=300)Β β the number of castle polygon vertices.
The next *n* lines describe the polygon vertices in counter-clockwise order. *i*-th of them contains two integers *x**i* and *y**i* (|*x**i*|,<=|*y**i*|<=β€<=104)Β β the coordinates of *i*-th point of the c... | In the first line print the single number *r*Β β minimum radius of guys' watching circles.
In the second line print the pair of coordinates of point where Barney should stay.
In the third line print the pair of coordinates of point where Lyanna should stay.
Points should lie inside the polygon.
Coordinates may not b... | [
"4\n-41 67\n-16 20\n25 25\n-36 85\n",
"7\n-7 54\n-5 31\n-2 17\n20 19\n32 23\n34 27\n26 57\n"
] | [
"0\n-16 20\n-36 85\n",
"2.9342248\n32.019503 23.0390067\n-6.929116 54.006444\n"
] | In the first example guys can stay in opposite corners of the castle. | [] | 46 | 0 | 0 | 235,215 | |
158 | Cd and pwd commands | [
"*special",
"data structures",
"implementation"
] | null | null | Vasya is writing an operating system shell, and it should have commands for working with directories. To begin with, he decided to go with just two commands: cd (change the current directory) and pwd (display the current directory).
Directories in Vasya's operating system form a traditional hierarchical tree structure... | The first line of the input data contains the single integer *n* (1<=β€<=*n*<=β€<=50) β the number of commands.
Then follow *n* lines, each contains one command. Each of these lines contains either command pwd, or command cd, followed by a space-separated non-empty parameter.
The command parameter cd only contains lowe... | For each command pwd you should print the full absolute path of the given directory, ending with a slash. It should start with a slash and contain the list of slash-separated directories in the order of being nested from the root to the current folder. It should contain no dots. | [
"7\npwd\ncd /home/vasya\npwd\ncd ..\npwd\ncd vasya/../petya\npwd\n",
"4\ncd /a/b\npwd\ncd ../a/b\npwd\n"
] | [
"/\n/home/vasya/\n/home/\n/home/petya/\n",
"/a/b/\n/a/a/b/\n"
] | none | [
{
"input": "7\npwd\ncd /home/vasya\npwd\ncd ..\npwd\ncd vasya/../petya\npwd",
"output": "/\n/home/vasya/\n/home/\n/home/petya/"
},
{
"input": "4\ncd /a/b\npwd\ncd ../a/b\npwd",
"output": "/a/b/\n/a/a/b/"
},
{
"input": "1\npwd",
"output": "/"
},
{
"input": "2\ncd /test/../test... | 62 | 0 | 0 | 237,404 | |
119 | Before Exam | [
"constructive algorithms",
"implementation",
"sortings"
] | null | null | Vasya is about to take his first university exam in about several minutes. And it's not just some ordinary exam, it's on mathematical analysis. Of course, right now Vasya can only think of one thing: what the result of his talk with the examiner will be...
To prepare for the exam, one has to study proofs of *n* theore... | The first line contains two integers *n* and *k* (1<=β€<=*k*<=β€<=*n*<=β€<=100) β the number of theorems and the number of cards correspondingly. The second line contains *n* integers *a**i* (0<=β€<=*a**i*<=β€<=100), the *i*-th number (1<=β€<=*i*<=β€<=*n*) corresponds to Vasya's proficiency in the *i*-th theorem.
The third l... | Print two real numbers, representing Vasya's minimum and maximum proficiency in the card he will get on the exam. The absolute or relative error should not exceed 10<=-<=6. | [
"7 3\n7 15 0 19 10 5 12\n2\n1 6\n7 4\n",
"4 2\n10 8 1 17\n2\n2 3\n3 2\n"
] | [
"5.0000000000 15.5000000000",
"4.5000000000 13.5000000000"
] | Let's analyze the first sample. Vasya's proficiency in the cards whose content he already knows equals 6 and 15.5 correspondingly. The three theorems that are left are only enough to make one exam card. If we consider all possible variants of theorems included in the card we can see that in the best case scenario Vasya... | [
{
"input": "7 3\n7 15 0 19 10 5 12\n2\n1 6\n7 4",
"output": "5.0000000000 15.5000000000"
},
{
"input": "4 2\n10 8 1 17\n2\n2 3\n3 2",
"output": "4.5000000000 13.5000000000"
},
{
"input": "10 5\n3 10 0 15 45 84 67 100 46 73\n3\n1 6\n8 9\n6 1",
"output": "5.0000000000 73.0000000000"
... | 122 | 0 | -1 | 238,995 | |
173 | Spiral Maximum | [
"brute force",
"dp"
] | null | null | Let's consider a *k*<=Γ<=*k* square, divided into unit squares. Please note that *k*<=β₯<=3 and is odd. We'll paint squares starting from the upper left square in the following order: first we move to the right, then down, then to the left, then up, then to the right again and so on. We finish moving in some direction i... | The first line contains two integers *n* and *m* (3<=β€<=*n*,<=*m*<=β€<=500) β the sizes of the table.
Each of the next *n* lines contains *m* space-separated integers: the *j*-th number in the *i*-th line *a**ij* (<=-<=1000<=β€<=*a**ij*<=β€<=1000) is the number recorded in the *j*-th cell of the *i*-th row of the table. | Print a single number β the maximum sum of numbers among all spirals. | [
"6 5\n0 0 0 0 0\n1 1 1 1 1\n0 0 0 0 1\n1 1 1 0 1\n1 0 0 0 1\n1 1 1 1 1\n",
"3 3\n1 1 1\n1 0 0\n1 1 1\n",
"6 6\n-3 2 0 1 5 -1\n4 -1 2 -3 0 1\n-5 1 2 4 1 -2\n0 -2 1 3 -1 2\n3 1 4 -3 -2 0\n-1 2 -1 3 1 2\n"
] | [
"17",
"6",
"13"
] | In the first sample the spiral with maximum sum will cover all 1's of the table.
In the second sample the spiral may cover only six 1's. | [
{
"input": "6 5\n0 0 0 0 0\n1 1 1 1 1\n0 0 0 0 1\n1 1 1 0 1\n1 0 0 0 1\n1 1 1 1 1",
"output": "17"
},
{
"input": "3 3\n1 1 1\n1 0 0\n1 1 1",
"output": "6"
},
{
"input": "6 6\n-3 2 0 1 5 -1\n4 -1 2 -3 0 1\n-5 1 2 4 1 -2\n0 -2 1 3 -1 2\n3 1 4 -3 -2 0\n-1 2 -1 3 1 2",
"output": "13"
}... | 280 | 23,142,400 | 0 | 239,014 | |
0 | none | [
"none"
] | null | null | You have a grid with *n* rows and *n* columns. Each cell is either empty (denoted by '.') or blocked (denoted by 'X').
Two empty cells are directly connected if they share a side. Two cells (*r*1,<=*c*1) (located in the row *r*1 and column *c*1) and (*r*2,<=*c*2) are connected if there exists a sequence of empty cells... | The first line of the input contains two integers *n* and *k* (1<=β€<=*k*<=β€<=*n*<=β€<=500)Β β the size of the grid and Limak's range, respectively.
Each of the next *n* lines contains a string with *n* characters, denoting the *i*-th row of the grid. Each character is '.' or 'X', denoting an empty cell or a blocked one,... | Print the maximum possible size (the number of cells) of the biggest connected component, after using Limak's help. | [
"5 2\n..XXX\nXX.XX\nX.XXX\nX...X\nXXXX.\n",
"5 3\n.....\n.XXX.\n.XXX.\n.XXX.\n.....\n"
] | [
"10\n",
"25\n"
] | In the first sample, you can choose a square of size 2βΓβ2. It's optimal to choose a square in the red frame on the left drawing below. Then, you will get a connected component with 10 cells, marked blue in the right drawing. | [] | 46 | 0 | 0 | 239,025 | |
860 | Arkady and a Nobody-men | [
"data structures",
"dfs and similar",
"trees"
] | null | null | Arkady words in a large company. There are *n* employees working in a system of a strict hierarchy. Namely, each employee, with an exception of the CEO, has exactly one immediate manager. The CEO is a manager (through a chain of immediate managers) of all employees.
Each employee has an integer rank. The CEO has rank ... | The first line contains single integer *n* (1<=β€<=*n*<=β€<=5Β·105)Β β the number of employees in the company.
The second line contains *n* integers *p*1,<=*p*2,<=...,<=*p**n* (0<=β€<=*p**i*<=β€<=*n*), where *p**i*<==<=0 if the *i*-th employee is the CEO, otherwise *p**i* equals the id of the immediate manager of the employ... | Print *n* integersΒ β the negligibilities of all employees in the order of their ids: *z*1,<=*z*2,<=...,<=*z**n*. | [
"4\n0 1 2 1\n",
"5\n2 3 4 5 0\n",
"5\n0 1 1 1 3\n"
] | [
"0 2 4 2 \n",
"10 6 3 1 0 \n",
"0 3 3 3 5 \n"
] | Consider the first example:
- The CEO has no managers, thus *z*<sub class="lower-index">1</sub>β=β0. - *r*(2,β1)β=β2 (employees 2 and 4 suit the conditions, employee 3 has too large rank). Thus *z*<sub class="lower-index">2</sub>β=β*r*(2,β1)β=β2. - Similarly, *z*<sub class="lower-index">4</sub>β=β*r*(4,β1)β=β2. - ... | [] | 46 | 0 | 0 | 239,187 | |
480 | Parcels | [
"dp",
"graphs"
] | null | null | Jaroslav owns a small courier service. He has recently got and introduced a new system of processing parcels. Each parcel is a box, the box has its weight and strength. The system works as follows. It originally has an empty platform where you can put boxes by the following rules:
- If the platform is empty, then th... | The first line of the input contains two space-separated integers *n* and *S* (1<=β€<=*n*<=β€<=500, 0<=β€<=*S*<=β€<=1000). Then *n* lines follow, the *i*-th line contains five space-separated integers: *in**i*, *out**i*, *w**i*, *s**i* and *v**i* (0<=β€<=*in**i*<=<<=*out**i*<=<<=2*n*, 0<=β€<=*w**i*,<=*s**i*<=β€<=1000, 1... | Print a single number β the maximum sum in bourles that Jaroslav can get. | [
"3 2\n0 1 1 1 1\n1 2 1 1 1\n0 2 1 1 1\n",
"5 5\n0 6 1 2 1\n1 2 1 1 1\n1 3 1 1 1\n3 6 2 1 2\n4 5 1 1 1\n"
] | [
"3\n",
"5\n"
] | Note to the second sample (*T* is the moment in time):
- *T*β=β0: The first parcel arrives, we put in on the first platform. - *T*β=β1: The second and third parcels arrive, we put the third one on the current top (i.e. first) parcel on the platform, then we put the secod one on the third one. Now the first parcel h... | [] | 140 | 1,228,800 | -1 | 239,394 | |
617 | XOR and Favorite Number | [
"data structures"
] | null | null | Bob has a favorite number *k* and *a**i* of length *n*. Now he asks you to answer *m* queries. Each query is given by a pair *l**i* and *r**i* and asks you to count the number of pairs of integers *i* and *j*, such that *l*<=β€<=*i*<=β€<=*j*<=β€<=*r* and the xor of the numbers *a**i*,<=*a**i*<=+<=1,<=...,<=*a**j* is equal... | The first line of the input contains integers *n*, *m* and *k* (1<=β€<=*n*,<=*m*<=β€<=100<=000, 0<=β€<=*k*<=β€<=1<=000<=000)Β β the length of the array, the number of queries and Bob's favorite number respectively.
The second line contains *n* integers *a**i* (0<=β€<=*a**i*<=β€<=1<=000<=000)Β β Bob's array.
Then *m* lines fo... | Print *m* lines, answer the queries in the order they appear in the input. | [
"6 2 3\n1 2 1 1 0 3\n1 6\n3 5\n",
"5 3 1\n1 1 1 1 1\n1 5\n2 4\n1 3\n"
] | [
"7\n0\n",
"9\n4\n4\n"
] | In the first sample the suitable pairs of *i* and *j* for the first query are: (1, 2), (1, 4), (1, 5), (2, 3), (3, 6), (5, 6), (6, 6). Not a single of these pairs is suitable for the second query.
In the second sample xor equals 1 for all subarrays of an odd length. | [] | 4,000 | 76,288,000 | 0 | 239,532 | |
901 | Cyclic Cipher | [
"fft",
"math"
] | null | null | Senor Vorpal Kickass'o invented an innovative method to encrypt integer sequences of length *n*. To encrypt a sequence, one has to choose a secret sequence , that acts as a key.
Vorpal is very selective, so the key should be such a sequence *b**i*, that its cyclic shifts are linearly independent, that is, there is no ... | The first line contains a single integer *n* ().
The second line contains *n* integers *b*0,<=*b*1,<=...,<=*b**n*<=-<=1 ().
The third line contains *n* integers *c*0,<=*c*1,<=...,<=*c**n*<=-<=1 ().
It is guaranteed that all cyclic shifts of sequence *b**i* are linearly independent. | In the first line print a single integer *k*Β β the number of sequences *a**i*, such that after encrypting them with key *b**i* you get the sequence *c**i*.
After that in each of *k* next lines print *n* integers *a*0,<=*a*1,<=...,<=*a**n*<=-<=1. Print the sequences in lexicographical order.
Note that *k* could be equ... | [
"1\n1\n0\n",
"1\n100\n81\n",
"3\n1 1 3\n165 185 197\n"
] | [
"1\n1\n",
"2\n91\n109\n",
"2\n-6 -9 -1\n8 5 13\n"
] | none | [] | 31 | 0 | 0 | 240,106 | |
319 | Have You Ever Heard About the Word? | [
"greedy",
"hashing",
"string suffix structures",
"strings"
] | null | null | A substring of a string is a contiguous subsequence of that string. So, string bca is substring of string abcabc, but string cc is not.
A repeating block is a string formed by concatenating some string with itself. So, string abcabc is a repeating block, but strings abcabd, ababab are not.
You've got a sequence of La... | In the first line of input you're given a string of small Latin characters with length between 1 to 50000, inclusive. | Print the final string after applying changes. | [
"abccabc\n",
"aaaabaaab\n",
"birdbirdbirdistheword\n"
] | [
"abc\n",
"ab\n",
"birdistheword\n"
] | At the first sample the string transforms as follows: abccabc βββ abcabc βββ abc.
At the second sample the string transforms as follows: aaaabaaab βββ aaabaaab βββ aabaaab βββ abaaab βββ abaab βββ abab βββ ab. | [] | 6,000 | 921,600 | 0 | 240,363 | |
183 | T-shirt | [
"dp",
"greedy",
"probabilities"
] | null | null | You are going to work in Codeforces as an intern in a team of *n* engineers, numbered 1 through *n*. You want to give each engineer a souvenir: a T-shirt from your country (T-shirts are highly desirable in Codeforces). Unfortunately you don't know the size of the T-shirt each engineer fits in. There are *m* different s... | The first line contains two space-separated integers *n* and *m* (1<=β€<=*n*<=β€<=3000,<=1<=β€<=*m*<=β€<=300), denoting the number of engineers and the number of T-shirt sizes, respectively.
Then *n* lines follow, each line contains *m* space-separated integers. The *j*-th integer in the *i*-th line represents the probabi... | Print a single real number denoting the maximum possible expected number of engineers that will receive a T-shirt.
For the answer the absolute or relative error of 10<=-<=9 is acceptable. | [
"2 2\n500 500\n500 500\n",
"3 3\n1000 0 0\n1000 0 0\n0 1000 0\n",
"1 4\n100 200 300 400\n"
] | [
"1.500000000000\n",
"3.000000000000\n",
"0.400000000000\n"
] | For the first example, bring one T-shirt of each size. With 0.5 chance, either both engineers fit inside T-shirts of size 1 or both fit inside T-shirts of size 2. With the other 0.5 chance, one engineer fits inside a T-shirt of size 1 and the other inside a T-shirt of size 2. If the first is true, the number of enginee... | [] | 60 | 0 | 0 | 241,845 | |
213 | Two Permutations | [
"data structures",
"hashing",
"strings"
] | null | null | Rubik is very keen on number permutations.
A permutation *a* with length *n* is a sequence, consisting of *n* different numbers from 1 to *n*. Element number *i* (1<=β€<=*i*<=β€<=*n*) of this permutation will be denoted as *a**i*.
Furik decided to make a present to Rubik and came up with a new problem on permutations.... | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=*m*<=β€<=200000) β the sizes of the given permutations. The second line contains *n* distinct integers β permutation *a*, the third line contains *m* distinct integers β permutation *b*. Numbers on the lines are separated by spaces. | On a single line print the answer to the problem. | [
"1 1\n1\n1\n",
"1 2\n1\n2 1\n",
"3 3\n2 3 1\n1 2 3\n"
] | [
"1\n",
"2\n",
"0\n"
] | none | [
{
"input": "1 1\n1\n1",
"output": "1"
},
{
"input": "1 2\n1\n2 1",
"output": "2"
},
{
"input": "3 3\n2 3 1\n1 2 3",
"output": "0"
},
{
"input": "2 4\n2 1\n1 4 2 3",
"output": "1"
},
{
"input": "2 5\n1 2\n2 5 1 3 4",
"output": "2"
},
{
"input": "1 6\n1\... | 62 | 0 | -1 | 242,779 | |
484 | Sign on Fence | [
"binary search",
"constructive algorithms",
"data structures"
] | null | null | Bizon the Champion has recently finished painting his wood fence. The fence consists of a sequence of *n* panels of 1 meter width and of arbitrary height. The *i*-th panel's height is *h**i* meters. The adjacent planks follow without a gap between them.
After Bizon painted the fence he decided to put a "for sale" sign... | The first line of the input contains integer *n*Β β the number of panels in the fence (1<=β€<=*n*<=β€<=105).
The second line contains *n* space-separated integers *h**i*,Β β the heights of the panels (1<=β€<=*h**i*<=β€<=109).
The third line contains an integer *m*Β β the number of the queries (1<=β€<=*m*<=β€<=105).
The ne... | For each query print the answer on a separate lineΒ β the maximum height of the sign that can be put in the corresponding segment of the fence with all the conditions being satisfied. | [
"5\n1 2 2 3 3\n3\n2 5 3\n2 5 2\n1 5 5\n"
] | [
"2\n3\n1\n"
] | The fence described in the sample looks as follows:
The possible positions for the signs for all queries are given below. | [] | 4,000 | 20,684,800 | 0 | 242,914 | |
83 | Track | [
"graphs",
"greedy",
"shortest paths"
] | C. Track | 5 | 256 | 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 | 243,463 |
231 | Cactus | [
"data structures",
"dfs and similar",
"dp",
"graphs",
"trees"
] | null | null | A connected undirected graph is called a vertex cactus, if each vertex of this graph belongs to at most one simple cycle.
A simple cycle in a undirected graph is a sequence of distinct vertices *v*1,<=*v*2,<=...,<=*v**t* (*t*<=><=2), such that for any *i* (1<=β€<=*i*<=<<=*t*) exists an edge between vertices *v**i... | The first line contains two space-separated integers *n*,<=*m* (2<=β€<=*n*<=β€<=105;Β 1<=β€<=*m*<=β€<=105) β the number of vertices and edges in the graph, correspondingly. Next *m* lines contain the description of the edges: the *i*-th line contains two space-separated integers *a**i*,<=*b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n... | Print *k* lines: in the *i*-th line print a single integer β the number of distinct simple ways, starting at *x**i* and ending at *y**i*, modulo 1000000007 (109<=+<=7). | [
"10 11\n1 2\n2 3\n3 4\n1 4\n3 5\n5 6\n8 6\n8 7\n7 6\n7 9\n9 10\n6\n1 2\n3 5\n6 9\n9 2\n9 3\n9 10\n"
] | [
"2\n2\n2\n4\n4\n1\n"
] | none | [
{
"input": "10 11\n1 2\n2 3\n3 4\n1 4\n3 5\n5 6\n8 6\n8 7\n7 6\n7 9\n9 10\n6\n1 2\n3 5\n6 9\n9 2\n9 3\n9 10",
"output": "2\n2\n2\n4\n4\n1"
},
{
"input": "2 1\n1 2\n3\n1 2\n1 2\n2 1",
"output": "1\n1\n1"
},
{
"input": "6 6\n1 2\n2 3\n3 4\n4 5\n5 6\n6 1\n4\n1 2\n1 6\n6 5\n4 3",
"output... | 62 | 0 | 0 | 243,868 | |
336 | Vasily the Bear and Painting Square | [
"bitmasks",
"combinatorics",
"dp",
"implementation"
] | null | null | Vasily the bear has two favorite integers *n* and *k* and a pencil. Besides, he's got *k* jars with different water color paints. All jars are numbered in some manner from 1 to *k*, inclusive. The jar number *i* contains the paint of the *i*-th color.
Initially the bear took a pencil and drew four segments on the coo... | The first line contains two integers *n* and *k*, separated by a space (0<=β€<=*n*,<=*k*<=β€<=200). | Print exactly one integer β the answer to the problem modulo 1000000007 (109<=+<=7). | [
"0 0\n",
"0 1\n",
"0 2\n",
"1 1\n"
] | [
"1\n",
"8\n",
"32\n",
"32\n"
] | none | [] | 92 | 0 | 0 | 245,597 | |
382 | Ksenia and Pawns | [
"dfs and similar",
"graphs",
"implementation",
"trees"
] | null | null | Ksenia has a chessboard of size *n*<=Γ<=*m*. Each cell of the chessboard contains one of the characters: "<", ">", "^", "v", "#". The cells that contain character "#" are blocked. We know that all chessboard cells that touch the border are blocked.
Ksenia is playing with two pawns on this chessboard. Initially, ... | The first line contains two integers, *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=2000) β the sizes of the board. Each of the following *n* lines contains *m* characters β the board's description. Each character is one of the characters: "<", ">", "^", "v", "#".
It is guaranteed that the border cells of the table are block... | If Ksenia can get infinitely many points, print -1. Otherwise, print the maximum number of points she can get. | [
"1 1\n#\n",
"3 4\n####\n#>^{}#\n####\n",
"3 4\n####\n#><#\n####\n",
"7 5\n#####\n##v##\n##v##\n#####\n##^{}##\n##^{}##\n#####\n",
"7 5\n#####\n##v##\n##v##\n##<##\n##^{}##\n##^{}##\n#####"
] | [
"0\n",
"3",
"-1",
"4",
"5"
] | none | [] | 46 | 0 | 0 | 245,615 | |
120 | Boom | [
"implementation"
] | null | null | Let's consider the famous game called Boom (aka Hat) with simplified rules.
There are *n* teams playing the game. Each team has two players. The purpose of the game is to explain the words to the teammate without using any words that contain the same root or that sound similarly.
Player *j* from team *i* (1<=β€<=*i*<... | The first line contains two integers *n*,<=*t* (1<=β€<=*n*,<=*t*<=β€<=100), which correspondingly denote the number of teams and a turn's duration.
Next *n* lines of the input file contain four integers each: *a**i*1,<=*b**i*1,<=*a**i*2,<=*b**i*2 (1<=β€<=*a**ij*,<=*b**ij*<=β€<=100) β the skills of the first and the second... | Print *n* lines. On the *i*-th line first print number *s**i* the number of points the *i*-th team wins. Then print *s**i* space-separated words β the words from the cards guessed by team *i* in the order in which they were guessed. | [
"2 2\n1 1 1 1\n1 1 1 1\n3\nhome\n1\ncar\n1\nbrother\n1\n",
"2 4\n1 2 2 1\n2 3 2 2\n4\narmchair\n3\nquetzalcoatl\n10\npilotage\n5\ndefibrillator\n7\n"
] | [
"2 home car \n1 brother \n",
"2 armchair quetzalcoatl \n2 pilotage defibrillator \n"
] | none | [
{
"input": "2 2\n1 1 1 1\n1 1 1 1\n3\nhome\n1\ncar\n1\nbrother\n1",
"output": "2 home car \n1 brother "
},
{
"input": "2 4\n1 2 2 1\n2 3 2 2\n4\narmchair\n3\nquetzalcoatl\n10\npilotage\n5\ndefibrillator\n7",
"output": "2 armchair quetzalcoatl \n2 pilotage defibrillator "
},
{
"input": "5... | 124 | 0 | 0 | 246,390 | |
314 | Sereja and Straight Lines | [
"binary search",
"data structures",
"geometry",
"sortings",
"two pointers"
] | null | null | Sereja placed *n* points on a plane. Now Sereja wants to place on the plane two straight lines, intersecting at a right angle, so that one of the straight lines intersect the *Ox* axis at an angle of 45 degrees and the maximum distance from the points to the straight lines were minimum.
In this problem we consider th... | The first line contains integer *n* (1<=β€<=*n*<=β€<=105). Next *n* lines contain the coordinates of the lines. The *i*-th line contains two integers *x**i*,<=*y**i* (|*x**i*|,<=|*y**i*|<=β€<=109). | In a single line print a real number β the answer to the problem. Your answer will be considered correct iff its absolute or relative error doesn't exceed 10<=-<=6. | [
"4\n0 0\n2 0\n0 2\n2 2\n",
"4\n1 0\n0 1\n2 1\n1 2\n"
] | [
"0.000000000000000\n",
"1.000000000000000\n"
] | none | [] | 60 | 0 | 0 | 246,802 | |
553 | Kyoya and Train | [
"dp",
"fft",
"graphs",
"math",
"probabilities"
] | null | null | Kyoya Ootori wants to take the train to get to school. There are *n* train stations and *m* one-way train lines going between various stations. Kyoya is currently at train station 1, and the school is at station *n*. To take a train, he must pay for a ticket, and the train also takes a certain amount of time. However, ... | The first line of input contains four integers *n*,<=*m*,<=*t*,<=*x* (2<=<=β€<=<=*n*<=<=β€<=50, 1<=β€<=*m*<=β€<=100, 1<=β€<=*t*<=β€<=20<=000, 0<=β€<=*x*<=β€<=106).
The next 2*m* lines contain the description of the trains.
The 2*i*-th line will have 3 integers *a**i*,<=*b**i*,<=*c**i*, representing a one way train from stat... | Print a single real number that is equal to an optimal expected cost of getting to school. The answer will be considered correct if its relative or absolute error doesn't exceed 10<=-<=6. | [
"4 4 5 1\n1 2 0\n50000 0 50000 0 0\n2 3 0\n10000 0 0 0 90000\n3 4 0\n100000 0 0 0 0\n2 4 0\n0 0 0 50000 50000\n",
"4 4 5 1\n1 2 100\n50000 0 50000 0 0\n2 3 100\n10000 0 0 0 90000\n3 4 100\n100000 0 0 0 0\n2 4 100\n0 0 0 50000 50000\n"
] | [
"0.7000000000\n",
"200.7500000000\n"
] | The optimal strategy in the first case is as follows:
First, travel along first train line. With probability 1β/β2 Kyoya will take 1 time unit. Otherwise, Kyoya will take 3 time units.
If the train takes 1 time unit, travel along the 4th train line. Kyoya will make it to school in time with probability 1β/β2. Otherwi... | [] | 30 | 0 | 0 | 247,162 | |
707 | Persistent Bookcase | [
"bitmasks",
"data structures",
"dfs and similar",
"implementation"
] | null | null | Recently in school Alina has learned what are the persistent data structures: they are data structures that always preserves the previous version of itself and access to it when it is modified.
After reaching home Alina decided to invent her own persistent data structure. Inventing didn't take long: there is a bookcas... | The first line of the input contains three integers *n*, *m* and *q* (1<=β€<=*n*,<=*m*<=β€<=103, 1<=β€<=*q*<=β€<=105)Β β the bookcase dimensions and the number of operations respectively.
The next *q* lines describes operations in chronological orderΒ β *i*-th of them describes *i*-th operation in one of the four formats de... | For each operation, print the number of books in the bookcase after applying it in a separate line. The answers should be printed in chronological order. | [
"2 3 3\n1 1 1\n3 2\n4 0\n",
"4 2 6\n3 2\n2 2 2\n3 3\n3 2\n2 2 2\n3 2\n",
"2 2 2\n3 2\n2 2 1\n"
] | [
"1\n4\n0\n",
"2\n1\n3\n3\n2\n4\n",
"2\n1\n"
] | This image illustrates the second sample case. | [
{
"input": "2 3 3\n1 1 1\n3 2\n4 0",
"output": "1\n4\n0"
},
{
"input": "4 2 6\n3 2\n2 2 2\n3 3\n3 2\n2 2 2\n3 2",
"output": "2\n1\n3\n3\n2\n4"
},
{
"input": "2 2 2\n3 2\n2 2 1",
"output": "2\n1"
},
{
"input": "4 1 6\n3 4\n3 3\n3 3\n2 2 1\n1 1 1\n4 0",
"output": "1\n2\n1\n... | 93 | 0 | 0 | 248,842 | |
323 | Two permutations | [
"data structures"
] | null | null | You are given two permutations *p* and *q*, consisting of *n* elements, and *m* queries of the form: *l*1,<=*r*1,<=*l*2,<=*r*2 (*l*1<=β€<=*r*1;Β *l*2<=β€<=*r*2). The response for the query is the number of such integers from 1 to *n*, that their position in the first permutation is in segment [*l*1,<=*r*1] (borders includ... | The first line contains one integer *n* (1<=β€<=*n*<=β€<=106), the number of elements in both permutations. The following line contains *n* integers, separated with spaces: *p*1,<=*p*2,<=...,<=*p**n* (1<=β€<=*p**i*<=β€<=*n*). These are elements of the first permutation. The next line contains the second permutation *q*1,<=... | Print a response for each query in a separate line. | [
"3\n3 1 2\n3 2 1\n1\n1 2 3 3\n",
"4\n4 3 2 1\n2 3 4 1\n3\n1 2 3 4\n1 3 2 1\n1 4 2 3\n"
] | [
"1\n",
"1\n1\n2\n"
] | none | [] | 30 | 0 | -1 | 249,339 | |
833 | Red-Black Cobweb | [
"data structures",
"divide and conquer",
"implementation",
"trees"
] | null | null | Slastyona likes to watch life of nearby grove's dwellers. This time she watches a strange red-black spider sitting at the center of a huge cobweb.
The cobweb is a set of *n* nodes connected by threads, each of the treads is either red of black. Using these threads, the spider can move between nodes. No thread connects... | The first line contains the number of nodes *n* (2<=β€<=*n*<=β€<=105).
The next *n*<=-<=1 lines contain four integers each, denoting the *i*-th thread of the cobweb: the nodes it connects *u**i*,<=*v**i* (1<=β€<=*u**i*<=β€<=*n*,<=1<=β€<=*v**i*<=β€<=*n*), the clamminess of the thread *x**i* (1<=β€<=*x*<=β€<=109<=+<=6), and the... | Print single integer the jelliness of the cobweb modulo 109<=+<=7. If there are no paths such that the numbers of red and black threads differ at most twice, print 1. | [
"5\n1 2 9 0\n2 3 5 1\n2 4 5 0\n2 5 5 1\n",
"8\n1 2 7 1\n2 3 4 1\n3 4 19 1\n5 1 2 0\n6 2 3 0\n7 3 3 0\n8 4 4 0\n"
] | [
"1265625\n",
"452841614\n"
] | In the first example there are 4 pairs of nodes such that the numbers of threads of both colors on them differ at most twice. There pairs are (1,β3) with product of clamminess equal to 45, (1,β5) with product of clamminess equal to 45, (3,β4) with product of clamminess equal to 25 and (4,β5) with product of clamminess ... | [] | 46 | 0 | 0 | 251,243 | |
386 | Game with Points | [
"dp",
"graphs",
"implementation",
"shortest paths"
] | null | null | You are playing the following game. There are *n* points on a plane. They are the vertices of a regular *n*-polygon. Points are labeled with integer numbers from 1 to *n*. Each pair of distinct points is connected by a diagonal, which is colored in one of 26 colors. Points are denoted by lowercase English letters. Ther... | In the first line there is one integer *n* (3<=β€<=*n*<=β€<=70) β the number of points. In the second line there are three space-separated integer from 1 to *n* β numbers of vertices, where stones are initially located.
Each of the following *n* lines contains *n* symbols β the matrix denoting the colors of the diagonal... | If there is no way to put stones on vertices 1, 2 and 3, print -1 on a single line. Otherwise, on the first line print minimal required number of moves and in the next lines print the description of each move, one move per line. To describe a move print two integers. The point from which to remove the stone, and the po... | [
"4\n2 3 4\n*aba\na*ab\nba*b\nabb*\n",
"4\n2 3 4\n*abc\na*ab\nba*b\ncbb*\n"
] | [
"1\n4 1\n",
"-1\n"
] | In the first example we can move stone from point 4 to point 1 because this points are connected by the diagonal of color 'a' and the diagonal connection point 2 and 3, where the other stones are located, are connected by the diagonal of the same color. After that stones will be on the points 1, 2 and 3. | [
{
"input": "4\n2 3 4\n*aba\na*ab\nba*b\nabb*",
"output": "1\n4 1"
},
{
"input": "4\n2 3 4\n*abc\na*ab\nba*b\ncbb*",
"output": "-1"
},
{
"input": "3\n1 2 3\n*aa\na*a\naa*",
"output": "0"
},
{
"input": "10\n9 8 6\n*abbbababb\na*babbaaaa\nbb*bbaabab\nbab*bbbaab\nbbbb*baaab\nabab... | 701 | 13,209,600 | 3 | 251,522 | |
245 | Log Stream Analysis | [
"binary search",
"brute force",
"implementation",
"strings"
] | null | null | You've got a list of program warning logs. Each record of a log stream is a string in this format:
String "MESSAGE" consists of spaces, uppercase and lowercase English letters and characters "!", ".", ",", "?". String "2012-MM-DD" determines a correct date in the year of 2012. String "HH:MM:SS" determines a correct t... | The first line of the input contains two space-separated integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=10000).
The second and the remaining lines of the input represent the log stream. The second line of the input contains the first record of the log stream, the third line contains the second record and so on. Each record... | If there is no sought moment of time, print -1. Otherwise print a string in the format "2012-MM-DD HH:MM:SS" (without the quotes) β the first moment of time when the number of warnings for the last *n* seconds got no less than *m*. | [
"60 3\n2012-03-16 16:15:25: Disk size is\n2012-03-16 16:15:25: Network failute\n2012-03-16 16:16:29: Cant write varlog\n2012-03-16 16:16:42: Unable to start process\n2012-03-16 16:16:43: Disk size is too small\n2012-03-16 16:16:53: Timeout detected\n",
"1 2\n2012-03-16 23:59:59:Disk size\n2012-03-17 00:00:00: Net... | [
"2012-03-16 16:16:43\n",
"-1\n",
"2012-03-17 00:00:00\n"
] | none | [
{
"input": "60 3\n2012-03-16 16:15:25: Disk size is\n2012-03-16 16:15:25: Network failute\n2012-03-16 16:16:29: Cant write varlog\n2012-03-16 16:16:42: Unable to start process\n2012-03-16 16:16:43: Disk size is too small\n2012-03-16 16:16:53: Timeout detected",
"output": "2012-03-16 16:16:43"
},
{
"... | 30 | 102,400 | -1 | 251,524 | |
409 | Feed the Golorp | [
"*special"
] | null | null | Golorps are mysterious creatures who feed on variables. Golorp's name is a program in some programming language. Some scientists believe that this language is Befunge; golorps are tantalizingly silent.
Variables consumed by golorps can take values from 0 to 9, inclusive. For each golorp its daily diet is defined by it... | The input is a single string (between 13 and 1024 characters long) β the name of the visiting golorp. All names are similar and will resemble the ones given in the samples. The name is guaranteed to be valid. | Output lexicographically smallest sequence of variable values fit for feeding this golorp. Values should be listed in the order in which they get into the jaws. If the golorp is impossible to feed, output "false". | [
"?(_-_/___*__):-___>__.\n",
"?(__-_+_/_____):-__>__,_____<__.\n",
"?(______________________/____+_______*__-_____*______-___):-__<___,___<____,____<_____,_____<______,______<_______.\n",
"?(__+___+__-___):-___>__.\n"
] | [
"0010\n",
"false\n",
"0250341\n",
"0101\n"
] | none | [
{
"input": "?(_-_/___*__):-___>__.",
"output": "0010"
},
{
"input": "?(__-_+_/_____):-__>__,_____<__.",
"output": "false"
},
{
"input": "?(______________________/____+_______*__-_____*______-___):-__<___,___<____,____<_____,_____<______,______<_______.",
"output": "0250341"
},
{
... | 31 | 0 | 0 | 251,562 | |
0 | none | [
"none"
] | null | null | Consider a regular Codeforces round consisting of three problems that uses dynamic scoring.
You are given an almost final scoreboard. For each participant (including yourself), the time of the accepted submission for each of the problems is given. Also, for each solution you already know whether you are able to hack i... | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=5000)Β β the number of participants. You are the participant number 1.
Each of the following *n* lines contains three integers *a**i*, *b**i* and *c**i*. Here *a**i*<==<=0 means that the participant number *i* didn't manage to accept first problem... | Print the only integerΒ β the best place you can take at the end of the round. | [
"4\n120 120 1\n61 61 120\n-61 61 120\n0 0 0\n",
"4\n0 0 119\n-3 -17 -42\n0 7 0\n51 0 0\n"
] | [
"1\n",
"2\n"
] | Consider the first sample. If you do not hack any solutions, you will win the contest (scoreboard to the left). However, if you hack the solution of the first problem of the third participant (the only one you can hack), the maximum score for the first problem will change and you will finish second (scoreboard to the r... | [] | 46 | 0 | 0 | 251,644 | |
258 | Little Elephant and Tree | [
"data structures",
"dfs and similar",
"trees"
] | null | null | The Little Elephant loves trees very much, he especially loves root trees.
He's got a tree consisting of *n* nodes (the nodes are numbered from 1 to *n*), with root at node number 1. Each node of the tree contains some list of numbers which initially is empty.
The Little Elephant wants to apply *m* operations. On th... | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=105) β the number of the tree nodes and the number of operations.
Each of the following *n*<=-<=1 lines contains two space-separated integers, *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*,<=*u**i*<=β <=*v**i*), that mean that there is an edge be... | In a single line print *n* space-separated integers β *c*1,<=*c*2,<=...,<=*c**n*. | [
"5 1\n1 2\n1 3\n3 5\n3 4\n2 3\n",
"11 3\n1 2\n2 3\n2 4\n1 5\n5 6\n5 7\n5 8\n6 9\n8 10\n8 11\n2 9\n3 6\n2 8\n"
] | [
"0 3 3 3 3 ",
"0 6 7 6 0 2 0 5 4 5 5 "
] | none | [] | 60 | 0 | 0 | 252,869 | |
575 | Run for beer | [
"dfs and similar",
"shortest paths"
] | null | null | People in BubbleLand like to drink beer. Little do you know, beer here is so good and strong that every time you drink it your speed goes 10 times slower than before you drank it.
Birko lives in city Beergrade, but wants to go to city Beerburg. You are given a road map of BubbleLand and you need to find the fastest wa... | The first line of input contains integer *N* β the number of cities in Bubbleland and integer *M* β the number of roads in this country. Cities are enumerated from 0 to *N*<=-<=1, with city 0 being Beergrade, and city *N*<=-<=1 being Beerburg. Each of the following *M* lines contains three integers *a*, *b* (*a*<=β <=*b... | The first line of output should contain minimal time needed to go from Beergrade to Beerburg.
The second line of the output should contain the number of cities on the path from Beergrade to Beerburg that takes minimal time.
The third line of output should contain the numbers of cities on this path in the order they ... | [
"8 10\n0 1 1\n1 2 5\n2 7 6\n0 3 2\n3 7 3\n0 4 0\n4 5 0\n5 7 2\n0 6 0\n6 7 7\n"
] | [
"32\n3\n0 3 7\n"
] | none | [
{
"input": "8 10\n0 1 1\n1 2 5\n2 7 6\n0 3 2\n3 7 3\n0 4 0\n4 5 0\n5 7 2\n0 6 0\n6 7 7",
"output": "32\n3\n0 3 7"
},
{
"input": "10 20\n0 2 7\n0 3 5\n0 4 8\n0 9 9\n1 2 7\n1 4 7\n1 5 7\n1 6 8\n1 7 0\n1 9 9\n2 3 6\n2 5 6\n2 6 8\n3 6 1\n3 7 5\n3 9 1\n5 9 8\n6 8 9\n7 9 7\n8 9 5",
"output": "9\n2\n0 ... | 30 | 307,200 | 0 | 253,353 | |
868 | Yet Another Minimization Problem | [
"divide and conquer",
"dp"
] | null | null | You are given an array of *n* integers *a*1... *a**n*. The cost of a subsegment is the number of unordered pairs of distinct indices within the subsegment that contain equal elements. Split the given array into *k* non-intersecting non-empty subsegments so that the sum of their costs is minimum possible. Each element s... | The first line contains two integers *n* and *k* (2<=β€<=*n*<=β€<=105, 2<=β€<=*k*<=β€<=*min* (*n*,<=20)) Β β the length of the array and the number of segments you need to split the array into.
The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=*n*)Β β the elements of the array. | Print single integer: the minimum possible total cost of resulting subsegments. | [
"7 3\n1 1 3 3 3 2 1\n",
"10 2\n1 2 1 2 1 2 1 2 1 2\n",
"13 3\n1 2 2 2 1 2 1 1 1 2 2 1 1\n"
] | [
"1\n",
"8\n",
"9\n"
] | In the first example it's optimal to split the sequence into the following three subsegments: [1], [1,β3], [3,β3,β2,β1]. The costs are 0, 0 and 1, thus the answer is 1.
In the second example it's optimal to split the sequence in two equal halves. The cost for each half is 4.
In the third example it's optimal to split... | [
{
"input": "7 3\n1 1 3 3 3 2 1",
"output": "1"
},
{
"input": "10 2\n1 2 1 2 1 2 1 2 1 2",
"output": "8"
},
{
"input": "13 3\n1 2 2 2 1 2 1 1 1 2 2 1 1",
"output": "9"
},
{
"input": "2 2\n2 2",
"output": "0"
},
{
"input": "15 2\n11 9 15 15 15 15 15 15 9 9 11 15 9 1... | 30 | 0 | 0 | 253,816 | |
0 | none | [
"none"
] | null | null | You are given a tree with *n* vertices and you are allowed to perform no more than 2*n* transformations on it. Transformation is defined by three vertices *x*,<=*y*,<=*y*' and consists of deleting edge (*x*,<=*y*) and adding edge (*x*,<=*y*'). Transformation *x*,<=*y*,<=*y*' could be performed if all the following cond... | The first line of input contains integer *n* (1<=β€<=*n*<=β€<=2Β·105) β number of vertices in tree.
The next *n*<=-<=1 lines of input contains integers *a* and *b* (1<=β€<=*a*,<=*b*<=β€<=*n*,<=*a*<=β <=*b*) β the descriptions of edges. It is guaranteed that the given edges form a tree. | In the first line output integer *k* (0<=β€<=*k*<=β€<=2*n*) β the number of transformations from your example, minimizing sum of squared distances between all pairs of vertices.
In each of the next *k* lines output three integers *x*,<=*y*,<=*y*' β indices of vertices from the corresponding transformation.
Transformati... | [
"3\n3 2\n1 3\n",
"7\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n"
] | [
"0\n",
"2\n4 3 2\n4 5 6"
] | This is a picture for the second sample. Added edges are dark, deleted edges are dotted.
<img class="tex-graphics" src="https://espresso.codeforces.com/e673f9bdb501a0674ec4a21da846eef00abe1aaf.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [] | 2,324 | 134,348,800 | -1 | 254,120 | |
542 | Duck Hunt | [
"data structures"
] | null | null | A duck hunter is doing his favorite thing, hunting. He lives in a two dimensional world and is located at point (0,<=0). As he doesn't like walking for his prey, he prefers to shoot only vertically up (because in this case, the ducks fall straight into his hands). The hunter doesn't reload the gun immediately β *r* or ... | The first line of the input contains integers *n*, *r* (1<=β€<=*n*<=β€<=200<=000, 1<=β€<=*r*<=β€<=109) β the number of ducks and the minimum time in seconds between the shots.
Then *n* lines follow, each of them contains two integers *h**i*,<=*t**i* (<=-<=109<=β€<=*h**i*<=<<=*t**i*<=β€<=109)Β β the *x*-coordinate of the ... | Print a single integer β the maximum number of ducks that can be shot by the hunter. | [
"3 3\n-3 0\n1 3\n-1 2\n",
"4 5\n-1 1\n2 4\n5 9\n6 8\n"
] | [
"3\n",
"3\n"
] | In the first sample the hunter must shoot at time 0, this shot kills ducks 1 and 3. Then the hunter needs to reload the gun and shoot again at time 3. His second shot hits the tail of duck 2.
In the second sample the hunter can make shots at times 0 and 6 to hit three ducks. | [] | 46 | 0 | 0 | 254,529 | |
420 | Playing the ball | [
"geometry"
] | null | null | A coder cannot sit and code all day. Sometimes it is a good idea to rise from the desk, have a rest, have small talk with colleagues and even play. The coders of the F company have their favorite ball game.
Let's imagine the game on the plane with a cartesian coordinate system. The point (0, 0) contains the player who... | The first line contains two space-separated integers β *n* ΠΈ *d* (1<=β€<=*n*<=β€<=2Β·104;Β 5<=β€<=*d*<=β€<=10). Next *n* lines contain the circles' description. The *i*-th line contains three space-separated integers *x**i*, *y**i*, *r**i* (<=-<=10000<=β€<=*x**i*,<=*y**i*<=β€<=10000;Β 1<=β€<=*r*<=β€<=50), where (*x**i*,<=*y**i*,<... | Print a single integer β the maximum number of points you can get. | [
"2 5\n1 1 1\n5 0 1\n",
"2 5\n4 0 3\n5 3 1\n",
"1 10\n20 0 10\n"
] | [
"1\n",
"2\n",
"3\n"
] | none | [] | 2,000 | 512,000 | 0 | 258,946 | |
152 | Frames | [
"brute force"
] | null | null | One day Vasya got hold of a sheet of checkered paper *n*<=Γ<=*m* squares in size. Our Vasya adores geometrical figures, so he painted two rectangles on the paper. The rectangles' sides are parallel to the coordinates' axes, also the length of each side of each rectangle is no less than 3 squares and the sides are paint... | The first input line contains two integers *n* and *m* (3<=β€<=*n*,<=*m*<=β€<=1000) β the sizes of the sheet of paper Vasya found. Next *n* lines, each consisting of *m* symbols "." (dot) and "#" (number sign), describe the found sheet of paper. The symbol "#" represents a hatched square and the symbol "." represents a n... | In the first line print the single word "YES" or "NO", meaning whether it is true that the found sheet of paper has two frames painted on it. If the answer is positive, then print in the second line 4 integers: the coordinates of the upper left and lower right corners of the first frame. In the third line print 4 integ... | [
"4 5\n#####\n#.#.#\n###.#\n#####\n",
"5 6\n...###\n...###\n#####.\n#...#.\n#####.\n"
] | [
"YES\n1 1 3 3\n1 1 4 5\n",
"NO\n"
] | In the first sample there are two frames on the picture. The first one is:
The second one is:
In the second sample the painted figures are not frames. Note that the height and width of valid frames is no less than 3. | [] | 60 | 0 | 0 | 259,132 | |
633 | Yash And Trees | [
"bitmasks",
"data structures",
"dfs and similar",
"math",
"number theory"
] | null | null | Yash loves playing with trees and gets especially excited when they have something to do with prime numbers. On his 20th birthday he was granted with a rooted tree of *n* nodes to answer queries on. Hearing of prime numbers on trees, Yash gets too intoxicated with excitement and asks you to help out and answer queries ... | The first of the input contains two integers *n* and *m* (1<=β€<=*n*<=β€<=100<=000,<=1<=β€<=*m*<=β€<=1000)Β β the number of nodes in the tree and value *m* from the problem statement, respectively.
The second line consists of *n* integers *a**i* (0<=β€<=*a**i*<=β€<=109)Β β initial values of the nodes.
Then follow *n*<=-<=1 l... | For each of the queries of the second type print the number of suitable prime numbers. | [
"8 20\n3 7 9 8 4 11 7 3\n1 2\n1 3\n3 4\n4 5\n4 6\n4 7\n5 8\n4\n2 1\n1 1 1\n2 5\n2 4\n",
"5 10\n8 7 5 1 0\n1 2\n2 3\n1 5\n2 4\n3\n1 1 0\n1 1 2\n2 2\n"
] | [
"3\n1\n1\n",
"2\n"
] | none | [] | 46 | 0 | 0 | 259,705 | |
341 | Iahub and Xors | [
"data structures"
] | null | null | Iahub does not like background stories, so he'll tell you exactly what this problem asks you for.
You are given a matrix *a* with *n* rows and *n* columns. Initially, all values of the matrix are zeros. Both rows and columns are 1-based, that is rows are numbered 1, 2, ..., *n* and columns are numbered 1, 2, ..., *n*.... | The first line contains two integers: *n* (1<=β€<=*n*<=β€<=1000) and *m* (1<=β€<=*m*<=β€<=105). The number *m* represents the number of operations you need to perform. Each of the next *m* lines contains five or six integers, depending on operation type.
If the *i*-th operation from the input is a query, the first number... | For each query operation, output on a new line the result. | [
"3 5\n2 1 1 2 2 1\n2 1 3 2 3 2\n2 3 1 3 3 3\n1 2 2 3 3\n1 2 2 3 2\n"
] | [
"3\n2\n"
] | After the first 3 operations, the matrix will look like this:
The fourth operation asks us to compute 1 xor 2 xor 3 xor 3 = 3.
The fifth operation asks us to compute 1 xor 3 = 2. | [] | 92 | 0 | 0 | 261,490 | |
917 | Upside Down | [
"data structures",
"string suffix structures",
"strings",
"trees"
] | null | null | As we all know, Eleven has special abilities. Thus, Hopper convinced her to close the gate to the Upside Down World with her mind. Upside down monsters like to move between the worlds, so they are going to attack Hopper and Eleven in order to make them stop. The monsters live in the vines. The vines form a tree with *n... | The first line of input contains three integers, *n*,<=*m* and *q* (2<=β€<=*n*<=β€<=105, 1<=β€<=*m*,<=*q*<=β€<=105).
The next *n*<=-<=1 lines contain the tunnels (edges). Each line contains two integers *v* and *u* (1<=β€<=*v*,<=*u*<=β€<=*n*, *v*<=β <=*u*) and a lowercase English letter *c*, meaning there's a tunnel connecti... | Print *q* lines. *i*-th line should contain a single integer, the power the *i*-th monster gains after moving. | [
"6 4 5\n1 6 b\n2 3 a\n1 2 b\n5 3 b\n4 5 b\na\nb\nbb\naa\n1 2 1\n6 2 3\n1 6 2\n4 5 4\n1 6 2\n",
"10 6 7\n1 3 s\n10 1 d\n2 6 s\n5 2 d\n7 4 l\n8 9 d\n8 10 l\n7 2 d\n8 7 l\ndl\ndssld\nd\nd\nl\nsl\n4 5 4\n3 7 5\n10 6 2\n3 1 4\n7 5 6\n10 9 4\n9 8 4\n"
] | [
"0\n1\n1\n0\n1\n",
"2\n2\n0\n0\n0\n1\n1\n"
] | none | [] | 15 | 0 | 0 | 261,764 | |
512 | Fox And Polygon | [
"constructive algorithms",
"divide and conquer"
] | null | null | Fox Ciel just designed a puzzle game called "Polygon"! It is played using triangulations of a regular *n*-edge polygon. The goal is to transform one triangulation to another by some tricky rules.
Triangulation of an *n*-edge poylgon is a set of *n*<=-<=3 diagonals satisfying the condition that no two diagonals share a... | The first line contain an integer *n* (4<=β€<=*n*<=β€<=1000), number of edges of the regular polygon.
Then follows two groups of (*n*<=-<=3) lines describing the original triangulation and goal triangulation.
Description of each triangulation consists of (*n*<=-<=3) lines. Each line contains 2 integers *a**i* and *b**... | First, output an integer *k* (0<=β€<=*k*<=β€<=20,<=000): number of steps.
Then output *k* lines, each containing 2 integers *a**i* and *b**i*: the endpoints of a diagonal you are going to flip at step *i*. You may output *a**i* and *b**i* in any order.
If there are several possible solutions, output any of them. | [
"4\n1 3\n2 4\n",
"6\n2 6\n3 6\n4 6\n6 2\n5 2\n4 2\n",
"8\n7 1\n2 7\n7 3\n6 3\n4 6\n6 1\n6 2\n6 3\n6 4\n6 8\n"
] | [
"1\n1 3\n",
"2\n6 3\n6 4\n",
"3\n7 3\n7 2\n7 1"
] | Sample test 2 is discussed above and shown on the picture. | [
{
"input": "4\n1 3\n2 4",
"output": "1\n3 1"
},
{
"input": "6\n2 6\n3 6\n4 6\n6 2\n5 2\n4 2",
"output": "5\n6 2\n6 4\n3 1\n6 3\n5 3"
},
{
"input": "8\n7 1\n2 7\n7 3\n6 3\n4 6\n6 1\n6 2\n6 3\n6 4\n6 8",
"output": "8\n6 3\n7 3\n7 2\n7 1\n7 4\n8 4\n4 1\n4 2"
},
{
"input": "5\n5 ... | 46 | 0 | 0 | 265,538 | |
706 | Working routine | [
"data structures",
"implementation"
] | null | null | Vasiliy finally got to work, where there is a huge amount of tasks waiting for him. Vasiliy is given a matrix consisting of *n* rows and *m* columns and *q* tasks. Each task is to swap two submatrices of the given matrix.
For each task Vasiliy knows six integers *a**i*, *b**i*, *c**i*, *d**i*, *h**i*, *w**i*, where *a... | The first line of the input contains three integers *n*, *m* and *q* (2<=β€<=*n*,<=*m*<=β€<=1000, 1<=β€<=*q*<=β€<=10<=000)Β β the number of rows and columns in matrix, and the number of tasks Vasiliy has to perform.
Then follow *n* lines containing *m* integers *v**i*,<=*j* (1<=β€<=*v**i*,<=*j*<=β€<=109) eachΒ β initial value... | Print *n* lines containing *m* integers eachΒ β the resulting matrix. | [
"4 4 2\n1 1 2 2\n1 1 2 2\n3 3 4 4\n3 3 4 4\n1 1 3 3 2 2\n3 1 1 3 2 2\n",
"4 2 1\n1 1\n1 1\n2 2\n2 2\n1 1 4 1 1 2\n"
] | [
"4 4 3 3\n4 4 3 3\n2 2 1 1\n2 2 1 1\n",
"2 2\n1 1\n2 2\n1 1\n"
] | none | [] | 46 | 0 | 0 | 265,788 | |
358 | Dima and Kicks | [
"brute force",
"dsu",
"graphs",
"implementation"
] | null | null | Dima is a good person. In fact, he's great. But all good things come to an end...
Seryozha is going to kick Dima just few times.. For this reason he divides the room into unit squares. Now the room is a rectangle *n*<=Γ<=*m* consisting of unit squares.
For the beginning, Seryozha put Dima in a center of some square. ... | The first line contains *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=103) β size of the room.
Next *n* lines goes, each contains *m* numbers *a**ij* β Dima's notes: *a**ij*<==<=1, if Dima was staying in the square (*i*,<=*j*) or was flying above it. Otherwise *a**ij*<==<=0.
At least one *a**ij* equals 1. | In a single line in accending order print all *k* (*k*<=><=1), which matches the Dima's notes. If there are no such *k* and Dima invented this story with kicks, print -1. | [
"5 5\n1 1 1 1 1\n1 0 0 0 1\n1 0 0 0 1\n1 0 0 0 1\n1 1 1 1 1\n",
"7 7\n0 0 1 1 1 0 0\n0 0 1 0 1 0 0\n1 1 1 1 1 1 1\n1 0 1 0 1 0 1\n1 1 1 1 1 1 1\n0 0 1 0 1 0 0\n0 0 1 1 1 0 0\n",
"3 3\n1 1 1\n1 1 1\n1 1 1\n",
"4 4\n1 1 1 1\n0 0 0 0\n0 0 0 0\n0 0 0 0\n",
"5 5\n0 0 1 0 0\n0 0 1 0 0\n1 1 1 1 1\n0 0 1 0 0\n0 0 1... | [
"2 4\n",
"2\n",
"-1\n",
"3\n",
"-1\n"
] | none | [
{
"input": "5 5\n1 1 1 1 1\n1 0 0 0 1\n1 0 0 0 1\n1 0 0 0 1\n1 1 1 1 1",
"output": "2 4"
},
{
"input": "7 7\n0 0 1 1 1 0 0\n0 0 1 0 1 0 0\n1 1 1 1 1 1 1\n1 0 1 0 1 0 1\n1 1 1 1 1 1 1\n0 0 1 0 1 0 0\n0 0 1 1 1 0 0",
"output": "2"
},
{
"input": "3 3\n1 1 1\n1 1 1\n1 1 1",
"output": "-1... | 77 | 921,600 | 0 | 266,450 | |
630 | Arrow | [
"geometry"
] | null | null | Petya has recently started working as a programmer in the IT city company that develops computer games.
Besides game mechanics implementation to create a game it is necessary to create tool programs that can be used by game designers to create game levels. Petya's first assignment is to create a tool that allows to pa... | The only line of the input contains eight integers *px*,<=*py*,<=*vx*,<=*vy* (<=-<=1000<=β€<=*px*,<=*py*,<=*vx*,<=*vy*<=β€<=1000,<=*vx*2<=+<=*vy*2<=><=0), *a*,<=*b*,<=*c*,<=*d* (1<=β€<=*a*,<=*b*,<=*c*,<=*d*<=β€<=1000,<=*a*<=><=*c*). | Output coordinates of the arrow points in counter-clockwise order. Each line should contain two coordinates, first *x*, then *y*. Relative or absolute error should not be greater than 10<=-<=9. | [
"8 8 0 2 8 3 4 5\n"
] | [
"8.000000000000 11.000000000000\n4.000000000000 8.000000000000\n6.000000000000 8.000000000000\n6.000000000000 3.000000000000\n10.000000000000 3.000000000000\n10.000000000000 8.000000000000\n12.000000000000 8.000000000000\n"
] | none | [
{
"input": "8 8 0 2 8 3 4 5",
"output": "8.000000000000 11.000000000000\n4.000000000000 8.000000000000\n6.000000000000 8.000000000000\n6.000000000000 3.000000000000\n10.000000000000 3.000000000000\n10.000000000000 8.000000000000\n12.000000000000 8.000000000000"
},
{
"input": "10 10 7 0 5 8 2 11",
... | 61 | 3,072,000 | -1 | 270,985 | |
335 | Rectangles and Square | [
"brute force",
"dp"
] | null | null | You are given *n* rectangles, labeled 1 through *n*. The corners of rectangles have integer coordinates and their edges are parallel to the *Ox* and *Oy* axes. The rectangles may touch each other, but they do not overlap (that is, there are no points that belong to the interior of more than one rectangle).
Your task i... | First line contains a single integer *n* (1<=β€<=*n*<=β€<=105) β the number of rectangles. Each of the next *n* lines contains a description of a rectangle, with the *i*-th such line describing the rectangle labeled *i*. Each rectangle description consists of four integers: *x*1, *y*1, *x*2, *y*2 β coordinates of the bot... | If such a subset exists, print "YES" (without quotes) on the first line of the output file, followed by *k*, the number of rectangles in the subset. On the second line print *k* numbers β the labels of rectangles in the subset in any order. If more than one such subset exists, print any one. If no such subset exists, p... | [
"9\n0 0 1 9\n1 0 9 1\n1 8 9 9\n8 1 9 8\n2 2 3 6\n3 2 7 3\n2 6 7 7\n5 3 7 6\n3 3 5 6\n",
"4\n0 0 1 9\n1 0 9 1\n1 8 9 9\n8 1 9 8\n"
] | [
"YES 5\n5 6 7 8 9\n",
"NO\n"
] | The first test case looks as follows:
Note that rectangles 6, 8, and 9 form a square as well, and would be an acceptable answer.
The second test case looks as follows: | [] | 62 | 0 | 0 | 271,508 | |
1,010 | Tree | [
"fft",
"graphs",
"trees"
] | null | null | The Main Martian Tree grows on Mars. It is a binary tree (a rooted tree, with no more than two sons at each vertex) with $n$ vertices, where the root vertex has the number $1$. Its fruits are the Main Martian Fruits. It's summer now, so this tree does not have any fruit yet.
Autumn is coming soon, and leaves and branc... | The first line contains two integers: $n$ and $x$ ($1 \le n \le 10^5$, $0 \le x \le 10^{18}$)Β β the size of the tree and the number of fruits in the root.
The $i$-th of the following $(n-1)$ lines contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le n$)Β β vertices connected by the $i$-th edge of the tree.
It is... | Print one numberΒ β the number of configurations of the resulting tree modulo $998244353$. | [
"3 2\n1 2\n1 3\n",
"2 5\n1 2\n",
"4 10\n1 2\n1 3\n3 4\n"
] | [
"13\n",
"7\n",
"441\n"
] | Consider the first example. <img class="tex-graphics" src="https://espresso.codeforces.com/5d4507ad53aaf6b231fe52f14f47257b4e5a6677.png" style="max-width: 100.0%;max-height: 100.0%;"/>
There are $2$ fruits at the vertex $1$. The following $13$ options are possible:
- there is no vertex $2$, there is no vertex $3$; <i... | [] | 62 | 0 | 0 | 272,985 | |
316 | Good Substrings | [
"hashing",
"strings"
] | null | null | Smart Beaver recently got interested in a new word game. The point is as follows: count the number of distinct good substrings of some string *s*. To determine if a string is good or not the game uses rules. Overall there are *n* rules. Each rule is described by a group of three (*p*,<=*l*,<=*r*), where *p* is a string... | The first line contains string *s*. The second line contains integer *n*. Next *n* lines contain the rules, one per line. Each of these lines contains a string and two integers *p**i*,<=*l**i*,<=*r**i*, separated by single spaces (0<=β€<=*l**i*<=β€<=*r**i*<=β€<=|*p**i*|). It is guaranteed that all the given strings are no... | Print a single integer β the number of good substrings of string *s*. | [
"aaab\n2\naa 0 0\naab 1 1\n",
"ltntlnen\n3\nn 0 0\nttlneenl 1 4\nlelllt 1 1\n",
"a\n0\n"
] | [
"3\n",
"2\n",
"1\n"
] | There are three good substrings in the first sample test: Β«aabΒ», Β«abΒ» and Β«bΒ».
In the second test only substrings Β«eΒ» and Β«tΒ» are good. | [
{
"input": "aaab\n2\naa 0 0\naab 1 1",
"output": "3"
},
{
"input": "ltntlnen\n3\nn 0 0\nttlneenl 1 4\nlelllt 1 1",
"output": "2"
},
{
"input": "a\n0",
"output": "1"
},
{
"input": "nysnvneyavzcebsbsvrsbcvzsrcr\n5\nycaa 1 3\nzsayyyvseccsbcbvzrr 5 16\nznz 1 3\nbvnzrccvcb 4 7\nse... | 186 | 0 | -1 | 273,200 | |
372 | Choosing Subtree is Fun | [
"binary search",
"data structures",
"dfs and similar",
"trees",
"two pointers"
] | null | null | There is a tree consisting of *n* vertices. The vertices are numbered from 1 to *n*.
Let's define the length of an interval [*l*,<=*r*] as the value *r*<=-<=*l*<=+<=1. The score of a subtree of this tree is the maximum length of such an interval [*l*,<=*r*] that, the vertices with numbers *l*,<=*l*<=+<=1,<=...,<=*r* b... | There are two integers in the first line, *n* and *k* (1<=β€<=*k*<=β€<=*n*<=β€<=105). Each of the next *n*<=-<=1 lines contains integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*,<=*a**i*<=β <=*b**i*). That means *a**i* and *b**i* are connected by a tree edge.
It is guaranteed that the input represents a tree. | Output should contain a single integer β the maximum possible score. | [
"10 6\n4 10\n10 6\n2 9\n9 6\n8 5\n7 1\n4 7\n7 3\n1 8\n",
"16 7\n13 11\n12 11\n2 14\n8 6\n9 15\n16 11\n5 14\n6 15\n4 3\n11 15\n15 14\n10 1\n3 14\n14 7\n1 7\n"
] | [
"3\n",
"6\n"
] | For the first case, there is some subtree whose size is at most 6, including 3 consecutive numbers of vertices. For example, the subtree that consists of {1,β3,β4,β5,β7,β8} or of {1,β4,β6,β7,β8,β10} includes 3 consecutive numbers of vertices. But there is no subtree whose size is at most 6, which includes 4 or more con... | [] | 46 | 0 | 0 | 274,019 | |
960 | Santa's Gift | [
"data structures",
"trees"
] | null | null | Santa has an infinite number of candies for each of $m$ flavours. You are given a rooted tree with $n$ vertices. The root of the tree is the vertex $1$. Each vertex contains exactly one candy. The $i$-th vertex has a candy of flavour $f_i$.
Sometimes Santa fears that candies of flavour $k$ have melted. He chooses any ... | The first line of the input contains four integers $n$ ($2<=\leqslant<=n<=\leqslant<=5 \cdot 10^4$), $m$, $q$, $C$ ($1<=\leqslant<=m, q<=\leqslant<=5 \cdot 10^4$, $0 \leqslant C \leqslant 10^6$) β the number of nodes, total number of different flavours of candies, the number of queries and the price charged by the Bake... | Output the answer to each query of the second type in a separate line.
Your answer is considered correct if its absolute or relative error does not exceed $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. The checker program considers your answer correct if and only if $\frac{|a-b|}{max(1,b)}... | [
"3 5 5 7\n3 1 4\n1 1\n73 1 48 85 89\n2 1\n2 3\n1 2 3\n2 1\n2 3\n"
] | [
"2920.333333333333\n593.000000000000\n49.000000000000\n3217.000000000000\n"
] | For $1$-st query, the error in calculating the cost of replacement for flavour $1$ if vertex $1$, $2$ or $3$ is chosen are $66^2$, $66^2$ and $(-7)^2$ respectively. Since the probability of choosing any vertex is same, therefore the expected value of error is $\frac{66^2+66^2+(-7)^2}{3}$.
Similarly, for $2$-nd query t... | [] | 46 | 0 | 0 | 274,933 | |
871 | Restore the Tree | [
"graphs",
"greedy",
"trees"
] | null | null | Petya had a tree consisting of *n* vertices numbered with integers from 1 to *n*. Accidentally he lost his tree.
Petya remembers information about *k* vertices: distances from each of them to each of the *n* tree vertices.
Your task is to restore any tree that satisfies the information that Petya remembers or report... | The first line contains two integers *n* and *k* (2<=β€<=*n*<=β€<=30<=000, 1<=β€<=*k*<=β€<=*min*(200,<=*n*)) β the number of vertices in the tree and the number of vertices about which Petya remembers distance information.
The following *k* lines contain remembered information. The *i*-th line contains *n* integers *d**i*... | If there are no suitable trees, print -1.
In the other case, print *n*<=-<=1 lines: each line should contain two vertices connected by edge in the required tree. You can print edges and vertices in an edge in any order. The tree vertices are enumerated from 1 to *n*.
If there are many solutions print any of them. | [
"5 2\n0 1 2 3 2\n2 1 0 1 2\n",
"3 1\n1 2 1\n"
] | [
"2 1\n3 2\n4 3\n5 2\n",
"-1\n"
] | Picture for the first sample: | [] | 30 | 0 | 0 | 275,330 | |
700 | Break Up | [
"dfs and similar",
"graphs"
] | null | null | Again, there are hard times in Berland! Many towns have such tensions that even civil war is possible.
There are *n* towns in Reberland, some pairs of which connected by two-way roads. It is not guaranteed that it is possible to reach one town from any other town using these roads.
Towns *s* and *t* announce the fi... | The first line of the input contains two integers *n* and *m* (2<=β€<=*n*<=β€<=1000, 0<=β€<=*m*<=β€<=30<=000)Β β the number of towns in Berland and the number of roads.
The second line contains integers *s* and *t* (1<=β€<=*s*,<=*t*<=β€<=*n*, *s*<=β <=*t*)Β β indices of towns which break up the relationships.
Then follow *m* ... | In the first line print the minimum budget required to break up the relations between *s* and *t*, if it is allowed to close no more than two roads.
In the second line print the value *c* (0<=β€<=*c*<=β€<=2)Β β the number of roads to be closed in the found solution.
In the third line print in any order *c* diverse integ... | [
"6 7\n1 6\n2 1 6\n2 3 5\n3 4 9\n4 6 4\n4 6 5\n4 5 1\n3 1 3\n",
"6 7\n1 6\n2 3 1\n1 2 2\n1 3 3\n4 5 4\n3 6 5\n4 6 6\n1 5 7\n",
"5 4\n1 5\n2 1 3\n3 2 1\n3 4 4\n4 5 2\n",
"2 3\n1 2\n1 2 734458840\n1 2 817380027\n1 2 304764803\n"
] | [
"8\n2\n2 7\n",
"9\n2\n4 5\n",
"1\n1\n2\n",
"-1\n"
] | none | [
{
"input": "6 7\n1 6\n2 1 6\n2 3 5\n3 4 9\n4 6 4\n4 6 5\n4 5 1\n3 1 3",
"output": "8\n2\n2 7"
},
{
"input": "6 7\n1 6\n2 3 1\n1 2 2\n1 3 3\n4 5 4\n3 6 5\n4 6 6\n1 5 7",
"output": "9\n2\n4 5"
},
{
"input": "5 4\n1 5\n2 1 3\n3 2 1\n3 4 4\n4 5 2",
"output": "1\n1\n2"
},
{
"input... | 46 | 0 | 0 | 275,845 | |
981 | Round Marriage | [
"binary search",
"graph matchings",
"greedy"
] | null | null | It's marriage season in Ringland!
Ringland has a form of a circle's boundary of length $L$. There are $n$ bridegrooms and $n$ brides, and bridegrooms decided to marry brides.
Of course, each bridegroom should choose exactly one bride, and each bride should be chosen by exactly one bridegroom.
All objects in Ringland... | The first line contains two integers $n$ and $L$ ($1 \leq n \leq 2 \cdot 10^{5}$, $1 \leq L \leq 10^{9}$)Β β the number of bridegrooms and brides and the length of Ringland.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < L$)Β β the distances from the capital to the castles of bridegrooms i... | In the only line print the smallest possible inconvenience of the wedding, where the inconvenience is the largest distance traveled by a bride. | [
"2 4\n0 1\n2 3\n",
"10 100\n3 14 15 92 65 35 89 79 32 38\n2 71 82 81 82 84 5 90 45 23\n"
] | [
"1\n",
"27\n"
] | In the first example the first bridegroom should marry the second bride, the second bridegroom should marry the first bride. This way, the second bride should walk the distance of $1$, and the first bride should also walk the same distance. Thus, the inconvenience is equal to $1$.
In the second example let $p_i$ be th... | [] | 3,000 | 12,800,000 | 0 | 275,864 | |
412 | E-mail Addresses | [
"implementation"
] | null | null | One of the most important products of the R1 company is a popular @r1.com mail service. The R1 mailboxes receive and send millions of emails every day.
Today, the online news thundered with terrible information. The R1 database crashed and almost no data could be saved except for one big string. The developers assume ... | The first and the only line contains the sequence of characters *s*1*s*2... *s**n* (1<=β€<=*n*<=β€<=106) β the saved string. It is guaranteed that the given string contains only small English letters, digits and characters '.', '_', '@'. | Print in a single line the number of substrings that are valid e-mail addresses. | [
"[emailΒ protected]\n",
"[emailΒ protected]@[emailΒ protected]\n",
"[emailΒ protected]\n",
".asd123__..@\n"
] | [
"18\n",
"8\n",
"1\n",
"0\n"
] | In the first test case all the substrings that are correct e-mail addresses begin from one of the letters of the word agapov and end in one of the letters of the word com.
In the second test case note that the e-mail [[emailΒ protected]](/cdn-cgi/l/email-protection) is considered twice in the answer. Note that in this ... | [
{
"input": "gerald.agapov1991@gmail.com",
"output": "18"
},
{
"input": "x@x.x@x.x_e_@r1.com",
"output": "8"
},
{
"input": "a___@1.r",
"output": "1"
},
{
"input": ".asd123__..@",
"output": "0"
},
{
"input": "@",
"output": "0"
},
{
"input": ".",
"out... | 0 | 0 | -1 | 276,293 | |
434 | Nanami's Power Plant | [
"flows"
] | null | null | Nanami likes playing games, and is also really good at it. This day she was playing a new game which involved operating a power plant. Nanami's job is to control the generators in the plant and produce maximum output.
There are *n* generators in the plant. Each generator should be set to a generating level. Generating... | The first line contains two integers *n* and *m*Β (1<=β€<=*n*<=β€<=50;Β 0<=β€<=*m*<=β€<=100) β the number of generators and the number of restrictions.
Then follow *n* lines, each line contains three integers *a**i*, *b**i*, and *c**i*Β (|*a**i*|<=β€<=10;Β |*b**i*|,<=|*c**i*|<=β€<=1000) β the coefficients of the function *f**i*... | Print a single line containing a single integer β the maximum output of all the generators. It is guaranteed that there exists at least one valid configuration. | [
"3 3\n0 1 0\n0 1 1\n0 1 2\n0 3\n1 2\n-100 100\n1 2 0\n2 3 0\n3 1 0\n",
"5 8\n1 -8 20\n2 -4 0\n-1 10 -10\n0 1 0\n0 -1 1\n1 9\n1 4\n0 10\n3 11\n7 9\n2 1 3\n1 2 3\n2 3 3\n3 2 3\n3 4 3\n4 3 3\n4 5 3\n5 4 3\n"
] | [
"9\n",
"46\n"
] | In the first sample, *f*<sub class="lower-index">1</sub>(*x*)β=β*x*, *f*<sub class="lower-index">2</sub>(*x*)β=β*x*β+β1, and *f*<sub class="lower-index">3</sub>(*x*)β=β*x*β+β2, so we are to maximize the sum of the generating levels. The restrictions are *x*<sub class="lower-index">1</sub>ββ€β*x*<sub class="lower-index">... | [] | 46 | 0 | 0 | 277,280 | |
0 | none | [
"none"
] | D. Falling Anvils | 2 | 256 | For some reason in many American cartoons anvils fall from time to time onto heroes' heads. Of course, safes, wardrobes, cruisers, planes fall sometimes too... But anvils do so most of all.
Anvils come in different sizes and shapes. Quite often they get the hero stuck deep in the ground. But have you ever thought who ... | The first line contains integer *t* (1<=β€<=*t*<=β€<=10000) β amount of testcases.
Each of the following *t* lines contain two space-separated integers *a* and *b* (0<=β€<=*a*,<=*b*<=β€<=106).
Pretests contain all the tests with 0<=<<=*a*<=<<=10,<=0<=β€<=*b*<=<<=10. | Print *t* lines β the probability of a successful anvil hit for each testcase. The absolute or relative error of the answer should not exceed 10<=-<=6. | [
"2\n4 2\n1 2\n"
] | [
"0.6250000000\n0.5312500000\n"
] | none | [] | 248 | 0 | 0 | 278,556 |
407 | Largest Submatrix 3 | [
"dp",
"hashing"
] | null | null | You are given matrix *a* of size *n*<=Γ<=*m*, its elements are integers. We will assume that the rows of the matrix are numbered from top to bottom from 1 to *n*, the columns are numbered from left to right from 1 to *m*. We will denote the element on the intersecting of the *i*-th row and the *j*-th column as *a**ij*.... | The first line contains two integers *n*, *m* (1<=β€<=*n*,<=*m*<=β€<=400)Β β the number of rows and columns of the matrix, correspondingly.
Each of the next *n* lines contains *m* integers *a**ij* (1<=β€<=*a**ij*<=β€<=160000)Β β the elements of the matrix. | Print a single integer β the area of the optimal inhomogenous submatrix. | [
"3 3\n1 3 1\n4 5 6\n2 6 1\n",
"3 4\n5 2 3 1\n3 3 5 3\n4 4 4 5\n",
"2 6\n1 2 3 4 5 6\n8 6 7 8 9 1\n"
] | [
"6\n",
"4\n",
"8\n"
] | none | [] | 3,000 | 2,764,800 | 0 | 278,697 | |
620 | Xors on Segments | [
"data structures",
"strings",
"trees"
] | null | null | You are given an array with *n* integers *a**i* and *m* queries. Each query is described by two integers (*l**j*,<=*r**j*).
Let's define the function . The function is defined for only *u*<=β€<=*v*.
For each query print the maximal value of the function *f*(*a**x*,<=*a**y*) over all *l**j*<=β€<=*x*,<=*y*<=β€<=*r**j*,<= ... | The first line contains two integers *n*,<=*m* (1<=β€<=*n*<=β€<=5Β·104,<= 1<=β€<=*m*<=β€<=5Β·103) β the size of the array and the number of the queries.
The second line contains *n* integers *a**i* (1<=β€<=*a**i*<=β€<=106) β the elements of the array *a*.
Each of the next *m* lines contains two integers *l**j*,<=*r**j* (1<=β€... | For each query print the value *a**j* on a separate line β the maximal value of the function *f*(*a**x*,<=*a**y*) over all *l**j*<=β€<=*x*,<=*y*<=β€<=*r**j*,<= *a**x*<=β€<=*a**y*. | [
"6 3\n1 2 3 4 5 6\n1 6\n2 5\n3 4\n",
"1 1\n1\n1 1\n",
"6 20\n10 21312 2314 214 1 322\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n2 2\n2 3\n2 4\n2 5\n2 6\n3 4\n3 5\n3 6\n4 4\n4 5\n4 6\n5 5\n5 6\n6 6\n"
] | [
"7\n7\n7\n",
"1\n",
"10\n21313\n21313\n21313\n21313\n21313\n21312\n21313\n21313\n21313\n21313\n2314\n2315\n2315\n214\n215\n323\n1\n323\n322\n"
] | none | [
{
"input": "6 3\n1 2 3 4 5 6\n1 6\n2 5\n3 4",
"output": "7\n7\n7"
},
{
"input": "1 1\n1\n1 1",
"output": "1"
},
{
"input": "6 20\n10 21312 2314 214 1 322\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n2 2\n2 3\n2 4\n2 5\n2 6\n3 4\n3 5\n3 6\n4 4\n4 5\n4 6\n5 5\n5 6\n6 6",
"output": "10\n21313\n21313\... | 46 | 0 | 0 | 278,769 | |
596 | Wilbur and Trees | [
"dp",
"math",
"probabilities",
"sortings"
] | null | null | Wilbur the pig really wants to be a beaver, so he decided today to pretend he is a beaver and bite at trees to cut them down.
There are *n* trees located at various positions on a line. Tree *i* is located at position *x**i*. All the given positions of the trees are distinct.
The trees are equal, i.e. each tree has h... | The first line of the input contains two integers, *n* (1<=β€<=*n*<=β€<=2000) and *h* (1<=β€<=*h*<=β€<=108) and a real number *p* (0<=β€<=*p*<=β€<=1), given with no more than six decimal places.
The second line of the input contains *n* integers, *x*1,<=*x*2,<=...,<=*x**n* (<=-<=108<=β€<=*x**i*<=β€<=108) in no particular orde... | Print a single real numberΒ β the expected total length of the ground covered by trees when they have all fallen down. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6.
Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program ... | [
"2 2 0.500000\n1 2\n",
"4 3 0.4\n4 3 1 2\n"
] | [
"3.250000000\n",
"6.631200000\n"
] | Consider the first example, we have 2 trees with height 2.
1. Both trees falls left. This can either happen with the right tree falling left first, which has <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/d0f634778b5b369e93c5e4521921161fd08259cb.png" style="max-width: 100.0%;max-height:... | [] | 1,949 | 268,390,400 | 0 | 279,214 | |
887 | Little Brother | [
"binary search",
"geometry",
"sortings"
] | null | null | Masha's little brother draw two points on a sheet of paper. After that, he draws some circles and gave the sheet to his sister.
Masha has just returned from geometry lesson so she instantly noticed some interesting facts about brother's drawing.
At first, the line going through two points, that brother drew, doesn't... | First line contains four integers *x*1, *y*1, *x*2, *y*2 (<=-<=105<=β€<=*x*1,<=*y*1,<=*x*2,<=*y*2<=β€<=105)Β β coordinates of points that brother drew. First point has coordinates (*x*1, *y*1) and second point has coordinates (*x*2, *y*2). These two points are different.
The second line contains single integer *n* (1<=β€<... | Output smallest real number, that it's possible to draw a circle with such radius through given points in such a way that it doesn't intersect other circles.
The output is considered correct if it has a relative or absolute error of at most 10<=-<=4. | [
"2 4 7 13\n3\n3 0 1\n12 4 2\n-4 14 2\n",
"-2 3 10 -10\n2\n7 0 3\n-5 -5 2\n"
] | [
"5.1478150705",
"9.1481831923"
] | <img class="tex-graphics" src="https://espresso.codeforces.com/5a8f62f2439db62d006a0959f077351937f109a0.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img class="tex-graphics" src="https://espresso.codeforces.com/0500120c641e4aab8bb5490323ce5a68957a6621.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [] | 62 | 0 | 0 | 280,357 | |
919 | A Game With Numbers | [
"games",
"graphs",
"shortest paths"
] | null | null | Imagine that Alice is playing a card game with her friend Bob. They both have exactly $8$ cards and there is an integer on each card, ranging from $0$ to $4$. In each round, Alice or Bob in turns choose two cards from different players, let them be $a$ and $b$, where $a$ is the number on the player's card, and $b$ is t... | The first line contains one positive integer $T$ ($1 \leq T \leq 100\,000$), denoting the number of situations you need to consider.
The following lines describe those $T$ situations. For each situation:
- The first line contains a non-negative integer $f$ ($0 \leq f \leq 1$), where $f = 0$ means that Alice plays fi... | Output $T$ lines. For each situation, determine who wins. Output
- "Alice" (without quotes) if Alice wins. - "Bob" (without quotes) if Bob wins. - "Deal" (without quotes) if it gets into a deal, i.e. no one wins. | [
"4\n1\n0 0 0 0 0 0 0 0\n1 2 3 4 1 2 3 4\n1\n0 0 0 1 0 0 0 0\n0 0 0 0 4 0 0 0\n0\n1 0 0 0 0 0 0 0\n0 0 0 4 0 0 2 0\n1\n1 1 1 1 1 1 1 1\n1 1 1 1 1 1 1 1\n"
] | [
"Alice\nBob\nAlice\nDeal\n"
] | In the first situation, Alice has all her numbers $0$. So she wins immediately.
In the second situation, Bob picks the numbers $4$ and $1$. Because we have $(4 + 1) \bmod 5 = 0$, Bob wins after this operation.
In the third situation, Alice picks the numbers $1$ and $4$. She wins after this operation.
In the fourth s... | [
{
"input": "4\n1\n0 0 0 0 0 0 0 0\n1 2 3 4 1 2 3 4\n1\n0 0 0 1 0 0 0 0\n0 0 0 0 4 0 0 0\n0\n1 0 0 0 0 0 0 0\n0 0 0 4 0 0 2 0\n1\n1 1 1 1 1 1 1 1\n1 1 1 1 1 1 1 1",
"output": "Alice\nBob\nAlice\nDeal"
},
{
"input": "1\n0\n0 2 2 0 1 2 1 2\n1 2 4 3 2 1 1 0",
"output": "Alice"
}
] | 46 | 0 | 0 | 280,497 | |
73 | Need For Brake | [
"binary search",
"greedy",
"sortings"
] | B. Need For Brake | 4 | 256 | Vasya plays the Need For Brake. He plays because he was presented with a new computer wheel for birthday! Now he is sure that he will win the first place in the championship in his favourite racing computer game!
*n* racers take part in the championship, which consists of a number of races. After each race racers are... | The first line contains number *n* (1<=β€<=*n*<=β€<=105) β number of racers. Each of the next *n* lines contains *s**i* and *a**i* β nick of the racer (nonempty string, which consist of no more than 20 lowercase Latin letters) and the racer's points (0<=β€<=*a**i*<=β€<=106). Racers are given in the arbitrary order.
The ne... | Output two numbers β the highest and the lowest place Vasya can take up as a result of the championship. | [
"3\nteama 10\nteamb 20\nteamc 40\n2\n10 20\nteama\n",
"2\nteama 10\nteamb 10\n2\n10 10\nteamb\n"
] | [
"2 3",
"2 2"
] | none | [
{
"input": "3\nteama 10\nteamb 20\nteamc 40\n2\n10 20\nteama",
"output": "2 3"
},
{
"input": "2\nteama 10\nteamb 10\n2\n10 10\nteamb",
"output": "2 2"
},
{
"input": "5\nteamc 8\nteamd 7\nteame 15\nteama 3\nteamb 21\n3\n1 2 3\nteamb",
"output": "1 1"
},
{
"input": "10\njcfdh 1... | 92 | 0 | 0 | 281,589 |
609 | Frogs and mosquitoes | [
"data structures",
"greedy"
] | null | null | There are *n* frogs sitting on the coordinate axis *Ox*. For each frog two values *x**i*,<=*t**i* are known β the position and the initial length of the tongue of the *i*-th frog (it is guaranteed that all positions *x**i* are different). *m* mosquitoes one by one are landing to the coordinate axis. For each mosquito t... | First line contains two integers *n*,<=*m* (1<=β€<=*n*,<=*m*<=β€<=2Β·105) β the number of frogs and mosquitoes.
Each of the next *n* lines contains two integers *x**i*,<=*t**i* (0<=β€<=*x**i*,<=*t**i*<=β€<=109) β the position and the initial length of the tongue of the *i*-th frog. It is guaranteed that all *x**i* are diff... | Print *n* lines. The *i*-th line should contain two integer values *c**i*,<=*l**i* β the number of mosquitoes eaten by the *i*-th frog and the length of the tongue of the *i*-th frog. | [
"4 6\n10 2\n15 0\n6 1\n0 1\n110 10\n1 1\n6 0\n15 10\n14 100\n12 2\n",
"1 2\n10 2\n20 2\n12 1\n"
] | [
"3 114\n1 10\n1 1\n1 2\n",
"1 3\n"
] | none | [
{
"input": "4 6\n10 2\n15 0\n6 1\n0 1\n110 10\n1 1\n6 0\n15 10\n14 100\n12 2",
"output": "3 114\n1 10\n1 1\n1 2"
},
{
"input": "1 2\n10 2\n20 2\n12 1",
"output": "1 3"
},
{
"input": "10 10\n33 2\n922 34\n480 105\n844 5\n739 39\n325 20\n999 88\n462 104\n225 5\n93 4\n13 15\n323 9\n152 20\n... | 108 | 5,324,800 | 0 | 281,921 | |
746 | Music in Car | [
"data structures",
"greedy",
"two pointers"
] | null | null | Sasha reaches the work by car. It takes exactly *k* minutes. On his way he listens to music. All songs in his playlist go one by one, after listening to the *i*-th song Sasha gets a pleasure which equals *a**i*. The *i*-th song lasts for *t**i* minutes.
Before the beginning of his way Sasha turns on some song *x* and... | The first line contains three integers *n*, *w* and *k* (1<=β€<=*w*<=β€<=*n*<=β€<=2Β·105, 1<=β€<=*k*<=β€<=2Β·109)Β β the number of songs in the playlist, the number of songs Sasha can listen to partly and time in minutes which Sasha needs to reach work.
The second line contains *n* positive integers *a*1,<=*a*2,<=...,<=*a**n... | Print the maximum pleasure Sasha can get after listening to the songs on the way to work. | [
"7 2 11\n3 4 3 5 1 4 6\n7 7 3 6 5 3 9\n",
"8 4 20\n5 6 4 3 7 5 4 1\n10 12 5 12 14 8 5 8\n",
"1 1 5\n6\n9\n",
"1 1 3\n4\n7\n"
] | [
"12\n",
"19\n",
"6\n",
"0\n"
] | In the first example Sasha needs to start listening from the song number 2. He should listen to it partly (for 4 minutes), then listen to the song number 3 to the end (for 3 minutes) and then partly listen to the song number 4 (for 3 minutes). After listening to these songs Sasha will get pleasure which equals 4β+β3β+β... | [
{
"input": "7 2 11\n3 4 3 5 1 4 6\n7 7 3 6 5 3 9",
"output": "12"
},
{
"input": "8 4 20\n5 6 4 3 7 5 4 1\n10 12 5 12 14 8 5 8",
"output": "19"
},
{
"input": "1 1 5\n6\n9",
"output": "6"
},
{
"input": "1 1 3\n4\n7",
"output": "0"
},
{
"input": "3 1 5\n2 5 3\n4 4 5"... | 30 | 0 | 0 | 282,819 | |
802 | Send the Fool Further! (medium) | [
"dp",
"trees"
] | null | null | Thank you for helping Heidi! It is now the second of April, but she has been summoned by Jenny again. The pranks do not seem to end...
In the meantime, Heidi has decided that she does not trust her friends anymore. Not too much, anyway. Her relative lack of trust is manifested as follows: whereas previously she would ... | The first line contains two space-separated integers β the number of friends *n* () and the parameter *k* (1<=β€<=*k*<=β€<=105). The next *n*<=-<=1 lines each contain three space-separated integers *u*, *v* and *c* (0<=β€<=*u*,<=*v*<=β€<=*n*<=-<=1, 1<=β€<=*c*<=β€<=104) meaning that *u* and *v* are friends and the cost for tr... | Again, output a single integer β the maximum sum of costs of tickets. | [
"9 3\n0 1 1\n0 2 1\n1 3 2\n1 4 2\n1 5 2\n2 6 3\n2 7 3\n2 8 3\n",
"9 5\n0 1 1\n0 2 1\n1 3 2\n1 4 2\n1 5 2\n2 6 3\n2 7 3\n2 8 3\n",
"11 6\n1 0 7932\n2 1 1952\n3 2 2227\n4 0 9112\n5 4 6067\n6 0 6786\n7 6 3883\n8 4 7137\n9 1 2796\n10 5 6200\n"
] | [
"15\n",
"17\n",
"54092\n"
] | In the first example, the worst-case scenario for Heidi is to visit the friends in the following order: 0,β1,β5,β1,β3,β1,β0,β2,β6,β2,β7,β2,β8. Observe that no friend is visited more than 3 times. | [] | 46 | 0 | 0 | 283,755 | |
891 | Lust | [
"combinatorics",
"math",
"matrices"
] | null | null | A false witness that speaketh lies!
You are given a sequence containing *n* integers. There is a variable *res* that is equal to 0 initially. The following process repeats *k* times.
Choose an index from 1 to *n* uniformly at random. Name it *x*. Add to *res* the multiply of all *a**i*'s such that 1<=β€<=*i*<=β€<=*n*, ... | The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=5000, 1<=β€<=*k*<=β€<=109) β the number of elements and parameter *k* that is specified in the statement.
The second line contains *n* space separated integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=109). | Output a single integerΒ β the value . | [
"2 1\n5 5\n",
"1 10\n80\n",
"2 2\n0 0\n",
"9 4\n0 11 12 9 20 7 8 18 2\n"
] | [
"5",
"10",
"500000003",
"169316356"
] | none | [
{
"input": "2 1\n5 5",
"output": "5"
},
{
"input": "1 10\n80",
"output": "10"
},
{
"input": "2 2\n0 0",
"output": "500000003"
},
{
"input": "9 4\n0 11 12 9 20 7 8 18 2",
"output": "169316356"
},
{
"input": "5 1\n14 2 0 0 1",
"output": "0"
},
{
"input":... | 2,000 | 409,600 | 0 | 284,977 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.