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 trainers, there are *e* pairs of trainers who hate each other. Their hate is mutual, there are no identical pairs among these, and no trainer hates himself (the world of Pokermon is a joyful place!). Each trainer has a wish-list of length *l**i* of teams he’d like to join.
Your task is to divide players into teams and the teams into two conferences, so that:
- each trainer belongs to exactly one team; - no team is in both conferences; - total hate between conferences is at least *e*<=/<=2; - every trainer is in a team from his wish-list.
Total hate between conferences is calculated as the number of pairs of trainers from teams from different conferences who hate each other. | 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*<=≤<=*n*) indicating that Pokermon trainers *a* and *b* hate each other. Next 2*n* lines are in a following format. Starting with Pokermon trainer 1, for each trainer in consecutive order: first number *l**i* (16<=≤<=*l**i*<=≤<=20) — a size of Pokermon trainers wish list, then *l**i* positive integers *t**i*,<=*j* (1<=≤<=*t**i*,<=*j*<=≤<=*T*), providing the teams the *i*-th trainer would like to be on.
Each trainers wish list will contain each team no more than once. Teams on the wish lists are numbered in such a way that the set of all teams that appear on at least 1 wish list is set of consecutive positive integers {1,<=2,<=3,<=…,<=*T*}. Here *T* might be up to 1<=000<=000. | 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 keep up with the neighbours, they decided to call the baby so that the name was lexicographically strictly larger than the neighbour's son's name *t*.
On the other hand, they suspect that a name tax will be introduced shortly. According to it, the Taucetians with lexicographically larger names will pay larger taxes. That's the reason abac and bbad want to call the newborn so that the name was lexicographically strictly larger than name *t* and lexicographically minimum at that.
The lexicographical order of strings is the order we are all used to, the "dictionary" order. Such comparison is used in all modern programming languages to compare strings. Formally, a string *p* of length *n* is lexicographically less than string *q* of length *m*, if one of the two statements is correct:
- *n*<=<<=*m*, and *p* is the beginning (prefix) of string *q* (for example, "aba" is less than string "abaa"), - *p*1<==<=*q*1, *p*2<==<=*q*2, ..., *p**k*<=-<=1<==<=*q**k*<=-<=1, *p**k*<=<<=*q**k* for some *k* (1<=≤<=*k*<=≤<=*min*(*n*,<=*m*)), here characters in strings are numbered starting from 1.
Write a program that, given string *s* and the heighbours' child's name *t* determines the string that is the result of permutation of letters in *s*. The string should be lexicographically strictly more than *t* and also, lexicographically minimum. | 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 is denoted by a symbol "*" on the plan) or a free space (such square is denoted on the plan by a symbol ".").
Room in an apartment is a maximal connected area consisting of free squares. Squares are considered adjacent if they share a common side.
The old Arthur dream is to live in an apartment where all rooms are rectangles. He asks you to calculate minimum number of walls you need to remove in order to achieve this goal. After removing a wall from a square it becomes a free square. While removing the walls it is possible that some rooms unite into a single one. | 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 this cell is free from walls and also this cell is contained in some of the rooms. | 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 late to change it — Limak has already announced initial scores for problems. Though it's still possible to adjust the speed of losing points, denoted by *c* in this statement.
Let *T* denote the total number of minutes needed to solve all problems (so, *T*<==<=*t*1<=+<=*t*2<=+<=...<=+<=*t**n*). The contest will last exactly *T* minutes. So it's just enough to solve all problems.
Points given for solving a problem decrease linearly. Solving the *i*-th problem after *x* minutes gives exactly points, where is some real constant that Limak must choose.
Let's assume that *c* is fixed. During a contest a participant chooses some order in which he or she solves problems. There are *n*! possible orders and each of them gives some total number of points, not necessarily integer. We say that an order is optimal if it gives the maximum number of points. In other words, the total number of points given by this order is greater or equal than the number of points given by any other order. It's obvious that there is at least one optimal order. However, there may be more than one optimal order.
Limak assumes that every participant will properly estimate *t**i* at the very beginning and will choose some optimal order. He also assumes that testers correctly predicted time needed to solve each problem.
For two distinct problems *i* and *j* such that *p**i*<=<<=*p**j* Limak wouldn't be happy to see a participant with strictly more points for problem *i* than for problem *j*. He calls such a situation a paradox.
It's not hard to prove that there will be no paradox for *c*<==<=0. The situation may be worse for bigger *c*. What is the maximum real value *c* (remember that ) for which there is no paradox possible, that is, there will be no paradox for any optimal order of solving problems?
It can be proved that the answer (the maximum *c* as described) always exists. | 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 minutes needed to solve the *i*-th problem. | 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 will consider your answer correct if . | [
"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 best total score, equal to the sum of:
1. solved 1 minute after the start: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e300b226dba2622235193889de3334147547cfcb.png" style="max-width: 100.0%;max-height: 100.0%;"/> 1. solved 2 minutes after the start: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/899352b92886f248a9c8e31247a7e4043f9f45e9.png" style="max-width: 100.0%;max-height: 100.0%;"/> 1. solved 10 minutes after the start: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ed13f9d5e6cc1ef056b90a9157397d02abab0b16.png" style="max-width: 100.0%;max-height: 100.0%;"/>
So, this order gives 3.72 + 2.58 + 3 = 9.3 points in total and this is the only optimal order (you can calculate total scores for other 5 possible orders too see that they are lower). You should check points for problems 1 and 3 to see a paradox. There is 4 < 10 but 3.72 > 3. It turns out that there is no paradox for *c* = 0.625 but there is a paradox for any bigger *c*.
In the second sample, all 24 orders are optimal.
In the third sample, even for *c* = 1 there is no paradox. | [] | 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 on the street.
The street is split into *n* equal length parts from left to right, the *i*-th part is characterized by two integers: width of road *s**i* and width of lawn *g**i*.
For each of *n* parts the Mayor should decide the size of lawn to demolish. For the *i*-th part he can reduce lawn width by integer *x**i* (0<=≤<=*x**i*<=≤<=*g**i*). After it new road width of the *i*-th part will be equal to *s*'*i*<==<=*s**i*<=+<=*x**i* and new lawn width will be equal to *g*'*i*<==<=*g**i*<=-<=*x**i*.
On the one hand, the Mayor wants to demolish as much lawn as possible (and replace it with road). On the other hand, he does not want to create a rapid widening or narrowing of the road, which would lead to car accidents. To avoid that, the Mayor decided that width of the road for consecutive parts should differ by at most 1, i.e. for each *i* (1<=≤<=*i*<=<<=*n*) the inequation |*s*'*i*<=+<=1<=-<=*s*'*i*|<=≤<=1 should hold. Initially this condition might not be true.
You need to find the the total width of lawns the Mayor will destroy according to his plan. | 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 line. | [
"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 repeats the following process *n* times, starting from the vertex 1:
Assume you've ended last operation in vertex *x* (consider *x*<==<=1 if it is the first operation). Draw a new segment from vertex *x* to *k*-th next vertex in clockwise direction. This is a vertex *x*<=+<=*k* or *x*<=+<=*k*<=-<=*n* depending on which of these is a valid index of polygon's vertex.
Your task is to calculate number of polygon's sections after each drawing. A section is a clear area inside the polygon bounded with drawn diagonals or the polygon's sides. | 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 number); - swap the elements on positions *i* and *j*; in other words, you are allowed to apply the following sequence of assignments: *tmp*<==<=*a*[*i*],<=*a*[*i*]<==<=*a*[*j*],<=*a*[*j*]<==<=*tmp* (*tmp* is a temporary variable).
You do not need to minimize the number of used operations. However, you need to make sure that there are at most 5*n* operations. | 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 puzzles for seemingly unknown reasons.
One day, Sanz has created a crossword for you. Not any kind of crossword, but a 1D crossword! You are given *m* words and a string of length *n*. You are also given an array *p*, which designates how much each word is worth — the *i*-th word is worth *p**i* points. Whenever you find one of the *m* words in the string, you are given the corresponding number of points. Each position in the crossword can be used at most *x* times. A certain word can be counted at different places, but you cannot count the same appearance of a word multiple times. If a word is a substring of another word, you can count them both (presuming you haven’t used the positions more than *x* times).
In order to solve the puzzle, you need to tell Sanz what’s the maximum achievable number of points in the crossword. There is no need to cover all postions, just get the maximal score! Crossword and words contain only lowercase English letters. | 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 string representing a non-empty word (its length doesn't exceed the length of the crossword) and integer *p**i* (0<=≤<=*p**i*<=≤<=100). Last line of the input will contain *x* (1<=≤<=*x*<=≤<=100) — maximum number of times a position in crossword can be used. | 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 first appearance of "ba". | [
{
"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 studying the tree Petya can add electrons to vertices or induce random decay with synchrophasotron. Random decay is a process during which the edges of some path from the root to the random leaf of the tree are deleted. All the leaves are equiprobable. As the half-decay tree is the school property, Petya will return back the deleted edges into the tree after each decay.
After being desintegrated, the tree decomposes into connected components. Charge of each component is the total quantity of electrons placed in vertices of the component. Potential of desintegerated tree is the maximum from the charges of its connected components. Each time before inducing random decay Petya is curious about the mathematical expectation of potential of the tree after being desintegrated. | 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 numbered in the following way: the root is numbered with 1, the children of the vertex with number *x* are numbered with 2*x* and 2*x*<=+<=1.- decayPetya induces tree decay. | 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 trees harms the nature and the humankind. The amount of oxygen in the atmosphere has dropped to 17 per cent and, as the best minds of the world think, that is not the end.
In the middle of the 50-s of the previous century a group of soviet scientists succeed in foreseeing the situation with beavers and worked out a secret technology to clean territory. The technology bears a mysterious title "Beavermuncher-0xFF". Now the fate of the planet lies on the fragile shoulders of a small group of people who has dedicated their lives to science.
The prototype is ready, you now need to urgently carry out its experiments in practice.
You are given a tree, completely occupied by beavers. A tree is a connected undirected graph without cycles. The tree consists of *n* vertices, the *i*-th vertex contains *k**i* beavers.
"Beavermuncher-0xFF" works by the following principle: being at some vertex *u*, it can go to the vertex *v*, if they are connected by an edge, and eat exactly one beaver located at the vertex *v*. It is impossible to move to the vertex *v* if there are no beavers left in *v*. "Beavermuncher-0xFF" cannot just stand at some vertex and eat beavers in it. "Beavermuncher-0xFF" must move without stops.
Why does the "Beavermuncher-0xFF" works like this? Because the developers have not provided place for the battery in it and eating beavers is necessary for converting their mass into pure energy.
It is guaranteed that the beavers will be shocked by what is happening, which is why they will not be able to move from a vertex of the tree to another one. As for the "Beavermuncher-0xFF", it can move along each edge in both directions while conditions described above are fulfilled.
The root of the tree is located at the vertex *s*. This means that the "Beavermuncher-0xFF" begins its mission at the vertex *s* and it must return there at the end of experiment, because no one is going to take it down from a high place.
Determine the maximum number of beavers "Beavermuncher-0xFF" can eat and return to the starting vertex. | 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 integers represent two vertices connected by an edge. Vertices are numbered from 1 to *n*. The last line contains integer *s* — the number of the starting vertex (1<=≤<=*s*<=≤<=*n*). | 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*<=≤<=105). A subsequence *a**i*1,<=*a**i*2,<=...,<=*a**i**k* where 1<=≤<=*i*1<=<<=*i*2<=<<=...<=<<=*i**k*<=≤<=*n* is called increasing if *a**i*1<=<<=*a**i*2<=<<=*a**i*3<=<<=...<=<<=*a**i**k*. An increasing subsequence is called longest if it has maximum length among all increasing subsequences.
Nam realizes that a sequence may have several longest increasing subsequences. Hence, he divides all indexes *i* (1<=≤<=*i*<=≤<=*n*), into three groups:
1. group of all *i* such that *a**i* belongs to no longest increasing subsequences.1. group of all *i* such that *a**i* belongs to at least one but not every longest increasing subsequence.1. group of all *i* such that *a**i* belongs to every longest increasing subsequence.
Since the number of longest increasing subsequences of *a* may be very large, categorizing process is very difficult. Your task is to help him finish this job. | 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="lower-index">1</sub>, *a*<sub class="lower-index">2</sub>, *a*<sub class="lower-index">4</sub>} = {1, 3, 5} and {*a*<sub class="lower-index">1</sub>, *a*<sub class="lower-index">3</sub>, *a*<sub class="lower-index">4</sub>} = {1, 2, 5}.
In the third 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, 5, 2, 3}. Sequence *a* have exactly 1 longest increasing subsequence of length 3, that is {*a*<sub class="lower-index">1</sub>, *a*<sub class="lower-index">3</sub>, *a*<sub class="lower-index">4</sub>} = {1, 2, 3}. | [
{
"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 to integer value *a**i*. As Vlad wants to be sure the new game will work he wants to buy not one, but several video cards and unite their powers using the cutting-edge technology. To use this technology one of the cards is chosen as the leading one and other video cards are attached to it as secondary. For this new technology to work it's required that the power of each of the secondary video cards is divisible by the power of the leading video card. In order to achieve that the power of any secondary video card can be reduced to any integer value less or equal than the current power. However, the power of the leading video card should remain unchanged, i.e. it can't be reduced.
Vlad has an infinite amount of money so he can buy any set of video cards. Help him determine which video cards he should buy such that after picking the leading video card and may be reducing some powers of others to make them work together he will get the maximum total value of video power. | 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 the leading, the powers of all other video cards should be reduced by 1, thus the total power would be 2 + 2 + 14 + 8 = 26, that is less than 27. Please note, that it's not allowed to reduce the power of the leading video card, i.e. one can't get the total power 3 + 1 + 15 + 9 = 28.
In the second sample, the optimal answer is to buy all video cards and pick the one with the power 2 as the leading. The video card with the power 7 needs it power to be reduced down to 6. The total power would be 8 + 2 + 2 + 6 = 18. | [
{
"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 indices. So, for example, if *s* is aaaa, *a* is aa and index 3 is forbidden, then *f*(*a*)<==<=2 because there are three occurences of *a* in *s* (starting in indices 1, 2 and 3), but one of them (starting in index 2) ends in a forbidden index.
Calculate the maximum possible value of |*a*|·*f*(*a*) you can get. | 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 (otherwise *i* is not forbidden). | 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 wealthy parents that he was able to obtain the graduation certificate.
The situation is complicated by the fact that each high education institution has the determined amount of voluntary donations, paid by the new students for admission — *n**i* berubleys. He cannot pay more than *n**i*, because then the difference between the paid amount and *n**i* can be regarded as a bribe!
Each rector is wearing the distinctive uniform of his university. Therefore, the uniform's pockets cannot contain coins of denomination more than *r**i*. The rector also does not carry coins of denomination less than *l**i* in his pocket — because if everyone pays him with so small coins, they gather a lot of weight and the pocket tears. Therefore, a donation can be paid only by coins of denomination *x* berubleys, where *l**i*<=≤<=*x*<=≤<=*r**i* (Berland uses coins of any positive integer denomination). Alexey can use the coins of different denominations and he can use the coins of the same denomination any number of times. When Alexey was first confronted with such orders, he was puzzled because it turned out that not all universities can accept him! Alexey is very afraid of going into the army (even though he had long wanted to get the green uniform, but his dad says that the army bullies will beat his son and he cannot pay to ensure the boy's safety). So, Alexey wants to know for sure which universities he can enter so that he could quickly choose his alma mater.
Thanks to the parents, Alexey is not limited in money and we can assume that he has an unlimited number of coins of each type.
In other words, you are given *t* requests, each of them contains numbers *n**i*,<=*l**i*,<=*r**i*. For each query you need to answer, whether it is possible to gather the sum of exactly *n**i* berubleys using only coins with an integer denomination from *l**i* to *r**i* berubleys. You can use coins of different denominations. Coins of each denomination can be used any number of times. | 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 sign, not necessarily symmetrical.
The top-left corner of the crossword coincides with the top-left corner of the rectangle. The same thing is correct for the right-bottom corners. The crossword can't degrade, i.e. it always has exactly four blank areas, two of which are surrounded by letters. Look into the output for the samples for clarification.
Help Vasya — compose a crossword of the described type using the given six words. It is allowed to use the words in any order. | 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 solutions should be printed. If the two lines are equal, compare the second lines and so on. The lexicographical comparison of lines is realized by the < operator in the modern programming languages. | [
"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 are numbered from top to bottom with integers from 1 to *n*, while columns are numbered from left to right with integers from 1 to *m*.
Animals decided to complete the picture and play with it, as it might be even more fun! Owl and hedgehog ask each other some queries. Each query is provided by four integers *x*1, *y*1, *x*2, *y*2 which define the rectangle, where (*x*1,<=*y*1) stands for the coordinates of the up left cell of the rectangle, while (*x*2,<=*y*2) stands for the coordinates of the bottom right cell. The answer to the query is the size of the maximum square consisting of picture parts only (only parts denoted by 1) and located fully inside the query rectangle.
Help Sonya and Filya answer *t* queries. | 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<=000<=000) — the number of queries.
Then follow *t* lines with queries' descriptions. Each of them contains four integers *x*1, *y*1, *x*2, *y*2 (1<=≤<=*x*1<=≤<=*x*2<=≤<=*n*, 1<=≤<=*y*1<=≤<=*y*2<=≤<=*m*) — coordinates of the up left and bottom right cells of the query rectangle. | 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
- represents a lucky number without leading zeroes,- is not empty,- is contained in *s* as a substring the maximum number of times.
Among all the strings for which the three conditions given above are fulfilled, Petya only needs the lexicographically minimum one. Find this string for Petya. | 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-index">*i*</sub> and for any *j* (1 ≤ *j* < *i*) *x*<sub class="lower-index">*j*</sub> = *y*<sub class="lower-index">*j*</sub>. Here |*a*| denotes the length of string *a*.
In the first sample three conditions are fulfilled for strings "4", "7" and "47". The lexicographically minimum one is "4".
In the second sample *s* has no substrings which are lucky numbers.
In the third sample the three conditions are only fulfilled for string "7". | [
{
"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 all nodes on the same level as node with value *X* by *K* units. (The values/nodes of any other level are not affected).1. Cyclically shift the nodes on the same level as node with value *X* by *K* units. (The subtrees of these nodes will move along with them).1. Print the value of every node encountered on the simple path from the node with value *X* to the root.
Positive *K* implies right cyclic shift and negative *K* implies left cyclic shift.
It is guaranteed that atleast one type 3 query is present. | 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* (1<=≤<=*X*<=≤<=1018). | 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 some of the entries of the matrix and changed the order of some of its columns. When Lenny got back home he was very upset. Now Lenny wants to recover his matrix.
Help him to find an order for the columns of the matrix so that it's possible to fill in the erased entries of the matrix to achieve a lovely matrix again. Note, that you can fill the erased entries of the matrix with any integers. | 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 entries. | 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*2-th column of the initial matrix and so on. | [
"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, so the glass can contain only integer number of liters of each Coke type (some types can be not presented in the glass). Also, they want to minimize the total volume of Coke in the glass.
Carbon dioxide concentration is defined as the volume of carbone dioxide in the Coke divided by the total volume of Coke. When you mix two Cokes, the volume of carbon dioxide sums up, and the total volume of Coke sums up as well.
Help them, find the minimal natural number of liters needed to create a glass with carbon dioxide concentration . Assume that the friends have unlimited amount of each Coke type. | 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 types can have same concentration. | 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.codeforces.com/9b37ab6b0795f08ffcc699d9101a9efb89374478.png" style="max-width: 100.0%;max-height: 100.0%;"/> and <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/d82574f3d78c4bd9d8ab9bda103e05a51e1b3161.png" style="max-width: 100.0%;max-height: 100.0%;"/>: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/b23f59a536403f9a2364e971aa0bfc9a3411b366.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the second case, we can achieve concentration <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/46aa9afb7ee4d932ca2c3f0d6535a9955fc8f0a8.png" style="max-width: 100.0%;max-height: 100.0%;"/> using two liters of <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/69b8967d23533c2caada3910f564294509450a59.png" style="max-width: 100.0%;max-height: 100.0%;"/> type and one liter of <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/abf5cbf9e8a81a0eff83ff53574dcabb097df44e.png" style="max-width: 100.0%;max-height: 100.0%;"/> type: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4d2331fc733efc58d37745ff9a495a116ebd7e8a.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [
{
"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 his string. Some letters of the string *s* were replaced by character '?'.
Let's call a sequence of positions *i*,<=*i*<=+<=1,<=...,<=*i*<=+<=*m*<=-<=1 as occurrence of string *t* in *s*, if 1<=≤<=*i*<=≤<=*n*<=-<=*m*<=+<=1 and *t*1<==<=*s**i*,<=*t*2<==<=*s**i*<=+<=1,<=...,<=*t**m*<==<=*s**i*<=+<=*m*<=-<=1.
The boy defines the beauty of the string *s* as maximum number of disjoint occurrences of string *t* in *s*. Vasya can replace some letters '?' with 'a' or 'b' (letters on different positions can be replaced with different letter). Vasya wants to make some replacements in such a way that beauty of string *s* is maximum possible. From all such options, he wants to choose one with the minimum number of replacements. Find the number of replacements he should make. | 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 letters 'a' on odd positions and 'b' on even positions. | 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 represent the largest value that can be obtained from variable *x*, using the described operation any number of times and sequence *a*.
Sereja has sequence *b*1,<=*b*2,<=...,<=*b*|*b*|. Help Sereja calculate sum: . Record [*b**i*,<=*b**i*<=+<=1,<=...,<=*b**j*] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |*b*|2. | 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 operations. | 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/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> aba; 1. abacabadabacaba <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> abcabadabacaba <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> aabadabacaba <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> aabacaba. | [
{
"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 simple task: Oleg is to find the *k*-th smallest integer, such that all its prime divisors are in this set. | 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 are in this set does not exceed 1018. | 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, 3, 9]$ and $[2, 3, 1]$ are not.
It follows from the definition that any sequence of length one or two is an arithmetic progression.
Polycarp found some sequence of positive integers $[b_1, b_2, \dots, b_n]$. He agrees to change each element by at most one. In the other words, for each element there are exactly three options: an element can be decreased by $1$, an element can be increased by $1$, an element can be left unchanged.
Determine a minimum possible number of elements in $b$ which can be changed (by exactly one), so that the sequence $b$ becomes an arithmetic progression, or report that it is impossible.
It is possible that the resulting sequence contains element equals $0$. | 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 use operation twice to the same position). | [
"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.
In the second example Polycarp should not change anything, because his sequence is an arithmetic progression.
In the third example it is impossible to make an arithmetic progression.
In the fourth example Polycarp should change only the first element, he should decrease it on one. After that his sequence will looks like $[0, 3, 6, 9, 12]$, 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 ordinary sorting problem, but we can make it more interesting by adding nondeterministic element. There are *n* people, each person will use either his/her first name or last name as a handle. Can the lexicographical order of the handles be exactly equal to the given permutation *p*?
More formally, if we denote the handle of the *i*-th person as *h**i*, then the following condition must hold: . | 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 letters. All of the given 2*n* strings will be distinct.
The next line contains *n* distinct integers: *p*1,<=*p*2,<=...,<=*p**n* (1<=≤<=*p**i*<=≤<=*n*). | 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 to 'upgrade' his character via the game store. This store offers *n* possible ways to improve the hero's skills; Each of these ways belongs to one of three types:
1. assign the *i*-th skill to *b*; 1. add *b* to the *i*-th skill; 1. multiply the *i*-th skill by *b*.
Unfortunately, a) every improvement can only be used once; b) the money on Vasya's card is enough only to purchase not more than *m* of the *n* improvements. Help Vasya to reach the highest ranking in the game. To do this tell Vasya which of improvements he has to purchase and in what order he should use them to make his rating become as high as possible. If there are several ways to achieve it, print any of them. | 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.
Next *n* lines contain 3 space-separated numbers *t**j*,<=*i**j*,<=*b**j* (1<=≤<=*t**j*<=≤<=3,<=1<=≤<=*i**j*<=≤<=*k*,<=1<=≤<=*b**j*<=≤<=106) — the type of the *j*-th improvement (1 for assigning, 2 for adding, 3 for multiplying), the skill to which it can be applied and the value of *b* for this improvement. | 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 starting from 1, in the order in which they appear in the input. | [
"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*) with probability *p*3 and to the point (*x*,<=*y*<=+<=1) with probability *p*4. It's guaranteed that *p*1<=+<=*p*2<=+<=*p*3<=+<=*p*4<==<=1. The moves are independent.
Find out the expected time after which chip will move away from origin at a distance greater than *R* (i.e. will be satisfied). | 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/4698d4bc5d3f09c8d2261ff8d109754f88454614.png" style="max-width: 100.0%;max-height: 100.0%;"/> and <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/43b509b340db034f967064e721a5926972df0e73.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [] | 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 directions (i.e. north-east, north-west, south-east or south-west). If the beam hits a blocked cell or the border of the grid it will reflect. The behavior of the beam reflection in different situations is depicted in the figure below.
After a while the beam enters an infinite cycle. Count the number of empty cells that the beam goes through at least once. We consider that the beam goes through cell if it goes through its center. | 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* (1<=≤<=*x**s*<=≤<=*n*,<=1<=≤<=*y**s*<=≤<=*m*) and the flash direction which is equal to "NE", "NW", "SE" or "SW". These strings denote directions (<=-<=1,<=1), (<=-<=1,<=<=-<=1), (1,<=1), (1,<=<=-<=1).
It's guaranteed that no two blocked cells have the same coordinates. | 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, there are 2*n* days in a year. ZS the Coder wants to interview *k* people from Udayland, each of them has birthday in one of 2*n* days (each day with equal probability). He is interested in the probability of at least two of them have the birthday at the same day.
ZS the Coder knows that the answer can be written as an irreducible fraction . He wants to find the values of *A* and *B* (he does not like to deal with floating point numbers). Can you help him? | 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 taken. | [
"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%;max-height: 100.0%;"/>, so *A* = 1, *B* = 8.
In the second sample case, there are only 2<sup class="upper-index">1</sup> = 2 days in Udayland, but there are 3 people, so it is guaranteed that two of them have the same birthday. Thus, the probability is 1 and *A* = *B* = 1. | [
{
"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. There is no other way to get gas in Kekoland.
You were hired by the Kekoland president Keko to organize the most beautiful race Kekoland has ever seen. Let race be between cities *l* and *r* (*l*<=≤<=*r*). Race will consist of two stages. On the first stage cars will go from city *l* to city *r*. After completing first stage, next day second stage will be held, now racers will go from *r* to *l* with their cars. Of course, as it is a race, racers drive directly from start city to finish city. It means that at the first stage they will go only right, and at the second stage they go only left. Beauty of the race between *l* and *r* is equal to *r*<=-<=*l*<=+<=1 since racers will see *r*<=-<=*l*<=+<=1 beautiful cities of Kekoland. Cars have infinite tank so racers will take all the gas given to them.
At the beginning of each stage racers start the race with empty tank (0 liters of gasoline). They will immediately take their gasoline in start cities (*l* for the first stage and *r* for the second stage) right after the race starts.
It may not be possible to organize a race between *l* and *r* if cars will run out of gas before they reach finish.
You have *k* presents. Each time you give a present to city *i* its value *g**i* increases by 1. You may distribute presents among cities in any way (also give many presents to one city, each time increasing *g**i* by 1). What is the most beautiful race you can organize?
Each car consumes 1 liter of gas per one kilometer. | 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 city *i* and *i*<=+<=1.
Next line contains *n* integers. The *i*-th of them is *g**i* (0<=≤<=*g**i*<=≤<=109) — the amount of gas you receive every time you enter city *i*. | 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 from 1 to *m*). Each person has and ID number. ID number of *i*<=-<=*th* person is *i* and he/she lives in city number *c**i*. Note that there may be more than one person in a city, also there may be no people living in the city.
Malek loves to order. That's why he asks Duff to answer to *q* queries. In each query, he gives her numbers *v*,<=*u* and *a*.
To answer a query:
Assume there are *x* people living in the cities lying on the path from city *v* to city *u*. Assume these people's IDs are *p*1,<=*p*2,<=...,<=*p**x* in increasing order.
If *k*<==<=*min*(*x*,<=*a*), then Duff should tell Malek numbers *k*,<=*p*1,<=*p*2,<=...,<=*p**k* in this order. In the other words, Malek wants to know *a* minimums on that path (or less, if there are less than *a* people).
Duff is very busy at the moment, so she asked you to help her and answer the queries. | 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 by spaces (1<=≤<=*c**i*<=≤<=*n* for each 1<=≤<=*i*<=≤<=*m*).
Next *q* lines contain the queries. Each of them contains three integers, *v*,<=*u* and *a* (1<=≤<=*v*,<=*u*<=≤<=*n* and 1<=≤<=*a*<=≤<=10). | 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 the *j*-th row and the *i*-th column of the table. | 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 should belong to exactly one subset after the split.
There are *n* queries for you. Each query is described with three integers *l*, *r* and *k*, which mean that you are asked how many integers *x* between *l* and *r* (inclusive) are *k*-beautiful. | 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 again is very difficult task, so they decided to make such an operation every hour: remove two tiles, which form a 2<=×<=2 square, rotate them 90 degrees and put them back on the same place.
They have no idea how to obtain the desired configuration using these operations, and whether it is possible at all.
Help Peter to make a plan for the workers or tell that it is impossible. The plan should contain at most 100<=000 commands. | 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', 'R', 'U' and 'D' correspond to the left, right, upper and lower halves, respectively.
The following *n* lines contain *m* characters each, describing the desired configuration in the same format. | 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* on the plane such that the multiset is [centrally symmetric](https://en.wikipedia.org/wiki/Point_reflection) in respect of 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 root. We will denote it as *LCA*(*u*,<=*v*).
Sandy had a rooted tree consisting of *n* vertices that she used to store her nuts. Unfortunately, the underwater storm broke her tree and she doesn't remember all it's edges. She only managed to restore *m* edges of the initial tree and *q* triples *a**i*, *b**i* and *c**i*, for which she supposes *LCA*(*a**i*,<=*b**i*)<==<=*c**i*.
Help Sandy count the number of trees of size *n* with vertex 1 as a root, that match all the information she remembered. If she made a mess and there are no such trees then print 0. Two rooted trees are considered to be distinct if there exists an edge that occur in one of them and doesn't occur in the other one. | 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*<=≤<=*n*,<=*u**i*<=≠<=*v**i*) — the numbers of vertices connected by the *i*-th edge. It's guaranteed that this set of edges is a subset of edges of some tree.
The last *q* lines contain the triplets of numbers *a**i*, *b**i*, *c**i* (1<=≤<=*a**i*,<=*b**i*,<=*c**i*<=≤<=*n*). Each of these triples define *LCA*(*a**i*,<=*b**i*)<==<=*c**i*. It's not guaranteed that there exists a tree that satisfy all the given *LCA* conditions. | 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 get bored, and if one compartment contain three or four students, then the compartment has fun throughout the entire trip.
The students want to swap with other people, so that no compartment with students had bored students. To swap places with another person, you need to convince him that it is really necessary. The students can not independently find the necessary arguments, so they asked a sympathetic conductor for help. The conductor can use her life experience to persuade any passenger to switch places with some student.
However, the conductor does not want to waste time persuading the wrong people, so she wants to know what is the minimum number of people necessary to persuade her to change places with the students. Your task is to find the number.
After all the swaps each compartment should either have no student left, or have a company of three or four students. | 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 list (one command can be changed several times). How far from the starting point can the turtle move after it follows all the commands of the modified list? | 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 number of lucky digits of number *x*. Find the minimum *b* (*a*<=<<=*b*) such, that *F*(*a*) = *F*(*b*), *F*(*a*<=+<=1) = *F*(*b*<=+<=1), ..., *F*(*a*<=+<=*l*<=-<=1) = *F*(*b*<=+<=*l*<=-<=1). | 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 add 1 to it. In other words, you are allowed to increase some array element by 1 no more than *k* times (you are allowed to increase the same element of the array multiple times).
Your task is to find the maximum number of occurrences of some number in the array after performing no more than *k* allowed operations. If there are several such numbers, your task is to find the minimum one. | 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. The numbers in the lines are separated by single spaces. | 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, we get array 5, 5, 5, if we increase each by one, we get 6, 6, 6. In both cases the maximum number of occurrences equals 3. So we should do nothing, as number 5 is less than number 6.
In the third sample we should increase the second array element once and the fifth element once. Thus, we get sequence 3, 2, 2, 2, 2, where number 2 occurs 4 times. | [
{
"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 players in the team and $i < j < k$, then the rank of the team is equal to $A \cdot i + B \cdot j + C \cdot k$.
You are given information about the pairs of players who have a conflict. Calculate the total sum of ranks over all possible valid teams modulo $2^{64}$. | 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 calculation.
Each of the next $m$ lines contains two space-separated integers $u_i$ and $v_i$ ($0 \le u_i, v_i < n, u_i \neq v_i$) — pair of conflicting players.
It's guaranteed that each unordered pair of players appears in the input file no more than once. | 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 of each team, instead he thought that absolute difference between number of wins of first and second team will be *d*1 and that of between second and third team will be *d*2.
You don't want any of team win the tournament, that is each team should have the same number of wins after *n* games. That's why you want to know: does there exist a valid tournament satisfying the friend's guess such that no team will win this tournament?
Note that outcome of a match can not be a draw, it has to be either win or loss. | 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="lower-index">1</sub> = 0 and *d*<sub class="lower-index">2</sub> = 0, and there is a way to play three games with no winner of tournament (described in the previous sample), the answer is "yes".
Sample 3. You missed 4 matches, and *d*<sub class="lower-index">1</sub> = 1, *d*<sub class="lower-index">2</sub> = 0. These four matches can be: 1-2 (win 2), 1-3 (win 3), 1-2 (win 1), 1-3 (win 1). Currently the first team has 2 wins, the second team has 1 win, the third team has 1 win. Two remaining matches can be: 1-2 (win 2), 1-3 (win 3). In the end all the teams have equal number of wins (2 wins). | [
{
"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 wants to make this walk, he needs each point of the segment to be lit. That's why the bear called his friend (and yes, in the middle of the night) asking for a very delicate favor.
The *Oxy* axis contains *n* floodlights. Floodlight *i* is at point (*x**i*,<=*y**i*) and can light any angle of the plane as large as *a**i* degree with vertex at point (*x**i*,<=*y**i*). The bear asked his friend to turn the floodlights so that he (the bear) could go as far away from his house as possible during the walking along the segment. His kind friend agreed to fulfill his request. And while he is at it, the bear wonders: what is the furthest he can go away from his house? Hep him and find this distance.
Consider that the plane has no obstacles and no other light sources besides the floodlights. The bear's friend cannot turn the floodlights during the bear's walk. Assume that after all the floodlights are turned in the correct direction, the bear goes for a walk and his friend goes to bed. | 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' description.
Note that two floodlights can be at the same point of the plane. | 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 additional edges that she can add to a tree. For each edge she knows which vertices it would connect and the beauty of this edge. Masha can add some of these edges to the graph if the resulting graph is a cactus. Beauty of the resulting cactus is sum of beauties of all added edges.
Help Masha find out what maximum beauty of the resulting cactus she can achieve. | 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*, here *p**i* — is the parent of a vertex *i* — the first vertex on a path from the vertex *i* to the root of the tree (1<=≤<=*p**i*<=<<=*i*).
The following *m* lines contain three integers *u**i*, *v**i* and *c**i* — pairs of vertices to be connected by the additional edges that Masha can add to the tree and beauty of edge (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*; *u**i*<=≠<=*v**i*; 1<=≤<=*c**i*<=≤<=104).
It is guaranteed that no additional edge coincides with the edge of the tree. | 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*<=≥<=*N*).
Following is an example of an almost cyclic sequence with a cycle of length 4:
Notice that the only value of *s* for which the equality does not hold is *s*6 (*s*6<==<=7 and *s*2<==<=8). You are given *s*0,<=*s*1,<=...*s**N*<=-<=1 and all the values of sequence *s* for which (*i*<=≥<=*N*).
Find . | 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 following *M* lines contains two numbers *j* and *v*, indicating that and *s**j*<==<=*v*. All j-s are distinct.
- 1<=≤<=*N*,<=*M*<=≤<=50000 - 0<=≤<=*K*<=≤<=1018 - 1<=≤<=*P*<=≤<=109 - 1<=≤<=*s**i*<=≤<=109, for all *i*<==<=0,<=1,<=...*N*<=-<=1 - *N*<=≤<=*j*<=≤<=1018 - 1<=≤<=*v*<=≤<=109 - All values are integers | 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 each hole. The holes are in a straight line.
Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?".
Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. | 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*<=≤<=*n*) shows that the *y**i*-th ray exits from the *i*-th hole. All rays are numbered from 1 to *n*. | 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 by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint. | [
{
"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 neighbours in the tree other than adjacent vertices of corresponding paths. After that, one of the paths may be merged into the other, that is, the vertices *b*1, ..., *b**k* can be effectively erased:
Help Vanya determine if it possible to make the tree into a path via a sequence of described operations, and if the answer is positive, also determine the shortest length of such path. | 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 given graph is a tree. | 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 corresponding path. | [
{
"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 them back only if Chris can pick such a non-empty subset *Y* from the remaining blocks, that the equality holds:
For example, consider a case where *s*<==<=8 and Chris's teacher took the blocks with numbers 1, 4 and 5. One way for Chris to choose a set is to pick the blocks with numbers 3 and 6, see figure. Then the required sums would be equal: (1<=-<=1)<=+<=(4<=-<=1)<=+<=(5<=-<=1)<==<=(8<=-<=3)<=+<=(8<=-<=6)<==<=7.
However, now Chris has exactly *s*<==<=106 blocks. Given the set *X* of blocks his teacher chooses, help Chris to find the required set *Y*! | 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 large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++. | 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**i*<=≠<=*y**j* for all *i*, *j* (1<=≤<=*i*<=≤<=*n*; 1<=≤<=*j*<=≤<=*m*). It is guaranteed that at least one solution always exists. If there are multiple solutions, output any of them. | [
"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*1<=/<=4<=≤<=*y*<=≤<=*x*1<=/<=2) and decrease the number of coins in this pile to *y*. In other words, after the described move the pile will have *y* coins left.
The player who can't make a move, loses.
Your task is to find out, who wins in the given game if both Furlo and Rublo play optimally well. | 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 С++. It is preferred to use the cin, cout streams or the %I64d specifier. | 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 all the employees are not there and the coat hanger in the cloakroom is empty. At some moments of time the employees arrive and some of them leave.
When some employee arrives, he hangs his cloak on one of the available hooks. To be of as little discomfort to his colleagues as possible, the hook where the coat will hang, is chosen like this. First the employee chooses the longest segment among available hooks following in a row. If there are several of such segments, then he chooses the one closest to the right. After that the coat is hung on the hook located in the middle of this segment. If the segment has an even number of hooks, then among two central hooks we choose the one closest to the right.
When an employee leaves, he takes his coat. As all the company workers deeply respect each other, no one takes somebody else's coat.
From time to time the director of this respectable company gets bored and he sends his secretary to see how many coats hang on the coat hanger from the *i*-th to the *j*-th hook inclusive. And this whim is always to be fulfilled, otherwise the director gets angry and has a mental breakdown.
Not to spend too much time traversing from the director's office to the cloakroom and back again, the secretary asked you to write a program, emulating the company cloakroom's work. | 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 request. The input data has at least one director's request. In all other cases the request contains a positive integer not exceeding 109 — an employee identificator. Each odd appearance of the identificator of an employee in the request list is his arrival. Each even one is his leaving. All employees have distinct identificators. When any employee arrives, there is always at least one free hook. | 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 has an attractiveness level which is specified by the integer number *a**i* for the *i*-th friend. You know that the attractiveness of a picture containing the *i*-th and the *j*-th friends is equal to the exclusive-or (*xor* operation) of integers *a**i* and *a**j*.
You want to take pictures in a way that the total sum of attractiveness of your pictures is maximized. You have to calculate this value. Since the result may not fit in a 32-bit integer number, print it modulo 1000000007 (109<=+<=7). | 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 don't know which contestants that will be. The plan is to get the T-Shirt sizes of all contestants before the actual competition, and then order enough T-shirts so that no matter who is in the top *C* we'll have T-shirts available in order to award them.
In order to get the T-shirt sizes of the contestants, we will send out a survey. The survey will allow contestants to either specify a single desired T-shirt size, or two adjacent T-shirt sizes. If a contestant specifies two sizes, it means that they can be awarded either size.
As you can probably tell, this plan could require ordering a lot of unnecessary T-shirts. We'd like your help to determine the minimum number of T-shirts we'll need to order to ensure that we'll be able to award T-shirts no matter the outcome of the competition. | 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 contestants desiring T-shirt size ((*i*<=+<=1)<=/<=2). For even *i*, *s**i* indicates the number of contestants okay receiving either of T-shirt sizes (*i*<=/<=2) and (*i*<=/<=2<=+<=1). *C* will not exceed the total number of contestants. | 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 when it visits a vertex of the graph which it has already visited. - Law 2. The Robot will destroy itself when it has no way to go (that is when it reaches a vertex whose out-degree is zero). - Law 3. The Robot will move randomly when it has multiple ways to move (that is when it reach a vertex whose out-degree is more than one). Of course, the robot can move only along the directed edges of the graph.
Can you imagine a robot behaving like that? That's why they are sold at a very low price, just for those who are short of money, including mzry1992, of course. mzry1992 has such a robot, and she wants to move it from vertex *s* to vertex *t* in a directed graph safely without self-destruction. Luckily, she can send her robot special orders at each vertex. A special order shows the robot which way to move, if it has multiple ways to move (to prevent random moving of the robot according to Law 3). When the robot reaches vertex *t*, mzry1992 takes it off the graph immediately. So you can see that, as long as there exists a path from *s* to *t*, she can always find a way to reach the goal (whatever the vertex *t* has the outdegree of zero or not).
However, sending orders is expensive, so your task is to find the minimum number of orders mzry1992 needs to send in the worst case. Please note that mzry1992 can give orders to the robot while it is walking on the graph. Look at the first sample to clarify that part of the problem. | 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 from vertex *u**i* to vertex *v**i*. The last line contains two integers *s* and *t* (1<=≤<=*s*,<=*t*<=≤<=*n*).
It is guaranteed that there are no multiple edges and self-loops. | 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 choose the "bad" outgoing edge (return to vertex 1) according Law 3. So, the answer is one. | [] | 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 positive integers.
Edo figured out how he wants to place the magnets on the refrigerator. He introduced a system of coordinates on the plane, where each magnet is represented as a rectangle with sides parallel to the coordinate axes.
Now he wants to remove no more than *k* magnets (he may choose to keep all of them) and attach all remaining magnets to the refrigerator door, and the area of the door should be as small as possible. A magnet is considered to be attached to the refrigerator door if its center lies on the door or on its boundary. The relative positions of all the remaining magnets must correspond to the plan.
Let us explain the last two sentences. Let's suppose we want to hang two magnets on the refrigerator. If the magnet in the plan has coordinates of the lower left corner (*x*1, *y*1) and the upper right corner (*x*2, *y*2), then its center is located at (, ) (may not be integers). By saying the relative position should correspond to the plan we mean that the only available operation is translation, i.e. the vector connecting the centers of two magnets in the original plan, must be equal to the vector connecting the centers of these two magnets on the refrigerator.
The sides of the refrigerator door must also be parallel to coordinate axes. | 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,<=*y*1,<=*x*2,<=*y*2 (1<=≤<=*x*1<=<<=*x*2<=≤<=109, 1<=≤<=*y*1<=<<=*y*2<=≤<=109) — the coordinates of the lower left and upper right corners of the current magnet. The magnets can partially overlap or even fully coincide. | 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 the second test sample it doesn't matter which magnet to remove, the answer will not change — we need a fridge with door width 8 and door height 8.
In the third sample you cannot remove anything as *k* = 0. | [
{
"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 complete all levels of a very weird game to get back. The gaming reality is a three-dimensional space where *n* points are given. The game has *m* levels and at the beginning of the *i*-th level the player is positioned at some plane *Q**i* that passes through the origin. On each level Vasya has to use special robots to construct and activate *n* powerful energy spheres of the equal radius with centers at the given points. The player chooses the radius of the spheres himself. The player has to spend *R* units of money to construct spheres whose radius equals *R* (consequently, one can construct spheres whose radius equals zero for free). Besides, once for each level a player can choose any point in space and release a laser ray from there, perpendicular to plane *Q**i* (this action costs nothing). The ray can either be directed towards the plane or from the plane. The spheres that share at least one point with the ray will be immediately activated. The level is considered completed if the player has managed to activate all spheres. Note that the centers of the spheres are the same for all *m* levels but the spheres do not remain: the player should construct them anew on each new level.
Help Vasya find out what minimum sum of money will be enough to complete each level. | 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 the center of the *i*-th sphere. Assume that these points do not change their positions throughout the game.
Then follow *m* lines, each containing three integers *a**i*, *b**i*, *c**i* (0<=≤<=*a**i*,<=*b**i*,<=*c**i*<=≤<=100, *a**i*2<=+<=*b**i*2<=+<=*c**i*2<=><=0). These numbers are the coefficients in the equation of plane *Q**i* (*a**i**x*<=+<=*b**i**y*<=+<=*c**i**z*<==<=0), where the player is positioned at the beginning of the *i*-th level. | 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 consists of segments. Each segment takes up exactly one passable square of the field, but any passable square contains at most one segment. All segments are indexed by integers from 1 to *k*, where *k* is the snake's length. The 1-th segment is the head and the *k*-th segment is the tail. For any *i* (1<=≤<=*i*<=<<=*k*), segments with indexes *i* and *i*<=+<=1 are located in the adjacent squares of the field, that is, these squares share a common side.
One of the passable field squares contains an apple. The snake's aim is to reach the apple and eat it (that is, to position its head in the square with the apple).
The snake moves throughout the game. During one move the snake can move its head to an adjacent field square. All other segments follow the head. That is, each segment number *i* (1<=<<=*i*<=≤<=*k*) moves to the square that has just had segment number *i*<=-<=1. Consider that all segments including the head move simultaneously (see the second test sample). If the snake's head moves to an unpassable square or to the square, occupied by its other segment, the snake dies. That's why we will consider such moves unvalid.
Your task is to determine the minimum number of valid moves that the snake needs to reach the apple. | 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 first segment is represented by character "1", the second one segment — by character "2" and so on.
The game field description doesn't contain any characters besides "#', ".", "@" and digits (except 0). It is guaranteed that the described field is correct. It is guaranteed that the described field contains exactly one apple and exactly one snake, the snake's length is at least 3 and at most 9. | 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 a weighted tree containing *n* nodes and a sequence *a*1,<=*a*2...,<=*a**n* which is a permutation of 1,<=2,<=...,<=*n*. Now, Mewoth makes *q* queries of one of the following forms:
- 1 l r v: meaning Bash should report , where *dist*(*a*,<=*b*) is the length of the shortest path from node *a* to node *b* in the given tree. - 2 x: meaning Bash should swap *a**x* and *a**x*<=+<=1 in the given sequence. This new sequence is used for later queries.
Help Bash to answer the questions! | 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 next *n*<=-<=1 lines contain three space-separated integers *u*, *v*, and *w* denoting that there exists an undirected edge between node *u* and node *v* of weight *w*, (1<=≤<=*u*,<=*v*<=≤<=*n*, *u*<=≠<=*v*, 1<=≤<=*w*<=≤<=106). It is guaranteed that the given graph is a tree.
Each query consists of two lines. First line contains single integer *t*, indicating the type of the query. Next line contains the description of the query:
- t = 1: Second line contains three integers *a*, *b* and *c* (1<=≤<=*a*,<=*b*,<=*c*<=<<=230) using which *l*, *r* and *v* can be generated using the formula given below: , - , - . - .
The *ans**i* is the answer for the *i*-th query, assume that *ans*0<==<=0. If the *i*-th query is of type 2 then *ans**i* = *ans**i*<=-<=1. It is guaranteed that:
- for each query of type 1: 1<=≤<=*l*<=≤<=*r*<=≤<=*n*, 1<=≤<=*v*<=≤<=*n*, - for each query of type 2: 1<=≤<=*x*<=≤<=*n*<=-<=1.
The operation means bitwise exclusive OR. | 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 people will start running with their maximum speed in the direction they are facing. Also, two strange rays will start propagating from the bomb with speed *s*: one to the right, and one to the left. Of course, the speed *s* is strictly greater than people's maximum speed.
The rays are strange because if at any moment the position and the direction of movement of some ray and some person coincide, then the speed of the person immediately increases by the speed of the ray.
You need to place the bomb is such a point that the minimum time moment in which there is a person that has run through point 0, and there is a person that has run through point 106, is as small as possible. In other words, find the minimum time moment *t* such that there is a point you can place the bomb to so that at time moment *t* some person has run through 0, and some person has run through point 106. | 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<=≤<=*t**i*<=≤<=2) — the coordinate of the *i*-th person on the line, his maximum speed and the direction he will run to (1 is to the left, i.e. in the direction of coordinate decrease, 2 is to the right, i.e. in the direction of coordinate increase), respectively.
It is guaranteed that the points 0 and 106 will be reached independently of the bomb's position. | 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 500000.
In the second example, it is optimal to place the bomb at the point 500000. The rays will catch up with both people at the time 200. At this time moment, the first is at the point with a coordinate of 300000, and the second is at the point with a coordinate of 700000. Their speed will become 1500 and at the time 400 they will simultaneously run through points 0 and 10<sup class="upper-index">6</sup>. | [
{
"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 new word *s*2. It is considered that when Fangy erases some letter, there's no whitespace formed instead of the letter. That is, the string remains unbroken and it still only consists of lowercase Latin letters.
For example, the heading is "abc". If we take two such headings and glue them one to the other one, we get "abcabc". If we erase the letters on positions 1 and 5, we get a word "bcac".
Which least number of newspaper headings *s*1 will Fangy need to glue them, erase several letters and get word *s*2? | 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 points in the set by *k*. You would like to eventually end up with all numbers equal to 0. Is it possible?
A set of 2 points is considered evenly spaced if they are diametrically opposed, and a set of 3 or more points is considered evenly spaced if they form a regular polygon. | 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 integer *q* find the maximal value *x*·*q*<=+<=*y* over all pairs (*x*,<=*y*) from the set.
Help Lena to process the queries. | 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*<=≤<=*n*) follows in the query of the second type. It is guaranteed that *i* is less than the number of the query, the query number *i* has the first type and the pair from the *i*-th query is not already removed.
An integer *q* (<=-<=109<=≤<=*q*<=≤<=109) follows in the query of the third type. | 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 and *b* in the bottom as (*a*,<=*b*).
Each of the three horses can paint the special cards. If you show an (*a*,<=*b*) card to the gray horse, then the horse can paint a new (*a*<=+<=1,<=*b*<=+<=1) card. If you show an (*a*,<=*b*) card, such that *a* and *b* are even integers, to the white horse, then the horse can paint a new card. If you show two cards (*a*,<=*b*) and (*b*,<=*c*) to the gray-and-white horse, then he can paint a new (*a*,<=*c*) card.
Polycarpus really wants to get *n* special cards (1,<=*a*1), (1,<=*a*2), ..., (1,<=*a**n*). For that he is going to the horse land. He can take exactly one (*x*,<=*y*) card to the horse land, such that 1<=≤<=*x*<=<<=*y*<=≤<=*m*. How many ways are there to choose the card so that he can perform some actions in the horse land and get the required cards?
Polycarpus can get cards from the horses only as a result of the actions that are described above. Polycarpus is allowed to get additional cards besides the cards that he requires. | 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 travel estimation. You should help PMP finish his last contract.
Each BRT line is straight line that passes *n* intersecting on its ways. At each intersection there is traffic light that periodically cycles between green and red. It starts illuminating green at time zero. During the green phase which lasts for *g* seconds, traffic is allowed to proceed. After the green phase the light changes to red and remains in this color for *r* seconds. During the red phase traffic is prohibited from proceeding. If a vehicle reaches the intersection exactly at a time when the light changes to red, it should stop, but the vehicle is clear to proceed if the light has just changed to green.
All traffic lights have the same timing and are synchronized. In other words the period of red (and green) phase is the same for all of traffic lights and they all start illuminating green at time zero.
The BRT Company has calculated the time that a bus requires to pass each road segment. A road segment is the distance between two consecutive traffic lights or between a traffic light and source (or destination) station. More precisely BRT specialists provide *n*<=+<=1 positive integers *l**i*, the time in seconds that a bus needs to traverse *i*-th road segment in the path from source to destination. The *l*1 value denotes the time that a bus needs to pass the distance between source and the first intersection. The *l**n*<=+<=1 value denotes the time between the last intersection and destination.
In one day *q* buses leave the source station. The *i*-th bus starts from source at time *t**i* (in seconds). Decision makers of BRT Company want to know what time a bus gets to destination?
The bus is considered as point. A bus will always move if it can. The buses do not interfere with each other. | 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 road segment in the path from source to destination.
Next line contains a single integer *q* (1<=≤<=*q*<=≤<=105) — the number of buses in a day. The *i*-th of next *q* lines contains a single integer *t**i* (1<=≤<=*t**i*<=≤<=109) — the time when *i*-th bus leaves the source station. | 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 of pairs of integers *l* and *r* (1<=≤<=*l*<=≤<=*r*<=≤<=|*a**i*|) such that substring *a**i*[*l*... *r*] is a substring to at least *k* strings from array *a* (including the *i*-th string).
Help the Little Elephant solve this problem.
If you are not familiar with the basic notation in string problems, you can find the corresponding definitions in the notes. | 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* ≤ *r* ≤ |*a*|) of string *a* is string *a*<sub class="lower-index">*l*</sub>*a*<sub class="lower-index">*l* + 1</sub>... *a*<sub class="lower-index">*r*</sub>.
String *a* is a substring of string *b*, if there exists such pair of integers *l* and *r* (1 ≤ *l* ≤ *r* ≤ |*b*|), that *b*[*l*... *r*] = *a*. | [] | 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 holds:
- all points can be divided into pairs so that each point will belong to exactly one pair; - in each pair the point with the lesser abscissa will be marked with a small English letter and the point with the larger abscissa will be marked with the same large English letter; - if we built a square on each pair, the pair's points will be the square's opposite points and the segment between them will be the square's diagonal, then among the resulting squares there won't be any intersecting or touching ones.
Little Petya erased some small and all large letters marking the points. Now Sereja wonders how many ways are there to return the removed letters so that the points were marked beautifully. | 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 (Petya erased them). It is guaranteed that the input string doesn't contain letter "x". | 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:
1. given numbers *l*,<=*r* and *k*, Malek should perform for each *l*<=≤<=*i*<=≤<=*r* (, bitwise exclusive OR of numbers *a* and *b*). 1. given numbers *l* and *r* Malek should tell her the score of sequence *a**l*,<=*a**l*<=+<=1,<=... ,<=*a**r*.
Score of a sequence *b*1,<=...,<=*b**k* is the number of its different Kheshtaks. A non-negative integer *w* is a Kheshtak of this sequence if and only if there exists a subsequence of *b*, let's denote it as *b**i*1,<=*b**i*2,<=... ,<=*b**i**x* (possibly empty) such that (1<=≤<=*i*1<=<<=*i*2<=<<=...<=<<=*i**x*<=≤<=*k*). If this subsequence is empty, then *w*<==<=0.
Unlike Duff, Malek is not a programmer. That's why he asked for your help. Please help him perform these 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 with an integer *t* (1<=≤<=*t*<=≤<=2), type of the corresponding query. If *t*<==<=1, then there are three more integers in that line, *l*,<=*r* and *k*. Otherwise there are two more integers, *l* and *r*. (1<=≤<=*l*<=≤<=*r*<=≤<=*n* and 0<=≤<=*k*<=≤<=109) | 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 trouble again. This year a lot of sky-scrapers have been built which makes it difficult for the airplanes to fly above the city. It was decided that the planes should fly strictly from south to north. Moreover, there must be no sky scraper on a plane’s route, otherwise the anniversary will become a tragedy. The Ministry of Building gave the data on *n* sky scrapers (the rest of the buildings are rather small and will not be a problem to the planes). When looking at the city from south to north as a geometrical plane, the *i*-th building is a rectangle of height *h**i*. Its westernmost point has the x-coordinate of *l**i* and the easternmost — of *r**i*. The terrain of the area is plain so that all the buildings stand on one level. Your task as the Ministry of Defence’s head programmer is to find an enveloping polyline using the data on the sky-scrapers. The polyline’s properties are as follows:
- If you look at the city from south to north as a plane, then any part of any building will be inside or on the boarder of the area that the polyline encloses together with the land surface. - The polyline starts and ends on the land level, i.e. at the height equal to 0. - The segments of the polyline are parallel to the coordinate axes, i.e. they can only be vertical or horizontal. - The polyline’s vertices should have integer coordinates. - If you look at the city from south to north the polyline (together with the land surface) must enclose the minimum possible area. - The polyline must have the smallest length among all the polylines, enclosing the minimum possible area with the land. - The consecutive segments of the polyline must be perpendicular. | 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 last vertices of the polyline should have the height of 0. | [
"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* *mod* *y* means that we take the remainder of the division of number *x* by number *y*. 1. Then we write the contents of the array *s* to the array *a*. Element number *i* (1<=≤<=*i*<=≤<=*n*) of the array *s* becomes the *i*-th element of the array *a* (*a**i*<==<=*s**i*).
You task is to find array *a* after exactly *k* described operations are applied. | 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*<=≤<=*r*) are suitable for pair programming. They decided that the day *i* (*l*<=≤<=*i*<=≤<=*r*) is suitable for pair programming if and only if the number *i* is lucky for Peter and lucky for Bob at the same time. Help the boys to find the number of such days. | 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 take part in the programming track and one to take part in the sports track.
There should be exactly *p* students in the programming team and exactly *s* students in the sports team. A student can't be a member of both teams.
The university management considers that the strength of the university on the Olympiad is equal to the sum of two values: the programming team strength and the sports team strength. The strength of a team is the sum of skills of its members in the corresponding area, so the strength of the programming team is the sum of all *a**i* and the strength of the sports team is the sum of all *b**i* over corresponding team members.
Help Berland State University to compose two teams to maximize the total strength of the university on the Olympiad. | 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* is the programming skill of the *i*-th student.
The third line contains *n* positive integers *b*1,<=*b*2,<=...,<=*b**n* (1<=≤<=*b**i*<=≤<=3000), where *b**i* is the sports skill of the *i*-th student. | 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 printed in the second and in the third lines should be distinct and can be printed in arbitrary order.
If there are multiple solutions, print any of them. | [
"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 multiple times) between vertices *u* and *v* such that xor of all numbers written on the edges of this path is equal to *s*. When we compute the value s for some path, each edge is counted in xor as many times, as it appear on this path. It's not hard to prove that there are finite number of such triples.
Calculate the sum over modulo 109<=+<=7 of the values of *s* over all interesting triples. | 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 connected by the edge and integer written on it. It is guaranteed that graph doesn't contain self-loops and multiple edges. | 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) 1. (1, 3, 12) 1. (2, 3, 13) 1. (1, 2, 14) | [
{
"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», «racecar», «rotor», «madam».
Nick started to look carefully for all palindromes in the text that they were reading in the class. For each occurrence of each palindrome in the text he wrote a pair — the position of the beginning and the position of the ending of this occurrence in the text. Nick called each occurrence of each palindrome he found in the text subpalindrome. When he found all the subpalindromes, he decided to find out how many different pairs among these subpalindromes cross. Two subpalindromes cross if they cover common positions in the text. No palindrome can cross itself.
Let's look at the actions, performed by Nick, by the example of text «babb». At first he wrote out all subpalindromes:
Then Nick counted the amount of different pairs among these subpalindromes that cross. These pairs were six:
Since it's very exhausting to perform all the described actions manually, Nick asked you to help him and write a program that can find out the amount of different subpalindrome pairs that cross. Two subpalindrome pairs are regarded as different if one of the pairs contains a subpalindrome that the other does not. | 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 all ants are distinct.
All the ants move simultaneously, and whenever two ants touch, they will both switch their directions. Note that it is possible for an ant to move in some direction for a half of a unit of time and in opposite direction for another half of a unit of time.
Print the positions of the ants after *t* time units. | 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) — the position and the direction of the *i*-th ant at the start. The directions L and R corresponds to the clockwise and counterclockwise directions, respectively.
It is guaranteed that all positions *s**i* are distinct. | 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 planned to locate three base stations so as to provide every house in the village with cellular communication. The base station having power *d* located in the point *t* provides with communication all the houses on the segment [*t*<=-<=*d*,<=*t*<=+<=*d*] (including boundaries).
To simplify the integration (and simply not to mix anything up) all the three stations are planned to possess the equal power of *d*. Which minimal value of *d* is enough to provide all the houses in the village with cellular communication. | 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 coordinates are given in a arbitrary order. | 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 coordinates. If there are many solutions, print any of them. | [
"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 introduced a rectangular Cartesian coordinate system on a flat plate and selected four distinct points with integer coordinates where bots will be placed initially. Next each bot will be assigned with one of the four directions (up, down, left or right) parallel to the coordinate axes. After that, each bot is shifted by an integer distance (which may be different for different bots) along its direction. The chain reaction starts, if the bots are in the corners of a square with positive area with sides parallel to the coordinate axes. Each corner of the square must contain one nanobot. This reaction will be stronger, if bots spend less time to move. We can assume that bots move with unit speed. In other words, the lesser is the maximum length traveled by bot, the stronger is reaction.
Scientists have prepared a set of plates and selected starting position for the bots for each plate. Now they ask you to assign the direction for each bot to move after landing such that the maximum length traveled by bot is as small as possible. | 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 are in different locations.
Note, though, the problem can include several records in one test, you can hack other people's submissions only with the test of one plate, i.e. parameter *t* in a hack test should be equal to 1. | 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 lines print two integer numbers — positions of each bot after moving. Print bots' positions in the order they are specified in the input data.
If there are multiple solution, you can print any of them. | [
"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 adventure stories that featured lasers and robots.
Today Petya is watching a shocking blockbuster about the Martians called "R2:D2". What can "R2:D2" possibly mean? It might be the Martian time represented in the Martian numeral system. Petya knows that time on Mars is counted just like on the Earth (that is, there are 24 hours and each hour has 60 minutes). The time is written as "*a*:*b*", where the string *a* stands for the number of hours (from 0 to 23 inclusive), and string *b* stands for the number of minutes (from 0 to 59 inclusive). The only thing Petya doesn't know is in what numeral system the Martian time is written.
Your task is to print the radixes of all numeral system which can contain the time "*a*:*b*". | 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 minutes. The lengths of strings *a* and *b* are from 1 to 5 characters, inclusive. Please note that strings *a* and *b* can have leading zeroes that do not influence the result in any way (for example, string "008:1" in decimal notation denotes correctly written time).
We consider characters 0, 1, ..., 9 as denoting the corresponding digits of the number's representation in some numeral system, and characters A, B, ..., Z correspond to numbers 10, 11, ..., 35. | 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 "*a*:*b*", print the single integer -1.
Note that on Mars any positional numeral systems with positive radix strictly larger than one are possible. | [
"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 numeral notation. There the given string represents time 32:14 that isn't a correct time.
Let's consider the third sample. String "000B:00001" can be perceived as a correct time in the infinite number of numeral systems. If you need an example, you can take any numeral system with radix no less than 12. | [
{
"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 with a few spotlights. The spotlights have already been delivered to certain places and Gerald will not be able to move them. Each spotlight illuminates a specific segment of the path of the given length, one end of the segment is the location of the spotlight, and it can be directed so that it covers the segment to the south or to the north of spotlight.
The trail contains a monument to the mayor of the island, and although you can walk in either directions from the monument, no spotlight is south of the monument.
You are given the positions of the spotlights and their power. Help Gerald direct all the spotlights so that the total length of the illuminated part of the path is as much as possible. | 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 the segment it illuminates.
It is guaranteed that all the *a**i*'s are distinct. | 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 depends on have already completed.
Some of the tasks in the graph can only be executed on a coprocessor, and the rest can only be executed on the main processor. In one coprocessor call you can send it a set of tasks which can only be executed on it. For each task of the set, all tasks on which it depends must be either already completed or be included in the set. The main processor starts the program execution and gets the results of tasks executed on the coprocessor automatically.
Find the minimal number of coprocessor calls which are necessary to execute the given program. | 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, otherwise it can only be executed on the coprocessor.
The next *M* lines describe the dependencies between tasks. Each line contains two space-separated integers *T*1 and *T*2 and means that task *T*1 depends on task *T*2 (*T*1<=≠<=*T*2). Tasks are indexed from 0 to *N*<=-<=1. All *M* pairs (*T*1,<=*T*2) are distinct. It is guaranteed that there are no circular dependencies between tasks. | 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, and finally you execute task 0 on the main processor.
In the second test, tasks 0, 1 and 2 can only be executed on the coprocessor. Tasks 1 and 2 have no dependencies, and task 0 depends on tasks 1 and 2, so all three tasks 0, 1 and 2 can be sent in one coprocessor call. After that task 3 is executed on the main processor. | [
{
"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*) and (*p*,<=*q*) of the matrix are adjacent if |*i*<=-<=*p*|<=+<=|*j*<=-<=*q*|<==<=1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.
Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the *k* candies in the matrix one by one. For each candy Inna chooses cell (*i*,<=*j*) that will contains the candy, and also chooses the path that starts in cell (1,<=1) and ends in cell (*i*,<=*j*) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1,<=1) to cell (*i*,<=*j*), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.
Help Inna to minimize the penalty in the game. | 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 in the format (*i*,<=*j*), where *i* is the number of the row and *j* is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.
Please follow the output format strictly! If your program passes the first pretest, then the output format is correct. | [
"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), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.
Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy. | [
{
"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 with some old name of another or same variable). Among such possibilities he wants to find the way with the smallest possible total length of the new names.
A string $a$ is a prefix of a string $b$ if you can delete some (possibly none) characters from the end of $b$ and obtain $a$.
Please find this minimum possible total length of new names. | 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*. Specially *g*(*z*,<=*z*)<==<=0 for every *z*.
For every integer sequence *p*1,<=*p*2,<=...,<=*p**n* (1<=≤<=*p**i*<=≤<=*n*), DZY defines *f*(*p*) as .
DZY wants to find such a sequence *p* that *f*(*p*) has maximum possible value. But there is one more restriction: the element *j* can appear in *p* at most *x**j* times.
Please, find the maximum possible *f*(*p*) under the described restrictions. | 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 the next *n* lines describes an element of sequence *x*. The *j*-th line contains an integer *x**j* (1<=≤<=*x**j*<=≤<=*n*). | 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 that
- The root is number 1 - Each internal node *i* (*i*<=≤<=2*h*<=-<=1<=-<=1) will have a left child with index = 2*i* and a right child with index = 2*i*<=+<=1
The level of a node is defined as 1 for a root, or 1 + level of parent of the node otherwise. The vertices of the level *h* are called leaves. The exit to the maze is located at some leaf node *n*, the player doesn't know where the exit is so he has to guess his way out!
In the new version of the game the player is allowed to ask questions on the format "Does the *ancestor*(*exit*,<=*i*) node number belong to the range [*L*,<=*R*]?". Here *ancestor*(*v*,<=*i*) is the ancestor of a node *v* that located in the level *i*. The game will answer with "Yes" or "No" only. The game is designed such that it doesn't always answer correctly, and sometimes it cheats to confuse the player!.
Amr asked a lot of questions and got confused by all these answers, so he asked you to help him. Given the questions and its answers, can you identify whether the game is telling contradictory information or not? If the information is not contradictory and the exit node can be determined uniquely, output its number. If the information is not contradictory, but the exit node isn't defined uniquely, output that the number of questions is not sufficient. Otherwise output that the information is contradictory. | 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 described in the statement with its answer (*ans*<==<=1 if the answer is "Yes" and *ans*<==<=0 if the answer is "No"). | 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 number 7.
In the second sample test there are 8 leaf nodes. After the first question the exit is in the range [10, 14]. After the second and the third questions only node number 14 is correct. Check the picture below to fully understand.
<img class="tex-graphics" src="https://espresso.codeforces.com/18ab34534095660d59fe39e53e385d64d0346d2a.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"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 Gorynych has at the moment), or several tails (from 1 to *m*, but not more than Gorynych has at the moment). At the same time, horrible though it seems, Gorynych the Dragon can also grow new heads and tails. And the number of growing heads and tails is determined uniquely by the number of heads or tails cut by the current strike. When the total number of heads and tails exceeds *R*, Gorynych the Dragon strikes its final blow and destroys Ivan the Fool. That’s why Ivan aims to cut off all the dragon’s heads and tails as quickly as possible and win. The events can also develop in a third way: neither of the opponents can win over the other one and they will continue fighting forever.
The tale goes like this; easy to say, hard to do. Your task is to write a program that will determine the battle’s outcome. Consider that Ivan strikes consecutively. After each blow Gorynych grows a number of new heads and tails depending on the number of cut ones. Gorynych the Dragon is defeated if after the blow he loses all his heads and tails and can’t grow new ones. Ivan fights in the optimal way (fools are lucky), i.e.
- if Ivan can win, he wins having struck the least number of blows; - if it is impossible to defeat Gorynych, but is possible to resist him for an infinitely long period of time, then that’s the strategy Ivan chooses; - if Gorynych wins in any case, Ivan aims to resist him for as long as possible. | 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* (1<=≤<=*n*<=≤<=200). The next *n* contain pairs of non-negative numbers "*h**i* *t**i*" which represent the number of heads and the number of tails correspondingly, that will grow if Gorynych has *i* heads (1<=≤<=*i*<=≤<=*n*) cut. The next line contains an integer *m* (1<=≤<=*m*<=≤<=200) and then — the description of Gorynych’s behavior when his tails are cut off in the format identical to the one described above. All the numbers in the input file do not exceed 200. | 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, cycles of length 3. That's why they wonder: what total number of triangles is there in the two graphs formed by Alice and Bob's edges, correspondingly? | 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**i*,<=*b**i*<=≤<=*n*, *a**i*<=≠<=*b**i*), — the numbers of the two vertices connected by the *i*-th edge in Alice's graph. It is guaranteed that Alice's graph contains no multiple edges and self-loops. It is guaranteed that the initial complete graph also contains no multiple edges and self-loops.
Consider the graph vertices to be indexed in some way from 1 to *n*. | 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 the answer to the problem is 4. | [] | 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 to *i*.
On day *i* the person numbered as *a**i* learns the news with the rating of *b**i* (*b**i*<=≥<=1). He phones the friend immediately and tells it. While he is doing it, the news becomes old and its rating falls a little and becomes equal to *b**i*<=-<=1. The friend does the same thing — he also calls his friend and also tells the news. The friend of the friend gets the news already rated as *b**i*<=-<=2. It all continues until the rating of the news reaches zero as nobody wants to tell the news with zero rating.
More formally, everybody acts like this: if a person *x* learns the news with a non-zero rating *y*, he calls his friend *g*(*i*) and his friend learns the news with the rating of *y*<=-<=1 and, if it is possible, continues the process.
Let us note that during a day one and the same person may call his friend and tell him one and the same news with different ratings. Thus, the news with the rating of *b**i* will lead to as much as *b**i* calls.
Your task is to count the values of *res**i* — how many students learned their first news on day *i*.
The values of *b**i* are known initially, whereas *a**i* is determined from the following formula: | 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 *m* space-separated integers *v**i* (1<=≤<=*v**i*<=≤<=107). The fourth line contains *m* space-separated integers *b**i* (1<=≤<=*b**i*<=≤<=107). | 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 the order in which they are given in the input file. Don't output *res*0. | [
"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 lots of cookies.
At the beginning of the game (time 0), Kostya has 0 cookies and no buildings. He has *n* available buildings to choose from: the *i*-th building is worth *c**i* cookies and when it's built it brings *v**i* cookies at the end of each second. Also, to make the game more interesting to play, Kostya decided to add a limit: at each moment of time, he can use only one building. Of course, he can change the active building each second at his discretion.
It's important that Kostya is playing a version of the game where he can buy new buildings and change active building only at time moments that are multiples of one second. Kostya can buy new building and use it at the same time. If Kostya starts to use a building at the time moment *t*, he can get the first profit from it only at the time moment *t*<=+<=1.
Kostya wants to earn at least *s* cookies as quickly as possible. Determine the number of seconds he needs to do that. | 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*-th building brings per second and the building's price. | 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*<=+<=1.
A simple cycle of length *d* (*d*<=><=1) in graph *G* is a sequence of distinct graph nodes *v*1,<=*v*2,<=...,<=*v**d* such, that nodes *v*1 and *v**d* are connected by an edge of the graph, also for any integer *i* (1<=≤<=*i*<=<<=*d*) nodes *v**i* and *v**i*<=+<=1 are connected by an edge of the graph. | 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* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*; *a**i*<=≠<=*b**i*) — the indexes of the graph nodes that are connected by the *i*-th edge.
It is guaranteed that the given graph doesn't contain any multiple edges or self-loops. It is guaranteed that each node of the graph is connected by the edges with at least *k* other nodes of the graph. | 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 of them. | [
"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 of their seating, then she continues to ask pupils from the next row. If the teacher asked the last row, then the direction of the poll changes, it means that she asks the previous row. The order of asking the rows looks as follows: the 1-st row, the 2-nd row, ..., the *n*<=-<=1-st row, the *n*-th row, the *n*<=-<=1-st row, ..., the 2-nd row, the 1-st row, the 2-nd row, ...
The order of asking of pupils on the same row is always the same: the 1-st pupil, the 2-nd pupil, ..., the *m*-th pupil.
During the lesson the teacher managed to ask exactly *k* questions from pupils in order described above. Sergei seats on the *x*-th row, on the *y*-th place in the row. Sergei decided to prove to the teacher that pupils are asked irregularly, help him count three values:
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.
If there is only one row in the class, then the teacher always asks children from this row. | 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 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 table, it means it is Sergei; 1. the pupil from the first row who seats at the second table;
The order of asking pupils in the second test:
1. the pupil from the first row who seats at the first table; 1. the pupil from the first row who seats at the second table; 1. the pupil from the second row who seats at the first table; 1. the pupil from the second row who seats at the second table; 1. the pupil from the third row who seats at the first table; 1. the pupil from the third row who seats at the second table; 1. the pupil from the fourth row who seats at the first table; 1. the pupil from the fourth row who seats at the second table, it means it is Sergei; 1. the pupil from the third row who seats at the first table; | [
{
"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 least *k* edges. Minimal *k*-covering is such a *k*-covering that the size of the subset is minimal possible.
Your task is to find minimal *k*-covering for each , where *minDegree* is the minimal degree of any vertex in graph *G*. | 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*<=≤<=*n*1,<=1<=≤<=*v**i*<=≤<=*n*2) — the description of the *i*-th edge, *u**i* is the index of the vertex in the first part and *v**i* is the index of the vertex in the second part. | 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 pairwise distinct. Edges are numbered 1 through *m* in order they are given in the input. | [
"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 line. Output numbers with 9 characters after a decimal point.
If the required tetragon doen't exist, output NO in the first line, and leave the second line empty. | [
"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); - Direction pointer (DP) which can point left or right;
Initially CP points to the leftmost character of the sequence and DP points to the right.
We repeat the following steps until the first moment that CP points to somewhere outside the sequence.
- If CP is pointing to a digit the interpreter prints that digit then CP moves one step according to the direction of DP. After that the value of the printed digit in the sequence decreases by one. If the printed digit was 0 then it cannot be decreased therefore it's erased from the sequence and the length of the sequence decreases by one. - If CP is pointing to "<" or ">" then the direction of DP changes to "left" or "right" correspondingly. Then CP moves one step according to DP. If the new character that CP is pointing to is "<" or ">" then the previous character will be erased from the sequence.
If at any moment the CP goes outside of the sequence the execution is terminated.
It's obvious the every program in this language terminates after some steps.
We have a sequence *s*1,<=*s*2,<=...,<=*s**n* of "<", ">" and digits. You should answer *q* queries. Each query gives you *l* and *r* and asks how many of each digit will be printed if we run the sequence *s**l*,<=*s**l*<=+<=1,<=...,<=*s**r* as an independent program in this language. | 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 spaces.
The next *q* lines each contains two integers *l**i* and *r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*) — the *i*-th query. | 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 operations may be applied at most once (and may be not applied at all) so you can remove only one segment and each number may be changed (increased or decreased) by at most 1. Also note, that you are not allowed to delete the whole array.
Your goal is to calculate the minimum number of coins that you need to spend in order to make the greatest common divisor of the elements of the resulting array be greater than 1. | 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*<=≤<=109) — elements of the array. | 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 queried we check if it is present in the cache and move it here if it's not. If there are more than *k* objects in the cache after this, the least recently used one should be removed. In other words, we remove the object that has the smallest time of the last query.
Consider there are *n* videos being stored on the server, all of the same size. Cache can store no more than *k* videos and caching algorithm described above is applied. We know that any time a user enters the server he pick the video *i* with probability *p**i*. The choice of the video is independent to any events before.
The goal of this problem is to count for each of the videos the probability it will be present in the cache after 10100 queries. | 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 of all *p**i* is equal to 1. | 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 jury is *b*. The checker program will consider your answer correct, if . | [
"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 piece should be at most *s*.
Please help Alexandra to find the minimal number of pieces meeting the condition above. | 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 and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to *a* and *b* correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to *l* and *w* (*l*<=≥<=*w*) correspondingly. Dwarf Greg has already determined the coffin's length (*l*), which is based on his height; your task is to determine the coffin's maximally possible width (*w*), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him... | 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 answer is positive, it will be not less than 10<=-<=7. All the hacks will also be checked to meet that condition. | [
"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, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | [] | 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,<=*b*2,<=...,<=*b**n* of length *n*. Also, you've got *m* queries of two types:
1. Copy the subsegment of array *a* of length *k*, starting from position *x*, into array *b*, starting from position *y*, that is, execute *b**y*<=+<=*q*<==<=*a**x*<=+<=*q* for all integer *q* (0<=≤<=*q*<=<<=*k*). The given operation is correct — both subsegments do not touch unexistent elements. 1. Determine the value in position *x* of array *b*, that is, find value *b**x*.
For each query of the second type print the result — the value of the corresponding element of array *b*. | 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,<=*b*2,<=...,<=*b**n* (|*b**i*|<=≤<=109).
Next *m* lines contain the descriptions of the queries. The *i*-th line first contains integer *t**i* — the type of the *i*-th query (1<=≤<=*t**i*<=≤<=2). If *t**i*<==<=1, then the *i*-th query means the copying operation. If *t**i*<==<=2, then the *i*-th query means taking the value in array *b*. If *t**i*<==<=1, then the query type is followed by three integers *x**i*,<=*y**i*,<=*k**i* (1<=≤<=*x**i*,<=*y**i*,<=*k**i*<=≤<=*n*) — the parameters of the copying query. If *t**i*<==<=2, then the query type is followed by integer *x**i* (1<=≤<=*x**i*<=≤<=*n*) — the position in array *b*.
All numbers in the lines are separated with single spaces. It is guaranteed that all the queries are correct, that is, the copying borders fit into the borders of arrays *a* and *b*. | 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 whose length equals *L*. Edward's tree is *n* layers high and Edward plans to hang the garland so as to decorate the first layer with the first *l*1 lamps, the second layer — with the next *l*2 lamps and so on. The last *n*-th layer should be decorated with the last *l**n* lamps,
Edward adores all sorts of math puzzles, so he suddenly wondered: how many different ways to assemble the garland are there given that the both following two conditions are met:
1. Any two lamps that follow consecutively in the same layer should have different colors. 1. The sets of used colors in every two neighbouring layers must be different. We consider unordered sets (not multisets), where every color occurs no more than once. So the number of lamps of particular color does not matter.
Help Edward find the answer to this nagging problem or else he won't manage to decorate the Tree by New Year. You may consider that Edward has an unlimited number of lamps of each of *m* colors and it is not obligatory to use all *m* colors. The garlands are considered different if they differ in at least one position when represented as sequences. Calculate the answer modulo *p*. | 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*<=≥<=(*i*<=+<=*k*).
For example, if *n*<==<=5, a possible *A* is {5,<=1,<=4,<=2,<=3}. For *k*<==<=2, *B* is given by {1,<=2,<=1,<=0,<=0}. But if *k*<==<=3, then *B*<==<={1,<=1,<=0,<=0,<=0}.
For two sequences *X*<==<={*x*1,<=*x*2,<=...,<=*x**n*} and *Y*<==<={*y*1,<=*y*2,<=...,<=*y**n*}, let *i*-th elements be the first elements such that *x**i*<=≠<=*y**i*. If *x**i*<=<<=*y**i*, then *X* is lexicographically smaller than *Y*, while if *x**i*<=><=*y**i*, then *X* is lexicographically greater than *Y*.
Given *n*, *k* and *B*, you need to determine the lexicographically smallest *A*. | 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 *n*<=-<=1 roads. You can reach any city from the capital, that is, the road network forms a tree.
The Ministry of Roads fears that after the reform those roads that will connect the towns of different states will bring a lot of trouble.
Your task is to come up with a plan to divide the country into states such that:
- each state is connected, i.e. for each state it is possible to get from any town to any other using its roads (that is, the roads that connect the state towns), - there is a state that consisted of exactly *k* cities, - the number of roads that connect different states is minimum. | 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 are numbered from 1 to *n*. | 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 there are no "problem" roads at all, print a single integer 0 and either leave the second line empty or do not print it at all. | [
"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.