problem_statement
stringlengths
147
8.53k
input
stringlengths
1
771
output
stringlengths
1
592
time_limit
stringclasses
32 values
memory_limit
stringclasses
21 values
tags
stringlengths
6
168
A. Sonya and Hotelstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputSonya decided that having her own hotel business is the best way of earning money because she can profit and rest wherever she wants.The country where Sonya lives is an endless line. There is a city i...
Input4 3-3 2 9 16
Output6
1 second
256 megabytes
['implementation', '*900']
F. Abbreviationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou 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 ...
Input6to be or not to be
Output12
1 second
256 megabytes
['dp', 'hashing', 'strings', '*2200']
E. Tree Constructingtime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given three integers n, d and k.Your task is to construct an undirected tree on n vertices with diameter d and degree of each vertex at most k, or say that it is impossible.An undirected tre...
Input6 3 3
OutputYES3 14 11 25 22 6
4 seconds
256 megabytes
['constructive algorithms', 'graphs', '*2100']
D. Coins and Queriestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp has n coins, the value of the i-th coin is a_i. It is guaranteed that all the values are integer powers of 2 (i.e. a_i = 2^d for some non-negative integer number d).Polycarp wants to know a...
Input5 42 4 8 2 4851410
Output1-132
2 seconds
256 megabytes
['greedy', '*1600']
C. Intense Heattime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe heat during the last few days has been really intense. Scientists from all over the Berland study how the temperatures and weather change, and they claim that this summer is abnormally hot. But any s...
Input4 33 4 1 2
Output2.666666666666667
4 seconds
256 megabytes
['brute force', 'implementation', 'math', '*1300']
B. Binary String Constructingtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given three integers a, b and x. Your task is to construct a binary string s of length n = a + b such that there are exactly a zeroes, exactly b ones and exactly x indices i (where 1...
Input2 2 1
Output1100
1 second
256 megabytes
['constructive algorithms', '*1300']
A. Polycarp's Pocketstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp has n coins, the value of the i-th coin is a_i. Polycarp wants to distribute all the coins between his pockets, but he cannot put two coins with the same value into the same pocket.For exam...
Input61 2 4 3 3 2
Output2
1 second
256 megabytes
['implementation', '*800']
G. Two-Pathstime limit per test3.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a weighted tree (undirected connected graph with no cycles, loops or multiple edges) with n vertices. The edge \{u_j, v_j\} has weight w_j. Also each vertex i has its own value a_i assigned ...
Input7 66 5 5 3 2 1 21 2 22 3 22 4 14 5 16 4 27 3 251 14 45 66 43 43 7
Output999812-14
3.5 seconds
256 megabytes
['data structures', 'dp', 'trees', '*2700']
F. One Occurrencetime limit per test3 secondsmemory limit per test768 megabytesinputstandard inputoutputstandard outputYou are given an array a consisting of n integers, and q queries to it. i-th query is denoted by two integers l_i and r_i. For each query, you have to find any integer that occurs exactly once in the s...
Input61 1 2 3 2 422 61 2
Output40
3 seconds
768 megabytes
['data structures', 'divide and conquer', '*2400']
E. We Need More Bossestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYour friend is developing a computer game. He has already decided how the game world should look like — it should consist of n locations connected by m two-way passages. The passages are designed ...
Input5 51 22 33 14 15 2
Output2
2 seconds
256 megabytes
['dfs and similar', 'graphs', 'trees', '*2100']
D. Yet Another Problem On a Subsequencetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe sequence of integers a_1, a_2, \dots, a_k is called a good array if a_1 = k - 1 and a_1 > 0. For example, the sequences [3, -1, 44, 0], [1, -99] are good arrays, and the seque...
Input32 1 1
Output2
2 seconds
256 megabytes
['combinatorics', 'dp', '*1900']
C. Covered Points Counttime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given n segments on a coordinate line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in...
Input30 31 33 8
Output6 2 1
3 seconds
256 megabytes
['data structures', 'implementation', 'sortings', '*1700']
B. Light It Uptime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputRecently, you bought a brand new smart lamp with programming features. At first, you set up a schedule to the lamp. Every day it will turn power on at moment 0 and turn power off at moment M. Moreover, the...
Input3 104 6 7
Output8
1 second
256 megabytes
['greedy', '*1500']
A. Codehorses T-shirtstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputCodehorses has just hosted the second Codehorses Cup. This year, the same as the previous one, organizers are giving T-shirts for the winners.The valid sizes of T-shirts are either "M" or from 0 t...
Input3XSXSMXLSXS
Output2
2 seconds
256 megabytes
['greedy', 'implementation', '*1200']
F. Cards and Joytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n players sitting at the card table. Each player has a favorite number. The favorite number of the j-th player is f_j.There are k \cdot n cards on the table. Each card contains a single intege...
Input4 31 3 2 8 5 5 8 2 2 8 5 21 2 2 52 6 7
Output21
2 seconds
256 megabytes
['dp', '*2000']
E. Reachability from the Capitaltime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n cities and m roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way.What is the minimum number of new roads that need to be built to make all...
Input9 9 11 21 32 31 55 66 11 89 87 1
Output3
2 seconds
256 megabytes
['dfs and similar', 'graphs', 'greedy', '*2000']
D. Equalize the Remainderstime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array consisting of n integers a_1, a_2, \dots, a_n, and a positive integer m. It is guaranteed that m is a divisor of n.In a single move, you can choose any position i betwee...
Input6 33 2 0 6 10 12
Output33 2 0 7 10 14
3 seconds
256 megabytes
['data structures', 'greedy', 'implementation', '*1900']
C. Alphabetic Removalstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a string s consisting of n lowercase Latin letters. Polycarp wants to remove exactly k characters (k \le n) from the string s. Polycarp uses the following algorithm k times: if ther...
Input15 3cccaabababaccbc
Outputcccbbabaccbc
2 seconds
256 megabytes
['implementation', '*1200']
B. Reversing Encryptiontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputA string s of length n can be encrypted by the following algorithm: iterate over all divisors of n in decreasing order (i.e. from n to 1), for each divisor d, reverse the substring s[1 \dots d] ...
Input10rocesfedoc
Outputcodeforces
1 second
256 megabytes
['implementation', '*900']
A. Mishka and Contesttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputMishka started participating in a programming contest. There are n problems in the contest. Mishka's problem-solving skill is equal to k.Mishka arranges all problems from the contest into a list. Be...
Input8 44 2 3 1 5 1 6 4
Output5
1 second
256 megabytes
['brute force', 'implementation', '*800']
B. Cuttingtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are a lot of things which could be cut — trees, paper, "the rope". In this problem you are going to cut a sequence of integers.There is a sequence of integers, which contains the equal number of even an...
Input6 41 2 5 10 15 20
Output1
2 seconds
256 megabytes
['dp', 'greedy', 'sortings', '*1200']
A. Balloonstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are quite a lot of ways to have fun with inflatable balloons. For example, you can fill them with water and see what happens.Grigory and Andrew have the same opinion. So, once upon a time, they went to ...
Input31 2 1
Output21 2
1 second
256 megabytes
['constructive algorithms', 'implementation', '*1000']
E. Good Subsegmentstime limit per test7 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputA permutation p of length n is a sequence p_1, p_2, \ldots, p_n consisting of n distinct integers, each of which from 1 to n (1 \leq p_i \leq n) .Let's call the subsegment [l,r] of the permutation go...
Input51 3 2 5 4151 11 21 31 41 52 22 32 42 53 33 43 54 44 55 5
Output1256101347124131
7 seconds
512 megabytes
['data structures', '*3000']
D. Cycles in producttime limit per test7 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputConsider a tree (that is, an undirected connected graph without loops) T_1 and a tree T_2. Let's define their cartesian product T_1 \times T_2 in a following way.Let V be the set of vertices in T_1 ...
Input2 2 21 21 2
Output8
7 seconds
256 megabytes
['combinatorics', 'divide and conquer', 'trees', '*2900']
C. Sky Full of Starstime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputOn one of the planets of Solar system, in Atmosphere University, many students are fans of bingo game.It is well known that one month on this planet consists of n^2 days, so calendars, represented a...
Input1
Output3
4 seconds
256 megabytes
['combinatorics', 'math', '*2500']
B. Roman Digitstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputLet'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.Num...
Input1
Output4
1 second
256 megabytes
['brute force', 'combinatorics', 'dp', 'greedy', 'math', '*2000']
A. Convert to Onestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou've got a string a_1, a_2, \dots, a_n, consisting of zeros and ones.Let's call a sequence of consecutive elements a_i, a_{i + 1}, \ldots, a_j (1\leq i\leq j\leq n) a substring of string a. You can a...
Input5 1 1001000
Output11
1 second
256 megabytes
['brute force', 'greedy', 'implementation', 'math', '*1500']
B. World Cuptime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAllen wants to enter a fan zone that occupies a round square and has n entrances.There already is a queue of a_i people in front of the i-th entrance. Each entrance allows one person from its queue to enter ...
Input42 3 2 0
Output3
1 second
256 megabytes
['binary search', 'math', '*1300']
A. Hit the Lotterytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAllen has a LOT of money. He has n dollars in the bank. For security reasons, he wants to withdraw it in cash (we will not disclose the reasons here). The denominations for dollar bills are 1, 5, 10, 2...
Input125
Output3
1 second
256 megabytes
['dp', 'greedy', '*800']
F. Cowmpany Cowmpensationtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAllen, having graduated from the MOO Institute of Techcowlogy (MIT), has started a startup! Allen is the president of his startup. He also hires n-1 other employees, each of which is assigned a...
Input3 211
Output5
2 seconds
256 megabytes
['combinatorics', 'dp', 'math', 'trees', '*2700']
E. Number Clickertime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAllen is playing Number Clicker on his phone.He starts with an integer u on the screen. Every second, he can press one of 3 buttons. Turn u \to u+1 \pmod{p}. Turn u \to u+p-1 \pmod{p}. Turn u \to u^...
Input1 3 5
Output21 1
5 seconds
256 megabytes
['divide and conquer', 'graphs', 'meet-in-the-middle', 'number theory', '*2700']
D. Gametime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAllen and Bessie are playing a simple number game. They both know a function f: \{0, 1\}^n \to \mathbb{R}, i. e. the function takes n binary arguments and returns a real value. At the start of the game, the vari...
Input2 20 1 2 32 50 4
Output1.5000002.2500003.250000
3 seconds
256 megabytes
['math', '*2500']
C. Leaving the Bartime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputFor a vector \vec{v} = (x, y), define |v| = \sqrt{x^2 + y^2}.Allen had a bit too much to drink at the bar, which is at the origin. There are n vectors \vec{v_1}, \vec{v_2}, \cdots, \vec{v_n}. Allen wi...
Input3999999 00 999999999999 0
Output1 1 -1
2 seconds
256 megabytes
['brute force', 'data structures', 'geometry', 'greedy', 'math', 'sortings', '*2300']
B. Suit and Tietime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAllen is hosting a formal dinner party. 2n people come to the event in n pairs (couples). After a night of fun, Allen wants to line everyone up for a final picture. The 2n people line up, but Allen doesn...
Input41 1 2 3 3 2 4 4
Output2
2 seconds
256 megabytes
['greedy', 'implementation', 'math', '*1400']
A. Teslatime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAllen dreams of one day owning a enormous fleet of electric cars, the car of the future! He knows that this will give him a big status boost. As Allen is planning out all of the different types of cars he will ...
Input4 51 2 0 41 2 0 45 0 0 30 5 0 3
Output61 1 12 1 24 1 43 4 45 3 25 4 2
3 seconds
256 megabytes
['constructive algorithms', 'implementation', '*2100']
B. Knights of a Polygonal Tabletime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputUnlike Knights of a Round Table, Knights of a Polygonal Table deprived of nobility and happy to kill each other. But each knight has some power and a knight can kill another knight if and ...
Input4 24 5 9 71 2 11 33
Output1 3 46 36
1 second
256 megabytes
['greedy', 'implementation', 'sortings', '*1400']
A. Fingerprintstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are locked in a room with a door that has a keypad with 10 keys corresponding to digits from 0 to 9. To escape from the room, you need to enter a correct code. You also have a sequence of digits.Some ...
Input7 33 5 7 1 6 2 81 2 7
Output7 1 2
1 second
256 megabytes
['implementation', '*800']
F. The Moral Dilemmatime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputHibiki and Dita are in love with each other, but belong to communities that are in a long lasting conflict. Hibiki is deeply concerned with the state of affairs, and wants to figure out if his relati...
Input2 2 2and xx nand xxand xx or xx
Output1
1 second
256 megabytes
['*3200']
E. Nikita and Order Statisticstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputNikita likes tasks on order statistics, for example, he can easily find the k-th number in increasing order on a segment of an array. But now Nikita wonders how many segments of an array t...
Input5 31 2 3 4 5
Output6 5 4 0 0 0
2 seconds
256 megabytes
['chinese remainder theorem', 'fft', 'math', '*2300']
D. Compute Powertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou need to execute several tasks, each associated with number of processors it needs, and the compute power it will consume.You have sufficient number of analog computers, each with enough processors fo...
Input68 10 9 9 8 101 1 1 1 1 1
Output9000
1 second
256 megabytes
['binary search', 'dp', 'greedy', '*2500']
C. Careful Maneuveringtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are two small spaceship, surrounded by two groups of enemy larger spaceships. The space is a two-dimensional plane, and one group of the enemy spaceships is positioned in such a way that the...
Input3 91 2 31 2 3 7 8 9 11 12 13
Output9
2 seconds
256 megabytes
['bitmasks', 'brute force', 'geometry', '*2100']
B. Open Communicationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputTwo participants are each given a pair of distinct numbers from 1 to 9 such that there's exactly one number that is present in both pairs. They want to figure out the number that matches by using a ...
Input2 21 2 3 41 5 3 4
Output1
1 second
256 megabytes
['bitmasks', 'brute force', '*1900']
A. Two Squarestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two squares, one with sides parallel to the coordinate axes, and another one with sides at 45 degrees to the coordinate axes. Find whether the two squares intersect.The interior of the square...
Input0 0 6 0 6 6 0 61 3 3 5 5 3 3 1
OutputYES
1 second
256 megabytes
['geometry', 'implementation', '*1600']
E. Nastya and King-Shamanstime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputNastya likes reading and even spends whole days in a library sometimes. Today she found a chronicle of Byteland in the library, and it stated that there lived shamans long time ago. It is know...
Input2 11 31 2
Output-1
3 seconds
256 megabytes
['binary search', 'data structures', '*2500']
D. Nastya and a Gametime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputNastya received one more array on her birthday, this array can be used to play a traditional Byteland game on it. However, to play the game the players should first select such a subsegment of the a...
Input1 11
Output1
2 seconds
256 megabytes
['brute force', 'implementation', 'math', '*2100']
C. Nastya and a Wardrobetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputNastya received a gift on New Year — a magic wardrobe. It is magic because in the end of each month the number of dresses in it doubles (i.e. the number of dresses becomes twice as large as it is...
Input2 0
Output4
1 second
256 megabytes
['math', '*1600']
B. Nastya Studies Informaticstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputToday on Informatics class Nastya learned about GCD and LCM (see links below). Nastya is very intelligent, so she solved all the tasks momentarily and now suggests you to solve one of them a...
Input1 2 1 2
Output2
1 second
256 megabytes
['math', 'number theory', '*1600']
A. Nastya and an Arraytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputNastya owns too many arrays now, so she wants to delete the least important of them. However, she discovered that this array is magic! Nastya now knows that the array has the following properties: ...
Input51 1 1 1 1
Output1
1 second
256 megabytes
['implementation', 'sortings', '*800']
F. Concise and cleartime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputVasya is a regular participant at programming contests and is already experienced in finding important sentences in long statements. Of course, numbers constraints are important — factorization of a...
Input2018
Output2018
2 seconds
256 megabytes
['brute force', 'greedy', 'implementation', 'math', '*2700']
E. Bus Numbertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis night wasn't easy on Vasya. His favorite team lost, and he didn't find himself victorious either — although he played perfectly, his teammates let him down every time. He had to win at least one more t...
Input97
Output2
1 second
256 megabytes
['brute force', 'combinatorics', 'math', '*1800']
D. Bishwocktime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputBishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states: XX XX .X X.X. .X XX XX B...
Input0000
Output1
1 second
256 megabytes
['dp', 'greedy', '*1500']
C. Candiestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAfter passing a test, Vasya got himself a box of n candies. He decided to eat an equal amount of candies each morning until there are no more candies. However, Petya also noticed the box and decided to get som...
Input68
Output3
1 second
256 megabytes
['binary search', 'implementation', '*1500']
B. Getting an Atime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputTranslator's note: in Russia's most widespread grading system, there are four grades: 5, 4, 3, 2, the higher the better, roughly corresponding to A, B, C and F respectively in American grading system.The ...
Input34 4 4
Output2
1 second
256 megabytes
['greedy', 'sortings', '*900']
A. If at first you don't succeed...time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputEach student eagerly awaits the day he would pass the exams successfully. Thus, Vasya was ready to celebrate, but, alas, he didn't pass it. However, many of Vasya's fellow students fro...
Input10 10 5 20
Output5
1 second
256 megabytes
['implementation', '*1000']
G. GCD Countingtime limit per test4.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a tree consisting of n vertices. A number is written on each vertex; the number on vertex i is equal to a_i.Let's denote the function g(x, y) as the greatest common divisor of the numbers...
Input31 2 31 22 3
Output1 42 13 1
4.5 seconds
256 megabytes
['divide and conquer', 'dp', 'dsu', 'number theory', 'trees', '*2400']
F. Flow Controltime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have to handle a very complex water distribution system. The system consists of n junctions and m pipes, i-th pipe connects junctions x_i and y_i.The only thing you can do is adjusting the pipes. You...
Input43 -10 6 151 23 22 43 43 1
OutputPossible4-68-77
2 seconds
256 megabytes
['dfs and similar', 'dp', 'greedy', 'trees', '*2400']
E. Post Lampstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAdilbek's house is located on a street which can be represented as the OX axis. This street is really dark, so Adilbek wants to install some post lamps to illuminate it. Street has n positions to install l...
Input6 2 31 31 2 3
Output6
2 seconds
256 megabytes
['brute force', 'greedy', '*2100']
D. Graph And Its Complementtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven three numbers n, a, b. You need to find an adjacency matrix of such an undirected graph that the number of components in it is equal to a, and the number of components in its complement...
Input3 1 2
OutputYES001001110
2 seconds
256 megabytes
['constructive algorithms', 'graphs', 'implementation', '*1700']
C. Bracket Sequences Concatenation Problemtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputA bracket sequence is a string containing only characters "(" and ")".A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic express...
Input3)()(
Output2
2 seconds
256 megabytes
['implementation', '*1500']
B. Micro-Worldtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.You know that you have n bac...
Input7 1101 53 42 102 101 55 54
Output3
2 seconds
256 megabytes
['greedy', 'sortings', '*1200']
A. Commentary Boxestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputBerland Football Cup starts really soon! Commentators from all over the world come to the event.Organizers have already built n commentary boxes. m regional delegations will come to the Cup. Every de...
Input9 7 3 8
Output15
2 seconds
256 megabytes
['implementation', 'math', '*1000']
E. A Trance of Nightfalltime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output The cool breeze blows gently, the flowing water ripples steadily."Flowing and passing like this, the water isn't gone ultimately; Waxing and waning like that, the moon doesn't shrink or grow ev...
Input50 01 32 23 14 4101 12 13 14 15 13 23 33 43 53 6
Output0.500000000000000000000.500000000000000000000.333333333333333314830.500000000000000000000.500000000000000000000.185185185185185174910.152263374485596708620.144947416552354824140.143321648122745504140.14296036624949901017
2 seconds
256 megabytes
['dp', 'geometry', 'matrices', 'probabilities', '*2700']
D. A Shade of Moonlighttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output Gathering darkness shrouds the woods and the world. The moon sheds its light on the boat and the river."To curtain off the moonlight should be hardly possible; the shades present its mellow beau...
Input5 1 2-2 12 13 -15 -17 -1
Output4
2 seconds
256 megabytes
['binary search', 'geometry', 'math', 'sortings', 'two pointers', '*2500']
C. A Mist of Florescencetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output As the boat drifts down the river, a wood full of blossoms shows up on the riverfront."I've been here once," Mino exclaims with delight, "it's breathtakingly amazing.""What is it like?""Look, Ka...
Input5 3 2 1
Output4 7DDDDDDDDABACADDBABACDDDDDDDD
1 second
256 megabytes
['constructive algorithms', 'graphs', '*1800']
B. A Tide of Riverscapetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output Walking along a riverside, Mino silently takes a note of something."Time," Mino thinks aloud."What?""Time and tide wait for no man," explains Mino. "My name, taken from the river, always reminds ...
Input10 71.0.1.0.1.
Output1000100010
1 second
256 megabytes
['constructive algorithms', 'strings', '*1200']
A. A Blend of Springtimetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output When the curtains are opened, a canvas unfolds outside. Kanno marvels at all the blonde colours along the riverside — not tangerines, but blossoms instead."What a pity it's already late spring,"...
Input.BAC.
OutputYes
1 second
256 megabytes
['implementation', 'strings', '*900']
F. Rain and Umbrellastime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp lives on a coordinate line at the point x = 0. He goes to his friend that lives at the point x = a. Polycarp can move only from left to right, he can pass one unit of length each second.No...
Input10 2 43 78 100 103 48 11 2
Output14
2 seconds
256 megabytes
['dp', '*2100']
E. Divisibility by 25time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an integer n from 1 to 10^{18} without leading zeroes.In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain lead...
Input5071
Output4
1 second
256 megabytes
['brute force', 'greedy', '*2100']
D. Points and Powers of Twotime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n distinct points on a coordinate line, the coordinate of i-th point equals to x_i. Choose a subset of the given set of points such that the distance between each pair of points in ...
Input63 5 4 7 10 12
Output37 3 5
4 seconds
256 megabytes
['brute force', 'math', '*1800']
C. Equal Sumstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given k sequences of integers. The length of the i-th sequence equals to n_i.You have to choose exactly two sequences i and j (i \ne j) such that you can remove exactly one element in each of them ...
Input252 3 1 3 261 1 2 2 2 1
OutputYES2 61 2
2 seconds
256 megabytes
['implementation', 'sortings', '*1400']
B. Substrings Sorttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given n strings. Each string consists of lowercase English letters. Rearrange (reorder) the given strings in such a way that for every string, all strings that are placed before it are its subs...
Input5aabaabacababaaba
OutputYESabaabaabaabacaba
1 second
256 megabytes
['sortings', 'strings', '*1100']
A. Diverse Teamtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n students in a school class, the rating of the i-th student on Codehorses is a_i. You have to form a team consisting of k students (1 \le k \le n) such that the ratings of all team members are ...
Input5 315 13 15 15 12
OutputYES1 2 5
1 second
256 megabytes
['brute force', 'implementation', '*800']
C. Three displaystime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputIt is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.There are n displays placed ...
Input52 4 5 4 1040 30 20 10 40
Output90
1 second
256 megabytes
['brute force', 'dp', 'implementation', '*1400']
B. High School: Become Humantime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYear 2118. Androids are in mass production for decades now, and they do all the work for humans. But androids have to go to school to be able to solve creative tasks. Just like humans before....
Input5 8
Output>
1 second
256 megabytes
['math', '*1100']
A. Infinity Gauntlettime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou took a peek on Thanos wearing Infinity Gauntlet. In the Gauntlet there is a place for six Infinity Gems: the Power Gem of purple color, the Time Gem of green color, the Space Gem of blue color...
Input4redpurpleyelloworange
Output2SpaceTime
1 second
256 megabytes
['implementation', '*800']
F. Oppa Funcan Style Remasteredtime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputSurely you have seen insane videos by South Korean rapper PSY, such as "Gangnam Style", "Gentleman" and "Daddy". You might also hear that PSY has been recording video "Oppa Funcan Style" ...
Input37 73 85 6
OutputYESNOYES
5 seconds
256 megabytes
['graphs', 'math', 'number theory', 'shortest paths', '*3300']
E. Prince's Problemtime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputLet the main characters of this problem be personages from some recent movie. New Avengers seem to make a lot of buzz. I didn't watch any part of the franchise and don't know its heroes well, but it ...
Input41 21 31 46 4 9 532 3 62 3 23 4 7
Output3641
3 seconds
512 megabytes
['brute force', 'data structures', 'math', 'number theory', 'trees', '*2800']
D. Perfect Encodingtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are working as an analyst in a company working on a new system for big data storage. This system will store n different objects. Each object should have a unique ID.To create the system, you choo...
Input36
Output10
2 seconds
256 megabytes
['fft', 'math', '*3100']
C. AND Graphtime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a set of size m with integer elements between 0 and 2^{n}-1 inclusive. Let's build an undirected graph on these integers in the following way: connect two integers x and y with an edge if and ...
Input2 31 2 3
Output2
4 seconds
256 megabytes
['bitmasks', 'dfs and similar', 'dsu', 'graphs', '*2500']
B. Petr and Permutationstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputPetr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutat...
Input52 4 5 1 3
OutputPetr
2 seconds
256 megabytes
['combinatorics', 'math', '*1800']
A. Fairtime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputSome company is going to hold a fair in Byteland. There are n towns in Byteland and m two-way roads between towns. Of course, you can reach any town from any other town using roads.There are k types of goods pro...
Input5 5 4 31 2 4 3 21 22 33 44 14 5
Output2 2 2 2 3
2 seconds
512 megabytes
['graphs', 'greedy', 'number theory', 'shortest paths', '*1600']
G. Team Playerstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n players numbered from 0 to n-1 with ranks. The i-th player has rank i.Players can form teams: the team should consist of three players and no pair of players in the team should have a conflic...
Input4 02 3 4
Output64
2 seconds
256 megabytes
['combinatorics', '*2700']
F. Isomorphic Stringstime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a string s of length n consisting of lowercase English letters.For two given strings s and t, say S is the set of distinct characters of s and T is the set of distinct characters of t...
Input7 4abacaba1 1 11 4 22 1 32 4 3
OutputYESYESNOYES
3 seconds
256 megabytes
['hashing', 'strings', '*2300']
E. Pencils and Boxestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be rep...
Input6 3 107 2 7 7 4 2
OutputYES
2 seconds
256 megabytes
['binary search', 'data structures', 'dp', 'greedy', 'two pointers', '*2100']
D. Sand Fortresstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to p...
Input5 2
Output3
2 seconds
256 megabytes
['binary search', 'constructive algorithms', 'math', '*2100']
C. Liebig's Barrelstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exa...
Input4 2 12 2 1 2 3 2 2 3
Output7
2 seconds
256 megabytes
['greedy', '*1500']
B. Switches and Lampstime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given n switches and m lamps. The i-th switch turns on some subset of the lamps. This information is given as the matrix a consisting of n rows and m columns where ai, j = 1 if the i-th swi...
Input4 510101010000011110000
OutputYES
3 seconds
256 megabytes
['implementation', '*1200']
A. Chess Placingtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a chessboard of size 1 × n. It is guaranteed that n is even. The chessboard is painted like this: "BWBW...BW".Some cells of the board are occupied by the chess pieces. Each cell contains no...
Input61 2 6
Output2
1 second
256 megabytes
['implementation', '*1100']
B. Minesweepertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputOne day Alex decided to remember childhood when computers were not too powerful and lots of people played only default games. Alex enjoyed playing Minesweeper that time. He imagined that he saved world fro...
Input3 31111*1111
OutputYES
1 second
256 megabytes
['implementation', '*1100']
A. Gametime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputTwo players play a game.Initially there are n integers a_1, a_2, \ldots, a_n written on the board. Each turn a player selects one number and erases it from the board. This continues until there is only one numbe...
Input32 1 3
Output2
2 seconds
256 megabytes
['sortings', '*800']
E. NN countrytime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn the NN country, there are n cities, numbered from 1 to n, and n - 1 roads, connecting them. There is a roads path between any two cities.There are m bidirectional bus routes between cities. Buses drive ...
Input71 1 1 4 5 644 25 41 36 764 53 57 24 53 25 3
Output13-1123
3 seconds
256 megabytes
['binary search', 'data structures', 'trees', '*2800']
D. Arkady and Rectanglestime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputArkady has got an infinite plane painted in color 0. Then he draws n rectangles filled with paint with sides parallel to the Cartesian coordinate axes, one after another. The color of the i-th r...
Input5-1 -1 1 1-4 0 0 40 0 4 4-4 -4 0 00 -4 4 0
Output5
4 seconds
256 megabytes
['data structures', '*3300']
C. Elevatortime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou work in a big office. It is a 9 floor building with an elevator that can accommodate up to 4 people. It is your responsibility to manage this elevator.Today you are late, so there are queues on some floo...
Input23 55 3
Output10
3 seconds
256 megabytes
['dp', 'graphs', 'shortest paths', '*2400']
B. XOR-pyramidtime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputFor an array b of length m we define the function f as f(b) = \begin{cases} b[1] & \quad \text{if } m = 1 \\ f(b[1] \oplus b[2],b[2] \oplus b[3],\dots,b[m-1] \oplus b[m]) & \quad \text{otherwise,} \end{...
Input38 4 122 31 2
Output512
2 seconds
512 megabytes
['dp', '*1800']
A. Finite or not?time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given several queries. Each query consists of three integers p, q and b. You need to answer whether the result of p/q in notation with base b is a finite fraction.A fraction in notation with bas...
Input26 12 104 3 10
OutputFiniteInfinite
1 second
256 megabytes
['implementation', 'math', '*1700']
F. The Meeting Place Cannot Be Changedtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputPetr is a detective in Braginsk. Somebody stole a huge amount of money from a bank and Petr is to catch him. Somebody told Petr that some luxurious car moves along the roads without...
Input5 61 22 33 13 44 55 3
Output3
1 second
256 megabytes
['dfs and similar', 'graphs', '*2700']
E. Billiardtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputConsider a 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,...
Input4 3 2 2 -1 1
Output0 0
1 second
256 megabytes
['geometry', 'number theory', '*2600']
D. Sharktime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputFor long time scientists study the behavior of sharks. Sharks, as many other species, alternate short movements in a certain location and long movements between locations.Max is a young biologist. For n days he ...
Input81 2 7 3 4 8 5 6
Output7
1 second
256 megabytes
['brute force', 'data structures', 'dsu', 'trees', '*1900']
C. Cut 'em all!time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou're given a tree with n vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size.InputTh...
Input42 44 13 1
Output1
1 second
256 megabytes
['dfs and similar', 'dp', 'graphs', 'greedy', 'trees', '*1500']
B. Bus of Characterstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn the Bus of Characters there are n rows of seat, each having 2 seats. The width of both seats in the i-th row is w_i centimeters. All integers w_i are distinct.Initially the bus is empty. On each ...
Input23 10011
Output2 1 1 2
2 seconds
256 megabytes
['data structures', 'greedy', 'implementation', '*1300']