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
975
Mancala
[ "brute force", "implementation" ]
null
null
Mancala is a game famous in the Middle East. It is played on a board that consists of 14 holes. Initially, each hole has $a_i$ stones. When a player makes a move, he chooses a hole which contains a positive number of stones. He takes all the stones inside it and then redistributes these stones one by one in the next ...
The only line contains 14 integers $a_1, a_2, \ldots, a_{14}$ ($0 \leq a_i \leq 10^9$)Β β€” the number of stones in each hole. It is guaranteed that for any $i$ ($1\leq i \leq 14$) $a_i$ is either zero or odd, and there is at least one stone in the board.
Output one integer, the maximum possible score after one move.
[ "0 1 1 0 0 0 0 0 0 7 0 0 0 0\n", "5 1 1 1 1 0 0 0 0 0 0 0 0 0\n" ]
[ "4\n", "8\n" ]
In the first test case the board after the move from the hole with $7$ stones will look like 1 2 2 0 0 0 0 0 0 0 1 1 1 1. Then the player collects the even numbers and ends up with a score equal to $4$.
[ { "input": "0 1 1 0 0 0 0 0 0 7 0 0 0 0", "output": "4" }, { "input": "5 1 1 1 1 0 0 0 0 0 0 0 0 0", "output": "8" }, { "input": "10001 10001 10001 10001 10001 10001 10001 10001 10001 10001 10001 10001 10001 1", "output": "54294" }, { "input": "0 0 0 0 0 0 0 0 0 0 0 0 0 15", ...
0
0
-1
3,363
920
Water The Garden
[ "implementation" ]
null
null
It is winter now, and Max decided it's about time he watered the garden. The garden can be represented as *n* consecutive garden beds, numbered from 1 to *n*. *k* beds contain water taps (*i*-th tap is located in the bed *x**i*), which, if turned on, start delivering water to neighbouring beds. If the tap on the bed *...
The first line contains one integer *t* β€” the number of test cases to solve (1<=≀<=*t*<=≀<=200). Then *t* test cases follow. The first line of each test case contains two integers *n* and *k* (1<=≀<=*n*<=≀<=200, 1<=≀<=*k*<=≀<=*n*) β€” the number of garden beds and water taps, respectively. Next line contains *k* intege...
For each test case print one integer β€” the minimum number of seconds that have to pass after Max turns on some of the water taps, until the whole garden is watered.
[ "3\n5 1\n3\n3 3\n1 2 3\n4 1\n1\n" ]
[ "3\n1\n4\n" ]
The first example consists of 3 tests: 1. There are 5 garden beds, and a water tap in the bed 3. If we turn it on, then after 1 second passes, only bed 3 will be watered; after 2 seconds pass, beds [1, 3] will be watered, and after 3 seconds pass, everything will be watered. 1. There are 3 garden beds, and there is ...
[ { "input": "3\n5 1\n3\n3 3\n1 2 3\n4 1\n1", "output": "3\n1\n4" }, { "input": "26\n1 1\n1\n2 1\n2\n2 1\n1\n2 2\n1 2\n3 1\n3\n3 1\n2\n3 2\n2 3\n3 1\n1\n3 2\n1 3\n3 2\n1 2\n3 3\n1 2 3\n4 1\n4\n4 1\n3\n4 2\n3 4\n4 1\n2\n4 2\n2 4\n4 2\n2 3\n4 3\n2 3 4\n4 1\n1\n4 2\n1 4\n4 2\n1 3\n4 3\n1 3 4\n4 2\n1 2\n4...
93
3,584,000
-1
3,370
733
Parade
[ "math" ]
null
null
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits beg...
The first line contains single integer *n* (1<=≀<=*n*<=≀<=105)Β β€” the number of columns. The next *n* lines contain the pairs of integers *l**i* and *r**i* (1<=≀<=*l**i*,<=*r**i*<=≀<=500)Β β€” the number of soldiers in the *i*-th column which start to march from the left or the right leg respectively.
Print single integer *k*Β β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to *n* in the order they are given in the input data. If there are several answers, print any of them.
[ "3\n5 6\n8 9\n10 3\n", "2\n6 5\n5 6\n", "6\n5 9\n1 3\n4 8\n4 5\n23 54\n12 32\n" ]
[ "3\n", "1\n", "0\n" ]
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right legΒ β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third co...
[ { "input": "3\n5 6\n8 9\n10 3", "output": "3" }, { "input": "2\n6 5\n5 6", "output": "1" }, { "input": "6\n5 9\n1 3\n4 8\n4 5\n23 54\n12 32", "output": "0" }, { "input": "2\n500 499\n500 500", "output": "0" }, { "input": "1\n139 252", "output": "0" }, { ...
1,000
10,240,000
0
3,371
809
Do you want a date?
[ "implementation", "math", "sortings" ]
null
null
Leha decided to move to a quiet town Vičkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the week Leha managed to get access to *n* computers throughout the town. Incidentally all the computers, which were hacked by Leha, lie on t...
The first line contains one integer *n* (1<=≀<=*n*<=≀<=3Β·105) denoting the number of hacked computers. The second line contains *n* integers *x*1,<=*x*2,<=...,<=*x**n* (1<=≀<=*x**i*<=≀<=109) denoting the coordinates of hacked computers. It is guaranteed that all *x**i* are distinct.
Print a single integerΒ β€” the required sum modulo 109<=+<=7.
[ "2\n4 7\n", "3\n4 3 1\n" ]
[ "3\n", "9\n" ]
There are three non-empty subsets in the first sample test:<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/02b2d12556dad85f1c6c6912786eb87d4be2ea17.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/22f6a537962c...
[ { "input": "2\n4 7", "output": "3" }, { "input": "3\n4 3 1", "output": "9" }, { "input": "20\n8 11 13 19 21 34 36 44 57 58 61 63 76 78 79 81 85 86 90 95", "output": "83396599" }, { "input": "20\n1 8 9 12 15 17 18 24 30 33 36 41 53 54 59 62 64 66 72 73", "output": "6805914...
545
268,390,400
0
3,373
911
Three Garlands
[ "brute force", "constructive algorithms" ]
null
null
Mishka is decorating the Christmas tree. He has got three garlands, and all of them will be put on the tree. After that Mishka will switch these garlands on. When a garland is switched on, it periodically changes its state β€” sometimes it is lit, sometimes not. Formally, if *i*-th garland is switched on during *x*-th s...
The first line contains three integers *k*1, *k*2 and *k*3 (1<=≀<=*k**i*<=≀<=1500) β€” time intervals of the garlands.
If Mishka can choose moments of time to switch on the garlands in such a way that each second after switching the garlands on at least one garland will be lit, print YES. Otherwise, print NO.
[ "2 2 3\n", "4 2 3\n" ]
[ "YES\n", "NO\n" ]
In the first example Mishka can choose *x*<sub class="lower-index">1</sub> = 1, *x*<sub class="lower-index">2</sub> = 2, *x*<sub class="lower-index">3</sub> = 1. The first garland will be lit during seconds 1, 3, 5, 7, ..., the second β€” 2, 4, 6, 8, ..., which already cover all the seconds after the 2-nd one. It doesn't...
[ { "input": "2 2 3", "output": "YES" }, { "input": "4 2 3", "output": "NO" }, { "input": "1499 1498 1500", "output": "NO" }, { "input": "1500 1500 1500", "output": "NO" }, { "input": "100 4 1", "output": "YES" }, { "input": "4 2 4", "output": "YES" ...
61
5,632,000
0
3,377
216
Forming Teams
[ "dfs and similar", "implementation" ]
null
null
One day *n* students come to the stadium. They want to play football, and for that they need to split into teams, the teams must have an equal number of people. We know that this group of people has archenemies. Each student has at most two archenemies. Besides, if student *A* is an archenemy to student *B*, then stud...
The first line contains two integers *n* and *m* (2<=≀<=*n*<=≀<=100, 1<=≀<=*m*<=≀<=100) β€” the number of students and the number of pairs of archenemies correspondingly. Next *m* lines describe enmity between students. Each enmity is described as two numbers *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*, *a**i*<=β‰ <=...
Print a single integer β€” the minimum number of students you will have to send to the bench in order to start the game.
[ "5 4\n1 2\n2 4\n5 3\n1 4\n", "6 2\n1 4\n3 4\n", "6 6\n1 2\n2 3\n3 1\n4 5\n5 6\n6 4\n" ]
[ "1", "0", "2" ]
none
[ { "input": "5 4\n1 2\n2 4\n5 3\n1 4", "output": "1" }, { "input": "6 2\n1 4\n3 4", "output": "0" }, { "input": "6 6\n1 2\n2 3\n3 1\n4 5\n5 6\n6 4", "output": "2" }, { "input": "5 1\n1 2", "output": "1" }, { "input": "8 8\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 1", ...
810
31,539,200
0
3,379
702
Cellular Network
[ "binary search", "implementation", "two pointers" ]
null
null
You are given *n* points on the straight line β€” the positions (*x*-coordinates) of the cities and *m* points on the same line β€” the positions (*x*-coordinates) of the cellular towers. All towers work in the same way β€” they provide cellular network for all cities, which are located at the distance which is no more than ...
The first line contains two positive integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=105) β€” the number of cities and the number of cellular towers. The second line contains a sequence of *n* integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=≀<=*a**i*<=≀<=109) β€” the coordinates of cities. It is allowed that there are any number...
Print minimal *r* so that each city will be covered by cellular network.
[ "3 2\n-2 2 4\n-3 0\n", "5 3\n1 5 10 14 17\n4 11 15\n" ]
[ "4\n", "3\n" ]
none
[ { "input": "3 2\n-2 2 4\n-3 0", "output": "4" }, { "input": "5 3\n1 5 10 14 17\n4 11 15", "output": "3" }, { "input": "1 1\n-1000000000\n1000000000", "output": "2000000000" }, { "input": "1 1\n1000000000\n-1000000000", "output": "2000000000" }, { "input": "10 10\n...
31
0
-1
3,382
221
Little Elephant and Numbers
[ "implementation" ]
null
null
The Little Elephant loves numbers. He has a positive integer *x*. The Little Elephant wants to find the number of positive integers *d*, such that *d* is the divisor of *x*, and *x* and *d* have at least one common (the same) digit in their decimal representations. Help the Little Elephant to find the described num...
A single line contains a single integer *x* (1<=≀<=*x*<=≀<=109).
In a single line print an integer β€” the answer to the problem.
[ "1\n", "10\n" ]
[ "1\n", "2\n" ]
none
[ { "input": "1", "output": "1" }, { "input": "10", "output": "2" }, { "input": "47", "output": "1" }, { "input": "100", "output": "5" }, { "input": "128", "output": "6" }, { "input": "2", "output": "1" }, { "input": "17", "output": "2" ...
248
21,606,400
3
3,394
811
Vladik and Memorable Trip
[ "dp", "implementation" ]
null
null
Vladik often travels by trains. He remembered some of his trips especially well and I would like to tell you about one of these trips: Vladik is at initial train station, and now *n* people (including Vladik) want to get on the train. They are already lined up in some order, and for each of them the city code *a**i* i...
First line contains single integer *n* (1<=≀<=*n*<=≀<=5000)Β β€” number of people. Second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=5000), where *a**i* denotes code of the city to which *i*-th person is going.
The output should contain a single integerΒ β€” maximal possible total comfort.
[ "6\n4 4 2 5 2 3\n", "9\n5 1 3 1 5 2 4 2 5\n" ]
[ "14\n", "9\n" ]
In the first test case best partition into segments is: [4, 4] [2, 5, 2] [3], answer is calculated as follows: 4 + (2 *xor* 5) + 3 = 4 + 7 + 3 = 14 In the second test case best partition into segments is: 5 1 [3] 1 5 [2, 4, 2] 5, answer calculated as follows: 3 + (2 *xor* 4) = 3 + 6 = 9.
[ { "input": "6\n4 4 2 5 2 3", "output": "14" }, { "input": "9\n5 1 3 1 5 2 4 2 5", "output": "9" }, { "input": "5\n1558 4081 3591 1700 3232", "output": "14162" }, { "input": "10\n3838 1368 4825 2068 4755 2048 1342 4909 2837 4854", "output": "32844" }, { "input": "1...
30
0
0
3,396
567
Berland National Library
[ "implementation" ]
null
null
Berland National Library has recently been built in the capital of Berland. In addition, in the library you can take any of the collected works of Berland leaders, the library has a reading room. Today was the pilot launch of an automated reading room visitors' accounting system! The scanner of the system is installed...
The first line contains a positive integer *n* (1<=≀<=*n*<=≀<=100) β€” the number of records in the system log. Next follow *n* events from the system journal in the order in which the were made. Each event was written on a single line and looks as "+ *r**i*" or "- *r**i*", where *r**i* is an integer from 1 to 106, the r...
Print a single integer β€” the minimum possible capacity of the reading room.
[ "6\n+ 12001\n- 12001\n- 1\n- 1200\n+ 1\n+ 7\n", "2\n- 1\n- 2\n", "2\n+ 1\n- 1\n" ]
[ "3", "2", "1" ]
In the first sample test, the system log will ensure that at some point in the reading room were visitors with registration numbers 1, 1200 and 12001. More people were not in the room at the same time based on the log. Therefore, the answer to the test is 3.
[ { "input": "6\n+ 12001\n- 12001\n- 1\n- 1200\n+ 1\n+ 7", "output": "3" }, { "input": "2\n- 1\n- 2", "output": "2" }, { "input": "2\n+ 1\n- 1", "output": "1" }, { "input": "5\n+ 1\n- 1\n+ 2\n+ 3\n- 4", "output": "3" }, { "input": "3\n- 1\n- 2\n- 3", "output": "...
46
102,400
0
3,411
746
Compote
[ "implementation", "math" ]
null
null
Nikolay has *a* lemons, *b* apples and *c* pears. He decided to cook a compote. According to the recipe the fruits should be in the ratio 1:<=2:<=4. It means that for each lemon in the compote should be exactly 2 apples and exactly 4 pears. You can't crumble up, break up or cut these fruits into pieces. These fruitsΒ β€” ...
The first line contains the positive integer *a* (1<=≀<=*a*<=≀<=1000)Β β€” the number of lemons Nikolay has. The second line contains the positive integer *b* (1<=≀<=*b*<=≀<=1000)Β β€” the number of apples Nikolay has. The third line contains the positive integer *c* (1<=≀<=*c*<=≀<=1000)Β β€” the number of pears Nikolay has...
Print the maximum total number of lemons, apples and pears from which Nikolay can cook the compote.
[ "2\n5\n7\n", "4\n7\n13\n", "2\n3\n2\n" ]
[ "7\n", "21\n", "0\n" ]
In the first example Nikolay can use 1 lemon, 2 apples and 4 pears, so the answer is 1 + 2 + 4 = 7. In the second example Nikolay can use 3 lemons, 6 apples and 12 pears, so the answer is 3 + 6 + 12 = 21. In the third example Nikolay don't have enough pears to cook any compote, so the answer is 0.
[ { "input": "2\n5\n7", "output": "7" }, { "input": "4\n7\n13", "output": "21" }, { "input": "2\n3\n2", "output": "0" }, { "input": "1\n1\n1", "output": "0" }, { "input": "1\n2\n4", "output": "7" }, { "input": "1000\n1000\n1000", "output": "1750" }...
77
6,758,400
3
3,414
0
none
[ "none" ]
null
null
Ivan had string *s* consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string *s*. Ivan preferred making a new string to finding the old one. Ivan knows some information about the string *s*. Namely, he remembers, that string *t**i* occurs in string *s* at least *k**...
The first line contains single integer *n* (1<=≀<=*n*<=≀<=105) β€” the number of strings Ivan remembers. The next *n* lines contain information about the strings. The *i*-th of these lines contains non-empty string *t**i*, then positive integer *k**i*, which equal to the number of times the string *t**i* occurs in strin...
Print lexicographically minimal string that fits all the information Ivan remembers.
[ "3\na 4 1 3 5 7\nab 2 1 5\nca 1 4\n", "1\na 1 3\n", "3\nab 1 1\naba 1 3\nab 2 3 5\n" ]
[ "abacaba\n", "aaa\n", "ababab\n" ]
none
[ { "input": "3\na 4 1 3 5 7\nab 2 1 5\nca 1 4", "output": "abacaba" }, { "input": "1\na 1 3", "output": "aaa" }, { "input": "3\nab 1 1\naba 1 3\nab 2 3 5", "output": "ababab" }, { "input": "6\nba 2 16 18\na 1 12\nb 3 4 13 20\nbb 2 6 8\nababbbbbaab 1 3\nabababbbbb 1 1", "ou...
46
4,812,800
-1
3,419
140
New Year Table
[ "geometry", "math" ]
null
null
Gerald is setting the New Year table. The table has the form of a circle; its radius equals *R*. Gerald invited many guests and is concerned whether the table has enough space for plates for all those guests. Consider all plates to be round and have the same radii that equal *r*. Each plate must be completely inside th...
The first line contains three integers *n*, *R* and *r* (1<=≀<=*n*<=≀<=100, 1<=≀<=*r*,<=*R*<=≀<=1000) β€” the number of plates, the radius of the table and the plates' radius.
Print "YES" (without the quotes) if it is possible to place *n* plates on the table by the rules given above. If it is impossible, print "NO". Remember, that each plate must touch the edge of the table.
[ "4 10 4\n", "5 10 4\n", "1 10 10\n" ]
[ "YES\n", "NO\n", "YES\n" ]
The possible arrangement of the plates for the first sample is:
[ { "input": "4 10 4", "output": "YES" }, { "input": "5 10 4", "output": "NO" }, { "input": "1 10 10", "output": "YES" }, { "input": "3 10 20", "output": "NO" }, { "input": "2 20 11", "output": "NO" }, { "input": "6 9 3", "output": "YES" }, { ...
92
0
0
3,420
710
Optimal Point on a Line
[ "brute force", "sortings" ]
null
null
You are given *n* points on a line with their coordinates *x**i*. Find the point *x* so the sum of distances to the given points is minimal.
The first line contains integer *n* (1<=≀<=*n*<=≀<=3Β·105) β€” the number of points on the line. The second line contains *n* integers *x**i* (<=-<=109<=≀<=*x**i*<=≀<=109) β€” the coordinates of the given *n* points.
Print the only integer *x* β€” the position of the optimal point on the line. If there are several optimal points print the position of the leftmost one. It is guaranteed that the answer is always the integer.
[ "4\n1 2 3 4\n" ]
[ "2\n" ]
none
[ { "input": "4\n1 2 3 4", "output": "2" }, { "input": "5\n-1 -10 2 6 7", "output": "2" }, { "input": "10\n-68 10 87 22 30 89 82 -97 -52 25", "output": "22" }, { "input": "100\n457 827 807 17 871 935 907 -415 536 170 551 -988 865 758 -457 -892 -875 -488 684 19 0 555 -807 -624 -...
249
37,171,200
-1
3,424
0
none
[ "none" ]
null
null
Polycarpus has a finite sequence of opening and closing brackets. In order not to fall asleep in a lecture, Polycarpus is having fun with his sequence. He is able to perform two operations: - adding any bracket in any position (in the beginning, the end, or between any two existing brackets); - cyclic shift β€” moving...
The first line contains Polycarpus's sequence consisting of characters "(" and ")". The length of a line is from 1 to 1<=000<=000.
Print a correct bracket sequence of the minimum length that Polycarpus can obtain by his operations. If there are multiple such sequences, print the lexicographically minimum one.
[ "()(())\n", "()(\n" ]
[ "(())()", "(())" ]
The sequence in the first example is already correct, but to get the lexicographically minimum answer, you need to perform four cyclic shift operations. In the second example you need to add a closing parenthesis between the second and third brackets and make a cyclic shift. You can first make the shift, and then add t...
[]
62
0
0
3,433
399
Pages
[ "implementation" ]
null
null
User ainta is making a web site. This time he is going to make a navigation of the pages. In his site, there are *n* pages numbered by integers from 1 to *n*. Assume that somebody is on the *p*-th page now. The navigation will look like this: When someone clicks the button "&lt;&lt;" he is redirected to page 1, and wh...
The first and the only line contains three integers *n*, *p*, *k* (3<=≀<=*n*<=≀<=100; 1<=≀<=*p*<=≀<=*n*; 1<=≀<=*k*<=≀<=*n*)
Print the proper navigation. Follow the format of the output from the test samples.
[ "17 5 2\n", "6 5 2\n", "6 1 2\n", "6 2 2\n", "9 6 3\n", "10 6 3\n", "8 5 4\n" ]
[ "&lt;&lt; 3 4 (5) 6 7 &gt;&gt; ", "&lt;&lt; 3 4 (5) 6 ", "(1) 2 3 &gt;&gt; ", "1 (2) 3 4 &gt;&gt;", "&lt;&lt; 3 4 5 (6) 7 8 9", "&lt;&lt; 3 4 5 (6) 7 8 9 &gt;&gt;", "1 2 3 4 (5) 6 7 8 " ]
none
[ { "input": "17 5 2", "output": "<< 3 4 (5) 6 7 >> " }, { "input": "6 5 2", "output": "<< 3 4 (5) 6 " }, { "input": "6 1 2", "output": "(1) 2 3 >> " }, { "input": "6 2 2", "output": "1 (2) 3 4 >> " }, { "input": "9 6 3", "output": "<< 3 4 5 (6) 7 8 9 " }, {...
109
0
3
3,437
620
Professor GukiZ's Robot
[ "implementation", "math" ]
null
null
Professor GukiZ makes a new robot. The robot are in the point with coordinates (*x*1,<=*y*1) and should go to the point (*x*2,<=*y*2). In a single step the robot can change any of its coordinates (maybe both of them) by one (decrease or increase). So the robot can move in one of the 8 directions. Find the minimal numbe...
The first line contains two integers *x*1,<=*y*1 (<=-<=109<=≀<=*x*1,<=*y*1<=≀<=109) β€” the start position of the robot. The second line contains two integers *x*2,<=*y*2 (<=-<=109<=≀<=*x*2,<=*y*2<=≀<=109) β€” the finish position of the robot.
Print the only integer *d* β€” the minimal number of steps to get the finish position.
[ "0 0\n4 5\n", "3 4\n6 1\n" ]
[ "5\n", "3\n" ]
In the first example robot should increase both of its coordinates by one four times, so it will be in position (4, 4). After that robot should simply increase its *y* coordinate and get the finish position. In the second example robot should simultaneously increase *x* coordinate and decrease *y* coordinate by one th...
[ { "input": "0 0\n4 5", "output": "5" }, { "input": "3 4\n6 1", "output": "3" }, { "input": "0 0\n4 6", "output": "6" }, { "input": "1 1\n-3 -5", "output": "6" }, { "input": "-1 -1\n-10 100", "output": "101" }, { "input": "1 -1\n100 -100", "output":...
92
20,172,800
0
3,441
712
Memory and De-Evolution
[ "greedy", "math" ]
null
null
Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length *x*, and he wishes to perform operations to obtain an equilateral triangle of side length *y*. In a single second, he can modify the length of a single side of the current triangle suc...
The first and only line contains two integers *x* and *y* (3<=≀<=*y*<=&lt;<=*x*<=≀<=100<=000)Β β€” the starting and ending equilateral triangle side lengths respectively.
Print a single integerΒ β€” the minimum number of seconds required for Memory to obtain the equilateral triangle of side length *y* if he starts with the equilateral triangle of side length *x*.
[ "6 3\n", "8 5\n", "22 4\n" ]
[ "4\n", "3\n", "6\n" ]
In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides *a*, *b*, and *c* as (*a*, *b*, *c*). Then, Memory can do <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/18af21f738bad490df83097a90e1f2879a4b21...
[ { "input": "6 3", "output": "4" }, { "input": "8 5", "output": "3" }, { "input": "22 4", "output": "6" }, { "input": "4 3", "output": "3" }, { "input": "57 27", "output": "4" }, { "input": "61 3", "output": "9" }, { "input": "5 4", "out...
124
0
3
3,444
979
Kuro and GCD and XOR and SUM
[ "binary search", "bitmasks", "brute force", "data structures", "dp", "dsu", "greedy", "math", "number theory", "strings", "trees" ]
null
null
Kuro is currently playing an educational game about numbers. The game focuses on the greatest common divisor (GCD), the XOR value, and the sum of two numbers. Kuro loves the game so much that he solves levels by levels day by day. Sadly, he's going on a vacation for a day, and he isn't able to continue his solving str...
The first line contains one integer $q$ ($2 \leq q \leq 10^{5}$) β€” the number of tasks the game wants you to perform. $q$ lines follow, each line begins with an integer $t_i$ β€” the type of the task: - If $t_i = 1$, an integer $u_i$ follow ($1 \leq u_i \leq 10^{5}$) β€” you have to add $u_i$ to the array $a$. - If $t...
For each task of type $2$, output on one line the desired number $v$, or -1 if no such numbers are found.
[ "5\n1 1\n1 2\n2 1 1 3\n2 1 1 2\n2 1 1 1\n", "10\n1 9\n2 9 9 22\n2 3 3 18\n1 25\n2 9 9 20\n2 25 25 14\n1 20\n2 26 26 3\n1 14\n2 20 20 9\n" ]
[ "2\n1\n-1\n", "9\n9\n9\n-1\n-1\n-1\n" ]
In the first example, there are 5 tasks: - The first task requires you to add $1$ into $a$. $a$ is now $\left\{1\right\}$. - The second task requires you to add $2$ into $a$. $a$ is now $\left\{1, 2\right\}$. - The third task asks you a question with $x = 1$, $k = 1$ and $s = 3$. Taking both $1$ and $2$ as $v$ sat...
[]
2,000
104,243,200
0
3,451
171
Star
[ "*special", "combinatorics" ]
null
null
The input contains a single integer *a* (1<=≀<=*a*<=≀<=18257).
Print a single integer *output* (1<=≀<=*output*<=≀<=2Β·109).
[ "2\n" ]
[ "13" ]
none
[ { "input": "2", "output": "13" }, { "input": "1", "output": "1" }, { "input": "3", "output": "37" }, { "input": "4", "output": "73" }, { "input": "5", "output": "121" }, { "input": "6", "output": "181" }, { "input": "7", "output": "253"...
248
0
3
3,453
607
Zuma
[ "dp" ]
null
null
Genos recently installed the game Zuma on his phone. In Zuma there exists a line of *n* gemstones, the *i*-th of which has color *c**i*. The goal of the game is to destroy all the gemstones in the line as quickly as possible. In one second, Genos is able to choose exactly one continuous substring of colored gemstones ...
The first line of input contains a single integer *n* (1<=≀<=*n*<=≀<=500)Β β€” the number of gemstones. The second line contains *n* space-separated integers, the *i*-th of which is *c**i* (1<=≀<=*c**i*<=≀<=*n*)Β β€” the color of the *i*-th gemstone in a line.
Print a single integerΒ β€” the minimum number of seconds needed to destroy the entire line.
[ "3\n1 2 1\n", "3\n1 2 3\n", "7\n1 4 4 2 3 2 1\n" ]
[ "1\n", "3\n", "2\n" ]
In the first sample, Genos can destroy the entire line in one second. In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds. In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 ...
[ { "input": "3\n1 2 1", "output": "1" }, { "input": "3\n1 2 3", "output": "3" }, { "input": "7\n1 4 4 2 3 2 1", "output": "2" }, { "input": "1\n1", "output": "1" }, { "input": "2\n1 1", "output": "1" }, { "input": "2\n1 2", "output": "2" }, { ...
2,000
5,120,000
0
3,457
346
Lucky Common Subsequence
[ "dp", "strings" ]
null
null
In mathematics, a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. For example, the sequence BDF is a subsequence of ABCDEF. A substring of a string is a continuous subsequence of the string. For example, BCD is a substri...
The input contains three strings in three separate lines: *s*1, *s*2 and *virus* (1<=≀<=|*s*1|,<=|*s*2|,<=|*virus*|<=≀<=100). Each string consists only of uppercase English letters.
Output the longest common subsequence of *s*1 and *s*2 without *virus* as a substring. If there are multiple answers, any of them will be accepted. If there is no valid common subsequence, output 0.
[ "AJKEQSLOBSROFGZ\nOVGURWZLWVLUXTH\nOZ\n", "AA\nA\nA\n" ]
[ "ORZ\n", "0\n" ]
none
[ { "input": "AJKEQSLOBSROFGZ\nOVGURWZLWVLUXTH\nOZ", "output": "ORZ" }, { "input": "AA\nA\nA", "output": "0" }, { "input": "PWBJTZPQHA\nZJMKLWSROQ\nUQ", "output": "WQ" }, { "input": "QNHRPFYMAAPJDUHBAEXNEEZSTMYHVGQPYKNMVKMBVSVLIYGUVMJHEFLJEPIWFHSLISTGOKRXNMSCXYKMAXBPKCOCNTIRPCU...
62
0
0
3,459
776
Molly's Chemicals
[ "binary search", "brute force", "data structures", "implementation", "math" ]
null
null
Molly Hooper has *n* different kinds of chemicals arranged in a line. Each of the chemicals has an affection value, The *i*-th of them has affection value *a**i*. Molly wants Sherlock to fall in love with her. She intends to do this by mixing a contiguous segment of chemicals together to make a love potion with total ...
The first line of input contains two integers, *n* and *k*, the number of chemicals and the number, such that the total affection value is a non-negative power of this number *k*. (1<=≀<=*n*<=≀<=105, 1<=≀<=|*k*|<=≀<=10). Next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=≀<=*a**i*<=≀<=109)Β β€” affecti...
Output a single integerΒ β€” the number of valid segments.
[ "4 2\n2 2 2 2\n", "4 -3\n3 -6 -3 12\n" ]
[ "8\n", "3\n" ]
Do keep in mind that *k*<sup class="upper-index">0</sup> = 1. In the first sample, Molly can get following different affection values: - 2: segments [1, 1], [2, 2], [3, 3], [4, 4]; - 4: segments [1, 2], [2, 3], [3, 4]; - 6: segments [1, 3], [2, 4]; - 8: segments [1, 4]. Out of these, 2, 4 and 8 are powers of *k* = ...
[ { "input": "4 2\n2 2 2 2", "output": "8" }, { "input": "4 -3\n3 -6 -3 12", "output": "3" }, { "input": "14 -9\n-2 -4 62 53 90 41 35 21 85 74 85 57 10 39", "output": "0" }, { "input": "20 9\n90 21 -6 -61 14 -21 -17 -65 -84 -75 -48 56 67 -50 16 65 -79 -61 92 85", "output": ...
2,500
63,488,000
0
3,461
0
none
[ "none" ]
null
null
Vasya became interested in bioinformatics. He's going to write an article about similar cyclic DNA sequences, so he invented a new method for determining the similarity of cyclic sequences. Let's assume that strings *s* and *t* have the same length *n*, then the function *h*(*s*,<=*t*) is defined as the number of posi...
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=105). The second line of the input contains a single string of length *n*, consisting of characters "ACGT".
Print a single numberΒ β€” the answer modulo 109<=+<=7.
[ "1\nC\n", "2\nAG\n", "3\nTTT\n" ]
[ "1\n", "4\n", "1\n" ]
Please note that if for two distinct strings *t*<sub class="lower-index">1</sub> and *t*<sub class="lower-index">2</sub> values ρ(*s*, *t*<sub class="lower-index">1</sub>) ΠΈ ρ(*s*, *t*<sub class="lower-index">2</sub>) are maximum among all possible *t*, then both strings must be taken into account in the answer even if...
[ { "input": "1\nC", "output": "1" }, { "input": "2\nAG", "output": "4" }, { "input": "3\nTTT", "output": "1" }, { "input": "4\nGACT", "output": "256" }, { "input": "1\nT", "output": "1" }, { "input": "2\nAG", "output": "4" }, { "input": "3\n...
0
0
-1
3,463
39
Cubical Planet
[ "math" ]
D. Cubical Planet
2
64
You can find anything whatsoever in our Galaxy! A cubical planet goes round an icosahedral star. Let us introduce a system of axes so that the edges of the cubical planet are parallel to the coordinate axes and two opposite vertices lay in the points (0,<=0,<=0) and (1,<=1,<=1). Two flies live on the planet. At the mom...
The first line contains three space-separated integers (0 or 1) β€” the coordinates of the first fly, the second line analogously contains the coordinates of the second fly.
Output "YES" (without quotes) if the flies see each other. Otherwise, output "NO".
[ "0 0 0\n0 1 0\n", "1 1 0\n0 1 0\n", "0 0 0\n1 1 1\n" ]
[ "YES\n", "YES\n", "NO\n" ]
none
[ { "input": "0 0 0\n0 1 0", "output": "YES" }, { "input": "1 1 0\n0 1 0", "output": "YES" }, { "input": "0 0 0\n1 1 1", "output": "NO" }, { "input": "0 0 0\n1 0 0", "output": "YES" }, { "input": "0 0 0\n0 1 0", "output": "YES" }, { "input": "0 0 0\n1 1 ...
218
0
3.9455
3,467
936
Save Energy!
[ "binary search", "implementation", "math" ]
null
null
Julia is going to cook a chicken in the kitchen of her dormitory. To save energy, the stove in the kitchen automatically turns off after *k* minutes after turning on. During cooking, Julia goes to the kitchen every *d* minutes and turns on the stove if it is turned off. While the cooker is turned off, it stays warm. T...
The single line contains three integers *k*, *d* and *t* (1<=≀<=*k*,<=*d*,<=*t*<=≀<=1018).
Print a single number, the total time of cooking in minutes. The relative or absolute error must not exceed 10<=-<=9. Namely, let's assume that your answer is *x* and the answer of the jury is *y*. The checker program will consider your answer correct if .
[ "3 2 6\n", "4 2 20\n" ]
[ "6.5\n", "20.0\n" ]
In the first example, the chicken will be cooked for 3 minutes on the turned on stove, after this it will be cooked for <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/cce5d3f2f46552034d5ae5d487725705429ec7a5.png" style="max-width: 100.0%;max-height: 100.0%;"/>. Then the chicken will be coo...
[ { "input": "3 2 6", "output": "6.5" }, { "input": "4 2 20", "output": "20.0" }, { "input": "8 10 9", "output": "10.0" }, { "input": "43 50 140", "output": "150.5" }, { "input": "251 79 76", "output": "76.0" }, { "input": "892 67 1000", "output": "1...
77
0
0
3,473
961
Tufurama
[ "data structures" ]
null
null
One day Polycarp decided to rewatch his absolute favourite episode of well-known TV series "Tufurama". He was pretty surprised when he got results only for season 7 episode 3 with his search query of "Watch Tufurama season 3 episode 7 online full hd free". This got Polycarp confused β€” what if he decides to rewatch the ...
The first line contains one integer *n* (1<=<=≀<=*n*<=<=≀<=<=2Β·105) β€” the number of seasons. The second line contains *n* integers separated by space *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109) β€” number of episodes in each season.
Print one integer β€” the number of pairs *x* and *y* (*x*<=&lt;<=*y*) such that there exist both season *x* episode *y* and season *y* episode *x*.
[ "5\n1 2 3 4 5\n", "3\n8 12 7\n", "3\n3 2 1\n" ]
[ "0\n", "3\n", "2\n" ]
Possible pairs in the second example: 1. *x* = 1, *y* = 2 (season 1 episode 2 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/8774ca35b6e628888a4670e4539d47857e5e5841.png" style="max-width: 100.0%;max-height: 100.0%;"/> season 2 episode 1); 1. *x* = 2, *y* = 3 (season 2 episode 3 <img al...
[ { "input": "5\n1 2 3 4 5", "output": "0" }, { "input": "3\n8 12 7", "output": "3" }, { "input": "3\n3 2 1", "output": "2" }, { "input": "5\n2 3 4 5 6", "output": "4" }, { "input": "8\n7 2 6 6 5 1 4 9", "output": "9" }, { "input": "10\n1000000000 100000...
655
22,528,000
3
3,474
417
Football
[ "constructive algorithms", "graphs", "implementation" ]
null
null
One day, at the "Russian Code Cup" event it was decided to play football as an out of competition event. All participants was divided into *n* teams and played several matches, two teams could not play against each other more than once. The appointed Judge was the most experienced member β€” Pavel. But since he was the ...
The first line contains two integers β€” *n* and *k* (1<=≀<=*n*,<=*k*<=≀<=1000).
In the first line print an integer *m* β€” number of the played games. The following *m* lines should contain the information about all the matches, one match per line. The *i*-th line should contain two integers *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*; *a**i*<=β‰ <=*b**i*). The numbers *a**i* and *b**i* mean, tha...
[ "3 1\n" ]
[ "3\n1 2\n2 3\n3 1\n" ]
none
[ { "input": "3 1", "output": "3\n1 2\n2 3\n3 1" }, { "input": "7 3", "output": "21\n1 2\n1 3\n1 4\n2 3\n2 4\n2 5\n3 4\n3 5\n3 6\n4 5\n4 6\n4 7\n5 6\n5 7\n5 1\n6 7\n6 1\n6 2\n7 1\n7 2\n7 3" }, { "input": "4 1", "output": "4\n1 2\n2 3\n3 4\n4 1" }, { "input": "5 2", "output"...
1,000
307,200
0
3,475
427
Prison Transfer
[ "data structures", "implementation" ]
null
null
The prison of your city has *n* prisoners. As the prison can't accommodate all of them, the city mayor has decided to transfer *c* of the prisoners to a prison located in another city. For this reason, he made the *n* prisoners to stand in a line, with a number written on their chests. The number is the severity of th...
The first line of input will contain three space separated integers *n*Β (1<=≀<=*n*<=≀<=2Β·105), *t*Β (0<=≀<=*t*<=≀<=109) and *c*Β (1<=≀<=*c*<=≀<=*n*). The next line will contain *n* space separated integers, the *i**th* integer is the severity *i**th* prisoner's crime. The value of crime severities will be non-negative an...
Print a single integer β€” the number of ways you can choose the *c* prisoners.
[ "4 3 3\n2 3 1 1\n", "1 1 1\n2\n", "11 4 2\n2 2 0 7 3 2 2 4 9 1 4\n" ]
[ "2\n", "0\n", "6\n" ]
none
[ { "input": "4 3 3\n2 3 1 1", "output": "2" }, { "input": "1 1 1\n2", "output": "0" }, { "input": "11 4 2\n2 2 0 7 3 2 2 4 9 1 4", "output": "6" }, { "input": "57 2 10\n7 5 2 7 4 1 0 5 2 9 2 9 8 6 6 5 9 6 8 1 0 1 0 3 2 6 5 2 8 8 8 8 0 9 4 3 6 6 2 4 5 1 2 0 1 7 1 1 5 4 5 0 7 5 ...
0
0
-1
3,486
39
Pacifist frogs
[ "implementation" ]
F. Pacifist frogs
2
64
Thumbelina has had an accident. She has found herself on a little island in the middle of a swamp and wants to get to the shore very much. One can get to the shore only by hills that are situated along a straight line that connects the little island with the shore. Let us assume that the hills are numbered from 1 to *...
The first line contains three integers *n*, *m* and *k* (1<=≀<=*n*<=≀<=109, 1<=≀<=*m*,<=*k*<=≀<=100) β€” the number of hills, frogs and mosquitoes respectively. The second line contains *m* integers *d**i* (1<=≀<=*d**i*<=≀<=109) β€” the lengths of the frogs’ jumps. The third line contains *k* integers β€” the numbers of the ...
In the first line output the number of frogs that smash the minimal number of mosquitoes, in the second line β€” their numbers in increasing order separated by spaces. The frogs are numbered from 1 to *m* in the order of the jump length given in the input data.
[ "5 3 5\n2 3 4\n1 2 3 4 5\n", "1000000000 2 3\n2 5\n999999995 999999998 999999996\n" ]
[ "2\n2 3\n", "1\n2\n" ]
none
[ { "input": "5 3 5\n2 3 4\n1 2 3 4 5", "output": "2\n2 3" }, { "input": "1000000000 2 3\n2 5\n999999995 999999998 999999996", "output": "1\n2" }, { "input": "1 1 1\n1\n1", "output": "1\n1" }, { "input": "2 2 1\n2 1\n1", "output": "1\n1" }, { "input": "3 2 2\n2 4\n3...
154
2,150,400
3.945478
3,488
269
Greenhouse Effect
[ "dp" ]
null
null
Emuskald is an avid horticulturist and owns the world's longest greenhouse β€” it is effectively infinite in length. Over the years Emuskald has cultivated *n* plants in his greenhouse, of *m* different plant species numbered from 1 to *m*. His greenhouse is very narrow and can be viewed as an infinite line, with each p...
The first line of input contains two space-separated integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=5000, *n*<=β‰₯<=*m*), the number of plants and the number of different species. Each of the following *n* lines contain two space-separated numbers: one integer number *s**i* (1<=≀<=*s**i*<=≀<=*m*), and one real number *x**i* (...
Output a single integer β€” the minimum number of plants to be replanted.
[ "3 2\n2 1\n1 2.0\n1 3.100\n", "3 3\n1 5.0\n2 5.5\n3 6.0\n", "6 3\n1 14.284235\n2 17.921382\n1 20.328172\n3 20.842331\n1 25.790145\n1 27.204125\n" ]
[ "1\n", "0\n", "2\n" ]
In the first test case, Emuskald can replant the first plant to the right of the last plant, so the answer is 1. In the second test case, the species are already in the correct order, so no replanting is needed.
[ { "input": "3 2\n2 1\n1 2.0\n1 3.100", "output": "1" }, { "input": "3 3\n1 5.0\n2 5.5\n3 6.0", "output": "0" }, { "input": "6 3\n1 14.284235\n2 17.921382\n1 20.328172\n3 20.842331\n1 25.790145\n1 27.204125", "output": "2" }, { "input": "1 1\n1 0", "output": "0" }, { ...
30
0
-1
3,496
346
Alice and Bob
[ "games", "math", "number theory" ]
null
null
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of *n* distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two ...
The first line contains an integer *n* (2<=≀<=*n*<=≀<=100) β€” the initial number of elements in the set. The second line contains *n* distinct space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109) β€” the elements of the set.
Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes).
[ "2\n2 3\n", "2\n5 3\n", "3\n5 6 7\n" ]
[ "Alice\n", "Alice\n", "Bob\n" ]
Consider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice.
[ { "input": "2\n2 3", "output": "Alice" }, { "input": "2\n5 3", "output": "Alice" }, { "input": "3\n5 6 7", "output": "Bob" }, { "input": "10\n72 96 24 66 6 18 12 30 60 48", "output": "Bob" }, { "input": "10\n78 66 6 60 18 84 36 96 72 48", "output": "Bob" }, ...
186
0
0
3,501
707
Pythagorean Triples
[ "math", "number theory" ]
null
null
Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples. For example, tripl...
The only line of the input contains single integer *n* (1<=≀<=*n*<=≀<=109)Β β€” the length of some side of a right triangle.
Print two integers *m* and *k* (1<=≀<=*m*,<=*k*<=≀<=1018), such that *n*, *m* and *k* form a Pythagorean triple, in the only line. In case if there is no any Pythagorean triple containing integer *n*, print <=-<=1 in the only line. If there are many answers, print any of them.
[ "3\n", "6\n", "1\n", "17\n", "67\n" ]
[ "4 5", "8 10", "-1", "144 145", "2244 2245" ]
Illustration for the first sample.
[ { "input": "3", "output": "4 5" }, { "input": "6", "output": "8 10" }, { "input": "1", "output": "-1" }, { "input": "17", "output": "144 145" }, { "input": "67", "output": "2244 2245" }, { "input": "10", "output": "24 26" }, { "input": "14"...
62
0
3
3,502
229
Planets
[ "binary search", "data structures", "graphs", "shortest paths" ]
null
null
Goa'uld Apophis captured Jack O'Neill's team again! Jack himself was able to escape, but by that time Apophis's ship had already jumped to hyperspace. But Jack knows on what planet will Apophis land. In order to save his friends, Jack must repeatedly go through stargates to get to this planet. Overall the galaxy has *...
The first line contains two space-separated integers: *n* (2<=≀<=*n*<=≀<=105), the number of planets in the galaxy, and *m* (0<=≀<=*m*<=≀<=105) β€” the number of pairs of planets between which Jack can travel using stargates. Then *m* lines follow, containing three integers each: the *i*-th line contains numbers of plane...
Print a single number β€” the least amount of time Jack needs to get from planet 1 to planet *n*. If Jack can't get to planet *n* in any amount of time, print number -1.
[ "4 6\n1 2 2\n1 3 3\n1 4 8\n2 3 4\n2 4 5\n3 4 3\n0\n1 3\n2 3 4\n0\n", "3 1\n1 2 3\n0\n1 3\n0\n" ]
[ "7\n", "-1\n" ]
In the first sample Jack has three ways to go from planet 1. If he moves to planet 4 at once, he spends 8 seconds. If he transfers to planet 3, he spends 3 seconds, but as other travellers arrive to planet 3 at time 3 and 4, he can travel to planet 4 only at time 5, thus spending 8 seconds in total. But if Jack moves t...
[ { "input": "4 6\n1 2 2\n1 3 3\n1 4 8\n2 3 4\n2 4 5\n3 4 3\n0\n1 3\n2 3 4\n0", "output": "7" }, { "input": "3 1\n1 2 3\n0\n1 3\n0", "output": "-1" }, { "input": "2 1\n1 2 3\n0\n1 3", "output": "3" }, { "input": "2 1\n1 2 3\n1 0\n0", "output": "4" }, { "input": "3 3...
2,000
19,968,000
0
3,507
813
The Tag Game
[ "dfs and similar", "graphs" ]
null
null
Alice got tired of playing the tag game by the usual rules so she offered Bob a little modification to it. Now the game should be played on an undirected rooted tree of *n* vertices. Vertex 1 is the root of the tree. Alice starts at vertex 1 and Bob starts at vertex *x* (*x*<=β‰ <=1). The moves are made in turns, Bob go...
The first line contains two integer numbers *n* and *x* (2<=≀<=*n*<=≀<=2Β·105, 2<=≀<=*x*<=≀<=*n*). Each of the next *n*<=-<=1 lines contains two integer numbers *a* and *b* (1<=≀<=*a*,<=*b*<=≀<=*n*) β€” edges of the tree. It is guaranteed that the edges form a valid tree.
Print the total number of moves Alice and Bob will make.
[ "4 3\n1 2\n2 3\n2 4\n", "5 2\n1 2\n2 3\n3 4\n2 5\n" ]
[ "4\n", "6\n" ]
In the first example the tree looks like this: The red vertex is Alice's starting position, the blue one is Bob's. Bob will make the game run the longest by standing at the vertex 3 during all the game. So here are the moves: B: stay at vertex 3 A: go to vertex 2 B: stay at vertex 3 A: go to vertex 3 In the secon...
[ { "input": "4 3\n1 2\n2 3\n2 4", "output": "4" }, { "input": "5 2\n1 2\n2 3\n3 4\n2 5", "output": "6" }, { "input": "2 2\n2 1", "output": "2" }, { "input": "3 3\n2 1\n3 1", "output": "2" }, { "input": "3 3\n1 2\n3 2", "output": "4" }, { "input": "10 4\...
62
0
0
3,512
490
Queue
[ "dsu", "implementation" ]
null
null
During the lunch break all *n* Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working. Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID ...
The first line contains integer *n* (2<=≀<=*n*<=≀<=2Β·105) β€” the number of students in the queue. Then *n* lines follow, *i*-th line contains the pair of integers *a**i*,<=*b**i* (0<=≀<=*a**i*,<=*b**i*<=≀<=106), where *a**i* is the ID number of a person in front of a student and *b**i* is the ID number of a person beh...
Print a sequence of *n* integers *x*1,<=*x*2,<=...,<=*x**n* β€” the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one.
[ "4\n92 31\n0 7\n31 0\n7 141\n" ]
[ "92 7 31 141 \n" ]
The picture illustrates the queue for the first sample.
[ { "input": "4\n92 31\n0 7\n31 0\n7 141", "output": "92 7 31 141 " }, { "input": "2\n0 1\n2 0", "output": "2 1 " }, { "input": "3\n0 2\n1 3\n2 0", "output": "1 2 3 " }, { "input": "4\n101 0\n0 102\n102 100\n103 101", "output": "103 102 101 100 " }, { "input": "5\n0...
218
10,444,800
-1
3,513
49
Disposition
[ "constructive algorithms", "math" ]
C. Disposition
2
256
Vasya bought the collected works of a well-known Berland poet Petya in *n* volumes. The volumes are numbered from 1 to *n*. He thinks that it does not do to arrange the book simply according to their order. Vasya wants to minimize the number of the disposition’s divisors β€” the positive integers *i* such that for at lea...
The first line contains number *n* (1<=≀<=*n*<=≀<=100000) which represents the number of volumes and free places.
Print *n* numbers β€” the sought disposition with the minimum divisor number. The *j*-th number (1<=≀<=*j*<=≀<=*n*) should be equal to *p*(*j*) β€” the number of tome that stands on the *j*-th place. If there are several solutions, print any of them.
[ "2\n", "3\n" ]
[ "2 1 \n", "1 3 2 \n" ]
none
[ { "input": "2", "output": "2 1 " }, { "input": "3", "output": "1 3 2 " }, { "input": "4", "output": "2 1 4 3 " }, { "input": "5", "output": "1 3 2 5 4 " }, { "input": "6", "output": "2 1 4 3 6 5 " }, { "input": "1", "output": "1 " }, { "inp...
218
12,288,000
3.922612
3,518
351
Jeff and Brackets
[ "dp", "matrices" ]
null
null
Jeff loves regular bracket sequences. Today Jeff is going to take a piece of paper and write out the regular bracket sequence, consisting of *nm* brackets. Let's number all brackets of this sequence from 0 to *nm* - 1 from left to right. Jeff knows that he is going to spend *a**i* *mod* *n* liters of ink on the *i*-th...
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=20;Β 1<=≀<=*m*<=≀<=107; *m* is even). The next line contains *n* integers: *a*0, *a*1, ..., *a**n*<=-<=1 (1<=≀<=*a**i*<=≀<=10). The next line contains *n* integers: *b*0, *b*1, ..., *b**n*<=-<=1 (1<=≀<=*b**i*<=≀<=10). The numbers are separated by spaces.
In a single line print the answer to the problem β€” the minimum required amount of ink in liters.
[ "2 6\n1 2\n2 1\n", "1 10000000\n2\n3\n" ]
[ "12\n", "25000000\n" ]
In the first test the optimal sequence is: ()()()()()(), the required number of ink liters is 12.
[ { "input": "2 6\n1 2\n2 1", "output": "12" }, { "input": "1 10000000\n2\n3", "output": "25000000" }, { "input": "3 184\n3 2 8\n3 9 2", "output": "1288" }, { "input": "4 26\n10 2 5 9\n5 4 2 5", "output": "444" }, { "input": "3 76\n4 7 9\n10 1 1", "output": "684...
62
0
0
3,521
23
Party
[ "constructive algorithms", "graphs", "math" ]
B. Party
2
256
*n* people came to a party. Then those, who had no friends among people at the party, left. Then those, who had exactly 1 friend among those who stayed, left as well. Then those, who had exactly 2,<=3,<=...,<=*n*<=-<=1 friends among those who stayed by the moment of their leaving, did the same. What is the maximum amo...
The first input line contains one number *t* β€” amount of tests (1<=≀<=*t*<=≀<=105). Each of the following *t* lines contains one integer number *n* (1<=≀<=*n*<=≀<=105).
For each test output in a separate line one number β€” the maximum amount of people that could stay in the end.
[ "1\n3\n" ]
[ "1\n" ]
none
[ { "input": "1\n3", "output": "1" } ]
1,496
0
3.626
3,522
300
Beautiful Numbers
[ "brute force", "combinatorics" ]
null
null
Vitaly is a very weird man. He's got two favorite digits *a* and *b*. Vitaly calls a positive integer good, if the decimal representation of this integer only contains digits *a* and *b*. Vitaly calls a good number excellent, if the sum of its digits is a good number. For example, let's say that Vitaly's favourite dig...
The first line contains three integers: *a*, *b*, *n* (1<=≀<=*a*<=&lt;<=*b*<=≀<=9,<=1<=≀<=*n*<=≀<=106).
Print a single integer β€” the answer to the problem modulo 1000000007 (109<=+<=7).
[ "1 3 3\n", "2 3 10\n" ]
[ "1\n", "165\n" ]
none
[ { "input": "1 3 3", "output": "1" }, { "input": "2 3 10", "output": "165" }, { "input": "6 8 14215", "output": "651581472" }, { "input": "4 9 104671", "output": "329390901" }, { "input": "6 7 78755", "output": "0" }, { "input": "1 8 265", "output":...
810
62,054,400
3
3,525
893
Chess For Three
[ "implementation" ]
null
null
Alex, Bob and Carl will soon participate in a team chess tournament. Since they are all in the same team, they have decided to practise really hard before the tournament. But it's a bit difficult for them because chess is a game for two players, not three. So they play with each other according to following rules: - ...
The first line contains one integer *n* (1<=≀<=*n*<=≀<=100) β€” the number of games Alex, Bob and Carl played. Then *n* lines follow, describing the game log. *i*-th line contains one integer *a**i* (1<=≀<=*a**i*<=≀<=3) which is equal to 1 if Alex won *i*-th game, to 2 if Bob won *i*-th game and 3 if Carl won *i*-th gam...
Print YES if the situation described in the log was possible. Otherwise print NO.
[ "3\n1\n1\n2\n", "2\n1\n2\n" ]
[ "YES\n", "NO\n" ]
In the first example the possible situation is: 1. Alex wins, Carl starts playing instead of Bob; 1. Alex wins, Bob replaces Carl; 1. Bob wins. The situation in the second example is impossible because Bob loses the first game, so he cannot win the second one.
[ { "input": "3\n1\n1\n2", "output": "YES" }, { "input": "2\n1\n2", "output": "NO" }, { "input": "100\n2\n3\n1\n2\n3\n3\n3\n1\n1\n1\n1\n3\n3\n3\n3\n1\n2\n3\n3\n3\n3\n3\n3\n3\n1\n2\n2\n2\n3\n1\n1\n3\n3\n3\n3\n3\n3\n3\n3\n1\n2\n3\n3\n3\n1\n1\n1\n1\n3\n3\n3\n3\n1\n2\n3\n1\n2\n2\n2\n3\n3\n2\n1...
46
0
3
3,533
52
123-sequence
[ "implementation" ]
A. 123-sequence
2
256
There is a given sequence of integers *a*1,<=*a*2,<=...,<=*a**n*, where every number is from 1 to 3 inclusively. You have to replace the minimum number of numbers in it so that all the numbers in the sequence are equal to each other.
The first line contains an integer *n* (1<=≀<=*n*<=≀<=106). The second line contains a sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=3).
Print the minimum number of replacements needed to be performed to make all the numbers in the sequence equal.
[ "9\n1 3 2 2 2 1 1 2 3\n" ]
[ "5\n" ]
In the example all the numbers equal to 1 and 3 should be replaced by 2.
[ { "input": "9\n1 3 2 2 2 1 1 2 3", "output": "5" }, { "input": "6\n3 3 2 2 1 3", "output": "3" }, { "input": "12\n3 1 3 1 2 1 3 2 2 1 2 1", "output": "7" }, { "input": "15\n3 2 1 1 1 1 3 2 2 3 3 1 2 3 2", "output": "10" }, { "input": "2\n2 1", "output": "1" ...
498
13,619,200
3.850132
3,549
446
DZY Loves Sequences
[ "dp", "implementation", "two pointers" ]
null
null
DZY has a sequence *a*, consisting of *n* integers. We'll call a sequence *a**i*,<=*a**i*<=+<=1,<=...,<=*a**j* (1<=≀<=*i*<=≀<=*j*<=≀<=*n*) a subsegment of the sequence *a*. The value (*j*<=-<=*i*<=+<=1) denotes the length of the subsegment. Your task is to find the longest subsegment of *a*, such that it is possible ...
The first line contains integer *n*Β (1<=≀<=*n*<=≀<=105). The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n*Β (1<=≀<=*a**i*<=≀<=109).
In a single line print the answer to the problem β€” the maximum length of the required subsegment.
[ "6\n7 2 3 1 5 6\n" ]
[ "5\n" ]
You can choose subsegment *a*<sub class="lower-index">2</sub>, *a*<sub class="lower-index">3</sub>, *a*<sub class="lower-index">4</sub>, *a*<sub class="lower-index">5</sub>, *a*<sub class="lower-index">6</sub> and change its 3rd element (that is *a*<sub class="lower-index">4</sub>) to 4.
[ { "input": "6\n7 2 3 1 5 6", "output": "5" }, { "input": "10\n424238336 649760493 681692778 714636916 719885387 804289384 846930887 957747794 596516650 189641422", "output": "9" }, { "input": "50\n804289384 846930887 681692778 714636916 957747794 424238336 719885387 649760493 596516650 1...
108
0
0
3,552
710
King Moves
[ "implementation" ]
null
null
The only king stands on the standard chess board. You are given his position in format "cd", where *c* is the column from 'a' to 'h' and *d* is the row from '1' to '8'. Find the number of moves permitted for the king. Check the king's moves here [https://en.wikipedia.org/wiki/King_(chess)](https://en.wikipedia.org/wik...
The only line contains the king's position in the format "cd", where 'c' is the column from 'a' to 'h' and 'd' is the row from '1' to '8'.
Print the only integer *x* β€” the number of moves permitted for the king.
[ "e4\n" ]
[ "8\n" ]
none
[ { "input": "e4", "output": "8" }, { "input": "a1", "output": "3" }, { "input": "h8", "output": "3" }, { "input": "a4", "output": "5" }, { "input": "g7", "output": "8" }, { "input": "e1", "output": "5" }, { "input": "b2", "output": "8" ...
46
0
3
3,555
448
Suffix Structures
[ "implementation", "strings" ]
null
null
Bizon the Champion isn't just a bison. He also is a favorite of the "Bizons" team. At a competition the "Bizons" got the following problem: "You are given two distinct words (strings of English letters), *s* and *t*. You need to transform word *s* into word *t*". The task looked simple to the guys because they know th...
The first line contains a non-empty word *s*. The second line contains a non-empty word *t*. Words *s* and *t* are different. Each word consists only of lowercase English letters. Each word contains at most 100 letters.
In the single line print the answer to the problem. Print "need tree" (without the quotes) if word *s* cannot be transformed into word *t* even with use of both suffix array and suffix automaton. Print "automaton" (without the quotes) if you need only the suffix automaton to solve the problem. Print "array" (without th...
[ "automaton\ntomat\n", "array\narary\n", "both\nhot\n", "need\ntree\n" ]
[ "automaton\n", "array\n", "both\n", "need tree\n" ]
In the third sample you can act like that: first transform "both" into "oth" by removing the first character using the suffix automaton and then make two swaps of the string using the suffix array and get "hot".
[ { "input": "automaton\ntomat", "output": "automaton" }, { "input": "array\narary", "output": "array" }, { "input": "both\nhot", "output": "both" }, { "input": "need\ntree", "output": "need tree" }, { "input": "abacaba\naaaa", "output": "automaton" }, { ...
93
0
0
3,556
691
Exponential notation
[ "implementation", "strings" ]
null
null
You are given a positive decimal number *x*. Your task is to convert it to the "simple exponential notation". Let *x*<==<=*a*Β·10*b*, where 1<=≀<=*a*<=&lt;<=10, then in general case the "simple exponential notation" looks like "aEb". If *b* equals to zero, the part "Eb" should be skipped. If *a* is an integer, it shou...
The only line contains the positive decimal number *x*. The length of the line will not exceed 106. Note that you are given too large number, so you can't use standard built-in data types "float", "double" and other.
Print the only line β€” the "simple exponential notation" of the given number *x*.
[ "16\n", "01.23400\n", ".100\n", "100.\n" ]
[ "1.6E1\n", "1.234\n", "1E-1\n", "1E2\n" ]
none
[ { "input": "16", "output": "1.6E1" }, { "input": "01.23400", "output": "1.234" }, { "input": ".100", "output": "1E-1" }, { "input": "100.", "output": "1E2" }, { "input": "9000", "output": "9E3" }, { "input": "0.0012", "output": "1.2E-3" }, { ...
62
9,011,200
3
3,557
0
none
[ "none" ]
null
null
Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0. There are also *n* cards, each card has 2 attributes: length *l**i* and cost *c**i*. If she pays *c**i* dollars then she can apply *i*-th...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=300), number of cards. The second line contains *n* numbers *l**i* (1<=≀<=*l**i*<=≀<=109), the jump lengths of cards. The third line contains *n* numbers *c**i* (1<=≀<=*c**i*<=≀<=105), the costs of cards.
If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards.
[ "3\n100 99 9900\n1 1 1\n", "5\n10 20 30 40 50\n1 1 1 1 1\n", "7\n15015 10010 6006 4290 2730 2310 1\n1 1 1 1 1 1 10\n", "8\n4264 4921 6321 6984 2316 8432 6120 1026\n4264 4921 6321 6984 2316 8432 6120 1026\n" ]
[ "2\n", "-1\n", "6\n", "7237\n" ]
In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell. In the second sample test, even if you buy all cards, you c...
[ { "input": "3\n100 99 9900\n1 1 1", "output": "2" }, { "input": "5\n10 20 30 40 50\n1 1 1 1 1", "output": "-1" }, { "input": "7\n15015 10010 6006 4290 2730 2310 1\n1 1 1 1 1 1 10", "output": "6" }, { "input": "8\n4264 4921 6321 6984 2316 8432 6120 1026\n4264 4921 6321 6984 23...
77
2,867,200
-1
3,560
757
Bash's Big Day
[ "greedy", "math", "number theory" ]
null
null
Bash has set out on a journey to become the greatest Pokemon master. To get his first Pokemon, he went to Professor Zulu's Lab. Since Bash is Professor Zulu's favourite student, Zulu allows him to take as many Pokemon from his lab as he pleases. But Zulu warns him that a group of *k*<=&gt;<=1 Pokemon with strengths {*...
The input consists of two lines. The first line contains an integer *n* (1<=≀<=*n*<=≀<=105), the number of Pokemon in the lab. The next line contains *n* space separated integers, where the *i*-th of them denotes *s**i* (1<=≀<=*s**i*<=≀<=105), the strength of the *i*-th Pokemon.
Print single integerΒ β€” the maximum number of Pokemons Bash can take.
[ "3\n2 3 4\n", "5\n2 3 4 6 7\n" ]
[ "2\n", "3\n" ]
*gcd* (greatest common divisor) of positive integers set {*a*<sub class="lower-index">1</sub>, *a*<sub class="lower-index">2</sub>, ..., *a*<sub class="lower-index">*n*</sub>} is the maximum positive integer that divides all the integers {*a*<sub class="lower-index">1</sub>, *a*<sub class="lower-index">2</sub>, ..., *a...
[ { "input": "3\n2 3 4", "output": "2" }, { "input": "5\n2 3 4 6 7", "output": "3" }, { "input": "3\n5 6 4", "output": "2" }, { "input": "8\n41 74 4 27 85 39 100 36", "output": "4" }, { "input": "6\n89 20 86 81 62 23", "output": "3" }, { "input": "71\n23...
1,419
13,209,600
3
3,564
452
Eevee
[ "brute force", "implementation", "strings" ]
null
null
You are solving the crossword problem K from IPSC 2014. You solved all the clues except for one: who does Eevee evolve into? You are not very into pokemons, but quick googling helped you find out, that Eevee can evolve into eight different pokemons: Vaporeon, Jolteon, Flareon, Espeon, Umbreon, Leafeon, Glaceon, and Syl...
First line contains an integer *n* (6<=≀<=*n*<=≀<=8) – the length of the string. Next line contains a string consisting of *n* characters, each of which is either a lower case english letter (indicating a known letter) or a dot character (indicating an empty cell in the crossword).
Print a name of the pokemon that Eevee can evolve into that matches the pattern in the input. Use lower case letters only to print the name (in particular, do not capitalize the first letter).
[ "7\nj......\n", "7\n...feon\n", "7\n.l.r.o.\n" ]
[ "jolteon\n", "leafeon\n", "flareon\n" ]
Here's a set of names in a form you can paste into your solution: ["vaporeon", "jolteon", "flareon", "espeon", "umbreon", "leafeon", "glaceon", "sylveon"] {"vaporeon", "jolteon", "flareon", "espeon", "umbreon", "leafeon", "glaceon", "sylveon"}
[ { "input": "7\n...feon", "output": "leafeon" }, { "input": "7\n.l.r.o.", "output": "flareon" }, { "input": "6\n.s..o.", "output": "espeon" }, { "input": "7\nglaceon", "output": "glaceon" }, { "input": "8\n.a.o.e.n", "output": "vaporeon" }, { "input": "...
46
0
0
3,575
625
K-special Tables
[ "constructive algorithms", "implementation" ]
null
null
People do many crazy things to stand out in a crowd. Some of them dance, some learn by heart rules of Russian language, some try to become an outstanding competitive programmers, while others collect funny math objects. Alis is among these collectors. Right now she wants to get one of *k*-special tables. In case you f...
The first line of the input contains two integers *n* and *k* (1<=≀<=*n*<=≀<=500,<=1<=≀<=*k*<=≀<=*n*)Β β€” the size of the table Alice is looking for and the column that should have maximum possible sum.
First print the sum of the integers in the *k*-th column of the required table. Next *n* lines should contain the description of the table itself: first line should contains *n* elements of the first row, second line should contain *n* elements of the second row and so on. If there are multiple suitable table, you ar...
[ "4 1\n", "5 3\n" ]
[ "28\n1 2 3 4\n5 6 7 8\n9 10 11 12\n13 14 15 16\n", "85\n5 6 17 18 19\n9 10 23 24 25\n7 8 20 21 22\n3 4 14 15 16\n1 2 11 12 13\n\n" ]
none
[ { "input": "4 1", "output": "28\n1 2 3 4\n5 6 7 8\n9 10 11 12\n13 14 15 16" }, { "input": "5 3", "output": "85\n1 2 11 12 13\n3 4 14 15 16\n5 6 17 18 19\n7 8 20 21 22\n9 10 23 24 25" }, { "input": "1 1", "output": "1\n1" }, { "input": "2 1", "output": "4\n1 2\n3 4" }, ...
358
7,680,000
3
3,581
171
A Piece of Cake
[ "*special", "implementation" ]
null
null
How to make a cake you'll never eat. Ingredients. - 2 carrots - 0 calories - 100 g chocolate spread - 1 pack of flour - 1 egg Method. 1. Put calories into the mixing bowl. 1. Take carrots from refrigerator. 1. Chop carrots. 1. Take chocolate spread from refrigerator. 1. Put chocolate spread into the mix...
The only line of input contains a sequence of integers *a*0,<=*a*1,<=... (1<=≀<=*a*0<=≀<=100, 0<=≀<=*a**i*<=≀<=1000 for *i*<=β‰₯<=1).
Output a single integer.
[ "4 1 2 3 4\n" ]
[ "30\n" ]
none
[ { "input": "4 1 2 3 4", "output": "30" }, { "input": "4 802 765 992 1", "output": "5312" }, { "input": "4 220 380 729 969", "output": "7043" }, { "input": "3 887 104 641", "output": "3018" }, { "input": "12 378 724 582 387 583 241 294 159 198 653 369 418", "ou...
62
0
3
3,589
1,010
Fly
[ "binary search", "math" ]
null
null
Natasha is going to fly on a rocket to Mars and return to Earth. Also, on the way to Mars, she will land on $n - 2$ intermediate planets. Formally: we number all the planets from $1$ to $n$. $1$ is Earth, $n$ is Mars. Natasha will make exactly $n$ flights: $1 \to 2 \to \ldots n \to 1$. Flight from $x$ to $y$ consists ...
The first line contains a single integer $n$ ($2 \le n \le 1000$)Β β€” number of planets. The second line contains the only integer $m$ ($1 \le m \le 1000$)Β β€” weight of the payload. The third line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 1000$), where $a_i$ is the number of tons, which can be lifted...
If Natasha can fly to Mars through $(n - 2)$ planets and return to Earth, print the minimum mass of fuel (in tons) that Natasha should take. Otherwise, print a single number $-1$. It is guaranteed, that if Natasha can make a flight, then it takes no more than $10^9$ tons of fuel. The answer will be considered correct...
[ "2\n12\n11 8\n7 5\n", "3\n1\n1 4 1\n2 5 3\n", "6\n2\n4 6 3 3 5 6\n2 6 3 6 5 3\n" ]
[ "10.0000000000\n", "-1\n", "85.4800000000\n" ]
Let's consider the first example. Initially, the mass of a rocket with fuel is $22$ tons. - At take-off from Earth one ton of fuel can lift off $11$ tons of cargo, so to lift off $22$ tons you need to burn $2$ tons of fuel. Remaining weight of the rocket with fuel is $20$ tons.- During landing on Mars, one ton of fu...
[ { "input": "2\n12\n11 8\n7 5", "output": "10.0000000000" }, { "input": "3\n1\n1 4 1\n2 5 3", "output": "-1" }, { "input": "6\n2\n4 6 3 3 5 6\n2 6 3 6 5 3", "output": "85.4800000000" }, { "input": "3\n3\n1 2 1\n2 2 2", "output": "-1" }, { "input": "4\n4\n2 3 2 2\n2...
77
1,843,200
3
3,615
698
LRU
[ "bitmasks", "dp", "math", "probabilities" ]
null
null
While creating high loaded systems one should pay a special attention to caching. This problem will be about one of the most popular caching algorithms called LRU (Least Recently Used). Suppose the cache may store no more than *k* objects. At the beginning of the workflow the cache is empty. When some object is querie...
The first line of the input contains two integers *n* and *k* (1<=≀<=*k*<=≀<=*n*<=≀<=20)Β β€” the number of videos and the size of the cache respectively. Next line contains *n* real numbers *p**i* (0<=≀<=*p**i*<=≀<=1), each of them is given with no more than two digits after decimal point. It's guaranteed that the sum o...
Print *n* real numbers, the *i*-th of them should be equal to the probability that the *i*-th video will be present in the cache after 10100 queries. You answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. Namely: let's assume that your answer is *a*, and the answer of the ju...
[ "3 1\n0.3 0.2 0.5\n", "2 1\n0.0 1.0\n", "3 2\n0.3 0.2 0.5\n", "3 3\n0.2 0.3 0.5\n" ]
[ "0.3 0.2 0.5 ", "0.0 1.0 ", "0.675 0.4857142857142857 0.8392857142857143 ", "1.0 1.0 1.0 " ]
none
[ { "input": "3 1\n0.3 0.2 0.5", "output": "0.3 0.2 0.5 " }, { "input": "2 1\n0.0 1.0", "output": "0.0 1.0 " }, { "input": "3 2\n0.3 0.2 0.5", "output": "0.675 0.4857142857142857 0.8392857142857143 " }, { "input": "3 3\n0.2 0.3 0.5", "output": "1.0 1.0 1.0 " }, { "i...
77
2,867,200
-1
3,627
296
Yaroslav and Two Strings
[ "combinatorics", "dp" ]
null
null
Yaroslav thinks that two strings *s* and *w*, consisting of digits and having length *n* are non-comparable if there are two numbers, *i* and *j* (1<=≀<=*i*,<=*j*<=≀<=*n*), such that *s**i*<=&gt;<=*w**i* and *s**j*<=&lt;<=*w**j*. Here sign *s**i* represents the *i*-th digit of string *s*, similarly, *w**j* represents t...
The first line contains integer *n* (1<=≀<=*n*<=≀<=105) β€” the length of both templates. The second line contains the first template β€” a string that consists of digits and characters "?". The string's length equals *n*. The third line contains the second template in the same format.
In a single line print the remainder after dividing the answer to the problem by number 1000000007 (109<=+<=7).
[ "2\n90\n09\n", "2\n11\n55\n", "5\n?????\n?????\n" ]
[ "1\n", "0\n", "993531194\n" ]
The first test contains no question marks and both strings are incomparable, so the answer is 1. The second test has no question marks, but the given strings are comparable, so the answer is 0.
[ { "input": "2\n90\n09", "output": "1" }, { "input": "2\n11\n55", "output": "0" }, { "input": "5\n?????\n?????", "output": "993531194" }, { "input": "10\n104?3?1??3\n?1755?1??7", "output": "91015750" }, { "input": "10\n6276405116\n6787?352?9", "output": "46" ...
92
5,529,600
0
3,629
681
Heap Operations
[ "constructive algorithms", "data structures", "greedy" ]
null
null
Petya has recently learned data structure named "Binary heap". The heap he is now operating with allows the following operations: - put the given number into the heap; - get the value of the minimum element in the heap; - extract the minimum element from the heap; Thus, at any moment of time the heap contains se...
The first line of the input contains the only integer *n* (1<=≀<=*n*<=≀<=100<=000)Β β€” the number of the records left in Petya's journal. Each of the following *n* lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers...
The first line of the output should contain a single integer *m*Β β€” the minimum possible number of records in the modified sequence of operations. Next *m* lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applie...
[ "2\ninsert 3\ngetMin 4\n", "4\ninsert 1\ninsert 1\nremoveMin\ngetMin 2\n" ]
[ "4\ninsert 3\nremoveMin\ninsert 4\ngetMin 4\n", "6\ninsert 1\ninsert 1\nremoveMin\nremoveMin\ninsert 2\ngetMin 2\n" ]
In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap. In the second sample case number 1 is inserted two times, so should be similarly removed twice.
[ { "input": "2\ninsert 3\ngetMin 4", "output": "4\ninsert 3\nremoveMin\ninsert 4\ngetMin 4" }, { "input": "4\ninsert 1\ninsert 1\nremoveMin\ngetMin 2", "output": "6\ninsert 1\ninsert 1\nremoveMin\nremoveMin\ninsert 2\ngetMin 2" }, { "input": "1\ninsert 1", "output": "1\ninsert 1" },...
46
0
0
3,631
868
Bark to Unlock
[ "brute force", "implementation", "strings" ]
null
null
As technologies develop, manufacturers are making the process of unlocking a phone as user-friendly as possible. To unlock its new phone, Arkady's pet dog Mu-mu has to bark the password once. The phone represents a password as a string of two lowercase English letters. Mu-mu's enemy Kashtanka wants to unlock Mu-mu's p...
The first line contains two lowercase English lettersΒ β€” the password on the phone. The second line contains single integer *n* (1<=≀<=*n*<=≀<=100)Β β€” the number of words Kashtanka knows. The next *n* lines contain two lowercase English letters each, representing the words Kashtanka knows. The words are guaranteed to b...
Print "YES" if Kashtanka can bark several words in a line forming a string containing the password, and "NO" otherwise. You can print each letter in arbitrary case (upper or lower).
[ "ya\n4\nah\noy\nto\nha\n", "hp\n2\nht\ntp\n", "ah\n1\nha\n" ]
[ "YES\n", "NO\n", "YES\n" ]
In the first example the password is "ya", and Kashtanka can bark "oy" and then "ah", and then "ha" to form the string "oyahha" which contains the password. So, the answer is "YES". In the second example Kashtanka can't produce a string containing password as a substring. Note that it can bark "ht" and then "tp" produ...
[ { "input": "ya\n4\nah\noy\nto\nha", "output": "YES" }, { "input": "hp\n2\nht\ntp", "output": "NO" }, { "input": "ah\n1\nha", "output": "YES" }, { "input": "bb\n4\nba\nab\naa\nbb", "output": "YES" }, { "input": "bc\n4\nca\nba\nbb\ncc", "output": "YES" }, { ...
108
0
0
3,639
0
none
[ "none" ]
null
null
Π’ БСрляндском государствСнном унивСрситСтС локальная ΡΠ΅Ρ‚ΡŒ ΠΌΠ΅ΠΆΠ΄Ρƒ сСрвСрами Π½Π΅ всСгда Ρ€Π°Π±ΠΎΡ‚Π°Π΅Ρ‚ Π±Π΅Π· ошибок. ΠŸΡ€ΠΈ ΠΏΠ΅Ρ€Π΅Π΄Π°Ρ‡Π΅ Π΄Π²ΡƒΡ… ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²Ρ‹Ρ… сообщСний подряд Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Π° ошибка, Π² Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Π΅ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ эти Π΄Π²Π° сообщСния ΡΠ»ΠΈΠ²Π°ΡŽΡ‚ΡΡ Π² ΠΎΠ΄Π½ΠΎ. ΠŸΡ€ΠΈ Ρ‚Π°ΠΊΠΎΠΌ слиянии ΠΊΠΎΠ½Π΅Ρ† ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ сообщСния совмСщаСтся с Π½Π°Ρ‡Π°Π»ΠΎΠΌ Π²Ρ‚ΠΎΡ€ΠΎΠ³ΠΎ. ΠšΠΎΠ½Π΅Ρ‡Π½ΠΎ, совмСщСниС ΠΌΠΎ...
Π’ СдинствСнной строкС Π²Ρ‹Ρ…ΠΎΠ΄Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ… слСдуСт нСпустая строка *t*, состоящая ΠΈΠ· строчных Π±ΡƒΠΊΠ² латинского Π°Π»Ρ„Π°Π²ΠΈΡ‚Π°. Π”Π»ΠΈΠ½Π° строки *t* Π½Π΅ прСвосходит 100 символов.
Если сообщСниС *t* Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ‚ ΡΠΎΠ΄Π΅Ρ€ΠΆΠ°Ρ‚ΡŒ ошибки, Π²Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ Β«NOΒ» (Π±Π΅Π· ΠΊΠ°Π²Ρ‹Ρ‡Π΅ΠΊ) Π² Π΅Π΄ΠΈΠ½ΡΡ‚Π²Π΅Π½Π½ΡƒΡŽ строку Π²Ρ‹Ρ…ΠΎΠ΄Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ…. Π’ ΠΏΡ€ΠΎΡ‚ΠΈΠ²Π½ΠΎΠΌ случаС Π² ΠΏΠ΅Ρ€Π²ΠΎΠΉ строкС Π²Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ Β«YESΒ» (Π±Π΅Π· ΠΊΠ°Π²Ρ‹Ρ‡Π΅ΠΊ), Π° Π² ΡΠ»Π΅Π΄ΡƒΡŽΡ‰Π΅ΠΉ строкС Π²Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ строку *s*Β β€” Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΠ΅ сообщСниС, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ ΠΌΠΎΠ³Π»ΠΎ привСсти ΠΊ ошибкС. Если Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Ρ‹Ρ… ΠΎΡ‚Π²Π΅Ρ‚ΠΎΠ² нСсколько, Ρ€Π°Π·Ρ€Π΅ΡˆΠ°Π΅Ρ‚Ρ...
[ "abrakadabrabrakadabra\n", "acacacaca\n", "abcabc\n", "abababab\n", "tatbt\n" ]
[ "YES\nabrakadabra\n", "YES\nacaca\n", "NO\n", "YES\nababab\n", "NO\n" ]
Π’ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠΌ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π΅ подходящим ΠΎΡ‚Π²Π΅Ρ‚ΠΎΠΌ Ρ‚Π°ΠΊΠΆΠ΅ являСтся строка acacaca.
[ { "input": "abrakadabrabrakadabra", "output": "YES\nabrakadabra" }, { "input": "acacacaca", "output": "YES\nacaca" }, { "input": "abcabc", "output": "NO" }, { "input": "abababab", "output": "YES\nababab" }, { "input": "tatbt", "output": "NO" }, { "inpu...
46
4,608,000
0
3,642
689
Mike and Cellphone
[ "brute force", "constructive algorithms", "implementation" ]
null
null
While swimming at the beach, Mike has accidentally dropped his cellphone into the water. There was no worry as he bought a cheap replacement phone with an old-fashioned keyboard. The keyboard has only ten digital equal-sized keys, located in the following way: Together with his old phone, he lost all his contacts and ...
The first line of the input contains the only integer *n* (1<=≀<=*n*<=≀<=9)Β β€” the number of digits in the phone number that Mike put in. The second line contains the string consisting of *n* digits (characters from '0' to '9') representing the number that Mike put in.
If there is no other phone number with the same finger movements and Mike can be sure he is calling the correct number, print "YES" (without quotes) in the only line. Otherwise print "NO" (without quotes) in the first line.
[ "3\n586\n", "2\n09\n", "9\n123456789\n", "3\n911\n" ]
[ "NO\n", "NO\n", "YES\n", "YES\n" ]
You can find the picture clarifying the first sample case in the statement above.
[ { "input": "3\n586", "output": "NO" }, { "input": "2\n09", "output": "NO" }, { "input": "9\n123456789", "output": "YES" }, { "input": "3\n911", "output": "YES" }, { "input": "3\n089", "output": "NO" }, { "input": "3\n159", "output": "YES" }, { ...
46
0
-1
3,650
253
Physics Practical
[ "binary search", "dp", "sortings", "two pointers" ]
null
null
One day Vasya was on a physics practical, performing the task on measuring the capacitance. He followed the teacher's advice and did as much as *n* measurements, and recorded the results in the notebook. After that he was about to show the results to the teacher, but he remembered that at the last lesson, the teacher h...
The first line contains integer *n* (2<=≀<=*n*<=≀<=105) β€” the number of measurements Vasya made. The second line contains *n* integers *c*1,<=*c*2,<=...,<=*c**n* (1<=≀<=*c**i*<=≀<=5000) β€” the results of the measurements. The numbers on the second line are separated by single spaces.
Print a single integer β€” the minimum number of results Vasya will have to remove.
[ "6\n4 5 3 8 3 7\n", "4\n4 3 2 4\n" ]
[ "2\n", "0\n" ]
In the first sample you can remove the fourth and the sixth measurement results (values 8 and 7). Then the maximum of the remaining values will be 5, and the minimum one will be 3. Or else, you can remove the third and fifth results (both equal 3). After that the largest remaining result will be 8, and the smallest one...
[ { "input": "6\n4 5 3 8 3 7", "output": "2" }, { "input": "4\n4 3 2 4", "output": "0" }, { "input": "6\n5 6 4 9 4 8", "output": "1" }, { "input": "4\n5 4 1 5", "output": "1" }, { "input": "2\n3 2", "output": "0" }, { "input": "10\n39 9 18 13 6 16 47 15 ...
30
0
0
3,680
331
Oh Sweet Beaverette
[ "data structures", "sortings" ]
null
null
β€” Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me? β€” Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night? At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcom...
The first line contains a single integer *n* β€” the initial number of trees in the woodland belt, 2<=≀<=*n*. The second line contains space-separated integers *a**i* β€” the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value. - to get 30 points, you need to solve the problem wi...
In the first line print two integers β€” the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees *k*. In the next line print *k* integers β€” the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to *n* from left t...
[ "5\n1 2 3 1 2\n", "5\n1 -2 3 1 -2\n" ]
[ "8 1\n1 ", "5 2\n2 5 " ]
none
[ { "input": "5\n1 2 3 1 2", "output": "8 1\n1 " }, { "input": "5\n1 -2 3 1 -2", "output": "5 2\n2 5 " }, { "input": "2\n0 0", "output": "0 0" }, { "input": "3\n0 -1 0", "output": "0 1\n2 " }, { "input": "3\n1 1 1", "output": "3 0" }, { "input": "4\n-1 1...
310
20,275,200
0
3,685
15
Cottage Village
[ "implementation", "sortings" ]
A. Cottage Village
2
64
A new cottage village called «Flatville» is being built in Flatland. By now they have already built in «Flatville» *n* square houses with the centres on the *Оx*-axis. The houses' sides are parallel to the coordinate axes. It's known that no two houses overlap, but they can touch each other. The architect bureau, wher...
The first line of the input data contains numbers *n* and *t* (1<=≀<=*n*,<=*t*<=≀<=1000). Then there follow *n* lines, each of them contains two space-separated integer numbers: *x**i* *a**i*, where *x**i* β€” *x*-coordinate of the centre of the *i*-th house, and *a**i* β€” length of its side (<=-<=1000<=≀<=*x**i*<=≀<=1000...
Output the amount of possible positions of the new house.
[ "2 2\n0 4\n6 2\n", "2 2\n0 4\n5 2\n", "2 3\n0 4\n5 2\n" ]
[ "4\n", "3\n", "2\n" ]
It is possible for the *x*-coordinate of the new house to have non-integer value.
[ { "input": "2 2\n0 4\n6 2", "output": "4" }, { "input": "2 2\n0 4\n5 2", "output": "3" }, { "input": "2 3\n0 4\n5 2", "output": "2" }, { "input": "1 1\n1 1", "output": "2" }, { "input": "1 2\n2 1", "output": "2" }, { "input": "2 1\n2 1\n1 1", "outp...
746
0
3.8135
3,691
789
Anastasia and pebbles
[ "implementation", "math" ]
null
null
Anastasia loves going for a walk in Central Uzhlyandian Park. But she became uninterested in simple walking, so she began to collect Uzhlyandian pebbles. At first, she decided to collect all the pebbles she could find in the park. She has only two pockets. She can put at most *k* pebbles in each pocket at the same tim...
The first line contains two integers *n* and *k* (1<=≀<=*n*<=≀<=105, 1<=≀<=*k*<=≀<=109)Β β€” the number of different pebble types and number of pebbles Anastasia can place in one pocket. The second line contains *n* integers *w*1,<=*w*2,<=...,<=*w**n* (1<=≀<=*w**i*<=≀<=104)Β β€” number of pebbles of each type.
The only line of output contains one integerΒ β€” the minimum number of days Anastasia needs to collect all the pebbles.
[ "3 2\n2 3 4\n", "5 4\n3 1 8 9 7\n" ]
[ "3\n", "5\n" ]
In the first sample case, Anastasia can collect all pebbles of the first type on the first day, of second typeΒ β€” on the second day, and of third typeΒ β€” on the third day. Optimal sequence of actions in the second sample case: - In the first day Anastasia collects 8 pebbles of the third type. - In the second day she...
[ { "input": "3 2\n2 3 4", "output": "3" }, { "input": "5 4\n3 1 8 9 7", "output": "5" }, { "input": "1 22\n1", "output": "1" }, { "input": "3 57\n78 165 54", "output": "3" }, { "input": "5 72\n74 10 146 189 184", "output": "6" }, { "input": "9 13\n132 8...
1,000
11,059,200
0
3,695
467
George and Job
[ "dp", "implementation" ]
null
null
The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, he didn't have enough money, so George was going to work as a programmer. Now he faced the following problem at the work. Given a sequence of *n* integers *p*1,<=*p*2,<=...,<=*p**n*. You are to choose *k* pairs of integers:...
The first line contains three integers *n*, *m* and *k* (1<=≀<=(*m*<=Γ—<=*k*)<=≀<=*n*<=≀<=5000). The second line contains *n* integers *p*1,<=*p*2,<=...,<=*p**n* (0<=≀<=*p**i*<=≀<=109).
Print an integer in a single line β€” the maximum possible value of sum.
[ "5 2 1\n1 2 3 4 5\n", "7 1 3\n2 10 7 18 5 33 0\n" ]
[ "9\n", "61\n" ]
none
[ { "input": "5 2 1\n1 2 3 4 5", "output": "9" }, { "input": "7 1 3\n2 10 7 18 5 33 0", "output": "61" }, { "input": "13 8 1\n73 7 47 91 54 74 99 11 67 35 84 18 19", "output": "515" }, { "input": "8 3 1\n8 46 37 81 81 57 11 2", "output": "219" }, { "input": "20 5 3\...
1,000
0
0
3,708
722
Destroying Array
[ "data structures", "dsu" ]
null
null
You are given an array consisting of *n* non-negative integers *a*1,<=*a*2,<=...,<=*a**n*. You are going to destroy integers in the array one by one. Thus, you are given the permutation of integers from 1 to *n* defining the order elements of the array are destroyed. After each element is destroyed you have to find o...
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=100<=000)Β β€” the length of the array. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=109). The third line contains a permutation of integers from 1 to *n*Β β€” the order used to destroy elements.
Print *n* lines. The *i*-th line should contain a single integerΒ β€” the maximum possible sum of elements on the segment containing no destroyed elements, after first *i* operations are performed.
[ "4\n1 3 2 5\n3 4 1 2\n", "5\n1 2 3 4 5\n4 2 3 5 1\n", "8\n5 5 4 4 6 6 5 5\n5 2 8 7 1 3 4 6\n" ]
[ "5\n4\n3\n0\n", "6\n5\n5\n1\n0\n", "18\n16\n11\n8\n8\n6\n6\n0\n" ]
Consider the first sample: 1. Third element is destroyed. Array is now 1Β 3  *  5. Segment with maximum sum 5 consists of one integer 5. 1. Fourth element is destroyed. Array is now 1Β 3  *   * . Segment with maximum sum 4 consists of two integers 1Β 3. 1. First element is destroyed. Array is now  *  3  *   * . Segme...
[ { "input": "4\n1 3 2 5\n3 4 1 2", "output": "5\n4\n3\n0" }, { "input": "5\n1 2 3 4 5\n4 2 3 5 1", "output": "6\n5\n5\n1\n0" }, { "input": "8\n5 5 4 4 6 6 5 5\n5 2 8 7 1 3 4 6", "output": "18\n16\n11\n8\n8\n6\n6\n0" }, { "input": "10\n3 3 3 5 6 9 3 1 7 3\n3 4 6 7 5 1 10 9 2 8"...
1,000
6,451,200
0
3,710
53
Autocomplete
[ "implementation" ]
A. Autocomplete
2
256
Autocomplete is a program function that enables inputting the text (in editors, command line shells, browsers etc.) completing the text by its inputted part. Vasya is busy working on a new browser called 'BERowser'. He happens to be working on the autocomplete function in the address line at this very moment. A list co...
The first line contains the *s* line which is the inputted part. The second line contains an integer *n* (1<=≀<=*n*<=≀<=100) which is the number of visited pages. Then follow *n* lines which are the visited pages, one on each line. All the lines have lengths of from 1 to 100 symbols inclusively and consist of lowercase...
If *s* is not the beginning of any of *n* addresses of the visited pages, print *s*. Otherwise, print the lexicographically minimal address of one of the visited pages starting from *s*. The lexicographical order is the order of words in a dictionary. The lexicographical comparison of lines is realized by the '&lt;' o...
[ "next\n2\nnextpermutation\nnextelement\n", "find\n4\nfind\nfindfirstof\nfindit\nfand\n", "find\n4\nfondfind\nfondfirstof\nfondit\nfand\n" ]
[ "nextelement\n", "find\n", "find\n" ]
none
[ { "input": "next\n2\nnextpermutation\nnextelement", "output": "nextelement" }, { "input": "find\n4\nfind\nfindfirstof\nfindit\nfand", "output": "find" }, { "input": "find\n4\nfondfind\nfondfirstof\nfondit\nfand", "output": "find" }, { "input": "kudljmxcse\n4\nkudljmxcse\nszje...
216
0
3.946
3,715
471
MUH and Cube Walls
[ "string suffix structures", "strings" ]
null
null
Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev got hold of lots of wooden cubes somewhere. They started making cube towers by placing the cubes one on top of the other. They defined multiple towers standing in a line as a wall. A wall can consist of towers of di...
The first line contains two integers *n* and *w* (1<=≀<=*n*,<=*w*<=≀<=2Β·105) β€” the number of towers in the bears' and the elephant's walls correspondingly. The second line contains *n* integers *a**i* (1<=≀<=*a**i*<=≀<=109) β€” the heights of the towers in the bears' wall. The third line contains *w* integers *b**i* (1<=...
Print the number of segments in the bears' wall where Horace can "see an elephant".
[ "13 5\n2 4 5 5 4 3 2 2 2 3 3 2 1\n3 4 4 3 2\n" ]
[ "2" ]
The picture to the left shows Horace's wall from the sample, the picture to the right shows the bears' wall. The segments where Horace can "see an elephant" are in gray.
[ { "input": "13 5\n2 4 5 5 4 3 2 2 2 3 3 2 1\n3 4 4 3 2", "output": "2" }, { "input": "5 1\n8 71 1 24 2\n31", "output": "5" }, { "input": "6 3\n2 2 2 2 2 2\n5 5 5", "output": "4" }, { "input": "1 1\n576560149\n691846236", "output": "1" }, { "input": "10 5\n5 10 8 1...
31
0
0
3,718
361
Levko and Permutation
[ "constructive algorithms", "math", "number theory" ]
null
null
Levko loves permutations very much. A permutation of length *n* is a sequence of distinct positive integers, each is at most *n*. Let’s assume that value *gcd*(*a*,<=*b*) shows the greatest common divisor of numbers *a* and *b*. Levko assumes that element *p**i* of permutation *p*1,<=*p*2,<=... ,<=*p**n* is good if *g...
The single line contains two integers *n* and *k* (1<=≀<=*n*<=≀<=105, 0<=≀<=*k*<=≀<=*n*).
In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist. If there are multiple suitable permutations, you are allowed to print any of them.
[ "4 2\n", "1 1\n" ]
[ "2 4 3 1", "-1\n" ]
In the first sample elements 4 and 3 are good because *gcd*(2, 4) = 2 &gt; 1 and *gcd*(3, 3) = 3 &gt; 1. Elements 2 and 1 are not good because *gcd*(1, 2) = 1 and *gcd*(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful. The second sample has no beautiful permutations.
[ { "input": "4 2", "output": "2 1 3 4 " }, { "input": "1 1", "output": "-1" }, { "input": "7 4", "output": "3 1 2 4 5 6 7 " }, { "input": "10 9", "output": "1 2 3 4 5 6 7 8 9 10 " }, { "input": "10000 5000", "output": "5000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1...
77
0
0
3,723
0
none
[ "none" ]
null
null
Let's introduce a number system which is based on a roman digits. There are digits I, V, X, L which correspond to the numbers $1$, $5$, $10$ and $50$ respectively. The use of other roman digits is not allowed. Numbers in this system are written as a sequence of one or more digits. We define the value of the sequence s...
The only line of the input file contains a single integer $n$ ($1 \le n \le 10^9$)Β β€” the number of roman digits to use.
Output a single integerΒ β€” the number of distinct integers which can be represented using $n$ roman digits exactly.
[ "1\n", "2\n", "10\n" ]
[ "4\n", "10\n", "244\n" ]
In the first sample there are exactly $4$ integers which can be representedΒ β€” I, V, X and L. In the second sample it is possible to represent integers $2$ (II), $6$ (VI), $10$ (VV), $11$ (XI), $15$ (XV), $20$ (XX), $51$ (IL), $55$ (VL), $60$ (XL) and $100$ (LL).
[ { "input": "1", "output": "4" }, { "input": "2", "output": "10" }, { "input": "10", "output": "244" }, { "input": "1000", "output": "48753" }, { "input": "2000", "output": "97753" }, { "input": "5000", "output": "244753" }, { "input": "1000...
124
409,600
3
3,746
729
Road to Cinema
[ "binary search", "greedy", "sortings" ]
null
null
Vasya is currently at a car rental service, and he wants to reach cinema. The film he has bought a ticket for starts in *t* minutes. There is a straight road of length *s* from the service to the cinema. Let's introduce a coordinate system so that the car rental service is at the point 0, and the cinema is at the point...
The first line contains four positive integers *n*, *k*, *s* and *t* (1<=≀<=*n*<=≀<=2Β·105, 1<=≀<=*k*<=≀<=2Β·105, 2<=≀<=*s*<=≀<=109, 1<=≀<=*t*<=≀<=2Β·109)Β β€” the number of cars at the car rental service, the number of gas stations along the road, the length of the road and the time in which the film starts. Each of the n...
Print the minimum rent price of an appropriate car, i.e. such car that Vasya will be able to reach the cinema before the film starts (not later than in *t* minutes). If there is no appropriate car, print -1.
[ "3 1 8 10\n10 8\n5 7\n11 9\n3\n", "2 2 10 18\n10 4\n20 6\n5 3\n" ]
[ "10\n", "20\n" ]
In the first sample, Vasya can reach the cinema in time using the first or the third cars, but it would be cheaper to choose the first one. Its price is equal to 10, and the capacity of its fuel tank is 8. Then Vasya can drive to the first gas station in the accelerated mode in 3 minutes, spending 6 liters of fuel. Aft...
[ { "input": "3 1 8 10\n10 8\n5 7\n11 9\n3", "output": "10" }, { "input": "2 2 10 18\n10 4\n20 6\n5 3", "output": "20" }, { "input": "2 1 1000000000 2000000000\n111 1000000000\n101 1000000000\n5", "output": "101" }, { "input": "2 1 1000000000 2000000000\n111 999999998\n101 9999...
1,000
37,376,000
0
3,770
52
Right Triangles
[ "combinatorics" ]
B. Right Triangles
2
256
You are given a *n*<=Γ—<=*m* field consisting only of periods ('.') and asterisks ('*'). Your task is to count all right triangles with two sides parallel to the square sides, whose vertices are in the centers of '*'-cells. A right triangle is a triangle in which one angle is a right angle (that is, a 90 degree angle).
The first line contains two positive integer numbers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=1000). The following *n* lines consist of *m* characters each, describing the field. Only '.' and '*' are allowed.
Output a single number β€” total number of square triangles in the field. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
[ "2 2\n**\n*.\n", "3 4\n*..*\n.**.\n*.**\n" ]
[ "1\n", "9\n" ]
none
[ { "input": "2 2\n**\n*.", "output": "1" }, { "input": "3 4\n*..*\n.**.\n*.**", "output": "9" }, { "input": "3 2\n..\n..\n*.", "output": "0" }, { "input": "1 2\n**", "output": "0" }, { "input": "1 3\n*.*", "output": "0" }, { "input": "5 2\n*.\n**\n.*\n....
218
3,891,200
3.938252
3,785
898
Squares and not squares
[ "constructive algorithms", "greedy" ]
null
null
Ann and Borya have *n* piles with candies and *n* is even number. There are *a**i* candies in pile with number *i*. Ann likes numbers which are square of some integer and Borya doesn't like numbers which are square of any integer. During one move guys can select some pile with candies and add one candy to it (this can...
First line contains one even integer *n* (2<=≀<=*n*<=≀<=200<=000)Β β€” number of piles with candies. Second line contains sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=109)Β β€” amounts of candies in each pile.
Output minimal number of steps required to make exactly *n*<=/<=2 piles contain number of candies that is a square of some integer and exactly *n*<=/<=2 piles contain number of candies that is not a square of any integer. If condition is already satisfied output 0.
[ "4\n12 14 30 4\n", "6\n0 0 0 0 0 0\n", "6\n120 110 23 34 25 45\n", "10\n121 56 78 81 45 100 1 0 54 78\n" ]
[ "2\n", "6\n", "3\n", "0\n" ]
In first example you can satisfy condition in two moves. During each move you should add one candy to second pile. After it size of second pile becomes 16. After that Borya and Ann will have two piles with number of candies which is a square of integer (second and fourth pile) and two piles with number of candies which...
[ { "input": "4\n12 14 30 4", "output": "2" }, { "input": "6\n0 0 0 0 0 0", "output": "6" }, { "input": "6\n120 110 23 34 25 45", "output": "3" }, { "input": "10\n121 56 78 81 45 100 1 0 54 78", "output": "0" }, { "input": "10\n0 675178538 310440616 608075179 0 0 0 ...
1,684
21,708,800
3
3,791
490
Hacking Cypher
[ "brute force", "math", "number theory", "strings" ]
null
null
Polycarpus participates in a competition for hacking into a new secure messenger. He's almost won. Having carefully studied the interaction protocol, Polycarpus came to the conclusion that the secret key can be obtained if he properly cuts the public key of the application into two parts. The public key is a long inte...
The first line of the input contains the public key of the messenger β€” an integer without leading zeroes, its length is in range from 1 to 106 digits. The second line contains a pair of space-separated positive integers *a*, *b* (1<=≀<=*a*,<=*b*<=≀<=108).
In the first line print "YES" (without the quotes), if the method satisfying conditions above exists. In this case, next print two lines β€” the left and right parts after the cut. These two parts, being concatenated, must be exactly identical to the public key. The left part must be divisible by *a*, and the right part ...
[ "116401024\n97 1024\n", "284254589153928171911281811000\n1009 1000\n", "120\n12 1\n" ]
[ "YES\n11640\n1024\n", "YES\n2842545891539\n28171911281811000\n", "NO\n" ]
none
[ { "input": "116401024\n97 1024", "output": "YES\n11640\n1024" }, { "input": "284254589153928171911281811000\n1009 1000", "output": "YES\n2842545891539\n28171911281811000" }, { "input": "120\n12 1", "output": "NO" }, { "input": "604\n6 4", "output": "YES\n60\n4" }, { ...
77
2,867,200
-1
3,792
606
Magic Spheres
[ "implementation" ]
null
null
Carl is a beginner magician. He has *a* blue, *b* violet and *c* orange magic spheres. In one move he can transform two spheres of the same color into one sphere of any other color. To make a spell that has never been seen before, he needs at least *x* blue, *y* violet and *z* orange spheres. Can he get them (possible,...
The first line of the input contains three integers *a*, *b* and *c* (0<=≀<=*a*,<=*b*,<=*c*<=≀<=1<=000<=000)Β β€” the number of blue, violet and orange spheres that are in the magician's disposal. The second line of the input contains three integers, *x*, *y* and *z* (0<=≀<=*x*,<=*y*,<=*z*<=≀<=1<=000<=000)Β β€” the number o...
If the wizard is able to obtain the required numbers of spheres, print "Yes". Otherwise, print "No".
[ "4 4 0\n2 1 2\n", "5 6 1\n2 7 2\n", "3 3 3\n2 2 2\n" ]
[ "Yes\n", "No\n", "Yes\n" ]
In the first sample the wizard has 4 blue and 4 violet spheres. In his first action he can turn two blue spheres into one violet one. After that he will have 2 blue and 5 violet spheres. Then he turns 4 violet spheres into 2 orange spheres and he ends up with 2 blue, 1 violet and 2 orange spheres, which is exactly what...
[ { "input": "4 4 0\n2 1 2", "output": "Yes" }, { "input": "5 6 1\n2 7 2", "output": "No" }, { "input": "3 3 3\n2 2 2", "output": "Yes" }, { "input": "0 0 0\n0 0 0", "output": "Yes" }, { "input": "0 0 0\n0 0 1", "output": "No" }, { "input": "0 1 0\n0 0 0...
62
0
0
3,802
975
Aramic script
[ "implementation", "strings" ]
null
null
In Aramic language words can only represent objects. Words in Aramic have special properties: - A word is a root if it does not contain the same letter more than once. - A root and all its permutations represent the same object. - The root $x$ of a word $y$ is the word that contains all letters that appear in $y$...
The first line contains one integer $n$ ($1 \leq n \leq 10^3$)Β β€” the number of words in the script. The second line contains $n$ words $s_1, s_2, \ldots, s_n$Β β€” the script itself. The length of each string does not exceed $10^3$. It is guaranteed that all characters of the strings are small latin letters.
Output one integerΒ β€” the number of different objects mentioned in the given ancient Aramic script.
[ "5\na aa aaa ab abb\n", "3\namer arem mrea\n" ]
[ "2", "1" ]
In the first test, there are two objects mentioned. The roots that represent them are "a","ab". In the second test, there is only one object, its root is "amer", the other strings are just permutations of "amer".
[ { "input": "5\na aa aaa ab abb", "output": "2" }, { "input": "3\namer arem mrea", "output": "1" }, { "input": "10\nbda bbb cda dca dda dcb bcd dcb ada ddd", "output": "6" }, { "input": "2\nfhjlqs aceginpr", "output": "2" }, { "input": "2\nbcdfghimn efghijlmo", ...
93
2,969,600
3
3,808
745
Hongcow Solves A Puzzle
[ "implementation" ]
null
null
Hongcow likes solving puzzles. One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an *n* by *m* grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed tha...
The first line of input will contain two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=500), the dimensions of the puzzle piece. The next *n* lines will describe the jigsaw piece. Each line will have length *m* and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty ...
Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise.
[ "2 3\nXXX\nXXX\n", "2 2\n.X\nXX\n", "5 5\n.....\n..X..\n.....\n.....\n.....\n" ]
[ "YES\n", "NO\n", "YES\n" ]
For the first sample, one example of a rectangle we can form is as follows For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle. In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle:
[ { "input": "2 3\nXXX\nXXX", "output": "YES" }, { "input": "2 2\n.X\nXX", "output": "NO" }, { "input": "1 500\n.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX...
62
5,529,600
3
3,817
859
Declined Finalists
[ "greedy", "implementation" ]
null
null
This year, as in previous years, MemSQL is inviting the top 25 competitors from the Start[c]up qualification round to compete onsite for the final round. Not everyone who is eligible to compete onsite can afford to travel to the office, though. Initially the top 25 contestants are invited to come onsite. Each eligible ...
The first line of input contains *K* (1<=≀<=*K*<=≀<=25), the number of onsite finalists you know. The second line of input contains *r*1,<=*r*2,<=...,<=*r**K* (1<=≀<=*r**i*<=≀<=106), the qualifying ranks of the finalists you know. All these ranks are distinct.
Print the minimum possible number of contestants that declined the invitation to compete onsite.
[ "25\n2 3 4 5 6 7 8 9 10 11 12 14 15 16 17 18 19 20 21 22 23 24 25 26 28\n", "5\n16 23 8 15 4\n", "3\n14 15 92\n" ]
[ "3\n", "0\n", "67\n" ]
In the first example, you know all 25 onsite finalists. The contestants who ranked 1-st, 13-th, and 27-th must have declined, so the answer is 3.
[ { "input": "25\n2 3 4 5 6 7 8 9 10 11 12 14 15 16 17 18 19 20 21 22 23 24 25 26 28", "output": "3" }, { "input": "5\n16 23 8 15 4", "output": "0" }, { "input": "3\n14 15 92", "output": "67" }, { "input": "1\n1000000", "output": "999975" }, { "input": "25\n1000000 ...
31
0
-1
3,840
105
Transmigration
[ "implementation" ]
A. Transmigration
2
256
In Disgaea as in most role-playing games, characters have skills that determine the character's ability to use certain weapons or spells. If the character does not have the necessary skill, he cannot use it. The skill level is represented as an integer that increases when you use this skill. Different character classes...
The first line contains three numbers *n*, *m* and *k* β€” the number of skills the current character has, the number of skills specific for the class into which the character is going to transmigrate and the reducing coefficient respectively; *n* and *m* are integers, and *k* is a real number with exactly two digits aft...
Print on the first line number *z* β€” the number of skills the character will have after the transmigration. Then print *z* lines, on each of which print a skill's name and level, separated by a single space. The skills should be given in the lexicographical order.
[ "5 4 0.75\naxe 350\nimpaler 300\nionize 80\nmegafire 120\nmagicboost 220\nheal\nmegafire\nshield\nmagicboost\n" ]
[ "6\naxe 262\nheal 0\nimpaler 225\nmagicboost 165\nmegafire 0\nshield 0\n" ]
none
[ { "input": "5 4 0.75\naxe 350\nimpaler 300\nionize 80\nmegafire 120\nmagicboost 220\nheal\nmegafire\nshield\nmagicboost", "output": "6\naxe 262\nheal 0\nimpaler 225\nmagicboost 165\nmegafire 0\nshield 0" }, { "input": "1 1 0.50\nstaff 1005\nionize", "output": "2\nionize 0\nstaff 502" }, { ...
156
0
0
3,846
888
Buggy Robot
[ "greedy" ]
null
null
Ivan has a robot which is situated on an infinite grid. Initially the robot is standing in the starting cell (0,<=0). The robot can process commands. There are four types of commands it can perform: - U β€” move from the cell (*x*,<=*y*) to (*x*,<=*y*<=+<=1); - D β€” move from (*x*,<=*y*) to (*x*,<=*y*<=-<=1); - L β€” mo...
The first line contains one number *n* β€” the length of sequence of commands entered by Ivan (1<=≀<=*n*<=≀<=100). The second line contains the sequence itself β€” a string consisting of *n* characters. Each character can be U, D, L or R.
Print the maximum possible number of commands from the sequence the robot could perform to end up in the starting cell.
[ "4\nLDUR\n", "5\nRRRUU\n", "6\nLLRRRR\n" ]
[ "4\n", "0\n", "4\n" ]
none
[ { "input": "4\nLDUR", "output": "4" }, { "input": "5\nRRRUU", "output": "0" }, { "input": "6\nLLRRRR", "output": "4" }, { "input": "88\nLLUUULRDRRURDDLURRLRDRLLRULRUUDDLLLLRRDDURDURRLDURRLDRRRUULDDLRRRDDRRLUULLURDURUDDDDDLDR", "output": "76" }, { "input": "89\nLDL...
62
5,632,000
0
3,848
363
Soroban
[ "implementation" ]
null
null
You know that Japan is the country with almost the largest 'electronic devices per person' ratio. So you might be quite surprised to find out that the primary school in Japan teaches to count using a Soroban β€” an abacus developed in Japan. This phenomenon has its reasons, of course, but we are not going to speak about ...
The first line contains a single integer *n* (0<=≀<=*n*<=&lt;<=109).
Print the description of the decimal digits of number *n* from the last one to the first one (as mentioned on the picture in the statement), one per line. Print the beads as large English letters 'O', rod pieces as character '-' and the reckoning bar as '|'. Print as many rods, as many digits are in the decimal represe...
[ "2\n", "13\n", "720\n" ]
[ "O-|OO-OO\n", "O-|OOO-O\nO-|O-OOO\n", "O-|-OOOO\nO-|OO-OO\n-O|OO-OO\n" ]
none
[ { "input": "2", "output": "O-|OO-OO" }, { "input": "13", "output": "O-|OOO-O\nO-|O-OOO" }, { "input": "720", "output": "O-|-OOOO\nO-|OO-OO\n-O|OO-OO" }, { "input": "0", "output": "O-|-OOOO" }, { "input": "1", "output": "O-|O-OOO" }, { "input": "3", ...
62
0
3
3,853
633
A Trivial Problem
[ "brute force", "constructive algorithms", "math", "number theory" ]
null
null
Mr. Santa asks all the great programmers of the world to solve a trivial problem. He gives them an integer *m* and asks for the number of positive integers *n*, such that the factorial of *n* ends with exactly *m* zeroes. Are you among those great programmers who can solve this problem?
The only line of input contains an integer *m* (1<=≀<=*m*<=≀<=100<=000)Β β€” the required number of trailing zeroes in factorial.
First print *k*Β β€” the number of values of *n* such that the factorial of *n* ends with *m* zeroes. Then print these *k* integers in increasing order.
[ "1\n", "5\n" ]
[ "5\n5 6 7 8 9 ", "0" ]
The factorial of *n* is equal to the product of all integers from 1 to *n* inclusive, that is *n*! = 1Β·2Β·3Β·...Β·*n*. In the first sample, 5! = 120, 6! = 720, 7! = 5040, 8! = 40320 and 9! = 362880.
[ { "input": "1", "output": "5\n5 6 7 8 9 " }, { "input": "5", "output": "0" }, { "input": "2", "output": "5\n10 11 12 13 14 " }, { "input": "3", "output": "5\n15 16 17 18 19 " }, { "input": "7", "output": "5\n30 31 32 33 34 " }, { "input": "12", "ou...
124
0
0
3,860
656
Scrambled
[ "*special", "implementation" ]
null
null
Btoh yuo adn yuor roomatme lhoate wianshg disehs, btu stlil sdmoeboy msut peorrfm tihs cohre dialy. Oen dya yuo decdie to idourtcne smoe syestm. Yuor rmmotaoe sstgegus teh fooniwllg dael. Yuo argee on tow arayrs of ientgres M adn R, nmebur upmicnog dyas (induiclng teh cunrret oen) wtih sicsescuve irnegets (teh ceurrnt ...
The first line of input contains a single integer N (1<=≀<=*N*<=≀<=16). The second and third lines of input contain N integers each, all between 0 and 16, inclusive, and represent arrays M and R, respectively. All *M*[*i*] are positive, for each *i* *R*[*i*]<=&lt;<=*M*[*i*].
Output a single real number. The answer is considered to be correct if its absolute or relative error does not exceed 10<=-<=4.
[ "1\n2\n0\n", "2\n2 3\n1 0\n" ]
[ "0.500000\n", "0.666667\n" ]
none
[ { "input": "1\n2\n0", "output": "0.500000" }, { "input": "2\n2 3\n1 0", "output": "0.666667" }, { "input": "3\n2 4 4\n0 1 3", "output": "1.000000" }, { "input": "1\n16\n15", "output": "0.062500" }, { "input": "16\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16\n0 1 2 3 4 ...
77
5,836,800
0
3,881
920
Connected Components?
[ "data structures", "dfs and similar", "dsu", "graphs" ]
null
null
You are given an undirected graph consisting of *n* vertices and edges. Instead of giving you the edges that exist in the graph, we give you *m* unordered pairs (*x*,<=*y*) such that there is no edge between *x* and *y*, and if some pair of vertices is not listed in the input, then there is an edge between these verti...
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=200000, ). Then *m* lines follow, each containing a pair of integers *x* and *y* (1<=≀<=*x*,<=*y*<=≀<=*n*, *x*<=β‰ <=*y*) denoting that there is no edge between *x* and *y*. Each pair is listed at most once; (*x*,<=*y*) and (*y*,<=*x*) are considered the sa...
Firstly print *k* β€” the number of connected components in this graph. Then print *k* integers β€” the sizes of components. You should output these integers in non-descending order.
[ "5 5\n1 2\n3 4\n3 2\n4 2\n2 5\n" ]
[ "2\n1 4 " ]
none
[ { "input": "5 5\n1 2\n3 4\n3 2\n4 2\n2 5", "output": "2\n1 4 " }, { "input": "8 15\n2 1\n4 5\n2 4\n3 4\n2 5\n3 5\n2 6\n3 6\n5 6\n4 6\n2 7\n3 8\n2 8\n3 7\n6 7", "output": "1\n8 " }, { "input": "12 58\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 10\n1 11\n1 12\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n...
561
54,272,000
3
3,884
98
Help King
[ "implementation", "probabilities", "trees" ]
B. Help King
2
256
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"...
30
0
0
3,887
237
Build String
[ "flows", "graphs" ]
null
null
You desperately need to build some string *t*. For that you've got *n* more strings *s*1,<=*s*2,<=...,<=*s**n*. To build string *t*, you are allowed to perform exactly |*t*| (|*t*| is the length of string *t*) operations on these strings. Each operation looks like that: 1. choose any non-empty string from strings *s*...
The first line of the input contains string *t* β€” the string that you need to build. The second line contains a single integer *n* (1<=≀<=*n*<=≀<=100) β€” the number of strings to which you are allowed to apply the described operation. Each of the next *n* lines contains a string and an integer. The *i*-th line contains...
Print a single number β€” the minimum money (in rubles) you need in order to build string *t*. If there is no solution, print -1.
[ "bbaze\n3\nbzb 2\naeb 3\nba 10\n", "abacaba\n4\naba 2\nbcc 1\ncaa 2\nbbb 5\n", "xyz\n4\naxx 8\nza 1\nefg 4\nt 1\n" ]
[ "8\n", "18\n", "-1\n" ]
Notes to the samples: In the first sample from the first string you should take characters "b" and "z" with price 1 ruble, from the second string characters "a", "e" ΠΈ "b" with price 2 rubles. The price of the string *t* in this case is 2Β·1 + 3Β·2 = 8. In the second sample from the first string you should take two cha...
[ { "input": "bbaze\n3\nbzb 2\naeb 3\nba 10", "output": "8" }, { "input": "abacaba\n4\naba 2\nbcc 1\ncaa 2\nbbb 5", "output": "18" }, { "input": "xyz\n4\naxx 8\nza 1\nefg 4\nt 1", "output": "-1" }, { "input": "aaabbtttefg\n6\nabbbca 3\nffatgg 2\nyioa 4\nppaeg 2\naetgffff 4\ntre...
92
0
0
3,892
982
Billiard
[ "geometry", "number theory" ]
null
null
Consider a [billiard table](https://en.wikipedia.org/wiki/Billiard_table) of rectangular size $n \times m$ with four pockets. Let's introduce a coordinate system with the origin at the lower left corner (see the picture). There is one ball at the point $(x, y)$ currently. Max comes to the table and strikes the ball. ...
The only line contains $6$ integers $n$, $m$, $x$, $y$, $v_x$, $v_y$ ($1 \leq n, m \leq 10^9$, $0 \leq x \leq n$; $0 \leq y \leq m$; $-1 \leq v_x, v_y \leq 1$; $(v_x, v_y) \neq (0, 0)$)Β β€” the width of the table, the length of the table, the $x$-coordinate of the initial position of the ball, the $y$-coordinate of the i...
Print the coordinates of the pocket the ball will fall into, or $-1$ if the ball will move indefinitely.
[ "4 3 2 2 -1 1\n", "4 4 2 0 1 1\n", "10 10 10 1 -1 0\n" ]
[ "0 0", "-1", "-1" ]
The first sample: The second sample: In the third sample the ball will never change its $y$ coordinate, so the ball will never fall into a pocket.
[ { "input": "4 3 2 2 -1 1", "output": "0 0" }, { "input": "4 4 2 0 1 1", "output": "-1" }, { "input": "10 10 10 1 -1 0", "output": "-1" }, { "input": "1000000000 1000000000 1 1000000000 0 1", "output": "-1" }, { "input": "2 1 1 0 -1 -1", "output": "0 1" }, ...
78
307,200
0
3,901
156
Message
[ "brute force" ]
null
null
Dr. Moriarty is about to send a message to Sherlock Holmes. He has a string *s*. String *p* is called a substring of string *s* if you can read it starting from some position in the string *s*. For example, string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba". Dr. Moriarty plans to take string *s* and c...
The first line contains a non-empty string *s*, consisting of lowercase Latin letters. The second line contains a non-empty string *u*, consisting of lowercase Latin letters. The lengths of both strings are in the range from 1 to 2000, inclusive.
Print the only integer β€” the minimum number of changes that Dr. Moriarty has to make with the string that you choose.
[ "aaaaa\naaa\n", "abcabc\nbcd\n", "abcdef\nklmnopq\n" ]
[ "0\n", "1\n", "7\n" ]
In the first sample Moriarty can take any substring of length 3, and it will be equal to the required message *u*, so Moriarty won't have to make any changes. In the second sample you should take a substring consisting of characters from second to fourth ("bca") or from fifth to sixth ("bc"). Then you will only have t...
[ { "input": "aaaaa\naaa", "output": "0" }, { "input": "abcabc\nbcd", "output": "1" }, { "input": "abcdef\nklmnopq", "output": "7" }, { "input": "aaabbbaaa\naba", "output": "1" }, { "input": "a\na", "output": "0" }, { "input": "z\nz", "output": "0" ...
342
2,048,000
3
3,903
191
Dynasty Puzzles
[ "dp" ]
null
null
The ancient Berlanders believed that the longer the name, the more important its bearer is. Thus, Berland kings were famous for their long names. But long names are somewhat inconvenient, so the Berlanders started to abbreviate the names of their kings. They called every king by the first letters of its name. Thus, the...
The first line contains integer *n* (1<=≀<=*n*<=≀<=5Β·105) β€” the number of names in Vasya's list. Next *n* lines contain *n* abbreviated names, one per line. An abbreviated name is a non-empty sequence of lowercase Latin letters. Its length does not exceed 10 characters.
Print a single number β€” length of the sought dynasty's name in letters. If Vasya's list is wrong and no dynasty can be found there, print a single number 0.
[ "3\nabc\nca\ncba\n", "4\nvvp\nvvp\ndam\nvvp\n", "3\nab\nc\ndef\n" ]
[ "6\n", "0\n", "1\n" ]
In the first sample two dynasties can exist: the one called "abcca" (with the first and second kings) and the one called "abccba" (with the first and third kings). In the second sample there aren't acceptable dynasties. The only dynasty in the third sample consists of one king, his name is "c".
[ { "input": "3\nabc\nca\ncba", "output": "6" }, { "input": "4\nvvp\nvvp\ndam\nvvp", "output": "0" }, { "input": "3\nab\nc\ndef", "output": "1" }, { "input": "5\nab\nbc\ncd\nde\nffffffffff", "output": "10" }, { "input": "5\ncab\nbbc\ncaa\nccc\naca", "output": "9...
2,000
29,388,800
0
3,908
952
I'm Feeling Lucky!
[ "probabilities" ]
null
null
You have one chip and one chance to play roulette. Are you feeling lucky?
none
Print your bet. Your chip must be placed entirely within some square (not on an edge or a corner shared by adjacent squares).
[]
[]
none
[ { "input": "1", "output": "Red" }, { "input": "2", "output": "Red" } ]
77
0
0
3,909
84
Biathlon
[ "binary search", "implementation" ]
C. Biathlon
1
256
Perhaps many have heard that the World Biathlon Championship has finished. Although our hero Valera was not present at this spectacular event himself and only watched it on TV, it excited him so much that he decided to enroll in a biathlon section. Of course, biathlon as any sport, proved very difficult in practice. I...
The first line of the input file contains the integer *n* (1<=≀<=*n*<=≀<=104), which is the number of targets. The next *n* lines contain descriptions of the targets. Each target is a circle whose center is located on the *Ox* axis. Each circle is given by its coordinate of the center *x* (<=-<=2Β·104<=≀<=*x*<=≀<=2Β·104)...
Print on the first line a single number, the number of targets hit by Valera. Print on the second line for each of the targets the number of its first hit or "-1" (without quotes) if this number does not exist. Separate numbers with spaces.
[ "3\n2 1\n5 2\n10 1\n5\n0 1\n1 3\n3 0\n4 0\n4 0\n", "3\n3 2\n7 1\n11 2\n4\n2 1\n6 0\n6 4\n11 2\n" ]
[ "2\n3 3 -1 \n", "3\n1 2 4 \n" ]
none
[ { "input": "3\n2 1\n5 2\n10 1\n5\n0 1\n1 3\n3 0\n4 0\n4 0", "output": "2\n3 3 -1 " }, { "input": "3\n3 2\n7 1\n11 2\n4\n2 1\n6 0\n6 4\n11 2", "output": "3\n1 2 4 " }, { "input": "2\n0 5\n10 5\n2\n7 2\n6 1", "output": "1\n-1 1 " }, { "input": "3\n-3 3\n-10 2\n10 2\n4\n10 2\n2 ...
623
27,340,800
3.637574
3,913
196
Lexicographically Maximum Subsequence
[ "greedy", "strings" ]
null
null
You've got string *s*, consisting of only lowercase English letters. Find its lexicographically maximum subsequence. We'll call a non-empty string *s*[*p*1*p*2... *p**k*]<==<=*s**p*1*s**p*2... *s**p**k*(1<=≀<=*p*1<=&lt;<=*p*2<=&lt;<=...<=&lt;<=*p**k*<=≀<=|*s*|) a subsequence of string *s*<==<=*s*1*s*2... *s*|*s*|. St...
The single line contains a non-empty string *s*, consisting only of lowercase English letters. The string's length doesn't exceed 105.
Print the lexicographically maximum subsequence of string *s*.
[ "ababba\n", "abbcbccacbbcbaaba\n" ]
[ "bbba\n", "cccccbba\n" ]
Let's look at samples and see what the sought subsequences look like (they are marked with uppercase bold letters). The first sample: aBaBBA The second sample: abbCbCCaCbbCBaaBA
[ { "input": "ababba", "output": "bbba" }, { "input": "abbcbccacbbcbaaba", "output": "cccccbba" }, { "input": "thankstosamarasauteddybearsforthiscontest", "output": "yttt" }, { "input": "cantouristsolveitlessthaninoneminute", "output": "vute" }, { "input": "areprete...
1,308
614,400
3
3,922
9
Running Student
[ "brute force", "geometry", "implementation" ]
B. Running Student
1
64
And again a misfortune fell on Poor Student. He is being late for an exam. Having rushed to a bus stop that is in point (0,<=0), he got on a minibus and they drove along a straight line, parallel to axis *OX*, in the direction of increasing *x*. Poor Student knows the following: - during one run the minibus makes ...
The first line contains three integer numbers: 2<=≀<=*n*<=≀<=100, 1<=≀<=*v**b*,<=*v**s*<=≀<=1000. The second line contains *n* non-negative integers in ascending order: coordinates *x**i* of the bus stop with index *i*. It is guaranteed that *x*1 equals to zero, and *x**n*<=≀<=105. The third line contains the coordinat...
In the only line output the answer to the problem β€” index of the optimum bus stop.
[ "4 5 2\n0 2 4 6\n4 1\n", "2 1 1\n0 100000\n100000 100000\n" ]
[ "3", "2" ]
As you know, students are a special sort of people, and minibuses usually do not hurry. That's why you should not be surprised, if Student's speed is higher than the speed of the minibus.
[ { "input": "4 5 2\n0 2 4 6\n4 1", "output": "3" }, { "input": "2 1 1\n0 100000\n100000 100000", "output": "2" }, { "input": "6 5 1\n0 1 2 3 4 5\n0 0", "output": "2" }, { "input": "4 100 10\n0 118 121 178\n220 220", "output": "4" }, { "input": "4 3 3\n0 6 8 10\n7 -...
218
307,200
3.888711
3,925
149
Coloring Brackets
[ "dp" ]
null
null
Once Petya read a problem about a bracket sequence. He gave it much thought but didn't find a solution. Today you will face it. You are given string *s*. It represents a correct bracket sequence. A correct bracket sequence is the sequence of opening ("(") and closing (")") brackets, such that it is possible to obtain ...
The first line contains the single string *s* (2<=≀<=|*s*|<=≀<=700) which represents a correct bracket sequence.
Print the only number β€” the number of ways to color the bracket sequence that meet the above given conditions modulo 1000000007 (109<=+<=7).
[ "(())\n", "(()())\n", "()\n" ]
[ "12\n", "40\n", "4\n" ]
Let's consider the first sample test. The bracket sequence from the sample can be colored, for example, as is shown on two figures below. The two ways of coloring shown below are incorrect.
[ { "input": "(())", "output": "12" }, { "input": "(()())", "output": "40" }, { "input": "()", "output": "4" }, { "input": "((()))", "output": "36" }, { "input": "()(())", "output": "42" }, { "input": "()()()", "output": "48" }, { "input": "(...
62
0
0
3,929
1,003
Abbreviation
[ "dp", "hashing", "strings" ]
null
null
You are given a text consisting of $n$ space-separated words. There is exactly one space character between any pair of adjacent words. There are no spaces before the first word and no spaces after the last word. The length of text is the number of letters and spaces in it. $w_i$ is the $i$-th word of text. All words co...
The first line of the input contains one integer $n$ ($1 \le n \le 300$) β€” the number of words in the text. The next line contains $n$ space-separated words of the text $w_1, w_2, \dots, w_n$. Each word consists only of lowercase Latin letters. It is guaranteed that the length of text does not exceed $10^5$.
Print one integer β€” the minimum length of the text after at most one abbreviation.
[ "6\nto be or not to be\n", "10\na ab a a b ab a a b c\n", "6\naa bb aa aa bb bb\n" ]
[ "12\n", "13\n", "11\n" ]
In the first example you can obtain the text "TB or not TB". In the second example you can obtain the text "a AAAB AAAB c". In the third example you can obtain the text "AB aa AB bb".
[ { "input": "6\nto be or not to be", "output": "12" }, { "input": "10\na ab a a b ab a a b c", "output": "13" }, { "input": "6\naa bb aa aa bb bb", "output": "11" }, { "input": "45\nxr l pl sx c c u py sv j f x h u y w w bs u cp e ad ib b tz gy lm e s n ln kg fs rd ln v f sh t...
140
1,433,600
0
3,957
667
Pouring Rain
[ "geometry", "math" ]
null
null
A lot of people in Berland hates rain, but you do not. Rain pacifies, puts your thoughts in order. By these years you have developed a good tradition β€” when it rains, you go on the street and stay silent for a moment, contemplate all around you, enjoy freshness, think about big deeds you have to do. Today everything ...
The only line of the input contains four integer numbers *d*,<=*h*,<=*v*,<=*e* (1<=≀<=*d*,<=*h*,<=*v*,<=*e*<=≀<=104), where: - *d* β€” the diameter of your cylindrical cup, - *h* β€” the initial level of water in the cup, - *v* β€” the speed of drinking process from the cup in milliliters per second, - *e* β€” the growth ...
If it is impossible to make the cup empty, print "NO" (without quotes). Otherwise print "YES" (without quotes) in the first line. In the second line print a real number β€” time in seconds needed the cup will be empty. The answer will be considered correct if its relative or absolute error doesn't exceed 10<=-<=4. It is...
[ "1 2 3 100\n", "1 1 1 1\n" ]
[ "NO\n", "YES\n3.659792366325\n" ]
In the first example the water fills the cup faster than you can drink from it. In the second example area of the cup's bottom equals to <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/419dc74dcd7bc392019c9fe748fe1fdb08ab521a.png" style="max-width: 100.0%;max-height: 100.0%;"/>, thus we ca...
[ { "input": "1 2 3 100", "output": "NO" }, { "input": "1 1 1 1", "output": "YES\n3.659792366325" }, { "input": "48 7946 7992 72", "output": "NO" }, { "input": "72 6791 8546 46", "output": "NO" }, { "input": "100 5635 9099 23", "output": "NO" }, { "input...
46
0
3
3,961
978
File Name
[ "greedy", "strings" ]
null
null
You can not just take the file and send it. When Polycarp trying to send a file in the social network "Codehorses", he encountered an unexpected problem. If the name of the file contains three or more "x" (lowercase Latin letters "x") in a row, the system considers that the file content does not correspond to the socia...
The first line contains integer $n$ $(3 \le n \le 100)$ β€” the length of the file name. The second line contains a string of length $n$ consisting of lowercase Latin letters only β€” the file name.
Print the minimum number of characters to remove from the file name so after that the name does not contain "xxx" as a substring. If initially the file name dost not contain a forbidden substring "xxx", print 0.
[ "6\nxxxiii\n", "5\nxxoxx\n", "10\nxxxxxxxxxx\n" ]
[ "1\n", "0\n", "8\n" ]
In the first example Polycarp tried to send a file with name contains number $33$, written in Roman numerals. But he can not just send the file, because it name contains three letters "x" in a row. To send the file he needs to remove any one of this letters.
[ { "input": "6\nxxxiii", "output": "1" }, { "input": "5\nxxoxx", "output": "0" }, { "input": "10\nxxxxxxxxxx", "output": "8" }, { "input": "100\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "output": "98" }, { ...
46
0
3
3,962
62
A Student's Dream
[ "greedy", "math" ]
A. A Student's Dream
2
256
Statistics claims that students sleep no more than three hours a day. But even in the world of their dreams, while they are snoring peacefully, the sense of impending doom is still upon them. A poor student is dreaming that he is sitting the mathematical analysis exam. And he is examined by the most formidable profess...
The first line contains two positive integers not exceeding 100. They are the number of fingers on the Venusian girl's left and right hand correspondingly. The second line contains two integers not exceeding 100. They are the number of fingers on the Marsian boy's left and right hands correspondingly.
Print YES or NO, that is, the answer to Petr Palych's question.
[ "5 1\n10 5\n", "4 5\n3 3\n", "1 2\n11 6\n" ]
[ "YES", "YES", "NO" ]
The boy and the girl don't really care who goes to the left.
[ { "input": "5 1\n10 5", "output": "YES" }, { "input": "4 5\n3 3", "output": "YES" }, { "input": "1 2\n11 6", "output": "NO" }, { "input": "1 1\n1 1", "output": "YES" }, { "input": "2 2\n1 1", "output": "YES" }, { "input": "3 3\n1 1", "output": "NO"...
92
0
0
3,969
886
ACM ICPC
[ "brute force" ]
null
null
In a small but very proud high school it was decided to win ACM ICPC. This goal requires to compose as many teams of three as possible, but since there were only 6 students who wished to participate, the decision was to build exactly two teams. After practice competition, participant number *i* got a score of *a**i*. ...
The single line contains six integers *a*1,<=...,<=*a*6 (0<=≀<=*a**i*<=≀<=1000) β€” scores of the participants
Print "YES" (quotes for clarity), if it is possible to build teams with equal score, and "NO" otherwise. You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
[ "1 3 2 1 2 1\n", "1 1 1 1 1 99\n" ]
[ "YES\n", "NO\n" ]
In the first sample, first team can be composed of 1st, 2nd and 6th participant, second β€” of 3rd, 4th and 5th: team scores are 1 + 3 + 1 = 2 + 1 + 2 = 5. In the second sample, score of participant number 6 is too high: his team score will be definitely greater.
[ { "input": "1 3 2 1 2 1", "output": "YES" }, { "input": "1 1 1 1 1 99", "output": "NO" }, { "input": "1000 1000 1000 1000 1000 1000", "output": "YES" }, { "input": "0 0 0 0 0 0", "output": "YES" }, { "input": "633 609 369 704 573 416", "output": "NO" }, { ...
62
5,632,000
0
3,970
847
Preparing for Merge Sort
[ "binary search", "data structures" ]
null
null
Ivan has an array consisting of *n* different integers. He decided to reorder all elements in increasing order. Ivan loves merge sort so he decided to represent his array with one or several increasing sequences which he then plans to merge into one sorted array. Ivan represent his array with increasing sequences with...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=2Β·105) β€” the number of elements in Ivan's array. The second line contains a sequence consisting of distinct integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109) β€” Ivan's array.
Print representation of the given array in the form of one or more increasing sequences in accordance with the algorithm described above. Each sequence must be printed on a new line.
[ "5\n1 3 2 5 4\n", "4\n4 3 2 1\n", "4\n10 30 50 101\n" ]
[ "1 3 5 \n2 4 \n", "4 \n3 \n2 \n1 \n", "10 30 50 101 \n" ]
none
[ { "input": "5\n1 3 2 5 4", "output": "1 3 5 \n2 4 " }, { "input": "4\n4 3 2 1", "output": "4 \n3 \n2 \n1 " }, { "input": "4\n10 30 50 101", "output": "10 30 50 101 " }, { "input": "1\n1", "output": "1 " }, { "input": "1\n200000", "output": "200000 " }, { ...
2,000
7,372,800
0
3,976
896
Willem, Chtholly and Seniorious
[ "data structures", "probabilities" ]
null
null
β€” Willem... β€” What's the matter? β€” It seems that there's something wrong with Seniorious... β€” I'll have a look... Seniorious is made by linking special talismans in particular order. After over 500 years, the carillon is now in bad condition, so Willem decides to examine it thoroughly. Seniorious has *n* pieces...
The only line contains four integers *n*,<=*m*,<=*seed*,<=*v**max* (1<=≀<=*n*,<=*m*<=≀<=105,<=0<=≀<=*seed*<=&lt;<=109<=+<=7,<=1<=≀<=*vmax*<=≀<=109). The initial values and operations are generated using following pseudo code: Here *op* is the type of the operation mentioned in the legend.
For each operation of types 3 or 4, output a line containing the answer.
[ "10 10 7 9\n", "10 10 9 9\n" ]
[ "2\n1\n0\n3\n", "1\n1\n3\n3\n" ]
In the first example, the initial array is {8, 9, 7, 2, 3, 1, 5, 6, 4, 8}. The operations are: - 2 6 7 9 - 1 3 10 8 - 4 4 6 2 4 - 1 4 5 8 - 2 1 7 1 - 4 7 9 4 4 - 1 2 7 9 - 4 5 8 1 1 - 2 5 7 5 - 4 3 10 8 5
[ { "input": "10 10 7 9", "output": "2\n1\n0\n3" }, { "input": "10 10 9 9", "output": "1\n1\n3\n3" }, { "input": "1000 1000 658073485 946088556", "output": "375432604\n52885108\n732131239\n335583873\n375432604\n582199284\n235058938\n682619432\n62026709\n631048460\n51394660\n25596188\n2...
0
0
-1
3,977
294
Shaass and Lights
[ "combinatorics", "number theory" ]
null
null
There are *n* lights aligned in a row. These lights are numbered 1 to *n* from left to right. Initially some of the lights are switched on. Shaass wants to switch all the lights on. At each step he can switch a light on (this light should be switched off at that moment) if there's at least one adjacent light which is a...
The first line of the input contains two integers *n* and *m* where *n* is the number of lights in the sequence and *m* is the number of lights which are initially switched on, (1<=≀<=*n*<=≀<=1000,<=1<=≀<=*m*<=≀<=*n*). The second line contains *m* distinct integers, each between 1 to *n* inclusive, denoting the indices...
In the only line of the output print the number of different possible ways to switch on all the lights modulo 1000000007Β (109<=+<=7).
[ "3 1\n1\n", "4 2\n1 4\n", "11 2\n4 8\n" ]
[ "1\n", "2\n", "6720\n" ]
none
[ { "input": "3 1\n1", "output": "1" }, { "input": "4 2\n1 4", "output": "2" }, { "input": "11 2\n4 8", "output": "6720" }, { "input": "4 2\n1 3", "output": "2" }, { "input": "4 4\n1 2 3 4", "output": "1" }, { "input": "4 2\n1 3", "output": "2" }, ...
108
307,200
3
3,984