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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
717 | Pokermon League challenge | [
"math",
"probabilities"
] | null | null | Welcome to the world of Pokermon, yellow little mouse-like creatures, who absolutely love playing poker!
Yeah, right…
In the ensuing Pokermon League, there are *n* registered Pokermon trainers, and *t* existing trainer teams each of which belongs to one of two conferences. Since there is a lot of jealousy between t... | The first line of the input contains two integer *n* (4<=≤<=*n*<=≤<=50<=000) and *e* (2<=≤<=*e*<=≤<=100<=000) — the total number of Pokermon trainers and the number of pairs of trainers who hate each other.
Pokermon trainers are numbered from 1 to *n*. Next *e* lines contain two integers *a* and *b* (1<=≤<=*a*,<=*b*<=... | Print two lines. The first line should contain *n* numbers, specifying for each trainer the team he is in.
The second line should contain *T* numbers, specifying the conference for each team (1 or 2). | [
"4 3\n1 2\n2 3\n4 1\n16\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 15\n16\n2 3 4 5 6 7 8 9 10 11 12 13 14 15 17 18\n16\n2 3 4 5 6 7 8 9 10 11 12 13 14 15 18 19\n16\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 19\n"
] | [
"16 15 19 14 \n2 2 2 1 1 1 2 1 1 2 1 1 1 2 2 1 1 1 1 "
] | [] | 0 | 0 | -1 | 71,481 | ||
180 | Name | [
"greedy",
"strings"
] | null | null | Everything got unclear to us in a far away constellation Tau Ceti. Specifically, the Taucetians choose names to their children in a very peculiar manner.
Two young parents abac and bbad think what name to give to their first-born child. They decided that the name will be the permutation of letters of string *s*. To ke... | The first line contains a non-empty string *s* (1<=≤<=|*s*|<=≤<=5000), where |*s*| is its length. The second line contains a non-empty string *t* (1<=≤<=|*t*|<=≤<=5000), where |*t*| is its length. Both strings consist of lowercase Latin letters. | Print the sought name or -1 if it doesn't exist. | [
"aad\naac\n",
"abad\nbob\n",
"abc\ndefg\n",
"czaaab\nabcdef\n"
] | [
"aad\n",
"daab\n",
"-1\n",
"abczaa\n"
] | In the first sample the given string *s* is the sought one, consequently, we do not need to change the letter order there. | [
{
"input": "aad\naac",
"output": "aad"
},
{
"input": "abad\nbob",
"output": "daab"
},
{
"input": "abc\ndefg",
"output": "-1"
},
{
"input": "czaaab\nabcdef",
"output": "abczaa"
},
{
"input": "a\na",
"output": "-1"
},
{
"input": "a\nb",
"output": "-1... | 124 | 2,867,200 | -1 | 71,492 | |
525 | Arthur and Walls | [
"constructive algorithms",
"data structures",
"graphs",
"greedy",
"shortest paths"
] | null | null | Finally it is a day when Arthur has enough money for buying an apartment. He found a great option close to the center of the city with a nice price.
Plan of the apartment found by Arthur looks like a rectangle *n*<=×<=*m* consisting of squares of size 1<=×<=1. Each of those squares contains either a wall (such square ... | The first line of the input contains two integers *n*,<=*m* (1<=≤<=*n*,<=*m*<=≤<=2000) denoting the size of the Arthur apartments.
Following *n* lines each contain *m* symbols — the plan of the apartment.
If the cell is denoted by a symbol "*" then it contains a wall.
If the cell is denoted by a symbol "." then it t... | Output *n* rows each consisting of *m* symbols that show how the Arthur apartment plan should look like after deleting the minimum number of walls in order to make each room (maximum connected area free from walls) be a rectangle.
If there are several possible answers, output any of them. | [
"5 5\n.*.*.\n*****\n.*.*.\n*****\n.*.*.\n",
"6 7\n***.*.*\n..*.*.*\n*.*.*.*\n*.*.*.*\n..*...*\n*******\n",
"4 5\n.....\n.....\n..***\n..*..\n"
] | [
".*.*.\n*****\n.*.*.\n*****\n.*.*.\n",
"***...*\n..*...*\n..*...*\n..*...*\n..*...*\n*******\n",
".....\n.....\n.....\n.....\n"
] | none | [
{
"input": "5 5\n.*.*.\n*****\n.*.*.\n*****\n.*.*.",
"output": ".*.*.\n*****\n.*.*.\n*****\n.*.*."
},
{
"input": "6 7\n***.*.*\n..*.*.*\n*.*.*.*\n*.*.*.*\n..*...*\n*******",
"output": "***...*\n..*...*\n..*...*\n..*...*\n..*...*\n*******"
},
{
"input": "4 5\n.....\n.....\n..***\n..*..",
... | 2,000 | 184,012,800 | 0 | 71,578 | |
0 | none | [
"none"
] | null | null | Limak is a big polar bear. He prepared *n* problems for an algorithmic contest. The *i*-th problem has initial score *p**i*. Also, testers said that it takes *t**i* minutes to solve the *i*-th problem. Problems aren't necessarily sorted by difficulty and maybe harder problems have smaller initial score but it's too lat... | The first line contains one integer *n* (2<=≤<=*n*<=≤<=150<=000) — the number of problems.
The second line contains *n* integers *p*1,<=*p*2,<=...,<=*p**n* (1<=≤<=*p**i*<=≤<=108) — initial scores.
The third line contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (1<=≤<=*t**i*<=≤<=108) where *t**i* is the number of minu... | Print one real value on a single line — the maximum value of *c* that and there is no optimal order with a paradox. 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 wi... | [
"3\n4 3 10\n1 1 8\n",
"4\n7 20 15 10\n7 20 15 10\n",
"2\n10 20\n10 1\n"
] | [
"0.62500000000\n",
"0.31901840491\n",
"1.00000000000\n"
] | In the first sample, there are 3 problems. The first is (4, 1) (initial score is 4 and required time is 1 minute), the second problem is (3, 1) and the third one is (10, 8). The total time is *T* = 1 + 1 + 8 = 10.
Let's show that there is a paradox for *c* = 0.7. Solving problems in order 1, 2, 3 turns out to give the... | [] | 31 | 0 | 0 | 71,665 | |
883 | Road Widening | [
"constructive algorithms",
"greedy",
"implementation"
] | null | null | Mayor of city S just hates trees and lawns. They take so much space and there could be a road on the place they occupy!
The Mayor thinks that one of the main city streets could be considerably widened on account of lawn nobody needs anyway. Moreover, that might help reduce the car jams which happen from time to time o... | The first line contains integer *n* (1<=≤<=*n*<=≤<=2·105) — number of parts of the street.
Each of the following *n* lines contains two integers *s**i*,<=*g**i* (1<=≤<=*s**i*<=≤<=106, 0<=≤<=*g**i*<=≤<=106) — current width of road and width of the lawn on the *i*-th part of the street. | In the first line print the total width of lawns which will be removed.
In the second line print *n* integers *s*'1,<=*s*'2,<=...,<=*s*'*n* (*s**i*<=≤<=*s*'*i*<=≤<=*s**i*<=+<=*g**i*) — new widths of the road starting from the first part and to the last.
If there is no solution, print the only integer -1 in the first ... | [
"3\n4 5\n4 5\n4 10\n",
"4\n1 100\n100 1\n1 100\n100 1\n",
"3\n1 1\n100 100\n1 1\n"
] | [
"16\n9 9 10 \n",
"202\n101 101 101 101 \n",
"-1\n"
] | none | [
{
"input": "3\n4 5\n4 5\n4 10",
"output": "16\n9 9 10 "
},
{
"input": "4\n1 100\n100 1\n1 100\n100 1",
"output": "202\n101 101 101 101 "
},
{
"input": "3\n1 1\n100 100\n1 1",
"output": "-1"
},
{
"input": "10\n21005 10850\n27020 13372\n28183 3724\n22874 13564\n27446 11493\n225... | 62 | 0 | 0 | 71,837 | |
755 | PolandBall and Polygon | [
"data structures"
] | null | null | PolandBall has such a convex polygon with *n* veritces that no three of its diagonals intersect at the same point. PolandBall decided to improve it and draw some red segments.
He chose a number *k* such that *gcd*(*n*,<=*k*)<==<=1. Vertices of the polygon are numbered from 1 to *n* in a clockwise way. PolandBall repe... | There are only two numbers in the input: *n* and *k* (5<=≤<=*n*<=≤<=106, 2<=≤<=*k*<=≤<=*n*<=-<=2, *gcd*(*n*,<=*k*)<==<=1). | You should print *n* values separated by spaces. The *i*-th value should represent number of polygon's sections after drawing first *i* lines. | [
"5 2\n",
"10 3\n"
] | [
"2 3 5 8 11 ",
"2 3 4 6 9 12 16 21 26 31 "
] | The greatest common divisor (gcd) of two integers *a* and *b* is the largest positive integer that divides both *a* and *b* without a remainder.
For the first sample testcase, you should output "2 3 5 8 11". Pictures below correspond to situations after drawing lines. | [
{
"input": "5 2",
"output": "2 3 5 8 11 "
},
{
"input": "10 3",
"output": "2 3 4 6 9 12 16 21 26 31 "
},
{
"input": "17 5",
"output": "2 3 4 6 9 12 16 21 26 31 37 44 51 59 68 77 86 "
},
{
"input": "1337 550",
"output": "2 3 5 8 12 17 22 28 35 43 52 61 71 82 94 107 120 134... | 1,809 | 11,468,800 | 3 | 71,855 | |
432 | Prime Swaps | [
"greedy",
"sortings"
] | null | null | You have an array *a*[1],<=*a*[2],<=...,<=*a*[*n*], containing distinct integers from 1 to *n*. Your task is to sort this array in increasing order with the following operation (you may need to apply it multiple times):
- choose two indexes, *i* and *j* (1<=≤<=*i*<=<<=*j*<=≤<=*n*; (*j*<=-<=*i*<=+<=1) is a prime nu... | The first line contains integer *n* (1<=≤<=*n*<=≤<=105). The next line contains *n* distinct integers *a*[1],<=*a*[2],<=...,<=*a*[*n*] (1<=≤<=*a*[*i*]<=≤<=*n*). | In the first line, print integer *k* (0<=≤<=*k*<=≤<=5*n*) — the number of used operations. Next, print the operations. Each operation must be printed as "*i* *j*" (1<=≤<=*i*<=<<=*j*<=≤<=*n*; (*j*<=-<=*i*<=+<=1) is a prime).
If there are multiple answers, you can print any of them. | [
"3\n3 2 1\n",
"2\n1 2\n",
"4\n4 2 3 1\n"
] | [
"1\n1 3\n",
"0\n",
"3\n2 4\n1 2\n2 4\n"
] | none | [
{
"input": "3\n3 2 1",
"output": "1\n1 3"
},
{
"input": "2\n1 2",
"output": "0"
},
{
"input": "4\n4 2 3 1",
"output": "3\n2 4\n1 2\n2 4"
},
{
"input": "10\n10 9 8 7 6 5 4 3 2 1",
"output": "15\n4 10\n2 4\n1 2\n3 9\n2 3\n4 8\n3 4\n5 7\n4 5\n5 6\n6 7\n8 10\n7 8\n8 9\n9 10"
... | 61 | 0 | 0 | 72,021 | |
717 | Underfail | [
"flows"
] | null | null | You have recently fallen through a hole and, after several hours of unconsciousness, have realized you are in an underground city. On one of your regular, daily walks through the unknown, you have encountered two unusually looking skeletons called Sanz and P’pairus, who decided to accompany you and give you some puzzle... | The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=500) — the length of the crossword. The second line contains the crossword string. The third line contains a single integer *m* (1<=≤<=*m*<=≤<=100) — the number of given words, and next *m* lines contain description of words: each line will have a... | Output single integer — maximum number of points you can get. | [
"6\nabacba\n2\naba 6\nba 3\n3\n"
] | [
"12\n"
] | For example, with the string "abacba", words "aba" (6 points) and "ba" (3 points), and *x* = 3, you can get at most 12 points - the word "aba" appears once ("abacba"), while "ba" appears two times ("abacba"). Note that for *x* = 1, you could get at most 9 points, since you wouldn’t be able to count both "aba" and the f... | [
{
"input": "6\nabacba\n2\naba 6\nba 3\n3",
"output": "12"
},
{
"input": "6\nabacba\n2\naba 6\nba 3\n1",
"output": "9"
},
{
"input": "6\nabacba\n5\naba 6\nba 3\nbac 4\ncb 3\nc 6\n2",
"output": "21"
},
{
"input": "6\nabacba\n5\naba 6\nba 3\nbac 4\ncb 3\nc 6\n1",
"output": "... | 46 | 0 | 0 | 72,372 | |
68 | Half-decay tree | [
"data structures",
"divide and conquer",
"dp",
"math",
"probabilities"
] | D. Half-decay tree | 3 | 256 | Recently Petya has become keen on physics. Anna V., his teacher noticed Petya's interest and gave him a fascinating physical puzzle — a half-decay tree.
A half-decay tree is a complete binary tree with the height *h*. The height of a tree is the length of the path (in edges) from the root to a leaf in the tree. While... | First line will contain two integers *h* and *q* (1<=≤<=*h*<=≤<=30,<=1<=≤<=*q*<=≤<=105). Next *q* lines will contain a query of one of two types:
- add *v* *e*Petya adds *e* electrons to vertex number *v* (1<=≤<=*v*<=≤<=2*h*<=+<=1<=-<=1,<=0<=≤<=*e*<=≤<=104). *v* and *e* are integers.The vertices of the tree are numbe... | For each query decay solution you should output the mathematical expectation of potential of the tree after being desintegrated. The absolute or relative error in the answer should not exceed 10<=-<=4. | [
"1 4\nadd 1 3\nadd 2 10\nadd 3 11\ndecay\n"
] | [
"13.50000000\n"
] | none | [
{
"input": "1 4\nadd 1 3\nadd 2 10\nadd 3 11\ndecay",
"output": "13.50000000"
},
{
"input": "3 6\ndecay\ndecay\nadd 6 872\ndecay\nadd 13 813\nadd 8 531",
"output": "0.00000000\n0.00000000\n872.00000000"
},
{
"input": "3 6\nadd 2 101\nadd 6 830\nadd 11 899\nadd 2 421\ndecay\ndecay",
"... | 62 | 0 | 0 | 72,404 |
77 | Beavermuncher-0xFF | [
"dfs and similar",
"dp",
"dsu",
"greedy",
"trees"
] | C. Beavermuncher-0xFF | 3 | 256 | "Eat a beaver, save a tree!" — That will be the motto of ecologists' urgent meeting in Beaverley Hills.
And the whole point is that the population of beavers on the Earth has reached incredible sizes! Each day their number increases in several times and they don't even realize how much their unhealthy obsession with t... | The first line contains integer *n* — the number of vertices in the tree (1<=≤<=*n*<=≤<=105). The second line contains *n* integers *k**i* (1<=≤<=*k**i*<=≤<=105) — amounts of beavers on corresponding vertices. Following *n*<=-<=1 lines describe the tree. Each line contains two integers separated by space. These integer... | Print the maximum number of beavers munched by the "Beavermuncher-0xFF".
Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use cout (also you may use %I64d). | [
"5\n1 3 1 3 2\n2 5\n3 4\n4 5\n1 5\n4\n",
"3\n2 1 1\n3 2\n1 2\n3\n"
] | [
"6\n",
"2\n"
] | none | [
{
"input": "5\n1 3 1 3 2\n2 5\n3 4\n4 5\n1 5\n4",
"output": "6"
},
{
"input": "3\n2 1 1\n3 2\n1 2\n3",
"output": "2"
},
{
"input": "6\n3 3 1 2 3 5\n2 6\n1 6\n4 5\n5 1\n3 4\n5",
"output": "16"
},
{
"input": "4\n1 1 1 1\n4 1\n2 1\n3 4\n2",
"output": "2"
},
{
"input"... | 122 | 614,400 | 0 | 72,415 |
486 | LIS of Sequence | [
"data structures",
"dp",
"greedy",
"hashing",
"math"
] | null | null | The next "Data Structures and Algorithms" lesson will be about Longest Increasing Subsequence (LIS for short) of a sequence. For better understanding, Nam decided to learn it a few days before the lesson.
Nam created a sequence *a* consisting of *n* (1<=≤<=*n*<=≤<=105) elements *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*... | The first line contains the single integer *n* (1<=≤<=*n*<=≤<=105) denoting the number of elements of sequence *a*.
The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=105). | Print a string consisting of *n* characters. *i*-th character should be '1', '2' or '3' depending on which group among listed above index *i* belongs to. | [
"1\n4\n",
"4\n1 3 2 5\n",
"4\n1 5 2 3\n"
] | [
"3\n",
"3223\n",
"3133\n"
] | In the second sample, sequence *a* consists of 4 elements: {*a*<sub class="lower-index">1</sub>, *a*<sub class="lower-index">2</sub>, *a*<sub class="lower-index">3</sub>, *a*<sub class="lower-index">4</sub>} = {1, 3, 2, 5}. Sequence *a* has exactly 2 longest increasing subsequences of length 3, they are {*a*<sub class=... | [
{
"input": "1\n4",
"output": "3"
},
{
"input": "4\n1 3 2 5",
"output": "3223"
},
{
"input": "4\n1 5 2 3",
"output": "3133"
},
{
"input": "10\n2 2 2 17 8 9 10 17 10 5",
"output": "2221333311"
},
{
"input": "100\n102 157 177 149 138 193 19 74 127 156 128 122 6 101 1... | 77 | 3,276,800 | -1 | 72,556 | |
731 | Video Cards | [
"brute force",
"data structures",
"implementation",
"math",
"number theory"
] | null | null | Little Vlad is fond of popular computer game Bota-2. Recently, the developers announced the new add-on named Bota-3. Of course, Vlad immediately bought only to find out his computer is too old for the new game and needs to be updated.
There are *n* video cards in the shop, the power of the *i*-th video card is equal t... | The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=200<=000) — the number of video cards in the shop.
The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=≤<=*a**i*<=≤<=200<=000) — powers of video cards. | The only line of the output should contain one integer value — the maximum possible total power of video cards working together. | [
"4\n3 2 15 9\n",
"4\n8 2 2 7\n"
] | [
"27\n",
"18\n"
] | In the first sample, it would be optimal to buy video cards with powers 3, 15 and 9. The video card with power 3 should be chosen as the leading one and all other video cards will be compatible with it. Thus, the total power would be 3 + 15 + 9 = 27. If he buys all the video cards and pick the one with the power 2 as t... | [
{
"input": "4\n3 2 15 9",
"output": "27"
},
{
"input": "4\n8 2 2 7",
"output": "18"
},
{
"input": "1\n1",
"output": "1"
},
{
"input": "1\n123819",
"output": "123819"
},
{
"input": "10\n9 6 8 5 5 2 8 9 2 2",
"output": "52"
},
{
"input": "100\n17 23 71 2... | 1,000 | 2,969,600 | 0 | 72,716 | |
873 | Forbidden Indices | [
"dsu",
"string suffix structures",
"strings"
] | null | null | You are given a string *s* consisting of *n* lowercase Latin letters. Some indices in this string are marked as forbidden.
You want to find a string *a* such that the value of |*a*|·*f*(*a*) is maximum possible, where *f*(*a*) is the number of occurences of *a* in *s* such that these occurences end in non-forbidden in... | The first line contains an integer number *n* (1<=≤<=*n*<=≤<=200000) — the length of *s*.
The second line contains a string *s*, consisting of *n* lowercase Latin letters.
The third line contains a string *t*, consisting of *n* characters 0 and 1. If *i*-th character in *t* is 1, then *i* is a forbidden index (otherw... | Print the maximum possible value of |*a*|·*f*(*a*). | [
"5\nababa\n00100\n",
"5\nababa\n00000\n",
"5\nababa\n11111\n"
] | [
"5\n",
"6\n",
"0\n"
] | none | [
{
"input": "5\nababa\n00100",
"output": "5"
},
{
"input": "5\nababa\n00000",
"output": "6"
},
{
"input": "5\nababa\n11111",
"output": "0"
},
{
"input": "100\neebdeddddbecdbddaaecbbaccbecdeacedddcaddcdebedbabbceeeadecadbbeaecdaeabbceacbdbdbbdacebbbccdcbbeedbe\n1101101101110110... | 30 | 0 | 0 | 72,873 | |
397 | On Corruption and Numbers | [
"constructive algorithms",
"implementation",
"math"
] | null | null | Alexey, a merry Berland entrant, got sick of the gray reality and he zealously wants to go to university. There are a lot of universities nowadays, so Alexey is getting lost in the diversity — he has not yet decided what profession he wants to get. At school, he had bad grades in all subjects, and it's only thanks to w... | The first line contains the number of universities *t*, (1<=≤<=*t*<=≤<=1000) Each of the next *t* lines contain three space-separated integers: *n**i*,<=*l**i*,<=*r**i* (1<=≤<=*n**i*,<=*l**i*,<=*r**i*<=≤<=109; *l**i*<=≤<=*r**i*). | For each query print on a single line: either "Yes", if Alexey can enter the university, or "No" otherwise. | [
"2\n5 2 3\n6 4 5\n"
] | [
"Yes\nNo\n"
] | You can pay the donation to the first university with two coins: one of denomination 2 and one of denomination 3 berubleys. The donation to the second university cannot be paid. | [
{
"input": "2\n5 2 3\n6 4 5",
"output": "Yes\nNo"
},
{
"input": "50\n69 6 6\n22 1 1\n23 3 3\n60 13 13\n13 3 3\n7 4 7\n6 1 1\n49 7 9\n68 8 8\n20 2 2\n34 1 1\n79 5 5\n22 1 1\n77 58 65\n10 3 3\n72 5 5\n47 1 1\n82 3 3\n92 8 8\n34 1 1\n42 9 10\n63 14 14\n10 3 3\n38 2 2\n80 6 6\n79 5 5\n53 5 5\n44 7 7\n85... | 62 | 0 | 3 | 73,006 | |
47 | Crossword | [
"implementation"
] | C. Crossword | 2 | 256 | Vasya trains to compose crossword puzzles. He can only compose crosswords of a very simplе type so far. All of them consist of exactly six words; the words can be read only from top to bottom vertically and from the left to the right horizontally. The words are arranged in the form of a rectangular "eight" or infinity ... | Six lines contain the given words. Every word consists of no more than 30 and no less than 3 uppercase Latin letters. | If it is impossible to solve the problem, print Impossible. Otherwise, print the sought crossword. All the empty squares should be marked as dots.
If there can be several solutions to that problem, print the lexicographically minimum one. I.e. the solution where the first line is less than the first line of other solu... | [
"NOD\nBAA\nYARD\nAIRWAY\nNEWTON\nBURN\n",
"AAA\nAAA\nAAAAA\nAAA\nAAA\nAAAAA\n",
"PTC\nJYNYFDSGI\nZGPPC\nIXEJNDOP\nJJFS\nSSXXQOFGJUZ\n"
] | [
"BAA...\nU.I...\nR.R...\nNEWTON\n..A..O\n..YARD\n",
"AAA..\nA.A..\nAAAAA\n..A.A\n..AAA\n",
"JJFS....\nY..S....\nN..X....\nY..X....\nF..Q....\nD..O....\nS..F....\nG..G....\nIXEJNDOP\n...U...T\n...ZGPPC\n"
] | none | [
{
"input": "NOD\nBAA\nYARD\nAIRWAY\nNEWTON\nBURN",
"output": "BAA...\nU.I...\nR.R...\nNEWTON\n..A..O\n..YARD"
},
{
"input": "AAA\nAAA\nAAAAA\nAAA\nAAA\nAAAAA",
"output": "AAA..\nA.A..\nAAAAA\n..A.A\n..AAA"
},
{
"input": "PTC\nJYNYFDSGI\nZGPPC\nIXEJNDOP\nJJFS\nSSXXQOFGJUZ",
"output": ... | 62 | 102,400 | 0 | 73,009 |
713 | Animals and Puzzle | [
"binary search",
"data structures"
] | null | null | Owl Sonya gave a huge lake puzzle of size *n*<=×<=*m* to hedgehog Filya as a birthday present. Friends immediately started to assemble the puzzle, but some parts of it turned out to be empty — there was no picture on them. Parts with picture on it are denoted by 1, while empty parts are denoted by 0. Rows of the puzzle... | The first line of the input contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=1000) — sizes of the puzzle.
Each of the following *n* lines contains *m* integers *a**ij*. Each of them is equal to 1 if the corresponding cell contains a picture and 0 if it's empty.
Next line contains an integer *t* (1<=≤<=*t*<=≤<=1... | Print *t* lines. The *i*-th of them should contain the maximum size of the square consisting of 1-s and lying fully inside the query rectangle. | [
"3 4\n1 1 0 1\n0 1 1 0\n0 1 1 0\n5\n1 1 2 3\n2 1 3 2\n3 2 3 4\n1 1 3 4\n1 2 3 4\n"
] | [
"1\n1\n1\n2\n2\n"
] | none | [] | 0 | 0 | -1 | 73,247 | |
122 | Lucky Substring | [
"brute force",
"implementation"
] | null | null | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya was delivered a string *s*, containing only digits. He needs to find a string that
- ... | The single line contains a non-empty string *s* whose length can range from 1 to 50, inclusive. The string only contains digits. The string can contain leading zeroes. | In the only line print the answer to Petya's problem. If the sought string does not exist, print "-1" (without quotes). | [
"047\n",
"16\n",
"472747\n"
] | [
"4\n",
"-1\n",
"7\n"
] | The lexicographical comparison of strings is performed by the < operator in the modern programming languages. String *x* is lexicographically less than string *y* either if *x* is a prefix of *y*, or exists such *i* (1 ≤ *i* ≤ *min*(|*x*|, |*y*|)), that *x*<sub class="lower-index">*i*</sub> < *y*<sub class="lower... | [
{
"input": "047",
"output": "4"
},
{
"input": "16",
"output": "-1"
},
{
"input": "472747",
"output": "7"
},
{
"input": "1925",
"output": "-1"
},
{
"input": "5486846414848445484",
"output": "4"
},
{
"input": "516160414",
"output": "4"
},
{
"... | 186 | 0 | 3 | 73,327 | |
960 | Full Binary Tree Queries | [
"brute force",
"implementation",
"trees"
] | null | null | You have a full binary tree having infinite levels.
Each node has an initial value. If a node has value *x*, then its left child has value 2·*x* and its right child has value 2·*x*<=+<=1.
The value of the root is 1.
You need to answer *Q* queries.
There are 3 types of queries:
1. Cyclically shift the values of... | The first line contains a single integer *Q* (1<=≤<=*Q*<=≤<=105).
Then *Q* queries follow, one per line:
- Queries of type 1 and 2 have the following format: *T* *X* *K* (1<=≤<=*T*<=≤<=2; 1<=≤<=*X*<=≤<=1018; 0<=≤<=|*K*|<=≤<=1018), where *T* is type of the query.- Queries of type 3 have the following format: 3 *X* (... | For each query of type 3, print the values of all nodes encountered in descending order. | [
"5\n3 12\n1 2 1\n3 12\n2 4 -1\n3 8\n",
"5\n3 14\n1 5 -3\n3 14\n1 3 1\n3 14\n"
] | [
"12 6 3 1 \n12 6 2 1 \n8 4 2 1 \n",
"14 7 3 1 \n14 6 3 1 \n14 6 2 1 \n"
] | Following are the images of the first 4 levels of the tree in the first test case:
Original:
After query 1 2 1:
After query 2 4 -1: | [
{
"input": "5\n3 12\n1 2 1\n3 12\n2 4 -1\n3 8",
"output": "12 6 3 1 \n12 6 2 1 \n8 4 2 1 "
},
{
"input": "5\n3 14\n1 5 -3\n3 14\n1 3 1\n3 14",
"output": "14 7 3 1 \n14 6 3 1 \n14 6 2 1 "
},
{
"input": "6\n3 1\n2 1 0\n3 10\n2 1 -4\n3 10\n2 10 -5",
"output": "1 \n10 5 2 1 \n10 5 2 1 "
... | 78 | 204,800 | 0 | 73,409 | |
274 | Lovely Matrix | [
"dfs and similar",
"graphs",
"greedy",
"sortings"
] | null | null | Lenny had an *n*<=×<=*m* matrix of positive integers. He loved the matrix so much, because each row of the matrix was sorted in non-decreasing order. For the same reason he calls such matrices of integers lovely.
One day when Lenny was at school his little brother was playing with Lenny's matrix in his room. He erased... | The first line of the input contains two positive integers *n* and *m* (1<=≤<=*n*·*m*<=≤<=105). Each of the next *n* lines contains *m* space-separated integers representing the matrix. An integer -1 shows an erased entry of the matrix. All other integers (each of them is between 0 and 109 inclusive) represent filled e... | If there exists no possible reordering of the columns print -1. Otherwise the output should contain *m* integers *p*1,<=*p*2,<=...,<=*p**m* showing the sought permutation of columns. So, the first column of the lovely matrix will be *p*1-th column of the initial matrix, the second column of the lovely matrix will be *p... | [
"3 3\n1 -1 -1\n1 2 1\n2 -1 1\n",
"2 3\n1 2 2\n2 5 4\n",
"2 3\n1 2 3\n3 2 1\n"
] | [
"3 1 2 \n",
"1 3 2 \n",
"-1\n"
] | none | [] | 92 | 0 | 0 | 73,565 | |
0 | none | [
"none"
] | null | null | Sasha and Kolya decided to get drunk with Coke, again. This time they have *k* types of Coke. *i*-th type is characterised by its carbon dioxide concentration . Today, on the party in honour of Sergiy of Vancouver they decided to prepare a glass of Coke with carbon dioxide concentration . The drink should also be tasty... | The first line contains two integers *n*, *k* (0<=≤<=*n*<=≤<=1000, 1<=≤<=*k*<=≤<=106) — carbon dioxide concentration the friends want and the number of Coke types.
The second line contains *k* integers *a*1,<=*a*2,<=...,<=*a**k* (0<=≤<=*a**i*<=≤<=1000) — carbon dioxide concentration of each type of Coke. Some Coke typ... | Print the minimal natural number of liter needed to prepare a glass with carbon dioxide concentration , or -1 if it is impossible. | [
"400 4\n100 300 450 500\n",
"50 2\n100 25\n"
] | [
"2\n",
"3\n"
] | In the first sample case, we can achieve concentration <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/bed0f5c3640139492194728ccc3ac55accf16a8e.png" style="max-width: 100.0%;max-height: 100.0%;"/> using one liter of Coke of types <img align="middle" class="tex-formula" src="https://espresso... | [
{
"input": "400 4\n100 300 450 500",
"output": "2"
},
{
"input": "50 2\n100 25",
"output": "3"
},
{
"input": "500 3\n1000 5 5",
"output": "199"
},
{
"input": "500 1\n1000",
"output": "-1"
},
{
"input": "874 3\n873 974 875",
"output": "2"
},
{
"input": ... | 1,000 | 12,595,200 | 0 | 73,646 | |
900 | Maximum Questions | [
"data structures",
"dp",
"strings"
] | null | null | Vasya wrote down two strings *s* of length *n* and *t* of length *m* consisting of small English letters 'a' and 'b'. What is more, he knows that string *t* has a form "abab...", namely there are letters 'a' on odd positions and letters 'b' on even positions.
Suddenly in the morning, Vasya found that somebody spoiled ... | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=105) — the length of *s*.
The second line contains the string *s* of length *n*. It contains small English letters 'a', 'b' and characters '?' only.
The third line contains a single integer *m* (1<=≤<=*m*<=≤<=105) — the length of *t*. The string *t* contains... | Print the only integer — the minimum number of replacements Vasya has to perform to make the beauty of string *s* the maximum possible. | [
"5\nbb?a?\n1\n",
"9\nab??ab???\n3\n"
] | [
"2\n",
"2\n"
] | In the first sample string *t* has a form 'a'. The only optimal option is to replace all characters '?' by 'a'.
In the second sample using two replacements we can make string equal to "aba?aba??". It is impossible to get more than two occurrences. | [
{
"input": "5\nbb?a?\n1",
"output": "2"
},
{
"input": "9\nab??ab???\n3",
"output": "2"
},
{
"input": "6\nab??ab\n4",
"output": "2"
},
{
"input": "14\n?abaa?abb?b?a?\n3",
"output": "3"
},
{
"input": "17\nb??a?abbbaaababba\n4",
"output": "1"
},
{
"input"... | 31 | 0 | 0 | 73,758 | |
380 | Sereja and Dividing | [
"data structures"
] | null | null | Let's assume that we have a sequence of doubles *a*1,<=*a*2,<=...,<=*a*|*a*| and a double variable *x*. You are allowed to perform the following two-staged operation:
1. choose an index of the sequence element *i* (1<=≤<=*i*<=≤<=|*a*|); 1. consecutively perform assignments: .
Let's use function *g*(*a*,<=*x*) to re... | The first line contains integer |*b*| (1<=≤<=|*b*|<=≤<=3·105) — the length of sequence *b*. The second line contains |*b*| integers *b*1, *b*2, ..., *b*|*b*| (1<=≤<=*b**i*<=≤<=105). | In a single line print a real number — the required sum divided by |*b*|2. Your answer will be considered correct if its absolute or relative error won't exceed 10<=-<=6. | [
"5\n1 2 3 4 1\n"
] | [
"1.238750000000000\n"
] | none | [] | 46 | 0 | 0 | 73,936 | |
938 | Erasing Substrings | [
"bitmasks",
"dp",
"greedy"
] | null | null | You are given a string *s*, initially consisting of *n* lowercase Latin letters. After that, you perform *k* operations with it, where . During *i*-th operation you must erase some substring of length exactly 2*i*<=-<=1 from *s*.
Print the lexicographically minimal string you may obtain after performing *k* such opera... | The only line contains one string *s* consisting of *n* lowercase Latin letters (1<=≤<=*n*<=≤<=5000). | Print the lexicographically minimal string you may obtain after performing *k* operations. | [
"adcbca\n",
"abacabadabacaba\n"
] | [
"aba\n",
"aabacaba\n"
] | Possible operations in examples:
1. adcbca <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> adcba <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0e... | [
{
"input": "adcbca",
"output": "aba"
},
{
"input": "abacabadabacaba",
"output": "aabacaba"
},
{
"input": "a",
"output": "a"
},
{
"input": "b",
"output": "b"
}
] | 46 | 5,529,600 | 0 | 74,128 | |
912 | Prime Gift | [
"binary search",
"dfs and similar",
"math",
"meet-in-the-middle",
"number theory",
"two pointers"
] | null | null | Opposite to Grisha's nice behavior, Oleg, though he has an entire year at his disposal, didn't manage to learn how to solve number theory problems in the past year. That's why instead of Ded Moroz he was visited by his teammate Andrew, who solemnly presented him with a set of *n* distinct prime numbers alongside with a... | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=16).
The next line lists *n* distinct prime numbers *p*1,<=*p*2,<=...,<=*p**n* (2<=≤<=*p**i*<=≤<=100) in ascending order.
The last line gives a single integer *k* (1<=≤<=*k*). It is guaranteed that the *k*-th smallest integer such that all its prime divisors... | Print a single line featuring the *k*-th smallest integer. It's guaranteed that the answer doesn't exceed 1018. | [
"3\n2 3 5\n7\n",
"5\n3 7 11 13 31\n17\n"
] | [
"8\n",
"93\n"
] | The list of numbers with all prime divisors inside {2, 3, 5} begins as follows:
(1, 2, 3, 4, 5, 6, 8, ...)
The seventh number in this list (1-indexed) is eight. | [
{
"input": "3\n2 3 5\n7",
"output": "8"
},
{
"input": "5\n3 7 11 13 31\n17",
"output": "93"
},
{
"input": "2\n41 61\n66",
"output": "550329031716248441"
},
{
"input": "1\n2\n55",
"output": "18014398509481984"
},
{
"input": "7\n2 3 5 7 11 13 17\n2666471",
"outp... | 31 | 0 | 0 | 74,217 | |
978 | Almost Arithmetic Progression | [
"brute force",
"implementation",
"math"
] | null | null | Polycarp likes arithmetic progressions. A sequence $[a_1, a_2, \dots, a_n]$ is called an arithmetic progression if for each $i$ ($1 \le i < n$) the value $a_{i+1} - a_i$ is the same. For example, the sequences $[42]$, $[5, 5, 5]$, $[2, 11, 20, 29]$ and $[3, 2, 1, 0]$ are arithmetic progressions, but $[1, 0, 1]$, $[1... | The first line contains a single integer $n$ $(1 \le n \le 100\,000)$ — the number of elements in $b$.
The second line contains a sequence $b_1, b_2, \dots, b_n$ $(1 \le b_i \le 10^{9})$. | If it is impossible to make an arithmetic progression with described operations, print -1. In the other case, print non-negative integer — the minimum number of elements to change to make the given sequence becomes an arithmetic progression. The only allowed operation is to add/to subtract one from an element (can't us... | [
"4\n24 21 14 10\n",
"2\n500 500\n",
"3\n14 5 1\n",
"5\n1 3 6 9 12\n"
] | [
"3\n",
"0\n",
"-1\n",
"1\n"
] | In the first example Polycarp should increase the first number on $1$, decrease the second number on $1$, increase the third number on $1$, and the fourth number should left unchanged. So, after Polycarp changed three elements by one, his sequence became equals to $[25, 20, 15, 10]$, which is an arithmetic progression.... | [
{
"input": "4\n24 21 14 10",
"output": "3"
},
{
"input": "2\n500 500",
"output": "0"
},
{
"input": "3\n14 5 1",
"output": "-1"
},
{
"input": "5\n1 3 6 9 12",
"output": "1"
},
{
"input": "1\n1000000000",
"output": "0"
},
{
"input": "2\n1000000000 1",
... | 0 | 0 | -1 | 74,222 | |
472 | Design Tutorial: Make It Nondeterministic | [
"greedy"
] | null | null | A way to make a new task is to make it nondeterministic or probabilistic. For example, the hard task of Topcoder SRM 595, Constellation, is the probabilistic version of a convex hull.
Let's try to make a new task. Firstly we will use the following task. There are *n* people, sort them by their name. It is just an ordi... | The first line contains an integer *n* (1<=≤<=*n*<=≤<=105) — the number of people.
The next *n* lines each contains two strings. The *i*-th line contains strings *f**i* and *s**i* (1<=≤<=|*f**i*|,<=|*s**i*|<=≤<=50) — the first name and last name of the *i*-th person. Each string consists only of lowercase English lett... | If it is possible, output "YES", otherwise output "NO". | [
"3\ngennady korotkevich\npetr mitrichev\ngaoyuan chen\n1 2 3\n",
"3\ngennady korotkevich\npetr mitrichev\ngaoyuan chen\n3 1 2\n",
"2\ngalileo galilei\nnicolaus copernicus\n2 1\n",
"10\nrean schwarzer\nfei claussell\nalisa reinford\neliot craig\nlaura arseid\njusis albarea\nmachias regnitz\nsara valestin\nemma... | [
"NO\n",
"YES\n",
"YES\n",
"NO\n",
"YES\n"
] | In example 1 and 2, we have 3 people: tourist, Petr and me (cgy4ever). You can see that whatever handle is chosen, I must be the first, then tourist and Petr must be the last.
In example 3, if Copernicus uses "copernicus" as his handle, everything will be alright. | [
{
"input": "3\ngennady korotkevich\npetr mitrichev\ngaoyuan chen\n1 2 3",
"output": "NO"
},
{
"input": "3\ngennady korotkevich\npetr mitrichev\ngaoyuan chen\n3 1 2",
"output": "YES"
},
{
"input": "2\ngalileo galilei\nnicolaus copernicus\n2 1",
"output": "YES"
},
{
"input": "1... | 264 | 55,193,600 | 3 | 74,241 | |
521 | Shop | [
"greedy"
] | null | null | Vasya plays one very well-known and extremely popular MMORPG game. His game character has *k* skill; currently the *i*-th of them equals to *a**i*. Also this game has a common rating table in which the participants are ranked according to the product of all the skills of a hero in the descending order.
Vasya decided t... | The first line contains three numbers — *k*,<=*n*,<=*m* (1<=≤<=*k*<=≤<=105, 0<=≤<=*m*<=≤<=*n*<=≤<=105) — the number of skills, the number of improvements on sale and the number of them Vasya can afford.
The second line contains *k* space-separated numbers *a**i* (1<=≤<=*a**i*<=≤<=106), the initial values of skills.
N... | The first line should contain a number *l* (0<=≤<=*l*<=≤<=*m*) — the number of improvements you should use.
The second line should contain *l* distinct space-separated numbers *v**i* (1<=≤<=*v**i*<=≤<=*n*) — the indices of improvements in the order in which they should be applied. The improvements are numbered startin... | [
"2 4 3\n13 20\n1 1 14\n1 2 30\n2 1 6\n3 2 2\n"
] | [
"3\n2 3 4\n"
] | none | [
{
"input": "2 4 3\n13 20\n1 1 14\n1 2 30\n2 1 6\n3 2 2",
"output": "3\n2 3 4"
},
{
"input": "1 0 0\n1",
"output": "0"
},
{
"input": "1 1 1\n1\n3 1 8",
"output": "1\n1"
},
{
"input": "1 1 1\n1\n2 1 8",
"output": "1\n1"
},
{
"input": "1 1 1\n1\n1 1 8",
"output":... | 701 | 20,480,000 | 0 | 74,727 | |
963 | Circles of Waiting | [
"math"
] | null | null | A chip was placed on a field with coordinate system onto point (0,<=0).
Every second the chip moves randomly. If the chip is currently at a point (*x*,<=*y*), after a second it moves to the point (*x*<=-<=1,<=*y*) with probability *p*1, to the point (*x*,<=*y*<=-<=1) with probability *p*2, to the point (*x*<=+<=1,<=*y... | First line contains five integers *R*,<=*a*1,<=*a*2,<=*a*3 and *a*4 (0<=≤<=*R*<=≤<=50,<=1<=≤<=*a*1,<=*a*2,<=*a*3,<=*a*4<=≤<=1000).
Probabilities *p**i* can be calculated using formula . | It can be shown that answer for this problem is always a rational number of form , where .
Print *P*·*Q*<=-<=1 modulo 109<=+<=7. | [
"0 1 1 1 1\n",
"1 1 1 1 1\n",
"1 1 2 1 2\n"
] | [
"1",
"666666674",
"538461545"
] | In the first example initially the chip is located at a distance 0 from origin. In one second chip will move to distance 1 is some direction, so distance to origin will become 1.
Answers to the second and the third tests: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4698d4bc5d3f09c8d226... | [] | 46 | 0 | 0 | 75,021 | |
274 | Mirror Room | [
"data structures",
"implementation"
] | null | null | Imagine an *n*<=×<=*m* grid with some blocked cells. The top left cell in the grid has coordinates (1,<=1) and the bottom right cell has coordinates (*n*,<=*m*). There are *k* blocked cells in the grid and others are empty. You flash a laser beam from the center of an empty cell (*x**s*,<=*y**s*) in one of the diagonal... | The first line of the input contains three integers *n*, *m* and *k* (1<=≤<=*n*,<=*m*<=≤<=105,<=0<=≤<=*k*<=≤<=105). Each of the next *k* lines contains two integers *x**i* and *y**i* (1<=≤<=*x**i*<=≤<=*n*,<=1<=≤<=*y**i*<=≤<=*m*) indicating the position of the *i*-th blocked cell.
The last line contains *x**s*, *y**s*... | In the only line of the output print the number of empty cells that the beam goes through at least once.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"3 3 0\n1 2 SW\n",
"7 5 3\n3 3\n4 3\n5 3\n2 1 SE\n"
] | [
"6\n",
"14\n"
] | none | [] | 30 | 0 | 0 | 75,197 | |
711 | ZS and The Birthday Paradox | [
"math",
"number theory",
"probabilities"
] | null | null | ZS the Coder has recently found an interesting concept called the Birthday Paradox. It states that given a random set of 23 people, there is around 50% chance that some two of them share the same birthday. ZS the Coder finds this very interesting, and decides to test this with the inhabitants of Udayland.
In Udayland,... | The first and only line of the input contains two integers *n* and *k* (1<=≤<=*n*<=≤<=1018,<=2<=≤<=*k*<=≤<=1018), meaning that there are 2*n* days in a year and that ZS the Coder wants to interview exactly *k* people. | If the probability of at least two *k* people having the same birthday in 2*n* days long year equals (*A*<=≥<=0, *B*<=≥<=1, ), print the *A* and *B* in a single line.
Since these numbers may be too large, print them modulo 106<=+<=3. Note that *A* and *B* must be coprime before their remainders modulo 106<=+<=3 are t... | [
"3 2\n",
"1 3\n",
"4 3\n"
] | [
"1 8",
"1 1",
"23 128"
] | In the first sample case, there are 2<sup class="upper-index">3</sup> = 8 days in Udayland. The probability that 2 people have the same birthday among 2 people is clearly <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5947a169159fe867f85f3fd8b9690019b48152f5.png" style="max-width: 100.0%;m... | [
{
"input": "3 2",
"output": "1 8"
},
{
"input": "1 3",
"output": "1 1"
},
{
"input": "4 3",
"output": "23 128"
},
{
"input": "1000000000000000000 1000000000000000000",
"output": "906300 906300"
},
{
"input": "59 576460752303423489",
"output": "1 1"
},
{
... | 2,000 | 198,758,400 | 0 | 75,209 | |
671 | Organizing a Race | [
"data structures",
"greedy"
] | null | null | Kekoland is a country with *n* beautiful cities numbered from left to right and connected by *n*<=-<=1 roads. The *i*-th road connects cities *i* and *i*<=+<=1 and length of this road is *w**i* kilometers.
When you drive in Kekoland, each time you arrive in city *i* by car you immediately receive *g**i* liters of gas... | The first line of the input contains two integers *n* and *k* (2<=≤<=*n*<=≤<=100<=000, 0<=≤<=*k*<=≤<=109) — the number of cities in Kekoland and the number of presents you have, respectively.
Next line contains *n*<=-<=1 integers. The *i*-th of them is *w**i* (1<=≤<=*w**i*<=≤<=109) — the length of the road between cit... | Print a single line — the beauty of the most beautiful race you can organize. | [
"4 4\n2 2 2\n1 1 1 1\n",
"8 5\n2 2 2 3 7 3 1\n1 3 1 5 4 0 2 5\n"
] | [
"4\n",
"7\n"
] | In first sample if you give one present to each city then it will be possible to make a race between city 1 and city 4.
In second sample you should add 1 to *g*<sub class="lower-index">5</sub> and 4 to *g*<sub class="lower-index">6</sub>, then it will be possible to make a race between cities 2 and 8. | [] | 30 | 0 | 0 | 75,248 | |
587 | Duff in the Army | [
"data structures",
"trees"
] | null | null | Recently Duff has been a soldier in the army. Malek is her commander.
Their country, Andarz Gu has *n* cities (numbered from 1 to *n*) and *n*<=-<=1 bidirectional roads. Each road connects two different cities. There exist a unique path between any two cities.
There are also *m* people living in Andarz Gu (numbered f... | The first line of input contains three integers, *n*,<=*m* and *q* (1<=≤<=*n*,<=*m*,<=*q*<=≤<=105).
The next *n*<=-<=1 lines contain the roads. Each line contains two integers *v* and *u*, endpoints of a road (1<=≤<=*v*,<=*u*<=≤<=*n*, *v*<=≠<=*u*).
Next line contains *m* integers *c*1,<=*c*2,<=...,<=*c**m* separated ... | For each query, print numbers *k*,<=*p*1,<=*p*2,<=...,<=*p**k* separated by spaces in one line. | [
"5 4 5\n1 3\n1 2\n1 4\n4 5\n2 1 4 3\n4 5 6\n1 5 2\n5 5 10\n2 3 3\n5 3 1\n"
] | [
"1 3\n2 2 3\n0\n3 1 2 4\n1 2\n"
] | Graph of Andarz Gu in the sample case is as follows (ID of people in each city are written next to them): | [
{
"input": "5 4 5\n1 3\n1 2\n1 4\n4 5\n2 1 4 3\n4 5 6\n1 5 2\n5 5 10\n2 3 3\n5 3 1",
"output": "1 3\n2 2 3\n0\n3 1 2 4\n1 2"
},
{
"input": "1 1 1\n1\n1 1 3",
"output": "1 1"
},
{
"input": "5 1 1\n2 3\n3 5\n4 3\n3 1\n5\n4 2 7",
"output": "0"
},
{
"input": "5 5 5\n2 5\n3 2\n2 1... | 46 | 0 | 0 | 75,465 | |
364 | Empty Rectangles | [
"divide and conquer",
"two pointers"
] | null | null | You've got an *n*<=×<=*m* table (*n* rows and *m* columns), each cell of the table contains a "0" or a "1".
Your task is to calculate the number of rectangles with the sides that are parallel to the sides of the table and go along the cell borders, such that the number one occurs exactly *k* times in the rectangle. | The first line contains three space-separated integers *n*, *m* and *k* (1<=≤<=*n*,<=*m*<=≤<=2500, 0<=≤<=*k*<=≤<=6) — the sizes of the table and the required number of numbers one.
Next *n* lines each contains *m* characters "0" or "1". The *i*-th character of the *j*-th line corresponds to the character that is in th... | Print a single number — the number of rectangles that contain exactly *k* numbers one.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"3 3 2\n101\n000\n101\n",
"5 5 1\n00000\n00000\n00100\n00000\n00000\n",
"5 5 6\n01010\n10101\n01010\n10101\n01010\n",
"3 3 0\n001\n010\n000\n",
"4 4 0\n0000\n0101\n0000\n0000\n"
] | [
"8\n",
"81\n",
"12\n",
"15\n",
"52\n"
] | none | [] | 30 | 0 | 0 | 75,490 | |
0 | none | [
"none"
] | null | null | We call a positive integer *x* a *k*-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to *k*. Each digit shou... | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=5·104), indicating the number of queries.
Each of the next *n* lines describes a query, containing three integers *l*, *r* and *k* (1<=≤<=*l*<=≤<=*r*<=≤<=1018, 0<=≤<=*k*<=≤<=9). | For each query print a single number — the answer to the query. | [
"10\n1 100 0\n1 100 1\n1 100 2\n1 100 3\n1 100 4\n1 100 5\n1 100 6\n1 100 7\n1 100 8\n1 100 9\n",
"10\n1 1000 0\n1 1000 1\n1 1000 2\n1 1000 3\n1 1000 4\n1 1000 5\n1 1000 6\n1 1000 7\n1 1000 8\n1 1000 9\n"
] | [
"9\n28\n44\n58\n70\n80\n88\n94\n98\n100\n",
"135\n380\n573\n721\n830\n906\n955\n983\n996\n1000\n"
] | If 1 ≤ *x* ≤ 9, integer *x* is *k*-beautiful if and only if *x* ≤ *k*.
If 10 ≤ *x* ≤ 99, integer *x* = 10*a* + *b* is *k*-beautiful if and only if |*a* - *b*| ≤ *k*, where *a* and *b* are integers between 0 and 9, inclusive.
100 is *k*-beautiful if and only if *k* ≥ 1. | [] | 46 | 0 | 0 | 75,553 | |
778 | Parquet Re-laying | [
"constructive algorithms"
] | null | null | Peter decided to lay a parquet in the room of size *n*<=×<=*m*, the parquet consists of tiles of size 1<=×<=2. When the workers laid the parquet, it became clear that the tiles pattern looks not like Peter likes, and workers will have to re-lay it.
The workers decided that removing entire parquet and then laying it ag... | The first line contains integer *n* and *m*, size of the room (1<=≤<=*n*,<=*m*<=≤<=50). At least one of them is even number.
The following *n* lines contain *m* characters each, the description of the current configuration of the parquet tiles. Each character represents the position of the half-tile. Characters 'L', '... | In the first line output integer *k*, the number of operations. In the next *k* lines output description of operations. The operation is specified by coordinates (row and column) of the left upper half-tile on which the operation is performed.
If there is no solution, output -1 in the first line. | [
"2 3\nULR\nDLR\nLRU\nLRD\n",
"4 3\nULR\nDLR\nLRU\nLRD\nULR\nDUU\nUDD\nDLR"
] | [
"2\n1 2\n1 1\n",
"3\n3 1\n3 2\n2 2"
] | In the first sample test first operation is to rotate two rightmost tiles, after this all tiles lie vertically. Second operation is to rotate two leftmost tiles, after this we will get desired configuration. | [
{
"input": "2 3\nULR\nDLR\nLRU\nLRD",
"output": "2\n1 2\n1 1"
},
{
"input": "4 3\nULR\nDLR\nLRU\nLRD\nULR\nDUU\nUDD\nDLR",
"output": "5\n3 1\n1 2\n3 2\n1 2\n2 2"
},
{
"input": "2 5\nLRUUU\nLRDDD\nLRLRU\nLRLRD",
"output": "3\n1 1\n1 3\n1 1"
},
{
"input": "3 8\nLRULRLRU\nUUDLRL... | 0 | 0 | -1 | 75,715 | |
886 | Symmetric Projections | [
"geometry"
] | null | null | You are given a set of *n* points on the plane. A line containing the origin is called good, if projection of the given set to this line forms a symmetric multiset of points. Find the total number of good lines.
Multiset is a set where equal elements are allowed.
Multiset is called symmetric, if there is a point *P* ... | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=2000) — the number of points in the set.
Each of the next *n* lines contains two integers *x**i* and *y**i* (<=-<=106<=<=≤<=<=*x**i*,<=<=*y**i*<=<=≤<=<=106) — the coordinates of the points. It is guaranteed that no two points coincide. | If there are infinitely many good lines, print -1.
Otherwise, print single integer — the number of good lines. | [
"3\n1 2\n2 1\n3 3\n",
"2\n4 3\n1 2\n"
] | [
"3\n",
"-1\n"
] | Picture to the first sample test:
<img class="tex-graphics" src="https://espresso.codeforces.com/eedc60313be8684bd6169b8b23f0f0afd92479a8.png" style="max-width: 100.0%;max-height: 100.0%;"/>
In the second sample, any line containing the origin is good. | [
{
"input": "3\n1 2\n2 1\n3 3",
"output": "3"
},
{
"input": "2\n4 3\n1 2",
"output": "-1"
},
{
"input": "6\n0 4\n1 5\n2 1\n3 2\n4 3\n5 0",
"output": "5"
},
{
"input": "1\n5 2",
"output": "-1"
},
{
"input": "4\n2 4\n1 2\n0 0\n-2 -4",
"output": "1"
},
{
"... | 342 | 29,696,000 | 0 | 75,798 | |
599 | Sandy and Nuts | [
"bitmasks",
"dp",
"trees"
] | null | null | Rooted tree is a connected graph without any simple cycles with one vertex selected as a root. In this problem the vertex number 1 will always serve as a root.
Lowest common ancestor of two vertices *u* and *v* is the farthest from the root vertex that lies on both the path from *u* to the root and on path from *v* to... | The first line of the input contains three integers *n*, *m* and *q* (1<=≤<=*n*<=≤<=13,<=0<=≤<=*m*<=<<=*n*,<=0<=≤<=*q*<=≤<=100) — the number of vertices, the number of edges and *LCA* triples remembered by Sandy respectively.
Each of the next *m* lines contains two integers *u**i* and *v**i* (1<=≤<=*u**i*,<=*v**i*<... | Print a single integer — the number of trees of size *n* that satisfy all the conditions. | [
"4 0 0\n",
"4 0 1\n3 4 2\n",
"3 1 0\n1 2\n",
"3 0 2\n2 3 2\n2 3 1\n",
"4 1 2\n1 2\n2 2 2\n3 4 2\n"
] | [
"16\n",
"1\n",
"2\n",
"0\n",
"1\n"
] | In the second sample correct answer looks like this:
In the third sample there are two possible trees:
In the fourth sample the answer is 0 because the information about *LCA* is inconsistent. | [] | 30 | 0 | 0 | 75,838 | |
0 | none | [
"none"
] | null | null | A team of students from the city S is sent to the All-Berland Olympiad in Informatics. Traditionally, they go on the train. All students have bought tickets in one carriage, consisting of *n* compartments (each compartment has exactly four people). We know that if one compartment contain one or two students, then they ... | The first line contains integer *n* (1<=≤<=*n*<=≤<=106) — the number of compartments in the carriage. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* showing how many students ride in each compartment (0<=≤<=*a**i*<=≤<=4). It is guaranteed that at least one student is riding in the train. | If no sequence of swapping seats with other people leads to the desired result, print number "-1" (without the quotes). In another case, print the smallest number of people you need to persuade to swap places. | [
"5\n1 2 2 4 3\n",
"3\n4 1 1\n",
"4\n0 3 0 4\n"
] | [
"2\n",
"2\n",
"0\n"
] | none | [] | 61 | 0 | 0 | 75,971 | |
0 | none | [
"none"
] | null | null | A lot of people associate Logo programming language with turtle graphics. In this case the turtle moves along the straight line and accepts commands "T" ("turn around") and "F" ("move 1 unit forward").
You are given a list of commands that will be given to the turtle. You have to change exactly *n* commands from the l... | The first line of input contains a string *commands* — the original list of commands. The string *commands* contains between 1 and 100 characters, inclusive, and contains only characters "T" and "F".
The second line contains an integer *n* (1<=≤<=*n*<=≤<=50) — the number of commands you have to change in the list. | Output the maximum distance from the starting point to the ending point of the turtle's path. The ending point of the turtle's path is turtle's coordinate after it follows all the commands of the modified list. | [
"FT\n1\n",
"FFFTFFF\n2\n"
] | [
"2\n",
"6\n"
] | In the first example the best option is to change the second command ("T") to "F" — this way the turtle will cover a distance of 2 units.
In the second example you have to change two commands. One of the ways to cover maximal distance of 6 units is to change the fourth command and first or last one. | [] | 62 | 0 | 0 | 76,308 | |
109 | Lucky Interval | [
"brute force",
"math"
] | E. Lucky Interval | 4 | 512 | Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya came across an interval of numbers [*a*,<=*a*<=+<=*l*<=-<=1]. Let *F*(*x*) be the numb... | The single line contains two integers *a* and *l* (1<=≤<=*a*,<=*l*<=≤<=109) — the interval's first number and the interval's length correspondingly. | On the single line print number *b* — the answer to the problem. | [
"7 4\n",
"4 7\n"
] | [
"17\n",
"14\n"
] | Consider that [*a*, *b*] denotes an interval of integers; this interval includes the boundaries. That is, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/18b4a6012d95ad18891561410f0314497a578d63.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "7 4",
"output": "17"
},
{
"input": "4 7",
"output": "14"
},
{
"input": "10 10",
"output": "20"
},
{
"input": "47 74",
"output": "147"
},
{
"input": "469 1",
"output": "480"
},
{
"input": "47 74",
"output": "147"
},
{
"input": "1... | 62 | 4,608,000 | 0 | 76,444 |
231 | To Add or Not to Add | [
"binary search",
"sortings",
"two pointers"
] | null | null | A piece of paper contains an array of *n* integers *a*1,<=*a*2,<=...,<=*a**n*. Your task is to find a number that occurs the maximum number of times in this array.
However, before looking for such number, you are allowed to perform not more than *k* following operations — choose an arbitrary element from the array and... | The first line contains two integers *n* and *k* (1<=≤<=*n*<=≤<=105; 0<=≤<=*k*<=≤<=109) — the number of elements in the array and the number of operations you are allowed to perform, correspondingly.
The third line contains a sequence of *n* integers *a*1,<=*a*2,<=...,<=*a**n* (|*a**i*|<=≤<=109) — the initial array. T... | In a single line print two numbers — the maximum number of occurrences of some number in the array after at most *k* allowed operations are performed, and the minimum number that reaches the given maximum. Separate the printed numbers by whitespaces. | [
"5 3\n6 3 4 0 2\n",
"3 4\n5 5 5\n",
"5 3\n3 1 2 2 1\n"
] | [
"3 4\n",
"3 5\n",
"4 2\n"
] | In the first sample your task is to increase the second element of the array once and increase the fifth element of the array twice. Thus, we get sequence 6, 4, 4, 0, 4, where number 4 occurs 3 times.
In the second sample you don't need to perform a single operation or increase each element by one. If we do nothing, w... | [
{
"input": "5 3\n6 3 4 0 2",
"output": "3 4"
},
{
"input": "3 4\n5 5 5",
"output": "3 5"
},
{
"input": "5 3\n3 1 2 2 1",
"output": "4 2"
},
{
"input": "6 0\n3 2 3 2 3 2",
"output": "3 2"
},
{
"input": "10 15\n1 1 1 4 4 1 4 4 1 4",
"output": "10 4"
},
{
... | 92 | 0 | 0 | 76,775 | |
985 | Team Players | [
"combinatorics"
] | null | null | There are $n$ players numbered from $0$ to $n-1$ with ranks. The $i$-th player has rank $i$.
Players can form teams: the team should consist of three players and no pair of players in the team should have a conflict. The rank of the team is calculated using the following algorithm: let $i$, $j$, $k$ be the ranks of pl... | The first line contains two space-separated integers $n$ and $m$ ($3 \le n \le 2 \cdot 10^5$, $0 \le m \le 2 \cdot 10^5$) — the number of players and the number of conflicting pairs.
The second line contains three space-separated integers $A$, $B$ and $C$ ($1 \le A, B, C \le 10^6$) — coefficients for team rank calcula... | Print single integer — the total sum of ranks over all possible teams modulo $2^{64}$. | [
"4 0\n2 3 4\n",
"4 1\n2 3 4\n1 0\n",
"6 4\n1 5 3\n0 3\n3 5\n5 4\n4 3\n"
] | [
"64\n",
"38\n",
"164\n"
] | In the first example all $4$ teams are valid, i.e. triples: {0, 1, 2}, {0, 1, 3}, {0, 2, 3} {1, 2, 3}.
In the second example teams are following: {0, 2, 3}, {1, 2, 3}.
In the third example teams are following: {0, 1, 2}, {0, 1, 4}, {0, 1, 5}, {0, 2, 4}, {0, 2, 5}, {1, 2, 3}, {1, 2, 4}, {1, 2, 5}. | [
{
"input": "4 0\n2 3 4",
"output": "64"
},
{
"input": "4 1\n2 3 4\n1 0",
"output": "38"
},
{
"input": "6 4\n1 5 3\n0 3\n3 5\n5 4\n4 3",
"output": "164"
},
{
"input": "10 9\n1 1 1\n1 0\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9",
"output": "1232"
},
{
"input": "3 0\n2... | 1,809 | 95,436,800 | 0 | 76,819 | |
451 | Predict Outcome of the Game | [
"brute force",
"implementation",
"math"
] | null | null | There are *n* games in a football tournament. Three teams are participating in it. Currently *k* games had already been played.
You are an avid football fan, but recently you missed the whole *k* games. Fortunately, you remember a guess of your friend for these *k* games. Your friend did not tell exact number of wins... | The first line of the input contains a single integer corresponding to number of test cases *t* (1<=≤<=*t*<=≤<=105).
Each of the next *t* lines will contain four space-separated integers *n*,<=*k*,<=*d*1,<=*d*2 (1<=≤<=*n*<=≤<=1012; 0<=≤<=*k*<=≤<=*n*; 0<=≤<=*d*1,<=*d*2<=≤<=*k*) — data for the current test case. | For each test case, output a single line containing either "yes" if it is possible to have no winner of tournament, or "no" otherwise (without quotes). | [
"5\n3 0 0 0\n3 3 0 0\n6 4 1 0\n6 3 3 0\n3 3 3 2\n"
] | [
"yes\nyes\nyes\nno\nno\n"
] | Sample 1. There has not been any match up to now (*k* = 0, *d*<sub class="lower-index">1</sub> = 0, *d*<sub class="lower-index">2</sub> = 0). If there will be three matches (1-2, 2-3, 3-1) and each team wins once, then at the end each team will have 1 win.
Sample 2. You missed all the games (*k* = 3). As *d*<sub class... | [
{
"input": "5\n3 0 0 0\n3 3 0 0\n6 4 1 0\n6 3 3 0\n3 3 3 2",
"output": "yes\nyes\nyes\nno\nno"
}
] | 1,060 | 10,444,800 | 0 | 76,975 | |
385 | Bear and Floodlight | [
"bitmasks",
"dp",
"geometry"
] | null | null | One day a bear lived on the *Oxy* axis. He was afraid of the dark, so he couldn't move at night along the plane points that aren't lit. One day the bear wanted to have a night walk from his house at point (*l*,<=0) to his friend's house at point (*r*,<=0), along the segment of length (*r*<=-<=*l*). Of course, if he wan... | The first line contains three space-separated integers *n*, *l*, *r* (1<=≤<=*n*<=≤<=20; <=-<=105<=≤<=*l*<=≤<=*r*<=≤<=105). The *i*-th of the next *n* lines contain three space-separated integers *x**i*, *y**i*, *a**i* (<=-<=1000<=≤<=*x**i*<=≤<=1000; 1<=≤<=*y**i*<=≤<=1000; 1<=≤<=*a**i*<=≤<=90) — the floodlights' descrip... | Print a single real number — the answer to the problem. The answer will be considered correct if its relative or absolute error doesn't exceed 10<=-<=6. | [
"2 3 5\n3 1 45\n5 1 45\n",
"1 0 1\n1 1 30\n",
"1 0 1\n1 1 45\n",
"1 0 2\n0 2 90\n"
] | [
"2.000000000\n",
"0.732050808\n",
"1.000000000\n",
"2.000000000\n"
] | In the first sample, one of the possible solutions is:
In the second sample, a single solution is:
In the third sample, a single solution is: | [] | 0 | 0 | -1 | 77,070 | |
856 | Masha and Cactus | [
"dp",
"trees"
] | null | null | Masha is fond of cacti. When she was a little girl, she decided to plant a tree. Now Masha wants to make a nice cactus out of her tree.
Recall that tree is a connected undirected graph that has no cycles. Cactus is a connected undirected graph such that each vertex belongs to at most one cycle.
Masha has some additio... | The first line of the input data contains two integers *n* and *m* — the number of vertices in a tree, and the number of additional edges available (3<=≤<=*n*<=≤<=2·105; 0<=≤<=*m*<=≤<=2·105).
Let us describe Masha's tree. It has a root at vertex 1. The second line contains *n*<=-<=1 integers: *p*2,<=*p*3,<=...,<=*p**n... | Output one integer — the maximum beauty of a cactus Masha can achieve. | [
"7 3\n1 1 2 2 3 3\n4 5 1\n6 7 1\n2 3 1\n"
] | [
"2\n"
] | none | [] | 30 | 0 | 0 | 77,185 | |
575 | Fibonotci | [
"data structures",
"math",
"matrices"
] | null | null | Fibonotci sequence is an integer recursive sequence defined by the recurrence relation
Sequence *s* is an infinite and almost cyclic sequence with a cycle of length *N*. A sequence *s* is called almost cyclic with a cycle of length *N* if , for *i*<=≥<=*N*, except for a finite number of values *s**i*, for which (*i*... | The first line contains two numbers *K* and *P*. The second line contains a single number *N*. The third line contains *N* numbers separated by spaces, that represent the first *N* numbers of the sequence *s*. The fourth line contains a single number *M*, the number of values of sequence *s* for which . Each of the fol... | Output should contain a single integer equal to . | [
"10 8\n3\n1 2 1\n2\n7 3\n5 4\n"
] | [
"4\n"
] | none | [] | 46 | 0 | 0 | 77,531 | |
67 | Optical Experiment | [
"binary search",
"data structures",
"dp"
] | D. Optical Experiment | 5 | 256 | Professor Phunsuk Wangdu has performed some experiments on rays. The setup for *n* rays is as follows.
There is a rectangular box having exactly *n* holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from ... | The first line contains *n* (1<=≤<=*n*<=≤<=106), the number of rays. The second line contains *n* distinct integers. The *i*-th integer *x**i* (1<=≤<=*x**i*<=≤<=*n*) shows that the *x**i*-th ray enters from the *i*-th hole. Similarly, third line contains *n* distinct integers. The *i*-th integer *y**i* (1<=≤<=*y**i*<=≤... | Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. | [
"5\n1 4 5 2 3\n3 4 2 1 5\n",
"3\n3 1 2\n2 3 1\n"
] | [
"3\n",
"2\n"
] | For the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected b... | [
{
"input": "5\n1 4 5 2 3\n3 4 2 1 5",
"output": "3"
},
{
"input": "3\n3 1 2\n2 3 1",
"output": "2"
},
{
"input": "5\n1 2 4 5 3\n1 5 4 2 3",
"output": "3"
},
{
"input": "3\n3 1 2\n1 3 2",
"output": "2"
},
{
"input": "7\n1 5 2 7 4 3 6\n6 3 1 2 5 4 7",
"output": ... | 0 | 0 | -1 | 77,585 |
765 | Tree Folding | [
"dfs and similar",
"dp",
"greedy",
"implementation",
"trees"
] | null | null | Vanya wants to minimize a tree. He can perform the following operation multiple times: choose a vertex *v*, and two disjoint (except for *v*) paths of equal length *a*0<==<=*v*, *a*1, ..., *a**k*, and *b*0<==<=*v*, *b*1, ..., *b**k*. Additionally, vertices *a*1, ..., *a**k*, *b*1, ..., *b**k* must not have any neighbou... | The first line of input contains the number of vertices *n* (2<=≤<=*n*<=≤<=2·105).
Next *n*<=-<=1 lines describe edges of the tree. Each of these lines contains two space-separated integers *u* and *v* (1<=≤<=*u*,<=*v*<=≤<=*n*, *u*<=≠<=*v*) — indices of endpoints of the corresponding edge. It is guaranteed that the gi... | If it is impossible to obtain a path, print -1. Otherwise, print the minimum number of edges in a possible path. | [
"6\n1 2\n2 3\n2 4\n4 5\n1 6\n",
"7\n1 2\n1 3\n3 4\n1 5\n5 6\n6 7\n"
] | [
"3\n",
"-1\n"
] | In the first sample case, a path of three edges is obtained after merging paths 2 - 1 - 6 and 2 - 4 - 5.
It is impossible to perform any operation in the second sample case. For example, it is impossible to merge paths 1 - 3 - 4 and 1 - 5 - 6, since vertex 6 additionally has a neighbour 7 that is not present in the co... | [
{
"input": "6\n1 2\n2 3\n2 4\n4 5\n1 6",
"output": "3"
},
{
"input": "7\n1 2\n1 3\n3 4\n1 5\n5 6\n6 7",
"output": "-1"
},
{
"input": "2\n1 2",
"output": "1"
},
{
"input": "3\n3 1\n1 2",
"output": "1"
},
{
"input": "10\n5 10\n7 8\n8 3\n2 6\n3 2\n9 7\n4 5\n10 1\n6 4... | 108 | 3,379,200 | -1 | 77,841 | |
0 | none | [
"none"
] | null | null | Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to solve more problems, so he decided to play a trick on Chris.
There are exactly *s* blocks in Chris's set, each block has a unique number from 1 to *s*. Chris's teacher picks a subset of blocks *X* and keeps it to himself. He will give th... | The first line of input contains a single integer *n* (1<=≤<=*n*<=≤<=5·105), the number of blocks in the set *X*. The next line contains *n* distinct space-separated integers *x*1, *x*2, ..., *x**n* (1<=≤<=*x**i*<=≤<=106), the numbers of the blocks in *X*.
Note: since the size of the input and output could be very lar... | In the first line of output print a single integer *m* (1<=≤<=*m*<=≤<=106<=-<=*n*), the number of blocks in the set *Y*. In the next line output *m* distinct space-separated integers *y*1, *y*2, ..., *y**m* (1<=≤<=*y**i*<=≤<=106), such that the required equality holds. The sets *X* and *Y* should not intersect, i.e. *x... | [
"3\n1 4 5\n",
"1\n1\n"
] | [
"2\n999993 1000000",
"1\n1000000 \n"
] | none | [
{
"input": "3\n1 4 5",
"output": "3\n999996 999997 1000000 "
},
{
"input": "1\n1",
"output": "1\n1000000 "
},
{
"input": "1\n1000000",
"output": "1\n1 "
},
{
"input": "2\n2 999999",
"output": "2\n1 1000000 "
},
{
"input": "9\n1 2 3 100 500000 500001 999901 999997 ... | 842 | 61,337,600 | 0 | 77,935 | |
0 | none | [
"none"
] | null | null | Furlo and Rublo play a game. The table has *n* piles of coins lying on it, the *i*-th pile has *a**i* coins. Furlo and Rublo move in turns, Furlo moves first. In one move you are allowed to:
- choose some pile, let's denote the current number of coins in it as *x*; - choose some integer *y* (0<=≤<=*y*<=<<=*x*; *x... | The first line contains integer *n* (1<=≤<=*n*<=≤<=77777) — the number of piles. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=777777777777) — the sizes of piles. The numbers are separated by single spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in С++... | If both players play optimally well and Furlo wins, print "Furlo", otherwise print "Rublo". Print the answers without the quotes. | [
"1\n1\n",
"2\n1 2\n",
"10\n1 2 3 4 5 6 7 8 9 10\n"
] | [
"Rublo\n",
"Rublo\n",
"Furlo\n"
] | none | [] | 92 | 0 | 0 | 77,996 | |
74 | Hanger | [
"data structures"
] | D. Hanger | 4 | 256 | In one very large and very respectable company there is a cloakroom with a coat hanger. It is represented by *n* hooks, positioned in a row. The hooks are numbered with positive integers from 1 to *n* from the left to the right.
The company workers have a very complicated work schedule. At the beginning of a work day ... | The first line contains two integers *n*, *q* (1<=≤<=*n*<=≤<=109, 1<=≤<=*q*<=≤<=105), which are the number of hooks on the hanger and the number of requests correspondingly. Then follow *q* lines with requests, sorted according to time. The request of the type "0 *i* *j*" (1<=≤<=*i*<=≤<=*j*<=≤<=*n*) — is the director's... | For each director's request in the input data print a single number on a single line — the number of coats hanging on the hooks from the *i*-th one to the *j*-th one inclusive. | [
"9 11\n1\n2\n0 5 8\n1\n1\n3\n0 3 8\n9\n0 6 9\n6\n0 1 9\n"
] | [
"2\n3\n2\n5\n"
] | none | [] | 60 | 0 | 0 | 78,035 |
241 | Friends | [
"binary search",
"bitmasks",
"data structures",
"math"
] | null | null | You have *n* friends and you want to take *m* pictures of them. Exactly two of your friends should appear in each picture and no two pictures should contain the same pair of your friends. So if you have *n*<==<=3 friends you can take 3 different pictures, each containing a pair of your friends.
Each of your friends ha... | The first line of input contains two integers *n* and *m* — the number of friends and the number of pictures that you want to take.
Next line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=109) — the values of attractiveness of the friends. | The only line of output should contain an integer — the optimal total sum of attractiveness of your pictures. | [
"3 1\n1 2 3\n",
"3 2\n1 2 3\n",
"3 3\n1 2 3\n"
] | [
"3\n",
"5\n",
"6\n"
] | none | [] | 92 | 0 | -1 | 78,073 | |
859 | Ordering T-Shirts | [
"greedy"
] | null | null | It's another Start[c]up, and that means there are T-shirts to order. In order to make sure T-shirts are shipped as soon as possible, we've decided that this year we're going to order all of the necessary T-shirts before the actual competition. The top *C* contestants are going to be awarded T-shirts, but we obviously d... | Input will begin with two integers *N* and *C* (1<=≤<=*N*<=≤<=2·105, 1<=≤<=*C*), the number of T-shirt sizes and number of T-shirts to be awarded, respectively.
Following this is a line with 2·*N*<=-<=1 integers, *s*1 through *s*2·*N*<=-<=1 (0<=≤<=*s**i*<=≤<=108). For odd *i*, *s**i* indicates the number of contestant... | Print the minimum number of T-shirts we need to buy. | [
"2 200\n100 250 100\n",
"4 160\n88 69 62 29 58 52 44\n"
] | [
"200\n",
"314\n"
] | In the first example, we can buy 100 of each size. | [
{
"input": "2 200\n100 250 100",
"output": "200"
},
{
"input": "4 160\n88 69 62 29 58 52 44",
"output": "314"
},
{
"input": "1 1000\n1000000",
"output": "1000"
},
{
"input": "10 50\n10 1 9 2 8 3 7 4 6 5 5 6 4 7 3 8 2 9 1",
"output": "95"
},
{
"input": "5 99\n87 0 ... | 61 | 0 | 0 | 78,207 | |
346 | Robot Control | [
"dp",
"graphs",
"shortest paths"
] | null | null | The boss of the Company of Robot is a cruel man. His motto is "Move forward Or Die!". And that is exactly what his company's product do. Look at the behavior of the company's robot when it is walking in the directed graph. This behavior has been called "Three Laws of Robotics":
- Law 1. The Robot will destroy itself ... | The first line contains two integers *n* (1<=≤<=*n*<=≤<=106) — the number of vertices of the graph, and *m* (1<=≤<=*m*<=≤<=106) — the number of edges. Then *m* lines follow, each with two integers *u**i* and *v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*; *v**i*<=≠<=*u**i*), these integers denote that there is a directed edge f... | If there is a way to reach a goal, print the required minimum number of orders in the worst case. Otherwise, print -1. | [
"4 6\n1 2\n2 1\n1 3\n3 1\n2 4\n3 4\n1 4\n",
"4 5\n1 2\n2 1\n1 3\n2 4\n3 4\n1 4\n"
] | [
"1\n",
"1\n"
] | Consider the first test sample. Initially the robot is on vertex 1. So, on the first step the robot can go to vertex 2 or 3. No matter what vertex the robot chooses, mzry1992 must give an order to the robot. This order is to go to vertex 4. If mzry1992 doesn't give an order to the robot at vertex 2 or 3, the robot can ... | [] | 92 | 204,800 | 0 | 78,275 | |
594 | Edo and Magnets | [
"brute force",
"greedy",
"implementation",
"two pointers"
] | null | null | Edo has got a collection of *n* refrigerator magnets!
He decided to buy a refrigerator and hang the magnets on the door. The shop can make the refrigerator with any size of the door that meets the following restrictions: the refrigerator door must be rectangle, and both the length and the width of the door must be pos... | The first line contains two integers *n* and *k* (1<=≤<=*n*<=≤<=100<=000, 0<=≤<=*k*<=≤<=*min*(10,<=*n*<=-<=1)) — the number of magnets that Edo has and the maximum number of magnets Edo may not place on the refrigerator.
Next *n* lines describe the initial plan of placing magnets. Each line contains four integers *x*1... | Print a single integer — the minimum area of the door of refrigerator, which can be used to place at least *n*<=-<=*k* magnets, preserving the relative positions. | [
"3 1\n1 1 2 2\n2 2 3 3\n3 3 4 4\n",
"4 1\n1 1 2 2\n1 9 2 10\n9 9 10 10\n9 1 10 2\n",
"3 0\n1 1 2 2\n1 1 1000000000 1000000000\n1 3 8 12\n"
] | [
"1\n",
"64\n",
"249999999000000001\n"
] | In the first test sample it is optimal to remove either the first or the third magnet. If we remove the first magnet, the centers of two others will lie at points (2.5, 2.5) and (3.5, 3.5). Thus, it is enough to buy a fridge with door width 1 and door height 1, the area of the door also equals one, correspondingly.
In... | [
{
"input": "3 1\n1 1 2 2\n2 2 3 3\n3 3 4 4",
"output": "1"
},
{
"input": "4 1\n1 1 2 2\n1 9 2 10\n9 9 10 10\n9 1 10 2",
"output": "64"
},
{
"input": "3 0\n1 1 2 2\n1 1 1000000000 1000000000\n1 3 8 12",
"output": "249999999000000001"
},
{
"input": "11 8\n9 1 11 5\n2 2 8 12\n3 ... | 46 | 0 | 0 | 78,278 | |
119 | Alternative Reality | [
"geometry"
] | null | null | In the year of 3000 travelling around parallel realities became a routine thing. However one has to take into consideration that travelling like that is highly dangerous as you never know beforehand where you're gonna get...
Little Vasya, for instance, found himself in a gaming reality and now he has to successfully c... | The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=900,<=1<=≤<=*m*<=≤<=100) — the number of energetic spheres and the number of levels in the game correspondingly.
Each of the following *n* lines contains three integers *x**i*, *y**i*, *z**i* (0<=≤<=*x**i*,<=*y**i*,<=*z**i*<=≤<=104) — the coordinates of ... | Print *m* numbers, one per line: the *i*-th line should contain the minimum sum of money needed to complete the *i*-th level. The absolute or relative error should not exceed 10<=-<=6. | [
"4 1\n0 0 0\n0 1 0\n1 0 0\n1 1 0\n0 0 1\n",
"5 3\n0 1 0\n1 0 1\n1 2 1\n2 0 1\n1 3 0\n1 1 1\n1 2 3\n3 0 3\n",
"2 1\n0 20 0\n0 0 0\n0 10 0\n"
] | [
"0.7071067812\n",
"1.6329931619\n1.6366341768\n1.5411035007\n",
"0.0000000000\n"
] | none | [] | 92 | 0 | 0 | 78,577 | |
225 | Snake | [
"bitmasks",
"dfs and similar",
"graphs",
"implementation"
] | null | null | Let us remind you the rules of a very popular game called "Snake" (or sometimes "Boa", "Python" or "Worm").
The game field is represented by an *n*<=×<=*m* rectangular table. Some squares of the field are considered impassable (walls), all other squares of the fields are passable.
You control a snake, the snake consi... | The first line contains two space-separated integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=15) — the number of rows and columns of the game field.
Next *n* lines describe the game field. Each of these lines contains *m* characters. Character "#" represents a wall, "." is a passable square, "@" is an apple. The snake's firs... | Print a single integer to the output — the minimum number of moves needed to reach the apple. If the snake can't reach the apple, print -1. | [
"4 5\n##...\n..1#@\n432#.\n...#.\n",
"4 4\n#78#\n.612\n.543\n..@.\n",
"3 2\n3@\n2#\n1#\n"
] | [
"4\n",
"6\n",
"-1\n"
] | none | [] | 60 | 0 | 0 | 78,580 | |
757 | Can Bash Save the Day? | [
"data structures",
"divide and conquer",
"graphs",
"trees"
] | null | null | Whoa! You did a great job helping Team Rocket who managed to capture all the Pokemons sent by Bash. Meowth, part of Team Rocket, having already mastered the human language, now wants to become a master in programming as well. He agrees to free the Pokemons if Bash can answer his questions.
Initially, Meowth gives Bash... | The first line contains two integers *n* and *q* (1<=≤<=*n*<=≤<=2·105, 1<=≤<=*q*<=≤<=2·105) — the number of nodes in the tree and the number of queries, respectively.
The next line contains *n* space-separated integers — the sequence *a*1,<=*a*2,<=...,<=*a**n* which is a permutation of 1,<=2,<=...,<=*n*.
Each of the ... | For each query of type 1, output a single integer in a separate line, denoting the answer to the query. | [
"5 5\n4 5 1 3 2\n4 2 4\n1 3 9\n4 1 4\n4 5 2\n1\n1 5 4\n1\n22 20 20\n2\n38\n2\n39\n1\n36 38 38\n"
] | [
"23\n37\n28\n"
] | In the sample, the actual queries are the following:
- 1 1 5 4 - 1 1 3 3 - 2 3 - 2 2 - 1 1 3 3 | [] | 0 | 0 | -1 | 78,607 | |
832 | Strange Radiation | [
"binary search",
"implementation",
"math"
] | null | null | *n* people are standing on a coordinate axis in points with positive integer coordinates strictly less than 106. For each person we know in which direction (left or right) he is facing, and his maximum speed.
You can put a bomb in some point with non-negative integer coordinate, and blow it up. At this moment all peop... | The first line contains two integers *n* and *s* (2<=≤<=*n*<=≤<=105, 2<=≤<=*s*<=≤<=106) — the number of people and the rays' speed.
The next *n* lines contain the description of people. The *i*-th of these lines contains three integers *x**i*, *v**i* and *t**i* (0<=<<=*x**i*<=<<=106, 1<=≤<=*v**i*<=<<=*s*, 1<=... | Print the minimum time needed for both points 0 and 106 to be reached.
Your answer is considered correct if its absolute or relative error doesn't exceed 10<=-<=6. Namely, if your answer is *a*, and the jury's answer is *b*, then your answer is accepted, if . | [
"2 999\n400000 1 2\n500000 1 1\n",
"2 1000\n400000 500 1\n600000 500 2\n"
] | [
"500000.000000000000000000000000000000\n",
"400.000000000000000000000000000000\n"
] | In the first example, it is optimal to place the bomb at a point with a coordinate of 400000. Then at time 0, the speed of the first person becomes 1000 and he reaches the point 10<sup class="upper-index">6</sup> at the time 600. The bomb will not affect on the second person, and he will reach the 0 point at the time 5... | [
{
"input": "2 999\n400000 1 2\n500000 1 1",
"output": "500000.000000000000000000000000000000"
},
{
"input": "2 1000\n400000 500 1\n600000 500 2",
"output": "400.000000000000000000000000000000"
},
{
"input": "2 99999\n500 1 1\n499 10000 2",
"output": "99.950100000000000000088817841970... | 30 | 0 | 0 | 78,866 | |
0 | none | [
"none"
] | null | null | A newspaper is published in Walrusland. Its heading is *s*1, it consists of lowercase Latin letters. Fangy the little walrus wants to buy several such newspapers, cut out their headings, glue them one to another in order to get one big string. After that walrus erase several letters from this string in order to get a n... | The input data contain two lines. The first line contain the heading *s*1, the second line contains the word *s*2. The lines only consist of lowercase Latin letters (1<=≤<=|*s*1|<=≤<=104,<=1<=≤<=|*s*2|<=≤<=106). | If it is impossible to get the word *s*2 in the above-described manner, print "-1" (without the quotes). Otherwise, print the least number of newspaper headings *s*1, which Fangy will need to receive the word *s*2. | [
"abc\nxyz\n",
"abcd\ndabc\n"
] | [
"-1\n",
"2\n"
] | none | [
{
"input": "abc\nxyz",
"output": "-1"
},
{
"input": "abcd\ndabc",
"output": "2"
},
{
"input": "ab\nbabaaab",
"output": "5"
},
{
"input": "ab\nbaaabba",
"output": "6"
},
{
"input": "fbaaigiihhfaahgdbddgeggjdeigfadhfddja\nhbghjgijijcdafcbgiedichdeebaddfddb",
"ou... | 186 | 10,240,000 | -1 | 78,878 | |
859 | Circle of Numbers | [
"math"
] | null | null | *n* evenly spaced points have been marked around the edge of a circle. There is a number written at each point. You choose a positive real number *k*. Then you may repeatedly select a set of 2 or more points which are evenly spaced, and either increase all numbers at points in the set by *k* or decrease all numbers at ... | The first line of input contains an integer *n* (3<=≤<=*n*<=≤<=100000), the number of points along the circle.
The following line contains a string *s* with exactly *n* digits, indicating the numbers initially present at each of the points, in clockwise order. | Print "YES" (without quotes) if there is some sequence of operations that results in all numbers being 0, otherwise "NO" (without quotes).
You can print each letter in any case (upper or lower). | [
"30\n000100000100000110000000001100\n",
"6\n314159\n"
] | [
"YES\n",
"NO\n"
] | If we label the points from 1 to *n*, then for the first test case we can set *k* = 1. Then we increase the numbers at points 7 and 22 by 1, then decrease the numbers at points 7, 17, and 27 by 1, then decrease the numbers at points 4, 10, 16, 22, and 28 by 1. | [
{
"input": "30\n000100000100000110000000001100",
"output": "YES"
},
{
"input": "6\n314159",
"output": "NO"
},
{
"input": "3\n000",
"output": "YES"
},
{
"input": "15\n522085220852208",
"output": "YES"
},
{
"input": "300\n51849955123882532841766314023795544655059625... | 62 | 921,600 | 0 | 78,956 | |
678 | Lena and Queries | [
"data structures",
"divide and conquer",
"geometry"
] | null | null | Lena is a programmer. She got a task to solve at work.
There is an empty set of pairs of integers and *n* queries to process. Each query is one of three types:
1. Add a pair (*a*,<=*b*) to the set. 1. Remove a pair added in the query number *i*. All queries are numbered with integers from 1 to *n*. 1. For a given ... | The first line of input contains integer *n* (1<=≤<=*n*<=≤<=3·105) — the number of queries.
Each of the next *n* lines starts with integer *t* (1<=≤<=*t*<=≤<=3) — the type of the query.
A pair of integers *a* and *b* (<=-<=109<=≤<=*a*,<=*b*<=≤<=109) follows in the query of the first type.
An integer *i* (1<=≤<=*i*<=... | For the queries of the third type print on a separate line the desired maximal value of *x*·*q*<=+<=*y*.
If there are no pairs in the set print "EMPTY SET". | [
"7\n3 1\n1 2 3\n3 1\n1 -1 100\n3 1\n2 4\n3 1\n"
] | [
"EMPTY SET\n5\n99\n5\n"
] | none | [
{
"input": "7\n3 1\n1 2 3\n3 1\n1 -1 100\n3 1\n2 4\n3 1",
"output": "EMPTY SET\n5\n99\n5"
},
{
"input": "5\n1 -1 2\n1 0 -1\n2 2\n1 -2 -1\n3 -1",
"output": "3"
},
{
"input": "3\n1 -2 2\n1 -3 0\n3 -1",
"output": "4"
},
{
"input": "3\n1 0 -2\n1 2 -2\n3 1",
"output": "0"
},... | 140 | 7,065,600 | 0 | 79,243 | |
271 | Three Horses | [
"constructive algorithms",
"math",
"number theory"
] | null | null | There are three horses living in a horse land: one gray, one white and one gray-and-white. The horses are really amusing animals, which is why they adore special cards. Each of those cards must contain two integers, the first one on top, the second one in the bottom of the card. Let's denote a card with *a* on the top ... | The first line contains two integers *n*,<=*m* (1<=≤<=*n*<=≤<=105,<=2<=≤<=*m*<=≤<=109). The second line contains the sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (2<=≤<=*a**i*<=≤<=109). Note, that the numbers in the sequence can coincide.
The numbers in the lines are separated by single spaces. | Print a single integer — the answer to the problem.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"1 6\n2\n",
"1 6\n7\n",
"2 10\n13 7\n"
] | [
"11\n",
"14\n",
"36\n"
] | none | [] | 60 | 0 | 0 | 79,268 | |
187 | BRT Contract | [
"data structures"
] | null | null | In the last war of PMP, he defeated all his opponents and advanced to the final round. But after the end of semi-final round evil attacked him from behind and killed him! God bless him.
Before his death, PMP signed a contract with the bus rapid transit (BRT) that improves public transportations by optimizing time of ... | The first line of input contains three space-separated positive integers *n*,<=*g*,<=*r* (1<=≤<=*n*<=≤<=105,<=2<=≤<=*g*<=+<=*r*<=≤<=109) — the number of intersections, duration of green phase and duration of red phase. Next line contains *n*<=+<=1 integers *l**i* (1<=≤<=*l**i*<=≤<=109) — the time to pass the *i*-th roa... | In the *i*-th line of output you should print a single integer — the time that *i*-th bus gets to destination.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier. | [
"1 3 2\n5 2\n5\n1\n2\n3\n4\n5\n",
"5 3 7\n10 1 1 8 900000005 1000000000\n3\n1\n10\n1000000000\n"
] | [
"8\n9\n12\n12\n12\n",
"1900000040\n1900000040\n2900000030\n"
] | In the first sample, buses #1, #2 and #5 will reach the destination without waiting behind the red light. But buses #3 and #4 should wait.
In the second sample, first bus should wait at third, fourth and fifth intersections. Second and third buses should wait only at the fifth intersection. | [] | 92 | 0 | -1 | 79,288 | |
204 | Little Elephant and Strings | [
"data structures",
"implementation",
"string suffix structures",
"two pointers"
] | null | null | The Little Elephant loves strings very much.
He has an array *a* from *n* strings, consisting of lowercase English letters. Let's number the elements of the array from 1 to *n*, then let's denote the element number *i* as *a**i*. For each string *a**i* (1<=≤<=*i*<=≤<=*n*) the Little Elephant wants to find the number ... | The first line contains two space-separated integers — *n* and *k* (1<=≤<=*n*,<=*k*<=≤<=105). Next *n* lines contain array *a*. The *i*-th line contains a non-empty string *a**i*, consisting of lowercase English letter. The total length of all strings *a**i* does not exceed 105. | On a single line print *n* space-separated integers — the *i*-th number is the answer for string *a**i*.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"3 1\nabc\na\nab\n",
"7 4\nrubik\nfurik\nabab\nbaba\naaabbbababa\nabababababa\nzero\n"
] | [
"6 1 3 \n",
"1 0 9 9 21 30 0 \n"
] | Let's assume that you 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 the string's *i*-th character as *a*<sub class="lower-index">*i*</sub>.
A substring *a*[*l*... *r*] (1 ≤ *l*... | [] | 248 | 819,200 | -1 | 79,348 | |
314 | Sereja and Squares | [
"dp"
] | null | null | Sereja painted *n* points on the plane, point number *i* (1<=≤<=*i*<=≤<=*n*) has coordinates (*i*,<=0). Then Sereja marked each point with a small or large English letter. Sereja don't like letter "x", so he didn't use it to mark points. Sereja thinks that the points are marked beautifully if the following conditions h... | The first line contains integer *n* the number of points (1<=≤<=*n*<=≤<=105). The second line contains a sequence consisting of *n* small English letters and question marks — the sequence of letters, that mark points, in order of increasing *x*-coordinate of points. Question marks denote the points without letters (Pet... | In a single line print the answer to the problem modulo 4294967296. If there is no way to return the removed letters, print number 0.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"4\na???\n",
"4\nabc?\n",
"6\nabc???\n"
] | [
"50\n",
"0\n",
"1\n"
] | none | [] | 62 | 0 | 0 | 79,379 | |
587 | Duff as a Queen | [
"data structures"
] | null | null | Duff is the queen of her country, Andarz Gu. She's a competitive programming fan. That's why, when he saw her minister, Malek, free, she gave her a sequence consisting of *n* non-negative integers, *a*1,<=*a*2,<=...,<=*a**n* and asked him to perform *q* queries for her on this sequence.
There are two types of queries:... | The first line of input contains two integers, *n* and *q* (1<=≤<=*n*<=≤<=2<=×<=105 and 1<=≤<=*q*<=≤<=4<=×<=104).
The second line of input contains *n* integers, *a*1,<=*a*2,<=...,<=*a**n* separated by spaces (0<=≤<=*a**i*<=≤<=109 for each 1<=≤<=*i*<=≤<=*n*).
The next *q* lines contain the queries. Each line starts w... | Print the answer of each query of the second type in one line. | [
"5 5\n1 2 3 4 2\n2 1 5\n1 2 2 8\n2 1 5\n1 1 3 10\n2 2 2\n"
] | [
"8\n16\n1\n"
] | In the first query, we want all Kheshtaks of sequence 1, 2, 3, 4, 2 which are: 0, 1, 2, 3, 4, 5, 6, 7.
In the third query, we want all Khestaks of sequence 1, 10, 3, 4, 2 which are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15.
In the fifth query, we want all Kheshtaks of sequence 0 which is 0. | [] | 46 | 0 | 0 | 79,468 | |
35 | Parade | [
"data structures",
"sortings"
] | E. Parade | 2 | 64 | No Great Victory anniversary in Berland has ever passed without the war parade. This year is not an exception. That’s why the preparations are on in full strength. Tanks are building a line, artillery mounts are ready to fire, soldiers are marching on the main square... And the air forces general Mr. Generalov is in tr... | The first input line contains integer *n* (1<=≤<=*n*<=≤<=100000). Then follow *n* lines, each containing three integers *h**i*, *l**i*, *r**i* (1<=≤<=*h**i*<=≤<=109,<=<=-<=109<=≤<=*l**i*<=<<=*r**i*<=≤<=109). | In the first line output integer *m* — amount of vertices of the enveloping polyline. The next *m* lines should contain 2 integers each — the position and the height of the polyline’s vertex. Output the coordinates of each vertex in the order of traversing the polyline from west to east. Remember that the first and the... | [
"2\n3 0 2\n4 1 3\n",
"5\n3 -3 0\n2 -1 1\n4 2 4\n2 3 7\n3 6 8\n"
] | [
"6\n0 0\n0 3\n1 3\n1 4\n3 4\n3 0\n",
"14\n-3 0\n-3 3\n0 3\n0 2\n1 2\n1 0\n2 0\n2 4\n4 4\n4 2\n6 2\n6 3\n8 3\n8 0\n"
] | none | [
{
"input": "2\n3 0 2\n4 1 3",
"output": "6\n0 0\n0 3\n1 3\n1 4\n3 4\n3 0"
},
{
"input": "5\n3 -3 0\n2 -1 1\n4 2 4\n2 3 7\n3 6 8",
"output": "14\n-3 0\n-3 3\n0 3\n0 2\n1 2\n1 0\n2 0\n2 4\n4 4\n4 2\n6 2\n6 3\n8 3\n8 0"
},
{
"input": "7\n5 -5 -4\n3 -3 0\n2 -1 1\n1 0 1\n4 2 4\n2 3 7\n3 6 8",... | 77 | 2,867,200 | -1 | 79,481 |
223 | Partial Sums | [
"combinatorics",
"math",
"number theory"
] | null | null | You've got an array *a*, consisting of *n* integers. The array elements are indexed from 1 to *n*. Let's determine a two step operation like that:
1. First we build by the array *a* an array *s* of partial sums, consisting of *n* elements. Element number *i* (1<=≤<=*i*<=≤<=*n*) of array *s* equals . The operation *x*... | The first line contains two space-separated integers *n* and *k* (1<=≤<=*n*<=≤<=2000, 0<=≤<=*k*<=≤<=109). The next line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* — elements of the array *a* (0<=≤<=*a**i*<=≤<=109). | Print *n* integers — elements of the array *a* after the operations are applied to it. Print the elements in the order of increasing of their indexes in the array *a*. Separate the printed numbers by spaces. | [
"3 1\n1 2 3\n",
"5 0\n3 14 15 92 6\n"
] | [
"1 3 6\n",
"3 14 15 92 6\n"
] | none | [
{
"input": "3 1\n1 2 3",
"output": "1 3 6"
},
{
"input": "5 0\n3 14 15 92 6",
"output": "3 14 15 92 6"
},
{
"input": "1 1\n3",
"output": "3"
},
{
"input": "1 0\n0",
"output": "0"
},
{
"input": "1 0\n123",
"output": "123"
},
{
"input": "1 1\n0",
"ou... | 218 | 2,252,800 | -1 | 79,790 | |
0 | none | [
"none"
] | null | null | On the math lesson a teacher asked each pupil to come up with his own lucky numbers. As a fan of number theory Peter chose prime numbers. Bob was more original. He said that number *t* is his lucky number, if it can be represented as:
Now, the boys decided to find out how many days of the interval [*l*,<=*r*] (*l*<=≤... | The first line of the input contains integer numbers *l*,<=*r* (1<=≤<=*l*,<=*r*<=≤<=3·108). | In the only line print the number of days on the segment [*l*,<=*r*], which are lucky for Peter and Bob at the same time. | [
"3 5\n",
"6 66\n"
] | [
"1\n",
"7\n"
] | none | [] | 46 | 0 | 0 | 79,856 | |
730 | Olympiad in Programming and Sports | [
"dp",
"flows",
"graphs",
"greedy"
] | null | null | There are *n* students at Berland State University. Every student has two skills, each measured as a number: *a**i* — the programming skill and *b**i* — the sports skill.
It is announced that an Olympiad in programming and sports will be held soon. That's why Berland State University should choose two teams: one to ta... | The first line contains three positive integer numbers *n*, *p* and *s* (2<=≤<=*n*<=≤<=3000, *p*<=+<=*s*<=≤<=*n*) — the number of students, the size of the programming team and the size of the sports team.
The second line contains *n* positive integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=3000), where *a**i* i... | In the first line, print the the maximum strength of the university on the Olympiad. In the second line, print *p* numbers — the members of the programming team. In the third line, print *s* numbers — the members of the sports team.
The students are numbered from 1 to *n* as they are given in the input. All numbers pr... | [
"5 2 2\n1 3 4 5 2\n5 3 2 1 4\n",
"4 2 2\n10 8 8 3\n10 7 9 4\n",
"5 3 1\n5 2 5 1 7\n6 3 1 6 3\n"
] | [
"18\n3 4 \n1 5 \n",
"31\n1 2 \n3 4 \n",
"23\n1 3 5 \n4 \n"
] | none | [
{
"input": "5 2 2\n1 3 4 5 2\n5 3 2 1 4",
"output": "18\n3 4 \n1 5 "
},
{
"input": "4 2 2\n10 8 8 3\n10 7 9 4",
"output": "31\n1 2 \n3 4 "
},
{
"input": "5 3 1\n5 2 5 1 7\n6 3 1 6 3",
"output": "23\n1 3 5 \n4 "
},
{
"input": "2 1 1\n100 101\n1 100",
"output": "200\n1 \n2 ... | 77 | 6,246,400 | 3 | 79,945 | |
724 | Xor-matic Number of the Graph | [
"bitmasks",
"graphs",
"math",
"number theory",
"trees"
] | null | null | You are given an undirected graph, constisting of *n* vertices and *m* edges. Each edge of the graph has some non-negative integer written on it.
Let's call a triple (*u*,<=*v*,<=*s*) interesting, if 1<=≤<=*u*<=<<=*v*<=≤<=*n* and there is a path (possibly non-simple, i.e. it can visit the same vertices and edges mu... | The first line of the input contains two integers *n* and *m* (1<=≤<=*n*<=≤<=100<=000, 0<=≤<=*m*<=≤<=200<=000) — numbers of vertices and edges in the given graph.
The follow *m* lines contain three integers *u**i*, *v**i* and *t**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*, 0<=≤<=*t**i*<=≤<=1018, *u**i*<=≠<=*v**i*) — vertices c... | Print the single integer, equal to the described sum over modulo 109<=+<=7. | [
"4 4\n1 2 1\n1 3 2\n2 3 3\n3 4 1\n",
"4 4\n1 2 1\n2 3 2\n3 4 4\n4 1 8\n",
"8 6\n1 2 2\n2 3 1\n2 4 4\n4 5 5\n4 6 3\n7 8 5\n"
] | [
"12\n",
"90\n",
"62\n"
] | In the first example the are 6 interesting triples:
1. (1, 2, 1) 1. (1, 3, 2) 1. (1, 4, 3) 1. (2, 3, 3) 1. (2, 4, 2) 1. (3, 4, 1)
In the second example the are 12 interesting triples:
1. (1, 2, 1) 1. (2, 3, 2) 1. (1, 3, 3) 1. (3, 4, 4) 1. (2, 4, 6) 1. (1, 4, 7) 1. (1, 4, 8) 1. (2, 4, 9) 1. (3, 4, 11... | [
{
"input": "4 4\n1 2 1\n1 3 2\n2 3 3\n3 4 1",
"output": "12"
},
{
"input": "4 4\n1 2 1\n2 3 2\n3 4 4\n4 1 8",
"output": "90"
},
{
"input": "8 6\n1 2 2\n2 3 1\n2 4 4\n4 5 5\n4 6 3\n7 8 5",
"output": "62"
},
{
"input": "10 20\n1 2 0\n4 3 3\n6 8 7\n10 1 4\n3 8 0\n10 7 0\n9 7 9\n... | 31 | 0 | 0 | 79,967 | |
17 | Palisection | [
"strings"
] | E. Palisection | 2 | 128 | In an English class Nick had nothing to do at all, and remembered about wonderful strings called palindromes. We should remind you that a string is called a palindrome if it can be read the same way both from left to right and from right to left. Here are examples of such strings: «eye», «pop», «level», «aba», «deed», ... | The first input line contains integer *n* (1<=≤<=*n*<=≤<=2·106) — length of the text. The following line contains *n* lower-case Latin letters (from a to z). | In the only line output the amount of different pairs of two subpalindromes that cross each other. Output the answer modulo 51123987. | [
"4\nbabb\n",
"2\naa\n"
] | [
"6\n",
"2\n"
] | none | [] | 124 | 0 | 0 | 80,005 |
652 | Ants on a Circle | [
"constructive algorithms",
"math"
] | null | null | *n* ants are on a circle of length *m*. An ant travels one unit of distance per one unit of time. Initially, the ant number *i* is located at the position *s**i* and is facing in the direction *d**i* (which is either L or R). Positions are numbered in counterclockwise order starting from some point. Positions of the al... | The first line contains three integers *n*, *m* and *t* (2<=≤<=*n*<=≤<=3·105,<=2<=≤<=*m*<=≤<=109,<=0<=≤<=*t*<=≤<=1018) — the number of ants, the length of the circle and the number of time units.
Each of the next *n* lines contains integer *s**i* and symbol *d**i* (1<=≤<=*s**i*<=≤<=*m* and *d**i* is either L or R) — t... | Print *n* integers *x**j* — the position of the *j*-th ant after *t* units of time. The ants are numbered from 1 to *n* in order of their appearing in input. | [
"2 4 8\n1 R\n3 L\n",
"4 8 6\n6 R\n5 L\n1 R\n8 L\n",
"4 8 2\n1 R\n5 L\n6 L\n8 R\n"
] | [
"1 3\n",
"7 4 2 7\n",
"3 3 4 2\n"
] | none | [
{
"input": "2 4 8\n1 R\n3 L",
"output": "1 3"
},
{
"input": "4 8 6\n6 R\n5 L\n1 R\n8 L",
"output": "7 4 2 7"
},
{
"input": "4 8 2\n1 R\n5 L\n6 L\n8 R",
"output": "3 3 4 2"
},
{
"input": "10 10 90\n2 R\n1 R\n3 L\n4 R\n7 L\n8 L\n6 R\n9 R\n5 R\n10 L",
"output": "10 9 1 2 5 6... | 2,000 | 57,548,800 | 0 | 80,202 | |
51 | Three Base Stations | [
"binary search",
"greedy"
] | C. Three Base Stations | 1 | 256 | The New Vasjuki village is stretched along the motorway and that's why every house on it is characterized by its shift relative to some fixed point — the *x**i* coordinate. The village consists of *n* houses, the *i*-th house is located in the point with coordinates of *x**i*.
TELE3, a cellular communication provider ... | The first line contains an integer *n* (1<=≤<=*n*<=≤<=2·105) which represents the number of houses in the village. The second line contains the coordinates of houses — the sequence *x*1,<=*x*2,<=...,<=*x**n* of integer numbers (1<=≤<=*x**i*<=≤<=109). It is possible that two or more houses are located on one point. The ... | Print the required minimal power *d*. In the second line print three numbers — the possible coordinates of the base stations' location. Print the coordinates with 6 digits after the decimal point. The positions of the stations can be any from 0 to 2·109 inclusively. It is accepted for the base stations to have matching... | [
"4\n1 2 3 4\n",
"3\n10 20 30\n",
"5\n10003 10004 10001 10002 1\n"
] | [
"0.500000\n1.500000 2.500000 3.500000\n",
"0\n10.000000 20.000000 30.000000\n",
"0.500000\n1.000000 10001.500000 10003.500000\n"
] | none | [
{
"input": "4\n1 2 3 4",
"output": "0.500000\n1.500000 2.500000 3.500000"
},
{
"input": "3\n10 20 30",
"output": "0\n10.000000 20.000000 30.000000"
},
{
"input": "5\n10003 10004 10001 10002 1",
"output": "0.500000\n1.000000 10001.500000 10003.500000"
},
{
"input": "1\n1",
... | 436 | 16,793,600 | 3.750719 | 80,278 |
0 | none | [
"none"
] | null | null | Group of Berland scientists, with whom you have a close business relationship, makes a research in the area of peaceful nuclear energy. In particular, they found that a group of four nanobots, placed on a surface of a plate, can run a powerful chain reaction under certain conditions.
To be precise, researchers introd... | The first line contains an integer number *t* (1<=≤<=*t*<=≤<=50) — the number of plates.
*t* descriptions of plates follow. A description of each plate consists of four lines. Each line consists of a pair of integers numbers *x**i*,<=*y**i* (<=-<=108<=≤<=*x**i*,<=*y**i*<=≤<=108) — coordinates of the next bot. All bots... | Print answers for all plates separately. First goes a single integer number in a separate line. If scientists have made an unfortunate mistake and nanobots are not able to form the desired square, print -1. Otherwise, print the minimum possible length of the longest bot's path.
If a solution exists, in the next four l... | [
"2\n1 1\n1 -1\n-1 1\n-1 -1\n1 1\n2 2\n4 4\n6 6\n"
] | [
"0\n1 1\n1 -1\n-1 1\n-1 -1\n-1\n"
] | none | [
{
"input": "2\n1 1\n1 -1\n-1 1\n-1 -1\n1 1\n2 2\n4 4\n6 6",
"output": "0\n1 1\n1 -1\n-1 1\n-1 -1\n-1"
},
{
"input": "1\n31 85\n9 49\n52 57\n11 85",
"output": "15\n37 85\n9 57\n37 57\n9 85"
},
{
"input": "1\n183 65\n100 121\n138 31\n81 31",
"output": "49\n183 114\n100 114\n183 31\n100... | 15 | 0 | 0 | 80,325 | |
149 | Martian Clock | [
"implementation"
] | null | null | Having stayed home alone, Petya decided to watch forbidden films on the Net in secret. "What ungentlemanly behavior!" — you can say that, of course, but don't be too harsh on the kid. In his country films about the Martians and other extraterrestrial civilizations are forbidden. It was very unfair to Petya as he adored... | The first line contains a single string as "*a*:*b*" (without the quotes). There *a* is a non-empty string, consisting of numbers and uppercase Latin letters. String *a* shows the number of hours. String *b* is a non-empty string that consists of numbers and uppercase Latin letters. String *b* shows the number of minut... | Print the radixes of the numeral systems that can represent the time "*a*:*b*" in the increasing order. Separate the numbers with spaces or line breaks. If there is no numeral system that can represent time "*a*:*b*", print the single integer 0. If there are infinitely many numeral systems that can represent the time "... | [
"11:20\n",
"2A:13\n",
"000B:00001\n"
] | [
"3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22",
"0\n",
"-1\n"
] | Let's consider the first sample. String "11:20" can be perceived, for example, as time 4:6, represented in the ternary numeral system or as time 17:32 in hexadecimal system.
Let's consider the second sample test. String "2A:13" can't be perceived as correct time in any notation. For example, let's take the base-11 nu... | [
{
"input": "11:20",
"output": "3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22"
},
{
"input": "2A:13",
"output": "0"
},
{
"input": "000B:00001",
"output": "-1"
},
{
"input": "00000:00000",
"output": "-1"
},
{
"input": "70:00",
"output": "0"
},
{
"... | 154 | 0 | 3 | 80,347 | |
559 | Gerald and Path | [
"dp",
"sortings"
] | null | null | The main walking trail in Geraldion is absolutely straight, and it passes strictly from the north to the south, it is so long that no one has ever reached its ends in either of the two directions. The Geraldionians love to walk on this path at any time, so the mayor of the city asked the Herald to illuminate this path ... | The first line contains integer *n* (1<=≤<=*n*<=≤<=100) — the number of spotlights. Each of the *n* lines contains two space-separated integers, *a**i* and *l**i* (0<=≤<=*a**i*<=≤<=108, 1<=≤<=*l**i*<=≤<=108). Number *a**i* shows how much further the *i*-th spotlight to the north, and number *l**i* shows the length of t... | Print a single integer — the maximum total length of the illuminated part of the path. | [
"3\n1 1\n2 2\n3 3\n",
"4\n1 2\n3 3\n4 3\n6 2\n"
] | [
"5\n",
"9\n"
] | none | [
{
"input": "3\n1 1\n2 2\n3 3",
"output": "5"
},
{
"input": "4\n1 2\n3 3\n4 3\n6 2",
"output": "9"
},
{
"input": "5\n3 3\n4 1\n2 2\n0 3\n9 5",
"output": "13"
},
{
"input": "5\n3 3\n4 3\n6 4\n2 3\n1 5",
"output": "14"
},
{
"input": "5\n1 2\n7 5\n9 4\n5 1\n3 5",
... | 61 | 0 | 0 | 80,374 | |
909 | Coprocessor | [
"dfs and similar",
"dp",
"graphs",
"greedy"
] | null | null | You are given a program you want to execute as a set of tasks organized in a dependency graph. The dependency graph is a directed acyclic graph: each task can depend on results of one or several other tasks, and there are no directed circular dependencies between tasks. A task can only be executed if all tasks it depen... | The first line contains two space-separated integers *N* (1<=≤<=*N*<=≤<=105) — the total number of tasks given, and *M* (0<=≤<=*M*<=≤<=105) — the total number of dependencies between tasks.
The next line contains *N* space-separated integers . If *E**i*<==<=0, task *i* can only be executed on the main processor, other... | Output one line containing an integer — the minimal number of coprocessor calls necessary to execute the program. | [
"4 3\n0 1 0 1\n0 1\n1 2\n2 3\n",
"4 3\n1 1 1 0\n0 1\n0 2\n3 0\n"
] | [
"2\n",
"1\n"
] | In the first test, tasks 1 and 3 can only be executed on the coprocessor. The dependency graph is linear, so the tasks must be executed in order 3 -> 2 -> 1 -> 0. You have to call coprocessor twice: first you call it for task 3, then you execute task 2 on the main processor, then you call it for for task 1, an... | [
{
"input": "4 3\n0 1 0 1\n0 1\n1 2\n2 3",
"output": "2"
},
{
"input": "4 3\n1 1 1 0\n0 1\n0 2\n3 0",
"output": "1"
},
{
"input": "10 39\n0 1 0 1 0 1 1 0 1 1\n0 1\n0 2\n0 3\n0 4\n0 5\n0 6\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n3 4\n3 6\n3 7\n3 8\n3 9\n... | 311 | 30,822,400 | 0 | 80,516 | |
390 | Inna and Sweet Matrix | [
"constructive algorithms"
] | null | null | Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".
Inna sees an *n*<=×<=*m* matrix and *k* candies. We'll index the matrix rows from 1 to *n* and the matrix columns from 1 to *m*. We'll represent the cell in the *i*-th row and *j*-th column as (*i*,<=*j*). Two cells (*i*,<=*j*) a... | The first line of the input contains three integers *n*, *m* and *k* (1<=≤<=*n*,<=*m*<=≤<=50,<=1<=≤<=*k*<=≤<=*n*·*m*). | In the first line print an integer — Inna's minimum penalty in the game.
In the next *k* lines print the description of the path for each candy. The description of the path of the candy that is placed *i*-th should follow on the *i*-th line. The description of a path is a sequence of cells. Each cell must be written i... | [
"4 4 4\n"
] | [
"8\n(1,1) (2,1) (2,2)\n(1,1) (1,2)\n(1,1) (2,1)\n(1,1)\n"
] | Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), ... | [
{
"input": "4 4 4",
"output": "8\n(1,1) (2,1) (2,2)\n(1,1) (1,2)\n(1,1) (2,1)\n(1,1)"
},
{
"input": "1 1 1",
"output": "1\n(1,1)"
},
{
"input": "1 50 50",
"output": "1275\n(1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) (1,9) (1,10) (1,11) (1,12) (1,13) (1,14) (1,15) (1,16) (1,17) (1... | 311 | 307,200 | 3 | 80,613 | |
965 | Short Code | [
"data structures",
"dp",
"greedy",
"strings",
"trees"
] | null | null | Arkady's code contains $n$ variables. Each variable has a unique name consisting of lowercase English letters only. One day Arkady decided to shorten his code.
He wants to replace each variable name with its non-empty prefix so that these new names are still unique (however, a new name of some variable can coincide wi... | The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the number of variables.
The next $n$ lines contain variable names, one per line. Each name is non-empty and contains only lowercase English letters. The total length of these strings is not greater than $10^5$. The variable names are distinct. | Print a single integer — the minimum possible total length of new variable names. | [
"3\ncodeforces\ncodehorses\ncode\n",
"5\nabba\nabb\nab\naa\naacada\n",
"3\ntelegram\ndigital\nresistance\n"
] | [
"6\n",
"11\n",
"3\n"
] | In the first example one of the best options is to shorten the names in the given order as "cod", "co", "c".
In the second example we can shorten the last name to "aac" and the first name to "a" without changing the other names. | [
{
"input": "3\ncodeforces\ncodehorses\ncode",
"output": "6"
},
{
"input": "5\nabba\nabb\nab\naa\naacada",
"output": "11"
},
{
"input": "3\ntelegram\ndigital\nresistance",
"output": "3"
},
{
"input": "1\na",
"output": "1"
},
{
"input": "10\naaaba\nbabba\nbbba\naaab... | 15 | 0 | 0 | 80,728 | |
444 | DZY Loves Planting | [
"binary search",
"dsu",
"trees"
] | null | null | DZY loves planting, and he enjoys solving tree problems.
DZY has a weighted tree (connected undirected graph without cycles) containing *n* nodes (they are numbered from 1 to *n*). He defines the function *g*(*x*,<=*y*) (1<=≤<=*x*,<=*y*<=≤<=*n*) as the longest edge in the shortest path between nodes *x* and *y*. Speci... | The first line contains an integer *n* (1<=≤<=*n*<=≤<=3000).
Each of the next *n*<=-<=1 lines contains three integers *a**i*,<=*b**i*,<=*c**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*; 1<=≤<=*c**i*<=≤<=10000), denoting an edge between *a**i* and *b**i* with length *c**i*. It is guaranteed that these edges form a tree.
Each of ... | Print a single integer representing the answer. | [
"4\n1 2 1\n2 3 2\n3 4 3\n1\n1\n1\n1\n",
"4\n1 2 1\n2 3 2\n3 4 3\n4\n4\n4\n4\n"
] | [
"2\n",
"3\n"
] | In the first sample, one of the optimal *p* is [4, 3, 2, 1]. | [
{
"input": "4\n1 2 1\n2 3 2\n3 4 3\n1\n1\n1\n1",
"output": "2"
},
{
"input": "4\n1 2 1\n2 3 2\n3 4 3\n4\n4\n4\n4",
"output": "3"
},
{
"input": "10\n2 1 8760\n3 1 3705\n4 1 1862\n5 2 7332\n6 3 7015\n7 5 4866\n8 3 4465\n9 7 8886\n10 3 9362\n2\n5\n5\n4\n4\n5\n4\n5\n1\n2",
"output": "876... | 46 | 0 | 0 | 80,829 | |
558 | Guess Your Way Out! II | [
"data structures",
"implementation",
"sortings"
] | null | null | Amr bought a new video game "Guess Your Way Out! II". The goal of the game is to find an exit from the maze that looks like a perfect binary tree of height *h*. The player is initially standing at the root of the tree and the exit from the tree is located at some leaf node.
Let's index all the nodes of the tree such t... | The first line contains two integers *h*,<=*q* (1<=≤<=*h*<=≤<=50, 0<=≤<=*q*<=≤<=105), the height of the tree and the number of questions respectively.
The next *q* lines will contain four integers each *i*,<=*L*,<=*R*,<=*ans* (1<=≤<=*i*<=≤<=*h*, 2*i*<=-<=1<=≤<=*L*<=≤<=*R*<=≤<=2*i*<=-<=1, ), representing a question as ... | If the information provided by the game is contradictory output "Game cheated!" without the quotes.
Else if you can uniquely identify the exit to the maze output its index.
Otherwise output "Data not sufficient!" without the quotes. | [
"3 1\n3 4 6 0\n",
"4 3\n4 10 14 1\n3 6 6 0\n2 3 3 1\n",
"4 2\n3 4 6 1\n4 12 15 1\n",
"4 2\n3 4 5 1\n2 3 3 1\n"
] | [
"7",
"14",
"Data not sufficient!",
"Game cheated!"
] | Node *u* is an ancestor of node *v* if and only if
- *u* is the same node as *v*, - *u* is the parent of node *v*, - or *u* is an ancestor of the parent of node *v*.
In the first sample test there are 4 leaf nodes 4, 5, 6, 7. The first question says that the node isn't in the range [4, 6] so the exit is node numb... | [
{
"input": "3 1\n3 4 6 0",
"output": "7"
},
{
"input": "4 3\n4 10 14 1\n3 6 6 0\n2 3 3 1",
"output": "14"
},
{
"input": "4 2\n3 4 6 1\n4 12 15 1",
"output": "Data not sufficient!"
},
{
"input": "4 2\n3 4 5 1\n2 3 3 1",
"output": "Game cheated!"
},
{
"input": "1 0"... | 233 | 2,150,400 | 0 | 80,982 | |
48 | Ivan the Fool VS Gorynych the Dragon | [
"dp",
"games",
"graphs"
] | E. Ivan the Fool VS Gorynych the Dragon | 2 | 256 | Once upon a time in a kingdom far, far away… Okay, let’s start at the point where Ivan the Fool met Gorynych the Dragon. Ivan took out his magic sword and the battle began. First Gorynych had *h* heads and *t* tails. With each strike of the sword Ivan can either cut off several heads (from 1 to *n*, but not more than G... | The first line contains three integers *h*, *t* and *R* (0<=≤<=*h*,<=*t*,<=*R*<=≤<=200, 0<=<<=*h*<=+<=*t*<=≤<=*R*) which represent the initial numbers of Gorynych’s heads and tails and the largest total number of heads and tails with which Gorynych the Dragon does not yet attack. The next line contains integer *n* (... | Print "Ivan" (without quotes) in the first line if Ivan wins, or "Zmey" (that means a dragon in Russian) if Gorynych the Dragon wins. In the second line print a single integer which represents the number of blows Ivan makes. If the battle will continue forever, print in the first line "Draw". | [
"2 2 4\n2\n1 0\n0 1\n3\n0 1\n0 1\n0 0\n",
"2 2 4\n1\n0 1\n1\n1 0\n",
"2 2 5\n1\n1 1\n1\n3 0\n"
] | [
"Ivan\n2\n",
"Draw\n",
"Zmey\n2\n"
] | none | [] | 92 | 0 | 0 | 81,120 |
229 | Triangles | [
"combinatorics",
"graphs",
"math"
] | null | null | Alice and Bob don't play games anymore. Now they study properties of all sorts of graphs together. Alice invented the following task: she takes a complete undirected graph with *n* vertices, chooses some *m* edges and keeps them. Bob gets the remaining edges.
Alice and Bob are fond of "triangles" in graphs, that is, ... | The first line contains two space-separated integers *n* and *m* (1<=≤<=*n*<=≤<=106,<=0<=≤<=*m*<=≤<=106) — the number of vertices in the initial complete graph and the number of edges in Alice's graph, correspondingly. Then *m* lines follow: the *i*-th line contains two space-separated integers *a**i*, *b**i* (1<=≤<=*a... | Print a single number — the total number of cycles of length 3 in Alice and Bob's graphs together.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use the cin, cout streams or the %I64d specifier. | [
"5 5\n1 2\n1 3\n2 3\n2 4\n3 4\n",
"5 3\n1 2\n2 3\n1 3\n"
] | [
"3\n",
"4\n"
] | In the first sample Alice has 2 triangles: (1, 2, 3) and (2, 3, 4). Bob's graph has only 1 triangle : (1, 4, 5). That's why the two graphs in total contain 3 triangles.
In the second sample Alice's graph has only one triangle: (1, 2, 3). Bob's graph has three triangles: (1, 4, 5), (2, 4, 5) and (3, 4, 5). In this case... | [] | 92 | 0 | 0 | 81,171 | |
45 | School | [
"dp",
"dsu"
] | B. School | 2 | 256 | There are *n* students studying in the 6th grade, in group "B" of a berland secondary school. Every one of them has exactly one friend whom he calls when he has some news. Let us denote the friend of the person number *i* by *g*(*i*). Note that the friendships are not mutual, i.e. *g*(*g*(*i*)) is not necessarily equal... | The first line contains two space-separated integers *n* and *m* (2<=≤<=*n*,<=*m*<=≤<=105) — the number of students and the number of days. The second line contains *n* space-separated integers *g*(*i*) (1<=≤<=*g*(*i*)<=≤<=*n*,<=*g*(*i*)<=≠<=*i*) — the number of a friend of the *i*-th student. The third line contains *... | Print *m* lines containing one number each. The *i*-th line should contain *res**i* — for what number of students the first news they've learned over the *m* days in question, was the news number *i*. The number of the news is the number of the day on which it can be learned. The days are numbered starting from one in ... | [
"3 4\n2 3 1\n1 2 3 4\n1 2 3 4\n",
"8 6\n7 6 4 2 3 5 5 7\n10 4 3 8 9 1\n1 1 1 2 2 2\n"
] | [
"1\n1\n1\n0\n",
"1\n1\n1\n2\n1\n1\n"
] | none | [
{
"input": "3 4\n2 3 1\n1 2 3 4\n1 2 3 4",
"output": "1\n1\n1\n0"
},
{
"input": "8 6\n7 6 4 2 3 5 5 7\n10 4 3 8 9 1\n1 1 1 2 2 2",
"output": "1\n1\n1\n2\n1\n1"
},
{
"input": "2 2\n2 1\n3304501 9446989\n1 1",
"output": "1\n1"
},
{
"input": "7 3\n7 5 5 1 1 1 1\n9034254 4422892 ... | 62 | 0 | 0 | 81,366 |
377 | Cookie Clicker | [
"dp",
"geometry"
] | null | null | Kostya is playing the computer game Cookie Clicker. The goal of this game is to gather cookies. You can get cookies using different buildings: you can just click a special field on the screen and get the cookies for the clicks, you can buy a cookie factory, an alchemy lab, a time machine and it all will bring lots and ... | The first line contains two integers *n* and *s* (1<=≤<=*n*<=≤<=2·105, 1<=≤<=*s*<=≤<=1016) — the number of buildings in the game and the number of cookies Kostya wants to earn.
Each of the next *n* lines contains two integers *v**i* and *c**i* (1<=≤<=*v**i*<=≤<=108, 0<=≤<=*c**i*<=≤<=108) — the number of cookies the *i... | Output the only integer — the minimum number of seconds Kostya needs to earn at least *s* cookies. It is guaranteed that he can do it. | [
"3 9\n1 0\n2 3\n5 4\n",
"3 6\n1 0\n2 2\n5 4\n",
"3 13\n1 0\n2 2\n6 5\n",
"1 10000000000000000\n1 0\n"
] | [
"6\n",
"5\n",
"7\n",
"10000000000000000\n"
] | none | [] | 46 | 0 | 0 | 81,446 | |
263 | Cycle in Graph | [
"dfs and similar",
"graphs"
] | null | null | You've got a undirected graph *G*, consisting of *n* nodes. We will consider the nodes of the graph indexed by integers from 1 to *n*. We know that each node of graph *G* is connected by edges with at least *k* other nodes of this graph. Your task is to find in the given graph a simple cycle of length of at least *k*<=... | The first line contains three integers *n*, *m*, *k* (3<=≤<=*n*,<=*m*<=≤<=105; 2<=≤<=*k*<=≤<=*n*<=-<=1) — the number of the nodes of the graph, the number of the graph's edges and the lower limit on the degree of the graph node. Next *m* lines contain pairs of integers. The *i*-th line contains integers *a**i*, *b**i* ... | In the first line print integer *r* (*r*<=≥<=*k*<=+<=1) — the length of the found cycle. In the next line print *r* distinct integers *v*1,<=*v*2,<=...,<=*v**r* (1<=≤<=*v**i*<=≤<=*n*) — the found simple cycle.
It is guaranteed that the answer exists. If there are multiple correct answers, you are allowed to print any ... | [
"3 3 2\n1 2\n2 3\n3 1\n",
"4 6 3\n4 3\n1 2\n1 3\n1 4\n2 3\n2 4\n"
] | [
"3\n1 2 3 ",
"4\n3 4 1 2 "
] | none | [
{
"input": "3 3 2\n1 2\n2 3\n3 1",
"output": "3\n1 2 3 "
},
{
"input": "4 6 3\n4 3\n1 2\n1 3\n1 4\n2 3\n2 4",
"output": "4\n3 4 1 2 "
},
{
"input": "9 9 2\n5 6\n6 7\n7 8\n8 9\n9 5\n1 2\n2 3\n3 4\n4 1",
"output": "4\n1 2 3 4 "
},
{
"input": "5 10 4\n1 2\n2 3\n1 3\n1 5\n4 1\n2 ... | 1,964 | 18,739,200 | 3 | 81,723 | |
758 | Unfair Poll | [
"binary search",
"constructive algorithms",
"implementation",
"math"
] | null | null | On the Literature lesson Sergei noticed an awful injustice, it seems that some students are asked more often than others.
Seating in the class looks like a rectangle, where *n* rows with *m* pupils in each.
The teacher asks pupils in the following order: at first, she asks all pupils from the first row in the order ... | The first and the only line contains five integers *n*, *m*, *k*, *x* and *y* (1<=≤<=*n*,<=*m*<=≤<=100,<=1<=≤<=*k*<=≤<=1018,<=1<=≤<=*x*<=≤<=*n*,<=1<=≤<=*y*<=≤<=*m*). | Print three integers:
1. the maximum number of questions a particular pupil is asked, 1. the minimum number of questions a particular pupil is asked, 1. how many times the teacher asked Sergei. | [
"1 3 8 1 1\n",
"4 2 9 4 2\n",
"5 5 25 4 3\n",
"100 100 1000000000000000000 100 100\n"
] | [
"3 2 3",
"2 1 1",
"1 1 1",
"101010101010101 50505050505051 50505050505051"
] | The order of asking pupils in the first test:
1. the pupil from the first row who seats at the first table, it means it is Sergei; 1. the pupil from the first row who seats at the second table; 1. the pupil from the first row who seats at the third table; 1. the pupil from the first row who seats at the first tab... | [
{
"input": "1 3 8 1 1",
"output": "3 2 3"
},
{
"input": "4 2 9 4 2",
"output": "2 1 1"
},
{
"input": "5 5 25 4 3",
"output": "1 1 1"
},
{
"input": "100 100 1000000000000000000 100 100",
"output": "101010101010101 50505050505051 50505050505051"
},
{
"input": "3 2 1... | 46 | 0 | 0 | 81,745 | |
976 | Minimal k-covering | [
"flows",
"graphs"
] | null | null | You are given a bipartite graph *G*<==<=(*U*,<=*V*,<=*E*), *U* is the set of vertices of the first part, *V* is the set of vertices of the second part and *E* is the set of edges. There might be multiple edges.
Let's call some subset of its edges *k*-covering iff the graph has each of its vertices incident to at lea... | The first line contains three integers *n*1, *n*2 and *m* (1<=≤<=*n*1,<=*n*2<=≤<=2000, 0<=≤<=*m*<=≤<=2000) — the number of vertices in the first part, the number of vertices in the second part and the number of edges, respectively.
The *i*-th of the next *m* lines contain two integers *u**i* and *v**i* (1<=≤<=*u**i*<=... | For each print the subset of edges (minimal *k*-covering) in separate line.
The first integer *cnt**k* of the *k*-th line is the number of edges in minimal *k*-covering of the graph. Then *cnt**k* integers follow — original indices of the edges which belong to the minimal *k*-covering, these indices should be pairwis... | [
"3 3 7\n1 2\n2 3\n1 3\n3 2\n3 3\n2 1\n2 1\n",
"1 1 5\n1 1\n1 1\n1 1\n1 1\n1 1\n"
] | [
"0 \n3 3 7 4 \n6 1 3 6 7 4 5 \n",
"0 \n1 5 \n2 4 5 \n3 3 4 5 \n4 2 3 4 5 \n5 1 2 3 4 5 \n"
] | none | [
{
"input": "3 3 7\n1 2\n2 3\n1 3\n3 2\n3 3\n2 1\n2 1",
"output": "0 \n3 3 7 4 \n6 1 3 6 7 4 5 "
},
{
"input": "1 1 5\n1 1\n1 1\n1 1\n1 1\n1 1",
"output": "0 \n1 5 \n2 4 5 \n3 3 4 5 \n4 2 3 4 5 \n5 1 2 3 4 5 "
},
{
"input": "1 1 0",
"output": "0 "
},
{
"input": "2000 2000 1\n1... | 858 | 60,313,600 | 0 | 81,802 | |
23 | Tetragon | [
"geometry",
"math"
] | D. Tetragon | 3 | 256 | You're given the centers of three equal sides of a strictly convex tetragon. Your task is to restore the initial tetragon. | The first input line contains one number *T* — amount of tests (1<=≤<=*T*<=≤<=5·104). Each of the following *T* lines contains numbers *x*1, *y*1, *x*2, *y*2, *x*3, *y*3 — coordinates of different points that are the centers of three equal sides (non-negative integer numbers, not exceeding 10). | For each test output two lines. If the required tetragon exists, output in the first line YES, in the second line — four pairs of numbers — coordinates of the polygon's vertices in clockwise or counter-clockwise order. Don't forget, please, that the tetragon should be strictly convex, i.e. no 3 of its points lie on one... | [
"3\n1 1 2 2 3 3\n0 1 1 0 2 2\n9 3 7 9 9 8\n"
] | [
"NO\n\nYES\n3.5 1.5 0.5 2.5 -0.5 -0.5 2.5 0.5\nNO\n\n"
] | none | [] | 92 | 0 | 0 | 81,846 |
239 | Easy Tape Programming | [
"brute force",
"implementation"
] | null | null | There is a programming language in which every program is a non-empty sequence of "<" and ">" signs and digits. Let's explain how the interpreter of this programming language works. A program is interpreted using movement of instruction pointer (IP) which consists of two parts.
- Current character pointer (CP);... | The first line of input contains two integers *n* and *q* (1<=≤<=*n*,<=*q*<=≤<=100) — represents the length of the sequence *s* and the number of queries.
The second line contains *s*, a sequence of "<", ">" and digits (0..9) written from left to right. Note, that the characters of *s* are not separated with sp... | For each query print 10 space separated integers: *x*0,<=*x*1,<=...,<=*x*9 where *x**i* equals the number of times the interpreter prints *i* while running the corresponding program. Print answers to the queries in the order they are given in input. | [
"7 4\n1>3>22<\n1 3\n4 7\n7 7\n1 7\n"
] | [
"0 1 0 1 0 0 0 0 0 0 \n2 2 2 0 0 0 0 0 0 0 \n0 0 0 0 0 0 0 0 0 0 \n2 3 2 1 0 0 0 0 0 0 \n"
] | none | [
{
"input": "7 4\n1>3>22<\n1 3\n4 7\n7 7\n1 7",
"output": "0 1 0 1 0 0 0 0 0 0 \n2 2 2 0 0 0 0 0 0 0 \n0 0 0 0 0 0 0 0 0 0 \n2 3 2 1 0 0 0 0 0 0 "
},
{
"input": "5 2\n>>>>>\n1 5\n1 2",
"output": "0 0 0 0 0 0 0 0 0 0 \n0 0 0 0 0 0 0 0 0 0 "
},
{
"input": "1 3\n9\n1 1\n1 1\n1 1",
"outpu... | 218 | 4,505,600 | 3 | 82,091 | |
0 | none | [
"none"
] | null | null | You are given array *a**i* of length *n*. You may consecutively apply two operations to this array:
- remove some subsegment (continuous subsequence) of length *m*<=<<=*n* and pay for it *m*·*a* coins; - change some elements of the array by at most 1, and pay *b* coins for each change.
Please note that each of o... | The first line of the input contains integers *n*, *a* and *b* (1<=≤<=*n*<=≤<=1<=000<=000,<=0<=≤<=*a*,<=*b*<=≤<=109) — the length of the array, the cost of removing a single element in the first operation and the cost of changing an element, respectively.
The second line contains *n* integers *a**i* (2<=≤<=*a**i*<=≤<=... | Print a single number — the minimum cost of changes needed to obtain an array, such that the greatest common divisor of all its elements is greater than 1. | [
"3 1 4\n4 2 3\n",
"5 3 2\n5 17 13 5 6\n",
"8 3 4\n3 7 5 4 3 12 9 4\n"
] | [
"1\n",
"8\n",
"13\n"
] | In the first sample the optimal way is to remove number 3 and pay 1 coin for it.
In the second sample you need to remove a segment [17, 13] and then decrease number 6. The cost of these changes is equal to 2·3 + 2 = 8 coins. | [] | 46 | 0 | 0 | 82,166 | |
698 | LRU | [
"bitmasks",
"dp",
"math",
"probabilities"
] | null | null | While creating high loaded systems one should pay a special attention to caching. This problem will be about one of the most popular caching algorithms called LRU (Least Recently Used).
Suppose the cache may store no more than *k* objects. At the beginning of the workflow the cache is empty. When some object is querie... | The first line of the input contains two integers *n* and *k* (1<=≤<=*k*<=≤<=*n*<=≤<=20) — the number of videos and the size of the cache respectively. Next line contains *n* real numbers *p**i* (0<=≤<=*p**i*<=≤<=1), each of them is given with no more than two digits after decimal point.
It's guaranteed that the sum o... | Print *n* real numbers, the *i*-th of them should be equal to the probability that the *i*-th video will be present in the cache after 10100 queries. You answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6.
Namely: let's assume that your answer is *a*, and the answer of the ju... | [
"3 1\n0.3 0.2 0.5\n",
"2 1\n0.0 1.0\n",
"3 2\n0.3 0.2 0.5\n",
"3 3\n0.2 0.3 0.5\n"
] | [
"0.3 0.2 0.5 ",
"0.0 1.0 ",
"0.675 0.4857142857142857 0.8392857142857143 ",
"1.0 1.0 1.0 "
] | none | [
{
"input": "3 1\n0.3 0.2 0.5",
"output": "0.3 0.2 0.5 "
},
{
"input": "2 1\n0.0 1.0",
"output": "0.0 1.0 "
},
{
"input": "3 2\n0.3 0.2 0.5",
"output": "0.675 0.4857142857142857 0.8392857142857143 "
},
{
"input": "3 3\n0.2 0.3 0.5",
"output": "1.0 1.0 1.0 "
},
{
"i... | 108 | 22,323,200 | -1 | 83,069 | |
487 | Strip | [
"binary search",
"data structures",
"dp",
"two pointers"
] | null | null | Alexandra has a paper strip with *n* numbers on it. Let's call them *a**i* from left to right.
Now Alexandra wants to split it into some pieces (possibly 1). For each piece of strip, it must satisfy:
- Each piece should contain at least *l* numbers.- The difference between the maximal and the minimal number on the p... | The first line contains three space-separated integers *n*,<=*s*,<=*l* (1<=≤<=*n*<=≤<=105,<=0<=≤<=*s*<=≤<=109,<=1<=≤<=*l*<=≤<=105).
The second line contains *n* integers *a**i* separated by spaces (<=-<=109<=≤<=*a**i*<=≤<=109). | Output the minimal number of strip pieces.
If there are no ways to split the strip, output -1. | [
"7 2 2\n1 3 1 2 4 1 2\n",
"7 2 2\n1 100 1 100 1 100 1\n"
] | [
"3\n",
"-1\n"
] | For the first sample, we can split the strip into 3 pieces: [1, 3, 1], [2, 4], [1, 2].
For the second sample, we can't let 1 and 100 be on the same piece, so no solution exists. | [
{
"input": "7 2 2\n1 3 1 2 4 1 2",
"output": "3"
},
{
"input": "7 2 2\n1 100 1 100 1 100 1",
"output": "-1"
},
{
"input": "1 0 1\n0",
"output": "1"
},
{
"input": "6 565 2\n31 76 162 -182 -251 214",
"output": "1"
},
{
"input": "1 0 1\n0",
"output": "1"
},
{... | 280 | 30,208,000 | 3 | 83,268 | |
98 | Help Greg the Dwarf | [
"geometry",
"ternary search"
] | C. Help Greg the Dwarf | 2 | 256 | 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... | [] | 60 | 0 | 0 | 83,322 |
292 | Copying Data | [
"data structures"
] | null | null | We often have to copy large volumes of information. Such operation can take up many computer resources. Therefore, in this problem you are advised to come up with a way to copy some part of a number array into another one, quickly.
More formally, you've got two arrays of integers *a*1,<=*a*2,<=...,<=*a**n* and *b*1,<=... | The first line contains two space-separated integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=105) — the number of elements in the arrays and the number of queries, correspondingly. The second line contains an array of integers *a*1,<=*a*2,<=...,<=*a**n* (|*a**i*|<=≤<=109). The third line contains an array of integers *b*1,<=*... | For each second type query print the result on a single line. | [
"5 10\n1 2 0 -1 3\n3 1 5 -2 0\n2 5\n1 3 3 3\n2 5\n2 4\n2 1\n1 2 1 4\n2 1\n2 4\n1 4 2 1\n2 2\n"
] | [
"0\n3\n-1\n3\n2\n3\n-1\n"
] | none | [
{
"input": "5 10\n1 2 0 -1 3\n3 1 5 -2 0\n2 5\n1 3 3 3\n2 5\n2 4\n2 1\n1 2 1 4\n2 1\n2 4\n1 4 2 1\n2 2",
"output": "0\n3\n-1\n3\n2\n3\n-1"
},
{
"input": "1 4\n-2\n1\n1 1 1 1\n2 1\n1 1 1 1\n1 1 1 1",
"output": "-2"
},
{
"input": "2 5\n-3 2\n3 -4\n1 1 1 2\n2 1\n2 1\n1 2 2 1\n2 1",
"out... | 2,000 | 11,673,600 | 0 | 83,332 | |
140 | New Year Garland | [
"combinatorics",
"dp"
] | null | null | As Gerald, Alexander, Sergey and Gennady are already busy with the usual New Year chores, Edward hastily decorates the New Year Tree. And any decent New Year Tree must be decorated with a good garland. Edward has lamps of *m* colors and he wants to make a garland from them. That garland should represent a sequence whos... | The first line contains three integers *n*, *m* and *p* (1<=≤<=*n*,<=*m*<=≤<=106, 2<=≤<=*p*<=≤<=109) which are the number of the tree's layers, the number of the lamps' colors and module correspondingly. The next line contains *n* integers *l**i* (1<=≤<=*l**i*<=≤<=5000, ). | Print the only integer — the number of garlands modulo *p*. | [
"3 2 1000\n3 1 2\n",
"2 3 1000\n2 2\n",
"1 1 1000\n5\n"
] | [
"8\n",
"24\n",
"0\n"
] | In the first sample the following variants are possible: 121|1|12, 121|1|21, 121|2|12, 121|2|21, 212|1|12, 212|1|21, 212|2|12, 212|2|21. In the second sample the following variants are possible: 12|13, 12|23, 12|31, 12|32 and so on. | [] | 1,622 | 268,390,400 | 0 | 83,432 | |
67 | Restoration of the Permutation | [
"greedy"
] | B. Restoration of the Permutation | 1 | 256 | Let *A*<==<={*a*1,<=*a*2,<=...,<=*a**n*} be any permutation of the first *n* natural numbers {1,<=2,<=...,<=*n*}. You are given a positive integer *k* and another sequence *B*<==<={*b*1,<=*b*2,<=...,<=*b**n*}, where *b**i* is the number of elements *a**j* in *A* to the left of the element *a**t*<==<=*i* such that *a**j... | The first line contains two space separated integers *n* and *k* (1<=≤<=*n*<=≤<=1000, 1<=≤<=*k*<=≤<=*n*). On the second line are *n* integers specifying the values of *B*<==<={*b*1,<=*b*2,<=...,<=*b**n*}. | Print on a single line *n* integers of *A*<==<={*a*1,<=*a*2,<=...,<=*a**n*} such that *A* is lexicographically minimal. It is guaranteed that the solution exists. | [
"5 2\n1 2 1 0 0\n",
"4 2\n1 0 0 0\n"
] | [
"4 1 5 2 3 ",
"2 3 1 4 "
] | none | [
{
"input": "5 2\n1 2 1 0 0",
"output": "4 1 5 2 3 "
},
{
"input": "4 2\n1 0 0 0",
"output": "2 3 1 4 "
},
{
"input": "10 3\n4 2 4 2 1 0 1 0 0 0",
"output": "6 8 2 5 9 1 4 10 3 7 "
},
{
"input": "15 3\n4 2 7 5 1 1 1 0 0 0 0 0 0 0 0",
"output": "8 5 2 9 6 1 10 7 11 4 12 3 1... | 312 | 2,048,000 | 3.840185 | 83,433 |
440 | Berland Federalization | [
"dp",
"trees"
] | null | null | Recently, Berland faces federalization requests more and more often. The proponents propose to divide the country into separate states. Moreover, they demand that there is a state which includes exactly *k* towns.
Currently, Berland has *n* towns, some pairs of them are connected by bilateral roads. Berland has only *... | The first line contains integers *n*, *k* (1<=≤<=*k*<=≤<=*n*<=≤<=400). Then follow *n*<=-<=1 lines, each of them describes a road in Berland. The roads are given as pairs of integers *x**i*,<=*y**i* (1<=≤<=*x**i*,<=*y**i*<=≤<=*n*; *x**i*<=≠<=*y**i*) — the numbers of towns connected by the road. Assume that the towns ar... | The the first line print the required minimum number of "problem" roads *t*. Then print a sequence of *t* integers — their indices in the found division. The roads are numbered starting from 1 in the order they follow in the input. If there are multiple possible solutions, print any of them.
If the solution shows that... | [
"5 2\n1 2\n2 3\n3 4\n4 5\n",
"5 3\n1 2\n1 3\n1 4\n1 5\n",
"1 1\n"
] | [
"1\n2\n",
"2\n3 4\n",
"0\n\n"
] | none | [
{
"input": "5 2\n1 2\n2 3\n3 4\n4 5",
"output": "1\n2"
},
{
"input": "5 3\n1 2\n1 3\n1 4\n1 5",
"output": "2\n3 4"
},
{
"input": "1 1",
"output": "0"
},
{
"input": "11 4\n1 2\n1 3\n1 4\n2 6\n2 7\n1 5\n2 8\n4 9\n4 10\n4 11",
"output": "1\n1"
},
{
"input": "11 10\n1... | 46 | 0 | 0 | 83,620 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.