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
67
Restoration of the Permutation
[ "greedy" ]
B. Restoration of the Permutation
1
256
Let *A*<==<={*a*1,<=*a*2,<=...,<=*a**n*} be any permutation of the first *n* natural numbers {1,<=2,<=...,<=*n*}. You are given a positive integer *k* and another sequence *B*<==<={*b*1,<=*b*2,<=...,<=*b**n*}, where *b**i* is the number of elements *a**j* in *A* to the left of the element *a**t*<==<=*i* such that *a**j...
The first line contains two space separated integers *n* and *k* (1<=≀<=*n*<=≀<=1000, 1<=≀<=*k*<=≀<=*n*). On the second line are *n* integers specifying the values of *B*<==<={*b*1,<=*b*2,<=...,<=*b**n*}.
Print on a single line *n* integers of *A*<==<={*a*1,<=*a*2,<=...,<=*a**n*} such that *A* is lexicographically minimal. It is guaranteed that the solution exists.
[ "5 2\n1 2 1 0 0\n", "4 2\n1 0 0 0\n" ]
[ "4 1 5 2 3 ", "2 3 1 4 " ]
none
[ { "input": "5 2\n1 2 1 0 0", "output": "4 1 5 2 3 " }, { "input": "4 2\n1 0 0 0", "output": "2 3 1 4 " }, { "input": "10 3\n4 2 4 2 1 0 1 0 0 0", "output": "6 8 2 5 9 1 4 10 3 7 " }, { "input": "15 3\n4 2 7 5 1 1 1 0 0 0 0 0 0 0 0", "output": "8 5 2 9 6 1 10 7 11 4 12 3 1...
590
409,600
3.704237
57,876
612
Simba on the Circle
[ "dp" ]
null
null
You are given a circular array with *n* elements. The elements are numbered from some element with values from 1 to *n* in clockwise order. The *i*-th cell contains the value *a**i*. The robot Simba is in cell *s*. Each moment of time the robot is in some of the *n* cells (at the begin he is in *s*). In one turn the r...
The first line contains two integers *n* and *s* (1<=≀<=*s*<=≀<=*n*<=≀<=2000) β€” the number of cells in the circular array and the starting position of Simba. The second line contains *n* integers *a**i* (<=-<=109<=≀<=*a**i*<=≀<=109) β€” the number written in the *i*-th cell. The numbers are given for cells in order from...
In the first line print the number *t* β€” the least number of time units. Each of the next *n* lines should contain the direction of robot movement and the number of cells to move in that direction. After that movement the robot writes out the number from the cell in which it turns out. The direction and the number of ...
[ "9 1\n0 1 2 2 2 1 0 1 1\n", "8 1\n0 1 0 1 0 1 0 1\n", "8 1\n1 2 3 4 5 6 7 8\n", "8 1\n0 0 0 0 0 0 0 0\n" ]
[ "12\n+0\n-3\n-1\n+2\n+1\n+2\n+1\n+1\n+1\n", "13\n+0\n+2\n+2\n+2\n-1\n+2\n+2\n+2\n", "7\n+0\n+1\n+1\n+1\n+1\n+1\n+1\n+1\n", "7\n+0\n+1\n+1\n+1\n+1\n+1\n+1\n+1\n" ]
none
[ { "input": "9 1\n0 1 2 2 2 1 0 1 1", "output": "12\n+0\n-3\n-1\n+2\n+1\n+2\n+1\n+1\n+1" }, { "input": "8 1\n0 1 0 1 0 1 0 1", "output": "13\n+0\n+2\n+2\n+2\n-1\n+2\n+2\n+2" }, { "input": "8 1\n1 2 3 4 5 6 7 8", "output": "7\n+0\n+1\n+1\n+1\n+1\n+1\n+1\n+1" }, { "input": "8 1\...
61
409,600
0
58,003
794
Labelling Cities
[ "dfs and similar", "graphs", "hashing" ]
null
null
Oleg the bank client lives in Bankopolia. There are *n* cities in Bankopolia and some pair of cities are connected directly by bi-directional roads. The cities are numbered from 1 to *n*. There are a total of *m* roads in Bankopolia, the *i*-th road connects cities *u**i* and *v**i*. It is guaranteed that from each cit...
The first line of input contains two space-separated integers *n* and *m* (2<=≀<=*n*<=≀<=3Β·105, 1<=≀<=*m*<=≀<=3Β·105)Β β€” the number of cities and the number of roads. Next, *m* lines follow. The *i*-th line contains two space-separated integers *u**i* and *v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*, *u**i*<=β‰ <=*v**i*)Β β€” the c...
If the required labeling is not possible, output a single line containing the string "NO" (without quotes). Otherwise, output the string "YES" (without quotes) on the first line. On the next line, output *n* space-separated integers, *x*1,<=*x*2,<=...,<=*x**n*. The condition 1<=≀<=*x**i*<=≀<=109 must hold for all *i*,...
[ "4 4\n1 2\n1 3\n1 4\n3 4\n", "5 10\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4\n3 5\n5 4\n", "4 3\n1 2\n1 3\n1 4\n" ]
[ "YES\n2 3 1 1 \n", "YES\n1 1 1 1 1 \n", "NO\n" ]
For the first sample, *x*<sub class="lower-index">1</sub> = 2, *x*<sub class="lower-index">2</sub> = 3, *x*<sub class="lower-index">3</sub> = *x*<sub class="lower-index">4</sub> = 1 is a valid labeling. Indeed, (3, 4), (1, 2), (1, 3), (1, 4) are the only pairs of cities with difference of labels not greater than 1, and...
[ { "input": "4 4\n1 2\n1 3\n1 4\n3 4", "output": "YES\n2 3 1 1 " }, { "input": "5 10\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4\n3 5\n5 4", "output": "YES\n1 1 1 1 1 " }, { "input": "4 3\n1 2\n1 3\n1 4", "output": "NO" }, { "input": "8 12\n1 2\n1 3\n1 4\n2 3\n2 4\n4 5\n5 6\n5 7\n...
1,621
98,201,600
0
58,053
155
Combination
[ "greedy", "sortings" ]
null
null
Ilya plays a card game by the following rules. A player has several cards. Each card contains two non-negative integers inscribed, one at the top of the card and one at the bottom. At the beginning of the round the player chooses one of his cards to play it. If the top of the card contains number *a**i*, and the botto...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=1000) β€” the number of cards Ilya has. Each of the next *n* lines contains two non-negative space-separated integers β€” *a**i* and *b**i* (0<=≀<=*a**i*,<=*b**i*<=≀<=104) β€” the numbers, written at the top and the bottom of the *i*-th card correspondingly.
Print the single number β€” the maximum number of points you can score in one round by the described rules.
[ "2\n1 0\n2 0\n", "3\n1 0\n2 0\n0 2\n" ]
[ "2\n", "3\n" ]
In the first sample none of two cards brings extra moves, so you should play the one that will bring more points. In the second sample you should first play the third card that doesn't bring any points but lets you play both remaining cards.
[ { "input": "2\n1 0\n2 0", "output": "2" }, { "input": "3\n1 0\n2 0\n0 2", "output": "3" }, { "input": "5\n0 0\n2 0\n2 0\n3 0\n5 1", "output": "8" }, { "input": "7\n9 1\n8 1\n9 0\n9 1\n5 1\n1 1\n0 1", "output": "41" }, { "input": "7\n5 0\n4 0\n3 0\n5 2\n3 0\n4 2\n0...
92
0
0
58,088
575
Bribes
[ "dfs and similar", "graphs", "trees" ]
null
null
Ruritania is a country with a very badly maintained road network, which is not exactly good news for lorry drivers that constantly have to do deliveries. In fact, when roads are maintained, they become one-way. It turns out that it is sometimes impossible to get from one town to another in a legal way – however, we kno...
The first line contains *N*, the number of towns in Ruritania. The following *N*<=-<=1 lines contain information regarding individual roads between towns. A road is represented by a tuple of integers (*a*,*b*,*x*), which are separated with a single whitespace character. The numbers *a* and *b* represent the cities conn...
The output should contain a single number: the least amount of thousands of Ruritanian dinars Borna should allocate for bribes, modulo 109<=+<=7.
[ "5\n1 2 0\n2 3 0\n5 1 1\n3 4 1\n5\n5 4 5 2 2\n" ]
[ "4\n" ]
Borna first takes the route 1 → 5 and has to pay 1000 dinars. After that, he takes the route 5 → 1 → 2 → 3 → 4 and pays nothing this time. However, when he has to return via 4 → 3 → 2 → 1 → 5, he needs to prepare 3000 (1000+2000) dinars. Afterwards, getting to 2 via 5 → 1 → 2 will cost him nothing. Finally, he doesn't ...
[]
30
0
0
58,090
723
st-Spanning Tree
[ "dsu", "graphs", "greedy", "implementation" ]
null
null
You are given an undirected connected graph consisting of *n* vertices and *m* edges. There are no loops and no multiple edges in the graph. You are also given two distinct vertices *s* and *t*, and two values *d**s* and *d**t*. Your task is to build any spanning tree of the given graph (note that the graph is not wei...
The first line of the input contains two integers *n* and *m* (2<=≀<=*n*<=≀<=200<=000, 1<=≀<=*m*<=≀<=*min*(400<=000,<=*n*Β·(*n*<=-<=1)<=/<=2))Β β€” the number of vertices and the number of edges in the graph. The next *m* lines contain the descriptions of the graph's edges. Each of the lines contains two integers *u* and...
If the answer doesn't exist print "No" (without quotes) in the only line of the output. Otherwise, in the first line print "Yes" (without quotes). In the each of the next (*n*<=-<=1) lines print two integers β€” the description of the edges of the spanning tree. Each of the edges of the spanning tree must be printed ex...
[ "3 3\n1 2\n2 3\n3 1\n1 2 1 1\n", "7 8\n7 4\n1 3\n5 4\n5 7\n3 2\n2 4\n6 1\n1 2\n6 4 1 4\n" ]
[ "Yes\n3 2\n1 3\n", "Yes\n1 3\n5 7\n3 2\n7 4\n2 4\n6 1\n" ]
none
[ { "input": "3 3\n1 2\n2 3\n3 1\n1 2 1 1", "output": "Yes\n3 2\n1 3" }, { "input": "7 8\n7 4\n1 3\n5 4\n5 7\n3 2\n2 4\n6 1\n1 2\n6 4 1 4", "output": "Yes\n1 3\n5 7\n3 2\n7 4\n2 4\n6 1" }, { "input": "10 15\n4 1\n5 10\n2 1\n5 7\n9 2\n4 6\n6 7\n9 1\n6 9\n8 4\n8 3\n9 8\n3 9\n2 3\n7 10\n10 1 ...
30
0
0
58,109
67
Save the City!
[ "geometry" ]
E. Save the City!
1
256
In the town of Aalam-Aara (meaning the Light of the Earth), previously there was no crime, no criminals but as the time progressed, sins started creeping into the hearts of once righteous people. Seeking solution to the problem, some of the elders found that as long as the corrupted part of population was kept away fro...
The first line of the test case will consist of the number of vertices *n* (3<=≀<=*n*<=≀<=1000). Next *n* lines will contain the coordinates of the vertices in the clockwise order of the polygon. On the *i*-th line are integers *x**i* and *y**i* (0<=≀<=*x**i*,<=*y**i*<=≀<=106) separated by a space. The endpoints of t...
Output consists of a single line containing the number of points where the watchtower can be set up.
[ "5\n4 8\n8 8\n9 4\n4 0\n0 4\n", "5\n4 8\n5 8\n5 4\n7 4\n2 2\n" ]
[ "5\n", "0\n" ]
Figure 2 shows the first test case. All the points in the figure are watchable from any point on fence *AB*. Since, *AB* has 5 integer coordinates, so answer is 5. For case two, fence *CD* and *DE* are not completely visible, thus answer is 0.
[]
92
0
0
58,154
82
Two out of Three
[ "dp" ]
D. Two out of Three
2
256
Vasya has recently developed a new algorithm to optimize the reception of customer flow and he considered the following problem. Let the queue to the cashier contain *n* people, at that each of them is characterized by a positive integer *a**i* β€” that is the time needed to work with this customer. What is special abou...
The first line of the input file contains a single number *n* (1<=≀<=*n*<=≀<=1000), which is the number of people in the sequence. The second line contains space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=106). The people are numbered starting from the cashier to the end of the queue.
Print on the first line a single number β€” the minimum time needed to process all *n* people. Then on ⌈*n*<=/<=2βŒ‰ lines print the order in which customers will be served. Each line (probably, except for the last one) must contain two numbers separated by a space β€” the numbers of customers who will be served at the curre...
[ "4\n1 2 3 4\n", "5\n2 4 3 1 4\n" ]
[ "6\n1 2\n3 4\n", "8\n1 3\n2 5\n4\n" ]
none
[ { "input": "4\n1 2 3 4", "output": "6\n1 2\n3 4" }, { "input": "5\n2 4 3 1 4", "output": "8\n1 3\n2 5\n4" }, { "input": "1\n10", "output": "10\n1" }, { "input": "2\n3 5", "output": "5\n1 2" }, { "input": "3\n1 10 1", "output": "11\n1 2\n3" }, { "input"...
62
0
0
58,428
283
Cow Program
[ "dfs and similar", "dp", "graphs" ]
null
null
Farmer John has just given the cows a program to play with! The program contains two integer variables, *x* and *y*, and performs the following operations on a sequence *a*1,<=*a*2,<=...,<=*a**n* of positive integers: 1. Initially, *x*<==<=1 and *y*<==<=0. If, after any step, *x*<=≀<=0 or *x*<=&gt;<=*n*, the program ...
The first line contains a single integer, *n* (2<=≀<=*n*<=≀<=2Β·105). The next line contains *n*<=-<=1 space separated integers, *a*2,<=*a*3,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109).
Output *n*<=-<=1 lines. On the *i*-th line, print the requested value when the program is run on the sequence *i*,<=*a*2,<=*a*3,<=...*a**n*. 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.
[ "4\n2 4 1\n", "3\n1 2\n" ]
[ "3\n6\n8\n", "-1\n-1\n" ]
In the first sample 1. For *i* = 1,  *x* becomes <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/d87035805e6f1c669693d86603da6d89c5730833.png" style="max-width: 100.0%;max-height: 100.0%;"/> and *y* becomes 1 + 2 = 3. 1. For *i* = 2,  *x* becomes <img align="middle" class="tex-formula" ...
[ { "input": "4\n2 4 1", "output": "3\n6\n8" }, { "input": "3\n1 2", "output": "-1\n-1" }, { "input": "5\n2 2 1 3", "output": "3\n-1\n-1\n-1" }, { "input": "2\n1", "output": "-1" }, { "input": "8\n7 6 2 6 2 6 6", "output": "8\n8\n12\n10\n-1\n-1\n20" }, { ...
466
29,696,000
3
58,492
732
Sanatorium
[ "binary search", "constructive algorithms", "greedy", "implementation", "math" ]
null
null
Vasiliy spent his vacation in a sanatorium, came back and found that he completely forgot details of his vacation! Every day there was a breakfast, a dinner and a supper in a dining room of the sanatorium (of course, in this order). The only thing that Vasiliy has now is a card from the dining room contaning notes ho...
The only line contains three integers *b*, *d* and *s* (0<=≀<=*b*,<=*d*,<=*s*<=≀<=1018,<=<=*b*<=+<=*d*<=+<=*s*<=β‰₯<=1)Β β€” the number of breakfasts, dinners and suppers which Vasiliy had during his vacation in the sanatorium.
Print single integerΒ β€” the minimum possible number of meals which Vasiliy could have missed during his vacation.
[ "3 2 1\n", "1 0 0\n", "1 1 1\n", "1000000000000000000 0 1000000000000000000\n" ]
[ "1\n", "0\n", "0\n", "999999999999999999\n" ]
In the first sample, Vasiliy could have missed one supper, for example, in case he have arrived before breakfast, have been in the sanatorium for two days (including the day of arrival) and then have left after breakfast on the third day. In the second sample, Vasiliy could have arrived before breakfast, have had it,...
[ { "input": "3 2 1", "output": "1" }, { "input": "1 0 0", "output": "0" }, { "input": "1 1 1", "output": "0" }, { "input": "1000000000000000000 0 1000000000000000000", "output": "999999999999999999" }, { "input": "1000 0 0", "output": "1998" }, { "input...
93
0
3
58,539
788
The Great Mixing
[ "dfs and similar", "graphs", "shortest paths" ]
null
null
Sasha and Kolya decided to get drunk with Coke, again. This time they have *k* types of Coke. *i*-th type is characterised by its carbon dioxide concentration . Today, on the party in honour of Sergiy of Vancouver they decided to prepare a glass of Coke with carbon dioxide concentration . The drink should also be tasty...
The first line contains two integers *n*, *k* (0<=≀<=*n*<=≀<=1000, 1<=≀<=*k*<=≀<=106)Β β€” carbon dioxide concentration the friends want and the number of Coke types. The second line contains *k* integers *a*1,<=*a*2,<=...,<=*a**k* (0<=≀<=*a**i*<=≀<=1000)Β β€” carbon dioxide concentration of each type of Coke. Some Coke typ...
Print the minimal natural number of liter needed to prepare a glass with carbon dioxide concentration , or -1 if it is impossible.
[ "400 4\n100 300 450 500\n", "50 2\n100 25\n" ]
[ "2\n", "3\n" ]
In the first sample case, we can achieve concentration <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/bed0f5c3640139492194728ccc3ac55accf16a8e.png" style="max-width: 100.0%;max-height: 100.0%;"/> using one liter of Coke of types <img align="middle" class="tex-formula" src="https://espresso...
[ { "input": "400 4\n100 300 450 500", "output": "2" }, { "input": "50 2\n100 25", "output": "3" }, { "input": "500 3\n1000 5 5", "output": "199" }, { "input": "500 1\n1000", "output": "-1" }, { "input": "874 3\n873 974 875", "output": "2" }, { "input": ...
530
44,851,200
0
58,855
0
none
[ "none" ]
null
null
We'll call a set of positive integers *a* beautiful if the following condition fulfills: for any prime *p*, if , then . In other words, if one number from the set is divisible by prime *p*, then at least half of numbers from the set is divisible by *p*. Your task is to find any beautiful set, where the number of eleme...
The first line contains integer *k* (10<=≀<=*k*<=≀<=5000) that shows how many numbers the required beautiful set should have.
In the first line print *k* space-separated integers that are a beautiful set. If there are multiple such sets, you are allowed to print any of them.
[ "10\n" ]
[ "16 18 24 27 36 48 54 72 108 144 \n" ]
none
[]
46
0
0
59,010
41
3-cycles
[ "constructive algorithms", "graphs", "greedy" ]
E. 3-cycles
2
256
During a recent research Berland scientists found out that there were *n* cities in Ancient Berland, joined by two-way paths. Any two cities are joined by no more than one path. No path joins a city with itself. According to a well-known tradition, the road network was built so that it would be impossible to choose thr...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=100) β€” the number of cities in Berland.
On the first line must be printed number *m* β€” the maximal number of roads in Berland. Then print *m* lines containing two numbers each β€” the numbers of cities that the given road joins. The cities are numbered with integers from 1 to *n*. If there are several variants of solving the problem, print any of them.
[ "3\n", "4\n" ]
[ "2\n1 2\n2 3\n", "4\n1 2\n2 3\n3 4\n4 1\n" ]
none
[ { "input": "3", "output": "2\n1 2\n1 3" }, { "input": "4", "output": "4\n1 3\n1 4\n2 3\n2 4" }, { "input": "5", "output": "6\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5" }, { "input": "6", "output": "9\n1 4\n1 5\n1 6\n2 4\n2 5\n2 6\n3 4\n3 5\n3 6" }, { "input": "7", "output...
92
0
0
59,184
132
Constants in the language of Shakespeare
[ "constructive algorithms", "dp", "greedy" ]
null
null
Shakespeare is a widely known esoteric programming language in which programs look like plays by Shakespeare, and numbers are given by combinations of ornate epithets. In this problem we will have a closer look at the way the numbers are described in Shakespeare. Each constant in Shakespeare is created from non-negati...
The only line of input contains a positive integer *n*, written as its binary notation. The length of the notation is at most 106. The first digit of the notation is guaranteed to be 1.
Output the required minimal *m*. After it output *m* lines. Each line has to be formatted as "+2^x" or "-2^x", where *x* is the power coefficient of the corresponding term. The order of the lines doesn't matter.
[ "1111\n", "1010011\n" ]
[ "2\n+2^4\n-2^0\n", "4\n+2^0\n+2^1\n+2^4\n+2^6\n" ]
none
[ { "input": "1111", "output": "2\n+2^4\n-2^0" }, { "input": "1010011", "output": "4\n+2^0\n+2^1\n+2^4\n+2^6" }, { "input": "1", "output": "1\n+2^0" }, { "input": "10110111", "output": "4\n+2^8\n-2^6\n-2^3\n-2^0" }, { "input": "10101110", "output": "4\n+2^8\n-2^...
46
0
0
59,220
926
Segments
[ "data structures" ]
null
null
There is a straight line colored in white. *n* black segments are added on it one by one. After each segment is added, determine the number of connected components of black segments (i.Β e. the number of black segments in the union of the black segments). In particular, if one segment ends in a point *x*, and another...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=200<=000) β€” the number of segments. The *i*-th of the next *n* lines contains two integers *l**i* and *r**i* (1<=≀<=*l**i*<=&lt;<=*r**i*<=≀<=109) β€” the coordinates of the left and the right ends of the *i*-th segment. The segments are listed in the order they...
Print *n* integers β€” the number of connected components of black segments after each segment is added.
[ "3\n1 3\n4 5\n2 4\n", "9\n10 20\n50 60\n30 40\n70 80\n90 100\n60 70\n10 40\n40 50\n80 90\n" ]
[ "1 2 1 \n", "1 2 3 4 5 4 3 2 1 \n" ]
In the first example there are two components after the addition of the first two segments, because these segments do not intersect. The third added segment intersects the left segment and touches the right segment at the point 4 (these segments belong to the same component, according to the statements). Thus the numbe...
[]
31
0
0
59,231
812
Sagheer and Apple Tree
[ "games", "trees" ]
null
null
Sagheer is playing a game with his best friend Soliman. He brought a tree with *n* nodes numbered from 1 to *n* and rooted at node 1. The *i*-th node has *a**i* apples. This tree has a special property: the lengths of all paths from the root to any leaf have the same parity (i.e. all paths have even length or all paths...
The first line will contain one integer *n* (2<=≀<=*n*<=≀<=105) β€” the number of nodes in the apple tree. The second line will contain *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=107) β€” the number of apples on each node of the tree. The third line will contain *n*<=-<=1 integers *p*2,<=*p*3,<=...,<=*p**n...
On a single line, print the number of different pairs of nodes (*u*,<=*v*), *u*<=β‰ <=*v* such that if they start playing after swapping the apples of both nodes, Sagheer will win the game. (*u*,<=*v*) and (*v*,<=*u*) are considered to be the same pair.
[ "3\n2 2 3\n1 1\n", "3\n1 2 3\n1 1\n", "8\n7 2 2 5 4 3 1 1\n1 1 1 4 4 5 6\n" ]
[ "1\n", "0\n", "4\n" ]
In the first sample, Sagheer can only win if he swapped node 1 with node 3. In this case, both leaves will have 2 apples. If Soliman makes a move in a leaf node, Sagheer can make the same move in the other leaf. If Soliman moved some apples from a root to a leaf, Sagheer will eat those moved apples. Eventually, Soliman...
[ { "input": "3\n2 2 3\n1 1", "output": "1" }, { "input": "3\n1 2 3\n1 1", "output": "0" }, { "input": "8\n7 2 2 5 4 3 1 1\n1 1 1 4 4 5 6", "output": "4" }, { "input": "6\n7 7 7 7 7 7\n1 1 1 1 1", "output": "0" }, { "input": "6\n3 1 1 1 2 2\n1 1 1 1 1", "output"...
217
9,830,400
3
59,283
0
none
[ "none" ]
null
null
Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his motherlandΒ β€” Uzhlyandia. It is widely known that Uzhlyandia has *n* cities connected with *m* bidirectional roads. Also, there are no two roads in the country that connect the same pair of cities, but roads starting and en...
The first line contains two integers *n*, *m* (1<=≀<=*n*,<=*m*<=≀<=106)Β β€” the number of cities and roads in Uzhlyandia, respectively. Each of the next *m* lines contains two integers *u* and *v* (1<=≀<=*u*,<=*v*<=≀<=*n*) that mean that there is road between cities *u* and *v*. It is guaranteed that no road will be gi...
Print out the only integerΒ β€” the number of good paths in Uzhlyandia.
[ "5 4\n1 2\n1 3\n1 4\n1 5\n", "5 3\n1 2\n2 3\n4 5\n", "2 2\n1 1\n1 2\n" ]
[ "6", "0", "1" ]
In first sample test case the good paths are: - 2 → 1 → 3 → 1 → 4 → 1 → 5, - 2 → 1 → 3 → 1 → 5 → 1 → 4, - 2 → 1 → 4 → 1 → 5 → 1 → 3, - 3 → 1 → 2 → 1 → 4 → 1 → 5, - 3 → 1 → 2 → 1 → 5 → 1 → 4, - 4 → 1 → 2 → 1 → 3 → 1 → 5. There are good paths that are same with displayed above, because the sets of roads they pas...
[]
77
5,632,000
0
59,428
174
Range Increments
[ "data structures", "greedy" ]
null
null
Polycarpus is an amateur programmer. Now he is analyzing a friend's program. He has already found there the function rangeIncrement(l, r), that adds 1 to each element of some array *a* for all indexes in the segment [*l*,<=*r*]. In other words, this function does the following: Polycarpus knows the state of the array...
The first input line contains a single integer *n* (1<=≀<=*n*<=≀<=105) β€” the length of the array *a*[1... *n*]. The second line contains its integer space-separated elements, *a*[1],<=*a*[2],<=...,<=*a*[*n*] (0<=≀<=*a*[*i*]<=≀<=105) after some series of function calls rangeIncrement(l, r). It is guaranteed that at ...
Print on the first line *t* β€” the minimum number of calls of function rangeIncrement(l, r), that lead to the array from the input data. It is guaranteed that this number will turn out not more than 105. Then print *t* lines β€” the descriptions of function calls, one per line. Each line should contain two integers *l**i...
[ "6\n1 2 1 1 4 1\n", "5\n1 0 1 0 1\n" ]
[ "5\n2 2\n5 5\n5 5\n5 5\n1 6\n", "3\n1 1\n3 3\n5 5\n" ]
The first sample requires a call for the entire array, and four additional calls: - one for the segment [2,2] (i.e. the second element of the array), - three for the segment [5,5] (i.e. the fifth element of the array).
[ { "input": "6\n1 2 1 1 4 1", "output": "5\n2 2\n5 5\n5 5\n5 5\n1 6" }, { "input": "5\n1 0 1 0 1", "output": "3\n1 1\n3 3\n5 5" }, { "input": "1\n1", "output": "1\n1 1" }, { "input": "1\n100000", "output": "100000\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1...
280
24,576,000
0
59,450
0
none
[ "none" ]
null
null
After all the events in Orlando we all know, Sasha and Roma decided to find out who is still the team's biggest loser. Thankfully, Masha found somewhere a revolver with a rotating cylinder of *n* bullet slots able to contain exactly *k* bullets, now the boys have a chance to resolve the problem once and for all. Sash...
The first line contains three integers *n*, *k* and *p* (1<=≀<=*n*<=≀<=1018,<=0<=≀<=*k*<=≀<=*n*,<=1<=≀<=*p*<=≀<=1000) β€” the number of slots in the cylinder, the number of bullets and the number of queries. Then follow *p* lines; they are the queries. Each line contains one integer *x**i* (1<=≀<=*x**i*<=≀<=*n*) the numb...
For each query print "." if the slot should be empty and "X" if the slot should be charged.
[ "3 1 3\n1\n2\n3\n", "6 3 6\n1\n2\n3\n4\n5\n6\n", "5 2 5\n1\n2\n3\n4\n5\n" ]
[ "..X", ".X.X.X", "...XX" ]
The lexicographical comparison of is performed by the &lt; operator in modern programming languages. The *a* string is lexicographically less that the *b* string, if there exists such *i* (1 ≀ *i* ≀ *n*), that *a*<sub class="lower-index">*i*</sub> &lt; *b*<sub class="lower-index">*i*</sub>, and for any *j* (1 ≀ *j* &lt...
[ { "input": "3 1 3\n1\n2\n3", "output": "..X" }, { "input": "6 3 6\n1\n2\n3\n4\n5\n6", "output": ".X.X.X" }, { "input": "5 2 5\n1\n2\n3\n4\n5", "output": "...XX" }, { "input": "4 2 8\n1\n3\n4\n2\n3\n4\n1\n2", "output": "..XX.X.X" }, { "input": "4 0 4\n1\n2\n3\n4", ...
0
0
-1
59,518
39
Moon Craters
[ "dp", "sortings" ]
C. Moon Craters
1
256
There are lots of theories concerning the origin of moon craters. Most scientists stick to the meteorite theory, which says that the craters were formed as a result of celestial bodies colliding with the Moon. The other version is that the craters were parts of volcanoes. An extraterrestrial intelligence research spec...
The first line has an integer *n* (1<=≀<=*n*<=≀<=2000) β€” the number of discovered craters. The next *n* lines contain crater descriptions in the "*c**i* *r**i*" format, where *c**i* is the coordinate of the center of the crater on the moon robot’s path, *r**i* is the radius of the crater. All the numbers *c**i* and *r*...
In the first line output the number of craters in the required largest set. In the next line output space-separated numbers of craters that this set consists of. The craters are numbered from 1 to *n* in the order in which they were given in the input data. The numbers may be output in any order. If the result is not u...
[ "4\n1 1\n2 2\n4 1\n5 1\n" ]
[ "3\n1 2 4\n" ]
none
[]
1,000
4,710,400
0
59,685
630
Pyramids
[ "geometry", "math" ]
null
null
IT City administration has no rest because of the fame of the Pyramids in Egypt. There is a project of construction of pyramid complex near the city in the place called Emerald Walley. The distinction of the complex is that its pyramids will be not only quadrangular as in Egypt but also triangular and pentagonal. Of co...
The only line of the input contains three integers *l*3,<=*l*4,<=*l*5 (1<=≀<=*l*3,<=*l*4,<=*l*5<=≀<=1000) β€” the edge lengths of triangular, quadrangular and pentagonal pyramids correspondingly.
Output one number β€” the total volume of the pyramids. Absolute or relative error should not be greater than 10<=-<=9.
[ "2 5 3\n" ]
[ "38.546168065709" ]
none
[ { "input": "2 5 3", "output": "38.546168065709" }, { "input": "3 4 5", "output": "55.954779230131" }, { "input": "1 1 1", "output": "0.655056222989" }, { "input": "1000 1000 1000", "output": "655056222.989098310000" }, { "input": "999 997 998", "output": "6507...
62
0
0
60,000
140
New Year Snowflake
[ "geometry", "sortings" ]
null
null
As Gerald ..., in other words, on a New Year Eve Constantine prepared an unusual present for the Beautiful Lady. The present is the magic New Year snowflake that can make any dream come true. The New Year snowflake consists of tiny ice crystals, which can be approximately regarded as points on the plane. The beauty of...
The first line contains two integers *n* and *k* (1<=≀<=*n*<=≀<=200<=000, 0<=≀<=*k*<=≀<=10) β€” the number of the surviving snowflake crystals and the maximum number of melted crystals, correspondingly. Next *n* lines contain the coordinates of the crystals that are left in the following form: "*x**i* *y**i*". The coordi...
The first line contains an integer *c* β€” the number of possible symmetry centers. Next *c* lines should contain the centers' descriptions. Each symmetry center is described by a couple of coordinates "*x* *y*", separated by a space. Print the coordinates with absolute error not exceeding 10<=-<=6. You are allowed to pr...
[ "4 0\n0 0\n0 1\n1 0\n1 1\n", "4 2\n0 0\n0 1\n1 0\n1 1\n", "4 4\n0 0\n0 1\n1 0\n1 1\n" ]
[ "1\n0.5 0.5\n", "5\n0.0 0.5\n0.5 0.0\n0.5 0.5\n0.5 1.0\n1.0 0.5\n", "-1\n" ]
none
[]
1,000
15,052,800
0
60,081
164
Variable, or There and Back Again
[ "dfs and similar", "graphs" ]
null
null
Life is not easy for the perfectly common variable named Vasya. Wherever it goes, it is either assigned a value, or simply ignored, or is being used! Vasya's life goes in states of a program. In each state, Vasya can either be used (for example, to calculate the value of another variable), or be assigned a value, or i...
The first line contains two space-separated integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=105) β€” the numbers of states and transitions, correspondingly. The second line contains space-separated *n* integers *f*1,<=*f*2,<=...,<=*f**n* (0<=≀<=*f**i*<=≀<=2), *f**i* described actions performed upon Vasya in state *i*: 0 repre...
Print *n* integers *r*1,<=*r*2,<=...,<=*r**n*, separated by spaces or new lines. Number *r**i* should equal 1, if Vasya's value in state *i* is interesting to the world and otherwise, it should equal 0. The states are numbered from 1 to *n* in the order, in which they are described in the input.
[ "4 3\n1 0 0 2\n1 2\n2 3\n3 4\n", "3 1\n1 0 2\n1 3\n", "3 1\n2 0 1\n1 3\n" ]
[ "1\n1\n1\n1\n", "1\n0\n1\n", "0\n0\n0\n" ]
In the first sample the program states can be used to make the only path in which the value of Vasya interests the world, 1 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> 2 <img align="middle" cla...
[ { "input": "4 3\n1 0 0 2\n1 2\n2 3\n3 4", "output": "1\n1\n1\n1" }, { "input": "3 1\n1 0 2\n1 3", "output": "1\n0\n1" }, { "input": "3 1\n2 0 1\n1 3", "output": "0\n0\n0" }, { "input": "4 4\n1 0 2 0\n1 2\n2 3\n3 4\n4 1", "output": "1\n1\n1\n0" }, { "input": "2 1\n...
154
0
0
60,128
774
Pens And Days Of Week
[ "*special", "binary search", "number theory" ]
null
null
Stepan has *n* pens. Every day he uses them, and on the *i*-th day he uses the pen number *i*. On the (*n*<=+<=1)-th day again he uses the pen number 1, on the (*n*<=+<=2)-th β€” he uses the pen number 2 and so on. On every working day (from Monday to Saturday, inclusive) Stepan spends exactly 1 milliliter of ink of the...
The first line contains the integer *n* (1<=≀<=*n*<=≀<=50<=000) β€” the number of pens Stepan has. The second line contains the sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109), where *a**i* is equal to the number of milliliters of ink which the pen number *i* currently has.
Print the index of the pen which will run out of ink before all (it means that there will be no ink left in it), if Stepan will use pens according to the conditions described above. Pens are numbered in the order they are given in input data. The numeration begins from one. Note that the answer is always unambiguou...
[ "3\n3 3 3\n", "5\n5 4 5 4 4\n" ]
[ "2\n", "5\n" ]
In the first test Stepan uses ink of pens as follows: 1. on the day number 1 (Monday) Stepan will use the pen number 1, after that there will be 2 milliliters of ink in it; 1. on the day number 2 (Tuesday) Stepan will use the pen number 2, after that there will be 2 milliliters of ink in it; 1. on the day number 3...
[ { "input": "3\n3 3 3", "output": "2" }, { "input": "5\n5 4 5 4 4", "output": "5" }, { "input": "28\n2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033 2033", "output": "1" }, { "input": "7\n10...
46
5,529,600
0
60,194
862
Mahmoud and Ehab and the final stage
[ "data structures", "strings" ]
null
null
Mahmoud and Ehab solved Dr. Evil's questions so he gave them the password of the door of the evil land. When they tried to open the door using it, the door gave them a final question to solve before they leave (yes, the door is digital, Dr. Evil is modern). If they don't solve it, all the work will be useless and they ...
The first line of input contains 2 integers *n* and *q* (1<=≀<=*n*<=≀<=105,<=1<=≀<=*q*<=≀<=105) – The number of strings and the number of queries, respectively. The second line contains *n* strings *str**i* consisting of lowercase English letters. The next *q* lines describe the queries and may have one of the 2 form...
For each query of first type output its answer in a new line.
[ "5 9\nmahmoud mahmoudbadawy drmahmoud drevil mahmoud\n1 1 5\n1 1 2\n1 2 3\n2 3 mahmoud\n2 4 mahmoud\n2 2 mahmouu\n1 1 5\n1 2 3\n1 1 1\n" ]
[ "14\n14\n13\n30\n12\n7\n" ]
none
[]
77
2,764,800
-1
60,269
167
Wizards and Bets
[ "dfs and similar", "graphs", "math", "matrices" ]
null
null
In some country live wizards. They like to make weird bets. Two wizards draw an acyclic directed graph with *n* vertices and *m* edges (the graph's vertices are numbered from 1 to *n*). A source is a vertex with no incoming edges, and a sink is the vertex with no outgoing edges. Note that a vertex could be the sink an...
The first line contains three space-separated integers *n*, *m*, *p* (1<=≀<=*n*<=≀<=600, 0<=≀<=*m*<=≀<=105, 2<=≀<=*p*<=≀<=109<=+<=7). It is guaranteed that *p* is prime number. Next *m* lines contain edges of the graph. Each line contains a pair of space-separated integers, *a**i*Β *b**i* β€” an edge from vertex *a**i* t...
Print the answer to the problem β€” the total winnings of the first player modulo a prime number *p*. Please note that the winnings may be negative, but the modulo residue must be non-negative (see the sample).
[ "4 2 1000003\n1 3\n2 4\n", "4 2 1000003\n4 1\n3 2\n", "4 4 1000003\n2 1\n2 4\n3 1\n3 4\n", "6 5 1000003\n1 4\n1 5\n1 6\n2 6\n3 6\n", "5 2 1000003\n5 1\n3 4\n" ]
[ "1\n", "1000002\n", "0\n", "0\n", "1\n" ]
In the first sample, there is exactly one set of paths β€” <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4c100300071b70505c9eb8fd6f97456068e38f93.png" style="max-width: 100.0%;max-height: 100.0%;"/>. The number of inversions is 0, which is an even number. Therefore, the first wizard gets 1 ...
[]
92
0
0
60,299
436
Banners
[ "brute force", "data structures", "dp" ]
null
null
All modern mobile applications are divided into free and paid. Even a single application developers often release two versions: a paid version without ads and a free version with ads. Suppose that a paid version of the app costs *p* (*p* is an integer) rubles, and the free version of the application contains *c* ad ba...
The first line contains two integers *n* and *w* (1<=≀<=*n*<=≀<=105;Β 1<=≀<=*w*<=≀<=105) β€” the number of users and the profit from a single banner. Each of the next *n* lines contains two integers *a**i* and *b**i* (0<=≀<=*a**i*,<=*b**i*<=≀<=105) β€” the characteristics of the *i*-th user.
Print (*max*Β *b**i*)<=+<=2 lines, in the *i*-th line print two integers: *pay* β€” the maximum gained profit at *c*<==<=*i*<=-<=1, *p* (0<=≀<=*p*<=≀<=109) β€” the corresponding optimal app cost. If there are multiple optimal solutions, print any of them.
[ "2 1\n2 0\n0 2\n", "3 1\n3 1\n2 2\n1 3\n" ]
[ "0 3\n3 2\n4 2\n2 2\n", "0 4\n3 4\n7 3\n7 2\n4 2\n" ]
none
[]
5,000
0
0
60,536
490
Chocolate
[ "brute force", "dfs and similar", "math", "meet-in-the-middle", "number theory" ]
null
null
Polycarpus likes giving presents to Paraskevi. He has bought two chocolate bars, each of them has the shape of a segmented rectangle. The first bar is *a*1<=Γ—<=*b*1 segments large and the second one is *a*2<=Γ—<=*b*2 segments large. Polycarpus wants to give Paraskevi one of the bars at the lunch break and eat the other...
The first line of the input contains integers *a*1,<=*b*1 (1<=≀<=*a*1,<=*b*1<=≀<=109) β€” the initial sizes of the first chocolate bar. The second line of the input contains integers *a*2,<=*b*2 (1<=≀<=*a*2,<=*b*2<=≀<=109) β€” the initial sizes of the second bar. You can use the data of type int64 (in Pascal), long long (...
In the first line print *m* β€” the sought minimum number of minutes. In the second and third line print the possible sizes of the bars after they are leveled in *m* minutes. Print the sizes using the format identical to the input format. Print the sizes (the numbers in the printed pairs) in any order. The second line mu...
[ "2 6\n2 3\n", "36 5\n10 16\n", "3 5\n2 1\n" ]
[ "1\n1 6\n2 3\n", "3\n16 5\n5 16\n", "-1\n" ]
none
[ { "input": "2 6\n2 3", "output": "1\n1 6\n2 3" }, { "input": "36 5\n10 16", "output": "3\n16 5\n5 16" }, { "input": "3 5\n2 1", "output": "-1" }, { "input": "36 5\n10 12", "output": "1\n24 5\n10 12" }, { "input": "1 1\n1 1", "output": "0\n1 1\n1 1" }, { ...
92
0
3
60,748
843
Dynamic Shortest Path
[ "graphs", "shortest paths" ]
null
null
You are given a weighted directed graph, consisting of *n* vertices and *m* edges. You should answer *q* queries of two types: - 1 vΒ β€” find the length of shortest path from vertex 1 to vertex *v*. - 2 c *l*1 *l*2 ... *l**c*Β β€” add 1 to weights of edges with indices *l*1,<=*l*2,<=...,<=*l**c*.
The first line of input data contains integers *n*, *m*, *q* (1<=≀<=*n*,<=*m*<=≀<=105, 1<=≀<=*q*<=≀<=2000)Β β€” the number of vertices and edges in the graph, and the number of requests correspondingly. Next *m* lines of input data contain the descriptions of edges: *i*-th of them contains description of edge with index ...
For each query of first type print the length of the shortest path from 1 to *v* in a separate line. Print -1, if such path does not exists.
[ "3 2 9\n1 2 0\n2 3 0\n2 1 2\n1 3\n1 2\n2 1 1\n1 3\n1 2\n2 2 1 2\n1 3\n1 2\n", "5 4 9\n2 3 1\n2 4 1\n3 4 1\n1 2 0\n1 5\n1 4\n2 1 2\n2 1 2\n1 4\n2 2 1 3\n1 4\n2 1 4\n1 4\n" ]
[ "1\n0\n2\n1\n4\n2\n", "-1\n1\n2\n3\n4\n" ]
The description of changes of the graph in the first sample case: <img class="tex-graphics" src="https://espresso.codeforces.com/aeb5751e557f6f6158f15919da64eee550146483.png" style="max-width: 100.0%;max-height: 100.0%;"/> The description of changes of the graph in the second sample case: <img class="tex-graphics" s...
[]
46
0
0
61,080
852
Dating
[ "brute force", "dfs and similar", "graphs", "trees" ]
null
null
This story is happening in a town named BubbleLand. There are *n* houses in BubbleLand. In each of these *n* houses lives a boy or a girl. People there really love numbers and everyone has their favorite number *f*. That means that the boy or girl that lives in the *i*-th house has favorite number equal to *f**i*. The...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=105), the number of houses in the town. The second line contains *n* integers, where the *i*-th number is 1 if a boy lives in the *i*-th house or 0 if a girl lives in *i*-th house. The third line contains *n* integers, where the *i*-th number represents the favori...
For each of the *q* questions output a single number, the answer to the citizens question.
[ "7\n1 0 0 1 0 1 0\n9 2 9 2 2 9 9\n2 6\n1 2\n4 2\n6 5\n3 6\n7 4\n2\n1 3\n7 5\n" ]
[ "2\n3\n" ]
In the first question from house 1 to house 3, the potential couples are (1, 3) and (6, 3). In the second question from house 7 to house 5, the potential couples are (7, 6), (4, 2) and (4, 5).
[]
46
0
0
61,091
97
Superset
[ "constructive algorithms", "divide and conquer" ]
B. Superset
2
256
A set of points on a plane is called good, if for any two points at least one of the three conditions is true: - those two points lie on same horizontal line; - those two points lie on same vertical line; - the rectangle, with corners in these two points, contains inside or on its borders at least one point of the s...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=104) β€” the number of points in the initial set. Next *n* lines describe the set's points. Each line contains two integers *x**i* and *y**i* (<=-<=109<=≀<=*x**i*,<=*y**i*<=≀<=109) β€” a corresponding point's coordinates. It is guaranteed that all the points are differe...
Print on the first line the number of points *m* (*n*<=≀<=*m*<=≀<=2Β·105) in a good superset, print on next *m* lines the points. The absolute value of the points' coordinates should not exceed 109. Note that you should not minimize *m*, it is enough to find any good superset of the given set, whose size does not exceed...
[ "2\n1 1\n2 2\n" ]
[ "3\n1 1\n2 2\n1 2\n" ]
none
[]
92
0
0
61,144
732
Tourist Reform
[ "dfs and similar", "graphs" ]
null
null
Berland is a tourist country! At least, it can become suchΒ β€” the government of Berland is confident about this. There are *n* cities in Berland, some pairs of which are connected by two-ways roads. Each road connects two different cities. In Berland there are no roads which connect the same pair of cities. It is poss...
The first line contains two integers *n*,<=*m* (2<=≀<=*n*<=≀<=400<=000,<=1<=≀<=*m*<=≀<=400<=000)Β β€” the number of cities and the number of roads. The next *m* lines describe roads in Berland: the *j*-th of them contains two integers *u**j* and *v**j* (1<=≀<=*u**j*,<=*v**j*<=≀<=*n*, *u**j*<=β‰ <=*v**j*), where *u**j* and...
In the first line print single integerΒ β€” the maximum possible value *min*1<=≀<=*i*<=≀<=*n*{*r**i*} after the orientation of roads. The next *m* lines must contain the description of roads after the orientation: the *j*-th of them must contain two integers *u**j*,<=*v**j*, it means that the *j*-th road will be directe...
[ "7 9\n4 3\n2 6\n7 1\n4 1\n7 3\n3 5\n7 4\n6 5\n2 5\n" ]
[ "4\n4 3\n6 2\n7 1\n1 4\n3 7\n5 3\n7 4\n5 6\n2 5\n" ]
none
[ { "input": "7 9\n4 3\n2 6\n7 1\n4 1\n7 3\n3 5\n7 4\n6 5\n2 5", "output": "4\n4 3\n6 2\n7 1\n1 4\n3 7\n5 3\n7 4\n5 6\n2 5" }, { "input": "2 1\n2 1", "output": "1\n2 1" }, { "input": "3 2\n1 3\n1 2", "output": "1\n3 1\n2 1" }, { "input": "3 3\n3 1\n3 2\n1 2", "output": "3\n...
30
0
0
61,179
48
Black and White
[ "constructive algorithms" ]
H. Black and White
2
256
According to the legends the king of Berland Berl I was noted for his love of beauty and order. One day he ordered to tile the palace hall's floor where balls and receptions used to take place with black and white tiles according to a regular geometrical pattern invented by him. However, as is after the case, due to lo...
The first line contains given integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=100) which represent the sizes of the rectangle that needs to be tiled. The next line contains non-negative numbers *a*, *b* and *c*, *a*<=+<=*b*<=+<=*c*<==<=*nm*, *c*<=β‰₯<=*m*.
Print 2*n* lines containing 2*m* characters each β€” the tiling scheme. Every tile is represented by a square 2<=Γ—<=2 in the following manner (the order corresponds to the order of the picture above):
[ "2 2\n0 0 4\n", "2 3\n1 2 3\n" ]
[ "\\../\n#\\/#\n\\##/\n.\\/.\n", "###/\\#\n##/..\\\n#/....\n/.....\n" ]
none
[]
60
0
-1
61,247
9
How many trees?
[ "combinatorics", "divide and conquer", "dp" ]
D. How many trees?
1
64
In one very old text file there was written Great Wisdom. This Wisdom was so Great that nobody could decipher it, even Phong β€” the oldest among the inhabitants of Mainframe. But still he managed to get some information from there. For example, he managed to learn that User launches games for pleasure β€” and then terribl...
The input data contains two space-separated positive integer numbers *n* and *h* (*n*<=≀<=35, *h*<=≀<=*n*).
Output one number β€” the answer to the problem. It is guaranteed that it does not exceed 9Β·1018.
[ "3 2\n", "3 3\n" ]
[ "5", "4" ]
none
[ { "input": "3 2", "output": "5" }, { "input": "3 3", "output": "4" }, { "input": "1 1", "output": "1" }, { "input": "2 1", "output": "2" }, { "input": "2 2", "output": "2" }, { "input": "27 11", "output": "61162698256896" }, { "input": "32 ...
0
0
-1
61,449
0
none
[ "none" ]
null
null
Julia is conducting an experiment in her lab. She placed several luminescent bacterial colonies in a horizontal testtube. Different types of bacteria can be distinguished by the color of light they emit. Julia marks types of bacteria with small Latin letters "a", ..., "z". The testtube is divided into *n* consecutive ...
The first line contains an integer *n*Β β€” the number of regions in the testtube (1<=≀<=*n*<=≀<=5<=000). The second line contains *n* small Latin letters that describe the initial population of the testtube.
Print one numberΒ β€” the answer to the problem modulo 109<=+<=7.
[ "3\naaa\n", "2\nab\n", "4\nbabb\n", "7\nabacaba\n" ]
[ "1\n", "3\n", "11\n", "589\n" ]
In the first sample the population can never change since all bacteria are of the same type. In the second sample three configurations are possible: "ab" (no attacks), "aa" (the first colony conquers the second colony), and "bb" (the second colony conquers the first colony). To get the answer for the third sample, no...
[]
46
0
0
61,497
411
Multi-core Processor
[ "implementation" ]
null
null
The research center Q has developed a new multi-core processor. The processor consists of *n* cores and has *k* cells of cache memory. Consider the work of this processor. At each cycle each core of the processor gets one instruction: either do nothing, or the number of the memory cell (the core will write an informat...
The first line contains three integers *n*, *m*, *k* (1<=≀<=*n*,<=*m*,<=*k*<=≀<=100). Then follow *n* lines describing instructions. The *i*-th line contains *m* integers: *x**i*1,<=*x**i*2,<=...,<=*x**im* (0<=≀<=*x**ij*<=≀<=*k*), where *x**ij* is the instruction that must be executed by the *i*-th core at the *j*-th c...
Print *n* lines. In the *i*-th line print integer *t**i*. This number should be equal to 0 if the *i*-th core won't be locked, or it should be equal to the number of the cycle when this core will be locked.
[ "4 3 5\n1 0 0\n1 0 2\n2 3 1\n3 2 0\n", "3 2 2\n1 2\n1 2\n2 2\n", "1 1 1\n0\n" ]
[ "1\n1\n3\n0\n", "1\n1\n0\n", "0\n" ]
none
[ { "input": "4 3 5\n1 0 0\n1 0 2\n2 3 1\n3 2 0", "output": "1\n1\n3\n0" }, { "input": "3 2 2\n1 2\n1 2\n2 2", "output": "1\n1\n0" }, { "input": "1 1 1\n0", "output": "0" }, { "input": "1 1 1\n1", "output": "0" }, { "input": "2 1 1\n1\n1", "output": "1\n1" }, ...
77
307,200
3
61,715
0
none
[ "none" ]
D. Falling Anvils
2
256
For some reason in many American cartoons anvils fall from time to time onto heroes' heads. Of course, safes, wardrobes, cruisers, planes fall sometimes too... But anvils do so most of all. Anvils come in different sizes and shapes. Quite often they get the hero stuck deep in the ground. But have you ever thought who ...
The first line contains integer *t* (1<=≀<=*t*<=≀<=10000) β€” amount of testcases. Each of the following *t* lines contain two space-separated integers *a* and *b* (0<=≀<=*a*,<=*b*<=≀<=106). Pretests contain all the tests with 0<=&lt;<=*a*<=&lt;<=10,<=0<=≀<=*b*<=&lt;<=10.
Print *t* lines β€” the probability of a successful anvil hit for each testcase. The absolute or relative error of the answer should not exceed 10<=-<=6.
[ "2\n4 2\n1 2\n" ]
[ "0.6250000000\n0.5312500000\n" ]
none
[]
154
0
0
61,974
843
Maximum Flow
[ "flows", "graphs" ]
null
null
You are given a directed graph, consisting of *n* vertices and *m* edges. The vertices *s* and *t* are marked as source and sink correspondingly. Additionally, there are no edges ending at *s* and there are no edges beginning in *t*. The graph was constructed in a following way: initially each edge had capacity *c**i*...
The first line of input data contains four positive integers *n*,<=*m*,<=*s*,<=*t* (2<=≀<=*n*<=≀<=100, 1<=≀<=*m*<=≀<=1000, 1<=≀<=*s*,<=*t*<=≀<=*n*, *s*<=β‰ <=*t*) β€” the number of vertices, the number of edges, index of source vertex and index of sink vertex correspondingly. Each of next *m* lines of input data contain n...
In the first line print single non-negative integer *k* β€” minimum number of edges, which should be saturated in maximum flow. In each of next *m* lines print two integers *f**i*,<=*c**i* (1<=≀<=*c**i*<=≀<=109, 0<=≀<=*f**i*<=≀<=*c**i*) β€” the flow value passing through edge *i* and capacity of edge *i*. This data shou...
[ "5 6 1 5\n1 2 1\n2 3 1\n3 5 1\n1 4 1\n4 3 0\n4 5 1\n" ]
[ "2\n3 3\n3 8\n3 4\n4 4\n0 5\n4 9\n" ]
The illustration for second sample case. The saturated edges are marked dark, while edges with *g*<sub class="lower-index">*i*</sub> = 0 are marked with dotted line. The integer on edge is the index of this edge in input list. <img class="tex-graphics" src="https://espresso.codeforces.com/a80cb6067d5d58458863f416f6ac95...
[]
1,000
9,011,200
0
62,160
4
Mysterious Present
[ "dp", "sortings" ]
D. Mysterious Present
1
64
Peter decided to wish happy birthday to his friend from Australia and send him a card. To make his present more mysterious, he decided to make a chain. Chain here is such a sequence of envelopes *A*<==<={*a*1,<=<=*a*2,<=<=...,<=<=*a**n*}, where the width and the height of the *i*-th envelope is strictly higher than the...
The first line contains integers *n*, *w*, *h* (1<=<=≀<=*n*<=≀<=5000, 1<=≀<=*w*,<=<=*h*<=<=≀<=106) β€” amount of envelopes Peter has, the card width and height respectively. Then there follow *n* lines, each of them contains two integer numbers *w**i* and *h**i* β€” width and height of the *i*-th envelope (1<=≀<=*w**i*,<=<...
In the first line print the maximum chain size. In the second line print the numbers of the envelopes (separated by space), forming the required chain, starting with the number of the smallest envelope. Remember, please, that the card should fit into the smallest envelope. If the chain of maximum size is not unique, pr...
[ "2 1 1\n2 2\n2 2\n", "3 3 3\n5 4\n12 11\n9 8\n" ]
[ "1\n1 \n", "3\n1 3 2 \n" ]
none
[ { "input": "2 1 1\n2 2\n2 2", "output": "1\n1 " }, { "input": "3 3 3\n5 4\n12 11\n9 8", "output": "3\n1 3 2 " }, { "input": "5 10 10\n22 23\n17 19\n13 17\n8 12\n2 6", "output": "3\n3 2 1 " }, { "input": "5 13 13\n4 4\n10 10\n7 7\n1 1\n13 13", "output": "0" }, { "i...
77
0
0
62,436
350
Resort
[ "graphs" ]
null
null
Valera's finally decided to go on holiday! He packed up and headed for a ski resort. Valera's fancied a ski trip but he soon realized that he could get lost in this new place. Somebody gave him a useful hint: the resort has *n* objects (we will consider the objects indexed in some way by integers from 1 to *n*), each ...
The first line contains integer *n* (1<=≀<=*n*<=≀<=105) β€” the number of objects. The second line contains *n* space-separated integers *type*1,<=*type*2,<=...,<=*type**n* β€” the types of the objects. If *type**i* equals zero, then the *i*-th object is the mountain. If *type**i* equals one, then the *i*-th object is the...
In the first line print *k* β€” the maximum possible path length for Valera. In the second line print *k* integers *v*1,<=*v*2,<=...,<=*v**k* β€” the path. If there are multiple solutions, you can print any of them.
[ "5\n0 0 0 0 1\n0 1 2 3 4\n", "5\n0 0 1 0 1\n0 1 2 2 4\n", "4\n1 0 0 0\n2 3 4 2\n" ]
[ "5\n1 2 3 4 5\n", "2\n4 5\n", "1\n1\n" ]
none
[ { "input": "5\n0 0 0 0 1\n0 1 2 3 4", "output": "5\n1 2 3 4 5" }, { "input": "5\n0 0 1 0 1\n0 1 2 2 4", "output": "2\n4 5" }, { "input": "4\n1 0 0 0\n2 3 4 2", "output": "1\n1" }, { "input": "10\n0 0 0 0 0 0 0 0 0 1\n4 0 8 4 7 8 5 5 7 2", "output": "2\n2 10" }, { ...
374
36,352,000
0
62,445
508
Tanya and Password
[ "dfs and similar", "graphs" ]
null
null
While dad was at work, a little girl Tanya decided to play with dad's password to his secret database. Dad's password is a string consisting of *n*<=+<=2 characters. She has written all the possible *n* three-letter continuous substrings of the password on pieces of paper, one for each piece of paper, and threw the pas...
The first line contains integer *n* (1<=≀<=*n*<=≀<=2Β·105), the number of three-letter substrings Tanya got. Next *n* lines contain three letters each, forming the substring of dad's password. Each character in the input is a lowercase or uppercase Latin letter or a digit.
If Tanya made a mistake somewhere during the game and the strings that correspond to the given set of substrings don't exist, print "NO". If it is possible to restore the string that corresponds to given set of substrings, print "YES", and then print any suitable password option.
[ "5\naca\naba\naba\ncab\nbac\n", "4\nabc\nbCb\ncb1\nb13\n", "7\naaa\naaa\naaa\naaa\naaa\naaa\naaa\n" ]
[ "YES\nabacaba\n", "NO\n", "YES\naaaaaaaaa\n" ]
none
[ { "input": "5\naca\naba\naba\ncab\nbac", "output": "YES\nabacaba" }, { "input": "4\nabc\nbCb\ncb1\nb13", "output": "NO" }, { "input": "7\naaa\naaa\naaa\naaa\naaa\naaa\naaa", "output": "YES\naaaaaaaaa" }, { "input": "1\nabc", "output": "YES\nabc" }, { "input": "2\n...
31
0
0
62,519
109
Lucky Probability
[ "brute force", "probabilities" ]
B. Lucky Probability
2
256
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya and his friend Vasya play an interesting game. Petya randomly chooses an integer *p* from the ...
The single line contains five integers *p**l*, *p**r*, *v**l*, *v**r* and *k* (1<=≀<=*p**l*<=≀<=*p**r*<=≀<=109,<=1<=≀<=*v**l*<=≀<=*v**r*<=≀<=109,<=1<=≀<=*k*<=≀<=1000).
On the single line print the result with an absolute error of no more than 10<=-<=9.
[ "1 10 1 10 2\n", "5 6 8 10 1\n" ]
[ "0.320000000000\n", "1.000000000000\n" ]
Consider that [*a*, *b*] denotes an interval of integers; this interval includes the boundaries. That is, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/18b4a6012d95ad18891561410f0314497a578d63.png" style="max-width: 100.0%;max-height: 100.0%;"/> In first case there are 32 suitable pairs:...
[ { "input": "1 10 1 10 2", "output": "0.320000000000" }, { "input": "5 6 8 10 1", "output": "1.000000000000" }, { "input": "1 20 100 120 5", "output": "0.150000000000" }, { "input": "1 10 1 10 3", "output": "0.000000000000" }, { "input": "1 100 1 100 2", "outpu...
92
0
0
62,527
472
Design Tutorial: Change the Goal
[ "constructive algorithms", "math", "matrices" ]
null
null
There are some tasks which have the following structure: you are given a model, and you can do some operations, you should use these operations to achive the goal. One way to create a new task is to use the same model and same operations, but change the goal. Let's have a try. I have created the following task for Top...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=10000). The second line contains *n* integers: *x*1 to *x**n* (0<=≀<=*x**i*<=≀<=109). The third line contains *n* integers: *y*1 to *y**n* (0<=≀<=*y**i*<=≀<=109).
If there is no solution, output -1. If there is a solution, then in the first line output an integer *m* (0<=≀<=*m*<=≀<=1000000) – the number of assignments you need to perform. Then print *m* lines, each line should contain two integers *i* and *j* (1<=≀<=*i*,<=*j*<=≀<=*n*), which denote assignment *x**i* ^= *x**j*. ...
[ "2\n3 5\n6 0\n", "5\n0 0 0 0 0\n1 2 3 4 5\n", "3\n4 5 6\n1 2 3\n", "3\n1 2 3\n4 5 6\n" ]
[ "2\n1 2\n2 2\n", "-1\n", "5\n3 1\n1 2\n2 2\n2 3\n3 1\n", "-1\n" ]
Assignment *a* ^= *b* denotes assignment *a* = *a* ^ *b*, where operation "^" is bitwise XOR of two integers.
[]
62
0
0
62,529
375
Maximum Submatrix 2
[ "data structures", "dp", "implementation", "sortings" ]
null
null
You are given a matrix consisting of digits zero and one, its size is *n*<=Γ—<=*m*. You are allowed to rearrange its rows. What is the maximum area of the submatrix that only consists of ones and can be obtained in the given problem by the described operations? Let's assume that the rows of matrix *a* are numbered from...
The first line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=5000). Next *n* lines contain *m* characters each β€” matrix *a*. Matrix *a* only contains characters: "0" and "1". Note that the elements of the matrix follow without any spaces in the lines.
Print a single integer β€” the area of the maximum obtained submatrix. If we cannot obtain a matrix of numbers one, print 0.
[ "1 1\n1\n", "2 2\n10\n11\n", "4 3\n100\n011\n000\n101\n" ]
[ "1\n", "2\n", "2\n" ]
none
[ { "input": "1 1\n1", "output": "1" }, { "input": "2 2\n10\n11", "output": "2" }, { "input": "4 3\n100\n011\n000\n101", "output": "2" }, { "input": "11 16\n0111110101100011\n1000101100010000\n0010110110010101\n0110110010110010\n0011101101110000\n1001100011010111\n0010011111111...
2,000
54,476,800
0
62,600
718
Sasha and Array
[ "data structures", "math", "matrices" ]
null
null
Sasha has an array of integers *a*1,<=*a*2,<=...,<=*a**n*. You have to perform *m* queries. There might be queries of two types: 1. 1 l r xΒ β€” increase all integers on the segment from *l* to *r* by values *x*; 1. 2 l rΒ β€” find , where *f*(*x*) is the *x*-th Fibonacci number. As this number may be large, you only have...
The first line of the input contains two integers *n* and *m* (1<=≀<=*n*<=≀<=100<=000, 1<=≀<=*m*<=≀<=100<=000)Β β€” the number of elements in the array and the number of queries respectively. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109). Then follow *m* lines with queries descrip...
For each query of the second type print the answer modulo 109<=+<=7.
[ "5 4\n1 1 2 1 1\n2 1 5\n1 2 4 2\n2 2 4\n2 1 5\n" ]
[ "5\n7\n9\n" ]
Initially, array *a* is equal to 1, 1, 2, 1, 1. The answer for the first query of the second type is *f*(1) + *f*(1) + *f*(2) + *f*(1) + *f*(1) = 1 + 1 + 1 + 1 + 1 = 5. After the query 1 2 4 2 array *a* is equal to 1, 3, 4, 3, 1. The answer for the second query of the second type is *f*(3) + *f*(4) + *f*(3) = 2 + 3...
[ { "input": "5 4\n1 1 2 1 1\n2 1 5\n1 2 4 2\n2 2 4\n2 1 5", "output": "5\n7\n9" }, { "input": "2 3\n1 3\n2 1 1\n1 1 2 3\n1 1 2 2", "output": "1" }, { "input": "7 4\n2 2 1 1 3 3 2\n2 1 5\n2 6 7\n1 3 4 3\n2 6 6", "output": "6\n3\n2" }, { "input": "9 4\n2 1 2 3 3 3 2 1 3\n2 1 8\n...
15
0
0
62,740
732
Exams
[ "binary search", "greedy", "sortings" ]
null
null
Vasiliy has an exam period which will continue for *n* days. He has to pass exams on *m* subjects. Subjects are numbered from 1 to *m*. About every day we know exam for which one of *m* subjects can be passed on that day. Perhaps, some day you can't pass any exam. It is not allowed to pass more than one exam on any da...
The first line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=105)Β β€” the number of days in the exam period and the number of subjects. The second line contains *n* integers *d*1,<=*d*2,<=...,<=*d**n* (0<=≀<=*d**i*<=≀<=*m*), where *d**i* is the number of subject, the exam of which can be passed on the day numb...
Print one integerΒ β€” the minimum number of days in which Vasiliy can pass all exams. If it is impossible, print -1.
[ "7 2\n0 1 0 2 1 0 2\n2 1\n", "10 3\n0 0 1 2 3 0 2 0 1 2\n1 1 4\n", "5 1\n1 1 1 1 1\n5\n" ]
[ "5\n", "9\n", "-1\n" ]
In the first example Vasiliy can behave as follows. On the first and the second day he can prepare for the exam number 1 and pass it on the fifth day, prepare for the exam number 2 on the third day and pass it on the fourth day. In the second example Vasiliy should prepare for the exam number 3 during the first four d...
[ { "input": "7 2\n0 1 0 2 1 0 2\n2 1", "output": "5" }, { "input": "10 3\n0 0 1 2 3 0 2 0 1 2\n1 1 4", "output": "9" }, { "input": "5 1\n1 1 1 1 1\n5", "output": "-1" }, { "input": "100 10\n1 1 6 6 6 2 5 7 6 5 3 7 10 10 8 9 7 6 9 2 6 7 8 6 7 5 2 5 10 1 10 1 8 10 2 9 7 1 6 8 3 ...
139
12,492,800
3
62,800
756
Nikita and stack
[ "data structures" ]
null
null
Nikita has a stack. A stack in this problem is a data structure that supports two operations. Operation push(x) puts an integer *x* on the top of the stack, and operation pop() deletes the top integer from the stack, i.Β e. the last added. If the stack is empty, then the operation pop() does nothing. Nikita made *m* op...
The first line contains the integer *m* (1<=≀<=*m*<=≀<=105)Β β€” the number of operations Nikita made. The next *m* lines contain the operations Nikita remembers. The *i*-th line starts with two integers *p**i* and *t**i* (1<=≀<=*p**i*<=≀<=*m*, *t**i*<==<=0 or *t**i*<==<=1)Β β€” the index of operation he remembers on the st...
Print *m* integers. The integer *i* should equal the number on the top of the stack after performing all the operations Nikita remembered on the steps from 1 to *i*. If the stack is empty after performing all these operations, print -1.
[ "2\n2 1 2\n1 0\n", "3\n1 1 2\n2 1 3\n3 0\n", "5\n5 0\n4 0\n3 1 1\n2 1 1\n1 1 2\n" ]
[ "2\n2\n", "2\n3\n2\n", "-1\n-1\n-1\n-1\n2\n" ]
In the first example, after Nikita remembers the operation on the first step, the operation push(2) is the only operation, so the answer is 2. After he remembers the operation pop() which was done before push(2), answer stays the same. In the second example, the operations are push(2), push(3) and pop(). Nikita rememb...
[]
31
0
0
62,945
360
Levko and Array
[ "binary search", "dp" ]
null
null
Levko has an array that consists of integers: *a*1,<=*a*2,<=... ,<=*a**n*. But he doesn’t like this array at all. Levko thinks that the beauty of the array *a* directly depends on value *c*(*a*), which can be calculated by the formula: It’s time to change the world and Levko is going to change his array for the bett...
The first line contains two integers *n* and *k* (1<=≀<=*k*<=≀<=*n*<=≀<=2000). The second line contains space-separated integers *a*1,<=*a*2,<=... ,<=*a**n* (<=-<=109<=≀<=*a**i*<=≀<=109).
A single number β€” the minimum value of *c*(*a*) Levko can get.
[ "5 2\n4 7 4 7 4\n", "3 1\n-100 0 100\n", "6 3\n1 2 3 7 8 9\n" ]
[ "0\n", "100\n", "1\n" ]
In the first sample Levko can change the second and fourth elements and get array: 4, 4, 4, 4, 4. In the third sample he can get array: 1, 2, 3, 4, 5, 6.
[ { "input": "5 2\n4 7 4 7 4", "output": "0" }, { "input": "3 1\n-100 0 100", "output": "100" }, { "input": "6 3\n1 2 3 7 8 9", "output": "1" }, { "input": "4 1\n-1000000000 -1000000000 1000000000 1000000000", "output": "1000000000" }, { "input": "10 1\n-6 5 -7 -7 -...
2,000
4,608,000
0
62,992
993
Compute Power
[ "binary search", "dp", "greedy" ]
null
null
You need to execute several tasks, each associated with number of processors it needs, and the compute power it will consume. You have sufficient number of analog computers, each with enough processors for any task. Each computer can execute up to one task at a time, and no more than two tasks total. The first task ca...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=50) β€” the number of tasks. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=108), where *a**i* represents the amount of power required for the *i*-th task. The third line contains *n* integers *b*1,<=*b*2,<=...,<=*b**n* (1<=...
Print a single integer value β€” the lowest threshold for which it is possible to assign all tasks in such a way that the system will not blow up after the first round of computation, multiplied by 1000 and rounded up.
[ "6\n8 10 9 9 8 10\n1 1 1 1 1 1\n", "6\n8 10 9 9 8 10\n1 10 5 5 1 10\n" ]
[ "9000\n", "1160\n" ]
In the first example the best strategy is to run each task on a separate computer, getting average compute per processor during the first round equal to 9. In the second task it is best to run tasks with compute 10 and 9 on one computer, tasks with compute 10 and 8 on another, and tasks with compute 9 and 8 on the las...
[ { "input": "6\n8 10 9 9 8 10\n1 1 1 1 1 1", "output": "9000" }, { "input": "6\n8 10 9 9 8 10\n1 10 5 5 1 10", "output": "1160" }, { "input": "1\n1\n100", "output": "10" }, { "input": "50\n83 43 73 75 11 53 6 43 67 38 83 12 70 27 60 13 9 79 61 30 29 71 10 11 95 87 26 26 19 99 ...
46
0
0
63,083
681
Gifts by the List
[ "constructive algorithms", "dfs and similar", "graphs", "trees" ]
null
null
Sasha lives in a big happy family. At the Man's Day all the men of the family gather to celebrate it following their own traditions. There are *n* men in Sasha's family, so let's number them with integers from 1 to *n*. Each man has at most one father but may have arbitrary number of sons. Man number *A* is considere...
In the first line of the input two integers *n* and *m* (0<=≀<=*m*<=&lt;<=*n*<=≀<=100<=000) are givenΒ β€” the number of the men in the Sasha's family and the number of family relations in it respectively. The next *m* lines describe family relations: the (*i*<=+<=1)*th* line consists of pair of integers *p**i* and *q**i...
Print an integer *k* (1<=≀<=*k*<=≀<=*n*)Β β€” the number of the men in the list of candidates, in the first line. Print then *k* pairwise different positive integers not exceeding *n* β€” the numbers of the men in the list in an order satisfying every of the men's wishes, one per line. If there are more than one appropria...
[ "3 2\n1 2\n2 3\n1 2 1\n", "4 2\n1 2\n3 4\n1 2 3 3\n" ]
[ "-1", "3\n2\n1\n3\n" ]
The first sample explanation: - if there would be no 1 in the list then the first and the third man's wishes would not be satisfied (*a*<sub class="lower-index">1</sub> = *a*<sub class="lower-index">3</sub> = 1); - if there would be no 2 in the list then the second man wish would not be satisfied (*a*<sub class="lo...
[ { "input": "3 2\n1 2\n2 3\n1 2 1", "output": "-1" }, { "input": "4 2\n1 2\n3 4\n1 2 3 3", "output": "3\n2\n1\n3" }, { "input": "1 0\n1", "output": "1\n1" }, { "input": "2 1\n2 1\n2 2", "output": "1\n2" }, { "input": "2 1\n2 1\n1 2", "output": "2\n1\n2" }, ...
1,000
19,046,400
0
63,374
323
Tournament-graph
[ "constructive algorithms", "graphs" ]
null
null
In this problem you have to build tournament graph, consisting of *n* vertices, such, that for any oriented pair of vertices (*v*,<=*u*) (*v*<=β‰ <=*u*) there exists a path from vertex *v* to vertex *u* consisting of no more then two edges. A directed graph without self-loops is a tournament, if there is exactly one edg...
The first line contains an integer *n* (3<=≀<=*n*<=≀<=1000), the number of the graph's vertices.
Print -1 if there is no graph, satisfying the described conditions. Otherwise, print *n* lines with *n* integers in each. The numbers should be separated with spaces. That is adjacency matrix *a* of the found tournament. Consider the graph vertices to be numbered with integers from 1 to *n*. Then *a**v*,<=*u*<==<=0, i...
[ "3\n", "4\n" ]
[ "0 1 0\n0 0 1\n1 0 0\n", "-1\n" ]
none
[]
78
102,400
0
63,654
720
Slalom
[ "data structures", "dp", "sortings" ]
null
null
Little girl Masha likes winter sports, today she's planning to take part in slalom skiing. The track is represented as a grid composed of *n*<=Γ—<=*m* squares. There are rectangular obstacles at the track, composed of grid squares. Masha must get from the square (1,<=1) to the square (*n*,<=*m*). She can move from a sq...
The first line of input data contains three positive integers: *n*, *m* and *k* (3<=≀<=*n*,<=*m*<=≀<=106, 0<=≀<=*k*<=≀<=105)Β β€” the size of the track and the number of obstacles. The following *k* lines contain four positive integers each: *x*1, *y*1, *x*2, *y*2 (1<=≀<=*x*1<=≀<=*x*2<=≀<=*n*, 1<=≀<=*y*1<=≀<=*y*2<=≀<=*m*...
Output one integerΒ β€” the number of ways to pass the track modulo 109<=+<=7.
[ "3 3 0\n", "4 5 1\n2 2 3 4\n", "5 5 3\n2 2 2 3\n4 2 5 2\n4 4 4 4\n" ]
[ "1\n", "2\n", "3\n" ]
none
[]
30
0
0
63,736
362
Insertion Sort
[ "data structures", "dp", "implementation", "math" ]
null
null
Petya is a beginner programmer. He has already mastered the basics of the C++ language and moved on to learning algorithms. The first algorithm he encountered was insertion sort. Petya has already written the code that implements this algorithm and sorts the given integer zero-indexed array *a* of size *n* in the non-d...
The first line contains a single integer *n* (2<=≀<=*n*<=≀<=5000) β€” the length of the permutation. The second line contains *n* different integers from 0 to *n*<=-<=1, inclusive β€” the actual permutation.
Print two integers: the minimum number of times the swap function is executed and the number of such pairs (*i*,<=*j*) that swapping the elements of the input permutation with indexes *i* and *j* leads to the minimum number of the executions.
[ "5\n4 0 3 1 2\n", "5\n1 2 3 4 0\n" ]
[ "3 2\n", "3 4\n" ]
In the first sample the appropriate pairs are (0, 3) and (0, 4). In the second sample the appropriate pairs are (0, 4), (1, 4), (2, 4) and (3, 4).
[ { "input": "5\n4 0 3 1 2", "output": "3 2" }, { "input": "5\n1 2 3 4 0", "output": "3 4" }, { "input": "5\n1 3 4 0 2", "output": "4 5" }, { "input": "10\n9 8 7 6 5 4 3 2 1 0", "output": "28 1" }, { "input": "5\n0 4 1 3 2", "output": "1 1" }, { "input":...
31
307,200
0
63,846
958
Maximum Control (medium)
[ "data structures", "dfs and similar", "graphs", "greedy", "trees" ]
null
null
The Resistance is trying to take control over as many planets of a particular solar system as possible. Princess Heidi is in charge of the fleet, and she must send ships to some planets in order to maximize the number of controlled planets. The Galaxy contains *N* planets, connected by bidirectional hyperspace tunnels...
The first line of the input contains an integer *N* (1<=≀<=*N*<=≀<=105) – the number of planets in the galaxy. The next *N*<=-<=1 lines describe the hyperspace tunnels between the planets. Each of the *N*<=-<=1 lines contains two space-separated integers *u* and *v* (1<=≀<=*u*,<=*v*<=≀<=*N*) indicating that there is a...
On a single line, print *N* space-separated integers. The *K*-th number should correspond to the maximum number of planets that can be controlled by the Resistance using a fleet of *K* ships.
[ "3\n1 2\n2 3\n", "4\n1 2\n3 2\n4 2\n" ]
[ "1 3 3 ", "1 3 4 4 " ]
Consider the first example. If *K* = 1, then Heidi can only send one ship to some planet and control it. However, for *K* β‰₯ 2, sending ships to planets 1 and 3 will allow the Resistance to control all planets.
[ { "input": "3\n1 2\n2 3", "output": "1 3 3 " }, { "input": "4\n1 2\n3 2\n4 2", "output": "1 3 4 4 " }, { "input": "19\n2 19\n7 15\n8 10\n16 1\n12 5\n11 5\n6 18\n12 14\n14 15\n2 6\n9 14\n4 17\n16 10\n4 2\n7 18\n3 2\n9 13\n11 10", "output": "1 14 16 17 18 19 19 19 19 19 19 19 19 19 19 ...
77
7,372,800
0
64,084
0
none
[ "none" ]
null
null
One must train much to do well on wizardry contests. So, there are numerous wizardry schools and magic fees. One of such magic schools consists of *n* tours. A winner of each tour gets a huge prize. The school is organised quite far away, so one will have to take all the prizes home in one go. And the bags that you've...
The first line contains three integers *n*, *l*, *k* (1<=≀<=*n*<=≀<=200,<=0<=≀<=*l*,<=*k*<=≀<=200) β€” the number of tours, the minimum number of tours to win, and the number of prizes that you can fit in the bags brought from home, correspondingly. The second line contains *n* space-separated integers, *p**i* (0<=≀<=*p...
Print a single real number β€” the answer to the problem. The answer will be accepted if the absolute or relative error does not exceed 10<=-<=6.
[ "3 1 0\n10 20 30\n-1 -1 2\n", "1 1 1\n100\n123\n" ]
[ "0.300000000000\n", "1.000000000000\n" ]
In the first sample we need either win no tour or win the third one. If we win nothing we wouldn't perform well. So, we must to win the third tour. Other conditions will be satisfied in this case. Probability of wining the third tour is 0.3. In the second sample we win the only tour with probability 1.0, and go back h...
[ { "input": "3 1 0\n10 20 30\n-1 -1 2", "output": "0.300000000000" }, { "input": "1 1 1\n100\n123", "output": "1.000000000000" }, { "input": "5 1 2\n36 44 13 83 63\n-1 2 -1 2 1", "output": "0.980387276800" }, { "input": "9 9 2\n91 96 99 60 42 67 46 39 62\n5 -1 2 -1 -1 -1 7 -1 ...
60
0
0
64,275
959
Mahmoud and Ehab and yet another xor task
[ "bitmasks", "dp", "math", "matrices" ]
null
null
Ehab has an array *a* of *n* integers. He likes the [bitwise-xor operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) and he likes to bother Mahmoud so he came up with a problem. He gave Mahmoud *q* queries. In each of them, he gave Mahmoud 2 integers *l* and *x*, and asked him to find the number of subseque...
The first line contains integers *n* and *q* (1<=≀<=*n*,<=*q*<=≀<=105), the number of elements in the array and the number of queries. The next line contains *n* integers *a*1, *a*2, ..., *a**n* (0<=≀<=*a**i*<=&lt;<=220), the elements of the array. The next *q* lines, each contains integers *l* and *x* (1<=≀<=*l*<=≀<...
For each query, output its answer modulo 109<=+<=7 in a newline.
[ "5 5\n0 1 2 3 4\n4 3\n2 0\n3 7\n5 7\n5 8\n", "3 2\n1 1 1\n3 1\n2 0\n" ]
[ "4\n2\n0\n4\n0\n", "4\n2\n" ]
The bitwise-xor sum of the empty set is 0 and the bitwise-xor sum of a set containing one element is that element itself.
[ { "input": "5 5\n0 1 2 3 4\n4 3\n2 0\n3 7\n5 7\n5 8", "output": "4\n2\n0\n4\n0" }, { "input": "3 2\n1 1 1\n3 1\n2 0", "output": "4\n2" }, { "input": "20 20\n353123 353123 677328 353123 0 997043 677328 420884 968991 566539 420884 801220 1030642 937882 762558 599450 196420 299659 101896 60...
46
0
0
64,369
842
Nikita and game
[ "binary search", "dfs and similar", "divide and conquer", "graphs", "trees" ]
null
null
Nikita plays a new computer game. There are *m* levels in this game. In the beginning of each level a new class appears in the game; this class is a child-class of the class *y**i* (and *y**i* is called parent-class for this new class). Thus, the classes form a tree. Initially there is only one class with index 1. Cha...
First line contains one integer number *m*Β β€” number of queries (1<=≀<=*m*<=≀<=3Β·105). Next *m* lines contain description of queries. *i*Β -th line (1<=≀<=*i*<=≀<=*m*) describes the *i*Β -th level and contains an integer *y**i*Β β€” the index of the parent-class of class with index *i*<=+<=1 (1<=≀<=*y**i*<=≀<=*i*).
Suppose that at *i*Β -th level the maximum cost of changing one class to another is *x*. For each level output the number of classes such that for each of these classes there exists some other class *y*, and the distance from this class to *y* is exactly *x*.
[ "4\n1\n1\n2\n1\n", "4\n1\n1\n2\n3\n" ]
[ "2\n2\n2\n3\n", "2\n2\n2\n2\n" ]
none
[]
30
0
0
64,481
708
Incorrect Flow
[ "flows" ]
null
null
At the entrance examination for the magistracy of the MSU Cyber-Mechanics Department Sasha got the question about Ford-Fulkerson algorithm. He knew the topic perfectly as he worked with it many times on programming competition. As the task for the question he was given a network with partially build flow that he had to...
The first line of the input contains two integers *n* and *m* (2<=≀<=*n*<=≀<=100, 0<=≀<=*m*<=≀<=100)Β β€” the number of nodes and edges in the graph respectively. Each of the following *m* lines contains the description of the edges, consisting of four integers *u**i*, *v**i*, *c**i* and *f**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=...
Print one integerΒ β€” the minimum total sum of changes that Sasha has to do in order to get the correct flow description.
[ "2 1\n1 2 2 1\n", "2 1\n1 2 1 2\n", "3 3\n1 2 1 1\n2 3 2 2\n1 3 3 3\n", "4 2\n2 3 1 1\n3 2 1 1\n" ]
[ "0\n", "1\n", "1\n", "0\n" ]
In the first sample, the flow is initially correct. Note, that the flow is not maximum, but this is not required. In the second sample, the flow value of the only edge is greater than its capacity. There are two ways to fix this: either increase the capacity up to 2 or reduce the flow down to 1. In the third sample, ...
[]
30
0
0
64,592
898
Restoring the Expression
[ "brute force", "hashing", "math" ]
null
null
A correct expression of the form a+b=c was written; *a*, *b* and *c* are non-negative integers without leading zeros. In this expression, the plus and equally signs were lost. The task is to restore the expression. In other words, one character '+' and one character '=' should be inserted into given sequence of digits ...
The first line contains a non-empty string consisting of digits. The length of the string does not exceed 106.
Output the restored expression. If there are several solutions, you can print any of them. Note that the answer at first should contain two terms (divided with symbol '+'), and then the result of their addition, before which symbol'=' should be. Do not separate numbers and operation signs with spaces. Strictly follo...
[ "12345168\n", "099\n", "199100\n", "123123123456456456579579579\n" ]
[ "123+45=168\n", "0+9=9\n", "1+99=100\n", "123123123+456456456=579579579\n" ]
none
[ { "input": "12345168", "output": "123+45=168" }, { "input": "099", "output": "0+9=9" }, { "input": "199100", "output": "1+99=100" }, { "input": "123123123456456456579579579", "output": "123123123+456456456=579579579" }, { "input": "112", "output": "1+1=2" },...
93
307,200
0
64,662
220
Little Elephant and Inversions
[ "data structures", "two pointers" ]
null
null
The Little Elephant has array *a*, consisting of *n* positive integers, indexed from 1 to *n*. Let's denote the number with index *i* as *a**i*. The Little Elephant wants to count, how many pairs of integers *l* and *r* are there, such that 1<=≀<=*l*<=&lt;<=*r*<=≀<=*n* and sequence *b*<==<=*a*1*a*2... *a**l**a**r**a**...
The first line contains two integers *n* and *k* (2<=≀<=*n*<=≀<=105,<=0<=≀<=*k*<=≀<=1018) β€” the size of array *a* and the maximum allowed number of inversions respectively. The next line contains *n* positive integers, separated by single spaces, *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109) β€” elements of array *a*...
In a single line print a single number β€” the answer to the problem.
[ "3 1\n1 3 2\n", "5 2\n1 3 2 1 7\n" ]
[ "3\n", "6\n" ]
none
[ { "input": "3 1\n1 3 2", "output": "3" }, { "input": "5 2\n1 3 2 1 7", "output": "6" }, { "input": "7 3\n1 7 6 4 9 5 3", "output": "6" }, { "input": "5 0\n1 2 3 4 5", "output": "10" }, { "input": "2 1\n2 1", "output": "1" }, { "input": "3 1000000000000...
310
0
0
64,675
630
Indivisibility
[ "math", "number theory" ]
null
null
IT City company developing computer games decided to upgrade its way to reward its employees. Now it looks the following way. After a new game release users start buying it actively, and the company tracks the number of sales with precision to each transaction. Every time when the next number of sales is not divisible ...
The only line of the input contains one integer *n* (1<=≀<=*n*<=≀<=1018) β€” the prediction on the number of people who will buy the game.
Output one integer showing how many numbers from 1 to *n* are not divisible by any number from 2 to 10.
[ "12\n" ]
[ "2" ]
none
[ { "input": "12", "output": "2" }, { "input": "2519", "output": "576" }, { "input": "2521", "output": "577" }, { "input": "1", "output": "1" }, { "input": "314159265", "output": "71807832" }, { "input": "718281828459045235", "output": "1641787036477...
46
0
0
64,880
409
On a plane
[ "*special", "geometry" ]
null
null
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=1000) β€” the number of points on a plane. Each of the next *n* lines contains two real coordinates *x**i* and *y**i* of the point, specified with exactly 2 fractional digits. All coordinates are between <=-<=1000 and 1000, inclusive. Output a single real num...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=1000) β€” the number of points on a plane. Each of the next *n* lines contains two real coordinates *x**i* and *y**i* of the point, specified with exactly 2 fractional digits. All coordinates are between <=-<=1000 and 1000, inclusive.
Output a single real number ΞΈ β€” the answer to the problem statement. The absolute or relative error of your answer should be at most 10<=-<=2.
[ "8\n-2.14 2.06\n-1.14 2.04\n-2.16 1.46\n-2.14 0.70\n-1.42 0.40\n-0.94 -0.48\n-1.42 -1.28\n-2.16 -1.62\n", "5\n2.26 1.44\n2.28 0.64\n2.30 -0.30\n1.58 0.66\n3.24 0.66\n", "8\n6.98 2.06\n6.40 1.12\n5.98 0.24\n5.54 -0.60\n7.16 0.30\n7.82 1.24\n8.34 0.24\n8.74 -0.76\n", "5\n10.44 2.06\n10.90 0.80\n11.48 -0.48\n12....
[ "5.410\n", "5.620\n", "5.480\n", "6.040\n", "6.040\n", "6.720\n" ]
none
[ { "input": "8\n-2.14 2.06\n-1.14 2.04\n-2.16 1.46\n-2.14 0.70\n-1.42 0.40\n-0.94 -0.48\n-1.42 -1.28\n-2.16 -1.62", "output": "5.410" }, { "input": "5\n2.26 1.44\n2.28 0.64\n2.30 -0.30\n1.58 0.66\n3.24 0.66", "output": "5.620" }, { "input": "8\n6.98 2.06\n6.40 1.12\n5.98 0.24\n5.54 -0.60\...
109
307,200
3
65,294
37
Trial for Chief
[ "graphs", "greedy", "shortest paths" ]
E. Trial for Chief
2
256
Having unraveled the Berland Dictionary, the scientists managed to read the notes of the chroniclers of that time. For example, they learned how the chief of the ancient Berland tribe was chosen. As soon as enough pretenders was picked, the following test took place among them: the chief of the tribe took a slab divid...
The first line contains two integers *N* and *M* (1<=≀<=*N*,<=*M*<=≀<=50) β€” the number of lines and columns on the slab. The next *N* lines contain *M* symbols each β€” the final coloration of the slab. *W* stands for the square that should be painted white and *B* β€” for the square that should be painted black.
In the single line output the minimal number of repaintings of side-linked areas needed to get the required coloration of the slab.
[ "3 3\nWBW\nBWB\nWBW\n", "2 3\nBBB\nBWB\n" ]
[ "2\n", "1\n" ]
none
[ { "input": "3 3\nWBW\nBWB\nWBW", "output": "2" }, { "input": "2 3\nBBB\nBWB", "output": "1" }, { "input": "9 29\nBWBBBBBBBBBWBWWBBBWBWBBBWWBWW\nWBWBBWBBWBWWBWBBBWBWWWBWBBBBB\nBWBBBBWWBBBWBWBBWWBBWBBBBBBBB\nBBBWWBBWWBBBWBWBBBWWWWWWBBBBW\nBBWWWWBBBBBBBBBWBBBBBBBBBBWBW\nBBBWWBBBBWBBBWWBBBWB...
62
0
0
65,381
58
Calendar
[ "greedy", "strings" ]
D. Calendar
2
256
BerOilGasDiamondBank has branches in *n* cities, at that *n* is an even number. The bank management wants to publish a calendar with the names of all those cities written in two columns: the calendar should consist of exactly *n*<=/<=2 lines of strictly equal length, each of which contains exactly two names and exactly...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=104, *n* is even) which is the number of branches. Then follow *n* lines which are the names of the cities. All the names consist of lowercase Latin letters; their lengths are no less than 1 and no more than 10 symbols. The next line contains a single symbol *d* (*d...
Print *n*<=/<=2 lines of similar length which are the required calendar. Every line should contain exactly two words and exactly one separator between them. If there are several solutions, print the lexicographically minimal one. The lexicographical comparison of lines is realized by the "&lt;" operator in the modern p...
[ "4\nb\naa\nhg\nc\n.\n", "2\naa\na\n!\n", "2\naa\na\n|\n" ]
[ "aa.b\nc.hg\n", "a!aa\n", "aa|a\n" ]
none
[ { "input": "4\nb\naa\nhg\nc\n.", "output": "aa.b\nc.hg" }, { "input": "2\naa\na\n!", "output": "a!aa" }, { "input": "2\naa\na\n|", "output": "aa|a" }, { "input": "4\nqhcivbxotj\nirgxzzxvw\npxdmcyszvk\nyyaevcdal\n~", "output": "irgxzzxvw~pxdmcyszvk\nqhcivbxotj~yyaevcdal" ...
186
0
0
65,410
611
New Year and Cake
[ "geometry", "two pointers" ]
null
null
Limak is a little polar bear. According to some old traditions, his bear family prepared a New Year cake. And Limak likes cakes. As you may know, a New Year cake is a strictly convex polygon with *n* vertices. Parents won't allow Limak to eat more than half of a cake because he would get sick. After some thinking the...
The first line of the input contains a single integer *n* (4<=≀<=*n*<=≀<=500<=000)Β β€” the number of vertices in the polygon denoting the cake. Each of the next *n* lines contains two integers *x**i* and *y**i* (|*x**i*|,<=|*y**i*|<=≀<=109)Β β€” coordinates of the *i*-th point. It's guaranteed that all points are distinct...
Print the sum of values of Limak's disappointment over all possible scenarios modulo 109<=+<=7.
[ "5\n2 4\n2 7\n5 7\n5 4\n3 -2\n", "4\n-1000000000 -5000000\n0 1234567\n1 1\n-5 -100000000\n", "8\n-10 0\n-6 6\n0 10\n6 6\n10 0\n6 -6\n0 -10\n-6 -6\n" ]
[ "90\n", "525185196\n", "5216\n" ]
In the first sample possible values of Limak's disappointment are 0, 18, 18, 24, 30.
[]
2,000
43,622,400
0
65,765
216
Martian Luck
[ "math", "number theory" ]
null
null
You know that the Martians use a number system with base *k*. Digit *b* (0<=≀<=*b*<=&lt;<=*k*) is considered lucky, as the first contact between the Martians and the Earthlings occurred in year *b* (by Martian chronology). A digital root *d*(*x*) of number *x* is a number that consists of a single digit, resulting aft...
The first line contains three integers *k*, *b* and *n* (2<=≀<=*k*<=≀<=109, 0<=≀<=*b*<=&lt;<=*k*, 1<=≀<=*n*<=≀<=105). The second line contains string *s* as a sequence of *n* integers, representing digits in the *k*-base notation: the *i*-th integer equals *a**i* (0<=≀<=*a**i*<=&lt;<=*k*) β€” the *i*-th digit of string ...
Print a single integer β€” the number of substrings that are lucky numbers. 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 5 6\n3 2 0 5 6 1\n", "7 6 4\n3 5 0 4\n", "257 0 3\n0 0 256\n" ]
[ "5", "1", "3" ]
In the first sample the following substrings have the sought digital root: *s*[1... 2] = "3 2", *s*[1... 3] = "3 2 0", *s*[3... 4] = "0 5", *s*[4... 4] = "5" and *s*[2... 6] = "2 0 5 6 1".
[ { "input": "10 5 6\n3 2 0 5 6 1", "output": "5" }, { "input": "7 6 4\n3 5 0 4", "output": "1" }, { "input": "257 0 3\n0 0 256", "output": "3" }, { "input": "2 1 1\n0", "output": "0" }, { "input": "2 0 20\n1 1 1 0 1 1 1 1 0 0 0 0 1 0 0 0 0 1 0 1", "output": "22...
92
0
0
65,969
31
Chocolate
[ "dfs and similar", "implementation" ]
D. Chocolate
2
256
Bob has a rectangular chocolate bar of the size *W*<=Γ—<=*H*. He introduced a cartesian coordinate system so that the point (0,<=0) corresponds to the lower-left corner of the bar, and the point (*W*,<=*H*) corresponds to the upper-right corner. Bob decided to split the bar into pieces by breaking it. Each break is a se...
The first line contains 3 integers *W*, *H* and *n* (1<=≀<=*W*,<=*H*,<=*n*<=≀<=100) β€” width of the bar, height of the bar and amount of breaks. Each of the following *n* lines contains four integers *x**i*,<=1,<=*y**i*,<=1,<=*x**i*,<=2,<=*y**i*,<=2 β€” coordinates of the endpoints of the *i*-th break (0<=≀<=*x**i*,<=1<=≀...
Output *n*<=+<=1 numbers β€” areas of the resulting parts in the increasing order.
[ "2 2 2\n1 0 1 2\n0 1 1 1\n", "2 2 3\n1 0 1 2\n0 1 1 1\n1 1 2 1\n", "2 4 2\n0 1 2 1\n0 3 2 3\n" ]
[ "1 1 2 ", "1 1 1 1 ", "2 2 4 " ]
none
[ { "input": "2 2 2\n1 0 1 2\n0 1 1 1", "output": "1 1 2 " }, { "input": "2 2 3\n1 0 1 2\n0 1 1 1\n1 1 2 1", "output": "1 1 1 1 " }, { "input": "2 4 2\n0 1 2 1\n0 3 2 3", "output": "2 2 4 " }, { "input": "5 5 3\n2 1 2 5\n0 1 5 1\n4 0 4 1", "output": "1 4 8 12 " }, { ...
374
2,252,800
3.902304
66,034
494
Obsessive String
[ "dp", "strings" ]
null
null
Hamed has recently found a string *t* and suddenly became quite fond of it. He spent several days trying to find all occurrences of *t* in other strings he had. Finally he became tired and started thinking about the following problem. Given a string *s* how many ways are there to extract *k*<=β‰₯<=1 non-overlapping subst...
Input consists of two lines containing strings *s* and *t* (1<=≀<=|*s*|,<=|*t*|<=≀<=105). Each string consists of lowercase Latin letters.
Print the answer in a single line.
[ "ababa\naba\n", "welcometoroundtwohundredandeightytwo\nd\n", "ddd\nd\n" ]
[ "5\n", "274201\n", "12\n" ]
none
[ { "input": "ababa\naba", "output": "5" }, { "input": "welcometoroundtwohundredandeightytwo\nd", "output": "274201" }, { "input": "ddd\nd", "output": "12" }, { "input": "vnssnssnssnssnssnssnssnssnssnssnssnssnssnssnssnssn\nnssnssns", "output": "943392" }, { "input":...
2,000
28,876,800
0
66,125
580
Kefa and Dishes
[ "bitmasks", "dp" ]
null
null
When Kefa came to the restaurant and sat at a table, the waiter immediately brought him the menu. There were *n* dishes. Kefa knows that he needs exactly *m* dishes. But at that, he doesn't want to order the same dish twice to taste as many dishes as possible. Kefa knows that the *i*-th dish gives him *a**i* units of...
The first line of the input contains three space-separated numbers, *n*, *m* and *k* (1<=≀<=*m*<=≀<=*n*<=≀<=18, 0<=≀<=*k*<=≀<=*n*<=*<=(*n*<=-<=1)) β€” the number of dishes on the menu, the number of portions Kefa needs to eat to get full and the number of eating rules. The second line contains *n* space-separated number...
In the single line of the output print the maximum satisfaction that Kefa can get from going to the restaurant.
[ "2 2 1\n1 1\n2 1 1\n", "4 3 2\n1 2 3 4\n2 1 5\n3 4 2\n" ]
[ "3\n", "12\n" ]
In the first sample it is best to first eat the second dish, then the first one. Then we get one unit of satisfaction for each dish and plus one more for the rule. In the second test the fitting sequences of choice are 4 2 1 or 2 1 4. In both cases we get satisfaction 7 for dishes and also, if we fulfill rule 1, we ge...
[ { "input": "2 2 1\n1 1\n2 1 1", "output": "3" }, { "input": "4 3 2\n1 2 3 4\n2 1 5\n3 4 2", "output": "12" }, { "input": "1 1 0\n1000000000", "output": "1000000000" }, { "input": "4 2 1\n100 1 2 200\n2 3 400", "output": "403" }, { "input": "7 4 3\n2 6 13 5 7 1 9\n...
0
0
-1
66,191
0
none
[ "none" ]
null
null
In the year 2500 the annual graduation ceremony in the German University in Cairo (GUC) has run smoothly for almost 500 years so far. The most important part of the ceremony is related to the arrangement of the professors in the ceremonial hall. Traditionally GUC has *n* professors. Each professor has his seniority l...
The first line contains three integers *n*, *y* and *m* (1<=≀<=*n*<=≀<=16,<=2001<=≀<=*y*<=≀<=1018,<=0<=≀<=*m*<=≀<=100) β€” the number of professors, the year for which the arrangement should be computed, and the number of pairs of seats for which the seniority relation should be kept, respectively. The next *m* lines co...
Print the order in which the professors should be seated in the requested year. If by this year the GUC would have ran out of arrangements, or the given "senior-junior" relation are contradictory, print "The times have changed" (without quotes).
[ "3 2001 2\n1 2\n2 3\n", "7 2020 6\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n", "10 3630801 0\n", "3 2001 3\n1 2\n2 3\n3 1\n" ]
[ "1 2 3\n", "1 2 3 7 4 6 5\n", "The times have changed\n", "The times have changed\n" ]
In the first example the lexicographically first order of seating is 1 2 3. In the third example the GUC will run out of arrangements after the year 3630800. In the fourth example there are no valid arrangements for the seating. The lexicographical comparison of arrangements is performed by the &lt; operator in mode...
[]
92
0
0
66,200
9
Interestring graph and Apples
[ "dfs and similar", "dsu", "graphs" ]
E. Interesting Graph and Apples
1
64
Hexadecimal likes drawing. She has drawn many graphs already, both directed and not. Recently she has started to work on a still-life Β«interesting graph and applesΒ». An undirected graph is called interesting, if each of its vertices belongs to one cycle only β€” a funny ring β€” and does not belong to any other cycles. A f...
The first line of the input data contains a pair of integers *n* and *m* (1<=≀<=*n*<=≀<=50, 0<=≀<=*m*<=≀<=2500) β€” the amount of vertices and edges respectively. The following lines contain pairs of numbers *x**i* and *y**i* (1<=≀<=*x**i*, *y**i*<=≀<=*n*) β€” the vertices that are already connected by edges. The initial g...
In the first line output Β«YESΒ» or Β«NOΒ»: if it is possible or not to construct an interesting graph. If the answer is Β«YESΒ», in the second line output *k* β€” the amount of edges that should be added to the initial graph. Finally, output *k* lines: pairs of vertices *x**j* and *y**j*, between which edges should be drawn. ...
[ "3 2\n1 2\n2 3\n" ]
[ "YES\n1\n1 3\n" ]
none
[ { "input": "3 2\n1 2\n2 3", "output": "YES\n1\n1 3" }, { "input": "1 1\n1 1", "output": "YES\n0" }, { "input": "1 2\n1 1\n1 1", "output": "NO" }, { "input": "1 3\n1 1\n1 1\n1 1", "output": "NO" }, { "input": "2 0", "output": "YES\n2\n1 2\n1 2" }, { "in...
186
512,000
-1
66,256
0
none
[ "none" ]
null
null
На ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π½ΠΎΠΉ прямой сидит *n* собачСк, *i*-я собачка находится Π² Ρ‚ΠΎΡ‡ΠΊΠ΅ *x**i*. ΠšΡ€ΠΎΠΌΠ΅ Ρ‚ΠΎΠ³ΠΎ, Π½Π° прямой Π΅ΡΡ‚ΡŒ *m* мисок с Π΅Π΄ΠΎΠΉ, для ΠΊΠ°ΠΆΠ΄ΠΎΠΉ извСстна Π΅Ρ‘ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π° Π½Π° прямой *u**j* ΠΈ врСмя *t**j*, Ρ‡Π΅Ρ€Π΅Π· ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ Π΅Π΄Π° Π² мискС остынСт ΠΈ станСт нСвкусной. Π­Ρ‚ΠΎ Π·Π½Π°Ρ‡ΠΈΡ‚, Ρ‡Ρ‚ΠΎ Ссли собачка ΠΏΡ€ΠΈΠ±Π΅ΠΆΠΈΡ‚ ΠΊ мискС Π² ΠΌΠΎΠΌΠ΅Π½Ρ‚ Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ, строго Π±...
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΉ строкС находится ΠΏΠ°Ρ€Π° Ρ†Π΅Π»Ρ‹Ρ… чисСл *n* ΠΈ *m* (1<=≀<=*n*,<=*m*<=≀<=200<=000)Β β€” количСство собачСк ΠΈ мисок соотвСтствСнно. Π’ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠΉ строкС находятся *n* Ρ†Π΅Π»Ρ‹Ρ… чисСл *x**i* (<=-<=109<=≀<=*x**i*<=≀<=109)Β β€” ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π° *i*-ΠΉ собачки. Π’ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΡ… *m* строках находятся ΠΏΠ°Ρ€Ρ‹ Ρ†Π΅Π»Ρ‹Ρ… чисСл *u**j* ΠΈ *t**j* (<=-<=109<=≀<=...
Π’Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΎΠ΄Π½ΠΎ Ρ†Π΅Π»ΠΎΠ΅ число *a*Β β€” максимальноС количСство собачСк, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ смогут ΠΏΠΎΠΊΡƒΡˆΠ°Ρ‚ΡŒ.
[ "5 4\n-2 0 4 8 13\n-1 1\n4 3\n6 3\n11 2\n", "3 3\n-1 3 7\n1 1\n4 1\n7 1\n", "4 4\n20 1 10 30\n1 1\n2 5\n22 2\n40 10\n" ]
[ "4\n", "2\n", "3\n" ]
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΌ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π΅ пСрвая собачка ΠΏΠΎΠ±Π΅ΠΆΠΈΡ‚ Π½Π°ΠΏΡ€Π°Π²ΠΎ ΠΊ ΠΏΠ΅Ρ€Π²ΠΎΠΉ мискС, Ρ‚Ρ€Π΅Ρ‚ΡŒΡ собачка сразу Π½Π°Ρ‡Π½Ρ‘Ρ‚ Π΅ΡΡ‚ΡŒ ΠΈΠ· Π²Ρ‚ΠΎΡ€ΠΎΠΉ миски, чСтвёртая собачка ΠΏΠΎΠ±Π΅ΠΆΠΈΡ‚ Π²Π»Π΅Π²ΠΎ ΠΊ Ρ‚Ρ€Π΅Ρ‚ΡŒΠ΅ΠΉ мискС, Π° пятая собачка ΠΏΠΎΠ±Π΅ΠΆΠΈΡ‚ Π²Π»Π΅Π²ΠΎ ΠΊ Ρ‡Π΅Ρ‚Π²Ρ‘Ρ€Ρ‚ΠΎΠΉ мискС.
[]
0
0
-1
66,318
887
Ratings and Reality Shows
[ "data structures", "two pointers" ]
null
null
There are two main kinds of events in the life of top-model: fashion shows and photo shoots. Participating in any of these events affects the rating of appropriate top-model. After each photo shoot model's rating increases by *a* and after each fashion show decreases by *b* (designers do too many experiments nowadays)....
In first line there are 7 positive integers *n*, *a*, *b*, *c*, *d*, *start*, *len* (1<=≀<=*n*<=≀<=3Β·105, 0<=≀<=*start*<=≀<=109, 1<=≀<=*a*,<=*b*,<=*c*,<=*d*,<=*len*<=≀<=109), where *n* is a number of fashion shows and photo shoots, *a*, *b*, *c* and *d* are rating changes described above, *start* is an initial rating o...
Print one non-negative integer *t*Β β€” the moment of time in which talk show should happen to make Izabella's rating non-negative before talk show and during period of influence of talk show. If there are multiple answers print smallest of them. If there are no such moments, print <=-<=1.
[ "5 1 1 1 4 0 5\n1 1\n2 1\n3 1\n4 0\n5 0\n", "1 1 2 1 2 1 2\n1 0\n" ]
[ "6", "-1" ]
none
[ { "input": "5 1 1 1 4 0 5\n1 1\n2 1\n3 1\n4 0\n5 0", "output": "6" }, { "input": "1 1 2 1 2 1 2\n1 0", "output": "-1" }, { "input": "10 1 1 1 2 0 10\n1 1\n2 1\n3 0\n4 0\n5 1\n6 1\n7 1\n8 1\n9 1\n10 1", "output": "5" } ]
62
0
0
66,838
62
World Evil
[ "dp", "flows" ]
E. World Evil
5
256
As a result of Pinky and Brain's mysterious experiments in the Large Hadron Collider some portals or black holes opened to the parallel dimension. And the World Evil has crept to the veil between their world and ours. Brain quickly evaluated the situation and he understood that the more evil tentacles creep out and bec...
The first line of the input file contains two integers *n* and *m* (2<=≀<=*n*<=≀<=5, 2<=≀<=*m*<=≀<=105). They are the sizes of the Large Hadron Collider grid. The next *m*<=-<=1 lines contain *n* numbers each. They are the horizontal corridors' capacities. The next *m* lines contain *n* numbers each. They are the verti...
Print a single number, the number of the World Evil tentacles Pinky and Brain will command. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
[ "3 4\n4 4 4\n1 1 5\n5 5 3\n4 1 2\n1 3 1\n3 5 4\n1 4 3\n", "2 2\n9 2\n2 3\n6 1\n" ]
[ "7\n", "11\n" ]
none
[]
0
0
-1
66,875
0
none
[ "none" ]
null
null
You are given a set of *n* points on the plane. A line containing the origin is called good, if projection of the given set to this line forms a symmetric multiset of points. Find the total number of good lines. Multiset is a set where equal elements are allowed. Multiset is called symmetric, if there is a point *P* ...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=2000) β€” the number of points in the set. Each of the next *n* lines contains two integers *x**i* and *y**i* (<=-<=106<=<=≀<=<=*x**i*,<=<=*y**i*<=<=≀<=<=106) β€” the coordinates of the points. It is guaranteed that no two points coincide.
If there are infinitely many good lines, print -1. Otherwise, print single integerΒ β€” the number of good lines.
[ "3\n1 2\n2 1\n3 3\n", "2\n4 3\n1 2\n" ]
[ "3\n", "-1\n" ]
Picture to the first sample test: <img class="tex-graphics" src="https://espresso.codeforces.com/eedc60313be8684bd6169b8b23f0f0afd92479a8.png" style="max-width: 100.0%;max-height: 100.0%;"/> In the second sample, any line containing the origin is good.
[]
77
512,000
0
66,929
85
Embassy Queue
[ "data structures", "greedy" ]
B. Embassy Queue
2
256
In an embassy of a well-known kingdom an electronic queue is organised. Every person who comes to the embassy, needs to make the following three actions: show the ID, pay money to the cashier and be fingerprinted. Besides, the actions should be performed in the given order. For each action several separate windows are...
The first line contains three space-separated integers *k*1, *k*2, *k*3 (1<=≀<=*k**i*<=≀<=109), they are the number of windows of the first, second and third type correspondingly. The second line contains three space-separated integers *t*1, *t*2, *t*3 (1<=≀<=*t**i*<=≀<=105), they are the periods of time needed to ser...
Print the single number, the maximum time a person will spend in the embassy if the queue is organized optimally. Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams (also you may use the %I64d specificator).
[ "1 1 1\n1 1 1\n5\n1 1 1 1 1\n", "2 1 1\n5 1 1\n5\n1 2 3 3 5\n" ]
[ "7\n", "13\n" ]
In the first test 5 people come simultaneously at the moment of time equal to 1. There is one window of every type, it takes 1 unit of time to be served at each window. That's why the maximal time a person spends in the embassy is the time needed to be served at the windows (3 units of time) plus the time the last pers...
[ { "input": "1 1 1\n1 1 1\n5\n1 1 1 1 1", "output": "7" }, { "input": "2 1 1\n5 1 1\n5\n1 2 3 3 5", "output": "13" }, { "input": "1 1 1\n1 1 1\n5\n1 2 3 4 5", "output": "3" }, { "input": "1 1 1\n1 1 1\n5\n1 2 3 3 4", "output": "4" }, { "input": "9 5 6\n8 2 2\n1\n5"...
530
9,420,800
3.849952
67,097
731
Coupons and Discounts
[ "constructive algorithms", "greedy" ]
null
null
The programming competition season has already started and it's time to train for ICPC. Sereja coaches his teams for a number of year and he knows that to get ready for the training session it's not enough to prepare only problems and editorial. As the training sessions lasts for several hours, teams become hungry. Thu...
The first line of input contains a single integer *n* (1<=≀<=*n*<=≀<=200<=000)Β β€” the number of training sessions. The second line contains *n* integers *a*1, *a*2, ..., *a**n* (0<=≀<=*a**i*<=≀<=10<=000)Β β€” the number of teams that will be present on each of the days.
If there is a way to order pizzas using only coupons and discounts and do not buy any extra pizzas on any of the days, then print "YES" (without quotes) in the only line of output. Otherwise, print "NO" (without quotes).
[ "4\n1 2 1 2\n", "3\n1 0 1\n" ]
[ "YES\n", "NO\n" ]
In the first sample, Sereja can use one coupon to buy one pizza on the first and the second days, one coupon to buy pizza on the second and the third days and one discount to buy pizzas on the fourth days. This is the only way to order pizzas for this sample. In the second sample, Sereja can't use neither the coupon n...
[ { "input": "4\n1 2 1 2", "output": "YES" }, { "input": "3\n1 0 1", "output": "NO" }, { "input": "3\n1 3 1", "output": "NO" }, { "input": "3\n2 0 2", "output": "YES" }, { "input": "1\n179", "output": "NO" }, { "input": "10\n0 0 5 9 9 3 0 0 0 10", "o...
108
20,172,800
0
67,268
249
Donkey and Stars
[ "data structures", "dp", "geometry", "math", "sortings" ]
null
null
In the evenings Donkey would join Shrek to look at the stars. They would sit on a log, sipping tea and they would watch the starry sky. The sky hung above the roof, right behind the chimney. Shrek's stars were to the right of the chimney and the Donkey's stars were to the left. Most days the Donkey would just count the...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=105) β€” the number of stars. The second line contains simple fractions representing relationships "*a*/*b* *c*/*d*", such that and (0<=≀<=*a*,<=*b*,<=*c*,<=*d*<=≀<=105; ; ; ). The given numbers *a*, *b*, *c*, *d* are integers. Next *n* lines contain pairs of integ...
In a single line print number *m* β€” the answer to the problem.
[ "15\n1/3 2/1\n3 1\n6 2\n4 2\n2 5\n4 5\n6 6\n3 4\n1 6\n2 1\n7 4\n9 3\n5 3\n1 3\n15 5\n12 4\n" ]
[ "4\n" ]
In the sample the longest chain the Donkey can build consists of four stars. Note that the Donkey can't choose the stars that lie on the rays he imagines.
[ { "input": "15\n1/3 2/1\n3 1\n6 2\n4 2\n2 5\n4 5\n6 6\n3 4\n1 6\n2 1\n7 4\n9 3\n5 3\n1 3\n15 5\n12 4", "output": "4" }, { "input": "15\n2/1 2/0\n3 1\n6 2\n9 3\n12 4\n15 5\n2 1\n4 2\n5 3\n7 4\n1 3\n3 4\n2 5\n4 5\n1 6\n6 6", "output": "1" }, { "input": "15\n2/1 2/0\n3 1\n6 2\n9 3\n12 4\n15...
2,000
8,704,000
0
67,466
0
none
[ "none" ]
null
null
This is the modification of the problem used during the official round. Unfortunately, author's solution of the original problem appeared wrong, so the problem was changed specially for the archive. Once upon a time in a far away kingdom lived the King. The King had a beautiful daughter, Victoria. They lived happily, ...
The first line contains a single integer *n* from the problem's statement (1<=≀<=*n*<=≀<=10000).
Print the sought expected number of tosses as an irreducible fraction in the following form: "*a*/*b*" (without the quotes) without leading zeroes.
[ "2\n", "3\n", "4\n" ]
[ "1/1\n", "8/3\n", "2/1\n" ]
none
[ { "input": "2", "output": "1/1" }, { "input": "3", "output": "8/3" }, { "input": "4", "output": "2/1" }, { "input": "8", "output": "3/1" }, { "input": "7", "output": "24/7" }, { "input": "6", "output": "11/3" }, { "input": "1", "output"...
60
0
0
67,530
0
none
[ "none" ]
null
null
Radewoosh is playing a computer game. There are *n* levels, numbered 1 through *n*. Levels are divided into *k* regions (groups). Each region contains some positive number of consecutive levels. The game repeats the the following process: 1. If all regions are beaten then the game ends immediately. Otherwise, the sys...
The first line of the input contains two integers *n* and *k* (1<=≀<=*n*<=≀<=200<=000, 1<=≀<=*k*<=≀<=*min*(50,<=*n*))Β β€” the number of levels and the number of regions, respectively. The second line contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (1<=≀<=*t**i*<=≀<=100<=000).
Print one real numberΒ β€” the minimum possible expected value of the number of hours spent to finish the game if levels are distributed between regions in the optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=4. Namely: let's assume that your answer is *a*, and ...
[ "4 2\n100 3 5 7\n", "6 2\n1 2 4 8 16 32\n" ]
[ "5.7428571429\n", "8.5000000000\n" ]
In the first sample, we are supposed to split 4 levels into 2 regions. It's optimal to create the first region with only one level (it must be the first level). Then, the second region must contain other three levels. In the second sample, it's optimal to split levels into two regions with 3 levels each.
[]
3,000
10,035,200
0
67,594
679
Bear and Chase
[ "brute force", "dfs and similar", "graphs", "implementation", "math", "probabilities" ]
null
null
Bearland has *n* cities, numbered 1 through *n*. There are *m* bidirectional roads. The *i*-th road connects two distinct cities *a**i* and *b**i*. No two roads connect the same pair of cities. It's possible to get from any city to any other city (using one or more roads). The distance between cities *a* and *b* is de...
The first line of the input contains two integers *n* and *m* (2<=≀<=*n*<=≀<=400, )Β β€” the number of cities and the number of roads, respectively. Then, *m* lines follow. The *i*-th of them contains two integers *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*, *a**i*<=β‰ <=*b**i*)Β β€” cities connected by the *i*-th road. ...
Print one real numberΒ β€” the probability of finding Limak, if you behave optimally. Your answer will be considered correct if its absolute error does not exceed 10<=-<=6. Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct if |*a*<=-<=*b...
[ "3 3\n1 2\n1 3\n2 3\n", "5 4\n1 2\n3 1\n5 1\n1 4\n", "4 4\n1 2\n1 3\n2 3\n1 4\n", "5 5\n1 2\n2 3\n3 4\n4 5\n1 5\n" ]
[ "0.833333333333\n", "1.000000000000\n", "0.916666666667\n", "0.900000000000\n" ]
In the first sample test, there are three cities and there is a road between every pair of cities. Let's analyze one of optimal scenarios. 1. Use BCD in city 1. <li> With probability <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/64c94d13eeb330b494061e86538db66574ad0f7d.png" style="max-...
[]
30
0
0
67,625
980
Perfect Groups
[ "dp", "math", "number theory" ]
null
null
SaMer has written the greatest test case of all time for one of his problems. For a given array of integers, the problem asks to find the minimum number of groups the array can be divided into, such that the product of any pair of integers in the same group is a perfect square. Each integer must be in exactly one gro...
The first line of input contains a single integer $n$ ($1 \leq n \leq 5000$), the size of the array. The second line contains $n$ integers $a_1$,$a_2$,$\dots$,$a_n$ ($-10^8 \leq a_i \leq 10^8$), the values of the array.
Output $n$ space-separated integers, the $k$-th integer should be the number of contiguous subarrays of $A$ that have an answer to the problem equal to $k$.
[ "2\n5 5\n", "5\n5 -4 2 1 8\n", "1\n0\n" ]
[ "3 0\n", "5 5 3 2 0\n", "1\n" ]
none
[ { "input": "2\n5 5", "output": "3 0" }, { "input": "5\n5 -4 2 1 8", "output": "5 5 3 2 0" }, { "input": "1\n0", "output": "1" }, { "input": "3\n-10 -5 7", "output": "3 2 1" }, { "input": "5\n-6 -7 -2 -3 -10", "output": "5 4 3 2 1" }, { "input": "8\n-5 ...
1,000
31,129,600
0
67,777
575
Fibonotci
[ "data structures", "math", "matrices" ]
null
null
Fibonotci sequence is an integer recursive sequence defined by the recurrence relation Sequence *s* is an infinite and almost cyclic sequence with a cycle of length *N*. A sequence *s* is called almost cyclic with a cycle of length *N* if , for *i*<=β‰₯<=*N*, except for a finite number of values *s**i*, for which (*i*...
The first line contains two numbers *K* and *P*. The second line contains a single number *N*. The third line contains *N* numbers separated by spaces, that represent the first *N* numbers of the sequence *s*. The fourth line contains a single number *M*, the number of values of sequence *s* for which . Each of the fol...
Output should contain a single integer equal to .
[ "10 8\n3\n1 2 1\n2\n7 3\n5 4\n" ]
[ "4\n" ]
none
[]
2,000
17,715,200
0
67,854
660
Bear and Bowling 4
[ "binary search", "data structures", "divide and conquer", "geometry", "ternary search" ]
null
null
Limak is an old brown bear. He often goes bowling with his friends. Today he feels really good and tries to beat his own record! For rolling a ball one gets a score β€” an integer (maybe negative) number of points. Score for the *i*-th roll is multiplied by *i* and scores are summed up. So, for *k* rolls with scores *s*...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=2Β·105) β€” the total number of rolls made by Limak. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (|*a**i*|<=≀<=107) β€” scores for Limak's rolls.
Print the maximum possible total score after cancelling rolls.
[ "6\n5 -1000 1 -3 7 -8\n", "5\n1000 1000 1001 1000 1000\n", "3\n-60 -70 -80\n" ]
[ "16\n", "15003\n", "0\n" ]
In the first sample test, Limak should cancel the first two rolls, and one last roll. He will be left with rolls 1,  - 3, 7 what gives him the total score 1Β·1 + 2Β·( - 3) + 3Β·7 = 1 - 6 + 21 = 16.
[ { "input": "6\n5 -1000 1 -3 7 -8", "output": "16" }, { "input": "5\n1000 1000 1001 1000 1000", "output": "15003" }, { "input": "3\n-60 -70 -80", "output": "0" }, { "input": "1\n-4", "output": "0" }, { "input": "2\n-3 6", "output": "9" }, { "input": "3\...
46
0
0
67,952
848
Days of Floral Colours
[ "combinatorics", "divide and conquer", "dp", "fft", "math" ]
null
null
The Floral Clock has been standing by the side of Mirror Lake for years. Though unable to keep time, it reminds people of the passage of time and the good old days. On the rim of the Floral Clock are 2*n* flowers, numbered from 1 to 2*n* clockwise, each of which has a colour among all *n* possible ones. For each colou...
The first and only line of input contains a lonely positive integer *n* (3<=≀<=*n*<=≀<=50<=000)Β β€” the number of colours present on the Floral Clock.
Output one integer β€” the sum of beauty over all possible arrangements of flowers, modulo 998<=244<=353.
[ "3\n", "4\n", "7\n", "15\n" ]
[ "24\n", "4\n", "1316\n", "3436404\n" ]
With *n* = 3, the following six arrangements each have a beauty of 2 × 2 = 4. While many others, such as the left one in the figure below, have a beauty of 0. The right one is invalid, since it's asymmetric.
[ { "input": "3", "output": "24" }, { "input": "4", "output": "4" }, { "input": "7", "output": "1316" }, { "input": "15", "output": "3436404" }, { "input": "10", "output": "26200" }, { "input": "99", "output": "620067986" }, { "input": "1317"...
46
0
0
68,015
733
Epidemic in Monstropolis
[ "constructive algorithms", "dp", "greedy", "two pointers" ]
null
null
There was an epidemic in Monstropolis and all monsters became sick. To recover, all monsters lined up in queue for an appointment to the only doctor in the city. Soon, monsters became hungry and began to eat each other. One monster can eat other monster if its weight is strictly greater than the weight of the monste...
The first line contains single integer *n* (1<=≀<=*n*<=≀<=500)Β β€” the number of monsters in the initial queue. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=106)Β β€” the initial weights of the monsters. The third line contains single integer *k* (1<=≀<=*k*<=≀<=*n*)Β β€” the number of mo...
In case if no actions could lead to the final queue, print "NO" (without quotes) in the only line. Otherwise print "YES" (without quotes) in the first line. In the next *n*<=-<=*k* lines print actions in the chronological order. In each line print *x*Β β€” the index number of the monster in the current queue which eats ...
[ "6\n1 2 2 2 1 2\n2\n5 5\n", "5\n1 2 3 4 5\n1\n15\n", "5\n1 1 1 3 3\n3\n2 1 6\n" ]
[ "YES\n2 L\n1 R\n4 L\n3 L\n", "YES\n5 L\n4 L\n3 L\n2 L\n", "NO" ]
In the first example, initially there were *n* = 6 monsters, their weights are [1, 2, 2, 2, 1, 2] (in order of queue from the first monster to the last monster). The final queue should be [5, 5]. The following sequence of eatings leads to the final queue: - the second monster eats the monster to the left (i.e. the fi...
[ { "input": "6\n1 2 2 2 1 2\n2\n5 5", "output": "YES\n2 L\n1 R\n4 L\n3 L" }, { "input": "5\n1 2 3 4 5\n1\n15", "output": "YES\n5 L\n4 L\n3 L\n2 L" }, { "input": "5\n1 1 1 3 3\n3\n2 1 6", "output": "NO" }, { "input": "5\n1 1 1 1 2\n3\n1 1 4", "output": "YES\n5 L\n4 L" }, ...
0
0
-1
68,253
553
Nudist Beach
[ "binary search", "graphs", "greedy" ]
null
null
Nudist Beach is planning a military operation to attack the Life Fibers. In this operation, they will attack and capture several cities which are currently under the control of the Life Fibers. There are *n* cities, labeled from 1 to *n*, and *m* bidirectional roads between them. Currently, there are Life Fibers in ev...
The first line of input contains three integers *n*,<=*m*,<=*k* (2<=<=≀<=<=*n*<=<=≀<=100<=000, 1<=≀<=*m*<=≀<=100<=000, 1<=≀<=*k*<=≀<=*n*<=-<=1). The second line of input contains *k* integers, representing the cities with fortresses. These cities will all be distinct. The next *m* lines contain the roads. The *i*-th...
The first line should contain an integer *r*, denoting the size of an optimum set (1<=≀<=*r*<=≀<=*n*<=-<=*k*). The second line should contain *r* integers, denoting the cities in the set. Cities may follow in an arbitrary order. This line should not contain any of the cities with fortresses. If there are multiple po...
[ "9 8 4\n3 9 6 8\n1 2\n1 3\n1 4\n1 5\n2 6\n2 7\n2 8\n2 9\n", "10 8 2\n2 9\n1 3\n2 9\n4 5\n5 6\n6 7\n7 8\n8 10\n10 4\n" ]
[ "3\n1 4 5\n", "8\n1 5 4 8 10 6 3 7\n" ]
The first example case achieves a strength of 1/2. No other subset is strictly better. The second example case achieves a strength of 1. Note that the subset doesn't necessarily have to be connected.
[ { "input": "9 8 4\n3 9 6 8\n1 2\n1 3\n1 4\n1 5\n2 6\n2 7\n2 8\n2 9", "output": "3\n5 1 4" }, { "input": "10 8 2\n2 9\n1 3\n2 9\n4 5\n5 6\n6 7\n7 8\n8 10\n10 4", "output": "8\n3 10 6 1 5 8 7 4" }, { "input": "2 1 1\n1\n2 1", "output": "1\n2" } ]
452
30,617,600
-1
68,355
804
Expected diameter of a tree
[ "binary search", "brute force", "dfs and similar", "dp", "sortings", "trees" ]
null
null
Pasha is a good student and one of MoJaK's best friends. He always have a problem to think about. Today they had a talk about the following problem. We have a forest (acyclic undirected graph) with *n* vertices and *m* edges. There are *q* queries we should answer. In each query two vertices *v* and *u* are given. Let...
The first line contains three integers *n*, *m* and *q*(1<=≀<=*n*,<=*m*,<=*q*<=≀<=105)Β β€” the number of vertices, the number of edges in the graph and the number of queries. Each of the next *m* lines contains two integers *u**i* and *v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*), that means there is an edge between vertices *...
For each query print the expected value of *d* as described in the problem statement. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. Let's assume that your answer is *a*, and the jury's answer is *b*. The checker program will consider your answer correct, if .
[ "3 1 2\n1 3\n3 1\n2 3\n", "5 2 3\n2 4\n4 3\n4 2\n4 1\n2 5\n" ]
[ "-1\n2.0000000000\n", "-1\n2.6666666667\n2.6666666667\n" ]
In the first example the vertices 1 and 3 are in the same component, so the answer for the first query is -1. For the second query there are two options to add the edge: one option is to add the edge 1 - 2, the other one is 2 - 3. In both ways the resulting diameter is 2, so the answer is 2. In the second example the ...
[ { "input": "3 1 2\n1 3\n3 1\n2 3", "output": "-1\n2.0000000000" }, { "input": "5 2 3\n2 4\n4 3\n4 2\n4 1\n2 5", "output": "-1\n2.6666666667\n2.6666666667" }, { "input": "17 15 13\n3 15\n3 1\n15 9\n16 6\n1 5\n1 8\n16 12\n15 7\n9 4\n6 11\n15 14\n9 10\n15 13\n1 17\n11 2\n7 3\n9 6\n9 7\n1 8\...
3,000
22,323,200
0
68,586
209
Pixels
[ "constructive algorithms", "math" ]
null
null
Flatland is inhabited by pixels of three colors: red, green and blue. We know that if two pixels of different colors meet in a violent fight, only one of them survives the fight (that is, the total number of pixels decreases by one). Besides, if pixels of colors *x* and *y* (*x*<=β‰ <=*y*) meet in a violent fight, then t...
The first line contains three space-separated integers *a*, *b* and *c* (0<=≀<=*a*,<=*b*,<=*c*<=≀<=231;Β *a*<=+<=*b*<=+<=*c*<=&gt;<=0) β€” the number of red, green and blue pixels, correspondingly.
Print a single number β€” the minimum number of pixel fights before the country becomes peaceful and prosperous. If making the country peaceful and prosperous is impossible, print -1.
[ "1 1 1\n", "3 1 0\n" ]
[ "1\n", "3\n" ]
In the first test sample the country needs only one fight to achieve peace and prosperity. Besides, it can be any fight whatsoever. For example, let's assume that the green and the blue pixels fight, then the surviving pixel will be red. As a result, after the fight there are two red pixels. There won't be other pixels...
[ { "input": "1 1 1", "output": "1" }, { "input": "3 1 0", "output": "3" }, { "input": "1 4 4", "output": "4" }, { "input": "5 10 6", "output": "10" }, { "input": "6 8 10", "output": "8" }, { "input": "1 10 2", "output": "10" }, { "input": "1...
248
0
3
68,597
715
Digit Tree
[ "dfs and similar", "divide and conquer", "dsu", "trees" ]
null
null
ZS the Coder has a large tree. It can be represented as an undirected connected graph of *n* vertices numbered from 0 to *n*<=-<=1 and *n*<=-<=1 edges between them. There is a single nonzero digit written on each edge. One day, ZS the Coder was bored and decided to investigate some properties of the tree. He chose a p...
The first line of the input contains two integers, *n* and *M* (2<=≀<=*n*<=≀<=100<=000,<=1<=≀<=*M*<=≀<=109, )Β β€” the number of vertices and the number ZS has chosen respectively. The next *n*<=-<=1 lines contain three integers each. *i*-th of them contains *u**i*,<=*v**i* and *w**i*, denoting an edge between vertices *...
Print a single integerΒ β€” the number of interesting (by ZS the Coder's consideration) pairs.
[ "6 7\n0 1 2\n4 2 4\n2 0 1\n3 0 9\n2 5 7\n", "5 11\n1 2 3\n2 0 3\n3 0 3\n4 3 3\n" ]
[ "7\n", "8\n" ]
In the first sample case, the interesting pairs are (0, 4), (1, 2), (1, 5), (3, 2), (2, 5), (5, 2), (3, 5). The numbers that are formed by these pairs are 14, 21, 217, 91, 7, 7, 917 respectively, which are all multiples of 7. Note that (2, 5) and (5, 2) are considered different. In the second sample case, the interes...
[ { "input": "6 7\n0 1 2\n4 2 4\n2 0 1\n3 0 9\n2 5 7", "output": "7" }, { "input": "5 11\n1 2 3\n2 0 3\n3 0 3\n4 3 3", "output": "8" }, { "input": "4 3\n0 1 4\n1 2 4\n2 3 4", "output": "2" }, { "input": "2 7\n1 0 9", "output": "0" }, { "input": "2 7\n1 0 7", "ou...
0
0
-1
68,701
358
Dima and Hares
[ "dp", "greedy" ]
null
null
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more. Dima felt so grateful to Inna about the present that he decided to buy her *n* hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to *n* from left to right and started feeding them wi...
The first line of the input contains integer *n* (1<=≀<=*n*<=≀<=3000) β€” the number of hares. Then three lines follow, each line has *n* integers. The first line contains integers *a*1 *a*2 ... *a**n*. The second line contains *b*1,<=*b*2,<=...,<=*b**n*. The third line contains *c*1,<=*c*2,<=...,<=*c**n*. The following ...
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
[ "4\n1 2 3 4\n4 3 2 1\n0 1 1 0\n", "7\n8 5 7 6 1 8 9\n2 7 9 5 4 3 1\n2 3 3 4 1 1 3\n", "3\n1 1 1\n1 2 1\n1 1 1\n" ]
[ "13\n", "44\n", "4\n" ]
none
[ { "input": "4\n1 2 3 4\n4 3 2 1\n0 1 1 0", "output": "13" }, { "input": "7\n8 5 7 6 1 8 9\n2 7 9 5 4 3 1\n2 3 3 4 1 1 3", "output": "44" }, { "input": "3\n1 1 1\n1 2 1\n1 1 1", "output": "4" }, { "input": "7\n1 3 8 9 3 4 4\n6 0 6 6 1 8 4\n9 6 3 7 8 8 2", "output": "42" ...
170
2,252,800
3
68,756
250
Mad Joe
[ "brute force" ]
null
null
Joe has been hurt on the Internet. Now he is storming around the house, destroying everything in his path. Joe's house has *n* floors, each floor is a segment of *m* cells. Each cell either contains nothing (it is an empty cell), or has a brick or a concrete wall (always something one of three). It is believed that ea...
The first line contains two integers *n* and *m* (2<=≀<=*n*<=≀<=100, 1<=≀<=*m*<=≀<=104). Next *n* lines contain the description of Joe's house. The *i*-th of these lines contains the description of the (*n*<=-<=*i*<=+<=1)-th floor of the house β€” a line that consists of *m* characters: "." means an empty cell, "+" mean...
Print a single number β€” the number of seconds Joe needs to reach the first floor; or else, print word "Never" (without the quotes), if it can never happen. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
[ "3 5\n..+.#\n#+..+\n+.#+.\n", "4 10\n...+.##+.+\n+#++..+++#\n++.#++++..\n.+##.++#.+\n", "2 2\n..\n++\n" ]
[ "14", "42\n", "Never" ]
none
[ { "input": "3 5\n..+.#\n#+..+\n+.#+.", "output": "14" }, { "input": "4 10\n...+.##+.+\n+#++..+++#\n++.#++++..\n.+##.++#.+", "output": "42" }, { "input": "2 2\n..\n++", "output": "Never" }, { "input": "5 1\n.\n.\n.\n.\n.", "output": "4" }, { "input": "20 20\n..+#+....
46
0
0
68,770
550
Regular Bridge
[ "constructive algorithms", "graphs", "implementation" ]
null
null
An undirected graph is called *k*-regular, if the degrees of all its vertices are equal *k*. An edge of a connected graph is called a bridge, if after removing it the graph is being split into two connected components. Build a connected undirected *k*-regular graph containing at least one bridge, or else state that su...
The single line of the input contains integer *k* (1<=≀<=*k*<=≀<=100) β€” the required degree of the vertices of the regular graph.
Print "NO" (without quotes), if such graph doesn't exist. Otherwise, print "YES" in the first line and the description of any suitable graph in the next lines. The description of the made graph must start with numbers *n* and *m* β€” the number of vertices and edges respectively. Each of the next *m* lines must cont...
[ "1\n" ]
[ "YES\n2 1\n1 2\n" ]
In the sample from the statement there is a suitable graph consisting of two vertices, connected by a single edge.
[ { "input": "1", "output": "YES\n2 1\n1 2" }, { "input": "3", "output": "YES\n10 15\n1 6\n1 2\n1 3\n2 4\n2 5\n3 4\n3 5\n4 5\n6 7\n6 8\n7 9\n7 10\n8 9\n8 10\n9 10" }, { "input": "11", "output": "YES\n26 143\n1 14\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n2 12\n2 13\n2 4\n2 5...
62
102,400
3
68,832
158
Ice Sculptures
[ "*special", "brute force", "number theory" ]
null
null
The Berland University is preparing to celebrate the 256-th anniversary of its founding! A specially appointed Vice Rector for the celebration prepares to decorate the campus. In the center of the campus *n* ice sculptures were erected. The sculptures are arranged in a circle at equal distances from each other, so they...
The first input line contains an integer *n* (3<=≀<=*n*<=≀<=20000) β€” the initial number of sculptures. The second line contains a sequence of integers *t*1,<=*t*2,<=...,<=*t**n*, *t**i* β€” the degree of the *i*-th sculpture's attractiveness (<=-<=1000<=≀<=*t**i*<=≀<=1000). The numbers on the line are separated by spaces...
Print the required maximum sum of the sculptures' attractiveness.
[ "8\n1 2 -3 4 -5 5 2 3\n", "6\n1 -2 3 -4 5 -6\n", "6\n1 2 3 4 5 6\n" ]
[ "14\n", "9\n", "21\n" ]
In the first sample it is best to leave every second sculpture, that is, leave sculptures with attractivenesses: 2, 4, 5 ΠΈ 3.
[ { "input": "8\n1 2 -3 4 -5 5 2 3", "output": "14" }, { "input": "6\n1 -2 3 -4 5 -6", "output": "9" }, { "input": "6\n1 2 3 4 5 6", "output": "21" }, { "input": "4\n1 -10 1 -10", "output": "-18" }, { "input": "6\n1 0 1 0 1 0", "output": "3" }, { "input"...
216
4,403,200
3
68,893
757
Can Bash Save the Day?
[ "data structures", "divide and conquer", "graphs", "trees" ]
null
null
Whoa! You did a great job helping Team Rocket who managed to capture all the Pokemons sent by Bash. Meowth, part of Team Rocket, having already mastered the human language, now wants to become a master in programming as well. He agrees to free the Pokemons if Bash can answer his questions. Initially, Meowth gives Bash...
The first line contains two integers *n* and *q* (1<=≀<=*n*<=≀<=2Β·105, 1<=≀<=*q*<=≀<=2Β·105)Β β€” the number of nodes in the tree and the number of queries, respectively. The next line contains *n* space-separated integersΒ β€” the sequence *a*1,<=*a*2,<=...,<=*a**n* which is a permutation of 1,<=2,<=...,<=*n*. Each of the ...
For each query of type 1, output a single integer in a separate line, denoting the answer to the query.
[ "5 5\n4 5 1 3 2\n4 2 4\n1 3 9\n4 1 4\n4 5 2\n1\n1 5 4\n1\n22 20 20\n2\n38\n2\n39\n1\n36 38 38\n" ]
[ "23\n37\n28\n" ]
In the sample, the actual queries are the following: - 1 1 5 4 - 1 1 3 3 - 2 3 - 2 2 - 1 1 3 3
[]
46
0
0
69,018
604
Uncowed Forces
[ "implementation" ]
null
null
Kevin Sun has just finished competing in Codeforces Round #334! The round was 120 minutes long and featured five problems with maximum point values of 500, 1000, 1500, 2000, and 2500, respectively. Despite the challenging tasks, Kevin was uncowed and bulldozed through all of them, distinguishing himself from the herd a...
The first line of the input contains five space-separated integers *m*1, *m*2, *m*3, *m*4, *m*5, where *m**i* (0<=≀<=*m**i*<=≀<=119) is the time of Kevin's last submission for problem *i*. His last submission is always correct and gets accepted. The second line contains five space-separated integers *w*1, *w*2, *w*3, ...
Print a single integer, the value of Kevin's final score.
[ "20 40 60 80 100\n0 1 2 3 4\n1 0\n", "119 119 119 119 119\n0 0 0 0 0\n10 0\n" ]
[ "4900\n", "4930\n" ]
In the second sample, Kevin takes 119 minutes on all of the problems. Therefore, he gets <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/42158dc2bc78cd21fa679530ae9ef8b9ea298d15.png" style="max-width: 100.0%;max-height: 100.0%;"/> of the points on each problem. So his score from solving pro...
[ { "input": "20 40 60 80 100\n0 1 2 3 4\n1 0", "output": "4900" }, { "input": "119 119 119 119 119\n0 0 0 0 0\n10 0", "output": "4930" }, { "input": "3 6 13 38 60\n6 10 10 3 8\n9 9", "output": "5088" }, { "input": "21 44 11 68 75\n6 2 4 8 4\n2 8", "output": "4522" }, {...
77
6,963,200
3
0
281
Word Capitalization
[ "implementation", "strings" ]
null
null
Capitalization is writing a word with its first letter as a capital letter. Your task is to capitalize the given word. Note, that during capitalization all the letters except the first one remains unchanged.
A single line contains a non-empty word. This word consists of lowercase and uppercase English letters. The length of the word will not exceed 103.
Output the given word after capitalization.
[ "ApPLe\n", "konjac\n" ]
[ "ApPLe\n", "Konjac\n" ]
none
[ { "input": "ApPLe", "output": "ApPLe" }, { "input": "konjac", "output": "Konjac" }, { "input": "a", "output": "A" }, { "input": "A", "output": "A" }, { "input": "z", "output": "Z" }, { "input": "ABACABA", "output": "ABACABA" }, { "input": "...
92
0
3
1
322
Ciel and Flowers
[ "combinatorics", "math" ]
null
null
Fox Ciel has some flowers: *r* red flowers, *g* green flowers and *b* blue flowers. She wants to use these flowers to make several bouquets. There are 4 types of bouquets: - To make a "red bouquet", it needs 3 red flowers. - To make a "green bouquet", it needs 3 green flowers. - To make a "blue bouquet", it needs 3...
The first line contains three integers *r*, *g* and *b* (0<=≀<=*r*,<=*g*,<=*b*<=≀<=109) β€” the number of red, green and blue flowers.
Print the maximal number of bouquets Fox Ciel can make.
[ "3 6 9\n", "4 4 4\n", "0 0 0\n" ]
[ "6\n", "4\n", "0\n" ]
In test case 1, we can make 1 red bouquet, 2 green bouquets and 3 blue bouquets. In test case 2, we can make 1 red, 1 green, 1 blue and 1 mixing bouquet.
[ { "input": "3 6 9", "output": "6" }, { "input": "4 4 4", "output": "4" }, { "input": "0 0 0", "output": "0" }, { "input": "0 3 6", "output": "3" }, { "input": "7 8 9", "output": "7" }, { "input": "8 8 9", "output": "8" }, { "input": "15 3 9...
78
102,400
3
2
298
Snow Footprints
[ "greedy", "implementation" ]
null
null
There is a straight snowy road, divided into *n* blocks. The blocks are numbered from 1 to *n* from left to right. If one moves from the *i*-th block to the (*i*<=+<=1)-th block, he will leave a right footprint on the *i*-th block. Similarly, if one moves from the *i*-th block to the (*i*<=-<=1)-th block, he will leave...
The first line of the input contains integer *n* (3<=≀<=*n*<=≀<=1000). The second line contains the description of the road β€” the string that consists of *n* characters. Each character will be either "." (a block without footprint), or "L" (a block with a left footprint), "R" (a block with a right footprint). It's gu...
Print two space-separated integers β€” the values of *s* and *t*. If there are several possible solutions you can print any of them.
[ "9\n..RRLL...\n", "11\n.RRRLLLLL..\n" ]
[ "3 4\n", "7 5\n" ]
The first test sample is the one in the picture.
[ { "input": "11\n.RRRLLLLL..", "output": "7 5" }, { "input": "4\n.RL.", "output": "3 2" }, { "input": "3\n.L.", "output": "2 1" }, { "input": "3\n.R.", "output": "2 3" } ]
0
0
-1
3
804
Minimum number of steps
[ "combinatorics", "greedy", "implementation", "math" ]
null
null
We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings "ab" in the string and replace it with the string "bba". If we have no "ab" as a substring, our job is done. Print the minimum number of steps we should perform to make our job done modulo 109<=+<...
The first line contains the initial string consisting of letters 'a' and 'b' only with length from 1 to 106.
Print the minimum number of steps modulo 109<=+<=7.
[ "ab\n", "aab\n" ]
[ "1\n", "3\n" ]
The first example: "ab"  →  "bba". The second example: "aab"  →  "abba"  →  "bbaba"  →  "bbbbaa".
[ { "input": "ab", "output": "1" }, { "input": "aab", "output": "3" }, { "input": "aaaaabaabababaaaaaba", "output": "17307" }, { "input": "abaabaaabbabaabab", "output": "1795" }, { "input": "abbaa", "output": "2" }, { "input": "abbaaabaabaaaaabbbbaababaa...
218
6,348,800
3
4