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
0
none
[ "none" ]
null
null
A subsequence of length |*x*| of string *s*<==<=*s*1*s*2... *s*|*s*| (where |*s*| is the length of string *s*) is a string *x*<==<=*s**k*1*s**k*2... *s**k*|*x*| (1<=≀<=*k*1<=&lt;<=*k*2<=&lt;<=...<=&lt;<=*k*|*x*|<=≀<=|*s*|). You've got two stringsΒ β€” *s* and *t*. Let's consider all subsequences of string *s*, coinciding...
The first line contains string *s*, the second line contains string *t*. Each line consists only of lowercase English letters. The given strings are non-empty, the length of each string does not exceed 2Β·105.
Print "Yes" (without the quotes), if each character of the string *s* occurs in at least one of the described subsequences, or "No" (without the quotes) otherwise.
[ "abab\nab\n", "abacaba\naba\n", "abc\nba\n" ]
[ "Yes\n", "No\n", "No\n" ]
In the first sample string *t* can occur in the string *s* as a subsequence in three ways: abab, abab and abab. In these occurrences each character of string *s* occurs at least once. In the second sample the 4-th character of the string *s* doesn't occur in any occurrence of string *t*. In the third sample there is ...
[ { "input": "abab\nab", "output": "Yes" }, { "input": "abacaba\naba", "output": "No" }, { "input": "abc\nba", "output": "No" }, { "input": "babbbbbaba\nab", "output": "No" }, { "input": "accbacabaa\nbada", "output": "No" }, { "input": "aaaaaaaaaaaaaaaaa...
30
0
0
52,058
594
Max and Bike
[ "binary search", "geometry" ]
null
null
For months Maxim has been coming to work on his favorite bicycle. And quite recently he decided that he is ready to take part in a cyclists' competitions. He knows that this year *n* competitions will take place. During the *i*-th competition the participant must as quickly as possible complete a ride along a straight...
The first line contains three integers *n*, *r* and *v* (1<=≀<=*n*<=≀<=100<=000,<=1<=≀<=*r*,<=*v*<=≀<=109)Β β€” the number of competitions, the radius of the front wheel of Max's bike and his maximum speed, respectively. Next *n* lines contain the descriptions of the contests. The *i*-th line contains two integers *s**i...
Print *n* real numbers, the *i*-th number should be equal to the minimum possible time measured by the time counter. Your answer will be considered correct if its absolute or relative error will not exceed 10<=-<=6. Namely: let's assume that your answer equals *a*, and the answer of the jury is *b*. The checker progr...
[ "2 1 2\n1 10\n5 9\n" ]
[ "3.849644710502\n1.106060157705\n" ]
none
[ { "input": "2 1 2\n1 10\n5 9", "output": "3.849644710502\n1.106060157705" }, { "input": "1 9 2\n8 11", "output": "0.750434631667" }, { "input": "5 9 2\n15 18\n13 15\n9 16\n5 21\n10 11", "output": "0.750434631667\n0.500128680269\n1.755555861769\n4.068584337764\n0.250016077584" }, ...
2,000
16,486,400
0
52,113
69
Bets
[ "greedy", "implementation" ]
B. Bets
2
256
In Chelyabinsk lives a much respected businessman Nikita with a strange nickname "Boss". Once Nikita decided to go with his friend Alex to the Summer Biathlon World Cup. Nikita, as a very important person, received a token which allows to place bets on each section no more than on one competitor. To begin with friends...
The first line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=100). Then follow *m* lines, each containing 4 integers *l**i*, *r**i*, *t**i*, *c**i* (1<=≀<=*l**i*<=≀<=*r**i*<=≀<=*n*, 1<=≀<=*t**i*,<=*c**i*<=≀<=1000).
Print a single integer, the maximal profit in roubles that the friends can get. In each of *n* sections it is not allowed to place bets on more than one sportsman.
[ "4 4\n1 4 20 5\n1 3 21 10\n3 3 4 30\n3 4 4 20\n", "8 4\n1 5 24 10\n2 4 6 15\n4 6 30 50\n6 7 4 20\n" ]
[ "60", "105" ]
In the first test the optimal bet is: in the 1-2 sections on biathlete 1, in section 3 on biathlete 3, in section 4 on biathlete 4. Total: profit of 5 rubles for 1 section, the profit of 5 rubles for 2 section, profit of 30 rubles for a 3 section, profit of 20 rubles for 4 section. Total profit 60 rubles. In the secon...
[ { "input": "4 4\n1 4 20 5\n1 3 21 10\n3 3 4 30\n3 4 4 20", "output": "60" }, { "input": "8 4\n1 5 24 10\n2 4 6 15\n4 6 30 50\n6 7 4 20", "output": "105" }, { "input": "2 2\n1 2 3 1\n2 2 3 10", "output": "2" }, { "input": "20 30\n15 17 54 46\n4 18 26 18\n18 20 49 94\n12 12 83 ...
154
5,632,000
3.95101
52,168
103
Russian Roulette
[ "constructive algorithms", "greedy" ]
C. Russian Roulette
2
256
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", ...
186
307,200
0
52,289
723
Lakes in Berland
[ "dfs and similar", "dsu", "graphs", "greedy", "implementation" ]
null
null
The map of Berland is a rectangle of the size *n*<=Γ—<=*m*, which consists of cells of size 1<=Γ—<=1. Each cell is either land or water. The map is surrounded by the ocean. Lakes are the maximal regions of water cells, connected by sides, which are not connected with the ocean. Formally, lake is a set of water cells, s...
The first line of the input contains three integers *n*, *m* and *k* (1<=≀<=*n*,<=*m*<=≀<=50, 0<=≀<=*k*<=≀<=50)Β β€” the sizes of the map and the number of lakes which should be left on the map. The next *n* lines contain *m* characters each β€” the description of the map. Each of the characters is either '.' (it means tha...
In the first line print the minimum number of cells which should be transformed from water to land. In the next *n* lines print *m* symbols β€” the map after the changes. The format must strictly follow the format of the map in the input data (there is no need to print the size of the map). If there are several answers...
[ "5 4 1\n****\n*..*\n****\n**.*\n..**\n", "3 3 0\n***\n*.*\n***\n" ]
[ "1\n****\n*..*\n****\n****\n..**\n", "1\n***\n***\n***\n" ]
In the first example there are only two lakes β€” the first consists of the cells (2, 2) and (2, 3), the second consists of the cell (4, 3). It is profitable to cover the second lake because it is smaller. Pay attention that the area of water in the lower left corner is not a lake because this area share a border with th...
[ { "input": "5 4 1\n****\n*..*\n****\n**.*\n..**", "output": "1\n****\n*..*\n****\n****\n..**" }, { "input": "3 3 0\n***\n*.*\n***", "output": "1\n***\n***\n***" }, { "input": "3 5 1\n.**.*\n*.*.*\n***..", "output": "0\n.**.*\n*.*.*\n***.." }, { "input": "3 5 0\n.**.*\n*.*.*\n...
31
0
0
52,471
0
none
[ "none" ]
null
null
Polycarpus got an internship in one well-known social network. His test task is to count the number of unique users who have visited a social network during the day. Polycarpus was provided with information on all user requests for this time period. For each query, we know its time... and nothing else, because Polycarp...
The first line contains three integers *n*, *M* and *T* (1<=≀<=*n*,<=*M*<=≀<=20<=000, 1<=≀<=*T*<=≀<=86400) β€” the number of queries, the record number of online users and the time when the user was online after a query was sent. Next *n* lines contain the times of the queries in the format "hh:mm:ss", where hh are hours...
In the first line print number *R* β€” the largest possible number of distinct users. The following *n* lines should contain the user IDs for requests in the same order in which the requests are given in the input. User IDs must be integers from 1 to *R*. The requests of the same user must correspond to the same identifi...
[ "4 2 10\n17:05:53\n17:05:58\n17:06:01\n22:39:47\n", "1 2 86400\n00:00:00\n" ]
[ "3\n1\n2\n2\n3\n", "No solution\n" ]
Consider the first sample. The user who sent the first request was online from 17:05:53 to 17:06:02, the user who sent the second request was online from 17:05:58 to 17:06:07, the user who sent the third request, was online from 17:06:01 to 17:06:10. Thus, these IDs cannot belong to three distinct users, because in tha...
[]
31
0
0
52,505
439
Devu and Partitioning of the Array
[ "brute force", "constructive algorithms", "implementation", "number theory" ]
null
null
Devu being a small kid, likes to play a lot, but he only likes to play with arrays. While playing he came up with an interesting question which he could not solve, can you please solve it for him? Given an array consisting of distinct integers. Is it possible to partition the whole array into *k* disjoint non-empty pa...
The first line will contain three space separated integers *n*, *k*, *p* (1<=≀<=*k*<=≀<=*n*<=≀<=105;Β 0<=≀<=*p*<=≀<=*k*). The next line will contain *n* space-separated distinct integers representing the content of array *a*: *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109).
In the first line print "YES" (without the quotes) if it is possible to partition the array in the required way. Otherwise print "NO" (without the quotes). If the required partition exists, print *k* lines after the first line. The *i**th* of them should contain the content of the *i**th* part. Print the content of th...
[ "5 5 3\n2 6 10 5 9\n", "5 5 3\n7 14 2 9 5\n", "5 3 1\n1 2 3 7 5\n" ]
[ "YES\n1 9\n1 5\n1 10\n1 6\n1 2\n", "NO\n", "YES\n3 5 1 3\n1 7\n1 2\n" ]
none
[ { "input": "5 5 3\n2 6 10 5 9", "output": "YES\n1 9\n1 5\n1 10\n1 6\n1 2" }, { "input": "5 5 3\n7 14 2 9 5", "output": "NO" }, { "input": "5 3 1\n1 2 3 7 5", "output": "YES\n3 5 1 3\n1 7\n1 2" }, { "input": "10 5 3\n194757070 828985446 11164 80016 84729 117765558 111730436 16...
482
16,076,800
3
52,519
756
Byteland coins
[ "combinatorics", "dp", "math" ]
null
null
There are *n* types of coins in Byteland. Conveniently, the denomination of the coin type *k* divides the denomination of the coin type *k*<=+<=1, the denomination of the coin type 1 equals 1 tugrick. The ratio of the denominations of coin types *k*<=+<=1 and *k* equals *a**k*. It is known that for each *x* there are a...
The first line contains single integer *n* (1<=≀<=*n*<=≀<=3Β·105)Β β€” the number of coin types. The second line contains *n*<=-<=1 integers *a*1, *a*2, ..., *a**n*<=-<=1 (1<=≀<=*a**k*<=≀<=109)Β β€” the ratios between the coin types denominations. It is guaranteed that for each *x* there are at most 20 coin types of denomina...
Print single integerΒ β€” the number of ways to pay exactly *m* tugricks modulo 109<=+<=7.
[ "1\n\n4\n2\n", "2\n1\n4 4\n2\n", "3\n3 3\n10 10 10\n17\n" ]
[ "1\n", "3\n", "6\n" ]
In the first example Byteasar has 4 coins of denomination 1, and he has to pay 2 tugricks. There is only one way. In the second example Byteasar has 4 coins of each of two different types of denomination 1, he has to pay 2 tugricks. There are 3 ways: pay one coin of the first type and one coin of the other, pay two co...
[]
46
4,608,000
0
52,647
758
Geometrical Progression
[ "brute force", "math", "number theory" ]
null
null
For given *n*, *l* and *r* find the number of distinct geometrical progression, each of which contains *n* distinct integers not less than *l* and not greater than *r*. In other words, for each progression the following must hold: *l*<=≀<=*a**i*<=≀<=*r* and *a**i*<=β‰ <=*a**j* , where *a*1,<=*a*2,<=...,<=*a**n* is the ge...
The first and the only line cotains three integers *n*, *l* and *r* (1<=≀<=*n*<=≀<=107,<=1<=≀<=*l*<=≀<=*r*<=≀<=107).
Print the integer *K*Β β€” is the answer to the problem.
[ "1 1 10\n", "2 6 9\n", "3 1 10\n", "3 3 10\n" ]
[ "10", "12", "8", "2" ]
These are possible progressions for the first test of examples: - 1; - 2; - 3; - 4; - 5; - 6; - 7; - 8; - 9; - 10. These are possible progressions for the second test of examples: - 6, 7; - 6, 8; - 6, 9; - 7, 6; - 7, 8; - 7, 9; - 8, 6; - 8, 7; - 8, 9; - 9, 6; - 9, 7; - 9, 8. These are possib...
[ { "input": "1 1 10", "output": "10" }, { "input": "2 6 9", "output": "12" }, { "input": "3 1 10", "output": "8" }, { "input": "3 3 10", "output": "2" }, { "input": "1 25 845", "output": "821" }, { "input": "2 25 845", "output": "673220" }, { ...
30
0
0
52,712
116
Little Pigs and Wolves
[ "greedy", "implementation" ]
null
null
Once upon a time there were several little pigs and several wolves on a two-dimensional grid of size *n*<=Γ—<=*m*. Each cell in this grid was either empty, containing one little pig, or containing one wolf. A little pig and a wolf are adjacent if the cells that they are located at share a side. The little pigs are afra...
The first line contains integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=10) which denotes the number of rows and columns in our two-dimensional grid, respectively. Then follow *n* lines containing *m* characters each β€” that is the grid description. "." means that this cell is empty. "P" means that this cell contains a little...
Print a single number β€” the maximal number of little pigs that may be eaten by the wolves.
[ "2 3\nPPW\nW.P\n", "3 3\nP.W\n.P.\nW.P\n" ]
[ "2\n", "0\n" ]
In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows.
[ { "input": "2 3\nPPW\nW.P", "output": "2" }, { "input": "3 3\nP.W\n.P.\nW.P", "output": "0" }, { "input": "1 1\nP", "output": "0" }, { "input": "2 6\nWW..WW\n.PPPP.", "output": "2" }, { "input": "6 2\n.W\n.W\n.P\nWP\n.P\nPW", "output": "3" }, { "input"...
0
0
-1
52,741
721
Maxim and Array
[ "constructive algorithms", "data structures", "greedy", "math" ]
null
null
Recently Maxim has found an array of *n* integers, needed by no one. He immediately come up with idea of changing it: he invented positive integer *x* and decided to add or subtract it from arbitrary array elements. Formally, by applying single operation Maxim chooses integer *i* (1<=≀<=*i*<=≀<=*n*) and replaces the *i...
The first line of the input contains three integers *n*,<=*k* and *x* (1<=≀<=*n*,<=*k*<=≀<=200<=000,<=1<=≀<=*x*<=≀<=109)Β β€” the number of elements in the array, the maximum number of operations and the number invented by Maxim, respectively. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* ()Β β€” the elem...
Print *n* integers *b*1,<=*b*2,<=...,<=*b**n* in the only lineΒ β€” the array elements after applying no more than *k* operations to the array. In particular, should stay true for every 1<=≀<=*i*<=≀<=*n*, but the product of all array elements should be minimum possible. If there are multiple answers, print any of them.
[ "5 3 1\n5 4 3 5 2\n", "5 3 1\n5 4 3 5 5\n", "5 3 1\n5 4 4 5 5\n", "3 2 7\n5 4 2\n" ]
[ "5 4 3 5 -1 \n", "5 4 0 5 5 \n", "5 1 4 5 5 \n", "5 11 -5 \n" ]
none
[ { "input": "5 3 1\n5 4 3 5 2", "output": "5 4 3 5 -1 " }, { "input": "5 3 1\n5 4 3 5 5", "output": "5 4 0 5 5 " }, { "input": "5 3 1\n5 4 4 5 5", "output": "5 1 4 5 5 " }, { "input": "3 2 7\n5 4 2", "output": "5 11 -5 " }, { "input": "100 1 1\n-1 -1 -2 0 -2 -1 0 0...
826
24,576,000
3
52,830
370
Berland Bingo
[ "implementation" ]
null
null
Lately, a national version of a bingo game has become very popular in Berland. There are *n* players playing the game, each player has a card with numbers. The numbers on each card are distinct, but distinct cards can have equal numbers. The card of the *i*-th player contains *m**i* numbers. During the game the host t...
The first line of the input contains integer *n* (1<=≀<=*n*<=≀<=100) β€” the number of the players. Then follow *n* lines, each line describes a player's card. The line that describes a card starts from integer *m**i* (1<=≀<=*m**i*<=≀<=100) that shows how many numbers the *i*-th player's card has. Then follows a sequence...
Print *n* lines, the *i*-th line must contain word "YES" (without the quotes), if the *i*-th player can win, and "NO" (without the quotes) otherwise.
[ "3\n1 1\n3 2 4 1\n2 10 11\n", "2\n1 1\n1 1\n" ]
[ "YES\nNO\nYES\n", "NO\nNO\n" ]
none
[ { "input": "3\n1 1\n3 2 4 1\n2 10 11", "output": "YES\nNO\nYES" }, { "input": "2\n1 1\n1 1", "output": "NO\nNO" }, { "input": "1\n1 1", "output": "YES" }, { "input": "2\n1 2\n1 3", "output": "YES\nYES" }, { "input": "2\n1 1\n2 1 2", "output": "YES\nNO" }, ...
46
0
0
52,929
601
Lipshitz Sequence
[ "data structures", "math" ]
null
null
A function is called Lipschitz continuous if there is a real constant *K* such that the inequality |*f*(*x*)<=-<=*f*(*y*)|<=≀<=*K*Β·|*x*<=-<=*y*| holds for all . We'll deal with a more... discrete version of this term. For an array , we define it's Lipschitz constant as follows: - if *n*<=&lt;<=2, - if *n*<=β‰₯<=2,...
The first line of the input contains two space-separated integers *n* and *q* (2<=≀<=*n*<=≀<=100<=000 and 1<=≀<=*q*<=≀<=100)Β β€” the number of elements in array and the number of queries respectively. The second line contains *n* space-separated integers (). The following *q* lines describe queries. The *i*-th of tho...
Print the answers to all queries in the order in which they are given in the input. For the *i*-th query, print one line containing a single integerΒ β€” the sum of Lipschitz constants of all subarrays of .
[ "10 4\n1 5 2 9 1 3 4 2 1 7\n2 4\n3 8\n7 10\n1 9\n", "7 6\n5 7 7 4 6 6 2\n1 2\n2 3\n2 6\n1 7\n4 7\n3 5\n" ]
[ "17\n82\n23\n210\n", "2\n0\n22\n59\n16\n8\n" ]
In the first query of the first sample, the Lipschitz constants of subarrays of <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ddadffaf9e222576b1abb771dcd83dbad588d7fe.png" style="max-width: 100.0%;max-height: 100.0%;"/> with length at least 2 are: - <img align="middle" class="tex-formul...
[ { "input": "10 4\n1 5 2 9 1 3 4 2 1 7\n2 4\n3 8\n7 10\n1 9", "output": "17\n82\n23\n210" }, { "input": "7 6\n5 7 7 4 6 6 2\n1 2\n2 3\n2 6\n1 7\n4 7\n3 5", "output": "2\n0\n22\n59\n16\n8" }, { "input": "2 2\n0 0\n1 2\n1 2", "output": "0\n0" }, { "input": "2 2\n0 100000000\n1 2...
46
0
0
53,040
0
none
[ "none" ]
null
null
Vasya became interested in bioinformatics. He's going to write an article about similar cyclic DNA sequences, so he invented a new method for determining the similarity of cyclic sequences. Let's assume that strings *s* and *t* have the same length *n*, then the function *h*(*s*,<=*t*) is defined as the number of posi...
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=105). The second line of the input contains a single string of length *n*, consisting of characters "ACGT".
Print a single numberΒ β€” the answer modulo 109<=+<=7.
[ "1\nC\n", "2\nAG\n", "3\nTTT\n" ]
[ "1\n", "4\n", "1\n" ]
Please note that if for two distinct strings *t*<sub class="lower-index">1</sub> and *t*<sub class="lower-index">2</sub> values ρ(*s*, *t*<sub class="lower-index">1</sub>) ΠΈ ρ(*s*, *t*<sub class="lower-index">2</sub>) are maximum among all possible *t*, then both strings must be taken into account in the answer even if...
[ { "input": "1\nC", "output": "1" }, { "input": "2\nAG", "output": "4" }, { "input": "3\nTTT", "output": "1" }, { "input": "4\nGACT", "output": "256" }, { "input": "1\nT", "output": "1" }, { "input": "2\nAG", "output": "4" }, { "input": "3\n...
62
204,800
3
53,043
932
Palindrome Partition
[ "dp", "string suffix structures", "strings" ]
null
null
Given a string *s*, find the number of ways to split *s* to substrings such that if there are *k* substrings (*p*1,<=*p*2,<=*p*3,<=...,<=*p**k*) in partition, then *p**i*<==<=*p**k*<=-<=*i*<=+<=1 for all *i* (1<=≀<=*i*<=≀<=*k*) and *k* is even. Since the number of ways can be large, print it modulo 109<=+<=7.
The only line of input contains a string *s* (2<=≀<=|*s*|<=≀<=106) of even length consisting of lowercase Latin letters.
Print one integer, the number of ways of partitioning the string modulo 109<=+<=7.
[ "abcdcdab\n", "abbababababbab\n" ]
[ "1", "3" ]
In the first case, the only way to partition the string is *ab*|*cd*|*cd*|*ab*. In the second case, the string can be partitioned as *ab*|*b*|*ab*|*ab*|*ab*|*ab*|*b*|*ab* or *ab*|*b*|*abab*|*abab*|*b*|*ab* or *abbab*|*ab*|*ab*|*abbab*.
[]
46
0
0
53,053
1,005
Berland and the Shortest Paths
[ "brute force", "dfs and similar", "graphs", "shortest paths" ]
null
null
There are $n$ cities in Berland. Some pairs of cities are connected by roads. All roads are bidirectional. Each road connects two different cities. There is at most one road between a pair of cities. The cities are numbered from $1$ to $n$. It is known that, from the capital (the city with the number $1$), you can rea...
The first line of the input contains integers $n$, $m$ and $k$ ($2 \le n \le 2\cdot10^5, n-1 \le m \le 2\cdot10^5, 1 \le k \le 2\cdot10^5$), where $n$ is the number of cities in the country, $m$ is the number of roads and $k$ is the number of options to choose a set of roads for repair. It is guaranteed that $m \cdot k...
Print $t$ ($1 \le t \le k$) β€” the number of ways to choose a set of roads for repair. Recall that you need to find $k$ different options; if there are fewer than $k$ of them, then you need to find all possible different valid options. In the following $t$ lines, print the options, one per line. Print an option as a st...
[ "4 4 3\n1 2\n2 3\n1 4\n4 3\n", "4 6 3\n1 2\n2 3\n1 4\n4 3\n2 4\n1 3\n", "5 6 2\n1 2\n1 3\n2 4\n2 5\n3 4\n3 5\n" ]
[ "2\n1110\n1011\n", "1\n101001\n", "2\n111100\n110110\n" ]
none
[ { "input": "4 4 3\n1 2\n2 3\n1 4\n4 3", "output": "2\n1110\n1011" }, { "input": "4 6 3\n1 2\n2 3\n1 4\n4 3\n2 4\n1 3", "output": "1\n101001" }, { "input": "5 6 2\n1 2\n1 3\n2 4\n2 5\n3 4\n3 5", "output": "2\n111100\n110110" }, { "input": "2 1 200000\n2 1", "output": "1\n1...
0
0
-1
53,157
855
Salazar Slytherin's Locket
[ "bitmasks", "dp" ]
null
null
Harry came to know from Dumbledore that Salazar Slytherin's locket is a horcrux. This locket was present earlier at 12 Grimmauld Place, the home of Sirius Black's mother. It was stolen from there and is now present in the Ministry of Magic in the office of Dolorous Umbridge, Harry's former Defense Against the Dark Arts...
First line of input contains *q* (1<=≀<=*q*<=≀<=105)Β β€” number of queries. Each of the next *q* lines contain three space separated integers *b**i*, *l**i*, *r**i* (2<=≀<=*b**i*<=≀<=10, 1<=≀<=*l**i*<=≀<=*r**i*<=≀<=1018).
You have to output *q* lines, each containing a single integer, the answer to the corresponding query.
[ "2\n2 4 9\n3 1 10\n", "2\n2 1 100\n5 1 100\n" ]
[ "1\n2\n", "21\n4\n" ]
In sample test case 1, for first query, when we convert numbers 4 to 9 into base 2, we get: - 4 = 100<sub class="lower-index">2</sub>, - 5 = 101<sub class="lower-index">2</sub>, - 6 = 110<sub class="lower-index">2</sub>, - 7 = 111<sub class="lower-index">2</sub>, - 8 = 1000<sub class="lower-index">2</sub>, - 9 ...
[ { "input": "2\n2 4 9\n3 1 10", "output": "1\n2" }, { "input": "2\n2 1 100\n5 1 100", "output": "21\n4" }, { "input": "10\n4 108 114\n5 30 155\n8 193 197\n9 71 169\n2 163 166\n8 120 144\n8 22 151\n4 21 166\n2 46 127\n8 38 51", "output": "0\n3\n0\n1\n3\n0\n5\n16\n9\n1" }, { "in...
46
307,200
0
53,562
191
Thwarting Demonstrations
[ "binary search", "data structures", "trees" ]
null
null
It is dark times in Berland. Berlyand opposition, funded from a neighboring state, has organized a demonstration in Berland capital Bertown. Through the work of intelligence we know that the demonstrations are planned to last for *k* days. Fortunately, Berland has a special police unit, which can save the country. It ...
The first line contains two integers *n* and *k* β€” the number of soldiers in the detachment and the number of times somebody goes on duty. The second line contains *n* space-separated integers *a**i*, their absolute value doesn't exceed 109 β€” the soldiers' reliabilities. Please do not use the %lld specifier to read ...
Print a single number β€” the sought minimum reliability of the groups that go on duty during these *k* days.
[ "3 4\n1 4 2\n", "4 6\n2 -1 2 -1\n", "8 10\n1 -2 3 -4 5 -6 7 -8\n" ]
[ "4\n", "1\n", "2\n" ]
none
[ { "input": "3 4\n1 4 2", "output": "4" }, { "input": "4 6\n2 -1 2 -1", "output": "1" }, { "input": "8 10\n1 -2 3 -4 5 -6 7 -8", "output": "2" }, { "input": "10 13\n11 73 57 -34 61 38 -83 10 -88 -32", "output": "99" }, { "input": "20 31\n19 38 -67 83 -83 79 98 -8 8...
6,000
334,438,400
0
53,652
325
The Red Button
[ "combinatorics", "dfs and similar", "dsu", "graphs", "greedy" ]
null
null
Piegirl found the red button. You have one last chance to change the inevitable end. The circuit under the button consists of *n* nodes, numbered from 0 to *n* - 1. In order to deactivate the button, the *n* nodes must be disarmed in a particular order. Node 0 must be disarmed first. After disarming node *i*, the next...
Input consists of a single integer *n* (2<=≀<=*n*<=≀<=105).
Print an order in which you can to disarm all nodes. If it is impossible, print -1 instead. If there are multiple orders, print any one of them.
[ "2\n", "3\n", "4\n", "16\n" ]
[ "0 1 0\n", "-1", "0 1 3 2 0\n", "0 1 2 4 9 3 6 13 10 5 11 7 15 14 12 8 0\n" ]
none
[ { "input": "2", "output": "0 1 0" }, { "input": "3", "output": "-1" }, { "input": "4", "output": "0 1 3 2 0" }, { "input": "16", "output": "0 1 2 4 9 3 6 13 10 5 11 7 15 14 12 8 0" }, { "input": "5", "output": "-1" }, { "input": "7", "output": "-1"...
92
0
0
53,667
982
Shark
[ "brute force", "data structures", "dsu", "trees" ]
null
null
For long time scientists study the behavior of sharks. Sharks, as many other species, alternate short movements in a certain location and long movements between locations. Max is a young biologist. For $n$ days he watched a specific shark, and now he knows the distance the shark traveled in each of the days. All the d...
The first line contains a single integer $n$ ($1 \leq n \leq 10^5$) β€” the number of days. The second line contains $n$ distinct positive integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) β€” the distance traveled in each of the day.
Print a single integer $k$, such that 1. the shark was in each location the same number of days, 1. the number of locations is maximum possible satisfying the first condition, 1. $k$ is smallest possible satisfying the first and second conditions.
[ "8\n1 2 7 3 4 8 5 6\n", "6\n25 1 2 3 14 36\n" ]
[ "7", "2" ]
In the first example the shark travels inside a location on days $1$ and $2$ (first location), then on $4$-th and $5$-th days (second location), then on $7$-th and $8$-th days (third location). There are three locations in total. In the second example the shark only moves inside a location on the $2$-nd day, so there ...
[ { "input": "8\n1 2 7 3 4 8 5 6", "output": "7" }, { "input": "6\n25 1 2 3 14 36", "output": "2" }, { "input": "20\n1 20 2 19 3 18 4 17 5 16 6 15 7 14 8 13 9 12 10 11", "output": "11" }, { "input": "7\n1 2 5 7 3 4 6", "output": "5" }, { "input": "1\n1000000000", ...
389
22,016,000
3
53,694
518
Ilya and Escalator
[ "combinatorics", "dp", "math", "probabilities" ]
null
null
Ilya got tired of sports programming, left university and got a job in the subway. He was given the task to determine the escalator load factor. Let's assume that *n* people stand in the queue for the escalator. At each second one of the two following possibilities takes place: either the first person in the queue en...
The first line of the input contains three numbers *n*,<=*p*,<=*t* (1<=≀<=*n*,<=*t*<=≀<=2000, 0<=≀<=*p*<=≀<=1). Numbers *n* and *t* are integers, number *p* is real, given with exactly two digits after the decimal point.
Print a single real number β€” the expected number of people who will be standing on the escalator after *t* seconds. The absolute or relative error mustn't exceed 10<=-<=6.
[ "1 0.50 1\n", "1 0.50 4\n", "4 0.20 2\n" ]
[ "0.5\n", "0.9375\n", "0.4\n" ]
none
[ { "input": "1 0.50 1", "output": "0.500000000000000" }, { "input": "1 0.50 4", "output": "0.937500000000000" }, { "input": "4 0.20 2", "output": "0.400000000000000" }, { "input": "2000 0.61 2000", "output": "1219.999999999999545" }, { "input": "100 1.00 200", ...
124
10,649,600
-1
53,711
126
Fibonacci Sums
[ "dp", "math" ]
null
null
Fibonacci numbers have the following form: Let's consider some non-empty set *S*<==<={*s*1,<=*s*2,<=...,<=*s**k*}, consisting of different Fibonacci numbers. Let's find the sum of values of this set's elements: Let's call the set *S* a number *n*'s decomposition into Fibonacci sum. It's easy to see that several num...
The first line contains an integer *t* β€” the number of tests (1<=≀<=*t*<=≀<=105). Each of the following *t* lines contains one test. Each test is an integer *n* (1<=≀<=*n*<=≀<=1018). 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 or the %I...
For each input data test print a single number on a single line β€” the answer to the problem.
[ "2\n13\n16\n" ]
[ "3\n4\n" ]
Two decompositions are different if there exists a number that is contained in the first decomposition, but is not contained in the second one. Decompositions that differ only in the order of summands are considered equal.
[ { "input": "2\n13\n16", "output": "3\n4" }, { "input": "10\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10", "output": "1\n1\n2\n1\n2\n2\n1\n3\n2\n2" }, { "input": "10\n24\n37\n42\n58\n60\n73\n79\n84\n92\n99", "output": "5\n6\n6\n7\n6\n6\n8\n7\n8\n6" }, { "input": "5\n484775665757\n9686857765...
30
0
0
53,998
137
Palindromes
[ "dp", "strings" ]
null
null
Friday is Polycarpus' favourite day of the week. Not because it is followed by the weekend, but because the lessons on Friday are 2 IT lessons, 2 math lessons and 2 literature lessons. Of course, Polycarpus has prepared to all of them, unlike his buddy Innocentius. Innocentius spent all evening playing his favourite ga...
The first input line contains a non-empty string *s* which is the text of "Storm and Calm" (without spaces). The length of the string *s* does not exceed 500 characters. String *s* consists of uppercase and lowercase Latin letters. The second line contains a single number *k* (1<=≀<=*k*<=≀<=|*s*|, where |*s*| represent...
Print on the first line the minimum number of changes that Innocentius will have to make. Print on the second line the string consisting of no more than *k* palindromes. Each palindrome should be non-empty and consist of uppercase and lowercase Latin letters. Use the character "+" (ASCII-code 43) to separate consecutiv...
[ "abacaba\n1\n", "abdcaba\n2\n", "abdcaba\n5\n", "abacababababbcbabcd\n3\n" ]
[ "0\nabacaba\n", "1\nabdcdba\n", "0\na+b+d+c+aba\n", "1\nabacaba+babab+bcbabcb\n" ]
none
[ { "input": "abacaba\n1", "output": "0\nabacaba" }, { "input": "abdcaba\n2", "output": "1\nabdcdba" }, { "input": "abdcaba\n5", "output": "0\na+b+d+c+aba" }, { "input": "abacababababbcbabcd\n3", "output": "1\nabacaba+babab+bcbabcb" }, { "input": "abcdefghijklmnopqr...
122
0
-1
54,062
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\...
108
1,638,400
3
54,091
835
Roads in the Kingdom
[ "dfs and similar", "dp", "graphs", "trees" ]
null
null
In the Kingdom K., there are *n* towns numbered with integers from 1 to *n*. The towns are connected by *n* bi-directional roads numbered with integers from 1 to *n*. The *i*-th road connects the towns *u**i* and *v**i* and its length is *l**i*. There is no more than one road between two towns. Also, there are no roads...
The first line contains the integer *n* (3<=≀<=*n*<=≀<=2Β·105)Β β€” the number of towns and roads. The next *n* lines contain the roads description. The *i*-th from these lines contains three integers *u**i*, *v**i*, *l**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*, 1<=≀<=*l**i*<=≀<=109)Β β€” the numbers of towns connected by the *i*-t...
Print a single integerΒ β€” the minimum possible inconvenience of the roads after the refusal from one of the roads.
[ "3\n1 2 4\n2 3 5\n1 3 1\n", "5\n2 3 7\n3 1 9\n4 1 8\n3 5 4\n4 5 5\n" ]
[ "5\n", "18\n" ]
none
[]
31
0
0
54,280
989
A Trance of Nightfall
[ "dp", "geometry", "matrices", "probabilities" ]
null
null
"Flowing and passing like this, the water isn't gone ultimately; Waxing and waning like that, the moon doesn't shrink or grow eventually." "Everything is transient in a way and perennial in another." Kanno doesn't seem to make much sense out of Mino's isolated words, but maybe it's time that they enjoy the gentle bre...
The first line contains a positive integer $n$ ($2 \leq n \leq 200$)Β β€” the number of points in $S$. The $i$-th of the following $n$ lines contains two space-separated integers $x_i$ and $y_i$ ($-10^4 \leq x_i, y_i \leq 10^4$)Β β€” the coordinates of the $i$-th point in $S$. The input guarantees that for all $1 \leq i \lt...
Output $q$ lines each containing a decimal numberΒ β€” the $i$-th among them denotes the maximum probability of staying on the $t_i$-th point after $m_i$ steps, with a proper choice of starting position $P$. Your answer will be considered correct if each number in your output differs from the corresponding one in jury's ...
[ "5\n0 0\n1 3\n2 2\n3 1\n4 4\n10\n1 1\n2 1\n3 1\n4 1\n5 1\n3 2\n3 3\n3 4\n3 5\n3 6\n" ]
[ "0.50000000000000000000\n0.50000000000000000000\n0.33333333333333331483\n0.50000000000000000000\n0.50000000000000000000\n0.18518518518518517491\n0.15226337448559670862\n0.14494741655235482414\n0.14332164812274550414\n0.14296036624949901017\n" ]
The points in $S$ and possible candidates for line $l$ are depicted in the following figure. For the first query, when $P = (-1, -3)$, $l$ is uniquely determined to be $3x = y$, and thus Kanno will move to $(0, 0)$ with a probability of $\frac 1 2$. For the third query, when $P = (2, 2)$, $l$ is chosen equiprobably b...
[]
46
0
0
54,285
741
Arpa’s overnight party and Mehrdad’s silent entering
[ "constructive algorithms", "dfs and similar", "graphs" ]
null
null
Note that girls in Arpa’s land are really attractive. Arpa loves overnight parties. In the middle of one of these parties Mehrdad suddenly appeared. He saw *n* pairs of friends sitting around a table. *i*-th pair consisted of a boy, sitting on the *a**i*-th chair, and his girlfriend, sitting on the *b**i*-th chair. Th...
The first line contains an integer *n* (1<=<=≀<=<=*n*<=<=≀<=<=105)Β β€” the number of pairs of guests. The *i*-th of the next *n* lines contains a pair of integers *a**i* and *b**i* (1<=<=≀<=*a**i*,<=*b**i*<=≀<=<=2*n*)Β β€” the number of chair on which the boy in the *i*-th pair was sitting and the number of chair on which ...
If there is no solution, print -1. Otherwise print *n* lines, the *i*-th of them should contain two integers which represent the type of food for the *i*-th pair. The first integer in the line is the type of food the boy had, and the second integer is the type of food the girl had. If someone had Kooft, print 1, other...
[ "3\n1 4\n2 5\n3 6\n" ]
[ "1 2\n2 1\n1 2\n" ]
none
[ { "input": "3\n1 4\n2 5\n3 6", "output": "1 2\n2 1\n1 2" }, { "input": "6\n3 2\n5 11\n7 12\n6 9\n8 4\n1 10", "output": "1 2\n1 2\n2 1\n2 1\n1 2\n1 2" }, { "input": "19\n30 27\n6 38\n10 28\n20 5\n14 18\n32 2\n36 29\n12 1\n31 24\n15 4\n35 11\n3 7\n21 17\n25 19\n16 8\n23 22\n37 33\n13 9\n34...
264
22,118,400
-1
54,394
164
Machine Programming
[ "flows", "graphs" ]
null
null
One remarkable day company "X" received *k* machines. And they were not simple machines, they were mechanical programmers! This was the last unsuccessful step before switching to android programmers, but that's another story. The company has now *n* tasks, for each of them we know the start time of its execution *s**i...
The first line contains two integer numbers *n* and *k* (1<=≀<=*n*<=≀<=1000, 1<=≀<=*k*<=≀<=50) β€” the numbers of tasks and machines, correspondingly. The next *n* lines contain space-separated groups of three integers *s**i*,<=*t**i*,<=*c**i* (1<=≀<=*s**i*,<=*t**i*<=≀<=109, 1<=≀<=*c**i*<=≀<=106), *s**i* is the time whe...
Print *n* integers *x*1,<=*x*2,<=...,<=*x**n*. Number *x**i* should equal 1, if task *i* should be completed and otherwise it should equal 0. If there are several optimal solutions, print any of them.
[ "3 1\n2 7 5\n1 3 3\n4 1 3\n", "5 2\n1 5 4\n1 4 5\n1 3 2\n4 1 2\n5 6 1\n" ]
[ "0 1 1\n", "1 1 0 0 1\n" ]
In the first sample the tasks need to be executed at moments of time 2 ... 8, 1 ... 3 and 4 ... 4, correspondingly. The first task overlaps with the second and the third ones, so we can execute either task one (profit 5) or tasks two and three (profit 6).
[]
92
0
0
54,407
493
Vasya and Polynomial
[ "math" ]
null
null
Vasya is studying in the last class of school and soon he will take exams. He decided to study polynomials. Polynomial is a function *P*(*x*)<==<=*a*0<=+<=*a*1*x*1<=+<=...<=+<=*a**n**x**n*. Numbers *a**i* are called coefficients of a polynomial, non-negative integer *n* is called a degree of a polynomial. Vasya has ma...
The input contains three integer positive numbers no greater than 1018.
If there is an infinite number of such polynomials, then print "inf" without quotes, otherwise print the reminder of an answer modulo 109<=+<=7.
[ "2 2 2\n", "2 3 3\n" ]
[ "2\n", "1\n" ]
none
[ { "input": "2 2 2", "output": "2" }, { "input": "2 3 3", "output": "1" }, { "input": "1 1 1", "output": "inf" }, { "input": "3 5 10", "output": "0" }, { "input": "2 3 1000000000000000000", "output": "0" }, { "input": "7 8 9", "output": "1" }, {...
93
0
-1
54,521
700
Cool Slogans
[ "string suffix structures", "strings" ]
null
null
Bomboslav set up a branding agency and now helps companies to create new logos and advertising slogans. In term of this problems, slogan of the company should be a non-empty substring of its name. For example, if the company name is "hornsandhoofs", then substrings "sand" and "hor" could be its slogans, while strings "...
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=200<=000)Β β€” the length of the company name that asks Bomboslav to help. The second line contains the string *w* of length *n*, that consists of lowercase English letters.
Print a single integerΒ β€” the maximum possible length of the sequence of slogans of the company named *w*, such that any slogan in the sequence (except the first one) is cooler than the previous
[ "3\nabc\n", "5\nddddd\n", "11\nabracadabra\n" ]
[ "1\n", "5\n", "3\n" ]
none
[]
61
0
-1
54,546
549
Haar Features
[ "greedy", "implementation" ]
null
null
The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept. Let's consider a rectangular image that is represent...
The first line contains two space-separated integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=100) β€” the number of rows and columns in the feature. Next *n* lines contain the description of the feature. Each line consists of *m* characters, the *j*-th character of the *i*-th line equals to "W", if this element of the feature ...
Print a single number β€” the minimum number of operations that you need to make to calculate the value of the feature.
[ "6 8\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nWWWWWWWW\nWWWWWWWW\nWWWWWWWW\n", "3 3\nWBW\nBWW\nWWW\n", "3 6\nWWBBWW\nWWBBWW\nWWBBWW\n", "4 4\nBBBB\nBBBB\nBBBB\nBBBW\n" ]
[ "2\n", "4\n", "3\n", "4\n" ]
The first sample corresponds to feature *B*, the one shown in the picture. The value of this feature in an image of size 6 × 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations: 1. add the sum of pixels...
[ { "input": "6 8\nBBBBBBBB\nBBBBBBBB\nBBBBBBBB\nWWWWWWWW\nWWWWWWWW\nWWWWWWWW", "output": "2" }, { "input": "3 3\nWBW\nBWW\nWWW", "output": "4" }, { "input": "3 6\nWWBBWW\nWWBBWW\nWWBBWW", "output": "3" }, { "input": "4 4\nBBBB\nBBBB\nBBBB\nBBBW", "output": "4" }, { ...
155
1,331,200
3
54,581
982
The Meeting Place Cannot Be Changed
[ "dfs and similar", "graphs" ]
null
null
Petr is a detective in Braginsk. Somebody stole a huge amount of money from a bank and Petr is to catch him. Somebody told Petr that some luxurious car moves along the roads without stopping. Petr knows that it is the robbers who drive the car. The roads in Braginsk are one-directional and each of them connects two in...
The first line of the input contains two integers $n$ and $m$ ($2 \leq n \le 10^5$, $2 \leq m \leq 5 \cdot 10^5$)Β β€” the number of intersections and the number of directed roads in Braginsk, respectively. Each of the next $m$ lines contains two integers $u_i$ and $v_i$ ($1 \le u_i, v_i \le n$, $u_i \ne v_i$)Β β€” the star...
Print a single integer $k$Β β€” the intersection Petr needs to choose. If there are multiple answers, print any. If there are no such intersections, print $-1$.
[ "5 6\n1 2\n2 3\n3 1\n3 4\n4 5\n5 3\n", "3 3\n1 2\n2 3\n3 1\n" ]
[ "3", "1" ]
In the first example the robbers can move, for example, along the following routes: $(1-2-3-1)$, $(3-4-5-3)$, $(1-2-3-4-5-3-1)$. We can show that if Petr chooses the $3$-rd intersection, he will eventually meet the robbers independently of their route.
[]
124
102,400
0
54,799
847
Students Initiation
[ "binary search", "flows", "graphs" ]
null
null
Soon the first year students will be initiated into students at the University of Berland. The organizers of the initiation come up with a program for this holiday. In their opinion, it would be good if the first-year students presented small souvenirs to each other. When they voiced this idea to the first-year student...
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=5000, 0<=≀<=*m*<=≀<=*min*(5000,<=*n*Β·(*n*<=-<=1)<=/<=2)) β€” the number of the first year students and the number of pairs of the students that know each other. The students are numbered from 1 to *n*. Each of the following *m* lines contains two integers *...
Print a single integer into the first line β€” the smallest number of souvenirs that the unluckiest student will have to present. Following should be *m* lines, each containing two integers β€” the students which are familiar with each other. The first number in the pair must be the student that will present the souvenir ...
[ "5 4\n2 1\n1 3\n2 3\n2 5\n", "4 3\n1 2\n1 3\n1 4\n", "4 6\n1 2\n4 1\n4 2\n3 2\n4 3\n1 3\n" ]
[ "1\n1 2\n2 3\n3 1\n5 2\n", "1\n1 4\n2 1\n3 1\n", "2\n1 3\n2 1\n2 4\n3 2\n4 1\n4 3\n" ]
none
[]
46
0
0
54,882
899
Segments Removal
[ "data structures", "dsu", "flows", "implementation", "two pointers" ]
null
null
Vasya has an array of integers of length *n*. Vasya performs the following operations on the array: on each step he finds the longest segment of consecutive equal integers (the leftmost, if there are several such segments) and removes it. For example, if Vasya's array is [13,<=13,<=7,<=7,<=7,<=2,<=2,<=2], then after o...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=200<=000) β€” the length of the array. The second line contains a sequence *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109) β€” Vasya's array.
Print the number of operations Vasya should make to remove all elements from the array.
[ "4\n2 5 5 2\n", "5\n6 3 4 1 5\n", "8\n4 4 4 2 2 100 100 100\n", "6\n10 10 50 10 50 50\n" ]
[ "2\n", "5\n", "3\n", "4\n" ]
In the first example, at first Vasya removes two fives at the second and third positions. The array becomes [2, 2]. In the second operation Vasya removes two twos at the first and second positions. After that the array becomes empty. In the second example Vasya has to perform five operations to make the array empty. I...
[ { "input": "4\n2 5 5 2", "output": "2" }, { "input": "5\n6 3 4 1 5", "output": "5" }, { "input": "8\n4 4 4 2 2 100 100 100", "output": "3" }, { "input": "6\n10 10 50 10 50 50", "output": "4" }, { "input": "1\n1", "output": "1" }, { "input": "100\n45 45...
30
0
0
55,027
666
World Tour
[ "graphs", "shortest paths" ]
null
null
A famous sculptor Cicasso goes to a world tour! Well, it is not actually a world-wide. But not everyone should have the opportunity to see works of sculptor, shouldn't he? Otherwise there will be no any exclusivity. So Cicasso will entirely hold the world tour in his native country β€” Berland. Cicasso is very devoted ...
In the first line there is a pair of integers *n* and *m* (4<=≀<=*n*<=≀<=3000,<=3<=≀<=*m*<=≀<=5000) β€” a number of cities and one-way roads in Berland. Each of the next *m* lines contains a pair of integers *u**i*,<=*v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*) β€” a one-way road from the city *u**i* to the city *v**i*. Note th...
Print four integers β€” numbers of cities which Cicasso will visit according to optimal choice of the route. Numbers of cities should be printed in the order that Cicasso will visit them. If there are multiple solutions, print any of them.
[ "8 9\n1 2\n2 3\n3 4\n4 1\n4 5\n5 6\n6 7\n7 8\n8 5\n" ]
[ "2 1 8 7\n" ]
Let *d*(*x*, *y*) be the shortest distance between cities *x* and *y*. Then in the example *d*(2, 1) = 3, *d*(1, 8) = 7, *d*(8, 7) = 3. The total distance equals 13.
[]
31
0
0
55,074
677
Vanya and Food Processor
[ "implementation", "math" ]
null
null
Vanya smashes potato in a vertical food processor. At each moment of time the height of the potato in the processor doesn't exceed *h* and the processor smashes *k* centimeters of potato each second. If there are less than *k* centimeters remaining, than during this second processor smashes all the remaining potato. V...
The first line of the input contains integers *n*, *h* and *k* (1<=≀<=*n*<=≀<=100<=000,<=1<=≀<=*k*<=≀<=*h*<=≀<=109)Β β€” the number of pieces of potato, the height of the food processor and the amount of potato being smashed each second, respectively. The second line contains *n* integers *a**i* (1<=≀<=*a**i*<=≀<=*h*)Β β€” ...
Print a single integerΒ β€” the number of seconds required to smash all the potatoes following the process described in the problem statement.
[ "5 6 3\n5 4 3 2 1\n", "5 6 3\n5 5 5 5 5\n", "5 6 3\n1 2 1 1 1\n" ]
[ "5\n", "10\n", "2\n" ]
Consider the first sample. 1. First Vanya puts the piece of potato of height 5 into processor. At the end of the second there is only amount of height 2 remaining inside. 1. Now Vanya puts the piece of potato of height 4. At the end of the second there is amount of height 3 remaining. 1. Vanya puts the piece of he...
[ { "input": "5 6 3\n5 4 3 2 1", "output": "5" }, { "input": "5 6 3\n5 5 5 5 5", "output": "10" }, { "input": "5 6 3\n1 2 1 1 1", "output": "2" }, { "input": "10 100 80\n76 75 73 71 76 74 73 70 78 75", "output": "10" }, { "input": "10 100 88\n11 23 69 6 71 15 25 1 4...
139
13,619,200
3
55,096
37
Lesson Timetable
[ "combinatorics", "dp", "math" ]
D. Lesson Timetable
1
256
When Petya has free from computer games time, he attends university classes. Every day the lessons on Petya’s faculty consist of two double classes. The floor where the lessons take place is a long corridor with *M* classrooms numbered from 1 to *M*, situated along it. All the students of Petya’s year are divided into...
The first line contains one integer *M* (1<=≀<=*M*<=≀<=100) β€” the number of classrooms. The second line contains *M* space-separated integers β€” *X**i* (0<=≀<=*X**i*<=≀<=100) the amount of groups present in classroom *i* during the first lesson. The third line contains *M* space-separated integers β€” *Y**i* (0<=≀<=*Y**...
In the single line output the answer to the problem modulo 109<=+<=7.
[ "3\n1 1 1\n1 2 3\n", "3\n1 1 1\n1 1 1\n" ]
[ "36\n", "6\n" ]
In the second sample test the first and the second lessons of each group must take place in the same classroom, that’s why the timetables will only be different in the rearrangement of the classrooms’ numbers for each group, e.g. 3! = 6.
[ { "input": "3\n1 1 1\n1 2 3", "output": "36" }, { "input": "3\n1 1 1\n1 1 1", "output": "6" }, { "input": "3\n2 1 1\n5 1 2", "output": "72" }, { "input": "5\n2 1 1 1 1\n5 3 1 1 3", "output": "49320" }, { "input": "5\n1 3 15 3 18\n2 6 18 5 19", "output": "92148...
1,000
31,744,000
0
55,115
183
Headquarters
[ "constructive algorithms", "math" ]
null
null
Sensation, sensation in the two-dimensional kingdom! The police have caught a highly dangerous outlaw, member of the notorious "Pihters" gang. The law department states that the outlaw was driving from the gang's headquarters in his car when he crashed into an ice cream stall. The stall, the car, and the headquarters e...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=2Β·105) β€” the number of the car's movements from the headquarters to the stall. Each of the following *n* lines describes the car's possible movements. It is guaranteed that each possible movement is one of the following strings: "UL", "UR", "DL", "DR" or "ULD...
Print a single integer β€” the number of different possible locations of the gang's headquarters.
[ "3\nUR\nUL\nULDR\n", "2\nDR\nDL\n" ]
[ "9\n", "4\n" ]
The figure below shows the nine possible positions of the gang headquarters from the first sample: For example, the following movements can get the car from point (1, 0) to point (0, 0):
[ { "input": "3\nUR\nUL\nULDR", "output": "9" }, { "input": "2\nDR\nDL", "output": "4" }, { "input": "4\nUL\nUR\nDR\nDL", "output": "9" }, { "input": "10\nUL\nUL\nUL\nUL\nUL\nUL\nUL\nUL\nUL\nUL", "output": "11" }, { "input": "6\nUL\nDL\nDL\nUL\nUL\nDL", "output"...
778
0
3
55,169
1,005
Median on Segments (Permutations Edition)
[ "sortings" ]
null
null
You are given a permutation $p_1, p_2, \dots, p_n$. A permutation of length $n$ is a sequence such that each integer between $1$ and $n$ occurs exactly once in the sequence. Find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of the median of $p_l, p_{l+1}, \dots, p_r$ is exactly t...
The first line contains integers $n$ and $m$ ($1 \le n \le 2\cdot10^5$, $1 \le m \le n$) β€” the length of the given sequence and the required value of the median. The second line contains a permutation $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$). Each integer between $1$ and $n$ occurs in $p$ exactly once.
Print the required number.
[ "5 4\n2 4 5 3 1\n", "5 5\n1 2 3 4 5\n", "15 8\n1 15 2 14 3 13 4 8 12 5 11 6 10 7 9\n" ]
[ "4\n", "1\n", "48\n" ]
In the first example, the suitable pairs of indices are: $(1, 3)$, $(2, 2)$, $(2, 3)$ and $(2, 4)$.
[ { "input": "5 4\n2 4 5 3 1", "output": "4" }, { "input": "5 5\n1 2 3 4 5", "output": "1" }, { "input": "15 8\n1 15 2 14 3 13 4 8 12 5 11 6 10 7 9", "output": "48" }, { "input": "1 1\n1", "output": "1" }, { "input": "2 1\n1 2", "output": "2" }, { "input...
3,000
13,107,200
0
55,226
528
Fuzzy Search
[ "bitmasks", "brute force", "fft" ]
null
null
Leonid works for a small and promising start-up that works on decoding the human genome. His duties include solving complex problems of finding certain patterns in long strings consisting of letters 'A', 'T', 'G' and 'C'. Let's consider the following scenario. There is a fragment of a human DNA chain, recorded as a st...
The first line contains three integers |*S*|,<=|*T*|,<=*k* (1<=≀<=|*T*|<=≀<=|*S*|<=≀<=200<=000, 0<=≀<=*k*<=≀<=200<=000) β€” the lengths of strings *S* and *T* and the error threshold. The second line contains string *S*. The third line contains string *T*. Both strings consist only of uppercase letters 'A', 'T', 'G' a...
Print a single number β€” the number of occurrences of *T* in *S* with the error threshold *k* by the given definition.
[ "10 4 1\nAGCAATTCAT\nACAT\n" ]
[ "3\n" ]
If you happen to know about the structure of the human genome a little more than the author of the problem, and you are not impressed with Leonid's original approach, do not take everything described above seriously.
[ { "input": "10 4 1\nAGCAATTCAT\nACAT", "output": "3" }, { "input": "1 1 0\nA\nA", "output": "1" }, { "input": "1 1 0\nA\nT", "output": "0" }, { "input": "1 1 1\nA\nT", "output": "0" }, { "input": "5 3 0\nATATA\nATA", "output": "2" }, { "input": "5 3 1\...
31
0
0
55,233
767
Change-free
[ "greedy" ]
null
null
Student Arseny likes to plan his life for *n* days ahead. He visits a canteen every day and he has already decided what he will order in each of the following *n* days. Prices in the canteen do not change and that means Arseny will spend *c**i* rubles during the *i*-th day. There are 1-ruble coins and 100-ruble notes ...
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=105, 0<=≀<=*m*<=≀<=109)Β β€” the amount of days Arseny planned his actions for and the amount of coins he currently has. The second line contains a sequence of integers *c*1,<=*c*2,<=...,<=*c**n* (1<=≀<=*c**i*<=≀<=105)Β β€” the amounts of money in rubles which...
In the first line print one integerΒ β€” minimum possible total dissatisfaction of the cashier. Then print *n* lines, the *i*-th of then should contain two numbersΒ β€” the amount of notes and the amount of coins which Arseny should use to pay in the canteen on the *i*-th day. Of course, the total amount of money Arseny gi...
[ "5 42\n117 71 150 243 200\n1 1 1 1 1\n", "3 0\n100 50 50\n1 3 2\n", "5 42\n117 71 150 243 200\n5 4 3 2 1\n" ]
[ "79\n1 17\n1 0\n2 0\n2 43\n2 0\n", "150\n1 0\n1 0\n0 50\n", "230\n1 17\n1 0\n1 50\n3 0\n2 0\n" ]
none
[ { "input": "5 42\n117 71 150 243 200\n1 1 1 1 1", "output": "79\n1 17\n1 0\n2 0\n2 43\n2 0" }, { "input": "3 0\n100 50 50\n1 3 2", "output": "150\n1 0\n1 0\n0 50" }, { "input": "5 42\n117 71 150 243 200\n5 4 3 2 1", "output": "230\n1 17\n1 0\n1 50\n3 0\n2 0" }, { "input": "5 ...
30
0
0
55,325
0
none
[ "none" ]
null
null
There are two decks of cards lying on the table in front of you, some cards in these decks lay face up, some of them lay face down. You want to merge them into one deck in which each card is face down. You're going to do it in two stages. The first stage is to merge the two decks in such a way that the relative order ...
The first input line contains a single integer *n* β€” the number of cards in the first deck (1<=≀<=*n*<=≀<=105). The second input line contains *n* integers, separated by single spaces *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=1). Value *a**i* equals 0, if the *i*-th card is lying face down, and 1, if the card is ly...
In the first line print *n*<=+<=*m* space-separated integers β€” the numbers of the cards in the order, in which they will lie after the first stage. List the cards from top to bottom. The cards from the first deck should match their indexes from 1 to *n* in the order from top to bottom. The cards from the second deck sh...
[ "3\n1 0 1\n4\n1 1 1 1\n", "5\n1 1 1 1 1\n5\n0 1 0 1 0\n" ]
[ "1 4 5 6 7 2 3\n3\n5 6 7\n", "6 1 2 3 4 5 7 8 9 10\n4\n1 7 8 9\n" ]
none
[]
92
0
0
55,380
115
Linear Kingdom Races
[ "data structures", "dp" ]
null
null
You are a car race organizer and would like to arrange some races in Linear Kingdom. Linear Kingdom has *n* consecutive roads spanning from left to right. The roads are numbered from 1 to *n* from left to right, thus the roads follow in the order of their numbers' increasing. There will be several races that may be he...
The first line contains two single-space separated integers, *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=2Β·105), denoting the number of roads and the number of races, respectively. Then *n* lines follow, each line will contain a single non-negative integer not exceeding 109 denoting the cost to repair a road. The costs are given...
Print a single integer denoting the maximum possible profit you can gain. Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is recommended to use cin, cout stream (also you may use %I64d specificator).
[ "7 4\n3\n2\n3\n2\n1\n2\n3\n1 2 5\n2 3 5\n3 5 3\n7 7 5\n", "2 1\n0\n3\n1 2 5\n", "3 1\n10\n10\n10\n1 3 10\n" ]
[ "4\n", "2\n", "0\n" ]
In the first sample the optimal solution is to repair roads 1, 2, 3, and 7. Three races will take place which nets you 15. The road repair costs 11, hence your profit is 4.
[ { "input": "7 4\n3\n2\n3\n2\n1\n2\n3\n1 2 5\n2 3 5\n3 5 3\n7 7 5", "output": "4" }, { "input": "2 1\n0\n3\n1 2 5", "output": "2" }, { "input": "3 1\n10\n10\n10\n1 3 10", "output": "0" }, { "input": "3 2\n10\n10\n12\n1 2 25\n2 3 5", "output": "5" }, { "input": "7 5...
31
0
0
55,508
894
Ralph and Mushrooms
[ "dp", "graphs" ]
null
null
Ralph is going to collect mushrooms in the Mushroom Forest. There are *m* directed paths connecting *n* trees in the Mushroom Forest. On each path grow some mushrooms. When Ralph passes a path, he collects all the mushrooms on the path. The Mushroom Forest has a magical fertile ground where mushrooms grow at a fantas...
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=106, 0<=≀<=*m*<=≀<=106), representing the number of trees and the number of directed paths in the Mushroom Forest, respectively. Each of the following *m* lines contains three integers *x*, *y* and *w* (1<=≀<=*x*,<=*y*<=≀<=*n*, 0<=≀<=*w*<=≀<=108), denotin...
Print an integer denoting the maximum number of the mushrooms Ralph can collect during his route.
[ "2 2\n1 2 4\n2 1 4\n1\n", "3 3\n1 2 4\n2 3 3\n1 3 8\n1\n" ]
[ "16", "8" ]
In the first sample Ralph can pass three times on the circle and collect 4 + 4 + 3 + 3 + 1 + 1 = 16 mushrooms. After that there will be no mushrooms for Ralph to collect. In the second sample, Ralph can go to tree 3 and collect 8 mushrooms on the path from tree 1 to tree 3.
[ { "input": "2 2\n1 2 4\n2 1 4\n1", "output": "16" }, { "input": "3 3\n1 2 4\n2 3 3\n1 3 8\n1", "output": "8" }, { "input": "1 0\n1", "output": "0" } ]
732
26,009,600
-1
55,535
492
Vanya and Computer Game
[ "binary search", "implementation", "math", "sortings" ]
null
null
Vanya and his friend Vova play a computer game where they need to destroy *n* monsters to pass a level. Vanya's character performs attack with frequency *x* hits per second and Vova's character performs attack with frequency *y* hits per second. Each character spends fixed time to raise a weapon and then he hits (the t...
The first line contains three integers *n*,*x*,*y* (1<=≀<=*n*<=≀<=105, 1<=≀<=*x*,<=*y*<=≀<=106) β€” the number of monsters, the frequency of Vanya's and Vova's attack, correspondingly. Next *n* lines contain integers *a**i* (1<=≀<=*a**i*<=≀<=109)Β β€” the number of hits needed do destroy the *i*-th monster.
Print *n* lines. In the *i*-th line print word "Vanya", if the last hit on the *i*-th monster was performed by Vanya, "Vova", if Vova performed the last hit, or "Both", if both boys performed it at the same time.
[ "4 3 2\n1\n2\n3\n4\n", "2 1 1\n1\n2\n" ]
[ "Vanya\nVova\nVanya\nBoth\n", "Both\nBoth\n" ]
In the first sample Vanya makes the first hit at time 1 / 3, Vova makes the second hit at time 1 / 2, Vanya makes the third hit at time 2 / 3, and both boys make the fourth and fifth hit simultaneously at the time 1. In the second sample Vanya and Vova make the first and second hit simultaneously at time 1.
[ { "input": "4 3 2\n1\n2\n3\n4", "output": "Vanya\nVova\nVanya\nBoth" }, { "input": "2 1 1\n1\n2", "output": "Both\nBoth" }, { "input": "7 5 20\n26\n27\n28\n29\n30\n31\n32", "output": "Vova\nVova\nVova\nBoth\nBoth\nVova\nVova" }, { "input": "10 10 1\n1\n2\n3\n4\n5\n6\n7\n8\n9\...
31
0
0
55,628
0
none
[ "none" ]
null
null
It's well-known that blog posts are an important part of Codeforces platform. Every blog post has a global characteristic changing over timeΒ β€” its community rating. A newly created blog post's community rating is 0. Codeforces users may visit the blog post page and rate it, changing its community rating by +1 or -1. C...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=5Β·105)Β β€” the number of Codeforces users. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=5Β·105<=≀<=*a**i*<=≀<=5Β·105)Β β€” estimated blog post ratings for users in order from 1 to *n*.
For each *k* from 1 to *n*, output a single integer equal to the maximum possible community rating of the blog post after users with indices from 1 to *k*, in some order, visit the blog post page, rate the blog post, and close the page.
[ "4\n2 0 2 2\n", "7\n2 -3 -2 5 0 -3 1\n" ]
[ "1\n1\n2\n2\n", "1\n0\n-1\n0\n1\n1\n2\n" ]
none
[]
30
0
0
55,735
489
SwapSort
[ "greedy", "implementation", "sortings" ]
null
null
In this problem your goal is to sort an array consisting of *n* integers in at most *n* swaps. For the given array find the sequence of swaps that makes the array sorted in the non-descending order. Swaps are performed consecutively, one after another. Note that in this problem you do not have to minimize the number o...
The first line of the input contains integer *n* (1<=≀<=*n*<=≀<=3000) β€” the number of array elements. The second line contains elements of array: *a*0,<=*a*1,<=...,<=*a**n*<=-<=1 (<=-<=109<=≀<=*a**i*<=≀<=109), where *a**i* is the *i*-th element of the array. The elements are numerated from 0 to *n*<=-<=1 from left to r...
In the first line print *k* (0<=≀<=*k*<=≀<=*n*) β€” the number of swaps. Next *k* lines must contain the descriptions of the *k* swaps, one per line. Each swap should be printed as a pair of integers *i*, *j* (0<=≀<=*i*,<=*j*<=≀<=*n*<=-<=1), representing the swap of elements *a**i* and *a**j*. You can print indices in th...
[ "5\n5 2 5 1 4\n", "6\n10 20 20 40 60 60\n", "2\n101 100\n" ]
[ "2\n0 3\n4 2\n", "0\n", "1\n0 1\n" ]
none
[ { "input": "5\n5 2 5 1 4", "output": "2\n0 3\n4 2" }, { "input": "6\n10 20 20 40 60 60", "output": "0" }, { "input": "2\n101 100", "output": "1\n0 1" }, { "input": "1\n1000", "output": "0" }, { "input": "2\n1000000000 -1000000000", "output": "1\n0 1" }, { ...
857
204,800
3
55,816
478
Red-Green Towers
[ "dp" ]
null
null
There are *r* red and *g* green blocks for construction of the red-green tower. Red-green tower can be built following next rules: - Red-green tower is consisting of some number of levels; - Let the red-green tower consist of *n* levels, then the first level of this tower should consist of *n* blocks, second level β€”...
The only line of input contains two integers *r* and *g*, separated by a single space β€” the number of available red and green blocks respectively (0<=≀<=*r*,<=*g*<=≀<=2Β·105, *r*<=+<=*g*<=β‰₯<=1).
Output the only integer β€” the number of different possible red-green towers of height *h* moduloΒ 109<=+<=7.
[ "4 6\n", "9 7\n", "1 1\n" ]
[ "2\n", "6\n", "2\n" ]
The image in the problem statement shows all possible red-green towers for the first sample.
[ { "input": "4 6", "output": "2" }, { "input": "9 7", "output": "6" }, { "input": "1 1", "output": "2" }, { "input": "3 3", "output": "2" }, { "input": "2 19", "output": "1" }, { "input": "18 3", "output": "2" }, { "input": "100000 1", "...
46
0
0
55,833
0
none
[ "none" ]
null
null
There are *n* animals in the queue to Dr. Dolittle. When an animal comes into the office, the doctor examines him, gives prescriptions, appoints tests and may appoint extra examination. Doc knows all the forest animals perfectly well and therefore knows exactly that the animal number *i* in the queue will have to visit...
The first line of input data contains two space-separated integers *n* and *k* (1<=≀<=*n*<=≀<=105, 0<=≀<=*k*<=≀<=1014). In the second line are given space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109). Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommen...
If the doctor will overall carry out less than *k* examinations, print a single number "-1" (without quotes). Otherwise, print the sequence of numbers β€” number of animals in the order in which they stand in the queue. Note that this sequence may be empty. This case is present in pretests. You can just print nothing o...
[ "3 3\n1 2 1\n", "4 10\n3 3 2 1\n", "7 10\n1 3 3 1 2 3 1\n" ]
[ "2 ", "-1\n", "6 2 3 " ]
In the first sample test: - Before examination: {1, 2, 3} - After the first examination: {2, 3} - After the second examination: {3, 2} - After the third examination: {2} In the second sample test: - Before examination: {1, 2, 3, 4, 5, 6, 7} - After the first examination: {2, 3, 4, 5, 6, 7} - After the second ...
[ { "input": "3 3\n1 2 1", "output": "2 " }, { "input": "4 10\n3 3 2 1", "output": "-1" }, { "input": "7 10\n1 3 3 1 2 3 1", "output": "6 2 3 " }, { "input": "1 0\n1", "output": "1 " }, { "input": "6 101\n9 78 54 62 2 91", "output": "4 6 2 3 " }, { "inpu...
60
0
0
55,898
474
Captain Marmot
[ "brute force", "geometry" ]
null
null
Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has *n* regiments, each consisting of 4 moles. Initially, each mole *i* (1<=≀<=*i*<=≀<=4*n*) is placed at some position (*x**i*,<=*y**i*) in the Cartesian plane. Captain Marmot wants to move some moles to m...
The first line contains one integer *n* (1<=≀<=*n*<=≀<=100), the number of regiments. The next 4*n* lines contain 4 integers *x**i*, *y**i*, *a**i*, *b**i* (<=-<=104<=≀<=*x**i*,<=*y**i*,<=*a**i*,<=*b**i*<=≀<=104).
Print *n* lines to the standard output. If the regiment *i* can be made compact, the *i*-th line should contain one integer, the minimal number of required moves. Otherwise, on the *i*-th line print "-1" (without quotes).
[ "4\n1 1 0 0\n-1 1 0 0\n-1 1 0 0\n1 -1 0 0\n1 1 0 0\n-2 1 0 0\n-1 1 0 0\n1 -1 0 0\n1 1 0 0\n-1 1 0 0\n-1 1 0 0\n-1 1 0 0\n2 2 0 1\n-1 0 0 -2\n3 0 0 -2\n-1 1 -2 0\n" ]
[ "1\n-1\n3\n3\n" ]
In the first regiment we can move once the second or the third mole. We can't make the second regiment compact. In the third regiment, from the last 3 moles we can move once one and twice another one. In the fourth regiment, we can move twice the first mole and once the third mole.
[ { "input": "4\n1 1 0 0\n-1 1 0 0\n-1 1 0 0\n1 -1 0 0\n1 1 0 0\n-2 1 0 0\n-1 1 0 0\n1 -1 0 0\n1 1 0 0\n-1 1 0 0\n-1 1 0 0\n-1 1 0 0\n2 2 0 1\n-1 0 0 -2\n3 0 0 -2\n-1 1 -2 0", "output": "1\n-1\n3\n3" }, { "input": "3\n-2248 6528 -2144 6181\n-2245 6663 -2100 7054\n-4378 7068 -4061 7516\n-4274 6026 -391...
187
4,198,400
3
56,162
212
Privatization
[ "flows", "graphs" ]
null
null
There is a developed network of flights between Berland and Beerland. All of them belong to the Berland state company BerAvia. Each flight connects some Berland city with some Beerland city. For each flight airplanes fly in both directions. Changes are coming to Berland β€” the state decided to privatize BerAvia, namely...
The first input line contains four integers *n*,<=*m*,<=*k* and *t* (1<=≀<=*n*,<=*m*,<=*t*<=≀<=200;1<=≀<=*k*<=≀<=5000), where *n*,<=*m* are the numbers of cities in Berland and Beerland, correspondingly, *k* is the number of flights between them, and *t* is the number of private companies. Next *k* lines describe the f...
Print the unevenness of the sought plan on the first line. On the second line print a sequence of *k* integers *c*1,<=*c*2,<=...,<=*c**k* (1<=≀<=*c**i*<=≀<=*t*), where *c**i* is the index of the company that should buy the *i*-th flight. Assume that the flights are indexed from 1 to *k* in the order they appear in the ...
[ "3 5 8 2\n1 4\n1 3\n3 3\n1 2\n1 1\n2 1\n1 5\n2 2\n" ]
[ "4\n2 1 2 1 2 1 2 2 " ]
none
[]
92
0
0
56,170
605
Freelancer's Dreams
[ "geometry" ]
null
null
Mikhail the Freelancer dreams of two things: to become a cool programmer and to buy a flat in Moscow. To become a cool programmer, he needs at least *p* experience points, and a desired flat in Moscow costs *q* dollars. Mikhail is determined to follow his dreams and registered at a freelance site. He has suggestions t...
The first line of the input contains three integers *n*, *p* and *q* (1<=≀<=*n*<=≀<=100<=000,<=1<=≀<=*p*,<=*q*<=≀<=1<=000<=000)Β β€” the number of projects and the required number of experience and money. Each of the next *n* lines contains two integers *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=1<=000<=000)Β β€” the dail...
Print a real valueΒ β€” the minimum number of days Mikhail needs to get the required amount of experience and money. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will...
[ "3 20 20\n6 2\n1 3\n2 6\n", "4 1 1\n2 3\n3 2\n2 3\n3 2\n" ]
[ "5.000000000000000\n", "0.400000000000000\n" ]
First sample corresponds to the example in the problem statement.
[ { "input": "3 20 20\n6 2\n1 3\n2 6", "output": "5.000000000000000" }, { "input": "4 1 1\n2 3\n3 2\n2 3\n3 2", "output": "0.400000000000000" }, { "input": "3 12 12\n5 1\n2 2\n1 5", "output": "4.000000000000000" }, { "input": "3 12 12\n5 1\n4 4\n1 5", "output": "3.000000000...
0
0
-1
56,264
818
Four Melodies
[ "flows", "graphs" ]
null
null
Author note: I think some of you might remember the problem "Two Melodies" from Eductational Codeforces Round 22. Now it's time to make it a bit more difficult! Alice is a composer, and recently she had recorded two tracks that became very popular. Now she has got a lot of fans who are waiting for new tracks. This t...
The first line contains one integer number *n* (4<=≀<=*n*<=≀<=3000). The second line contains *n* integer numbers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=105) β€” notes written on a sheet.
Print maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody.
[ "5\n1 3 5 7 9\n", "5\n1 3 5 7 2\n" ]
[ "4\n", "5\n" ]
In the first example it is possible to compose 4 melodies by choosing any 4 notes (and each melody will consist of only one note). In the second example it is possible to compose one melody with 2 notes β€” {1, 2}. Remaining notes are used in other three melodies (one note per each melody).
[ { "input": "5\n1 3 5 7 9", "output": "4" }, { "input": "5\n1 3 5 7 2", "output": "5" }, { "input": "4\n1 3 5 7", "output": "4" }, { "input": "4\n1 1 1 1", "output": "4" }, { "input": "4\n1 1 2 1", "output": "4" }, { "input": "4\n3 2 3 1", "output":...
31
409,600
-1
56,328
101
Vectors
[ "implementation", "math" ]
C. Vectors
1
256
At a geometry lesson Gerald was given a task: to get vector *B* out of vector *A*. Besides, the teacher permitted him to perform the following operations with vector *А*: - Turn the vector by 90 degrees clockwise.- Add to the vector a certain vector *C*. Operations could be performed in any order any number of times....
The first line contains integers *x*1 ΠΈ *y*1 β€” the coordinates of the vector *A* (<=-<=108<=≀<=*x*1,<=*y*1<=≀<=108). The second and the third line contain in the similar manner vectors *B* and *C* (their coordinates are integers; their absolute value does not exceed 108).
Print "YES" (without the quotes) if it is possible to get vector *B* using the given operations. Otherwise print "NO" (without the quotes).
[ "0 0\n1 1\n0 1\n", "0 0\n1 1\n1 1\n", "0 0\n1 1\n2 2\n" ]
[ "YES\n", "YES\n", "NO\n" ]
none
[ { "input": "0 0\n1 1\n0 1", "output": "YES" }, { "input": "0 0\n1 1\n1 1", "output": "YES" }, { "input": "0 0\n1 1\n2 2", "output": "NO" }, { "input": "2 3\n2 3\n0 0", "output": "YES" }, { "input": "-4 -2\n0 0\n-2 -1", "output": "YES" }, { "input": "-1...
62
0
0
56,442
360
Levko and Strings
[ "combinatorics", "dp" ]
null
null
Levko loves strings of length *n*, consisting of lowercase English letters, very much. He has one such string *s*. For each string *t* of length *n*, Levko defines its beauty relative to *s* as the number of pairs of indexes *i*, *j* (1<=≀<=*i*<=≀<=*j*<=≀<=*n*), such that substring *t*[*i*..*j*] is lexicographically la...
The first line contains two integers *n* and *k* (1<=≀<=*n*<=≀<=2000, 0<=≀<=*k*<=≀<=2000). The second line contains a non-empty string *s* of length *n*. String *s* consists only of lowercase English letters.
Print a single number β€” the answer to the problem modulo 1000000007 (109<=+<=7).
[ "2 2\nyz\n", "2 3\nyx\n", "4 7\nabcd\n" ]
[ "26\n", "2\n", "21962\n" ]
none
[]
31
0
0
56,521
895
Eyes Closed
[ "data structures", "probabilities" ]
null
null
Vasya and Petya were tired of studying so they decided to play a game. Before the game begins Vasya looks at array *a* consisting of *n* integers. As soon as he remembers all elements of *a* the game begins. Vasya closes his eyes and Petya does *q* actions of one of two types: 1) Petya says 4 integers *l*1,<=*r*1,<=*l...
The first line contains two integers *n*,<=*q* (2<=≀<=*n*<=≀<=105,<=1<=≀<=*q*<=≀<=105) Β β€” the number of elements in the array and the number of queries you need to handle. The second line contains *n* integers *a**i* (1<=≀<=*a**i*<=≀<=109) Β β€” elements of the array. The next *q* lines contain Petya's actions of type 1...
For each type 2 query print one real numberΒ β€” the mathematical expectation of the sum of elements in the segment. Your answer will be considered correct if its absolute or relative error doesn't exceed 10<=-<=4 Β β€” formally, the answer is correct if where *x* is jury's answer and *y* is yours.
[ "4 4\n1 1 2 2\n1 2 2 3 3\n2 1 2\n1 1 2 3 4\n2 1 2\n", "10 5\n1 1 1 1 1 2 2 2 2 2\n1 1 5 6 10\n2 1 5\n1 1 5 6 10\n1 1 5 6 10\n2 6 10\n", "10 10\n1 2 3 4 5 6 7 8 9 10\n1 1 5 6 10\n1 1 5 6 10\n2 1 5\n1 1 3 6 9\n2 1 3\n1 5 7 8 10\n1 1 1 10 10\n2 1 5\n2 7 10\n2 1 10\n" ]
[ "3.0000000\n3.0000000\n", "6.0000000\n8.0400000\n", "23.0000000\n14.0000000\n28.0133333\n21.5733333\n55.0000000\n" ]
none
[]
2,500
49,561,600
0
56,548
519
A and B and Interesting Substrings
[ "data structures", "dp", "two pointers" ]
null
null
A and B are preparing themselves for programming contests. After several years of doing sports programming and solving many problems that require calculating all sorts of abstract objects, A and B also developed rather peculiar tastes. A likes lowercase letters of the Latin alphabet. He has assigned to each letter a ...
The first line contains 26 integers *x**a*,<=*x**b*,<=...,<=*x**z* (<=-<=105<=≀<=*x**i*<=≀<=105) β€” the value assigned to letters *a*,<=*b*,<=*c*,<=...,<=*z* respectively. The second line contains string *s* of length between 1 and 105 characters, consisting of Lating lowercase lettersβ€” the string for which you need to...
Print the answer to the problem.
[ "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\nxabcab\n", "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\naaa\n" ]
[ "2\n", "2\n" ]
In the first sample test strings satisfying the condition above are *abca* and *bcab*. In the second sample test strings satisfying the condition above are two occurences of *aa*.
[ { "input": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\nxabcab", "output": "2" }, { "input": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\naaa", "output": "2" }, { "input": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\naabbccdd", "output": "4" }, { ...
62
0
0
56,797
455
Serega and Fun
[ "data structures" ]
null
null
Serega loves fun. However, everyone has fun in the unique manner. Serega has fun by solving query problems. One day Fedor came up with such a problem. You are given an array *a* consisting of *n* positive integers and queries to it. The queries can be of two types: 1. Make a unit cyclic shift to the right on the seg...
The first line contains integer *n* (1<=≀<=*n*<=≀<=105) β€” the number of elements of the array. The second line contains *n* integers *a*[1],<=*a*[2],<=...,<=*a*[*n*] (1<=≀<=*a*[*i*]<=≀<=*n*). The third line contains a single integer *q* (1<=≀<=*q*<=≀<=105) β€” the number of queries. The next *q* lines contain the querie...
For each query of the 2-nd type print the answer on a single line.
[ "7\n6 6 2 7 4 2 5\n7\n1 3 6\n2 2 4 2\n2 2 4 7\n2 2 2 5\n1 2 6\n1 1 4\n2 1 7 3\n", "8\n8 4 2 2 7 7 8 8\n8\n1 8 8\n2 8 1 7\n1 8 1\n1 7 3\n2 8 8 3\n1 1 4\n1 2 7\n1 4 5\n" ]
[ "2\n1\n0\n0\n", "2\n0\n" ]
none
[ { "input": "7\n6 6 2 7 4 2 5\n7\n1 3 6\n2 2 4 2\n2 2 4 7\n2 2 2 5\n1 2 6\n1 1 4\n2 1 7 3", "output": "2\n1\n0\n0" }, { "input": "8\n8 4 2 2 7 7 8 8\n8\n1 8 8\n2 8 1 7\n1 8 1\n1 7 3\n2 8 8 3\n1 1 4\n1 2 7\n1 4 5", "output": "2\n0" }, { "input": "10\n7 2 3 4 3 2 4 4 9 1\n10\n1 4 5\n1 1 6\n...
77
5,836,800
-1
56,898
448
Divisors
[ "brute force", "dfs and similar", "implementation", "number theory" ]
null
null
Bizon the Champion isn't just friendly, he also is a rigorous coder. Let's define function *f*(*a*), where *a* is a sequence of integers. Function *f*(*a*) returns the following sequence: first all divisors of *a*1 go in the increasing order, then all divisors of *a*2 go in the increasing order, and so on till the las...
A single line contains two space-separated integers β€” *X* (1<=≀<=*X*<=≀<=1012) and *k* (0<=≀<=*k*<=≀<=1018).
Print the elements of the sequence *X**k* in a single line, separated by a space. If the number of elements exceeds 105, then print only the first 105 elements.
[ "6 1\n", "4 2\n", "10 3\n" ]
[ "1 2 3 6 \n", "1 1 2 1 2 4 \n", "1 1 1 2 1 1 5 1 1 2 1 5 1 2 5 10 \n" ]
none
[ { "input": "6 1", "output": "1 2 3 6 " }, { "input": "4 2", "output": "1 1 2 1 2 4 " }, { "input": "10 3", "output": "1 1 1 2 1 1 5 1 1 2 1 5 1 2 5 10 " }, { "input": "10 0", "output": "10 " }, { "input": "1 1", "output": "1 " }, { "input": "1 10000000...
155
7,168,000
-1
56,998
89
Space mines
[ "geometry" ]
D. Space mines
2
256
Once upon a time in the galaxy of far, far away... Darth Wader found out the location of a rebels' base. Now he is going to destroy the base (and the whole planet that the base is located at), using the Death Star. When the rebels learnt that the Death Star was coming, they decided to use their new secret weapon β€” sp...
The first input data line contains 7 integers *A**x*,<=*A**y*,<=*A**z*,<=*v**x*,<=*v**y*,<=*v**z*,<=*R*. They are the Death Star's initial position, the direction of its movement, and its radius (<=-<=10<=≀<=*v**x*,<=*v**y*,<=*v**z*<=≀<=10, |*v*|<=&gt;<=0, 0<=&lt;<=*R*<=≀<=100). The second line contains an integer *n*...
If the rebels will succeed in stopping the Death Star using space mines, print the time from the moment the Death Star was noticed to the blast. If the Death Star will not touch a mine, print "-1" (without quotes). For the answer the absolute or relative error of 10<=-<=6 is acceptable.
[ "0 0 0 1 0 0 5\n2\n10 8 0 2 2\n0 -3 0\n2 2 0\n20 0 0 4 3\n2 4 0\n-4 3 0\n1 -5 0\n", "8 8 4 4 4 2 6\n1\n-2 -2 -1 3 0\n", "30 30 2 1 2 1 20\n3\n0 0 40 5 1\n1 4 4\n-10 -40 -5 7 0\n100 200 95 8 1\n-10 0 0\n" ]
[ "10.0000000000", "-1", "74.6757620881" ]
none
[ { "input": "0 0 0 1 0 0 5\n2\n10 8 0 2 2\n0 -3 0\n2 2 0\n20 0 0 4 3\n2 4 0\n-4 3 0\n1 -5 0", "output": "10.0000000000" }, { "input": "8 8 4 4 4 2 6\n1\n-2 -2 -1 3 0", "output": "-1" }, { "input": "30 30 2 1 2 1 20\n3\n0 0 40 5 1\n1 4 4\n-10 -40 -5 7 0\n100 200 95 8 1\n-10 0 0", "outp...
92
0
0
57,049
317
Princess and Her Shadow
[ "constructive algorithms", "shortest paths" ]
null
null
Princess Vlada enjoys springing in the meadows and walking in the forest. One day β€” wonderful, sunny day β€” during her walk Princess found out with astonishment that her shadow was missing! "Blimey!", β€” she thought and started her search of the shadow in the forest. Normally the Shadow is too lazy and simply sleeps und...
First line of the input contains the coordinates of the characters *v**x*, *v**y*, *s**x*, *s**y* and the number of trees *m* (0<=≀<=*m*<=≀<=400). The following *m* lines contain the coordinates of the trees. All the coordinates are integers between -100 and 100, inclusive. The Princess, The Shadow and the trees are l...
If it is impossible for the Princess to catch the Shadow, print "-1" (without quotes). Otherwise print a sequence of characters "L", "R", "D", "U", corresponding to the Princess's moves, following which she will be able to catch the Shadow at some turn (L β€” move to the left, R β€” to the right, U β€” up, D β€” down; axis *x...
[ "0 0 1 0 1\n0 1\n", "5 0 3 0 8\n2 -1\n2 0\n2 1\n3 -1\n4 1\n4 0\n3 1\n4 -1\n", "3 2 1 1 3\n0 1\n1 0\n0 0\n" ]
[ "LLUR\n", "-1\n", "DLL" ]
Below the pictures for the samples are given (Princess, Shadow and the trees are colored in pink, gray and black correspondingly; the blue dot marks the lattice center). In the first case the Princess may make two left steps, one step upwards and one right step: <img class="tex-graphics" src="https://espresso.codeforc...
[]
92
0
0
57,092
730
Running Over The Bridges
[ "greedy", "implementation", "math" ]
null
null
Polycarp is playing a game called "Running Over The Bridges". In this game he has to run over *n* bridges from the left to the right. Bridges are arranged one after the other, so the *i*-th bridge begins where the (*i*<=-<=1)-th bridge ends. You have the following data about bridges: *l**i* and *t**i* β€” the length of ...
The first line contains two integers *n* and *r* (1<=≀<=*n*<=≀<=2Β·105, 1<=≀<=*r*<=≀<=1012) β€” the number of bridges and the duration of the effect of a magical drink. The second line contains a sequence of integers *l*1,<=*l*2,<=...,<=*l**n* (1<=≀<=*l**i*<=≀<=5Β·106), where *l**i* is equal to the length of the *i*-th br...
The first line of the output should contain *k* β€” the minimal number of drinks which Polycarp has to use, or -1 if there is no solution. If the solution exists and the value of *k* is not greater than 105 then output *k* integers on the next line β€” moments of time from beginning of the game when Polycarp has to use dr...
[ "1 3\n7\n10\n", "3 3\n3 3 3\n3 3 2\n", "3 100000\n5 5 5\n5 7 8\n", "4 1000\n1 2 3 4\n10 9 10 9\n" ]
[ "2\n0 3\n", "-1\n", "1\n0 \n", "0\n\n" ]
In the first case, there is only one bridge and it is clear that Polycarp cannot run over it without magical drinks. So, if he will use one magical drink on start (moment of time 0), and the second one β€” three seconds later (moment of time 3), he will be able to reach the end of the bridge in time. Please note, in this...
[]
46
0
0
57,291
449
Jzzhu and Apples
[ "constructive algorithms", "number theory" ]
null
null
Jzzhu has picked *n* apples from his big apple tree. All the apples are numbered from 1 to *n*. Now he wants to sell them to an apple store. Jzzhu will pack his apples into groups and then sell them. Each group must contain two apples, and the greatest common divisor of numbers of the apples in each group must be gre...
A single integer *n* (1<=≀<=*n*<=≀<=105), the number of the apples.
The first line must contain a single integer *m*, representing the maximum number of groups he can get. Each of the next *m* lines must contain two integers β€” the numbers of apples in the current group. If there are several optimal answers you can print any of them.
[ "6\n", "9\n", "2\n" ]
[ "2\n6 3\n2 4\n", "3\n9 3\n2 4\n6 8\n", "0\n" ]
none
[ { "input": "6", "output": "2\n6 3\n2 4" }, { "input": "9", "output": "3\n9 3\n2 4\n6 8" }, { "input": "2", "output": "0" }, { "input": "10", "output": "4\n2 4\n6 8\n10 5\n9 3" }, { "input": "100", "output": "44\n33 27\n22 11\n25 5\n64 66\n42 44\n31 62\n58 29\n...
546
3,379,200
3
57,451
509
Restoring Numbers
[ "constructive algorithms", "math" ]
null
null
Vasya had two arrays consisting of non-negative integers: *a* of size *n* and *b* of size *m*. Vasya chose a positive integer *k* and created an *n*<=Γ—<=*m* matrix *v* using the following formula: Vasya wrote down matrix *v* on a piece of paper and put it in the table. A year later Vasya was cleaning his table when h...
The first line contains integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=100), separated by a space β€” the number of rows and columns in the found matrix, respectively. The *i*-th of the following lines contains numbers *w**i*,<=1,<=*w**i*,<=2,<=...,<=*w**i*,<=*m* (0<=≀<=*w**i*,<=*j*<=≀<=109), separated by spaces β€” the eleme...
If the matrix *w* could not have been obtained in the manner described above, print "NO" (without quotes) in the single line of output. Otherwise, print four lines. In the first line print "YES" (without quotes). In the second line print an integer *k* (1<=≀<=*k*<=≀<=1018). Note that each element of table *w* should...
[ "2 3\n1 2 3\n2 3 4\n", "2 2\n1 2\n2 0\n", "2 2\n1 2\n2 1\n" ]
[ "YES\n1000000007\n0 1 \n1 2 3 ", "YES\n3\n0 1 \n1 2 ", "NO\n" ]
By <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e4ee2bc16f1508a982cfc739e1c7ddc442223116.png" style="max-width: 100.0%;max-height: 100.0%;"/> we denote the remainder of integer division of *b* by *c*. It is guaranteed that if there exists some set of numbers *k*, *a*<sub class="lower-in...
[ { "input": "2 3\n1 2 3\n2 3 4", "output": "YES\n1000000007\n0 1 \n1 2 3 " }, { "input": "2 2\n1 2\n2 0", "output": "YES\n3\n0 1 \n1 2 " }, { "input": "2 2\n1 2\n2 1", "output": "NO" }, { "input": "2 2\n2 3\n1 2", "output": "YES\n1000000007\n0 1000000006 \n2 3 " }, { ...
46
614,400
3
57,454
17
Balance
[ "dp" ]
C. Balance
3
128
Nick likes strings very much, he likes to rotate them, sort them, rearrange characters within a string... Once he wrote a random string of characters a, b, c on a piece of paper and began to perform the following operations: - to take two adjacent characters and replace the second character with the first one, - to...
The first line contains integer *n* (1<=≀<=*n*<=≀<=150) β€” the length of the given string *s*. Next line contains the given string *s*. The initial string can be balanced as well, in this case it should be counted too. The given string *s* consists only of characters a, b and c.
Output the only number β€” the number of different balanced strings that can be obtained by performing the described operations, perhaps multiple times, on the given string *s*, modulo 51123987.
[ "4\nabca\n", "4\nabbc\n", "2\nab\n" ]
[ "7\n", "3\n", "1\n" ]
In the first sample it is possible to get 51 different strings through the described operations, but only 7 of them are balanced: Β«abcaΒ», Β«bbcaΒ», Β«bccaΒ», Β«bcaaΒ», Β«abccΒ», Β«abbcΒ», Β«aabcΒ». In the second sample: Β«abbcΒ», Β«aabcΒ», Β«abccΒ». In the third sample there is only one balanced string β€” Β«abΒ» itself.
[]
92
0
0
57,638
314
Sereja and Periods
[ "binary search", "dfs and similar", "strings" ]
null
null
Let's introduce the designation , where *x* is a string, *n* is a positive integer and operation "<=+<=" is the string concatenation operation. For example, [*abc*,<=2]<==<=*abcabc*. We'll say that string *s* can be obtained from string *t*, if we can remove some characters from string *t* and obtain string *s*. For e...
The first line contains two integers *b*, *d* (1<=≀<=*b*,<=*d*<=≀<=107). The second line contains string *a*. The third line contains string *c*. The given strings are not empty and consist of lowercase English letters. Their lengths do not exceed 100.
In a single line print an integer β€” the largest number *p*. If the required value of *p* doesn't exist, print 0.
[ "10 3\nabab\nbab\n" ]
[ "3\n" ]
none
[ { "input": "10 3\nabab\nbab", "output": "3" }, { "input": "841 7\nqjqhrksmvedtqldrqgchhsofokfcovut\nqhtmothoulodshrfejterjlguvooccsvqrrdfqfvkqhtecuhhuqhshthrkusrc", "output": "5" }, { "input": "901 8\nkikjbkgkkjeeficiigjjidfhkfdckjdkkbkfhkhdcjidjbfdfkbhbfjeiffkfgcaigck\ngcjifhjjfedbfbdhi...
124
2,252,800
0
57,774
711
Directed Roads
[ "combinatorics", "dfs and similar", "graphs", "math" ]
null
null
ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it consists of *n* towns numbered from 1 to *n*. There are *n* directed roads in the Udayland. *i*-th of them goes from town *i* to some other town *a**i* (*a**i*<=β‰ <=*i*). ZS the Coder can flip the direction of any road in...
The first line of the input contains single integer *n* (2<=≀<=*n*<=≀<=2Β·105)Β β€” the number of towns in Udayland. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=*n*,<=*a**i*<=β‰ <=*i*), *a**i* denotes a road going from town *i* to town *a**i*.
Print a single integerΒ β€” the number of ways to flip some set of the roads so that the resulting whole set of all roads is not confusing. Since this number may be too large, print the answer modulo 109<=+<=7.
[ "3\n2 3 1\n", "4\n2 1 1 1\n", "5\n2 4 2 5 3\n" ]
[ "6\n", "8\n", "28\n" ]
Consider the first sample case. There are 3 towns and 3 roads. The towns are numbered from 1 to 3 and the roads are <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e5b18c46402af724bd3841d549d5d6f52fc16253.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-f...
[ { "input": "3\n2 3 1", "output": "6" }, { "input": "4\n2 1 1 1", "output": "8" }, { "input": "5\n2 4 2 5 3", "output": "28" }, { "input": "4\n2 1 4 3", "output": "4" }, { "input": "7\n2 3 4 1 6 5 4", "output": "56" }, { "input": "20\n2 3 4 5 6 7 8 9 10...
62
7,168,000
0
57,847
311
Interval Cubing
[ "data structures", "math" ]
null
null
While learning Computational Geometry, Tiny is simultaneously learning a useful data structure called segment tree or interval tree. He has scarcely grasped it when comes out a strange problem: Given an integer sequence *a*1,<=*a*2,<=...,<=*a**n*. You should run *q* queries of two types: 1. Given two integers *l* an...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=105), representing the length of the sequence. The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=109). The third line contains an integer *q* (1<=≀<=*q*<=≀<=105), representing the number of queries. Then follow *q* l...
For each 1-type query, print the answer to it per line. You should notice that each printed number should be non-negative and less than 95542721.
[ "8\n1 2 3 4 5 6 7 8\n5\n1 2 5\n2 2 5\n1 2 5\n2 3 6\n1 4 7\n" ]
[ "14\n224\n2215492\n" ]
none
[]
6,000
14,131,200
0
57,854
333
Characteristics of Rectangles
[ "binary search", "bitmasks", "brute force", "implementation", "sortings" ]
null
null
Gerald found a table consisting of *n* rows and *m* columns. As a prominent expert on rectangular tables, he immediately counted the table's properties, that is, the minimum of the numbers in the corners of the table (minimum of four numbers). However, he did not like the final value β€” it seemed to be too small. And to...
The first line contains two space-separated integers *n* and *m* (2<=≀<=*n*,<=*m*<=≀<=1000). The following *n* lines describe the table. The *i*-th of these lines lists the space-separated integers *a**i*,<=1,<=*a**i*,<=2,<=...,<=*a**i*,<=*m* (0<=≀<=*a**i*,<=*j*<=≀<=109) β€” the *m* numbers standing in the *i*-th row of ...
Print the answer to the problem.
[ "2 2\n1 2\n3 4\n", "3 3\n1 0 0\n0 1 1\n1 0 0\n" ]
[ "1\n", "0\n" ]
In the first test case Gerald cannot crop the table β€” table contains only two rows and only two columns. In the second test case if we'll crop the table, the table will contain zero in some corner cell. Also initially it contains two zeros in the corner cells, so the answer is 0.
[ { "input": "2 2\n1 2\n3 4", "output": "1" }, { "input": "3 3\n1 0 0\n0 1 1\n1 0 0", "output": "0" }, { "input": "2 2\n0 0\n0 0", "output": "0" }, { "input": "2 2\n1000000000 1000000000\n1000000000 1000000000", "output": "1000000000" }, { "input": "10 2\n1 20\n19 2...
60
0
0
57,876
630
Game
[ "games", "math" ]
null
null
There is a legend in the IT City college. A student that failed to answer all questions on the game theory exam is given one more chance by his professor. The student has to play a game with the professor. The game is played on a square field consisting of *n*<=Γ—<=*n* cells. Initially all cells are empty. On each turn...
The only line of the input contains one integer *n* (1<=≀<=*n*<=≀<=1018) β€” the size of the field.
Output number 1, if the player making the first turn wins when both players play optimally, otherwise print number 2.
[ "1\n", "2\n" ]
[ "1", "2" ]
none
[ { "input": "1", "output": "1" }, { "input": "2", "output": "2" }, { "input": "3", "output": "1" }, { "input": "4", "output": "2" }, { "input": "5", "output": "1" }, { "input": "1000000000000000000", "output": "2" }, { "input": "999999999999...
0
0
-1
57,885
295
Greg and Friends
[ "combinatorics", "dp", "graphs", "shortest paths" ]
null
null
One day Greg and his friends were walking in the forest. Overall there were *n* people walking, including Greg. Soon he found himself in front of a river. The guys immediately decided to get across the river. Luckily, there was a boat by the river bank, just where the guys were standing. We know that the boat can hold ...
The first line contains two integers *n*, *k* (1<=≀<=*n*<=≀<=50,<=1<=≀<=*k*<=≀<=5000) β€” the number of people, including Greg, and the boat's weight limit. The next line contains *n* integers β€” the people's weights. A person's weight is either 50 kilos or 100 kilos. You can consider Greg and his friends indexed in some...
In the first line print an integer β€” the minimum number of rides. If transporting everyone to the other bank is impossible, print an integer -1. In the second line print the remainder after dividing the number of ways to transport the people in the minimum number of rides by number 1000000007 (109<=+<=7). If transport...
[ "1 50\n50\n", "3 100\n50 50 100\n", "2 50\n50 50\n" ]
[ "1\n1\n", "5\n2\n", "-1\n0\n" ]
In the first test Greg walks alone and consequently, he needs only one ride across the river. In the second test you should follow the plan: 1. transport two 50 kg. people; 1. transport one 50 kg. person back; 1. transport one 100 kg. person; 1. transport one 50 kg. person back; 1. transport two 50 kg. people. ...
[ { "input": "1 50\n50", "output": "1\n1" }, { "input": "3 100\n50 50 100", "output": "5\n2" }, { "input": "2 50\n50 50", "output": "-1\n0" }, { "input": "5 258\n100 100 50 50 50", "output": "3\n72" }, { "input": "8 191\n50 100 50 100 50 100 100 50", "output": "...
0
0
-1
57,911
535
Tavas and Malekas
[ "greedy", "hashing", "string suffix structures", "strings" ]
null
null
Tavas is a strange creature. Usually "zzz" comes out of people's mouth while sleeping, but string *s* of length *n* comes out from Tavas' mouth instead. Today Tavas fell asleep in Malekas' place. While he was sleeping, Malekas did a little process on *s*. Malekas has a favorite string *p*. He determined all positions ...
The first line contains two integers *n* and *m*, the length of *s* and the length of the subsequence Malekas wrote down (1<=≀<=*n*<=≀<=106 and 0<=≀<=*m*<=≀<=*n*<=-<=|*p*|<=+<=1). The second line contains string *p* (1<=≀<=|*p*|<=≀<=*n*). The next line contains *m* space separated integers *y*1,<=*y*2,<=...,<=*y**m*,...
In a single line print the answer modulo 1000<=000<=007.
[ "6 2\nioi\n1 3\n", "5 2\nioi\n1 2\n" ]
[ "26\n", "0\n" ]
In the first sample test all strings of form "ioioi?" where the question mark replaces arbitrary English letter satisfy. Here |*x*| denotes the length of string x. Please note that it's possible that there is no such string (answer is 0).
[ { "input": "6 2\nioi\n1 3", "output": "26" }, { "input": "5 2\nioi\n1 2", "output": "0" }, { "input": "173700 6\nbcabcbcbcbaaacaccaacaccaabacabaacbcacbbccaccbcacbabcaccccccaacacabbbbbacabbaaacbcbbaccaccabbbbaabbacacbabccaabcabbbcacaaccbabbcaaaaaabccbbcabcacbcbcabcbcbbaabacaaccccabacaaacc...
2,000
268,390,400
0
57,956
1,000
One Occurrence
[ "data structures", "divide and conquer" ]
null
null
You are given an array $a$ consisting of $n$ integers, and $q$ queries to it. $i$-th query is denoted by two integers $l_i$ and $r_i$. For each query, you have to find any integer that occurs exactly once in the subarray of $a$ from index $l_i$ to index $r_i$ (a subarray is a contiguous subsegment of an array). For exa...
The first line contains one integer $n$ ($1 \le n \le 5 \cdot 10^5$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 5 \cdot 10^5$). The third line contains one integer $q$ ($1 \le q \le 5 \cdot 10^5$). Then $q$ lines follow, $i$-th line containing two integers $l_i$ and $r_i$ represent...
Answer the queries as follows: If there is no integer such that it occurs in the subarray from index $l_i$ to index $r_i$ exactly once, print $0$. Otherwise print any such integer.
[ "6\n1 1 2 3 2 4\n2\n2 6\n1 2\n" ]
[ "4\n0\n" ]
none
[ { "input": "6\n1 1 2 3 2 4\n2\n2 6\n1 2", "output": "4\n0" }, { "input": "10\n5 9 6 4 8 7 4 9 7 6\n10\n4 9\n4 7\n4 7\n4 8\n9 10\n4 9\n4 8\n1 5\n4 4\n2 5", "output": "8\n8\n8\n8\n6\n8\n8\n8\n4\n9" } ]
3,000
41,062,400
0
58,017
379
New Year Letter
[ "bitmasks", "brute force", "dp" ]
null
null
Many countries have such a New Year or Christmas tradition as writing a letter to Santa including a wish list for presents. Vasya is an ordinary programmer boy. Like all ordinary boys, he is going to write the letter to Santa on the New Year Eve (we Russians actually expect Santa for the New Year, not for Christmas). ...
The first line contains four integers *k*,<=*x*,<=*n*,<=*m* (3<=≀<=*k*<=≀<=50;Β 0<=≀<=*x*<=≀<=109;Β 1<=≀<=*n*,<=*m*<=≀<=100).
In the first line print string *s*1, consisting of *n* uppercase English letters. In the second line print string *s*2, consisting of *m* uppercase English letters. If there are multiple valid strings, print any of them. If the required pair of strings doesn't exist, print "Happy new year!" without the quotes.
[ "3 2 2 2\n", "3 3 2 2\n", "3 0 2 2\n", "4 3 2 1\n", "4 2 2 1\n" ]
[ "AC\nAC\n", "Happy new year!\n", "AA\nAA\n", "Happy new year!\n", "Happy new year!\n" ]
none
[ { "input": "3 2 2 2", "output": "AC\nAC" }, { "input": "3 3 2 2", "output": "Happy new year!" }, { "input": "3 0 2 2", "output": "AA\nAA" }, { "input": "4 3 2 1", "output": "Happy new year!" }, { "input": "4 2 2 1", "output": "Happy new year!" }, { "in...
46
0
0
58,246
101
Castle
[ "dp", "greedy", "probabilities", "sortings", "trees" ]
D. Castle
2
256
Gerald is positioned in an old castle which consists of *n* halls connected with *n*<=-<=1 corridors. It is exactly one way to go from any hall to any other one. Thus, the graph is a tree. Initially, at the moment of time 0, Gerald is positioned in hall 1. Besides, some other hall of the castle contains the treasure Ge...
The first line contains the only integer *n* (2<=≀<=*n*<=≀<=105) β€” the number of halls in the castle. Next *n*<=-<=1 lines each contain three integers. The *i*-th line contains numbers *a**i*, *b**i* and *t**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*, *a**i*<=β‰ <=*b**i*, 1<=≀<=*t**i*<=≀<=1000) β€” the numbers of halls connected wi...
Print the only real number: the sought expectation of time needed to find the treasure. The answer should differ from the right one in no less than 10<=-<=6.
[ "2\n1 2 1\n", "4\n1 3 2\n4 2 1\n3 2 3\n", "5\n1 2 1\n1 3 1\n1 4 1\n1 5 1\n" ]
[ "1.0\n", "4.333333333333334\n", "4.0\n" ]
In the first test the castle only has two halls which means that the treasure is located in the second hall. Gerald will only need one minute to go to the second hall from the first one. In the second test Gerald can only go from the first hall to the third one. He can get from the third room to the first one or to th...
[]
124
0
0
58,304
986
Petr and Permutations
[ "combinatorics", "math" ]
null
null
Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutation of numbers from $1$ to $n$ and then $3n$ times takes a random pair of different elements and swaps them. Alex envies Petr ...
In the first line of input there is one integer $n$ ($10^{3} \le n \le 10^{6}$). In the second line there are $n$ distinct integers between $1$ and $n$Β β€” the permutation of size $n$ from the test. It is guaranteed that all tests except for sample are generated this way: First we choose $n$Β β€” the size of the permutati...
If the test is generated via Petr's method print "Petr" (without quotes). If the test is generated via Alex's method print "Um_nik" (without quotes).
[ "5\n2 4 5 1 3\n" ]
[ "Petr\n" ]
Please note that the sample is not a valid test (because of limitations for $n$) and is given only to illustrate input/output format. Your program still has to print correct answer to this test to get AC. Due to randomness of input hacks in this problem are forbidden.
[ { "input": "5\n2 4 5 1 3", "output": "Petr" } ]
217
22,323,200
-1
58,499
778
Peterson Polyglot
[ "brute force", "dfs and similar", "dsu", "hashing", "strings", "trees" ]
null
null
Peterson loves to learn new languages, but his favorite hobby is making new ones. Language is a set of words, and word is a sequence of lowercase Latin letters. Peterson makes new language every morning. It is difficult task to store the whole language, so Peterson have invented new data structure for storing his lang...
The first line of input contains integer *n* (2<=≀<=*n*<=≀<=3Β·105)Β β€” the size of the broom. Next *n*<=-<=1 lines describe the broom: *i*-th of them contains integers *u**i*, *v**i* and letter *x**i*Β β€” describing the edge from *u**i* to *v**i* marked with letter *x**i*. Vertices are numbered from 1 to *n*. All *x**i* ...
The first line of output should contain the minimum possible size of the broom after its simplification. The second line of output should contain integer *p* to choose. If there are several suitable *p* values, print the smallest one.
[ "5\n1 2 c\n2 3 a\n3 4 t\n2 5 t\n", "16\n1 2 o\n2 3 f\n1 4 p\n4 5 i\n5 6 e\n6 7 c\n7 8 e\n4 9 r\n9 10 e\n10 11 t\n11 12 t\n12 13 y\n10 14 f\n14 15 i\n15 16 x\n" ]
[ "3\n2\n", "12\n2\n" ]
<img class="tex-graphics" src="https://espresso.codeforces.com/4b46644a485274790bd64830c23320ae20be3097.png" style="max-width: 100.0%;max-height: 100.0%;"/> Broom from the second sample test can be built using language "piece", "of", "pie", "pretty", "prefix". Its simplification with *p* = 2 obtains the language of wo...
[ { "input": "5\n1 2 c\n2 3 a\n3 4 t\n2 5 t", "output": "3\n2" }, { "input": "16\n1 2 o\n2 3 f\n1 4 p\n4 5 i\n5 6 e\n6 7 c\n7 8 e\n4 9 r\n9 10 e\n10 11 t\n11 12 t\n12 13 y\n10 14 f\n14 15 i\n15 16 x", "output": "12\n2" }, { "input": "2\n1 2 o", "output": "1\n1" }, { "input": "3...
4,000
24,780,800
0
58,552
0
none
[ "none" ]
null
null
Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of *n* small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to...
The first input data line contains a string whose length is equal to *n* (1<=≀<=*n*<=≀<=105). The string consists of lowercase Latin letters. The second line contains the number *k* (0<=≀<=*k*<=≀<=105).
Print on the first line the only number *m* β€” the least possible number of different characters that could remain in the given string after it loses no more than *k* characters. Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly *m* distinct character...
[ "aaaaa\n4\n", "abacaba\n4\n", "abcdefgh\n10\n" ]
[ "1\naaaaa\n", "1\naaaa\n", "0\n\n" ]
In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length. In the second sample you are allowed to delete 4 characters. You cann...
[ { "input": "aaaaa\n4", "output": "1\naaaaa" }, { "input": "abacaba\n4", "output": "1\naaaa" }, { "input": "abcdefgh\n10", "output": "0" }, { "input": "aaaaaaaaaaaaaaaaaaaa\n19", "output": "1\naaaaaaaaaaaaaaaaaaaa" }, { "input": "abcdefghijjihgedcba\n0", "outpu...
404
512,000
3
58,553
149
Coloring Brackets
[ "dp" ]
null
null
Once Petya read a problem about a bracket sequence. He gave it much thought but didn't find a solution. Today you will face it. You are given string *s*. It represents a correct bracket sequence. A correct bracket sequence is the sequence of opening ("(") and closing (")") brackets, such that it is possible to obtain ...
The first line contains the single string *s* (2<=≀<=|*s*|<=≀<=700) which represents a correct bracket sequence.
Print the only number β€” the number of ways to color the bracket sequence that meet the above given conditions modulo 1000000007 (109<=+<=7).
[ "(())\n", "(()())\n", "()\n" ]
[ "12\n", "40\n", "4\n" ]
Let's consider the first sample test. The bracket sequence from the sample can be colored, for example, as is shown on two figures below. The two ways of coloring shown below are incorrect.
[ { "input": "(())", "output": "12" }, { "input": "(()())", "output": "40" }, { "input": "()", "output": "4" }, { "input": "((()))", "output": "36" }, { "input": "()(())", "output": "42" }, { "input": "()()()", "output": "48" }, { "input": "(...
310
4,300,800
3
58,728
420
Bug in Code
[ "data structures", "graphs", "implementation", "two pointers" ]
null
null
Recently a serious bug has been found in the FOS code. The head of the F company wants to find the culprit and punish him. For that, he set up an organizational meeting, the issue is: who's bugged the code? Each of the *n* coders on the meeting said: 'I know for sure that either *x* or *y* did it!' The head of the com...
The first line contains integers *n* and *p* (3<=≀<=*n*<=≀<=3Β·105;Β 0<=≀<=*p*<=≀<=*n*) β€” the number of coders in the F company and the minimum number of agreed people. Each of the next *n* lines contains two integers *x**i*, *y**i* (1<=≀<=*x**i*,<=*y**i*<=≀<=*n*) β€” the numbers of coders named by the *i*-th coder. It is...
Print a single integer –– the number of possible two-suspect sets. Note that the order of the suspects doesn't matter, that is, sets (1,<=2) ΠΈ (2,<=1) are considered identical.
[ "4 2\n2 3\n1 4\n1 4\n2 1\n", "8 6\n5 6\n5 7\n5 8\n6 2\n2 1\n7 3\n1 3\n1 4\n" ]
[ "6\n", "1\n" ]
none
[ { "input": "4 2\n2 3\n1 4\n1 4\n2 1", "output": "6" }, { "input": "8 6\n5 6\n5 7\n5 8\n6 2\n2 1\n7 3\n1 3\n1 4", "output": "1" }, { "input": "3 2\n2 3\n3 1\n2 1", "output": "3" }, { "input": "4 1\n3 2\n4 1\n4 2\n1 2", "output": "6" }, { "input": "4 2\n3 4\n4 3\n4 ...
1,000
54,681,600
0
58,891
961
Partitions
[ "combinatorics", "math", "number theory" ]
null
null
You are given a set of *n* elements indexed from 1 to *n*. The weight of *i*-th element is *w**i*. The weight of some subset of a given set is denoted as . The weight of some partition *R* of a given set into *k* subsets is (recall that a partition of a given set is a set of its subsets such that every element of the ...
The first line contains two integers *n* and *k* (1<=≀<=*k*<=≀<=*n*<=≀<=2Β·105) β€” the number of elements and the number of subsets in each partition, respectively. The second line contains *n* integers *w**i* (1<=≀<=*w**i*<=≀<=109)β€” weights of elements of the set.
Print one integer β€” the sum of weights of all partitions of a given set into *k* non-empty subsets, taken modulo 109<=+<=7.
[ "4 2\n2 3 2 3\n", "5 2\n1 2 3 4 5\n" ]
[ "160\n", "645\n" ]
Possible partitions in the first sample: 1. {{1, 2, 3}, {4}}, *W*(*R*) = 3Β·(*w*<sub class="lower-index">1</sub> + *w*<sub class="lower-index">2</sub> + *w*<sub class="lower-index">3</sub>) + 1Β·*w*<sub class="lower-index">4</sub> = 24; 1. {{1, 2, 4}, {3}}, *W*(*R*) = 26; 1. {{1, 3, 4}, {2}}, *W*(*R*) = 24; 1. {{1, ...
[ { "input": "4 2\n2 3 2 3", "output": "160" }, { "input": "5 2\n1 2 3 4 5", "output": "645" }, { "input": "1 1\n1", "output": "1" }, { "input": "1 1\n1000000000", "output": "1000000000" }, { "input": "2 1\n6042 8885", "output": "29854" }, { "input": "2 ...
280
36,761,600
0
59,146
870
Paths
[ "data structures", "number theory" ]
null
null
You are given a positive integer *n*. Let's build a graph on vertices 1,<=2,<=...,<=*n* in such a way that there is an edge between vertices *u* and *v* if and only if . Let *d*(*u*,<=*v*) be the shortest distance between *u* and *v*, or 0 if there is no path between them. Compute the sum of values *d*(*u*,<=*v*) over ...
Single integer *n* (1<=≀<=*n*<=≀<=107).
Print the sum of *d*(*u*,<=*v*) over all 1<=≀<=*u*<=&lt;<=*v*<=≀<=*n*.
[ "6\n", "10\n" ]
[ "8\n", "44\n" ]
All shortest paths in the first example: - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7b7a110945393c2b47718b811aadf12cbe125f3f.png" style="max-width: 100.0%;max-height: 100.0%;"/> - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e9af98cb64a31b6dae850db...
[]
31
0
0
59,219
1,010
Store
[ "data structures" ]
null
null
Natasha was already going to fly back to Earth when she remembered that she needs to go to the Martian store to buy Martian souvenirs for her friends. It is known, that the Martian year lasts $x_{max}$ months, month lasts $y_{max}$ days, day lasts $z_{max}$ seconds. Natasha also knows that this store works according t...
The first line contains $6$ integers $x_{max}$, $y_{max}$, $z_{max}$, $n$, $m$, $k$ ($1\le x_{max},y_{max},z_{max}\le 10^5$, $1\le n\le 10^5$, $0\le m\le 10^5$, $1\le k\le 10^5$)Β β€” number of months in a year, days in a month, seconds in a day, times when the store (according to a Martian) was opened, when it was closed...
If the Martian was mistaken and his information about when the store is open and when it is closed is inconsistent, print a single line "INCORRECT" (without quotes). Otherwise, print the first line "CORRECT" (without quotes). Next output $k$ lines: in $i$-th of them, output an answer to $i$-th Natasha's query: "OPEN" ...
[ "10 10 10 3 1 3\n2 6 2\n4 2 4\n6 4 6\n9 9 9\n3 3 3\n10 10 10\n8 8 8\n", "10 10 10 1 1 1\n2 5 7\n2 5 7\n8 9 10\n" ]
[ "CORRECT\nOPEN\nCLOSED\nUNKNOWN\n", "INCORRECT\n" ]
Consider the first test case. There are $10$ months in a year, $10$ days in a month, and $10$ seconds in a day. The store was opened in $3$ moments: - month $2$, day $6$, second $2$;- month $4$, day $2$, second $4$;- month $6$, day $4$, second $6$. The store was closed at the time: month $9$, day $9$, second $9$. ...
[]
108
0
0
59,302
630
A rectangle
[ "math" ]
null
null
Developing tools for creation of locations maps for turn-based fights in a new game, Petya faced the following problem. A field map consists of hexagonal cells. Since locations sizes are going to be big, a game designer wants to have a tool for quick filling of a field part with identical enemy units. This action will...
The only line of input contains four integers *x*1,<=*y*1,<=*x*2,<=*y*2 (<=-<=109<=≀<=*x*1<=≀<=*x*2<=≀<=109,<=<=-<=109<=≀<=*y*1<=≀<=*y*2<=≀<=109) β€” the coordinates of the centers of two cells.
Output one integer β€” the number of cells to be filled.
[ "1 1 5 5\n" ]
[ "13" ]
none
[ { "input": "1 1 5 5", "output": "13" }, { "input": "-1 -3 1 3", "output": "11" }, { "input": "-2 -2 2 2", "output": "13" }, { "input": "0 0 2 2", "output": "5" }, { "input": "0 0 2 0", "output": "2" }, { "input": "0 0 0 0", "output": "1" }, { ...
31
0
0
59,308
659
Fence Divercity
[ "combinatorics", "dp", "number theory" ]
null
null
Long ago, Vasily built a good fence at his country house. Vasily calls a fence good, if it is a series of *n* consecutively fastened vertical boards of centimeter width, the height of each in centimeters is a positive integer. The house owner remembers that the height of the *i*-th board to the left is *h**i*. Today V...
The first line contains integer *n* (1<=≀<=*n*<=≀<=1<=000<=000)Β β€” the number of boards in Vasily's fence. The second line contains *n* space-separated numbers *h*1,<=*h*2,<=...,<=*h**n* (1<=≀<=*h**i*<=≀<=109), where *h**i* equals the height of the *i*-th board to the left.
Print the remainder after dividing *r* by 1<=000<=000<=007, where *r* is the number of ways to cut exactly one connected part so that the part consisted of the upper parts of the boards and the remaining fence was good.
[ "2\n1 1\n", "3\n3 4 2\n" ]
[ "0\n", "13\n" ]
From the fence from the first example it is impossible to cut exactly one piece so as the remaining fence was good. All the possible variants of the resulting fence from the second sample look as follows (the grey shows the cut out part):
[ { "input": "2\n1 1", "output": "0" }, { "input": "3\n3 4 2", "output": "13" }, { "input": "1\n1", "output": "0" }, { "input": "1\n1000000000", "output": "999999999" }, { "input": "3\n1 2 3", "output": "4" }, { "input": "2\n2 3", "output": "4" }, ...
2,000
137,318,400
0
59,374
268
Wall Bars
[ "dp" ]
null
null
Manao is working for a construction company. Recently, an order came to build wall bars in a children's park. Manao was commissioned to develop a plan of construction, which will enable the company to save the most money. After reviewing the formal specifications for the wall bars, Manao discovered a number of controv...
A single line contains two space-separated integers, *n* and *h* (1<=≀<=*n*<=≀<=1000, 1<=≀<=*h*<=≀<=*min*(*n*,<=30)).
In a single line print the remainder after dividing the number of designs by 1000000009Β (109<=+<=9).
[ "5 1\n", "4 2\n", "4 3\n", "5 2\n" ]
[ "4\n", "148\n", "256\n", "376\n" ]
Consider several designs for *h* = 2. A design with the first bar sticked out in direction *d*<sub class="lower-index">1</sub>, the second β€” in direction *d*<sub class="lower-index">2</sub> and so on (1 ≀ *d*<sub class="lower-index">*i*</sub> ≀ 4) is denoted as string *d*<sub class="lower-index">1</sub>*d*<sub class="l...
[]
60
0
0
59,411
460
Little Victor and Set
[ "brute force", "constructive algorithms", "math" ]
null
null
Little Victor adores the sets theory. Let us remind you that a set is a group of numbers where all numbers are pairwise distinct. Today Victor wants to find a set of integers *S* that has the following properties: - for all *x* the following inequality holds *l*<=≀<=*x*<=≀<=*r*; - 1<=≀<=|*S*|<=≀<=*k*; - lets denot...
The first line contains three space-separated integers *l*,<=*r*,<=*k* (1<=≀<=*l*<=≀<=*r*<=≀<=1012;Β 1<=≀<=*k*<=≀<=*min*(106,<=*r*<=-<=*l*<=+<=1)).
Print the minimum possible value of *f*(*S*). Then print the cardinality of set |*S*|. Then print the elements of the set in any order. If there are multiple optimal sets, you can print any of them.
[ "8 15 3\n", "8 30 7\n" ]
[ "1\n2\n10 11\n", "0\n5\n14 9 28 11 16\n" ]
Operation <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/b364f2e04c665b78b924ec10666327a4ef4635bc.png" style="max-width: 100.0%;max-height: 100.0%;"/> represents the operation of bitwise exclusive OR. In other words, it is the XOR operation.
[ { "input": "8 15 3", "output": "1\n2\n10 11" }, { "input": "8 30 7", "output": "0\n5\n14 9 28 11 16" }, { "input": "1 1 1", "output": "1\n1\n1 " }, { "input": "3 987654321502 1", "output": "3\n1\n3" }, { "input": "15603259690 63210239992 2", "output": "1\n2\n1...
61
2,867,200
-1
59,497
314
Sereja and Contest
[ "implementation" ]
null
null
During the last Sereja's Codesecrof round the server crashed many times, so the round was decided to be made unrated for some participants. Let's assume that *n* people took part in the contest. Let's assume that the participant who got the first place has rating *a*1, the second place participant has rating *a*2, .....
The first line contains two integers *n*, *k* (1<=≀<=*n*<=≀<=2Β·105,<=<=-<=109<=≀<=*k*<=≀<=0). The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109) β€” ratings of the participants in the initial table.
Print the numbers of participants in the order in which they were removed from the table. Print the initial numbers of the participants, that is, the numbers that the participants had in the initial table.
[ "5 0\n5 3 4 1 2\n", "10 -10\n5 5 1 7 5 1 2 4 9 2\n" ]
[ "2\n3\n4\n", "2\n4\n5\n7\n8\n9\n" ]
Consider the first test sample. 1. Initially the sequence of the contest participants' ratings equals [5, 3, 4, 1, 2]. You can use this sequence to calculate the sequence of rating changes: [0, -9, -13, 8, 14]. According to the problem statement, the application of the participant who won the second place will be con...
[ { "input": "5 0\n5 3 4 1 2", "output": "2\n3\n4" }, { "input": "10 -10\n5 5 1 7 5 1 2 4 9 2", "output": "2\n4\n5\n7\n8\n9" } ]
2,000
17,715,200
0
59,507
363
Renting Bikes
[ "binary search", "greedy" ]
null
null
A group of *n* schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them. The renting site offered them *m* bikes. The renting price is different for different bikes, renting the *j*-th bike costs *p**j* rubles. In total, the boys' shared budget is *a* rubles. Besides, each of them ha...
The first line of the input contains three integers *n*, *m* and *a* (1<=≀<=*n*,<=*m*<=≀<=105; 0<=≀<=*a*<=≀<=109). The second line contains the sequence of integers *b*1,<=*b*2,<=...,<=*b**n* (1<=≀<=*b**i*<=≀<=104), where *b**i* is the amount of the *i*-th boy's personal money. The third line contains the sequence of i...
Print two integers *r* and *s*, where *r* is the maximum number of schoolboys that can rent a bike and *s* is the minimum total personal money needed to rent *r* bikes. If the schoolchildren cannot rent any bikes, then *r*<==<=*s*<==<=0.
[ "2 2 10\n5 5\n7 6\n", "4 5 2\n8 1 1 2\n6 3 7 5 2\n" ]
[ "2 3\n", "3 8\n" ]
In the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal ...
[ { "input": "2 2 10\n5 5\n7 6", "output": "2 3" }, { "input": "4 5 2\n8 1 1 2\n6 3 7 5 2", "output": "3 8" }, { "input": "1 1 2\n1\n2", "output": "1 0" }, { "input": "4 1 1\n3 2 3 2\n3", "output": "1 2" }, { "input": "1 4 1\n3\n2 4 5 5", "output": "1 1" }, ...
30
0
0
59,560
733
Drivers Dissatisfaction
[ "data structures", "dsu", "graphs", "trees" ]
null
null
In one kingdom there are *n* cities and *m* two-way roads. Each road connects a pair of cities, and for each road we know the level of drivers dissatisfactionΒ β€” the value *w**i*. For each road we know the value *c**i*Β β€” how many lamziks we should spend to reduce the level of dissatisfaction with this road by one. Thus...
The first line contains two integers *n* and *m* (2<=≀<=*n*<=≀<=2Β·105, *n*<=-<=1<=≀<=*m*<=≀<=2Β·105)Β β€” the number of cities and the number of roads in the kingdom, respectively. The second line contains *m* integers *w*1,<=*w*2,<=...,<=*w**m* (1<=≀<=*w**i*<=≀<=109), where *w**i* is the drivers dissatisfaction with the ...
In the first line print *K*Β β€” the minimum possible total dissatisfaction with main roads. In each of the next *n*<=-<=1 lines print two integers *x*,<=*v**x*, which mean that the road *x* is among main roads and the road *x*, after the reform, has the level of dissatisfaction *v**x*. Consider that roads are numbered ...
[ "6 9\n1 3 1 1 3 1 2 2 2\n4 1 4 2 2 5 3 1 6\n1 2\n1 3\n2 3\n2 4\n2 5\n3 5\n3 6\n4 5\n5 6\n7\n", "3 3\n9 5 1\n7 7 2\n2 1\n3 1\n3 2\n2\n" ]
[ "0\n1 1\n3 1\n6 1\n7 2\n8 -5\n", "5\n3 0\n2 5\n" ]
none
[ { "input": "6 9\n1 3 1 1 3 1 2 2 2\n4 1 4 2 2 5 3 1 6\n1 2\n1 3\n2 3\n2 4\n2 5\n3 5\n3 6\n4 5\n5 6\n7", "output": "0\n1 1\n3 1\n6 1\n7 2\n8 -5" }, { "input": "3 3\n9 5 1\n7 7 2\n2 1\n3 1\n3 2\n2", "output": "5\n3 0\n2 5" }, { "input": "7 6\n8 10 4 8 4 4\n45 51 13 13 37 26\n2 7\n7 1\n6 3\...
78
0
0
59,604
35
Warehouse
[ "implementation" ]
B. Warehouse
2
64
Once upon a time, when the world was more beautiful, the sun shone brighter, the grass was greener and the sausages tasted better Arlandia was the most powerful country. And its capital was the place where our hero DravDe worked. He couldn’t program or make up problems (in fact, few people saw a computer those days) bu...
The first input line contains integers *n*, *m* and *k* (1<=≀<=*n*,<=*m*<=≀<=30, 1<=≀<=*k*<=≀<=2000) β€” the height, the width of shelving and the amount of the operations in the warehouse that you need to analyze. In the following *k* lines the queries are given in the order of appearance in the format described above.
For each query of the Β«-1 idΒ» type output two numbers in a separate line β€” index of the shelf and index of the section where the box with this identifier lay. If there was no such box in the warehouse when the query was made, output Β«-1 -1Β» without quotes.
[ "2 2 9\n+1 1 1 cola\n+1 1 1 fanta\n+1 1 1 sevenup\n+1 1 1 whitekey\n-1 cola\n-1 fanta\n-1 sevenup\n-1 whitekey\n-1 cola\n", "2 2 8\n+1 1 1 cola\n-1 cola\n+1 1 1 fanta\n-1 fanta\n+1 1 1 sevenup\n-1 sevenup\n+1 1 1 whitekey\n-1 whitekey\n" ]
[ "1 1\n1 2\n2 1\n2 2\n-1 -1\n", "1 1\n1 1\n1 1\n1 1\n" ]
none
[ { "input": "2 2 9\n+1 1 1 cola\n+1 1 1 fanta\n+1 1 1 sevenup\n+1 1 1 whitekey\n-1 cola\n-1 fanta\n-1 sevenup\n-1 whitekey\n-1 cola", "output": "1 1\n1 2\n2 1\n2 2\n-1 -1" }, { "input": "2 2 8\n+1 1 1 cola\n-1 cola\n+1 1 1 fanta\n-1 fanta\n+1 1 1 sevenup\n-1 sevenup\n+1 1 1 whitekey\n-1 whitekey", ...
530
20,787,200
3.712623
59,634
196
Infinite Maze
[ "dfs and similar", "graphs" ]
null
null
We've got a rectangular *n*<=Γ—<=*m*-cell maze. Each cell is either passable, or is a wall (impassable). A little boy found the maze and cyclically tiled a plane with it so that the plane became an infinite maze. Now on this plane cell (*x*,<=*y*) is a wall if and only if cell is a wall. In this problem is a remainde...
The first line contains two space-separated integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=1500) β€” the height and the width of the maze that the boy used to cyclically tile the plane. Each of the next *n* lines contains *m* characters β€” the description of the labyrinth. Each character is either a "#", that marks a wall, a ...
Print "Yes" (without the quotes), if the little boy can walk infinitely far from the starting point. Otherwise, print "No" (without the quotes).
[ "5 4\n##.#\n##S#\n#..#\n#.##\n#..#\n", "5 4\n##.#\n##S#\n#..#\n..#.\n#.##\n" ]
[ "Yes\n", "No\n" ]
In the first sample the little boy can go up for infinitely long as there is a "clear path" that goes vertically. He just needs to repeat the following steps infinitely: up, up, left, up, up, right, up. In the second sample the vertical path is blocked. The path to the left doesn't work, too β€” the next "copy" of the m...
[]
716
18,124,800
-1
59,969
466
Information Graph
[ "dfs and similar", "dsu", "graphs", "trees" ]
null
null
There are *n* employees working in company "X" (let's number them from 1 to *n* for convenience). Initially the employees didn't have any relationships among each other. On each of *m* next days one of the following events took place: - either employee *y* became the boss of employee *x* (at that, employee *x* didn't...
The first line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=105) β€” the number of employees and the number of events. Each of the next *m* lines contains the description of one event (the events are given in the chronological order). The first number of the line determines the type of event *t* (1<=≀<=*t*<=≀...
For each query of the third type print "YES" if the employee signed the document package and "NO" otherwise. Print all the words without the quotes.
[ "4 9\n1 4 3\n2 4\n3 3 1\n1 2 3\n2 2\n3 1 2\n1 3 1\n2 2\n3 1 3\n" ]
[ "YES\nNO\nYES\n" ]
none
[]
31
0
0
60,021
467
Fedor and Essay
[ "dfs and similar", "dp", "graphs", "hashing", "strings" ]
null
null
After you had helped Fedor to find friends in the Β«Call of Soldiers 3Β» game, he stopped studying completely. Today, the English teacher told him to prepare an essay. Fedor didn't want to prepare the essay, so he asked Alex for help. Alex came to help and wrote the essay for Fedor. But Fedor didn't like the essay at all...
The first line contains a single integer *m* (1<=≀<=*m*<=≀<=105) β€” the number of words in the initial essay. The second line contains words of the essay. The words are separated by a single space. It is guaranteed that the total length of the words won't exceed 105 characters. The next line contains a single integer *...
Print two integers β€” the minimum number of letters Β«RΒ» in an optimal essay and the minimum length of an optimal essay.
[ "3\nAbRb r Zz\n4\nxR abRb\naA xr\nzz Z\nxr y\n", "2\nRuruRu fedya\n1\nruruRU fedor\n" ]
[ "2 6\n", "1 10\n" ]
none
[ { "input": "3\nAbRb r Zz\n4\nxR abRb\naA xr\nzz Z\nxr y", "output": "2 6" }, { "input": "2\nRuruRu fedya\n1\nruruRU fedor", "output": "1 10" }, { "input": "1\nffff\n1\nffff r", "output": "0 4" }, { "input": "2\nYURA YUrA\n1\nyura fedya", "output": "0 10" }, { "inp...
30
0
0
60,081
679
Bear and Bad Powers of 42
[ "data structures" ]
null
null
Limak, a bear, isn't good at handling queries. So, he asks you to do it. We say that powers of 42 (numbers 1,<=42,<=1764,<=...) are bad. Other numbers are good. You are given a sequence of *n* good integers *t*1,<=*t*2,<=...,<=*t**n*. Your task is to handle *q* queries of three types: 1. 1 iΒ β€” print *t**i* in a sep...
The first line of the input contains two integers *n* and *q* (1<=≀<=*n*,<=*q*<=≀<=100<=000)Β β€” the size of Limak's sequence and the number of queries, respectively. The second line of the input contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (2<=≀<=*t**i*<=≀<=109)Β β€” initial elements of Limak's sequence. All *t**i* ar...
For each query of the first type, print the answer in a separate line.
[ "6 12\n40 1700 7 1672 4 1722\n3 2 4 42\n1 2\n1 3\n3 2 6 50\n1 2\n1 4\n1 6\n2 3 4 41\n3 1 5 1\n1 1\n1 3\n1 5\n" ]
[ "1742\n49\n1842\n1814\n1822\n43\n44\n107\n" ]
After a query 3 2 4 42 the sequence is 40, 1742, 49, 1714, 4, 1722. After a query 3 2 6 50 the sequence is 40, 1842, 149, 1814, 104, 1822. After a query 2 3 4 41 the sequence is 40, 1842, 41, 41, 104, 1822. After a query 3 1 5 1 the sequence is 43, 1845, 44, 44, 107, 1822.
[]
0
0
-1
60,134
883
Downloading B++
[ "binary search", "implementation" ]
null
null
Only *T* milliseconds left before the start of well-known online programming contest Codehorses Round 2017. Polycarp needs to download B++ compiler to take part in the contest. The size of the file is *f* bytes. Polycarp's internet tariff allows to download data at the rate of one byte per *t*0 milliseconds. This tar...
The first line contains three integer numbers *f*, *T* and *t*0 (1<=≀<=*f*,<=*T*,<=*t*0<=≀<=107) β€” size of the file to download (in bytes), maximal time to download the file (in milliseconds) and number of milliseconds to download one byte using the regular internet tariff. The second line contains a description of th...
Print the minimum amount of money that Polycarp needs to pay to download B++ compiler no more than in *T* milliseconds. If there is no solution, print the only integer -1.
[ "120 964 20\n26 8 8\n13 10 4\n", "10 200 20\n1 1 1\n2 2 3\n", "8 81 11\n4 10 16\n3 10 12\n", "8 79 11\n4 10 16\n3 10 12\n" ]
[ "40\n", "0\n", "28\n", "-1\n" ]
In the first example Polycarp has to buy the first additional package 5 times and do not buy the second additional package. He downloads 120 bytes (of total 26Β·5 = 130 bytes) in 120Β·8 = 960 milliseconds (960 ≀ 964). He spends 8Β·5 = 40 burles on it. In the second example Polycarp has enough time to download 10 bytes. I...
[ { "input": "120 964 20\n26 8 8\n13 10 4", "output": "40" }, { "input": "10 200 20\n1 1 1\n2 2 3", "output": "0" }, { "input": "8 81 11\n4 10 16\n3 10 12", "output": "28" }, { "input": "8 79 11\n4 10 16\n3 10 12", "output": "-1" }, { "input": "62 10000 209\n95 106 ...
46
0
0
60,153
723
Text Document Analysis
[ "expression parsing", "implementation", "strings" ]
null
null
Modern text editors usually show some information regarding the document being edited. For example, the number of words, the number of pages, or the number of characters. In this problem you should implement the similar functionality. You are given a string which only consists of: - uppercase and lowercase English ...
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=255)Β β€” the length of the given string. The second line contains the string consisting of only lowercase and uppercase English letters, parentheses and underscore symbols.
Print two space-separated integers: - the length of the longest word outside the parentheses (print 0, if there is no word outside the parentheses), - the number of words inside the parentheses (print 0, if there is no word inside the parentheses).
[ "37\n_Hello_Vasya(and_Petya)__bye_(and_OK)\n", "37\n_a_(_b___c)__de_f(g_)__h__i(j_k_l)m__\n", "27\n(LoooonG)__shOrt__(LoooonG)\n", "5\n(___)\n" ]
[ "5 4", "2 6", "5 2", "0 0\n" ]
In the first sample, the words "Hello", "Vasya" and "bye" are outside any of the parentheses, and the words "and", "Petya", "and" and "OK" are inside. Note, that the word "and" is given twice and you should count it twice in the answer.
[ { "input": "37\n_Hello_Vasya(and_Petya)__bye_(and_OK)", "output": "5 4" }, { "input": "37\n_a_(_b___c)__de_f(g_)__h__i(j_k_l)m__", "output": "2 6" }, { "input": "27\n(LoooonG)__shOrt__(LoooonG)", "output": "5 2" }, { "input": "5\n(___)", "output": "0 0" }, { "inpu...
46
0
3
60,163
567
President and Roads
[ "dfs and similar", "graphs", "hashing", "shortest paths" ]
null
null
Berland has *n* cities, the capital is located in city *s*, and the historic home town of the President is in city *t* (*s*<=β‰ <=*t*). The cities are connected by one-way roads, the travel time for each of the road is a positive integer. Once a year the President visited his historic home town *t*, for which his motorc...
The first lines contain four integers *n*, *m*, *s* and *t* (2<=≀<=*n*<=≀<=105;Β 1<=≀<=*m*<=≀<=105;Β 1<=≀<=*s*,<=*t*<=≀<=*n*) β€” the number of cities and roads in Berland, the numbers of the capital and of the Presidents' home town (*s*<=β‰ <=*t*). Next *m* lines contain the roads. Each road is given as a group of three in...
Print *m* lines. The *i*-th line should contain information about the *i*-th road (the roads are numbered in the order of appearance in the input). If the president will definitely ride along it during his travels, the line must contain a single word "YES" (without the quotes). Otherwise, if the *i*-th road can be re...
[ "6 7 1 6\n1 2 2\n1 3 10\n2 3 7\n2 4 8\n3 5 3\n4 5 2\n5 6 1\n", "3 3 1 3\n1 2 10\n2 3 10\n1 3 100\n", "2 2 1 2\n1 2 1\n1 2 2\n" ]
[ "YES\nCAN 2\nCAN 1\nCAN 1\nCAN 1\nCAN 1\nYES\n", "YES\nYES\nCAN 81\n", "YES\nNO\n" ]
The cost of repairing the road is the difference between the time needed to ride along it before and after the repairing. In the first sample president initially may choose one of the two following ways for a ride: 1 → 2 → 4 → 5 → 6 or 1 → 2 → 3 → 5 → 6.
[ { "input": "6 7 1 6\n1 2 2\n1 3 10\n2 3 7\n2 4 8\n3 5 3\n4 5 2\n5 6 1", "output": "YES\nCAN 2\nCAN 1\nCAN 1\nCAN 1\nCAN 1\nYES" }, { "input": "3 3 1 3\n1 2 10\n2 3 10\n1 3 100", "output": "YES\nYES\nCAN 81" }, { "input": "2 2 1 2\n1 2 1\n1 2 2", "output": "YES\nNO" }, { "inpu...
46
0
0
60,189
959
Mahmoud and Ehab and the wrong algorithm
[ "constructive algorithms", "trees" ]
null
null
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is: Given an undirected tree consisting of *n* nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (*u*,<=*v*) that belongs to the tree, either *u* i...
The only line contains an integer *n* (2<=≀<=*n*<=≀<=105), the number of nodes in the desired trees.
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only. If the answer for a...
[ "2\n", "8\n" ]
[ "-1\n1 2\n", "1 2\n1 3\n2 4\n2 5\n3 6\n4 7\n4 8\n1 2\n1 3\n2 4\n2 5\n2 6\n3 7\n6 8" ]
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed  - 1 in the first section, but notice that we printed this tree in the second section. In the second sample: In the first tree, the algorithm will find an answer with 4 ...
[ { "input": "2", "output": "-1\n1 2" }, { "input": "8", "output": "1 2\n1 3\n1 4\n2 5\n2 6\n1 7\n1 8\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8" }, { "input": "99", "output": "1 2\n1 3\n1 4\n2 5\n2 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\...
46
0
0
60,216
97
Winning Strategy
[ "binary search", "graphs", "math", "shortest paths" ]
C. Winning Strategy
2
256
One university has just found out about a sport programming contest called ACM ICPC v2.0. This contest doesn't differ much from the well-known ACM ICPC, for example, the participants are not allowed to take part in the finals more than two times. However, there is one notable difference: the teams in the contest should...
The first line contains an integer *n* (3<=≀<=*n*<=≀<=100), *n* is the number of team participants. The second line contains *n*<=+<=1 real numbers with no more than 6 digits after decimal point *p**i* (0<=≀<=*i*<=≀<=*n*,<=0<=≀<=*p**i*<=≀<=1) β€” the probability of that the team will win a medal if it contains *i* partic...
Print the only real number β€” the expected average number of medals won per year if the optimal strategy is used. The result may have absolute or relative error 10<=-<=6.
[ "3\n0.115590 0.384031 0.443128 0.562356\n", "3\n1 1 1 1\n" ]
[ "0.4286122500\n", "0.9999999999\n" ]
In the second test, no matter what participants the team contains, it is doomed to be successful.
[ { "input": "3\n0.115590 0.384031 0.443128 0.562356", "output": "0.4286122500" }, { "input": "3\n1 1 1 1", "output": "0.9999999999" }, { "input": "10\n0.054228 0.284367 0.307914 0.319911 0.325274 0.336089 0.549308 0.554288 0.814348 0.817238 0.861607", "output": "0.5872132857" }, {...
92
0
0
60,802