contest_id int32 1 2.13k | index stringclasses 62
values | problem_id stringlengths 2 6 | title stringlengths 0 67 | rating int32 0 3.5k | tags stringlengths 0 139 | statement stringlengths 0 6.96k | input_spec stringlengths 0 2.32k | output_spec stringlengths 0 1.52k | note stringlengths 0 5.06k | sample_tests stringlengths 0 1.02k | difficulty_category stringclasses 6
values | tag_count int8 0 11 | statement_length int32 0 6.96k | input_spec_length int16 0 2.32k | output_spec_length int16 0 1.52k | contest_year int16 0 21 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1,614 | E | 1614E | E. Divan and a Cottage | 2,600 | binary search; data structures | Divan's new cottage is finally complete! However, after a thorough inspection, it turned out that the workers had installed the insulation incorrectly, and now the temperature in the house directly depends on the temperature outside. More precisely, if the temperature in the house is \(P\) in the morning, and the stree... | The first line of the input contains the number \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) — the number of days.The following is a description of \(n\) days in the following format.The first line of the description contains an integer \(T_i\) (\(0 \leq T_i \leq 10^9\)) — the temperature on that day.The second line contains... | For each query, output a single integer — the temperature in the house after day \(i\). | Let's look at the first four queries from the example input.The temperature is \(50\) on the first day, \(50\) on the second day, and \(0\) on the third day.Note that \(lastans = 0\) initially. The initial temperature of the first query of the first day is \((1 \, + \, lastans) \bmod (10^9 + 1) = 1\). After the first d... | Input: 3 50 3 1 2 3 50 3 4 5 6 0 3 7 8 9 | Output: 2 5 9 15 22 30 38 47 53 | Expert | 2 | 1,185 | 884 | 87 | 16 |
1,547 | D | 1547D | D. Co-growing Sequence | 1,300 | bitmasks; constructive algorithms; greedy | A sequence of non-negative integers \(a_1, a_2, \dots, a_n\) is called growing if for all \(i\) from \(1\) to \(n - 1\) all ones (of binary representation) in \(a_i\) are in the places of ones (of binary representation) in \(a_{i + 1}\) (in other words, \(a_i \:\&\: a_{i + 1} = a_i\), where \(\&\) denotes bitwise AND).... | The first line contains an integer \(t\) (\(1 \le t \le 10^4\)). Then \(t\) test cases follow.The first line of each test case contains an integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — length of the sequence \(x_i\).The second line contains \(n\) integers \(x_1, x_2, \dots, x_n\) (\(0 \le x_i < 2^{30}\)) — elements of... | For each test case, print \(n\) integers \(y_1, y_2, \dots, y_n\) (\(0 \le y_i < 2^{30}\)) — lexicographically minimal sequence such that such that it's co-growing with given sequence \(x_i\). | Input: 5 4 1 3 7 15 4 1 2 4 8 5 1 2 3 4 5 4 11 13 15 1 1 0 | Output: 0 0 0 0 0 1 3 7 0 1 0 3 2 0 2 0 14 0 | Easy | 3 | 1,570 | 436 | 192 | 15 | |
1,750 | C | 1750C | C. Complementary XOR | 1,400 | constructive algorithms; implementation | You have two binary strings \(a\) and \(b\) of length \(n\). You would like to make all the elements of both strings equal to \(0\). Unfortunately, you can modify the contents of these strings using only the following operation: You choose two indices \(l\) and \(r\) (\(1 \le l \le r \le n\)); For every \(i\) that resp... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) — the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) — the length of the strings.The second line... | For each testcase, print first ""YES"" if it's possible to make all the elements of both strings equal to \(0\). Otherwise, print ""NO"". If the answer is ""YES"", on the next line print a single integer \(k\) (\(0 \le k \le n + 5\)) — the number of operations. Then \(k\) lines follows, each contains two integers \(l\)... | In the first test case, we can perform one operation with \(l = 2\) and \(r = 2\). So \(a_2 := 1 - 1 = 0\) and string \(a\) became equal to 000. \(b_1 := 1 - 1 = 0\), \(b_3 := 1 - 1 = 0\) and string \(b\) became equal to 000.In the second and in the third test cases, it can be proven that it's impossible to make all el... | Input: 5301010121110410000011210103111111 | Output: YES 1 2 2 NO NO YES 2 1 2 2 2 YES 2 1 1 2 3 | Easy | 2 | 806 | 633 | 448 | 17 |
1,293 | B | 1293B | B. JOE is on TV! | 1,000 | combinatorics; greedy; math | 3R2 - Standby for ActionOur dear Cafe's owner, JOE Miller, will soon take part in a new game TV-show ""1 vs. \(n\)""!The game goes in rounds, where in each round the host asks JOE and his opponents a common question. All participants failing to answer are eliminated. The show ends when only JOE remains (we assume that ... | The first and single line contains a single integer \(n\) (\(1 \le n \le 10^5\)), denoting the number of JOE's opponents in the show. | Print a number denoting the maximum prize (in dollars) JOE could have.Your answer will be considered correct if it's absolute or relative error won't exceed \(10^{-4}\). In other words, if your answer is \(a\) and the jury answer is \(b\), then it must hold that \(\frac{|a - b|}{max(1, b)} \le 10^{-4}\). | In the second example, the best scenario would be: one contestant fails at the first question, the other fails at the next one. The total reward will be \(\displaystyle \frac{1}{2} + \frac{1}{1} = 1.5\) dollars. | Input: 1 | Output: 1.000000000000 | Beginner | 3 | 806 | 133 | 305 | 12 |
893 | F | 893F | F. Subtree Minimum Query | 2,300 | data structures; trees | You are given a rooted tree consisting of n vertices. Each vertex has a number written on it; number ai is written on vertex i.Let's denote d(i, j) as the distance between vertices i and j in the tree (that is, the number of edges in the shortest path from i to j). Also let's denote the k-blocked subtree of vertex x as... | The first line contains two integers n and r (1 ≤ r ≤ n ≤ 100000) — the number of vertices in the tree and the index of the root, respectively.The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the numbers written on the vertices.Then n - 1 lines follow, each containing two integers x and y (1 ≤ x, y ... | Print m integers. i-th of them has to be equal to the answer to i-th query. | Input: 5 21 3 2 3 52 35 13 44 121 22 3 | Output: 25 | Expert | 2 | 782 | 785 | 75 | 8 | |
120 | F | 120F | F. Spiders | 1,400 | dp; greedy; trees | One day mum asked Petya to sort his toys and get rid of some of them. Petya found a whole box of toy spiders. They were quite dear to him and the boy didn't want to throw them away. Petya conjured a cunning plan: he will glue all the spiders together and attach them to the ceiling. Besides, Petya knows that the lower t... | The first input file line contains one integer n (1 ≤ n ≤ 100) — the number of spiders. Next n lines contain the descriptions of each spider: integer ni (2 ≤ ni ≤ 100) — the number of beads, then ni - 1 pairs of numbers denoting the numbers of the beads connected by threads. The beads that make up each spider are numbe... | Print a single number — the length of the required construction. | Input: 13 1 2 2 3 | Output: 2 | Easy | 3 | 1,705 | 337 | 64 | 1 | |
59 | E | 59E | E. Shortest Path | 2,000 | graphs; shortest paths | In Ancient Berland there were n cities and m two-way roads of equal length. The cities are numbered with integers from 1 to n inclusively. According to an ancient superstition, if a traveller visits three cities ai, bi, ci in row, without visiting other cities between them, a great disaster awaits him. Overall there ar... | The first line contains three integers n, m, k (2 ≤ n ≤ 3000, 1 ≤ m ≤ 20000, 0 ≤ k ≤ 105) which are the number of cities, the number of roads and the number of the forbidden triplets correspondingly. Then follow m lines each containing two integers xi, yi (1 ≤ xi, yi ≤ n) which are the road descriptions. The road is de... | If there are no path from 1 to n print -1. Otherwise on the first line print the number of roads d along the shortest path from the city 1 to the city n. On the second line print d + 1 numbers — any of the possible shortest paths for Vasya. The path should start in the city 1 and end in the city n. | Input: 4 4 11 22 33 41 31 4 3 | Output: 21 3 4 | Hard | 2 | 671 | 726 | 299 | 0 | |
681 | D | 681D | D. Gifts by the List | 2,000 | constructive algorithms; dfs and similar; graphs; trees | Sasha lives in a big happy family. At the Man's Day all the men of the family gather to celebrate it following their own traditions. There are n men in Sasha's family, so let's number them with integers from 1 to n.Each man has at most one father but may have arbitrary number of sons.Man number A is considered to be th... | In the first line of the input two integers n and m (0 ≤ m < n ≤ 100 000) are given — the number of the men in the Sasha's family and the number of family relations in it respectively.The next m lines describe family relations: the (i + 1)th line consists of pair of integers pi and qi (1 ≤ pi, qi ≤ n, pi ≠ qi) meaning ... | Print an integer k (1 ≤ k ≤ n) — the number of the men in the list of candidates, in the first line.Print then k pairwise different positive integers not exceeding n — the numbers of the men in the list in an order satisfying every of the men's wishes, one per line.If there are more than one appropriate lists, print an... | The first sample explanation: if there would be no 1 in the list then the first and the third man's wishes would not be satisfied (a1 = a3 = 1); if there would be no 2 in the list then the second man wish would not be satisfied (a2 = 2); if 1 would stay before 2 in the answer then the second man would have to give his ... | Input: 3 21 22 31 2 1 | Output: -1 | Hard | 4 | 1,716 | 836 | 390 | 6 |
365 | A | 365A | A. Good Number | 1,100 | implementation | Let's call a number k-good if it contains all digits not exceeding k (0, ..., k). You've got a number k and an array a containing n numbers. Find out how many k-good numbers are in a (count each number every time it occurs in array a). | The first line contains integers n and k (1 ≤ n ≤ 100, 0 ≤ k ≤ 9). The i-th of the following n lines contains integer ai without leading zeroes (1 ≤ ai ≤ 109). | Print a single integer — the number of k-good numbers in a. | Input: 10 61234560123456012345601234560123456012345601234560123456012345601234560 | Output: 10 | Easy | 1 | 235 | 159 | 59 | 3 | |
1,918 | C | 1918C | C. XOR-distance | 1,400 | bitmasks; greedy; implementation; math | You are given integers \(a\), \(b\), \(r\). Find the smallest value of \(|({a \oplus x}) - ({b \oplus x})|\) among all \(0 \leq x \leq r\).\(\oplus\) is the operation of bitwise XOR, and \(|y|\) is absolute value of \(y\). | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.Each test case contains integers \(a\), \(b\), \(r\) (\(0 \le a, b, r \le 10^{18}\)). | For each test case, output a single number — the smallest possible value. | In the first test, when \(r = 0\), then \(x\) is definitely equal to \(0\), so the answer is \(|{4 \oplus 0} - {6 \oplus 0}| = |4 - 6| = 2\).In the second test: When \(x = 0\), \(|{0 \oplus 0} - {3 \oplus 0}| = |0 - 3| = 3\). When \(x = 1\), \(|{0 \oplus 1} - {3 \oplus 1}| = |1 - 2| = 1\). When \(x = 2\), \(|{0 \oplus ... | Input: 104 6 00 3 29 6 1092 256 23165 839 2011 14 52 7 296549 34359 13851853686404475946 283666553522252166 127929199446003072735268590557942972 916721749674600979 895150420120690183 | Output: 2 1 1 164 542 5 3 37102 27934920819538516 104449824168870225 | Easy | 4 | 222 | 182 | 73 | 19 |
2,093 | F | 2093F | F. Hackers and Neural Networks | 1,800 | bitmasks; brute force; greedy | Hackers are once again trying to create entertaining phrases using the output of neural networks. This time, they want to obtain an array of strings \(a\) of length \(n\).Initially, they have an array \(c\) of length \(n\), filled with blanks, which are denoted by the symbol \(*\). Thus, if \(n=4\), then initially \(c=... | The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 500\)) — the length of the original array \(a\) and the number of neural networks, respectively.The second line of each test case cont... | Output \(t\) numbers — one number for each test case, each on a separate line.If there exists a sequence of operations that guarantees obtaining array \(a\) from the \(i\)-th test case, then the \(i\)-th number is the number of operations in the minimum such sequence.Otherwise, for the \(i\)-th number, output \(-1\). | Input: 4 3 3 I love apples He likes apples I love cats They love dogs 3 2 Icy wake up wake Icy up wake up Icy 4 3 c o D E c o D s c O l S c o m E 4 5 a s k A d s D t O R i A a X b Y b a k A u s k J | Output: 5 -1 6 8 | Medium | 3 | 2,074 | 1,012 | 318 | 20 | |
873 | F | 873F | F. Forbidden Indices | 2,400 | dsu; string suffix structures; strings | You are given a string s consisting of n lowercase Latin letters. Some indices in this string are marked as forbidden.You want to find a string a such that the value of |a|·f(a) is maximum possible, where f(a) is the number of occurences of a in s such that these occurences end in non-forbidden indices. So, for example... | The first line contains an integer number n (1 ≤ n ≤ 200000) — the length of s.The second line contains a string s, consisting of n lowercase Latin letters.The third line contains a string t, consisting of n characters 0 and 1. If i-th character in t is 1, then i is a forbidden index (otherwise i is not forbidden). | Print the maximum possible value of |a|·f(a). | Input: 5ababa00100 | Output: 5 | Expert | 3 | 588 | 316 | 45 | 8 | |
1,242 | A | 1242A | A. Tile Painting | 1,500 | constructive algorithms; math; number theory | Ujan has been lazy lately, but now has decided to bring his yard to good shape. First, he decided to paint the path from his house to the gate.The path consists of \(n\) consecutive tiles, numbered from \(1\) to \(n\). Ujan will paint each tile in some color. He will consider the path aesthetic if for any two different... | The first line of input contains a single integer \(n\) (\(1 \leq n \leq 10^{12}\)), the length of the path. | Output a single integer, the maximum possible number of colors that the path can be painted in. | In the first sample, two colors is the maximum number. Tiles \(1\) and \(3\) should have the same color since \(4 \bmod |3-1| = 0\). Also, tiles \(2\) and \(4\) should have the same color since \(4 \bmod |4-2| = 0\).In the second sample, all five colors can be used. | Input: 4 | Output: 2 | Medium | 3 | 769 | 108 | 95 | 12 |
1,618 | F | 1618F | F. Reverse | 2,000 | bitmasks; constructive algorithms; dfs and similar; implementation; math; strings | You are given two positive integers \(x\) and \(y\). You can perform the following operation with \(x\): write it in its binary form without leading zeros, add \(0\) or \(1\) to the right of it, reverse the binary form and turn it into a decimal number which is assigned as the new value of \(x\).For example: \(34\) can... | The only line of the input contains two integers \(x\) and \(y\) (\(1 \le x, y \le 10^{18}\)). | Print YES if you can make \(x\) equal to \(y\) and NO if you can't. | In the first example, you don't even need to do anything.The fourth example is described in the statement. | Input: 3 3 | Output: YES | Hard | 6 | 1,172 | 94 | 67 | 16 |
580 | E | 580E | E. Kefa and Watch | 2,500 | data structures; hashing; strings | One day Kefa the parrot was walking down the street as he was on the way home from the restaurant when he saw something glittering by the road. As he came nearer he understood that it was a watch. He decided to take it to the pawnbroker to earn some money. The pawnbroker said that each watch contains a serial number re... | The first line of the input contains three positive integers n, m and k (1 ≤ n ≤ 105, 1 ≤ m + k ≤ 105) — the length of the serial number, the number of change made by Kefa and the number of quality checks.The second line contains a serial number consisting of n digits.Then m + k lines follow, containing either checks o... | For each check on a single line print ""YES"" if the watch passed it, otherwise print ""NO"". | In the first sample test two checks will be made. In the first one substring ""12"" is checked on whether or not it has period 1, so the answer is ""NO"". In the second one substring ""88"", is checked on whether or not it has period 1, and it has this period, so the answer is ""YES"".In the second statement test three... | Input: 3 1 21122 2 3 11 1 3 82 1 2 1 | Output: NOYES | Expert | 3 | 1,034 | 538 | 93 | 5 |
1,142 | E | 1142E | E. Pink Floyd | 3,200 | graphs; interactive | This is an interactive task.Scientists are about to invent a new optimization for the Floyd-Warshall algorithm, which will allow it to work in linear time. There is only one part of the optimization still unfinished.It is well known that the Floyd-Warshall algorithm takes a graph with \(n\) nodes and exactly one edge b... | The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 100\,000\), \(0 \le m \le 100\,000\)) — the number of nodes and the number of pink edges.The next \(m\) lines describe the pink edges, the \(i\)-th of these lines contains two integers \(u_i\), \(v_i\) (\(1 \le u_i, v_i \le n\), \(u_i \ne v_i\)) — the ... | When you found the answer, print ""!"" and the number of the node from which every other node can be reached by a single-colored path. | In the example above the answer for the query ""? 1 3"" is 0, so the edge is directed from 3 to 1. The answer for the query ""? 4 2"" is 1, so the edge is directed from 4 to 2. The answer for the query ""? 3 2"" is 1, so the edge is directed from 3 to 2. So there are green paths from node 3 to nodes 1 and 2 and there i... | Input: 4 2 1 2 3 4 0 1 1 | Output: ? 1 3 ? 4 2 ? 3 2 ! 3 | Master | 2 | 1,083 | 436 | 134 | 11 |
926 | H | 926H | H. Endless Roses Most Beautiful | 2,200 | Arkady decided to buy roses for his girlfriend.A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the s... | The first line contains two integers n and k (1 ≤ k ≤ n ≤ 200 000) — the number of roses in the show and the number of roses Arkady wants to buy.The second line contains a sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 10 000), where bi equals the beauty of the i-th rose.The third line contains a string c of length n, ... | Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1. | In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all ro... | Input: 5 34 3 4 1 6RROWW | Output: 11 | Hard | 0 | 710 | 466 | 164 | 9 | |
486 | E | 486E | E. LIS of Sequence | 2,200 | data structures; dp; greedy; hashing; math | The next ""Data Structures and Algorithms"" lesson will be about Longest Increasing Subsequence (LIS for short) of a sequence. For better understanding, Nam decided to learn it a few days before the lesson.Nam created a sequence a consisting of n (1 ≤ n ≤ 105) elements a1, a2, ..., an (1 ≤ ai ≤ 105). A subsequence ai1,... | The first line contains the single integer n (1 ≤ n ≤ 105) denoting the number of elements of sequence a.The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 105). | Print a string consisting of n characters. i-th character should be '1', '2' or '3' depending on which group among listed above index i belongs to. | In the second sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 3, 2, 5}. Sequence a has exactly 2 longest increasing subsequences of length 3, they are {a1, a2, a4} = {1, 3, 5} and {a1, a3, a4} = {1, 2, 5}.In the third sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 5, 2, 3}. Sequence... | Input: 14 | Output: 3 | Hard | 5 | 1,075 | 188 | 147 | 4 |
163 | A | 163A | A. Substring and Subsequence | 1,700 | dp | One day Polycarpus got hold of two non-empty strings s and t, consisting of lowercase Latin letters. Polycarpus is quite good with strings, so he immediately wondered, how many different pairs of ""x y"" are there, such that x is a substring of string s, y is a subsequence of string t, and the content of x and y is the... | The input consists of two lines. The first of them contains s (1 ≤ |s| ≤ 5000), and the second one contains t (1 ≤ |t| ≤ 5000). Both strings consist of lowercase Latin letters. | Print a single number — the number of different pairs ""x y"" such that x is a substring of string s, y is a subsequence of string t, and the content of x and y is the same. As the answer can be rather large, print it modulo 1000000007 (109 + 7). | Let's write down all pairs ""x y"" that form the answer in the first sample: ""s[1...1] t[1]"", ""s[2...2] t[1]"", ""s[1...1] t[2]"",""s[2...2] t[2]"", ""s[1...2] t[1 2]"". | Input: aaaa | Output: 5 | Medium | 1 | 1,371 | 176 | 246 | 1 |
201 | C | 201C | C. Fragile Bridges | 2,000 | dp | You are playing a video game and you have just reached the bonus level, where the only possible goal is to score as many points as possible. Being a perfectionist, you've decided that you won't leave this level until you've gained the maximum possible number of points there.The bonus level consists of n small platforms... | The first line contains a single integer n (2 ≤ n ≤ 105) — the number of platforms on the bonus level. The second line contains (n - 1) integers ai (1 ≤ ai ≤ 109, 1 ≤ i < n) — the number of transitions from one end to the other that the bridge between platforms i and i + 1 can bear. | Print a single integer — the maximum number of points a player can get on the bonus level.Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | One possibility of getting 5 points in the sample is starting from platform 3 and consequently moving to platforms 4, 3, 2, 1 and 2. After that the only undestroyed bridge is the bridge between platforms 4 and 5, but this bridge is too far from platform 2 where the hero is located now. | Input: 52 1 2 1 | Output: 5 | Hard | 1 | 1,390 | 283 | 237 | 2 |
178 | B3 | 178B3 | B3. Greedy Merchants | 1,800 | In ABBYY a wonderful Smart Beaver lives. This time, he began to study history. When he read about the Roman Empire, he became interested in the life of merchants.The Roman Empire consisted of n cities numbered from 1 to n. It also had m bidirectional roads numbered from 1 to m. Each road connected two different cities.... | The first input line contains two integers n and m, separated by a space, n is the number of cities, and m is the number of roads in the empire.The following m lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi), separated by a space — the numbers of cities connected by the i-th road. It is guaranteed that... | Print exactly k lines, the i-th line should contain a single integer di — the number of dinars that the i-th merchant paid. | The given sample is illustrated in the figure below. Let's describe the result for the first merchant. The merchant's warehouse is located in city 1 and his shop is in city 5. Let us note that if either road, (1, 2) or (2, 3) is destroyed, there won't be any path between cities 1 and 5 anymore. If any other road is des... | Input: 7 81 22 33 44 55 65 73 54 741 52 42 64 7 | Output: 2120 | Medium | 0 | 1,676 | 1,052 | 123 | 1 | |
983 | C | 983C | C. Elevator | 2,400 | dp; graphs; shortest paths | You 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 floors already. For each person you know the floor where he currently is and the floor he wants to reach. Also, you k... | The first line contains an integer n (1 ≤ n ≤ 2000) — the number of employees.The i-th of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 9, ai ≠ bi) — the floor on which an employee initially is, and the floor he wants to reach.The employees are given in the order they came to the elevator. | Print a single integer — the minimal possible time in seconds. | Explaination for the first sample t = 0 t = 2 t = 3 t = 5 t = 6 t = 7 t = 9 t = 10 | Input: 23 55 3 | Output: 10 | Expert | 3 | 1,351 | 307 | 62 | 9 |
1,354 | C1 | 1354C1 | C1. Simple Polygon Embedding | 1,400 | binary search; geometry; math; ternary search | The statement of this problem is the same as the statement of problem C2. The only difference is that, in problem C1, \(n\) is always even, and in C2, \(n\) is always odd.You are given a regular polygon with \(2 \cdot n\) vertices (it's convex and has equal sides and equal angles) and all its sides have length \(1\). L... | The first line contains a single integer \(T\) (\(1 \le T \le 200\)) — the number of test cases.Next \(T\) lines contain descriptions of test cases — one per line. Each line contains single even integer \(n\) (\(2 \le n \le 200\)). Don't forget you need to embed \(2n\)-gon, not an \(n\)-gon. | Print \(T\) real numbers — one per test case. For each test case, print the minimum length of a side of the square \(2n\)-gon can be embedded in. Your answer will be considered correct if its absolute or relative error doesn't exceed \(10^{-6}\). | Input: 3 2 4 200 | Output: 1.000000000 2.414213562 127.321336469 | Easy | 4 | 709 | 292 | 246 | 13 | |
1,599 | A | 1599A | A. Weights | 2,600 | constructive algorithms; greedy; two pointers | You are given an array \(A\) of length \(N\) weights of masses \(A_1\), \(A_2\)...\(A_N\). No two weights have the same mass. You can put every weight on one side of the balance (left or right). You don't have to put weights in order \(A_1\),...,\(A_N\). There is also a string \(S\) consisting of characters ""L"" and "... | The first line contains one integer \(N\) (\(1 \leq N \leq 2*10^5\)) - the length of the array \(A\) The second line contains \(N\) distinct integers: \(A_1\), \(A_2\),...,\(A_N\) (\(1 \leq A_i \leq 10^9\)) - the weights given The third line contains string \(S\) of length \(N\) consisting only of letters ""L"" and ""R... | The output contains \(N\) lines. In every line, you should print one integer and one letter - integer representing the weight you are putting on the balance in that move and the letter representing the side of the balance where you are putting the weight. If there is no solution, print \(-1\). | Explanation for the test case: after the 1st weight: 3 L (left side is heavier)after the 2nd weight: 2 R (left side is heavier)after the 3rd weight: 8 R (right side is heavier)after the 4th weight: 13 L (left side is heavier)after the 5th weight: 7 L (left side is heavier)So, the rules given by string \(S\) are fulfill... | Input: 5 3 8 2 13 7 LLRLL | Output: 3 L 2 R 8 R 13 L 7 L | Expert | 3 | 575 | 436 | 294 | 15 |
1,651 | F | 1651F | F. Tower Defense | 3,000 | binary search; brute force; data structures | Monocarp is playing a tower defense game. A level in the game can be represented as an OX axis, where each lattice point from \(1\) to \(n\) contains a tower in it.The tower in the \(i\)-th point has \(c_i\) mana capacity and \(r_i\) mana regeneration rate. In the beginning, before the \(0\)-th second, each tower has f... | The first line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the number of towers.The \(i\)-th of the next \(n\) lines contains two integers \(c_i\) and \(r_i\) (\(1 \le r_i \le c_i \le 10^9\)) — the mana capacity and the mana regeneration rate of the \(i\)-th tower.The next line contains a single in... | Print a single integer — the total health of all monsters after they pass all towers. | Input: 3 5 1 7 4 4 2 4 0 14 1 10 3 16 10 16 | Output: 4 | Master | 3 | 1,149 | 699 | 85 | 16 | |
1,257 | A | 1257A | A. Two Rival Students | 800 | greedy; math | You are the gym teacher in the school.There are \(n\) students in the row. And there are two rivalling students among them. The first one is in position \(a\), the second in position \(b\). Positions are numbered from \(1\) to \(n\) from left to right.Since they are rivals, you want to maximize the distance between the... | The first line contains one integer \(t\) (\(1 \le t \le 100\)) — the number of test cases.The only line of each test case contains four integers \(n\), \(x\), \(a\) and \(b\) (\(2 \le n \le 100\), \(0 \le x \le 100\), \(1 \le a, b \le n\), \(a \neq b\)) — the number of students in the row, the number of swaps which yo... | For each test case print one integer — the maximum distance between two rivaling students which you can obtain. | In the first test case you can swap students in positions \(3\) and \(4\). And then the distance between the rivals is equal to \(|4 - 2| = 2\).In the second test case you don't have to swap students. In the third test case you can't swap students. | Input: 3 5 1 3 2 100 33 100 1 6 0 2 3 | Output: 2 99 1 | Beginner | 2 | 627 | 395 | 111 | 12 |
2,018 | B | 2018B | B. Speedbreaker | 1,900 | binary search; data structures; dp; greedy; implementation; two pointers | Djjaner - Speedbreaker⠀There are \(n\) cities in a row, numbered \(1, 2, \ldots, n\) left to right. At time \(1\), you conquer exactly one city, called the starting city. At time \(2, 3, \ldots, n\), you can choose a city adjacent to the ones conquered so far and conquer it. You win if, for each \(i\), you conquer city... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the number of cities.The second line of each test case contai... | For each test case, output a single integer: the number of starting cities that allow you to win. | In the first test case, cities \(2\), \(3\), and \(4\) are good starting cities.In the second test case, there are no good starting cities.In the third test case, the only good starting city is city \(5\). | Input: 366 3 3 3 5 565 6 4 1 4 598 6 4 2 1 3 5 7 9 | Output: 3 0 1 | Hard | 6 | 480 | 520 | 97 | 20 |
859 | C | 859C | C. Pie Rules | 1,500 | dp; games | You may have heard of the pie rule before. It states that if two people wish to fairly share a slice of pie, one person should cut the slice in half, and the other person should choose who gets which slice. Alice and Bob have many slices of pie, and rather than cutting the slices in half, each individual slice will be ... | Input will begin with an integer N (1 ≤ N ≤ 50), the number of slices of pie. Following this is a line with N integers indicating the sizes of the slices (each between 1 and 100000, inclusive), in the order in which they must be handed out. | Print two integers. First, the sum of the sizes of slices eaten by Alice, then the sum of the sizes of the slices eaten by Bob, assuming both players make their decisions optimally. | In the first example, Bob takes the size 141 slice for himself and gives the decider token to Alice. Then Alice gives the size 592 slice to Bob and keeps the decider token for herself, so that she can then give the size 653 slice to herself. | Input: 3141 592 653 | Output: 653 733 | Medium | 2 | 1,002 | 240 | 181 | 8 |
1,695 | D2 | 1695D2 | D2. Tree Queries (Hard Version) | 2,300 | constructive algorithms; dfs and similar; dp; greedy; trees | The only difference between this problem and D1 is the bound on the size of the tree.You are given an unrooted tree with \(n\) vertices. There is some hidden vertex \(x\) in that tree that you are trying to find.To do this, you may ask \(k\) queries \(v_1, v_2, \ldots, v_k\) where the \(v_i\) are vertices in the tree. ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2\cdot10^5\)) — the number of vertices in the tree.Each of the next \(n-1\) lines c... | For each test case print a single nonnegative integer, the minimum number of queries you need, on its own line. | In the first test case, there is only one vertex, so you don't need any queries.In the second test case, you can ask a single query about the node \(1\). Then, if \(x = 1\), you will get \(0\), otherwise you will get \(1\). | Input: 3121 2102 42 15 73 108 66 11 34 79 6 | Output: 0 1 2 | Expert | 5 | 787 | 592 | 111 | 16 |
1,984 | D | 1984D | D. ""a"" String Problem | 2,000 | brute force; hashing; implementation; math; string suffix structures; strings | You are given a string \(s\) consisting of lowercase Latin characters. Count the number of nonempty strings \(t \neq\) ""\(\texttt{a}\)"" such that it is possible to partition\(^{\dagger}\) \(s\) into some substrings satisfying the following conditions: each substring either equals \(t\) or ""\(\texttt{a}\)"", and at l... | The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases.The only line of each test case contains a string \(s\) consisting of lowercase Latin characters (\(2 \leq |s| \leq 2 \cdot 10^5\)).The sum of \(|s|\) over all test cases does not exceed \(3 \cdot 10^5\). | For each test case, output a single integer — the number of nonempty strings \(t \neq\) ""\(\texttt{a}\)"" that satisfy all constraints. | In the first test case, \(t\) can be ""\(\texttt{aa}\)"", ""\(\texttt{aaa}\)"", ""\(\texttt{aaaa}\)"", or the full string.In the second test case, \(t\) can be ""\(\texttt{b}\)"", ""\(\texttt{bab}\)"", ""\(\texttt{ba}\)"", or the full string.In the third test case, the only such \(t\) is the full string. | Input: 8aaaaababacabacbaaabaaabitsetababbaaaabbbyearnineteeneightyfour | Output: 4 4 1 16 1 2 3 1 | Hard | 6 | 584 | 303 | 136 | 19 |
1,614 | A | 1614A | A. Divan and a Store | 800 | brute force; constructive algorithms; greedy | Businessman Divan loves chocolate! Today he came to a store to buy some chocolate. Like all businessmen, Divan knows the value of money, so he will not buy too expensive chocolate. At the same time, too cheap chocolate tastes bad, so he will not buy it as well.The store he came to has \(n\) different chocolate bars, an... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The description of each test case consists of two lines. The first line contains integers \(n\), \(l\), \(r\), \(k\) (\(1 \le n \le 100\), \(1 \le l \le r \le 10^9\... | For each test case print a single integer — the maximum number of chocolate bars Divan can buy. | In the first example Divan can buy chocolate bars \(1\) and \(3\) and spend \(100\) dollars on them.In the second example Divan can buy chocolate bars \(3\) and \(4\) and spend \(7\) dollars on them.In the third example Divan can buy chocolate bars \(3\), \(4\), and \(5\) for \(12\) dollars.In the fourth example Divan ... | Input: 8 3 1 100 100 50 100 50 6 3 5 10 1 2 3 4 5 6 6 3 5 21 1 2 3 4 5 6 10 50 69 100 20 30 40 77 1 1 12 4 70 10000 3 50 80 30 20 60 70 10 2 7 100 2 2 2 2 2 7 7 7 7 7 4 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1 1 1 1 1 | Output: 2 2 3 0 0 10 1 1 | Beginner | 3 | 812 | 614 | 95 | 16 |
292 | B | 292B | B. Network Topology | 1,200 | graphs; implementation | This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are... | The first line contains two space-separated integers n and m (4 ≤ n ≤ 105; 3 ≤ m ≤ 105) — the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≤ xi, yi ≤ n) — the numbers of nodes that ... | In a single line print the network topology name of the given graph. If the answer is the bus, print ""bus topology"" (without the quotes), if the answer is the ring, print ""ring topology"" (without the quotes), if the answer is the star, print ""star topology"" (without the quotes). If no answer fits, print ""unknown... | Input: 4 31 22 33 4 | Output: bus topology | Easy | 2 | 1,797 | 489 | 353 | 2 | |
132 | B | 132B | B. Piet | 2,100 | implementation | Piet is one of the most known visual esoteric programming languages. The programs in Piet are constructed from colorful blocks of pixels and interpreted using pretty complicated rules. In this problem we will use a subset of Piet language with simplified rules.The program will be a rectangular image consisting of color... | The first line of the input contains two integer numbers m (1 ≤ m ≤ 50) and n (1 ≤ n ≤ 5·107). Next m lines contain the rows of the program. All the lines have the same length between 1 and 50 pixels, and consist of characters 0-9. The first character of the first line will not be equal to 0. | Output the color of the block which will be current after n steps of program interpretation. | In the first example IP changes in the following way. After step 1 block 2 becomes current one and stays it after two more steps. After step 4 BP moves to block 3, after step 7 — to block 4, and finally after step 10 BP returns to block 1. The sequence of states of IP is shown on the image: the arrows are traversed clo... | Input: 2 101243 | Output: 1 | Hard | 1 | 2,328 | 293 | 92 | 1 |
1,913 | D | 1913D | D. Array Collapse | 2,100 | data structures; divide and conquer; dp; trees | You are given an array \([p_1, p_2, \dots, p_n]\), where all elements are distinct.You can perform several (possibly zero) operations with it. In one operation, you can choose a contiguous subsegment of \(p\) and remove all elements from that subsegment, except for the minimum element on that subsegment. For example, i... | The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.Each test case consists of two lines. The first line contains one integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)). The second line contains \(n\) distinct integers \(p_1, p_2, \dots, p_n\) (\(1 \le p_i \le 10^9\)).Addi... | For each test case, print one integer — the number of reachable arrays, taken modulo \(998244353\). | Input: 322 142 4 1 3510 2 6 3 4 | Output: 2 6 12 | Hard | 4 | 671 | 422 | 99 | 19 | |
1,111 | C | 1111C | C. Creative Snap | 1,700 | binary search; brute force; divide and conquer; math | Thanos wants to destroy the avengers base, but he needs to destroy the avengers along with their base.Let we represent their base with an array, where each position can be occupied by many avengers, but one avenger can occupy only one position. Length of their base is a perfect power of \(2\). Thanos wants to destroy t... | The first line contains four integers \(n\), \(k\), \(A\) and \(B\) (\(1 \leq n \leq 30\), \(1 \leq k \leq 10^5\), \(1 \leq A,B \leq 10^4\)), where \(2^n\) is the length of the base, \(k\) is the number of avengers and \(A\) and \(B\) are the constants explained in the question.The second line contains \(k\) integers \... | Output one integer — the minimum power needed to destroy the avengers base. | Consider the first example.One option for Thanos is to burn the whole base \(1-4\) with power \(2 \cdot 2 \cdot 4 = 16\).Otherwise he can divide the base into two parts \(1-2\) and \(3-4\).For base \(1-2\), he can either burn it with power \(2 \cdot 1 \cdot 2 = 4\) or divide it into \(2\) parts \(1-1\) and \(2-2\).For ... | Input: 2 2 1 2 1 3 | Output: 6 | Medium | 4 | 848 | 450 | 75 | 11 |
1,833 | C | 1833C | C. Vlad Building Beautiful Array | 800 | greedy; math | Vlad was given an array \(a\) of \(n\) positive integers. Now he wants to build a beautiful array \(b\) of length \(n\) from it.Vlad considers an array beautiful if all the numbers in it are positive and have the same parity. That is, all numbers in the beautiful array are greater than zero and are either all even or a... | The first line of input contains an integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.Then follow the descriptions of the test cases.The first line of each case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the length of the array \(a\).The second line of each case contains \(n\) positi... | Output \(t\) strings, each of which is the answer to the corresponding test case. As the answer, output ""YES"" if Vlad can build a beautiful array \(b\), and ""NO"" otherwise.You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be recognized as a positive answer). | Input: 752 6 8 4 351 4 7 6 942 6 4 1075 29 13 9 10000001 11 352 1 2 4 252 4 5 4 342 5 5 4 | Output: NO YES YES YES YES NO NO | Beginner | 2 | 634 | 503 | 319 | 18 | |
1,002 | B1 | 1002B1 | B1. Distinguish zero state and W state | 1,300 | *special | You are given N qubits (2 ≤ N ≤ 8) which are guaranteed to be in one of the two states: state, or state. Your task is to perform necessary operations and measurements to figure out which state it was and to return 0 if it was state or 1 if it was W state. The state of the qubits after the operations does not matter.You... | Easy | 1 | 620 | 0 | 0 | 10 | ||||
2,115 | B | 2115B | B. Gellyfish and Camellia Japonica | 2,100 | brute force; constructive algorithms; dfs and similar; dp; graphs; greedy; trees | Gellyfish has an array of \(n\) integers \(c_1, c_2, \ldots, c_n\). In the beginning, \(c = [a_1, a_2, \ldots, a_n]\).Gellyfish will make \(q\) modifications to \(c\).For \(i = 1,2,\ldots,q\), Gellyfish is given three integers \(x_i\), \(y_i\), and \(z_i\) between \(1\) and \(n\). Then Gellyfish will set \(c_{z_i} := \... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains two integers \(n\) and \(q\) (\(1 \leq n, q \leq 3 \cdot 10^5\)) — the size of the array and the number of modificat... | For each test case, if \(a\) exists, output \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i \leq 10^9\)) in a single line. Otherwise, output ""-1"" in a single line.If there are multiple solutions, print any of them. | In the first test case, based on the given sequence of modifications, we know that \(b_1 = a_1\) and \(b_2 = \min(a_1, a_2)\). Therefore, it is necessary that \(b_2 \leq b_1\). However, for the given \(b\), we have \(b_1<b_2\). Therefore, there is no solution.In the second test case, we can see that the given \(c\) bec... | Input: 32 11 22 1 23 21 2 32 3 21 2 16 41 2 2 3 4 55 6 64 5 53 4 42 3 3 | Output: -1 1 2 3 1 2 3 4 5 5 | Hard | 7 | 752 | 767 | 223 | 21 |
1,393 | A | 1393A | A. Rainbow Dash, Fluttershy and Chess Coloring | 800 | greedy; math | One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size... | The first line contains a single integer \(T\) (\(1 \le T \le 100\)): the number of grids of the games. Each of the next \(T\) lines contains a single integer \(n\) (\(1 \le n \le 10^9\)): the size of the side of the grid of the game. | For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it. | For \(3\times3\) grid ponies can make two following moves: | Input: 2 3 4 | Output: 2 3 | Beginner | 2 | 1,458 | 234 | 123 | 13 |
1,519 | B | 1519B | B. The Cake Is a Lie | 800 | dp; math | There is a \(n \times m\) grid. You are standing at cell \((1, 1)\) and your goal is to finish at cell \((n, m)\).You can move to the neighboring cells to the right or down. In other words, suppose you are standing at cell \((x, y)\). You can: move right to the cell \((x, y + 1)\) — it costs \(x\) burles; move down to ... | The first line contains the single integer \(t\) (\(1 \le t \le 100\)) — the number of test cases.The first and only line of each test case contains three integers \(n\), \(m\), and \(k\) (\(1 \le n, m \le 100\); \(0 \le k \le 10^4\)) — the sizes of grid and the exact amount of money you need to spend. | For each test case, if you can reach cell \((n, m)\) spending exactly \(k\) burles, print YES. Otherwise, print NO.You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer). | In the first test case, you are already in the final cell, so you spend \(0\) burles.In the second, third and fourth test cases, there are two paths from \((1, 1)\) to \((2, 2)\): \((1, 1)\) \(\rightarrow\) \((1, 2)\) \(\rightarrow\) \((2, 2)\) or \((1, 1)\) \(\rightarrow\) \((2, 1)\) \(\rightarrow\) \((2, 2)\). Both c... | Input: 6 1 1 0 2 2 2 2 2 3 2 2 4 1 4 3 100 100 10000 | Output: YES NO YES NO YES NO | Beginner | 2 | 429 | 303 | 254 | 15 |
188 | A | 188A | A. Hexagonal Numbers | 1,100 | *special | Hexagonal numbers are figurate numbers which can be calculated using the formula hn = 2n2 - n. You are given n; calculate n-th hexagonal number. | The only line of input contains an integer n (1 ≤ n ≤ 100). | Output the n-th hexagonal number. | Input: 3 | Output: 15 | Easy | 1 | 144 | 59 | 33 | 1 | |
602 | A | 602A | A. Two Bases | 1,100 | brute force; implementation | After seeing the ""ALL YOUR BASE ARE BELONG TO US"" meme for the first time, numbers X and Y realised that they have different bases, which complicated their relations.You're given a number X represented in base bx and a number Y represented in base by. Compare those two numbers. | The first line of the input contains two space-separated integers n and bx (1 ≤ n ≤ 10, 2 ≤ bx ≤ 40), where n is the number of digits in the bx-based representation of X. The second line contains n space-separated integers x1, x2, ..., xn (0 ≤ xi < bx) — the digits of X. They are given in the order from the most signif... | Output a single character (quotes for clarity): '<' if X < Y '>' if X > Y '=' if X = Y | In the first sample, X = 1011112 = 4710 = Y.In the second sample, X = 1023 = 215 and Y = 245 = 1123, thus X < Y.In the third sample, and Y = 48031509. We may notice that X starts with much larger digits and bx is much larger than by, so X is clearly larger than Y. | Input: 6 21 0 1 1 1 12 104 7 | Output: = | Easy | 2 | 280 | 824 | 86 | 6 |
877 | F | 877F | F. Ann and Books | 2,300 | data structures; flows; hashing | In Ann's favorite book shop are as many as n books on math and economics. Books are numbered from 1 to n. Each of them contains non-negative number of problems.Today there is a sale: any subsegment of a segment from l to r can be bought at a fixed price. Ann decided that she wants to buy such non-empty subsegment that ... | The first line contains two integers n and k (1 ≤ n ≤ 100 000, - 109 ≤ k ≤ 109) — the number of books and the needed difference between the number of math problems and the number of economics problems.The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 2), where ti is 1 if the i-th book is on math or 2 if the... | Print q lines, in the i-th of them print the number of subsegments for the i-th Ann's assumption. | In the first sample Ann can buy subsegments [1;1], [2;2], [3;3], [2;4] if they fall into the sales segment, because the number of math problems is greater by 1 on them that the number of economics problems. So we should count for each assumption the number of these subsegments that are subsegments of the given segment.... | Input: 4 11 1 1 21 1 1 141 21 31 43 4 | Output: 2341 | Expert | 3 | 1,012 | 663 | 97 | 8 |
1,631 | A | 1631A | A. Min Max Swap | 800 | greedy | You are given two arrays \(a\) and \(b\) of \(n\) positive integers each. You can apply the following operation to them any number of times: Select an index \(i\) (\(1\leq i\leq n\)) and swap \(a_i\) with \(b_i\) (i. e. \(a_i\) becomes \(b_i\) and vice versa). Find the minimum possible value of \(\max(a_1, a_2, \ldots,... | The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) — the number of test cases. Description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1\le n\le 100\)) — the length of the arrays.The second line of each test case ... | For each test case, print a single integer, the minimum possible value of \(\max(a_1, a_2, \ldots, a_n) \cdot \max(b_1, b_2, \ldots, b_n)\) you can get after applying such operation any number of times. | In the first test, you can apply the operations at indices \(2\) and \(6\), then \(a = [1, 4, 6, 5, 1, 5]\) and \(b = [3, 2, 3, 2, 2, 2]\), \(\max(1, 4, 6, 5, 1, 5) \cdot \max(3, 2, 3, 2, 2, 2) = 6 \cdot 3 = 18\).In the second test, no matter how you apply the operations, \(a = [3, 3, 3]\) and \(b = [3, 3, 3]\) will al... | Input: 361 2 6 5 1 23 4 3 2 2 533 3 33 3 321 22 1 | Output: 18 9 2 | Beginner | 1 | 440 | 623 | 202 | 16 |
656 | G | 656G | G. You're a Professional | 1,900 | *special | A simple recommendation system would recommend a user things liked by a certain number of their friends. In this problem you will implement part of such a system.You are given user's friends' opinions about a list of items. You are also given a threshold T — the minimal number of ""likes"" necessary for an item to be r... | The first line of the input will contain three space-separated integers: the number of friends F (1 ≤ F ≤ 10), the number of items I (1 ≤ I ≤ 10) and the threshold T (1 ≤ T ≤ F).The following F lines of input contain user's friends' opinions. j-th character of i-th line is 'Y' if i-th friend likes j-th item, and 'N' ot... | Output an integer — the number of items liked by at least T of user's friends. | Input: 3 3 2YYYNNNYNY | Output: 2 | Hard | 1 | 420 | 328 | 78 | 6 | |
90 | B | 90B | B. African Crossword | 1,100 | implementation; strings | An African crossword is a rectangular table n × m in size. Each cell of the table contains exactly one letter. This table (it is also referred to as grid) contains some encrypted word that needs to be decoded.To solve the crossword you should cross out all repeated letters in rows and columns. In other words, a letter ... | The first line contains two integers n and m (1 ≤ n, m ≤ 100). Next n lines contain m lowercase Latin letters each. That is the crossword grid. | Print the encrypted word on a single line. It is guaranteed that the answer consists of at least one letter. | Input: 3 3cbabcdcbc | Output: abcd | Easy | 2 | 919 | 143 | 108 | 0 | |
1,264 | A | 1264A | A. Beautiful Regional Contest | 1,500 | greedy; implementation | So the Beautiful Regional Contest (BeRC) has come to an end! \(n\) students took part in the contest. The final standings are already known: the participant in the \(i\)-th place solved \(p_i\) problems. Since the participants are primarily sorted by the number of solved problems, then \(p_1 \ge p_2 \ge \dots \ge p_n\)... | The first line of the input contains an integer \(t\) (\(1 \le t \le 10000\)) — the number of test cases in the input. Then \(t\) test cases follow.The first line of a test case contains an integer \(n\) (\(1 \le n \le 4\cdot10^5\)) — the number of BeRC participants. The second line of a test case contains integers \(p... | Print \(t\) lines, the \(j\)-th line should contain the answer to the \(j\)-th test case.The answer consists of three non-negative integers \(g, s, b\). Print \(g=s=b=0\) if there is no way to reward participants with medals so that all requirements from the statement are satisfied at the same time. Otherwise, print th... | In the first test case, it is possible to reward \(1\) gold, \(2\) silver and \(3\) bronze medals. In this case, the participant solved \(5\) tasks will be rewarded with the gold medal, participants solved \(4\) tasks will be rewarded with silver medals, participants solved \(2\) or \(3\) tasks will be rewarded with br... | Input: 5 12 5 4 4 3 2 2 1 1 1 1 1 1 4 4 3 2 1 1 1000000 20 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 32 64 64 63 58 58 58 58 58 37 37 37 37 34 34 28 28 28 28 28 28 24 24 19 17 17 17 17 16 16 16 16 11 | Output: 1 2 3 0 0 0 0 0 0 2 5 3 2 6 6 | Medium | 2 | 1,531 | 653 | 525 | 12 |
1,659 | B | 1659B | B. Bit Flipping | 1,300 | bitmasks; constructive algorithms; greedy; strings | You are given a binary string of length \(n\). You have exactly \(k\) moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped (\(0\) becomes \(1\), \(1\) becomes \(0\)). You need to output the lexicographically largest string that you can get after using all \(k\) moves.... | The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases.Each test case has two lines. The first line has two integers \(n\) and \(k\) (\(1 \leq n \leq 2 \cdot 10^5\); \(0 \leq k \leq 10^9\)).The second line has a binary string of length \(n\), each character is either \(0\) or \... | For each test case, output two lines.The first line should contain the lexicographically largest string you can obtain.The second line should contain \(n\) integers \(f_1, f_2, \ldots, f_n\), where \(f_i\) is the number of times the \(i\)-th bit is selected. The sum of all the integers must be equal to \(k\). | Here is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit \(1\): \(\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}\). Choose bit \(4\): \(\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underlin... | Input: 66 31000016 41000116 00000006 11110016 111011006 12001110 | Output: 111110 1 0 0 2 0 0 111110 0 1 1 1 0 1 000000 0 0 0 0 0 0 100110 1 0 0 0 0 0 111111 1 2 1 3 0 4 111110 1 1 4 2 0 4 | Easy | 4 | 704 | 395 | 310 | 16 |
622 | B | 622B | B. The Time | 900 | implementation | You are given the current time in 24-hour format hh:mm. Find and print the time after a minutes.Note that you should find only the time after a minutes, see the examples to clarify the problem statement.You can read more about 24-hour format here https://en.wikipedia.org/wiki/24-hour_clock. | The first line contains the current time in the format hh:mm (0 ≤ hh < 24, 0 ≤ mm < 60). The hours and the minutes are given with two digits (the hours or the minutes less than 10 are given with the leading zeroes).The second line contains integer a (0 ≤ a ≤ 104) — the number of the minutes passed. | The only line should contain the time after a minutes in the format described in the input. Note that you should print exactly two digits for the hours and the minutes (add leading zeroes to the numbers if needed).See the examples to check the input/output format. | Input: 23:5910 | Output: 00:09 | Beginner | 1 | 291 | 299 | 264 | 6 | |
2,028 | D | 2028D | D. Alice's Adventures in Cards | 2,000 | constructive algorithms; data structures; dp; graphs; greedy; implementation; ternary search | Alice is playing cards with the Queen of Hearts, King of Hearts, and Jack of Hearts. There are \(n\) different types of cards in their card game. Alice currently has a card of type \(1\) and needs a card of type \(n\) to escape Wonderland. The other players have one of each kind of card.In this card game, Alice can tra... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains an integer \(n\) (\(2\le n\le 2\cdot 10^5\)) — the number of card types.The next three lines contain the preferences ... | For each test case, on the first line output a single string ""YES"" or ""NO"" (without the quotes) denoting whether Alice can trade up to card \(n\).If the first line was ""YES"", then on the next line output \(k\) — the number of trades Alice will make. On the next \(k\) lines output space separated a character \(c\i... | In the first testcase, Alice can trade with the King to get card \(2\). She can then trade with the Queen to get card \(3\).In the second testcase, even though Alice can trade with the Queen to get card \(3\), with the King to get card \(2\), and then with the Jack to get card \(4\), this is not a valid solution since ... | Input: 231 3 22 1 31 2 342 3 1 41 2 3 41 4 2 3 | Output: YES 2 k 2 q 3 NO | Hard | 7 | 1,382 | 580 | 886 | 20 |
1,197 | A | 1197A | A. DIY Wooden Ladder | 900 | greedy; math; sortings | Let's denote a \(k\)-step ladder as the following structure: exactly \(k + 2\) wooden planks, of which two planks of length at least \(k+1\) — the base of the ladder; \(k\) planks of length at least \(1\) — the steps of the ladder; Note that neither the base planks, nor the steps planks are required to be equal.For exa... | The first line contains a single integer \(T\) (\(1 \le T \le 100\)) — the number of queries. The queries are independent.Each query consists of two lines. The first line contains a single integer \(n\) (\(2 \le n \le 10^5\)) — the number of planks you have.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n... | Print \(T\) integers — one per query. The \(i\)-th integer is the maximum number \(k\), such that you can choose some subset of the planks given in the \(i\)-th query and assemble a \(k\)-step ladder using them.Print \(0\) if you can't make even \(1\)-step ladder from the given set of planks. | Examples for the queries \(1-3\) are shown at the image in the legend section.The Russian meme to express the quality of the ladders: | Input: 4 4 1 3 1 3 3 3 3 2 5 2 3 3 4 2 3 1 1 2 | Output: 2 1 2 0 | Beginner | 3 | 1,076 | 479 | 293 | 11 |
1,326 | F2 | 1326F2 | F2. Wise Men (Hard Version) | 3,200 | bitmasks; dp; math | This is the hard version of the problem. The difference is constraints on the number of wise men and the time limit. You can make hacks only if all versions of this task are solved.\(n\) wise men live in a beautiful city. Some of them know each other.For each of the \(n!\) possible permutations \(p_1, p_2, \ldots, p_n\... | The first line of input contains one integer \(n\) (\(2 \leq n \leq 18)\) — the number of wise men in the city.The next \(n\) lines contain a binary string of length \(n\) each, such that the \(j\)-th character of the \(i\)-th string is equal to '1' if wise man \(i\) knows wise man \(j\), and equals '0' otherwise.It is... | Print \(2^{n-1}\) space-separated integers. For each \(0 \leq x < 2^{n-1}\): Let's consider a string \(s\) of length \(n-1\), such that \(s_i = \lfloor \frac{x}{2^{i-1}} \rfloor \bmod 2\) for all \(1 \leq i \leq n - 1\). The \((x+1)\)-th number should be equal to the required answer for \(s\). | In the first test, each wise man knows each other, so every permutation will produce the string \(11\).In the second test: If \(p = \{1, 2, 3, 4\}\), the produced string is \(101\), because wise men \(1\) and \(2\) know each other, \(2\) and \(3\) don't know each other, and \(3\) and \(4\) know each other; If \(p = \{4... | Input: 3 011 101 110 | Output: 0 0 0 6 | Master | 3 | 607 | 447 | 294 | 13 |
38 | G | 38G | G. Queue | 2,300 | data structures | On a cold winter evening our hero Vasya stood in a railway queue to buy a ticket for Codeforces championship final. As it usually happens, the cashier said he was going to be away for 5 minutes and left for an hour. Then Vasya, not to get bored, started to analyze such a mechanism as a queue. The findings astonished Va... | The first input line contains an integer n which is the number of people who has joined the queue (1 ≤ n ≤ 105). In the next n lines descriptions of the people are given in order of their coming — space-separated integers ai and ci (1 ≤ ai ≤ n, 0 ≤ ci ≤ n). Every description is located on s single line. All the ai's ar... | Output the permutation of numbers from 1 to n, which signifies the queue formed according to the above described rules, starting from the beginning to the end. In this succession the i-th number stands for the number of a person who will stand in line on the place number i after the swaps ends. People are numbered star... | Input: 21 02 1 | Output: 2 1 | Expert | 1 | 1,572 | 332 | 412 | 0 | |
1,910 | E | 1910E | E. Maximum Sum Subarrays | 2,100 | *special; dp | You are given two integer arrays \(a\) and \(b\), both of length \(n\).You can perform the following operation any number of times (possibly zero): swap \(a_i\) and \(b_i\).Let \(f(c)\) be the maximum sum of a contiguous subarray of the array \(c\) (including the empty subsegment, which sum is \(0\)).Your task is to ca... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(-10^9 \le a_i \le 10^9\)).The third line contains \(n\) int... | For each test case, print a single integer — the maximum possible value of \(f(a) + f(b)\), using the aforementioned operation any number of times. | Input: 332 -1 3-4 0 164 2 -6 1 6 -4-6 -2 -3 7 -3 22-2 -50 -1 | Output: 6 21 0 | Hard | 2 | 430 | 448 | 147 | 19 | |
2,018 | F2 | 2018F2 | F2. Speedbreaker Counting (Medium Version) | 3,000 | dp; greedy; math | NightHawk22 - Isolation⠀This is the medium version of the problem. In the three versions, the constraints on \(n\) and the time limit are different. You can make hacks only if all the versions of the problem are solved.This is the statement of Problem D1B: There are \(n\) cities in a row, numbered \(1, 2, \ldots, n\) l... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). The description of the test cases follows.The only line of each test case contains two integers \(n\), \(p\) (\(1 \le n \le 500\), \(10^8 \leq p \leq 10^9\), \(p\) is prime) — the number of cities and t... | For each test case, output \(n+1\) integers: the \(i\)-th integer should be the number of arrays that satisfy the conditions for \(k = i-1\). | In the first test case, arrays with \(1\) good starting city: \([1]\). In the second test case, arrays with \(0\) good starting cities: \([1, 1]\); arrays with \(1\) good starting city: \([1, 2]\), \([2, 1]\); arrays with \(2\) good starting cities: \([2, 2]\). In the third test case, arrays with \(0\) good starting ci... | Input: 111 9982443532 9982443533 9982443534 9982443535 9982443536 9982443537 9982443538 9982443539 99824435310 10227585710 999662017 | Output: 0 1 1 2 1 14 7 4 2 183 34 19 16 4 2624 209 112 120 48 12 42605 1546 793 992 468 216 36 785910 13327 6556 9190 4672 2880 864 144 16382863 130922 61939 94992 50100 36960 14256 460... | Master | 3 | 1,002 | 413 | 141 | 20 |
1,693 | D | 1693D | D. Decinc Dividing | 2,800 | brute force; data structures; divide and conquer; dp; greedy | Let's call an array \(a\) of \(m\) integers \(a_1, a_2, \ldots, a_m\) Decinc if \(a\) can be made increasing by removing a decreasing subsequence (possibly empty) from it. For example, if \(a = [3, 2, 4, 1, 5]\), we can remove the decreasing subsequence \([a_1, a_4]\) from \(a\) and obtain \(a = [2, 4, 5]\), which is i... | The first line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the size of \(p\).The second line contains \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le n\), all \(p_i\) are distinct) — elements of the permutation. | Output the number of pairs of integers \((l, r)\) such that \(p[l \ldots r]\) (the subarray of \(p\) from \(l\) to \(r\)) is a Decinc array. \((1 \le l \le r \le n)\) | In the first sample, all subarrays are Decinc.In the second sample, all subarrays except \(p[1 \ldots 6]\) and \(p[2 \ldots 6]\) are Decinc. | Input: 3 2 3 1 | Output: 6 | Master | 5 | 563 | 242 | 166 | 16 |
2,028 | B | 2028B | B. Alice's Adventures in Permuting | 1,400 | binary search; implementation; math | Alice mixed up the words transmutation and permutation! She has an array \(a\) specified via three integers \(n\), \(b\), \(c\): the array \(a\) has length \(n\) and is given via \(a_i = b\cdot (i - 1) + c\) for \(1\le i\le n\). For example, if \(n=3\), \(b=2\), and \(c=1\), then \(a=[2 \cdot 0 + 1, 2 \cdot 1 + 1, 2 \c... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). The description of the test cases follows.The only line of each test case contains three integers \(n\), \(b\), \(c\) (\(1\le n\le 10^{18}\); \(0\le b\), \(c\le 10^{18}\)) — the parameters of the array... | For each test case, if the array can never become a permutation, output \(-1\). Otherwise, output the minimum number of operations for the array to become a permutation. | In the first test case, the array is already \([0, 1, \ldots, 9]\), so no operations are required.In the third test case, the starting array is \([1, 3, 5, \ldots, 199]\). After the first operation, the \(199\) gets transformed into a \(0\). In the second operation, the \(197\) gets transformed into a \(2\). If we cont... | Input: 710 1 01 2 3100 2 13 0 13 0 01000000000000000000 0 01000000000000000000 1000000000000000000 1000000000000000000 | Output: 0 1 50 2 -1 -1 1000000000000000000 | Easy | 3 | 1,517 | 321 | 169 | 20 |
97 | C | 97C | C. Winning Strategy | 2,400 | binary search; graphs; math; shortest paths | One university has just found out about a sport programming contest called ACM ICPC v2.0. This contest doesn't differ much from the well-known ACM ICPC, for example, the participants are not allowed to take part in the finals more than two times. However, there is one notable difference: the teams in the contest should... | The first line contains an integer n (3 ≤ n ≤ 100), n is the number of team participants. The second line contains n + 1 real numbers with no more than 6 digits after decimal point pi (0 ≤ i ≤ n, 0 ≤ pi ≤ 1) — the probability of that the team will win a medal if it contains i participants who has already been on the fi... | Print the only real number — the expected average number of medals won per year if the optimal strategy is used. The result may have absolute or relative error 10 - 6. | In the second test, no matter what participants the team contains, it is doomed to be successful. | Input: 30.115590 0.384031 0.443128 0.562356 | Output: 0.4286122500 | Expert | 4 | 2,091 | 399 | 167 | 0 |
671 | A | 671A | A. Recycling Bottles | 1,800 | dp; geometry; greedy; implementation | It was recycling day in Kekoland. To celebrate it Adil and Bera went to Central Perk where they can take bottles from the ground and put them into a recycling bin.We can think Central Perk as coordinate plane. There are n bottles on the ground, the i-th bottle is located at position (xi, yi). Both Adil and Bera can car... | First line of the input contains six integers ax, ay, bx, by, tx and ty (0 ≤ ax, ay, bx, by, tx, ty ≤ 109) — initial positions of Adil, Bera and recycling bin respectively.The second line contains a single integer n (1 ≤ n ≤ 100 000) — the number of bottles on the ground.Then follow n lines, each of them contains two i... | Print one real number — the minimum possible total distance Adil and Bera need to walk in order to put all bottles into recycling bin. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.Namely: let's assume that your answer is a, and the answer of the jury is b. The checker ... | Consider the first sample.Adil will use the following path: .Bera will use the following path: .Adil's path will be units long, while Bera's path will be units long. | Input: 3 1 1 2 0 031 12 12 3 | Output: 11.084259940083 | Medium | 4 | 1,038 | 476 | 366 | 6 |
839 | A | 839A | A. Arya and Bran | 900 | implementation | Bran and his older sister Arya are from the same house. Bran like candies so much, so Arya is going to give him some Candies.At first, Arya and Bran have 0 Candies. There are n days, at the i-th day, Arya finds ai candies in a box, that is given by the Many-Faced God. Every day she can give Bran at most 8 of her candie... | The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10000).The second line contains n integers a1, a2, a3, ..., an (1 ≤ ai ≤ 100). | If it is impossible for Arya to give Bran k candies within n days, print -1.Otherwise print a single integer — the minimum number of days Arya needs to give Bran k candies before the end of the n-th day. | In the first sample, Arya can give Bran 3 candies in 2 days.In the second sample, Arya can give Bran 17 candies in 3 days, because she can give him at most 8 candies per day.In the third sample, Arya can't give Bran 9 candies, because she can give him at most 8 candies per day and she must give him the candies within 1... | Input: 2 31 2 | Output: 2 | Beginner | 1 | 741 | 145 | 203 | 8 |
64 | E | 64E | E. Prime Segment | 1,800 | *special; brute force; math; number theory | Positive integer number x is called prime, if it has exactly two positive integer divisors. For example, 2, 3, 17, 97 are primes, but 1, 10, 120 are not.You are given an integer number n, find the shortest segment [a, b], which contains n (i.e. a ≤ n ≤ b) and a, b are primes. | The only given line contains an integer number n (2 ≤ n ≤ 10000). | Print the space separated pair of the required numbers a, b. | Input: 10 | Output: 7 11 | Medium | 4 | 276 | 65 | 60 | 0 | |
1,446 | B | 1446B | B. Catching Cheaters | 1,800 | dp; strings | You are given two strings \(A\) and \(B\) representing essays of two students who are suspected cheaters. For any two strings \(C\), \(D\) we define their similarity score \(S(C,D)\) as \(4\cdot LCS(C,D) - |C| - |D|\), where \(LCS(C,D)\) denotes the length of the Longest Common Subsequence of strings \(C\) and \(D\). Y... | The first line contains two positive integers \(n\) and \(m\) (\(1 \leq n, m \leq 5000\)) — lengths of the two strings \(A\) and \(B\). The second line contains a string consisting of \(n\) lowercase Latin letters — string \(A\).The third line contains a string consisting of \(m\) lowercase Latin letters — string \(B\)... | Output maximal \(S(C, D)\) over all pairs \((C, D)\), where \(C\) is some substring of \(A\), and \(D\) is some substring of \(B\). | For the first case:abb from the first string and abab from the second string have LCS equal to abb.The result is \(S(abb, abab) = (4 \cdot |abb|\)) - \(|abb|\) - \(|abab|\) = \(4 \cdot 3 - 3 - 4 = 5\). | Input: 4 5 abba babab | Output: 5 | Medium | 2 | 1,270 | 321 | 131 | 14 |
1,776 | C | 1776C | C. Library game | 2,500 | games; greedy; interactive; sortings | Alessia and Bernardo are discovering the world of competitive programming through the books of their university library.The library consists of \(m\) sections numbered from \(1\) to \(m\). Each section contains only books dedicated to a particular subject and different sections correspond to different subjects. In orde... | The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 100\), \(n \le m \le 5000\)) — the number of passes and the number of sections.The second line contains \(n\) integers \(x_1, \, x_2, \, \dots, \, x_n\) (\(1 \le x_i \le m\)) — the lengths of the passes available. | In the first sample, it can be shown that Alessia can accomplish her goal. An example of interaction (after reading the input) follows:$$$$$$ \begin{array}{|c|c|c|} \hline \textbf{Contestant} & \textbf{Judge} & \textbf{Explanation} \\ \hline \texttt{Alessia} & & \text{The program will act as Alessia} \\ \hline 3 \quad ... | Input: 5 14 3 7 2 3 10 | Output: - | Expert | 4 | 1,881 | 281 | 0 | 17 | |
1,357 | C1 | 1357C1 | C1. Prepare superposition of basis states with 0s | 0 | *special | You are given \(N\) qubits in the state \(|0...0 \rangle\). Your task is to prepare an equal superposition of all basis states that have one or more \(0\) in them.For example, for \(N = 2\) the required state would be \(\frac{1}{\sqrt{3}} \big( |00 \rangle + |01 \rangle + |10 \rangle)\).You are not allowed to use any g... | Beginner | 1 | 895 | 0 | 0 | 13 | ||||
1,720 | E | 1720E | E. Misha and Paintings | 2,700 | constructive algorithms; data structures; greedy; implementation; math | Misha has a square \(n \times n\) matrix, where the number in row \(i\) and column \(j\) is equal to \(a_{i, j}\). Misha wants to modify the matrix to contain exactly \(k\) distinct integers. To achieve this goal, Misha can perform the following operation zero or more times: choose any square submatrix of the matrix (y... | The first input line contains two integers \(n\) and \(k\) (\(1 \leq n \leq 500, 1 \leq k \leq n^2\)) — the size of the matrix and the desired amount of distinct elements in the matrix.Then \(n\) lines follows. The \(i\)-th of them contains \(n\) integers \(a_{i, 1}, a_{i, 2}, \ldots, a_{i, n}\) (\(1 \leq a_{i,j} \leq ... | Output one integer — the minimum number of operations required. | In the first test case the answer is \(1\), because one can change the value in the bottom right corner of the matrix to \(1\). The resulting matrix can be found below: 111112341 In the second test case the answer is \(2\). First, one can change the entire matrix to contain only \(1\)s, and the change the value of any ... | Input: 3 4 1 1 1 1 1 2 3 4 5 | Output: 1 | Master | 5 | 742 | 376 | 63 | 17 |
652 | A | 652A | A. Gabriel and Caterpillar | 1,400 | implementation; math | The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.Gabriel is interested when the caterpillar... | The first line contains two integers h1, h2 (1 ≤ h1 < h2 ≤ 105) — the heights of the position of the caterpillar and the apple in centimeters.The second line contains two integers a, b (1 ≤ a, b ≤ 105) — the distance the caterpillar goes up by day and slips down by night, in centimeters per hour. | Print the only integer k — the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.If the caterpillar can't get the apple print the only integer - 1. | In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the nex... | Input: 10 302 1 | Output: 1 | Easy | 2 | 826 | 297 | 198 | 6 |
321 | D | 321D | D. Ciel and Flipboard | 2,900 | dp; greedy; math | Fox Ciel has a board with n rows and n columns, there is one integer in each cell.It's known that n is an odd number, so let's introduce . Fox Ciel can do the following operation many times: she choose a sub-board with size x rows and x columns, then all numbers in it will be multiplied by -1.Return the maximal sum of ... | The first line contains an integer n, (1 ≤ n ≤ 33, and n is an odd integer) — the size of the board.Each of the next n lines contains n integers — the numbers in the board. Each number doesn't exceed 1000 by its absolute value. | Output a single integer: the maximal sum of numbers in the board that can be accomplished. | In the first test, we can apply this operation twice: first on the top left 2 × 2 sub-board, then on the bottom right 2 × 2 sub-board. Then all numbers will become positive. | Input: 3-1 -1 1-1 1 -11 -1 -1 | Output: 9 | Master | 3 | 378 | 227 | 90 | 3 |
1,497 | A | 1497A | A. Meximization | 800 | brute force; data structures; greedy; sortings | You are given an integer \(n\) and an array \(a_1, a_2, \ldots, a_n\). You should reorder the elements of the array \(a\) in such way that the sum of \(\textbf{MEX}\) on prefixes (\(i\)-th prefix is \(a_1, a_2, \ldots, a_i\)) is maximized.Formally, you should find an array \(b_1, b_2, \ldots, b_n\), such that the sets ... | The first line contains a single integer \(t\) \((1 \le t \le 100)\) — the number of test cases.The first line of each test case contains a single integer \(n\) \((1 \le n \le 100)\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) \((0 \le a_i \le 100)\). | For each test case print an array \(b_1, b_2, \ldots, b_n\) — the optimal reordering of \(a_1, a_2, \ldots, a_n\), so the sum of \(\textbf{MEX}\) on its prefixes is maximized.If there exist multiple optimal answers you can find any. | In the first test case in the answer \(\textbf{MEX}\) for prefixes will be: \(\textbf{MEX}(\{0\}) = 1\) \(\textbf{MEX}(\{0, 1\}) = 2\) \(\textbf{MEX}(\{0, 1, 2\}) = 3\) \(\textbf{MEX}(\{0, 1, 2, 3\}) = 4\) \(\textbf{MEX}(\{0, 1, 2, 3, 4\}) = 5\) \(\textbf{MEX}(\{0, 1, 2, 3, 4, 7\}) = 5\) \(\textbf{MEX}(\{0, 1, 2, 3, 4,... | Input: 3 7 4 2 0 1 3 3 7 5 2 2 8 6 9 1 0 | Output: 0 1 2 3 4 7 3 2 6 8 9 2 0 | Beginner | 4 | 753 | 291 | 232 | 14 |
2,123 | G | 2123G | G. Modular Sorting | 2,100 | brute force; data structures; greedy; math; number theory; sortings | You are given an integer \(m\) (\(2\leq m\leq 5\cdot 10^5\)) and an array \(a\) consisting of nonnegative integers smaller than \(m\).Answer queries of the following form: \(1\) \(i\) \(x\): assign \(a_i := x\) \(2\) \(k\): in one operation, you may choose an element \(a_i\) and assign \(a_i := (a_i + k) \pmod m\)\(^{\... | The first line contains an integer \(t\) (\(1\leq t\leq 10^4\)) — the number of test cases.The first line of each test case contains three integers, \(n\), \(m\), and \(q\) (\(2\leq n \leq 10^5\), \(2\leq m\leq 5\cdot10^5\), \(1\leq q\leq 10^5\)) — the size of the array \(a\), the integer \(m\), and the number of queri... | For each instance of query \(2\), output on a single line ""YES"" if there exists some sequence of (possibly zero) operations to make \(a\) nondecreasing, and ""NO"" otherwise. You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as p... | In the first sample, the array is initially: 4522410By applying the operation twice on \(a_1\), twice on \(a_2\), once on \(a_5\), twice on \(a_6\), and once on \(a_7\), the array becomes: 0122234 which is in nondecreasing order.After the second query, the array becomes: 4525410 and it can be shown that it is impossibl... | Input: 27 6 64 5 2 2 4 1 02 41 4 52 42 31 7 22 38 8 30 1 2 3 4 5 6 72 41 3 42 4 | Output: YES NO NO YES YES NO | Hard | 6 | 847 | 683 | 338 | 21 |
492 | C | 492C | C. Vanya and Exams | 1,400 | greedy; sortings | Vanya wants to pass n exams and get the academic scholarship. He will get the scholarship if the average grade mark for all the exams is at least avg. The exam grade cannot exceed r. Vanya has passed the exams and got grade ai for the i-th exam. To increase the grade for the i-th exam by 1 point, Vanya must write bi es... | The first line contains three integers n, r, avg (1 ≤ n ≤ 105, 1 ≤ r ≤ 109, 1 ≤ avg ≤ min(r, 106)) — the number of exams, the maximum grade and the required grade point average, respectively.Each of the following n lines contains space-separated integers ai and bi (1 ≤ ai ≤ r, 1 ≤ bi ≤ 106). | In the first line print the minimum number of essays. | In the first sample Vanya can write 2 essays for the 3rd exam to raise his grade by 2 points and 2 essays for the 4th exam to raise his grade by 1 point.In the second sample, Vanya doesn't need to write any essays as his general point average already is above average. | Input: 5 5 45 24 73 13 22 5 | Output: 4 | Easy | 2 | 451 | 292 | 53 | 4 |
1,864 | G | 1864G | G. Magic Square | 3,100 | combinatorics; constructive algorithms; implementation | Aquamoon has a Rubik's Square which can be seen as an \(n \times n\) matrix, the elements of the matrix constitute a permutation of numbers \(1, \ldots, n^2\).Aquamoon can perform two operations on the matrix: Row shift, i.e. shift an entire row of the matrix several positions (at least \(1\) and at most \(n-1\)) to th... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2\cdot 10^4\)). The description of the test cases follows.The first line of each test case contains an integer \(n\) (\(3\le n \le 500\)).The \(i\)-th of the following \(n\) lines contains \(n\) integers \(a_{i... | For each test case, if it is possible to convert the initial state to the target state respecting all the restrictions, output one integer — the number of ways to do so, modulo \(998\,244\,353\).If there is no solution, print a single integer \(0\). | In the first test case, the only way to transform the initial matrix to the target one is to shift the second row by \(1\) position to the right, and then shift the first column by \(1\) position downwards.In the second test case, it can be shown that there is no correct way to transform the matrix, thus, the answer is... | Input: 431 2 34 5 67 8 97 2 31 4 56 8 931 2 34 5 67 8 93 2 16 5 49 7 831 2 34 5 67 8 97 8 12 3 45 6 931 2 34 5 67 8 93 8 45 1 97 6 2 | Output: 1 0 0 4 | Master | 3 | 2,054 | 838 | 249 | 18 |
2,044 | C | 2044C | C. Hard Problem | 800 | greedy; math | Ball is the teacher in Paperfold University. The seats of his classroom are arranged in \(2\) rows with \(m\) seats each.Ball is teaching \(a + b + c\) monkeys, and he wants to assign as many monkeys to a seat as possible. Ball knows that \(a\) of them only want to sit in row \(1\), \(b\) of them only want to sit in ro... | The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases.Each test case contains four integers \(m\), \(a\), \(b\), and \(c\) (\(1 \leq m, a, b, c \leq 10^8\)). | For each test case, output the maximum number of monkeys you can seat. | In the second test case, \(6\) monkeys want to sit in the front row, but only \(3\) seats are available. The monkeys that have no preference and the monkeys who prefer sitting in the second row can sit in the second row together. Thus, the answer is \(3+2=5\). | Input: 510 5 5 103 6 1 115 14 12 41 1 1 1420 6 9 69 | Output: 20 5 30 2 84 | Beginner | 2 | 524 | 196 | 70 | 20 |
1,500 | F | 1500F | F. Cupboards Jumps | 3,500 | dp | In the house where Krosh used to live, he had \(n\) cupboards standing in a line, the \(i\)-th cupboard had the height of \(h_i\). Krosh moved recently, but he wasn't able to move the cupboards with him. Now he wants to buy \(n\) new cupboards so that they look as similar to old ones as possible.Krosh does not remember... | The first line contains two integers \(n\) and \(C\) (\(3 \leq n \leq 10^6\), \(0 \leq C \leq 10^{12}\)) — the number of cupboards and the limit on possible \(w_i\).The second line contains \(n - 2\) integers \(w_1, w_2, \ldots, w_{n - 2}\) (\(0 \leq w_i \leq C\)) — the values defined in the statement. | If there is no suitable sequence of \(n\) cupboards, print ""NO"".Otherwise print ""YES"" in the first line, then in the second line print \(n\) integers \(h'_1, h'_2, \ldots, h'_n\) (\(0 \le h'_i \le 10^{18}\)) — the heights of the cupboards to buy, from left to right.We can show that if there is a solution, there is ... | Consider the first example: \(w_1 = \max(4, 8, 8) - \min(4, 8, 8) = 8 - 4 = 4\) \(w_2 = \max(8, 8, 16) - \min(8, 8, 16) = 16 - 8 = 8\) \(w_3 = \max(8, 16, 20) - \min(8, 16, 20) = 20 - 8 = 12\) \(w_4 = \max(16, 20, 4) - \min(16, 20, 4) = 20 - 4 = 16\) \(w_5 = \max(20, 4, 0) - \min(20, 4, 0) = 20 - 0 = 20\) There are oth... | Input: 7 20 4 8 12 16 20 | Output: YES 4 8 8 16 20 4 0 | Master | 1 | 933 | 303 | 415 | 15 |
1,154 | G | 1154G | G. Minimum Possible LCM | 2,200 | brute force; greedy; math; number theory | You are given an array \(a\) consisting of \(n\) integers \(a_1, a_2, \dots, a_n\).Your problem is to find such pair of indices \(i, j\) (\(1 \le i < j \le n\)) that \(lcm(a_i, a_j)\) is minimum possible.\(lcm(x, y)\) is the least common multiple of \(x\) and \(y\) (minimum positive number such that both \(x\) and \(y\... | The first line of the input contains one integer \(n\) (\(2 \le n \le 10^6\)) — the number of elements in \(a\).The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^7\)), where \(a_i\) is the \(i\)-th element of \(a\). | Print two integers \(i\) and \(j\) (\(1 \le i < j \le n\)) such that the value of \(lcm(a_i, a_j)\) is minimum among all valid pairs \(i, j\). If there are multiple answers, you can print any. | Input: 5 2 4 8 3 6 | Output: 1 2 | Hard | 4 | 351 | 263 | 192 | 11 | |
1,977 | D | 1977D | D. XORificator | 2,300 | bitmasks; brute force; greedy; hashing | You are given a binary (consisting only of 0s and 1s) \(n \times m\) matrix. You are also given a XORificator, using which you can invert all the values in a chosen row (i.e. replace 0 with 1 and 1 with 0).A column in the matrix is considered special if it contains exactly one 1. Your task is to find the maximum number... | Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 3 \cdot 10^5\), \(n \cdot m \leq 3 \cdot... | For each test case, output two lines.In the first line, output the maximum number of special columns that is possible to get simultaneously.In the second line, output a binary string of length \(n\), where the \(i\)-th character is 0, if you don't use the XORificator on the \(i\)-th row, and 1, if you use the XORificat... | In the first test case, you can use the XORificator on the second row to make the columns \(2\), \(3\), and \(4\) special.In the second test case, the only column is already special, so you don't need to use the XORificator. | Input: 53 41010011001001 111 102 500101101103 3101111000 | Output: 3 010 1 0 1 1 3 00 2 010 | Expert | 4 | 445 | 521 | 458 | 19 |
1,061 | F | 1061F | F. Lost Root | 2,400 | interactive; probabilities | The graph is called tree if it is connected and has no cycles. Suppose the tree is rooted at some vertex. Then tree is called to be perfect \(k\)-ary tree if each vertex is either a leaf (has no children) or has exactly \(k\) children. Also, in perfect \(k\)-ary tree all leafs must have same depth.For example, the pict... | The tree in the example is as follows:The input and output for example illustrate possible interaction on that test (empty lines are inserted only for clarity).The hack corresponding to the example would look like:3 22 3 1 | Input: 3 2NoYes | Output: ? 1 3 2? 1 2 3! 2 | Expert | 2 | 1,133 | 0 | 0 | 10 | ||
595 | B | 595B | B. Pasha and Phone | 1,600 | binary search; math | Pasha has recently bought a new phone jPager and started adding his friends' phone numbers there. Each phone number consists of exactly n digits.Also Pasha has a number k and two sequences of length n / k (n is divisible by k) a1, a2, ..., an / k and b1, b2, ..., bn / k. Let's split the phone number into blocks of leng... | The first line of the input contains two integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ min(n, 9)) — the length of all phone numbers and the length of each block, respectively. It is guaranteed that n is divisible by k.The second line of the input contains n / k space-separated positive integers — sequence a1, a2, ..., an ... | Print a single integer — the number of good phone numbers of length n modulo 109 + 7. | In the first test sample good phone numbers are: 000000, 000098, 005600, 005698, 380000, 380098, 385600, 385698. | Input: 6 238 56 497 3 4 | Output: 8 | Medium | 2 | 1,037 | 460 | 85 | 5 |
1,917 | F | 1917F | F. Construct Tree | 2,500 | bitmasks; constructive algorithms; dp; trees | You are given an array of integers \(l_1, l_2, \dots, l_n\) and an integer \(d\). Is it possible to construct a tree satisfying the following three conditions? The tree contains \(n + 1\) nodes. The length of the \(i\)-th edge is equal to \(l_i\). The (weighted) diameter of the tree is equal to \(d\). | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 250\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\), \(d\) (\(2 \leq n \leq 2000, 1 \leq d \leq 2000\)).The second line of each... | For each test case, output \(\texttt{Yes}\) if it is possible to construct a tree that satisfies all the conditions, and \(\texttt{No}\) otherwise.You can print the letters in any case (upper or lower). | Below, you are given the illustrations of trees for the first and third test cases. One of the diameters is highlighted by coloring its edges in red. | Input: 34 101 2 3 44 71 4 3 46 182 4 3 7 6 7 | Output: Yes No Yes | Expert | 4 | 302 | 488 | 202 | 19 |
1,375 | H | 1375H | H. Set Merging | 3,300 | constructive algorithms; divide and conquer | You are given a permutation \(a_1, a_2, \dots, a_n\) of numbers from \(1\) to \(n\). Also, you have \(n\) sets \(S_1,S_2,\dots, S_n\), where \(S_i=\{a_i\}\). Lastly, you have a variable \(cnt\), representing the current number of sets. Initially, \(cnt = n\).We define two kinds of functions on sets:\(f(S)=\min\limits_{... | The first line contains two integers \(n,q\) \((1\leq n \leq 2^{12},1 \leq q \leq 2^{16})\) — the length of the permutation and the number of needed sets correspondently.The next line consists of \(n\) integers \(a_1,a_2,\cdots, a_n\) (\(1\leq a_i\leq n\), \(a_i\) are pairwise distinct) — given permutation.\(i\)-th of ... | It is guaranteed that a solution under given constraints exists.The first line should contain one integer \(cnt_E\) \((n\leq cnt_E\leq 2.2\times 10^6)\), representing the number of sets after all operations.\(cnt_E-n\) lines must follow, each line should contain two integers \(u\), \(v\) (\(1\leq u, v\leq cnt'\), where... | In the first sample:We have \(S_1=\{1\},S_2=\{3\},S_3=\{2\}\) initially.In the first operation, because \(g(S_3)=2<f(S_2)=3\), we can merge \(S_3,S_2\) into \(S_4=\{2,3\}\).In the second operation, because \(g(S_1)=1<f(S_3)=2\), we can merge \(S_1,S_3\) into \(S_5=\{1,2\}\).In the third operation, because \(g(S_5)=2<f(... | Input: 3 2 1 3 2 2 3 1 3 | Output: 6 3 2 1 3 5 2 4 6 | Master | 2 | 1,252 | 451 | 702 | 13 |
1,799 | E | 1799E | E. City Union | 2,300 | constructive algorithms; dfs and similar; dsu; geometry; greedy; implementation; math | You are given \(n \times m\) grid. Some cells are filled and some are empty.A city is a maximal (by inclusion) set of filled cells such that it is possible to get from any cell in the set to any other cell in the set by moving to adjacent (by side) cells, without moving into any cells not in the set. In other words, a ... | Input consists of multiple test cases. The first line contains a single integer \(t\), the number of test cases (\(1 \le t \le 5000\)).The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 50\), \(nm \geq 3\)).The next \(n\) lines describe the grid. The \(i\)-th line contains a string... | For each test case, output \(n\) lines, each containing a string of length \(m\), describing the grid you create in the same format as the input.If there are multiple possible answers with the minimum number of filled cells print any. | In the first test case, we can add a single filled cell between the two cities to connect them. We can verify that the second condition is satisfied.In the second test case, we can also connect the cities with a single filled cell, while satisfying the second condition. In the third test case, note that if we filled th... | Input: 111 3#.#2 2.##.4 4..##...##...##..6 6.##...##..............##.....#...###6 5.#..#.#..#.#..#.#.##.#...##...5 5######...##.#.##...######4 4.##.##.##.##.##.5 5..###....#.....#....#....5 6.##...##....#....#....##...##.6 5..##....##....##....##....##..5 4..##..#...#.#...#... | Output: ### .# ## ..## ..## ###. ##.. .#... | Expert | 7 | 948 | 599 | 234 | 17 |
1,510 | J | 1510J | J. Japanese Game | 2,700 | constructive algorithms; math | Joseph really likes the culture of Japan. Last year he learned Japanese traditional clothes and visual arts and now he is trying to find out the secret of the Japanese game called Nonogram.In the one-dimensional version of the game, there is a row of \(n\) empty cells, some of which are to be filled with a pen. There i... | The only line contains a string \(m\) — the mask of the source profile \(p\). The length of \(m\) is \(n\) (\(1 \le n \le 100\,000\)). The string \(m\) consists of symbols # and _ — denoting filled and empty cells respectively. | If there is no profile with the mask \(m\), output the number \(-1\). Otherwise, on the first line, output an integer \(k\) — the number of integers in the profile \(p'\). On the second line, output \(k\) integers of the profile \(p'\). | Input: __#_____ | Output: 2 3 2 | Master | 2 | 1,508 | 227 | 236 | 15 | |
581 | C | 581C | C. Developing Skills | 1,400 | implementation; math; sortings | Petya loves computer games. Finally a game that he's been waiting for so long came out!The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is c... | The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 107) — the number of skills of the character and the number of units of improvements at Petya's disposal.The second line of the input contains a sequence of n integers ai (0 ≤ ai ≤ 100), where ai characterizes the level of the i-th... | The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using k or less improvement units. | In the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rf... | Input: 2 47 9 | Output: 2 | Easy | 3 | 1,097 | 344 | 171 | 5 |
2,119 | B | 2119B | B. Line Segments | 1,200 | geometry; greedy; math | PIKASONIC - Lost My Mind (feat.nakotanmaru) You are given two points \((p_x,p_y)\) and \((q_x,q_y)\) on a Euclidean plane.You start at the starting point \((p_x,p_y)\) and will perform \(n\) operations. In the \(i\)-th operation, you must choose any point such that the Euclidean distance\(^{\text{∗}}\) between your cur... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(1\leq n \leq 10^3\)) — the length of the sequence \(a\).The second line of each test case ... | For each test case, output ""Yes"" if it is possible to reach the terminal point \((q_x,q_y)\) after all operations; otherwise, output ""No"".You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses. | Here is a picture that shows a possible movement of the first test case. The coordinates of point \(r_1\) are \((3,1+\sqrt{5})\). The first test case. Here is a picture that shows a possible movement of the second test case. The coordinates of point \(r_1\) are \((1+\sqrt{3},0)\), and the coordinates of point \(r_2\) a... | Input: 521 1 5 13 331 1 3 32 3 42100 100 100 1004 515 1 1 45210000000 10000000 10000000 1000000010000 10000 | Output: Yes Yes No Yes Yes | Easy | 3 | 626 | 696 | 303 | 21 |
855 | F | 855F | F. Nagini | 3,100 | binary search; data structures | Nagini, being a horcrux You-know-who created with the murder of Bertha Jorkins, has accumulated its army of snakes and is launching an attack on Hogwarts school. Hogwarts' entrance can be imagined as a straight line (x-axis) from 1 to 105. Nagini is launching various snakes at the Hogwarts entrance. Each snake lands pa... | First line of input contains a single integer q (1 ≤ q ≤ 5·104) denoting the number of queries.Next q lines each describe a query. Each query description first contains the query type typei (1 ≤ typei ≤ 2). This is followed by further description of the query. In case of the type being 1, it is followed by integers li,... | Output the answer for each query of type 2 in a separate line. | In the first sample case, the danger value for x-coordinates 1 is 0 as there is no y2 satisfying the above condition for x = 1.Danger values for x-coordinates 2 and 3 is 10 + | - 7| = 17.Danger values for x-coordinates 4 to 9 is again 0 as there is no y2 satisfying the above condition for these coordinates.Thus, total ... | Input: 31 1 10 101 2 4 -72 1 10 | Output: 34 | Master | 2 | 1,439 | 432 | 62 | 8 |
1,153 | C | 1153C | C. Serval and Parenthesis Sequence | 1,700 | greedy; strings | Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School.In his favorite math class, the teacher taught him the following interesting definitions.A parenthesis sequence is a string, containing only characters ""("" and "")"".A correct parenthesis sequence is a parenthesis sequence th... | The first line contains a single integer \(|s|\) (\(1\leq |s|\leq 3 \cdot 10^5\)), the length of the string.The second line contains a string \(s\), containing only ""("", "")"" and ""?"". | A single line contains a string representing the answer.If there are many solutions, any of them is acceptable.If there is no answer, print a single line containing "":("" (without the quotes). | It can be proved that there is no solution for the second sample, so print "":("". | Input: 6 (????? | Output: (()()) | Medium | 2 | 1,606 | 188 | 193 | 11 |
1,949 | J | 1949J | J. Amanda the Amoeba | 2,600 | graphs; implementation; trees; two pointers | This problem has an attachment. You can use it to simulate and visualize the movements of the amoeba.Amoeba Amanda lives inside a rectangular grid of square pixels. Her body occupies some of these pixels. Other pixels may be either free or blocked. Amanda moves across the grid using the so-called amoeboid movement. In ... | The first line contains two integers \(r\) and \(c\) (\(1\le r,c \le 50\)) — the size of the rectangular grid in pixels.The next \(r\) lines contain \(c\) characters each, describing the initial position of Amanda. Each of those characters is either a dot \(\texttt{.}\) denoting a free pixel, an asterisk \(\texttt{*}\)... | Print \(\texttt{YES}\) if it is possible for Amanda to go from the initial position to the final one. Otherwise, print \(\texttt{NO}\).If it is possible, on the next line print one integer \(m\) (\(0\le m\le 10\,000\)) — the number of moves to execute.The following \(m\) lines must contain four integer coordinates each... | In the first sample, Amanda executes 5 moves to reach the final position, as shown in the figure below. | Input: 5 8.******.**.X**..*******.**.X**...******..******....X****.*******...X****.******. | Output: YES 5 3 1 3 8 2 1 2 8 4 1 4 8 2 2 4 7 4 2 2 7 | Expert | 4 | 1,266 | 920 | 1,009 | 19 |
1,809 | F | 1809F | F. Traveling in Berland | 2,500 | binary search; data structures; graphs; greedy; implementation | There are \(n\) cities in Berland, arranged in a circle and numbered from \(1\) to \(n\) in clockwise order.You want to travel all over Berland, starting in some city, visiting all the other cities and returning to the starting city. Unfortunately, you can only drive along the Berland Ring Highway, which connects all \... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(3 \le n \le 2 \cdot 10^5\); \(1 \le k \le 10^9\)) — the number of cities and the volume of fuel tank, respectively.The second line contains \(n\) int... | For each test case, print \(n\) integers, where the \(i\)-th of them is equal to the minimum cost of the journey if you start and finish in the \(i\)-th city. | Input: 43 53 4 41 2 25 71 3 2 5 12 1 1 1 24 31 2 1 32 2 2 23 22 2 21 2 1 | Output: 17 19 17 13 12 12 12 14 14 14 14 14 8 8 8 | Expert | 5 | 1,076 | 528 | 158 | 18 | |
282 | D | 282D | D. Yet Another Number Game | 2,100 | dp; games | Since most contestants do not read this part, I have to repeat that Bitlandians are quite weird. They have their own jobs, their own working method, their own lives, their own sausages and their own games!Since you are so curious about Bitland, I'll give you the chance of peeking at one of these games.BitLGM and BitAry... | The first line contains an integer n (1 ≤ n ≤ 3).The next line contains n integers a1, a2, ..., an (0 ≤ ai < 300). | Write the name of the winner (provided that both players play optimally well). Either ""BitLGM"" or ""BitAryo"" (without the quotes). | Input: 21 1 | Output: BitLGM | Hard | 2 | 1,068 | 114 | 133 | 2 | |
1,859 | C | 1859C | C. Another Permutation Problem | 1,200 | brute force; dp; greedy; math | Andrey is just starting to come up with problems, and it's difficult for him. That's why he came up with a strange problem about permutations\(^{\dagger}\) and asks you to solve it. Can you do it?Let's call the cost of a permutation \(p\) of length \(n\) the value of the expression: \((\sum_{i = 1}^{n} p_i \cdot i) - (... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 30\)) — the number of test cases. The description of the test cases follows.The only line of each test case contains a single integer \(n\) (\(2 \le n \le 250\)) — the length of the permutation.It is guaranteed that... | For each test case, output a single integer — the maximum cost among all permutations of length \(n\). | In the first test case, the permutation with the maximum cost is \([2, 1]\). The cost is equal to \(2 \cdot 1 + 1 \cdot 2 - \max (2 \cdot 1, 1 \cdot 2)= 2 + 2 - 2 = 2\).In the second test case, the permutation with the maximum cost is \([1, 2, 4, 3]\). The cost is equal to \(1 \cdot 1 + 2 \cdot 2 + 4 \cdot 3 + 3 \cdot ... | Input: 52431020 | Output: 2 17 7 303 2529 | Easy | 4 | 752 | 382 | 102 | 18 |
796 | C | 796C | C. Bank Hacking | 1,900 | constructive algorithms; data structures; dp; trees | Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To search for Zane, he would need a lot of money, of which he sadly has none. To deal with the problem, he has decided to hack the banks. There are n banks, numbered from 1 to n. There are also n - 1 wires connecting the banks. All... | The first line contains one integer n (1 ≤ n ≤ 3·105) — the total number of banks.The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the strengths of the banks.Each of the next n - 1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — meaning that there is a wire directly connecting... | Print one integer — the minimum strength of the computer Inzane needs to accomplish the goal. | In the first sample, Inzane can hack all banks using a computer with strength 5. Here is how: Initially, strengths of the banks are [1, 2, 3, 4, 5]. He hacks bank 5, then strengths of the banks become [1, 2, 4, 5, - ]. He hacks bank 4, then strengths of the banks become [1, 3, 5, - , - ]. He hacks bank 3, then strength... | Input: 51 2 3 4 51 22 33 44 5 | Output: 5 | Hard | 4 | 1,469 | 487 | 93 | 7 |
2,123 | B | 2123B | B. Tournament | 800 | greedy | You are given an array of integers \(a_1,a_2,\dots,a_n\). A tournament is held with \(n\) players. Player \(i\) has strength \(a_i\).While more than \(k\) players remain, Two remaining players are chosen at random; Then the chosen player with the lower strength is eliminated. If the chosen players have the same strengt... | The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases.The first line of each test case contains three integers \(n\), \(j\), and \(k\) (\(2\leq n \leq 2\cdot 10^5\), \(1\leq j, k\leq n\)).The second line of each test case contains \(n\) integers, \(a_1,a_2,\dots,a_n\) (\(1\leq a_i... | For each test case, output on a single line ""YES"" if player \(j\) can be one of the last \(k\) remaining players, and ""NO"" otherwise. You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses. | In the first sample, suppose that players \(2\) and \(5\) are chosen. Then player \(2\) defeats player \(5\). Now, the remaining player strengths are 3244 Next, suppose that players \(3\) and \(4\) are chosen. Then player \(3\) might defeat player \(4\). Now, the remaining player strengths are 324 Player \(2\) is one o... | Input: 35 2 33 2 4 4 15 4 15 3 4 5 26 1 11 2 3 4 5 6 | Output: YES YES NO | Beginner | 1 | 501 | 421 | 299 | 21 |
2,087 | E | 2087E | E. Color the Arrows | 0 | *special; *special; dp; dp | There are \(n\) arrows drawn in a row on a strip of paper, numbered from \(1\) to \(n\). Each arrow points either to the left or to the right. Initially, all arrows are painted blue.In one operation, you can repaint a blue arrow into red. For the first operation, you can choose any arrow. For each subsequent operation,... | The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.In the first line of each test case, there is a single integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)) — the number of arrows.In the second line, there is a string consisting of \(n\) characters '<' and/or '>' (ASCII codes 60 ... | For each test case, print a single integer — the maximum score that can be achieved if you are allowed to perform any number of operations (including zero). | In the first test case, you can first repaint the arrow at index \(2\). This arrow points to the right, so in the next operation, you have to choose an arrow to the right of it. Let's repaint the arrow at index \(3\). This arrow also points to the right, so no more arrows can be repainted. The answer is \(c_2 + c_3 = 4... | Input: 53<>>5 4 65<><>>5 -2 4 -3 72>>-1 -28>>>><<<<1 -1 1 -1 1 -1 1 -15><<<>-1 100 100 100 100 | Output: 10 9 0 4 399 | Beginner | 4 | 1,063 | 588 | 156 | 20 |
1,505 | F | 1505F | F. Math | 2,200 | *special; math | *The two images are equivalent, feel free to use either one. | The input contains a single integer \(a\) (\(-100 \le a \le 100\)). | Output the result – an integer number. | Input: 1 | Output: 1 | Hard | 2 | 60 | 67 | 38 | 15 | |
1,794 | B | 1794B | B. Not Dividing | 900 | constructive algorithms; greedy; math | You are given an array of \(n\) positive integers \(a_1, a_2, \ldots, a_n\). In one operation, you can choose any number of the array and add \(1\) to it. Make at most \(2n\) operations so that the array satisfies the following property: \(a_{i+1}\) is not divisible by \(a_i\), for each \(i = 1, 2, \ldots, n-1\). You d... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1\le n\le 10^4\)) — the length of the given array. The second line of each test case contains \(n... | For each test case, print the answer on a separate line. In the only line, print \(n\) integers — the resulting array \(a\) after applying at most \(2n\) operations. We can show that an answer always exists under the given constraints. If there are multiple answers, print any of them. | In the first test case, the array \([4, 5, 6, 7]\) can be achieved by applying \(2\) operations to the first element, \(1\) operation to the second element, \(3\) operations to the third element, and \(1\) operation to the last element. The total number of operations performed is \(7\), which is less than the allowed \... | Input: 342 4 3 631 2 324 2 | Output: 4 5 6 7 3 2 3 4 2 | Beginner | 3 | 368 | 488 | 285 | 17 |
2,005 | E1 | 2005E1 | E1. Subtangle Game (Easy Version) | 2,100 | dp; games; greedy; implementation | This is the easy version of the problem. The differences between the two versions are the constraints on all the variables. You can make hacks only if both versions of the problem are solved.Tsovak and Narek are playing a game. They have an array \(a\) and a matrix \(b\) of integers with \(n\) rows and \(m\) columns, n... | The first line of the input contains \(t\) (\(1 \le t \le 300\)) – the number of test cases.The first line of each test case contains three integers \(l\), \(n\), and \(m\) (\(1 \le l, n, m \le 300\)) – the size of the array and the sizes of the matrix.The second line contains \(l\) integers \(a_1, a_2, a_3, \ldots a_l... | You should output \(t\) lines, the \(i\)-th of them containing a character representing the answer of the \(i\)-th test case: ""T"" if Tsovak wins or ""N"", otherwise (without quotes). | In the first example, Tsovak starts by looking for \(1\). There is only one occurrence of \(1\) at \((1,1)\), so he chooses it. Then Narek needs to look for \(2\) in the submatrix of \((2, 2)\), which consists of just the last two elements: \(5\) and \(2\). He chooses \(2\), and then Tsovak loses since the array has en... | Input: 32 2 31 21 3 54 5 22 2 41 21 1 3 24 2 5 12 4 21 23 45 55 55 5 | Output: N T N | Hard | 4 | 1,079 | 767 | 184 | 20 |
526 | A | 526A | A. King of Thieves | 1,300 | brute force; implementation | In this problem you will meet the simplified model of game King of Thieves.In a new ZeptoLab game called ""King of Thieves"" your aim is to reach a chest with gold by controlling your character, avoiding traps and obstacles on your way. An interesting feature of the game is that you can design your own levels that will... | The first line contains integer n (1 ≤ n ≤ 100) — the number of segments on the level.Next line contains the scheme of the level represented as a string of n characters '*' and '.'. | If the level is good, print the word ""yes"" (without the quotes), otherwise print the word ""no"" (without the quotes). | In the first sample test you may perform a sequence of jumps through platforms 2, 5, 8, 11, 14. | Input: 16.**.*..*.***.**. | Output: yes | Easy | 2 | 1,447 | 181 | 120 | 5 |
1,795 | D | 1795D | D. Triangle Coloring | 1,600 | combinatorics; math | You are given an undirected graph consisting of \(n\) vertices and \(n\) edges, where \(n\) is divisible by \(6\). Each edge has a weight, which is a positive (greater than zero) integer.The graph has the following structure: it is split into \(\frac{n}{3}\) triples of vertices, the first triple consisting of vertices ... | The first line contains one integer \(n\) (\(6 \le n \le 3 \cdot 10^5\), \(n\) is divisible by \(6\)).The second line contains \(n\) integers \(w_1, w_2, \dots, w_n\) (\(1 \le w_i \le 1000\)) — the weights of the edges. Edge \(1\) connects vertices \(1\) and \(2\), edge \(2\) connects vertices \(1\) and \(3\), edge \(3... | Print one integer — the number of valid colorings with maximum possible weight, taken modulo \(998244353\). | The following picture describes the graph from the first example test. The maximum possible weight of a valid coloring of this graph is \(31\). | Input: 12 1 3 3 7 8 5 2 2 2 2 4 2 | Output: 36 | Medium | 2 | 1,051 | 506 | 107 | 17 |
53 | C | 53C | C. Little Frog | 1,200 | constructive algorithms | Once upon a time a little frog whose name was Vasya decided to travel around his home swamp. Overall there are n mounds on the swamp, located on one line. The distance between the neighboring mounds is one meter. Vasya wants to visit all the mounds in one day; besides, he wants to visit each one exactly once. For that ... | The single line contains a number n (1 ≤ n ≤ 104) which is the number of mounds. | Print n integers pi (1 ≤ pi ≤ n) which are the frog's route plan. All the pi's should be mutually different. All the |pi–pi + 1|'s should be mutually different (1 ≤ i ≤ n - 1). If there are several solutions, output any. | Input: 2 | Output: 1 2 | Easy | 1 | 618 | 80 | 220 | 0 | |
1,250 | M | 1250M | M. SmartGarden | 2,500 | constructive algorithms; divide and conquer | Berland Gardeners United Inc. hired you for the project called ""SmartGarden"". The main feature of this project is automatic garden watering.Formally the garden can be represented as a square of \(n \times n\) cells with rows numbered \(1\) to \(n\) from top to bottom and columns numbered \(1\) to \(n\) from left to r... | The first and the only line of the input contains a single integer \(n\) (\(2 \le n \le 5000\)), where \(n\) is the size of the garden. | In the first line print the total number of commands for the robot \(k\) (\(1 \le k \le 50\)). In the next \(2 \cdot k\) lines print all the commands. Each command should be specified by \(2\) lines. The first line of each command should describe rows in the command and the second line should describe columns in the co... | Input: 2 | Output: 2 1 1 1 2 1 1 1 2 | Expert | 2 | 2,091 | 135 | 725 | 12 | |
1,812 | E | 1812E | E. Not a Geometry Problem | 0 | *special; *special; constructive algorithms; geometry; math | The only line of input contains three integers \(x\), \(y\), \(z\) (\(-1000 \le x, y, z \le 1000\)). | Output one real number — the answer.Your answer is considered correct if its absolute or relative error does not exceed \(10^6\). Formally, let your answer be \(a\), and the jury's answer be \(b\). Your answer is accepted if and only if \(\frac{|a-b|}{\max(1,|b|)} \le 10^6\). | Input: 1 1 1 | Output: 1.7320508075688772 | Beginner | 5 | 0 | 100 | 276 | 18 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.