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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
578 | A Problem about Polyline | [
"geometry",
"math"
] | null | null | There is a polyline going through points (0,<=0)<=β<=(*x*,<=*x*)<=β<=(2*x*,<=0)<=β<=(3*x*,<=*x*)<=β<=(4*x*,<=0)<=β<=...<=-<=(2*kx*,<=0)<=β<=(2*kx*<=+<=*x*,<=*x*)<=β<=....
We know that the polyline passes through the point (*a*,<=*b*). Find minimum positive value *x* such that it is true or determine that there is no such *x*. | Only one line containing two positive integers *a* and *b* (1<=β€<=*a*,<=*b*<=β€<=109). | Output the only line containing the answer. Your answer will be considered correct if its relative or absolute error doesn't exceed 10<=-<=9. If there is no such *x* then output <=-<=1 as the answer. | [
"3 1\n",
"1 3\n",
"4 1\n"
] | [
"1.000000000000\n",
"-1\n",
"1.250000000000\n"
] | You can see following graphs for sample 1 and sample 3. | [
{
"input": "3 1",
"output": "1.000000000000"
},
{
"input": "1 3",
"output": "-1"
},
{
"input": "4 1",
"output": "1.250000000000"
},
{
"input": "1000000000 1000000000",
"output": "1000000000.000000000000"
},
{
"input": "1000000000 1",
"output": "1.000000001000"... | 93 | 0 | 3 | 9,156 | |
665 | Simple Subset | [
"constructive algorithms",
"greedy",
"number theory"
] | null | null | A tuple of positive integers {*x*1,<=*x*2,<=...,<=*x**k*} is called simple if for all pairs of positive integers (*i*,<=<=*j*) (1<=<=β€<=*i*<=<=<<=<=*j*<=β€<=*k*), *x**i*<=<=+<=<=*x**j* is a prime.
You are given an array *a* with *n* positive integers *a*1,<=<=*a*2,<=<=...,<=<=*a**n* (not necessary distinct). You want to find a simple subset of the array *a* with the maximum size.
A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.
Let's define a subset of the array *a* as a tuple that can be obtained from *a* by removing some (possibly all) elements of it. | The first line contains integer *n* (1<=β€<=*n*<=β€<=1000) β the number of integers in the array *a*.
The second line contains *n* integers *a**i* (1<=β€<=*a**i*<=β€<=106) β the elements of the array *a*. | On the first line print integer *m* β the maximum possible size of simple subset of *a*.
On the second line print *m* integers *b**l* β the elements of the simple subset of the array *a* with the maximum size.
If there is more than one solution you can print any of them. You can print the elements of the subset in any order. | [
"2\n2 3\n",
"2\n2 2\n",
"3\n2 1 1\n",
"2\n83 14\n"
] | [
"2\n3 2\n",
"1\n2\n",
"3\n1 1 2\n",
"2\n14 83\n"
] | none | [
{
"input": "2\n2 3",
"output": "2\n3 2"
},
{
"input": "2\n2 2",
"output": "1\n2"
},
{
"input": "3\n2 1 1",
"output": "3\n1 1 2"
},
{
"input": "2\n83 14",
"output": "2\n14 83"
},
{
"input": "10\n10 10 1 2 3 3 1 2 1 5",
"output": "4\n1 1 10 1"
},
{
"inpu... | 373 | 31,641,600 | 3 | 9,158 | |
123 | String | [
"string suffix structures"
] | null | null | You are given a string *s*. Each pair of numbers *l* and *r* that fulfill the condition 1<=β€<=*l*<=β€<=*r*<=β€<=|*s*|, correspond to a substring of the string *s*, starting in the position *l* and ending in the position *r* (inclusive).
Let's define the function of two strings *F*(*x*,<=*y*) like this. We'll find a list of such pairs of numbers for which the corresponding substrings of string *x* are equal to string *y*. Let's sort this list of pairs according to the pair's first number's increasing. The value of function *F*(*x*,<=*y*) equals the number of non-empty continuous sequences in the list.
For example: *F*(*babbabbababbab*,<=*babb*)<==<=6. The list of pairs is as follows:
(1,<=4),<=(4,<=7),<=(9,<=12)
Its continuous sequences are:
- (1,<=4) - (4,<=7) - (9,<=12) - (1,<=4),<=(4,<=7) - (4,<=7),<=(9,<=12) - (1,<=4),<=(4,<=7),<=(9,<=12)
Your task is to calculate for the given string *s* the sum *F*(*s*,<=*x*) for all *x*, that *x* belongs to the set of all substrings of a string *s*. | The only line contains the given string *s*, consisting only of small Latin letters (1<=β€<=|*s*|<=β€<=105). | Print the single number β the sought sum.
Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator. | [
"aaaa\n",
"abcdef\n",
"abacabadabacaba\n"
] | [
"20\n",
"21\n",
"188\n"
] | In the first sample the function values at *x* equal to "a", "aa", "aaa" and "aaaa" equal 10, 6, 3 and 1 correspondingly.
In the second sample for any satisfying *x* the function value is 1. | [
{
"input": "aaaa",
"output": "20"
},
{
"input": "abcdef",
"output": "21"
},
{
"input": "abacabadabacaba",
"output": "188"
},
{
"input": "tkth",
"output": "11"
},
{
"input": "eqkrqe",
"output": "23"
},
{
"input": "cwuiax",
"output": "21"
},
{
... | 30 | 0 | 0 | 9,208 | |
599 | Spongebob and Joke | [
"implementation"
] | null | null | While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. The naughty Sponge browsed through Patrick's personal stuff and found a sequence *a*1,<=*a*2,<=...,<=*a**m* of length *m*, consisting of integers from 1 to *n*, not necessarily distinct. Then he picked some sequence *f*1,<=*f*2,<=...,<=*f**n* of length *n* and for each number *a**i* got number *b**i*<==<=*f**a**i*. To finish the prank he erased the initial sequence *a**i*.
It's hard to express how sad Patrick was when he returned home from shopping! We will just say that Spongebob immediately got really sorry about what he has done and he is now trying to restore the original sequence. Help him do this or determine that this is impossible. | The first line of the input contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=100<=000)Β β the lengths of sequences *f**i* and *b**i* respectively.
The second line contains *n* integers, determining sequence *f*1,<=*f*2,<=...,<=*f**n* (1<=β€<=*f**i*<=β€<=*n*).
The last line contains *m* integers, determining sequence *b*1,<=*b*2,<=...,<=*b**m* (1<=β€<=*b**i*<=β€<=*n*). | Print "Possible" if there is exactly one sequence *a**i*, such that *b**i*<==<=*f**a**i* for all *i* from 1 to *m*. Then print *m* integers *a*1,<=*a*2,<=...,<=*a**m*.
If there are multiple suitable sequences *a**i*, print "Ambiguity".
If Spongebob has made a mistake in his calculations and no suitable sequence *a**i* exists, print "Impossible". | [
"3 3\n3 2 1\n1 2 3\n",
"3 3\n1 1 1\n1 1 1\n",
"3 3\n1 2 1\n3 3 3\n"
] | [
"Possible\n3 2 1 \n",
"Ambiguity\n",
"Impossible\n"
] | In the first sample 3 is replaced by 1 and vice versa, while 2 never changes. The answer exists and is unique.
In the second sample all numbers are replaced by 1, so it is impossible to unambiguously restore the original sequence.
In the third sample *f*<sub class="lower-index">*i*</sub>ββ β3 for all *i*, so no sequence *a*<sub class="lower-index">*i*</sub> transforms into such *b*<sub class="lower-index">*i*</sub> and we can say for sure that Spongebob has made a mistake. | [
{
"input": "3 3\n3 2 1\n1 2 3",
"output": "Possible\n3 2 1 "
},
{
"input": "3 3\n1 1 1\n1 1 1",
"output": "Ambiguity"
},
{
"input": "3 3\n1 2 1\n3 3 3",
"output": "Impossible"
},
{
"input": "2 100\n2 1\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1... | 280 | 13,824,000 | 3 | 9,269 | |
523 | Statistics of Recompressing Videos | [
"*special",
"data structures",
"implementation"
] | null | null | A social network for dogs called DH (DogHouse) has *k* special servers to recompress uploaded videos of cute cats. After each video is uploaded, it should be recompressed on one (any) of the servers, and only after that it can be saved in the social network.
We know that each server takes one second to recompress a one minute fragment. Thus, any server takes *m* seconds to recompress a *m* minute video.
We know the time when each of the *n* videos were uploaded to the network (in seconds starting from the moment all servers started working). All videos appear at different moments of time and they are recompressed in the order they appear. If some video appeared at time *s*, then its recompressing can start at that very moment, immediately. Some videos can await recompressing when all the servers are busy. In this case, as soon as a server is available, it immediately starts recompressing another video. The videos that await recompressing go in a queue. If by the moment the videos started being recompressed some servers are available, then any of them starts recompressing the video.
For each video find the moment it stops being recompressed. | The first line of the input contains integers *n* and *k* (1<=β€<=*n*,<=*k*<=β€<=5Β·105) β the number of videos and servers, respectively.
Next *n* lines contain the descriptions of the videos as pairs of integers *s**i*,<=*m**i* (1<=β€<=*s**i*,<=*m**i*<=β€<=109), where *s**i* is the time in seconds when the *i*-th video appeared and *m**i* is its duration in minutes. It is guaranteed that all the *s**i*'s are distinct and the videos are given in the chronological order of upload, that is in the order of increasing *s**i*. | Print *n* numbers *e*1,<=*e*2,<=...,<=*e**n*, where *e**i* is the time in seconds after the servers start working, when the *i*-th video will be recompressed. | [
"3 2\n1 5\n2 5\n3 5\n",
"6 1\n1 1000000000\n2 1000000000\n3 1000000000\n4 1000000000\n5 1000000000\n6 3\n"
] | [
"6\n7\n11\n",
"1000000001\n2000000001\n3000000001\n4000000001\n5000000001\n5000000004\n"
] | none | [
{
"input": "3 2\n1 5\n2 5\n3 5",
"output": "6\n7\n11"
},
{
"input": "6 1\n1 1000000000\n2 1000000000\n3 1000000000\n4 1000000000\n5 1000000000\n6 3",
"output": "1000000001\n2000000001\n3000000001\n4000000001\n5000000001\n5000000004"
},
{
"input": "1 1\n1 1",
"output": "2"
},
{
... | 3,000 | 3,174,400 | 0 | 9,276 | |
407 | Long Path | [
"dp",
"implementation"
] | null | null | One day, little Vasya found himself in a maze consisting of (*n*<=+<=1) rooms, numbered from 1 to (*n*<=+<=1). Initially, Vasya is at the first room and to get out of the maze, he needs to get to the (*n*<=+<=1)-th one.
The maze is organized as follows. Each room of the maze has two one-way portals. Let's consider room number *i* (1<=β€<=*i*<=β€<=*n*), someone can use the first portal to move from it to room number (*i*<=+<=1), also someone can use the second portal to move from it to room number *p**i*, where 1<=β€<=*p**i*<=β€<=*i*.
In order not to get lost, Vasya decided to act as follows.
- Each time Vasya enters some room, he paints a cross on its ceiling. Initially, Vasya paints a cross at the ceiling of room 1. - Let's assume that Vasya is in room *i* and has already painted a cross on its ceiling. Then, if the ceiling now contains an odd number of crosses, Vasya uses the second portal (it leads to room *p**i*), otherwise Vasya uses the first portal.
Help Vasya determine the number of times he needs to use portals to get to room (*n*<=+<=1) in the end. | The first line contains integer *n* (1<=β€<=*n*<=β€<=103)Β β the number of rooms. The second line contains *n* integers *p**i* (1<=β€<=*p**i*<=β€<=*i*). Each *p**i* denotes the number of the room, that someone can reach, if he will use the second portal in the *i*-th room. | Print a single number β the number of portal moves the boy needs to go out of the maze. As the number can be rather large, print it modulo 1000000007 (109<=+<=7). | [
"2\n1 2\n",
"4\n1 1 2 3\n",
"5\n1 1 1 1 1\n"
] | [
"4\n",
"20\n",
"62\n"
] | none | [
{
"input": "2\n1 2",
"output": "4"
},
{
"input": "4\n1 1 2 3",
"output": "20"
},
{
"input": "5\n1 1 1 1 1",
"output": "62"
},
{
"input": "7\n1 2 1 3 1 2 1",
"output": "154"
},
{
"input": "1\n1",
"output": "2"
},
{
"input": "3\n1 1 3",
"output": "8"... | 93 | 20,172,800 | 3 | 9,284 | |
215 | Olympic Medal | [
"greedy",
"math"
] | null | null | The World Programming Olympics Medal is a metal disk, consisting of two parts: the first part is a ring with outer radius of *r*1 cm, inner radius of *r*2 cm, (0<=<<=*r*2<=<<=*r*1) made of metal with density *p*1 g/cm3. The second part is an inner disk with radius *r*2 cm, it is made of metal with density *p*2 g/cm3. The disk is nested inside the ring.
The Olympic jury decided that *r*1 will take one of possible values of *x*1,<=*x*2,<=...,<=*x**n*. It is up to jury to decide which particular value *r*1 will take. Similarly, the Olympic jury decided that *p*1 will take one of possible value of *y*1,<=*y*2,<=...,<=*y**m*, and *p*2 will take a value from list *z*1,<=*z*2,<=...,<=*z**k*.
According to most ancient traditions the ratio between the outer ring mass *m**out* and the inner disk mass *m**in* must equal , where *A*,<=*B* are constants taken from ancient books. Now, to start making medals, the jury needs to take values for *r*1, *p*1, *p*2 and calculate the suitable value of *r*2.
The jury wants to choose the value that would maximize radius *r*2. Help the jury find the sought value of *r*2. Value *r*2 doesn't have to be an integer.
Medal has a uniform thickness throughout the area, the thickness of the inner disk is the same as the thickness of the outer ring. | The first input line contains an integer *n* and a sequence of integers *x*1,<=*x*2,<=...,<=*x**n*. The second input line contains an integer *m* and a sequence of integers *y*1,<=*y*2,<=...,<=*y**m*. The third input line contains an integer *k* and a sequence of integers *z*1,<=*z*2,<=...,<=*z**k*. The last line contains two integers *A* and *B*.
All numbers given in the input are positive and do not exceed 5000. Each of the three sequences contains distinct numbers. The numbers in the lines are separated by spaces. | Print a single real number β the sought value *r*2 with absolute or relative error of at most 10<=-<=6. It is guaranteed that the solution that meets the problem requirements exists. | [
"3 1 2 3\n1 2\n3 3 2 1\n1 2\n",
"4 2 3 6 4\n2 1 2\n3 10 6 8\n2 1\n"
] | [
"2.683281573000\n",
"2.267786838055\n"
] | In the first sample the jury should choose the following values: *r*<sub class="lower-index">1</sub>β=β3, *p*<sub class="lower-index">1</sub>β=β2, *p*<sub class="lower-index">2</sub>β=β1. | [
{
"input": "3 1 2 3\n1 2\n3 3 2 1\n1 2",
"output": "2.683281573000"
},
{
"input": "4 2 3 6 4\n2 1 2\n3 10 6 8\n2 1",
"output": "2.267786838055"
},
{
"input": "1 5\n1 3\n1 7\n515 892",
"output": "3.263613058533"
},
{
"input": "2 3 2\n3 2 3 1\n2 2 1\n733 883",
"output": "2.... | 186 | 0 | 0 | 9,290 | |
0 | none | [
"none"
] | null | null | On vacations *n* pupils decided to go on excursion and gather all together. They need to overcome the path with the length *l* meters. Each of the pupils will go with the speed equal to *v*1. To get to the excursion quickly, it was decided to rent a bus, which has seats for *k* people (it means that it can't fit more than *k* people at the same time) and the speed equal to *v*2. In order to avoid seasick, each of the pupils want to get into the bus no more than once.
Determine the minimum time required for all *n* pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected. | The first line of the input contains five positive integers *n*, *l*, *v*1, *v*2 and *k* (1<=β€<=*n*<=β€<=10<=000, 1<=β€<=*l*<=β€<=109, 1<=β€<=*v*1<=<<=*v*2<=β€<=109, 1<=β€<=*k*<=β€<=*n*)Β β the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus. | Print the real numberΒ β the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10<=-<=6. | [
"5 10 1 2 5\n",
"3 6 1 2 1\n"
] | [
"5.0000000000\n",
"4.7142857143\n"
] | In the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10β/β2β=β5. | [
{
"input": "5 10 1 2 5",
"output": "5.0000000000"
},
{
"input": "3 6 1 2 1",
"output": "4.7142857143"
},
{
"input": "39 252 51 98 26",
"output": "3.5344336938"
},
{
"input": "59 96 75 98 9",
"output": "1.2315651330"
},
{
"input": "87 237 3 21 40",
"output": "3... | 46 | 0 | 0 | 9,321 | |
794 | Naming Company | [
"games",
"greedy",
"sortings"
] | null | null | Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little things. Recently, they started a new company, but they are having trouble finding a name for the company.
To settle this problem, they've decided to play a game. The company name will consist of *n* letters. Oleg and Igor each have a set of *n* letters (which might contain multiple copies of the same letter, the sets can be different). Initially, the company name is denoted by *n* question marks. Oleg and Igor takes turns to play the game, Oleg moves first. In each turn, a player can choose one of the letters *c* in his set and replace any of the question marks with *c*. Then, a copy of the letter *c* is removed from his set. The game ends when all the question marks has been replaced by some letter.
For example, suppose Oleg has the set of letters {*i*,<=*o*,<=*i*} and Igor has the set of letters {*i*,<=*m*,<=*o*}. One possible game is as follows :
Initially, the company name is ???.
Oleg replaces the second question mark with 'i'. The company name becomes ?i?. The set of letters Oleg have now is {*i*,<=*o*}.
Igor replaces the third question mark with 'o'. The company name becomes ?io. The set of letters Igor have now is {*i*,<=*m*}.
Finally, Oleg replaces the first question mark with 'o'. The company name becomes oio. The set of letters Oleg have now is {*i*}.
In the end, the company name is oio.
Oleg wants the company name to be as lexicographically small as possible while Igor wants the company name to be as lexicographically large as possible. What will be the company name if Oleg and Igor always play optimally?
A string *s*<==<=*s*1*s*2...*s**m* is called lexicographically smaller than a string *t*<==<=*t*1*t*2...*t**m* (where *s*<=β <=*t*) if *s**i*<=<<=*t**i* where *i* is the smallest index such that *s**i*<=β <=*t**i*. (so *s**j*<==<=*t**j* for all *j*<=<<=*i*) | The first line of input contains a string *s* of length *n* (1<=β€<=*n*<=β€<=3Β·105). All characters of the string are lowercase English letters. This string denotes the set of letters Oleg has initially.
The second line of input contains a string *t* of length *n*. All characters of the string are lowercase English letters. This string denotes the set of letters Igor has initially. | The output should contain a string of *n* lowercase English letters, denoting the company name if Oleg and Igor plays optimally. | [
"tinkoff\nzscoder\n",
"xxxxxx\nxxxxxx\n",
"ioi\nimo\n"
] | [
"fzfsirk\n",
"xxxxxx\n",
"ioi\n"
] | One way to play optimally in the first sample is as follows :
- Initially, the company name is ???????.- Oleg replaces the first question mark with 'f'. The company name becomes f??????.- Igor replaces the second question mark with 'z'. The company name becomes fz?????.- Oleg replaces the third question mark with 'f'. The company name becomes fzf????.- Igor replaces the fourth question mark with 's'. The company name becomes fzfs???.- Oleg replaces the fifth question mark with 'i'. The company name becomes fzfsi??.- Igor replaces the sixth question mark with 'r'. The company name becomes fzfsir?.- Oleg replaces the seventh question mark with 'k'. The company name becomes fzfsirk.
For the second sample, no matter how they play, the company name will always be xxxxxx. | [
{
"input": "tinkoff\nzscoder",
"output": "fzfsirk"
},
{
"input": "xxxxxx\nxxxxxx",
"output": "xxxxxx"
},
{
"input": "ioi\nimo",
"output": "ioi"
},
{
"input": "abc\naaa",
"output": "aab"
},
{
"input": "reddit\nabcdef",
"output": "dfdeed"
},
{
"input": "... | 31 | 0 | -1 | 9,337 | |
241 | Old Peykan | [
"greedy"
] | null | null | There are *n* cities in the country where the Old Peykan lives. These cities are located on a straight line, we'll denote them from left to right as *c*1,<=*c*2,<=...,<=*c**n*. The Old Peykan wants to travel from city *c*1 to *c**n* using roads. There are (*n*<=-<=1) one way roads, the *i*-th road goes from city *c**i* to city *c**i*<=+<=1 and is *d**i* kilometers long.
The Old Peykan travels 1 kilometer in 1 hour and consumes 1 liter of fuel during this time.
Each city *c**i* (except for the last city *c**n*) has a supply of *s**i* liters of fuel which immediately transfers to the Old Peykan if it passes the city or stays in it. This supply refreshes instantly *k* hours after it transfers. The Old Peykan can stay in a city for a while and fill its fuel tank many times.
Initially (at time zero) the Old Peykan is at city *c*1 and *s*1 liters of fuel is transferred to it's empty tank from *c*1's supply. The Old Peykan's fuel tank capacity is unlimited. Old Peykan can not continue its travel if its tank is emptied strictly between two cities.
Find the minimum time the Old Peykan needs to reach city *c**n*. | The first line of the input contains two space-separated integers *m* and *k* (1<=β€<=*m*,<=*k*<=β€<=1000). The value *m* specifies the number of roads between cities which is equal to *n*<=-<=1.
The next line contains *m* space-separated integers *d*1,<=*d*2,<=...,<=*d**m* (1<=β€<=*d**i*<=β€<=1000) and the following line contains *m* space-separated integers *s*1,<=*s*2,<=...,<=*s**m* (1<=β€<=*s**i*<=β€<=1000). | In the only line of the output print a single integer β the minimum time required for The Old Peykan to reach city *c**n* from city *c*1. | [
"4 6\n1 2 5 2\n2 3 3 4\n",
"2 3\n5 6\n5 5\n"
] | [
"10\n",
"14\n"
] | In the second sample above, the Old Peykan stays in *c*<sub class="lower-index">1</sub> for 3 hours. | [
{
"input": "4 6\n1 2 5 2\n2 3 3 4",
"output": "10"
},
{
"input": "2 3\n5 6\n5 5",
"output": "14"
},
{
"input": "24 3\n11 8 8 12 17 4 4 25 39 37 31 32 38 34 29 29 34 39 39 39 17 9 24 6\n3 5 4 3 3 3 4 3 4 3 3 3 3 4 3 3 4 3 4 3 3 3 3 3",
"output": "862"
},
{
"input": "43 5\n6 7 ... | 92 | 0 | 0 | 9,339 | |
656 | Out of Controls | [
"*special"
] | null | null | You are given a complete undirected graph. For each pair of vertices you are given the length of the edge that connects them. Find the shortest paths between each pair of vertices in the graph and return the length of the longest of them. | The first line of the input contains a single integer *N* (3<=β€<=*N*<=β€<=10).
The following *N* lines each contain *N* space-separated integers. *j*th integer in *i*th line *a**ij* is the length of the edge that connects vertices *i* and *j*. *a**ij*<==<=*a**ji*, *a**ii*<==<=0, 1<=β€<=*a**ij*<=β€<=100 for *i*<=β <=*j*. | Output the maximum length of the shortest path between any pair of vertices in the graph. | [
"3\n0 1 1\n1 0 4\n1 4 0\n",
"4\n0 1 2 3\n1 0 4 5\n2 4 0 6\n3 5 6 0\n"
] | [
"2\n",
"5\n"
] | You're running short of keywords, so you can't use some of them: | [
{
"input": "3\n0 1 1\n1 0 4\n1 4 0",
"output": "2"
},
{
"input": "4\n0 1 2 3\n1 0 4 5\n2 4 0 6\n3 5 6 0",
"output": "5"
},
{
"input": "10\n0 16 67 7 82 44 25 13 25 42\n16 0 24 37 63 20 19 87 55 99\n67 24 0 81 19 71 35 6 20 91\n7 37 81 0 82 89 34 80 7 32\n82 63 19 82 0 42 66 96 42 99\n44 ... | 46 | 4,915,200 | -1 | 9,350 | |
0 | none | [
"none"
] | null | null | You've gotten an *n*<=Γ<=*m* sheet of squared paper. Some of its squares are painted. Let's mark the set of all painted squares as *A*. Set *A* is connected. Your task is to find the minimum number of squares that we can delete from set *A* to make it not connected.
A set of painted squares is called connected, if for every two squares *a* and *b* from this set there is a sequence of squares from the set, beginning in *a* and ending in *b*, such that in this sequence any square, except for the last one, shares a common side with the square that follows next in the sequence. An empty set and a set consisting of exactly one square are connected by definition. | The first input line contains two space-separated integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=50) β the sizes of the sheet of paper.
Each of the next *n* lines contains *m* characters β the description of the sheet of paper: the *j*-th character of the *i*-th line equals either "#", if the corresponding square is painted (belongs to set *A*), or equals "." if the corresponding square is not painted (does not belong to set *A*). It is guaranteed that the set of all painted squares *A* is connected and isn't empty. | On the first line print the minimum number of squares that need to be deleted to make set *A* not connected. If it is impossible, print -1. | [
"5 4\n####\n#..#\n#..#\n#..#\n####\n",
"5 5\n#####\n#...#\n#####\n#...#\n#####\n"
] | [
"2\n",
"2\n"
] | In the first sample you can delete any two squares that do not share a side. After that the set of painted squares is not connected anymore.
The note to the second sample is shown on the figure below. To the left there is a picture of the initial set of squares. To the right there is a set with deleted squares. The deleted squares are marked with crosses. | [
{
"input": "5 4\n####\n#..#\n#..#\n#..#\n####",
"output": "2"
},
{
"input": "5 5\n#####\n#...#\n#####\n#...#\n#####",
"output": "2"
},
{
"input": "1 10\n.########.",
"output": "1"
},
{
"input": "1 1\n#",
"output": "-1"
},
{
"input": "3 3\n.#.\n###\n.#.",
"outp... | 2,000 | 30,515,200 | 0 | 9,383 | |
768 | Game of Stones | [
"bitmasks",
"dp",
"games"
] | null | null | Sam has been teaching Jon the Game of Stones to sharpen his mind and help him devise a strategy to fight the white walkers. The rules of this game are quite simple:
- The game starts with *n* piles of stones indexed from 1 to *n*. The *i*-th pile contains *s**i* stones.- The players make their moves alternatively. A move is considered as removal of some number of stones from a pile. Removal of 0 stones does not count as a move.- The player who is unable to make a move loses.
Now Jon believes that he is ready for battle, but Sam does not think so. To prove his argument, Sam suggested that they play a modified version of the game.
In this modified version, no move can be made more than once on a pile. For example, if 4 stones are removed from a pile, 4 stones cannot be removed from that pile again.
Sam sets up the game and makes the first move. Jon believes that Sam is just trying to prevent him from going to battle. Jon wants to know if he can win if both play optimally. | First line consists of a single integer *n* (1<=β€<=*n*<=β€<=106) β the number of piles.
Each of next *n* lines contains an integer *s**i* (1<=β€<=*s**i*<=β€<=60) β the number of stones in *i*-th pile. | Print a single line containing "YES" (without quotes) if Jon wins, otherwise print "NO" (without quotes) | [
"1\n5\n",
"2\n1\n2\n"
] | [
"NO",
"YES"
] | In the first case, Sam removes all the stones and Jon loses.
In second case, the following moves are possible by Sam: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/53b9c060b675da85f39a960b8ab29df7fe51f6e3.png" style="max-width: 100.0%;max-height: 100.0%;"/>
In each of these cases, last move can be made by Jon to win the game as follows: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5089ff5bcdbeb10a07b0bf16566d6f4703e99334.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "1\n5",
"output": "NO"
},
{
"input": "2\n1\n2",
"output": "YES"
},
{
"input": "3\n34\n44\n21",
"output": "NO"
},
{
"input": "6\n34\n44\n21\n55\n1\n36",
"output": "NO"
},
{
"input": "14\n34\n44\n21\n55\n1\n36\n53\n31\n58\n59\n11\n40\n20\n32",
"output... | 3,000 | 10,956,800 | 0 | 9,385 | |
0 | none | [
"none"
] | null | null | Students love to celebrate their holidays. Especially if the holiday is the day of the end of exams!
Despite the fact that Igor K., unlike his groupmates, failed to pass a programming test, he decided to invite them to go to a cafe so that each of them could drink a bottle of... fresh cow milk. Having entered the cafe, the *m* friends found *n* different kinds of milk on the menu, that's why they ordered *n* bottles β one bottle of each kind. We know that the volume of milk in each bottle equals *w*.
When the bottles were brought in, they decided to pour all the milk evenly among the *m* cups, so that each got a cup. As a punishment for not passing the test Igor was appointed the person to pour the milk. He protested that he was afraid to mix something up and suggested to distribute the drink so that the milk from each bottle was in no more than two different cups. His friends agreed but they suddenly faced the following problem β and what is actually the way to do it?
Help them and write the program that will help to distribute the milk among the cups and drink it as quickly as possible!
Note that due to Igor K.'s perfectly accurate eye and unswerving hands, he can pour any fractional amount of milk from any bottle to any cup. | The only input data file contains three integers *n*, *w* and *m* (1<=β€<=*n*<=β€<=50, 100<=β€<=*w*<=β€<=1000, 2<=β€<=*m*<=β€<=50), where *n* stands for the number of ordered bottles, *w* stands for the volume of each of them and *m* stands for the number of friends in the company. | Print on the first line "YES" if it is possible to pour the milk so that the milk from each bottle was in no more than two different cups. If there's no solution, print "NO".
If there is a solution, then print *m* more lines, where the *i*-th of them describes the content of the *i*-th student's cup. The line should consist of one or more pairs that would look like "*b* *v*". Each such pair means that *v* (*v*<=><=0) units of milk were poured into the *i*-th cup from bottle *b* (1<=β€<=*b*<=β€<=*n*). All numbers *b* on each line should be different.
If there are several variants to solve the problem, print any of them. Print the real numbers with no less than 6 digits after the decimal point. | [
"2 500 3\n",
"4 100 5\n",
"4 100 7\n",
"5 500 2\n"
] | [
"YES\n1 333.333333\n2 333.333333\n2 166.666667 1 166.666667\n",
"YES\n3 20.000000 4 60.000000\n1 80.000000\n4 40.000000 2 40.000000\n3 80.000000\n2 60.000000 1 20.000000\n",
"NO\n",
"YES\n4 250.000000 5 500.000000 2 500.000000\n3 500.000000 1 500.000000 4 250.000000\n"
] | none | [
{
"input": "2 500 3",
"output": "YES\n1 333.333333\n2 333.333333\n2 166.666667 1 166.666667"
},
{
"input": "4 100 5",
"output": "YES\n3 20.000000 4 60.000000\n1 80.000000\n4 40.000000 2 40.000000\n3 80.000000\n2 60.000000 1 20.000000"
},
{
"input": "4 100 7",
"output": "NO"
},
{
... | 15 | 0 | 0 | 9,446 | |
432 | Football Kit | [
"brute force",
"greedy",
"implementation"
] | null | null | Consider a football tournament where *n* teams participate. Each team has two football kits: for home games, and for away games. The kit for home games of the *i*-th team has color *x**i* and the kit for away games of this team has color *y**i* (*x**i*<=β <=*y**i*).
In the tournament, each team plays exactly one home game and exactly one away game with each other team (*n*(*n*<=-<=1) games in total). The team, that plays the home game, traditionally plays in its home kit. The team that plays an away game plays in its away kit. However, if two teams has the kits of the same color, they cannot be distinguished. In this case the away team plays in its home kit.
Calculate how many games in the described tournament each team plays in its home kit and how many games it plays in its away kit. | The first line contains a single integer *n* (2<=β€<=*n*<=β€<=105) β the number of teams. Next *n* lines contain the description of the teams. The *i*-th line contains two space-separated numbers *x**i*, *y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=105;Β *x**i*<=β <=*y**i*) β the color numbers for the home and away kits of the *i*-th team. | For each team, print on a single line two space-separated integers β the number of games this team is going to play in home and away kits, correspondingly. Print the answers for the teams in the order they appeared in the input. | [
"2\n1 2\n2 1\n",
"3\n1 2\n2 1\n1 3\n"
] | [
"2 0\n2 0\n",
"3 1\n4 0\n2 2\n"
] | none | [
{
"input": "2\n1 2\n2 1",
"output": "2 0\n2 0"
},
{
"input": "3\n1 2\n2 1\n1 3",
"output": "3 1\n4 0\n2 2"
},
{
"input": "2\n1 2\n1 2",
"output": "1 1\n1 1"
},
{
"input": "2\n1 2\n3 4",
"output": "1 1\n1 1"
},
{
"input": "3\n1 100000\n1 100000\n100000 2",
"out... | 545 | 2,764,800 | 3 | 9,477 | |
244 | Dividing Orange | [
"implementation"
] | null | null | One day Ms Swan bought an orange in a shop. The orange consisted of *n*Β·*k* segments, numbered with integers from 1 to *n*Β·*k*.
There were *k* children waiting for Ms Swan at home. The children have recently learned about the orange and they decided to divide it between them. For that each child took a piece of paper and wrote the number of the segment that he would like to get: the *i*-th (1<=β€<=*i*<=β€<=*k*) child wrote the number *a**i* (1<=β€<=*a**i*<=β€<=*n*Β·*k*). All numbers *a**i* accidentally turned out to be different.
Now the children wonder, how to divide the orange so as to meet these conditions:
- each child gets exactly *n* orange segments; - the *i*-th child gets the segment with number *a**i* for sure; - no segment goes to two children simultaneously.
Help the children, divide the orange and fulfill the requirements, described above. | The first line contains two integers *n*, *k* (1<=β€<=*n*,<=*k*<=β€<=30). The second line contains *k* space-separated integers *a*1,<=*a*2,<=...,<=*a**k* (1<=β€<=*a**i*<=β€<=*n*Β·*k*), where *a**i* is the number of the orange segment that the *i*-th child would like to get.
It is guaranteed that all numbers *a**i* are distinct. | Print exactly *n*Β·*k* distinct integers. The first *n* integers represent the indexes of the segments the first child will get, the second *n* integers represent the indexes of the segments the second child will get, and so on. Separate the printed numbers with whitespaces.
You can print a child's segment indexes in any order. It is guaranteed that the answer always exists. If there are multiple correct answers, print any of them. | [
"2 2\n4 1\n",
"3 1\n2\n"
] | [
"2 4 \n1 3 \n",
"3 2 1 \n"
] | none | [
{
"input": "2 2\n4 1",
"output": "2 4 \n1 3 "
},
{
"input": "3 1\n2",
"output": "3 2 1 "
},
{
"input": "5 5\n25 24 23 22 21",
"output": "2 3 1 25 4 \n7 6 8 5 24 \n10 12 9 23 11 \n13 15 14 16 22 \n19 21 20 17 18 "
},
{
"input": "1 30\n8 22 13 25 10 30 12 27 6 4 7 2 20 16 26 14... | 124 | 6,963,200 | 3 | 9,484 | |
710 | Two Arithmetic Progressions | [
"math",
"number theory"
] | null | null | You are given two arithmetic progressions: *a*1*k*<=+<=*b*1 and *a*2*l*<=+<=*b*2. Find the number of integers *x* such that *L*<=β€<=*x*<=β€<=*R* and *x*<==<=*a*1*k*'<=+<=*b*1<==<=*a*2*l*'<=+<=*b*2, for some integers *k*',<=*l*'<=β₯<=0. | The only line contains six integers *a*1,<=*b*1,<=*a*2,<=*b*2,<=*L*,<=*R* (0<=<<=*a*1,<=*a*2<=β€<=2Β·109,<=<=-<=2Β·109<=β€<=*b*1,<=*b*2,<=*L*,<=*R*<=β€<=2Β·109,<=*L*<=β€<=*R*). | Print the desired number of integers *x*. | [
"2 0 3 3 5 21\n",
"2 4 3 0 6 17\n"
] | [
"3\n",
"2\n"
] | none | [
{
"input": "2 0 3 3 5 21",
"output": "3"
},
{
"input": "2 4 3 0 6 17",
"output": "2"
},
{
"input": "2 0 4 2 -39 -37",
"output": "0"
},
{
"input": "1 9 3 11 49 109",
"output": "20"
},
{
"input": "3 81 5 72 -1761 501",
"output": "28"
},
{
"input": "8 -89... | 0 | 0 | -1 | 9,539 | |
431 | Shower Line | [
"brute force",
"implementation"
] | null | null | Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks.
There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower.
Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the (2*i*<=-<=1)-th man in the line (for the current moment) talks with the (2*i*)-th one.
Let's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower.
We know that if students *i* and *j* talk, then the *i*-th student's happiness increases by *g**ij* and the *j*-th student's happiness increases by *g**ji*. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower. | The input consists of five lines, each line contains five space-separated integers: the *j*-th number in the *i*-th line shows *g**ij* (0<=β€<=*g**ij*<=β€<=105). It is guaranteed that *g**ii*<==<=0 for all *i*.
Assume that the students are numbered from 1 to 5. | Print a single integer β the maximum possible total happiness of the students. | [
"0 0 0 0 9\n0 0 0 0 0\n0 0 0 0 0\n0 0 0 0 0\n7 0 0 0 0\n",
"0 43 21 18 2\n3 0 21 11 65\n5 2 0 1 4\n54 62 12 0 99\n87 64 81 33 0\n"
] | [
"32\n",
"620\n"
] | In the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals: | [
{
"input": "0 0 0 0 9\n0 0 0 0 0\n0 0 0 0 0\n0 0 0 0 0\n7 0 0 0 0",
"output": "32"
},
{
"input": "0 43 21 18 2\n3 0 21 11 65\n5 2 0 1 4\n54 62 12 0 99\n87 64 81 33 0",
"output": "620"
},
{
"input": "0 4 2 4 9\n6 0 2 5 0\n2 5 0 6 3\n6 3 3 0 10\n0 3 1 3 0",
"output": "63"
},
{
... | 62 | 0 | 3 | 9,548 | |
29 | Ant on the Tree | [
"constructive algorithms",
"dfs and similar",
"trees"
] | D. Ant on the Tree | 2 | 256 | Connected undirected graph without cycles is called a tree. Trees is a class of graphs which is interesting not only for people, but for ants too.
An ant stands at the root of some tree. He sees that there are *n* vertexes in the tree, and they are connected by *n*<=-<=1 edges so that there is a path between any pair of vertexes. A leaf is a distinct from root vertex, which is connected with exactly one other vertex.
The ant wants to visit every vertex in the tree and return to the root, passing every edge twice. In addition, he wants to visit the leaves in a specific order. You are to find some possible route of the ant. | The first line contains integer *n* (3<=β€<=*n*<=β€<=300) β amount of vertexes in the tree. Next *n*<=-<=1 lines describe edges. Each edge is described with two integers β indexes of vertexes which it connects. Each edge can be passed in any direction. Vertexes are numbered starting from 1. The root of the tree has number 1. The last line contains *k* integers, where *k* is amount of leaves in the tree. These numbers describe the order in which the leaves should be visited. It is guaranteed that each leaf appears in this order exactly once. | If the required route doesn't exist, output -1. Otherwise, output 2*n*<=-<=1 numbers, describing the route. Every time the ant comes to a vertex, output it's index. | [
"3\n1 2\n2 3\n3\n",
"6\n1 2\n1 3\n2 4\n4 5\n4 6\n5 6 3\n",
"6\n1 2\n1 3\n2 4\n4 5\n4 6\n5 3 6\n"
] | [
"1 2 3 2 1 ",
"1 2 4 5 4 6 4 2 1 3 1 ",
"-1\n"
] | none | [
{
"input": "3\n1 2\n2 3\n3",
"output": "1 2 3 2 1 "
},
{
"input": "6\n1 2\n1 3\n2 4\n4 5\n4 6\n5 6 3",
"output": "1 2 4 5 4 6 4 2 1 3 1 "
},
{
"input": "6\n1 2\n1 3\n2 4\n4 5\n4 6\n5 3 6",
"output": "-1"
},
{
"input": "10\n8 10\n2 1\n7 5\n5 4\n6 10\n2 3\n3 10\n2 9\n7 2\n6 9 4... | 248 | 512,000 | 0 | 9,566 |
56 | Changing a String | [
"dp"
] | D. Changing a String | 2 | 256 | There is a string *s*, consisting of capital Latin letters. Let's denote its current length as |*s*|. During one move it is allowed to apply one of the following operations to it:
- INSERT *pos* *ch* β insert a letter *ch* in the string *s* in the position *pos* (1<=β€<=*pos*<=β€<=|*s*|<=+<=1,<=*A*<=β€<=*ch*<=β€<=*Z*). The letter *ch* becomes the *pos*-th symbol of the string *s*, at that the letters shift aside and the length of the string increases by 1. - DELETE *pos* β delete a character number *pos* (1<=β€<=*pos*<=β€<=|*s*|) from the string *s*. At that the letters shift together and the length of the string decreases by 1. - REPLACE *pos* *ch* β the letter in the position *pos* of the line *s* is replaced by *ch* (1<=β€<=*pos*<=β€<=|*s*|,<=*A*<=β€<=*ch*<=β€<=*Z*). At that the length of the string does not change.
Your task is to find in which minimal number of moves one can get a *t* string from an *s* string. You should also find the sequence of actions leading to the required results. | The first line contains *s*, the second line contains *t*. The lines consist only of capital Latin letters, their lengths are positive numbers from 1 to 1000. | In the first line print the number of moves *k* in the given sequence of operations. The number should be the minimal possible one. Then print *k* lines containing one operation each. Print the operations in the format, described above. If there are several solutions, print any of them. | [
"ABA\nABBBA\n",
"ACCEPTED\nWRONGANSWER\n"
] | [
"2\nINSERT 3 B\nINSERT 4 B\n",
"10\nREPLACE 1 W\nREPLACE 2 R\nREPLACE 3 O\nREPLACE 4 N\nREPLACE 5 G\nREPLACE 6 A\nINSERT 7 N\nINSERT 8 S\nINSERT 9 W\nREPLACE 11 R\n"
] | none | [
{
"input": "ABA\nABBBA",
"output": "2\nINSERT 3 B\nINSERT 4 B"
},
{
"input": "ACCEPTED\nWRONGANSWER",
"output": "10\nREPLACE 1 W\nREPLACE 2 R\nREPLACE 3 O\nREPLACE 4 N\nREPLACE 5 G\nREPLACE 6 A\nINSERT 7 N\nINSERT 8 S\nINSERT 9 W\nREPLACE 11 R"
},
{
"input": "V\nBNBNE",
"output": "5\... | 2,000 | 1,228,800 | 0 | 9,574 |
272 | Dima and Staircase | [
"data structures",
"implementation"
] | null | null | Dima's got a staircase that consists of *n* stairs. The first stair is at height *a*1, the second one is at *a*2, the last one is at *a**n* (1<=β€<=*a*1<=β€<=*a*2<=β€<=...<=β€<=*a**n*).
Dima decided to play with the staircase, so he is throwing rectangular boxes at the staircase from above. The *i*-th box has width *w**i* and height *h**i*. Dima throws each box vertically down on the first *w**i* stairs of the staircase, that is, the box covers stairs with numbers 1,<=2,<=...,<=*w**i*. Each thrown box flies vertically down until at least one of the two following events happen:
- the bottom of the box touches the top of a stair; - the bottom of the box touches the top of a box, thrown earlier.
We only consider touching of the horizontal sides of stairs and boxes, at that touching with the corners isn't taken into consideration. Specifically, that implies that a box with width *w**i* cannot touch the stair number *w**i*<=+<=1.
You are given the description of the staircase and the sequence in which Dima threw the boxes at it. For each box, determine how high the bottom of the box after landing will be. Consider a box to fall after the previous one lands. | The first line contains integer *n* (1<=β€<=*n*<=β€<=105) β the number of stairs in the staircase. The second line contains a non-decreasing sequence, consisting of *n* integers, *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109;Β *a**i*<=β€<=*a**i*<=+<=1).
The next line contains integer *m* (1<=β€<=*m*<=β€<=105) β the number of boxes. Each of the following *m* lines contains a pair of integers *w**i*,<=*h**i* (1<=β€<=*w**i*<=β€<=*n*;Β 1<=β€<=*h**i*<=β€<=109) β the size of the *i*-th thrown box.
The numbers in the lines are separated by spaces. | Print *m* integers β for each box the height, where the bottom of the box will be after landing. Print the answers for the boxes in the order, in which the boxes are given in the input.
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. | [
"5\n1 2 3 6 6\n4\n1 1\n3 1\n1 1\n4 3\n",
"3\n1 2 3\n2\n1 1\n3 1\n",
"1\n1\n5\n1 2\n1 10\n1 10\n1 10\n1 10\n"
] | [
"1\n3\n4\n6\n",
"1\n3\n",
"1\n3\n13\n23\n33\n"
] | The first sample are shown on the picture. | [
{
"input": "5\n1 2 3 6 6\n4\n1 1\n3 1\n1 1\n4 3",
"output": "1\n3\n4\n6"
},
{
"input": "3\n1 2 3\n2\n1 1\n3 1",
"output": "1\n3"
},
{
"input": "1\n1\n5\n1 2\n1 10\n1 10\n1 10\n1 10",
"output": "1\n3\n13\n23\n33"
},
{
"input": "8\n6 10 18 23 30 31 31 33\n1\n5 3",
"output":... | 872 | 15,155,200 | 3 | 9,605 | |
889 | Mod Mod Mod | [
"binary search",
"dp",
"math"
] | null | null | You are given a sequence of integers *a*1,<=*a*2,<=...,<=*a**n*. Let , and for 1<=β€<=*i*<=<<=*n*. Here, denotes the modulus operation. Find the maximum value of *f*(*x*,<=1) over all nonnegative integers *x*. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=200000)Β β the length of the sequence.
The second lines contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=1013)Β β the elements of the sequence. | Output a single integerΒ β the maximum value of *f*(*x*,<=1) over all nonnegative integers *x*. | [
"2\n10 5\n",
"5\n5 4 3 2 1\n",
"4\n5 10 5 10\n"
] | [
"13\n",
"6\n",
"16\n"
] | In the first example you can choose, for example, *x*β=β19.
In the second example you can choose, for example, *x*β=β3 or *x*β=β2. | [] | 1,247 | 0 | 0 | 9,613 | |
932 | Escape Through Leaf | [
"data structures",
"dp",
"geometry"
] | null | null | You are given a tree with *n* nodes (numbered from 1 to *n*) rooted at node 1. Also, each node has two values associated with it. The values for *i*-th node are *a**i* and *b**i*.
You can jump from a node to any node in its subtree. The cost of one jump from node *x* to node *y* is the product of *a**x* and *b**y*. The total cost of a path formed by one or more jumps is sum of costs of individual jumps. For every node, calculate the minimum total cost to reach any leaf from that node. Pay attention, that root can never be leaf, even if it has degree 1.
Note that you cannot jump from a node to itself. | The first line of input contains an integer *n* (2<=β€<=*n*<=β€<=105)Β β the number of nodes in the tree.
The second line contains *n* space-separated integers *a*1,<=<=*a*2,<=<=...,<=<=*a**n*(<=-<=105<=<=β€<=<=*a**i*<=<=β€<=<=105).
The third line contains *n* space-separated integers *b*1,<=<=*b*2,<=<=...,<=<=*b**n*(<=-<=105<=<=β€<=<=*b**i*<=<=β€<=<=105).
Next *n*<=<=-<=<=1 lines contains two space-separated integers *u**i* and *v**i* (1<=β€<=*u**i*,<=<=*v**i*<=β€<=<=*n*) describing edge between nodes *u**i* and *v**i* in the tree. | Output *n* space-separated integers, *i*-th of which denotes the minimum cost of a path from node *i* to reach any leaf. | [
"3\n2 10 -1\n7 -7 5\n2 3\n2 1\n",
"4\n5 -10 5 7\n-8 -80 -3 -10\n2 1\n2 4\n1 3\n"
] | [
"10 50 0 ",
"-300 100 0 0 "
] | In the first example, node 3 is already a leaf, so the cost is 0. For node 2, jump to node 3 with cost *a*<sub class="lower-index">2</sub>βΓβ*b*<sub class="lower-index">3</sub>β=β50. For node 1, jump directly to node 3 with cost *a*<sub class="lower-index">1</sub>βΓβ*b*<sub class="lower-index">3</sub>β=β10.
In the second example, node 3 and node 4 are leaves, so the cost is 0. For node 2, jump to node 4 with cost *a*<sub class="lower-index">2</sub>βΓβ*b*<sub class="lower-index">4</sub>β=β100. For node 1, jump to node 2 with cost *a*<sub class="lower-index">1</sub>βΓβ*b*<sub class="lower-index">2</sub>β=ββ-β400 followed by a jump from 2 to 4 with cost *a*<sub class="lower-index">2</sub>βΓβ*b*<sub class="lower-index">4</sub>β=β100. | [
{
"input": "3\n2 10 -1\n7 -7 5\n2 3\n2 1",
"output": "10 50 0 "
},
{
"input": "4\n5 -10 5 7\n-8 -80 -3 -10\n2 1\n2 4\n1 3",
"output": "-300 100 0 0 "
},
{
"input": "5\n7 -8 -8 -3 -10\n6 1 -6 7 5\n3 1\n2 5\n1 4\n4 5",
"output": "-42 0 0 -25 -10 "
},
{
"input": "6\n4 -8 9 -1 -2... | 0 | 0 | -1 | 9,615 | |
45 | Event Dates | [
"greedy",
"meet-in-the-middle",
"sortings"
] | D. Event Dates | 2 | 256 | On a history lesson the teacher asked Vasya to name the dates when *n* famous events took place. He doesn't remembers the exact dates but he remembers a segment of days [*l**i*,<=*r**i*] (inclusive) on which the event could have taken place. However Vasya also remembers that there was at most one event in one day. Help him choose such *n* dates of famous events that will fulfill both conditions. It is guaranteed that it is possible. | The first line contains one integer *n* (1<=β€<=*n*<=β€<=100) β the number of known events. Then follow *n* lines containing two integers *l**i* and *r**i* each (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=107) β the earliest acceptable date and the latest acceptable date of the *i*-th event. | Print *n* numbers β the dates on which the events took place. If there are several solutions, print any of them. It is guaranteed that a solution exists. | [
"3\n1 2\n2 3\n3 4\n",
"2\n1 3\n1 3\n"
] | [
"1 2 3 \n",
"1 2 \n"
] | none | [
{
"input": "3\n1 2\n2 3\n3 4",
"output": "1 2 3 "
},
{
"input": "2\n1 3\n1 3",
"output": "1 2 "
},
{
"input": "10\n1 10\n3 4\n8 10\n3 4\n5 9\n1 4\n7 10\n1 9\n1 8\n4 10",
"output": "7 3 10 4 6 1 9 5 2 8 "
},
{
"input": "10\n1 1\n8 10\n1 7\n6 8\n5 7\n1 9\n8 8\n6 10\n1 4\n3 4",
... | 186 | 20,172,800 | 0 | 9,625 |
712 | Memory and Casinos | [
"data structures",
"math",
"probabilities"
] | null | null | There are *n* casinos lined in a row. If Memory plays at casino *i*, he has probability *p**i* to win and move to the casino on the right (*i*<=+<=1) or exit the row (if *i*<==<=*n*), and a probability 1<=-<=*p**i* to lose and move to the casino on the left (*i*<=-<=1) or also exit the row (if *i*<==<=1).
We say that Memory dominates on the interval *i*... *j* if he completes a walk such that,
- He starts on casino *i*. - He never looses in casino *i*. - He finishes his walk by winning in casino *j*.
Note that Memory can still walk left of the 1-st casino and right of the casino *n* and that always finishes the process.
Now Memory has some requests, in one of the following forms:
- 1 *i* *a* *b*: Set . - 2 *l* *r*: Print the probability that Memory will dominate on the interval *l*... *r*, i.e. compute the probability that Memory will first leave the segment *l*... *r* after winning at casino *r*, if she starts in casino *l*.
It is guaranteed that at any moment of time *p* is a non-decreasing sequence, i.e. *p**i*<=β€<=*p**i*<=+<=1 for all *i* from 1 to *n*<=-<=1.
Please help Memory by answering all his requests! | The first line of the input contains two integers *n* and *q*(1<=β€<=*n*,<=*q*<=β€<=100<=000), Β β number of casinos and number of requests respectively.
The next *n* lines each contain integers *a**i* and *b**i* (1<=β€<=*a**i*<=<<=*b**i*<=β€<=109) Β β is the probability *p**i* of winning in casino *i*.
The next *q* lines each contain queries of one of the types specified above (1<=β€<=*a*<=<<=*b*<=β€<=109, 1<=β€<=*i*<=β€<=*n*, 1<=β€<=*l*<=β€<=*r*<=β€<=*n*).
It's guaranteed that there will be at least one query of type 2, i.e. the output will be non-empty. Additionally, it is guaranteed that *p* forms a non-decreasing sequence at all times. | Print a real number for every request of type 2Β β the probability that boy will "dominate" on that interval. Your answer will be considered correct if its absolute error does not exceed 10<=-<=4.
Namely: let's assume that one of your answers is *a*, and the corresponding answer of the jury is *b*. The checker program will consider your answer correct if |*a*<=-<=*b*|<=β€<=10<=-<=4. | [
"3 13\n1 3\n1 2\n2 3\n2 1 1\n2 1 2\n2 1 3\n2 2 2\n2 2 3\n2 3 3\n1 2 2 3\n2 1 1\n2 1 2\n2 1 3\n2 2 2\n2 2 3\n2 3 3\n"
] | [
"0.3333333333\n0.2000000000\n0.1666666667\n0.5000000000\n0.4000000000\n0.6666666667\n0.3333333333\n0.2500000000\n0.2222222222\n0.6666666667\n0.5714285714\n0.6666666667\n"
] | none | [] | 46 | 0 | 0 | 9,628 | |
763 | Timofey and a tree | [
"dfs and similar",
"dp",
"dsu",
"graphs",
"implementation",
"trees"
] | null | null | Each New Year Timofey and his friends cut down a tree of *n* vertices and bring it home. After that they paint all the *n* its vertices, so that the *i*-th vertex gets color *c**i*.
Now it's time for Timofey birthday, and his mother asked him to remove the tree. Timofey removes the tree in the following way: he takes some vertex in hands, while all the other vertices move down so that the tree becomes rooted at the chosen vertex. After that Timofey brings the tree to a trash can.
Timofey doesn't like it when many colors are mixing together. A subtree annoys him if there are vertices of different color in it. Timofey wants to find a vertex which he should take in hands so that there are no subtrees that annoy him. He doesn't consider the whole tree as a subtree since he can't see the color of the root vertex.
A subtree of some vertex is a subgraph containing that vertex and all its descendants.
Your task is to determine if there is a vertex, taking which in hands Timofey wouldn't be annoyed. | The first line contains single integer *n* (2<=β€<=*n*<=β€<=105)Β β the number of vertices in the tree.
Each of the next *n*<=-<=1 lines contains two integers *u* and *v* (1<=β€<=*u*,<=*v*<=β€<=*n*, *u*<=β <=*v*), denoting there is an edge between vertices *u* and *v*. It is guaranteed that the given graph is a tree.
The next line contains *n* integers *c*1,<=*c*2,<=...,<=*c**n* (1<=β€<=*c**i*<=β€<=105), denoting the colors of the vertices. | Print "NO" in a single line, if Timofey can't take the tree in such a way that it doesn't annoy him.
Otherwise print "YES" in the first line. In the second line print the index of the vertex which Timofey should take in hands. If there are multiple answers, print any of them. | [
"4\n1 2\n2 3\n3 4\n1 2 1 1\n",
"3\n1 2\n2 3\n1 2 3\n",
"4\n1 2\n2 3\n3 4\n1 2 1 2\n"
] | [
"YES\n2",
"YES\n2",
"NO"
] | none | [
{
"input": "4\n1 2\n2 3\n3 4\n1 2 1 1",
"output": "YES\n2"
},
{
"input": "3\n1 2\n2 3\n1 2 3",
"output": "YES\n2"
},
{
"input": "4\n1 2\n2 3\n3 4\n1 2 1 2",
"output": "NO"
},
{
"input": "3\n2 1\n2 3\n1 2 3",
"output": "YES\n2"
},
{
"input": "4\n1 2\n2 4\n4 3\n1 1 ... | 951 | 14,950,400 | 3 | 9,646 | |
252 | Unsorting Array | [
"brute force",
"sortings"
] | null | null | Little Petya likes arrays of integers a lot. Recently his mother has presented him one such array consisting of *n* elements. Petya is now wondering whether he can swap any two distinct integers in the array so that the array got unsorted. Please note that Petya can not swap equal integers even if they are in distinct positions in the array. Also note that Petya must swap some two integers even if the original array meets all requirements.
Array *a* (the array elements are indexed from 1) consisting of *n* elements is called sorted if it meets at least one of the following two conditions:
1. *a*1<=β€<=*a*2<=β€<=...<=β€<=*a**n*; 1. *a*1<=β₯<=*a*2<=β₯<=...<=β₯<=*a**n*.
Help Petya find the two required positions to swap or else say that they do not exist. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=105). The second line contains *n* non-negative space-separated integers *a*1,<=*a*2,<=...,<=*a**n* β the elements of the array that Petya's mother presented him. All integers in the input do not exceed 109. | If there is a pair of positions that make the array unsorted if swapped, then print the numbers of these positions separated by a space. If there are several pairs of positions, print any of them. If such pair does not exist, print -1. The positions in the array are numbered with integers from 1 to *n*. | [
"1\n1\n",
"2\n1 2\n",
"4\n1 2 3 4\n",
"3\n1 1 1\n"
] | [
"-1\n",
"-1\n",
"1 2\n",
"-1\n"
] | In the first two samples the required pairs obviously don't exist.
In the third sample you can swap the first two elements. After that the array will look like this: 2 1 3 4. This array is unsorted. | [
{
"input": "1\n1",
"output": "-1"
},
{
"input": "2\n1 2",
"output": "-1"
},
{
"input": "4\n1 2 3 4",
"output": "1 2"
},
{
"input": "3\n1 1 1",
"output": "-1"
},
{
"input": "3\n1 2 2",
"output": "1 2"
},
{
"input": "5\n1 1 1 1 2",
"output": "2 5"
... | 186 | 12,902,400 | 3 | 9,654 | |
807 | T-Shirt Hunt | [
"brute force",
"implementation"
] | null | null | Not so long ago the Codecraft-17 contest was held on Codeforces. The top 25 participants, and additionally random 25 participants out of those who got into top 500, will receive a Codeforces T-shirt.
Unfortunately, you didn't manage to get into top 25, but you got into top 500, taking place *p*.
Now the elimination round of 8VC Venture Cup 2017 is being held. It has been announced that the Codecraft-17 T-shirt winners will be chosen as follows. Let *s* be the number of points of the winner of the elimination round of 8VC Venture Cup 2017. Then the following pseudocode will be executed:
Here "div" is the integer division operator, "mod" is the modulo (the remainder of division) operator.
As the result of pseudocode execution, 25 integers between 26 and 500, inclusive, will be printed. These will be the numbers of places of the participants who get the Codecraft-17 T-shirts. It is guaranteed that the 25 printed integers will be pairwise distinct for any value of *s*.
You're in the lead of the elimination round of 8VC Venture Cup 2017, having *x* points. You believe that having at least *y* points in the current round will be enough for victory.
To change your final score, you can make any number of successful and unsuccessful hacks. A successful hack brings you 100 points, an unsuccessful one takes 50 points from you. It's difficult to do successful hacks, though.
You want to win the current round and, at the same time, ensure getting a Codecraft-17 T-shirt. What is the smallest number of successful hacks you have to do to achieve that? | The only line contains three integers *p*, *x* and *y* (26<=β€<=*p*<=β€<=500; 1<=β€<=*y*<=β€<=*x*<=β€<=20000)Β β your place in Codecraft-17, your current score in the elimination round of 8VC Venture Cup 2017, and the smallest number of points you consider sufficient for winning the current round. | Output a single integerΒ β the smallest number of successful hacks you have to do in order to both win the elimination round of 8VC Venture Cup 2017 and ensure getting a Codecraft-17 T-shirt.
It's guaranteed that your goal is achievable for any valid input data. | [
"239 10880 9889\n",
"26 7258 6123\n",
"493 8000 8000\n",
"101 6800 6500\n",
"329 19913 19900\n"
] | [
"0\n",
"2\n",
"24\n",
"0\n",
"8\n"
] | In the first example, there is no need to do any hacks since 10880 points already bring the T-shirt to the 239-th place of Codecraft-17 (that is, you). In this case, according to the pseudocode, the T-shirts will be given to the participants at the following places:
In the second example, you have to do two successful and one unsuccessful hack to make your score equal to 7408.
In the third example, you need to do as many as 24 successful hacks to make your score equal to 10400.
In the fourth example, it's sufficient to do 6 unsuccessful hacks (and no successful ones) to make your score equal to 6500, which is just enough for winning the current round and also getting the T-shirt. | [
{
"input": "239 10880 9889",
"output": "0"
},
{
"input": "26 7258 6123",
"output": "2"
},
{
"input": "493 8000 8000",
"output": "24"
},
{
"input": "101 6800 6500",
"output": "0"
},
{
"input": "329 19913 19900",
"output": "8"
},
{
"input": "264 19252 10... | 30 | 0 | -1 | 9,668 | |
7 | Defining Macros | [
"dp",
"expression parsing",
"implementation"
] | E. Defining Macros | 3 | 256 | Most C/C++ programmers know about excellent opportunities that preprocessor #define directives give; but many know as well about the problems that can arise because of their careless use.
In this problem we consider the following model of #define constructions (also called macros). Each macro has its name and value. The generic syntax for declaring a macro is the following:
#define macro_name macro_value
After the macro has been declared, "macro_name" is replaced with "macro_value" each time it is met in the program (only the whole tokens can be replaced; i.e. "macro_name" is replaced only when it is surrounded by spaces or other non-alphabetic symbol). A "macro_value" within our model can only be an arithmetic expression consisting of variables, four arithmetic operations, brackets, and also the names of previously declared macros (in this case replacement is performed sequentially). The process of replacing macros with their values is called substitution.
One of the main problems arising while using macros β the situation when as a result of substitution we get an arithmetic expression with the changed order of calculation because of different priorities of the operations.
Let's consider the following example. Say, we declared such a #define construction:
#define sum x + y
and further in the program the expression "2 * sum" is calculated. After macro substitution is performed we get "2 * x + y", instead of intuitively expected "2 * (x + y)".
Let's call the situation "suspicious", if after the macro substitution the order of calculation changes, falling outside the bounds of some macro. Thus, your task is to find out by the given set of #define definitions and the given expression if this expression is suspicious or not.
Let's speak more formally. We should perform an ordinary macros substitution in the given expression. Moreover, we should perform a "safe" macros substitution in the expression, putting in brackets each macro value; after this, guided by arithmetic rules of brackets expansion, we can omit some of the brackets. If there exist a way to get an expression, absolutely coinciding with the expression that is the result of an ordinary substitution (character-by-character, but ignoring spaces), then this expression and the macros system are called correct, otherwise β suspicious.
Note that we consider the "/" operation as the usual mathematical division, not the integer division like in C/C++. That's why, for example, in the expression "a*(b/c)" we can omit brackets to get the expression "a*b/c". | The first line contains the only number *n* (0<=β€<=*n*<=β€<=100) β the amount of #define constructions in the given program.
Then there follow *n* lines, each of them contains just one #define construction. Each construction has the following syntax:
#define name expression
where
- name β the macro name, - expression β the expression with which the given macro will be replaced. An expression is a non-empty string, containing digits,names of variables, names of previously declared macros, round brackets and operational signs +-*/. It is guaranteed that the expression (before and after macros substitution) is a correct arithmetic expression, having no unary operations. The expression contains only non-negative integers, not exceeding 109.
All the names (#define constructions' names and names of their arguments) are strings of case-sensitive Latin characters. It is guaranteed that the name of any variable is different from any #define construction.
Then, the last line contains an expression that you are to check. This expression is non-empty and satisfies the same limitations as the expressions in #define constructions.
The input lines may contain any number of spaces anywhere, providing these spaces do not break the word "define" or the names of constructions and variables. In particular, there can be any number of spaces before and after the "#" symbol.
The length of any line from the input file does not exceed 100 characters. | Output "OK", if the expression is correct according to the above given criterion, otherwise output "Suspicious". | [
"1\n#define sum x + y\n1 * sum\n",
"1\n#define sum (x + y)\nsum - sum\n",
"4\n#define sum x + y\n#define mul a * b\n#define div a / b\n#define expr sum + mul * div * mul\nexpr\n",
"3\n#define SumSafe (a+b)\n#define DivUnsafe a/b\n#define DenominatorUnsafe a*b\n((SumSafe) + DivUnsafe/DivUnsafe + x/Deno... | [
"Suspicious\n",
"OK\n",
"OK\n",
"Suspicious\n"
] | none | [
{
"input": "1\n#define sum x + y\n1 * sum",
"output": "Suspicious"
},
{
"input": "1\n#define sum (x + y)\nsum - sum",
"output": "OK"
},
{
"input": "4\n#define sum x + y\n#define mul a * b\n#define div a / b\n#define expr sum + mul * div * mul\nexpr",
"output": "OK"
},
{
"... | 280 | 1,228,800 | 0 | 9,673 |
0 | none | [
"none"
] | null | null | Lesha plays the recently published new version of the legendary game hacknet. In this version character skill mechanism was introduced. Now, each player character has exactly *n* skills. Each skill is represented by a non-negative integer *a**i*Β β the current skill level. All skills have the same maximum level *A*.
Along with the skills, global ranking of all players was added. Players are ranked according to the so-called Force. The Force of a player is the sum of the following values:
- The number of skills that a character has perfected (i.e., such that *a**i*<==<=*A*), multiplied by coefficient *c**f*.- The minimum skill level among all skills (*min* *a**i*), multiplied by coefficient *c**m*.
Now Lesha has *m* hacknetian currency units, which he is willing to spend. Each currency unit can increase the current level of any skill by 1 (if it's not equal to *A* yet). Help him spend his money in order to achieve the maximum possible value of the Force. | The first line of the input contains five space-separated integers *n*, *A*, *c**f*, *c**m* and *m* (1<=β€<=*n*<=β€<=100<=000, 1<=β€<=*A*<=β€<=109, 0<=β€<=*c**f*,<=*c**m*<=β€<=1000, 0<=β€<=*m*<=β€<=1015).
The second line contains exactly *n* integers *a**i* (0<=β€<=*a**i*<=β€<=*A*), separated by spaces,Β β the current levels of skills. | On the first line print the maximum value of the Force that the character can achieve using no more than *m* currency units.
On the second line print *n* integers *a*'*i* (*a**i*<=β€<=*a*'*i*<=β€<=*A*), skill levels which one must achieve in order to reach the specified value of the Force, while using no more than *m* currency units. Numbers should be separated by spaces. | [
"3 5 10 1 5\n1 3 1\n",
"3 5 10 1 339\n1 3 1\n"
] | [
"12\n2 5 2 \n",
"35\n5 5 5 \n"
] | In the first test the optimal strategy is to increase the second skill to its maximum, and increase the two others by 1.
In the second test one should increase all skills to maximum. | [
{
"input": "3 5 10 1 5\n1 3 1",
"output": "12\n2 5 2 "
},
{
"input": "3 5 10 1 339\n1 3 1",
"output": "35\n5 5 5 "
},
{
"input": "2 6 0 1 4\n5 1",
"output": "5\n5 5 "
},
{
"input": "1 1000000000 1000 1000 1000000000000000\n0",
"output": "1000000001000\n1000000000 "
},
... | 93 | 23,142,400 | 0 | 9,684 | |
81 | Average Score | [
"greedy",
"math",
"sortings"
] | C. Average Score | 2 | 256 | After the educational reform Polycarp studies only two subjects at school, Safety Studies and PE (Physical Education). During the long months of the fourth term, he received *n* marks in them. When teachers wrote a mark in the journal, they didn't write in what subject the mark was for, they just wrote the mark.
Now it's time to show the journal to his strict parents. Polycarp knows that recently at the Parent Meeting the parents were told that he received *a* Safety Studies marks and *b* PE marks (*a*<=+<=*b*<==<=*n*). Now Polycarp wants to write a subject's name in front of each mark so that:
- there are exactly *a* Safety Studies marks, - there are exactly *b* PE marks, - the total average score in both subjects is maximum.
An average subject grade is the sum of all marks in it, divided by the number of them. Of course, the division is performed in real numbers without rounding up or down. Polycarp aims to maximize the *x*1<=+<=*x*2, where *x*1 is the average score in the first subject (Safety Studies), and *x*2 is the average score in the second one (Physical Education). | The first line contains an integer *n* (2<=β€<=*n*<=β€<=105), *n* is the number of marks in Polycarp's Journal. The second line contains two positive integers *a*,<=*b* (1<=β€<=*a*,<=*b*<=β€<=*n*<=-<=1,<=*a*<=+<=*b*<==<=*n*). The third line contains a sequence of integers *t*1,<=*t*2,<=...,<=*t**n* (1<=β€<=*t**i*<=β€<=5), they are Polycarp's marks. | Print the sequence of integers *f*1,<=*f*2,<=...,<=*f**n*, where *f**i* (1<=β€<=*f**i*<=β€<=2) is the number of a subject to which the *i*-th mark should be attributed. If there are several possible solutions, then print such that the sequence *f*1,<=*f*2,<=...,<=*f**n* is the smallest lexicographically.
The sequence *p*1,<=*p*2,<=...,<=*p**n* is lexicographically less than *q*1,<=*q*2,<=...,<=*q**n* if there exists such *j* (1<=β€<=*j*<=β€<=*n*) that *p**i*<==<=*q**i* for all 1<=β€<=*i*<=<<=*j*, Π°nd *p**j*<=<<=*q**j*. | [
"5\n3 2\n4 4 5 4 4\n",
"4\n2 2\n3 5 4 5\n",
"6\n1 5\n4 4 4 5 4 4\n"
] | [
"1 1 2 1 2 ",
"1 1 2 2 ",
"2 2 2 1 2 2 "
] | In the first sample the average score in the first subject is equal to 4, and in the second one β to 4.5. The total average score is 8.5. | [
{
"input": "5\n3 2\n4 4 5 4 4",
"output": "1 1 2 1 2 "
},
{
"input": "4\n2 2\n3 5 4 5",
"output": "1 1 2 2 "
},
{
"input": "6\n1 5\n4 4 4 5 4 4",
"output": "2 2 2 1 2 2 "
},
{
"input": "4\n2 2\n2 1 3 3",
"output": "1 1 2 2 "
},
{
"input": "9\n3 6\n4 5 4 1 2 2 2 4 ... | 2,000 | 2,662,400 | 0 | 9,695 |
609 | Gadgets for dollars and pounds | [
"binary search",
"greedy",
"two pointers"
] | null | null | Nura wants to buy *k* gadgets. She has only *s* burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing.
Nura can buy gadgets for *n* days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds.
Each day (from 1 to *n*) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during *n* days.
Help Nura to find the minimum day index when she will have *k* gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to *m* in order of their appearing in input. | First line contains four integers *n*,<=*m*,<=*k*,<=*s* (1<=β€<=*n*<=β€<=2Β·105,<=1<=β€<=*k*<=β€<=*m*<=β€<=2Β·105,<=1<=β€<=*s*<=β€<=109) β number of days, total number and required number of gadgets, number of burles Nura has.
Second line contains *n* integers *a**i* (1<=β€<=*a**i*<=β€<=106) β the cost of one dollar in burles on *i*-th day.
Third line contains *n* integers *b**i* (1<=β€<=*b**i*<=β€<=106) β the cost of one pound in burles on *i*-th day.
Each of the next *m* lines contains two integers *t**i*,<=*c**i* (1<=β€<=*t**i*<=β€<=2,<=1<=β€<=*c**i*<=β€<=106) β type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds. | If Nura can't buy *k* gadgets print the only line with the number -1.
Otherwise the first line should contain integer *d* β the minimum day index, when Nura will have *k* gadgets. On each of the next *k* lines print two integers *q**i*,<=*d**i* β the number of gadget and the day gadget should be bought. All values *q**i* should be different, but the values *d**i* can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to *n*.
In case there are multiple possible solutions, print any of them. | [
"5 4 2 2\n1 2 3 2 1\n3 2 1 2 3\n1 1\n2 1\n1 2\n2 2\n",
"4 3 2 200\n69 70 71 72\n104 105 106 107\n1 1\n2 2\n1 2\n",
"4 3 1 1000000000\n900000 910000 940000 990000\n990000 999000 999900 999990\n1 87654\n2 76543\n1 65432\n"
] | [
"3\n1 1\n2 3\n",
"-1\n",
"-1\n"
] | none | [
{
"input": "5 4 2 2\n1 2 3 2 1\n3 2 1 2 3\n1 1\n2 1\n1 2\n2 2",
"output": "3\n1 1\n2 3"
},
{
"input": "4 3 2 200\n69 70 71 72\n104 105 106 107\n1 1\n2 2\n1 2",
"output": "-1"
},
{
"input": "4 3 1 1000000000\n900000 910000 940000 990000\n990000 999000 999900 999990\n1 87654\n2 76543\n1 65... | 46 | 0 | 0 | 9,699 | |
392 | Yet Another Number Sequence | [
"combinatorics",
"math",
"matrices"
] | null | null | Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recurrence relation:
We'll define a new number sequence *A**i*(*k*) by the formula:
In this problem, your task is to calculate the following sum: *A*1(*k*)<=+<=*A*2(*k*)<=+<=...<=+<=*A**n*(*k*). The answer can be very large, so print it modulo 1000000007 (109<=+<=7). | The first line contains two space-separated integers *n*, *k* (1<=β€<=*n*<=β€<=1017;Β 1<=β€<=*k*<=β€<=40). | Print a single integer β the sum of the first *n* elements of the sequence *A**i*(*k*) modulo 1000000007 (109<=+<=7). | [
"1 1\n",
"4 1\n",
"5 2\n",
"7 4\n"
] | [
"1\n",
"34\n",
"316\n",
"73825\n"
] | none | [] | 1,000 | 614,400 | 0 | 9,731 | |
0 | none | [
"none"
] | null | null | The employees of the F company have lots of ways to entertain themselves. Today they invited a famous magician who shows a trick with plastic cups and a marble.
The point is to trick the spectator's attention. Initially, the spectator stands in front of a line of *n* plastic cups. Then the magician places a small marble under one cup and shuffles the cups. Then the spectator should guess which cup hides the marble.
But the head coder of the F company isn't easy to trick. When he saw the performance, he noticed several important facts:
- each cup contains a mark β a number from 1 to *n*; all marks on the cups are distinct; - the magician shuffles the cups in *m* operations, each operation looks like that: take a cup marked *x**i*, sitting at position *y**i* in the row of cups (the positions are numbered from left to right, starting from 1) and shift it to the very beginning of the cup row (on the first position).
When the head coder came home after work he wanted to re-do the trick. Unfortunately, he didn't remember the starting or the final position of the cups. He only remembered which operations the magician performed. Help the coder: given the operations in the order they were made find at least one initial permutation of the cups that can go through the described operations in the given order. Otherwise, state that such permutation doesn't exist. | The first line contains integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=106). Each of the next *m* lines contains a couple of integers. The *i*-th line contains integers *x**i*, *y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=*n*) β the description of the *i*-th operation of the magician. Note that the operations are given in the order in which the magician made them and the coder wants to make them in the same order. | If the described permutation doesn't exist (the programmer remembered wrong operations), print -1. Otherwise, print *n* distinct integers, each from 1 to *n*: the *i*-th number should represent the mark on the cup that initially is in the row in position *i*.
If there are multiple correct answers, you should print the lexicographically minimum one. | [
"2 1\n2 1\n",
"3 2\n1 2\n1 1\n",
"3 3\n1 3\n2 3\n1 3\n"
] | [
"2 1 \n",
"2 1 3 \n",
"-1\n"
] | none | [] | 62 | 0 | 0 | 9,745 | |
848 | Rooter's Song | [
"constructive algorithms",
"data structures",
"geometry",
"implementation",
"sortings",
"two pointers"
] | null | null | Wherever the destination is, whoever we meet, let's render this song together.
On a Cartesian coordinate plane lies a rectangular stage of size *w*<=Γ<=*h*, represented by a rectangle with corners (0,<=0), (*w*,<=0), (*w*,<=*h*) and (0,<=*h*). It can be seen that no collisions will happen before one enters the stage.
On the sides of the stage stand *n* dancers. The *i*-th of them falls into one of the following groups:
- Vertical: stands at (*x**i*,<=0), moves in positive *y* direction (upwards); - Horizontal: stands at (0,<=*y**i*), moves in positive *x* direction (rightwards).
According to choreography, the *i*-th dancer should stand still for the first *t**i* milliseconds, and then start moving in the specified direction at 1 unit per millisecond, until another border is reached. It is guaranteed that no two dancers have the same group, position and waiting time at the same time.
When two dancers collide (i.e. are on the same point at some time when both of them are moving), they immediately exchange their moving directions and go on.
Dancers stop when a border of the stage is reached. Find out every dancer's stopping position. | The first line of input contains three space-separated positive integers *n*, *w* and *h* (1<=β€<=*n*<=β€<=100<=000, 2<=β€<=*w*,<=*h*<=β€<=100<=000) β the number of dancers and the width and height of the stage, respectively.
The following *n* lines each describes a dancer: the *i*-th among them contains three space-separated integers *g**i*, *p**i*, and *t**i* (1<=β€<=*g**i*<=β€<=2, 1<=β€<=*p**i*<=β€<=99<=999, 0<=β€<=*t**i*<=β€<=100<=000), describing a dancer's group *g**i* (*g**i*<==<=1 β vertical, *g**i*<==<=2 β horizontal), position, and waiting time. If *g**i*<==<=1 then *p**i*<==<=*x**i*; otherwise *p**i*<==<=*y**i*. It's guaranteed that 1<=β€<=*x**i*<=β€<=*w*<=-<=1 and 1<=β€<=*y**i*<=β€<=*h*<=-<=1. It is guaranteed that no two dancers have the same group, position and waiting time at the same time. | Output *n* lines, the *i*-th of which contains two space-separated integers (*x**i*,<=*y**i*) β the stopping position of the *i*-th dancer in the input. | [
"8 10 8\n1 1 10\n1 4 13\n1 7 1\n1 8 2\n2 2 0\n2 5 14\n2 6 0\n2 6 1\n",
"3 2 3\n1 1 2\n2 1 1\n1 1 5\n"
] | [
"4 8\n10 5\n8 8\n10 6\n10 2\n1 8\n7 8\n10 6\n",
"1 3\n2 1\n1 3\n"
] | The first example corresponds to the initial setup in the legend, and the tracks of dancers are marked with different colours in the following figure.
In the second example, no dancers collide. | [
{
"input": "8 10 8\n1 1 10\n1 4 13\n1 7 1\n1 8 2\n2 2 0\n2 5 14\n2 6 0\n2 6 1",
"output": "4 8\n10 5\n8 8\n10 6\n10 2\n1 8\n7 8\n10 6"
},
{
"input": "3 2 3\n1 1 2\n2 1 1\n1 1 5",
"output": "1 3\n2 1\n1 3"
},
{
"input": "1 10 10\n1 8 1",
"output": "8 10"
},
{
"input": "3 4 5\n... | 1,200 | 39,628,800 | 3 | 9,746 | |
533 | Board Game | [
"games",
"greedy",
"implementation",
"math"
] | null | null | Polycarp and Vasiliy love simple logical games. Today they play a game with infinite chessboard and one pawn for each player. Polycarp and Vasiliy move in turns, Polycarp starts. In each turn Polycarp can move his pawn from cell (*x*,<=*y*) to (*x*<=-<=1,<=*y*) or (*x*,<=*y*<=-<=1). Vasiliy can move his pawn from (*x*,<=*y*) to one of cells: (*x*<=-<=1,<=*y*),<=(*x*<=-<=1,<=*y*<=-<=1) and (*x*,<=*y*<=-<=1). Both players are also allowed to skip move.
There are some additional restrictions β a player is forbidden to move his pawn to a cell with negative *x*-coordinate or *y*-coordinate or to the cell containing opponent's pawn The winner is the first person to reach cell (0,<=0).
You are given the starting coordinates of both pawns. Determine who will win if both of them play optimally well. | The first line contains four integers: *x**p*,<=*y**p*,<=*x**v*,<=*y**v* (0<=β€<=*x**p*,<=*y**p*,<=*x**v*,<=*y**v*<=β€<=105) β Polycarp's and Vasiliy's starting coordinates.
It is guaranteed that in the beginning the pawns are in different cells and none of them is in the cell (0,<=0). | Output the name of the winner: "Polycarp" or "Vasiliy". | [
"2 1 2 2\n",
"4 7 7 4\n"
] | [
"Polycarp\n",
"Vasiliy\n"
] | In the first sample test Polycarp starts in (2,β1) and will move to (1,β1) in the first turn. No matter what his opponent is doing, in the second turn Polycarp can move to (1,β0) and finally to (0,β0) in the third turn. | [
{
"input": "2 1 2 2",
"output": "Polycarp"
},
{
"input": "4 7 7 4",
"output": "Vasiliy"
},
{
"input": "20 0 7 22",
"output": "Polycarp"
},
{
"input": "80 100 83 97",
"output": "Vasiliy"
},
{
"input": "80 100 77 103",
"output": "Vasiliy"
},
{
"input": "... | 124 | 1,638,400 | 3 | 9,784 | |
621 | Wet Shark and Blocks | [
"dp",
"matrices"
] | null | null | There are *b* blocks of digits. Each one consisting of the same *n* digits, which are given to you in the input. Wet Shark must choose exactly one digit from each block and concatenate all of those digits together to form one large integer. For example, if he chooses digit 1 from the first block and digit 2 from the second block, he gets the integer 12.
Wet Shark then takes this number modulo *x*. Please, tell him how many ways he can choose one digit from each block so that he gets exactly *k* as the final result. As this number may be too large, print it modulo 109<=+<=7.
Note, that the number of ways to choose some digit in the block is equal to the number of it's occurrences. For example, there are 3 ways to choose digit 5 from block 3 5 6 7 8 9 5 1 1 1 1 5. | The first line of the input contains four space-separated integers, *n*, *b*, *k* and *x* (2<=β€<=*n*<=β€<=50<=000,<=1<=β€<=*b*<=β€<=109,<=0<=β€<=*k*<=<<=*x*<=β€<=100,<=*x*<=β₯<=2)Β β the number of digits in one block, the number of blocks, interesting remainder modulo *x* and modulo *x* itself.
The next line contains *n* space separated integers *a**i* (1<=β€<=*a**i*<=β€<=9), that give the digits contained in each block. | Print the number of ways to pick exactly one digit from each blocks, such that the resulting integer equals *k* modulo *x*. | [
"12 1 5 10\n3 5 6 7 8 9 5 1 1 1 1 5\n",
"3 2 1 2\n6 2 2\n",
"3 2 1 2\n3 1 2\n"
] | [
"3\n",
"0\n",
"6\n"
] | In the second sample possible integers are 22, 26, 62 and 66. None of them gives the remainder 1 modulo 2.
In the third sample integers 11, 13, 21, 23, 31 and 33 have remainder 1 modulo 2. There is exactly one way to obtain each of these integers, so the total answer is 6. | [
{
"input": "12 1 5 10\n3 5 6 7 8 9 5 1 1 1 1 5",
"output": "3"
},
{
"input": "3 2 1 2\n6 2 2",
"output": "0"
},
{
"input": "3 2 1 2\n3 1 2",
"output": "6"
},
{
"input": "3 2 1 2\n6 3 2",
"output": "3"
},
{
"input": "3 2 1 2\n3 6 3",
"output": "6"
},
{
... | 2,000 | 921,600 | 0 | 9,809 | |
254 | Jury Size | [
"brute force",
"implementation"
] | null | null | In 2013, the writers of Berland State University should prepare problems for *n* Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to *n*. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number *i* should be prepared by *p**i* people for *t**i* days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.
For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.
In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. | The first line contains integer *n* β the number of Olympiads in 2013 (1<=β€<=*n*<=β€<=100). Each of the following *n* lines contains four integers *m**i*, *d**i*, *p**i* and *t**i* β the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the *i*-th Olympiad (1<=β€<=*m**i*<=β€<=12, *d**i*<=β₯<=1, 1<=β€<=*p**i*,<=*t**i*<=β€<=100), *d**i* doesn't exceed the number of days in month *m**i*. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day.
Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. | Print a single number β the minimum jury size. | [
"2\n5 23 1 2\n3 13 2 3\n",
"3\n12 9 2 1\n12 8 1 3\n12 8 2 2\n",
"1\n1 10 1 13\n"
] | [
"2\n",
"3\n",
"1\n"
] | none | [
{
"input": "2\n5 23 1 2\n3 13 2 3",
"output": "2"
},
{
"input": "3\n12 9 2 1\n12 8 1 3\n12 8 2 2",
"output": "3"
},
{
"input": "1\n1 10 1 13",
"output": "1"
},
{
"input": "3\n3 16 25 1\n3 15 9 34\n3 14 90 87",
"output": "99"
},
{
"input": "4\n2 15 52 53\n2 15 35 8... | 218 | 409,600 | 3 | 9,812 | |
524 | Π€ΠΎΡΠΎ Π½Π° ΠΏΠ°ΠΌΡΡΡ - 2 (round version) | [
"dp",
"greedy"
] | null | null | ΠΡΠΎΡΠ»ΠΎ ΠΌΠ½ΠΎΠ³ΠΎ Π»Π΅Ρ, ΠΈ Π½Π° Π²Π΅ΡΠ΅ΡΠΈΠ½ΠΊΠ΅ ΡΠ½ΠΎΠ²Π° Π²ΡΡΡΠ΅ΡΠΈΠ»ΠΈΡΡ *n* Π΄ΡΡΠ·Π΅ΠΉ. Π‘ ΠΌΠΎΠΌΠ΅Π½ΡΠ° ΠΏΠΎΡΠ»Π΅Π΄Π½Π΅ΠΉ Π²ΡΡΡΠ΅ΡΠΈ ΡΠ΅Ρ
Π½ΠΈΠΊΠ° ΡΠ°Π³Π½ΡΠ»Π° Π΄Π°Π»Π΅ΠΊΠΎ Π²ΠΏΠ΅ΡΡΠ΄, ΠΏΠΎΡΠ²ΠΈΠ»ΠΈΡΡ ΡΠΎΡΠΎΠ°ΠΏΠΏΠ°ΡΠ°ΡΡ Ρ Π°Π²ΡΠΎΡΠΏΡΡΠΊΠΎΠΌ, ΠΈ ΡΠ΅ΠΏΠ΅ΡΡ Π½Π΅ ΡΡΠ΅Π±ΡΠ΅ΡΡΡ, ΡΡΠΎΠ±Ρ ΠΎΠ΄ΠΈΠ½ ΠΈΠ· Π΄ΡΡΠ·Π΅ΠΉ ΡΡΠΎΡΠ» Ρ ΡΠΎΡΠΎΠ°ΠΏΠΏΠ°ΡΠ°ΡΠΎΠΌ, ΠΈ, ΡΠ΅ΠΌ ΡΠ°ΠΌΡΠΌ, ΠΎΠΊΠ°Π·ΡΠ²Π°Π»ΡΡ Π½Π΅ Π·Π°ΠΏΠ΅ΡΠ°ΡΠ»ΡΠ½Π½ΡΠΌ Π½Π° ΡΠ½ΠΈΠΌΠΊΠ΅.
Π£ΠΏΡΠΎΡΠ΅Π½Π½ΠΎ ΠΏΡΠΎΡΠ΅ΡΡ ΡΠΎΡΠΎΠ³ΡΠ°ΡΠΈΡΠΎΠ²Π°Π½ΠΈΡ ΠΌΠΎΠΆΠ½ΠΎ ΠΎΠΏΠΈΡΠ°ΡΡ ΡΠ»Π΅Π΄ΡΡΡΠΈΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ. ΠΠ° ΡΠΎΡΠΎΠ³ΡΠ°ΡΠΈΠΈ ΠΊΠ°ΠΆΠ΄ΡΠΉ ΠΈΠ· Π΄ΡΡΠ·Π΅ΠΉ Π·Π°Π½ΠΈΠΌΠ°Π΅Ρ ΠΏΡΡΠΌΠΎΡΠ³ΠΎΠ»ΡΠ½ΠΈΠΊ ΠΈΠ· ΠΏΠΈΠΊΡΠ΅Π»Π΅ΠΉ: Π² ΡΡΠΎΡΡΠ΅ΠΌ ΠΏΠΎΠ»ΠΎΠΆΠ΅Π½ΠΈΠΈ *i*-ΠΉ ΠΈΠ· Π½ΠΈΡ
Π·Π°Π½ΠΈΠΌΠ°Π΅Ρ ΠΏΡΡΠΌΠΎΡΠ³ΠΎΠ»ΡΠ½ΠΈΠΊ ΡΠΈΡΠΈΠ½Ρ *w**i* ΠΏΠΈΠΊΡΠ΅Π»Π΅ΠΉ ΠΈ Π²ΡΡΠΎΡΡ *h**i* ΠΏΠΈΠΊΡΠ΅Π»Π΅ΠΉ. ΠΠΎ ΡΠ°ΠΊΠΆΠ΅, ΠΏΡΠΈ ΡΠΎΡΠΎΠ³ΡΠ°ΡΠΈΡΠΎΠ²Π°Π½ΠΈΠΈ ΠΊΠ°ΠΆΠ΄ΡΠΉ ΡΠ΅Π»ΠΎΠ²Π΅ΠΊ ΠΌΠΎΠΆΠ΅Ρ Π»Π΅ΡΡ, ΠΈ ΡΠΎΠ³Π΄Π° ΠΎΠ½ Π±ΡΠ΄Π΅Ρ Π·Π°Π½ΠΈΠΌΠ°ΡΡ ΠΏΡΡΠΌΠΎΡΠ³ΠΎΠ»ΡΠ½ΠΈΠΊ ΡΠΈΡΠΈΠ½Ρ *h**i* ΠΏΠΈΠΊΡΠ΅Π»Π΅ΠΉ ΠΈ Π²ΡΡΠΎΡΡ *w**i* ΠΏΠΈΠΊΡΠ΅Π»Π΅ΠΉ.
ΠΠ±ΡΠ°Ρ ΡΠΎΡΠΎΠ³ΡΠ°ΡΠΈΡ Π±ΡΠ΄Π΅Ρ ΠΈΠΌΠ΅ΡΡ ΡΠ°Π·ΠΌΠ΅ΡΡ *W*<=Γ<=*H*, Π³Π΄Π΅ *W* β ΡΡΠΌΠΌΠ°ΡΠ½Π°Ρ ΡΠΈΡΠΈΠ½Π° Π²ΡΠ΅Ρ
ΠΏΡΡΠΌΠΎΡΠ³ΠΎΠ»ΡΠ½ΠΈΠΊΠΎΠ²-Π»ΡΠ΄Π΅ΠΉ, Π° *H* β ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½Π°Ρ ΠΈΠ· Π²ΡΡΠΎΡ. ΠΡΡΠ·ΡΡ Ρ
ΠΎΡΡΡ ΠΎΠΏΡΠ΅Π΄Π΅Π»ΠΈΡΡ, ΠΊΠ°ΠΊΡΡ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΡΡ ΠΏΠ»ΠΎΡΠ°Π΄Ρ ΠΌΠΎΠΆΠ΅Ρ ΠΈΠΌΠ΅ΡΡ ΠΎΠ±ΡΠ°Ρ ΡΠΎΡΠΎΠ³ΡΠ°ΡΠΈΡ. ΠΠΎΠΌΠΎΠ³ΠΈΡΠ΅ ΠΈΠΌ Π² ΡΡΠΎΠΌ. | Π ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ ΡΠ»Π΅Π΄ΡΠ΅Ρ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ *n* (1<=β€<=*n*<=β€<=1000) β ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π΄ΡΡΠ·Π΅ΠΉ.
Π ΠΏΠΎΡΠ»Π΅Π΄ΡΡΡΠΈΡ
*n* ΡΡΡΠΎΠΊΠ°Ρ
ΡΠ»Π΅Π΄ΡΡΡ ΠΏΠΎ Π΄Π²Π° ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ»Π° *w**i*,<=*h**i* (1<=β€<=*w**i*,<=*h**i*<=β€<=1000), ΠΎΠ±ΠΎΠ·Π½Π°ΡΠ°ΡΡΠΈΠ΅ ΡΠ°Π·ΠΌΠ΅ΡΡ ΠΏΡΡΠΌΠΎΡΠ³ΠΎΠ»ΡΠ½ΠΈΠΊΠ°, ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΡΡΡΠ΅Π³ΠΎ *i*-ΠΌΡ ΠΈΠ· Π΄ΡΡΠ·Π΅ΠΉ. | ΠΡΠ²Π΅Π΄ΠΈΡΠ΅ Π΅Π΄ΠΈΠ½ΡΡΠ²Π΅Π½Π½ΠΎΠ΅ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ, ΡΠ°Π²Π½ΠΎΠ΅ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠΉ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΠΉ ΠΏΠ»ΠΎΡΠ°Π΄ΠΈ ΡΠΎΡΠΎΠ³ΡΠ°ΡΠΈΠΈ, Π²ΠΌΠ΅ΡΠ°ΡΡΠ΅ΠΉ Π²ΡΠ΅Ρ
Π΄ΡΡΠ·Π΅ΠΉ. | [
"3\n10 1\n20 2\n30 3\n",
"3\n3 1\n2 2\n4 3\n",
"1\n5 10\n"
] | [
"180\n",
"21\n",
"50\n"
] | none | [
{
"input": "3\n10 1\n20 2\n30 3",
"output": "180"
},
{
"input": "3\n3 1\n2 2\n4 3",
"output": "21"
},
{
"input": "1\n5 10",
"output": "50"
},
{
"input": "10\n168 538\n836 439\n190 873\n206 47\n891 591\n939 481\n399 898\n859 466\n701 777\n629 222",
"output": "3478056"
},... | 31 | 0 | 0 | 9,827 | |
0 | none | [
"none"
] | null | null | Julia is going to cook a chicken in the kitchen of her dormitory. To save energy, the stove in the kitchen automatically turns off after *k* minutes after turning on.
During cooking, Julia goes to the kitchen every *d* minutes and turns on the stove if it is turned off. While the cooker is turned off, it stays warm. The stove switches on and off instantly.
It is known that the chicken needs *t* minutes to be cooked on the stove, if it is turned on, and 2*t* minutes, if it is turned off. You need to find out, how much time will Julia have to cook the chicken, if it is considered that the chicken is cooked evenly, with constant speed when the stove is turned on and at a constant speed when it is turned off. | The single line contains three integers *k*, *d* and *t* (1<=β€<=*k*,<=*d*,<=*t*<=β€<=1018). | Print a single number, the total time of cooking in minutes. The relative or absolute error must not exceed 10<=-<=9.
Namely, let's assume that your answer is *x* and the answer of the jury is *y*. The checker program will consider your answer correct if . | [
"3 2 6\n",
"4 2 20\n"
] | [
"6.5\n",
"20.0\n"
] | In the first example, the chicken will be cooked for 3 minutes on the turned on stove, after this it will be cooked for <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/cce5d3f2f46552034d5ae5d487725705429ec7a5.png" style="max-width: 100.0%;max-height: 100.0%;"/>. Then the chicken will be cooked for one minute on a turned off stove, it will be cooked for <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/a10fa55d1324328f9ba60c9343ed0ecb0506d678.png" style="max-width: 100.0%;max-height: 100.0%;"/>. Thus, after four minutes the chicken will be cooked for <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/6fcc8bd6c2188b260d9d18e7b6c9e3908848df71.png" style="max-width: 100.0%;max-height: 100.0%;"/>. Before the fifth minute Julia will turn on the stove and after 2.5 minutes the chicken will be ready <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/87a86c8e9632089279245fff912c077126c4e704.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the second example, when the stove is turned off, Julia will immediately turn it on, so the stove will always be turned on and the chicken will be cooked in 20 minutes. | [
{
"input": "3 2 6",
"output": "6.5"
},
{
"input": "4 2 20",
"output": "20.0"
},
{
"input": "8 10 9",
"output": "10.0"
},
{
"input": "43 50 140",
"output": "150.5"
},
{
"input": "251 79 76",
"output": "76.0"
},
{
"input": "892 67 1000",
"output": "1... | 61 | 21,401,600 | 0 | 9,836 | |
612 | Square Root of Permutation | [
"combinatorics",
"constructive algorithms",
"dfs and similar",
"graphs",
"math"
] | null | null | A permutation of length *n* is an array containing each integer from 1 to *n* exactly once. For example, *q*<==<=[4,<=5,<=1,<=2,<=3] is a permutation. For the permutation *q* the square of permutation is the permutation *p* that *p*[*i*]<==<=*q*[*q*[*i*]] for each *i*<==<=1... *n*. For example, the square of *q*<==<=[4,<=5,<=1,<=2,<=3] is *p*<==<=*q*2<==<=[2,<=3,<=4,<=5,<=1].
This problem is about the inverse operation: given the permutation *p* you task is to find such permutation *q* that *q*2<==<=*p*. If there are several such *q* find any of them. | The first line contains integer *n* (1<=β€<=*n*<=β€<=106) β the number of elements in permutation *p*.
The second line contains *n* distinct integers *p*1,<=*p*2,<=...,<=*p**n* (1<=β€<=*p**i*<=β€<=*n*) β the elements of permutation *p*. | If there is no permutation *q* such that *q*2<==<=*p* print the number "-1".
If the answer exists print it. The only line should contain *n* different integers *q**i* (1<=β€<=*q**i*<=β€<=*n*) β the elements of the permutation *q*. If there are several solutions print any of them. | [
"4\n2 1 4 3\n",
"4\n2 1 3 4\n",
"5\n2 3 4 5 1\n"
] | [
"3 4 2 1\n",
"-1\n",
"4 5 1 2 3\n"
] | none | [
{
"input": "4\n2 1 4 3",
"output": "3 4 2 1"
},
{
"input": "4\n2 1 3 4",
"output": "-1"
},
{
"input": "5\n2 3 4 5 1",
"output": "4 5 1 2 3"
},
{
"input": "1\n1",
"output": "1"
},
{
"input": "1\n1",
"output": "1"
},
{
"input": "10\n8 2 10 3 4 6 1 7 9 5"... | 841 | 144,896,000 | 3 | 9,843 | |
560 | Gerald is into Art | [
"constructive algorithms",
"implementation"
] | null | null | Gerald bought two very rare paintings at the Sotheby's auction and he now wants to hang them on the wall. For that he bought a special board to attach it to the wall and place the paintings on the board. The board has shape of an *a*1<=Γ<=*b*1 rectangle, the paintings have shape of a *a*2<=Γ<=*b*2 and *a*3<=Γ<=*b*3 rectangles.
Since the paintings are painted in the style of abstract art, it does not matter exactly how they will be rotated, but still, one side of both the board, and each of the paintings must be parallel to the floor. The paintings can touch each other and the edges of the board, but can not overlap or go beyond the edge of the board. Gerald asks whether it is possible to place the paintings on the board, or is the board he bought not large enough? | The first line contains two space-separated numbers *a*1 and *b*1 β the sides of the board. Next two lines contain numbers *a*2,<=*b*2,<=*a*3 and *b*3 β the sides of the paintings. All numbers *a**i*,<=*b**i* in the input are integers and fit into the range from 1 to 1000. | If the paintings can be placed on the wall, print "YES" (without the quotes), and if they cannot, print "NO" (without the quotes). | [
"3 2\n1 3\n2 1\n",
"5 5\n3 3\n3 3\n",
"4 2\n2 3\n1 2\n"
] | [
"YES\n",
"NO\n",
"YES\n"
] | That's how we can place the pictures in the first test:
<img class="tex-graphics" src="https://espresso.codeforces.com/b41bf40c649073c6d3dd62eb7ae7adfc4bd131bd.png" style="max-width: 100.0%;max-height: 100.0%;"/>
And that's how we can do it in the third one.
<img class="tex-graphics" src="https://espresso.codeforces.com/dafdf616eaa5ef10cd3c9ccdc7fba7ece392268c.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "3 2\n1 3\n2 1",
"output": "YES"
},
{
"input": "5 5\n3 3\n3 3",
"output": "NO"
},
{
"input": "4 2\n2 3\n1 2",
"output": "YES"
},
{
"input": "3 3\n1 1\n1 1",
"output": "YES"
},
{
"input": "1000 1000\n999 999\n1 1000",
"output": "YES"
},
{
"in... | 62 | 5,120,000 | 0 | 9,871 | |
773 | Prairie Partition | [
"binary search",
"constructive algorithms",
"greedy",
"math"
] | null | null | It can be shown that any positive integer *x* can be uniquely represented as *x*<==<=1<=+<=2<=+<=4<=+<=...<=+<=2*k*<=-<=1<=+<=*r*, where *k* and *r* are integers, *k*<=β₯<=0, 0<=<<=*r*<=β€<=2*k*. Let's call that representation prairie partition of *x*.
For example, the prairie partitions of 12, 17, 7 and 1 are:
17<==<=1<=+<=2<=+<=4<=+<=8<=+<=2,
7<==<=1<=+<=2<=+<=4,
1<==<=1.
Alice took a sequence of positive integers (possibly with repeating elements), replaced every element with the sequence of summands in its prairie partition, arranged the resulting numbers in non-decreasing order and gave them to Borys. Now Borys wonders how many elements Alice's original sequence could contain. Find all possible options! | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=105)Β β the number of numbers given from Alice to Borys.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=1012; *a*1<=β€<=*a*2<=β€<=...<=β€<=*a**n*)Β β the numbers given from Alice to Borys. | Output, in increasing order, all possible values of *m* such that there exists a sequence of positive integers of length *m* such that if you replace every element with the summands in its prairie partition and arrange the resulting numbers in non-decreasing order, you will get the sequence given in the input.
If there are no such values of *m*, output a single integer -1. | [
"8\n1 1 2 2 3 4 5 8\n",
"6\n1 1 1 2 2 2\n",
"5\n1 2 4 4 4\n"
] | [
"2 \n",
"2 3 \n",
"-1\n"
] | In the first example, Alice could get the input sequence from [6,β20] as the original sequence.
In the second example, Alice's original sequence could be either [4,β5] or [3,β3,β3]. | [
{
"input": "8\n1 1 2 2 3 4 5 8",
"output": "2 "
},
{
"input": "6\n1 1 1 2 2 2",
"output": "2 3 "
},
{
"input": "5\n1 2 4 4 4",
"output": "-1"
},
{
"input": "20\n1 1 1 1 2 2 2 2 4 4 4 4 8 8 8 8 8 10 10 11",
"output": "4 "
},
{
"input": "20\n1 1 1 1 1 1 1 1 1 1 1 1 ... | 546 | 13,414,400 | 3 | 9,881 | |
847 | Preparing for Merge Sort | [
"binary search",
"data structures"
] | null | null | Ivan has an array consisting of *n* different integers. He decided to reorder all elements in increasing order. Ivan loves merge sort so he decided to represent his array with one or several increasing sequences which he then plans to merge into one sorted array.
Ivan represent his array with increasing sequences with help of the following algorithm.
While there is at least one unused number in array Ivan repeats the following procedure:
- iterate through array from the left to the right; - Ivan only looks at unused numbers on current iteration; - if current number is the first unused number on this iteration or this number is greater than previous unused number on current iteration, then Ivan marks the number as used and writes it down.
For example, if Ivan's array looks like [1, 3, 2, 5, 4] then he will perform two iterations. On first iteration Ivan will use and write numbers [1, 3, 5], and on second one β [2, 4].
Write a program which helps Ivan and finds representation of the given array with one or several increasing sequences in accordance with algorithm described above. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=2Β·105) β the number of elements in Ivan's array.
The second line contains a sequence consisting of distinct integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109) β Ivan's array. | Print representation of the given array in the form of one or more increasing sequences in accordance with the algorithm described above. Each sequence must be printed on a new line. | [
"5\n1 3 2 5 4\n",
"4\n4 3 2 1\n",
"4\n10 30 50 101\n"
] | [
"1 3 5 \n2 4 \n",
"4 \n3 \n2 \n1 \n",
"10 30 50 101 \n"
] | none | [
{
"input": "5\n1 3 2 5 4",
"output": "1 3 5 \n2 4 "
},
{
"input": "4\n4 3 2 1",
"output": "4 \n3 \n2 \n1 "
},
{
"input": "4\n10 30 50 101",
"output": "10 30 50 101 "
},
{
"input": "1\n1",
"output": "1 "
},
{
"input": "1\n200000",
"output": "200000 "
},
{
... | 2,000 | 42,291,200 | 0 | 9,882 | |
770 | Draw Brackets! | [
"*special",
"implementation"
] | null | null | A sequence of square brackets is regular if by inserting symbols "+" and "1" into it, you can get a regular mathematical expression from it. For example, sequences "[[]][]", "[]" and "[[][[]]]" β are regular, at the same time "][", "[[]" and "[[]]][" β are irregular.
Draw the given sequence using a minimalistic pseudographics in the strip of the lowest possible height β use symbols '+', '-' and '|'. For example, the sequence "[[][]][]" should be represented as:
Each bracket should be represented with the hepl of one or more symbols '|' (the vertical part) and symbols '+' and '-' as on the example which is given above.
Brackets should be drawn without spaces one by one, only dividing pairs of consecutive pairwise brackets with a single-space bar (so that the two brackets do not visually merge into one symbol). The image should have the minimum possible height.
The enclosed bracket is always smaller than the surrounding bracket, but each bracket separately strives to maximize the height of the image. So the pair of final brackets in the example above occupies the entire height of the image.
Study carefully the examples below, they adequately explain the condition of the problem. Pay attention that in this problem the answer (the image) is unique. | The first line contains an even integer *n* (2<=β€<=*n*<=β€<=100) β the length of the sequence of brackets.
The second line contains the sequence of brackets β these are *n* symbols "[" and "]". It is guaranteed that the given sequence of brackets is regular. | Print the drawn bracket sequence in the format which is given in the condition. Don't print extra (unnecessary) spaces. | [
"8\n[[][]][]\n",
"6\n[[[]]]\n",
"6\n[[][]]\n",
"2\n[]\n",
"4\n[][]\n"
] | [
"+- -++- -+\n|+- -++- -+|| |\n|| || ||| |\n|+- -++- -+|| |\n+- -++- -+\n",
"+- -+\n|+- -+|\n||+- -+||\n||| |||\n||+- -+||\n|+- -+|\n+- -+\n",
"+- -+\n|+- -++- -+|\n|| || ||\n|+- -++- -+|\n+- -+\n",
"+- -+\n| |\n+- -+\n",
"+- -++- -+\n| || |\n+- -... | none | [
{
"input": "8\n[[][]][]",
"output": "+- -++- -+\n|+- -++- -+|| |\n|| || ||| |\n|+- -++- -+|| |\n+- -++- -+"
},
{
"input": "6\n[[[]]]",
"output": "+- -+\n|+- -+|\n||+- -+||\n||| |||\n||+- -+||\n|+- -+|\n+- -+"
},
{
"input": "6\n[[][]]",
"output": ... | 46 | 0 | 3 | 9,887 | |
0 | none | [
"none"
] | null | null | Little Artem likes electronics. He can spend lots of time making different schemas and looking for novelties in the nearest electronics store. The new control element was delivered to the store recently and Artem immediately bought it.
That element can store information about the matrix of integers size *n*<=Γ<=*m*. There are *n*<=+<=*m* inputs in that element, i.e. each row and each column can get the signal. When signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on. When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on. Rows are numbered with integers from 1 to *n* from top to bottom, while columns are numbered with integers from 1 to *m* from left to right.
Artem wants to carefully study this element before using it. For that purpose he is going to set up an experiment consisting of *q* turns. On each turn he either sends the signal to some input or checks what number is stored at some position of the matrix.
Artem has completed his experiment and has written down the results, but he has lost the chip! Help Artem find any initial matrix that will match the experiment results. It is guaranteed that experiment data is consistent, which means at least one valid matrix exists. | The first line of the input contains three integers *n*, *m* and *q* (1<=β€<=*n*,<=*m*<=β€<=100,<=1<=β€<=*q*<=β€<=10<=000)Β β dimensions of the matrix and the number of turns in the experiment, respectively.
Next *q* lines contain turns descriptions, one per line. Each description starts with an integer *t**i* (1<=β€<=*t**i*<=β€<=3) that defines the type of the operation. For the operation of first and second type integer *r**i* (1<=β€<=*r**i*<=β€<=*n*) or *c**i* (1<=β€<=*c**i*<=β€<=*m*) follows, while for the operations of the third type three integers *r**i*, *c**i* and *x**i* (1<=β€<=*r**i*<=β€<=*n*, 1<=β€<=*c**i*<=β€<=*m*, <=-<=109<=β€<=*x**i*<=β€<=109) are given.
Operation of the first type (*t**i*<==<=1) means that signal comes to the input corresponding to row *r**i*, that is it will shift cyclically. Operation of the second type (*t**i*<==<=2) means that column *c**i* will shift cyclically. Finally, operation of the third type means that at this moment of time cell located in the row *r**i* and column *c**i* stores value *x**i*. | Print the description of any valid initial matrix as *n* lines containing *m* integers each. All output integers should not exceed 109 by their absolute value.
If there are multiple valid solutions, output any of them. | [
"2 2 6\n2 1\n2 2\n3 1 1 1\n3 2 2 2\n3 1 2 8\n3 2 1 8\n",
"3 3 2\n1 2\n3 2 2 5\n"
] | [
"8 2 \n1 8 \n",
"0 0 0 \n0 0 5 \n0 0 0 \n"
] | none | [
{
"input": "2 2 6\n2 1\n2 2\n3 1 1 1\n3 2 2 2\n3 1 2 8\n3 2 1 8",
"output": "8 2 \n1 8 "
},
{
"input": "3 3 2\n1 2\n3 2 2 5",
"output": "0 0 0 \n0 0 5 \n0 0 0 "
},
{
"input": "5 5 1\n1 5",
"output": "0 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 "
},
{
"input": "1... | 342 | 29,593,600 | 3 | 9,926 | |
0 | none | [
"none"
] | null | null | You are given *n* distinct points on a plane with integral coordinates. For each point you can either draw a vertical line through it, draw a horizontal line through it, or do nothing.
You consider several coinciding straight lines as a single one. How many distinct pictures you can get? Print the answer modulo 109<=+<=7. | The first line contains single integer *n* (1<=β€<=*n*<=β€<=105)Β β the number of points.
*n* lines follow. The (*i*<=+<=1)-th of these lines contains two integers *x**i*, *y**i* (<=-<=109<=β€<=*x**i*,<=*y**i*<=β€<=109)Β β coordinates of the *i*-th point.
It is guaranteed that all points are distinct. | Print the number of possible distinct pictures modulo 109<=+<=7. | [
"4\n1 1\n1 2\n2 1\n2 2\n",
"2\n-1 -1\n0 1\n"
] | [
"16\n",
"9\n"
] | In the first example there are two vertical and two horizontal lines passing through the points. You can get pictures with any subset of these lines. For example, you can get the picture containing all four lines in two ways (each segment represents a line containing it).
In the second example you can work with two points independently. The number of pictures is 3<sup class="upper-index">2</sup>β=β9. | [] | 30 | 0 | 0 | 9,936 | |
0 | none | [
"none"
] | null | null | Pupils decided to go to amusement park. Some of them were with parents. In total, *n* people came to the park and they all want to get to the most extreme attraction and roll on it exactly once.
Tickets for group of *x* people are sold on the attraction, there should be at least one adult in each group (it is possible that the group consists of one adult). The ticket price for such group is *c*1<=+<=*c*2Β·(*x*<=-<=1)2 (in particular, if the group consists of one person, then the price is *c*1).
All pupils who came to the park and their parents decided to split into groups in such a way that each visitor join exactly one group, and the total price of visiting the most extreme attraction is as low as possible. You are to determine this minimum possible total price. There should be at least one adult in each group. | The first line contains three integers *n*, *c*1 and *c*2 (1<=β€<=*n*<=β€<=200<=000, 1<=β€<=*c*1,<=*c*2<=β€<=107)Β β the number of visitors and parameters for determining the ticket prices for a group.
The second line contains the string of length *n*, which consists of zeros and ones. If the *i*-th symbol of the string is zero, then the *i*-th visitor is a pupil, otherwise the *i*-th person is an adult. It is guaranteed that there is at least one adult. It is possible that there are no pupils. | Print the minimum price of visiting the most extreme attraction for all pupils and their parents. Each of them should roll on the attraction exactly once. | [
"3 4 1\n011\n",
"4 7 2\n1101\n"
] | [
"8\n",
"18\n"
] | In the first test one group of three people should go to the attraction. Then they have to pay 4β+β1β*β(3β-β1)<sup class="upper-index">2</sup>β=β8.
In the second test it is better to go to the attraction in two groups. The first group should consist of two adults (for example, the first and the second person), the second should consist of one pupil and one adult (the third and the fourth person). Then each group will have a size of two and for each the price of ticket is 7β+β2β*β(2β-β1)<sup class="upper-index">2</sup>β=β9. Thus, the total price for two groups is 18. | [
{
"input": "3 4 1\n011",
"output": "8"
},
{
"input": "4 7 2\n1101",
"output": "18"
},
{
"input": "1 2 2\n1",
"output": "2"
},
{
"input": "2 3 10\n01",
"output": "13"
},
{
"input": "5 10 3\n11100",
"output": "35"
},
{
"input": "10 2 2\n1111101111",
... | 2,000 | 5,529,600 | 0 | 9,949 | |
804 | Find Amir | [
"constructive algorithms",
"greedy",
"math"
] | null | null | A few years ago Sajjad left his school and register to another one due to security reasons. Now he wishes to find Amir, one of his schoolmates and good friends.
There are *n* schools numerated from 1 to *n*. One can travel between each pair of them, to do so, he needs to buy a ticket. The ticker between schools *i* and *j* costs and can be used multiple times. Help Sajjad to find the minimum cost he needs to pay for tickets to visit all schools. He can start and finish in any school. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=105)Β β the number of schools. | Print single integer: the minimum cost of tickets needed to visit all schools. | [
"2\n",
"10\n"
] | [
"0\n",
"4\n"
] | In the first example we can buy a ticket between the schools that costs <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c67d72010e0498bfd065a6a38fdeaec90358507b.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [
{
"input": "2",
"output": "0"
},
{
"input": "10",
"output": "4"
},
{
"input": "43670",
"output": "21834"
},
{
"input": "4217",
"output": "2108"
},
{
"input": "17879",
"output": "8939"
},
{
"input": "31809",
"output": "15904"
},
{
"input": "... | 46 | 0 | 0 | 9,966 | |
0 | none | [
"none"
] | null | null | ...Once upon a time a man came to the sea. The sea was stormy and dark. The man started to call for the little mermaid to appear but alas, he only woke up Cthulhu...
Whereas on the other end of the world Pentagon is actively collecting information trying to predict the monster's behavior and preparing the secret super weapon. Due to high seismic activity and poor weather conditions the satellites haven't yet been able to make clear shots of the monster. The analysis of the first shot resulted in an undirected graph with *n* vertices and *m* edges. Now the world's best minds are about to determine whether this graph can be regarded as Cthulhu or not.
To add simplicity, let's suppose that Cthulhu looks from the space like some spherical body with tentacles attached to it. Formally, we shall regard as Cthulhu such an undirected graph that can be represented as a set of three or more rooted trees, whose roots are connected by a simple cycle.
It is guaranteed that the graph contains no multiple edges and self-loops. | The first line contains two integers β the number of vertices *n* and the number of edges *m* of the graph (1<=β€<=*n*<=β€<=100, 0<=β€<=*m*<=β€<=).
Each of the following *m* lines contains a pair of integers *x* and *y*, that show that an edge exists between vertices *x* and *y* (1<=β€<=*x*,<=*y*<=β€<=*n*,<=*x*<=β <=*y*). For each pair of vertices there will be at most one edge between them, no edge connects a vertex to itself. | Print "NO", if the graph is not Cthulhu and "FHTAGN!" if it is. | [
"6 6\n6 3\n6 4\n5 1\n2 5\n1 4\n5 4\n",
"6 5\n5 6\n4 6\n3 1\n5 1\n1 2\n"
] | [
"FHTAGN!",
"NO"
] | Let us denote as a simple cycle a set of *v* vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, ..., *v*β-β1 and *v*, *v* and 1.
A tree is a connected undirected graph consisting of *n* vertices and *n*β-β1 edges (*n*β>β0).
A rooted tree is a tree where one vertex is selected to be the root. | [
{
"input": "6 6\n6 3\n6 4\n5 1\n2 5\n1 4\n5 4",
"output": "FHTAGN!"
},
{
"input": "6 5\n5 6\n4 6\n3 1\n5 1\n1 2",
"output": "NO"
},
{
"input": "10 10\n4 10\n8 5\n2 8\n4 9\n9 3\n2 7\n10 6\n10 2\n9 8\n1 8",
"output": "FHTAGN!"
},
{
"input": "5 4\n1 5\n1 3\n1 4\n3 2",
"outpu... | 122 | 409,600 | 3 | 9,968 | |
822 | Crossword solving | [
"brute force",
"implementation",
"strings"
] | null | null | Erelong Leha was bored by calculating of the greatest common divisor of two factorials. Therefore he decided to solve some crosswords. It's well known that it is a very interesting occupation though it can be very difficult from time to time. In the course of solving one of the crosswords, Leha had to solve a simple task. You are able to do it too, aren't you?
Leha has two strings *s* and *t*. The hacker wants to change the string *s* at such way, that it can be found in *t* as a substring. All the changes should be the following: Leha chooses one position in the string *s* and replaces the symbol in this position with the question mark "?". The hacker is sure that the question mark in comparison can play the role of an arbitrary symbol. For example, if he gets string *s*="ab?b" as a result, it will appear in *t*="aabrbb" as a substring.
Guaranteed that the length of the string *s* doesn't exceed the length of the string *t*. Help the hacker to replace in *s* as few symbols as possible so that the result of the replacements can be found in *t* as a substring. The symbol "?" should be considered equal to any other symbol. | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=*m*<=β€<=1000) β the length of the string *s* and the length of the string *t* correspondingly.
The second line contains *n* lowercase English letters β string *s*.
The third line contains *m* lowercase English letters β string *t*. | In the first line print single integer *k* β the minimal number of symbols that need to be replaced.
In the second line print *k* distinct integers denoting the positions of symbols in the string *s* which need to be replaced. Print the positions in any order. If there are several solutions print any of them. The numbering of the positions begins from one. | [
"3 5\nabc\nxaybz\n",
"4 10\nabcd\nebceabazcd\n"
] | [
"2\n2 3 \n",
"1\n2 \n"
] | none | [
{
"input": "3 5\nabc\nxaybz",
"output": "2\n2 3 "
},
{
"input": "4 10\nabcd\nebceabazcd",
"output": "1\n2 "
},
{
"input": "1 1\na\na",
"output": "0"
},
{
"input": "1 1\na\nz",
"output": "1\n1 "
},
{
"input": "3 5\naaa\naaaaa",
"output": "0"
},
{
"input... | 46 | 0 | 0 | 9,988 | |
724 | Dense Subsequence | [
"data structures",
"greedy",
"strings"
] | null | null | You are given a string *s*, consisting of lowercase English letters, and the integer *m*.
One should choose some symbols from the given string so that any contiguous subsegment of length *m* has at least one selected symbol. Note that here we choose positions of symbols, not the symbols themselves.
Then one uses the chosen symbols to form a new string. All symbols from the chosen position should be used, but we are allowed to rearrange them in any order.
Formally, we choose a subsequence of indices 1<=β€<=*i*1<=<<=*i*2<=<<=...<=<<=*i**t*<=β€<=|*s*|. The selected sequence must meet the following condition: for every *j* such that 1<=β€<=*j*<=β€<=|*s*|<=-<=*m*<=+<=1, there must be at least one selected index that belongs to the segment [*j*,<=Β *j*<=+<=*m*<=-<=1], i.e. there should exist a *k* from 1 to *t*, such that *j*<=β€<=*i**k*<=β€<=*j*<=+<=*m*<=-<=1.
Then we take any permutation *p* of the selected indices and form a new string *s**i**p*1*s**i**p*2... *s**i**p**t*.
Find the lexicographically smallest string, that can be obtained using this procedure. | The first line of the input contains a single integer *m* (1<=β€<=*m*<=β€<=100<=000).
The second line contains the string *s* consisting of lowercase English letters. It is guaranteed that this string is non-empty and its length doesn't exceed 100<=000. It is also guaranteed that the number *m* doesn't exceed the length of the string *s*. | Print the single line containing the lexicographically smallest string, that can be obtained using the procedure described above. | [
"3\ncbabc\n",
"2\nabcab\n",
"3\nbcabcbaccba\n"
] | [
"a\n",
"aab\n",
"aaabb\n"
] | In the first sample, one can choose the subsequence {3} and form a string "a".
In the second sample, one can choose the subsequence {1,β2,β4} (symbols on this positions are 'a', 'b' and 'a') and rearrange the chosen symbols to form a string "aab". | [
{
"input": "3\ncbabc",
"output": "a"
},
{
"input": "2\nabcab",
"output": "aab"
},
{
"input": "3\nbcabcbaccba",
"output": "aaabb"
},
{
"input": "5\nimmaydobun",
"output": "ab"
},
{
"input": "5\nwjjdqawypvtgrncmqvcsergermprauyevcegjtcrrblkwiugrcjfpjyxngyryxntauxlouv... | 46 | 0 | 0 | 10,000 | |
792 | Divide by Three | [
"dp",
"greedy",
"math",
"number theory"
] | null | null | A positive integer number *n* is written on a blackboard. It consists of not more than 105 digits. You have to transform it into a beautiful number by erasing some of the digits, and you want to erase as few digits as possible.
The number is called beautiful if it consists of at least one digit, doesn't have leading zeroes and is a multiple of 3. For example, 0, 99, 10110 are beautiful numbers, and 00, 03, 122 are not.
Write a program which for the given *n* will find a beautiful number such that *n* can be transformed into this number by erasing as few digits as possible. You can erase an arbitraty set of digits. For example, they don't have to go one after another in the number *n*.
If it's impossible to obtain a beautiful number, print -1. If there are multiple answers, print any of them. | The first line of input contains *n* β a positive integer number without leading zeroes (1<=β€<=*n*<=<<=10100000). | Print one number β any beautiful number obtained by erasing as few as possible digits. If there is no answer, print <=-<=1. | [
"1033\n",
"10\n",
"11\n"
] | [
"33\n",
"0\n",
"-1\n"
] | In the first example it is enough to erase only the first digit to obtain a multiple of 3. But if we erase the first digit, then we obtain a number with a leading zero. So the minimum number of digits to be erased is two. | [
{
"input": "1033",
"output": "33"
},
{
"input": "10",
"output": "0"
},
{
"input": "11",
"output": "-1"
},
{
"input": "3",
"output": "3"
},
{
"input": "1",
"output": "-1"
},
{
"input": "117",
"output": "117"
},
{
"input": "518",
"output"... | 140 | 0 | 0 | 10,018 | |
935 | Fafa and the Gates | [
"implementation"
] | null | null | Two neighboring kingdoms decided to build a wall between them with some gates to enable the citizens to go from one kingdom to another. Each time a citizen passes through a gate, he has to pay one silver coin.
The world can be represented by the first quadrant of a plane and the wall is built along the identity line (i.e. the line with the equation *x*<==<=*y*). Any point below the wall belongs to the first kingdom while any point above the wall belongs to the second kingdom. There is a gate at any integer point on the line (i.e. at points (0,<=0), (1,<=1), (2,<=2), ...). The wall and the gates do not belong to any of the kingdoms.
Fafa is at the gate at position (0,<=0) and he wants to walk around in the two kingdoms. He knows the sequence *S* of moves he will do. This sequence is a string where each character represents a move. The two possible moves Fafa will do are 'U' (move one step up, from (*x*,<=*y*) to (*x*,<=*y*<=+<=1)) and 'R' (move one step right, from (*x*,<=*y*) to (*x*<=+<=1,<=*y*)).
Fafa wants to know the number of silver coins he needs to pay to walk around the two kingdoms following the sequence *S*. Note that if Fafa visits a gate without moving from one kingdom to another, he pays no silver coins. Also assume that he doesn't pay at the gate at point (0,<=0), i.Β e. he is initially on the side he needs. | The first line of the input contains single integer *n* (1<=β€<=*n*<=β€<=105) β the number of moves in the walking sequence.
The second line contains a string *S* of length *n* consisting of the characters 'U' and 'R' describing the required moves. Fafa will follow the sequence *S* in order from left to right. | On a single line, print one integer representing the number of silver coins Fafa needs to pay at the gates to follow the sequence *S*. | [
"1\nU\n",
"6\nRURUUR\n",
"7\nURRRUUU\n"
] | [
"0\n",
"1\n",
"2\n"
] | The figure below describes the third sample. The red arrows represent the sequence of moves Fafa will follow. The green gates represent the gates at which Fafa have to pay silver coins. | [
{
"input": "1\nU",
"output": "0"
},
{
"input": "6\nRURUUR",
"output": "1"
},
{
"input": "7\nURRRUUU",
"output": "2"
},
{
"input": "100\nRUURUURRUURUUUUURRUUURRRRUURRURRURRRRUUUUUURRUURRRRURUUURUURURRRRRURUURRUURUURRUUURUUUUUURRUUUURUUUR",
"output": "3"
},
{
"input... | 46 | 0 | 0 | 10,032 | |
901 | GCD of Polynomials | [
"constructive algorithms",
"math"
] | null | null | Suppose you have two polynomials and . Then polynomial can be uniquely represented in the following way:
This can be done using [long division](https://en.wikipedia.org/wiki/Polynomial_long_division). Here, denotes the degree of polynomial *P*(*x*). is called the remainder of division of polynomial by polynomial , it is also denoted as .
Since there is a way to divide polynomials with remainder, we can define Euclid's algorithm of finding the greatest common divisor of two polynomials. The algorithm takes two polynomials . If the polynomial is zero, the result is , otherwise the result is the value the algorithm returns for pair . On each step the degree of the second argument decreases, so the algorithm works in finite number of steps. But how large that number could be? You are to answer this question.
You are given an integer *n*. You have to build two polynomials with degrees not greater than *n*, such that their coefficients are integers not exceeding 1 by their absolute value, the leading coefficients (ones with the greatest power of *x*) are equal to one, and the described Euclid's algorithm performs exactly *n* steps finding their greatest common divisor. Moreover, the degree of the first polynomial should be greater than the degree of the second. By a step of the algorithm we mean the transition from pair to pair . | You are given a single integer *n* (1<=β€<=*n*<=β€<=150)Β β the number of steps of the algorithm you need to reach. | Print two polynomials in the following format.
In the first line print a single integer *m* (0<=β€<=*m*<=β€<=*n*)Β β the degree of the polynomial.
In the second line print *m*<=+<=1 integers between <=-<=1 and 1Β β the coefficients of the polynomial, from constant to leading.
The degree of the first polynomial should be greater than the degree of the second polynomial, the leading coefficients should be equal to 1. Euclid's algorithm should perform exactly *n* steps when called using these polynomials.
If there is no answer for the given *n*, print -1.
If there are multiple answer, print any of them. | [
"1\n",
"2\n"
] | [
"1\n0 1\n0\n1\n",
"2\n-1 0 1\n1\n0 1\n"
] | In the second example you can print polynomials *x*<sup class="upper-index">2</sup>β-β1 and *x*. The sequence of transitions is
There are two steps in it. | [
{
"input": "1",
"output": "1\n0 1\n0\n1"
},
{
"input": "2",
"output": "2\n-1 0 1\n1\n0 1"
},
{
"input": "3",
"output": "3\n0 0 0 1\n2\n-1 0 1"
},
{
"input": "4",
"output": "4\n1 0 -1 0 1\n3\n0 0 0 1"
},
{
"input": "5",
"output": "5\n0 1 0 0 0 1\n4\n1 0 -1 0 1"... | 124 | 0 | 3 | 10,094 | |
630 | Challenge Pennants | [
"combinatorics",
"math"
] | null | null | Because of budget cuts one IT company established new non-financial reward system instead of bonuses.
Two kinds of actions are rewarded: fixing critical bugs and suggesting new interesting features. A man who fixed a critical bug gets "I fixed a critical bug" pennant on his table. A man who suggested a new interesting feature gets "I suggested a new feature" pennant on his table.
Because of the limited budget of the new reward system only 5 "I fixed a critical bug" pennants and 3 "I suggested a new feature" pennants were bought.
In order to use these pennants for a long time they were made challenge ones. When a man fixes a new critical bug one of the earlier awarded "I fixed a critical bug" pennants is passed on to his table. When a man suggests a new interesting feature one of the earlier awarded "I suggested a new feature" pennants is passed on to his table.
One man can have several pennants of one type and of course he can have pennants of both types on his table. There are *n* tables in the IT company. Find the number of ways to place the pennants on these tables given that each pennant is situated on one of the tables and each table is big enough to contain any number of pennants. | The only line of the input contains one integer *n* (1<=β€<=*n*<=β€<=500) β the number of tables in the IT company. | Output one integer β the amount of ways to place the pennants on *n* tables. | [
"2\n"
] | [
"24"
] | none | [
{
"input": "2",
"output": "24"
},
{
"input": "1",
"output": "1"
},
{
"input": "3",
"output": "210"
},
{
"input": "4",
"output": "1120"
},
{
"input": "5",
"output": "4410"
},
{
"input": "6",
"output": "14112"
},
{
"input": "7",
"output":... | 30 | 0 | 0 | 10,096 | |
427 | Match & Catch | [
"dp",
"string suffix structures",
"strings"
] | null | null | Police headquarter is monitoring signal on different frequency levels. They have got two suspiciously encoded strings *s*1 and *s*2 from two different frequencies as signals. They are suspecting that these two strings are from two different criminals and they are planning to do some evil task.
Now they are trying to find a common substring of minimum length between these two strings. The substring must occur only once in the first string, and also it must occur only once in the second string.
Given two strings *s*1 and *s*2 consist of lowercase Latin letters, find the smallest (by length) common substring *p* of both *s*1 and *s*2, where *p* is a unique substring in *s*1 and also in *s*2. See notes for formal definition of substring and uniqueness. | The first line of input contains *s*1 and the second line contains *s*2 (1<=β€<=|*s*1|,<=|*s*2|<=β€<=5000). Both strings consist of lowercase Latin letters. | Print the length of the smallest common unique substring of *s*1 and *s*2. If there are no common unique substrings of *s*1 and *s*2 print -1. | [
"apple\npepperoni\n",
"lover\ndriver\n",
"bidhan\nroy\n",
"testsetses\nteeptes\n"
] | [
"2\n",
"1\n",
"-1\n",
"3\n"
] | Imagine we have string *a*β=β*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">|*a*|</sub>, where |*a*| is the length of string *a*, and *a*<sub class="lower-index">*i*</sub> is the *i*<sup class="upper-index">*th*</sup> letter of the string.
We will call string *a*<sub class="lower-index">*l*</sub>*a*<sub class="lower-index">*l*β+β1</sub>*a*<sub class="lower-index">*l*β+β2</sub>...*a*<sub class="lower-index">*r*</sub> (1ββ€β*l*ββ€β*r*ββ€β|*a*|) the substring [*l*,β*r*] of the string *a*.
The substring [*l*,β*r*] is unique in *a* if and only if there is no pair *l*<sub class="lower-index">1</sub>,β*r*<sub class="lower-index">1</sub> such that *l*<sub class="lower-index">1</sub>ββ β*l* and the substring [*l*<sub class="lower-index">1</sub>,β*r*<sub class="lower-index">1</sub>] is equal to the substring [*l*,β*r*] in *a*. | [] | 1,000 | 10,342,400 | 0 | 10,150 | |
174 | File List | [
"dp",
"greedy",
"implementation"
] | null | null | Eudokimus, a system administrator is in trouble again. As a result of an error in some script, a list of names of very important files has been damaged. Since they were files in the BerFS file system, it is known that each file name has a form "name.ext", where:
- name is a string consisting of lowercase Latin letters, its length is from 1 to 8 characters; - ext is a string consisting of lowercase Latin letters, its length is from 1 to 3 characters.
For example, "read.me", "example.txt" and "b.cpp" are valid file names and "version.info", "ntldr" and "contestdata.zip" are not.
Damage to the list meant that all the file names were recorded one after another, without any separators. So now Eudokimus has a single string.
Eudokimus needs to set everything right as soon as possible. He should divide the resulting string into parts so that each part would be a valid file name in BerFS. Since Eudokimus has already proved that he is not good at programming, help him. The resulting file list can contain the same file names. | The input data consists of a single string *s*, its length is from 1 to 4Β·105 characters. The string can contain only lowercase Latin letters ('a' - 'z') and periods ('.'). | In the first line print "YES" (without the quotes), if it is possible to divide *s* into parts as required. In this case, the following lines should contain the parts of the required partition, one per line in the order in which they appear in *s*. The required partition can contain the same file names. If there are multiple solutions, print any of them.
If the solution does not exist, then print in a single line "NO" (without the quotes). | [
"read.meexample.txtb.cpp\n",
"version.infontldrcontestdata.zip\n"
] | [
"YES\nread.m\neexample.t\nxtb.cpp\n",
"NO\n"
] | none | [
{
"input": "read.meexample.txtb.cpp",
"output": "YES\nread.m\neexample.t\nxtb.cpp"
},
{
"input": "version.infontldrcontestdata.zip",
"output": "NO"
},
{
"input": "thisis.text.txt",
"output": "YES\nthisis.t\next.txt"
},
{
"input": "oops.t",
"output": "YES\noops.t"
},
{... | 592 | 10,240,000 | 3 | 10,168 | |
95 | Hockey | [
"implementation",
"strings"
] | A. Hockey | 2 | 256 | Petya loves hockey very much. One day, as he was watching a hockey match, he fell asleep. Petya dreamt of being appointed to change a hockey team's name. Thus, Petya was given the original team name *w* and the collection of forbidden substrings *s*1,<=*s*2,<=...,<=*s**n*. All those strings consist of uppercase and lowercase Latin letters. String *w* has the length of |*w*|, its characters are numbered from 1 to |*w*|.
First Petya should find all the occurrences of forbidden substrings in the *w* string. During the search of substrings the case of letter shouldn't be taken into consideration. That is, strings "aBC" and "ABc" are considered equal.
After that Petya should perform the replacement of all letters covered by the occurrences. More formally: a letter in the position *i* should be replaced by any other one if for position *i* in string *w* there exist pair of indices *l*,<=*r* (1<=β€<=*l*<=β€<=*i*<=β€<=*r*<=β€<=|*w*|) such that substring *w*[*l*Β ...Β *r*] is contained in the collection *s*1,<=*s*2,<=...,<=*s**n*, when using case insensitive comparison. During the replacement the letter's case should remain the same. Petya is not allowed to replace the letters that aren't covered by any forbidden substring.
Letter *letter* (uppercase or lowercase) is considered lucky for the hockey players. That's why Petya should perform the changes so that the *letter* occurred in the resulting string as many times as possible. Help Petya to find such resulting string. If there are several such strings, find the one that comes first lexicographically.
Note that the process of replacements is not repeated, it occurs only once. That is, if after Petya's replacements the string started to contain new occurrences of bad substrings, Petya pays no attention to them. | The first line contains the only integer *n* (1<=β€<=*n*<=β€<=100) β the number of forbidden substrings in the collection. Next *n* lines contain these substrings. The next line contains string *w*. All those *n*<=+<=1 lines are non-empty strings consisting of uppercase and lowercase Latin letters whose length does not exceed 100. The last line contains a lowercase letter *letter*. | Output the only line β Petya's resulting string with the maximum number of letters *letter*. If there are several answers then output the one that comes first lexicographically.
The lexicographical comparison is performed by the standard < operator in modern programming languages. The line *a* is lexicographically smaller than the line *b*, if *a* is a prefix of *b*, or there exists such an *i* (1<=β€<=*i*<=β€<=|*a*|), that *a**i*<=<<=*b**i*, and for any *j* (1<=β€<=*j*<=<<=*i*) *a**j*<==<=*b**j*. |*a*| stands for the length of string *a*. | [
"3\nbers\nucky\nelu\nPetrLoveLuckyNumbers\nt\n",
"4\nhello\nparty\nabefglghjdhfgj\nIVan\npetrsmatchwin\na\n",
"2\naCa\ncba\nabAcaba\nc\n"
] | [
"PetrLovtTttttNumtttt\n",
"petrsmatchwin\n",
"abCacba\n"
] | none | [
{
"input": "3\nbers\nucky\nelu\nPetrLoveLuckyNumbers\nt",
"output": "PetrLovtTttttNumtttt"
},
{
"input": "4\nhello\nparty\nabefglghjdhfgj\nIVan\npetrsmatchwin\na",
"output": "petrsmatchwin"
},
{
"input": "2\naCa\ncba\nabAcaba\nc",
"output": "abCacba"
},
{
"input": "3\nlol\nhe... | 216 | 0 | 0 | 10,170 |
271 | Prime Matrix | [
"binary search",
"brute force",
"math",
"number theory"
] | null | null | You've got an *n*<=Γ<=*m* matrix. The matrix consists of integers. In one move, you can apply a single transformation to the matrix: choose an arbitrary element of the matrix and increase it by 1. Each element can be increased an arbitrary number of times.
You are really curious about prime numbers. Let us remind you that a prime number is a positive integer that has exactly two distinct positive integer divisors: itself and number one. For example, numbers 2, 3, 5 are prime and numbers 1, 4, 6 are not.
A matrix is prime if at least one of the two following conditions fulfills:
- the matrix has a row with prime numbers only; - the matrix has a column with prime numbers only;
Your task is to count the minimum number of moves needed to get a prime matrix from the one you've got. | The first line contains two integers *n*,<=*m* (1<=β€<=*n*,<=*m*<=β€<=500) β the number of rows and columns in the matrix, correspondingly.
Each of the following *n* lines contains *m* integers β the initial matrix. All matrix elements are positive integers. All numbers in the initial matrix do not exceed 105.
The numbers in the lines are separated by single spaces. | Print a single integer β the minimum number of moves needed to get a prime matrix from the one you've got. If you've got a prime matrix, print 0. | [
"3 3\n1 2 3\n5 6 1\n4 4 1\n",
"2 3\n4 8 8\n9 2 9\n",
"2 2\n1 3\n4 2\n"
] | [
"1\n",
"3\n",
"0\n"
] | In the first sample you need to increase number 1 in cell (1, 1). Thus, the first row will consist of prime numbers: 2, 2, 3.
In the second sample you need to increase number 8 in cell (1, 2) three times. Thus, the second column will consist of prime numbers: 11, 2.
In the third sample you don't have to do anything as the second column already consists of prime numbers: 3, 2. | [
{
"input": "3 3\n1 2 3\n5 6 1\n4 4 1",
"output": "1"
},
{
"input": "2 3\n4 8 8\n9 2 9",
"output": "3"
},
{
"input": "2 2\n1 3\n4 2",
"output": "0"
},
{
"input": "1 1\n14",
"output": "3"
},
{
"input": "5 3\n2 14 8\n8 8 2\n8 10 10\n1 2 1\n100 100 8",
"output": "... | 62 | 0 | 0 | 10,171 | |
928 | Autocompletion | [
"*special",
"strings",
"trees"
] | null | null | Arcady is a copywriter. His today's task is to type up an already well-designed story using his favorite text editor.
Arcady types words, punctuation signs and spaces one after another. Each letter and each sign (including line feed) requires one keyboard click in order to be printed. Moreover, when Arcady has a non-empty prefix of some word on the screen, the editor proposes a possible autocompletion for this word, more precisely one of the already printed words such that its prefix matches the currently printed prefix if this word is unique. For example, if Arcady has already printed Β«codeforcesΒ», Β«codingΒ» and Β«codeforcesΒ» once again, then there will be no autocompletion attempt for Β«codΒ», but if he proceeds with Β«codeΒ», the editor will propose Β«codeforcesΒ».
With a single click Arcady can follow the editor's proposal, i.e. to transform the current prefix to it. Note that no additional symbols are printed after the autocompletion (no spaces, line feeds, etc). What is the minimum number of keyboard clicks Arcady has to perform to print the entire text, if he is not allowed to move the cursor or erase the already printed symbols?
A word here is a contiguous sequence of latin letters bordered by spaces, punctuation signs and line/text beginnings/ends. Arcady uses only lowercase letters. For example, there are 20 words in Β«it's well-known that tic-tac-toe is a paper-and-pencil game for two players, x and o.Β». | The only line contains Arcady's text, consisting only of lowercase latin letters, spaces, line feeds and the following punctuation signs: Β«.Β», Β«,Β», Β«?Β», Β«!Β», Β«'Β» and Β«-Β». The total amount of symbols doesn't exceed 3Β·105. It's guaranteed that all lines are non-empty. | Print a single integer β the minimum number of clicks. | [
"snow affects sports such as skiing, snowboarding, and snowmachine travel.\nsnowboarding is a recreational activity and olympic and paralympic sport.\n",
"'co-co-co, codeforces?!'\n",
"thun-thun-thunder, thunder, thunder\nthunder, thun-, thunder\nthun-thun-thunder, thunder\nthunder, feel the thunder\nlightning ... | [
"141\n",
"25\n",
"183\n"
] | In sample case one it's optimal to use autocompletion for the first instance of Β«snowboardingΒ» after typing up Β«snΒ» and for the second instance of Β«snowboardingΒ» after typing up Β«snowbΒ». This will save 7 clicks.
In sample case two it doesn't matter whether to use autocompletion or not. | [
{
"input": "snow affects sports such as skiing, snowboarding, and snowmachine travel.\nsnowboarding is a recreational activity and olympic and paralympic sport.",
"output": "141"
},
{
"input": "'co-co-co, codeforces?!'",
"output": "25"
},
{
"input": "thun-thun-thunder, thunder, thunder\n... | 46 | 5,632,000 | -1 | 10,172 | |
730 | Bottles | [
"dp"
] | null | null | Nick has *n* bottles of soda left after his birthday. Each bottle is described by two values: remaining amount of soda *a**i* and bottle volume *b**i* (*a**i*<=β€<=*b**i*).
Nick has decided to pour all remaining soda into minimal number of bottles, moreover he has to do it as soon as possible. Nick spends *x* seconds to pour *x* units of soda from one bottle to another.
Nick asks you to help him to determine *k* β the minimal number of bottles to store all remaining soda and *t* β the minimal time to pour soda into *k* bottles. A bottle can't store more soda than its volume. All remaining soda should be saved. | The first line contains positive integer *n* (1<=β€<=*n*<=β€<=100) β the number of bottles.
The second line contains *n* positive integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=100), where *a**i* is the amount of soda remaining in the *i*-th bottle.
The third line contains *n* positive integers *b*1,<=*b*2,<=...,<=*b**n* (1<=β€<=*b**i*<=β€<=100), where *b**i* is the volume of the *i*-th bottle.
It is guaranteed that *a**i*<=β€<=*b**i* for any *i*. | The only line should contain two integers *k* and *t*, where *k* is the minimal number of bottles that can store all the soda and *t* is the minimal time to pour the soda into *k* bottles. | [
"4\n3 3 4 3\n4 7 6 5\n",
"2\n1 1\n100 100\n",
"5\n10 30 5 6 24\n10 41 7 8 24\n"
] | [
"2 6\n",
"1 1\n",
"3 11\n"
] | In the first example Nick can pour soda from the first bottle to the second bottle. It will take 3 seconds. After it the second bottle will contain 3β+β3β=β6 units of soda. Then he can pour soda from the fourth bottle to the second bottle and to the third bottle: one unit to the second and two units to the third. It will take 1β+β2β=β3 seconds. So, all the soda will be in two bottles and he will spend 3β+β3β=β6 seconds to do it. | [
{
"input": "4\n3 3 4 3\n4 7 6 5",
"output": "2 6"
},
{
"input": "2\n1 1\n100 100",
"output": "1 1"
},
{
"input": "5\n10 30 5 6 24\n10 41 7 8 24",
"output": "3 11"
},
{
"input": "1\n1\n100",
"output": "1 0"
},
{
"input": "1\n100\n100",
"output": "1 0"
},
{
... | 140 | 37,171,200 | 0 | 10,183 | |
445 | DZY Loves Chemistry | [
"dfs and similar",
"dsu",
"greedy"
] | null | null | DZY loves chemistry, and he enjoys mixing chemicals.
DZY has *n* chemicals, and *m* pairs of them will react. He wants to pour these chemicals into a test tube, and he needs to pour them in one by one, in any order.
Let's consider the danger of a test tube. Danger of an empty test tube is 1. And every time when DZY pours a chemical, if there are already one or more chemicals in the test tube that can react with it, the danger of the test tube will be multiplied by 2. Otherwise the danger remains as it is.
Find the maximum possible danger after pouring all the chemicals one by one in optimal order. | The first line contains two space-separated integers *n* and *m* .
Each of the next *m* lines contains two space-separated integers *x**i* and *y**i* (1<=β€<=*x**i*<=<<=*y**i*<=β€<=*n*). These integers mean that the chemical *x**i* will react with the chemical *y**i*. Each pair of chemicals will appear at most once in the input.
Consider all the chemicals numbered from 1 to *n* in some order. | Print a single integer β the maximum possible danger. | [
"1 0\n",
"2 1\n1 2\n",
"3 2\n1 2\n2 3\n"
] | [
"1\n",
"2\n",
"4\n"
] | In the first sample, there's only one way to pour, and the danger won't increase.
In the second sample, no matter we pour the 1st chemical first, or pour the 2nd chemical first, the answer is always 2.
In the third sample, there are four ways to achieve the maximum possible danger: 2-1-3, 2-3-1, 1-2-3 and 3-2-1 (that is the numbers of the chemicals in order of pouring). | [
{
"input": "1 0",
"output": "1"
},
{
"input": "2 1\n1 2",
"output": "2"
},
{
"input": "3 2\n1 2\n2 3",
"output": "4"
},
{
"input": "10 10\n1 8\n4 10\n4 6\n5 10\n2 3\n1 7\n3 4\n3 6\n6 9\n3 7",
"output": "512"
},
{
"input": "20 20\n6 8\n13 20\n7 13\n6 17\n5 15\n1 12... | 46 | 307,200 | 0 | 10,237 | |
148 | Bag of mice | [
"dp",
"games",
"math",
"probabilities"
] | null | null | The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance.
They take turns drawing a mouse from a bag which initially contains *w* white and *b* black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning?
If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one. | The only line of input data contains two integers *w* and *b* (0<=β€<=*w*,<=*b*<=β€<=1000). | Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10<=-<=9. | [
"1 3\n",
"5 5\n"
] | [
"0.500000000\n",
"0.658730159\n"
] | Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag β one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins. | [
{
"input": "1 3",
"output": "0.500000000"
},
{
"input": "5 5",
"output": "0.658730159"
},
{
"input": "100 100",
"output": "0.666295063"
},
{
"input": "100 1",
"output": "0.990099010"
},
{
"input": "1 100",
"output": "0.336633663"
},
{
"input": "67 420"... | 154 | 614,400 | 0 | 10,242 | |
356 | Xenia and Hamming | [
"implementation",
"math"
] | null | null | Xenia is an amateur programmer. Today on the IT lesson she learned about the Hamming distance.
The Hamming distance between two strings *s*<==<=*s*1*s*2... *s**n* and *t*<==<=*t*1*t*2... *t**n* of equal length *n* is value . Record [*s**i*<=β <=*t**i*] is the Iverson notation and represents the following: if *s**i*<=β <=*t**i*, it is one, otherwise β zero.
Now Xenia wants to calculate the Hamming distance between two long strings *a* and *b*. The first string *a* is the concatenation of *n* copies of string *x*, that is, . The second string *b* is the concatenation of *m* copies of string *y*.
Help Xenia, calculate the required Hamming distance, given *n*,<=*x*,<=*m*,<=*y*. | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=1012). The second line contains a non-empty string *x*. The third line contains a non-empty string *y*. Both strings consist of at most 106 lowercase English letters.
It is guaranteed that strings *a* and *b* that you obtain from the input have the same length. | Print a single integer β the required Hamming distance.
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. | [
"100 10\na\naaaaaaaaaa\n",
"1 1\nabacaba\nabzczzz\n",
"2 3\nrzr\naz\n"
] | [
"0\n",
"4\n",
"5\n"
] | In the first test case string *a* is the same as string *b* and equals 100 letters a. As both strings are equal, the Hamming distance between them is zero.
In the second test case strings *a* and *b* differ in their 3-rd, 5-th, 6-th and 7-th characters. Thus, the Hamming distance equals 4.
In the third test case string *a* is rzrrzr and string *b* is azazaz. The strings differ in all characters apart for the second one, the Hamming distance between them equals 5. | [] | 1,000 | 13,619,200 | 0 | 10,261 | |
0 | none | [
"none"
] | null | null | Petya loves hockey very much. One day, as he was watching a hockey match, he fell asleep. Petya dreamt of being appointed to change a hockey team's name. Thus, Petya was given the original team name *w* and the collection of forbidden substrings *s*1,<=*s*2,<=...,<=*s**n*. All those strings consist of uppercase and lowercase Latin letters. String *w* has the length of |*w*|, its characters are numbered from 1 to |*w*|.
First Petya should find all the occurrences of forbidden substrings in the *w* string. During the search of substrings the case of letter shouldn't be taken into consideration. That is, strings "aBC" and "ABc" are considered equal.
After that Petya should perform the replacement of all letters covered by the occurrences. More formally: a letter in the position *i* should be replaced by any other one if for position *i* in string *w* there exist pair of indices *l*,<=*r* (1<=β€<=*l*<=β€<=*i*<=β€<=*r*<=β€<=|*w*|) such that substring *w*[*l*Β ...Β *r*] is contained in the collection *s*1,<=*s*2,<=...,<=*s**n*, when using case insensitive comparison. During the replacement the letter's case should remain the same. Petya is not allowed to replace the letters that aren't covered by any forbidden substring.
Letter *letter* (uppercase or lowercase) is considered lucky for the hockey players. That's why Petya should perform the changes so that the *letter* occurred in the resulting string as many times as possible. Help Petya to find such resulting string. If there are several such strings, find the one that comes first lexicographically.
Note that the process of replacements is not repeated, it occurs only once. That is, if after Petya's replacements the string started to contain new occurrences of bad substrings, Petya pays no attention to them. | The first line contains the only integer *n* (1<=β€<=*n*<=β€<=100) β the number of forbidden substrings in the collection. Next *n* lines contain these substrings. The next line contains string *w*. All those *n*<=+<=1 lines are non-empty strings consisting of uppercase and lowercase Latin letters whose length does not exceed 100. The last line contains a lowercase letter *letter*. | Output the only line β Petya's resulting string with the maximum number of letters *letter*. If there are several answers then output the one that comes first lexicographically.
The lexicographical comparison is performed by the standard < operator in modern programming languages. The line *a* is lexicographically smaller than the line *b*, if *a* is a prefix of *b*, or there exists such an *i* (1<=β€<=*i*<=β€<=|*a*|), that *a**i*<=<<=*b**i*, and for any *j* (1<=β€<=*j*<=<<=*i*) *a**j*<==<=*b**j*. |*a*| stands for the length of string *a*. | [
"3\nbers\nucky\nelu\nPetrLoveLuckyNumbers\nt\n",
"4\nhello\nparty\nabefglghjdhfgj\nIVan\npetrsmatchwin\na\n",
"2\naCa\ncba\nabAcaba\nc\n"
] | [
"PetrLovtTttttNumtttt\n",
"petrsmatchwin\n",
"abCacba\n"
] | none | [] | 92 | 0 | 0 | 10,275 | |
0 | none | [
"none"
] | null | null | We'll call an array of *n* non-negative integers *a*[1],<=*a*[2],<=...,<=*a*[*n*] interesting, if it meets *m* constraints. The *i*-th of the *m* constraints consists of three integers *l**i*, *r**i*, *q**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*n*) meaning that value should be equal to *q**i*.
Your task is to find any interesting array of *n* elements or state that such array doesn't exist.
Expression *x*&*y* means the bitwise AND of numbers *x* and *y*. In programming languages C++, Java and Python this operation is represented as "&", in Pascal β as "and". | The first line contains two integers *n*, *m* (1<=β€<=*n*<=β€<=105, 1<=β€<=*m*<=β€<=105)Β β the number of elements in the array and the number of limits.
Each of the next *m* lines contains three integers *l**i*, *r**i*, *q**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*n*, 0<=β€<=*q**i*<=<<=230) describing the *i*-th limit. | If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print *n* integers *a*[1],<=*a*[2],<=...,<=*a*[*n*] (0<=β€<=*a*[*i*]<=<<=230)Β decribing the interesting array. If there are multiple answers, print any of them.
If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. | [
"3 1\n1 3 3\n",
"3 2\n1 3 3\n1 3 2\n"
] | [
"YES\n3 3 3\n",
"NO\n"
] | none | [
{
"input": "3 1\n1 3 3",
"output": "YES\n3 3 3"
},
{
"input": "3 2\n1 3 3\n1 3 2",
"output": "NO"
},
{
"input": "3 2\n1 2 536870912\n2 3 536870911",
"output": "YES\n536870912 1073741823 536870911"
},
{
"input": "1 1\n1 1 10",
"output": "YES\n10"
},
{
"input": "1 2... | 109 | 12,595,200 | 0 | 10,284 | |
226 | Flying Saucer Segments | [
"math"
] | null | null | An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!).
The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the 2-nd one, the 2-nd one β to the 1-st and the 3-rd ones, the 3-rd one β only to the 2-nd one. The transitions are possible only between the adjacent sections.
The spacecraft team consists of *n* aliens. Each of them is given a rank β an integer from 1 to *n*. The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section *a* to section *b* only if it is senior in rank to all aliens who are in the segments *a* and *b* (besides, the segments *a* and *b* are of course required to be adjacent). Any alien requires exactly 1 minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship.
Alien *A* is senior in rank to alien *B*, if the number indicating rank *A*, is more than the corresponding number for *B*.
At the moment the whole saucer team is in the 3-rd segment. They all need to move to the 1-st segment. One member of the crew, the alien with the identification number CFR-140, decided to calculate the minimum time (in minutes) they will need to perform this task.
Help CFR-140, figure out the minimum time (in minutes) that all the astronauts will need to move from the 3-rd segment to the 1-st one. Since this number can be rather large, count it modulo *m*. | The first line contains two space-separated integers: *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=109) β the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly. | Print a single number β the answer to the problem modulo *m*. | [
"1 10\n",
"3 8\n"
] | [
"2\n",
"2\n"
] | In the first sample the only crew member moves from segment 3 to segment 2, and then from segment 2 to segment 1 without any problems. Thus, the whole moving will take two minutes.
To briefly describe the movements in the second sample we will use value <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4c7c8e716067e9c6251e8ca82a4ca7fde74fbacb.png" style="max-width: 100.0%;max-height: 100.0%;"/>, which would correspond to an alien with rank *i* moving from the segment in which it is at the moment, to the segment number *j*. Using these values, we will describe the movements between the segments in the second sample: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7668900e59f9bc3ccc78b0a77dca322fcb02fa04.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/1adb8798904e42944c35bd49feff02db6c3ea10b.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/954e2f634474269f53df1edbf2e7b214d8a2611c.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7668900e59f9bc3ccc78b0a77dca322fcb02fa04.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/be5f59dacfc6f7ded42f8f260c7b7aedf17ec1f0.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/d4fd9e68a9c6a277942eb188291d6d2744ea21d3.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7668900e59f9bc3ccc78b0a77dca322fcb02fa04.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/1adb8798904e42944c35bd49feff02db6c3ea10b.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/b73a9870e1b41a5e048c3ab3e3fd4b92c336c9ec.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7668900e59f9bc3ccc78b0a77dca322fcb02fa04.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/be5f59dacfc6f7ded42f8f260c7b7aedf17ec1f0.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/954e2f634474269f53df1edbf2e7b214d8a2611c.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7668900e59f9bc3ccc78b0a77dca322fcb02fa04.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/1adb8798904e42944c35bd49feff02db6c3ea10b.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/49b1ffd4dcd2e0da0acec04559e0c3efc7854b07.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7668900e59f9bc3ccc78b0a77dca322fcb02fa04.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/be5f59dacfc6f7ded42f8f260c7b7aedf17ec1f0.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ef8b3f32ee76c86f57fa63f7251fa290642f17f8.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7668900e59f9bc3ccc78b0a77dca322fcb02fa04.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/1adb8798904e42944c35bd49feff02db6c3ea10b.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/954e2f634474269f53df1edbf2e7b214d8a2611c.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7668900e59f9bc3ccc78b0a77dca322fcb02fa04.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/be5f59dacfc6f7ded42f8f260c7b7aedf17ec1f0.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/d4fd9e68a9c6a277942eb188291d6d2744ea21d3.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7668900e59f9bc3ccc78b0a77dca322fcb02fa04.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/1adb8798904e42944c35bd49feff02db6c3ea10b.png" style="max-width: 100.0%;max-height: 100.0%;"/>; In total: the aliens need 26 moves. The remainder after dividing 26 by 8 equals 2, so the answer to this test is 2. | [
{
"input": "1 10",
"output": "2"
},
{
"input": "3 8",
"output": "2"
},
{
"input": "8 12",
"output": "8"
},
{
"input": "4 84",
"output": "80"
},
{
"input": "9 95",
"output": "17"
},
{
"input": "331358794 820674098",
"output": "2619146"
},
{
... | 62 | 0 | 0 | 10,289 | |
630 | Selection of Personnel | [
"combinatorics",
"math"
] | null | null | One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received *n* resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate. | The only line of the input contains one integer *n* (7<=β€<=*n*<=β€<=777) β the number of potential employees that sent resumes. | Output one integer β the number of different variants of group composition. | [
"7\n"
] | [
"29"
] | none | [
{
"input": "7",
"output": "29"
},
{
"input": "8",
"output": "92"
},
{
"input": "9",
"output": "246"
},
{
"input": "10",
"output": "582"
},
{
"input": "321",
"output": "66715035255088"
},
{
"input": "624",
"output": "7147161340917624"
},
{
"... | 77 | 1,536,000 | 3 | 10,296 | |
464 | Restore Cube | [
"brute force",
"geometry"
] | null | null | Peter had a cube with non-zero length of a side. He put the cube into three-dimensional space in such a way that its vertices lay at integer points (it is possible that the cube's sides are not parallel to the coordinate axes). Then he took a piece of paper and wrote down eight lines, each containing three integers β coordinates of cube's vertex (a single line contains coordinates of a single vertex, each vertex is written exactly once), put the paper on the table and left. While Peter was away, his little brother Nick decided to play with the numbers on the paper. In one operation Nick could swap some numbers inside a single line (Nick didn't swap numbers from distinct lines). Nick could have performed any number of such operations.
When Peter returned and found out about Nick's mischief, he started recollecting the original coordinates. Help Peter restore the original position of the points or else state that this is impossible and the numbers were initially recorded incorrectly. | Each of the eight lines contains three space-separated integers β the numbers written on the piece of paper after Nick's mischief. All numbers do not exceed 106 in their absolute value. | If there is a way to restore the cube, then print in the first line "YES". In each of the next eight lines print three integers β the restored coordinates of the points. The numbers in the *i*-th output line must be a permutation of the numbers in *i*-th input line. The numbers should represent the vertices of a cube with non-zero length of a side. If there are multiple possible ways, print any of them.
If there is no valid way, print "NO" (without the quotes) in the first line. Do not print anything else. | [
"0 0 0\n0 0 1\n0 0 1\n0 0 1\n0 1 1\n0 1 1\n0 1 1\n1 1 1\n",
"0 0 0\n0 0 0\n0 0 0\n0 0 0\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n"
] | [
"YES\n0 0 0\n0 0 1\n0 1 0\n1 0 0\n0 1 1\n1 0 1\n1 1 0\n1 1 1\n",
"NO\n"
] | none | [
{
"input": "0 0 0\n0 0 1\n0 0 1\n0 0 1\n0 1 1\n0 1 1\n0 1 1\n1 1 1",
"output": "YES\n0 0 0\n0 0 1\n0 1 0\n1 0 0\n0 1 1\n1 0 1\n1 1 0\n1 1 1"
},
{
"input": "0 0 0\n0 0 0\n0 0 0\n0 0 0\n1 1 1\n1 1 1\n1 1 1\n1 1 1",
"output": "NO"
},
{
"input": "0 0 0\n1 0 0\n0 1 0\n1 1 0\n0 0 1\n1 0 1\n0 1... | 15 | 0 | -1 | 10,299 | |
5 | Follow Traffic Rules | [
"implementation",
"math"
] | D. Follow Traffic Rules | 1 | 64 | Everybody knows that the capital of Berland is connected to Bercouver (the Olympic capital) by a direct road. To improve the road's traffic capacity, there was placed just one traffic sign, limiting the maximum speed. Traffic signs in Berland are a bit peculiar, because they limit the speed only at that point on the road where they are placed. Right after passing the sign it is allowed to drive at any speed.
It is known that the car of an average Berland citizen has the acceleration (deceleration) speed of *a* km/h2, and has maximum speed of *v* km/h. The road has the length of *l* km, and the speed sign, limiting the speed to *w* km/h, is placed *d* km (1<=β€<=*d*<=<<=*l*) away from the capital of Berland. The car has a zero speed at the beginning of the journey. Find the minimum time that an average Berland citizen will need to get from the capital to Bercouver, if he drives at the optimal speed.
The car can enter Bercouver at any speed. | The first line of the input file contains two integer numbers *a* and *v* (1<=β€<=*a*,<=*v*<=β€<=10000). The second line contains three integer numbers *l*, *d* and *w* (2<=β€<=*l*<=β€<=10000; 1<=β€<=*d*<=<<=*l*; 1<=β€<=*w*<=β€<=10000). | Print the answer with at least five digits after the decimal point. | [
"1 1\n2 1 3\n",
"5 70\n200 170 40\n"
] | [
"2.500000000000\n",
"8.965874696353\n"
] | none | [
{
"input": "1 1\n2 1 3",
"output": "2.500000000000"
},
{
"input": "5 70\n200 170 40",
"output": "8.965874696353"
},
{
"input": "6 80\n100 50 10",
"output": "7.312347829731"
},
{
"input": "7 80\n100 50 50",
"output": "5.345224838248"
},
{
"input": "8 80\n100 50 199... | 154 | 20,172,800 | 0 | 10,301 |
291 | Command Line Arguments | [
"*special",
"implementation",
"strings"
] | null | null | The problem describes the properties of a command line. The description somehow resembles the one you usually see in real operating systems. However, there are differences in the behavior. Please make sure you've read the statement attentively and use it as a formal document.
In the Pindows operating system a strings are the lexemes of the command line β the first of them is understood as the name of the program to run and the following lexemes are its arguments. For example, as we execute the command " run.exe one, two . ", we give four lexemes to the Pindows command line: "run.exe", "one,", "two", ".". More formally, if we run a command that can be represented as string *s* (that has no quotes), then the command line lexemes are maximal by inclusion substrings of string *s* that contain no spaces.
To send a string with spaces or an empty string as a command line lexeme, we can use double quotes. The block of characters that should be considered as one lexeme goes inside the quotes. Embedded quotes are prohibited β that is, for each occurrence of character """ we should be able to say clearly that the quotes are opening or closing. For example, as we run the command ""run.exe o" "" " ne, " two . " " ", we give six lexemes to the Pindows command line: "run.exe o", "" (an empty string), " ne, ", "two", ".", " " (a single space).
It is guaranteed that each lexeme of the command line is either surrounded by spaces on both sides or touches the corresponding command border. One of its consequences is: the opening brackets are either the first character of the string or there is a space to the left of them.
You have a string that consists of uppercase and lowercase English letters, digits, characters ".,?!"" and spaces. It is guaranteed that this string is a correct OS Pindows command line string. Print all lexemes of this command line string. Consider the character """ to be used only in order to denote a single block of characters into one command line lexeme. In particular, the consequence is that the given string has got an even number of such characters. | The single line contains a non-empty string *s*. String *s* consists of at most 105 characters. Each character is either an uppercase or a lowercase English letter, or a digit, or one of the ".,?!"" signs, or a space.
It is guaranteed that the given string is some correct command line string of the OS Pindows. It is guaranteed that the given command line string contains at least one lexeme. | In the first line print the first lexeme, in the second line print the second one and so on. To make the output clearer, print the "<" (less) character to the left of your lexemes and the ">" (more) character to the right. Print the lexemes in the order in which they occur in the command.
Please, follow the given output format strictly. For more clarifications on the output format see the test samples. | [
"\"RUn.exe O\" \"\" \" 2ne, \" two! . \" \"\n",
"firstarg second \"\" \n"
] | [
"<RUn.exe O>\n<>\n< 2ne, >\n<two!>\n<.>\n< >\n",
"<firstarg>\n<second>\n<>\n"
] | none | [
{
"input": "\"RUn.exe O\" \"\" \" 2ne, \" two! . \" \"",
"output": "<RUn.exe O>\n<>\n< 2ne, >\n<two!>\n<.>\n< >"
},
{
"input": " firstarg second \"\" ",
"output": "<firstarg>\n<second>\n<>"
},
{
"input": " \" \" ",
"output": "< >"
},
{
"input": " a \" \... | 857 | 11,366,400 | 3 | 10,343 | |
246 | Beauty Pageant | [
"brute force",
"constructive algorithms",
"greedy"
] | null | null | General Payne has a battalion of *n* soldiers. The soldiers' beauty contest is coming up, it will last for *k* days. Payne decided that his battalion will participate in the pageant. Now he has choose the participants.
All soldiers in the battalion have different beauty that is represented by a positive integer. The value *a**i* represents the beauty of the *i*-th soldier.
On each of *k* days Generals has to send a detachment of soldiers to the pageant. The beauty of the detachment is the sum of the beauties of the soldiers, who are part of this detachment. Payne wants to surprise the jury of the beauty pageant, so each of *k* days the beauty of the sent detachment should be unique. In other words, all *k* beauties of the sent detachments must be distinct numbers.
Help Payne choose *k* detachments of different beauties for the pageant. Please note that Payne cannot just forget to send soldiers on one day, that is, the detachment of soldiers he sends to the pageant should never be empty. | The first line contains two integers *n*, *k* (1<=β€<=*n*<=β€<=50; 1<=β€<=*k*<=β€<= ) β the number of soldiers and the number of days in the pageant, correspondingly. The second line contains space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=107) β the beauties of the battalion soldiers.
It is guaranteed that Payne's battalion doesn't have two soldiers with the same beauty. | Print *k* lines: in the *i*-th line print the description of the detachment that will participate in the pageant on the *i*-th day. The description consists of integer *c**i* (1<=β€<=*c**i*<=β€<=*n*) β the number of soldiers in the detachment on the *i*-th day of the pageant and *c**i* distinct integers *p*1,<=*i*,<=*p*2,<=*i*,<=...,<=*p**c**i*,<=*i* β the beauties of the soldiers in the detachment on the *i*-th day of the pageant. The beauties of the soldiers are allowed to print in any order.
Separate numbers on the lines by spaces. It is guaranteed that there is the solution that meets the problem conditions. If there are multiple solutions, print any of them. | [
"3 3\n1 2 3\n",
"2 1\n7 12\n"
] | [
"1 1\n1 2\n2 3 2\n",
"1 12 \n"
] | none | [
{
"input": "3 3\n1 2 3",
"output": "1 1\n1 2\n2 3 2"
},
{
"input": "2 1\n7 12",
"output": "1 12 "
},
{
"input": "1 1\n1000",
"output": "1 1000 "
},
{
"input": "5 8\n10 3 8 31 20",
"output": "1 31 \n1 20 \n1 10 \n1 8 \n1 3 \n2 31 20 \n2 31 10 \n2 31 8 "
},
{
"input... | 154 | 0 | 0 | 10,365 | |
926 | Large Bouquets | [] | null | null | A flower shop has got *n* bouquets, and the *i*-th bouquet consists of *a**i* flowers. Vasya, the manager of the shop, decided to make large bouquets from these bouquets.
Vasya thinks that a bouquet is large if it is made of two or more initial bouquets, and there is a constraint: the total number of flowers in a large bouquet should be odd. Each of the initial bouquets can be a part of at most one large bouquet. If an initial bouquet becomes a part of a large bouquet, all its flowers are included in the large bouquet.
Determine the maximum possible number of large bouquets Vasya can make. | The first line contains a single positive integer *n* (1<=β€<=*n*<=β€<=105) β the number of initial bouquets.
The second line contains a sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=106) β the number of flowers in each of the initial bouquets. | Print the maximum number of large bouquets Vasya can make. | [
"5\n2 3 4 2 7\n",
"6\n2 2 6 8 6 12\n",
"3\n11 4 10\n"
] | [
"2\n",
"0\n",
"1\n"
] | In the first example Vasya can make 2 large bouquets. For example, the first bouquet can contain the first and the fifth initial bouquets (the total number of flowers is then equal to 9), and the second bouquet can consist of the second and the third initial bouquets (the total number of flowers is then equal to 7). The fourth initial bouquet is unused in this scheme.
In the second example it is not possible to form a single bouquet with odd number of flowers.
In the third example Vasya can make one large bouquet. For example, he can make it using all three initial bouquets. The size of the large bouquet is then equal to 11β+β4β+β10β=β25. | [
{
"input": "5\n2 3 4 2 7",
"output": "2"
},
{
"input": "6\n2 2 6 8 6 12",
"output": "0"
},
{
"input": "3\n11 4 10",
"output": "1"
},
{
"input": "1\n1",
"output": "0"
},
{
"input": "1\n2",
"output": "0"
},
{
"input": "1\n999999",
"output": "0"
},
... | 139 | 13,107,200 | 3 | 10,370 | |
383 | Milking cows | [
"data structures",
"greedy"
] | null | null | Iahub helps his grandfather at the farm. Today he must milk the cows. There are *n* cows sitting in a row, numbered from 1 to *n* from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk).
Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=200000). The second line contains *n* integers *a*1, *a*2, ..., *a**n*, where *a**i* is 0 if the cow number *i* is facing left, and 1 if it is facing right. | Print a single integer, the minimum amount of lost milk.
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\n0 0 1 0\n",
"5\n1 0 1 0 1\n"
] | [
"1",
"3"
] | In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost. | [
{
"input": "4\n0 0 1 0",
"output": "1"
},
{
"input": "5\n1 0 1 0 1",
"output": "3"
},
{
"input": "50\n1 1 0 1 1 1 1 1 1 0 0 1 1 0 1 1 0 0 1 0 1 1 0 1 1 1 1 0 1 0 1 0 1 1 1 0 0 0 0 0 0 0 1 1 0 1 0 0 1 0",
"output": "416"
},
{
"input": "100\n1 1 0 0 1 1 1 1 0 1 1 1 1 1 1 1 0 0 ... | 78 | 0 | 0 | 10,379 | |
852 | Neural Network country | [
"dp",
"matrices"
] | null | null | Due to the recent popularity of the Deep learning new countries are starting to look like Neural Networks. That is, the countries are being built deep with many layers, each layer possibly having many cities. They also have one entry, and one exit point.
There are exactly *L* layers, each having *N* cities. Let us look at the two adjacent layers *L*1 and *L*2. Each city from the layer *L*1 is connected to each city from the layer *L*2 with the traveling cost *c**ij* for , and each pair of adjacent layers has the same cost in between their cities as any other pair (they just stacked the same layers, as usual). Also, the traveling costs to each city from the layer *L*2 are same for all cities in the *L*1, that is *c**ij* is the same for , and fixed *j*.
Doctor G. needs to speed up his computations for this country so he asks you to find the number of paths he can take from entry to exit point such that his traveling cost is divisible by given number *M*. | The first line of input contains *N* (1<=β€<=*N*<=β€<=106), *L* (2<=β€<=*L*<=β€<=105) and *M* (2<=β€<=*M*<=β€<=100), the number of cities in each layer, the number of layers and the number that travelling cost should be divisible by, respectively.
Second, third and fourth line contain *N* integers each denoting costs 0<=β€<=*cost*<=β€<=*M* from entry point to the first layer, costs between adjacent layers as described above, and costs from the last layer to the exit point. | Output a single integer, the number of paths Doctor G. can take which have total cost divisible by *M*, modulo 109<=+<=7. | [
"2 3 13\n4 6\n2 1\n3 4\n"
] | [
"2"
] | <img class="tex-graphics" src="https://espresso.codeforces.com/959c8bea1eef9daad659ecab34d36a2f692c5e88.png" style="max-width: 100.0%;max-height: 100.0%;"/>
This is a country with 3 layers, each layer having 2 cities. Paths <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ea33f7ca0560180dc03b2657e6a1f9fd874e5adc.png" style="max-width: 100.0%;max-height: 100.0%;"/>, and <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4b3fe7f211ecca3ab05c72cd3b995d28d037ab45.png" style="max-width: 100.0%;max-height: 100.0%;"/> are the only paths having total cost divisible by 13. Notice that input edges for layer cities have the same cost, and that they are same for all layers. | [
{
"input": "2 3 13\n4 6\n2 1\n3 4",
"output": "2"
},
{
"input": "2 4 5\n1 1\n1 1\n1 1",
"output": "16"
},
{
"input": "1 1234 5\n1\n1\n1",
"output": "1"
},
{
"input": "3 2 2\n0 1 0\n0 0 1\n1 1 0",
"output": "3"
},
{
"input": "5 4 3\n2 1 0 1 2\n0 1 2 1 0\n1 2 1 0 2"... | 46 | 0 | 0 | 10,384 | |
928 | Dependency management | [
"*special",
"graphs",
"implementation"
] | null | null | Polycarp is currently developing a project in Vaja language and using a popular dependency management system called Vamen. From Vamen's point of view both Vaja project and libraries are treated projects for simplicity.
A project in Vaja has its own uniqie non-empty name consisting of lowercase latin letters with length not exceeding 10 and version β positive integer from 1 to 106. Each project (keep in mind that it is determined by both its name and version) might depend on other projects. For sure, there are no cyclic dependencies.
You're given a list of project descriptions. The first of the given projects is the one being developed by Polycarp at this moment. Help Polycarp determine all projects that his project depends on (directly or via a certain chain).
It's possible that Polycarp's project depends on two different versions of some project. In this case collision resolving is applied, i.e. for each such project the system chooses the version that minimizes the distance from it to Polycarp's project. If there are several options, the newer (with the maximum version) is preferred. This version is considered actual; other versions and their dependencies are ignored.
More formal, choose such a set of projects of minimum possible size that the following conditions hold:
- Polycarp's project is chosen; - Polycarp's project depends (directly or indirectly) on all other projects in the set; - no two projects share the name; - for each project *x* that some other project in the set depends on we have either *x* or some *y* with other version and shorter chain to Polycarp's project chosen. In case of ties the newer one is chosen.
Output all Polycarp's project's dependencies (Polycarp's project itself should't be printed) in lexicographical order. | The first line contains an only integer *n* (1<=β€<=*n*<=β€<=1<=000) β the number of projects in Vaja.
The following lines contain the project descriptions. Each project is described by a line consisting of its name and version separated by space. The next line gives the number of direct dependencies (from 0 to *n*<=-<=1) and the dependencies themselves (one in a line) in arbitrary order. Each dependency is specified by its name and version. The projects are also given in arbitrary order, but the first of them is always Polycarp's. Project descriptions are separated by one empty line. Refer to samples for better understanding.
It's guaranteed that there are no cyclic dependencies. | Output all Polycarp's project's dependencies in lexicographical order. | [
"4\na 3\n2\nb 1\nc 1\nΒ \nb 2\n0\nΒ \nb 1\n1\nb 2\nΒ \nc 1\n1\nb 2\n",
"9\ncodehorses 5\n3\nwebfrmk 6\nmashadb 1\nmashadb 2\nΒ \ncommons 2\n0\nΒ \nmashadb 3\n0\nΒ \nwebfrmk 6\n2\nmashadb 3\ncommons 2\nΒ \nextra 4\n1\nextra 3\nΒ \nextra 3\n0\nΒ \nextra 1\n0\nΒ \nmashadb 1\n1\nextra 3\nΒ \nmashadb 2\n1\nextra 1\n",
"3\nabc ... | [
"2\nb 1\nc 1\n",
"4\ncommons 2\nextra 1\nmashadb 2\nwebfrmk 6\n",
"1\ncba 2\n"
] | The first sample is given in the pic below. Arrow from *A* to *B* means that *B* directly depends on *A*. Projects that Polycarp's project Β«aΒ» (version 3) depends on are painted black.
The second sample is again given in the pic below. Arrow from *A* to *B* means that *B* directly depends on *A*. Projects that Polycarp's project Β«codehorsesΒ» (version 5) depends on are paint it black. Note that Β«extra 1Β» is chosen instead of Β«extra 3Β» since Β«mashadb 1Β» and all of its dependencies are ignored due to Β«mashadb 2Β». | [
{
"input": "4\na 3\n2\nb 1\nc 1\n\nb 2\n0\n\nb 1\n1\nb 2\n\nc 1\n1\nb 2",
"output": "2\nb 1\nc 1"
},
{
"input": "9\ncodehorses 5\n3\nwebfrmk 6\nmashadb 1\nmashadb 2\n\ncommons 2\n0\n\nmashadb 3\n0\n\nwebfrmk 6\n2\nmashadb 3\ncommons 2\n\nextra 4\n1\nextra 3\n\nextra 3\n0\n\nextra 1\n0\n\nmashadb 1\n... | 61 | 5,734,400 | 0 | 10,396 | |
690 | Collective Mindsets (easy) | [] | null | null | Tonight is brain dinner night and all zombies will gather together to scarf down some delicious brains. The artful Heidi plans to crash the party, incognito, disguised as one of them. Her objective is to get away with at least one brain, so she can analyze the zombies' mindset back home and gain a strategic advantage.
They will be *N* guests tonight: *N*<=-<=1 real zombies and a fake one, our Heidi. The living-dead love hierarchies as much as they love brains: each one has a unique rank in the range 1 to *N*<=-<=1, and Heidi, who still appears slightly different from the others, is attributed the highest rank, *N*. Tonight there will be a chest with brains on display and every attendee sees how many there are. These will then be split among the attendees according to the following procedure:
The zombie of the highest rank makes a suggestion on who gets how many brains (every brain is an indivisible entity). A vote follows. If at least half of the attendees accept the offer, the brains are shared in the suggested way and the feast begins. But if majority is not reached, then the highest-ranked zombie is killed, and the next zombie in hierarchy has to make a suggestion. If he is killed too, then the third highest-ranked makes one, etc. (It's enough to have exactly half of the votes β in case of a tie, the vote of the highest-ranked alive zombie counts twice, and he will of course vote in favor of his own suggestion in order to stay alive.)
You should know that zombies are very greedy and sly, and they know this too β basically all zombie brains are alike. Consequently, a zombie will never accept an offer which is suboptimal for him. That is, if an offer is not strictly better than a potential later offer, he will vote against it. And make no mistake: while zombies may normally seem rather dull, tonight their intellects are perfect. Each zombie's priorities for tonight are, in descending order:
1. survive the event (they experienced death already once and know it is no fun), 1. get as many brains as possible.
Heidi goes first and must make an offer which at least half of the attendees will accept, and which allocates at least one brain for Heidi herself.
What is the smallest number of brains that have to be in the chest for this to be possible? | The only line of input contains one integer: *N*, the number of attendees (1<=β€<=*N*<=β€<=109). | Output one integer: the smallest number of brains in the chest which allows Heidi to take one brain home. | [
"1\n",
"4\n"
] | [
"1\n",
"2\n"
] | [
{
"input": "1",
"output": "1"
},
{
"input": "4",
"output": "2"
},
{
"input": "2",
"output": "1"
},
{
"input": "3",
"output": "2"
},
{
"input": "5",
"output": "3"
},
{
"input": "6",
"output": "3"
},
{
"input": "7",
"output": "4"
},
{... | 0 | 0 | -1 | 10,403 | ||
0 | none | [
"none"
] | null | null | You can perfectly predict the price of a certain stock for the next *N* days. You would like to profit on this knowledge, but only want to transact one share of stock per day. That is, each day you will either buy one share, sell one share, or do nothing. Initially you own zero shares, and you cannot sell shares when you don't own any. At the end of the *N* days you would like to again own zero shares, but want to have as much money as possible. | Input begins with an integer *N* (2<=β€<=*N*<=β€<=3Β·105), the number of days.
Following this is a line with exactly *N* integers *p*1,<=*p*2,<=...,<=*p**N* (1<=β€<=*p**i*<=β€<=106). The price of one share of stock on the *i*-th day is given by *p**i*. | Print the maximum amount of money you can end up with at the end of *N* days. | [
"9\n10 5 4 7 9 12 6 2 10\n",
"20\n3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4\n"
] | [
"20\n",
"41\n"
] | In the first example, buy a share at 5, buy another at 4, sell one at 9 and another at 12. Then buy at 2 and sell at 10. The total profit is β-β5β-β4β+β9β+β12β-β2β+β10β=β20. | [
{
"input": "9\n10 5 4 7 9 12 6 2 10",
"output": "20"
},
{
"input": "20\n3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4",
"output": "41"
},
{
"input": "20\n9 29 8 9 13 4 14 27 16 11 27 14 4 29 23 17 3 9 30 19",
"output": "147"
},
{
"input": "100\n411 642 560 340 276 440 515 519 182 3... | 46 | 0 | 0 | 10,404 | |
939 | Hamster Farm | [
"implementation"
] | null | null | Dima has a hamsters farm. Soon *N* hamsters will grow up on it and Dima will sell them in a city nearby.
Hamsters should be transported in boxes. If some box is not completely full, the hamsters in it are bored, that's why each box should be completely full with hamsters.
Dima can buy boxes at a factory. The factory produces boxes of *K* kinds, boxes of the *i*-th kind can contain in themselves *a**i* hamsters. Dima can buy any amount of boxes, but he should buy boxes of only one kind to get a wholesale discount.
Of course, Dima would buy boxes in such a way that each box can be completely filled with hamsters and transported to the city. If there is no place for some hamsters, Dima will leave them on the farm.
Find out how many boxes and of which type should Dima buy to transport maximum number of hamsters. | The first line contains two integers *N* and *K* (0<=β€<=*N*<=β€<=1018, 1<=β€<=*K*<=β€<=105)Β β the number of hamsters that will grow up on Dima's farm and the number of types of boxes that the factory produces.
The second line contains *K* integers *a*1, *a*2, ..., *a**K* (1<=β€<=*a**i*<=β€<=1018 for all *i*)Β β the capacities of boxes. | Output two integers: the type of boxes that Dima should buy and the number of boxes of that type Dima should buy. Types of boxes are numbered from 1 to *K* in the order they are given in input.
If there are many correct answers, output any of them. | [
"19 3\n5 4 10\n",
"28 3\n5 6 30\n"
] | [
"2 4\n",
"1 5\n"
] | none | [
{
"input": "19 3\n5 4 10",
"output": "2 4"
},
{
"input": "28 3\n5 6 30",
"output": "1 5"
},
{
"input": "1 1\n1",
"output": "1 1"
},
{
"input": "0 2\n2 3",
"output": "1 0"
},
{
"input": "30 4\n4 5 5 4",
"output": "2 6"
},
{
"input": "120 7\n109 92 38 38... | 140 | 10,342,400 | 3 | 10,416 | |
384 | Multitasking | [
"greedy",
"implementation",
"sortings",
"two pointers"
] | null | null | Iahub wants to enhance his multitasking abilities. In order to do this, he wants to sort *n* arrays simultaneously, each array consisting of *m* integers.
Iahub can choose a pair of distinct indices *i* and *j* (1<=β€<=*i*,<=*j*<=β€<=*m*,<=*i*<=β <=*j*). Then in each array the values at positions *i* and *j* are swapped only if the value at position *i* is strictly greater than the value at position *j*.
Iahub wants to find an array of pairs of distinct indices that, chosen in order, sort all of the *n* arrays in ascending or descending order (the particular order is given in input). The size of the array can be at most (at most pairs). Help Iahub, find any suitable array. | The first line contains three integers *n* (1<=β€<=<=*n*<=β€<=1000), *m* (1<=β€<=*m*<=β€<=<=100) and *k*. Integer *k* is 0 if the arrays must be sorted in ascending order, and 1 if the arrays must be sorted in descending order. Each line *i* of the next *n* lines contains *m* integers separated by a space, representing the *i*-th array. For each element *x* of the array *i*, 1<=β€<=*x*<=β€<=106 holds. | On the first line of the output print an integer *p*, the size of the array (*p* can be at most ). Each of the next *p* lines must contain two distinct integers *i* and *j* (1<=β€<=*i*,<=*j*<=β€<=*m*,<=*i*<=β <=*j*), representing the chosen indices.
If there are multiple correct answers, you can print any. | [
"2 5 0\n1 3 2 5 4\n1 4 3 2 5\n",
"3 2 1\n1 2\n2 3\n3 4\n"
] | [
"3\n2 4\n2 3\n4 5\n",
"1\n2 1\n"
] | Consider the first sample. After the first operation, the arrays become [1,β3,β2,β5,β4] and [1,β2,β3,β4,β5]. After the second operation, the arrays become [1,β2,β3,β5,β4] and [1,β2,β3,β4,β5]. After the third operation they become [1,β2,β3,β4,β5] and [1,β2,β3,β4,β5]. | [
{
"input": "2 5 0\n1 3 2 5 4\n1 4 3 2 5",
"output": "3\n2 4\n2 3\n4 5"
},
{
"input": "3 2 1\n1 2\n2 3\n3 4",
"output": "1\n2 1"
},
{
"input": "2 5 0\n836096 600367 472071 200387 79763\n714679 505282 233544 157810 152591",
"output": "10\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4\n3 5\n4 ... | 92 | 3,481,600 | 3 | 10,428 | |
0 | none | [
"none"
] | null | null | Mashmokh's boss, Bimokh, didn't like Mashmokh. So he fired him. Mashmokh decided to go to university and participate in ACM instead of finding a new job. He wants to become a member of Bamokh's team. In order to join he was given some programming tasks and one week to solve them. Mashmokh is not a very experienced programmer. Actually he is not a programmer at all. So he wasn't able to solve them. That's why he asked you to help him with these tasks. One of these tasks is the following.
You have an array *a* of length 2*n* and *m* queries on it. The *i*-th query is described by an integer *q**i*. In order to perform the *i*-th query you must:
- split the array into 2*n*<=-<=*q**i* parts, where each part is a subarray consisting of 2*q**i* numbers; the *j*-th subarray (1<=β€<=*j*<=β€<=2*n*<=-<=*q**i*) should contain the elements *a*[(*j*<=-<=1)Β·2*q**i*<=+<=1],<=*a*[(*j*<=-<=1)Β·2*q**i*<=+<=2],<=...,<=*a*[(*j*<=-<=1)Β·2*q**i*<=+<=2*q**i*]; - reverse each of the subarrays; - join them into a single array in the same order (this array becomes new array *a*); - output the number of inversions in the new *a*.
Given initial array *a* and all the queries. Answer all the queries. Please, note that the changes from some query is saved for further queries. | The first line of input contains a single integer *n*Β (0<=β€<=*n*<=β€<=20).
The second line of input contains 2*n* space-separated integers *a*[1],<=*a*[2],<=...,<=*a*[2*n*]Β (1<=β€<=*a*[*i*]<=β€<=109), the initial array.
The third line of input contains a single integer *m*Β (1<=β€<=*m*<=β€<=106).
The fourth line of input contains *m* space-separated integers *q*1,<=*q*2,<=...,<=*q**m*Β (0<=β€<=*q**i*<=β€<=*n*), the queries.
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++. | Output *m* lines. In the *i*-th line print the answer (the number of inversions) for the *i*-th query. | [
"2\n2 1 4 3\n4\n1 2 0 2\n",
"1\n1 2\n3\n0 1 1\n"
] | [
"0\n6\n6\n0\n",
"0\n1\n0\n"
] | If we reverse an array *x*[1],β*x*[2],β...,β*x*[*n*] it becomes new array *y*[1],β*y*[2],β...,β*y*[*n*], where *y*[*i*]β=β*x*[*n*β-β*i*β+β1] for each *i*.
The number of inversions of an array *x*[1],β*x*[2],β...,β*x*[*n*] is the number of pairs of indices *i*,β*j* such that: *i*β<β*j* and *x*[*i*]β>β*x*[*j*]. | [] | 30 | 0 | 0 | 10,446 | |
717 | Potions Homework | [
"implementation",
"sortings"
] | null | null | Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class.
Each of the *n* students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each studentβs laziness level is equal to their taskβs difficulty level. Both sets of values are given by the sequence *a*, where *a**i* represents both the laziness level of the *i*-th student and the difficulty of his task.
The time a student needs to finish a task is equal to the product of their laziness level and the taskβs difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10<=007. | The first line of input contains integer *n* (1<=β€<=*n*<=β€<=100<=000)Β β the number of tasks. The next *n* lines contain exactly one integer number *a**i* (1<=β€<=*a**i*<=β€<=100<=000)Β β both the difficulty of the initial task and the laziness of the *i*-th students. | Print the minimum total time to finish all tasks modulo 10<=007. | [
"2\n1\n3\n"
] | [
"6\n"
] | In the first sample, if the students switch their tasks, they will be able to finish them in 3β+β3β=β6 time units. | [
{
"input": "2\n1\n3",
"output": "6"
}
] | 108 | 2,457,600 | 0 | 10,474 | |
475 | Kamal-ol-molk's Painting | [
"brute force",
"constructive algorithms",
"greedy"
] | null | null | Rumors say that one of Kamal-ol-molk's paintings has been altered. A rectangular brush has been moved right and down on the painting.
Consider the painting as a *n*<=Γ<=*m* rectangular grid. At the beginning an *x*<=Γ<=*y* rectangular brush is placed somewhere in the frame, with edges parallel to the frame, (1<=β€<=*x*<=β€<=*n*,<=1<=β€<=*y*<=β€<=*m*). Then the brush is moved several times. Each time the brush is moved one unit right or down. The brush has been strictly inside the frame during the painting. The brush alters every cell it has covered at some moment.
You have found one of the old Kamal-ol-molk's paintings. You want to know if it's possible that it has been altered in described manner. If yes, you also want to know minimum possible area of the brush. | The first line of input contains two integers *n* and *m*, (1<=β€<=*n*,<=*m*<=β€<=1000), denoting the height and width of the painting.
The next *n* lines contain the painting. Each line has *m* characters. Character 'X' denotes an altered cell, otherwise it's showed by '.'. There will be at least one altered cell in the painting. | Print the minimum area of the brush in a line, if the painting is possibly altered, otherwise print <=-<=1. | [
"4 4\nXX..\nXX..\nXXXX\nXXXX\n",
"4 4\n....\n.XXX\n.XXX\n....\n",
"4 5\nXXXX.\nXXXX.\n.XX..\n.XX..\n"
] | [
"4\n",
"2\n",
"-1\n"
] | none | [
{
"input": "4 4\nXX..\nXX..\nXXXX\nXXXX",
"output": "4"
},
{
"input": "4 5\nXXXX.\nXXXX.\n.XX..\n.XX..",
"output": "-1"
},
{
"input": "5 6\n.XXX..\n.XXXX.\n.XXXX.\n..XXX.\n..XXX.",
"output": "6"
},
{
"input": "5 6\n.XXXX.\n.XXXXX\n...XXX\n...XXX\n...XXX",
"output": "-1"
... | 2,000 | 1,536,000 | 0 | 10,486 | |
177 | Encrypting Messages | [
"brute force"
] | null | null | The Smart Beaver from ABBYY invented a new message encryption method and now wants to check its performance. Checking it manually is long and tiresome, so he decided to ask the ABBYY Cup contestants for help.
A message is a sequence of *n* integers *a*1,<=*a*2,<=...,<=*a**n*. Encryption uses a key which is a sequence of *m* integers *b*1,<=*b*2,<=...,<=*b**m* (*m*<=β€<=*n*). All numbers from the message and from the key belong to the interval from 0 to *c*<=-<=1, inclusive, and all the calculations are performed modulo *c*.
Encryption is performed in *n*<=-<=*m*<=+<=1 steps. On the first step we add to each number *a*1,<=*a*2,<=...,<=*a**m* a corresponding number *b*1,<=*b*2,<=...,<=*b**m*. On the second step we add to each number *a*2,<=*a*3,<=...,<=*a**m*<=+<=1 (changed on the previous step) a corresponding number *b*1,<=*b*2,<=...,<=*b**m*. And so on: on step number *i* we add to each number *a**i*,<=*a**i*<=+<=1,<=...,<=*a**i*<=+<=*m*<=-<=1 a corresponding number *b*1,<=*b*2,<=...,<=*b**m*. The result of the encryption is the sequence *a*1,<=*a*2,<=...,<=*a**n* after *n*<=-<=*m*<=+<=1 steps.
Help the Beaver to write a program that will encrypt messages in the described manner. | The first input line contains three integers *n*, *m* and *c*, separated by single spaces.
The second input line contains *n* integers *a**i* (0<=β€<=*a**i*<=<<=*c*), separated by single spaces β the original message.
The third input line contains *m* integers *b**i* (0<=β€<=*b**i*<=<<=*c*), separated by single spaces β the encryption key.
The input limitations for getting 30 points are:
- 1<=β€<=*m*<=β€<=*n*<=β€<=103 - 1<=β€<=*c*<=β€<=103
The input limitations for getting 100 points are:
- 1<=β€<=*m*<=β€<=*n*<=β€<=105 - 1<=β€<=*c*<=β€<=103 | Print *n* space-separated integers β the result of encrypting the original message. | [
"4 3 2\n1 1 1 1\n1 1 1\n",
"3 1 5\n1 2 3\n4\n"
] | [
"0 1 1 0\n",
"0 1 2\n"
] | In the first sample the encryption is performed in two steps: after the first step *a*β=β(0,β0,β0,β1) (remember that the calculations are performed modulo 2), after the second step *a*β=β(0,β1,β1,β0), and that is the answer. | [
{
"input": "4 3 2\n1 1 1 1\n1 1 1",
"output": "0 1 1 0"
},
{
"input": "3 1 5\n1 2 3\n4",
"output": "0 1 2"
},
{
"input": "5 2 7\n0 0 1 2 4\n3 5",
"output": "3 1 2 3 2"
},
{
"input": "20 15 17\n4 9 14 11 15 16 15 4 0 10 7 12 10 1 8 6 7 14 1 13\n6 3 14 8 8 11 16 4 5 9 2 13 6 14... | 216 | 2,048,000 | 3 | 10,518 | |
549 | Sasha Circle | [
"geometry",
"math"
] | null | null | Berlanders like to eat cones after a hard day. Misha Square and Sasha Circle are local authorities of Berland. Each of them controls its points of cone trade. Misha has *n* points, Sasha β *m*. Since their subordinates constantly had conflicts with each other, they decided to build a fence in the form of a circle, so that the points of trade of one businessman are strictly inside a circle, and points of the other one are strictly outside. It doesn't matter which of the two gentlemen will have his trade points inside the circle.
Determine whether they can build a fence or not. | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=10000), numbers of Misha's and Sasha's trade points respectively.
The next *n* lines contains pairs of space-separated integers *M**x*,<=*M**y* (<=-<=104<=β€<=*M**x*,<=*M**y*<=β€<=104), coordinates of Misha's trade points.
The next *m* lines contains pairs of space-separated integers *S**x*,<=*S**y* (<=-<=104<=β€<=*S**x*,<=*S**y*<=β€<=104), coordinates of Sasha's trade points.
It is guaranteed that all *n*<=+<=*m* points are distinct. | The only output line should contain either word "YES" without quotes in case it is possible to build a such fence or word "NO" in the other case. | [
"2 2\n-1 0\n1 0\n0 -1\n0 1\n",
"4 4\n1 0\n0 1\n-1 0\n0 -1\n1 1\n-1 1\n-1 -1\n1 -1\n"
] | [
"NO\n",
"YES\n"
] | In the first sample there is no possibility to separate points, because any circle that contains both points (β-β1,β0),β(1,β0) also contains at least one point from the set (0,ββ-β1),β(0,β1), and vice-versa: any circle that contains both points (0,ββ-β1),β(0,β1) also contains at least one point from the set (β-β1,β0),β(1,β0)
In the second sample one of the possible solution is shown below. Misha's points are marked with red colour and Sasha's are marked with blue. <img class="tex-graphics" src="https://espresso.codeforces.com/91e76198f6d74c0a8b0f92c94460d887bfebc9fa.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "2 2\n-1 0\n1 0\n0 -1\n0 1",
"output": "NO"
},
{
"input": "4 4\n1 0\n0 1\n-1 0\n0 -1\n1 1\n-1 1\n-1 -1\n1 -1",
"output": "YES"
},
{
"input": "2 3\n-1 0\n1 0\n0 -2\n0 0\n0 2",
"output": "NO"
},
{
"input": "3 3\n-3 -4\n3 2\n1 5\n4 0\n5 2\n-2 -1",
"output": "NO"
... | 61 | 307,200 | 0 | 10,529 | |
976 | Lara Croft and the New Game | [
"implementation",
"math"
] | null | null | You might have heard about the next game in Lara Croft series coming out this year. You also might have watched its trailer. Though you definitely missed the main idea about its plot, so let me lift the veil of secrecy.
Lara is going to explore yet another dangerous dungeon. Game designers decided to use good old 2D environment. The dungeon can be represented as a rectangle matrix of *n* rows and *m* columns. Cell (*x*,<=*y*) is the cell in the *x*-th row in the *y*-th column. Lara can move between the neighbouring by side cells in all four directions.
Moreover, she has even chosen the path for herself to avoid all the traps. She enters the dungeon in cell (1,<=1), that is top left corner of the matrix. Then she goes down all the way to cell (*n*,<=1) β the bottom left corner. Then she starts moving in the snake fashion β all the way to the right, one cell up, then to the left to the cell in 2-nd column, one cell up. She moves until she runs out of non-visited cells. *n* and *m* given are such that she always end up in cell (1,<=2).
Lara has already moved to a neighbouring cell *k* times. Can you determine her current position? | The only line contains three integers *n*, *m* and *k* (2<=β€<=*n*,<=*m*<=β€<=109, *n* is always even, 0<=β€<=*k*<=<<=*n*Β·*m*). Note that *k* doesn't fit into 32-bit integer type! | Print the cell (the row and the column where the cell is situated) where Lara ends up after she moves *k* times. | [
"4 3 0\n",
"4 3 11\n",
"4 3 7\n"
] | [
"1 1\n",
"1 2\n",
"3 2\n"
] | Here is her path on matrix 4 by 3: | [
{
"input": "4 3 0",
"output": "1 1"
},
{
"input": "4 3 11",
"output": "1 2"
},
{
"input": "4 3 7",
"output": "3 2"
},
{
"input": "1000000000 2 1999999999",
"output": "1 2"
},
{
"input": "1000000000 1000000000 999999999999999999",
"output": "1 2"
},
{
"... | 124 | 512,000 | 3 | 10,543 | |
710 | Magic Odd Square | [
"constructive algorithms",
"math"
] | null | null | Find an *n*<=Γ<=*n* matrix with different numbers from 1 to *n*2, so the sum in each row, column and both main diagonals are odd. | The only line contains odd integer *n* (1<=β€<=*n*<=β€<=49). | Print *n* lines with *n* integers. All the integers should be different and from 1 to *n*2. The sum in each row, column and both main diagonals should be odd. | [
"1\n",
"3\n"
] | [
"1\n",
"2 1 4\n3 5 7\n6 9 8\n"
] | none | [
{
"input": "1",
"output": "1"
},
{
"input": "3",
"output": "2 1 4\n3 5 7\n6 9 8"
},
{
"input": "5",
"output": "2 4 1 6 8\n10 3 5 7 12\n9 11 13 15 17\n14 19 21 23 16\n18 20 25 22 24"
},
{
"input": "7",
"output": "2 4 6 1 8 10 12\n14 16 3 5 7 18 20\n22 9 11 13 15 17 24\n19 ... | 30 | 0 | 0 | 10,568 | |
172 | Pseudorandom Sequence Period | [
"*special",
"implementation",
"number theory"
] | null | null | Polycarpus has recently got interested in sequences of pseudorandom numbers. He learned that many programming languages generate such sequences in a similar way: (for *i*<=β₯<=1). Here *a*, *b*, *m* are constants, fixed for the given realization of the pseudorandom numbers generator, *r*0 is the so-called *randseed* (this value can be set from the program using functions like RandSeed(r) or srand(n)), and denotes the operation of taking the remainder of division.
For example, if *a*<==<=2,<=*b*<==<=6,<=*m*<==<=12,<=*r*0<==<=11, the generated sequence will be: 4,<=2,<=10,<=2,<=10,<=2,<=10,<=2,<=10,<=2,<=10,<=....
Polycarpus realized that any such sequence will sooner or later form a cycle, but the cycle may occur not in the beginning, so there exist a preperiod and a period. The example above shows a preperiod equal to 1 and a period equal to 2.
Your task is to find the period of a sequence defined by the given values of *a*,<=*b*,<=*m* and *r*0. Formally, you have to find such minimum positive integer *t*, for which exists such positive integer *k*, that for any *i*<=β₯<=*k*: *r**i*<==<=*r**i*<=+<=*t*. | The single line of the input contains four integers *a*, *b*, *m* and *r*0 (1<=β€<=*m*<=β€<=105,<=0<=β€<=*a*,<=*b*<=β€<=1000,<=0<=β€<=*r*0<=<<=*m*), separated by single spaces. | Print a single integer β the period of the sequence. | [
"2 6 12 11\n",
"2 3 5 1\n",
"3 6 81 9\n"
] | [
"2\n",
"4\n",
"1\n"
] | The first sample is described above.
In the second sample the sequence is (starting from the first element): 0,β3,β4,β1,β0,β3,β4,β1,β0,β...
In the third sample the sequence is (starting from the first element): 33,β24,β78,β78,β78,β78,β... | [
{
"input": "2 6 12 11",
"output": "2"
},
{
"input": "2 3 5 1",
"output": "4"
},
{
"input": "3 6 81 9",
"output": "1"
},
{
"input": "10 11 12 3",
"output": "3"
},
{
"input": "4 4 5 4",
"output": "2"
},
{
"input": "0 1 6 5",
"output": "1"
},
{
... | 2,000 | 0 | 0 | 10,575 | |
402 | Strictly Positive Matrix | [
"graphs",
"math"
] | null | null | You have matrix *a* of size *n*<=Γ<=*n*. Let's number the rows of the matrix from 1 to *n* from top to bottom, let's number the columns from 1 to *n* from left to right. Let's use *a**ij* to represent the element on the intersection of the *i*-th row and the *j*-th column.
Matrix *a* meets the following two conditions:
- for any numbers *i*,<=*j* (1<=β€<=*i*,<=*j*<=β€<=*n*) the following inequality holds: *a**ij*<=β₯<=0; - .
Matrix *b* is strictly positive, if for any numbers *i*,<=*j* (1<=β€<=*i*,<=*j*<=β€<=*n*) the inequality *b**ij*<=><=0 holds. You task is to determine if there is such integer *k*<=β₯<=1, that matrix *a**k* is strictly positive. | The first line contains integer *n* (2<=β€<=*n*<=β€<=2000) β the number of rows and columns in matrix *a*.
The next *n* lines contain the description of the rows of matrix *a*. The *i*-th line contains *n* non-negative integers *a**i*1,<=*a**i*2,<=...,<=*a**in* (0<=β€<=*a**ij*<=β€<=50). It is guaranteed that . | If there is a positive integer *k*<=β₯<=1, such that matrix *a**k* is strictly positive, print "YES" (without the quotes). Otherwise, print "NO" (without the quotes). | [
"2\n1 0\n0 1\n",
"5\n4 5 6 1 2\n1 2 3 4 5\n6 4 1 2 4\n1 1 1 1 1\n4 4 4 4 4\n"
] | [
"NO\n",
"YES\n"
] | none | [
{
"input": "2\n1 0\n0 1",
"output": "NO"
},
{
"input": "5\n4 5 6 1 2\n1 2 3 4 5\n6 4 1 2 4\n1 1 1 1 1\n4 4 4 4 4",
"output": "YES"
},
{
"input": "5\n1 1 0 0 0\n0 0 1 0 0\n0 0 0 1 0\n0 0 0 1 1\n0 0 0 0 1",
"output": "NO"
},
{
"input": "5\n1 0 0 0 0\n1 1 0 0 0\n0 1 1 0 0\n0 0 1... | 30 | 0 | 0 | 10,577 | |
802 | Send the Fool Further! (easy) | [
"dfs and similar",
"graphs",
"trees"
] | null | null | Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their common friends. Since Jenny is Irish, Heidi thinks that this might be a prank. More precisely, she suspects that the message she is asked to deliver states: "Send the fool further!", and upon reading it the recipient will ask Heidi to deliver the same message to yet another friend (that the recipient has in common with Heidi), and so on.
Heidi believes that her friends want to avoid awkward situations, so she will not be made to visit the same person (including Jenny) twice. She also knows how much it costs to travel between any two of her friends who know each other. She wants to know: what is the maximal amount of money she will waste on travel if it really is a prank?
Heidi's *n* friends are labeled 0 through *n*<=-<=1, and their network of connections forms a tree. In other words, every two of her friends *a*, *b* know each other, possibly indirectly (there is a sequence of friends starting from *a* and ending on *b* and such that each two consecutive friends in the sequence know each other directly), and there are exactly *n*<=-<=1 pairs of friends who know each other directly.
Jenny is given the number 0. | The first line of the input contains the number of friends *n* (3<=β€<=*n*<=β€<=100). The next *n*<=-<=1 lines each contain three space-separated integers *u*, *v* and *c* (0<=β€<=*u*,<=*v*<=β€<=*n*<=-<=1, 1<=β€<=*c*<=β€<=104), meaning that *u* and *v* are friends (know each other directly) and the cost for travelling between *u* and *v* is *c*.
It is guaranteed that the social network of the input forms a tree. | Output a single integer β the maximum sum of costs. | [
"4\n0 1 4\n0 2 2\n2 3 3\n",
"6\n1 2 3\n0 2 100\n1 4 2\n0 3 7\n3 5 10\n",
"11\n1 0 1664\n2 0 881\n3 2 4670\n4 2 1555\n5 1 1870\n6 2 1265\n7 2 288\n8 7 2266\n9 2 1536\n10 6 3378\n"
] | [
"5\n",
"105\n",
"5551\n"
] | In the second example, the worst-case scenario goes like this: Jenny sends Heidi to the friend labeled by number 2 (incurring a cost of 100), then friend 2 sends her to friend 1 (costing Heidi 3), and finally friend 1 relays her to friend 4 (incurring an additional cost of 2). | [
{
"input": "4\n0 1 4\n0 2 2\n2 3 3",
"output": "5"
},
{
"input": "3\n1 0 5987\n2 0 8891",
"output": "8891"
},
{
"input": "10\n1 0 518\n2 0 4071\n3 1 121\n4 2 3967\n5 3 9138\n6 2 9513\n7 3 3499\n8 2 2337\n9 4 7647",
"output": "15685"
},
{
"input": "11\n1 0 6646\n2 0 8816\n3 2 ... | 155 | 20,172,800 | 3 | 10,604 | |
723 | One-Way Reform | [
"constructive algorithms",
"dfs and similar",
"flows",
"graphs",
"greedy"
] | null | null | There are *n* cities and *m* two-way roads in Berland, each road connects two cities. It is known that there is no more than one road connecting each pair of cities, and there is no road which connects the city with itself. It is possible that there is no way to get from one city to some other city using only these roads.
The road minister decided to make a reform in Berland and to orient all roads in the country, i.e. to make each road one-way. The minister wants to maximize the number of cities, for which the number of roads that begins in the city equals to the number of roads that ends in it. | The first line contains a positive integer *t* (1<=β€<=*t*<=β€<=200)Β β the number of testsets in the input.
Each of the testsets is given in the following way. The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=200, 0<=β€<=*m*<=β€<=*n*Β·(*n*<=-<=1)<=/<=2)Β β the number of cities and the number of roads in Berland.
The next *m* lines contain the description of roads in Berland. Each line contains two integers *u* and *v* (1<=β€<=*u*,<=*v*<=β€<=*n*)Β β the cities the corresponding road connects. It's guaranteed that there are no self-loops and multiple roads. It is possible that there is no way along roads between a pair of cities.
It is guaranteed that the total number of cities in all testset of input data doesn't exceed 200.
Pay attention that for hacks, you can only use tests consisting of one testset, so *t* should be equal to one. | For each testset print the maximum number of such cities that the number of roads that begins in the city, is equal to the number of roads that ends in it.
In the next *m* lines print oriented roads. First print the number of the city where the road begins and then the number of the city where the road ends. If there are several answers, print any of them. It is allowed to print roads in each test in arbitrary order. Each road should be printed exactly once. | [
"2\n5 5\n2 1\n4 5\n2 3\n1 3\n3 5\n7 2\n3 7\n4 2\n"
] | [
"3\n1 3\n3 5\n5 4\n3 2\n2 1\n3\n2 4\n3 7\n"
] | none | [
{
"input": "2\n5 5\n2 1\n4 5\n2 3\n1 3\n3 5\n7 2\n3 7\n4 2",
"output": "3\n1 3\n3 5\n5 4\n3 2\n2 1\n3\n2 4\n3 7"
},
{
"input": "4\n9 17\n3 6\n2 6\n6 9\n4 1\n2 8\n1 9\n7 9\n8 5\n1 7\n4 9\n6 7\n3 4\n9 3\n8 4\n2 1\n3 8\n2 7\n5 6\n2 5\n3 4\n1 3\n4 5\n5 3\n2 3\n12 8\n10 2\n9 2\n6 9\n10 6\n8 2\n4 10\n11 2... | 46 | 0 | 0 | 10,642 | |
70 | Text Messaging | [
"expression parsing",
"greedy",
"strings"
] | B. Text Messaging | 1 | 256 | Fangy the little walrus, as all the modern walruses, loves to communicate via text messaging. One day he faced the following problem: When he sends large texts, they are split into parts each containing *n* characters (which is the size of one text message). Thus, whole sentences and words get split!
Fangy did not like it, so he faced the task of breaking the text into minimal messages on his own so that no sentence were broken into pieces when it is sent and the number of text messages to be sent would be minimal. If two consecutive sentences are in different messages, the space between them can be ignored (Fangy does not write this space).
The little walrus's text looks in the following manner:
SPACE stands for the symbol of a space.
So, how many messages did Fangy send? | The first line contains an integer *n*, which is the size of one message (2<=β€<=*n*<=β€<=255). The second line contains the text. The length of the text does not exceed 104 characters. It is guaranteed that the text satisfies the above described format. Specifically, this implies that the text is not empty. | On the first and only line print the number of text messages Fangy will need. If it is impossible to split the text, print "Impossible" without the quotes. | [
"25\nHello. I am a little walrus.\n",
"2\nHow are you?\n",
"19\nHello! Do you like fish? Why?\n"
] | [
"2\n",
"Impossible\n",
"3\n"
] | Let's take a look at the third sample. The text will be split into three messages: "Hello!", "Do you like fish?" and "Why?". | [
{
"input": "25\nHello. I am a little walrus.",
"output": "2"
},
{
"input": "2\nHow are you?",
"output": "Impossible"
},
{
"input": "19\nHello! Do you like fish? Why?",
"output": "3"
},
{
"input": "4\na. A.",
"output": "2"
},
{
"input": "191\nEvx vnxZtUSgtzH yDNXCs... | 46 | 0 | 0 | 10,683 |
0 | none | [
"none"
] | null | null | ΠΡΡΡ *n*-ΠΏΠΎΠ΄ΡΠ΅Π·Π΄Π½ΡΠΉ Π΄ΠΎΠΌ, Π² ΠΊΠ°ΠΆΠ΄ΠΎΠΌ ΠΏΠΎΠ΄ΡΠ΅Π·Π΄Π΅ ΠΏΠΎ *m* ΡΡΠ°ΠΆΠ΅ΠΉ, ΠΈ Π½Π° ΠΊΠ°ΠΆΠ΄ΠΎΠΌ ΡΡΠ°ΠΆΠ΅ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΏΠΎΠ΄ΡΠ΅Π·Π΄Π° ΡΠΎΠ²Π½ΠΎ *k* ΠΊΠ²Π°ΡΡΠΈΡ. Π’Π°ΠΊΠΈΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ, Π² Π΄ΠΎΠΌΠ΅ Π²ΡΠ΅Π³ΠΎ *n*Β·*m*Β·*k* ΠΊΠ²Π°ΡΡΠΈΡ. ΠΠ½ΠΈ ΠΏΡΠΎΠ½ΡΠΌΠ΅ΡΠΎΠ²Π°Π½Ρ Π΅ΡΡΠ΅ΡΡΠ²Π΅Π½Π½ΡΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ ΠΎΡ 1 Π΄ΠΎ *n*Β·*m*Β·*k*, ΡΠΎ Π΅ΡΡΡ ΠΏΠ΅ΡΠ²Π°Ρ ΠΊΠ²Π°ΡΡΠΈΡΠ° Π½Π° ΠΏΠ΅ΡΠ²ΠΎΠΌ ΡΡΠ°ΠΆΠ΅ Π² ΠΏΠ΅ΡΠ²ΠΎΠΌ ΠΏΠΎΠ΄ΡΠ΅Π·Π΄Π΅ ΠΈΠΌΠ΅Π΅Ρ Π½ΠΎΠΌΠ΅Ρ 1, ΠΏΠ΅ΡΠ²Π°Ρ ΠΊΠ²Π°ΡΡΠΈΡΠ° Π½Π° Π²ΡΠΎΡΠΎΠΌ ΡΡΠ°ΠΆΠ΅ ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ ΠΏΠΎΠ΄ΡΠ΅Π·Π΄Π° ΠΈΠΌΠ΅Π΅Ρ Π½ΠΎΠΌΠ΅Ρ *k*<=+<=1 ΠΈ ΡΠ°ΠΊ Π΄Π°Π»Π΅Π΅. ΠΡΠΎΠ±Π΅Π½Π½ΠΎΡΡΡ ΡΡΠΎΠ³ΠΎ Π΄ΠΎΠΌΠ° ΡΠΎΡΡΠΎΠΈΡ Π² ΡΠΎΠΌ, ΡΡΠΎ ΠΎΠ½ ΠΊΡΡΠ³Π»ΡΠΉ. Π’ΠΎ Π΅ΡΡΡ Π΅ΡΠ»ΠΈ ΠΎΠ±Ρ
ΠΎΠ΄ΠΈΡΡ Π΅Π³ΠΎ ΠΏΠΎ ΡΠ°ΡΠΎΠ²ΠΎΠΉ ΡΡΡΠ΅Π»ΠΊΠ΅, ΡΠΎ ΠΏΠΎΡΠ»Π΅ ΠΏΠΎΠ΄ΡΠ΅Π·Π΄Π° Π½ΠΎΠΌΠ΅Ρ 1 ΡΠ»Π΅Π΄ΡΠ΅Ρ ΠΏΠΎΠ΄ΡΠ΅Π·Π΄ Π½ΠΎΠΌΠ΅Ρ 2, Π·Π°ΡΠ΅ΠΌ ΠΏΠΎΠ΄ΡΠ΅Π·Π΄ Π½ΠΎΠΌΠ΅Ρ 3 ΠΈ ΡΠ°ΠΊ Π΄Π°Π»Π΅Π΅ Π΄ΠΎ ΠΏΠΎΠ΄ΡΠ΅Π·Π΄Π° Π½ΠΎΠΌΠ΅Ρ *n*. ΠΠΎΡΠ»Π΅ ΠΏΠΎΠ΄ΡΠ΅Π·Π΄Π° Π½ΠΎΠΌΠ΅Ρ *n* ΡΠ½ΠΎΠ²Π° ΠΈΠ΄ΡΡ ΠΏΠΎΠ΄ΡΠ΅Π·Π΄ Π½ΠΎΠΌΠ΅Ρ 1.
ΠΠ΄Π²Π°ΡΠ΄ ΠΆΠΈΠ²ΡΡ Π² ΠΊΠ²Π°ΡΡΠΈΡΠ΅ Π½ΠΎΠΌΠ΅Ρ *a*, Π° ΠΠ°ΡΠ°ΡΠ°Β β Π² ΠΊΠ²Π°ΡΡΠΈΡΠ΅ Π½ΠΎΠΌΠ΅Ρ *b*. ΠΠ΅ΡΠ΅Ρ
ΠΎΠ΄ Π½Π° 1 ΡΡΠ°ΠΆ Π²Π²Π΅ΡΡ
ΠΈΠ»ΠΈ Π²Π½ΠΈΠ· ΠΏΠΎ Π»Π΅ΡΡΠ½ΠΈΡΠ΅ Π·Π°Π½ΠΈΠΌΠ°Π΅Ρ 5 ΡΠ΅ΠΊΡΠ½Π΄, ΠΏΠ΅ΡΠ΅Ρ
ΠΎΠ΄ ΠΎΡ Π΄Π²Π΅ΡΠΈ ΠΏΠΎΠ΄ΡΠ΅Π·Π΄Π° ΠΊ Π΄Π²Π΅ΡΠΈ ΡΠΎΡΠ΅Π΄Π½Π΅Π³ΠΎ ΠΏΠΎΠ΄ΡΠ΅Π·Π΄Π°Β β 15 ΡΠ΅ΠΊΡΠ½Π΄, Π° ΠΏΠ΅ΡΠ΅Ρ
ΠΎΠ΄ Π² ΠΏΡΠ΅Π΄Π΅Π»Π°Ρ
ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΡΡΠ°ΠΆΠ° ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΏΠΎΠ΄ΡΠ΅Π·Π΄Π° ΠΏΡΠΎΠΈΡΡ
ΠΎΠ΄ΠΈΡ ΠΌΠ³Π½ΠΎΠ²Π΅Π½Π½ΠΎ. Π’Π°ΠΊΠΆΠ΅ Π² ΠΊΠ°ΠΆΠ΄ΠΎΠΌ ΠΏΠΎΠ΄ΡΠ΅Π·Π΄Π΅ Π΄ΠΎΠΌΠ° Π΅ΡΡΡ Π»ΠΈΡΡ. ΠΠ½ ΡΡΡΡΠΎΠ΅Π½ ΡΠ»Π΅Π΄ΡΡΡΠΈΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ: ΠΎΠ½ Π²ΡΠ΅Π³Π΄Π° ΠΏΡΠΈΠ΅Π·ΠΆΠ°Π΅Ρ ΡΠΎΠ²Π½ΠΎ ΡΠ΅ΡΠ΅Π· 10 ΡΠ΅ΠΊΡΠ½Π΄ ΠΏΠΎΡΠ»Π΅ Π²ΡΠ·ΠΎΠ²Π°, Π° ΡΡΠΎΠ±Ρ ΠΏΠ΅ΡΠ΅ΠΌΠ΅ΡΡΠΈΡΡ ΠΏΠ°ΡΡΠ°ΠΆΠΈΡΠ° Π½Π° ΠΎΠ΄ΠΈΠ½ ΡΡΠ°ΠΆ Π²Π²Π΅ΡΡ
ΠΈΠ»ΠΈ Π²Π½ΠΈΠ·, Π»ΠΈΡΡ ΡΡΠ°ΡΠΈΡ ΡΠΎΠ²Π½ΠΎ 1 ΡΠ΅ΠΊΡΠ½Π΄Ρ. ΠΠΎΡΠ°Π΄ΠΊΠ° ΠΈ Π²ΡΡΠ°Π΄ΠΊΠ° ΠΏΡΠΎΠΈΡΡ
ΠΎΠ΄ΡΡ ΠΌΠ³Π½ΠΎΠ²Π΅Π½Π½ΠΎ.
ΠΠΎΠΌΠΎΠ³ΠΈΡΠ΅ ΠΠ΄Π²Π°ΡΠ΄Ρ Π½Π°ΠΉΡΠΈ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ Π²ΡΠ΅ΠΌΡ, Π·Π° ΠΊΠΎΡΠΎΡΠΎΠ΅ ΠΎΠ½ ΡΠΌΠΎΠΆΠ΅Ρ Π΄ΠΎΠ±ΡΠ°ΡΡΡΡ Π΄ΠΎ ΠΊΠ²Π°ΡΡΠΈΡΡ ΠΠ°ΡΠ°ΡΠΈ. Π‘ΡΠΈΡΠ°ΠΉΡΠ΅, ΡΡΠΎ ΠΠ΄Π²Π°ΡΠ΄ ΠΌΠΎΠΆΠ΅Ρ Π²ΡΠΉΡΠΈ ΠΈΠ· ΠΏΠΎΠ΄ΡΠ΅Π·Π΄Π° ΡΠΎΠ»ΡΠΊΠΎ Ρ ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ ΡΡΠ°ΠΆΠ° ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΡΡΡΠ΅Π³ΠΎ ΠΏΠΎΠ΄ΡΠ΅Π·Π΄Π° (ΡΡΠΎ ΠΏΡΠΎΠΈΡΡ
ΠΎΠ΄ΠΈΡ ΠΌΠ³Π½ΠΎΠ²Π΅Π½Π½ΠΎ). ΠΡΠ»ΠΈ ΠΠ΄Π²Π°ΡΠ΄ ΡΡΠΎΠΈΡ ΠΏΠ΅ΡΠ΅Π΄ Π΄Π²Π΅ΡΡΡ ΠΊΠ°ΠΊΠΎΠ³ΠΎ-ΡΠΎ ΠΏΠΎΠ΄ΡΠ΅Π·Π΄Π°, ΠΎΠ½ ΠΌΠΎΠΆΠ΅Ρ Π·Π°ΠΉΡΠΈ Π² Π½Π΅Π³ΠΎ ΠΈ ΡΡΠ°Π·Ρ ΠΎΠΊΠ°ΠΆΠ΅ΡΡΡ Π½Π° ΠΏΠ΅ΡΠ²ΠΎΠΌ ΡΡΠ°ΠΆΠ΅ ΡΡΠΎΠ³ΠΎ ΠΏΠΎΠ΄ΡΠ΅Π·Π΄Π° (ΡΡΠΎ ΡΠ°ΠΊΠΆΠ΅ ΠΏΡΠΎΠΈΡΡ
ΠΎΠ΄ΠΈΡ ΠΌΠ³Π½ΠΎΠ²Π΅Π½Π½ΠΎ). ΠΠ΄Π²Π°ΡΠ΄ ΠΌΠΎΠΆΠ΅Ρ Π²ΡΠ±ΠΈΡΠ°ΡΡ, Π² ΠΊΠ°ΠΊΠΎΠΌ Π½Π°ΠΏΡΠ°Π²Π»Π΅Π½ΠΈΠΈ ΠΈΠ΄ΡΠΈ Π²ΠΎΠΊΡΡΠ³ Π΄ΠΎΠΌΠ°. | Π ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
ΡΠ»Π΅Π΄ΡΡΡ ΡΡΠΈ ΡΠΈΡΠ»Π° *n*, *m*, *k* (1<=β€<=*n*,<=*m*,<=*k*<=β€<=1000)Β β ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΏΠΎΠ΄ΡΠ΅Π·Π΄ΠΎΠ² Π² Π΄ΠΎΠΌΠ΅, ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΡΠ°ΠΆΠ΅ΠΉ Π² ΠΊΠ°ΠΆΠ΄ΠΎΠΌ ΠΏΠΎΠ΄ΡΠ΅Π·Π΄Π΅ ΠΈ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΊΠ²Π°ΡΡΠΈΡ Π½Π° ΠΊΠ°ΠΆΠ΄ΠΎΠΌ ΡΡΠ°ΠΆΠ΅ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΏΠΎΠ΄ΡΠ΅Π·Π΄Π° ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²Π΅Π½Π½ΠΎ.
ΠΠΎ Π²ΡΠΎΡΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
Π·Π°ΠΏΠΈΡΠ°Π½Ρ Π΄Π²Π° ΡΠΈΡΠ»Π° *a* ΠΈ *b* (1<=β€<=*a*,<=*b*<=β€<=*n*Β·*m*Β·*k*)Β β Π½ΠΎΠΌΠ΅ΡΠ° ΠΊΠ²Π°ΡΡΠΈΡ, Π² ΠΊΠΎΡΠΎΡΡΡ
ΠΆΠΈΠ²ΡΡ ΠΠ΄Π²Π°ΡΠ΄ ΠΈ ΠΠ°ΡΠ°ΡΠ°, ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²Π΅Π½Π½ΠΎ. ΠΠ°ΡΠ°Π½ΡΠΈΡΡΠ΅ΡΡΡ, ΡΡΠΎ ΡΡΠΈ Π½ΠΎΠΌΠ΅ΡΠ° ΡΠ°Π·Π»ΠΈΡΠ½Ρ. | ΠΡΠ²Π΅Π΄ΠΈΡΠ΅ Π΅Π΄ΠΈΠ½ΡΡΠ²Π΅Π½Π½ΠΎΠ΅ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎΒ β ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ Π²ΡΠ΅ΠΌΡ (Π² ΡΠ΅ΠΊΡΠ½Π΄Π°Ρ
), Π·Π° ΠΊΠΎΡΠΎΡΠΎΠ΅ ΠΠ΄Π²Π°ΡΠ΄ ΡΠΌΠΎΠΆΠ΅Ρ Π΄ΠΎΠ±ΡΠ°ΡΡΡΡ ΠΎΡ ΡΠ²ΠΎΠ΅ΠΉ ΠΊΠ²Π°ΡΡΠΈΡΡ Π΄ΠΎ ΠΊΠ²Π°ΡΡΠΈΡΡ ΠΠ°ΡΠ°ΡΠΈ. | [
"4 10 5\n200 6\n",
"3 1 5\n7 2\n"
] | [
"39\n",
"15\n"
] | Π ΠΏΠ΅ΡΠ²ΠΎΠΌ ΡΠ΅ΡΡΠΎΠ²ΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ ΠΠ΄Π²Π°ΡΠ΄ Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡ Π² 4 ΠΏΠΎΠ΄ΡΠ΅Π·Π΄Π΅ Π½Π° 10 ΡΡΠ°ΠΆΠ΅, Π° ΠΠ°ΡΠ°ΡΠ° Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡ Π² 1 ΠΏΠΎΠ΄ΡΠ΅Π·Π΄Π΅ Π½Π° 2 ΡΡΠ°ΠΆΠ΅. ΠΠΎΡΡΠΎΠΌΡ ΠΠ΄Π²Π°ΡΠ΄Ρ Π²ΡΠ³ΠΎΠ΄Π½ΠΎ ΡΠ½Π°ΡΠ°Π»Π° ΡΠΏΡΡΡΠΈΡΡΡΡ Π½Π° Π»ΠΈΡΡΠ΅ Π½Π° ΠΏΠ΅ΡΠ²ΡΠΉ ΡΡΠ°ΠΆ (Π½Π° ΡΡΠΎ ΠΎΠ½ ΠΏΠΎΡΡΠ°ΡΠΈΡ 19 ΡΠ΅ΠΊΡΠ½Π΄, ΠΈΠ· ΠΊΠΎΡΠΎΡΡΡ
10Β β Π½Π° ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΠ΅ ΠΈ 9Β β Π½Π° ΠΏΠΎΠ΅Π·Π΄ΠΊΡ Π½Π° Π»ΠΈΡΡΠ΅), Π·Π°ΡΠ΅ΠΌ ΠΎΠ±ΠΎΠΉΡΠΈ Π΄ΠΎΠΌ ΠΏΡΠΎΡΠΈΠ² ΡΠ°ΡΠΎΠ²ΠΎΠΉ ΡΡΡΠ΅Π»ΠΊΠΈ Π΄ΠΎ ΠΏΠΎΠ΄ΡΠ΅Π·Π΄Π° Π½ΠΎΠΌΠ΅Ρ 1 (Π½Π° ΡΡΠΎ ΠΎΠ½ ΠΏΠΎΡΡΠ°ΡΠΈΡ 15 ΡΠ΅ΠΊΡΠ½Π΄), ΠΈ Π½Π°ΠΊΠΎΠ½Π΅Ρ ΠΏΠΎΠ΄Π½ΡΡΡΡΡ ΠΏΠΎ Π»Π΅ΡΡΠ½ΠΈΡΠ΅ Π½Π° ΡΡΠ°ΠΆ Π½ΠΎΠΌΠ΅Ρ 2 (Π½Π° ΡΡΠΎ ΠΎΠ½ ΠΏΠΎΡΡΠ°ΡΠΈΡ 5 ΡΠ΅ΠΊΡΠ½Π΄). Π’Π°ΠΊΠΈΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ, ΠΎΡΠ²Π΅Ρ ΡΠ°Π²Π΅Π½ 19β+β15β+β5β=β39.
ΠΠΎ Π²ΡΠΎΡΠΎΠΌ ΡΠ΅ΡΡΠΎΠ²ΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ ΠΠ΄Π²Π°ΡΠ΄ ΠΆΠΈΠ²ΡΡ Π² ΠΏΠΎΠ΄ΡΠ΅Π·Π΄Π΅ 2 Π½Π° ΡΡΠ°ΠΆΠ΅ 1, Π° ΠΠ°ΡΠ°ΡΠ° Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡ Π² ΠΏΠΎΠ΄ΡΠ΅Π·Π΄Π΅ 1 Π½Π° ΡΡΠ°ΠΆΠ΅ 1. ΠΠΎΡΡΠΎΠΌΡ ΠΠ΄Π²Π°ΡΠ΄Ρ Π²ΡΠ³ΠΎΠ΄Π½ΠΎ ΠΏΡΠΎΡΡΠΎ ΠΎΠ±ΠΎΠΉΡΠΈ Π΄ΠΎΠΌ ΠΏΠΎ ΡΠ°ΡΠΎΠ²ΠΎΠΉ ΡΡΡΠ΅Π»ΠΊΠ΅ Π΄ΠΎ ΠΏΠΎΠ΄ΡΠ΅Π·Π΄Π° 1, Π½Π° ΡΡΠΎ ΠΎΠ½ ΠΏΠΎΡΡΠ°ΡΠΈΡ 15 ΡΠ΅ΠΊΡΠ½Π΄. | [
{
"input": "4 10 5\n200 6",
"output": "39"
},
{
"input": "3 1 5\n7 2",
"output": "15"
},
{
"input": "100 100 100\n1 1000000",
"output": "124"
},
{
"input": "1000 1000 1000\n1 1000000000",
"output": "1024"
},
{
"input": "125 577 124\n7716799 6501425",
"output":... | 30 | 4,608,000 | 0 | 10,687 | |
212 | IT Restaurants | [
"dfs and similar",
"dp",
"trees"
] | null | null | Π‘ity N. has a huge problem with roads, food and IT-infrastructure. In total the city has *n* junctions, some pairs of them are connected by bidirectional roads. The road network consists of *n*<=-<=1 roads, you can get from any junction to any other one by these roads. Yes, you're right β the road network forms an undirected tree.
Recently, the Mayor came up with a way that eliminates the problems with the food and the IT-infrastructure at the same time! He decided to put at the city junctions restaurants of two well-known cafe networks for IT professionals: "iMac D0naldz" and "Burger Bing". Since the network owners are not friends, it is strictly prohibited to place two restaurants of different networks on neighboring junctions. There are other requirements. Here's the full list:
- each junction must have at most one restaurant; - each restaurant belongs either to "iMac D0naldz", or to "Burger Bing"; - each network should build at least one restaurant; - there is no pair of junctions that are connected by a road and contains restaurants of different networks.
The Mayor is going to take a large tax from each restaurant, so he is interested in making the total number of the restaurants as large as possible.
Help the Mayor to analyze the situation. Find all such pairs of (*a*,<=*b*) that *a* restaurants can belong to "iMac D0naldz", *b* restaurants can belong to "Burger Bing", and the sum of *a*<=+<=*b* is as large as possible. | The first input line contains integer *n* (3<=β€<=*n*<=β€<=5000) β the number of junctions in the city. Next *n*<=-<=1 lines list all roads one per line. Each road is given as a pair of integers *x**i*,<=*y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=*n*) β the indexes of connected junctions. Consider the junctions indexed from 1 to *n*.
It is guaranteed that the given road network is represented by an undirected tree with *n* vertexes. | Print on the first line integer *z* β the number of sought pairs. Then print all sought pairs (*a*,<=*b*) in the order of increasing of the first component *a*. | [
"5\n1 2\n2 3\n3 4\n4 5\n",
"10\n1 2\n2 3\n3 4\n5 6\n6 7\n7 4\n8 9\n9 10\n10 4\n"
] | [
"3\n1 3\n2 2\n3 1\n",
"6\n1 8\n2 7\n3 6\n6 3\n7 2\n8 1\n"
] | The figure below shows the answers to the first test case. The junctions with "iMac D0naldz" restaurants are marked red and "Burger Bing" restaurants are marked blue.
<img class="tex-graphics" src="https://espresso.codeforces.com/acf0a2618a71a09921a44d636776197510b78cd4.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "5\n1 2\n2 3\n3 4\n4 5",
"output": "3\n1 3\n2 2\n3 1"
},
{
"input": "10\n1 2\n2 3\n3 4\n5 6\n6 7\n7 4\n8 9\n9 10\n10 4",
"output": "6\n1 8\n2 7\n3 6\n6 3\n7 2\n8 1"
},
{
"input": "3\n3 1\n2 1",
"output": "1\n1 1"
},
{
"input": "4\n4 3\n4 1\n4 2",
"output": "2\n... | 31 | 0 | 0 | 10,704 | |
914 | Ember and Storm's Tree Game | [
"combinatorics",
"dp",
"games",
"trees"
] | null | null | Ember and Storm play a game. First, Ember picks a labelled tree *T* of *n* vertices, such that the degree of every vertex is at most *d*. Then, Storm picks two distinct vertices *u* and *v* in this tree and writes down the labels of the vertices in the path from *u* to *v* in a sequence *a*1,<=*a*2... *a**k*. Finally, Ember picks any index *i* (1<=β€<=*i*<=<<=*k*) in the array. Now he performs one of the following two operations exactly once:
- flip the subrange [*i*<=+<=1,<=*k*] and add *a**i* to it. After this, the sequence becomes *a*1,<=... *a**i*,<=*a**k*<=+<=*a**i*,<=*a**k*<=-<=1<=+<=*a**i*,<=... *a**i*<=+<=1<=+<=*a**i* - negate the subrange [*i*<=+<=1,<=*k*] and add *a**i* to it. i.e., the array becomes *a*1,<=... *a**i*,<=<=-<=*a**i*<=+<=1<=+<=*a**i*,<=<=-<=*a**i*<=+<=2<=+<=*a**i*,<=...<=-<=*a**k*<=+<=*a**i*
Ember wins if the array is monotonically increasing or decreasing after this. Otherwise Storm wins.
The game can be described by the tuple (*T*,<=*u*,<=*v*,<=*i*,<=*op*) where *op* is Β«flipΒ» or Β«negateΒ» depending on the action Ember chose in the last turn. Find the number of tuples that can occur if Ember and Storm play optimally. When they play optimally, if there are multiple moves by which they are guaranteed to win, then they may play any of the winning moves. Otherwise, if someone loses no matter what they play, then they may play any of the possible moves.
Report the answer modulo *m*. | The input consists of a single line containing three integers *n*, *d* and *m* (2<=β€<=*n*<=β€<=200,<=1<=β€<=*d*<=<<=*n*,<=1<=β€<=*m*<=β€<=2Β·109). | Print a single number Β β the number of possible tuples if Ember and Storm play as described, modulo *m*. | [
"2 1 1000000007\n",
"3 1 250\n",
"3 2 100\n"
] | [
"4\n",
"0\n",
"36\n"
] | In the first sample case, there is only one possible tree. There are two possible paths, 1 to 2 and 2 to 1. For both paths, *i* can only be 1, and *op* can take both possibilities. Therefore, the answer is 4.
In the second sample, there are no possible trees.
In the third sample, there are three possible trees. | [] | 15 | 0 | 0 | 10,706 | |
285 | Permutation Sum | [
"bitmasks",
"combinatorics",
"dp",
"implementation",
"meet-in-the-middle"
] | null | null | Permutation *p* is an ordered set of integers *p*1,<=<=*p*2,<=<=...,<=<=*p**n*, consisting of *n* distinct positive integers, each of them doesn't exceed *n*. We'll denote the *i*-th element of permutation *p* as *p**i*. We'll call number *n* the size or the length of permutation *p*1,<=<=*p*2,<=<=...,<=<=*p**n*.
Petya decided to introduce the sum operation on the set of permutations of length *n*. Let's assume that we are given two permutations of length *n*: *a*1,<=*a*2,<=...,<=*a**n* and *b*1,<=*b*2,<=...,<=*b**n*. Petya calls the sum of permutations *a* and *b* such permutation *c* of length *n*, where *c**i*<==<=((*a**i*<=-<=1<=+<=*b**i*<=-<=1) *mod* *n*)<=+<=1 (1<=β€<=*i*<=β€<=*n*).
Operation means taking the remainder after dividing number *x* by number *y*.
Obviously, not for all permutations *a* and *b* exists permutation *c* that is sum of *a* and *b*. That's why Petya got sad and asked you to do the following: given *n*, count the number of such pairs of permutations *a* and *b* of length *n*, that exists permutation *c* that is sum of *a* and *b*. The pair of permutations *x*,<=*y* (*x*<=β <=*y*) and the pair of permutations *y*,<=*x* are considered distinct pairs.
As the answer can be rather large, print the remainder after dividing it by 1000000007 (109<=+<=7). | The single line contains integer *n* (1<=β€<=*n*<=β€<=16). | In the single line print a single non-negative integer β the number of such pairs of permutations *a* and *b*, that exists permutation *c* that is sum of *a* and *b*, modulo 1000000007 (109<=+<=7). | [
"3\n",
"5\n"
] | [
"18\n",
"1800\n"
] | none | [
{
"input": "3",
"output": "18"
},
{
"input": "5",
"output": "1800"
},
{
"input": "13",
"output": "695720788"
},
{
"input": "1",
"output": "1"
},
{
"input": "2",
"output": "0"
},
{
"input": "4",
"output": "0"
},
{
"input": "6",
"output":... | 122 | 0 | 3 | 10,750 | |
586 | Laurenty and Shop | [
"implementation"
] | null | null | A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.
The town where Laurenty lives in is not large. The houses in it are located in two rows, *n* houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.
The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.
Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.
The traffic light on the crosswalk from the *j*-th house of the *i*-th row to the (*j*<=+<=1)-th house of the same row has waiting time equal to *a**ij* (1<=β€<=*i*<=β€<=2,<=1<=β€<=*j*<=β€<=*n*<=-<=1). For the traffic light on the crossing from the *j*-th house of one row to the *j*-th house of another row the waiting time equals *b**j* (1<=β€<=*j*<=β€<=*n*). The city doesn't have any other crossings.
The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing.
Help Laurenty determine the minimum total time he needs to wait at the crossroads. | The first line of the input contains integer *n* (2<=β€<=*n*<=β€<=50) β the number of houses in each row.
Each of the next two lines contains *n*<=-<=1 space-separated integer β values *a**ij* (1<=β€<=*a**ij*<=β€<=100).
The last line contains *n* space-separated integers *b**j* (1<=β€<=*b**j*<=β€<=100). | Print a single integer β the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home. | [
"4\n1 2 3\n3 2 1\n3 2 2 3\n",
"3\n1 2\n3 3\n2 1 3\n",
"2\n1\n1\n1 1\n"
] | [
"12\n",
"11\n",
"4\n"
] | The first sample is shown on the figure above.
In the second sample, Laurenty's path can look as follows:
- Laurenty crosses the avenue, the waiting time is 3; - Laurenty uses the second crossing in the first row, the waiting time is 2; - Laurenty uses the first crossing in the first row, the waiting time is 1; - Laurenty uses the first crossing in the first row, the waiting time is 1; - Laurenty crosses the avenue, the waiting time is 1; - Laurenty uses the second crossing in the second row, the waiting time is 3.
In the last sample Laurenty visits all the crossings, so the answer is 4. | [
{
"input": "4\n1 2 3\n3 2 1\n3 2 2 3",
"output": "12"
},
{
"input": "3\n1 2\n3 3\n2 1 3",
"output": "11"
},
{
"input": "2\n1\n1\n1 1",
"output": "4"
},
{
"input": "2\n1\n1\n2 1",
"output": "5"
},
{
"input": "3\n1 100\n1 1\n100 100 100",
"output": "204"
},
... | 62 | 0 | 3 | 10,754 | |
618 | Guess the Permutation | [
"constructive algorithms"
] | null | null | Bob has a permutation of integers from 1 to *n*. Denote this permutation as *p*. The *i*-th element of *p* will be denoted as *p**i*. For all pairs of distinct integers *i*,<=*j* between 1 and *n*, he wrote the number *a**i*,<=*j*<==<=*min*(*p**i*,<=*p**j*). He writes *a**i*,<=*i*<==<=0 for all integer *i* from 1 to *n*.
Bob gave you all the values of *a**i*,<=*j* that he wrote down. Your job is to reconstruct any permutation that could have generated these values. The input will be formed so that it is guaranteed that there is at least one solution that is consistent with the information given. | The first line of the input will contain a single integer *n* (2<=β€<=*n*<=β€<=50).
The next *n* lines will contain the values of *a**i*,<=*j*. The *j*-th number on the *i*-th line will represent *a**i*,<=*j*. The *i*-th number on the *i*-th line will be 0. It's guaranteed that *a**i*,<=*j*<==<=*a**j*,<=*i* and there is at least one solution consistent with the information given. | Print *n* space separated integers, which represents a permutation that could have generated these values. If there are multiple possible solutions, print any of them. | [
"2\n0 1\n1 0\n",
"5\n0 2 2 1 2\n2 0 4 1 3\n2 4 0 1 3\n1 1 1 0 1\n2 3 3 1 0\n"
] | [
"2 1\n",
"2 5 4 1 3\n"
] | In the first case, the answer can be {1,β2} or {2,β1}.
In the second case, another possible answer is {2,β4,β5,β1,β3}. | [
{
"input": "2\n0 1\n1 0",
"output": "2 1"
},
{
"input": "5\n0 2 2 1 2\n2 0 4 1 3\n2 4 0 1 3\n1 1 1 0 1\n2 3 3 1 0",
"output": "2 5 4 1 3"
},
{
"input": "10\n0 1 5 2 5 3 4 5 5 5\n1 0 1 1 1 1 1 1 1 1\n5 1 0 2 6 3 4 6 6 6\n2 1 2 0 2 2 2 2 2 2\n5 1 6 2 0 3 4 8 8 7\n3 1 3 2 3 0 3 3 3 3\n4 1 4... | 77 | 5,632,000 | 3 | 10,762 | |
896 | Welcome home, Chtholly | [
"data structures",
"dsu"
] | null | null | β I... I survived.
β Welcome home, Chtholly.
β I kept my promise...
β I made it... I really made it!
After several days of fighting, Chtholly Nota Seniorious miraculously returned from the fierce battle.
As promised, Willem is now baking butter cake for her.
However, although Willem is skilled in making dessert, he rarely bakes butter cake.
This time, Willem made a big mistake β he accidentally broke the oven!
Fortunately, Chtholly decided to help him.
Willem puts *n* cakes on a roll, cakes are numbered from 1 to *n*, the *i*-th cake needs *a**i* seconds of baking.
Willem needs Chtholly to do *m* operations to bake the cakes.
Operation 1: 1 *l* *r* *x*
Willem asks Chtholly to check each cake in the range [*l*,<=*r*], if the cake needs to be baked for more than *x* seconds, he would bake it for *x* seconds and put it back in its place. More precisely, for every *i* in range [*l*,<=*r*], if *a**i* is strictly more than *x*, *a**i* becomes equal *a**i*<=-<=*x*.
Operation 2: 2 *l* *r* *x*
Willem asks Chtholly to count the number of cakes in the range [*l*,<=*r*] that needs to be cooked for exactly *x* seconds. More formally you should find number of such *i* in range [*l*,<=*r*], that *a**i*<==<=*x*. | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=105).
The second line contains *n* integers, *i*-th of them is *a**i* (1<=β€<=*a**i*<=β€<=105).
The next *m* lines are the *m* operations described above. It is guaranteed that 1<=β€<=*l*<=β€<=*r*<=β€<=*n* and 1<=β€<=*x*<=β€<=105. | For each operation of the second type, print the answer. | [
"5 6\n1 5 5 5 8\n2 2 5 5\n1 2 4 3\n2 2 5 2\n2 2 5 5\n1 3 5 1\n2 1 5 1\n",
"7 7\n1 9 2 6 8 1 7\n2 1 7 1\n2 2 5 2\n1 4 7 7\n2 2 4 2\n1 3 4 5\n2 3 3 3\n2 3 7 2\n",
"8 13\n75 85 88 100 105 120 122 128\n1 1 8 70\n2 3 8 30\n1 3 8 3\n2 2 5 15\n1 2 4 10\n2 1 5 5\n1 2 7 27\n2 1 5 5\n1 3 7 12\n1 1 7 4\n2 1 8 1\n1 4 8 5\n... | [
"3\n3\n0\n3\n",
"2\n1\n1\n0\n1\n",
"1\n2\n3\n4\n5\n6\n"
] | none | [] | 30 | 5,529,600 | 0 | 10,777 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.