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
109
Lucky Tree
[ "dp", "dsu", "trees" ]
C. Lucky Tree
2
256
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. One day Petya encountered a tree with *n* vertexes. Besides, the tree was weighted, i. e. each edge ...
The first line contains the single integer *n* (1<=≀<=*n*<=≀<=105) β€” the number of tree vertexes. Next *n*<=-<=1 lines contain three integers each: *u**i* *v**i* *w**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*,<=1<=≀<=*w**i*<=≀<=109) β€” the pair of vertexes connected by the edge and the edge's weight.
On the single line print the single number β€” the answer. Please do not use the %lld specificator to read or write 64-bit numbers in Π‘++. It is recommended to use the cin, cout streams or the %I64d specificator.
[ "4\n1 2 4\n3 1 2\n1 4 7\n", "4\n1 2 4\n1 3 47\n1 4 7447\n" ]
[ "16\n", "24\n" ]
The 16 triples of vertexes from the first sample are: (1, 2, 4), (1, 4, 2), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3), (3, 2, 4), (3, 4, 2), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2). In the second sample all the triples should be counted: 4Β·3Β·2 = 24.
[ { "input": "4\n1 2 4\n3 1 2\n1 4 7", "output": "16" }, { "input": "4\n1 2 4\n1 3 47\n1 4 7447", "output": "24" }, { "input": "9\n1 2 7\n1 3 12\n4 1 2\n4 5 4\n4 6 47\n4 7 9\n5 8 2\n5 9 1", "output": "282" }, { "input": "2\n1 2 7", "output": "0" }, { "input": "2\n2 ...
466
2,867,200
-1
116,334
180
Cubes
[ "binary search", "dp", "two pointers" ]
null
null
Let's imagine that you're playing the following simple computer game. The screen displays *n* lined-up cubes. Each cube is painted one of *m* colors. You are allowed to delete not more than *k* cubes (that do not necessarily go one after another). After that, the remaining cubes join together (so that the gaps are clos...
The first line contains three integers *n*, *m* and *k* (1<=≀<=*n*<=≀<=2Β·105,<=1<=≀<=*m*<=≀<=105,<=0<=≀<=*k*<=&lt;<=*n*). The second line contains *n* integers from 1 to *m* β€” the numbers of cube colors. The numbers of colors are separated by single spaces.
Print the maximum possible number of points you can score.
[ "10 3 2\n1 2 1 1 3 2 1 1 2 2\n", "10 2 2\n1 2 1 2 1 1 2 1 1 2\n", "3 1 2\n1 1 1\n" ]
[ "4\n", "5\n", "3\n" ]
In the first sample you should delete the fifth and the sixth cubes. In the second sample you should delete the fourth and the seventh cubes. In the third sample you shouldn't delete any cubes.
[ { "input": "10 3 2\n1 2 1 1 3 2 1 1 2 2", "output": "4" }, { "input": "10 2 2\n1 2 1 2 1 1 2 1 1 2", "output": "5" }, { "input": "3 1 2\n1 1 1", "output": "3" }, { "input": "10 2 2\n1 1 1 2 1 2 1 2 1 1", "output": "5" }, { "input": "1 1 0\n1", "output": "1" ...
92
0
0
116,366
690
Recover Polygon (easy)
[]
null
null
The zombies are gathering in their secret lair! Heidi will strike hard to destroy them once and for all. But there is a little problem... Before she can strike, she needs to know where the lair is. And the intel she has is not very good. Heidi knows that the lair can be represented as a rectangle on a lattice, with si...
The first line of each test case contains one integer *N*, the size of the lattice grid (5<=≀<=*N*<=≀<=50). The next *N* lines each contain *N* characters, describing the level of Zombie Contamination of each cell in the lattice. Every character of every line is a digit between 0 and 4. Cells are given in the same ord...
The first line of the output should contain Yes if there exists a single non-zero area rectangular lair with corners on the grid for which checking the levels of Zombie Contamination gives the results given in the input, and No otherwise.
[ "6\n000000\n000000\n012100\n024200\n012100\n000000\n" ]
[ "Yes\n" ]
The lair, if it exists, has to be rectangular (that is, have corners at some grid points with coordinates (*x*<sub class="lower-index">1</sub>, *y*<sub class="lower-index">1</sub>), (*x*<sub class="lower-index">1</sub>, *y*<sub class="lower-index">2</sub>), (*x*<sub class="lower-index">2</sub>, *y*<sub class="lower-ind...
[ { "input": "6\n000000\n000000\n012100\n024200\n012100\n000000", "output": "Yes" }, { "input": "6\n000000\n012210\n024420\n012210\n000000\n000000", "output": "Yes" }, { "input": "6\n000100\n001210\n002420\n001210\n000000\n000000", "output": "No" }, { "input": "10\n0000000000\n...
62
0
0
116,804
346
Doodle Jump
[ "math", "number theory" ]
null
null
In Doodle Jump the aim is to guide a four-legged creature called "The Doodler" up a never-ending series of platforms without falling. β€” Wikipedia. It is a very popular game and xiaodao likes it very much. One day when playing the game she wondered whether there exists a platform that the doodler couldn't reach due to...
The first line contains an integer *t* (1<=≀<=*t*<=≀<=104) β€” the number of problem instances. Each of the next *t* lines contains four integers *a*, *n*, *p* and *h* (1<=≀<=*a*<=≀<=109, 1<=≀<=*n*<=&lt;<=*p*<=≀<=109, 0<=≀<=*h*<=≀<=109). It's guaranteed that *a* and *p* are co-prime.
For each problem instance, if the Doodler can reach the highest platform, output "YES", otherwise output "NO".
[ "3\n7 4 12 2\n7 1 9 4\n7 4 12 3\n" ]
[ "NO\nNO\nYES\n" ]
none
[]
2,000
102,400
0
116,910
546
Soldier and Badges
[ "brute force", "greedy", "implementation", "sortings" ]
null
null
Colonel has *n* badges. He wants to give one badge to every of his *n* soldiers. Each badge has a coolness factor, which shows how much it's owner reached. Coolness factor can be increased by one for the cost of one coin. For every pair of soldiers one of them should get a badge with strictly higher factor than the s...
First line of input consists of one integer *n* (1<=≀<=*n*<=≀<=3000). Next line consists of *n* integers *a**i* (1<=≀<=*a**i*<=≀<=*n*), which stand for coolness factor of each badge.
Output single integer β€” minimum amount of coins the colonel has to pay.
[ "4\n1 3 1 4\n", "5\n1 2 3 2 5\n" ]
[ "1", "2" ]
In first sample test we can increase factor of first badge by 1. In second sample test we can increase factors of the second and the third badge by 1.
[ { "input": "4\n1 3 1 4", "output": "1" }, { "input": "5\n1 2 3 2 5", "output": "2" }, { "input": "5\n1 5 3 2 4", "output": "0" }, { "input": "10\n1 1 2 3 4 5 6 7 8 9", "output": "9" }, { "input": "11\n9 2 10 3 1 5 7 1 4 8 6", "output": "10" }, { "input...
30
0
0
116,973
294
Shaass and Painter Robot
[ "brute force", "implementation", "number theory" ]
null
null
Shaass thinks a kitchen with all white floor tiles is so boring. His kitchen floor is made of *n*Β·*m* square tiles forming a *n*<=Γ—<=*m* rectangle. Therefore he's decided to color some of the tiles in black so that the floor looks like a checkerboard, which is no two side-adjacent tiles should have the same color. Sha...
The first line of the input contains two integers *n* and *m*, (2<=≀<=*n*,<=*m*<=≀<=105). The second line contains two integers *x**s* and *y**s* (1<=≀<=*x**s*<=≀<=*n*,<=1<=≀<=*y**s*<=≀<=*m*) and the direction robot is facing initially. Direction is one of the strings: "UL" (upper-left direction), "UR" (upper-right), "...
Print the amount of paint the robot consumes to obtain a checkered kitchen floor. Or print -1 if it never happens. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator.
[ "3 4\n1 1 DR\n", "3 4\n3 3 DR\n", "3 3\n1 1 DR\n", "3 3\n1 2 DL\n" ]
[ "7\n", "11\n", "-1\n", "4\n" ]
none
[ { "input": "3 4\n1 1 DR", "output": "7" }, { "input": "3 4\n3 3 DR", "output": "11" }, { "input": "3 3\n1 1 DR", "output": "-1" }, { "input": "3 3\n1 2 DL", "output": "4" }, { "input": "2 5\n2 3 DR", "output": "7" }, { "input": "2 3\n2 3 DL", "outp...
60
0
0
117,186
246
Blood Cousins Return
[ "binary search", "data structures", "dfs and similar", "dp", "sortings" ]
null
null
Polycarpus got hold of a family tree. The found tree describes the family relations of *n* people, numbered from 1 to *n*. Every person in this tree has at most one direct ancestor. Also, each person in the tree has a name, the names are not necessarily unique. We call the man with a number *a* a 1-ancestor of the man...
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=105) β€” the number of people in the tree. Next *n* lines contain the description of people in the tree. The *i*-th line contains space-separated string *s**i* and integer *r**i* (0<=≀<=*r**i*<=≀<=*n*), where *s**i* is the name of the man with a num...
Print *m* whitespace-separated integers β€” the answers to Polycarpus's records. Print the answers to the records in the order, in which the records occur in the input.
[ "6\npasha 0\ngerald 1\ngerald 1\nvalera 2\nigor 3\nolesya 1\n5\n1 1\n1 2\n1 3\n3 1\n6 1\n", "6\nvalera 0\nvalera 1\nvalera 1\ngerald 0\nvalera 4\nkolya 4\n7\n1 1\n1 2\n2 1\n2 2\n4 1\n5 1\n6 1\n" ]
[ "2\n2\n0\n1\n0\n", "1\n0\n0\n0\n2\n0\n0\n" ]
none
[ { "input": "6\npasha 0\ngerald 1\ngerald 1\nvalera 2\nigor 3\nolesya 1\n5\n1 1\n1 2\n1 3\n3 1\n6 1", "output": "2\n2\n0\n1\n0" }, { "input": "6\nvalera 0\nvalera 1\nvalera 1\ngerald 0\nvalera 4\nkolya 4\n7\n1 1\n1 2\n2 1\n2 2\n4 1\n5 1\n6 1", "output": "1\n0\n0\n0\n2\n0\n0" }, { "input":...
62
307,200
-1
117,922
33
Helper
[]
E. Helper
2
256
It's unbelievable, but an exam period has started at the OhWord University. It's even more unbelievable, that Valera got all the tests before the exam period for excellent work during the term. As now he's free, he wants to earn money by solving problems for his groupmates. He's made a *list* of subjects that he can he...
The first line contains integers *m*,<=*n*,<=*k* (1<=≀<=*m*,<=*n*<=≀<=100, 1<=≀<=*k*<=≀<=30) β€” amount of subjects on the *list*, amount of Valera's potential employers and the duration of the exam period in days. The following *m* lines contain the names of subjects *list**i* (*list**i* is a non-empty string of at mos...
In the first line output the maximum profit that Valera can get. The second line should contain number *p* β€” amount of problems that Valera is to solve. In the following *p* lines output the order of solving problems in chronological order in the following format: index of a student, to whom Valera is to help; index of...
[ "3 3 4\ncalculus\nalgebra\nhistory\n58 23 15\n00:00-08:15\n08:20-08:35\n09:30-10:25\n19:00-19:45\ncalculus 1 09:36 100\nenglish 4 21:15 5000\nhistory 1 19:50 50\n", "2 2 1\nmatan\ncodeforces\n1 2\n00:00-08:00\n09:00-09:00\n12:00-12:00\n18:00-18:00\ncodeforces 1 08:04 2\nmatan 1 08:02 1\n", "2 2 1\nmatan\ncodefo...
[ "150\n2\n1 1 08:16 1 09:29\n3 1 10:26 1 10:40\n", "3\n2\n2 1 08:01 1 08:01\n1 1 08:02 1 08:03\n", "2\n1\n1 1 08:01 1 08:02\n" ]
none
[]
124
6,656,000
0
117,936
46
Comb
[ "data structures", "dp" ]
E. Comb
1
256
Having endured all the hardships, Lara Croft finally found herself in a room with treasures. To her surprise she didn't find golden mountains there. Lara looked around and noticed on the floor a painted table *n*<=Γ—<=*m* panels in size with integers written on the panels. There also was a huge number of stones lying by...
The first line contains a pair of integers *n*,<=*m* (2<=≀<=*n*,<=*m*<=≀<=1500). Next *n* lines contain *m* integers each β€” that is the table itself. The absolute value of the numbers in the table does not exceed 10000.
Print the single number β€” the maximum number of coins Lara can get.
[ "2 2\n-1 2\n1 3\n" ]
[ "2\n" ]
none
[]
46
0
0
117,967
309
Morning run
[ "binary search", "math", "two pointers" ]
null
null
People like to be fit. That's why many of them are ready to wake up at dawn, go to the stadium and run. In this problem your task is to help a company design a new stadium. The city of N has a shabby old stadium. Many people like it and every morning thousands of people come out to this stadium to run. The stadium ca...
The first line of the input contains three integers *n*, *l*, *t* (1<=≀<=*n*<=≀<=106,<=1<=≀<=*l*<=≀<=109,<=1<=≀<=*t*<=≀<=109). The next line contains *n* distinct integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a*1<=&lt;<=*a*2<=&lt;<=...<=&lt;<=*a**n*<=&lt;<=*l*), here *a**i* is the clockwise distance from the start line t...
Print a single real number β€” the answer to the problem with absolute or relative error of at most 10<=-<=6.
[ "2 5 1\n0 2\n", "3 7 3\n0 1 6\n" ]
[ "0.2500000000\n", "1.5000000000\n" ]
There are two runners in the first example. If the first runner run clockwise direction, then in 1 time unit he will be 1m away from the start line. If the second runner run counter-clockwise direction then in 1 time unit he will be also 1m away from the start line. And it is the only possible way to meet. We assume th...
[]
0
0
-1
118,499
509
Pretty Song
[ "math", "strings" ]
null
null
When Sasha was studying in the seventh grade, he started listening to music a lot. In order to evaluate which songs he likes more, he introduced the notion of the song's prettiness. The title of the song is a word consisting of uppercase Latin letters. The prettiness of the song is the prettiness of its title. Let's d...
The input contains a single string *s* (1<=≀<=|*s*|<=≀<=5Β·105) β€” the title of the song.
Print the prettiness of the song with the absolute or relative error of at most 10<=-<=6.
[ "IEAIAIO\n", "BYOB\n", "YISVOWEL\n" ]
[ "28.0000000\n", "5.8333333\n", "17.0500000\n" ]
In the first sample all letters are vowels. The simple prettiness of each substring is 1. The word of length 7 has 28 substrings. So, the prettiness of the song equals to 28.
[ { "input": "IEAIAIO", "output": "28.0000000" }, { "input": "BYOB", "output": "5.8333333" }, { "input": "YISVOWEL", "output": "17.0500000" }, { "input": "EZYYOIYUZXEVRTOUYXIQ", "output": "124.0168163" }, { "input": "MTOESEPRFEIWAIWLAFJMGBIQB", "output": "127.22...
171
34,406,400
3
118,557
615
Running Track
[ "dp", "greedy", "strings", "trees" ]
null
null
A boy named Ayrat lives on planet AMI-1511. Each inhabitant of this planet has a talent. Specifically, Ayrat loves running, moreover, just running is not enough for him. He is dreaming of making running a real art. First, he wants to construct the running track with coating *t*. On planet AMI-1511 the coating of the t...
First line of the input contains the string *s*Β β€” the coating that is present in the shop. Second line contains the string *t*Β β€” the coating Ayrat wants to obtain. Both strings are non-empty, consist of only small English letters and their length doesn't exceed 2100.
The first line should contain the minimum needed number of coatings *n* or -1 if it's impossible to create the desired coating. If the answer is not -1, then the following *n* lines should contain two integers *x**i* and *y**i*Β β€” numbers of ending blocks in the corresponding piece. If *x**i*<=≀<=*y**i* then this piece...
[ "abc\ncbaabc\n", "aaabrytaaa\nayrat\n", "ami\nno\n" ]
[ "2\n3 1\n1 3\n", "3\n1 1\n6 5\n8 7\n", "-1\n" ]
In the first sample string "cbaabc" = "cba" + "abc". In the second sample: "ayrat" = "a" + "yr" + "at".
[ { "input": "abc\ncbaabc", "output": "2\n3 1\n1 3" }, { "input": "aaabrytaaa\nayrat", "output": "3\n1 1\n6 5\n8 7" }, { "input": "ami\nno", "output": "-1" }, { "input": "r\nr", "output": "1\n1 1" }, { "input": "r\nb", "output": "-1" }, { "input": "randb...
140
3,072,000
3
118,786
0
none
[ "none" ]
null
null
Julia is conducting an experiment in her lab. She placed several luminescent bacterial colonies in a horizontal testtube. Different types of bacteria can be distinguished by the color of light they emit. Julia marks types of bacteria with small Latin letters "a", ..., "z". The testtube is divided into *n* consecutive ...
The first line contains an integer *n*Β β€” the number of regions in the testtube (1<=≀<=*n*<=≀<=5<=000). The second line contains *n* small Latin letters that describe the initial population of the testtube.
Print one numberΒ β€” the answer to the problem modulo 109<=+<=7.
[ "3\naaa\n", "2\nab\n", "4\nbabb\n", "7\nabacaba\n" ]
[ "1\n", "3\n", "11\n", "589\n" ]
In the first sample the population can never change since all bacteria are of the same type. In the second sample three configurations are possible: "ab" (no attacks), "aa" (the first colony conquers the second colony), and "bb" (the second colony conquers the first colony). To get the answer for the third sample, no...
[]
30
0
0
119,086
305
Playing with String
[ "games" ]
null
null
Two people play the following string game. Initially the players have got some string *s*. The players move in turns, the player who cannot make a move loses. Before the game began, the string is written on a piece of paper, one letter per cell. A player's move is the sequence of actions: 1. The player chooses one...
The first line contains string *s* (1<=≀<=|*s*|<=≀<=5000). It is guaranteed that string *s* only contains lowercase English letters.
If the second player wins, print in the single line "Second" (without the quotes). Otherwise, print in the first line "First" (without the quotes), and in the second line print the minimal possible winning move β€” integer *i* (1<=≀<=*i*<=≀<=|*s*|).
[ "abacaba\n", "abcde\n" ]
[ "First\n2\n", "Second\n" ]
In the first sample the first player has multiple winning moves. But the minimum one is to cut the character in position 2. In the second sample the first player has no available moves.
[ { "input": "abacaba", "output": "First\n2" }, { "input": "abcde", "output": "Second" }, { "input": "aaaaa", "output": "First\n3" }, { "input": "aaabbbbbbbbabaaabbaabbbbabbabaabaabbbaabbbbbbabbbabaabaaabaaaabbaaabbbbaabbbaaabababbbbabbabbabaaaaabababbbaabbbaabababaaabababbaaaa...
78
0
0
119,116
720
Array Covering
[ "data structures" ]
null
null
Misha has an array of integers of length *n*. He wants to choose *k* different continuous subarrays, so that each element of the array belongs to at least one of the chosen subarrays. Misha wants to choose the subarrays in such a way that if he calculated the sum of elements for each subarray, and then add up all thes...
The first line of input contains two integers: *n*, *k* (1<=≀<=*n*<=≀<=100<=000, 1<=≀<=*k*<=≀<=*n*Β·(*n*<=+<=1)<=/<=2)Β β€” the number of elements in the array and the number of different subarrays that must be chosen. The second line contains *n* integers *a**i* (<=-<=50<=000<=≀<=*a**i*<=≀<=50<=000)Β β€” the elements of the...
Output one integerΒ β€” the maximum possible value Misha can get by choosing *k* different subarrays.
[ "5 4\n6 -4 -10 -4 7\n" ]
[ "11\n" ]
none
[]
46
0
0
119,266
414
Mashmokh's Designed Problem
[ "data structures" ]
null
null
After a lot of trying, Mashmokh designed a problem and it's your job to solve it. You have a tree *T* with *n* vertices. Each vertex has a unique index from 1 to *n*. The root of *T* has index 1. For each vertex of this tree *v*, you are given a list of its children in a specific order. You must perform three types of...
The first line of input contains two space-separated integers *n*,<=*m*Β (2<=≀<=*n*<=≀<=105;Β 1<=≀<=*m*<=≀<=105), the number of vertices of *T* and number of queries to perform. The *i*-th of the following *n* lines contains an integer *l**i*Β (0<=≀<=*l**i*<=≀<=*n*), number of *i*-th vertex's children. Then *l**i* space-...
For each query of the first or third type output one line containing the result of the query.
[ "4 9\n1 2\n1 3\n1 4\n0\n1 1 4\n2 4 2\n1 3 4\n3 1\n3 2\n2 3 2\n1 1 2\n3 1\n3 2\n", "2 2\n1 2\n0\n1 2 1\n3 1\n" ]
[ "3\n2\n2\n4\n1\n3\n4\n", "1\n2\n" ]
none
[]
46
0
0
119,322
753
Interactive Bulls and Cows (Hard)
[ "brute force", "constructive algorithms", "interactive" ]
null
null
The only difference from the previous problem is the constraint on the number of requests. In this problem your program should guess the answer doing at most 7 requests. This problem is a little bit unusual. Here you are to implement an interaction with a testing system. That means that you can make queries and get re...
To read answers to the queries, the program must use the standard input. The program will receive pairs of non-negative integers in the input, one pair per line. The first number in a pair is a number of bulls and the second one is a number of cows of the string *s* and the string *x**i* printed by your program. If th...
The program must use the standard output to print queries. Your program must output requests β€” 4-digit strings *x*1,<=*x*2,<=..., one per line. After the output of each line the program must execute flush operation. The program should read the answer to the query from the standard input. Your program is allowed to do...
[ "0 1\n2 0\n1 1\n0 4\n2 1\n4 0\n" ]
[ "8000\n0179\n3159\n3210\n0112\n0123" ]
The secret string *s* in the example is "0123".
[ { "input": "0123", "output": "1" }, { "input": "1234", "output": "4" }, { "input": "9876", "output": "5" }, { "input": "7158", "output": "3" }, { "input": "7590", "output": "7" }, { "input": "7325", "output": "5" }, { "input": "7524", "...
0
0
-1
120,039
442
Gena and Second Distance
[ "geometry" ]
null
null
Gena doesn't like geometry, so he asks you to solve this problem for him. A rectangle with sides parallel to coordinate axes contains *n* dots. Let's consider some point of the plane. Let's count the distances from this point to the given *n* points. Let's sort these numbers in the non-decreasing order. We'll call the...
The first line contains three integers *w*,<=*h*,<=*n* (1<=≀<=*w*,<=*h*<=≀<=106,<=2<=≀<=*n*<=≀<=1000) β€” the lengths of the rectangle sides and the number of points. Next *n* lines contain two integers *x**i*,<=*y**i* (0<=≀<=*x**i*<=≀<=*w*,<=0<=≀<=*y**i*<=≀<=*h*) each β€” the coordinates of a point. It is possible that it...
Print a single number β€” the maximum beauty of a point with the absolute or relative error of at most 10<=-<=9.
[ "5 5 4\n0 0\n5 0\n0 5\n5 5\n", "5 5 3\n4 0\n2 5\n4 1\n" ]
[ "4.99999999941792340\n", "5.65685424744772010\n" ]
The point which beauty we need to find must have coordinates (*x*, *y*), where 0 ≀ *x* ≀ *w*, 0 ≀ *y* ≀ *h*. Some of the *n* points can coincide.
[]
46
0
0
120,238
855
Helga Hufflepuff's Cup
[ "dp", "trees" ]
null
null
Harry, Ron and Hermione have figured out that Helga Hufflepuff's cup is a horcrux. Through her encounter with Bellatrix Lestrange, Hermione came to know that the cup is present in Bellatrix's family vault in Gringott's Wizarding Bank. The Wizarding bank is in the form of a tree with total *n* vaults where each vault ...
The first line of input contains two space separated integers, *n* and *m*Β β€” the number of vaults and the number of different vault types possible. (1<=≀<=*n*<=≀<=105,<=1<=≀<=*m*<=≀<=109). Each of the next *n*<=-<=1 lines contain two space separated integers *u**i* and *v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*) representi...
Output a single integer, the number of ways of giving each vault a type following the conditions modulo 109<=+<=7.
[ "4 2\n1 2\n2 3\n1 4\n1 2\n", "3 3\n1 2\n1 3\n2 1\n", "3 1\n1 2\n1 3\n1 1\n" ]
[ "1\n", "13\n", "0\n" ]
In test case 1, we cannot have any vault of the highest security as its type is 1 implying that its adjacent vaults would have to have a vault type less than 1, which is not allowed. Thus, there is only one possible combination, in which all the vaults have type 2.
[ { "input": "4 2\n1 2\n2 3\n1 4\n1 2", "output": "1" }, { "input": "3 3\n1 2\n1 3\n2 1", "output": "13" }, { "input": "3 1\n1 2\n1 3\n1 1", "output": "0" }, { "input": "3 1000000000\n2 3\n3 1\n585430050 9", "output": "91592837" }, { "input": "4 50000\n2 1\n4 2\n2 3...
46
29,081,600
0
120,350
0
none
[ "none" ]
null
null
The German University in Cairo (GUC) dorm houses are numbered from 1 to *n*. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle). For each house, t...
The first line contains two space-separated integers *n* and *p* (1<=≀<=*n*<=≀<=1000,<=0<=≀<=*p*<=≀<=*n*) β€” the number of houses and the number of pipes correspondingly. Then *p* lines follow β€” the description of *p* pipes. The *i*-th line contains three integers *a**i* *b**i* *d**i*, indicating a pipe of diameter *d...
Print integer *t* in the first line β€” the number of tank-tap pairs of houses. For the next *t* lines, print 3 integers per line, separated by spaces: *tank**i*, *tap**i*, and *diameter**i*, where *tank**i*<=β‰ <=*tap**i* (1<=≀<=*i*<=≀<=*t*). Here *tank**i* and *tap**i* are indexes of tank and tap houses respectively, an...
[ "3 2\n1 2 10\n2 3 20\n", "3 3\n1 2 20\n2 3 10\n3 1 5\n", "4 2\n1 2 60\n3 4 50\n" ]
[ "1\n1 3 10\n", "0\n", "2\n1 2 60\n3 4 50\n" ]
none
[ { "input": "3 2\n1 2 10\n2 3 20", "output": "1\n1 3 10" }, { "input": "3 3\n1 2 20\n2 3 10\n3 1 5", "output": "0" }, { "input": "4 2\n1 2 60\n3 4 50", "output": "2\n1 2 60\n3 4 50" }, { "input": "10 10\n10 3 70\n1 9 98\n9 10 67\n5 2 78\n8 6 71\n4 8 95\n7 1 10\n2 5 73\n6 7 94\...
46
0
0
120,793
650
Zip-line
[ "binary search", "data structures", "dp", "hashing" ]
null
null
Vasya has decided to build a zip-line on trees of a nearby forest. He wants the line to be as long as possible but he doesn't remember exactly the heights of all trees in the forest. He is sure that he remembers correct heights of all trees except, possibly, one of them. It is known that the forest consists of *n* tre...
The first line of the input contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=400<=000)Β β€” the number of the trees in the forest and the number of Petya's assumptions, respectively. The following line contains *n* integers *h**i* (1<=≀<=*h**i*<=≀<=109)Β β€” the heights of trees according to Vasya. Each of the follow...
For each of the Petya's assumptions output one integer, indicating the maximum length of a zip-line that can be built under this assumption.
[ "4 4\n1 2 3 4\n1 1\n1 4\n4 3\n4 5\n", "4 2\n1 3 2 6\n3 5\n2 4\n" ]
[ "4\n3\n3\n4\n", "4\n3\n" ]
Consider the first sample. The first assumption actually coincides with the height remembered by Vasya. In the second assumption the heights of the trees are (4, 2, 3, 4), in the third one they are (1, 2, 3, 3) and in the fourth one they are (1, 2, 3, 5).
[]
3,000
28,262,400
0
120,838
566
Logistical Questions
[ "dfs and similar", "divide and conquer", "trees" ]
null
null
Some country consists of *n* cities, connected by a railroad network. The transport communication of the country is so advanced that the network consists of a minimum required number of (*n*<=-<=1) bidirectional roads (in the other words, the graph of roads is a tree). The *i*-th road that directly connects cities *a**...
The first line of the input contains number *n* (1<=≀<=*n*<=≀<=200<=000) β€” the number of cities in the country. The next line contains *n* integers *w*1,<=*w*2,<=...,<=*w**n* (0<=≀<=*w**i*<=≀<=108) β€” the number of finalists living in each city of the country. Next (*n*<=-<=1) lines contain the descriptions of the rai...
Print two numbers β€” an integer *f* that is the number of the optimal city to conduct the competition, and the real number *c*, equal to the minimum total cost of transporting all the finalists to the competition. Your answer will be considered correct if two conditions are fulfilled at the same time: 1. The absolute...
[ "5\n3 1 2 6 5\n1 2 3\n2 3 1\n4 3 9\n5 3 1\n", "2\n5 5\n1 2 2\n" ]
[ "3 192.0", "1 14.142135623730951000\n" ]
In the sample test an optimal variant of choosing a city to conduct the finals of the competition is 3. At such choice the cost of conducting is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4219dfaeaed77b348dc7ca0c500739d1c0ff9e3f.png" style="max-width: 100.0%;max-height: 100.0%;"/> burl...
[]
2,000
716,800
0
121,034
161
Polycarpus the Safecracker
[ "brute force", "dp" ]
null
null
Polycarpus has *t* safes. The password for each safe is a square matrix consisting of decimal digits '0' ... '9' (the sizes of passwords to the safes may vary). Alas, Polycarpus has forgotten all passwords, so now he has to restore them. Polycarpus enjoys prime numbers, so when he chose the matrix passwords, he wrote ...
The first line of the input contains an integer *t* (1<=≀<=*t*<=≀<=30) β€” the number of safes. Next *t* lines contain integers *p**i* (10<=≀<=*p**i*<=≀<=99999), *p**i* is a prime number written in the first row of the password matrix for the *i*-th safe. All *p**i*'s are written without leading zeros.
Print *t* numbers, the *i*-th of them should be the number of matrices that can be a password to the *i*-th safe. Print the numbers on separate lines.
[ "4\n11\n239\n401\n9001\n" ]
[ "4\n28\n61\n2834\n" ]
Here is a possible password matrix for the second safe: Here is a possible password matrix for the fourth safe:
[]
92
0
0
121,048
54
Writing a Song
[ "brute force", "dp", "strings" ]
D. Writing a Song
2
256
One of the Hedgehog and his friend's favorite entertainments is to take some sentence or a song and replace half of the words (sometimes even all of them) with each other's names. The friend's birthday is approaching and the Hedgehog decided to make a special present to his friend: a very long song, where his name wil...
The first line contains numbers *N* and *K* which are the length of the required string and the alphabet size accordingly. The limitations are: 1<=≀<=*N*<=≀<=100, 2<=≀<=*K*<=≀<=26. The second line contains the name *P* which is a non-empty string whose length does not exceed *N* characters. The string consists only of...
Print the desired word *S*. If there are several answers, print any of them. If there is no solution, then print "No solution".
[ "5 2\naba\n101\n", "5 2\na\n10001\n", "6 2\nabba\n101\n" ]
[ "ababa", "abbba", "No solution" ]
none
[ { "input": "5 2\naba\n101", "output": "ababa" }, { "input": "5 2\na\n10001", "output": "abbba" }, { "input": "6 2\nabba\n101", "output": "No solution" }, { "input": "12 5\nabacaba\n010001", "output": "aabacabacaba" }, { "input": "9 3\nac\n10100101", "output": ...
92
0
0
121,772
611
New Year and Three Musketeers
[ "data structures", "greedy", "sortings" ]
null
null
Do you know the story about the three musketeers? Anyway, you must help them now. Richelimakieu is a cardinal in the city of Bearis. He found three brave warriors and called them the three musketeers. Athos has strength *a*, Borthos strength *b*, and Caramis has strength *c*. The year 2015 is almost over and there ar...
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=200<=000)Β β€” the number of criminals. The second line contains three integers *a*, *b* and *c* (1<=≀<=*a*,<=*b*,<=*c*<=≀<=108)Β β€” strengths of musketeers. The third line contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (1<=≀<=*t**i*<=≀<=108)Β β€” str...
Print one line with the answer. If it's impossible to defeat all criminals, print "-1" (without the quotes). Otherwise, print the minimum number of hours the three musketeers will spend on defeating all criminals.
[ "5\n10 20 30\n1 1 1 1 50\n", "5\n10 20 30\n1 1 1 1 51\n", "7\n30 20 10\n34 19 50 33 88 15 20\n", "6\n10 5 10\n10 9 5 25 20 5\n" ]
[ "2\n", "3\n", "-1\n", "3\n" ]
In the first sample Athos has strength 10, Borthos 20, and Caramis 30. They can defeat all criminals in two hours: - Borthos and Caramis should together fight a criminal with strength 50. In the same hour Athos can fight one of four criminals with strength 1. - There are three criminals left, each with strength 1. E...
[ { "input": "5\n10 20 30\n1 1 1 1 50", "output": "2" }, { "input": "5\n10 20 30\n1 1 1 1 51", "output": "3" }, { "input": "7\n30 20 10\n34 19 50 33 88 15 20", "output": "-1" }, { "input": "6\n10 5 10\n10 9 5 25 20 5", "output": "3" }, { "input": "9\n10 20 30\n5 5 5...
0
0
-1
122,412
372
Counting Rectangles is Fun
[ "brute force", "divide and conquer", "dp" ]
null
null
There is an *n*<=Γ—<=*m* rectangular grid, each cell of the grid contains a single integer: zero or one. Let's call the cell on the *i*-th row and the *j*-th column as (*i*,<=*j*). Let's define a "rectangle" as four integers *a*,<=*b*,<=*c*,<=*d* (1<=≀<=*a*<=≀<=*c*<=≀<=*n*;Β 1<=≀<=*b*<=≀<=*d*<=≀<=*m*). Rectangle denotes...
There are three integers in the first line: *n*, *m* and *q* (1<=≀<=*n*,<=*m*<=≀<=40,<=1<=≀<=*q*<=≀<=3Β·105). Each of the next *n* lines contains *m* characters β€” the grid. Consider grid rows are numbered from top to bottom, and grid columns are numbered from left to right. Both columns and rows are numbered starting fr...
For each query output an answer β€” a single integer in a separate line.
[ "5 5 5\n00101\n00000\n00001\n01000\n00001\n1 2 2 4\n4 5 4 5\n1 2 5 2\n2 2 4 5\n4 2 5 3\n", "4 7 5\n0000100\n0000010\n0011000\n0000000\n1 7 2 7\n3 1 3 1\n2 3 4 5\n1 2 2 7\n2 2 4 7\n" ]
[ "10\n1\n7\n34\n5\n", "3\n1\n16\n27\n52\n" ]
For the first example, there is a 5 × 5 rectangular grid, and the first, the second, and the third queries are represented in the following image. - For the first query, there are 10 good rectangles, five 1 × 1, two 2 × 1, two 1 × 2, and one 1 × 3. - For the second query, there is only one 1 × 1 good rectangle. - F...
[ { "input": "5 5 5\n00101\n00000\n00001\n01000\n00001\n1 2 2 4\n4 5 4 5\n1 2 5 2\n2 2 4 5\n4 2 5 3", "output": "10\n1\n7\n34\n5" }, { "input": "4 7 5\n0000100\n0000010\n0011000\n0000000\n1 7 2 7\n3 1 3 1\n2 3 4 5\n1 2 2 7\n2 2 4 7", "output": "3\n1\n16\n27\n52" }, { "input": "10 10 10\n00...
61
2,867,200
-1
122,416
175
Gnomes of Might and Magic
[ "data structures", "graphs", "implementation", "shortest paths" ]
null
null
Vasya plays a popular game the Gnomes of Might and Magic. In this game Vasya manages the kingdom of gnomes, consisting of several castles, connected by bidirectional roads. The kingdom road network has a special form. The kingdom has *m* main castles *a*1,<=*a*2,<=...,<=*a**m*, which form the Good Path. This path cons...
The first line contains two integers *n* and *m* (3<=≀<=*m*<=≀<=*n*<=≀<=100000) β€” the number of castles in the kingdom, and the number of castles on the Good Path, respectively. The second line contains *m* integers, which are numbers of Good Path castles (the castles are numbered from 1 to *n*) in the order of occurr...
For each query "?" print a single number on a single line β€” the number of very bad gnomes destroyed by the corresponding Mission of Death. Print the answers to queries in the chronological order.
[ "6 3\n1 2 3\n3 1 4 2\n3 2 5 3\n3 3 6 1\n10\n+ 1 2\n+ 4 2\n+ 1 3\n+ 2 3\n? 1 2\n+ 2 5\n? 1 2\n? 1 2\n+ 1 2\n? 1 2\n" ]
[ "0\n1\n0\n1\n" ]
In the example after the first four requests there is only one path from castle 1 to castle 2, which does not contain roads with very bad gnomes: 1 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> 6...
[]
60
0
0
122,889
273
Dima and Figure
[ "dp" ]
null
null
Dima loves making pictures on a piece of squared paper. And yet more than that Dima loves the pictures that depict one of his favorite figures. A piece of squared paper of size *n*<=Γ—<=*m* is represented by a table, consisting of *n* rows and *m* columns. All squares are white on blank squared paper. Dima defines a p...
The first line contains two integers *n* and *m* β€” the sizes of the piece of paper (1<=≀<=*n*,<=*m*<=≀<=150).
In a single line print the remainder after dividing the answer to the problem by number 1000000007Β (109<=+<=7).
[ "2 2\n", "3 4\n" ]
[ "13\n", "571\n" ]
none
[ { "input": "2 2", "output": "13" }, { "input": "3 4", "output": "571" }, { "input": "2 8", "output": "948" }, { "input": "5 1", "output": "15" }, { "input": "10 5", "output": "10779285" }, { "input": "9 9", "output": "571345021" }, { "input...
60
0
0
122,898
136
Ternary Logic
[ "implementation", "math" ]
null
null
Little Petya very much likes computers. Recently he has received a new "Ternatron IV" as a gift from his mother. Unlike other modern computers, "Ternatron IV" operates with ternary and not binary logic. Petya immediately wondered how the *xor* operation is performed on this computer (and whether there is anything like ...
The first line contains two integers *a* and *c* (0<=≀<=*a*,<=*c*<=≀<=109). Both numbers are written in decimal notation.
Print the single integer *b*, such that *a* *tor* *b*<==<=*c*. If there are several possible numbers *b*, print the smallest one. You should print the number in decimal notation.
[ "14 34\n", "50 34\n", "387420489 225159023\n", "5 5\n" ]
[ "50\n", "14\n", "1000000001\n", "0\n" ]
none
[ { "input": "14 34", "output": "50" }, { "input": "50 34", "output": "14" }, { "input": "387420489 225159023", "output": "1000000001" }, { "input": "5 5", "output": "0" }, { "input": "23476 23875625", "output": "23860906" }, { "input": "11111 10101010",...
124
0
3
123,455
838
Future Failure
[ "dp", "games" ]
null
null
Alice and Bob are playing a game with a string of characters, with Alice going first. The string consists *n* characters, each of which is one of the first *k* letters of the alphabet. On a player’s turn, they can either arbitrarily permute the characters in the words, or delete exactly one character in the word (if th...
The first line of input will contain three integers *n*,<=*k*,<=*p* (1<=≀<=*n*<=≀<=250<=000, 1<=≀<=*k*<=≀<=26, 108<=≀<=*p*<=≀<=109<=+<=100, *p* will be prime).
Print a single integer, the number of winning words for Alice, modulo *p*.
[ "4 2 100000007\n" ]
[ "14\n" ]
There are 14 strings that that Alice can win with. For example, some strings are "bbaa" and "baaa". Alice will lose on strings like "aaaa" or "bbbb".
[]
46
0
0
123,481
107
Darts
[ "geometry", "probabilities" ]
E. Darts
5
256
The night after the graduation ceremony graduate students of German University in Cairo (GUC) are playing darts. As there's no real dart board available, the photographs of members of the GUC upper management are being used. So, *n* rectangular photos are placed on the wall. They can overlap arbitrary and even coincid...
The first line of input contains integer *n* (1<=≀<=*n*<=≀<=500) β€” the number of photos on the wall. Then follow *n* lines describing the photos, each containing 8 single-space-separated integers (coordinates of 4 vertices): *x*1, *y*1, *x*2, *y*2, *x*3, *y*3, *x*4, *y*4. Each photo is a rectangle with a nonzero area. ...
Print the expected score of the throw. The answer will be accepted if it has absolute or relative error not exceeding 10<=-<=6.
[ "1\n0 0 0 2 2 2 2 0\n", "1\n-1 0 0 1 1 0 0 -1\n", "4\n0 0 0 1 3 1 3 0\n0 0 0 3 1 3 1 0\n3 3 2 3 2 0 3 0\n3 3 3 2 0 2 0 3\n", "2\n-1 0 0 1 1 0 0 -1\n0 0 1 1 2 0 1 -1\n" ]
[ "1.0000000000\n", "1.0000000000\n", "1.5000000000\n", "1.1428571429\n" ]
none
[]
60
0
0
123,523
82
General Mobilization
[ "data structures", "dfs and similar", "sortings" ]
C. General Mobilization
2
256
The Berland Kingdom is a set of *n* cities connected with each other with *n*<=-<=1 railways. Each road connects exactly two different cities. The capital is located in city 1. For each city there is a way to get from there to the capital by rail. In the *i*-th city there is a soldier division number *i*, each divisio...
The first line contains the single integer *n* (1<=≀<=*n*<=≀<=5000). It is the number of cities in Berland. The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n*, where *a**i* represents the priority of the division, located in the city number *i*. All numbers *a*1,<=*a*2,<=...,<=*a**n* are d...
Print sequence *t*1,<=*t*2,<=...,<=*t**n*, where *t**i* stands for the number of days it takes for the division of city *i* to arrive to the capital. Separate numbers with spaces.
[ "4\n40 10 30 20\n1 2 1\n2 3 1\n4 2 1\n", "5\n5 4 3 2 1\n1 2 1\n2 3 1\n2 4 1\n4 5 1\n" ]
[ "0 1 3 2 ", "0 1 4 2 3 " ]
none
[]
92
0
0
123,737
553
Nudist Beach
[ "binary search", "graphs", "greedy" ]
null
null
Nudist Beach is planning a military operation to attack the Life Fibers. In this operation, they will attack and capture several cities which are currently under the control of the Life Fibers. There are *n* cities, labeled from 1 to *n*, and *m* bidirectional roads between them. Currently, there are Life Fibers in ev...
The first line of input contains three integers *n*,<=*m*,<=*k* (2<=<=≀<=<=*n*<=<=≀<=100<=000, 1<=≀<=*m*<=≀<=100<=000, 1<=≀<=*k*<=≀<=*n*<=-<=1). The second line of input contains *k* integers, representing the cities with fortresses. These cities will all be distinct. The next *m* lines contain the roads. The *i*-th...
The first line should contain an integer *r*, denoting the size of an optimum set (1<=≀<=*r*<=≀<=*n*<=-<=*k*). The second line should contain *r* integers, denoting the cities in the set. Cities may follow in an arbitrary order. This line should not contain any of the cities with fortresses. If there are multiple po...
[ "9 8 4\n3 9 6 8\n1 2\n1 3\n1 4\n1 5\n2 6\n2 7\n2 8\n2 9\n", "10 8 2\n2 9\n1 3\n2 9\n4 5\n5 6\n6 7\n7 8\n8 10\n10 4\n" ]
[ "3\n1 4 5\n", "8\n1 5 4 8 10 6 3 7\n" ]
The first example case achieves a strength of 1/2. No other subset is strictly better. The second example case achieves a strength of 1. Note that the subset doesn't necessarily have to be connected.
[ { "input": "9 8 4\n3 9 6 8\n1 2\n1 3\n1 4\n1 5\n2 6\n2 7\n2 8\n2 9", "output": "3\n5 1 4" }, { "input": "10 8 2\n2 9\n1 3\n2 9\n4 5\n5 6\n6 7\n7 8\n8 10\n10 4", "output": "8\n3 10 6 1 5 8 7 4" }, { "input": "2 1 1\n1\n2 1", "output": "1\n2" } ]
30
0
0
123,972
666
Codeword
[ "combinatorics", "strings" ]
null
null
The famous sculptor Cicasso is a Reberlandian spy! These is breaking news in Berlandian papers today. And now the sculptor is hiding. This time you give the shelter to the maestro. You have a protected bunker and you provide it to your friend. You set the security system in such way that only you can open the bunker. ...
The first line contains integer *m* (1<=≀<=*m*<=≀<=105) β€” the number of the events in the test case. The second line contains nonempty string *s* β€” the string generated by the bunker for the current day. The next *m* lines contain the description of the events. The description starts from integer *t* β€” the type of th...
For each query of the type 2 print the answer modulo 109<=+<=7 on the separate line.
[ "3\na\n2 2\n1 bc\n2 5\n" ]
[ "51\n162626\n" ]
In the first event words of the form "a?" and "?a" are counted, where ? is an arbitrary symbol. There are 26 words of each of these types, but the word "aa" satisfies both patterns, so the answer is 51.
[]
46
0
0
124,025
1,000
We Need More Bosses
[ "dfs and similar", "graphs", "trees" ]
null
null
Your friend is developing a computer game. He has already decided how the game world should look like β€” it should consist of $n$ locations connected by $m$ two-way passages. The passages are designed in such a way that it should be possible to get from any location to any other location. Of course, some passages shoul...
The first line contains two integers $n$ and $m$ ($2 \le n \le 3 \cdot 10^5$, $n - 1 \le m \le 3 \cdot 10^5$) β€” the number of locations and passages, respectively. Then $m$ lines follow, each containing two integers $x$ and $y$ ($1 \le x, y \le n$, $x \ne y$) describing the endpoints of one of the passages. It is gua...
Print one integer β€” the maximum number of bosses your friend can place, considering all possible choices for $s$ and $t$.
[ "5 5\n1 2\n2 3\n3 1\n4 1\n5 2\n", "4 3\n1 2\n4 3\n3 2\n" ]
[ "2\n", "3\n" ]
none
[ { "input": "5 5\n1 2\n2 3\n3 1\n4 1\n5 2", "output": "2" }, { "input": "4 3\n1 2\n4 3\n3 2", "output": "3" }, { "input": "50 72\n35 38\n19 46\n35 12\n27 30\n23 41\n50 16\n31 6\n20 33\n38 1\n10 35\n13 43\n29 25\n25 4\n1 13\n4 20\n36 29\n13 47\n48 5\n30 21\n30 38\n28 50\n41 45\n25 43\n40 3...
342
16,179,200
0
124,111
785
Anton and Permutation
[ "brute force", "data structures" ]
null
null
Anton likes permutations, especially he likes to permute their elements. Note that a permutation of *n* elements is a sequence of numbers {*a*1,<=*a*2,<=...,<=*a**n*}, in which every number from 1 to *n* appears exactly once. One day Anton got a new permutation and started to play with it. He does the following operat...
The first line of the input contains two integers *n* and *q* (1<=≀<=*n*<=≀<=200<=000,<=1<=≀<=*q*<=≀<=50<=000)Β β€” the length of the permutation and the number of operations that Anton does. Each of the following *q* lines of the input contains two integers *l**i* and *r**i* (1<=≀<=*l**i*,<=*r**i*<=≀<=*n*)Β β€” the indices...
Output *q* lines. The *i*-th line of the output is the number of inversions in the Anton's permutation after the *i*-th operation.
[ "5 4\n4 5\n2 4\n2 5\n2 2\n", "2 1\n2 1\n", "6 7\n1 4\n3 5\n2 3\n3 3\n3 6\n2 1\n5 1\n" ]
[ "1\n4\n3\n3\n", "1\n", "5\n6\n7\n7\n10\n11\n8\n" ]
Consider the first sample. After the first Anton's operation the permutation will be {1, 2, 3, 5, 4}. There is only one inversion in it: (4, 5). After the second Anton's operation the permutation will be {1, 5, 3, 2, 4}. There are four inversions: (2, 3), (2, 4), (2, 5) and (3, 4). After the third Anton's operation ...
[ { "input": "5 4\n4 5\n2 4\n2 5\n2 2", "output": "1\n4\n3\n3" }, { "input": "2 1\n2 1", "output": "1" }, { "input": "6 7\n1 4\n3 5\n2 3\n3 3\n3 6\n2 1\n5 1", "output": "5\n6\n7\n7\n10\n11\n8" }, { "input": "42 42\n23 7\n9 6\n17 22\n19 23\n15 42\n14 10\n17 12\n2 25\n8 39\n13 9\...
46
0
0
124,220
0
none
[ "none" ]
null
null
Santa Claus has *n* tangerines, and the *i*-th of them consists of exactly *a**i* slices. Santa Claus came to a school which has *k* pupils. Santa decided to treat them with tangerines. However, there can be too few tangerines to present at least one tangerine to each pupil. So Santa decided to divide tangerines into ...
The first line contains two positive integers *n* and *k* (1<=≀<=*n*<=≀<=106, 1<=≀<=*k*<=≀<=2Β·109) denoting the number of tangerines and the number of pupils, respectively. The second line consists of *n* positive integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=107), where *a**i* stands for the number of slices ...
If there's no way to present a tangerine or a part of tangerine to everyone, print -1. Otherwise, print the maximum possible joy that Santa can have.
[ "3 2\n5 9 3\n", "2 4\n12 14\n", "2 3\n1 1\n" ]
[ "5\n", "6\n", "-1\n" ]
In the first example Santa should divide the second tangerine into two parts with 5 and 4 slices. After that he can present the part with 5 slices to the first pupil and the whole first tangerine (with 5 slices, too) to the second pupil. In the second example Santa should divide both tangerines, so that he'll be able ...
[ { "input": "3 2\n5 9 3", "output": "5" }, { "input": "2 4\n12 14", "output": "6" }, { "input": "2 3\n1 1", "output": "-1" }, { "input": "1 1\n10", "output": "10" }, { "input": "2 2\n6 10", "output": "6" }, { "input": "5 3\n199999 200000 199999 199999 2...
1,231
268,390,400
0
124,254
306
Optimizer
[ "data structures", "greedy", "sortings" ]
null
null
A process RAM is a sequence of bytes that are indexed from 1 to *n*. Polycarpus's program contains such instructions as "memset", that is, the operations of filling memory cells on a segment with some value. The details are: the code only contains *m* instructions that look like "set13 a_i l_i". Instruction *i* fills a...
The first line contains integers *n* and *m* (1<=≀<=*n*<=≀<=2Β·106,<=1<=≀<=*m*<=≀<=2Β·105) β€” the number of bytes (memory cells) and the number of instructions in Polycarpus's code. Then *m* lines follow, each line contains a pair of integers *a**i*, *l**i* (1<=≀<=*a**i*<=≀<=*n*,<=1<=≀<=*l**i*<=≀<=*n*<=-<=*a**i*<=+<=1).
Print in the first line the sought maximum number of instructions that can be removed from the code. In the second line print the numbers of the instructions. The instructions are numbered from 1 to *m* in the order they appeared in the input. If there are multiple solutions, print any of them.
[ "10 4\n3 3\n3 1\n4 1\n9 2\n", "1 1\n1 1\n" ]
[ "2\n2 3 ", "0\n" ]
none
[]
122
0
0
124,293
643
Bearish Fanpages
[]
null
null
There is a social website with *n* fanpages, numbered 1 through *n*. There are also *n* companies, and the *i*-th company owns the *i*-th fanpage. Recently, the website created a feature called following. Each fanpage must choose exactly one other fanpage to follow. The website doesn’t allow a situation where *i* fol...
The first line of the input contains two integers *n* and *q* (3<=≀<=*n*<=≀<=100<=000, 1<=≀<=*q*<=≀<=100<=000)Β β€” the number of fanpages and the number of queries, respectively. The second line contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (1<=≀<=*t**i*<=≀<=1012) where *t**i* denotes the number of people subscribing...
For each query of the second type print one integer in a separate line - the total income of the given company. For each query of the third type print two integers in a separate line - the minimum and the maximum total income, respectively.
[ "5 12\n10 20 30 40 50\n2 3 4 5 2\n2 1\n2 2\n2 3\n2 4\n2 5\n1 4 2\n2 1\n2 2\n2 3\n2 4\n2 5\n3\n" ]
[ "10\n36\n28\n40\n36\n9\n57\n27\n28\n29\n9 57\n" ]
In the sample test, there are 5 fanpages. The *i*-th of them has *i*Β·10 subscribers. On drawings, numbers of subscribers are written in circles. An arrow from *A* to *B* means that *A* follows *B*. The left drawing shows the initial situation. The first company gets income <img align="middle" class="tex-formula" src=...
[]
46
0
0
124,334
0
none
[ "none" ]
null
null
You are given an undirected graph that consists of *n* vertices and *m* edges. Initially, each edge is colored either red or blue. Each turn a player picks a single vertex and switches the color of all edges incident to it. That is, all red edges with an endpoint in this vertex change the color to blue, while all blue ...
The first line of the input contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=100<=000)Β β€” the number of vertices and edges, respectively. The following *m* lines provide the description of the edges, as the *i*-th of them contains two integers *u**i* and *v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*, *u**i*<=β‰ <=*v**i*)Β β€”...
If there is no way to make the colors of all edges equal output <=-<=1 in the only line of the output. Otherwise first output *k*Β β€” the minimum number of moves required to achieve the goal, then output *k* integers *a*1,<=*a*2,<=...,<=*a**k*, where *a**i* is equal to the index of the vertex that should be used at the *...
[ "3 3\n1 2 B\n3 1 R\n3 2 B\n", "6 5\n1 3 R\n2 3 R\n3 4 B\n4 5 R\n4 6 R\n", "4 5\n1 2 R\n1 3 R\n2 3 B\n3 4 B\n1 4 B\n" ]
[ "1\n2 \n", "2\n3 4 \n", "-1\n" ]
none
[]
46
0
0
124,367
0
none
[ "none" ]
null
null
Π’Π΄ΠΎΠ»ΡŒ Π΄ΠΎΡ€ΠΎΠ³ΠΈ стоят *n* ΠΏΡƒΡ‚Π΅ΡˆΠ΅ΡΡ‚Π²Π΅Π½Π½ΠΈΠΊΠΎΠ². Π”ΠΎΡ€ΠΎΠ³Π° прСдставляСт собой ΠΏΡ€ΡΠΌΡƒΡŽ, Ρ€Π°Π·ΠΌΠ΅Ρ€Π°ΠΌΠΈ ΠΏΡƒΡ‚Π΅ΡˆΠ΅ΡΡ‚Π²Π΅Π½Π½ΠΈΠΊΠΎΠ² ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡ€Π΅Π½Π΅Π±Ρ€Π΅Ρ‡ΡŒ, считая ΠΈΡ… Ρ‚ΠΎΡ‡ΠΊΠ°ΠΌΠΈ. Π’ΠΎΠ΄ΠΈΡ‚Π΅Π»ΡŒ автобуса Василий, благодаря ΠΌΠΎΠ±ΠΈΠ»ΡŒΠ½ΠΎΠΌΡƒ ΠΏΡ€ΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΡŽ, Π·Π½Π°Π΅Ρ‚ для ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΏΡƒΡ‚Π΅ΡˆΠ΅ΡΡ‚Π²Π΅Π½Π½ΠΈΠΊΠ° Ρ‚ΠΎΡ‡ΠΊΡƒ *x**i*, Π² ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ Ρ‚ΠΎΡ‚ стоит. ΠšΡ€ΠΎΠΌΠ΅ Ρ‚ΠΎΠ³ΠΎ, ΠΎΠ½ Π·Π½Π°Π΅Ρ‚ расстояниС *d**i*, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ *i*...
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΉ строкС Π²Ρ…ΠΎΠ΄Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ… слСдуСт Π΄Π²Π° Ρ†Π΅Π»Ρ‹Ρ… ΠΏΠΎΠ»ΠΎΠΆΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Ρ… числа *n* ΠΈ *a* (1<=≀<=*a*<=≀<=*n*<=≀<=200<=000)Β β€” количСство ΠΏΡƒΡ‚Π΅ΡˆΠ΅ΡΡ‚Π²Π΅Π½Π½ΠΈΠΊΠΎΠ², стоящих вдоль Π΄ΠΎΡ€ΠΎΠ³ΠΈ, ΠΈ минимальноС количСство ΠΏΡƒΡ‚Π΅ΡˆΠ΅ΡΡ‚Π²Π΅Π½Π½ΠΈΠΊΠΎΠ², ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… Василий Ρ…ΠΎΡ‡Π΅Ρ‚ ΠΏΠΎΠ΄Π²Π΅Π·Ρ‚ΠΈ. Π’ ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΈΠ· ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΡ… *n* строк содСрТится ΠΏΠΎ Π΄Π²Π° Ρ†Π΅Π»Ρ‹Ρ… числа *x**i* ΠΈ *d**i* (...
Π‘Π½Π°Ρ‡Π°Π»Π° Π²Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΎΠ΄Π½ΠΎ Ρ†Π΅Π»ΠΎΠ΅ число — минимальноС количСство пассаТирских мСст Π² автобусС, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… Π±ΡƒΠ΄Π΅Ρ‚ достаточно для ΠΏΠ΅Ρ€Π΅Π²ΠΎΠ·ΠΊΠΈ хотя Π±Ρ‹ *a* ΠΏΡƒΡ‚Π΅ΡˆΠ΅ΡΡ‚Π²Π΅Π½Π½ΠΈΠΊΠΎΠ². Π—Π°Ρ‚Π΅ΠΌ Π²Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ *a* Ρ†Π΅Π»Ρ‹Ρ… чисСл — Π½ΠΎΠΌΠ΅Ρ€Π° ΠΏΡƒΡ‚Π΅ΡˆΠ΅ΡΡ‚Π²Π΅Π½Π½ΠΈΠΊΠΎΠ², ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… ΠΏΠΎΠ΄Π²Π΅Π·Ρ‘Ρ‚ Василий. ΠŸΡƒΡ‚Π΅ΡˆΠ΅ΡΡ‚Π²Π΅Π½Π½ΠΈΠΊΠΈ Π½ΡƒΠΌΠ΅Ρ€ΡƒΡŽΡ‚ΡΡ, начиная с Π΅Π΄ΠΈΠ½ΠΈΡ†Ρ‹, Π² Ρ‚ΠΎΠΌ порядкС, Π² ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌ Π·Π°Π΄Π°...
[ "3 2\n8 9\n3 5\n1 3\n", "5 4\n20 40\n10 10\n15 5\n5 15\n20 30\n" ]
[ "1\n1 3 \n", "2\n4 2 5 1\n" ]
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΌ тСстовом ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π΅ достаточно одномСстного автобуса. К ΠΏΡ€ΠΈΠΌΠ΅Ρ€Ρƒ, Василий ΠΌΠΎΠΆΠ΅Ρ‚ ΠΏΠΎΠ΄Π²Π΅Π·Ρ‚ΠΈ Ρ‚Ρ€Π΅Ρ‚ΡŒΠ΅Π³ΠΎ ΠΈ ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ ΠΏΡƒΡ‚Π΅ΡˆΠ΅ΡΡ‚Π²Π΅Π½Π½ΠΈΠΊΠΎΠ², Π»ΠΈΠ±ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠ³ΠΎ ΠΈ ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ ΠΏΡƒΡ‚Π΅ΡˆΠ΅ΡΡ‚Π²Π΅Π½Π½ΠΈΠΊΠΎΠ².
[]
0
0
-1
124,606
0
none
[ "none" ]
null
null
When the river brought Gerda to the house of the Old Lady who Knew Magic, this lady decided to make Gerda her daughter. She wants Gerda to forget about Kay, so she puts all the roses from the garden underground. Mole, who lives in this garden, now can watch the roses without going up to the surface. Typical mole is bl...
The first line of the input contains an integer *t* *t* (1<=≀<=*t*<=≀<=100<=000)Β β€” the number of test cases. Then follow exactly *t* blocks, each containing the description of exactly one test. The first line of each block contains an integer *n**i* (1<=≀<=*n**i*<=≀<=100<=000)Β β€” the number of roses in the test. Then f...
For each of *t* test cases print three integersΒ β€” the coordinates of the optimal point to watch roses. If there are many optimal answers, print any of them. The coordinates of the optimal point may coincide with the coordinates of any rose.
[ "1\n5\n0 0 4\n0 0 -4\n0 4 0\n4 0 0\n1 1 1\n", "2\n1\n3 5 9\n2\n3 5 9\n3 5 9\n" ]
[ "0 0 0\n", "3 5 9\n3 5 9\n" ]
In the first sample, the maximum Manhattan distance from the point to the rose is equal to 4. In the second sample, the maximum possible distance is 0. Note that the positions of the roses may coincide with each other and with the position of the optimal point.
[]
30
0
0
124,889
513
Subarray Cuts
[ "dp" ]
null
null
You are given an array of length *n* and a number *k*. Let's pick *k* non-overlapping non-empty subarrays of the initial array. Let *s**i* be the sum of the *i*-th subarray in order from left to right. Compute the maximum value of the following expression: Here subarray is a contiguous part of an array.
The first line of input contains two integers *n* and *k*. The second line contains *n* integers β€” the elements of the array. The absolute values of elements do not exceed 104. The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct subm...
Output a single integer β€” the maximum possible value.
[ "5 3\n5 2 4 3 1\n", "4 2\n7 4 3 7\n" ]
[ "12\n", "8\n" ]
Consider the first sample test. The optimal solution is obtained if the first subarray contains the first element only, the second subarray spans the next three elements and the last subarray contains the last element only. The sums of these subarrays are 5, 9 and 1, correspondingly. Consider the second sample test. I...
[ { "input": "5 3\n5 2 4 3 1", "output": "12" }, { "input": "4 2\n7 4 3 7", "output": "8" }, { "input": "9 6\n9 4 10 1 6 3 3 8 4", "output": "55" }, { "input": "8 6\n2 5 3 4 2 10 9 3", "output": "45" }, { "input": "9 2\n7 10 7 2 5 7 10 1 3", "output": "47" }, ...
93
307,200
-1
125,136
509
Sums of Digits
[ "dp", "greedy", "implementation" ]
null
null
Vasya had a strictly increasing sequence of positive integers *a*1, ..., *a**n*. Vasya used it to build a new sequence *b*1, ..., *b**n*, where *b**i* is the sum of digits of *a**i*'s decimal representation. Then sequence *a**i* got lost and all that remained is sequence *b**i*. Vasya wonders what the numbers *a**i* c...
The first line contains a single integer number *n* (1<=≀<=*n*<=≀<=300). Next *n* lines contain integer numbers *b*1, ..., *b**n* Β β€” the required sums of digits. All *b**i* belong to the range 1<=≀<=*b**i*<=≀<=300.
Print *n* integer numbers, one per lineΒ β€” the correct option for numbers *a**i*, in order of following in sequence. The sequence should be strictly increasing. The sum of digits of the *i*-th number should be equal to *b**i*. If there are multiple sequences with least possible number *a**n*, print any of them. Print ...
[ "3\n1\n2\n3\n", "3\n3\n2\n1\n" ]
[ "1\n2\n3\n", "3\n11\n100\n" ]
none
[ { "input": "3\n1\n2\n3", "output": "1\n2\n3" }, { "input": "3\n3\n2\n1", "output": "3\n11\n100" }, { "input": "10\n1\n2\n3\n4\n5\n6\n7\n8\n9\n1", "output": "1\n2\n3\n4\n5\n6\n7\n8\n9\n10" }, { "input": "10\n8\n8\n5\n1\n2\n7\n3\n8\n9\n4", "output": "8\n17\n23\n100\n101\n10...
93
0
0
125,149
187
Weak Memory
[ "dfs and similar", "dsu" ]
null
null
Zart PMP is qualified for ICPC World Finals in Harbin, China. After team excursion to Sun Island Park for snow sculpture art exposition, PMP should get back to buses before they leave. But the park is really big and he does not know how to find them. The park has *n* intersections numbered 1 through *n*. There are *m*...
The first line contains three space-separated integers *n*,<=*m*,<=*k* (2<=≀<=*n*<=≀<=105,<=0<=≀<=*m*<=≀<=2Β·105,<=1<=≀<=*k*<=≀<=*n*) β€” the number of intersections, roads and volunteers, respectively. Next line contains *k* distinct space-separated integers between 1 and *n* inclusive β€” the numbers of cities where volun...
Print on the only line the answer to the problem β€” the minimum value of *q* which guarantees that PMP can find the buses. If PMP cannot reach the buses at all, output -1 instead.
[ "6 6 3\n1 3 6\n1 2\n2 3\n4 2\n5 6\n4 5\n3 4\n1 6\n", "6 5 3\n1 5 6\n1 2\n2 3\n3 4\n4 5\n6 3\n1 5\n" ]
[ "3\n", "3\n" ]
The first sample is illustrated below. Blue intersections are where volunteers are located. If PMP goes in the path of dashed line, it can reach the buses with *q* = 3: In the second sample, PMP uses intersection 6 as an intermediate intersection, thus the answer is 3.
[]
62
0
0
125,964
254
Rats
[ "brute force", "dfs and similar", "graphs", "implementation", "shortest paths" ]
null
null
Rats have bred to hundreds and hundreds in the basement of the store, owned by Vasily Petrovich. Vasily Petrovich may have not noticed their presence, but they got into the habit of sneaking into the warehouse and stealing food from there. Vasily Petrovich cannot put up with it anymore, he has to destroy the rats in th...
The first line contains three integers *n*, *m* and *d*, separated by single spaces (4<=≀<=*n*,<=*m*<=≀<=1000,<=1<=≀<=*d*<=≀<=8). Next *n* lines contain the table that represents the basement plan. Each row of the table consists of *m* characters. Character "X" means that the corresponding cell is occupied by the wall,...
If it is impossible to blow up all cells with sleeping rats, print a single integer -1. Otherwise, print four space-separated integers *r*1,<=*c*1,<=*r*2,<=*c*2, that mean that one grenade should go off in cell (*r*1,<=*c*1), and the other one β€” in cell (*r*2,<=*c*2). Consider the table rows numbered from top to bott...
[ "4 4 1\nXXXX\nXR.X\nX.RX\nXXXX\n", "9 14 5\nXXXXXXXXXXXXXX\nX....R...R...X\nX..R.........X\nX....RXR..R..X\nX..R...X.....X\nXR.R...X.....X\nX....XXR.....X\nX....R..R.R..X\nXXXXXXXXXXXXXX\n", "7 7 1\nXXXXXXX\nX.R.R.X\nX.....X\nX..X..X\nX..R..X\nX....RX\nXXXXXXX\n" ]
[ "2 2 2 3\n", "2 3 6 9\n", "-1\n" ]
none
[ { "input": "4 4 1\nXXXX\nXR.X\nX.RX\nXXXX", "output": "2 2 2 3" }, { "input": "9 14 5\nXXXXXXXXXXXXXX\nX....R...R...X\nX..R.........X\nX....RXR..R..X\nX..R...X.....X\nXR.R...X.....X\nX....XXR.....X\nX....R..R.R..X\nXXXXXXXXXXXXXX", "output": "2 3 6 9" }, { "input": "7 7 1\nXXXXXXX\nX.R.R...
46
0
0
126,017
750
New Year and Binary Tree Paths
[ "bitmasks", "brute force", "combinatorics", "dp" ]
null
null
The New Year tree is an infinite perfect binary tree rooted in the node 1. Each node *v* has two children: nodes indexed (2Β·*v*) and (2Β·*v*<=+<=1). Polar bears love decorating the New Year tree and Limak is no exception. As he is only a little bear, he was told to decorate only one simple path between some pair of nod...
The only line of the input contains a single integer *s* (1<=≀<=*s*<=≀<=1015).
Print one integer, denoting the number of unordered pairs of nodes indices defining simple paths with the sum of indices of vertices equal to *s*.
[ "10\n" ]
[ "4\n" ]
In sample test, there are 4 paths with the sum of indices equal to 10:
[]
46
0
0
126,222
802
Fake News (hard)
[ "string suffix structures" ]
null
null
Now that you have proposed a fake post for the HC2 Facebook page, Heidi wants to measure the quality of the post before actually posting it. She recently came across a (possibly fake) article about the impact of fractal structure on multimedia messages and she is now trying to measure the self-similarity of the message...
The input starts with a line indicating the number of test cases *T* (1<=≀<=*T*<=≀<=10). After that, *T* test cases follow, each of which consists of one line containing a string *s* (1<=≀<=|*s*|<=≀<=100<=000) composed of lowercase letters (a-z).
Output *T* lines, every line containing one number – the answer to the corresponding test case.
[ "4\naa\nabcd\nccc\nabcc\n" ]
[ "5\n10\n14\n12\n" ]
A string *s* contains another string *p* as a substring if *p* is a contiguous subsequence of *s*. For example, ab is a substring of cab but not of acb.
[]
30
0
0
126,508
216
Spider's Web
[ "binary search", "sortings", "two pointers" ]
null
null
Paw the Spider is making a web. Web-making is a real art, Paw has been learning to do it his whole life. Let's consider the structure of the web. There are *n* main threads going from the center of the web. All main threads are located in one plane and divide it into *n* equal infinite sectors. The sectors are indexed...
The first line contains integer *n* (3<=≀<=*n*<=≀<=1000) β€” the number of main threads. The *i*-th of following *n* lines describe the bridges located in the *i*-th sector: first it contains integer *k**i* (1<=≀<=*k**i*<=≀<=105) equal to the number of bridges in the given sector. Then follow *k**i* different integers *...
Print a single integer β€” the number of unstable cells in Paw the Spider's web.
[ "7\n3 1 6 7\n4 3 5 2 9\n2 8 1\n4 3 7 6 4\n3 2 5 9\n3 6 3 8\n3 4 2 9\n" ]
[ "6" ]
none
[ { "input": "7\n3 1 6 7\n4 3 5 2 9\n2 8 1\n4 3 7 6 4\n3 2 5 9\n3 6 3 8\n3 4 2 9", "output": "6" }, { "input": "3\n1 1\n1 2\n1 3", "output": "0" }, { "input": "3\n2 1 2\n2 3 4\n2 5 6", "output": "0" }, { "input": "5\n3 2 4 10\n2 1 6\n2 8 7\n3 2 4 10\n2 1 6", "output": "2" ...
560
30,412,800
3
126,514
0
none
[ "none" ]
null
null
You are given an infinite periodic array *a*0,<=*a*1,<=...,<=*a**n*<=-<=1,<=... with the period of length *n*. Formally, . A periodic subarray (*l*,<=*s*) (0<=≀<=*l*<=&lt;<=*n*, 1<=≀<=*s*<=&lt;<=*n*) of array *a* is an infinite periodic array with a period of length *s* that is a subsegment of array *a*, starting with ...
The first line contains number *n* (1<=≀<=*n*<=≀<=2Β·105). The second line contains *n* numbers *a*0,<=*a*1,<=...,<=*a**n*<=-<=1 (1<=≀<=*a**i*<=≀<=106), separated by a space.
Print a single integer β€” the sought number of pairs.
[ "4\n7 1 2 3\n", "2\n2 1\n", "3\n1 1 1\n" ]
[ "2\n", "1\n", "6\n" ]
In the first sample the superior subarrays are (0, 1) and (3, 2). Subarray (0, 1) is superior, as *a*<sub class="lower-index">0</sub> β‰₯ *a*<sub class="lower-index">0</sub>, *a*<sub class="lower-index">0</sub> β‰₯ *a*<sub class="lower-index">1</sub>, *a*<sub class="lower-index">0</sub> β‰₯ *a*<sub class="lower-index">2</su...
[]
124
3,481,600
0
126,570
869
The Untended Antiquity
[ "data structures", "hashing" ]
null
null
Adieu l'ami. Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around the abandoned Eikou Cram School building, Oshino's makeshift residence. The space is represented by a rectangular grid of *n*<=Γ—<=*m* cells, arranged into *n* rows and *m* columns. The *c*-th cell in the *r*-th row is ...
The first line of input contains three space-separated integers *n*, *m* and *q* (1<=≀<=*n*,<=*m*<=≀<=2<=500, 1<=≀<=*q*<=≀<=100<=000) β€” the number of rows and columns in the grid, and the total number of Oshino and Koyomi's actions, respectively. The following *q* lines each describes an action, containing five space-...
For each of Koyomi's attempts (actions with *t*<==<=3), output one line β€” containing "Yes" (without quotes) if it's feasible, and "No" (without quotes) otherwise.
[ "5 6 5\n1 2 2 4 5\n1 3 3 3 3\n3 4 4 1 1\n2 2 2 4 5\n3 1 1 4 4\n", "2500 2500 8\n1 549 1279 1263 2189\n1 303 795 1888 2432\n1 2227 622 2418 1161\n3 771 2492 1335 1433\n1 2017 2100 2408 2160\n3 48 60 798 729\n1 347 708 1868 792\n3 1940 2080 377 1546\n" ]
[ "No\nYes\n", "No\nYes\nNo\n" ]
For the first example, the situations of Koyomi's actions are illustrated below.
[ { "input": "5 6 5\n1 2 2 4 5\n1 3 3 3 3\n3 4 4 1 1\n2 2 2 4 5\n3 1 1 4 4", "output": "No\nYes" }, { "input": "2500 2500 8\n1 549 1279 1263 2189\n1 303 795 1888 2432\n1 2227 622 2418 1161\n3 771 2492 1335 1433\n1 2017 2100 2408 2160\n3 48 60 798 729\n1 347 708 1868 792\n3 1940 2080 377 1546", "ou...
1,122
248,012,800
-1
126,644
28
Don't fear, DravDe is kind
[ "binary search", "data structures", "dp", "hashing" ]
D. Don't fear, DravDe is kind
2
256
A motorcade of *n* trucks, driving from city Β«ZΒ» to city Β«Π—Β», has approached a tunnel, known as Tunnel of Horror. Among truck drivers there were rumours about monster DravDe, who hunts for drivers in that tunnel. Some drivers fear to go first, others - to be the last, but let's consider the general case. Each truck is ...
The first input line contains integer number *n* (1<=≀<=*n*<=≀<=105) β€” amount of trucks in the motorcade. The following *n* lines contain four integers each. Numbers in the *i*-th line: *v**i*,<=*c**i*,<=*l**i*,<=*r**i* (1<=≀<=*v**i*<=≀<=104,<=1<=≀<=*c**i*<=≀<=105,<=0<=≀<=*l**i*,<=*r**i*<=≀<=105) β€” describe the *i*-th ...
In the first line output number *k* β€” amount of trucks that will drive into the tunnel. In the second line output *k* numbers β€” indexes of these trucks in ascending order. Don't forget please that you are not allowed to change the order of trucks. If the answer is not unique, output any.
[ "5\n1 1 0 3\n1 1 1 2\n1 1 2 1\n1 1 3 0\n2 1 3 0\n", "5\n1 1 0 3\n10 1 2 1\n2 2 1 1\n10 1 1 2\n3 1 3 0\n" ]
[ "4\n1 2 3 5 \n", "3\n1 3 5 \n" ]
none
[ { "input": "5\n1 1 0 3\n1 1 1 2\n1 1 2 1\n1 1 3 0\n2 1 3 0", "output": "4\n1 2 3 5 " }, { "input": "5\n1 1 0 3\n10 1 2 1\n2 2 1 1\n10 1 1 2\n3 1 3 0", "output": "3\n1 3 5 " }, { "input": "10\n3007 1 0 12\n5126 1 1 11\n2245 2 2 9\n8547 1 4 8\n8531 1 5 7\n5017 1 6 6\n6222 1 7 5\n6748 1 8 4...
92
0
0
128,150
486
Palindrome Transformation
[ "brute force", "greedy", "implementation" ]
null
null
Nam is playing with a string on his computer. The string consists of *n* lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down. There is a cursor pointing at some symbol of the string. Suppose that...
The first line contains two space-separated integers *n* (1<=≀<=*n*<=≀<=105) and *p* (1<=≀<=*p*<=≀<=*n*), the length of Nam's string and the initial position of the text cursor. The next line contains *n* lowercase characters of Nam's string.
Print the minimum number of presses needed to change string into a palindrome.
[ "8 3\naeabcaez\n" ]
[ "6\n" ]
A string is a palindrome if it reads the same forward or reversed. In the sample test, initial Nam's string is: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7cd1b7b2498296406fff738215b48ddd9a12898d.png" style="max-width: 100.0%;max-height: 100.0%;"/> (cursor position is shown bold). In...
[ { "input": "8 3\naeabcaez", "output": "6" }, { "input": "8 3\nabcddcbb", "output": "3" }, { "input": "4 4\nrkoa", "output": "14" }, { "input": "39 30\nyehuqwaffoiyxhkmdipxroolhahbhzprioobxfy", "output": "138" }, { "input": "40 23\nvwjzsgpdsopsrpsyccavfkyyahdgkmdxr...
46
0
0
128,287
723
st-Spanning Tree
[ "dsu", "graphs", "greedy", "implementation" ]
null
null
You are given an undirected connected graph consisting of *n* vertices and *m* edges. There are no loops and no multiple edges in the graph. You are also given two distinct vertices *s* and *t*, and two values *d**s* and *d**t*. Your task is to build any spanning tree of the given graph (note that the graph is not wei...
The first line of the input contains two integers *n* and *m* (2<=≀<=*n*<=≀<=200<=000, 1<=≀<=*m*<=≀<=*min*(400<=000,<=*n*Β·(*n*<=-<=1)<=/<=2))Β β€” the number of vertices and the number of edges in the graph. The next *m* lines contain the descriptions of the graph's edges. Each of the lines contains two integers *u* and...
If the answer doesn't exist print "No" (without quotes) in the only line of the output. Otherwise, in the first line print "Yes" (without quotes). In the each of the next (*n*<=-<=1) lines print two integers β€” the description of the edges of the spanning tree. Each of the edges of the spanning tree must be printed ex...
[ "3 3\n1 2\n2 3\n3 1\n1 2 1 1\n", "7 8\n7 4\n1 3\n5 4\n5 7\n3 2\n2 4\n6 1\n1 2\n6 4 1 4\n" ]
[ "Yes\n3 2\n1 3\n", "Yes\n1 3\n5 7\n3 2\n7 4\n2 4\n6 1\n" ]
none
[ { "input": "3 3\n1 2\n2 3\n3 1\n1 2 1 1", "output": "Yes\n3 2\n1 3" }, { "input": "7 8\n7 4\n1 3\n5 4\n5 7\n3 2\n2 4\n6 1\n1 2\n6 4 1 4", "output": "Yes\n1 3\n5 7\n3 2\n7 4\n2 4\n6 1" }, { "input": "10 15\n4 1\n5 10\n2 1\n5 7\n9 2\n4 6\n6 7\n9 1\n6 9\n8 4\n8 3\n9 8\n3 9\n2 3\n7 10\n10 1 ...
935
90,316,800
3
128,631
993
The Moral Dilemma
[]
null
null
Hibiki and Dita are in love with each other, but belong to communities that are in a long lasting conflict. Hibiki is deeply concerned with the state of affairs, and wants to figure out if his relationship with Dita is an act of love or an act of treason. Hibiki prepared several binary features his decision will depen...
The first line contains three integers $n$, $m$, $k$ ($2 \le n, m \le 50$; $1 \le k \le 50$) β€” the number of input features, the number of gates in the first layer, and the number of gates in the second layer correspondingly. The second line contains $m$ pairs of strings separated by spaces describing the first layer....
Print the number of gates that need to be removed from the second layer so that the output of the remaining circuit doesn't depend on whether Hibiki is in love or not. If no matter how many gates are removed the output of the circuit continues to depend on Hibiki's feelings, print $-1$.
[ "2 2 2\nand xx nand xx\nand xx or xx\n", "3 2 2\nand xx. nor .xx\nand xx nor xx\n", "4 4 5\nnor x..x and ..xx and xx.. nand xx..\nnand ..xx nor ..xx and xx.. nor ..xx or ..xx\n" ]
[ "1\n", "-1\n", "2\n" ]
In the first example the two gates in the first layer are connected to the same inputs, but first computes "and" while second computes "nand", and as such their output is always different no matter what the input is and whether Hibiki is in love or not. The second layer has "or" and "and" gates both connected to the tw...
[]
61
0
-1
128,652
540
Infinite Inversions
[ "binary search", "data structures", "implementation", "sortings", "trees" ]
null
null
There is an infinite sequence consisting of all positive integers in the increasing order: *p*<==<={1,<=2,<=3,<=...}. We performed *n* swap operations with this sequence. A *swap*(*a*,<=*b*) is an operation of swapping the elements of the sequence on positions *a* and *b*. Your task is to find the number of inversions ...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=105)Β β€” the number of swap operations applied to the sequence. Each of the next *n* lines contains two integers *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=109, *a**i*<=β‰ <=*b**i*)Β β€” the arguments of the swap operation.
Print a single integer β€” the number of inversions in the resulting sequence.
[ "2\n4 2\n1 4\n", "3\n1 6\n3 4\n2 5\n" ]
[ "4\n", "15\n" ]
In the first sample the sequence is being modified as follows: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/014a32f190673020641bf266b205f41f0a704f2d.png" style="max-width: 100.0%;max-height: 100.0%;"/>. It has 4 inversions formed by index pairs (1, 4), (2, 3), (2, 4) and (3, 4).
[ { "input": "2\n4 2\n1 4", "output": "4" }, { "input": "3\n1 6\n3 4\n2 5", "output": "15" }, { "input": "1\n1000000000 1", "output": "1999999997" }, { "input": "5\n2 5\n6 3\n4 6\n5 4\n2 5", "output": "5" }, { "input": "4\n2 5\n4 3\n1 4\n6 2", "output": "8" },...
46
0
-1
128,978
98
Help Shrek and Donkey
[ "dp", "games", "math", "probabilities" ]
E. Help Shrek and Donkey
2
256
Shrek and the Donkey (as you can guess, they also live in the far away kingdom) decided to play a card game called YAGame. The rules are very simple: initially Shrek holds *m* cards and the Donkey holds *n* cards (the players do not see each other's cards), and one more card lies on the table face down so that both pla...
The first line contains space-separated integers *m* and *n* (0<=≀<=*m*,<=*n*<=≀<=1000).
Print space-separated probabilities that Shrek wins and Donkey wins correspondingly; the absolute error should not exceed 10<=-<=9.
[ "0 3\n", "1 0\n", "1 1\n" ]
[ "0.25 0.75\n", "1 0\n", "0.5 0.5\n" ]
none
[ { "input": "0 3", "output": "0.2500000000000000 0.7500000000000000" }, { "input": "1 0", "output": "1.0000000000000000 0.0000000000000000" }, { "input": "1 1", "output": "0.5000000000000000 0.5000000000000000" }, { "input": "0 0", "output": "1.0000000000000000 0.000000000...
218
17,920,000
3.912121
129,468
380
Sereja and Cinema
[ "combinatorics", "math" ]
null
null
The cinema theater hall in Sereja's city is *n* seats lined up in front of one large screen. There are slots for personal possessions to the left and to the right of each seat. Any two adjacent seats have exactly one shared slot. The figure below shows the arrangement of seats and slots for *n*<==<=4. Today it's the p...
The first line contains integer *n* (1<=≀<=*n*<=≀<=105). The second line contains *n* integers, the *i*-th integer shows either the index of the person (index in the entering queue) with the ticket for the *i*-th seat or a 0, if his index is not known. It is guaranteed that all positive numbers in the second line are d...
In a single line print the remainder after dividing the answer by number 1000000007 (109<=+<=7).
[ "11\n0 0 0 0 0 0 0 0 0 0 0\n", "6\n0 3 1 0 0 0\n" ]
[ "1024\n", "3\n" ]
none
[]
46
0
0
129,523
433
Tachibana Kanade's Tofu
[ "dp" ]
null
null
Tachibana Kanade likes Mapo Tofu very much. One day, the canteen cooked all kinds of tofu to sell, but not all tofu is Mapo Tofu, only those spicy enough can be called Mapo Tofu. Each piece of tofu in the canteen is given a *m*-based number, all numbers are in the range [*l*,<=*r*] (*l* and *r* being *m*-based numbers...
The first line contains three integers *n*, *m* and *k*Β (1<=≀<=*n*<=≀<=200;Β 2<=≀<=*m*<=≀<=20;Β 1<=≀<=*k*<=≀<=500). Where *n* denotes the number of strings, *m* denotes the base used, and *k* denotes the limit of the value for Mapo Tofu. The second line represents the number *l*. The first integer in the line is *len* (...
Output the number of pieces of Mapo Tofu modulo 1000000007 (109<=+<=7). The answer should be a decimal integer.
[ "2 10 1\n1 1\n3 1 0 0\n1 1 1\n1 0 1\n", "2 10 12\n2 5 9\n6 6 3 5 4 9 7\n2 0 6 1\n3 6 7 2 1\n", "4 2 6\n6 1 0 1 1 1 0\n6 1 1 0 1 0 0\n1 1 2\n3 0 1 0 5\n4 0 1 1 0 4\n3 1 0 1 2\n" ]
[ "97\n", "635439\n", "2\n" ]
In the first sample, 10, 11 and 100 are the only three decimal numbers in [1, 100] with a value greater than 1. Here the value of 1 is 1 but not 2, since numbers cannot contain leading zeros and thus cannot be written as "01". In the second sample, no numbers in the given interval have a value greater than 12. In the...
[]
46
0
0
129,790
63
Sweets Game
[ "bitmasks", "dfs and similar", "dp", "games", "implementation" ]
E. Sweets Game
3
256
Karlsson has visited Lillebror again. They found a box of chocolates and a big whipped cream cake at Lillebror's place. Karlsson immediately suggested to divide the sweets fairly between Lillebror and himself. Specifically, to play together a game he has just invented with the chocolates. The winner will get the cake a...
The input data contains 5 lines, containing 19 words consisting of one symbol. The word "O" means that the cell contains a chocolate and a "." stands for an empty cell. It is guaranteed that the box contains at least one chocolate. See the examples for better understanding.
If Karlsson gets the cake, print "Karlsson" (without the quotes), otherwise print "Lillebror" (yet again without the quotes).
[ ". . .\n . . O .\n. . O O .\n . . . .\n . . .\n", ". . .\n . . . O\n. . . O .\n O . O .\n . O .\n" ]
[ "Lillebror", "Karlsson" ]
none
[ { "input": " . . .\n . . O .\n. . O O .\n . . . .\n . . .", "output": "Lillebror" }, { "input": " . . .\n . . . O\n. . . O .\n O . O .\n . O .", "output": "Karlsson" }, { "input": " . . .\n . . . .\n. . . . .\n . . O .\n . . .", "output": "Karlsson" }, { "input": " . ....
0
0
-1
129,867
374
Inna and Dima
[ "dfs and similar", "dp", "graphs", "implementation" ]
null
null
Inna and Dima bought a table of size *n*<=Γ—<=*m* in the shop. Each cell of the table contains a single letter: "D", "I", "M", "A". Inna loves Dima, so she wants to go through his name as many times as possible as she moves through the table. For that, Inna acts as follows: 1. initially, Inna chooses some cell of the...
The first line of the input contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=103). Then follow *n* lines that describe Inna and Dima's table. Each line contains *m* characters. Each character is one of the following four characters: "D", "I", "M", "A". Note that it is not guaranteed that the table contains at...
If Inna cannot go through name DIMA once, print on a single line "Poor Dima!" without the quotes. If there is the infinite number of names DIMA Inna can go through, print "Poor Inna!" without the quotes. Otherwise print a single integer β€” the maximum number of times Inna can go through name DIMA.
[ "1 2\nDI\n", "2 2\nMA\nID\n", "5 5\nDIMAD\nDIMAI\nDIMAM\nDDMAA\nAAMID\n" ]
[ "Poor Dima!\n", "Poor Inna!\n", "4\n" ]
Notes to the samples: In the first test sample, Inna cannot go through name DIMA a single time. In the second test sample, Inna can go through the infinite number of words DIMA. For that, she should move in the clockwise direction starting from the lower right corner. In the third test sample the best strategy is to...
[ { "input": "1 2\nDI", "output": "Poor Dima!" }, { "input": "2 2\nMA\nID", "output": "Poor Inna!" }, { "input": "5 5\nDIMAD\nDIMAI\nDIMAM\nDDMAA\nAAMID", "output": "4" }, { "input": "1 1\nI", "output": "Poor Dima!" }, { "input": "5 5\nDIMAD\nADDDI\nMDDDM\nIDDDA\nDA...
1,000
66,764,800
0
129,924
704
Black Widow
[ "dp", "graphs", "implementation", "math" ]
null
null
Natalia Romanova is trying to test something on the new gun S.H.I.E.L.D gave her. In order to determine the result of the test, she needs to find the number of answers to a certain equation. The equation is of form: Where represents logical OR and represents logical exclusive OR (XOR), and *v**i*,<=*j* are some bo...
The first line of input contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=100<=000)Β β€” the number of clauses and the number of variables respectively. The next *n* lines contain the formula. The *i*-th of them starts with an integer *k**i*Β β€” the number of literals in the *i*-th clause. It is followed by *k**i* non...
Print the answer modulo 1<=000<=000<=007 (109<=+<=7) in one line.
[ "6 7\n2 4 -2\n2 6 3\n2 -7 1\n2 -5 1\n2 3 6\n2 -2 -5\n", "8 10\n1 -5\n2 4 -6\n2 -2 -6\n2 -7 9\n2 10 -1\n2 3 -1\n2 -8 9\n2 5 8\n", "2 3\n2 1 1\n2 -3 3\n" ]
[ "48\n", "544\n", "4\n" ]
The equation in the first sample is: The equation in the second sample is: The equation in the third sample is:
[]
46
0
0
130,400
774
Perfectionist Arkadiy
[ "*special", "number theory" ]
null
null
Arkadiy has lots square photos with size *a*<=Γ—<=*a*. He wants to put some of them on a rectangular wall with size *h*<=Γ—<=*w*. The photos which Arkadiy will put on the wall must form a rectangular grid and the distances between neighboring vertically and horizontally photos and also the distances between outside row...
The first line contains three integers *a*, *h* and *w* (1<=≀<=*a*,<=*h*,<=*w*<=≀<=109) β€” the size of photos and the height and the width of the wall.
Print one non-negative real number β€” the minimum value of *x* which can be obtained after putting the photos on the wall. The absolute or the relative error of the answer must not exceed 10<=-<=6. Print -1 if there is no way to put positive number of photos and satisfy the constraints.
[ "2 18 13\n", "4 4 4\n", "3 4 3\n" ]
[ "0.5\n", "0\n", "-1\n" ]
In the first example Arkadiy can put 7 rows of photos with 5 photos in each row, so the minimum value of *x* equals to 0.5. In the second example Arkadiy can put only 1 photo which will take the whole wall, so the minimum value of *x* equals to 0. In the third example there is no way to put positive number of photos ...
[ { "input": "2 18 13", "output": "0.50000000000000000000" }, { "input": "4 4 4", "output": "0.00000000000000000000" }, { "input": "3 4 3", "output": "-1" }, { "input": "9 81 23", "output": "-1" }, { "input": "11 21 21", "output": "5.00000000000000000000" }, ...
109
0
3
130,434
73
Morrowindows
[ "math", "number theory" ]
E. Morrowindows
2
256
Vasya plays The Elder Trolls III: Morrowindows. He has a huge list of items in the inventory, however, there is no limits on the size of things. Vasya does not know the total amount of items but he is sure that are not more than *x* and not less than 2 items in his inventory. A new patch for the game appeared to view i...
The first line contains two integers *n* and *x* (0<=≀<=*n*<=≀<=105,<=2<=≀<=*x*<=≀<=109). The second line contains integers *a**i* (1<=≀<=*a**i*<=≀<=109). Some numbers among all *a**i* may be equal.
Output the fewest amount of modes required to uniquely determine amount of items in the inventory. If there is no solution output <=-<=1.
[ "2 4\n2 3\n", "1 4\n2\n" ]
[ "2\n", "-1\n" ]
In the second example Vasya is not able to determine items count uniquely because 3 items, as well as 4 items, can be displayed on two pages.
[]
92
0
0
130,548
718
Andrew and Chemistry
[ "dp", "hashing", "trees" ]
null
null
During the chemistry lesson Andrew learned that the saturated hydrocarbons (alkanes) enter into radical chlorination reaction. Andrew is a very curious boy, so he wondered how many different products of the reaction may be forms for a given alkane. He managed to solve the task for small molecules, but for large ones he...
The first line of the input contains an integer *n* (1<=≀<=*n*<=≀<=100<=000)Β β€” the number of vertices in the tree. Then follow *n*<=-<=1 lines with edges descriptions. Each edge is given by two integers *u**i* and *v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*)Β β€” indices of vertices connected by an edge. It's guaranteed that t...
Print one integerΒ β€” the answer to the question.
[ "4\n1 2\n2 3\n2 4\n", "5\n1 2\n1 3\n1 4\n1 5\n", "5\n2 5\n5 3\n4 3\n4 1\n" ]
[ "2\n", "1\n", "3\n" ]
In the first sample, one can add new vertex to any existing vertex, but the trees we obtain by adding a new vertex to vertices 1, 3 and 4 are isomorphic, thus the answer is 2. In the second sample, one can't add new vertex to the first vertex, as its degree is already equal to four. Trees, obtained by adding a new ver...
[]
15
0
0
131,516
268
Playlist
[ "math", "probabilities", "sortings" ]
null
null
Manao's friends often send him new songs. He never listens to them right away. Instead, he compiles them into a playlist. When he feels that his mind is open to new music, he opens the playlist and starts to listen to the songs. Of course, there are some songs that Manao doesn't particuarly enjoy. To get more pleasure...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=50000). The *i*-th of the following *n* lines contains two integers, separated by a single space β€” *l**i* and *p**i* (15<=≀<=*l**i*<=≀<=1000, 0<=≀<=*p**i*<=≀<=100) β€” the length of the *i*-th song in seconds and the probability that Manao will like the song, in...
In a single line print a single real number β€” the maximum expected listening time over all permutations of songs. The answer will be considered valid if the absolute or relative error does not exceed 10<=-<=9.
[ "3\n150 20\n150 50\n100 50\n", "4\n300 0\n300 50\n240 50\n360 80\n" ]
[ "537.500000000\n", "2121.000000000\n" ]
Consider the first test case. If Manao listens to the songs in the order in which they were originally compiled, the mathematical expectation will be equal to 467.5 seconds. The maximum expected value is obtained by putting the first song at the end of the playlist. Consider the second test case. The song which is 360...
[]
92
0
0
131,704
0
none
[ "none" ]
null
null
Ostap already settled down in Rio de Janiero suburb and started to grow a tree in his garden. Recall that a tree is a connected undirected acyclic graph. Ostap's tree now has *n* vertices. He wants to paint some vertices of the tree black such that from any vertex *u* there is at least one black vertex *v* at distanc...
The first line of the input contains two integers *n* and *k* (1<=≀<=*n*<=≀<=100, 0<=≀<=*k*<=≀<=*min*(20,<=*n*<=-<=1))Β β€” the number of vertices in Ostap's tree and the maximum allowed distance to the nearest black vertex. Don't miss the unusual constraint for *k*. Each of the next *n*<=-<=1 lines contain two integers ...
Print one integerΒ β€” the remainder of division of the number of ways to paint the tree by 1<=000<=000<=007 (109<=+<=7).
[ "2 0\n1 2\n", "2 1\n1 2\n", "4 1\n1 2\n2 3\n3 4\n", "7 2\n1 2\n2 3\n1 4\n4 5\n1 6\n6 7\n" ]
[ "1\n", "3\n", "9\n", "91\n" ]
In the first sample, Ostap has to paint both vertices black. In the second sample, it is enough to paint only one of two vertices, thus the answer is 3: Ostap can paint only vertex 1, only vertex 2, vertices 1 and 2 both. In the third sample, the valid ways to paint vertices are: {1, 3}, {1, 4}, {2, 3}, {2, 4}, {1, 2...
[ { "input": "2 0\n1 2", "output": "1" }, { "input": "2 1\n1 2", "output": "3" }, { "input": "4 1\n1 2\n2 3\n3 4", "output": "9" }, { "input": "7 2\n1 2\n2 3\n1 4\n4 5\n1 6\n6 7", "output": "91" }, { "input": "1 0", "output": "1" }, { "input": "10 0\n5 6...
30
0
0
131,745
725
Family Photos
[ "games", "greedy" ]
null
null
Alice and Bonnie are sisters, but they don't like each other very much. So when some old family photos were found in the attic, they started to argue about who should receive which photos. In the end, they decided that they would take turns picking photos. Alice goes first. There are *n* stacks of photos. Each stack c...
The first line of input contains a single integer *n* (1<=≀<=*n*<=≀<=100<=000)Β β€” the number of two-photo stacks. Then follow *n* lines, each describing one of the stacks. A stack is described by four space-separated non-negative integers *a*1, *b*1, *a*2 and *b*2, each not exceeding 109. *a*1 and *b*1 describe the top ...
Output a single integer: the difference between Alice's and Bonnie's happiness if both play optimally.
[ "2\n12 3 4 7\n1 15 9 1\n", "2\n5 4 8 8\n4 12 14 0\n", "1\n0 10 0 10\n" ]
[ "1\n", "4\n", "-10\n" ]
none
[ { "input": "2\n12 3 4 7\n1 15 9 1", "output": "1" }, { "input": "2\n5 4 8 8\n4 12 14 0", "output": "4" }, { "input": "1\n0 10 0 10", "output": "-10" }, { "input": "10\n0 1000000000 0 1000000000\n0 1000000000 0 1000000000\n0 1000000000 0 1000000000\n0 1000000000 0 1000000000\n...
124
2,150,400
-1
131,860
370
Broken Monitor
[ "brute force", "constructive algorithms", "greedy", "implementation" ]
null
null
Innocentius has a problem β€” his computer monitor has broken. Now some of the pixels are "dead", that is, they are always black. As consequence, Innocentius can't play the usual computer games. He is recently playing the following game with his younger brother Polycarpus. Innocentius is touch-typing a program that pain...
The first line contains the resolution of the monitor as a pair of integers *n*, *m* (1<=≀<=*n*,<=*m*<=≀<=2000). The next *n* lines contain exactly *m* characters each β€” the state of the monitor pixels at the moment of the game. Character "." (period, ASCII code 46) corresponds to the black pixel, and character "w" (lo...
Print the monitor screen. Represent the sought frame by characters "+" (the "plus" character). The pixels that has become white during the game mustn't be changed. Print them as "w". If there are multiple possible ways to position the frame of the minimum size, print any of them. If the required frame doesn't exist, t...
[ "4 8\n..w..w..\n........\n........\n..w..w..\n", "5 6\n......\n.w....\n......\n..w...\n......\n", "2 4\n....\n.w..\n", "2 6\nw..w.w\n...w..\n" ]
[ "..w++w..\n..+..+..\n..+..+..\n..w++w..\n", "......\n+w+...\n+.+...\n++w...\n......\n", "....\n.w..\n", "-1\n" ]
In the first sample the required size of the optimal frame equals 4. In the second sample the size of the optimal frame equals 3. In the third sample, the size of the optimal frame is 1. In the fourth sample, the required frame doesn't exist.
[ { "input": "4 8\n..w..w..\n........\n........\n..w..w..", "output": "..w++w..\n..+..+..\n..+..+..\n..w++w.." }, { "input": "2 4\n....\n.w..", "output": "....\n.w.." }, { "input": "2 6\nw..w.w\n...w..", "output": "-1" }, { "input": "9 4\n....\n....\n....\n....\n....\n..w.\n......
1,465
35,123,200
3
132,299
615
Hexagons
[ "binary search", "implementation", "math" ]
null
null
Ayrat is looking for the perfect code. He decided to start his search from an infinite field tiled by hexagons. For convenience the coordinate system is introduced, take a look at the picture to see how the coordinates of hexagon are defined: Ayrat is searching through the field. He started at point (0,<=0) and is ...
The only line of the input contains integer *n* (0<=≀<=*n*<=≀<=1018)Β β€” the number of Ayrat's moves.
Print two integers *x* and *y*Β β€” current coordinates of Ayrat coordinates.
[ "3\n", "7\n" ]
[ "-2 0\n", "3 2\n" ]
none
[ { "input": "3", "output": "-2 0" }, { "input": "7", "output": "3 2" }, { "input": "39", "output": "5 6" }, { "input": "14", "output": "-2 -4" }, { "input": "94", "output": "8 8" }, { "input": "60", "output": "8 0" }, { "input": "60", "o...
15
0
0
133,023
156
Mrs. Hudson's Pancakes
[ "brute force", "dp" ]
null
null
Mrs. Hudson hasn't made her famous pancakes for quite a while and finally she decided to make them again. She has learned *m* new recipes recently and she can't wait to try them. Those recipes are based on *n* special spices. Mrs. Hudson has these spices in the kitchen lying in jars numbered with integers from 0 to *n*...
The first line contains the single integer *n* (1<=≀<=*n*<=≀<=104). The second line contains space-separated prices of the spices *a*0,<=*a*1,<=...,<=*a**n*<=-<=1, where *a**i* is an integer (1<=≀<=*a**i*<=≀<=1018). The third line contains the single integer *m* (1<=≀<=*m*<=≀<=3Β·104) β€” the number of recipes Mrs. Hudso...
For each recipe count by what minimum prime number the control number is divided and print this prime number on the single line. If this number turns out larger than 100, print -1.
[ "1\n1\n1\n2 ? 1\n", "4\n2 3 5 7\n4\n2 ?0 11\n2 ?1 13\n2 0? 17\n2 1? 19\n", "1\n1000000000000000000\n1\n16 ?????????????? 1\n" ]
[ "2\n", "3\n2\n23\n2\n", "-1\n" ]
In the first test any one-digit number in the binary system matches. The jar is only one and its price is equal to 1, the number *c* is also equal to 1, the control number equals 2. The minimal prime divisor of 2 is 2. In the second test there are 4 jars with numbers from 0 to 3, and the prices are equal 2, 3, 5 and 7...
[]
60
0
0
133,342
86
Genetic engineering
[ "dp", "string suffix structures", "trees" ]
C. Genetic engineering
2
256
"Multidimensional spaces are completely out of style these days, unlike genetics problems" β€” thought physicist Woll and changed his subject of study to bioinformatics. Analysing results of sequencing he faced the following problem concerning DNA sequences. We will further think of a DNA sequence as an arbitrary string ...
First line contains two integer numbers *n* and *m* (1<=≀<=*n*<=≀<=1000,<=1<=≀<=*m*<=≀<=10) β€” the length of the string and the number of sequences in the collection correspondently. Next *m* lines contain the collection sequences *s**i*, one per line. Each *s**i* is a nonempty string of length not greater than 10. Al...
Output should contain a single integer β€” the number of strings filtered by the collection modulo 1000000009 (109<=+<=9).
[ "2 1\nA\n", "6 2\nCAT\nTACT\n" ]
[ "1\n", "2\n" ]
In the first sample, a string has to be filtered by "A". Clearly, there is only one such string: "AA". In the second sample, there exist exactly two different strings satisfying the condition (see the pictures below).
[]
60
0
0
133,567
938
Shortest Path Queries
[ "bitmasks", "data structures", "dsu", "graphs" ]
null
null
You are given an undirected connected 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). There are three types of queries you ...
The first line contains two numbers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=200000) β€” the number of vertices and the number of edges in the graph, respectively. Then *m* lines follow denoting the edges of the graph. Each line contains three integers *x*, *y* and *d* (1<=≀<=*x*<=&lt;<=*y*<=≀<=*n*, 0<=≀<=*d*<=≀<=230<=-<=1). Ea...
Print the answers for all queries of type 3 in the order they appear in input.
[ "5 5\n1 2 3\n2 3 4\n3 4 5\n4 5 6\n1 5 1\n5\n3 1 5\n1 1 3 1\n3 1 5\n2 1 5\n3 1 5\n" ]
[ "1\n1\n2\n" ]
none
[ { "input": "5 5\n1 2 3\n2 3 4\n3 4 5\n4 5 6\n1 5 1\n5\n3 1 5\n1 1 3 1\n3 1 5\n2 1 5\n3 1 5", "output": "1\n1\n2" } ]
46
0
0
133,975
737
Tanya is 5!
[ "graph matchings", "graphs", "greedy", "schedules" ]
null
null
Tanya is now five so all her friends gathered together to celebrate her birthday. There are *n* children on the celebration, including Tanya. The celebration is close to its end, and the last planned attraction is gaming machines. There are *m* machines in the hall, they are numbered 1 through *m*. Each of the childre...
The first line contains three integers *n*, *m* and *b* (1<=≀<=*n*<=≀<=40, 1<=≀<=*m*<=≀<=10, 0<=≀<=*b*<=≀<=106)Β β€” the number of children, the number of gaming machines and the budget for renting additional machines. The second line contains *m* integers *p*1,<=*p*2,<=...,<=*p**m* (1<=≀<=*p**j*<=≀<=106), where *p**j* i...
In the first line print the minimum time in which all the children can finish their games. In the second line print a string of length *m* consisting of zeros and ones. The *j*-th character is '1', if the copy of *j*-th machine should be rated, and '0' otherwise. In the third line print integer *g* (0<=≀<=*g*<=≀<=106...
[ "2 2 100\n3 7\n2 1 3 2 1\n2 1 3 2 1\n", "3 2 15\n11 7\n2 2 10 1 5\n1 2 20\n2 1 4 2 3\n" ]
[ "4\n10\n8\n1 1 0 1\n2 2 0 1\n1 1 1 1\n2 1 1 1\n2 1 2 1\n1 1 2 1\n1 2 3 1\n2 1 3 1\n", "20\n01\n17\n2 2 0 4\n2 2 4 1\n1 1 5 2\n2 2 5 2\n1 2 7 5\n2 2 7 5\n2 2 12 1\n1 2 12 1\n3 1 13 4\n2 2 13 4\n1 2 13 4\n1 1 17 2\n3 2 17 2\n2 2 17 2\n1 1 19 1\n2 2 19 1\n3 2 19 1\n" ]
none
[]
30
0
0
134,087
838
Convex Countour
[ "dp" ]
null
null
You are given an strictly convex polygon with *n* vertices. It is guaranteed that no three points are collinear. You would like to take a maximum non intersecting path on the polygon vertices that visits each point at most once. More specifically your path can be represented as some sequence of distinct polygon vertic...
The first line of input will contain a single integer *n* (2<=≀<=*n*<=≀<=2<=500), the number of points. The next *n* lines will contain two integers *x**i*,<=*y**i* (|*x**i*|,<=|*y**i*|<=≀<=109), denoting the coordinates of the *i*-th vertex. It is guaranteed that these points are listed in clockwise order.
Print a single floating point number, representing the longest non-intersecting path that visits the vertices at most once. Your answer will be accepted if it has absolute or relative error at most 10<=-<=9. More specifically, if your answer is *a* and the jury answer is *b*, your answer will be accepted if .
[ "4\n0 0\n0 1\n1 1\n1 0\n" ]
[ "3.4142135624\n" ]
One optimal path is to visit points 0,1,3,2 in order.
[]
0
0
-1
134,320
666
Forensic Examination
[ "data structures", "string suffix structures" ]
null
null
The country of Reberland is the archenemy of Berland. Recently the authorities of Berland arrested a Reberlandian spy who tried to bring the leaflets intended for agitational propaganda to Berland illegally . The most leaflets contain substrings of the Absolutely Inadmissible Swearword and maybe even the whole word. B...
The first line contains the string *s* (1<=≀<=|*s*|<=≀<=5Β·105) β€” the Absolutely Inadmissible Swearword. The string *s* consists of only lowercase English letters. The second line contains the only integer *m* (1<=≀<=*m*<=≀<=5Β·104) β€” the number of texts of leaflets for expertise. Each of the next *m* lines contains th...
Print *q* lines. The *i*-th of them should contain two integers β€” the number of the text with the most occurences and the number of occurences of the substring [*p**l*,<=*p**r*] of the string *s*. If there are several text numbers print the smallest one.
[ "suffixtree\n3\nsuffixtreesareawesome\ncartesiantreeisworsethansegmenttree\nnyeeheeheee\n2\n1 2 1 10\n1 3 9 10\n" ]
[ "1 1\n3 4\n" ]
none
[]
0
0
-1
134,376
575
Party
[ "bitmasks", "brute force", "graph matchings" ]
null
null
Note the unusual memory limit for the problem. People working in MDCS (Microsoft Development Center Serbia) like partying. They usually go to night clubs on Friday and Saturday. There are *N* people working in MDCS and there are *N* clubs in the city. Unfortunately, if there is more than one Microsoft employee in nig...
The first line contains integer *N* β€” number of employees in MDCS. Then an *N*<=Γ—<=*N* matrix follows, where element in *i*-th row and *j*-th column is an integer number that represents how much *i*-th person likes *j*-th club’s Friday party. Then another *N*<=Γ—<=*N* matrix follows, where element in *i*-th row and *j...
Output should contain a single integer β€” maximum sum of happiness possible.
[ "4\n1 2 3 4\n2 3 4 1\n3 4 1 2\n4 1 2 3\n5 8 7 1\n6 9 81 3\n55 78 1 6\n1 1 1 1\n" ]
[ "167\n" ]
Here is how we matched people with clubs: Friday: 1st person with 4th club (4 happiness) and 4th person with 1st club (4 happiness). Saturday: 2nd person with 3rd club (81 happiness) and 3rd person with 2nd club (78 happiness). 4+4+81+78 = 167
[]
62
4,198,400
0
134,627
0
none
[ "none" ]
null
null
Instructors of Some Informatics School make students go to bed. The house contains *n* rooms, in each room exactly *b* students were supposed to sleep. However, at the time of curfew it happened that many students are not located in their assigned rooms. The rooms are arranged in a row and numbered from 1 to *n*. Init...
The first line contains three integers *n*, *d* and *b* (2<=≀<=*n*<=≀<=100<=000, 1<=≀<=*d*<=≀<=*n*<=-<=1, 1<=≀<=*b*<=≀<=10<=000), number of rooms in the house, running distance of a student, official number of students in a room. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=109), ...
Output one integer, the minimal possible value of the maximum of *x**i*.
[ "5 1 1\n1 0 0 0 4\n", "6 1 2\n3 8 0 1 0 0\n" ]
[ "1\n", "2\n" ]
In the first sample the first three rooms are processed by the first instructor, and the last two are processed by the second instructor. One of the optimal strategies is the following: firstly three students run from room 5 to room 4, on the next stage two of them run to room 3, and one of those two hides under a bed....
[]
15
0
0
134,637
217
Alien DNA
[ "data structures", "dsu", "trees" ]
null
null
Professor Bajtocy is conducting experiments on alien DNA. He has discovered that it is subject to repetitive mutations β€” each mutation happens in the same way: some continuous subsequence of the alien DNA becomes active, copies itself, the copy gets mangled and inserts itself right after the original subsequence. The m...
The first line of input contains the original DNA sequence, consisting only of letters "A", "C", "T" and "G" and not exceeding 3Β·106 in length. The second line contains a single integer *k* (1<=≀<=*k*<=≀<=3Β·106). The third line contains a single integer *n* (0<=≀<=*n*<=≀<=5000) β€” the number of mutations. The next *n...
Output a single line, containing the first *k* letters of the mutated DNA sequence.
[ "GAGA\n4\n0\n", "ACGTACGT\n16\n2\n1 2\n2 8\n" ]
[ "GAGA\n", "ACCAGTACCGACATCG\n" ]
In the second example, after the first mutation the sequence is "ACCAGTACGT". After the second mutation it's "ACCAGTACCGACATCGT".
[]
60
0
0
134,856
821
Okabe and City
[ "dfs and similar", "graphs", "shortest paths" ]
null
null
Okabe likes to be able to walk through his city on a path lit by street lamps. That way, he doesn't get beaten up by schoolchildren. Okabe's city is represented by a 2D grid of cells. Rows are numbered from 1 to *n* from top to bottom, and columns are numbered 1 to *m* from left to right. Exactly *k* cells in the city...
The first line of input contains three space-separated integers *n*, *m*, and *k* (2<=≀<=*n*,<=*m*,<=*k*<=≀<=104). Each of the next *k* lines contains two space-separated integers *r**i* and *c**i* (1<=≀<=*r**i*<=≀<=*n*, 1<=≀<=*c**i*<=≀<=*m*)Β β€” the row and the column of the *i*-th lit cell. It is guaranteed that all ...
Print the minimum number of coins Okabe needs to pay to complete his walk, or -1 if it's not possible.
[ "4 4 5\n1 1\n2 1\n2 3\n3 3\n4 3\n", "5 5 4\n1 1\n2 1\n3 1\n3 2\n", "2 2 4\n1 1\n1 2\n2 1\n2 2\n", "5 5 4\n1 1\n2 2\n3 3\n4 4\n" ]
[ "2\n", "-1\n", "0\n", "3\n" ]
In the first sample test, Okabe can take the path <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/fd8d0b719801e9cfe8f6be340d76a554f6ffb66b.png" style="max-width: 100.0%;max-height: 100.0%;"/>, paying only when moving to (2, 3) and (4, 4). In the fourth sample, Okabe can take the path <img ...
[ { "input": "4 4 5\n1 1\n2 1\n2 3\n3 3\n4 3", "output": "2" }, { "input": "5 5 4\n1 1\n2 1\n3 1\n3 2", "output": "-1" }, { "input": "2 2 4\n1 1\n1 2\n2 1\n2 2", "output": "0" }, { "input": "5 5 4\n1 1\n2 2\n3 3\n4 4", "output": "3" }, { "input": "7 10 53\n1 1\n7 5\...
78
23,142,400
-1
135,323
396
On Iteration of One Well-Known Function
[ "math" ]
null
null
Of course, many of you can calculate Ο†(*n*) β€” the number of positive integers that are less than or equal to *n*, that are coprime with *n*. But what if we need to calculate Ο†(Ο†(...Ο†(*n*))), where function Ο† is taken *k* times and *n* is given in the canonical decomposition into prime factors? You are given *n* and *...
The first line contains integer *m* (1<=≀<=*m*<=≀<=105) β€” the number of distinct prime divisors in the canonical representaion of *n*. Each of the next *m* lines contains a pair of space-separated integers *p**i*,<=*a**i* (2<=≀<=*p**i*<=≀<=106;Β 1<=≀<=*a**i*<=≀<=1017) β€” another prime divisor of number *n* and its power...
In the first line, print integer *w* β€” the number of distinct prime divisors of number Ο†(Ο†(...Ο†(*n*))), where function Ο† is taken *k* times. Each of the next *w* lines must contain two space-separated integers *q**i*,<=*b**i* (*b**i*<=β‰₯<=1) β€” another prime divisor and its power in the canonical representaion of the re...
[ "1\n7 1\n1\n", "1\n7 1\n2\n", "1\n2 100000000000000000\n10000000000000000\n" ]
[ "2\n2 1\n3 1\n", "1\n2 1\n", "1\n2 90000000000000000\n" ]
You can read about canonical representation of a positive integer here: http://en.wikipedia.org/wiki/Fundamental_theorem_of_arithmetic. You can read about function Ο†(*n*) here: http://en.wikipedia.org/wiki/Euler's_totient_function.
[]
30
0
0
135,431
121
Lucky Segments
[ "binary search", "implementation", "two pointers" ]
null
null
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya has *n* number segments [*l*1;Β *r*1], [*l*2;Β *r*2], ..., [*l**n*;Β *r**n*]. During one move Pet...
The first line contains two integers *n* and *k* (1<=≀<=*n*<=≀<=105, 1<=≀<=*k*<=≀<=1018) β€” the number of segments and the maximum number of moves. Next *n* lines contain pairs of integers *l**i* and *r**i* (1<=≀<=*l**i*<=≀<=*r**i*<=≀<=1018). Please do not use the %lld specificator to read or write 64-bit integers in Π‘...
Print on the single line the single number β€” the answer to the problem.
[ "4 7\n1 4\n6 9\n4 7\n3 5\n", "2 7\n40 45\n47 74\n" ]
[ "1\n", "2\n" ]
In the first sample Petya shifts the second segment by two units to the left (it turns into [4;Β 7]), after that number 4 becomes full. In the second sample Petya shifts the first segment by two units to the right (it turns into [42;Β 47]), and shifts the second segment by three units to the left (it turns into [44;Β 71]...
[]
93
2,150,400
-1
135,532
0
none
[ "none" ]
null
null
Polar bears like unique arrays β€” that is, arrays without repeated elements. You have got a unique array *s* with length *n* containing non-negative integers. Since you are good friends with Alice and Bob, you decide to split the array in two. Precisely, you need to construct two arrays *a* and *b* that are also of le...
The first line of the input contains integer *n* (1<=≀<=*n*<=≀<=105). The second line contains *n* distinct integers *s*1,<=*s*2,<=... *s**n* (0<=≀<=*s**i*<=≀<=109).
If it is possible to make Alice and Bob happy (if you can split the given array), print "YES" (without quotes) in the first line. In the second line, print the array *a*. In the third line, print the array *b*. There may be more than one solution. Any of them will be accepted. If it is impossible to split *s* into alm...
[ "6\n12 5 8 3 11 9\n" ]
[ "YES\n6 2 6 0 2 4\n6 3 2 3 9 5" ]
In the sample, we can remove the first two entries from *a* and the second entry from *b* to make them both unique.
[]
92
0
0
136,056
0
none
[ "none" ]
null
null
Stepan is a very experienced olympiad participant. He has *n* cups for Physics olympiads and *m* cups for Informatics olympiads. Each cup is characterized by two parameters β€” its significance *c**i* and width *w**i*. Stepan decided to expose some of his cups on a shelf with width *d* in such a way, that: - there is ...
The first line contains three integers *n*, *m* and *d* (1<=≀<=*n*,<=*m*<=≀<=100<=000, 1<=≀<=*d*<=≀<=109) β€” the number of cups for Physics olympiads, the number of cups for Informatics olympiads and the width of the shelf. Each of the following *n* lines contains two integers *c**i* and *w**i* (1<=≀<=*c**i*,<=*w**i*<=...
Print the maximum possible total significance, which Stepan can get exposing cups on the shelf with width *d*, considering all the rules described in the statement. If there is no way to expose cups on the shelf, then print 0.
[ "3 1 8\n4 2\n5 5\n4 2\n3 2\n", "4 3 12\n3 4\n2 4\n3 5\n3 4\n3 5\n5 2\n3 4\n", "2 2 2\n5 3\n6 3\n4 2\n8 1\n" ]
[ "8\n", "11\n", "0\n" ]
In the first example Stepan has only one Informatics cup which must be exposed on the shelf. Its significance equals 3 and width equals 2, so after Stepan exposes it, the width of free space on the shelf becomes equal to 6. Also, Stepan must expose the second Physics cup (which has width 5), because it is the most sign...
[ { "input": "3 1 8\n4 2\n5 5\n4 2\n3 2", "output": "8" }, { "input": "4 3 12\n3 4\n2 4\n3 5\n3 4\n3 5\n5 2\n3 4", "output": "11" }, { "input": "2 2 2\n5 3\n6 3\n4 2\n8 1", "output": "0" }, { "input": "10 10 229\n15 17\n5 4\n4 15\n4 17\n15 11\n7 6\n5 19\n14 8\n4 1\n10 12\n20 13...
46
0
0
136,201
802
Marmots (easy)
[ "math" ]
null
null
Heidi is a statistician to the core, and she likes to study the evolution of marmot populations in each of *V* (1<=≀<=*V*<=≀<=100) villages! So it comes that every spring, when Heidi sees the first snowdrops sprout in the meadows around her barn, she impatiently dons her snowshoes and sets out to the Alps, to welcome h...
The first line of input will contain the number of villages *V* (1<=≀<=*V*<=≀<=100). The following *V* lines each describe one village. The description of each village consists of 250 space-separated integers *k*, drawn from one of the above distributions.
Output one line per village, in the same order as provided in the input. The village's line shall state poisson if the village's distribution is of the Poisson type, and uniform if the answer came from a uniform distribution.
[ "2\n92 100 99 109 93 105 103 106 101 99 ... (input is truncated)\n28 180 147 53 84 80 180 85 8 16 ... (input is truncated)" ]
[ "poisson\nuniform\n" ]
The full example input is visually represented below, along with the probability distribution function it was drawn from (the *y*-axis is labeled by its values multiplied by 250). <img class="tex-graphics" src="https://espresso.codeforces.com/77563a6378b39d03eb21012c835c6e96df776b81.png" style="max-width: 100.0%;max-h...
[]
93
0
-1
136,282
494
Birthday
[ "data structures", "dfs and similar", "dp", "trees" ]
null
null
Ali is Hamed's little brother and tomorrow is his birthday. Hamed wants his brother to earn his gift so he gave him a hard programming problem and told him if he can successfully solve it, he'll get him a brand new laptop. Ali is not yet a very talented programmer like Hamed and although he usually doesn't cheat but th...
In the first line of input an integer *n* (1<=≀<=*n*<=≀<=105), number of vertices of the tree is given. In each of the next *n*<=-<=1 lines three space-separated integers *a**i*,<=*b**i*,<=*c**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*, 1<=≀<=*c**i*<=≀<=109) are given indicating an edge between *a**i* and *b**i* with weight eq...
Output *q* lines. In the *i*-th line print the value of *f*(*u**i*,<=*v**i*) modulo 109<=+<=7.
[ "5\n1 2 1\n4 3 1\n3 5 1\n1 3 1\n5\n1 1\n1 5\n2 4\n2 1\n3 5\n", "8\n1 2 100\n1 3 20\n2 4 2\n2 5 1\n3 6 1\n3 7 2\n6 8 5\n6\n1 8\n2 3\n5 8\n2 6\n4 7\n6 1\n" ]
[ "10\n1000000005\n1000000002\n23\n1000000002\n", "999968753\n49796\n999961271\n999991235\n999958569\n45130\n" ]
none
[]
46
0
0
136,992
538
Summer Dichotomy
[ "2-sat", "data structures", "dfs and similar", "greedy" ]
null
null
*T* students applied into the ZPP class of Summer Irrelevant School. The organizing committee of the school may enroll any number of them, but at least *t* students must be enrolled. The enrolled students should be divided into two groups in any manner (it is possible that one of the groups will be empty!) During a sh...
The first line contains two space-separated integers, *t* and *T* (1<=≀<=*t*<=≀<=*T*<=≀<=109). The second line contains two space-separated integers *n* and *m* (1<=≀<=*n*<=≀<=105, 0<=≀<=*m*<=≀<=105). The *i*-th of the next *n* lines contain integers *l**i* and *r**i* (0<=≀<=*l**i*<=≀<=*r**i*<=≀<=109). The next *m* ...
If the distribution is possible, print in the first line a single word 'POSSIBLE' (without the quotes). In the second line print two space-separated integers *n*1 and *n*2Β β€” the number of students in the first and second group, correspondingly, the contstraint *t*<=≀<=*n*1<=+<=*n*2<=≀<=*T* should be met. In the third l...
[ "10 20\n3 0\n3 6\n4 9\n16 25\n", "1 10\n3 3\n0 10\n0 10\n0 10\n1 2\n1 3\n2 3\n" ]
[ "POSSIBLE\n4 16\n112\n", "IMPOSSIBLE\n" ]
none
[]
30
0
0
137,809
822
My pretty girl Noora
[ "brute force", "dp", "greedy", "math", "number theory" ]
null
null
In Pavlopolis University where Noora studies it was decided to hold beauty contest "Miss Pavlopolis University". Let's describe the process of choosing the most beautiful girl in the university in more detail. The contest is held in several stages. Suppose that exactly *n* girls participate in the competition initiall...
The first and single line contains three integers *t*, *l* and *r* (1<=≀<=*t*<=&lt;<=109<=+<=7,<=2<=≀<=*l*<=≀<=*r*<=≀<=5Β·106).
In the first line print single integer β€” the value of the expression modulo 109<=+<=7.
[ "2 2 4\n" ]
[ "19\n" ]
Consider the sample. It is necessary to find the value of <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/3ecc798906ae9e9852061ba2dd5cf6b8fce7753b.png" style="max-width: 100.0%;max-height: 100.0%;"/>. *f*(2) = 1. From two girls you can form only one group of two people, in which there wil...
[ { "input": "2 2 4", "output": "19" }, { "input": "7 2444902 2613424", "output": "619309304" }, { "input": "7 2055976 2242066", "output": "231875164" }, { "input": "5 431999 611310", "output": "160643716" }, { "input": "9 1621304 1742530", "output": "797579313"...
982
82,329,600
0
137,869
724
Uniformly Branched Trees
[ "combinatorics", "dp", "trees" ]
null
null
A tree is a connected graph without cycles. Two trees, consisting of *n* vertices each, are called isomorphic if there exists a permutation *p*:<={1,<=...,<=*n*}<=β†’<={1,<=...,<=*n*} such that the edge (*u*,<=*v*) is present in the first tree if and only if the edge (*p**u*,<=*p**v*) is present in the second tree. Ver...
The single line of the input contains three integers *n*, *d* andΒ *mod* (1<=≀<=*n*<=≀<=1000, 2<=≀<=*d*<=≀<=10, 108<=≀<=*mod*<=≀<=109) Β β€” the number of vertices in the tree, the degree of internal vertices and the prime modulo.
Print the number of trees over the modulo *mod*.
[ "5 2 433416647\n", "10 3 409693891\n", "65 4 177545087\n" ]
[ "1\n", "2\n", "910726\n" ]
none
[]
30
0
0
137,931
404
Marathon
[ "implementation", "math" ]
null
null
Valera takes part in the Berland Marathon. The marathon race starts at the stadium that can be represented on the plane as a square whose lower left corner is located at point with coordinates (0,<=0) and the length of the side equals *a* meters. The sides of the square are parallel to coordinate axes. As the length o...
The first line contains two space-separated real numbers *a* and *d* (1<=≀<=*a*,<=*d*<=≀<=105), given with precision till 4 decimal digits after the decimal point. Number *a* denotes the length of the square's side that describes the stadium. Number *d* shows that after each *d* meters Valera gets an extra drink. The ...
Print *n* lines, each line should contain two real numbers *x**i* and *y**i*, separated by a space. Numbers *x**i* and *y**i* in the *i*-th line mean that Valera is at point with coordinates (*x**i*,<=*y**i*) after he covers *i*Β·*d* meters. Your solution will be considered correct if the absolute or relative error does...
[ "2 5\n2\n", "4.147 2.8819\n6\n" ]
[ "1.0000000000 2.0000000000\n2.0000000000 0.0000000000\n", "2.8819000000 0.0000000000\n4.1470000000 1.6168000000\n3.7953000000 4.1470000000\n0.9134000000 4.1470000000\n0.0000000000 2.1785000000\n0.7034000000 0.0000000000\n" ]
none
[ { "input": "2 5\n2", "output": "1.0000000000 2.0000000000\n2.0000000000 0.0000000000" }, { "input": "4.147 2.8819\n6", "output": "2.8819000000 0.0000000000\n4.1470000000 1.6168000000\n3.7953000000 4.1470000000\n0.9134000000 4.1470000000\n0.0000000000 2.1785000000\n0.7034000000 0.0000000000" },...
202
13,312,000
0
138,035
256
Liars and Serge
[ "dp" ]
null
null
There are *n* people, sitting in a line at the table. For each person we know that he always tells either the truth or lies. Little Serge asked them: how many of you always tell the truth? Each of the people at the table knows everything (who is an honest person and who is a liar) about all the people at the table. Th...
The first line contains two integers *n*, *k*, (1<=≀<=*k*<=≀<=*n*<=≀<=28). It is guaranteed that *n* β€” is the power of number 2.
Print a single integer β€” the answer to the problem modulo 777777777.
[ "1 1\n", "2 1\n" ]
[ "0\n", "2\n" ]
none
[]
92
0
0
139,170
424
Colored Jenga
[ "dfs and similar", "dp", "probabilities" ]
null
null
Cold winter evenings in Tomsk are very boring β€” nobody wants be on the streets at such a time. Residents of Tomsk while away the time sitting in warm apartments, inventing a lot of different games. One of such games is 'Colored Jenga'. This game requires wooden blocks of three colors: red, green and blue. A tower of *...
The first line of the input contains the only integer *n* (2<=≀<=*n*<=≀<=6) β€” the number of levels in the tower. Then *n* lines follow, describing the levels of the tower from the bottom to the top (the first line is the top of the tower). Each level is described by three characters, the first and the third of them se...
In the only line of the output print the sought mathematical expectation value. The answer will be considered correct if its relative or absolute error doesn't exceed 10<=-<=6.
[ "6\nRGB\nGRG\nBBB\nGGR\nBRG\nBRB\n" ]
[ "17.119213696601992\n" ]
none
[]
31
0
0
139,343
594
Cutting the Line
[ "string suffix structures", "strings" ]
null
null
You are given a non-empty line *s* and an integer *k*. The following operation is performed with this line exactly once: - A line is split into at most *k* non-empty substrings, i.e. string *s* is represented as a concatenation of a set of strings *s*<==<=*t*1<=+<=*t*2<=+<=...<=+<=*t**m*, 1<=≀<=*m*<=≀<=*k*. - Some o...
The first line of the input contains string *s* (1<=≀<=|*s*|<=≀<=5<=000<=000), consisting of lowercase English letters. The second line contains integer *k* (1<=≀<=*k*<=≀<=|*s*|)Β β€” the maximum number of parts in the partition.
In the single line print the lexicographically minimum string *s*' which can be obtained as a result of performing the described operation.
[ "aba\n2\n", "aaaabacaba\n2\n", "bababa\n1\n", "abacabadabacaba\n4\n" ]
[ "aab\n", "aaaaabacab\n", "ababab\n", "aababacabacabad\n" ]
none
[]
46
0
0
139,576
802
April Fools' Problem (medium)
[ "binary search", "flows", "graphs" ]
null
null
The marmots need to prepare *k* problems for HC2 over *n* days. Each problem, once prepared, also has to be printed. The preparation of a problem on day *i* (at most one per day) costs *a**i* CHF, and the printing of a problem on day *i* (also at most one per day) costs *b**i* CHF. Of course, a problem cannot be print...
The first line of input contains two space-separated integers *n* and *k* (1<=≀<=*k*<=≀<=*n*<=≀<=2200). The second line contains *n* space-separated integers *a*1,<=...,<=*a**n* () β€” the preparation costs. The third line contains *n* space-separated integers *b*1,<=...,<=*b**n* () β€” the printing costs.
Output the minimum cost of preparation and printing *k* problems β€” that is, the minimum possible sum *a**i*1<=+<=*a**i*2<=+<=...<=+<=*a**i**k*<=+<=*b**j*1<=+<=*b**j*2<=+<=...<=+<=*b**j**k*, where 1<=≀<=*i*1<=&lt;<=*i*2<=&lt;<=...<=&lt;<=*i**k*<=≀<=*n*, 1<=≀<=*j*1<=&lt;<=*j*2<=&lt;<=...<=&lt;<=*j**k*<=≀<=*n* and *i*1<=≀...
[ "8 4\n3 8 7 9 9 4 6 8\n2 5 9 4 3 8 9 1\n" ]
[ "32" ]
In the sample testcase, one optimum solution is to prepare the first problem on day 1 and print it on day 1, prepare the second problem on day 2 and print it on day 4, prepare the third problem on day 3 and print it on day 5, and prepare the fourth problem on day 6 and print it on day 8.
[ { "input": "8 4\n3 8 7 9 9 4 6 8\n2 5 9 4 3 8 9 1", "output": "32" }, { "input": "10 6\n60 8 63 72 1 100 23 59 71 59\n81 27 66 53 46 64 86 27 41 82", "output": "472" }, { "input": "13 13\n93 19 58 34 96 7 35 46 60 5 36 40 41\n57 3 42 68 26 85 25 45 50 21 60 23 79", "output": "1154" ...
3,868
13,926,400
3
140,240
291
Parallel Programming
[ "*special", "greedy" ]
null
null
Polycarpus has a computer with *n* processors. Also, his computer has *n* memory cells. We'll consider the processors numbered by integers from 1 to *n* and that the memory cells are consecutively numbered by integers from 1 to *n*. Polycarpus needs to come up with a parallel program model. For each memory cell number...
The first line contains two space-separated integers *n* and *k* (1<=≀<=*n*<=≀<=104,<=1<=≀<=*k*<=≀<=20). It is guaranteed that at the given *n* and *k* the required sequence of operations exists.
Print exactly *n*Β·*k* integers in *k* lines. In the first line print numbers *c*1,<=*c*2,<=...,<=*c**n* (1<=≀<=*c**i*<=≀<=*n*) for the first increment operation. In the second line print the numbers for the second increment operation. In the *k*-th line print the numbers for the *k*-th increment operation. As a result...
[ "1 1\n", "3 2\n" ]
[ "1\n", "2 3 3\n3 3 3\n" ]
none
[ { "input": "1 1", "output": "1" }, { "input": "3 2", "output": "2 3 3\n3 3 3" }, { "input": "4 2", "output": "2 3 4 4\n3 4 4 4" }, { "input": "2 1", "output": "2 2" }, { "input": "2 20", "output": "2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 ...
124
716,800
0
140,477
538
Demiurges Play Again
[ "dfs and similar", "dp", "math", "trees" ]
null
null
Demiurges Shambambukli and Mazukta love to watch the games of ordinary people. Today, they noticed two men who play the following game. There is a rooted tree on *n* nodes, *m* of which are leaves (a leaf is a nodes that does not have any children), edges of the tree are directed from parent to children. In the leaves...
The first line contains a single integer *n*Β β€” the number of nodes in the tree (1<=≀<=*n*<=≀<=2Β·105). Each of the next *n*<=-<=1 lines contains two integers *u**i* and *v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*)Β β€” the ends of the edge of the tree; the edge leads from node *u**i* to node *v**i*. It is guaranteed that the de...
Print two space-separated integers β€” the maximum possible and the minimum possible result of the game.
[ "5\n1 2\n1 3\n2 4\n2 5\n", "6\n1 2\n1 3\n3 4\n1 5\n5 6\n" ]
[ "3 2\n", "3 3\n" ]
Consider the first sample. The tree contains three leaves: 3, 4 and 5. If we put the maximum number 3 at node 3, then the first player moves there and the result will be 3. On the other hand, it is easy to see that for any rearrangement the first player can guarantee the result of at least 2. In the second sample no m...
[ { "input": "5\n1 2\n1 3\n2 4\n2 5", "output": "3 2" }, { "input": "6\n1 2\n1 3\n3 4\n1 5\n5 6", "output": "3 3" }, { "input": "1", "output": "1 1" }, { "input": "2\n1 2", "output": "1 1" }, { "input": "3\n1 2\n1 3", "output": "2 2" }, { "input": "10\n1...
108
20,172,800
0
140,582
731
Funny Game
[ "dp", "games" ]
null
null
Once upon a time Petya and Gena gathered after another programming competition and decided to play some game. As they consider most modern games to be boring, they always try to invent their own games. They have only stickers and markers, but that won't stop them. The game they came up with has the following rules. In...
The first line of input contains a single integer *n* (2<=≀<=*n*<=≀<=200<=000)Β β€” the number of stickers, initially located on the wall. The second line contains *n* integers *a*1, *a*2, ..., *a**n* (<=-<=10<=000<=≀<=*a**i*<=≀<=10<=000)Β β€” the numbers on stickers in order from left to right.
Print one integerΒ β€” the difference between the Petya's score and Gena's score at the end of the game if both players play optimally.
[ "3\n2 4 8\n", "4\n1 -7 -2 3\n" ]
[ "14\n", "-3\n" ]
In the first sample, the optimal move for Petya is to take all the stickers. As a result, his score will be equal to 14 and Gena's score will be equal to 0. In the second sample, the optimal sequence of moves is the following. On the first move Petya will take first three sticker and will put the new sticker with valu...
[ { "input": "3\n2 4 8", "output": "14" }, { "input": "4\n1 -7 -2 3", "output": "-3" }, { "input": "10\n35 11 35 28 48 25 2 43 23 10", "output": "260" }, { "input": "100\n437 89 481 95 29 326 10 304 97 414 52 46 106 181 385 173 337 148 437 133 52 136 86 250 289 61 480 314 166 6...
171
27,443,200
3
141,265
610
Vika and Segments
[ "constructive algorithms", "data structures", "geometry", "two pointers" ]
null
null
Vika has an infinite sheet of squared paper. Initially all squares are white. She introduced a two-dimensional coordinate system on this sheet and drew *n* black horizontal and vertical segments parallel to the coordinate axes. All segments have width equal to 1 square, that means every segment occupy some set of neigh...
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=100<=000)Β β€” the number of segments drawn by Vika. Each of the next *n* lines contains four integers *x*1, *y*1, *x*2 and *y*2 (<=-<=109<=≀<=*x*1,<=*y*1,<=*x*2,<=*y*2<=≀<=109)Β β€” the coordinates of the endpoints of the segments drawn by Vika. It is...
Print the number of cells painted by Vika. If a cell was painted more than once, it should be calculated exactly once in the answer.
[ "3\n0 1 2 1\n1 4 1 2\n0 3 2 3\n", "4\n-2 -1 2 -1\n2 1 -2 1\n-1 -2 -1 2\n1 2 1 -2\n" ]
[ "8\n", "16\n" ]
In the first sample Vika will paint squares (0, 1), (1, 1), (2, 1), (1, 2), (1, 3), (1, 4), (0, 3) and (2, 3).
[ { "input": "3\n0 1 2 1\n1 4 1 2\n0 3 2 3", "output": "8" }, { "input": "4\n-2 -1 2 -1\n2 1 -2 1\n-1 -2 -1 2\n1 2 1 -2", "output": "16" }, { "input": "1\n1 1 1 1", "output": "1" }, { "input": "10\n-357884841 -999999905 -357884841 999999943\n-130177221 999999983 -130177221 -999...
1,044
268,390,400
0
141,321
0
none
[ "none" ]
null
null
This is the modification of the problem used during the official round. Unfortunately, author's solution of the original problem appeared wrong, so the problem was changed specially for the archive. Once upon a time in a far away kingdom lived the King. The King had a beautiful daughter, Victoria. They lived happily, ...
The first line contains a single integer *n* from the problem's statement (1<=≀<=*n*<=≀<=10000).
Print the sought expected number of tosses as an irreducible fraction in the following form: "*a*/*b*" (without the quotes) without leading zeroes.
[ "2\n", "3\n", "4\n" ]
[ "1/1\n", "8/3\n", "2/1\n" ]
none
[ { "input": "2", "output": "1/1" }, { "input": "3", "output": "8/3" }, { "input": "4", "output": "2/1" }, { "input": "8", "output": "3/1" }, { "input": "7", "output": "24/7" }, { "input": "6", "output": "11/3" }, { "input": "1", "output"...
60
0
0
142,496
659
Polycarp and Hay
[ "dfs and similar", "dsu", "graphs", "greedy", "sortings" ]
null
null
The farmer Polycarp has a warehouse with hay, which can be represented as an *n*<=Γ—<=*m* rectangular table, where *n* is the number of rows, and *m* is the number of columns in the table. Each cell of the table contains a haystack. The height in meters of the hay located in the *i*-th row and the *j*-th column is equal...
The first line of the input contains three integers *n*, *m* (1<=≀<=*n*,<=*m*<=≀<=1000) and *k* (1<=≀<=*k*<=≀<=1018)Β β€” the number of rows and columns of the rectangular table where heaps of hay are lain and the required total number cubic meters of hay after the reorganization. Then *n* lines follow, each containing ...
In the first line print "YES" (without quotes), if Polycarpus can perform the reorganisation and "NO" (without quotes) otherwise. If the answer is "YES" (without quotes), then in next *n* lines print *m* numbersΒ β€” the heights of the remaining hay stacks. All the remaining non-zero values should be equal, represent a co...
[ "2 3 35\n10 4 9\n9 9 7\n", "4 4 50\n5 9 1 1\n5 1 1 5\n5 1 5 5\n5 5 7 1\n", "2 4 12\n1 1 3 1\n1 6 2 4\n" ]
[ "YES\n7 0 7 \n7 7 7 \n", "YES\n5 5 0 0 \n5 0 0 5 \n5 0 5 5 \n5 5 5 0 \n", "NO\n" ]
In the first sample non-zero values make up a connected area, their values do not exceed the initial heights of hay stacks. All the non-zero values equal 7, and their number is 5, so the total volume of the remaining hay equals the required value *k* = 7Β·5 = 35. At that the stack that is on the second line and third ro...
[ { "input": "2 3 35\n10 4 9\n9 9 7", "output": "YES\n7 0 7 \n7 7 7 " }, { "input": "4 4 50\n5 9 1 1\n5 1 1 5\n5 1 5 5\n5 5 7 1", "output": "YES\n5 5 0 0 \n5 0 0 5 \n5 0 5 5 \n5 5 5 0 " }, { "input": "2 4 12\n1 1 3 1\n1 6 2 4", "output": "NO" }, { "input": "6 6 38\n1 100 1 100 ...
2,917
130,355,200
0
142,649