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
61
Enemy is weak
[ "data structures", "trees" ]
E. Enemy is weak
5
256
The Romans have attacked again. This time they are much more than the Persians but Shapur is ready to defeat them. He says: "A lion is never afraid of a hundred sheep". Nevertheless Shapur has to find weaknesses in the Roman army to defeat them. So he gives the army a weakness number. In Shapur's opinion the weaknes...
The first line of input contains a single number *n* (3<=≀<=*n*<=≀<=106) β€” the number of men in Roman army. Next line contains *n* different positive integers *a**i* (1<=≀<=*i*<=≀<=*n*,<=1<=≀<=*a**i*<=≀<=109) β€” powers of men in the Roman army.
A single integer number, the weakness of the Roman army. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
[ "3\n3 2 1\n", "3\n2 3 1\n", "4\n10 8 3 1\n", "4\n1 5 4 3\n" ]
[ "1\n", "0\n", "4\n", "1\n" ]
none
[ { "input": "3\n3 2 1", "output": "1" }, { "input": "3\n2 3 1", "output": "0" }, { "input": "4\n10 8 3 1", "output": "4" }, { "input": "4\n1 5 4 3", "output": "1" }, { "input": "9\n10 9 5 6 8 3 4 7 11", "output": "20" }, { "input": "7\n11 3 8 4 2 9 6", ...
2,340
213,606,400
3.368127
14,724
873
Awards For Contestants
[ "brute force", "data structures", "dp" ]
null
null
Alexey recently held a programming contest for students from Berland. *n* students participated in a contest, *i*-th of them solved *a**i* problems. Now he wants to award some contestants. Alexey can award the students with diplomas of three different degrees. Each student either will receive one diploma of some degree...
The first line contains one integer number *n* (3<=≀<=*n*<=≀<=3000). The second line contains *n* integer numbers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=5000).
Output *n* numbers. *i*-th number must be equal to the degree of diploma *i*-th contestant will receive (or <=-<=1 if he doesn't receive any diploma). If there are multiple optimal solutions, print any of them. It is guaranteed that the answer always exists.
[ "4\n1 2 3 4\n", "6\n1 4 3 1 1 2\n" ]
[ "3 3 2 1 \n", "-1 1 2 -1 -1 3 \n" ]
none
[ { "input": "4\n1 2 3 4", "output": "3 3 2 1 " }, { "input": "6\n1 4 3 1 1 2", "output": "-1 1 2 -1 -1 3 " }, { "input": "100\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
140
0
0
14,759
145
Lucky Subsequence
[ "combinatorics", "dp", "math" ]
null
null
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya has sequence *a* consisting of *n* integers. The subsequence of the sequence *a* is such su...
The first line contains two integers *n* and *k* (1<=≀<=*k*<=≀<=*n*<=≀<=105). The next line contains *n* integers *a**i* (1<=≀<=*a**i*<=≀<=109) β€” the sequence *a*.
On the single line print the single number β€” the answer to the problem modulo prime number 1000000007 (109<=+<=7).
[ "3 2\n10 10 10\n", "4 2\n4 4 7 7\n" ]
[ "3\n", "4\n" ]
In the first sample all 3 subsequences of the needed length are considered lucky. In the second sample there are 4 lucky subsequences. For them the sets of indexes equal (the indexation starts from 1): {1, 3}, {1, 4}, {2, 3} and {2, 4}.
[ { "input": "3 2\n10 10 10", "output": "3" }, { "input": "4 2\n4 4 7 7", "output": "4" }, { "input": "7 4\n1 2 3 4 5 6 7", "output": "35" }, { "input": "7 4\n7 7 7 7 7 7 7", "output": "0" }, { "input": "10 1\n1 2 3 4 5 6 7 8 9 10", "output": "10" }, { "...
62
0
0
14,760
378
Semifinals
[ "implementation", "sortings" ]
null
null
Two semifinals have just been in the running tournament. Each semifinal had *n* participants. There are *n* participants advancing to the finals, they are chosen as follows: from each semifinal, we choose *k* people (0<=≀<=2*k*<=≀<=*n*) who showed the best result in their semifinals and all other places in the finals g...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=105) β€” the number of participants in each semifinal. Each of the next *n* lines contains two integers *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=109)Β β€” the results of the *i*-th participant (the number of milliseconds he needs to cover the semifinals distan...
Print two strings consisting of *n* characters, each equals either "0" or "1". The first line should correspond to the participants of the first semifinal, the second line should correspond to the participants of the second semifinal. The *i*-th character in the *j*-th line should equal "1" if the *i*-th participant of...
[ "4\n9840 9920\n9860 9980\n9930 10020\n10040 10090\n", "4\n9900 9850\n9940 9930\n10000 10020\n10060 10110\n" ]
[ "1110\n1100\n", "1100\n1100\n" ]
Consider the first sample. Each semifinal has 4 participants. The results of the first semifinal are 9840, 9860, 9930, 10040. The results of the second semifinal are 9920, 9980, 10020, 10090. - If *k* = 0, the finalists are determined by the time only, so players 9840, 9860, 9920 and 9930 advance to the finals. - If...
[ { "input": "4\n9840 9920\n9860 9980\n9930 10020\n10040 10090", "output": "1110\n1100" }, { "input": "4\n9900 9850\n9940 9930\n10000 10020\n10060 10110", "output": "1100\n1100" }, { "input": "1\n1 2", "output": "1\n0" }, { "input": "1\n2 1", "output": "0\n1" }, { "...
1,000
38,707,200
0
14,767
108
Datatypes
[ "math", "sortings" ]
B. Datatypes
2
256
Tattah's youngest brother, Tuftuf, is new to programming. Since his older brother is such a good programmer, his biggest dream is to outshine him. Tuftuf is a student at the German University in Cairo (GUC) where he learns to write programs in Gava. Today, Tuftuf was introduced to Gava's unsigned integer datatypes. G...
The first line contains integer *n* (2<=≀<=*n*<=≀<=105) β€” the number of Gava's unsigned integer datatypes' sizes. The second line contains a single-space-separated list of *n* integers (1<=≀<=*a**i*<=≀<=109) β€” sizes of datatypes in bits. Some datatypes may have equal sizes.
Print "YES" if Tuftuf will stop using Gava, and "NO" otherwise.
[ "3\n64 16 32\n", "4\n4 2 1 3\n" ]
[ "NO\n", "YES\n" ]
In the second example, *x* = 7 (111<sub class="lower-index">2</sub>) fits in 3 bits, but *x*<sup class="upper-index">2</sup> = 49 (110001<sub class="lower-index">2</sub>) does not fit in 4 bits.
[ { "input": "3\n64 16 32", "output": "NO" }, { "input": "4\n4 2 1 3", "output": "YES" }, { "input": "5\n1 5 3 3 2", "output": "YES" }, { "input": "52\n474 24 24 954 9 234 474 114 24 114 234 24 114 114 234 9 9 24 9 54 234 54 9 954 474 9 54 54 54 234 9 114 24 54 114 954 954 474 ...
466
10,854,400
3.863282
14,768
670
Restore a Number
[ "brute force", "constructive algorithms", "strings" ]
null
null
Vasya decided to pass a very large integer *n* to Kate. First, he wrote that number as a string, then he appended to the right integer *k*Β β€” the number of digits in *n*. Magically, all the numbers were shuffled in arbitrary order while this note was passed to Kate. The only thing that Vasya remembers, is a non-empty ...
The first line of the input contains the string received by Kate. The number of digits in this string does not exceed 1<=000<=000. The second line contains the substring of *n* which Vasya remembers. This string can contain leading zeroes. It is guaranteed that the input data is correct, and the answer always exists...
Print the smalles integer *n* which Vasya could pass to Kate.
[ "003512\n021\n", "199966633300\n63\n" ]
[ "30021\n", "3036366999\n" ]
none
[ { "input": "003512\n021", "output": "30021" }, { "input": "199966633300\n63", "output": "3036366999" }, { "input": "01\n0", "output": "0" }, { "input": "0000454312911\n9213544", "output": "92135440000" }, { "input": "13\n3", "output": "3" }, { "input":...
46
0
0
14,801
17
Hierarchy
[ "dfs and similar", "dsu", "greedy", "shortest paths" ]
B. Hierarchy
2
64
Nick's company employed *n* people. Now Nick needs to build a tree hierarchy of Β«supervisor-surbodinateΒ» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are *m* applications written in the following form: Β«employee *a**i* is ready to become a supervisor of emp...
The first input line contains integer *n* (1<=≀<=*n*<=≀<=1000) β€” amount of employees in the company. The following line contains *n* space-separated numbers *q**j* (0<=≀<=*q**j*<=≀<=106)β€” the employees' qualifications. The following line contains number *m* (0<=≀<=*m*<=≀<=10000) β€” amount of received applications. The f...
Output the only line β€” the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.
[ "4\n7 2 3 1\n4\n1 2 5\n2 4 1\n3 4 1\n1 3 5\n", "3\n1 2 3\n2\n3 1 2\n3 1 3\n" ]
[ "11\n", "-1\n" ]
In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1.
[ { "input": "4\n7 2 3 1\n4\n1 2 5\n2 4 1\n3 4 1\n1 3 5", "output": "11" }, { "input": "3\n1 2 3\n2\n3 1 2\n3 1 3", "output": "-1" }, { "input": "1\n2\n0", "output": "0" }, { "input": "2\n5 3\n4\n1 2 0\n1 2 5\n1 2 0\n1 2 7", "output": "0" }, { "input": "3\n9 4 5\n5\...
154
0
0
14,806
675
Trains and Statistic
[ "data structures", "dp", "greedy" ]
null
null
Vasya commutes by train every day. There are *n* train stations in the city, and at the *i*-th station it's possible to buy only tickets to stations from *i*<=+<=1 to *a**i* inclusive. No tickets are sold at the last station. Let ρ*i*,<=*j* be the minimum number of tickets one needs to buy in order to get from station...
The first line of the input contains a single integer *n* (2<=≀<=*n*<=≀<=100<=000)Β β€” the number of stations. The second line contains *n*<=-<=1 integer *a**i* (*i*<=+<=1<=≀<=*a**i*<=≀<=*n*), the *i*-th of them means that at the *i*-th station one may buy tickets to each station from *i*<=+<=1 to *a**i* inclusive.
Print the sum of ρ*i*,<=*j* among all pairs of 1<=≀<=*i*<=&lt;<=*j*<=≀<=*n*.
[ "4\n4 4 4\n", "5\n2 3 5 5\n" ]
[ "6\n", "17\n" ]
In the first sample it's possible to get from any station to any other (with greater index) using only one ticket. The total number of pairs is 6, so the answer is also 6. Consider the second sample: - ρ<sub class="lower-index">1, 2</sub> = 1 - ρ<sub class="lower-index">1, 3</sub> = 2 - ρ<sub class="lower-index">...
[ { "input": "4\n4 4 4", "output": "6" }, { "input": "5\n2 3 5 5", "output": "17" }, { "input": "2\n2", "output": "1" }, { "input": "10\n2 10 8 7 8 8 10 9 10", "output": "63" }, { "input": "3\n3 3", "output": "3" }, { "input": "4\n3 3 4", "output": "...
108
23,142,400
0
14,811
638
Three-dimensional Turtle Super Computer
[ "brute force", "dfs and similar", "graphs" ]
null
null
A super computer has been built in the Turtle Academy of Sciences. The computer consists of *n*Β·*m*Β·*k* CPUs. The architecture was the paralellepiped of size *n*<=Γ—<=*m*<=Γ—<=*k*, split into 1<=Γ—<=1<=Γ—<=1 cells, each cell contains exactly one CPU. Thus, each CPU can be simultaneously identified as a group of three numbe...
The first line contains three integers *n*, *m* and *k* (1<=≀<=*n*,<=*m*,<=*k*<=≀<=100)Β β€” the dimensions of the Super Computer. Then *n* blocks follow, describing the current state of the processes. The blocks correspond to the layers of the Super Computer in the order from 1 to *n*. Each block consists of *m* lines,...
Print a single integer β€” the number of critical CPUs, that is, such that turning only this CPU off will disrupt some control.
[ "2 2 3\n000\n000\n\n111\n111\n", "3 3 3\n111\n111\n111\n\n111\n111\n111\n\n111\n111\n111\n", "1 1 10\n0101010101\n" ]
[ "2\n", "19\n", "0\n" ]
In the first sample the whole first layer of CPUs is malfunctional. In the second layer when CPU (2, 1, 2) turns off, it disrupts the control by CPU (2, 1, 3) over CPU (2, 1, 1), and when CPU (2, 2, 2) is turned off, it disrupts the control over CPU (2, 2, 3) by CPU (2, 2, 1). In the second sample all processors excep...
[ { "input": "2 2 3\n000\n000\n\n111\n111", "output": "2" }, { "input": "3 3 3\n111\n111\n111\n\n111\n111\n111\n\n111\n111\n111", "output": "19" }, { "input": "1 1 10\n0101010101", "output": "0" }, { "input": "1 1 1\n0", "output": "0" }, { "input": "1 1 1\n1", "...
46
4,812,800
0
14,812
606
Testing Robots
[ "implementation" ]
null
null
The Cybernetics Failures (CF) organisation made a prototype of a bomb technician robot. To find the possible problems it was decided to carry out a series of tests. At the beginning of each test the robot prototype will be placed in cell (*x*0,<=*y*0) of a rectangular squared field of size *x*<=Γ—<=*y*, after that a min...
The first line of the input contains four integers *x*, *y*, *x*0, *y*0 (1<=≀<=*x*,<=*y*<=≀<=500,<=1<=≀<=*x*0<=≀<=*x*,<=1<=≀<=*y*0<=≀<=*y*)Β β€” the sizes of the field and the starting coordinates of the robot. The coordinate axis *X* is directed downwards and axis *Y* is directed to the right. The second line contains a...
Print the sequence consisting of (*length*(*s*)<=+<=1) numbers. On the *k*-th position, starting with zero, print the number of tests where the robot will run exactly *k* commands before it blows up.
[ "3 4 2 2\nUURDRDRL\n", "2 2 2 2\nULD\n" ]
[ "1 1 0 1 1 1 1 0 6\n", "1 1 1 1\n" ]
In the first sample, if we exclude the probable impact of the mines, the robot's route will look like that: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/16bfda1e4f41cc00665c31f0a1d754d68cd9b4ab.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
[ { "input": "3 4 2 2\nUURDRDRL", "output": "1 1 0 1 1 1 1 0 6" }, { "input": "2 2 2 2\nULD", "output": "1 1 1 1" }, { "input": "1 1 1 1\nURDLUURRDDLLURDL", "output": "1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0" }, { "input": "15 17 8 9\nURRDLUULLDD", "output": "1 1 1 1 1 1 0 1 1 1 ...
109
0
0
14,883
15
Industrial Nim
[ "games" ]
C. Industrial Nim
2
64
There are *n* stone quarries in Petrograd. Each quarry owns *m**i* dumpers (1<=≀<=*i*<=≀<=*n*). It is known that the first dumper of the *i*-th quarry has *x**i* stones in it, the second dumper has *x**i*<=+<=1 stones in it, the third has *x**i*<=+<=2, and the *m**i*-th dumper (the last for the *i*-th quarry) has *x**...
The first line of the input contains one integer number *n* (1<=≀<=*n*<=≀<=105) β€” the amount of quarries. Then there follow *n* lines, each of them contains two space-separated integers *x**i* and *m**i* (1<=≀<=*x**i*,<=*m**i*<=≀<=1016) β€” the amount of stones in the first dumper of the *i*-th quarry and the number of d...
Output Β«tolikΒ» if the oligarch who takes a stone first wins, and Β«bolikΒ» otherwise.
[ "2\n2 1\n3 2\n", "4\n1 1\n1 1\n1 1\n1 1\n" ]
[ "tolik\n", "bolik\n" ]
none
[ { "input": "2\n2 1\n3 2", "output": "tolik" }, { "input": "4\n1 1\n1 1\n1 1\n1 1", "output": "bolik" }, { "input": "10\n2 3\n1 4\n5 8\n4 10\n10 8\n7 2\n1 2\n1 7\n4 10\n5 3", "output": "tolik" }, { "input": "20\n8 6\n6 3\n2 9\n7 8\n9 1\n2 4\n3 6\n6 3\n5 6\n5 3\n6 5\n2 10\n2 9\...
216
6,656,000
0
14,885
986
Oppa Funcan Style Remastered
[ "graphs", "math", "number theory", "shortest paths" ]
null
null
Surely you have seen insane videos by South Korean rapper PSY, such as "Gangnam Style", "Gentleman" and "Daddy". You might also hear that PSY has been recording video "Oppa Funcan Style" two years ago (unfortunately we couldn't find it on the internet). We will remind you what this hit looked like (you can find origina...
In the first line of input there is one integer $t$ ($1 \le t \le 10^{4}$)Β β€” the number of options for $n$ and $k$ to check. In the next $t$ lines the options are given: each option is described with two integers $n$ and $k$ ($1 \le n \le 10^{18}$, $1 \le k \le 10^{15}$)Β β€” the number of dancers and the duration in sec...
Print $t$ lines. If the $i$-th option of the video is feasible, print "YES" (without quotes) in $i$-th line, otherwise print "NO" (without quotes).
[ "3\n7 7\n3 8\n5 6\n" ]
[ "YES\nNO\nYES\n" ]
none
[]
93
0
0
14,887
59
Shortest Path
[ "graphs", "shortest paths" ]
E. Shortest Path
3
256
In Ancient Berland there were *n* cities and *m* two-way roads of equal length. The cities are numbered with integers from 1 to *n* inclusively. According to an ancient superstition, if a traveller visits three cities *a**i*, *b**i*, *c**i* in row, without visiting other cities between them, a great disaster awaits him...
The first line contains three integers *n*, *m*, *k* (2<=≀<=*n*<=≀<=3000,<=1<=≀<=*m*<=≀<=20000,<=0<=≀<=*k*<=≀<=105) which are the number of cities, the number of roads and the number of the forbidden triplets correspondingly. Then follow *m* lines each containing two integers *x**i*, *y**i* (1<=≀<=*x**i*,<=*y**i*<=≀<...
If there are no path from 1 to *n* print -1. Otherwise on the first line print the number of roads *d* along the shortest path from the city 1 to the city *n*. On the second line print *d*<=+<=1 numbers β€” any of the possible shortest paths for Vasya. The path should start in the city 1 and end in the city *n*.
[ "4 4 1\n1 2\n2 3\n3 4\n1 3\n1 4 3\n", "3 1 0\n1 2\n", "4 4 2\n1 2\n2 3\n3 4\n1 3\n1 2 3\n1 3 4\n" ]
[ "2\n1 3 4\n", "-1\n", "4\n1 3 2 3 4\n" ]
none
[ { "input": "4 4 1\n1 2\n2 3\n3 4\n1 3\n1 4 3", "output": "2\n1 3 4" }, { "input": "3 1 0\n1 2", "output": "-1" }, { "input": "4 4 2\n1 2\n2 3\n3 4\n1 3\n1 2 3\n1 3 4", "output": "4\n1 3 2 3 4" }, { "input": "4 4 1\n1 2\n2 3\n3 4\n1 3\n1 2 3", "output": "2\n1 3 4" }, {...
3,000
21,401,600
0
14,916
894
Ralph And His Magic Field
[ "combinatorics", "constructive algorithms", "math", "number theory" ]
null
null
Ralph has a magic field which is divided into *n*<=Γ—<=*m* blocks. That is to say, there are *n* rows and *m* columns on the field. Ralph can put an integer in each block. However, the magic field doesn't always work properly. It works only if the product of integers in each row and each column equals to *k*, where *k* ...
The only line contains three integers *n*, *m* and *k* (1<=≀<=*n*,<=*m*<=≀<=1018, *k* is either 1 or -1).
Print a single number denoting the answer modulo 1000000007.
[ "1 1 -1\n", "1 3 1\n", "3 3 -1\n" ]
[ "1\n", "1\n", "16\n" ]
In the first example the only way is to put -1 into the only block. In the second example the only way is to put 1 into every block.
[ { "input": "1 1 -1", "output": "1" }, { "input": "1 3 1", "output": "1" }, { "input": "3 3 -1", "output": "16" }, { "input": "2 7 1", "output": "64" }, { "input": "1 1 1", "output": "1" }, { "input": "2 4 -1", "output": "8" }, { "input": "1...
62
0
0
14,937
527
Error Correct System
[ "greedy" ]
null
null
Ford Prefect got a job as a web developer for a small company that makes towels. His current work task is to create a search engine for the website of the company. During the development process, he needs to write a subroutine for comparing strings *S* and *T* of equal length to be "similar". After a brief search on th...
The first line contains integer *n* (1<=≀<=*n*<=≀<=200<=000) β€” the length of strings *S* and *T*. The second line contains string *S*. The third line contains string *T*. Each of the lines only contains lowercase Latin letters.
In the first line, print number *x* β€” the minimum possible Hamming distance between strings *S* and *T* if you swap at most one pair of letters in *S*. In the second line, either print the indexes *i* and *j* (1<=≀<=*i*,<=*j*<=≀<=*n*, *i*<=β‰ <=*j*), if reaching the minimum possible distance is possible by swapping lett...
[ "9\npergament\npermanent\n", "6\nwookie\ncookie\n", "4\npetr\negor\n", "6\ndouble\nbundle\n" ]
[ "1\n4 6\n", "1\n-1 -1\n", "2\n1 2\n", "2\n4 1\n" ]
In the second test it is acceptable to print *i* = 2, *j* = 3.
[ { "input": "9\npergament\npermanent", "output": "1\n4 6" }, { "input": "6\nwookie\ncookie", "output": "1\n-1 -1" }, { "input": "4\npetr\negor", "output": "2\n1 2" }, { "input": "6\ndouble\nbundle", "output": "2\n4 1" }, { "input": "1\na\na", "output": "0\n-1 -...
499
1,126,400
3
14,961
567
One-Dimensional Battle Ships
[ "binary search", "data structures", "greedy", "sortings" ]
null
null
Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting of *n* square cells (that is, on a 1<=Γ—<=*n* table). At the beginning of the game Alice puts *k* ships on the field without telling their positions to Bob. Each ship looks as a 1<=Γ—<=*a* rectangle (that is,...
The first line of the input contains three integers: *n*, *k* and *a* (1<=≀<=*n*,<=*k*,<=*a*<=≀<=2Β·105) β€” the size of the field, the number of the ships and the size of each ship. It is guaranteed that the *n*, *k* and *a* are such that you can put *k* ships of size *a* on the field, so that no two ships intersect or t...
Print a single integer β€” the number of such Bob's first move, after which you can be sure that Alice lied. Bob's moves are numbered from 1 to *m* in the order the were made. If the sought move doesn't exist, then print "-1".
[ "11 3 3\n5\n4 8 6 1 11\n", "5 1 3\n2\n1 5\n", "5 1 3\n1\n3\n" ]
[ "3\n", "-1\n", "1\n" ]
none
[ { "input": "11 3 3\n5\n4 8 6 1 11", "output": "3" }, { "input": "5 1 3\n2\n1 5", "output": "-1" }, { "input": "5 1 3\n1\n3", "output": "1" }, { "input": "1 1 1\n1\n1", "output": "1" }, { "input": "5000 1660 2\n20\n1 100 18 102 300 81 19 25 44 88 1337 4999 1054 120...
92
0
0
15,025
621
Wet Shark and Flowers
[ "combinatorics", "math", "number theory", "probabilities" ]
null
null
There are *n* sharks who grow flowers for Wet Shark. They are all sitting around the table, such that sharks *i* and *i*<=+<=1 are neighbours for all *i* from 1 to *n*<=-<=1. Sharks *n* and 1 are neighbours too. Each shark will grow some number of flowers *s**i*. For *i*-th shark value *s**i* is random integer equipro...
The first line of the input contains two space-separated integers *n* and *p* (3<=≀<=*n*<=≀<=100<=000,<=2<=≀<=*p*<=≀<=109)Β β€” the number of sharks and Wet Shark's favourite prime number. It is guaranteed that *p* is prime. The *i*-th of the following *n* lines contains information about *i*-th sharkΒ β€” two space-separat...
Print a single real number β€” the expected number of dollars that the sharks receive in total. You answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer...
[ "3 2\n1 2\n420 421\n420420 420421\n", "3 5\n1 4\n2 3\n11 14\n" ]
[ "4500.0\n", "0.0\n" ]
A prime number is a positive integer number that is divisible only by 1 and itself. 1 is not considered to be prime. Consider the first sample. First shark grows some number of flowers from 1 to 2, second sharks grows from 420 to 421 flowers and third from 420420 to 420421. There are eight cases for the quantities of ...
[ { "input": "3 2\n1 2\n420 421\n420420 420421", "output": "4500.0" }, { "input": "3 5\n1 4\n2 3\n11 14", "output": "0.0" }, { "input": "3 3\n3 3\n2 4\n1 1", "output": "4666.666666666667" }, { "input": "5 5\n5 204\n420 469\n417 480\n442 443\n44 46", "output": "3451.25" },...
327
7,475,200
3
15,086
831
Jury Marks
[ "brute force", "constructive algorithms" ]
null
null
Polycarp watched TV-show where *k* jury members one by one rated a participant by adding him a certain number of points (may be negative, i.Β e. points were subtracted). Initially the participant had some score, and each the marks were one by one added to his score. It is known that the *i*-th jury member gave *a**i* po...
The first line contains two integers *k* and *n* (1<=≀<=*n*<=≀<=*k*<=≀<=2<=000) β€” the number of jury members and the number of scores Polycarp remembers. The second line contains *k* integers *a*1,<=*a*2,<=...,<=*a**k* (<=-<=2<=000<=≀<=*a**i*<=≀<=2<=000) β€” jury's marks in chronological order. The third line contains ...
Print the number of options for the score the participant could have before the judges rated the participant. If Polycarp messes something up and there is no options, print "0" (without quotes).
[ "4 1\n-5 5 0 20\n10\n", "2 2\n-2000 -2000\n3998000 4000000\n" ]
[ "3\n", "1\n" ]
The answer for the first example is 3 because initially the participant could have  - 10, 10 or 15 points. In the second example there is only one correct initial score equaling to 4 002 000.
[ { "input": "4 1\n-5 5 0 20\n10", "output": "3" }, { "input": "2 2\n-2000 -2000\n3998000 4000000", "output": "1" }, { "input": "1 1\n-577\n1273042", "output": "1" }, { "input": "2 1\n614 -1943\n3874445", "output": "2" }, { "input": "3 1\n1416 -1483 1844\n3261895", ...
170
138,342,400
3
15,112
762
Tree nesting
[ "combinatorics", "graphs", "trees" ]
null
null
You are given two trees (connected undirected acyclic graphs) *S* and *T*. Count the number of subtrees (connected subgraphs) of *S* that are isomorphic to tree *T*. Since this number can get quite large, output it modulo 109<=+<=7. Two subtrees of tree *S* are considered different, if there exists a vertex in *S* th...
The first line contains a single integer |*S*| (1<=≀<=|*S*|<=≀<=1000) β€” the number of vertices of tree *S*. Next |*S*|<=-<=1 lines contain two integers *u**i* and *v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=|*S*|) and describe edges of tree *S*. The next line contains a single integer |*T*| (1<=≀<=|*T*|<=≀<=12) β€” the number of...
On the first line output a single integer β€” the answer to the given task modulo 109<=+<=7.
[ "5\n1 2\n2 3\n3 4\n4 5\n3\n1 2\n2 3\n", "3\n2 3\n3 1\n3\n1 2\n1 3\n", "7\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n4\n4 1\n4 2\n4 3\n", "5\n1 2\n2 3\n3 4\n4 5\n4\n4 1\n4 2\n4 3\n" ]
[ "3\n", "1\n", "20\n", "0\n" ]
none
[ { "input": "5\n1 2\n2 3\n3 4\n4 5\n3\n1 2\n2 3", "output": "3" }, { "input": "3\n2 3\n3 1\n3\n1 2\n1 3", "output": "1" }, { "input": "7\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n4\n4 1\n4 2\n4 3", "output": "20" }, { "input": "5\n1 2\n2 3\n3 4\n4 5\n4\n4 1\n4 2\n4 3", "output": "0" ...
61
2,048,000
0
15,123
420
Online Meeting
[ "implementation" ]
null
null
Nearly each project of the F company has a whole team of developers working on it. They often are in different rooms of the office in different cities and even countries. To keep in touch and track the results of the project, the F company conducts shared online meetings in a Spyke chat. One day the director of the F ...
The first line contains integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=105) β€” the number of team participants and the number of messages. Each of the next *m* lines contains a message in the format: - '+ *id*': the record means that the person with number *id* (1<=≀<=*id*<=≀<=*n*) has logged on to the meeting. - '- *id*'...
In the first line print integer *k* (0<=≀<=*k*<=≀<=*n*) β€” how many people can be leaders. In the next line, print *k* integers in the increasing order β€” the numbers of the people who can be leaders. If the data is such that no member of the team can be a leader, print a single number 0.
[ "5 4\n+ 1\n+ 2\n- 2\n- 1\n", "3 2\n+ 1\n- 2\n", "2 4\n+ 1\n- 1\n+ 2\n- 2\n", "5 6\n+ 1\n- 1\n- 3\n+ 3\n+ 4\n- 4\n", "2 4\n+ 1\n- 2\n+ 2\n- 1\n" ]
[ "4\n1 3 4 5 ", "1\n3 ", "0\n", "3\n2 3 5 ", "0\n" ]
none
[ { "input": "5 4\n+ 1\n+ 2\n- 2\n- 1", "output": "4\n1 3 4 5 " }, { "input": "3 2\n+ 1\n- 2", "output": "1\n3 " }, { "input": "2 4\n+ 1\n- 1\n+ 2\n- 2", "output": "0" }, { "input": "5 6\n+ 1\n- 1\n- 3\n+ 3\n+ 4\n- 4", "output": "3\n2 3 5 " }, { "input": "2 4\n+ 1\n...
124
0
0
15,202
949
A Leapfrog in the Array
[ "constructive algorithms", "math" ]
null
null
Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm. Let's consider that i...
The first line contains two integers *n* and *q* (1<=≀<=*n*<=≀<=1018, 1<=≀<=*q*<=≀<=200<=000), the number of elements in the array and the number of queries for which it is needed to find the answer. Next *q* lines contain integers *x**i* (1<=≀<=*x**i*<=≀<=*n*), the indices of cells for which it is necessary to output...
For each of *q* queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes.
[ "4 3\n2\n3\n4\n", "13 4\n10\n5\n4\n8\n" ]
[ "3\n2\n4\n", "13\n3\n8\n9\n" ]
The first example is shown in the picture. In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7].
[ { "input": "4 3\n2\n3\n4", "output": "3\n2\n4" }, { "input": "13 4\n10\n5\n4\n8", "output": "13\n3\n8\n9" }, { "input": "2 2\n1\n2", "output": "1\n2" }, { "input": "1 1\n1", "output": "1" }, { "input": "3 3\n3\n2\n1", "output": "2\n3\n1" }, { "input": ...
2,000
5,632,000
0
15,218
430
Balls Game
[ "brute force", "two pointers" ]
null
null
Iahub is training for the IOI. What is a better way to train than playing a Zuma-like game? There are *n* balls put in a row. Each ball is colored in one of *k* colors. Initially the row doesn't contain three or more contiguous balls with the same color. Iahub has a single ball of color *x*. He can insert his ball at...
The first line of input contains three integers: *n* (1<=≀<=*n*<=≀<=100), *k* (1<=≀<=*k*<=≀<=100) and *x* (1<=≀<=*x*<=≀<=*k*). The next line contains *n* space-separated integers *c*1,<=*c*2,<=...,<=*c**n* (1<=≀<=*c**i*<=≀<=*k*). Number *c**i* means that the *i*-th ball in the row has color *c**i*. It is guaranteed th...
Print a single integer β€” the maximum number of balls Iahub can destroy.
[ "6 2 2\n1 1 2 2 1 1\n", "1 1 1\n1\n" ]
[ "6\n", "0\n" ]
none
[ { "input": "6 2 2\n1 1 2 2 1 1", "output": "6" }, { "input": "1 1 1\n1", "output": "0" }, { "input": "10 2 1\n2 1 2 2 1 2 2 1 1 2", "output": "5" }, { "input": "50 2 1\n1 1 2 2 1 2 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 2 1 2 1 2 1 2 2 1 1 2 2 1 1 2 2 1 2 1 1 2 1 1 2 2 1 1 2", "...
109
0
0
15,301
900
Unusual Sequences
[ "bitmasks", "combinatorics", "dp", "math", "number theory" ]
null
null
Count the number of distinct sequences *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*) consisting of positive integers such that *gcd*(*a*1,<=*a*2,<=...,<=*a**n*)<==<=*x* and . As this number could be large, print the answer modulo 109<=+<=7. *gcd* here means the [greatest common divisor](https://en.wikipedia.org/wiki/Great...
The only line contains two positive integers *x* and *y* (1<=≀<=*x*,<=*y*<=≀<=109).
Print the number of such sequences modulo 109<=+<=7.
[ "3 9\n", "5 8\n" ]
[ "3\n", "0\n" ]
There are three suitable sequences in the first test: (3, 3, 3), (3, 6), (6, 3). There are no suitable sequences in the second test.
[ { "input": "3 9", "output": "3" }, { "input": "5 8", "output": "0" }, { "input": "2 12", "output": "27" }, { "input": "1 8", "output": "120" }, { "input": "1 9", "output": "252" }, { "input": "1000000000 1000000000", "output": "1" }, { "inp...
93
0
0
15,324
963
Destruction of a Tree
[ "constructive algorithms", "dfs and similar", "dp", "greedy", "trees" ]
null
null
You are given a tree (a graph with *n* vertices and *n*<=-<=1 edges in which it's possible to reach any vertex from any other vertex using only its edges). A vertex can be destroyed if this vertex has even degree. If you destroy a vertex, all edges connected to it are also deleted. Destroy all vertices in the given t...
The first line contains integer *n* (1<=≀<=*n*<=≀<=2Β·105)Β β€” number of vertices in a tree. The second line contains *n* integers *p*1,<=*p*2,<=...,<=*p**n* (0<=≀<=*p**i*<=≀<=*n*). If *p**i*<=β‰ <=0 there is an edge between vertices *i* and *p**i*. It is guaranteed that the given graph is a tree.
If it's possible to destroy all vertices, print "YES" (without quotes), otherwise print "NO" (without quotes). If it's possible to destroy all vertices, in the next *n* lines print the indices of the vertices in order you destroy them. If there are multiple correct answers, print any.
[ "5\n0 1 2 1 2\n", "4\n0 1 2 3\n" ]
[ "YES\n1\n2\n3\n5\n4\n", "NO\n" ]
In the first example at first you have to remove the vertex with index 1 (after that, the edges (1, 2) and (1, 4) are removed), then the vertex with index 2 (and edges (2, 3) and (2, 5) are removed). After that there are no edges in the tree, so you can remove remaining vertices in any order.
[ { "input": "5\n0 1 2 1 2", "output": "YES\n1\n2\n3\n5\n4" }, { "input": "4\n0 1 2 3", "output": "NO" }, { "input": "1\n0", "output": "YES\n1" }, { "input": "8\n3 1 4 0 4 2 4 5", "output": "NO" }, { "input": "100\n81 96 65 28 4 40 5 49 5 89 48 70 94 70 17 58 58 1 6...
46
0
0
15,402
533
Berland Miners
[ "binary search", "data structures", "dfs and similar", "greedy", "trees" ]
null
null
The biggest gold mine in Berland consists of *n* caves, connected by *n*<=-<=1 transitions. The entrance to the mine leads to the cave number 1, it is possible to go from it to any remaining cave of the mine by moving along the transitions. The mine is being developed by the InMine Inc., *k* miners work for it. Each ...
The first line contains integer *n* (1<=≀<=*n*<=≀<=5Β·105) β€” the number of caves in the mine. Then follows a line consisting of *n* positive integers *h*1,<=*h*2,<=...,<=*h**n* (1<=≀<=*h**i*<=≀<=109), where *h**i* is the height of the ceiling in the *i*-th cave. Next *n*<=-<=1 lines contain the descriptions of transit...
In the single line print the minimum number of meters that you need to raise the ceiling by in some cave so that all miners could be sorted into caves and be happy about the work conditions. If it is impossible to do, print <=-<=1. If it is initially possible and there's no need to raise any ceiling, print 0.
[ "6\n5 8 4 6 3 12\n1 2\n1 3\n4 2\n2 5\n6 3\n6\n7 4 2 5 3 11\n", "7\n10 14 7 12 4 50 1\n1 2\n2 3\n2 4\n5 1\n6 5\n1 7\n6\n7 3 4 8 8 10\n", "3\n4 2 8\n1 2\n1 3\n2\n17 15\n" ]
[ "6\n", "0\n", "-1\n" ]
In the first sample test we should increase ceiling height in the first cave from 5 to 11. After that we can distribute miners as following (first goes index of a miner, then index of a cave): <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5d9b256bdaa3f5b0f9a3fc3b9f56256306a7a570.png" styl...
[]
46
0
0
15,480
846
Monitor
[ "binary search", "data structures" ]
null
null
Recently Luba bought a monitor. Monitor is a rectangular matrix of size *n*<=Γ—<=*m*. But then she started to notice that some pixels cease to work properly. Luba thinks that the monitor will become broken the first moment when it contains a square *k*<=Γ—<=*k* consisting entirely of broken pixels. She knows that *q* pix...
The first line contains four integer numbers *n*,<=*m*,<=*k*,<=*q*Β (1<=≀<=*n*,<=*m*<=≀<=500,<=1<=≀<=*k*<=≀<=*min*(*n*,<=*m*),<=0<=≀<=*q*<=≀<=*n*Β·*m*) β€” the length and width of the monitor, the size of a rectangle such that the monitor is broken if there is a broken rectangle with this size, and the number of broken pix...
Print one number β€” the minimum moment the monitor became broken, or "-1" if it's still not broken after these *q* pixels stopped working.
[ "2 3 2 5\n2 1 8\n2 2 8\n1 2 1\n1 3 4\n2 3 2\n", "3 3 2 5\n1 2 2\n2 2 1\n2 3 5\n3 2 10\n2 1 100\n" ]
[ "8\n", "-1\n" ]
none
[ { "input": "2 3 2 5\n2 1 8\n2 2 8\n1 2 1\n1 3 4\n2 3 2", "output": "8" }, { "input": "3 3 2 5\n1 2 2\n2 2 1\n2 3 5\n3 2 10\n2 1 100", "output": "-1" }, { "input": "29 50 5 29\n21 42 1565821\n21 43 53275635\n21 44 2717830\n21 45 9579585\n21 46 20725775\n22 42 2568372\n22 43 9584662\n22 44...
2,000
5,734,400
0
15,482
203
Game on Paper
[ "brute force", "implementation" ]
null
null
One not particularly beautiful evening Valera got very bored. To amuse himself a little bit, he found the following game. He took a checkered white square piece of paper, consisting of *n*<=Γ—<=*n* cells. After that, he started to paint the white cells black one after the other. In total he painted *m* different cells ...
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=1000, 1<=≀<=*m*<=≀<=*min*(*n*Β·*n*,<=105)) β€” the size of the squared piece of paper and the number of moves, correspondingly. Then, *m* lines contain the description of the moves. The *i*-th line contains two integers *x**i*, *y**i* (1<=≀<=*x**i*,<=*y**i*...
On a single line print the answer to the problem β€” the minimum number of the move after which the piece of paper has a black square with side 3. If no such move exists, print -1.
[ "4 11\n1 1\n1 2\n1 3\n2 2\n2 3\n1 4\n2 4\n3 4\n3 2\n3 3\n4 1\n", "4 12\n1 1\n1 2\n1 3\n2 2\n2 3\n1 4\n2 4\n3 4\n3 2\n4 2\n4 1\n3 1\n" ]
[ "10\n", "-1\n" ]
none
[ { "input": "4 11\n1 1\n1 2\n1 3\n2 2\n2 3\n1 4\n2 4\n3 4\n3 2\n3 3\n4 1", "output": "10" }, { "input": "4 12\n1 1\n1 2\n1 3\n2 2\n2 3\n1 4\n2 4\n3 4\n3 2\n4 2\n4 1\n3 1", "output": "-1" }, { "input": "3 1\n1 3", "output": "-1" }, { "input": "3 8\n1 3\n3 3\n2 2\n3 2\n1 1\n1 2\...
1,964
4,812,800
3
15,527
195
Try and Catch
[ "expression parsing", "implementation" ]
null
null
Vasya is developing his own programming language VPL (Vasya Programming Language). Right now he is busy making the system of exceptions. He thinks that the system of exceptions must function like that. The exceptions are processed by try-catch-blocks. There are two operators that work with the blocks: 1. The try oper...
The first line contains a single integer: *n* (1<=≀<=*n*<=≀<=105) the number of lines in the program. Next *n* lines contain the program in language VPL. Each line contains no more than one operator. It means that input file can contain empty lines and lines, consisting only of spaces. The program contains only operat...
Print the message the screen will show after the given program is executed.
[ "8\ntry\n try\n throw ( AE ) \n catch ( BE, \"BE in line 3\")\n\n try\n catch(AE, \"AE in line 5\") \ncatch(AE,\"AE somewhere\")\n", "8\ntry\n try\n throw ( AE ) \n catch ( AE, \"AE in line 3\")\n\n try\n catch(BE, \"BE in line 5\") \ncatch(AE,\"AE somewhere\")\n", "8\ntry\...
[ "AE somewhere\n", "AE in line 3\n", "Unhandled Exception\n" ]
In the first sample there are 2 try-catch-blocks such that try operator is described earlier than throw operator and catch operator is described later than throw operator: try-catch(BE,"BE in line 3") and try-catch(AE,"AE somewhere"). Exception type is AE, so the second block will be activated, because operator catch(A...
[ { "input": "8\ntry\n try\n throw ( AE ) \n catch ( BE, \"BE in line 3\")\n\n try\n catch(AE, \"AE in line 5\") \ncatch(AE,\"AE somewhere\")", "output": "AE somewhere" }, { "input": "8\ntry\n try\n throw ( AE ) \n catch ( AE, \"AE in line 3\")\n\n try\n catch(BE,...
1,278
10,240,000
0
15,549
792
Paths in a Complete Binary Tree
[ "bitmasks", "trees" ]
null
null
*T* is a complete binary tree consisting of *n* vertices. It means that exactly one vertex is a root, and each vertex is either a leaf (and doesn't have children) or an inner node (and has exactly two children). All leaves of a complete binary tree have the same depth (distance from the root). So *n* is a number such t...
The first line contains two integer numbers *n* and *q* (1<=≀<=*n*<=≀<=1018, *q*<=β‰₯<=1). *n* is such that *n*<=+<=1 is a power of 2. The next 2*q* lines represent queries; each query consists of two consecutive lines. The first of these two lines contains *u**i* (1<=≀<=*u**i*<=≀<=*n*), the second contains non-empty st...
Print *q* numbers, *i*-th number must be the answer to the *i*-th query.
[ "15 2\n4\nUURL\n8\nLRLLLLLLLL\n" ]
[ "10\n5\n" ]
none
[ { "input": "15 2\n4\nUURL\n8\nLRLLLLLLLL", "output": "10\n5" }, { "input": "1 1\n1\nL", "output": "1" }, { "input": "1 1\n1\nR", "output": "1" }, { "input": "1 1\n1\nU", "output": "1" }, { "input": "1 10\n1\nURLRLULUR\n1\nLRRRURULULL\n1\nLURURRUUUU\n1\nRRULLLRRUL\...
108
409,600
0
15,550
845
Shortest Path Problem?
[ "dfs and similar", "graphs", "math" ]
null
null
You are given an undirected graph with weighted edges. The length of some path between two vertices is the bitwise xor of weights of all edges belonging to this path (if some edge is traversed more than once, then it is included in bitwise xor the same number of times). You have to find the minimum length of path betwe...
The first line contains two numbers *n* and *m* (1<=≀<=*n*<=≀<=100000, *n*<=-<=1<=≀<=*m*<=≀<=100000) β€” the number of vertices and the number of edges, respectively. Then *m* lines follow, each line containing three integer numbers *x*, *y* and *w* (1<=≀<=*x*,<=*y*<=≀<=*n*, 0<=≀<=*w*<=≀<=108). These numbers denote an e...
Print one number β€” the minimum length of path between vertices 1 and *n*.
[ "3 3\n1 2 3\n1 3 2\n3 2 0\n", "2 2\n1 1 3\n1 2 3\n" ]
[ "2\n", "0\n" ]
none
[ { "input": "3 3\n1 2 3\n1 3 2\n3 2 0", "output": "2" }, { "input": "2 2\n1 1 3\n1 2 3", "output": "0" }, { "input": "10 20\n8 5 64\n5 6 48\n4 5 91\n10 1 2\n3 4 51\n8 2 74\n6 1 98\n3 10 24\n2 10 35\n8 7 52\n10 5 72\n5 9 25\n2 9 65\n7 4 69\n5 7 26\n7 2 44\n6 8 61\n3 5 43\n10 7 33\n4 2 28",...
545
19,968,000
-1
15,577
999
Equalize the Remainders
[ "data structures", "greedy", "implementation" ]
null
null
You are given an array consisting of $n$ integers $a_1, a_2, \dots, a_n$, and a positive integer $m$. It is guaranteed that $m$ is a divisor of $n$. In a single move, you can choose any position $i$ between $1$ and $n$ and increase $a_i$ by $1$. Let's calculate $c_r$ ($0 \le r \le m-1)$ β€” the number of elements havin...
The first line of input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le n$). It is guaranteed that $m$ is a divisor of $n$. The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$), the elements of the array.
In the first line, print a single integer β€” the minimum number of moves required to satisfy the following condition: for each remainder from $0$ to $m - 1$, the number of elements of the array having this remainder equals $\frac{n}{m}$. In the second line, print any array satisfying the condition and can be obtained f...
[ "6 3\n3 2 0 6 10 12\n", "4 2\n0 1 2 3\n" ]
[ "3\n3 2 0 7 10 14 \n", "0\n0 1 2 3 \n" ]
none
[ { "input": "6 3\n3 2 0 6 10 12", "output": "3\n3 2 0 7 10 14 " }, { "input": "4 2\n0 1 2 3", "output": "0\n0 1 2 3 " }, { "input": "1 1\n1000000000", "output": "0\n1000000000 " }, { "input": "6 3\n3 2 0 6 10 11", "output": "1\n3 2 0 7 10 11 " }, { "input": "100 25...
3,000
40,448,000
0
15,593
730
Car Repair Shop
[ "implementation" ]
null
null
Polycarp starts his own business. Tomorrow will be the first working day of his car repair shop. For now the car repair shop is very small and only one car can be repaired at a given time. Polycarp is good at marketing, so he has already collected *n* requests from clients. The requests are numbered from 1 to *n* in o...
The first line contains integer *n* (1<=≀<=*n*<=≀<=200) β€” the number of requests from clients. The following *n* lines contain requests, one request per line. The *i*-th request is given as the pair of integers *s**i*,<=*d**i* (1<=≀<=*s**i*<=≀<=109, 1<=≀<=*d**i*<=≀<=5Β·106), where *s**i* is the preferred time to start ...
Print *n* lines. The *i*-th line should contain two integers β€” the start day to repair the *i*-th car and the finish day to repair the *i*-th car.
[ "3\n9 2\n7 3\n2 4\n", "4\n1000000000 1000000\n1000000000 1000000\n100000000 1000000\n1000000000 1000000\n" ]
[ "9 10\n1 3\n4 7\n", "1000000000 1000999999\n1 1000000\n100000000 100999999\n1000001 2000000\n" ]
none
[ { "input": "3\n9 2\n7 3\n2 4", "output": "9 10\n1 3\n4 7" }, { "input": "4\n1000000000 1000000\n1000000000 1000000\n100000000 1000000\n1000000000 1000000", "output": "1000000000 1000999999\n1 1000000\n100000000 100999999\n1000001 2000000" }, { "input": "1\n1 1", "output": "1 1" }, ...
0
0
-1
15,604
439
Devu and his Brother
[ "binary search", "sortings", "ternary search", "two pointers" ]
null
null
Devu and his brother love each other a lot. As they are super geeks, they only like to play with arrays. They are given two arrays *a* and *b* by their father. The array *a* is given to Devu and *b* to his brother. As Devu is really a naughty kid, he wants the minimum value of his array *a* should be at least as much...
The first line contains two space-separated integers *n*, *m* (1<=≀<=*n*,<=*m*<=≀<=105). The second line will contain *n* space-separated integers representing content of the array *a* (1<=≀<=*a**i*<=≀<=109). The third line will contain *m* space-separated integers representing content of the array *b* (1<=≀<=*b**i*<=≀...
You need to output a single integer representing the minimum number of operations needed to satisfy Devu's condition.
[ "2 2\n2 3\n3 5\n", "3 2\n1 2 3\n3 4\n", "3 2\n4 5 6\n1 2\n" ]
[ "3\n", "4\n", "0\n" ]
In example 1, you can increase *a*<sub class="lower-index">1</sub> by 1 and decrease *b*<sub class="lower-index">2</sub> by 1 and then again decrease *b*<sub class="lower-index">2</sub> by 1. Now array *a* will be [3; 3] and array *b* will also be [3; 3]. Here minimum element of *a* is at least as large as maximum elem...
[ { "input": "2 2\n2 3\n3 5", "output": "3" }, { "input": "3 2\n1 2 3\n3 4", "output": "4" }, { "input": "3 2\n4 5 6\n1 2", "output": "0" }, { "input": "10 10\n23 100 38 38 73 54 59 69 44 86\n100 100 100 100 100 100 100 100 100 100", "output": "416" }, { "input": "1...
171
10,444,800
0
15,626
0
none
[ "none" ]
null
null
Iahub and Iahubina went to a picnic in a forest full of trees. Less than 5 minutes passed before Iahub remembered of trees from programming. Moreover, he invented a new problem and Iahubina has to solve it, otherwise Iahub won't give her the food. Iahub asks Iahubina: can you build a rooted tree, such that - each i...
The first line of the input contains integer *n* (1<=≀<=*n*<=≀<=24). Next line contains *n* positive integers: the *i*-th number represents *c**i* (1<=≀<=*c**i*<=≀<=*n*).
Output on the first line "YES" (without quotes) if there exist at least one tree following Iahub's restrictions, otherwise output "NO" (without quotes).
[ "4\n1 1 1 4\n", "5\n1 1 5 2 1\n" ]
[ "YES", "NO" ]
none
[]
61
7,372,800
0
15,645
0
none
[ "none" ]
null
null
After years of hard work scientists invented an absolutely new e-reader display. The new display has a larger resolution, consumes less energy and its production is cheaper. And besides, one can bend it. The only inconvenience is highly unusual management. For that very reason the developers decided to leave the e-read...
The first line contains number *n* (1<=≀<=*n*<=≀<=2000). Next *n* lines contain *n* characters each: the description of the picture that needs to be shown. "0" represents the white color and "1" represents the black color.
Print one integer *z* β€” the least number of commands needed to display the picture.
[ "5\n01110\n10010\n10001\n10011\n11110\n" ]
[ "4\n" ]
none
[ { "input": "5\n01110\n10010\n10001\n10011\n11110", "output": "4" }, { "input": "4\n0000\n0111\n0001\n0001", "output": "1" }, { "input": "6\n100000\n010000\n001000\n000100\n000000\n000001", "output": "5" }, { "input": "10\n0000000000\n0000110000\n1001000000\n1000011110\n101111...
62
0
0
15,650
28
Bath Queue
[ "combinatorics", "dp", "probabilities" ]
C. Bath Queue
2
256
There are *n* students living in the campus. Every morning all students wake up at the same time and go to wash. There are *m* rooms with wash basins. The *i*-th of these rooms contains *a**i* wash basins. Every student independently select one the rooms with equal probability and goes to it. After all students selecte...
The first line contains two positive integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=50) β€” the amount of students and the amount of rooms. The second line contains *m* integers *a*1,<=*a*2,<=... ,<=*a**m* (1<=≀<=*a**i*<=≀<=50). *a**i* means the amount of wash basins in the *i*-th room.
Output single number: the expected value of the size of the largest queue. Your answer must have an absolute or relative error less than 10<=-<=9.
[ "1 1\n2\n", "2 2\n1 1\n", "2 3\n1 1 1\n", "7 5\n1 1 2 3 1\n" ]
[ "1.00000000000000000000\n", "1.50000000000000000000\n", "1.33333333333333350000\n", "2.50216960000000070000\n" ]
none
[ { "input": "1 1\n2", "output": "1.00000000000000000000" }, { "input": "2 2\n1 1", "output": "1.50000000000000000000" }, { "input": "2 3\n1 1 1", "output": "1.33333333333333350000" }, { "input": "7 5\n1 1 2 3 1", "output": "2.50216960000000070000" }, { "input": "10...
1,778
9,932,800
3.536999
15,685
862
Mahmoud and Ehab and the function
[ "binary search", "data structures", "sortings" ]
null
null
Dr. Evil is interested in math and functions, so he gave Mahmoud and Ehab array *a* of length *n* and array *b* of length *m*. He introduced a function *f*(*j*) which is defined for integers *j*, which satisfy 0<=≀<=*j*<=≀<=*m*<=-<=*n*. Suppose, *c**i*<==<=*a**i*<=-<=*b**i*<=+<=*j*. Then *f*(*j*)<==<=|*c*1<=-<=*c*2<=+<...
The first line contains three integers *n*,<=*m* and *q* (1<=≀<=*n*<=≀<=*m*<=≀<=105, 1<=≀<=*q*<=≀<=105)Β β€” number of elements in *a*, number of elements in *b* and number of queries, respectively. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n*. (<=-<=109<=≀<=*a**i*<=≀<=109)Β β€” elements of *a*. The thi...
The first line should contain the minimum value of the function *f* before any update. Then output *q* lines, the *i*-th of them should contain the minimum value of the function *f* after performing the *i*-th update .
[ "5 6 3\n1 2 3 4 5\n1 2 3 4 5 6\n1 1 10\n1 1 -9\n1 5 -1\n" ]
[ "0\n9\n0\n0\n" ]
For the first example before any updates it's optimal to choose *j* = 0, *f*(0) = |(1 - 1) - (2 - 2) + (3 - 3) - (4 - 4) + (5 - 5)| = |0| = 0. After the first update *a* becomes {11, 2, 3, 4, 5} and it's optimal to choose *j* = 1, *f*(1) = |(11 - 2) - (2 - 3) + (3 - 4) - (4 - 5) + (5 - 6) = |9| = 9. After the second ...
[ { "input": "5 6 3\n1 2 3 4 5\n1 2 3 4 5 6\n1 1 10\n1 1 -9\n1 5 -1", "output": "0\n9\n0\n0" }, { "input": "1 1 1\n937982044\n179683049\n1 1 821220804", "output": "758298995\n1579519799" } ]
46
5,529,600
0
15,694
156
Clues
[ "combinatorics", "graphs" ]
null
null
As Sherlock Holmes was investigating another crime, he found a certain number of clues. Also, he has already found direct links between some of those clues. The direct links between the clues are mutual. That is, the direct link between clues *A* and *B* and the direct link between clues *B* and *A* is the same thing. ...
The first line contains three space-separated integers *n*,<=*m*,<=*k* (1<=≀<=*n*<=≀<=105,<=0<=≀<=*m*<=≀<=105, 1<=≀<=*k*<=≀<=109) β€” the number of clues, the number of direct clue links that Holmes has already found and the divisor for the modulo operation. Each of next *m* lines contains two integers *a* and *b* (1<=≀...
Print the single number β€” the answer to the problem modulo *k*.
[ "2 0 1000000000\n", "3 0 100\n", "4 1 1000000000\n1 4\n" ]
[ "1\n", "3\n", "8\n" ]
The first sample only has two clues and Sherlock hasn't found any direct link between them yet. The only way to solve the crime is to find the link. The second sample has three clues and Sherlock hasn't found any direct links between them. He has to find two of three possible direct links between clues to solve the cr...
[ { "input": "2 0 1000000000", "output": "1" }, { "input": "3 0 100", "output": "3" }, { "input": "4 1 1000000000\n1 4", "output": "8" }, { "input": "6 4 100000\n1 4\n4 6\n6 1\n2 5", "output": "36" }, { "input": "10 0 123456789", "output": "100000000" }, { ...
1,964
13,721,600
-1
15,695
257
Sum
[ "greedy", "math" ]
null
null
Vasya has found a piece of paper with an array written on it. The array consists of *n* integers *a*1,<=*a*2,<=...,<=*a**n*. Vasya noticed that the following condition holds for the array *a**i*<=≀<=*a**i*<=+<=1<=≀<=2Β·*a**i* for any positive integer *i* (*i*<=&lt;<=*n*). Vasya wants to add either a "+" or a "-" before...
The first line contains integer *n* (1<=≀<=*n*<=≀<=105) β€” the size of the array. The second line contains space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=109) β€” the original array. It is guaranteed that the condition *a**i*<=≀<=*a**i*<=+<=1<=≀<=2Β·*a**i* fulfills for any positive integer *i* (*i*...
In a single line print the sequence of *n* characters "+" and "-", where the *i*-th character is the sign that is placed in front of number *a**i*. The value of the resulting expression *s* must fit into the limits 0<=≀<=*s*<=≀<=*a*1. If there are multiple solutions, you are allowed to print any of them.
[ "4\n1 2 3 5\n", "3\n3 3 5\n" ]
[ "+++-", "++-" ]
none
[ { "input": "4\n1 2 3 5", "output": "+++-" }, { "input": "3\n3 3 5", "output": "++-" }, { "input": "4\n2 4 5 6", "output": "-++-" }, { "input": "6\n3 5 10 11 12 20", "output": "++-++-" }, { "input": "10\n10 14 17 22 43 72 74 84 88 93", "output": "++---++--+" ...
2,000
10,956,800
0
15,698
490
Restoring Increasing Sequence
[ "binary search", "brute force", "greedy", "implementation" ]
null
null
Peter wrote on the board a strictly increasing sequence of positive integers *a*1,<=*a*2,<=...,<=*a**n*. Then Vasil replaced some digits in the numbers of this sequence by question marks. Thus, each question mark corresponds to exactly one lost digit. Restore the the original sequence knowing digits remaining on the b...
The first line of the input contains integer *n* (1<=≀<=*n*<=≀<=105) β€” the length of the sequence. Next *n* lines contain one element of the sequence each. Each element consists only of digits and question marks. No element starts from digit 0. Each element has length from 1 to 8 characters, inclusive.
If the answer exists, print in the first line "YES" (without the quotes). Next *n* lines must contain the sequence of positive integers β€” a possible variant of Peter's sequence. The found sequence must be strictly increasing, it must be transformed from the given one by replacing each question mark by a single digit. A...
[ "3\n?\n18\n1?\n", "2\n??\n?\n", "5\n12224\n12??5\n12226\n?0000\n?00000\n" ]
[ "YES\n1\n18\n19\n", "NO\n", "YES\n12224\n12225\n12226\n20000\n100000\n" ]
none
[ { "input": "3\n?\n18\n1?", "output": "YES\n1\n18\n19" }, { "input": "2\n??\n?", "output": "NO" }, { "input": "5\n12224\n12??5\n12226\n?0000\n?00000", "output": "YES\n12224\n12225\n12226\n20000\n100000" }, { "input": "10\n473883\n3499005\n4?74792\n58146??\n8?90593\n9203?71\n?3...
77
2,867,200
-1
15,704
930
Game with Tokens
[ "data structures", "games", "implementation" ]
null
null
Consider the following game for two players. There is one white token and some number of black tokens. Each token is placed on a plane in a point with integer coordinates *x* and *y*. The players take turn making moves, white starts. On each turn, a player moves all tokens of their color by 1 to up, down, left or righ...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=105) β€” the number of black points. The (*i*<=+<=1)-th line contains two integers *x**i*, *y**i* (<=-<=105<=≀<=*x**i*,<=*y**i*,<=<=≀<=105) β€” the coordinates of the point where the *i*-th black token is initially located. It is guaranteed that initial position...
Print the number of points where the white token can be located initially, such that if both players play optimally, the black player wins.
[ "4\n-2 -1\n0 1\n0 -3\n2 -1\n", "4\n-2 0\n-1 1\n0 -2\n1 -1\n", "16\n2 1\n1 2\n-1 1\n0 1\n0 0\n1 1\n2 -1\n2 0\n1 0\n-1 -1\n1 -1\n2 2\n0 -1\n-1 0\n0 2\n-1 2\n" ]
[ "4\n", "2\n", "4\n" ]
In the first and second examples initial positions of black tokens are shown with black points, possible positions of the white token (such that the black player wins) are shown with white points. The first example: <img class="tex-graphics" src="https://espresso.codeforces.com/5054b8d2df2fac92c92f96fae82d21c365d12983...
[ { "input": "4\n-2 -1\n0 1\n0 -3\n2 -1", "output": "4" }, { "input": "4\n-2 0\n-1 1\n0 -2\n1 -1", "output": "2" }, { "input": "16\n2 1\n1 2\n-1 1\n0 1\n0 0\n1 1\n2 -1\n2 0\n1 0\n-1 -1\n1 -1\n2 2\n0 -1\n-1 0\n0 2\n-1 2", "output": "4" }, { "input": "1\n1 2", "output": "0" ...
109
0
-1
15,736
818
Sofa Thief
[ "brute force", "implementation" ]
null
null
Yet another round on DecoForces is coming! Grandpa Maks wanted to participate in it but someone has stolen his precious sofa! And how can one perform well with such a major loss? Fortunately, the thief had left a note for Grandpa Maks. This note got Maks to the sofa storehouse. Still he had no idea which sofa belongs ...
The first line contains one integer number *d* (1<=≀<=*d*<=≀<=105) β€” the number of sofas in the storehouse. The second line contains two integer numbers *n*, *m* (1<=≀<=*n*,<=*m*<=≀<=105) β€” the size of the storehouse. Next *d* lines contains four integer numbers *x*1, *y*1, *x*2, *y*2 (1<=≀<=*x*1,<=*x*2<=≀<=*n*, 1<=≀...
Print the number of the sofa for which all the conditions are met. Sofas are numbered 1 through *d* as given in input. If there is no such sofa then print -1.
[ "2\n3 2\n3 1 3 2\n1 2 2 2\n1 0 0 1\n", "3\n10 10\n1 2 1 1\n5 5 6 5\n6 4 5 4\n2 1 2 0\n", "2\n2 2\n2 1 1 1\n1 2 2 2\n1 0 0 0\n" ]
[ "1\n", "2\n", "-1\n" ]
Let's consider the second example. - The first sofa has 0 to its left, 2 sofas to its right ((1, 1) is to the left of both (5, 5) and (5, 4)), 0 to its top and 2 to its bottom (both 2nd and 3rd sofas are below). - The second sofa has *cnt*<sub class="lower-index">*l*</sub> = 2, *cnt*<sub class="lower-index">*r*</su...
[ { "input": "2\n3 2\n3 1 3 2\n1 2 2 2\n1 0 0 1", "output": "1" }, { "input": "3\n10 10\n1 2 1 1\n5 5 6 5\n6 4 5 4\n2 1 2 0", "output": "2" }, { "input": "2\n2 2\n2 1 1 1\n1 2 2 2\n1 0 0 0", "output": "-1" }, { "input": "1\n1 2\n1 1 1 2\n0 0 0 0", "output": "1" }, { ...
763
26,828,800
0
15,774
172
Bus
[ "*special", "implementation", "sortings" ]
null
null
There is a bus stop near the university. The lessons are over, and *n* students come to the stop. The *i*-th student will appear at the bus stop at time *t**i* (all *t**i*'s are distinct). We shall assume that the stop is located on the coordinate axis *Ox*, at point *x*<==<=0, and the bus goes along the ray *Ox*, tha...
The first line contains two space-separated integers *n*,<=*m* (1<=≀<=*n*,<=*m*<=≀<=105) β€” the number of students and the number of passengers the bus can transport, correspondingly. Next *n* lines contain descriptions of the students, one per line. Each line contains a pair of integers *t**i*,<=*x**i* (1<=≀<=*t**i*<=≀...
Print *n* numbers *w*1,<=*w*2,<=...,<=*w**n*, *w**i* β€” the moment of time when the *i*-th student got off the bus. Print the numbers on one line and separate them with single spaces.
[ "1 10\n3 5\n", "2 1\n3 5\n4 5\n", "5 4\n3 5\n4 5\n5 5\n6 5\n7 1\n", "20 4\n28 13\n31 13\n35 6\n36 4\n52 6\n53 4\n83 2\n84 4\n87 1\n93 6\n108 4\n113 6\n116 1\n125 2\n130 2\n136 13\n162 2\n166 4\n184 1\n192 2\n" ]
[ "8\n", "8 19\n", "11 11 11 11 20\n", "51 51 43 40 93 89 86 89 114 121 118 121 137 139 139 152 195 199 193 195\n" ]
In the first sample the bus waits for the first student for 3 units of time and drives him to his destination in additional 5 units of time. So the student leaves the bus at the moment of time 3 + 5 = 8. In the second sample the capacity of the bus equals 1, that's why it will drive the first student alone. This stude...
[ { "input": "1 10\n3 5", "output": "8" }, { "input": "2 1\n3 5\n4 5", "output": "8 19" }, { "input": "5 4\n3 5\n4 5\n5 5\n6 5\n7 1", "output": "11 11 11 11 20" }, { "input": "20 4\n28 13\n31 13\n35 6\n36 4\n52 6\n53 4\n83 2\n84 4\n87 1\n93 6\n108 4\n113 6\n116 1\n125 2\n130 2\...
1,000
12,595,200
0
15,776
442
Adam and Tree
[ "data structures", "trees" ]
null
null
When Adam gets a rooted tree (connected non-directed graph without cycles), he immediately starts coloring it. More formally, he assigns a color to each edge of the tree so that it meets the following two conditions: - There is no vertex that has more than two incident edges painted the same color. - For any two ve...
The first line contains integer *n* (1<=≀<=*n*<=≀<=106) β€” the number of times a new vertex is added. The second line contains *n* numbers *p**i* (1<=≀<=*p**i*<=≀<=*i*) β€” the numbers of the vertexes to which we add another vertex.
Print *n* integers β€” the minimum costs of the tree painting after each addition.
[ "11\n1 1 1 3 4 4 7 3 7 6 6\n" ]
[ "1 1 1 1 1 2 2 2 2 2 3 " ]
The figure below shows one of the possible variants to paint a tree from the sample at the last moment. The cost of the vertexes with numbers 11 and 12 equals 3. <img class="tex-graphics" src="https://espresso.codeforces.com/3e0ae59416472763f3e14b7c4a5094de154d3b50.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[]
46
0
0
15,796
1,004
Sonya and Ice Cream
[ "binary search", "data structures", "dp", "greedy", "shortest paths", "trees" ]
null
null
Sonya likes ice cream very much. She eats it even during programming competitions. That is why the girl decided that she wants to open her own ice cream shops. Sonya lives in a city with $n$ junctions and $n-1$ streets between them. All streets are two-way and connect two junctions. It is possible to travel from any j...
The first line contains two integers $n$ and $k$ ($1\leq k\leq n\leq 10^5$)Β β€” the number of junctions and friends respectively. Each of the next $n-1$ lines contains three integers $u_i$, $v_i$, and $d_i$ ($1\leq u_i, v_i\leq n$, $v_i\neq u_i$, $1\leq d\leq 10^4$)Β β€” junctions that are connected by a street and the len...
Print one numberΒ β€” the minimal possible maximum distance that you need to pass to get from any junction to the nearest ice cream shop. Sonya's shops must form a simple path and the number of shops must be at most $k$.
[ "6 2\n1 2 3\n2 3 4\n4 5 2\n4 6 3\n2 4 6\n", "10 3\n1 2 5\n5 7 2\n3 2 6\n10 6 3\n3 8 1\n6 4 2\n4 1 6\n6 9 4\n5 2 5\n" ]
[ "4\n", "7\n" ]
In the first example, you can choose the path 2-4, so the answer will be 4. In the second example, you can choose the path 4-1-2, so the answer will be 7.
[ { "input": "6 2\n1 2 3\n2 3 4\n4 5 2\n4 6 3\n2 4 6", "output": "4" }, { "input": "10 3\n1 2 5\n5 7 2\n3 2 6\n10 6 3\n3 8 1\n6 4 2\n4 1 6\n6 9 4\n5 2 5", "output": "7" }, { "input": "8 4\n8 7 4\n5 6 7\n7 3 4\n8 4 3\n1 2 1\n2 3 5\n5 4 4", "output": "10" }, { "input": "1 1", ...
155
0
0
15,798
534
Handshakes
[ "binary search", "constructive algorithms", "data structures", "greedy" ]
null
null
On February, 30th *n* students came in the Center for Training Olympiad Programmers (CTOP) of the Berland State University. They came one by one, one after another. Each of them went in, and before sitting down at his desk, greeted with those who were present in the room by shaking hands. Each of the students who came ...
The first line contains integer *n* (1<=≀<=*n*<=≀<=2Β·105) β€” the number of students who came to CTOP. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=&lt;<=*n*), where *a**i* is the number of students with who the *i*-th student shook hands.
If the sought order of students exists, print in the first line "Possible" and in the second line print the permutation of the students' numbers defining the order in which the students entered the center. Number *i* that stands to the left of number *j* in this permutation means that the *i*-th student came earlier th...
[ "5\n2 1 3 0 1\n", "9\n0 2 3 4 1 1 0 2 2\n", "4\n0 2 1 1\n" ]
[ "Possible\n4 5 1 3 2 ", "Possible\n7 5 2 1 6 8 3 4 9", "Impossible\n" ]
In the first sample from the statement the order of events could be as follows: - student 4 comes in (*a*<sub class="lower-index">4</sub> = 0), he has no one to greet; - student 5 comes in (*a*<sub class="lower-index">5</sub> = 1), he shakes hands with student 4; - student 1 comes in (*a*<sub class="lower-index">1...
[ { "input": "5\n2 1 3 0 1", "output": "Possible\n4 5 1 3 2 " }, { "input": "9\n0 2 3 4 1 1 0 2 2", "output": "Possible\n7 6 9 3 4 8 1 5 2 " }, { "input": "4\n0 2 1 1", "output": "Impossible" }, { "input": "5\n1 0 2 1 0", "output": "Possible\n5 4 3 2 1 " }, { "input...
93
0
0
15,831
576
Points on Plane
[ "constructive algorithms", "divide and conquer", "geometry", "greedy", "sortings" ]
null
null
On a plane are *n* points (*x**i*, *y**i*) with integer coordinates between 0 and 106. The distance between the two points with numbers *a* and *b* is said to be the following value: (the distance calculated by such formula is called Manhattan distance). We call a hamiltonian path to be some permutation *p**i* of num...
The first line contains integer *n* (1<=≀<=*n*<=≀<=106). The *i*<=+<=1-th line contains the coordinates of the *i*-th point: *x**i* and *y**i* (0<=≀<=*x**i*,<=*y**i*<=≀<=106). It is guaranteed that no two points coincide.
Print the permutation of numbers *p**i* from 1 to *n* β€” the sought Hamiltonian path. The permutation must meet the inequality . If there are multiple possible answers, print any of them. It is guaranteed that the answer exists.
[ "5\n0 7\n8 10\n3 4\n5 0\n9 12\n" ]
[ "4 3 1 2 5 \n" ]
In the sample test the total distance is: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c772e61c616e1c27114e3facb9e6db6c5cf93b82.png" style="max-width: 100.0%;max-height: 100.0%;"/> (|5 - 3| + |0 - 4|) + (|3 - 0| + |4 - 7|) + (|0 - 8| + |7 - 10|) + (|8 - 9| + |10 - 12|) = 2 + 4 + 3 + 3 ...
[]
2,000
39,116,800
0
15,850
981
Addition on Segments
[ "bitmasks", "data structures", "divide and conquer", "dp" ]
null
null
Grisha come to a contest and faced the following problem. You are given an array of size $n$, initially consisting of zeros. The elements of the array are enumerated from $1$ to $n$. You perform $q$ operations on the array. The $i$-th operation is described with three integers $l_i$, $r_i$ and $x_i$ ($1 \leq l_i \leq ...
The first line contains two integers $n$ and $q$ ($1 \leq n, q \leq 10^{4}$)Β β€” the length of the array and the number of queries in the initial problem. The following $q$ lines contain queries, one per line. The $i$-th of these lines contains three integers $l_i$, $r_i$ and $x_i$ ($1 \leq l_i \leq r_i \leq n$, $1 \leq...
In the first line print the only integer $k$, denoting the number of integers from $1$ to $n$, inclusive, that can be equal to the maximum in the array after applying some subset (possibly empty) of the given operations. In the next line print these $k$ integers from $1$ to $n$Β β€” the possible values of the maximum. Pr...
[ "4 3\n1 3 1\n2 4 2\n3 4 4\n", "7 2\n1 5 1\n3 7 2\n", "10 3\n1 1 2\n1 1 3\n1 1 6\n" ]
[ "4\n1 2 3 4 \n", "3\n1 2 3 \n", "6\n2 3 5 6 8 9 \n" ]
Consider the first example. If you consider the subset only of the first query, the maximum is equal to $1$. If you take only the second query, the maximum equals to $2$. If you take the first two queries, the maximum becomes $3$. If you take only the fourth query, the maximum becomes $4$. If you take the fourth query ...
[ { "input": "4 3\n1 3 1\n2 4 2\n3 4 4", "output": "4\n1 2 3 4 " }, { "input": "7 2\n1 5 1\n3 7 2", "output": "3\n1 2 3 " }, { "input": "10 3\n1 1 2\n1 1 3\n1 1 6", "output": "6\n2 3 5 6 8 9 " }, { "input": "45 5\n37 38 16\n5 7 34\n1 42 31\n8 27 19\n15 28 39", "output": "5\...
202
28,774,400
-1
15,879
43
Journey
[ "brute force", "constructive algorithms", "implementation" ]
D. Journey
2
256
The territory of Berland is represented by a rectangular field *n*<=Γ—<=*m* in size. The king of Berland lives in the capital, located on the upper left square (1,<=1). The lower right square has coordinates (*n*,<=*m*). One day the king decided to travel through the whole country and return back to the capital, having ...
The first line contains two space-separated integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=100,<=2<=≀<= *n* Β· *m*) β€” the field size. The upper left square has coordinates (1,<=1), and the lower right square has coordinates of (*n*,<=*m*).
On the first line output integer *k* β€” the minimum number of teleporters. Then output *k* lines each containing 4 integers *x*1 *y*1 *x*2 *y*2 (1<=≀<=*x*1,<=*x*2<=≀<=*n*,<=1<=≀<=*y*1,<=*y*2<=≀<=*m*) β€” the coordinates of the square where the teleporter is installed (*x*1,<=*y*1), and the coordinates of the square where ...
[ "2 2\n", "3 3\n" ]
[ "0\n1 1\n1 2\n2 2\n2 1\n1 1\n", "1\n3 3 1 1\n1 1\n1 2\n1 3\n2 3\n2 2\n2 1\n3 1\n3 2\n3 3\n1 1\n" ]
none
[ { "input": "2 2", "output": "0\n1 1\n1 2\n2 2\n2 1\n1 1" }, { "input": "3 3", "output": "1\n3 3 1 1\n1 1\n1 2\n1 3\n2 3\n2 2\n2 1\n3 1\n3 2\n3 3\n1 1" }, { "input": "3 2", "output": "0\n1 1\n2 1\n3 1\n3 2\n2 2\n1 2\n1 1" }, { "input": "2 3", "output": "0\n1 1\n1 2\n1 3\n2...
0
0
-1
15,897
29
Traffic Lights
[ "implementation" ]
B. Traffic Lights
2
256
A car moves from point A to point B at speed *v* meters per second. The action takes place on the X-axis. At the distance *d* meters from A there are traffic lights. Starting from time 0, for the first *g* seconds the green light is on, then for the following *r* seconds the red light is on, then again the green light ...
The first line contains integers *l*, *d*, *v*, *g*, *r* (1<=≀<=*l*,<=*d*,<=*v*,<=*g*,<=*r*<=≀<=1000,<=*d*<=&lt;<=*l*) β€” the distance between A and B (in meters), the distance from A to the traffic lights, car's speed, the duration of green light and the duration of red light.
Output a single number β€” the minimum time that the car needs to get from point A to point B. Your output must have relative or absolute error less than 10<=-<=6.
[ "2 1 3 4 5\n", "5 4 3 1 1\n" ]
[ "0.66666667\n", "2.33333333\n" ]
none
[ { "input": "2 1 3 4 5", "output": "0.66666667" }, { "input": "5 4 3 1 1", "output": "2.33333333" }, { "input": "862 33 604 888 704", "output": "1.42715232" }, { "input": "458 251 49 622 472", "output": "9.34693878" }, { "input": "772 467 142 356 889", "output"...
92
0
3.977
15,909
173
Rock-Paper-Scissors
[ "implementation", "math" ]
null
null
Nikephoros and Polycarpus play rock-paper-scissors. The loser gets pinched (not too severely!). Let us remind you the rules of this game. Rock-paper-scissors is played by two players. In each round the players choose one of three items independently from each other. They show the items with their hands: a rock, scisso...
The first line contains integer *n* (1<=≀<=*n*<=≀<=2Β·109) β€” the number of the game's rounds. The second line contains sequence *A* as a string of *m* characters and the third line contains sequence *B* as a string of *k* characters (1<=≀<=*m*,<=*k*<=≀<=1000). The given lines only contain characters "R", "S" and "P". C...
Print two space-separated integers: the numbers of red spots Nikephoros and Polycarpus have.
[ "7\nRPS\nRSPP\n", "5\nRRRRRRRR\nR\n" ]
[ "3 2", "0 0" ]
In the first sample the game went like this: - R - R. Draw. - P - S. Nikephoros loses. - S - P. Polycarpus loses. - R - P. Nikephoros loses. - P - R. Polycarpus loses. - S - S. Draw. - R - P. Nikephoros loses. Thus, in total Nikephoros has 3 losses (and 3 red spots), and Polycarpus only has 2.
[ { "input": "7\nRPS\nRSPP", "output": "3 2" }, { "input": "5\nRRRRRRRR\nR", "output": "0 0" }, { "input": "23\nRSP\nRPSS", "output": "7 8" }, { "input": "52\nRRPSS\nRSSPRPRPPP", "output": "15 21" }, { "input": "1293\nRRPSSRSSPRPRPPPRPPPRPPPPPRPSPRSSRPSPPRPRR\nSSPSS...
2,744
0
3
15,941
367
Sereja and the Arrangement of Numbers
[ "graphs", "greedy", "sortings" ]
null
null
Let's call an array consisting of *n* integer numbers *a*1, *a*2, ..., *a**n*, beautiful if it has the following property: - consider all pairs of numbers *x*,<=*y* (*x*<=β‰ <=*y*), such that number *x* occurs in the array *a* and number *y* occurs in the array *a*; - for each pair *x*,<=*y* must exist some position *...
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=2Β·106,<=1<=≀<=*m*<=≀<=105). Next *m* lines contain pairs of integers. The *i*-th line contains numbers *q**i*,<=*w**i* (1<=≀<=*q**i*,<=*w**i*<=≀<=105). It is guaranteed that all *q**i* are distinct.
In a single line print maximum amount of money (in rubles) Sereja can pay. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
[ "5 2\n1 2\n2 3\n", "100 3\n1 2\n2 1\n3 1\n", "1 2\n1 1\n2 100\n" ]
[ "5\n", "4\n", "100\n" ]
In the first sample Sereja can pay 5 rubles, for example, if Dima constructs the following array: [1, 2, 1, 2, 2]. There are another optimal arrays for this test. In the third sample Sereja can pay 100 rubles, if Dima constructs the following array: [2].
[ { "input": "5 2\n1 2\n2 3", "output": "5" }, { "input": "100 3\n1 2\n2 1\n3 1", "output": "4" }, { "input": "1 2\n1 1\n2 100", "output": "100" }, { "input": "25 29\n82963 53706\n63282 73962\n14996 48828\n84392 31903\n96293 41422\n31719 45448\n46772 17870\n9668 85036\n36704 83...
670
8,294,400
0
15,960
762
Two strings
[ "binary search", "hashing", "strings", "two pointers" ]
null
null
You are given two strings *a* and *b*. You have to remove the minimum possible number of consecutive (standing one after another) characters from string *b* in such a way that it becomes a subsequence of string *a*. It can happen that you will not need to remove any characters at all, or maybe you will have to remove a...
The first line contains string *a*, and the second lineΒ β€” string *b*. Both of these strings are nonempty and consist of lowercase letters of English alphabet. The length of each string is no bigger than 105 characters.
On the first line output a subsequence of string *a*, obtained from *b* by erasing the minimum number of consecutive characters. If the answer consists of zero characters, output Β«-Β» (a minus sign).
[ "hi\nbob\n", "abca\naccepted\n", "abacaba\nabcdcba\n" ]
[ "-\n", "ac\n", "abcba\n" ]
In the first example strings *a* and *b* don't share any symbols, so the longest string that you can get is empty. In the second example ac is a subsequence of *a*, and at the same time you can obtain it by erasing consecutive symbols cepted from string *b*.
[ { "input": "hi\nbob", "output": "-" }, { "input": "abca\naccepted", "output": "ac" }, { "input": "abacaba\nabcdcba", "output": "abcba" }, { "input": "lo\neuhaqdhhzlnkmqnakgwzuhurqlpmdm", "output": "-" }, { "input": "aaeojkdyuilpdvyewjfrftkpcobhcumwlaoiocbfdtvjkhgd...
109
6,963,200
3
16,025
835
The penguin's game
[ "binary search", "constructive algorithms", "interactive" ]
null
null
Pay attention: this problem is interactive. Penguin Xoriy came up with a new game recently. He has *n* icicles numbered from 1 to *n*. Each icicle has a temperatureΒ β€” an integer from 1 to 109. Exactly two of these icicles are special: their temperature is *y*, while a temperature of all the others is *x*<=β‰ <=*y*. You ...
The first line contains three integers *n*, *x*, *y* (2<=≀<=*n*<=≀<=1000, 1<=≀<=*x*,<=*y*<=≀<=109, *x*<=β‰ <=*y*)Β β€” the number of icicles, the temperature of non-special icicles and the temperature of the special icicles.
To give your answer to the penguin you have to print character "!" (without quotes), then print two integers *p*1, *p*2 (*p*1<=&lt;<=*p*2)Β β€” the indexes of the special icicles in ascending order. Note that "!" and *p*1 should be separated by a space; the indexes should be separated by a space too. After you gave the an...
[ "4 2 1\n2\n1\n1" ]
[ "? 3 1 2 3\n? 1 1\n? 1 3\n! 1 3" ]
The answer for the first question is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/b32d8d96beb0d2be8d8a600f458c8cf2e2e28c54.png" style="max-width: 100.0%;max-height: 100.0%;"/>. The answer for the second and the third questions is 1, therefore, special icicles are indexes 1 and 3. You c...
[ { "input": "4 2 1 1 3", "output": "Correct answer 1 3, queries: 4." }, { "input": "6 1 2 5 6", "output": "Correct answer 5 6, queries: 5." }, { "input": "2 4523 4235 1 2", "output": "Correct answer 1 2, queries: 2." }, { "input": "511 42 1000000000 255 511", "output": "Co...
46
0
0
16,037
914
Travelling Salesman and Special Numbers
[ "brute force", "combinatorics", "dp" ]
null
null
The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer *x* and reduce it to the number of bits set to 1 in the binary representation of *x*. For example for number 13 it's true that 1310<...
The first line contains integer *n* (1<=≀<=*n*<=&lt;<=21000). The second line contains integer *k* (0<=≀<=*k*<=≀<=1000). Note that *n* is given in its binary representation without any leading zeros.
Output a single integerΒ β€” the number of special numbers not greater than *n*, modulo 109<=+<=7.
[ "110\n2\n", "111111011\n2\n" ]
[ "3\n", "169\n" ]
In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2).
[ { "input": "110\n2", "output": "3" }, { "input": "111111011\n2", "output": "169" }, { "input": "100011110011110110100\n7", "output": "0" }, { "input": "110100110\n0", "output": "1" }, { "input": "10000000000000000000000000000000000000000000\n2", "output": "792...
124
21,606,400
0
16,066
0
none
[ "none" ]
null
null
You are given a sequence *a*1,<=*a*2,<=...,<=*a**n* consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order. Sorting integers in a sub...
The first line of input data contains integer *n* (1<=≀<=*n*<=≀<=105)Β β€” the length of the sequence. The second line of input data contains *n* different integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=≀<=*a**i*<=≀<=109)Β β€” the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.
In the first line print the maximum number of subsequences *k*, which the original sequence can be split into while fulfilling the requirements. In the next *k* lines print the description of subsequences in the following format: the number of elements in subsequence *c**i* (0<=&lt;<=*c**i*<=≀<=*n*), then *c**i* integ...
[ "6\n3 2 1 6 5 4\n", "6\n83 -75 -49 11 37 62\n" ]
[ "4\n2 1 3\n1 2\n2 4 6\n1 5\n", "1\n6 1 2 3 4 5 6\n" ]
In the first sample output: After sorting the first subsequence we will get sequence 1Β 2Β 3Β 6Β 5Β 4. Sorting the second subsequence changes nothing. After sorting the third subsequence we will get sequence 1Β 2Β 3Β 4Β 5Β 6. Sorting the last subsequence changes nothing.
[ { "input": "6\n3 2 1 6 5 4", "output": "4\n2 1 3\n1 2\n2 4 6\n1 5" }, { "input": "6\n83 -75 -49 11 37 62", "output": "1\n6 1 2 3 4 5 6" }, { "input": "1\n1", "output": "1\n1 1" }, { "input": "2\n1 2", "output": "2\n1 1\n1 2" }, { "input": "2\n2 1", "output": "...
1,000
9,932,800
0
16,082
1,003
Abbreviation
[ "dp", "hashing", "strings" ]
null
null
You are given a text consisting of $n$ space-separated words. There is exactly one space character between any pair of adjacent words. There are no spaces before the first word and no spaces after the last word. The length of text is the number of letters and spaces in it. $w_i$ is the $i$-th word of text. All words co...
The first line of the input contains one integer $n$ ($1 \le n \le 300$) β€” the number of words in the text. The next line contains $n$ space-separated words of the text $w_1, w_2, \dots, w_n$. Each word consists only of lowercase Latin letters. It is guaranteed that the length of text does not exceed $10^5$.
Print one integer β€” the minimum length of the text after at most one abbreviation.
[ "6\nto be or not to be\n", "10\na ab a a b ab a a b c\n", "6\naa bb aa aa bb bb\n" ]
[ "12\n", "13\n", "11\n" ]
In the first example you can obtain the text "TB or not TB". In the second example you can obtain the text "a AAAB AAAB c". In the third example you can obtain the text "AB aa AB bb".
[ { "input": "6\nto be or not to be", "output": "12" }, { "input": "10\na ab a a b ab a a b c", "output": "13" }, { "input": "6\naa bb aa aa bb bb", "output": "11" }, { "input": "45\nxr l pl sx c c u py sv j f x h u y w w bs u cp e ad ib b tz gy lm e s n ln kg fs rd ln v f sh t...
92
2,150,400
0
16,104
416
Population Size
[ "greedy", "implementation", "math" ]
null
null
Polycarpus develops an interesting theory about the interrelation of arithmetic progressions with just everything in the world. His current idea is that the population of the capital of Berland changes over time like an arithmetic progression. Well, or like multiple arithmetic progressions. Polycarpus believes that if...
The first line of the input contains integer *n* (1<=≀<=*n*<=≀<=2Β·105) β€” the number of elements in the sequence. The second line contains integer values *a*1,<=*a*2,<=...,<=*a**n* separated by a space (1<=≀<=*a**i*<=≀<=109 or *a**i*<==<=<=-<=1).
Print the minimum number of arithmetic progressions that you need to write one after another to get sequence *a*. The positions marked as -1 in *a* can be represented by any positive integers.
[ "9\n8 6 4 2 1 4 7 10 2\n", "9\n-1 6 -1 2 -1 4 7 -1 2\n", "5\n-1 -1 -1 -1 -1\n", "7\n-1 -1 4 5 1 2 3\n" ]
[ "3\n", "3\n", "1\n", "2\n" ]
none
[ { "input": "9\n8 6 4 2 1 4 7 10 2", "output": "3" }, { "input": "9\n-1 6 -1 2 -1 4 7 -1 2", "output": "3" }, { "input": "5\n-1 -1 -1 -1 -1", "output": "1" }, { "input": "7\n-1 -1 4 5 1 2 3", "output": "2" }, { "input": "1\n1", "output": "1" }, { "input...
31
6,656,000
0
16,139
0
none
[ "none" ]
null
null
A two dimensional array is called a bracket array if each grid contains one of the two possible brackets β€” "(" or ")". A path through the two dimensional array cells is called monotonous if any two consecutive cells in the path are side-adjacent and each cell of the path is located below or to the right from the previo...
The first line contains integers *n*, *m* and *k* β€” the sizes of the array and the number of the sought correct bracket array (1<=≀<=*n*,<=*m*<=≀<=100, 1<=≀<=*k*<=≀<=1018). Then an array of priorities is given, *n* lines each containing *m* numbers, number *p**i*,<=*j* shows the priority of character *j* in line *i* (1...
Print the *k*-th two dimensional correct bracket array.
[ "1 2 1\n1 2\n", "2 3 1\n1 2 3\n4 5 6\n", "3 2 2\n3 6\n1 4\n2 5\n" ]
[ "()\n", "(()\n())\n", "()\n)(\n()\n" ]
In the first sample exists only one correct two-dimensional bracket array. In the second and in the third samples two arrays exist. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters Β«+Β» and Β«1Β» into this sequence. For example, sequences Β«(())()Β», Β«(...
[]
60
0
0
16,182
148
Escape
[ "implementation", "math" ]
null
null
The princess is going to escape the dragon's cave, and she needs to plan it carefully. The princess runs at *v**p* miles per hour, and the dragon flies at *v**d* miles per hour. The dragon will discover the escape after *t* hours and will chase the princess immediately. Looks like there's no chance to success, but the...
The input data contains integers *v**p*,<=*v**d*,<=*t*,<=*f* and *c*, one per line (1<=≀<=*v**p*,<=*v**d*<=≀<=100, 1<=≀<=*t*,<=*f*<=≀<=10, 1<=≀<=*c*<=≀<=1000).
Output the minimal number of bijous required for the escape to succeed.
[ "1\n2\n1\n1\n10\n", "1\n2\n1\n1\n8\n" ]
[ "2\n", "1\n" ]
In the first case one hour after the escape the dragon will discover it, and the princess will be 1 mile away from the cave. In two hours the dragon will overtake the princess 2 miles away from the cave, and she will need to drop the first bijou. Return to the cave and fixing the treasury will take the dragon two more ...
[ { "input": "1\n2\n1\n1\n10", "output": "2" }, { "input": "1\n2\n1\n1\n8", "output": "1" }, { "input": "5\n8\n1\n2\n100", "output": "2" }, { "input": "2\n100\n10\n10\n739", "output": "22" }, { "input": "17\n99\n2\n3\n293", "output": "3" }, { "input": "5...
122
0
3
16,183
0
none
[ "none" ]
null
null
It's a beautiful April day and Wallace is playing football with his friends. But his friends do not know that Wallace actually stayed home with Gromit and sent them his robotic self instead. Robo-Wallace has several advantages over the other guys. For example, he can hit the ball directly to the specified point. And ye...
The first and the single line contains integers *y*1, *y*2, *y**w*, *x**b*, *y**b*, *r* (1<=≀<=*y*1,<=*y*2,<=*y**w*,<=*x**b*,<=*y**b*<=≀<=106; *y*1<=&lt;<=*y*2<=&lt;<=*y**w*; *y**b*<=+<=*r*<=&lt;<=*y**w*; 2Β·*r*<=&lt;<=*y*2<=-<=*y*1). It is guaranteed that the ball is positioned correctly in the field, doesn't cross an...
If Robo-Wallace can't score a goal in the described manner, print "-1" (without the quotes). Otherwise, print a single number *x**w* β€” the abscissa of his point of aiming. If there are multiple points of aiming, print the abscissa of any of them. When checking the correctness of the answer, all comparisons are made w...
[ "4 10 13 10 3 1\n", "1 4 6 2 2 1\n", "3 10 15 17 9 2\n" ]
[ "4.3750000000\n", "-1\n", "11.3333333333\n" ]
Note that in the first and third samples other correct values of abscissa *x*<sub class="lower-index">*w*</sub> are also possible.
[ { "input": "4 10 13 10 3 1", "output": "4.3750000000" }, { "input": "1 4 6 2 2 1", "output": "-1" }, { "input": "3 10 15 17 9 2", "output": "11.3333333333" }, { "input": "4 9 30 3 3 1", "output": "-1" }, { "input": "4 9 13 2 3 1", "output": "-1" }, { "...
92
0
0
16,190
297
Fish Weight
[ "constructive algorithms", "greedy" ]
null
null
It is known that there are *k* fish species in the polar ocean, numbered from 1 to *k*. They are sorted by non-decreasing order of their weight, which is a positive number. Let the weight of the *i*-th type of fish be *w**i*, then 0<=&lt;<=*w*1<=≀<=*w*2<=≀<=...<=≀<=*w**k* holds. Polar bears Alice and Bob each have cau...
The first line contains three integers *n*,<=*m*,<=*k* (1<=≀<=*n*,<=*m*<=≀<=105,<=1<=≀<=*k*<=≀<=109) β€” the number of fish caught by Alice and Bob respectively, and the number of fish species. The second line contains *n* integers each from 1 to *k*, the list of fish type caught by Alice. The third line contains *m* in...
Output "YES" (without quotes) if it is possible, and "NO" (without quotes) otherwise.
[ "3 3 3\n2 2 2\n1 1 3\n", "4 7 9\n5 2 7 3\n3 5 2 7 3 8 7\n" ]
[ "YES\n", "NO\n" ]
In the first sample, if *w*<sub class="lower-index">1</sub> = 1, *w*<sub class="lower-index">2</sub> = 2, *w*<sub class="lower-index">3</sub> = 2.5, then Alice has a total of 2 + 2 + 2 = 6 weight units, while Bob only has 1 + 1 + 2.5 = 4.5. In the second sample, the fish that Alice caught is a subset of Bob's. Theref...
[ { "input": "3 3 3\n2 2 2\n1 1 3", "output": "YES" }, { "input": "4 7 9\n5 2 7 3\n3 5 2 7 3 8 7", "output": "NO" }, { "input": "5 5 10\n8 2 8 5 9\n9 1 7 5 1", "output": "YES" }, { "input": "7 7 10\n8 2 8 10 6 9 10\n2 4 9 5 6 2 5", "output": "YES" }, { "input": "15 ...
30
0
0
16,217
191
Metro Scheme
[ "graphs", "greedy" ]
null
null
Berland is very concerned with privacy, so almost all plans and blueprints are secret. However, a spy of the neighboring state managed to steal the Bertown subway scheme. The Bertown Subway has *n* stations, numbered from 1 to *n*, and *m* bidirectional tunnels connecting them. All Bertown Subway consists of lines. To...
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=105, 0<=≀<=*m*<=≀<=3Β·105) β€” the number of stations and the number of tunnels, correspondingly. Each of the next *m* lines contain two integers β€” the numbers of stations connected by the corresponding tunnel. The stations are numbered with integers from 1 ...
Print two numbers β€” the minimum and maximum number of lines correspondingly.
[ "3 3\n1 2\n2 3\n3 1\n", "8 8\n1 2\n2 3\n3 4\n4 5\n6 4\n4 7\n7 2\n2 8\n", "6 6\n1 2\n2 3\n2 5\n5 6\n3 4\n3 5\n" ]
[ "1 3\n", "2 8\n", "3 6\n" ]
The subway scheme with minimum possible number of lines for the second sample is:
[]
186
0
-1
16,220
106
Buns
[ "dp" ]
C. Buns
2
256
Lavrenty, a baker, is going to make several buns with stuffings and sell them. Lavrenty has *n* grams of dough as well as *m* different stuffing types. The stuffing types are numerated from 1 to *m*. Lavrenty knows that he has *a**i* grams left of the *i*-th stuffing. It takes exactly *b**i* grams of stuffing *i* and ...
The first line contains 4 integers *n*, *m*, *c*0 and *d*0 (1<=≀<=*n*<=≀<=1000, 1<=≀<=*m*<=≀<=10, 1<=≀<=*c*0,<=*d*0<=≀<=100). Each of the following *m* lines contains 4 integers. The *i*-th line contains numbers *a**i*, *b**i*, *c**i* and *d**i* (1<=≀<=*a**i*,<=*b**i*,<=*c**i*,<=*d**i*<=≀<=100).
Print the only number β€” the maximum number of tugriks Lavrenty can earn.
[ "10 2 2 1\n7 3 2 100\n12 3 1 10\n", "100 1 25 50\n15 5 20 10\n" ]
[ "241", "200" ]
To get the maximum number of tugriks in the first sample, you need to cook 2 buns with stuffing 1, 4 buns with stuffing 2 and a bun without any stuffing. In the second sample Lavrenty should cook 4 buns without stuffings.
[ { "input": "10 2 2 1\n7 3 2 100\n12 3 1 10", "output": "241" }, { "input": "100 1 25 50\n15 5 20 10", "output": "200" }, { "input": "10 1 5 2\n100 1 2 3", "output": "15" }, { "input": "10 1 5 11\n3 1 3 8", "output": "24" }, { "input": "10 2 11 5\n100 1 3 10\n100 1...
1,620
6,963,200
3.58203
16,239
754
Vladik and chat
[ "brute force", "constructive algorithms", "dp", "implementation", "strings" ]
null
null
Recently Vladik discovered a new entertainmentΒ β€” coding bots for social networks. He would like to use machine learning in his bots so now he want to prepare some learning data for them. At first, he need to download *t* chats. Vladik coded a script which should have downloaded the chats, however, something went wrong...
The first line contains single integer *t* (1<=≀<=*t*<=≀<=10) β€” the number of chats. The *t* chats follow. Each chat is given in the following format. The first line of each chat description contains single integer *n* (1<=≀<=*n*<=≀<=100)Β β€” the number of users in the chat. The next line contains *n* space-separated d...
Print the information about the *t* chats in the following format: If it is not possible to recover senders, print single line "Impossible" for this chat. Otherwise print *m* messages in the following format: &lt;username&gt;:&lt;text&gt; If there are multiple answers, print any of them.
[ "1\n2\nVladik netman\n2\n?: Hello, Vladik!\n?: Hi\n", "1\n2\nnetman vladik\n3\nnetman:how are you?\n?:wrong message\nvladik:im fine\n", "2\n3\nnetman vladik Fedosik\n2\n?: users are netman, vladik, Fedosik\nvladik: something wrong with this chat\n4\nnetman tigerrrrr banany2001 klinchuh\n4\n?: tigerrrrr, banany2...
[ "netman: Hello, Vladik!\nVladik: Hi\n", "Impossible\n", "Impossible\nnetman: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?\nklinchuh: yes, coach!\ntigerrrrr: yes, netman\nbanany2001: yes of course.\n" ]
none
[ { "input": "1\n2\nVladik netman\n2\n?: Hello, Vladik!\n?: Hi", "output": "netman: Hello, Vladik!\nVladik: Hi" }, { "input": "1\n2\nnetman vladik\n3\nnetman:how are you?\n?:wrong message\nvladik:im fine", "output": "Impossible" }, { "input": "2\n3\nnetman vladik Fedosik\n2\n?: users are n...
124
0
0
16,270
416
President's Path
[ "dp", "graphs", "shortest paths" ]
null
null
Good old Berland has *n* cities and *m* roads. Each road connects a pair of distinct cities and is bidirectional. Between any pair of cities, there is at most one road. For each road, we know its length. We also know that the President will soon ride along the Berland roads from city *s* to city *t*. Naturally, he wil...
The first line of the input contains integers *n*,<=*m* (2<=≀<=*n*<=≀<=500, 0<=≀<=*m*<=≀<=*n*Β·(*n*<=-<=1)<=/<=2) β€” the number of cities and roads, correspondingly. Then *m* lines follow, containing the road descriptions, one description per line. Each description contains three integers *x**i*,<=*y**i*,<=*l**i* (1<=≀<=...
Print the sequence of integers *c*12,<=*c*13,<=...,<=*c*1*n*,<=*c*23,<=*c*24,<=...,<=*c*2*n*,<=...,<=*c**n*<=-<=1,<=*n*, where *c**st* is the number of roads that can lie on the shortest path from *s* to *t*. Print the elements of sequence *c* in the described order. If the pair of cities *s* and *t* don't have a path...
[ "5 6\n1 2 1\n2 3 1\n3 4 1\n4 1 1\n2 4 2\n4 5 4\n" ]
[ "1 4 1 2 1 5 6 1 2 1 " ]
none
[]
31
0
0
16,271
117
Cycle
[ "dfs and similar", "graphs" ]
null
null
A tournament is a directed graph without self-loops in which every pair of vertexes is connected by exactly one directed edge. That is, for any two vertexes *u* and *v* (*u*<=β‰ <=*v*) exists either an edge going from *u* to *v*, or an edge from *v* to *u*. You are given a tournament consisting of *n* vertexes. Your tas...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=5000). Next *n* lines contain the adjacency matrix *A* of the graph (without spaces). *A**i*,<=*j*<==<=1 if the graph has an edge going from vertex *i* to vertex *j*, otherwise *A**i*,<=*j*<==<=0. *A**i*,<=*j* stands for the *j*-th character in the *i*-th line. It ...
Print three distinct vertexes of the graph *a*1, *a*2, *a*3 (1<=≀<=*a**i*<=≀<=*n*), such that *A**a*1,<=*a*2<==<=*A**a*2,<=*a*3<==<=*A**a*3,<=*a*1<==<=1, or "-1", if a cycle whose length equals three does not exist. If there are several solutions, print any of them.
[ "5\n00100\n10000\n01001\n11101\n11000\n", "5\n01111\n00000\n01000\n01100\n01110\n" ]
[ "1 3 2 ", "-1\n" ]
none
[ { "input": "5\n00100\n10000\n01001\n11101\n11000", "output": "1 3 2 " }, { "input": "5\n01111\n00000\n01000\n01100\n01110", "output": "-1" }, { "input": "5\n01000\n00101\n10010\n11001\n10100", "output": "1 2 3 " }, { "input": "5\n00110\n10110\n00011\n00000\n11010", "outpu...
0
0
-1
16,292
852
Dating
[ "brute force", "dfs and similar", "graphs", "trees" ]
null
null
This story is happening in a town named BubbleLand. There are *n* houses in BubbleLand. In each of these *n* houses lives a boy or a girl. People there really love numbers and everyone has their favorite number *f*. That means that the boy or girl that lives in the *i*-th house has favorite number equal to *f**i*. The...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=105), the number of houses in the town. The second line contains *n* integers, where the *i*-th number is 1 if a boy lives in the *i*-th house or 0 if a girl lives in *i*-th house. The third line contains *n* integers, where the *i*-th number represents the favori...
For each of the *q* questions output a single number, the answer to the citizens question.
[ "7\n1 0 0 1 0 1 0\n9 2 9 2 2 9 9\n2 6\n1 2\n4 2\n6 5\n3 6\n7 4\n2\n1 3\n7 5\n" ]
[ "2\n3\n" ]
In the first question from house 1 to house 3, the potential couples are (1, 3) and (6, 3). In the second question from house 7 to house 5, the potential couples are (7, 6), (4, 2) and (4, 5).
[]
46
0
0
16,294
0
none
[ "none" ]
null
null
Emuskald considers himself a master of flow algorithms. Now he has completed his most ingenious program yet β€” it calculates the maximum flow in an undirected graph. The graph consists of *n* vertices and *m* edges. Vertices are numbered from 1 to *n*. Vertices 1 and *n* being the source and the sink respectively. Howe...
The first line of input contains two space-separated integers *n* and *m* (2<=≀<=*n*<=≀<=2Β·105, *n*<=-<=1<=≀<=*m*<=≀<=2Β·105), the number of vertices and edges in the graph. The following *m* lines contain three space-separated integers *a**i*, *b**i* and *c**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*, *a**i*<=β‰ <=*b**i*, 1<=≀<=*...
Output *m* lines, each containing one integer *d**i*, which should be 0 if the direction of the *i*-th edge is *a**i*<=β†’<=*b**i* (the flow goes from vertex *a**i* to vertex *b**i*) and should be 1 otherwise. The edges are numbered from 1 to *m* in the order they are given in the input. If there are several solutions y...
[ "3 3\n3 2 10\n1 2 10\n3 1 5\n", "4 5\n1 2 10\n1 3 10\n2 3 5\n4 2 15\n3 4 5\n" ]
[ "1\n0\n1\n", "0\n0\n1\n1\n0\n" ]
In the first test case, 10 flow units pass through path <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/609340f155794c4e9eebcd9cdfa23c73cf982f28.png" style="max-width: 100.0%;max-height: 100.0%;"/>, and 5 flow units pass directly from source to sink: <img align="middle" class="tex-formula" ...
[ { "input": "3 3\n3 2 10\n1 2 10\n3 1 5", "output": "1\n0\n1" }, { "input": "4 5\n1 2 10\n1 3 10\n2 3 5\n4 2 15\n3 4 5", "output": "0\n0\n1\n1\n0" }, { "input": "10 17\n8 1 1\n4 8 2\n7 10 8\n1 4 1\n5 4 3\n6 9 6\n3 5 4\n1 9 1\n3 9 5\n7 1 1\n1 2 1\n1 3 1\n6 7 7\n8 2 1\n1 10 1\n1 5 1\n6 1 1"...
2,000
67,174,400
0
16,315
0
none
[ "none" ]
null
null
ΠŸΠ°ΠΌΡΡ‚ΡŒ ΠΊΠΎΠΌΠΏΡŒΡŽΡ‚Π΅Ρ€Π° состоит ΠΈΠ· *n* ячССк, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ выстроСны Π² ряд. ΠŸΡ€ΠΎΠ½ΡƒΠΌΠ΅Ρ€ΡƒΠ΅ΠΌ ячСйки ΠΎΡ‚ 1 Π΄ΠΎ *n* слСва Π½Π°ΠΏΡ€Π°Π²ΠΎ. ΠŸΡ€ΠΎ ΠΊΠ°ΠΆΠ΄ΡƒΡŽ ячСйку извСстно, свободна ΠΎΠ½Π° ΠΈΠ»ΠΈ ΠΏΡ€ΠΈΠ½Π°Π΄Π»Π΅ΠΆΠΈΡ‚ ΠΊΠ°ΠΊΠΎΠΌΡƒ-Π»ΠΈΠ±ΠΎ процСссу (Π² Ρ‚Π°ΠΊΠΎΠΌ случаС извСстСн процСсс, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌΡƒ ΠΎΠ½Π° ΠΏΡ€ΠΈΠ½Π°Π΄Π»Π΅ΠΆΠΈΡ‚). Для ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ процСсса извСстно, Ρ‡Ρ‚ΠΎ ΠΏΡ€ΠΈΠ½Π°Π΄Π»Π΅ΠΆΠ°Ρ‰ΠΈΠ΅ Π΅ΠΌΡƒ ячСйки Π·Π°Π½ΠΈΠΌΠ°ΡŽΡ‚ ...
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΉ строкС Π²Ρ…ΠΎΠ΄Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ… записано число *n* (1<=≀<=*n*<=≀<=200<=000)Β β€” количСство ячССк Π² памяти ΠΊΠΎΠΌΠΏΡŒΡŽΡ‚Π΅Ρ€Π°. Π’ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠΉ строкС Π²Ρ…ΠΎΠ΄Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ… ΡΠ»Π΅Π΄ΡƒΡŽΡ‚ *n* Ρ†Π΅Π»Ρ‹Ρ… чисСл *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=*n*), Π³Π΄Π΅ *a**i* Ρ€Π°Π²Π½ΠΎ Π»ΠΈΠ±ΠΎ 0 (это ΠΎΠ·Π½Π°Ρ‡Π°Π΅Ρ‚, Ρ‡Ρ‚ΠΎ *i*-я ячСйка памяти свободна), Π»ΠΈΠ±ΠΎ Π½ΠΎΠΌΠ΅Ρ€Ρƒ процСсса, ΠΊ...
Π’Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΎΠ΄Π½ΠΎ Ρ†Π΅Π»ΠΎΠ΅ число — минимальноС количСство ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΉ, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ Π½ΡƒΠΆΠ½ΠΎ ΡΠ΄Π΅Π»Π°Ρ‚ΡŒ для Π΄Π΅Ρ„Ρ€Π°Π³ΠΌΠ΅Π½Ρ‚Π°Ρ†ΠΈΠΈ памяти.
[ "4\n0 2 2 1\n", "8\n0 8 8 8 0 4 4 2\n" ]
[ "2\n", "4\n" ]
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΌ тСстовом ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π΅ достаточно Π΄Π²ΡƒΡ… ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΉ: 1. ΠŸΠ΅Ρ€Π΅ΠΏΠΈΡΠ°Ρ‚ΡŒ Π΄Π°Π½Π½Ρ‹Π΅ ΠΈΠ· Ρ‚Ρ€Π΅Ρ‚ΡŒΠ΅ΠΉ ячСйки Π² ΠΏΠ΅Ρ€Π²ΡƒΡŽ. ПослС этого ΠΏΠ°ΠΌΡΡ‚ΡŒ ΠΊΠΎΠΌΠΏΡŒΡŽΡ‚Π΅Ρ€Π° ΠΏΡ€ΠΈΠΌΠ΅Ρ‚ Π²ΠΈΠ΄: 2Β 2Β 0Β 1. 1. ΠŸΠ΅Ρ€Π΅ΠΏΠΈΡΠ°Ρ‚ΡŒ Π΄Π°Π½Π½Ρ‹Π΅ ΠΈΠ· Ρ‡Π΅Ρ‚Π²Π΅Ρ€Ρ‚ΠΎΠΉ ячСйки Π² Ρ‚Ρ€Π΅Ρ‚ΡŒΡŽ. ПослС этого ΠΏΠ°ΠΌΡΡ‚ΡŒ ΠΊΠΎΠΌΠΏΡŒΡŽΡ‚Π΅Ρ€Π° ΠΏΡ€ΠΈΠΌΠ΅Ρ‚ Π²ΠΈΠ΄: 2Β 2Β 1Β 0.
[ { "input": "4\n0 2 2 1", "output": "2" }, { "input": "8\n0 8 8 8 0 4 4 2", "output": "4" }, { "input": "5\n0 0 0 1 1", "output": "2" }, { "input": "6\n0 0 0 3 0 0", "output": "1" }, { "input": "10\n0 10 10 0 0 3 3 0 0 0", "output": "3" }, { "input": "1...
2,000
13,209,600
0
16,327
362
Petya and Pipes
[ "flows", "graphs", "shortest paths" ]
null
null
A little boy Petya dreams of growing up and becoming the Head Berland Plumber. He is thinking of the problems he will have to solve in the future. Unfortunately, Petya is too inexperienced, so you are about to solve one of such problems for Petya, the one he's the most interested in. The Berland capital has *n* water ...
The first line contains two space-separated integers *n* and *k* (2<=≀<=*n*<=≀<=50, 0<=≀<=*k*<=≀<=1000). Then follow *n* lines, each line contains *n* integers separated by single spaces. The *i*<=+<=1-th row and *j*-th column contain number *c**ij* β€” the width of the pipe that goes from tank *i* to tank *j* (0<=≀<=*c*...
Print a single integer β€” the maximum amount of water that can be transmitted from the main tank to the sewer tank per a unit of time.
[ "5 7\n0 1 0 2 0\n0 0 4 10 0\n0 0 0 0 5\n0 0 0 0 10\n0 0 0 0 0\n", "5 10\n0 1 0 0 0\n0 0 2 0 0\n0 0 0 3 0\n0 0 0 0 4\n100 0 0 0 0\n" ]
[ "10\n", "5\n" ]
In the first test Petya can increase width of the pipe that goes from the 1st to the 2nd water tank by 7 units. In the second test Petya can increase width of the pipe that goes from the 1st to the 2nd water tank by 4 units, from the 2nd to the 3rd water tank by 3 units, from the 3rd to the 4th water tank by 2 units a...
[]
31
0
0
16,358
883
Orientation of Edges
[ "dfs and similar", "graphs" ]
null
null
Vasya has a graph containing both directed (oriented) and undirected (non-oriented) edges. There can be multiple edges between a pair of vertices. Vasya has picked a vertex *s* from the graph. Now Vasya wants to create two separate plans: 1. to orient each undirected edge in one of two possible directions to maximiz...
The first line contains three integers *n*, *m* and *s* (2<=≀<=*n*<=≀<=3Β·105, 1<=≀<=*m*<=≀<=3Β·105, 1<=≀<=*s*<=≀<=*n*) β€” number of vertices and edges in the graph, and the vertex Vasya has picked. The following *m* lines contain information about the graph edges. Each line contains three integers *t**i*, *u**i* and *v*...
The first two lines should describe the plan which maximizes the number of reachable vertices. The lines three and four should describe the plan which minimizes the number of reachable vertices. A description of each plan should start with a line containing the number of reachable vertices. The second line of a plan s...
[ "2 2 1\n1 1 2\n2 2 1\n", "6 6 3\n2 2 6\n1 4 5\n2 3 4\n1 4 1\n1 3 1\n2 2 3\n" ]
[ "2\n-\n2\n+\n", "6\n++-\n2\n+-+\n" ]
none
[ { "input": "2 2 1\n1 1 2\n2 2 1", "output": "2\n-\n2\n+" }, { "input": "6 6 3\n2 2 6\n1 4 5\n2 3 4\n1 4 1\n1 3 1\n2 2 3", "output": "6\n++-\n2\n+-+" }, { "input": "5 5 5\n2 5 3\n1 2 3\n1 4 5\n2 5 2\n1 2 1", "output": "4\n++\n1\n--" }, { "input": "13 18 9\n2 3 10\n1 12 10\n1 1...
530
20,480,000
-1
16,378
656
Ace It!
[ "*special" ]
null
null
The only line of the input is a string of 7 characters. The first character is letter A, followed by 6 digits. The input is guaranteed to be valid (for certain definition of "valid"). Output a single integer.
The only line of the input is a string of 7 characters. The first character is letter A, followed by 6 digits. The input is guaranteed to be valid (for certain definition of "valid").
Output a single integer.
[ "A221033\n", "A223635\n", "A232726\n" ]
[ "21\n", "22\n", "23\n" ]
none
[ { "input": "A221033", "output": "21" }, { "input": "A223635", "output": "22" }, { "input": "A232726", "output": "23" }, { "input": "A102210", "output": "25" }, { "input": "A231010", "output": "26" }, { "input": "A222222", "output": "13" }, { ...
46
0
3
16,408
786
Berzerk
[ "dfs and similar", "dp", "games" ]
null
null
Rick and Morty are playing their own version of Berzerk (which has nothing in common with the famous Berzerk game). This game needs a huge space, so they play it with a computer. In this game there are *n* objects numbered from 1 to *n* arranged in a circle (in clockwise order). Object number 1 is a black hole and the...
The first line of input contains a single integer *n* (2<=≀<=*n*<=≀<=7000) β€” number of objects in game. The second line contains integer *k*1 followed by *k*1 distinct integers *s*1,<=1,<=*s*1,<=2,<=...,<=*s*1,<=*k*1 β€” Rick's set. The third line contains integer *k*2 followed by *k*2 distinct integers *s*2,<=1,<=*s*2...
In the first line print *n*<=-<=1 words separated by spaces where *i*-th word is "Win" (without quotations) if in the scenario that Rick plays first and monster is initially in object number *i*<=+<=1 he wins, "Lose" if he loses and "Loop" if the game will never end. Similarly, in the second line print *n*<=-<=1 words...
[ "5\n2 3 2\n3 1 2 3\n", "8\n4 6 2 3 4\n2 3 6\n" ]
[ "Lose Win Win Loop\nLoop Win Win Win\n", "Win Win Win Win Win Win Win\nLose Win Lose Lose Win Lose Lose\n" ]
none
[ { "input": "5\n2 3 2\n3 1 2 3", "output": "Lose Win Win Loop\nLoop Win Win Win" }, { "input": "8\n4 6 2 3 4\n2 3 6", "output": "Win Win Win Win Win Win Win\nLose Win Lose Lose Win Lose Lose" }, { "input": "10\n3 4 7 5\n2 8 5", "output": "Win Win Win Win Win Win Win Loop Win\nLose Win...
15
0
-1
16,431
35
Animals
[ "dp", "greedy" ]
D. Animals
2
64
Once upon a time DravDe, an outstanding person famous for his professional achievements (as you must remember, he works in a warehouse storing Ogudar-Olok, a magical but non-alcoholic drink) came home after a hard day. That day he had to drink 9875 boxes of the drink and, having come home, he went to bed at once. Drav...
The first input line contains integers *n* and *X* (1<=≀<=*n*<=≀<=100,<=1<=≀<=*X*<=≀<=104) β€” amount of days in DravDe’s dream and the total amount of food (in tons) that was there initially. The second line contains integers *c**i* (1<=≀<=*c**i*<=≀<=300). Numbers in the second line are divided by a space.
Output the only number β€” the maximum possible amount of animals on the farm by the end of the *n*-th day given that the food was enough for everybody.
[ "3 4\n1 1 1\n", "3 6\n1 1 1\n" ]
[ "2\n", "3\n" ]
Note to the first example: DravDe leaves the second and the third animal on the farm. The second animal will eat one ton of food on the second day and one ton on the third day. The third animal will eat one ton of food on the third day.
[ { "input": "3 4\n1 1 1", "output": "2" }, { "input": "3 6\n1 1 1", "output": "3" }, { "input": "1 12\n1", "output": "1" }, { "input": "3 100\n1 1 1", "output": "3" }, { "input": "5 75\n1 1 1 1 1", "output": "5" }, { "input": "7 115\n1 1 1 1 1 1 1", ...
92
0
-1
16,451
59
Fortune Telling
[ "implementation", "number theory" ]
B. Fortune Telling
2
256
Marina loves Sasha. But she keeps wondering whether Sasha loves her. Of course, the best way to know it is fortune telling. There are many ways of telling fortune, but Marina has picked the easiest one. She takes in her hand one or several camomiles and tears off the petals one by one. After each petal she pronounces a...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=100), which is the number of flowers growing in the field. The second line contains *n* integers *a**i* (1<=≀<=*a**i*<=≀<=100) which represent the number of petals on a given *i*-th camomile.
Print a single number which is the maximal number of petals in the bouquet, the fortune telling on which would result in "Loves". If there are no such bouquet, print 0 instead. The bouquet may consist of a single flower.
[ "1\n1\n", "1\n2\n", "3\n5 6 7\n" ]
[ "1\n", "0\n", "13\n" ]
none
[ { "input": "1\n1", "output": "1" }, { "input": "1\n2", "output": "0" }, { "input": "3\n5 6 7", "output": "13" }, { "input": "2\n5 7", "output": "7" }, { "input": "3\n1 2 3", "output": "5" }, { "input": "4\n4 3 1 2", "output": "9" }, { "inpu...
92
0
0
16,457
993
Compute Power
[ "binary search", "dp", "greedy" ]
null
null
You need to execute several tasks, each associated with number of processors it needs, and the compute power it will consume. You have sufficient number of analog computers, each with enough processors for any task. Each computer can execute up to one task at a time, and no more than two tasks total. The first task ca...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=50) β€” the number of tasks. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=108), where *a**i* represents the amount of power required for the *i*-th task. The third line contains *n* integers *b*1,<=*b*2,<=...,<=*b**n* (1<=...
Print a single integer value β€” the lowest threshold for which it is possible to assign all tasks in such a way that the system will not blow up after the first round of computation, multiplied by 1000 and rounded up.
[ "6\n8 10 9 9 8 10\n1 1 1 1 1 1\n", "6\n8 10 9 9 8 10\n1 10 5 5 1 10\n" ]
[ "9000\n", "1160\n" ]
In the first example the best strategy is to run each task on a separate computer, getting average compute per processor during the first round equal to 9. In the second task it is best to run tasks with compute 10 and 9 on one computer, tasks with compute 10 and 8 on another, and tasks with compute 9 and 8 on the las...
[ { "input": "6\n8 10 9 9 8 10\n1 1 1 1 1 1", "output": "9000" }, { "input": "6\n8 10 9 9 8 10\n1 10 5 5 1 10", "output": "1160" }, { "input": "1\n1\n100", "output": "10" }, { "input": "50\n83 43 73 75 11 53 6 43 67 38 83 12 70 27 60 13 9 79 61 30 29 71 10 11 95 87 26 26 19 99 ...
30
0
0
16,461
187
Permutations
[ "greedy" ]
null
null
Happy PMP is freshman and he is learning about algorithmic problems. He enjoys playing algorithmic games a lot. One of the seniors gave Happy PMP a nice game. He is given two permutations of numbers 1 through *n* and is asked to convert the first one to the second. In one move he can remove the last number from the pe...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=2Β·105) β€” the quantity of the numbers in the both given permutations. Next line contains *n* space-separated integers β€” the first permutation. Each number between 1 to *n* will appear in the permutation exactly once. Next line describe the second permutatio...
Print a single integer denoting the minimum number of moves required to convert the first permutation to the second.
[ "3\n3 2 1\n1 2 3\n", "5\n1 2 3 4 5\n1 5 2 3 4\n", "5\n1 5 2 3 4\n1 2 3 4 5\n" ]
[ "2\n", "1\n", "3\n" ]
In the first sample, he removes number 1 from end of the list and places it at the beginning. After that he takes number 2 and places it between 1 and 3. In the second sample, he removes number 5 and inserts it after 1. In the third sample, the sequence of changes are like this: - 1 5 2 3 4 - 1 4 5 2 3 - 1 3 4 5 2 ...
[ { "input": "3\n3 2 1\n1 2 3", "output": "2" }, { "input": "5\n1 2 3 4 5\n1 5 2 3 4", "output": "1" }, { "input": "5\n1 5 2 3 4\n1 2 3 4 5", "output": "3" }, { "input": "1\n1\n1", "output": "0" }, { "input": "7\n6 1 7 3 4 5 2\n6 1 7 3 4 5 2", "output": "0" },...
92
0
0
16,481
794
Leha and security system
[ "data structures" ]
null
null
Bankopolis, the city you already know, finally got a new bank opened! Unfortunately, its security system is not yet working fine... Meanwhile hacker Leha arrived in Bankopolis and decided to test the system! Bank has *n* cells for clients' money. A sequence from *n* numbers *a*1,<=*a*2,<=...,<=*a**n* describes the amo...
The first line of input contains two integers *n* and *q* (1<=≀<=*n*<=≀<=105, 1<=≀<=*q*<=≀<=105) denoting amount of cells in the bank and total amount of queries respectively. The following line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=&lt;<=109) denoting the amount of money in each cell initia...
For each second type query print a single number denoting the required sum.
[ "5 5\n38 43 4 12 70\n1 1 3 4 8\n2 2 4\n1 4 5 0 8\n1 2 5 8 7\n2 1 5\n", "5 5\n25 36 39 40 899\n1 1 3 2 7\n2 1 2\n1 3 5 9 1\n1 4 4 0 9\n2 1 5\n" ]
[ "103\n207\n", "111\n1002\n" ]
Let's look at the example testcase. Initially the sequence is [38, 43, 4, 12, 70]. After the first change each digit equal to 4 becomes 8 for each element with index in interval [1;Β 3]. Thus, the new sequence is [38, 83, 8, 12, 70]. The answer for the first sum's query is the sum in the interval [2;Β 4], which equal...
[]
108
0
0
16,493
510
Fox And Jumping
[ "bitmasks", "brute force", "dp", "math" ]
null
null
Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0. There are also *n* cards, each card has 2 attributes: length *l**i* and cost *c**i*. If she pays *c**i* dollars then she can apply *i*-th...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=300), number of cards. The second line contains *n* numbers *l**i* (1<=≀<=*l**i*<=≀<=109), the jump lengths of cards. The third line contains *n* numbers *c**i* (1<=≀<=*c**i*<=≀<=105), the costs of cards.
If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards.
[ "3\n100 99 9900\n1 1 1\n", "5\n10 20 30 40 50\n1 1 1 1 1\n", "7\n15015 10010 6006 4290 2730 2310 1\n1 1 1 1 1 1 10\n", "8\n4264 4921 6321 6984 2316 8432 6120 1026\n4264 4921 6321 6984 2316 8432 6120 1026\n" ]
[ "2\n", "-1\n", "6\n", "7237\n" ]
In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell. In the second sample test, even if you buy all cards, you c...
[ { "input": "3\n100 99 9900\n1 1 1", "output": "2" }, { "input": "5\n10 20 30 40 50\n1 1 1 1 1", "output": "-1" }, { "input": "7\n15015 10010 6006 4290 2730 2310 1\n1 1 1 1 1 1 10", "output": "6" }, { "input": "8\n4264 4921 6321 6984 2316 8432 6120 1026\n4264 4921 6321 6984 23...
46
0
0
16,514
21
Traveling Graph
[ "bitmasks", "graph matchings", "graphs" ]
D. Traveling Graph
0
64
You are given undirected weighted graph. Find the length of the shortest cycle which starts from the vertex 1 and passes throught all the edges at least once. Graph may contain multiply edges between a pair of vertices and loops (edges from the vertex to itself).
The first line of the input contains two integers *n* and *m* (1<=≀<=*n*<=≀<=15,<=0<=≀<=*m*<=≀<=2000), *n* is the amount of vertices, and *m* is the amount of edges. Following *m* lines contain edges as a triples *x*,<=*y*,<=*w* (1<=≀<=*x*,<=*y*<=≀<=*n*,<=1<=≀<=*w*<=≀<=10000), *x*,<=*y* are edge endpoints, and *w* is t...
Output minimal cycle length or -1 if it doesn't exists.
[ "3 3\n1 2 1\n2 3 1\n3 1 1\n", "3 2\n1 2 3\n2 3 4\n" ]
[ "3\n", "14\n" ]
none
[ { "input": "4 6\n1 2 10\n2 3 1000\n3 4 10\n4 1 1000\n4 2 5000\n1 3 2", "output": "7042" }, { "input": "2 9\n1 2 9\n1 2 9\n2 1 9\n1 2 8\n2 1 9\n1 2 9\n1 2 9\n1 2 11\n1 2 9", "output": "90" }, { "input": "2 10\n1 2 9\n1 2 9\n2 1 9\n1 2 8\n2 1 9\n1 2 9\n1 2 9\n1 2 11\n1 2 9\n1 2 9", "ou...
434
5,120,000
3
16,613
578
Weakness and Poorness
[ "ternary search" ]
null
null
You are given a sequence of n integers *a*1,<=*a*2,<=...,<=*a**n*. Determine a real number *x* such that the weakness of the sequence *a*1<=-<=*x*,<=*a*2<=-<=*x*,<=...,<=*a**n*<=-<=*x* is as small as possible. The weakness of a sequence is defined as the maximum value of the poorness over all segments (contiguous su...
The first line contains one integer *n* (1<=≀<=*n*<=≀<=200<=000), the length of a sequence. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (|*a**i*|<=≀<=10<=000).
Output a real number denoting the minimum possible weakness of *a*1<=-<=*x*,<=*a*2<=-<=*x*,<=...,<=*a**n*<=-<=*x*. Your answer will be considered correct if its relative or absolute error doesn't exceed 10<=-<=6.
[ "3\n1 2 3\n", "4\n1 2 3 4\n", "10\n1 10 2 9 3 8 4 7 5 6\n" ]
[ "1.000000000000000\n", "2.000000000000000\n", "4.500000000000000\n" ]
For the first case, the optimal value of *x* is 2 so the sequence becomes  - 1, 0, 1 and the max poorness occurs at the segment "-1" or segment "1". The poorness value (answer) equals to 1 in this case. For the second sample the optimal value of *x* is 2.5 so the sequence becomes  - 1.5,  - 0.5, 0.5, 1.5 and the max ...
[ { "input": "3\n1 2 3", "output": "1.000000000000000" }, { "input": "4\n1 2 3 4", "output": "2.000000000000000" }, { "input": "10\n1 10 2 9 3 8 4 7 5 6", "output": "4.500000000000000" }, { "input": "1\n-10000", "output": "0.000000000000000" }, { "input": "3\n10000 ...
92
0
0
16,623
425
Sereja and Two Sequences
[ "data structures", "dp" ]
null
null
Sereja has two sequences *a*1,<=*a*2,<=...,<=*a**n* and *b*1,<=*b*2,<=...,<=*b**m*, consisting of integers. One day Sereja got bored and he decided two play with them. The rules of the game was very simple. Sereja makes several moves, in one move he can perform one of the following actions: 1. Choose several (at leas...
The first line contains integers *n*, *m*, *s*, *e* (1<=≀<=*n*,<=*m*<=≀<=105;Β 1<=≀<=*s*<=≀<=3Β·105;Β 103<=≀<=*e*<=≀<=104). The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=≀<=*a**i*<=≀<=105). The third line contains *m* integers *b*1, *b*2, ..., *b**m* (1<=≀<=*b**i*<=≀<=105).
Print a single integer β€” maximum number of money in dollars that Sereja can get.
[ "5 5 100000 1000\n1 2 3 4 5\n3 2 4 5 1\n", "3 4 3006 1000\n1 2 3\n1 2 4 3\n" ]
[ "3\n", "2\n" ]
none
[]
30
0
0
16,627
18
Flag 2
[ "dp" ]
E. Flag 2
2
128
According to a new ISO standard, a flag of every country should have, strangely enough, a chequered field *n*<=Γ—<=*m*, each square should be wholly painted one of 26 colours. The following restrictions are set: - In each row at most two different colours can be used. - No two adjacent squares can be painted the sam...
The first input line contains 2 integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=500) β€” amount of rows and columns in Berland's flag respectively. Then there follows the flag's description: each of the following *n* lines contains *m* characters. Each character is a letter from a to z, and it stands for the colour of the corr...
In the first line output the minimum amount of squares that need to be repainted to make the flag meet the new ISO standard. The following *n* lines should contain one of the possible variants of the new flag. Don't forget that the variant of the flag, proposed by you, should be derived from the old flag with the minim...
[ "3 4\naaaa\nbbbb\ncccc\n", "3 3\naba\naba\nzzz\n" ]
[ "6\nabab\nbaba\nacac\n", "4\naba\nbab\nzbz\n" ]
none
[ { "input": "3 4\naaaa\nbbbb\ncccc", "output": "6\nabab\nbaba\nacac" }, { "input": "3 3\naba\naba\nzzz", "output": "4\naba\nbab\nzbz" }, { "input": "5 6\nababab\nbababa\nbbbbbb\nbababa\nababab", "output": "3\nababab\nbababa\nababab\nbababa\nababab" }, { "input": "1 1\nq", ...
30
0
-1
16,676
439
Devu and Birthday Celebration
[ "combinatorics", "dp", "math" ]
null
null
Today is Devu's birthday. For celebrating the occasion, he bought *n* sweets from the nearby market. He has invited his *f* friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to ea...
The first line contains an integer *q* representing the number of queries (1<=≀<=*q*<=≀<=105). Each of the next *q* lines contains two space space-separated integers *n*, *f* (1<=≀<=*f*<=≀<=*n*<=≀<=105).
For each query, output a single integer in a line corresponding to the answer of each query.
[ "5\n6 2\n7 2\n6 3\n6 4\n7 4\n" ]
[ "2\n6\n9\n10\n20\n" ]
For first query: *n* = 6, *f* = 2. Possible partitions are [1, 5] and [5, 1]. For second query: *n* = 7, *f* = 2. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning.
[ { "input": "5\n6 2\n7 2\n6 3\n6 4\n7 4", "output": "2\n6\n9\n10\n20" }, { "input": "10\n1 1\n1 1\n1 1\n7 2\n6 3\n9 5\n4 1\n2 1\n3 1\n2 2", "output": "1\n1\n1\n6\n9\n70\n0\n0\n0\n1" }, { "input": "40\n37 15\n48 10\n16 5\n25 23\n32 20\n24 4\n46 19\n16 13\n1 1\n37 22\n44 29\n24 6\n27 10\n39...
498
49,254,400
0
16,703
590
Median Smoothing
[ "implementation" ]
null
null
A schoolboy named Vasya loves reading books on programming and mathematics. He has recently read an encyclopedia article that described the method of median smoothing (or median filter) and its many applications in science and engineering. Vasya liked the idea of the method very much, and he decided to try it in practi...
The first input line of the input contains a single integer *n* (3<=≀<=*n*<=≀<=500<=000)Β β€” the length of the initial sequence. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (*a**i*<==<=0 or *a**i*<==<=1), giving the initial sequence itself.
If the sequence will never become stable, print a single number <=-<=1. Otherwise, first print a single integerΒ β€” the minimum number of times one needs to apply the median smoothing algorithm to the initial sequence before it becomes is stable. In the second line print *n* numbers separated by a space Β β€” the resulting...
[ "4\n0 0 1 1\n", "5\n0 1 0 1 0\n" ]
[ "0\n0 0 1 1\n", "2\n0 0 0 0 0\n" ]
In the second sample the stabilization occurs in two steps: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5a983e7baab048cbe43812cb997c15e9d7100231.png" style="max-width: 100.0%;max-height: 100.0%;"/>, and the sequence 00000 is obviously stable.
[ { "input": "4\n0 0 1 1", "output": "0\n0 0 1 1" }, { "input": "5\n0 1 0 1 0", "output": "2\n0 0 0 0 0" }, { "input": "3\n1 0 0", "output": "0\n1 0 0" }, { "input": "4\n1 0 0 1", "output": "0\n1 0 0 1" }, { "input": "7\n1 0 1 1 1 0 1", "output": "1\n1 1 1 1 1 1...
46
0
0
16,745
630
Cracking the Code
[ "implementation", "math" ]
null
null
The protection of a popular program developed by one of IT City companies is organized the following way. After installation it outputs a random five digit number which should be sent in SMS to a particular phone number. In response an SMS activation code arrives. A young hacker Vasya disassembled the program and foun...
The only line of the input contains a positive integer five digit number for which the activation code should be found.
Output exactly 5 digits without spaces between them β€” the found activation code of the program.
[ "12345\n" ]
[ "71232" ]
none
[ { "input": "12345", "output": "71232" }, { "input": "13542", "output": "84443" }, { "input": "71232", "output": "10151" }, { "input": "11111", "output": "36551" }, { "input": "10000", "output": "00000" }, { "input": "99999", "output": "99999" }, ...
46
0
3
16,751
538
Weird Chess
[ "brute force", "constructive algorithms", "implementation" ]
null
null
Igor has been into chess for a long time and now he is sick of the game by the ordinary rules. He is going to think of new rules of the game and become world famous. Igor's chessboard is a square of size *n*<=Γ—<=*n* cells. Igor decided that simple rules guarantee success, that's why his game will have only one type of...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=50). The next *n* lines contain *n* characters each describing the position offered by Igor. The *j*-th character of the *i*-th string can have the following values: - o β€” in this case the field (*i*,<=*j*) is occupied by a piece and the field may or may not...
If there is a valid set of moves, in the first line print a single word 'YES' (without the quotes). Next, print the description of the set of moves of a piece in the form of a (2*n*<=-<=1)<=Γ—<=(2*n*<=-<=1) board, the center of the board has a piece and symbols 'x' mark cells that are attacked by it, in a format similar...
[ "5\noxxxx\nx...x\nx...x\nx...x\nxxxxo\n", "6\n.x.x..\nx.x.x.\n.xo..x\nx..ox.\n.x.x.x\n..x.x.\n", "3\no.x\noxx\no.x\n" ]
[ "YES\n....x....\n....x....\n....x....\n....x....\nxxxxoxxxx\n....x....\n....x....\n....x....\n....x....\n", "YES\n...........\n...........\n...........\n....x.x....\n...x...x...\n.....o.....\n...x...x...\n....x.x....\n...........\n...........\n...........\n", "NO\n" ]
In the first sample test the piece is a usual chess rook, and in the second sample test the piece is a usual chess knight.
[ { "input": "5\noxxxx\nx...x\nx...x\nx...x\nxxxxo", "output": "YES\nxxxxxxxxx\nx...xxxxx\nx...xxxxx\nx...xxxxx\nxxxxoxxxx\nxxxxx...x\nxxxxx...x\nxxxxx...x\nxxxxxxxxx" }, { "input": "6\n.x.x..\nx.x.x.\n.xo..x\nx..ox.\n.x.x.x\n..x.x.", "output": "YES\nxxxxxxxxxxx\nxxxxxxxxxxx\nxx.x.x..xxx\nxxx.x.x....
2,000
4,812,800
0
16,778
540
Bad Luck Island
[ "dp", "probabilities" ]
null
null
The Bad Luck Island is inhabited by three kinds of species: *r* rocks, *s* scissors and *p* papers. At some moments of time two random individuals meet (all pairs of individuals can meet equiprobably), and if they belong to different species, then one individual kills the other one: a rock kills scissors, scissors kill...
The single line contains three integers *r*, *s* and *p* (1<=≀<=*r*,<=*s*,<=*p*<=≀<=100)Β β€” the original number of individuals in the species of rock, scissors and paper, respectively.
Print three space-separated real numbers: the probabilities, at which the rocks, the scissors and the paper will be the only surviving species, respectively. The answer will be considered correct if the relative or absolute error of each number doesn't exceed 10<=-<=9.
[ "2 2 2\n", "2 1 2\n", "1 1 3\n" ]
[ "0.333333333333 0.333333333333 0.333333333333\n", "0.150000000000 0.300000000000 0.550000000000\n", "0.057142857143 0.657142857143 0.285714285714\n" ]
none
[ { "input": "2 2 2", "output": "0.333333333333 0.333333333333 0.333333333333" }, { "input": "2 1 2", "output": "0.150000000000 0.300000000000 0.550000000000" }, { "input": "1 1 3", "output": "0.057142857143 0.657142857143 0.285714285714" }, { "input": "3 2 1", "output": "0...
1,107
81,510,400
3
16,808
0
none
[ "none" ]
null
null
Manao is taking part in a quiz. The quiz consists of *n* consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectl...
The single line contains three space-separated integers *n*, *m* and *k* (2<=≀<=*k*<=≀<=*n*<=≀<=109;Β 0<=≀<=*m*<=≀<=*n*).
Print a single integer β€” the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109<=+<=9).
[ "5 3 2\n", "5 4 2\n" ]
[ "3\n", "6\n" ]
Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points. Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong ...
[ { "input": "5 3 2", "output": "3" }, { "input": "5 4 2", "output": "6" }, { "input": "300 300 3", "output": "17717644" }, { "input": "300 282 7", "output": "234881124" }, { "input": "1000000000 1000000000 1000000000", "output": "999999991" }, { "input"...
218
0
3
16,837
440
One-Based Arithmetic
[ "brute force", "dfs and similar", "divide and conquer" ]
null
null
Prof. Vasechkin wants to represent positive integer *n* as a sum of addends, where each addends is an integer number containing only 1s. For example, he can represent 121 as 121=111+11+–1. Help him to find the least number of digits 1 in such sum.
The first line of the input contains integer *n* (1<=≀<=*n*<=&lt;<=1015).
Print expected minimal number of digits 1.
[ "121\n" ]
[ "6\n" ]
none
[ { "input": "121", "output": "6" }, { "input": "10", "output": "3" }, { "input": "72", "output": "15" }, { "input": "1", "output": "1" }, { "input": "2", "output": "2" }, { "input": "3", "output": "3" }, { "input": "4", "output": "4" }...
124
0
0
16,861
660
Different Subsets For All Tuples
[ "combinatorics", "math" ]
null
null
For a sequence *a* of *n* integers between 1 and *m*, inclusive, denote *f*(*a*) as the number of distinct subsequences of *a* (including the empty subsequence). You are given two positive integers *n* and *m*. Let *S* be the set of all sequences of length *n* consisting of numbers from 1 to *m*. Compute the sum *f*(*...
The only line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=106) β€” the number of elements in arrays and the upper bound for elements.
Print the only integer *c* β€” the desired sum modulo 109<=+<=7.
[ "1 3\n", "2 2\n", "3 3\n" ]
[ "6\n", "14\n", "174\n" ]
none
[ { "input": "1 3", "output": "6" }, { "input": "2 2", "output": "14" }, { "input": "3 3", "output": "174" }, { "input": "1 1000000", "output": "2000000" }, { "input": "1000000 1", "output": "1000001" }, { "input": "500 500", "output": "383255233" ...
46
0
0
16,872
685
Kay and Snowflake
[ "data structures", "dfs and similar", "dp", "trees" ]
null
null
After the piece of a devilish mirror hit the Kay's eye, he is no longer interested in the beauty of the roses. Now he likes to watch snowflakes. Once upon a time, he found a huge snowflake that has a form of the tree (connected acyclic graph) consisting of *n* nodes. The root of tree has index 1. Kay is very intereste...
The first line of the input contains two integers *n* and *q* (2<=≀<=*n*<=≀<=300<=000, 1<=≀<=*q*<=≀<=300<=000)Β β€” the size of the initial tree and the number of queries respectively. The second line contains *n*<=-<=1 integer *p*2,<=*p*3,<=...,<=*p**n* (1<=≀<=*p**i*<=≀<=*n*)Β β€” the indices of the parents of the nodes fr...
For each query print the index of a centroid of the corresponding subtree. If there are many suitable nodes, print any of them. It's guaranteed, that each subtree has at least one centroid.
[ "7 4\n1 1 3 3 5 3\n1\n2\n3\n5\n" ]
[ "3\n2\n3\n6\n" ]
The first query asks for a centroid of the whole treeΒ β€” this is node 3. If we delete node 3 the tree will split in four components, two of size 1 and two of size 2. The subtree of the second node consists of this node only, so the answer is 2. Node 3 is centroid of its own subtree. The centroids of the subtree of th...
[ { "input": "7 4\n1 1 3 3 5 3\n1\n2\n3\n5", "output": "3\n2\n3\n6" }, { "input": "2 2\n1\n1\n2", "output": "2\n2" } ]
872
70,656,000
0
16,883
460
Present
[ "binary search", "data structures", "greedy" ]
null
null
Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted *n* flowers in a row on his windowsill and started waiting for them to grow. However, after some time the beaver no...
The first line contains space-separated integers *n*, *m* and *w* (1<=≀<=*w*<=≀<=*n*<=≀<=105;Β 1<=≀<=*m*<=≀<=105). The second line contains space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109).
Print a single integer β€” the maximum final height of the smallest flower.
[ "6 2 3\n2 2 2 2 1 1\n", "2 5 1\n5 8\n" ]
[ "2\n", "9\n" ]
In the first sample beaver can water the last 3 flowers at the first day. On the next day he may not to water flowers at all. In the end he will get the following heights: [2, 2, 2, 3, 2, 2]. The smallest flower has height equal to 2. It's impossible to get height 3 in this test.
[ { "input": "6 2 3\n2 2 2 2 1 1", "output": "2" }, { "input": "2 5 1\n5 8", "output": "9" }, { "input": "1 1 1\n1", "output": "2" }, { "input": "3 2 3\n999999998 999999998 999999998", "output": "1000000000" }, { "input": "10 8 3\n499 498 497 497 497 497 497 497 498...
109
20,992,000
3
16,938
846
Chemistry in Berland
[ "dfs and similar", "greedy", "trees" ]
null
null
Igor is a post-graduate student of chemistry faculty in Berland State University (BerSU). He needs to conduct a complicated experiment to write his thesis, but laboratory of BerSU doesn't contain all the materials required for this experiment. Fortunately, chemical laws allow material transformations (yes, chemistry i...
The first line contains one integer number *n* (1<=≀<=*n*<=≀<=105) β€” the number of materials discovered by Berland chemists. The second line contains *n* integer numbers *b*1,<=*b*2... *b**n* (1<=≀<=*b**i*<=≀<=1012) β€” supplies of BerSU laboratory. The third line contains *n* integer numbers *a*1,<=*a*2... *a**n* (1<=...
Print YES if it is possible to conduct an experiment. Otherwise print NO.
[ "3\n1 2 3\n3 2 1\n1 1\n1 1\n", "3\n3 2 1\n1 2 3\n1 1\n1 2\n" ]
[ "YES\n", "NO\n" ]
none
[ { "input": "3\n1 2 3\n3 2 1\n1 1\n1 1", "output": "YES" }, { "input": "3\n3 2 1\n1 2 3\n1 1\n1 2", "output": "NO" }, { "input": "5\n2 1 1 2 3\n1 2 2 2 1\n1 2\n1 3\n2 4\n1 4", "output": "NO" }, { "input": "10\n2 8 6 1 2 7 6 9 2 8\n4 9 4 3 5 2 9 3 7 3\n1 8\n2 8\n3 8\n4 10\n5 1\...
1,419
268,390,400
0
16,954
201
Fragile Bridges
[ "dp" ]
null
null
You are playing a video game and you have just reached the bonus level, where the only possible goal is to score as many points as possible. Being a perfectionist, you've decided that you won't leave this level until you've gained the maximum possible number of points there. The bonus level consists of *n* small platf...
The first line contains a single integer *n* (2<=≀<=*n*<=≀<=105) β€” the number of platforms on the bonus level. The second line contains (*n*<=-<=1) integers *a**i* (1<=≀<=*a**i*<=≀<=109, 1<=≀<=*i*<=&lt;<=*n*) β€” the number of transitions from one end to the other that the bridge between platforms *i* and *i*<=+<=1 can b...
Print a single integer β€” the maximum number of points a player can get on the bonus level. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
[ "5\n2 1 2 1\n" ]
[ "5\n" ]
One possibility of getting 5 points in the sample is starting from platform 3 and consequently moving to platforms 4, 3, 2, 1 and 2. After that the only undestroyed bridge is the bridge between platforms 4 and 5, but this bridge is too far from platform 2 where the hero is located now.
[ { "input": "5\n2 1 2 1", "output": "5" }, { "input": "3\n2 2", "output": "4" }, { "input": "7\n1 2 3 4 5 6", "output": "19" }, { "input": "12\n10 14 8 6 2 7 5 9 9 4 8", "output": "82" }, { "input": "13\n1 1 1 1 1 1 1 1 1 1 1 1", "output": "12" }, { "in...
0
0
-1
16,979
981
Magic multisets
[ "data structures" ]
null
null
In the School of Magic in Dirtpolis a lot of interesting objects are studied on Computer Science lessons. Consider, for example, the magic multiset. If you try to add an integer to it that is already presented in the multiset, each element in the multiset duplicates. For example, if you try to add the integer $2$ to t...
The first line contains two integers $n$ and $q$ ($1 \leq n, q \leq 2 \cdot 10^{5}$)Β β€” the number of magic multisets in the array and the number of queries, respectively. The next $q$ lines describe queries, one per line. Each line starts with an integer $t$ ($1 \leq t \leq 2$)Β β€” the type of the query. If $t$ equals $...
For each query of the second type print the sum of sizes of multisets on the given segment. The answers can be large, so print them modulo $998244353$.
[ "4 4\n1 1 2 1\n1 1 2 2\n1 1 4 1\n2 1 4\n", "3 7\n1 1 1 3\n1 1 1 3\n1 1 1 2\n1 1 1 1\n2 1 1\n1 1 1 2\n2 1 1\n" ]
[ "10\n", "4\n8\n" ]
In the first example after the first two queries the multisets are equal to $[\{1, 2\},\{1, 2\},\{\},\{\}]$, after the third query they are equal to $[\{1, 1, 2, 2\},\{1, 1, 2, 2\},\{1\},\{1\}]$. In the second example the first multiset evolves as follows: $\{\} \to \{3\} \to \{3, 3\} \to \{2, 3, 3\} \to \{1, 2, 3, ...
[]
1,809
268,390,400
0
17,040
134
Pairs of Numbers
[ "brute force", "dfs and similar", "math", "number theory" ]
null
null
Let's assume that we have a pair of numbers (*a*,<=*b*). We can get a new pair (*a*<=+<=*b*,<=*b*) or (*a*,<=*a*<=+<=*b*) from the given pair in a single step. Let the initial pair of numbers be (1,1). Your task is to find number *k*, that is, the least number of steps needed to transform (1,1) into the pair where at ...
The input contains the only integer *n* (1<=≀<=*n*<=≀<=106).
Print the only integer *k*.
[ "5\n", "1\n" ]
[ "3\n", "0\n" ]
The pair (1,1) can be transformed into a pair containing 5 in three moves: (1,1)  →  (1,2)  →  (3,2)  →  (5,2).
[ { "input": "5", "output": "3" }, { "input": "1", "output": "0" }, { "input": "2", "output": "1" }, { "input": "3", "output": "2" }, { "input": "4", "output": "3" }, { "input": "10", "output": "5" }, { "input": "1009", "output": "15" }...
1,000
4,608,000
0
17,060
15
Laser
[ "math" ]
B. Laser
1
64
Petya is the most responsible worker in the Research Institute. So he was asked to make a very important experiment: to melt the chocolate bar with a new laser device. The device consists of a rectangular field of *n*<=Γ—<=*m* cells and a robotic arm. Each cell of the field is a 1<=Γ—<=1 square. The robotic arm has two l...
The first line contains one integer number *t* (1<=≀<=*t*<=≀<=10000) β€” the number of test sets. Each of the following *t* lines describes one test set. Each line contains integer numbers *n*, *m*, *x*1, *y*1, *x*2, *y*2, separated by a space (2<=≀<=*n*,<=*m*<=≀<=109, 1<=≀<=*x*1,<=*x*2<=≀<=*n*, 1<=≀<=*y*1,<=*y*2<=≀<=*m*...
Each of the *t* lines of the output should contain the answer to the corresponding input test set.
[ "2\n4 4 1 1 3 3\n4 3 1 1 2 2\n" ]
[ "8\n2\n" ]
none
[ { "input": "2\n4 4 1 1 3 3\n4 3 1 1 2 2", "output": "8\n2" }, { "input": "1\n2 2 1 2 2 1", "output": "2" }, { "input": "1\n2 2 1 2 2 1", "output": "2" }, { "input": "1\n3 3 3 2 1 1", "output": "5" }, { "input": "1\n3 4 1 1 1 2", "output": "0" }, { "inp...
0
0
-1
17,070
83
Numbers
[ "dp", "math", "number theory" ]
D. Numbers
3
256
One quite ordinary day Valera went to school (there's nowhere else he should go on a week day). In a maths lesson his favorite teacher Ms. Evans told students about divisors. Despite the fact that Valera loved math, he didn't find this particular topic interesting. Even more, it seemed so boring that he fell asleep in ...
The first and only line contains three positive integers *a*, *b*, *k* (1<=≀<=*a*<=≀<=*b*<=≀<=2Β·109,<=2<=≀<=*k*<=≀<=2Β·109).
Print on a single line the answer to the given problem.
[ "1 10 2\n", "12 23 3\n", "6 19 5\n" ]
[ "5\n", "2\n", "0\n" ]
Comments to the samples from the statement: In the first sample the answer is numbers 2, 4, 6, 8, 10. In the second one β€” 15, 21 In the third one there are no such numbers.
[ { "input": "1 10 2", "output": "5" }, { "input": "12 23 3", "output": "2" }, { "input": "6 19 5", "output": "0" }, { "input": "1 80 7", "output": "3" }, { "input": "100 1000 1009", "output": "0" }, { "input": "11 124 11", "output": "2" }, { ...
3,000
0
0
17,083