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
C. Hyperspace Highwaystime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn an unspecified solar system, there are N planets. A space government company has recently hired space contractors to build M bidirectional Hyperspace™ highways, each connecting two different pla...
Input5 7 21 21 31 42 32 43 41 51 42 5
Output12
1 second
256 megabytes
['dfs and similar', 'graphs', 'trees', '*2300']
B. Space Isaactime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputEverybody seems to think that the Martians are green, but it turns out they are metallic pink and fat. Ajs has two bags of distinct nonnegative integers. The bags are disjoint, and the union of the sets...
Input2 53 4
Output12
1.5 seconds
256 megabytes
['hashing', 'number theory', '*2400']
A. Last chancetime limit per test2 secondsmemory limit per test128 megabytesinputstandard inputoutputstandard outputIt is the year 2969. 1000 years have passed from the moon landing. Meanwhile, the humanity colonized the Hyperspace™ and lived in harmony.Until we realized that we were not alone.Not too far away from the...
Input3 50 1 42 5 4 11 1 4
Output42 13 21 42 5
2 seconds
128 megabytes
['data structures', 'flows', 'graph matchings', 'graphs', 'trees', '*2500']
F. DFStime limit per test6 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputLet T be a tree on n vertices. Consider a graph G_0, initially equal to T. You are given a sequence of q updates, where the i-th update is given as a pair of two distinct integers u_i and v_i. For every i from 1 ...
Input3 21 21 32 33 2
Output23
6 seconds
512 megabytes
['data structures', '*2700']
E. Grid Sorttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an n \times m grid. Each grid cell is filled with a unique integer from 1 to nm so that each integer appears exactly once.In one operation, you can choose an arbitrary cycle of the grid and mo...
Input3 34 1 27 6 38 5 9
Output18 1 4 7 8 5 6 3 2
2 seconds
256 megabytes
['implementation', '*3100']
D. Deduction Queriestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is an array a of 2^{30} integers, indexed from 0 to 2^{30}-1. Initially, you know that 0 \leq a_i < 2^{30} (0 \leq i < 2^{30}), but you do not know any of the values. Your task is to process q...
Input122 1 22 1 10737418221 0 3 42 0 02 3 32 0 31 6 7 32 4 41 0 2 12 0 02 4 42 0 0
Output-1-1-1-15-1635
2 seconds
256 megabytes
['data structures', 'dsu', '*2400']
C. Optimal Polygon Perimetertime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given n points on the plane. The polygon formed from all the n points is strictly convex, that is, the polygon is convex, and there are no three collinear points (i.e. lying in the s...
Input42 44 33 01 3
Output12 14
2 seconds
256 megabytes
['dp', 'geometry', '*2100']
B. Intersecting Subtreestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are playing a strange game with Li Chen. You have a tree with n nodes drawn on a piece of paper. All nodes are unlabeled and distinguishable. Each of you independently labeled the vertices f...
Input131 22 3111221
OutputA 1B 2C 1
2 seconds
256 megabytes
['dfs and similar', 'interactive', 'trees', '*1900']
A. The Tower is Going Hometime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputOn a chessboard with a width of 10^9 and a height of 10^9, the rows are numbered from bottom to top from 1 to 10^9, and the columns are numbered from left to right from 1 to 10^9. Therefore, fo...
Input2 3681 5 61 9 42 4 2
Output1
1 second
256 megabytes
['binary search', 'two pointers', '*1700']
G. Speckled Bandtime limit per test7 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputIldar took a band (a thin strip of cloth) and colored it. Formally, the band has n cells, each of them is colored into one of 26 colors, so we can denote each color with one of the lowercase letters of ...
Input9abcabcdce71 64 75 96 91 93 64 4
Output1-14322-1
7 seconds
512 megabytes
['data structures', 'divide and conquer', 'hashing', 'string suffix structures', 'strings', '*3500']
F. Make It Onetime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputJanusz is a businessman. He owns a company "Januszex", which produces games for teenagers. Last hit of Januszex was a cool one-person game "Make it one". The player is given a sequence of n integers a_i.I...
Input310 6 15
Output3
3 seconds
256 megabytes
['bitmasks', 'combinatorics', 'dp', 'math', 'number theory', 'shortest paths', '*2500']
E. Train Hard, Win Easytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputZibi is a competitive programming coach. There are n competitors who want to be prepared well. The training contests are quite unusual – there are two people in a team, two problems, and each com...
Input3 21 22 31 31 22 3
Output3 0 3
2 seconds
256 megabytes
['constructive algorithms', 'greedy', 'math', 'sortings', '*1900']
D. Mysterious Crimetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAcingel is a small town. There was only one doctor here — Miss Ada. She was very friendly and nobody has ever said something bad about her, so who could've expected that Ada will be found dead in her ...
Input3 21 2 32 3 1
Output4
1 second
256 megabytes
['brute force', 'combinatorics', 'math', 'meet-in-the-middle', 'two pointers', '*1700']
C. Smallest Wordtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputIA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and ...
Inputbbab
Output0 1 1 0
1 second
256 megabytes
['constructive algorithms', 'greedy', 'implementation', '*1500']
B. Lost Arraytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputBajtek, known for his unusual gifts, recently got an integer array x_0, x_1, \ldots, x_{k-1}.Unfortunately, after a huge array-party with his extraordinary friends, he realized that he'd lost it. After hour...
Input51 2 3 4 5
Output51 2 3 4 5
1 second
256 megabytes
['implementation', '*1200']
A. Electionstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAwruk is taking part in elections in his school. It is the final round. He has only one opponent — Elodreip. The are n students in the school. Each student has exactly k votes and is obligated to use all of ...
Input51 1 1 5 1
Output5
1 second
256 megabytes
['implementation', 'math', '*800']
F. Leaf Setstime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an undirected tree, consisting of n vertices.The vertex is called a leaf if it has exactly one vertex adjacent to it.The distance between some pair of vertices is the number of edges in the sh...
Input9 31 21 32 42 53 66 76 83 9
Output2
3 seconds
256 megabytes
['data structures', 'dfs and similar', 'dsu', 'graphs', 'greedy', 'sortings', 'trees', '*2400']
E. Vasya and Magic Matrixtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputVasya has got a magic matrix a of size n \times m. The rows of the matrix are numbered from 1 to n from top to bottom, the columns are numbered from 1 to m from left to right. Let a_{ij} be the...
Input1 41 1 2 11 3
Output2
3 seconds
256 megabytes
['dp', 'math', 'probabilities', '*2300']
D. Petya and Arraytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputPetya has an array a consisting of n integers. He has learned partial sums recently, and now he can calculate the sum of elements on any segment of the array really fast. The segment is a non-empty se...
Input5 45 -1 3 4 -1
Output5
2 seconds
256 megabytes
['data structures', 'divide and conquer', 'two pointers', '*1800']
C. Array Producttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a consisting of n integers. You can perform the following operations with it: Choose some positions i and j (1 \le i, j \le n, i \ne j), write the value of a_i \cdot a_j into the...
Input55 -2 0 1 -3
Output2 31 1 21 2 41 4 5
1 second
256 megabytes
['constructive algorithms', 'greedy', 'math', '*1700']
B. Vitaminstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputBerland shop sells n kinds of juices. Each juice has its price c_i. Each juice includes some set of vitamins in it. There are three types of vitamins: vitamin "A", vitamin "B" and vitamin "C". Each juice can...
Input45 C6 B16 BAC4 A
Output15
2 seconds
256 megabytes
['bitmasks', 'brute force', 'dp', 'implementation', '*1200']
A. Benchestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n benches in the Berland Central park. It is known that a_i people are currently sitting on the i-th bench. Another m people are coming to the park and each of them is going to have a seat on some be...
Input461111
Output3 7
1 second
256 megabytes
['binary search', 'implementation', '*1100']
F. Ray in the tubetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a tube which is reflective inside represented as two non-coinciding, but parallel to Ox lines. Each line has some special integer points — positions of sensors on sides of the tube.You a...
Input3 11 5 61 33
Output3
2 seconds
256 megabytes
['data structures', 'divide and conquer', 'dp', 'math', '*2500']
E. Tree Reconstructiontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputMonocarp has drawn a tree (an undirected connected acyclic graph) and then has given each vertex an index. All indices are distinct numbers from 1 to n. For every edge e of this tree, Monocarp has ...
Input43 41 43 4
OutputYES1 33 22 4
1 second
256 megabytes
['constructive algorithms', 'data structures', 'graphs', 'greedy', '*1900']
D. Glidertime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputA plane is flying at a constant height of h meters above the ground surface. Let's consider that it is flying from the point (-10^9, h) to the point (10^9, h) parallel with Ox axis.A glider is inside the plane...
Input3 42 57 910 11
Output10
2 seconds
256 megabytes
['binary search', 'data structures', 'two pointers', '*1700']
C. Coffee Breaktime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputRecently Monocarp got a job. His working day lasts exactly m minutes. During work, Monocarp wants to drink coffee at certain moments: there are n minutes a_1, a_2, \dots, a_n, when he is able and willing...
Input4 5 33 5 1 2
Output33 1 1 2
2 seconds
256 megabytes
['binary search', 'data structures', 'greedy', 'two pointers', '*1600']
B. Buying a TV Settime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputMonocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not grea...
Input17 15 5 3
Output3
1 second
256 megabytes
['math', '*1000']
A. Heisttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere was an electronic store heist last night.All keyboards which were in the store yesterday were numbered in ascending order from some integer number x. For example, if x = 4 and there were 3 keyboards in the...
Input410 13 12 8
Output2
1 second
256 megabytes
['greedy', 'implementation', 'sortings', '*800']
B. Shashlik Cookingtime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputLong story short, shashlik is Miroslav's favorite food. Shashlik is prepared on several skewers simultaneously. There are two states for each skewer: initial and turned over.This time Miroslav laid ou...
Input7 2
Output21 6
1 second
512 megabytes
['dp', 'greedy', 'math', '*1300']
A. Palindrome Dancetime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputA group of n dancers rehearses a performance for the closing ceremony. The dancers are arranged in a row, they've studied their dancing moves and can't change positions. For some of them, a white danc...
Input5 100 10 1 2 1 2
Output101
1 second
512 megabytes
['greedy', '*1000']
E. Summer Oenothera Exhibitiontime limit per test7 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputWhile some people enjoy spending their time solving programming contests, Dina prefers taking beautiful pictures. As soon as Byteland Botanical Garden announced Summer Oenothera Exhibition...
Input3 6 52 4 01 2 3 4 5
Output00112
7 seconds
512 megabytes
['data structures', '*3400']
D. You Are Given a Treetime limit per test7 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputA tree is an undirected graph with exactly one simple path between each pair of vertices. We call a set of simple paths k-valid if each vertex of the tree belongs to no more than one of these pat...
Input71 22 33 44 55 66 7
Output7321111
7 seconds
512 megabytes
['data structures', 'dp', 'trees', '*2800']
C. Network Safetytime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThe Metropolis computer network consists of n servers, each has an encryption key in the range from 0 to 2^k - 1 assigned to it. Let c_i be the encryption key assigned to the i-th server. Additionally,...
Input4 4 20 1 0 11 22 33 44 1
Output50
3 seconds
512 megabytes
['dfs and similar', 'dsu', 'graphs', 'math', 'sortings', '*2200']
B. Subway Pursuittime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThis is an interactive problem.In the Wonderful Metropolis of the Future, there is no need in subway train drivers. Due to the technological progress, they were replaced by the Artificial Intelligence ...
Input10 2YesNoYesYes
Output3 53 33 45 5
2 seconds
512 megabytes
['binary search', 'interactive', 'probabilities', '*2100']
A. Timetabletime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThere are two bus stops denoted A and B, and there n buses that go from A to B every day. The shortest path from A to B takes t units of time but some buses might take longer paths. Moreover, buses are allo...
Input3 104 6 82 2 3
OutputYes16 17 21
2 seconds
512 megabytes
['constructive algorithms', 'data structures', 'greedy', 'math', '*2300']
F. Wrap Aroundtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a binary string s.Find the number of distinct cyclical binary strings of length n which contain s as a substring.The cyclical string t contains s as a substring if there is some cyclical shi...
Input20
Output3
2 seconds
256 megabytes
['dp', 'strings', '*2900']
E. Maximum Matchingtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given n blocks, each of them is of the form [color_1|value|color_2], where the block can also be flipped to get [color_2|value|color_1]. A sequence of blocks is called valid if the touching e...
Input62 1 41 2 43 4 42 8 33 16 31 32 2
Output63
2 seconds
256 megabytes
['bitmasks', 'brute force', 'dfs and similar', 'dp', 'graphs', '*2400']
D. Slimetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it.Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that thi...
Input42 1 2 1
Output4
2 seconds
256 megabytes
['dp', 'greedy', 'implementation', '*1800']
C. Gamblingtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputTwo players A and B have a list of n integers each. They both want to maximize the subtraction between their score and their opponent's score. In one turn, a player can either add to his score any element fr...
Input21 45 1
Output0
2 seconds
256 megabytes
['greedy', 'sortings', '*1300']
B. Non-Coprime Partitiontime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputFind out if it is possible to partition the first n positive integers into two non-empty disjoint sets S_1 and S_2 such that:\mathrm{gcd}(\mathrm{sum}(S_1), \mathrm{sum}(S_2)) > 1 Here \mathrm{s...
Input1
OutputNo
2 seconds
256 megabytes
['constructive algorithms', 'math', '*1100']
A. Equalitytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a string s of length n, which consists only of the first k letters of the Latin alphabet. All letters in string s are uppercase.A subsequence of string s is a string that can be derived from s ...
Input9 3ACAABCCAB
Output6
2 seconds
256 megabytes
['implementation', 'strings', '*800']
H. Securitytime limit per test4 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputSome programming website is establishing a secure communication protocol. For security reasons, they want to choose several more or less random strings.Initially, they have a string s consisting of lowercase...
Inputbaa51 2 ba2 3 a1 2 b2 3 aa1 3 b
Output-1aaba-1ba
4 seconds
512 megabytes
['data structures', 'string suffix structures', '*3200']
G. A Game on Stringstime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAlice and Bob are playing a game on strings.Initially, they have some string t. In one move the first player selects the character c present in t and erases all it's occurrences in t, thus splitting...
Inputaaab21 21 4
OutputAliceBob
4 seconds
256 megabytes
['games', '*3200']
F. Maximum Reductiontime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven an array a of n integers and an integer k (2 \le k \le n), where each element of the array is denoted by a_i (0 \le i < n). Perform the operation z given below on a and print the value of z(a,...
Input3 29 1 10
Output29
2 seconds
256 megabytes
['combinatorics', 'data structures', 'math', '*2500']
E. Tripstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.We want to plan a trip for every evening of m days. On each trip, you have to s...
Input4 4 22 31 21 31 4
Output0033
2 seconds
256 megabytes
['graphs', '*2200']
D. Valid BFS?time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe BFS algorithm is defined as follows. Consider an undirected graph with vertices numbered from 1 to n. Initialize q as a new queue containing only vertex 1, mark the vertex 1 as used. Extract a vertex...
Input41 21 32 41 2 3 4
OutputYes
2 seconds
256 megabytes
['dfs and similar', 'graphs', 'shortest paths', 'trees', '*1700']
C. Equalizetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two binary strings a and b of the same length. You can perform the following two operations on the string a: Swap any two bits at indices i and j respectively (1 \le i, j \le n), the cost of th...
Input3100001
Output2
1 second
256 megabytes
['dp', 'greedy', 'strings', '*1300']
B. Reach Mediantime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a of n integers and an integer s. It is guaranteed that n is odd.In one operation you can either increase or decrease any single element by one. Calculate the minimum number of oper...
Input3 86 5 8
Output2
1 second
256 megabytes
['greedy', '*1300']
A. Packetstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have n coins, each of the same value of 1.Distribute them into packets such that any amount x (1 \leq x \leq n) can be formed using some (possibly one or all) number of these packets.Each packet may only b...
Input6
Output3
1 second
256 megabytes
['constructive algorithms', 'greedy', 'math', '*1300']
G. Sources and Sinkstime limit per test7 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given an acyclic directed graph, consisting of n vertices and m edges. The graph contains no multiple edges and no self-loops.The vertex is called a source if it has no incoming edges. The v...
Input3 11 2
OutputNO
7 seconds
512 megabytes
['bitmasks', 'brute force', 'dfs and similar', '*2700']
F. Relatively Prime Powerstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputConsider some positive integer x. Its prime factorization will be of form x = 2^{k_1} \cdot 3^{k_2} \cdot 5^{k_3} \cdot \dotsLet's call x elegant if the greatest common divisor of the sequence...
Input4427210
Output21616
2 seconds
256 megabytes
['combinatorics', 'math', 'number theory', '*2400']
E. Covered Pointstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given n segments on a Cartesian plane. Each segment's endpoints have integer coordinates. Segments can intersect with each other. No two segments lie on the same line.Count the number of distin...
Input90 0 4 4-1 5 4 04 0 4 45 2 11 26 1 6 75 6 11 610 1 10 77 0 9 810 -1 11 -1
Output42
2 seconds
256 megabytes
['fft', 'geometry', 'number theory', '*2400']
D. Vasya and Arraystime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputVasya has two arrays A and B of lengths n and m, respectively.He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and re...
Input511 2 3 5 7411 7 3 7
Output3
1 second
256 megabytes
['greedy', 'two pointers', '*1600']
C. Classy Numberstime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputLet's call some positive integer classy if its decimal representation contains no more than 3 non-zero digits. For example, numbers 4, 200000, 10203 are classy and numbers 4231, 102306, 7277420000 are ...
Input41 10001024 102465536 65536999999 1000001
Output1000102
3 seconds
256 megabytes
['combinatorics', 'dp', '*1900']
B. Diagonal Walking v.2time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputMikhail walks on a Cartesian plane. He starts at the point (0, 0), and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point (0, 0), he can go to...
Input32 2 34 3 710 1 9
Output16-1
1 second
256 megabytes
['math', '*1600']
A. Function Heighttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a set of 2n+1 integer points on a Cartesian plane. Points are numbered from 0 to 2n inclusive. Let P_i be the i-th point. The x-coordinate of the point P_i equals i. The y-coordinate of t...
Input4 3
Output1
1 second
256 megabytes
['math', '*1000']
E. Little C Loves 3 IIItime limit per test1 secondmemory limit per test64 megabytesinputstandard inputoutputstandard outputLittle C loves number «3» very much. He loves all things about it.Now he is interested in the following problem:There are two arrays of 2^n intergers a_0,a_1,...,a_{2^n-1} and b_0,b_1,...,b_{2^n-1}...
Input11111
Output12
1 second
64 megabytes
['bitmasks', 'dp', 'math', '*3200']
D. Intervals of Intervalstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputLittle D is a friend of Little C who loves intervals very much instead of number "3".Now he has n intervals on the number axis, the i-th of which is [a_i,b_i].Only the n intervals can not satis...
Input2 11 32 4
Output3
2 seconds
256 megabytes
['binary search', 'data structures', 'two pointers', '*3500']
C. Region Separationtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n cities in the Kingdom of Autumn, numbered from 1 to n. People can travel between any two cities using n-1 two-directional roads.This year, the government decides to separate the kingdom....
Input41 1 1 11 2 3
Output4
2 seconds
256 megabytes
['combinatorics', 'dp', 'number theory', 'trees', '*2700']
B. Little C Loves 3 IItime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputLittle C loves number «3» very much. He loves all things about it.Now he is playing a game on a chessboard of size n \times m. The cell in the x-th row and in the y-th column is called (x,y). Initi...
Input2 2
Output0
1 second
256 megabytes
['brute force', 'constructive algorithms', 'flows', 'graph matchings', '*2200']
A. Enlarge GCDtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputMr. F has n positive integers, a_1, a_2, \ldots, a_n.He thinks the greatest common divisor of these integers is too small. So he wants to enlarge it by removing some of the integers.But this problem is too...
Input31 2 4
Output1
1 second
256 megabytes
['number theory', '*1800']
G. Chip Gametime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAlice and Bob decided to play one ultimate game. They have n piles, the i-th pile initially contain v_i chips. Alice selects a positive integer a from interval [1, m], and Bob selects a number b the same wa...
Input2 24 5
Output1 1 1 1
5 seconds
256 megabytes
['games', '*3500']
F. Boolean Computertime limit per test7 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAlice has a computer that operates on w-bit integers. The computer has n registers for values. The current content of the registers is given as an array a_1, a_2, \ldots, a_n. The computer uses so-ca...
Input4 3 113 10 6AXoA
Output3
7 seconds
256 megabytes
['bitmasks', 'brute force', 'fft', 'math', '*2800']
E. Hidden Bipartite Graphtime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputBob has a simple undirected connected graph (without self-loops and multiple edges). He wants to learn whether his graph is bipartite (that is, you can paint all vertices of the graph into two ...
Input4401110
Output? 4 1 2 3 4? 21 2? 21 3? 21 4? 22 4? 23 4Y 21 2
4 seconds
256 megabytes
['binary search', 'constructive algorithms', 'dfs and similar', 'graphs', 'interactive', '*2800']
D. Divisorstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given n integers a_1, a_2, \ldots, a_n. Each of a_i has between 3 and 5 divisors. Consider a = \prod a_i — the product of all input integers. Find the number of divisors of a. As this number may be ve...
Input3915143
Output32
1 second
256 megabytes
['interactive', 'math', 'number theory', '*2000']
C. Permutation Gametime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAfter a long day, Alice and Bob decided to play a little game. The game board consists of n cells in a straight line, numbered from 1 to n, where each cell contains a number a_i between 1 and n. Furth...
Input83 6 5 4 2 7 1 8
OutputBAAAABAB
1 second
256 megabytes
['brute force', 'dp', 'games', '*1600']
B. Square Differencetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAlice has a lovely piece of cloth. It has the shape of a square with a side of length a centimeters. Bob also wants such piece of cloth. He would prefer a square with a side of length b centimeters ...
Input46 516 1361690850361 2477762263034 33
OutputYESNONOYES
2 seconds
256 megabytes
['math', 'number theory', '*1100']
A. King Escapetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAlice and Bob are playing chess on a huge chessboard with dimensions n \times n. Alice has a single piece left — a queen, located at (a_x, a_y), while Bob has only the king standing at (b_x, b_y). Alice th...
Input84 41 33 1
OutputYES
1 second
256 megabytes
['dfs and similar', 'graphs', 'implementation', '*1000']
G. Chatteringtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n parrots standing in a circle. Each parrot has a certain level of respect among other parrots, namely r_i. When a parrot with respect level x starts chattering, x neighbours to the right and to ...
4 1 1 4 1
2 2 1 2
2 seconds
256 megabytes
['*2900']
F. Vasya and Maximum Matchingtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputVasya has got a tree consisting of n vertices. He wants to delete some (possibly zero) edges in this tree such that the maximum matching in the resulting graph is unique. He asks you to cal...
Input41 21 31 4
Output4
3 seconds
256 megabytes
['dp', 'trees', '*2400']
E. The Unbearable Lightness of Weightstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a set of n weights. You know that their masses are a_1, a_2, ..., a_n grams, but you don't know which of them has which mass. You can't distinguish the weights.However, you...
4 1 4 2 2
2
1 second
256 megabytes
['dp', 'math', '*2100']
D. Barcelonian Distancetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn this problem we consider a very simplified model of Barcelona city.Barcelona can be represented as a plane with streets of kind x = c and y = c for every integer c (that is, the rectangular gri...
1 1 -3 0 3 3 0
4.2426406871
1 second
256 megabytes
['geometry', 'implementation', '*1900']
C. Playing Pianotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputLittle Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence a_1, a_2, \ldots, a_n of key numbers: the more a numb...
5 1 1 4 2 2
1 4 5 4 5
1 second
256 megabytes
['constructive algorithms', 'dp', '*1700']
B. Personalized Cuptime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAt many competitions that have a word «cup» in its official name the winner is presented with an actual cup. This time the organizers of one unusual programming competition have decided to please the ...
tourist
1 7 tourist
1 second
256 megabytes
['*1200']
A. Kitchen Utensilstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe king's birthday dinner was attended by k guests. The dinner was quite a success: every person has eaten several dishes (though the number of dishes was the same for every person) and every dish wa...
5 2 1 2 2 1 3
1
1 second
256 megabytes
['*900']
F. Familiar Operationstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two positive integers a and b. There are two possible operations: multiply one of the numbers by some prime p; divide one of the numbers on its prime factor p. What is the minimum...
Input89 10100 17220 7017 194 1832 20100 32224 385
Output13101011
2 seconds
256 megabytes
['brute force', 'graphs', 'math', '*2800']
E. Triple Flipstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a of length n that consists of zeros and ones.You can perform the following operation multiple times. The operation consists of two steps: Choose three integers 1 \le x < y < z \l...
Input51 1 0 1 1
OutputYES21 3 52 3 4
1 second
256 megabytes
['constructive algorithms', '*2600']
D. Minimum pathtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a matrix of size n \times n filled with lowercase English letters. You can change no more than k letters in this matrix.Consider all paths from the upper left corner to the lower right co...
Input4 2abcdbcdebcadbcde
Outputaaabcde
1.5 seconds
256 megabytes
['greedy', '*1900']
C. Cram Timetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn a galaxy far, far away Lesha the student has just got to know that he has an exam in two days. As always, he hasn't attended any single class during the previous year, so he decided to spend the remaining...
Input3 3
Output13 22 1
1 second
256 megabytes
['greedy', '*1600']
B. Curiosity Has No Limitstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputWhen Masha came to math classes today, she saw two integer sequences of length n - 1 on the blackboard. Let's denote the elements of the first sequence as a_i (0 \le a_i \le 3), and the element...
Input43 3 21 2 0
OutputYES1 3 2 0
1 second
256 megabytes
['*1500']
A. Golden Platetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a plate and you want to add some gilding to it. The plate is a rectangle that we split into w\times h cells. There should be k gilded rings, the first one should go along the edge of the plate, t...
Input3 3 1
Output8
1 second
256 megabytes
['implementation', 'math', '*800']
G. Linear Congruential Generatortime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given a tuple generator f^{(k)} = (f_1^{(k)}, f_2^{(k)}, \dots, f_n^{(k)}), where f_i^{(k)} = (a_i \cdot f_i^{(k - 1)} + b_i) \bmod p_i and f^{(0)} = (x_1, x_2, \dots, x_n). Here...
Input42 3 5 7
Output210
2 seconds
512 megabytes
['number theory', '*2900']
F. Putting Boxes Togethertime limit per test2.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is an infinite line consisting of cells. There are n boxes in some cells of this line. The i-th box stands in the cell a_i and has weight w_i. All a_i are distinct, moreover, a_{i - 1} ...
Input5 81 2 6 7 101 1 1 1 21 11 51 33 5-3 5-1 101 42 5
Output01034187
2.5 seconds
256 megabytes
['data structures', '*2500']
E. Vasya and Good Sequencestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputVasya has a sequence a consisting of n integers a_1, a_2, \dots, a_n. Vasya may pefrom the following operation: choose some number from the sequence and swap any pair of bits in its binary re...
Input36 7 14
Output2
2 seconds
256 megabytes
['bitmasks', 'dp', '*2000']
D. Vasya and Triangletime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputVasya has got three integers n, m and k. He'd like to find three integer points (x_1, y_1), (x_2, y_2), (x_3, y_3), such that 0 \le x_1, x_2, x_3 \le n, 0 \le y_1, y_2, y_3 \le m and the area of the...
Input4 3 3
OutputYES1 02 34 1
1 second
256 megabytes
['geometry', 'number theory', '*1800']
C. Vasya and Golden Tickettime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputRecently Vasya found a golden ticket — a sequence which consists of n digits a_1a_2\dots a_n. Vasya considers a ticket to be lucky if it can be divided into two or more non-intersecting segment...
Input573452
OutputYES
1 second
256 megabytes
['implementation', '*1300']
B. Vasya and Cornfieldtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputVasya owns a cornfield which can be defined with two integers n and d. The cornfield can be represented as rectangle with vertices having Cartesian coordinates (0, d), (d, 0), (n, n - d) and (n - d...
Input7 242 44 16 34 5
OutputYESNONOYES
1 second
256 megabytes
['geometry', '*1100']
A. In Search of an Easy Problemtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputWhen preparing a tournament, Codeforces coordinators try treir best to make the first problem as easy as possible. This time the coordinator had chosen some problem and asked n people abou...
Input30 0 1
OutputHARD
1 second
256 megabytes
['implementation', '*800']
F. Multicolored Markerstime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is an infinite board of square tiles. Initially all tiles are white.Vova has a red marker and a blue marker. Red marker can color a tiles. Blue marker can color b tiles. If some tile isn't ...
Input4 4
Output12
3 seconds
256 megabytes
['binary search', 'brute force', 'math', 'number theory', '*2000']
E. Tree with Small Distancestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an undirected tree consisting of n vertices. An undirected tree is a connected undirected graph with n - 1 edges.Your task is to add the minimum number of edges in such a way th...
Input71 22 32 44 54 65 7
Output2
1 second
256 megabytes
['dp', 'graphs', 'greedy', '*2100']
D. Concatenated Multiplestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a, consisting of n positive integers.Let's call a concatenation of numbers x and y the number that is obtained by writing down numbers x and y one right after another wit...
Input6 1145 1 10 12 11 7
Output7
2 seconds
256 megabytes
['implementation', 'math', '*1900']
C. Maximal Intersectiontime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given n segments on a number line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in eac...
Input41 32 60 43 3
Output1
3 seconds
256 megabytes
['greedy', 'math', 'sortings', '*1600']
B. Creating the Contesttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a problemset consisting of n problems. The difficulty of the i-th problem is a_i. It is guaranteed that all difficulties are distinct and are given in the increasing order.You have t...
Input101 2 5 6 7 10 21 23 24 49
Output4
1 second
256 megabytes
['dp', 'greedy', 'math', '*1200']
A. Many Equal Substringstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a string t consisting of n lowercase Latin letters and an integer number k.Let's define a substring of some string s with indices from l to r as s[l \dots r].Your task is to constru...
Input3 4aba
Outputababababa
1 second
256 megabytes
['implementation', 'strings', '*1300']
H. Make Squaretime limit per test7 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputWe call an array b_1, b_2, \ldots, b_m good, if there exist two indices i < j such that b_i \cdot b_j is a perfect square.Given an array b_1, b_2, \ldots, b_m, in one action you can perform one of the fo...
Input10 1034 37 28 16 44 36 43 50 22 131 34 86 109 103 108 95 61 41 72 6
Output2013011100
7 seconds
1024 megabytes
['math', '*2900']
G. Guess the numbertime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis problem is interactive.You should guess hidden number x which is between 1 and M = 10004205361450474, inclusive.You could use up to 5 queries.In each query, you can output an increasing sequence...
Input 20-1
Output2 2 32 20 303 5 7 9
2 seconds
256 megabytes
['dp', 'interactive', '*3000']
F. Make Symmetricaltime limit per test6 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputConsider a set of points A, initially it is empty. There are three types of queries: Insert a point (x_i, y_i) to A. It is guaranteed that this point does not belong to A at this moment. Remove a ...
Input121 1 61 6 11 5 51 2 33 4 41 3 23 7 72 2 32 6 13 8 82 5 53 1 1
Output1022
6 seconds
512 megabytes
['brute force', '*2900']
E. Restore Arraytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputWhile discussing a proper problem A for a Codeforces Round, Kostya created a cyclic array of positive integers a_1, a_2, \ldots, a_n. Since the talk was long and not promising, Kostya created a new cycl...
Input41 3 1 0
OutputYES1 3 5 2
2 seconds
256 megabytes
['constructive algorithms', '*2400']
D. Order booktime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputLet's consider a simplified version of order book of some stock. The order book is a list of orders (offers) from people that want to buy or sell one unit of the stock, each order is described by direction...
Input6ADD 1ACCEPT 1ADD 2ACCEPT 2ADD 3ACCEPT 3
Output8
2 seconds
256 megabytes
['combinatorics', 'data structures', 'greedy', '*2100']
C. Rectanglestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given n rectangles on a plane with coordinates of their bottom left and upper right points. Some (n-1) of the given n rectangles have some common point. A point belongs to a rectangle if this point...
Input30 0 1 11 1 2 23 0 4 1
Output1 1
2 seconds
256 megabytes
['geometry', 'implementation', 'sortings', '*1600']