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
983
NN country
[ "binary search", "data structures", "trees" ]
null
null
In the NN country, there are $n$ cities, numbered from $1$ to $n$, and $n - 1$ roads, connecting them. There is a roads path between any two cities. There are $m$ bidirectional bus routes between cities. Buses drive between two cities taking the shortest path with stops in every city they drive through. Travelling by ...
The first line contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$)Β β€” the number of cities. The second line contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \le p_i < i$), where $p_i$ means that cities $p_i$ and $i$ are connected by road. The third line contains a single integer $m$ ($1 \le m \le 2 \cdo...
Print the answer for each question on a separate line. If there is no way to get from one city to another, print $-1$. Otherwise print the minimum number of buses you have to use.
[ "7\n1 1 1 4 5 6\n4\n4 2\n5 4\n1 3\n6 7\n6\n4 5\n3 5\n7 2\n4 5\n3 2\n5 3\n", "7\n1 1 2 3 4 1\n4\n4 7\n3 5\n7 6\n7 6\n6\n4 6\n3 1\n3 2\n2 7\n6 3\n5 3\n" ]
[ "1\n3\n-1\n1\n2\n3\n", "1\n-1\n-1\n1\n-1\n1\n" ]
[]
3,000
7,168,000
0
37,502
453
Little Pony and Summer Sun Celebration
[ "constructive algorithms", "dfs and similar", "graphs" ]
null
null
Twilight Sparkle learnt that the evil Nightmare Moon would return during the upcoming Summer Sun Celebration after one thousand years of imprisonment on the moon. She tried to warn her mentor Princess Celestia, but the princess ignored her and sent her to Ponyville to check on the preparations for the celebration. Twi...
The first line contains two integers *n* and *m* (2<=≀<=*n*<=≀<=105;Β 0<=≀<=*m*<=≀<=105) β€” the number of places and the number of roads in Ponyville. Each of the following *m* lines contains two integers *u**i*,<=*v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*;Β *u**i*<=β‰ <=*v**i*), these integers describe a road between places *u*...
Output the number of visited places *k* in the first line (0<=≀<=*k*<=≀<=4*n*). Then output *k* integers β€” the numbers of places in the order of path. If *x**i*<==<=0, then the *i*-th place must appear in the path even number of times, else *i*-th place must appear in the path odd number of times. Note, that given road...
[ "3 2\n1 2\n2 3\n1 1 1\n", "5 7\n1 2\n1 3\n1 4\n1 5\n3 4\n3 5\n4 5\n0 1 0 1 0\n", "2 0\n0 0\n" ]
[ "3\n1 2 3\n", "10\n2 1 3 4 5 4 5 4 3 1 ", "0\n" ]
none
[ { "input": "3 2\n1 2\n2 3\n1 1 1", "output": "3\n1 2 3" }, { "input": "5 7\n1 2\n1 3\n1 4\n1 5\n3 4\n3 5\n4 5\n0 1 0 1 0", "output": "10\n2 1 3 4 5 4 5 4 3 1 " }, { "input": "2 0\n0 0", "output": "0" }, { "input": "10 10\n2 1\n2 3\n4 2\n4 5\n3 6\n5 7\n8 4\n4 9\n5 10\n4 7\n0 0...
967
28,160,000
3
37,586
0
none
[ "none" ]
null
null
Gerald plays the following game. He has a checkered field of size *n*<=Γ—<=*n* cells, where *m* various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for *n*<=-<=1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from ...
The first line contains two space-separated integers *n* and *m* (2<=≀<=*n*<=≀<=1000, 0<=≀<=*m*<=≀<=105) β€” the size of the field and the number of banned cells. Next *m* lines each contain two space-separated integers. Specifically, the *i*-th of these lines contains numbers *x**i* and *y**i* (1<=≀<=*x**i*,<=*y**i*<=≀<...
Print a single integer β€” the maximum points Gerald can earn in this game.
[ "3 1\n2 2\n", "3 0\n", "4 3\n3 1\n3 2\n3 3\n" ]
[ "0\n", "1\n", "1\n" ]
In the first test the answer equals zero as we can't put chips into the corner cells. In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips. In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4).
[ { "input": "3 1\n2 2", "output": "0" }, { "input": "3 0", "output": "1" }, { "input": "4 3\n3 1\n3 2\n3 3", "output": "1" }, { "input": "2 1\n1 1", "output": "0" }, { "input": "2 3\n1 2\n2 1\n2 2", "output": "0" }, { "input": "5 1\n3 2", "output": ...
156
0
0
37,587
40
Interesting Sequence
[ "math" ]
D. Interesting Sequence
3
256
Berland scientists noticed long ago that the world around them depends on Berland population. Due to persistent research in this area the scientists managed to find out that the Berland chronology starts from the moment when the first two people came to that land (it is considered to have happened in the first year). A...
The first line contains integer *A* (1<=≀<=*A*<=&lt;<=10300). It is guaranteed that the number doesn't contain leading zeros.
On the first output line print YES, if there could be a year in which the total population of the country equaled *A*, otherwise print NO. If the answer is YES, then you also have to print number *k* β€” the number of years in which the population could equal *A*. On the next line you have to output precisely *k* space...
[ "2\n", "3\n", "13\n", "1729\n" ]
[ "YES\n1\n1\n0\n", "NO\n", "YES\n1\n2\n0\n", "YES\n1\n4\n1\n156\n" ]
none
[ { "input": "2", "output": "YES\n1\n1\n0" }, { "input": "3", "output": "NO" }, { "input": "13", "output": "YES\n1\n2\n0" }, { "input": "1729", "output": "YES\n1\n4\n1\n156" }, { "input": "1", "output": "NO" }, { "input": "156", "output": "YES\n1\n4\...
248
20,992,000
-1
37,614
0
none
[ "none" ]
null
null
Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his research work. The girl finds an important permutation for the resea...
The first line contains integer *n* (2<=≀<=*n*<=≀<=2000). On the second line, there are *n* integers, representing Iahub's important permutation after Iahubina replaces some values with -1. It's guaranteed that there are no fixed points in the given permutation. Also, the given sequence contains at least two numbers ...
Output a single integer, the number of ways Iahub could recover his permutation, modulo 1000000007 (109<=+<=7).
[ "5\n-1 -1 4 3 -1\n" ]
[ "2\n" ]
For the first test example there are two permutations with no fixed points are [2, 5, 4, 3, 1] and [5, 1, 4, 3, 2]. Any other permutation would have at least one fixed point.
[ { "input": "5\n-1 -1 4 3 -1", "output": "2" }, { "input": "8\n2 4 5 3 -1 8 -1 6", "output": "1" }, { "input": "7\n-1 -1 4 -1 7 1 6", "output": "4" }, { "input": "6\n-1 -1 -1 -1 -1 -1", "output": "265" }, { "input": "2\n-1 -1", "output": "1" }, { "input...
1,000
23,961,600
0
37,643
363
Fixing Typos
[ "greedy", "implementation" ]
null
null
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" ...
The single line of the input contains word *s*, its length is from 1 to 200000 characters. The given word *s* consists of lowercase English letters.
Print such word *t* that it doesn't contain any typos described in the problem statement and is obtained from *s* by deleting the least number of letters. If there are multiple solutions, print any of them.
[ "helloo\n", "woooooow\n" ]
[ "hello\n", "woow\n" ]
The second valid answer to the test from the statement is "heloo".
[ { "input": "helloo", "output": "hello" }, { "input": "woooooow", "output": "woow" }, { "input": "aabbaa", "output": "aabaa" }, { "input": "yesssssss", "output": "yess" }, { "input": "aabbaabbaabbaabbaabbaabbcccccc", "output": "aabaabaabaabaabaabcc" }, { ...
296
11,776,000
3
37,760
200
Cinema
[ "brute force", "data structures" ]
null
null
The capital of Berland has the only movie theater in the country. Besides, it consists of only one room. The room is divided into *n* rows, each row consists of *m* seats. There are *k* people lined up to the box office, each person wants to buy exactly one ticket for his own entertainment. Before the box office start...
The first input line contains three integers *n*, *m*, *k* (1<=≀<=*n*,<=*m*<=≀<=2000, 1<=≀<=*k*<=≀<=*min*(*n*Β·*m*,<=105) β€” the number of rows in the room, the number of seats in each row and the number of people in the line, correspondingly. Each of the next *k* lines contains two integers *x**i*, *y**i* (1<=≀<=*x**i*<...
Print *k* lines, each containing a pair of integers. Print on the *i*-th line *x**i*,<=*y**i* β€” the coordinates of the seat, for which the person who stands *i*-th in the line will buy the ticket.
[ "3 4 6\n1 1\n1 1\n1 1\n1 2\n1 3\n1 3\n", "4 3 12\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n" ]
[ "1 1\n1 2\n2 1\n1 3\n1 4\n2 3\n", "2 2\n1 2\n2 1\n2 3\n3 2\n1 1\n1 3\n3 1\n3 3\n4 2\n4 1\n4 3\n" ]
none
[]
1,500
38,604,800
0
37,874
785
Anton and Classes
[ "greedy", "sortings" ]
null
null
Anton likes to play chess. Also he likes to do programming. No wonder that he decided to attend chess classes and programming classes. Anton has *n* variants when he will attend chess classes, *i*-th variant is given by a period of time (*l*1,<=*i*,<=*r*1,<=*i*). Also he has *m* variants when he will attend programmin...
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=200<=000)Β β€” the number of time periods when Anton can attend chess classes. Each of the following *n* lines of the input contains two integers *l*1,<=*i* and *r*1,<=*i* (1<=≀<=*l*1,<=*i*<=≀<=*r*1,<=*i*<=≀<=109)Β β€” the *i*-th variant of a period of...
Output one integerΒ β€” the maximal possible distance between time periods.
[ "3\n1 5\n2 6\n2 3\n2\n2 4\n6 8\n", "3\n1 5\n2 6\n3 7\n2\n2 4\n1 4\n" ]
[ "3\n", "0\n" ]
In the first sample Anton can attend chess classes in the period (2, 3) and attend programming classes in the period (6, 8). It's not hard to see that in this case the distance between the periods will be equal to 3. In the second sample if he chooses any pair of periods, they will intersect. So the answer is 0.
[ { "input": "3\n1 5\n2 6\n2 3\n2\n2 4\n6 8", "output": "3" }, { "input": "3\n1 5\n2 6\n3 7\n2\n2 4\n1 4", "output": "0" }, { "input": "20\n13 141\n57 144\n82 124\n16 23\n18 44\n64 65\n117 133\n84 117\n77 142\n40 119\n105 120\n71 92\n5 142\n48 132\n106 121\n5 80\n45 92\n66 81\n7 93\n27 71\...
623
0
3
37,892
369
Valera and Fools
[ "dfs and similar", "dp", "graphs", "shortest paths" ]
null
null
One fine morning, *n* fools lined up in a row. After that, they numbered each other with numbers from 1 to *n*, inclusive. Each fool got a unique number. The fools decided not to change their numbers before the end of the fun. Every fool has exactly *k* bullets and a pistol. In addition, the fool number *i* has probab...
The first line contains two integers *n*,<=*k* (1<=≀<=*n*,<=*k*<=≀<=3000) β€” the initial number of fools and the number of bullets for each fool. The second line contains *n* integers *p*1,<=*p*2,<=...,<=*p**n* (0<=≀<=*p**i*<=≀<=100) β€” the given probabilities (in percent).
Print a single number β€” the answer to the problem.
[ "3 3\n50 50 50\n", "1 1\n100\n", "2 1\n100 100\n", "3 3\n0 0 0\n" ]
[ "7\n", "1\n", "2\n", "1\n" ]
In the first sample, any situation is possible, except for situation {1, 2}. In the second sample there is exactly one fool, so he does not make shots. In the third sample the possible situations are {1, 2} (after zero rounds) and the "empty" situation {} (after one round). In the fourth sample, the only possible si...
[ { "input": "3 3\n50 50 50", "output": "7" }, { "input": "1 1\n100", "output": "1" }, { "input": "2 1\n100 100", "output": "2" }, { "input": "3 3\n0 0 0", "output": "1" }, { "input": "5 2\n0 63 92 89 28", "output": "5" }, { "input": "103 42\n78 30 16 12...
30
0
0
37,952
868
Policeman and a Tree
[ "dp", "graphs", "trees" ]
null
null
You are given a tree (a connected non-oriented graph without cycles) with vertices numbered from 1 to *n*, and the length of the *i*-th edge is *w**i*. In the vertex *s* there is a policeman, in the vertices *x*1,<=*x*2,<=...,<=*x**m* (*x**j*<=β‰ <=*s*) *m* criminals are located. The policeman can walk along the edges w...
The first line contains single integer *n* (1<=≀<=*n*<=≀<=50)Β β€” the number of vertices in the tree. The next *n*<=-<=1 lines contain three integers each: *u**i*, *v**i*, *w**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*, 1<=≀<=*w**i*<=≀<=50) denoting edges and their lengths. It is guaranteed that the given graph is a tree. The ne...
If the policeman can't catch criminals, print single line "Terrorists win" (without quotes). Otherwise, print single integerΒ β€” the time needed to catch all criminals.
[ "4\n1 2 2\n1 3 1\n1 4 1\n2\n4\n3 1 4 1\n", "6\n1 2 3\n2 3 5\n3 4 1\n3 5 4\n2 6 3\n2\n3\n1 3 5\n" ]
[ "8\n", "21\n" ]
In the first example one of the optimal scenarios is the following. The criminal number 2 moves to vertex 3, the criminal 4Β β€” to vertex 4. The policeman goes to vertex 4 and catches two criminals. After that the criminal number 1 moves to the vertex 2. The policeman goes to vertex 3 and catches criminal 2, then goes to...
[]
31
268,390,400
0
37,976
982
The Meeting Place Cannot Be Changed
[ "dfs and similar", "graphs" ]
null
null
Petr is a detective in Braginsk. Somebody stole a huge amount of money from a bank and Petr is to catch him. Somebody told Petr that some luxurious car moves along the roads without stopping. Petr knows that it is the robbers who drive the car. The roads in Braginsk are one-directional and each of them connects two in...
The first line of the input contains two integers $n$ and $m$ ($2 \leq n \le 10^5$, $2 \leq m \leq 5 \cdot 10^5$)Β β€” the number of intersections and the number of directed roads in Braginsk, respectively. Each of the next $m$ lines contains two integers $u_i$ and $v_i$ ($1 \le u_i, v_i \le n$, $u_i \ne v_i$)Β β€” the star...
Print a single integer $k$Β β€” the intersection Petr needs to choose. If there are multiple answers, print any. If there are no such intersections, print $-1$.
[ "5 6\n1 2\n2 3\n3 1\n3 4\n4 5\n5 3\n", "3 3\n1 2\n2 3\n3 1\n" ]
[ "3", "1" ]
In the first example the robbers can move, for example, along the following routes: $(1-2-3-1)$, $(3-4-5-3)$, $(1-2-3-4-5-3-1)$. We can show that if Petr chooses the $3$-rd intersection, he will eventually meet the robbers independently of their route.
[]
15
0
0
37,983
43
Race
[ "brute force", "implementation", "two pointers" ]
E. Race
2
256
Today *s* kilometer long auto race takes place in Berland. The track is represented by a straight line as long as *s* kilometers. There are *n* cars taking part in the race, all of them start simultaneously at the very beginning of the track. For every car is known its behavior β€” the system of segments on each of which...
The first line contains two integers *n* and *s* (2<=≀<=*n*<=≀<=100,<=1<=≀<=*s*<=≀<=106) β€” the number of cars and the length of the track in kilometers. Then follow *n* lines β€” the description of the system of segments for each car. Every description starts with integer *k* (1<=≀<=*k*<=≀<=100) β€” the number of segments ...
Print the single number β€” the number of times some car managed to take the lead over another car during the race.
[ "2 33\n2 5 1 2 14\n1 3 11\n", "2 33\n2 1 3 10 3\n1 11 3\n", "5 33\n2 1 3 3 10\n1 11 3\n2 5 3 3 6\n2 3 1 10 3\n2 6 3 3 5\n" ]
[ "1\n", "0\n", "2\n" ]
none
[ { "input": "2 33\n2 5 1 2 14\n1 3 11", "output": "1" }, { "input": "2 33\n2 1 3 10 3\n1 11 3", "output": "0" }, { "input": "5 33\n2 1 3 3 10\n1 11 3\n2 5 3 3 6\n2 3 1 10 3\n2 6 3 3 5", "output": "2" }, { "input": "2 166755\n2 733 187 362 82\n3 813 147 565 57 557 27", "out...
186
70,451,200
0
38,027
808
Selling Souvenirs
[ "binary search", "dp", "greedy", "ternary search" ]
null
null
After several latest reforms many tourists are planning to visit Berland, and Berland people understood that it's an opportunity to earn money and changed their jobs to attract tourists. Petya, for example, left the IT corporation he had been working for and started to sell souvenirs at the market. This morning, as us...
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=100000, 1<=≀<=*m*<=≀<=300000) β€” the number of Petya's souvenirs and total weight that he can carry to the market. Then *n* lines follow. *i*th line contains two integers *w**i* and *c**i* (1<=≀<=*w**i*<=≀<=3, 1<=≀<=*c**i*<=≀<=109) β€” the weight and the cos...
Print one number β€” maximum possible total cost of souvenirs that Petya can carry to the market.
[ "1 1\n2 1\n", "2 2\n1 3\n2 2\n", "4 3\n3 10\n2 7\n2 8\n1 1\n" ]
[ "0\n", "3\n", "10\n" ]
none
[ { "input": "1 1\n2 1", "output": "0" }, { "input": "2 2\n1 3\n2 2", "output": "3" }, { "input": "4 3\n3 10\n2 7\n2 8\n1 1", "output": "10" }, { "input": "5 5\n3 5\n2 6\n3 2\n1 1\n1 6", "output": "13" }, { "input": "6 6\n1 6\n1 4\n1 8\n3 2\n3 2\n2 8", "output":...
62
204,800
-1
38,089
988
Rain and Umbrellas
[ "dp" ]
null
null
Polycarp lives on a coordinate line at the point $x = 0$. He goes to his friend that lives at the point $x = a$. Polycarp can move only from left to right, he can pass one unit of length each second. Now it's raining, so some segments of his way are in the rain. Formally, it's raining on $n$ non-intersecting segments,...
The first line contains three integers $a$, $n$ and $m$ ($1 \le a, m \le 2000, 1 \le n \le \lceil\frac{a}{2}\rceil$) β€” the point at which Polycarp's friend lives, the number of the segments in the rain and the number of umbrellas. Each of the next $n$ lines contains two integers $l_i$ and $r_i$ ($0 \le l_i &lt; r_i \l...
Print "-1" (without quotes) if Polycarp can't make his way from point $x = 0$ to point $x = a$. Otherwise print one integer β€” the minimum total fatigue after reaching $x = a$, if Polycarp picks up and throws away umbrellas optimally.
[ "10 2 4\n3 7\n8 10\n0 10\n3 4\n8 1\n1 2\n", "10 1 1\n0 9\n0 5\n", "10 1 1\n0 9\n1 5\n" ]
[ "14\n", "45\n", "-1\n" ]
In the first example the only possible strategy is to take the fourth umbrella at the point $x = 1$, keep it till the point $x = 7$ (the total fatigue at $x = 7$ will be equal to $12$), throw it away, move on from $x = 7$ to $x = 8$ without an umbrella, take the third umbrella at $x = 8$ and keep it till the end (the t...
[ { "input": "10 2 4\n3 7\n8 10\n0 10\n3 4\n8 1\n1 2", "output": "14" }, { "input": "10 1 1\n0 9\n0 5", "output": "45" }, { "input": "10 1 1\n0 9\n1 5", "output": "-1" }, { "input": "1 1 1\n0 1\n1 100000", "output": "-1" }, { "input": "1 1 1\n0 1\n0 100000", "ou...
155
4,710,400
3
38,121
416
Population Size
[ "greedy", "implementation", "math" ]
null
null
Polycarpus develops an interesting theory about the interrelation of arithmetic progressions with just everything in the world. His current idea is that the population of the capital of Berland changes over time like an arithmetic progression. Well, or like multiple arithmetic progressions. Polycarpus believes that if...
The first line of the input contains integer *n* (1<=≀<=*n*<=≀<=2Β·105) β€” the number of elements in the sequence. The second line contains integer values *a*1,<=*a*2,<=...,<=*a**n* separated by a space (1<=≀<=*a**i*<=≀<=109 or *a**i*<==<=<=-<=1).
Print the minimum number of arithmetic progressions that you need to write one after another to get sequence *a*. The positions marked as -1 in *a* can be represented by any positive integers.
[ "9\n8 6 4 2 1 4 7 10 2\n", "9\n-1 6 -1 2 -1 4 7 -1 2\n", "5\n-1 -1 -1 -1 -1\n", "7\n-1 -1 4 5 1 2 3\n" ]
[ "3\n", "3\n", "1\n", "2\n" ]
none
[ { "input": "9\n8 6 4 2 1 4 7 10 2", "output": "3" }, { "input": "9\n-1 6 -1 2 -1 4 7 -1 2", "output": "3" }, { "input": "5\n-1 -1 -1 -1 -1", "output": "1" }, { "input": "7\n-1 -1 4 5 1 2 3", "output": "2" }, { "input": "1\n1", "output": "1" }, { "input...
31
512,000
0
38,276
0
none
[ "none" ]
null
null
Limak is a little grizzly bear. He will once attack Deerland but now he can only destroy trees in role-playing games. Limak starts with a tree with one vertex. The only vertex has index 1 and is a root of the tree. Sometimes, a game chooses a subtree and allows Limak to attack it. When a subtree is attacked then each ...
The first line of the input contains one integer *q* (1<=≀<=*q*<=≀<=500<=000)Β β€” the number of queries. Then, *q* lines follow. The *i*-th of them contains two integers *type**i* and *v**i* (1<=≀<=*type**i*<=≀<=2). If *type**i*<==<=1 then *v**i* denotes a parent of a new vertex, while if *type**i*<==<=2 then you should...
For each query of the second type print one real numberΒ β€”the expected value of the penalty if Limak attacks the given subtree. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker ...
[ "7\n1 1\n1 1\n2 1\n1 2\n1 3\n2 2\n2 1\n", "8\n2 1\n1 1\n1 2\n1 3\n1 4\n2 1\n1 4\n2 1\n" ]
[ "0.7500000000\n0.5000000000\n1.1875000000\n", "0.0000000000\n0.9375000000\n0.9687500000\n" ]
Below, you can see the drawing for the first sample. Red circles denote queries of the second type.
[]
46
0
0
38,349
623
Transforming Sequence
[ "combinatorics", "dp", "fft", "math" ]
null
null
Let's define the transformation *P* of a sequence of integers *a*1,<=*a*2,<=...,<=*a**n* as *b*1,<=*b*2,<=...,<=*b**n*, where *b**i*<==<=*a*1Β |Β *a*2Β |Β ...Β |Β *a**i* for all *i*<==<=1,<=2,<=...,<=*n*, where | is the bitwise OR operation. Vasya consequently applies the transformation *P* to all sequences of length *n* co...
The only line of the input contains two integers *n* and *k* (1<=≀<=*n*<=≀<=1018,<=1<=≀<=*k*<=≀<=30<=000).
Print a single integerΒ β€” the answer to the problem modulo 109<=+<=7.
[ "1 2\n", "2 3\n", "3 3\n" ]
[ "3\n", "30\n", "48\n" ]
none
[]
46
0
0
38,373
379
New Year Present
[ "constructive algorithms", "implementation" ]
null
null
The New Year is coming! That's why many people today are busy preparing New Year presents. Vasily the Programmer is no exception. Vasily knows that the best present is (no, it's not a contest) money. He's put *n* empty wallets from left to right in a row and decided how much money to put in what wallet. Vasily decided...
The first line contains integer *n* (2<=≀<=*n*<=≀<=300) β€” the number of wallets. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=300). It is guaranteed that at least one *a**i* is positive.
Print the sequence that consists of *k* (1<=≀<=*k*<=≀<=106) characters, each of them equals: "L", "R" or "P". Each character of the sequence is an instruction to the robot. Character "L" orders to move to the left, character "R" orders to move to the right, character "P" orders the robot to put a coin in the wallet. Th...
[ "2\n1 2\n", "4\n0 2 0 2\n" ]
[ "PRPLRP", "RPRRPLLPLRRRP" ]
none
[ { "input": "2\n1 2", "output": "PRPLRP" }, { "input": "4\n0 2 0 2", "output": "RPRRPLLPLRRRP" }, { "input": "10\n2 3 4 0 0 1 1 3 4 2", "output": "PRPRPRRRPRPRPRPRPLPLPLLLLLPLPLPRPRPRRRRRPRPRPLPLLLLLLPLL" }, { "input": "10\n0 0 0 0 0 0 0 0 1 0", "output": "RRRRRRRRPR" },...
171
2,048,000
-1
38,396
590
Top Secret Task
[ "dp" ]
null
null
A top-secret military base under the command of Colonel Zuev is expecting an inspection from the Ministry of Defence. According to the charter, each top-secret military base must include a top-secret troop that should... well, we cannot tell you exactly what it should do, it is a top secret troop at the end. The proble...
The first line of the input contains three positive integers *n*, *k*, *s* (1<=≀<=*k*<=≀<=*n*<=≀<=150, 1<=≀<=*s*<=≀<=109)Β β€” the number of soldiers in the line, the size of the top-secret troop to be formed and the maximum possible number of swap operations of the consecutive pair of soldiers, respectively. The second ...
Print a single integer β€” the minimum possible total loquacity of the top-secret troop.
[ "3 2 2\n2 4 1\n", "5 4 2\n10 1 6 2 5\n", "5 2 3\n3 1 4 2 5\n" ]
[ "3\n", "18\n", "3\n" ]
In the first sample Colonel has to swap second and third soldiers, he doesn't really need the remaining swap. The resulting soldiers order is: (2, 1, 4). Minimum possible summary loquacity of the secret troop is 3. In the second sample Colonel will perform swaps in the following order: 1. (10, 1, 6 β€” 2, 5) 1. (10, 1...
[]
30
0
0
38,454
724
Goods transportation
[ "dp", "flows", "greedy" ]
null
null
There are *n* cities located along the one-way road. Cities are numbered from 1 to *n* in the direction of the road. The *i*-th city had produced *p**i* units of goods. No more than *s**i* units of goods can be sold in the *i*-th city. For each pair of cities *i* and *j* such that 1<=≀<=*i*<=&lt;<=*j*<=≀<=*n* you can...
The first line of the input contains two integers *n* andΒ *c* (1<=≀<=*n*<=≀<=10<=000, 0<=≀<=*c*<=≀<=109)Β β€” the number of cities and the maximum amount of goods for a single transportation. The second line contains *n* integers *p**i* (0<=≀<=*p**i*<=≀<=109)Β β€” the number of units of goods that were produced in each city...
Print the maximum total number of produced goods that can be sold in all cities after a sequence of transportations.
[ "3 0\n1 2 3\n3 2 1\n", "5 1\n7 4 2 1 0\n1 2 3 4 5\n", "4 3\n13 10 7 4\n4 7 10 13\n" ]
[ "4\n", "12\n", "34\n" ]
none
[ { "input": "3 0\n1 2 3\n3 2 1", "output": "4" }, { "input": "5 1\n7 4 2 1 0\n1 2 3 4 5", "output": "12" }, { "input": "4 3\n13 10 7 4\n4 7 10 13", "output": "34" }, { "input": "10 1\n0 2 1 1 0 2 5 2 5 5\n4 0 1 4 2 4 4 5 2 3", "output": "18" }, { "input": "10 3\n10...
46
4,915,200
0
38,488
367
Sereja ans Anagrams
[ "binary search", "data structures" ]
null
null
Sereja has two sequences *a* and *b* and number *p*. Sequence *a* consists of *n* integers *a*1,<=*a*2,<=...,<=*a**n*. Similarly, sequence *b* consists of *m* integers *b*1,<=*b*2,<=...,<=*b**m*. As usual, Sereja studies the sequences he has. Today he wants to find the number of positions *q* (*q*<=+<=(*m*<=-<=1)Β·*p*<=...
The first line contains three integers *n*, *m* and *p* (1<=≀<=*n*,<=*m*<=≀<=2Β·105,<=1<=≀<=*p*<=≀<=2Β·105). The next line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=≀<=*a**i*<=≀<=109). The next line contains *m* integers *b*1, *b*2, ..., *b**m* (1<=≀<=*b**i*<=≀<=109).
In the first line print the number of valid *q*s. In the second line, print the valid values in the increasing order.
[ "5 3 1\n1 2 3 2 1\n1 2 3\n", "6 3 2\n1 3 2 2 3 1\n1 2 3\n" ]
[ "2\n1 3\n", "2\n1 2\n" ]
none
[ { "input": "5 3 1\n1 2 3 2 1\n1 2 3", "output": "2\n1 3" }, { "input": "6 3 2\n1 3 2 2 3 1\n1 2 3", "output": "2\n1 2" }, { "input": "68 16 3\n5 3 4 3 3 3 2 2 2 3 2 4 2 2 2 2 4 3 5 1 1 2 2 2 3 1 5 1 2 2 1 5 1 5 3 2 3 5 2 1 1 4 2 3 4 3 4 3 3 1 3 4 1 5 2 5 3 4 4 1 4 5 5 1 1 2 2 2\n5 4 4 3 ...
452
27,750,400
3
38,601
333
Characteristics of Rectangles
[ "binary search", "bitmasks", "brute force", "implementation", "sortings" ]
null
null
Gerald found a table consisting of *n* rows and *m* columns. As a prominent expert on rectangular tables, he immediately counted the table's properties, that is, the minimum of the numbers in the corners of the table (minimum of four numbers). However, he did not like the final value β€” it seemed to be too small. And to...
The first line contains two space-separated integers *n* and *m* (2<=≀<=*n*,<=*m*<=≀<=1000). The following *n* lines describe the table. The *i*-th of these lines lists the space-separated integers *a**i*,<=1,<=*a**i*,<=2,<=...,<=*a**i*,<=*m* (0<=≀<=*a**i*,<=*j*<=≀<=109) β€” the *m* numbers standing in the *i*-th row of ...
Print the answer to the problem.
[ "2 2\n1 2\n3 4\n", "3 3\n1 0 0\n0 1 1\n1 0 0\n" ]
[ "1\n", "0\n" ]
In the first test case Gerald cannot crop the table β€” table contains only two rows and only two columns. In the second test case if we'll crop the table, the table will contain zero in some corner cell. Also initially it contains two zeros in the corner cells, so the answer is 0.
[ { "input": "2 2\n1 2\n3 4", "output": "1" }, { "input": "3 3\n1 0 0\n0 1 1\n1 0 0", "output": "0" }, { "input": "2 2\n0 0\n0 0", "output": "0" }, { "input": "2 2\n1000000000 1000000000\n1000000000 1000000000", "output": "1000000000" }, { "input": "10 2\n1 20\n19 2...
62
0
0
38,655
771
Bear and Tree Jumps
[ "dfs and similar", "dp", "trees" ]
null
null
A tree is an undirected connected graph without cycles. The distance between two vertices is the number of edges in a simple path between them. Limak is a little polar bear. He lives in a tree that consists of *n* vertices, numbered 1 through *n*. Limak recently learned how to jump. He can jump from a vertex to any v...
The first line of the input contains two integers *n* and *k* (2<=≀<=*n*<=≀<=200<=000, 1<=≀<=*k*<=≀<=5)Β β€” the number of vertices in the tree and the maximum allowed jump distance respectively. The next *n*<=-<=1 lines describe edges in the tree. The *i*-th of those lines contains two integers *a**i* and *b**i* (1<=≀<=...
Print one integer, denoting the sum of *f*(*s*,<=*t*) over all pairs of vertices (*s*,<=*t*) such that *s*<=&lt;<=*t*.
[ "6 2\n1 2\n1 3\n2 4\n2 5\n4 6\n", "13 3\n1 2\n3 2\n4 2\n5 2\n3 6\n10 6\n6 7\n6 13\n5 8\n5 9\n9 11\n11 12\n", "3 5\n2 1\n3 1\n" ]
[ "20\n", "114\n", "3\n" ]
In the first sample, the given tree has 6 vertices and it's displayed on the drawing below. Limak can jump to any vertex within distance at most 2. For example, from the vertex 5 he can jump to any of vertices: 1, 2 and 4 (well, he can also jump to the vertex 5 itself). There are <img align="middle" class="tex-formula...
[ { "input": "6 2\n1 2\n1 3\n2 4\n2 5\n4 6", "output": "20" }, { "input": "13 3\n1 2\n3 2\n4 2\n5 2\n3 6\n10 6\n6 7\n6 13\n5 8\n5 9\n9 11\n11 12", "output": "114" }, { "input": "3 5\n2 1\n3 1", "output": "3" }, { "input": "2 1\n1 2", "output": "1" }, { "input": "2 5...
15
0
0
38,664
553
Love Triangles
[ "dfs and similar", "dsu", "graphs" ]
null
null
There are many anime that are about "love triangles": Alice loves Bob, and Charlie loves Bob as well, but Alice hates Charlie. You are thinking about an anime which has *n* characters. The characters are labeled from 1 to *n*. Every pair of two characters can either mutually love each other or mutually hate each other ...
The first line of input will contain two integers *n*,<=*m* (3<=≀<=*n*<=≀<=100<=000, 0<=≀<=*m*<=≀<=100<=000). The next *m* lines will contain the description of the known relationships. The *i*-th line will contain three integers *a**i*,<=*b**i*,<=*c**i*. If *c**i* is 1, then *a**i* and *b**i* are in love, otherwise, ...
Print a single integer equal to the number of ways to fill in the remaining pairs so that you are happy with every triangle modulo 1<=000<=000<=007.
[ "3 0\n", "4 4\n1 2 1\n2 3 1\n3 4 0\n4 1 0\n", "4 4\n1 2 1\n2 3 1\n3 4 0\n4 1 1\n" ]
[ "4\n", "1\n", "0\n" ]
In the first sample, the four ways are to: - Make everyone love each other - Make 1 and 2 love each other, and 3 hate 1 and 2 (symmetrically, we get 3 ways from this). In the second sample, the only possible solution is to make 1 and 3 love each other and 2 and 4 hate each other.
[ { "input": "3 0", "output": "4" }, { "input": "4 4\n1 2 1\n2 3 1\n3 4 0\n4 1 0", "output": "1" }, { "input": "4 4\n1 2 1\n2 3 1\n3 4 0\n4 1 1", "output": "0" }, { "input": "100000 0", "output": "303861760" }, { "input": "100 3\n1 2 0\n2 3 0\n3 1 0", "output": ...
264
25,497,600
3
38,708
453
Little Pony and Elements of Harmony
[ "dp", "matrices" ]
null
null
The Elements of Harmony are six supernatural artifacts representing subjective aspects of harmony. They are arguably the most powerful force in Equestria. The inside of Elements of Harmony can be seen as a complete graph with *n* vertices labeled from 0 to *n*<=-<=1, where *n* is a power of two, equal to 2*m*. The ene...
The first line contains three integers *m*, *t* and *p* (1<=≀<=*m*<=≀<=20;Β 0<=≀<=*t*<=≀<=1018;Β 2<=≀<=*p*<=≀<=109). The following line contains *n* (*n*<==<=2*m*) integers *e*0[*i*] (1<=≀<=*e*0[*i*]<=≀<=109;Β 0<=≀<=*i*<=&lt;<=*n*). The next line contains *m*<=+<=1 integers *b*[*i*] (0<=≀<=*b*[*i*]<=≀<=109;Β 0<=≀<=*i*<=≀<=...
Output *n* lines, the *i*-th line must contain a single integer *e**t*[*i*] modulo *p*.
[ "2 2 10000\n4 1 2 3\n0 1 0\n" ]
[ "14\n6\n6\n14\n" ]
none
[]
6,000
5,120,000
0
38,830
802
Marmots (easy)
[ "math" ]
null
null
Heidi is a statistician to the core, and she likes to study the evolution of marmot populations in each of *V* (1<=≀<=*V*<=≀<=100) villages! So it comes that every spring, when Heidi sees the first snowdrops sprout in the meadows around her barn, she impatiently dons her snowshoes and sets out to the Alps, to welcome h...
The first line of input will contain the number of villages *V* (1<=≀<=*V*<=≀<=100). The following *V* lines each describe one village. The description of each village consists of 250 space-separated integers *k*, drawn from one of the above distributions.
Output one line per village, in the same order as provided in the input. The village's line shall state poisson if the village's distribution is of the Poisson type, and uniform if the answer came from a uniform distribution.
[ "2\n92 100 99 109 93 105 103 106 101 99 ... (input is truncated)\n28 180 147 53 84 80 180 85 8 16 ... (input is truncated)" ]
[ "poisson\nuniform\n" ]
The full example input is visually represented below, along with the probability distribution function it was drawn from (the *y*-axis is labeled by its values multiplied by 250). <img class="tex-graphics" src="https://espresso.codeforces.com/77563a6378b39d03eb21012c835c6e96df776b81.png" style="max-width: 100.0%;max-h...
[]
30
0
0
38,874
838
Binary Blocks
[ "brute force" ]
null
null
You are given an image, that can be represented with a 2-d *n* by *m* grid of pixels. Each pixel of the image is either on or off, denoted by the characters "0" or "1", respectively. You would like to compress this image. You want to choose an integer *k*<=&gt;<=1 and split the image into *k* by *k* blocks. If *n* and ...
The first line of input will contain two integers *n*,<=*m* (2<=≀<=*n*,<=*m*<=≀<=2<=500), the dimensions of the image. The next *n* lines of input will contain a binary string with exactly *m* characters, representing the image.
Print a single integer, the minimum number of pixels needed to toggle to make the image compressible.
[ "3 5\n00100\n10110\n11001\n" ]
[ "5\n" ]
We first choose *k* = 2. The image is padded as follows: We can toggle the image to look as follows: We can see that this image is compressible for *k* = 2.
[ { "input": "3 5\n00100\n10110\n11001", "output": "5" } ]
62
7,065,600
-1
38,937
144
Anagram Search
[ "implementation", "strings" ]
null
null
A string *t* is called an anagram of the string *s*, if it is possible to rearrange letters in *t* so that it is identical to the string *s*. For example, the string "aab" is an anagram of the string "aba" and the string "aaa" is not. The string *t* is called a substring of the string *s* if it can be read starting fr...
The first line is non-empty string *s*, consisting of no more than 105 lowercase Latin letters and characters "?". The second line is non-empty string *p*, consisting of no more than 105 lowercase Latin letters. Please note that the length of the string *p* can exceed the length of the string *s*.
Print the single number representing the number of good substrings of string *s*. Two substrings are considered different in their positions of occurrence are different. Thus, if some string occurs several times, then it should be counted the same number of times.
[ "bb??x???\naab\n", "ab?c\nacb\n" ]
[ "2\n", "2\n" ]
Consider the first sample test. Here the string *s* has two good substrings: "b??" (after we replace the question marks we get "baa"), "???" (after we replace the question marks we get "baa"). Let's consider the second sample test. Here the string *s* has two good substrings: "ab?" ("?" can be replaced by "c"), "b?c" ...
[ { "input": "bb??x???\naab", "output": "2" }, { "input": "ab?c\nacb", "output": "2" }, { "input": "ccaac\ncbcbca", "output": "0" }, { "input": "?bba?\nbba", "output": "3" }, { "input": "aaaaa??a?a\naaa", "output": "8" }, { "input": "?bba?b?aaa\nabb", ...
62
0
0
38,962
677
Vanya and Food Processor
[ "implementation", "math" ]
null
null
Vanya smashes potato in a vertical food processor. At each moment of time the height of the potato in the processor doesn't exceed *h* and the processor smashes *k* centimeters of potato each second. If there are less than *k* centimeters remaining, than during this second processor smashes all the remaining potato. V...
The first line of the input contains integers *n*, *h* and *k* (1<=≀<=*n*<=≀<=100<=000,<=1<=≀<=*k*<=≀<=*h*<=≀<=109)Β β€” the number of pieces of potato, the height of the food processor and the amount of potato being smashed each second, respectively. The second line contains *n* integers *a**i* (1<=≀<=*a**i*<=≀<=*h*)Β β€” ...
Print a single integerΒ β€” the number of seconds required to smash all the potatoes following the process described in the problem statement.
[ "5 6 3\n5 4 3 2 1\n", "5 6 3\n5 5 5 5 5\n", "5 6 3\n1 2 1 1 1\n" ]
[ "5\n", "10\n", "2\n" ]
Consider the first sample. 1. First Vanya puts the piece of potato of height 5 into processor. At the end of the second there is only amount of height 2 remaining inside. 1. Now Vanya puts the piece of potato of height 4. At the end of the second there is amount of height 3 remaining. 1. Vanya puts the piece of he...
[ { "input": "5 6 3\n5 4 3 2 1", "output": "5" }, { "input": "5 6 3\n5 5 5 5 5", "output": "10" }, { "input": "5 6 3\n1 2 1 1 1", "output": "2" }, { "input": "10 100 80\n76 75 73 71 76 74 73 70 78 75", "output": "10" }, { "input": "10 100 88\n11 23 69 6 71 15 25 1 4...
92
14,028,800
3
39,071
288
Polo the Penguin and Trees
[ "combinatorics", "dfs and similar", "trees" ]
null
null
Little penguin Polo has got a tree β€” a non-directed connected acyclic graph, containing *n* nodes and *n*<=-<=1 edges. We will consider the tree nodes numbered by integers from 1 to *n*. Today Polo wonders, how to find the number of pairs of paths that don't have common nodes. More formally, he should find the number ...
The first line contains integer *n* (1<=≀<=*n*<=≀<=80000) β€” the number of tree nodes. Each of the following *n*<=-<=1 lines contains a pair of integers *u**i* and *v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*;Β *u**i*<=β‰ <=*v**i*) β€” the *i*-th edge of the tree. It is guaranteed that the given graph is a tree.
In a single line print a single integer β€” the answer to the problem. Please do not use the %lld specificator to read or write 64-bit numbers in Π‘++. It is recommended to use the cin, cout streams or the %I64d specificator.
[ "4\n1 2\n2 3\n3 4\n" ]
[ "2\n" ]
none
[]
1,372
51,712,000
-1
39,077
36
New Game with a Chess Piece
[ "games" ]
D. New Game with a Chess Piece
2
64
Petya and Vasya are inventing a new game that requires a rectangular board and one chess piece. At the beginning of the game the piece stands in the upper-left corner of the board. Two players move the piece in turns. Each turn the chess piece can be moved either one square to the right or one square down or jump *k* s...
The first input line contains two integers *t* and *k* (1<=≀<=*t*<=≀<=20, 1<=≀<=*k*<=≀<=109). Each of the following *t* lines contains two numbers *n*, *m* β€” the board’s length and width (1<=≀<=*n*,<=*m*<=≀<=109).
Output *t* lines that can determine the outcomes of the game on every board. Write Β«+Β» if the first player is a winner, and Β«-Β» otherwise.
[ "10 2\n1 1\n1 2\n2 1\n2 2\n1 3\n2 3\n3 1\n3 2\n3 3\n4 3\n" ]
[ "-\n+\n+\n-\n-\n+\n-\n+\n+\n+\n" ]
none
[ { "input": "10 2\n1 1\n1 2\n2 1\n2 2\n1 3\n2 3\n3 1\n3 2\n3 3\n4 3", "output": "-\n+\n+\n-\n-\n+\n-\n+\n+\n+" }, { "input": "20 2\n5 9\n6 7\n6 5\n9 5\n1 7\n7 5\n6 5\n2 10\n9 10\n5 5\n5 7\n3 3\n2 7\n6 1\n9 5\n1 1\n2 1\n5 8\n6 3\n2 9", "output": "+\n+\n-\n+\n-\n+\n-\n-\n+\n+\n+\n+\n+\n+\n+\n-\n+\n...
280
0
0
39,081
888
Xor-MST
[ "bitmasks", "constructive algorithms", "data structures" ]
null
null
You are given a complete undirected graph with *n* vertices. A number *a**i* is assigned to each vertex, and the weight of an edge between vertices *i* and *j* is equal to *a**i*<=*xor*<=*a**j*. Calculate the weight of the minimum spanning tree in this graph.
The first line contains *n* (1<=≀<=*n*<=≀<=200000) β€” the number of vertices in the graph. The second line contains *n* integers *a*1, *a*2, ..., *a**n* (0<=≀<=*a**i*<=&lt;<=230) β€” the numbers assigned to the vertices.
Print one number β€” the weight of the minimum spanning tree in the graph.
[ "5\n1 2 3 4 5\n", "4\n1 2 3 4\n" ]
[ "8\n", "8\n" ]
none
[ { "input": "5\n1 2 3 4 5", "output": "8" }, { "input": "4\n1 2 3 4", "output": "8" }, { "input": "1\n1", "output": "0" } ]
46
0
0
39,125
725
Messages on a Tree
[]
null
null
Alice and Bob are well-known for sending messages to each other. This time you have a rooted tree with Bob standing in the root node and copies of Alice standing in each of the other vertices. The root node has number 0, the rest are numbered 1 through *n*. At some moments of time some copies of Alice want to send a m...
The first line of input contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=200<=000)Β β€” the number of nodes with Alices and the number of messages. Second line contains *n* integers *p*1,<=*p*2,<=...,<=*p**n* (0<=≀<=*p**i*<=&lt;<=*i*). The integer *p**i* is the number of the parent node of node *i*. The next *m* l...
Print *m* integersΒ β€” the *i*-th of them is the moment of time when the answer for the *i*-th message will be received by the initiator.
[ "6 3\n0 1 2 3 2 5\n4 6\n6 9\n5 11\n", "3 2\n0 1 1\n2 1\n3 1\n", "8 3\n0 1 1 2 3 3 4 5\n6 1\n8 2\n4 5\n" ]
[ "14 13 11 ", "5 3 ", "7 6 11 " ]
In the first example the first message is initiated at the moment 6, reaches Bob at the moment 10, and the answer reaches the initiator at the moment 14. The second message reaches vertex 2 at the moment 11. At this moment the copy of Alice in this vertex is still waiting for the answer for the first message, so she re...
[]
30
0
0
39,131
724
Ray Tracing
[ "greedy", "hashing", "implementation", "math", "number theory", "sortings" ]
null
null
There are *k* sensors located in the rectangular room of size *n*<=Γ—<=*m* meters. The *i*-th sensor is located at point (*x**i*,<=*y**i*). All sensors are located at distinct points strictly inside the rectangle. Opposite corners of the room are located at points (0,<=0) and (*n*,<=*m*). Walls of the room are paralle...
The first line of the input contains three integers *n*, *m* and *k* (2<=≀<=*n*,<=*m*<=≀<=100<=000, 1<=≀<=*k*<=≀<=100<=000)Β β€” lengths of the room's walls and the number of sensors. Each of the following *k* lines contains two integers *x**i* and *y**i* (1<=≀<=*x**i*<=≀<=*n*<=-<=1, 1<=≀<=*y**i*<=≀<=*m*<=-<=1)Β β€” coordin...
Print *k* integers. The *i*-th of them should be equal to the number of seconds when the ray first passes through the point where the *i*-th sensor is located, or <=-<=1 if this will never happen.
[ "3 3 4\n1 1\n1 2\n2 1\n2 2\n", "3 4 6\n1 1\n2 1\n1 2\n2 2\n1 3\n2 3\n", "7 4 5\n1 3\n2 2\n5 1\n5 3\n4 3\n" ]
[ "1\n-1\n-1\n2\n", "1\n-1\n-1\n2\n5\n-1\n", "13\n2\n9\n5\n-1\n" ]
In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds. In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1...
[ { "input": "3 3 4\n1 1\n1 2\n2 1\n2 2", "output": "1\n-1\n-1\n2" }, { "input": "3 4 6\n1 1\n2 1\n1 2\n2 2\n1 3\n2 3", "output": "1\n-1\n-1\n2\n5\n-1" }, { "input": "7 4 5\n1 3\n2 2\n5 1\n5 3\n4 3", "output": "13\n2\n9\n5\n-1" }, { "input": "10 10 10\n3 8\n1 7\n2 3\n4 2\n4 8\n...
46
4,812,800
-1
39,390
264
Roadside Trees
[ "data structures", "dp" ]
null
null
Squirrel Liss loves nuts. Liss asks you to plant some nut trees. There are *n* positions (numbered 1 to *n* from west to east) to plant a tree along a street. Trees grow one meter per month. At the beginning of each month you should process one query. The query is one of the following types: 1. Plant a tree of heigh...
The first line contains two integers: *n* and *m* (1<=<=≀<=*n*<=≀<=105;Β 1<=≀<=*m*<=≀<=2Β·105) β€” the number of positions and the number of queries. Next *m* lines contains the information of queries by following formats: - If the *i*-th query is type 1, the *i*-th line contains three integers: 1, *p**i*, and *h**i* (1...
Print *m* integers β€” the length of the longest increasing subsequence after each query. Separate the numbers by whitespaces.
[ "4 6\n1 1 1\n1 4 4\n1 3 4\n2 2\n1 2 8\n2 3\n" ]
[ "1\n2\n3\n2\n2\n2\n" ]
States of street after each query you can see on the following animation: If your browser doesn't support animation png, please see the gif version here: http://212.193.37.254/codeforces/images/162/roadtree.gif
[]
124
0
0
39,451
509
Pretty Song
[ "math", "strings" ]
null
null
When Sasha was studying in the seventh grade, he started listening to music a lot. In order to evaluate which songs he likes more, he introduced the notion of the song's prettiness. The title of the song is a word consisting of uppercase Latin letters. The prettiness of the song is the prettiness of its title. Let's d...
The input contains a single string *s* (1<=≀<=|*s*|<=≀<=5Β·105) β€” the title of the song.
Print the prettiness of the song with the absolute or relative error of at most 10<=-<=6.
[ "IEAIAIO\n", "BYOB\n", "YISVOWEL\n" ]
[ "28.0000000\n", "5.8333333\n", "17.0500000\n" ]
In the first sample all letters are vowels. The simple prettiness of each substring is 1. The word of length 7 has 28 substrings. So, the prettiness of the song equals to 28.
[ { "input": "IEAIAIO", "output": "28.0000000" }, { "input": "BYOB", "output": "5.8333333" }, { "input": "YISVOWEL", "output": "17.0500000" }, { "input": "EZYYOIYUZXEVRTOUYXIQ", "output": "124.0168163" }, { "input": "MTOESEPRFEIWAIWLAFJMGBIQB", "output": "127.22...
77
13,516,800
3
39,455
10
Greedy Change
[ "constructive algorithms" ]
E. Greedy Change
2
256
Billy investigates the question of applying greedy algorithm to different spheres of life. At the moment he is studying the application of greedy algorithm to the problem about change. There is an amount of *n* coins of different face values, and the coins of each value are not limited in number. The task is to collect...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=400) β€” the amount of the coins' face values. The second line contains *n* integers *a**i* (1<=≀<=*a**i*<=≀<=109), describing the face values. It is guaranteed that *a*1<=&gt;<=*a*2<=&gt;<=...<=&gt;<=*a**n* and *a**n*<==<=1.
If greedy algorithm collects any sum in an optimal way, output -1. Otherwise output the smallest sum that greedy algorithm collects in a non-optimal way.
[ "5\n25 10 5 2 1\n", "3\n4 3 1\n" ]
[ "-1\n", "6\n" ]
none
[ { "input": "5\n25 10 5 2 1", "output": "-1" }, { "input": "3\n4 3 1", "output": "6" }, { "input": "5\n9 8 5 2 1", "output": "13" }, { "input": "5\n18 17 10 2 1", "output": "27" }, { "input": "4\n73 70 33 1", "output": "99" }, { "input": "4\n25 10 5 1",...
2,000
0
0
39,476
505
Mr. Kitayuta's Technology
[ "dfs and similar" ]
null
null
Shuseki Kingdom is the world's leading nation for innovation and technology. There are *n* cities in the kingdom, numbered from 1 to *n*. Thanks to Mr. Kitayuta's research, it has finally become possible to construct teleportation pipes between two cities. A teleportation pipe will connect two cities unidirectionally,...
The first line contains two space-separated integers *n* and *m* (2<=≀<=*n*<=≀<=105,<=1<=≀<=*m*<=≀<=105), denoting the number of the cities in Shuseki Kingdom and the number of the important pairs, respectively. The following *m* lines describe the important pairs. The *i*-th of them (1<=≀<=*i*<=≀<=*m*) contains two s...
Print the minimum required number of teleportation pipes to fulfill Mr. Kitayuta's purpose.
[ "4 5\n1 2\n1 3\n1 4\n2 3\n2 4\n", "4 6\n1 2\n1 4\n2 3\n2 4\n3 2\n3 4\n" ]
[ "3\n", "4\n" ]
For the first sample, one of the optimal ways to construct pipes is shown in the image below: For the second sample, one of the optimal ways is shown below:
[ { "input": "4 5\n1 2\n1 3\n1 4\n2 3\n2 4", "output": "3" }, { "input": "4 6\n1 2\n1 4\n2 3\n2 4\n3 2\n3 4", "output": "4" }, { "input": "4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4", "output": "3" }, { "input": "3 6\n1 2\n1 3\n2 1\n2 3\n3 1\n3 2", "output": "3" }, { "input"...
31
0
0
39,508
763
Timofey and a flat tree
[ "data structures", "graphs", "hashing", "shortest paths", "trees" ]
null
null
Little Timofey has a big treeΒ β€” an undirected connected graph with *n* vertices and no simple cycles. He likes to walk along it. His tree is flat so when he walks along it he sees it entirely. Quite naturally, when he stands on a vertex, he sees the tree as a rooted tree with the root in this vertex. Timofey assumes t...
First line contains single integer *n* (1<=≀<=*n*<=≀<=105)Β β€” number of vertices in the tree. Each of the next *n*<=-<=1 lines contains two integers *u**i* and *v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=105, *u**i*<=β‰ <=*v**i*), denoting the vertices the *i*-th edge connects. It is guaranteed that the given graph is a tree.
Print single integerΒ β€” the index of the vertex in which Timofey should stand. If there are many answers, you can print any of them.
[ "3\n1 2\n2 3\n", "7\n1 2\n4 2\n2 3\n5 6\n6 7\n3 7\n", "10\n1 7\n1 8\n9 4\n5 1\n9 2\n3 5\n10 6\n10 9\n5 10\n" ]
[ "1\n", "1\n", "2\n" ]
In the first example we can stand in the vertex 1 or in the vertex 3 so that every subtree is non-isomorphic. If we stand in the vertex 2, then subtrees of vertices 1 and 3 are isomorphic. In the second example, if we stand in the vertex 1, then only subtrees of vertices 4 and 5 are isomorphic. In the third example, ...
[]
30
0
0
39,613
467
Fedor and Essay
[ "dfs and similar", "dp", "graphs", "hashing", "strings" ]
null
null
After you had helped Fedor to find friends in the Β«Call of Soldiers 3Β» game, he stopped studying completely. Today, the English teacher told him to prepare an essay. Fedor didn't want to prepare the essay, so he asked Alex for help. Alex came to help and wrote the essay for Fedor. But Fedor didn't like the essay at all...
The first line contains a single integer *m* (1<=≀<=*m*<=≀<=105) β€” the number of words in the initial essay. The second line contains words of the essay. The words are separated by a single space. It is guaranteed that the total length of the words won't exceed 105 characters. The next line contains a single integer *...
Print two integers β€” the minimum number of letters Β«RΒ» in an optimal essay and the minimum length of an optimal essay.
[ "3\nAbRb r Zz\n4\nxR abRb\naA xr\nzz Z\nxr y\n", "2\nRuruRu fedya\n1\nruruRU fedor\n" ]
[ "2 6\n", "1 10\n" ]
none
[ { "input": "3\nAbRb r Zz\n4\nxR abRb\naA xr\nzz Z\nxr y", "output": "2 6" }, { "input": "2\nRuruRu fedya\n1\nruruRU fedor", "output": "1 10" }, { "input": "1\nffff\n1\nffff r", "output": "0 4" }, { "input": "2\nYURA YUrA\n1\nyura fedya", "output": "0 10" }, { "inp...
31
1,638,400
-1
39,656
757
Felicity's Big Secret Revealed
[ "bitmasks", "dp" ]
null
null
The gym leaders were fascinated by the evolutions which took place at Felicity camp. So, they were curious to know about the secret behind evolving Pokemon. The organizers of the camp gave the gym leaders a PokeBlock, a sequence of *n* ingredients. Each ingredient can be of type 0 or 1. Now the organizers told the gy...
The input consists of two lines. The first line consists an integer *n* (1<=≀<=*n*<=≀<=75)Β β€” the length of the PokeBlock. The next line contains the PokeBlock, a binary string of length *n*.
Output a single integer, containing the answer to the problem, i.e., the value of *s* modulo 109<=+<=7.
[ "4\n1011\n", "2\n10\n" ]
[ "10\n", "1\n" ]
In the first sample, the sets of valid cuts are: Size 2: |1|011, 1|01|1, 10|1|1, 101|1|. Size 3: |1|01|1, |10|1|1, 10|1|1|, 1|01|1|. Size 4: |10|1|1|, |1|01|1|. Hence, *f*(2) = 4, *f*(3) = 4 and *f*(4) = 2. So, the value of *s* = 10. In the second sample, the set of valid cuts is: Size 2: |1|0. Hence, *f*(2) = 1...
[ { "input": "4\n1011", "output": "10" }, { "input": "2\n10", "output": "1" }, { "input": "7\n0110011", "output": "28" }, { "input": "10\n0100011101", "output": "80" }, { "input": "12\n010010101011", "output": "298" }, { "input": "31\n1000000010111001111...
30
0
0
39,660
489
Unbearable Controversy of Being
[ "brute force", "combinatorics", "dfs and similar", "graphs" ]
null
null
Tomash keeps wandering off and getting lost while he is walking along the streets of Berland. It's no surprise! In his home town, for any pair of intersections there is exactly one way to walk from one intersection to the other one. The capital of Berland is very different! Tomash has noticed that even simple cases of...
The first line of the input contains a pair of integers *n*, *m* (1<=≀<=*n*<=≀<=3000,<=0<=≀<=*m*<=≀<=30000) β€” the number of intersections and roads, respectively. Next *m* lines list the roads, one per line. Each of the roads is given by a pair of integers *a**i*,<=*b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*;*a**i*<=β‰ <=*b**i...
Print the required number of "damn rhombi".
[ "5 4\n1 2\n2 3\n1 4\n4 3\n", "4 12\n1 2\n1 3\n1 4\n2 1\n2 3\n2 4\n3 1\n3 2\n3 4\n4 1\n4 2\n4 3\n" ]
[ "1\n", "12\n" ]
none
[ { "input": "5 4\n1 2\n2 3\n1 4\n4 3", "output": "1" }, { "input": "4 12\n1 2\n1 3\n1 4\n2 1\n2 3\n2 4\n3 1\n3 2\n3 4\n4 1\n4 2\n4 3", "output": "12" }, { "input": "1 0", "output": "0" }, { "input": "10 20\n6 10\n4 2\n1 5\n6 1\n8 9\n1 3\n2 6\n9 7\n4 5\n3 7\n9 2\n3 9\n4 8\n1 10...
1,000
81,920,000
0
39,706
185
Mushroom Scientists
[ "math", "ternary search" ]
null
null
As you very well know, the whole Universe traditionally uses three-dimensional Cartesian system of coordinates. In this system each point corresponds to three real coordinates (*x*,<=*y*,<=*z*). In this coordinate system, the distance between the center of the Universe and the point is calculated by the following formu...
The first line contains a single integer *S* (1<=≀<=*S*<=≀<=103) β€” the maximum sum of coordinates of the sought point. The second line contains three space-separated integers *a*, *b*, *c* (0<=≀<=*a*,<=*b*,<=*c*<=≀<=103) β€” the numbers that describe the metric of mushroom scientists.
Print three real numbers β€” the coordinates of the point that reaches maximum value in the metrics of mushroom scientists. If there are multiple answers, print any of them that meets the limitations. A natural logarithm of distance from the center of the Universe to the given point in the metric of mushroom scientists ...
[ "3\n1 1 1\n", "3\n2 0 0\n" ]
[ "1.0 1.0 1.0\n", "3.0 0.0 0.0\n" ]
none
[ { "input": "3\n1 1 1", "output": "1.0 1.0 1.0" }, { "input": "3\n2 0 0", "output": "3.0 0.0 0.0" }, { "input": "10\n1 6 3", "output": "1.0 6.0 3.0" }, { "input": "9\n8 2 0", "output": "7.2 1.8 0.0" }, { "input": "1\n0 9 2", "output": "0.0 0.8181818181818182 0....
466
2,662,400
-1
39,785
906
Seating of Students
[ "brute force", "constructive algorithms", "math" ]
null
null
Students went into a class to write a test and sat in some way. The teacher thought: "Probably they sat in this order to copy works of each other. I need to rearrange them in such a way that students that were neighbors are not neighbors in a new seating." The class can be represented as a matrix with *n* rows and *m*...
The only line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=105; *n*Β·*m*<=≀<=105)Β β€” the number of rows and the number of columns in the required matrix.
If there is no such matrix, output "NO" (without quotes). Otherwise in the first line output "YES" (without quotes), and in the next *n* lines output *m* integers which form the required matrix.
[ "2 4\n", "2 1\n" ]
[ "YES\n5 4 7 2 \n3 6 1 8 \n", "NO\n" ]
In the first test case the matrix initially looks like this: It's easy to see that there are no two students that are adjacent in both matrices. In the second test case there are only two possible seatings and in both of them students with numbers 1 and 2 are neighbors.
[ { "input": "2 4", "output": "YES\n5 4 7 2 \n3 6 1 8 " }, { "input": "2 1", "output": "NO" }, { "input": "1 1", "output": "YES\n1" }, { "input": "1 2", "output": "NO" }, { "input": "1 3", "output": "NO" }, { "input": "2 2", "output": "NO" }, { ...
46
5,529,600
0
39,913
535
Tavas and Pashmaks
[ "geometry", "math" ]
null
null
Tavas is a cheerleader in the new sports competition named "Pashmaks". This competition consists of two part: swimming and then running. People will immediately start running *R* meters after they finished swimming exactly *S* meters. A winner is a such person that nobody else finishes running before him/her (there ma...
The first line of input contains a single integer *n* (1<=≀<=*n*<=≀<=2<=Γ—<=105). The next *n* lines contain the details of competitors. *i*-th line contains two integers *s**i* and *r**i* (1<=≀<=*s**i*,<=*r**i*<=≀<=104).
In the first and the only line of output, print a sequence of numbers of possible winners in increasing order.
[ "3\n1 3\n2 2\n3 1\n", "3\n1 2\n1 1\n2 1\n" ]
[ "1 2 3 \n", "1 3 \n" ]
none
[]
701
23,961,600
0
39,961
131
The World is a Theatre
[ "combinatorics", "math" ]
null
null
There are *n* boys and *m* girls attending a theatre club. To set a play "The Big Bang Theory", they need to choose a group containing exactly *t* actors containing no less than 4 boys and no less than one girl. How many ways are there to choose a group? Of course, the variants that only differ in the composition of th...
The only line of the input data contains three integers *n*, *m*, *t* (4<=≀<=*n*<=≀<=30,<=1<=≀<=*m*<=≀<=30,<=5<=≀<=*t*<=≀<=*n*<=+<=*m*).
Find the required number of ways. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator.
[ "5 2 5\n", "4 3 5\n" ]
[ "10\n", "3\n" ]
none
[ { "input": "5 2 5", "output": "10" }, { "input": "4 3 5", "output": "3" }, { "input": "4 1 5", "output": "1" }, { "input": "7 3 6", "output": "168" }, { "input": "30 30 30", "output": "118264581548187697" }, { "input": "10 10 8", "output": "84990" ...
92
0
3
40,014
0
none
[ "none" ]
null
null
Sometime the classic solution are not powerful enough and we have to design our own. For the purpose of this problem you have to implement the part of the system of task scheduling. Each task should be executed at some particular moments of time. In our system you may set the exact value for the second, minute, hour, ...
The first line of the input contains six integers *s*, *m*, *h*, *day*, *date* and *month* (0<=≀<=*s*,<=*m*<=≀<=59, 0<=≀<=*h*<=≀<=23, 1<=≀<=*day*<=≀<=7, 1<=≀<=*date*<=≀<=31, 1<=≀<=*month*<=≀<=12). Each of the number can also be equal to <=-<=1. It's guaranteed, that there are infinitely many moments of time when this t...
Print *n* lines, the *i*-th of them should contain the first moment of time strictly greater than *t**i*, when the task should be executed.
[ "-1 59 23 -1 -1 -1\n6\n1467372658\n1467417540\n1467417541\n1467417598\n1467417599\n1467417600\n", "0 0 12 6 3 7\n3\n1467372658\n1467460810\n1467547200\n" ]
[ "1467417540\n1467417541\n1467417542\n1467417599\n1467503940\n1467503940\n", "1467460800\n1467547200\n1468065600\n" ]
The moment of time 1467372658 after the midnight of January 1st, 1970 is 11:30:58 July 1st, 2016.
[]
61
512,000
-1
40,051
774
Amusement Park
[ "*special", "ternary search" ]
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...
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...
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 sec...
[ { "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", ...
62
7,987,200
3
40,053
388
Fox and Card Game
[ "games", "greedy", "sortings" ]
null
null
Fox Ciel is playing a card game with her friend Fox Jiro. There are *n* piles of cards on the table. And there is a positive integer on each card. The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom...
The first line contain an integer *n* (1<=≀<=*n*<=≀<=100). Each of the next *n* lines contains a description of the pile: the first integer in the line is *s**i* (1<=≀<=*s**i*<=≀<=100) β€” the number of cards in the *i*-th pile; then follow *s**i* positive integers *c*1, *c*2, ..., *c**k*, ..., *c**s**i* (1<=≀<=*c**k*<=≀...
Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally.
[ "2\n1 100\n2 1 10\n", "1\n9 2 8 6 5 9 4 7 1 3\n", "3\n3 1 3 2\n3 5 4 6\n2 8 7\n", "3\n3 1000 1000 1000\n6 1000 1000 1000 1000 1000 1000\n5 1000 1000 1000 1000 1000\n" ]
[ "101 10\n", "30 15\n", "18 18\n", "7000 7000\n" ]
In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10. In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3.
[ { "input": "2\n1 100\n2 1 10", "output": "101 10" }, { "input": "1\n9 2 8 6 5 9 4 7 1 3", "output": "30 15" }, { "input": "3\n3 1 3 2\n3 5 4 6\n2 8 7", "output": "18 18" }, { "input": "3\n3 1000 1000 1000\n6 1000 1000 1000 1000 1000 1000\n5 1000 1000 1000 1000 1000", "out...
155
716,800
3
40,104
191
Thwarting Demonstrations
[ "binary search", "data structures", "trees" ]
null
null
It is dark times in Berland. Berlyand opposition, funded from a neighboring state, has organized a demonstration in Berland capital Bertown. Through the work of intelligence we know that the demonstrations are planned to last for *k* days. Fortunately, Berland has a special police unit, which can save the country. It ...
The first line contains two integers *n* and *k* β€” the number of soldiers in the detachment and the number of times somebody goes on duty. The second line contains *n* space-separated integers *a**i*, their absolute value doesn't exceed 109 β€” the soldiers' reliabilities. Please do not use the %lld specifier to read ...
Print a single number β€” the sought minimum reliability of the groups that go on duty during these *k* days.
[ "3 4\n1 4 2\n", "4 6\n2 -1 2 -1\n", "8 10\n1 -2 3 -4 5 -6 7 -8\n" ]
[ "4\n", "1\n", "2\n" ]
none
[ { "input": "3 4\n1 4 2", "output": "4" }, { "input": "4 6\n2 -1 2 -1", "output": "1" }, { "input": "8 10\n1 -2 3 -4 5 -6 7 -8", "output": "2" }, { "input": "10 13\n11 73 57 -34 61 38 -83 10 -88 -32", "output": "99" }, { "input": "20 31\n19 38 -67 83 -83 79 98 -8 8...
92
0
0
40,110
846
Monitor
[ "binary search", "data structures" ]
null
null
Recently Luba bought a monitor. Monitor is a rectangular matrix of size *n*<=Γ—<=*m*. But then she started to notice that some pixels cease to work properly. Luba thinks that the monitor will become broken the first moment when it contains a square *k*<=Γ—<=*k* consisting entirely of broken pixels. She knows that *q* pix...
The first line contains four integer numbers *n*,<=*m*,<=*k*,<=*q*Β (1<=≀<=*n*,<=*m*<=≀<=500,<=1<=≀<=*k*<=≀<=*min*(*n*,<=*m*),<=0<=≀<=*q*<=≀<=*n*Β·*m*) β€” the length and width of the monitor, the size of a rectangle such that the monitor is broken if there is a broken rectangle with this size, and the number of broken pix...
Print one number β€” the minimum moment the monitor became broken, or "-1" if it's still not broken after these *q* pixels stopped working.
[ "2 3 2 5\n2 1 8\n2 2 8\n1 2 1\n1 3 4\n2 3 2\n", "3 3 2 5\n1 2 2\n2 2 1\n2 3 5\n3 2 10\n2 1 100\n" ]
[ "8\n", "-1\n" ]
none
[ { "input": "2 3 2 5\n2 1 8\n2 2 8\n1 2 1\n1 3 4\n2 3 2", "output": "8" }, { "input": "3 3 2 5\n1 2 2\n2 2 1\n2 3 5\n3 2 10\n2 1 100", "output": "-1" }, { "input": "29 50 5 29\n21 42 1565821\n21 43 53275635\n21 44 2717830\n21 45 9579585\n21 46 20725775\n22 42 2568372\n22 43 9584662\n22 44...
2,000
32,358,400
0
40,156
949
A Leapfrog in the Array
[ "constructive algorithms", "math" ]
null
null
Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm. Let's consider that i...
The first line contains two integers *n* and *q* (1<=≀<=*n*<=≀<=1018, 1<=≀<=*q*<=≀<=200<=000), the number of elements in the array and the number of queries for which it is needed to find the answer. Next *q* lines contain integers *x**i* (1<=≀<=*x**i*<=≀<=*n*), the indices of cells for which it is necessary to output...
For each of *q* queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes.
[ "4 3\n2\n3\n4\n", "13 4\n10\n5\n4\n8\n" ]
[ "3\n2\n4\n", "13\n3\n8\n9\n" ]
The first example is shown in the picture. In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7].
[ { "input": "4 3\n2\n3\n4", "output": "3\n2\n4" }, { "input": "13 4\n10\n5\n4\n8", "output": "13\n3\n8\n9" }, { "input": "2 2\n1\n2", "output": "1\n2" }, { "input": "1 1\n1", "output": "1" }, { "input": "3 3\n3\n2\n1", "output": "2\n3\n1" }, { "input": ...
2,000
819,200
0
40,194
455
Function
[ "data structures" ]
null
null
Serega and Fedor play with functions. One day they came across a very interesting function. It looks like that: - *f*(1,<=*j*)<==<=*a*[*j*], 1<=≀<=*j*<=≀<=*n*. - *f*(*i*,<=*j*)<==<=*min*(*f*(*i*<=-<=1,<=*j*),<=*f*(*i*<=-<=1,<=*j*<=-<=1))<=+<=*a*[*j*], 2<=≀<=*i*<=≀<=*n*, *i*<=≀<=*j*<=≀<=*n*. Here *a* is an integer a...
The first line contains integer *n* (1<=≀<=*n*<=≀<=105) β€” the length of array *a*. The next line contains *n* integers: *a*[1],<=*a*[2],<=...,<=*a*[*n*] (0<=≀<=*a*[*i*]<=≀<=104). The next line contains integer *m* (1<=≀<=*m*<=≀<=105) β€” the number of queries. Each of the next *m* lines contains two integers: *x**i*, *y...
Print *m* lines β€” the answers to the guys' queries.
[ "6\n2 2 3 4 3 4\n4\n4 5\n3 4\n3 4\n2 3\n", "7\n1 3 2 3 4 0 2\n4\n4 5\n2 3\n1 4\n4 6\n" ]
[ "12\n9\n9\n5\n", "11\n4\n3\n0\n" ]
none
[]
31
0
0
40,236
690
Photographs (II)
[]
null
null
Zombies seem to have become much more intelligent lately – a few have somehow wandered into the base through the automatic gate. Heidi has had to beef up security, and a new gate has been installed. Unfortunately, now the questions being asked are more complicated, and even humans have trouble answering them. Can you s...
The input format is the same as in the previous version, except that the first line of every question now contains three space-separated numbers *h*, *w* and *k* (1<=≀<=*h*,<=*w*<=≀<=600, 2<=≀<=*k*<=≀<=16) – the height (number of rows) and width (number of columns) of the photograph and the number of pieces, respective...
Your program should print *q* lines. The *i*-th line should contain your answer for the *i*-th question: a space-separated sequence of *k* numbers Ο€1,<=Ο€2,<=...,<=Ο€*k* such that: - Ο€ is a permutation of {1,<=2,<=...,<=*k*}, that is, each number from 1 to *k* appears exactly once in Ο€, - for each *j*<==<=1,<=...,<=*...
[]
[]
The link to download all the necessary materials is http://assets.codeforces.com/files/690/medium_contestant_package.zip
[]
46
0
0
40,247
83
Numbers
[ "dp", "math", "number theory" ]
D. Numbers
3
256
One quite ordinary day Valera went to school (there's nowhere else he should go on a week day). In a maths lesson his favorite teacher Ms. Evans told students about divisors. Despite the fact that Valera loved math, he didn't find this particular topic interesting. Even more, it seemed so boring that he fell asleep in ...
The first and only line contains three positive integers *a*, *b*, *k* (1<=≀<=*a*<=≀<=*b*<=≀<=2Β·109,<=2<=≀<=*k*<=≀<=2Β·109).
Print on a single line the answer to the given problem.
[ "1 10 2\n", "12 23 3\n", "6 19 5\n" ]
[ "5\n", "2\n", "0\n" ]
Comments to the samples from the statement: In the first sample the answer is numbers 2, 4, 6, 8, 10. In the second one β€” 15, 21 In the third one there are no such numbers.
[ { "input": "1 10 2", "output": "5" }, { "input": "12 23 3", "output": "2" }, { "input": "6 19 5", "output": "0" }, { "input": "1 80 7", "output": "3" }, { "input": "100 1000 1009", "output": "0" }, { "input": "11 124 11", "output": "2" }, { ...
0
0
-1
40,373
691
Couple Cover
[ "brute force", "dp", "number theory" ]
null
null
Couple Cover, a wildly popular luck-based game, is about to begin! Two players must work together to construct a rectangle. A bag with *n* balls, each with an integer written on it, is placed on the table. The first player reaches in and grabs a ball randomly (all balls have equal probability of being chosen) β€” the num...
The input begins with a single positive integer *n* in its own line (1<=≀<=*n*<=≀<=106). The second line contains *n* positive integers β€” the *i*-th number in this line is equal to *a**i* (1<=≀<=*a**i*<=≀<=3Β·106), the number written on the *i*-th ball. The next line contains an integer *m* (1<=≀<=*m*<=≀<=106), the nu...
For each question, print the number of winning pairs of balls that exist for the given value of *p* in the separate line.
[ "5\n4 2 6 1 3\n4\n1 3 5 8\n", "2\n5 6\n2\n30 31\n" ]
[ "20\n18\n14\n10\n", "2\n0\n" ]
none
[ { "input": "5\n4 2 6 1 3\n4\n1 3 5 8", "output": "20\n18\n14\n10" }, { "input": "2\n5 6\n2\n30 31", "output": "2\n0" }, { "input": "2\n2000000 2000000\n1\n2000000", "output": "2" }, { "input": "1\n1\n1\n5", "output": "0" }, { "input": "10\n18 34 3 49 40 50 53 30 2...
3,000
71,372,800
0
40,426
832
Vasya and Shifts
[ "matrices" ]
null
null
Vasya has a set of 4*n* strings of equal length, consisting of lowercase English letters "a", "b", "c", "d" and "e". Moreover, the set is split into *n* groups of 4 equal strings each. Vasya also has one special string *a* of the same length, consisting of letters "a" only. Vasya wants to obtain from string *a* some f...
The first line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=500)Β β€” the number of groups of four strings in the set, and the length of all strings. Each of the next *n* lines contains a string *s* of length *m*, consisting of lowercase English letters "a", "b", "c", "d" and "e". This means that there is a gro...
For each string Vasya is interested in print the number of ways to obtain it from string *a*, modulo 109<=+<=7.
[ "1 1\nb\n2\na\ne\n", "2 4\naaaa\nbbbb\n1\ncccc\n" ]
[ "1\n1\n", "5\n" ]
In the first example, we have 4 strings "b". Then we have the only way for each string *b*: select 0 strings "b" to get "a" and select 4 strings "b" to get "e", respectively. So, we have 1 way for each request. In the second example, note that the choice of the string "aaaa" does not change anything, that is we can ch...
[]
31
0
0
40,458
746
Music in Car
[ "data structures", "greedy", "two pointers" ]
null
null
Sasha reaches the work by car. It takes exactly *k* minutes. On his way he listens to music. All songs in his playlist go one by one, after listening to the *i*-th song Sasha gets a pleasure which equals *a**i*. The *i*-th song lasts for *t**i* minutes. Before the beginning of his way Sasha turns on some song *x* and...
The first line contains three integers *n*, *w* and *k* (1<=≀<=*w*<=≀<=*n*<=≀<=2Β·105, 1<=≀<=*k*<=≀<=2Β·109)Β β€” the number of songs in the playlist, the number of songs Sasha can listen to partly and time in minutes which Sasha needs to reach work. The second line contains *n* positive integers *a*1,<=*a*2,<=...,<=*a**n...
Print the maximum pleasure Sasha can get after listening to the songs on the way to work.
[ "7 2 11\n3 4 3 5 1 4 6\n7 7 3 6 5 3 9\n", "8 4 20\n5 6 4 3 7 5 4 1\n10 12 5 12 14 8 5 8\n", "1 1 5\n6\n9\n", "1 1 3\n4\n7\n" ]
[ "12\n", "19\n", "6\n", "0\n" ]
In the first example Sasha needs to start listening from the song number 2. He should listen to it partly (for 4 minutes), then listen to the song number 3 to the end (for 3 minutes) and then partly listen to the song number 4 (for 3 minutes). After listening to these songs Sasha will get pleasure which equals 4 + 3 + ...
[ { "input": "7 2 11\n3 4 3 5 1 4 6\n7 7 3 6 5 3 9", "output": "12" }, { "input": "8 4 20\n5 6 4 3 7 5 4 1\n10 12 5 12 14 8 5 8", "output": "19" }, { "input": "1 1 5\n6\n9", "output": "6" }, { "input": "1 1 3\n4\n7", "output": "0" }, { "input": "3 1 5\n2 5 3\n4 4 5"...
46
0
0
40,497
0
none
[ "none" ]
null
null
Alexandra has a paper strip with *n* numbers on it. Let's call them *a**i* from left to right. Now Alexandra wants to split it into some pieces (possibly 1). For each piece of strip, it must satisfy: - Each piece should contain at least *l* numbers.- The difference between the maximal and the minimal number on the p...
The first line contains three space-separated integers *n*,<=*s*,<=*l* (1<=≀<=*n*<=≀<=105,<=0<=≀<=*s*<=≀<=109,<=1<=≀<=*l*<=≀<=105). The second line contains *n* integers *a**i* separated by spaces (<=-<=109<=≀<=*a**i*<=≀<=109).
Output the minimal number of strip pieces. If there are no ways to split the strip, output -1.
[ "7 2 2\n1 3 1 2 4 1 2\n", "7 2 2\n1 100 1 100 1 100 1\n" ]
[ "3\n", "-1\n" ]
For the first sample, we can split the strip into 3 pieces: [1, 3, 1], [2, 4], [1, 2]. For the second sample, we can't let 1 and 100 be on the same piece, so no solution exists.
[ { "input": "7 2 2\n1 3 1 2 4 1 2", "output": "3" }, { "input": "7 2 2\n1 100 1 100 1 100 1", "output": "-1" }, { "input": "1 0 1\n0", "output": "1" }, { "input": "6 565 2\n31 76 162 -182 -251 214", "output": "1" }, { "input": "1 0 1\n0", "output": "1" }, {...
62
0
0
40,499
267
Dominoes
[ "dfs and similar", "graphs" ]
null
null
You have a set of dominoes. Each domino is a rectangular tile with a line dividing its face into two square ends. Can you put all dominoes in a line one by one from left to right so that any two dominoes touched with the sides that had the same number of points? You can rotate the dominoes, changing the left and the ri...
The first line contains number *n* (1<=<=≀<=<=*n*<=<=≀<=<=100). Next *n* lines contains the dominoes. Each of these lines contains two numbers β€” the number of points (spots) on the left and the right half, correspondingly. The numbers of points (spots) are non-negative integers from 0 to 6.
Print "No solution", if it is impossible to arrange the dominoes in the required manner. If the solution exists, then describe any way to arrange the dominoes. You put the dominoes from left to right. In each of *n* lines print the index of the domino to put in the corresponding position and then, after a space, charac...
[ "5\n1 2\n2 4\n2 4\n6 4\n2 1\n" ]
[ "2 -\n1 -\n5 -\n3 +\n4 -\n" ]
none
[ { "input": "5\n1 2\n2 4\n2 4\n6 4\n2 1", "output": "2 -\n1 -\n5 -\n3 +\n4 -" }, { "input": "1\n0 0", "output": "1 +" }, { "input": "1\n5 5", "output": "1 +" }, { "input": "5\n0 0\n0 0\n0 0\n0 0\n0 0", "output": "1 +\n2 +\n3 +\n4 +\n5 +" }, { "input": "4\n0 0\n0 0\...
30
0
-1
40,524
717
Underfail
[ "flows" ]
null
null
You have recently fallen through a hole and, after several hours of unconsciousness, have realized you are in an underground city. On one of your regular, daily walks through the unknown, you have encountered two unusually looking skeletons called Sanz and P’pairus, who decided to accompany you and give you some puzzle...
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=500)Β β€” the length of the crossword. The second line contains the crossword string. The third line contains a single integer *m* (1<=≀<=*m*<=≀<=100)Β β€” the number of given words, and next *m* lines contain description of words: each line will have a...
Output single integerΒ β€” maximum number of points you can get.
[ "6\nabacba\n2\naba 6\nba 3\n3\n" ]
[ "12\n" ]
For example, with the string "abacba", words "aba" (6 points) and "ba" (3 points), and *x* = 3, you can get at most 12 points - the word "aba" appears once ("abacba"), while "ba" appears two times ("abacba"). Note that for *x* = 1, you could get at most 9 points, since you wouldn’t be able to count both "aba" and the f...
[ { "input": "6\nabacba\n2\naba 6\nba 3\n3", "output": "12" }, { "input": "6\nabacba\n2\naba 6\nba 3\n1", "output": "9" }, { "input": "6\nabacba\n5\naba 6\nba 3\nbac 4\ncb 3\nc 6\n2", "output": "21" }, { "input": "6\nabacba\n5\naba 6\nba 3\nbac 4\ncb 3\nc 6\n1", "output": "...
30
0
0
40,561
245
Suggested Friends
[ "brute force", "graphs" ]
null
null
Polycarpus works as a programmer in a start-up social network. His boss gave his a task to develop a mechanism for determining suggested friends. Polycarpus thought much about the task and came to the folowing conclusion. Let's say that all friendship relationships in a social network are given as *m* username pairs ...
The first line contains a single integer *m* (1<=≀<=*m*<=≀<=5000) β€” the number of pairs of friends in the social network. Next *m* lines contain pairs of names of the users who are friends with each other. The *i*-th line contains two space-separated names *a**i* and *b**i* (*a**i*<=β‰ <=*b**i*). The users' names are non...
In the first line print a single integer *n* β€” the number of network users. In next *n* lines print the number of suggested friends for each user. In the *i*-th line print the name of the user *c**i* and the number of his suggested friends *d**i* after a space. You can print information about the users in any order.
[ "5\nMike Gerald\nKate Mike\nKate Tank\nGerald Tank\nGerald David\n", "4\nvalera vanya\nvalera edik\npasha valera\nigor valera\n" ]
[ "5\nMike 1\nGerald 1\nKate 1\nTank 1\nDavid 2\n", "5\nvalera 0\nvanya 3\nedik 3\npasha 3\nigor 3\n" ]
In the first test case consider user David. Users Mike and Tank have one common friend (Gerald) with David. User Kate has no common friends with David. That's why David's suggested friends are users Mike and Tank.
[]
92
0
0
40,591
313
Ilya and Roads
[ "dp" ]
null
null
Everything is great about Ilya's city, except the roads. The thing is, the only ZooVille road is represented as *n* holes in a row. We will consider the holes numbered from 1 to *n*, from left to right. Ilya is really keep on helping his city. So, he wants to fix at least *k* holes (perharps he can fix more) on a sing...
The first line contains three integers *n*,<=*m*,<=*k* (1<=≀<=*n*<=≀<=300,<=1<=≀<=*m*<=≀<=105,<=1<=≀<=*k*<=≀<=*n*). The next *m* lines contain the companies' description. The *i*-th line contains three integers *l**i*,<=*r**i*,<=*c**i* (1<=≀<=*l**i*<=≀<=*r**i*<=≀<=*n*,<=1<=≀<=*c**i*<=≀<=109).
Print a single integer β€” the minimum money Ilya needs to fix at least *k* holes. If it is impossible to fix at least *k* holes, print -1. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
[ "10 4 6\n7 9 11\n6 9 13\n7 7 7\n3 5 6\n", "10 7 1\n3 4 15\n8 9 8\n5 6 8\n9 10 6\n1 4 2\n1 4 10\n8 10 13\n", "10 1 9\n5 10 14\n" ]
[ "17\n", "2\n", "-1\n" ]
none
[ { "input": "10 4 6\n7 9 11\n6 9 13\n7 7 7\n3 5 6", "output": "17" }, { "input": "10 7 1\n3 4 15\n8 9 8\n5 6 8\n9 10 6\n1 4 2\n1 4 10\n8 10 13", "output": "2" }, { "input": "10 1 9\n5 10 14", "output": "-1" }, { "input": "10 6 9\n6 8 7\n2 8 11\n2 6 10\n8 10 9\n2 5 8\n2 3 8", ...
62
0
0
40,715
863
Yet Another Array Queries Problem
[ "data structures", "implementation" ]
null
null
You are given an array *a* of size *n*, and *q* queries to it. There are queries of two types: - 1 *l**i* *r**i* β€” perform a cyclic shift of the segment [*l**i*,<=*r**i*] to the right. That is, for every *x* such that *l**i*<=≀<=*x*<=&lt;<=*r**i* new value of *a**x*<=+<=1 becomes equal to old value of *a**x*, and ne...
The first line contains three integer numbers *n*, *q* and *m* (1<=≀<=*n*,<=*q*<=≀<=2Β·105, 1<=≀<=*m*<=≀<=100). The second line contains *n* integer numbers *a*1, *a*2, ..., *a**n* (1<=≀<=*a**i*<=≀<=109). Then *q* lines follow. *i*-th of them contains three integer numbers *t**i*, *l**i*, *r**i*, where *t**i* is the...
Print *m* numbers, *i*-th of which is equal to the number at index *b**i* after all queries are done.
[ "6 3 5\n1 2 3 4 5 6\n2 1 3\n2 3 6\n1 1 6\n2 2 1 5 3\n" ]
[ "3 3 1 5 2 \n" ]
none
[ { "input": "6 3 5\n1 2 3 4 5 6\n2 1 3\n2 3 6\n1 1 6\n2 2 1 5 3", "output": "3 3 1 5 2 " }, { "input": "5 2 5\n64 3 4 665 2\n1 1 3\n2 1 5\n1 2 3 4 5", "output": "2 665 3 64 4 " }, { "input": "1 1 1\n474812122\n2 1 1\n1", "output": "474812122 " } ]
2,000
28,364,800
0
40,851
54
Cutting Jigsaw Puzzle
[ "hashing", "implementation" ]
B. Cutting Jigsaw Puzzle
2
256
The Hedgehog recently remembered one of his favorite childhood activities, β€” solving puzzles, and got into it with new vigor. He would sit day in, day out with his friend buried into thousands of tiny pieces of the picture, looking for the required items one by one. Soon the Hedgehog came up with a brilliant idea: ins...
The first line contains two numbers *A* and *B* which are the sizes of the picture. They are positive integers not exceeding 20. Then follow *A* lines containing *B* symbols each, describing the actual picture. The lines only contain uppercase English letters.
In the first line print the number of possible good puzzles (in other words, the number of pairs (*X*,<=*Y*) such that the puzzle with the corresponding element sizes will be good). This number should always be positive, because the whole picture is a good puzzle itself. In the second line print two numbers β€” the siz...
[ "2 4\nABDC\nABDC\n", "2 6\nABCCBA\nABCCBA\n" ]
[ "3\n2 1\n", "1\n2 6\n" ]
The picture in the first sample test has the following good puzzles: (2, 1), (2, 2), (2, 4).
[ { "input": "2 4\nABDC\nABDC", "output": "3\n2 1" }, { "input": "2 6\nABCCBA\nABCCBA", "output": "1\n2 6" }, { "input": "2 2\nAB\nCD", "output": "4\n1 1" }, { "input": "4 6\nABABAC\nBABABC\nABABAC\nCCCCCA", "output": "4\n2 3" }, { "input": "1 12\nABAAADCAAABX", ...
92
0
0
41,073
83
Doctor
[ "binary search", "math", "sortings" ]
B. Doctor
2
256
There are *n* animals in the queue to Dr. Dolittle. When an animal comes into the office, the doctor examines him, gives prescriptions, appoints tests and may appoint extra examination. Doc knows all the forest animals perfectly well and therefore knows exactly that the animal number *i* in the queue will have to visit...
The first line of input data contains two space-separated integers *n* and *k* (1<=≀<=*n*<=≀<=105, 0<=≀<=*k*<=≀<=1014). In the second line are given space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109). Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommen...
If the doctor will overall carry out less than *k* examinations, print a single number "-1" (without quotes). Otherwise, print the sequence of numbers β€” number of animals in the order in which they stand in the queue. Note that this sequence may be empty. This case is present in pretests. You can just print nothing o...
[ "3 3\n1 2 1\n", "4 10\n3 3 2 1\n", "7 10\n1 3 3 1 2 3 1\n" ]
[ "2 ", "-1\n", "6 2 3 " ]
In the first sample test: - Before examination: {1, 2, 3} - After the first examination: {2, 3} - After the second examination: {3, 2} - After the third examination: {2} In the second sample test: - Before examination: {1, 2, 3, 4, 5, 6, 7} - After the first examination: {2, 3, 4, 5, 6, 7} - After the second ...
[ { "input": "3 3\n1 2 1", "output": "2 " }, { "input": "4 10\n3 3 2 1", "output": "-1" }, { "input": "7 10\n1 3 3 1 2 3 1", "output": "6 2 3 " }, { "input": "1 0\n1", "output": "1 " }, { "input": "6 101\n9 78 54 62 2 91", "output": "4 6 2 3 " }, { "inpu...
2,000
12,595,200
0
41,080
348
Apple Tree
[ "dfs and similar", "number theory", "trees" ]
null
null
You are given a rooted tree with *n* vertices. In each leaf vertex there's a single integer β€” the number of apples in this vertex. The weight of a subtree is the sum of all numbers in this subtree leaves. For instance, the weight of a subtree that corresponds to some leaf is the number written in the leaf. A tree is...
The first line contains integer *n* (2<=≀<=*n*<=≀<=105), showing the number of vertices in the tree. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=108), *a**i* is the number of apples in the vertex number *i*. The number of apples in non-leaf vertices is guaranteed to be zero. Then ...
Print a single integer β€” the minimum number of apples to remove in order to make the tree balanced. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the sin, cout streams cin, cout or the %I64d specifier.
[ "6\n0 0 12 13 5 6\n1 2\n1 3\n1 4\n2 5\n2 6\n" ]
[ "6" ]
none
[]
60
0
0
41,096
557
Vitaly and Cycle
[ "combinatorics", "dfs and similar", "graphs", "math" ]
null
null
After Vitaly was expelled from the university, he became interested in the graph theory. Vitaly especially liked the cycles of an odd length in which each vertex occurs at most once. Vitaly was wondering how to solve the following problem. You are given an undirected graph consisting of *n* vertices and *m* edges, no...
The first line of the input contains two integers *n* and *m* (Β β€”Β the number of vertices in the graph and the number of edges in the graph. Next *m* lines contain the descriptions of the edges of the graph, one edge per line. Each edge is given by a pair of integers *a**i*, *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*)Β β€”Β the...
Print in the first line of the output two space-separated integers *t* and *w*Β β€”Β the minimum number of edges that should be added to the graph to form a simple cycle of an odd length consisting of more than one vertex where each vertex occurs at most once, and the number of ways to do this.
[ "4 4\n1 2\n1 3\n4 2\n4 3\n", "3 3\n1 2\n2 3\n3 1\n", "3 0\n" ]
[ "1 2\n", "0 1\n", "3 1\n" ]
The simple cycle is a cycle that doesn't contain any vertex twice.
[ { "input": "4 4\n1 2\n1 3\n4 2\n4 3", "output": "1 2" }, { "input": "3 3\n1 2\n2 3\n3 1", "output": "0 1" }, { "input": "3 0", "output": "3 1" }, { "input": "6 3\n1 2\n4 3\n6 5", "output": "2 12" }, { "input": "100000 0", "output": "3 166661666700000" }, {...
249
2,252,800
0
41,103
172
Calendar Reform
[ "*special", "number theory" ]
null
null
Reforms have started in Berland again! At this time, the Parliament is discussing the reform of the calendar. To make the lives of citizens of Berland more varied, it was decided to change the calendar. As more and more people are complaining that "the years fly by...", it was decided that starting from the next year t...
The only input line contains a pair of integers *a*, *n* (1<=≀<=*a*,<=*n*<=≀<=107; *a*<=+<=*n*<=-<=1<=≀<=107).
Print the required number *p*. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use cin, cout streams or the %I64d specifier.
[ "25 3\n", "50 5\n" ]
[ "30\n", "125\n" ]
A note to the first sample test. A year of 25 days will consist of one month containing 25 days. A year of 26 days will consist of 26 months, one day each. A year of 27 days will have three months, 9 days each.
[ { "input": "25 3", "output": "30" }, { "input": "50 5", "output": "125" }, { "input": "1 1", "output": "1" }, { "input": "1 2", "output": "3" }, { "input": "1 10", "output": "38" }, { "input": "1 5000000", "output": "8224640917276" }, { "in...
77
0
-1
41,108
925
May Holidays
[ "data structures", "trees" ]
null
null
It's May in Flatland, and there are $m$ days in this month. Despite the fact that May Holidays are canceled long time ago, employees of some software company still have a habit of taking short or long vacations in May. Of course, not all managers of the company like this. There are $n$ employees in the company that fo...
The first line contains two integers $n$ and $m$ ($2 \leq n, m \leq 10^5$) β€” the number of employees in the company and the number of days in May. The second line contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \leq p_i \leq n$), denoting the direct managers of employees. The third line contains $n$ integers $t...
Print a sequence of $m$ integers $a_1, a_2, \ldots, a_m$, where $a_i$ is the number of displeased employees on the $i$-th day.
[ "7 8\n4 5 1 1 5 5\n0 0 0 1 2 0 0\n2 6 3 7 -2 4 -3 1\n", "5 6\n1 2 3 4\n4 0 0 1 0\n1 5 2 3 -5 -1\n" ]
[ "1 1 1 2 2 2 1 0\n", "0 2 1 0 0 0\n" ]
In the first sample test after employee with id 2 leaves for a vacation at the first day, the head manager with id 1 becomes displeased as he does not want any of his subordinates to go for a vacation. At the fourth day employee with id 5 becomes displeased as his last remaining employee with id 7 leaves for a vacation...
[]
46
0
0
41,120
173
Deputies
[ "constructive algorithms", "graphs", "greedy", "implementation" ]
null
null
The Trinitarian kingdom has exactly *n*<==<=3*k* cities. All of them are located on the shores of river Trissisipi, which flows through the whole kingdom. Some of the cities are located on one side of the river, and all the rest are on the other side. Some cities are connected by bridges built between them. Each bridg...
The first line contains two integers *n* and *m* β€” the number of cities and bridges (3<=≀<=*n*<=&lt;<=105, *n*<==<=3*k*, 0<=≀<=*m*<=≀<=105). Next *m* lines describe the bridges. The *i*-th line contains two integers *a**i* and *b**i* β€” the numbers of cities that are connected by the *i*-th bridge (1<=≀<=*a**i*,<=*b**i*...
If distributing the deputies in the required manner is impossible, print in a single line "NO" (without the quotes). Otherwise, in the first line print "YES" (without the quotes), and in the second line print which deputy should be put in charge of each city. The *i*-th number should represent the number of the deputy...
[ "6 6\n1 2\n4 1\n3 5\n6 5\n2 6\n4 6\n", "3 1\n1 2\n" ]
[ "YES\n1 2 1 2 2 1 ", "NO" ]
none
[]
62
102,400
0
41,215
732
Cormen --- The Best Friend Of a Man
[ "dp", "greedy" ]
null
null
Recently a dog was bought for Polycarp. The dog's name is Cormen. Now Polycarp has a lot of troubles. For example, Cormen likes going for a walk. Empirically Polycarp learned that the dog needs at least *k* walks for any two consecutive days in order to feel good. For example, if *k*<==<=5 and yesterday Polycarp went...
The first line contains two integers *n* and *k* (1<=≀<=*n*,<=*k*<=≀<=500)Β β€” the number of days and the minimum number of walks with Cormen for any two consecutive days. The second line contains integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=500)Β β€” the number of walks with Cormen on the *i*-th day which Polyca...
In the first line print the smallest number of additional walks that Polycarp should do during the next *n* days so that Cormen will feel good during all days. In the second line print *n* integers *b*1,<=*b*2,<=...,<=*b**n*, where *b**i*Β β€” the total number of walks on the *i*-th day according to the found solutions ...
[ "3 5\n2 0 1\n", "3 1\n0 0 0\n", "4 6\n2 4 3 5\n" ]
[ "4\n2 3 2\n", "1\n0 1 0\n", "0\n2 4 3 5\n" ]
none
[ { "input": "3 5\n2 0 1", "output": "4\n2 3 2" }, { "input": "3 1\n0 0 0", "output": "1\n0 1 0" }, { "input": "4 6\n2 4 3 5", "output": "0\n2 4 3 5" }, { "input": "5 1\n0 0 0 0 1", "output": "2\n0 1 0 1 1" }, { "input": "10 500\n164 44 238 205 373 249 87 30 239 90"...
46
0
0
41,235
258
Little Elephant and Elections
[ "brute force", "combinatorics", "dp" ]
null
null
There have recently been elections in the zoo. Overall there were 7 main political parties: one of them is the Little Elephant Political Party, 6 other parties have less catchy names. Political parties find their number in the ballot highly important. Overall there are *m* possible numbers: 1,<=2,<=...,<=*m*. Each of ...
A single line contains a single positive integer *m* (7<=≀<=*m*<=≀<=109) β€” the number of possible numbers in the ballot.
In a single line print a single integer β€” the answer to the problem modulo 1000000007 (109<=+<=7).
[ "7\n", "8\n" ]
[ "0\n", "1440\n" ]
none
[ { "input": "7", "output": "0" }, { "input": "8", "output": "1440" }, { "input": "47", "output": "907362803" }, { "input": "10", "output": "40320" }, { "input": "9", "output": "10080" }, { "input": "11", "output": "120960" }, { "input": "25"...
60
0
0
41,238
380
Sereja and Tree
[ "graphs", "implementation" ]
null
null
Sereja adores trees. Today he came up with a revolutionary new type of binary root trees. His new tree consists of *n* levels, each vertex is indexed by two integers: the number of the level and the number of the vertex on the current level. The tree root is at level 1, its index is (1,<=1). Here is a pseudo code of t...
The first line contains integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=7000). Next *m* lines contain the descriptions of the operations. The operation of the first type is given by five integers: 1 *t* *l* *r* *x* (1<=≀<=*t*<=≀<=*n*;Β 1<=≀<=*l*<=≀<=*r*<=≀<=*cnt*[*t*];Β 1<=≀<=*x*<=≀<=106). The operation of the second type is...
For each operation of the second type, print the answer on a single line.
[ "4 5\n1 4 4 7 1\n1 3 1 2 2\n2 1 1\n2 4 1\n2 3 3\n" ]
[ "2\n0\n1\n" ]
You can find the definitions that are used while working with root trees by this link: http://en.wikipedia.org/wiki/Tree_(graph_theory) You can see an example of a constructed tree at *n* = 4 below.
[]
30
0
-1
41,329
0
none
[ "none" ]
null
null
While Duff was resting in the beach, she accidentally found a strange array *b*0,<=*b*1,<=...,<=*b**l*<=-<=1 consisting of *l* positive integers. This array was strange because it was extremely long, but there was another (maybe shorter) array, *a*0,<=...,<=*a**n*<=-<=1 that *b* can be build from *a* with formula: *b**...
The first line of input contains three integers, *n*,<=*l* and *k* (1<=≀<=*n*,<=*k*, *n*<=Γ—<=*k*<=≀<=106 and 1<=≀<=*l*<=≀<=1018). The second line contains *n* space separated integers, *a*0,<=*a*1,<=...,<=*a**n*<=-<=1 (1<=≀<=*a**i*<=≀<=109 for each 0<=≀<=*i*<=≀<=*n*<=-<=1).
Print the answer modulo 1<=000<=000<=007 in one line.
[ "3 5 3\n5 9 1\n", "5 10 3\n1 2 3 4 5\n" ]
[ "10\n", "25\n" ]
In the first sample case, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/d9d8fe92937aeef2bcddb9d213e5587f0f950087.png" style="max-width: 100.0%;max-height: 100.0%;"/>. So all such sequences are: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/54d7dd513b50dae341...
[ { "input": "3 5 3\n5 9 1", "output": "10" }, { "input": "5 10 3\n1 2 3 4 5", "output": "25" }, { "input": "1 1000000000000000000 1\n508953607", "output": "49" }, { "input": "13 1984343432234 32\n347580985 506695806 506695806 42598441 347580985 720568974 208035957 385072757 42...
2,000
26,316,800
0
41,407
471
MUH and House of Cards
[ "binary search", "brute force", "greedy", "math" ]
null
null
Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev decided to build a house of cards. For that they've already found a hefty deck of *n* playing cards. Let's describe the house they want to make: 1. The house consists of some non-zero number of floors. 1. Each f...
The single line contains integer *n* (1<=≀<=*n*<=≀<=1012) β€” the number of cards.
Print the number of distinct heights that the houses made of exactly *n* cards can have.
[ "13\n", "6\n" ]
[ "1", "0" ]
In the first sample you can build only these two houses (remember, you must use all the cards): Thus, 13 cards are enough only for two floor houses, so the answer is 1. The six cards in the second sample are not enough to build any house.
[ { "input": "13", "output": "1" }, { "input": "6", "output": "0" }, { "input": "26", "output": "2" }, { "input": "1000000000000", "output": "272165" }, { "input": "571684826707", "output": "205784" }, { "input": "178573947413", "output": "115012" ...
109
0
3
41,485
407
Curious Array
[ "brute force", "combinatorics", "implementation", "math" ]
null
null
You've got an array consisting of *n* integers: *a*[1],<=*a*[2],<=...,<=*a*[*n*]. Moreover, there are *m* queries, each query can be described by three integers *l**i*,<=*r**i*,<=*k**i*. Query *l**i*,<=*r**i*,<=*k**i* means that we should add to each element *a*[*j*], where *l**i*<=≀<=*j*<=≀<=*r**i*. Record means th...
The first line contains integers *n*, *m* (1<=≀<=*n*,<=*m*<=≀<=105). The second line contains *n* integers *a*[1],<=*a*[2],<=...,<=*a*[*n*] (0<=≀<=*a**i*<=≀<=109)Β β€” the initial array. Next *m* lines contain queries in the format *l**i*,<=*r**i*,<=*k**i*Β β€” to all elements of the segment *l**i*... *r**i* add number (1...
Print *n* integers: the *i*-th number is the value of element *a*[*i*] after all the queries. As the values can be rather large, print them modulo 1000000007 (109<=+<=7).
[ "5 1\n0 0 0 0 0\n1 5 0\n", "10 2\n1 2 3 4 5 0 0 0 0 0\n1 6 1\n6 10 2\n" ]
[ "1 1 1 1 1\n", "2 4 6 8 10 7 3 6 10 15\n" ]
none
[ { "input": "5 1\n0 0 0 0 0\n1 5 0", "output": "1 1 1 1 1" }, { "input": "10 2\n1 2 3 4 5 0 0 0 0 0\n1 6 1\n6 10 2", "output": "2 4 6 8 10 7 3 6 10 15" }, { "input": "5 3\n0 0 0 0 0\n1 5 0\n1 5 1\n1 5 2", "output": "3 6 10 15 21" }, { "input": "10 2\n0 0 0 0 0 0 0 0 0 0\n7 9 4...
61
9,113,600
-1
41,498
164
Ancient Berland Hieroglyphs
[ "two pointers" ]
null
null
Polycarpus enjoys studying Berland hieroglyphs. Once Polycarp got hold of two ancient Berland pictures, on each of which was drawn a circle of hieroglyphs. We know that no hieroglyph occurs twice in either the first or the second circle (but in can occur once in each of them). Polycarpus wants to save these pictures o...
The first line contains two integers *l**a* and *l**b* (1<=≀<=*l**a*,<=*l**b*<=≀<=1000000) β€” the number of hieroglyphs in the first and second circles, respectively. Below, due to difficulties with encoding of Berland hieroglyphs, they are given as integers from 1 to 106. The second line contains *l**a* integers β€” th...
Print a single number β€” the maximum length of the common substring and subsequence. If at any way of breaking the circles it does not exist, print 0.
[ "5 4\n1 2 3 4 5\n1 3 5 6\n", "4 6\n1 3 5 2\n1 2 3 4 5 6\n", "3 3\n1 2 3\n3 2 1\n" ]
[ "2\n", "3\n", "2\n" ]
In the first test Polycarpus picks a string that consists of hieroglyphs 5 and 1, and in the second sample β€” from hieroglyphs 1, 3 and 5.
[]
31
0
0
41,545
799
Field expansion
[ "brute force", "dp", "meet-in-the-middle" ]
null
null
In one of the games Arkady is fond of the game process happens on a rectangular field. In the game process Arkady can buy extensions for his field, each extension enlarges one of the field sizes in a particular number of times. Formally, there are *n* extensions, the *i*-th of them multiplies the width or the length (b...
The first line contains five integers *a*, *b*, *h*, *w* and *n* (1<=≀<=*a*,<=*b*,<=*h*,<=*w*,<=*n*<=≀<=100<=000)Β β€” the sizes of the rectangle needed to be placed, the initial sizes of the field and the number of available extensions. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (2<=≀<=*a**i*<=≀<=1...
Print the minimum number of extensions needed to reach Arkady's goal. If it is not possible to place the rectangle on the field with all extensions, print -1. If the rectangle can be placed on the initial field, print 0.
[ "3 3 2 4 4\n2 5 4 10\n", "3 3 3 3 5\n2 3 5 4 2\n", "5 5 1 2 3\n2 2 3\n", "3 4 1 1 3\n2 3 2\n" ]
[ "1\n", "0\n", "-1\n", "3\n" ]
In the first example it is enough to use any of the extensions available. For example, we can enlarge *h* in 5 times using the second extension. Then *h* becomes equal 10 and it is now possible to place the rectangle on the field.
[ { "input": "3 3 2 4 4\n2 5 4 10", "output": "1" }, { "input": "3 3 3 3 5\n2 3 5 4 2", "output": "0" }, { "input": "5 5 1 2 3\n2 2 3", "output": "-1" }, { "input": "3 4 1 1 3\n2 3 2", "output": "3" }, { "input": "572 540 6 2 12\n2 3 2 2 2 3 3 3 2 2 2 2", "outpu...
62
1,843,200
0
41,655
472
Design Tutorial: Make It Nondeterministic
[ "greedy" ]
null
null
A way to make a new task is to make it nondeterministic or probabilistic. For example, the hard task of Topcoder SRM 595, Constellation, is the probabilistic version of a convex hull. Let's try to make a new task. Firstly we will use the following task. There are *n* people, sort them by their name. It is just an ordi...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=105) β€” the number of people. The next *n* lines each contains two strings. The *i*-th line contains strings *f**i* and *s**i* (1<=≀<=|*f**i*|,<=|*s**i*|<=≀<=50) β€” the first name and last name of the *i*-th person. Each string consists only of lowercase English lett...
If it is possible, output "YES", otherwise output "NO".
[ "3\ngennady korotkevich\npetr mitrichev\ngaoyuan chen\n1 2 3\n", "3\ngennady korotkevich\npetr mitrichev\ngaoyuan chen\n3 1 2\n", "2\ngalileo galilei\nnicolaus copernicus\n2 1\n", "10\nrean schwarzer\nfei claussell\nalisa reinford\neliot craig\nlaura arseid\njusis albarea\nmachias regnitz\nsara valestin\nemma...
[ "NO\n", "YES\n", "YES\n", "NO\n", "YES\n" ]
In example 1 and 2, we have 3 people: tourist, Petr and me (cgy4ever). You can see that whatever handle is chosen, I must be the first, then tourist and Petr must be the last. In example 3, if Copernicus uses "copernicus" as his handle, everything will be alright.
[ { "input": "3\ngennady korotkevich\npetr mitrichev\ngaoyuan chen\n1 2 3", "output": "NO" }, { "input": "3\ngennady korotkevich\npetr mitrichev\ngaoyuan chen\n3 1 2", "output": "YES" }, { "input": "2\ngalileo galilei\nnicolaus copernicus\n2 1", "output": "YES" }, { "input": "1...
61
0
0
41,731
131
Yet Another Task with Queens
[ "sortings" ]
null
null
A queen is the strongest chess piece. In modern chess the queen can move any number of squares in any horizontal, vertical or diagonal direction (considering that there're no other pieces on its way). The queen combines the options given to the rook and the bishop. There are *m* queens on a square *n*<=Γ—<=*n* chessboa...
The first line of the input contains a pair of integers *n*,<=*m* (1<=≀<=*n*,<=*m*<=≀<=105), where *n* is the size of the board and *m* is the number of queens on the board. Then *m* following lines contain positions of the queens, one per line. Each line contains a pair of integers *r**i*,<=*c**i* (1<=≀<=*r**i*,<=*c**...
Print the required sequence *t*0,<=*t*1,<=...,<=*t*8, separating the numbers with spaces.
[ "8 4\n4 3\n4 8\n6 5\n1 6\n", "10 3\n1 1\n1 2\n1 3\n" ]
[ "0 3 0 1 0 0 0 0 0 ", "0 2 1 0 0 0 0 0 0 " ]
none
[]
92
0
0
41,798
141
Hopscotch
[ "geometry", "math" ]
null
null
So nearly half of the winter is over and Maria is dreaming about summer. She's fed up with skates and sleds, she was dreaming about Hopscotch all night long. It's a very popular children's game. The game field, the court, looks as is shown in the figure (all blocks are square and are numbered from bottom to top, blocks...
The only input line contains three integers: *a*, *x*, *y*, where *a* (1<=≀<=*a*<=≀<=100) is the side of the square, *x* and *y* (<=-<=106<=≀<=*x*<=≀<=106,<=0<=≀<=*y*<=≀<=106) are coordinates of the stone.
Print the number of the square, inside which the stone fell. If the stone is on a border of some stone or outside the court, print "-1" without the quotes.
[ "1 0 0\n", "3 1 1\n", "3 0 10\n", "3 0 7\n", "3 4 0\n" ]
[ "-1\n", "1\n", "5\n", "-1\n", "-1\n" ]
none
[ { "input": "1 0 0", "output": "-1" }, { "input": "3 1 1", "output": "1" }, { "input": "3 0 10", "output": "5" }, { "input": "3 0 7", "output": "-1" }, { "input": "3 4 0", "output": "-1" }, { "input": "9 3 2", "output": "1" }, { "input": "10...
92
0
0
41,799
767
Cartons of milk
[ "binary search", "data structures", "greedy", "sortings", "two pointers" ]
null
null
Olya likes milk very much. She drinks *k* cartons of milk each day if she has at least *k* and drinks all of them if she doesn't. But there's an issueΒ β€” expiration dates. Each carton has a date after which you can't drink it (you still can drink it exactly at the date written on the carton). Due to this, if Olya's frid...
In the first line there are three integers *n*, *m*, *k* (1<=≀<=*n*,<=*m*<=≀<=106, 1<=≀<=*k*<=≀<=*n*<=+<=*m*)Β β€” the amount of cartons in Olya's fridge, the amount of cartons in the shop and the number of cartons Olya drinks each day. In the second line there are *n* integers *f*1,<=*f*2,<=...,<=*f**n* (0<=≀<=*f**i*<=≀...
If there's no way for Olya to drink the cartons she already has in her fridge, print -1. Otherwise, in the first line print the maximum number *x* of cartons which Olya can buy so that she wouldn't have to throw a carton away. The next line should contain exactly *x* integersΒ β€” the numbers of the cartons that should b...
[ "3 6 2\n1 0 1\n2 0 2 0 0 2\n", "3 1 2\n0 0 0\n1\n", "2 1 2\n0 1\n0\n" ]
[ "3\n1 2 3", "-1", "1\n1 " ]
In the first example *k* = 2 and Olya has three cartons with expiry dates 0, 1 and 1 (they expire today, tomorrow and tomorrow), and the shop has 3 cartons with expiry date 0 and 3 cartons with expiry date 2. Olya can buy three cartons, for example, one with the expiry date 0 and two with expiry date 2. In the second ...
[]
2,000
171,110,400
0
41,878
133
Unary
[ "implementation" ]
null
null
Unary is a minimalistic Brainfuck dialect in which programs are written using only one token. Brainfuck programs use 8 commands: "+", "-", "[", "]", "&lt;", "&gt;", "." and "," (their meaning is not important for the purposes of this problem). Unary programs are created from Brainfuck programs using the following alg...
The input will consist of a single line *p* which gives a Brainfuck program. String *p* will contain between 1 and 100 characters, inclusive. Each character of *p* will be "+", "-", "[", "]", "&lt;", "&gt;", "." or ",".
Output the size of the equivalent Unary program modulo 1000003 (106<=+<=3).
[ ",.\n", "++++[&gt;,.&lt;-]\n" ]
[ "220\n", "61425\n" ]
To write a number *n* in unary numeral system, one simply has to write 1 *n* times. For example, 5 written in unary system will be 11111. In the first example replacing Brainfuck commands with binary code will give us 1101 1100. After we concatenate the codes, we'll get 11011100 in binary system, or 220 in decimal. Th...
[ { "input": ",.", "output": "220" }, { "input": "++++[>,.<-]", "output": "61425" }, { "input": "[-],<],<<,<[,>,+>[[<>.,[>-[-[<><>><<<<]>,.-].>-[[>+,>,[,-,.-,-[[]>..<>,<[+,-<]-++.<+.]<,[[.<<-><<<],", "output": "43789" }, { "input": "+", "output": "10" }, { "input": ...
124
0
3
41,883
717
Cowboy Beblop at his computer
[ "geometry" ]
null
null
Cowboy Beblop is a funny little boy who likes sitting at his computer. He somehow obtained two elastic hoops in the shape of 2D polygons, which are not necessarily convex. Since there's no gravity on his spaceship, the hoops are standing still in the air. Since the hoops are very elastic, Cowboy Beblop can stretch, rot...
The first line of input contains an integer *n* (3<=≀<=*n*<=≀<=100<=000), which denotes the number of edges of the first polygon. The next N lines each contain the integers *x*, *y* and *z* (<=-<=1<=000<=000<=≀<=*x*,<=*y*,<=*z*<=≀<=1<=000<=000)Β β€” coordinates of the vertices, in the manner mentioned above. The next line...
Your output should contain only one line, with the words "YES" or "NO", depending on whether the two given polygons are well-connected.
[ "4\n0 0 0\n2 0 0\n2 2 0\n0 2 0\n4\n1 1 -1\n1 1 1\n1 3 1\n1 3 -1\n" ]
[ "YES\n" ]
On the picture below, the two polygons are well-connected, as the edges of the vertical polygon cross the area of the horizontal one exactly once in one direction (for example, from above to below), and zero times in the other (in this case, from below to above). Note that the polygons do not have to be parallel to any...
[ { "input": "4\n0 0 0\n2 0 0\n2 2 0\n0 2 0\n4\n1 1 -1\n1 1 1\n1 3 1\n1 3 -1", "output": "YES" }, { "input": "4\n4 -2 0\n4 3 0\n-3 3 0\n-3 -2 0\n4\n6 -2 0\n3 2 2\n-3 7 0\n3 4 6", "output": "NO" }, { "input": "4\n-6 6 0\n13 9 0\n15 -7 0\n-5 -5 0\n4\n2 0 4\n2 6 8\n2 12 1\n2 4 -4", "outpu...
93
204,800
-1
41,956
309
Context Advertising
[ "dp", "two pointers" ]
null
null
Advertising has become part of our routine. And now, in the era of progressive technologies, we need your ideas to make advertising better! In this problem we'll look at a simplified version of context advertising. You've got a text, consisting of exactly *n* words. A standard advertising banner has exactly *r* lines,...
The first input line contains three integers *n*, *r*, *c* (1<=≀<=*n*,<=*r*,<=*c*<=≀<=106;Β *r*<=Γ—<=*c*<=≀<=106). The next line contains a text, consisting of *n* words. The words consist only of lowercase English letters and are not empty. The words in the lines are separated by single spaces. The total number of chara...
Print at most *r* lines, in each line print at most *c* characters β€” the optimal advertisement banner. If there are multiple advertisement banners, print any of them. Note that some lines of the banner can be empty. You are allowed not to print such lines.
[ "9 4 12\nthis is a sample text for croc final round\n", "9 1 9\nthis is a sample text for croc final round\n", "6 2 3\ncroc a a a croc a\n", "2 2 5\nfirst second\n" ]
[ "this is a\nsample text\nfor croc\nfinal round\n", "this is a\n", "a a\na\n", "first\n" ]
none
[]
46
0
0
42,029
703
Mishka and Interesting sum
[ "data structures" ]
null
null
Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her with array of non-negative integers *a*1,<=*a*2,<=...,<=*a**n* of *n* elements! Mishka loved the array and she instantly decided to determine its beauty value, but she is too little and can't process large arrays. ...
The first line of the input contains single integer *n* (1<=≀<=*n*<=≀<=1<=000<=000)Β β€” the number of elements in the array. The second line of the input contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109)Β β€” array elements. The third line of the input contains single integer *m* (1<=≀<=*m*<=≀<=1<=00...
Print *m* non-negative integersΒ β€” the answers for the queries in the order they appear in the input.
[ "3\n3 7 8\n1\n1 3\n", "7\n1 2 1 3 3 2 3\n5\n4 7\n4 5\n1 3\n1 7\n1 5\n" ]
[ "0\n", "0\n3\n1\n3\n2\n" ]
In the second sample: There is no integers in the segment of the first query, presented even number of times in the segmentΒ β€” the answer is 0. In the second query there is only integer 3 is presented even number of timesΒ β€” the answer is 3. In the third query only integer 1 is written downΒ β€” the answer is 1. In the ...
[ { "input": "3\n3 7 8\n1\n1 3", "output": "0" }, { "input": "7\n1 2 1 3 3 2 3\n5\n4 7\n4 5\n1 3\n1 7\n1 5", "output": "0\n3\n1\n3\n2" }, { "input": "10\n1 2 4 1 1 1 1 1 1 4\n55\n5 8\n3 9\n6 8\n4 6\n4 10\n2 8\n1 5\n7 8\n8 9\n7 9\n5 6\n8 10\n9 9\n2 2\n3 3\n3 7\n1 8\n2 3\n4 9\n8 8\n10 10\n1 ...
46
0
0
42,073
916
Jamie and Tree
[ "data structures", "trees" ]
null
null
To your surprise, Jamie is the final boss! Ehehehe. Jamie has given you a tree with *n* vertices, numbered from 1 to *n*. Initially, the root of the tree is the vertex with number 1. Also, each vertex has a value on it. Jamie also gives you three types of queries on the tree: 1 *v*Β β€” Change the tree's root to vertex...
The first line of input contains two space-separated integers *n* and *q* (1<=≀<=*n*<=≀<=105,<=1<=≀<=*q*<=≀<=105)Β β€” the number of vertices in the tree and the number of queries to process respectively. The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=108<=≀<=*a**i*<=≀<=108)Β β€” init...
For each query of the third type, output the required answer. It is guaranteed that at least one query of the third type is given by Jamie.
[ "6 7\n1 4 2 8 5 7\n1 2\n3 1\n4 3\n4 5\n3 6\n3 1\n2 4 6 3\n3 4\n1 6\n2 2 4 -5\n1 4\n3 3\n", "4 6\n4 3 5 6\n1 2\n2 3\n3 4\n3 1\n1 3\n2 2 4 3\n1 1\n2 2 4 -3\n3 1\n" ]
[ "27\n19\n5\n", "18\n21\n" ]
The following picture shows how the tree varies after the queries in the first sample.
[]
46
0
0
42,082
991
Bus Number
[ "brute force", "combinatorics", "math" ]
null
null
This night wasn't easy on Vasya. His favorite team lost, and he didn't find himself victorious eitherΒ β€” although he played perfectly, his teammates let him down every time. He had to win at least one more time, but the losestreak only grew longer and longer... It's no wonder he didn't get any sleep this night at all. ...
The first line contains one integer $n$ ($1 \leq n \leq 10^{18}$)Β β€” the number of the bus that was seen by Vasya. It is guaranteed that this number does not start with $0$.
Output a single integerΒ β€” the amount of possible variants of the real bus number.
[ "97\n", "2028\n" ]
[ "2\n", "13\n" ]
In the first sample, only variants $97$ and $79$ are possible. In the second sample, the variants (in the increasing order) are the following: $208$, $280$, $802$, $820$, $2028$, $2082$, $2208$, $2280$, $2802$, $2820$, $8022$, $8202$, $8220$.
[ { "input": "97", "output": "2" }, { "input": "2028", "output": "13" }, { "input": "1", "output": "1" }, { "input": "10", "output": "1" }, { "input": "168", "output": "6" }, { "input": "999999", "output": "6" }, { "input": "98765432002345678...
78
0
3
42,153
883
Road Widening
[ "constructive algorithms", "greedy", "implementation" ]
null
null
Mayor of city S just hates trees and lawns. They take so much space and there could be a road on the place they occupy! The Mayor thinks that one of the main city streets could be considerably widened on account of lawn nobody needs anyway. Moreover, that might help reduce the car jams which happen from time to time o...
The first line contains integer *n* (1<=≀<=*n*<=≀<=2Β·105) β€” number of parts of the street. Each of the following *n* lines contains two integers *s**i*,<=*g**i* (1<=≀<=*s**i*<=≀<=106, 0<=≀<=*g**i*<=≀<=106) β€” current width of road and width of the lawn on the *i*-th part of the street.
In the first line print the total width of lawns which will be removed. In the second line print *n* integers *s*'1,<=*s*'2,<=...,<=*s*'*n* (*s**i*<=≀<=*s*'*i*<=≀<=*s**i*<=+<=*g**i*) β€” new widths of the road starting from the first part and to the last. If there is no solution, print the only integer -1 in the first ...
[ "3\n4 5\n4 5\n4 10\n", "4\n1 100\n100 1\n1 100\n100 1\n", "3\n1 1\n100 100\n1 1\n" ]
[ "16\n9 9 10 \n", "202\n101 101 101 101 \n", "-1\n" ]
none
[ { "input": "3\n4 5\n4 5\n4 10", "output": "16\n9 9 10 " }, { "input": "4\n1 100\n100 1\n1 100\n100 1", "output": "202\n101 101 101 101 " }, { "input": "3\n1 1\n100 100\n1 1", "output": "-1" }, { "input": "10\n21005 10850\n27020 13372\n28183 3724\n22874 13564\n27446 11493\n225...
1,637
20,377,600
3
42,154
631
Product Sum
[ "data structures", "dp", "geometry" ]
null
null
Blake is the boss of Kris, however, this doesn't spoil their friendship. They often gather at the bar to talk about intriguing problems about maximising some values. This time the problem is really special. You are given an array *a* of length *n*. The characteristic of this array is the value Β β€” the sum of the produc...
The first line of the input contains a single integer *n* (2<=≀<=*n*<=≀<=200<=000)Β β€” the size of the array *a*. The second line contains *n* integers *a**i* (1<=≀<=*i*<=≀<=*n*, |*a**i*|<=≀<=1<=000<=000)Β β€” the elements of the array *a*.
Print a single integer β€” the maximum possible value of characteristic of *a* that can be obtained by performing no more than one move.
[ "4\n4 3 2 5\n", "5\n1 1 2 7 1\n", "3\n1 1 2\n" ]
[ "39", "49", "9" ]
In the first sample, one may pick the first element and place it before the third (before 5). Thus, the answer will be 3Β·1 + 2Β·2 + 4Β·3 + 5Β·4 = 39. In the second sample, one may pick the fifth element of the array and place it before the third. The answer will be 1Β·1 + 1Β·2 + 1Β·3 + 2Β·4 + 7Β·5 = 49.
[ { "input": "4\n4 3 2 5", "output": "39" }, { "input": "5\n1 1 2 7 1", "output": "49" }, { "input": "3\n1 1 2", "output": "9" }, { "input": "5\n1 2 3 4 5", "output": "55" }, { "input": "5\n-1 -2 -3 -4 -5", "output": "-45" }, { "input": "4\n0 0 0 0", ...
1,000
16,793,600
0
42,176
875
Delivery Club
[ "binary search", "data structures", "dp" ]
null
null
Petya and Vasya got employed as couriers. During the working day they are to deliver packages to *n* different points on the line. According to the company's internal rules, the delivery of packages must be carried out strictly in a certain order. Initially, Petya is at the point with the coordinate *s*1, Vasya is at t...
The first line contains three integers *n*, *s*1, *s*2 (1<=≀<=*n*<=≀<=100<=000, 0<=≀<=*s*1,<=*s*2<=≀<=109)Β β€” number of points of delivery and starting positions of Petya and Vasya. The second line contains *n* integers *x*1,<=*x*2,<=...,<=*x**n*Β β€” customers coordinates (0<=≀<=*x**i*<=≀<=109), in the order to make a de...
Output the only integer, minimum possible maximal distance between couriers during delivery.
[ "2 0 10\n5 6\n", "3 2 1\n3 4 5\n", "1 4 5\n2\n" ]
[ "10\n", "1\n", "2\n" ]
In the first test case the initial distance between the couriers is 10. This value will be the answer, for example, Petya can perform both deliveries, and Vasya will remain at the starting point. In the second test case you can optimally act, for example, like this: Vasya delivers the package to the first customer, Pe...
[]
30
0
0
42,198
145
Lucky Pair
[ "combinatorics", "data structures", "implementation" ]
null
null
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya has an array *a* of *n* integers. The numbers in the array are numbered starting from 1. Unf...
The first line contains an integer *n* (2<=≀<=*n*<=≀<=105) β€” the size of the array *a*. The second line contains *n* space-separated integers *a**i* (1<=≀<=*a**i*<=≀<=109) β€” array *a*. It is guaranteed that no more than 1000 elements in the array *a* are lucky numbers.
On the single line print the only number β€” the answer to the problem. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator.
[ "4\n1 4 2 4\n", "2\n4 7\n", "4\n4 4 7 7\n" ]
[ "9\n", "1\n", "9\n" ]
The subarray *a*[*l*..*r*] is an array that consists of elements *a*<sub class="lower-index">*l*</sub>, *a*<sub class="lower-index">*l* + 1</sub>, ..., *a*<sub class="lower-index">*r*</sub>. In the first sample there are 9 possible pairs that satisfy the condition: [1, 1] and [2, 2], [1, 1] and [2, 3], [1, 1] and [2, ...
[]
30
0
-1
42,215
190
Counter Attack
[ "data structures", "dsu", "graphs", "hashing", "sortings" ]
null
null
Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has *n* cities, numbered from 1 to *n*, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (...
The first line contains two space-separated integers *n* and *m* (1<=≀<=*n*<=≀<=5Β·105,<=0<=≀<=*m*<=≀<=106) β€” the number of cities and the number of roads marked on the flatland map, correspondingly. Next *m* lines contain descriptions of the cities on the map. The *i*-th line contains two integers *a**i* and *b**i* (1...
On the first line print number *k* β€” the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following *k* lines first print *t**i* (1<=≀<...
[ "4 4\n1 2\n1 3\n4 2\n4 3\n", "3 1\n1 2\n" ]
[ "2\n2 1 4 \n2 2 3 \n", "1\n3 1 2 3 \n" ]
In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3.
[ { "input": "4 4\n1 2\n1 3\n4 2\n4 3", "output": "2\n2 1 4 \n2 2 3 " }, { "input": "3 1\n1 2", "output": "1\n3 1 2 3 " }, { "input": "8 14\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n5 6\n6 7", "output": "2\n2 1 2 \n6 3 4 5 6 7 8 " }, { "input": "6 9\n1 4\n1 5...
60
0
0
42,274
82
Sets
[ "constructive algorithms", "hashing", "implementation" ]
B. Sets
2
256
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose *n* non-empty sets in such a way, that no two of them have common elements. One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possib...
The first input file line contains a number *n* (2<=≀<=*n*<=≀<=200), *n* is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on *n*Β·(*n*<=-<=1)<=/<=2 lines. Each set starts with the number *k**i* (2<=≀<=*k**i*<=≀<=200), which is the number of numbers written of the *i...
Print on *n* lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order...
[ "4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7\n", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2\n", "3\n2 1 2\n2 1 3\n2 2 3\n" ]
[ "1 7 \n2 2 4 \n2 1 3 \n1 5 \n", "3 7 8 9 \n2 6 100 \n1 1 \n1 2 \n", "1 1 \n1 2 \n1 3 \n" ]
none
[ { "input": "4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "output": "1 7 \n2 2 4 \n2 1 3 \n1 5 " }, { "input": "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "output": "3 7 8 9 \n2 6 100 \n1 1 \n1 2 " }, { "input": "3\n2 1 2\n2 1 3\n2 2 3", "output...
0
0
-1
42,497
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 ...
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...
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*...
[ "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...
31
102,400
-1
42,688
0
none
[ "none" ]
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<=&lt;<=*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<...
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 ther...
[ "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 ...
280
11,878,400
0
42,740
553
Kyoya and Permutation
[ "binary search", "combinatorics", "constructive algorithms", "greedy", "implementation", "math" ]
null
null
Let's define the permutation of length *n* as an array *p*<==<=[*p*1,<=*p*2,<=...,<=*p**n*] consisting of *n* distinct integers from range from 1 to *n*. We say that this permutation maps value 1 into the value *p*1, value 2 into the value *p*2 and so on. Kyota Ootori has just learned about cyclic representation of a ...
The first line will contain two integers *n*, *k* (1<=≀<=*n*<=≀<=50, 1<=≀<=*k*<=≀<=*min*{1018,<=*l*} where *l* is the length of the Kyoya's list).
Print *n* space-separated integers, representing the permutation that is the answer for the question.
[ "4 3\n", "10 1\n" ]
[ "1 3 2 4\n", "1 2 3 4 5 6 7 8 9 10\n" ]
The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3].
[ { "input": "4 3", "output": "1 3 2 4" }, { "input": "10 1", "output": "1 2 3 4 5 6 7 8 9 10" }, { "input": "1 1", "output": "1" }, { "input": "50 1", "output": "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 ...
0
0
-1
42,783
85
Guard Towers
[ "binary search", "dsu", "geometry", "graphs", "sortings" ]
E. Guard Towers
1
256
In a far away kingdom lives a very greedy king. To defend his land, he built *n* guard towers. Apart from the towers the kingdom has two armies, each headed by a tyrannical and narcissistic general. The generals can't stand each other, specifically, they will never let soldiers of two armies be present in one tower. D...
The first line contains an integer *n* (2<=≀<=*n*<=≀<=5000), *n* is the number of guard towers. Then follow *n* lines, each of which contains two integers *x*,<=*y* β€” the coordinates of the *i*-th tower (0<=≀<=*x*,<=*y*<=≀<=5000). No two towers are present at one point. Pretest 6 is one of the maximal tests for this p...
Print on the first line the smallest possible amount of money that will be enough to pay fees to the generals. Print on the second line the number of arrangements that can be carried out using the smallest possible fee. This number should be calculated modulo 1000000007 (109<=+<=7).
[ "2\n0 0\n1 1\n", "4\n0 0\n0 1\n1 0\n1 1\n", "3\n0 0\n1000 1000\n5000 5000\n" ]
[ "0\n2\n", "1\n4\n", "2000\n2\n" ]
In the first example there are only two towers, the distance between which is equal to 2. If we give both towers to one general, then we well have to pay 2 units of money. If each general receives a tower to manage, to fee will be equal to 0. That is the smallest possible fee. As you can easily see, we can obtain it in...
[]
46
0
0
42,831
398
Painting The Wall
[ "dp", "probabilities" ]
null
null
User ainta decided to paint a wall. The wall consists of *n*2 tiles, that are arranged in an *n*<=Γ—<=*n* table. Some tiles are painted, and the others are not. As he wants to paint it beautifully, he will follow the rules below. 1. Firstly user ainta looks at the wall. If there is at least one painted cell on each ro...
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=2Β·103; 0<=≀<=*m*<=≀<=*min*(*n*2,<=2Β·104)) β€” the size of the wall and the number of painted cells. Next *m* lines goes, each contains two integers *r**i* and *c**i* (1<=≀<=*r**i*,<=*c**i*<=≀<=*n*) β€” the position of the painted cell. It is guaranteed that t...
In a single line print the expected time to paint the wall in minutes. Your answer will be considered correct if it has at most 10<=-<=4 absolute or relative error.
[ "5 2\n2 3\n4 1\n", "2 2\n1 1\n1 2\n", "1 1\n1 1\n" ]
[ "11.7669491886\n", "2.0000000000\n", "0.0000000000\n" ]
none
[]
93
0
-1
42,968