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,933
B
1933B
B. Turtle Math: Fast Three Task
800
implementation; math; number theory
You are given an array \(a_1, a_2, \ldots, a_n\).In one move, you can perform either of the following two operations: Choose an element from the array and remove it from the array. As a result, the length of the array decreases by \(1\); Choose an element from the array and increase its value by \(1\). You can perform ...
The first line of the input 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 10^5\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^4\)).The sum of \(...
For each test case, output a single integer: the minimum number of moves.
In the first test case, initially the array \(a = [2, 2, 5, 4]\). One of the optimal ways to make moves is: remove the current \(4\)th element and get \(a = [2, 2, 5]\); As a result, the sum of the elements of the array \(a\) will be divisible by \(3\) (indeed, \(a_1 + a_2 + a_3 = 2 + 2 + 5 = 9\)).In the second test ca...
Input: 842 2 5 431 3 243 7 6 81142 2 4 225 572 4 8 1 9 3 424 10 | Output: 1 0 0 1 1 2 1 1
Beginner
3
679
377
73
19
1,902
D
1902D
D. Robot Queries
1,900
binary search; data structures; dp; implementation
There is an infinite \(2\)-dimensional grid. Initially, a robot stands in the point \((0, 0)\). The robot can execute four commands: U β€” move from point \((x, y)\) to \((x, y + 1)\); D β€” move from point \((x, y)\) to \((x, y - 1)\); L β€” move from point \((x, y)\) to \((x - 1, y)\); R β€” move from point \((x, y)\) to \((...
The first line contains two integers \(n\) and \(q\) (\(1 \le n, q \le 2 \cdot 10^5\)) β€” the length of the command sequence and the number of queries, respectively.The second line contains a string \(s\) of length \(n\), consisting of characters U, D, L and/or R.Then \(q\) lines follow, the \(i\)-th of them contains fo...
For each query, print YES if the robot visits the point \((x, y)\), while executing a sequence \(s\), but the substring from \(l\) to \(r\) is reversed; otherwise print NO.
In the first query of the first sample, the path of the robot looks as follows: In the second query of the first sample, the path of the robot looks as follows: In the third query of the first sample, the path of the robot looks as follows:
Input: 8 3RDLLUURU-1 2 1 70 0 3 40 1 7 8 | Output: YES YES NO
Hard
4
766
453
172
19
1,284
C
1284C
C. New Year and Permutation
1,600
combinatorics; math
Recall that the permutation is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array) and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array)...
The only line contains two integers \(n\) and \(m\) (\(1 \le n \le 250\,000\), \(10^8 \le m \le 10^9\), \(m\) is prime).
Print \(r\) (\(0 \le r < m\)), the sum of happiness for all permutations of length \(n\), modulo a prime number \(m\).
For sample input \(n=3\), let's consider all permutations of length \(3\): \([1, 2, 3]\), all subsegments are framed segment. Happiness is \(6\). \([1, 3, 2]\), all subsegments except \([1, 2]\) are framed segment. Happiness is \(5\). \([2, 1, 3]\), all subsegments except \([2, 3]\) are framed segment. Happiness is \(5...
Input: 1 993244853 | Output: 1
Medium
2
1,730
120
118
12
825
G
825G
G. Tree Queries
2,500
dfs and similar; graphs; trees
You are given a tree consisting of n vertices (numbered from 1 to n). Initially all vertices are white. You have to process q queries of two different types: 1 x β€” change the color of vertex x to black. It is guaranteed that the first query will be of this type. 2 x β€” for the vertex x, find the minimum index y such tha...
The first line contains two numbers n and q (3 ≀ n, q ≀ 106).Then n - 1 lines follow, each line containing two numbers xi and yi (1 ≀ xi < yi ≀ n) and representing the edge between vertices xi and yi.It is guaranteed that these edges form a tree.Then q lines follow. Each line contains two integers ti and zi, where ti i...
For each query of type 2 output the answer to it.
Input: 4 61 22 33 41 21 22 21 32 22 2 | Output: 321
Expert
3
553
652
49
8
1,673
E
1673E
E. Power or XOR?
2,500
bitmasks; combinatorics; math; number theory
The symbol \(\wedge\) is quite ambiguous, especially when used without context. Sometimes it is used to denote a power (\(a\wedge b = a^b\)) and sometimes it is used to denote the XOR operation (\(a\wedge b=a\oplus b\)). You have an ambiguous expression \(E=A_1\wedge A_2\wedge A_3\wedge\ldots\wedge A_n\). You can repla...
The first line of input contains two integers \(n\) and \(k\) \((1\leq n\leq 2^{20}, 0\leq k < n)\).The second line of input contains \(n\) integers \(B_1,B_2,\ldots,B_n\) \((1\leq B_i < 2^{20})\).
Print a single line containing a binary string without leading zeroes denoting the answer to the problem. If the answer is equal to \(0\), print \(0\).
For each of the testcases \(1\) to \(3\), \(A = \{2^3,2^1,2^2\} = \{8,2,4\}\) and \(E=8\wedge 2\wedge 4\).For the first testcase, there is only one possible valid unambiguous expression \(E' = 8\oplus 2\oplus 4 = 14 = (1110)_2\).For the second testcase, there are three possible valid unambiguous expressions \(E'\): \(8...
Input: 3 2 3 1 2 | Output: 1110
Expert
4
1,583
197
151
16
1,665
D
1665D
D. GCD Guess
2,000
bitmasks; chinese remainder theorem; constructive algorithms; games; interactive; math; number theory
This is an interactive problem.There is a positive integer \(1 \le x \le 10^9\) that you have to guess.In one query you can choose two positive integers \(a \neq b\). As an answer to this query you will get \(\gcd(x + a, x + b)\), where \(\gcd(n, m)\) is the greatest common divisor of the numbers \(n\) and \(m\).To gue...
The first line of input contains a single integer \(t\) (\(1 \le t \le 1000\)) denoting the number of test cases.The integer \(x\) that you have to guess satisfies the constraints: (\(1 \le x \le 10^9\)).
The first hidden number is \(4\), that's why the answers for the queries are:""? 1 2"" β€” \(\gcd(4 + 1, 4 + 2) = \gcd(5, 6) = 1\).""? 12 4"" β€” \(\gcd(4 + 12, 4 + 4) = \gcd(16, 8) = 8\).The second hidden number is \(10^9\), that's why the answer for the query is:""? 2000000000 1999999999"" β€” \(\gcd(3 \cdot 10^9, 3 \cdot ...
Input: 2 1 8 1 | Output: ? 1 2 ? 12 4 ! 4 ? 2000000000 1999999999 ! 1000000000
Hard
7
399
204
0
16
1,216
B
1216B
B. Shooting
900
greedy; implementation; sortings
Recently Vasya decided to improve his pistol shooting skills. Today his coach offered him the following exercise. He placed \(n\) cans in a row on a table. Cans are numbered from left to right from \(1\) to \(n\). Vasya has to knock down each can exactly once to finish the exercise. He is allowed to choose the order in...
The first line of the input contains one integer \(n\) \((2 \le n \le 1\,000)\) β€” the number of cans.The second line of the input contains the sequence \(a_1, a_2, \dots, a_n\) \((1 \le a_i \le 1\,000)\), where \(a_i\) is the durability of the \(i\)-th can.
In the first line print the minimum number of shots required to knock each of the \(n\) given cans down exactly once.In the second line print the sequence consisting of \(n\) distinct integers from \(1\) to \(n\) β€” the order of indices of cans that minimizes the number of shots required. If there are several answers, y...
In the first example Vasya can start shooting from the first can. He knocks it down with the first shot because he haven't knocked any other cans down before. After that he has to shoot the third can. To knock it down he shoots \(20 \cdot 1 + 1 = 21\) times. After that only second can remains. To knock it down Vasya sh...
Input: 3 20 10 20 | Output: 43 1 3 2
Beginner
3
862
257
345
12
1,858
A
1858A
A. Buttons
800
games; greedy; math
Anna and Katie ended up in a secret laboratory.There are \(a+b+c\) buttons in the laboratory. It turned out that \(a\) buttons can only be pressed by Anna, \(b\) buttons can only be pressed by Katie, and \(c\) buttons can be pressed by either of them. Anna and Katie decided to play a game, taking turns pressing these b...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.Each test case consists of three integers \(a\), \(b\), and \(c\) (\(1 \le a, b, c \le 10^9\)) β€” the number of buttons that can only be pressed by Anna, the number of buttons that can only be pressed by Katie, and the numbe...
For each test case, output First if Anna wins, or Second if Katie wins.
For the simplicity of the explanation, we will numerate the buttons by the numbers from \(1\) to \(a+b+c\): the first \(a\) buttons can only be pressed by Anna, the next \(b\) buttons can only be pressed by Katie, and the last \(c\) buttons can be pressed by either of them.In the first test case, Anna can press the \(3...
Input: 51 1 19 3 31 2 36 6 92 2 8 | Output: First First Second First Second
Beginner
3
559
385
71
18
803
E
803E
E. Roma and Poker
2,000
dp; graphs
Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser.Last evening Roma started to play poker. He decided to spend no more than k virtual bourles...
The first line contains two numbers n (the length of Roma's sequence) and k (1 ≀ n, k ≀ 1000).The second line contains the sequence s consisting of characters W, L, D and ?. There are exactly n characters in this sequence.
If there is no valid sequence that can be obtained from s by replacing all ? characters by W, L or D, print NO.Otherwise print this sequence. If there are multiple answers, print any of them.
Input: 3 2L?? | Output: LDL
Hard
2
1,353
222
191
8
592
C
592C
C. The Big Race
1,800
math
Vector Willman and Array Bolt are the two most famous athletes of Byteforces. They are going to compete in a race with a distance of L meters today. Willman and Bolt have exactly the same speed, so when they compete the result is always a tie. That is a problem for the organizers because they want a winner. While watch...
The first line of the input contains three integers t, w and b (1 ≀ t, w, b ≀ 5Β·1018) β€” the maximum possible length of the racetrack, the length of Willman's steps and the length of Bolt's steps respectively.
Print the answer to the problem as an irreducible fraction . Follow the format of the samples output.The fraction (p and q are integers, and both p β‰₯ 0 and q > 0 holds) is called irreducible, if there is no such integer d > 1, that both p and q are divisible by d.
In the first sample Willman and Bolt will tie in case 1, 6 or 7 are chosen as the length of the racetrack.
Input: 10 3 2 | Output: 3/10
Medium
1
1,345
208
264
5
1,585
E
1585E
E. Frequency Queries
2,400
data structures; dfs and similar; trees
Petya has a rooted tree with an integer written on each vertex. The vertex \(1\) is the root. You are to answer some questions about the tree.A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a node \(v\) is the next vertex on the shortest path from \(v\) to t...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 10^6\)). Description of the test cases follows.The first line of each test case contains two integers \(n\), \(q\) (\(1 \leq n, q \leq 10^6\)) β€” the number of vertices in the tree and the number of questions....
For each question of each test case print the answer to the question. In case of multiple answers, print any.
Input: 2 3 3 1 1 1 1 2 3 1 1 3 1 2 3 2 1 5 5 1 2 1 1 2 1 1 2 2 3 1 1 2 1 2 4 1 1 4 2 1 4 2 2 | Output: 1 -1 1 1 1 2 1 -1
Expert
3
1,455
897
109
15
1,209
E1
1209E1
E1. Rotate Columns (easy version)
2,000
bitmasks; brute force; dp; greedy; sortings
This is an easier version of the next problem. The difference is only in constraints.You are given a rectangular \(n \times m\) matrix \(a\). In one move you can choose any column and cyclically shift elements in this column. You can perform this operation as many times as you want (possibly zero). You can perform this...
The first line contains an integer \(t\) (\(1 \le t \le 40\)), the number of test cases in the input.The first line of each test case contains integers \(n\) and \(m\) (\(1 \le n \le 4\), \(1 \le m \le 100\)) β€” the number of rows and the number of columns in the given matrix \(a\). Each of the following \(n\) lines con...
Print \(t\) integers: answers for all test cases in the order they are given in the input.
In the first test case, you can shift the third column down by one, this way there will be \(r_1 = 5\) and \(r_2 = 7\).In the second case you can don't rotate anything at all, this way there will be \(r_1 = r_2 = 10\) and \(r_3 = 9\).
Input: 2 2 3 2 5 7 4 2 4 3 6 4 1 5 2 10 4 8 6 6 4 9 10 5 4 9 5 8 7 | Output: 12 29
Hard
5
562
394
90
12
1,906
C
1906C
C. Cursed Game
3,000
interactive
You found an antique box in your warehouse and decided to open it. The moment you open the box, it traps you into a cursed game where you are playing against a demon. The game consists of \(333\) rounds and you have to win all rounds in order to escape. The demon also gives you \(999\) coins that you can use throughout...
Sample Interaction #1The following interaction only shows \(2\) rounds. The actual interaction stays until you win all \(333\) rounds or you run out of coins. Explanation for the sample interaction #1For the first round, the following illustration shows how the demon finds the value in cell \((1, 1)\) of the result gri...
Master
1
2,114
0
0
19
681
E
681E
E. Runaway to a Shadow
2,500
geometry; sortings
Dima is living in a dormitory, as well as some cockroaches.At the moment 0 Dima saw a cockroach running on a table and decided to kill it. Dima needs exactly T seconds for aiming, and after that he will precisely strike the cockroach and finish it.To survive the cockroach has to run into a shadow, cast by round plates ...
In the first line of the input the four integers x0, y0, v, T (|x0|, |y0| ≀ 109, 0 ≀ v, T ≀ 109) are given β€” the cockroach initial position on the table in the Cartesian system at the moment 0, the cockroach's constant speed and the time in seconds Dima needs for aiming respectively.In the next line the only number n (...
Print the only real number p β€” the probability of that the cockroach will stay alive.Your answer will be considered correct if its absolute or relative error does not exceed 10 - 4.
The picture for the first sample is given below. Red color stands for points which being chosen as the cockroach's running direction will cause him being killed, green color for those standing for survival directions. Please note that despite containing a circle centered in ( - 2, 2) a part of zone is colored red becau...
Input: 0 0 1 131 1 1-1 -1 1-2 2 1 | Output: 0.50000000000
Expert
2
921
748
181
6
936
C
936C
C. Lock Puzzle
2,300
constructive algorithms; implementation; strings
Welcome to another task about breaking the code lock! Explorers Whitfield and Martin came across an unusual safe, inside of which, according to rumors, there are untold riches, among which one can find the solution of the problem of discrete logarithm!Of course, there is a code lock is installed on the safe. The lock h...
The first line contains an integer n, the length of the strings s and t (1 ≀ n ≀ 2 000).After that, there are two strings s and t, consisting of n lowercase Latin letters each.
If it is impossible to get string t from string s using no more than 6100 operations Β«shiftΒ», print a single number - 1.Otherwise, in the first line output the number of operations k (0 ≀ k ≀ 6100). In the next line output k numbers xi corresponding to the operations Β«shift xiΒ» (0 ≀ xi ≀ n) in the order in which they s...
Input: 6abacbbbabcba | Output: 46 3 2 3
Expert
3
1,241
176
337
9
793
D
793D
D. Presents in Bankopolis
2,100
dp; graphs; shortest paths
Bankopolis is an incredible city in which all the n crossroads are located on a straight line and numbered from 1 to n along it. On each crossroad there is a bank office.The crossroads are connected with m oriented bicycle lanes (the i-th lane goes from crossroad ui to crossroad vi), the difficulty of each of the lanes...
The first line contains two integers n and k (1 ≀ n, k ≀ 80) β€” the number of crossroads (and offices) and the number of offices Oleg wants to visit.The second line contains single integer m (0 ≀ m ≀ 2000) β€” the number of bicycle lanes in Bankopolis.The next m lines contain information about the lanes.The i-th of these ...
In the only line print the minimum possible total difficulty of the lanes in a valid path, or -1 if there are no valid paths.
In the first example Oleg visiting banks by path 1 β†’ 6 β†’ 2 β†’ 4.Path 1 β†’ 6 β†’ 2 β†’ 7 with smaller difficulity is incorrect because crossroad 2 β†’ 7 passes near already visited office on the crossroad 6.In the second example Oleg can visit banks by path 4 β†’ 1 β†’ 3.
Input: 7 441 6 26 2 22 4 22 7 1 | Output: 6
Hard
3
1,175
467
125
7
1,082
E
1082E
E. Increasing Frequency
2,000
binary search; dp; greedy
You are given array \(a\) of length \(n\). You can choose one segment \([l, r]\) (\(1 \le l \le r \le n\)) and integer value \(k\) (positive, negative or even zero) and change \(a_l, a_{l + 1}, \dots, a_r\) by \(k\) each (i.e. \(a_i := a_i + k\) for each \(l \le i \le r\)).What is the maximum possible number of element...
The first line contains two integers \(n\) and \(c\) (\(1 \le n \le 5 \cdot 10^5\), \(1 \le c \le 5 \cdot 10^5\)) β€” the length of array and the value \(c\) to obtain.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 5 \cdot 10^5\)) β€” array \(a\).
Print one integer β€” the maximum possible number of elements with value \(c\) which can be obtained after performing operation described above.
In the first example we can choose any segment and \(k = 0\). The array will stay same.In the second example we can choose segment \([1, 3]\) and \(k = -4\). The array will become \([2, -2, 2]\).
Input: 6 9 9 9 9 9 9 9 | Output: 6
Hard
3
385
278
142
10
45
H
45H
H. Road Problem
2,100
graphs
The Berland capital (as you very well know) contains n junctions, some pairs of which are connected by two-way roads. Unfortunately, the number of traffic jams in the capital has increased dramatically, that's why it was decided to build several new roads. Every road should connect two junctions. The city administratio...
The first input line contains a pair of integers n, m (2 ≀ n ≀ 900, 1 ≀ m ≀ 100000), where n is the number of junctions and m is the number of roads. Each of the following m lines contains a description of a road that is given by the numbers of the connected junctions ai, bi (1 ≀ ai, bi ≀ n, ai β‰  bi). The junctions are...
On the first line print t β€” the number of added roads. Then on t lines print the descriptions of the added roads in the format of the input data. You can use any order of printing the roads themselves as well as the junctions linked by every road. If there are several solutions to that problem, print any of them.If the...
Input: 4 31 22 33 4 | Output: 11 4
Hard
1
767
430
431
0
32
B
32B
B. Borze
800
expression parsing; implementation
Ternary numeric notation is quite popular in Berland. To telegraph the ternary number the Borze alphabet is used. Digit 0 is transmitted as Β«.Β», 1 as Β«-.Β» and 2 as Β«--Β». You are to decode the Borze code, i.e. to find out the ternary number given its representation in Borze alphabet.
The first line contains a number in Borze code. The length of the string is between 1 and 200 characters. It's guaranteed that the given string is a valid Borze code of some ternary number (this number can have leading zeroes).
Output the decoded ternary number. It can have leading zeroes.
Input: .-.-- | Output: 012
Beginner
2
283
227
62
0
1,056
G
1056G
G. Take Metro
2,900
brute force; data structures; graphs
Having problems with tram routes in the morning, Arkady decided to return home by metro. Fortunately for Arkady, there is only one metro line in the city.Unfortunately for Arkady, the line is circular. It means that the stations are enumerated from \(1\) to \(n\) and there is a tunnel between any pair of consecutive st...
The first line contains two integers \(n\) and \(m\) (\(3 \le n \le 10^5\), \(1 \le m < n\)) β€” the total number of stations and the number of stations that have red interior.The second line contains two integers \(s\) and \(t\) (\(1 \le s \le n\), \(1 \le t \le 10^{12}\)) β€” the starting station and the initial value of...
Output the only integer β€” the station where Arkady will exit the metro.
Consider the first example. There are \(10\) stations and the first \(4\) of them are red. Arkady starts at station \(3\) with value \(t = 1\), so just rides \(1\) station in clockwise direction and ends up on the station \(4\).In the second example the metro is same as in the first example, but Arkady starts at statio...
Input: 10 4 3 1 | Output: 4
Master
3
1,401
327
71
10
161
A
161A
A. Dress'em in Vests!
1,300
binary search; brute force; greedy; two pointers
The Two-dimensional kingdom is going through hard times... This morning the Three-Dimensional kingdom declared war on the Two-dimensional one. This (possibly armed) conflict will determine the ultimate owner of the straight line.The Two-dimensional kingdom has a regular army of n people. Each soldier registered himself...
The first input line contains four integers n, m, x and y (1 ≀ n, m ≀ 105, 0 ≀ x, y ≀ 109) β€” the number of soldiers, the number of vests and two numbers that specify the soldiers' unpretentiousness, correspondingly.The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109) in non-decreasing order, separated by ...
In the first line print a single integer k β€” the maximum number of soldiers equipped with bulletproof vests. In the next k lines print k pairs, one pair per line, as ""ui vi"" (without the quotes). Pair (ui, vi) means that soldier number ui must wear vest number vi. Soldiers and vests are numbered starting from one in ...
In the first sample you need the vests' sizes to match perfectly: the first soldier gets the first vest (size 1), the third soldier gets the second vest (size 3). This sample allows another answer, which gives the second vest to the fourth soldier instead of the third one.In the second sample the vest size can differ f...
Input: 5 3 0 01 2 3 3 41 3 5 | Output: 21 13 2
Easy
4
897
517
621
1
1,968
E
1968E
E. Cells Arrangement
1,600
constructive algorithms
You are given an integer \(n\). You choose \(n\) cells \((x_1,y_1), (x_2,y_2),\dots,(x_n,y_n)\) in the grid \(n\times n\) where \(1\le x_i\le n\) and \(1\le y_i\le n\).Let \(\mathcal{H}\) be the set of distinct Manhattan distances between any pair of cells. Your task is to maximize the size of \(\mathcal{H}\). Examples...
The first line contains a single integer \(t\) (\(1\le t\le 50\)) β€” the number of test cases.Each of the following \(t\) lines contains a single integer \(n\) (\(2\le n\le 10^3\)).
For each test case, output \(n\) points which maximize the size of \(\mathcal{H}\). It is not necessary to output an empty line at the end of the answer for each test case.
In the first testcase we have \(n=2\). One of the possible arrangements is: The arrangement with cells located in \((1,1)\) and \((1,2)\). In this case \(\mathcal{H}=\{|1-1|+|1-1|,|1-1|+|2-2|,|1-1|+|1-2|\}=\{0,0,1\}=\{0,1\}\). Hence, the size of \(\mathcal{H}\) is \(2\). It can be shown that it is the greatest possible...
Input: 523456 | Output: 1 1 1 2 2 1 2 3 3 1 1 1 1 3 4 3 4 4 1 1 1 3 1 4 2 1 5 5 1 4 1 5 1 6 5 2 5 5 6 1
Medium
1
541
180
172
19
1,315
A
1315A
A. Dead Pixel
800
implementation
Screen resolution of Polycarp's monitor is \(a \times b\) pixels. Unfortunately, there is one dead pixel at his screen. It has coordinates \((x, y)\) (\(0 \le x < a, 0 \le y < b\)). You can consider columns of pixels to be numbered from \(0\) to \(a-1\), and rows β€” from \(0\) to \(b-1\).Polycarp wants to open a rectang...
In the first line you are given an integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases in the test. In the next lines you are given descriptions of \(t\) test cases.Each test case contains a single line which consists of \(4\) integers \(a, b, x\) and \(y\) (\(1 \le a, b \le 10^4\); \(0 \le x < a\); \(0 \le...
Print \(t\) integers β€” the answers for each test case. Each answer should contain an integer equal to the maximal possible area (in pixels) of a rectangular window, that doesn't contain the dead pixel.
In the first test case, the screen resolution is \(8 \times 8\), and the upper left pixel is a dead pixel. Here you can see one of two possible layouts of the maximal window.
Input: 6 8 8 0 0 1 10 0 3 17 31 10 4 2 1 0 0 5 10 3 9 10 10 4 8 | Output: 56 6 442 1 45 80
Beginner
1
559
461
201
13
1,710
A
1710A
A. Color the Picture
1,500
constructive algorithms; greedy; math
A picture can be represented as an \(n\times m\) grid (\(n\) rows and \(m\) columns) so that each of the \(n \cdot m\) cells is colored with one color. You have \(k\) pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most \(a_i\) cells with the \(i\)-th pigment.A p...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 10^4\)). The description of the test cases follows.The first line of each test case contains three integers \(n\), \(m\), and \(k\) (\(3 \leq n,m \leq 10^9\), \(1 \leq k \leq 10^5\)) β€” the number of rows and ...
For each test case, print ""Yes"" (without quotes) if it is possible to color a beautiful picture. Otherwise, print ""No"" (without quotes).
In the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment \(1\).
Input: 64 6 312 9 83 3 28 83 3 29 54 5 210 115 4 29 1110 10 311 45 14 | Output: Yes No Yes Yes No No
Medium
3
1,309
629
140
17
1,208
E
1208E
E. Let Them Slide
2,200
data structures; implementation
You are given \(n\) arrays that can have different sizes. You also have a table with \(w\) columns and \(n\) rows. The \(i\)-th array is placed horizontally in the \(i\)-th row. You can slide each array within its row as long as it occupies several consecutive cells and lies completely inside the table.You need to find...
The first line contains two integers \(n\) (\(1 \le n \le 10^{6}\)) and \(w\) (\(1 \le w \le 10^{6}\)) β€” the number of arrays and the width of the table.Each of the next \(n\) lines consists of an integer \(l_{i}\) (\(1 \le l_{i} \le w\)), the length of the \(i\)-th array, followed by \(l_{i}\) integers \(a_{i1}, a_{i2...
Print \(w\) integers, the \(i\)-th of them should be the maximum sum for column \(i\).
Illustration for the first example is in the statement.
Input: 3 3 3 2 4 8 2 2 5 2 6 3 | Output: 10 15 16
Hard
2
661
463
86
12
1,278
A
1278A
A. Shuffle Hashing
1,000
brute force; implementation; strings
Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems.Polycarp decided to store the hash of the password, generated by the following algorithm: take the password \(p\), consisting of lowercase Latin letters, and shuffle the lette...
The first line contains one integer \(t\) (\(1 \le t \le 100\)) β€” the number of test cases.The first line of each test case contains a non-empty string \(p\), consisting of lowercase Latin letters. The length of \(p\) does not exceed \(100\).The second line of each test case contains a non-empty string \(h\), consistin...
For each test case print the answer to it β€” ""YES"" if the given hash \(h\) could be obtained from the given password \(p\) or ""NO"" otherwise.
The first test case is explained in the statement.In the second test case both \(s_1\) and \(s_2\) are empty and \(p'=\) ""threetwoone"" is \(p\) shuffled.In the third test case the hash could not be obtained from the password.In the fourth test case \(s_1=\) ""n"", \(s_2\) is empty and \(p'=\) ""one"" is \(p\) shuffle...
Input: 5 abacaba zyxaabcaabkjh onetwothree threetwoone one zzonneyy one none twenty ten | Output: YES YES NO YES NO
Beginner
3
1,115
394
144
12
1,474
F
1474F
F. 1 2 3 4 ...
3,000
dp; math; matrices
Igor had a sequence \(d_1, d_2, \dots, d_n\) of integers. When Igor entered the classroom there was an integer \(x\) written on the blackboard.Igor generated sequence \(p\) using the following algorithm: initially, \(p = [x]\); for each \(1 \leq i \leq n\) he did the following operation \(|d_i|\) times: if \(d_i \geq 0...
The first line contains a single integer \(n\) (\(1 \leq n \leq 50\)) β€” the length of the sequence \(d\).The second line contains a single integer \(x\) (\(-10^9 \leq x \leq 10^9\)) β€” the integer on the blackboard.The third line contains \(n\) integers \(d_1, d_2, \ldots, d_n\) (\(-10^9 \leq d_i \leq 10^9\)).
Print two integers: the first integer should be equal to the length of the longest increasing subsequence of \(p\); the second should be equal to the number of them modulo \(998244353\). You should print only the second number modulo \(998244353\).
The first test case was explained in the statement.In the second test case \(p = [100, 101, 102, 103, 104, 105, 104, 103, 102, 103, 104, 105, 106, 107, 108]\).In the third test case \(p = [1, 2, \ldots, 2000000000]\).
Input: 3 3 1 -1 2 | Output: 3 3
Master
3
1,315
310
248
14
1,176
B
1176B
B. Merge it!
1,100
math
You are given an array \(a\) consisting of \(n\) integers \(a_1, a_2, \dots , a_n\).In one operation you can choose two elements of the array and replace them with the element equal to their sum (it does not matter where you insert the new element). For example, from the array \([2, 1, 4]\) you can obtain the following...
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β€” the number of queries.The first line of each query contains one integer \(n\) (\(1 \le n \le 100\)).The second line of each query contains \(n\) integers \(a_1, a_2, \dots , a_n\) (\(1 \le a_i \le 10^9\)).
For each query print one integer in a single line β€” the maximum possible number of elements divisible by \(3\) that are in the array after performing described operation an arbitrary (possibly, zero) number of times.
In the first query of the example you can apply the following sequence of operations to obtain \(3\) elements divisible by \(3\): \([3, 1, 2, 3, 1] \rightarrow [3, 3, 3, 1]\).In the second query you can obtain \(3\) elements divisible by \(3\) with the following sequence of operations: \([1, 1, 1, 1, 1, 2, 2] \rightarr...
Input: 2 5 3 1 2 3 1 7 1 1 1 1 1 2 2 | Output: 3 3
Easy
1
592
272
216
11
792
D
792D
D. Paths in a Complete Binary Tree
1,900
bitmasks; trees
T is a complete binary tree consisting of n vertices. It means that exactly one vertex is a root, and each vertex is either a leaf (and doesn't have children) or an inner node (and has exactly two children). All leaves of a complete binary tree have the same depth (distance from the root). So n is a number such that n ...
The first line contains two integer numbers n and q (1 ≀ n ≀ 1018, q β‰₯ 1). n is such that n + 1 is a power of 2.The next 2q lines represent queries; each query consists of two consecutive lines. The first of these two lines contains ui (1 ≀ ui ≀ n), the second contains non-empty string si. si doesn't contain any charac...
Print q numbers, i-th number must be the answer to the i-th query.
Input: 15 24UURL8LRLLLLLLLL | Output: 105
Hard
2
1,693
452
66
7
1,628
D1
1628D1
D1. Game on Sum (Easy Version)
2,100
combinatorics; dp; games; math
This is the easy version of the problem. The difference is the constraints on \(n\), \(m\) and \(t\). You can make hacks only if all versions of the problem are solved.Alice and Bob are given the numbers \(n\), \(m\) and \(k\), and play a game as follows:The game has a score that Alice tries to maximize, and Bob tries ...
The first line of the input contains a single integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases. The description of test cases follows.Each test case consists of a single line containing the three integers, \(n\), \(m\), and \(k\) (\(1 \le m \le n \le 2000, 0 \le k < 10^9 + 7\)) β€” the number of turns, how...
For each test case output a single integer number β€” the score of the optimal game modulo \(10^9 + 7\).Formally, let \(M = 10^9 + 7\). It can be shown that the answer can be expressed as an irreducible fraction \(\frac{p}{q}\), where \(p\) and \(q\) are integers and \(q \not \equiv 0 \pmod{M}\). Output the integer equal...
In the first test case, the entire game has \(3\) turns, and since \(m = 3\), Bob has to add in each of them. Therefore Alice should pick the biggest number she can, which is \(k = 2\), every turn.In the third test case, Alice has a strategy to guarantee a score of \(\frac{75}{8} \equiv 375000012 \pmod{10^9 + 7}\).In t...
Input: 73 3 22 1 106 3 106 4 10100 1 14 4 069 4 20 | Output: 6 5 375000012 500000026 958557139 0 49735962
Hard
4
1,002
495
454
16
418
D
418D
D. Big Problems for Organizers
2,800
data structures; graphs; trees
The Finals of the ""Russian Code Cup"" 2214 will be held in n hotels. Two hotels (let's assume that they are the main hotels), will host all sorts of events, and the remaining hotels will accommodate the participants. The hotels are connected by n - 1 roads, you can get from any hotel to any other one.The organizers wo...
The first line contains an integer n (2 ≀ n ≀ 100000) β€” the number of hotels. The next n - 1 lines contain two integers each β€” the numbers of the hotels that have a road between them. Consider hotels are numbered from 1 to n.The next line contains an integer m (1 ≀ m ≀ 100000) β€” the number of queries. The following m l...
For each request of the organizers print a single integer β€” the time that all participants need to reach the main hotels.
Input: 32 33 132 12 33 1 | Output: 111
Master
3
671
414
121
4
1,805
D
1805D
D. A Wide, Wide Graph
1,800
dfs and similar; dp; graphs; greedy; trees
You are given a tree (a connected graph without cycles) with \(n\) vertices. Consider a fixed integer \(k\). Then, the graph \(G_k\) is an undirected graph with \(n\) vertices, where an edge between vertices \(u\) and \(v\) exists if and only if the distance between vertices \(u\) and \(v\) in the given tree is at leas...
The first line contains the integer \(n\) (\(2 \le n \le 10^5\)) β€” the number of vertices in the graph.Each of the next \(n-1\) lines contains two integers \(u\) and \(v\) (\(1 \le u, v \le n\)), denoting an edge between vertices \(u\) and \(v\) in the tree. It is guaranteed that these edges form a valid tree.
Output \(n\) integers: the number of connected components in the graph \(G_k\) for each \(k\) from \(1\) to \(n\).
In the first example: If \(k=1\), the graph has an edge between each pair of vertices, so it has one component. If \(k=4\), the graph has only edges \(4 \leftrightarrow 6\) and \(5 \leftrightarrow 6\), so the graph has \(4\) components.In the second example: when \(k=1\) or \(k=2\) the graph has one component. When \(k...
Input: 6 1 2 1 3 2 4 2 5 3 6 | Output: 1 1 2 4 6 6
Medium
5
426
311
114
18
446
A
446A
A. DZY Loves Sequences
1,600
dp; implementation; two pointers
DZY has a sequence a, consisting of n integers.We'll call a sequence ai, ai + 1, ..., aj (1 ≀ i ≀ j ≀ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number t...
The first line contains integer n (1 ≀ n ≀ 105). The next line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109).
In a single line print the answer to the problem β€” the maximum length of the required subsegment.
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4.
Input: 67 2 3 1 5 6 | Output: 5
Medium
3
469
114
97
4
64
B
64B
B. Expression
1,500
*special; expression parsing
You are given a line Β«n#mΒ», where Β«nΒ» and Β«mΒ» are digits, and Β«#Β» is a sign Β«+Β» or Β«-Β». Print the value of the given expression.
The only given line has a string in form Β«n#mΒ», where Β«nΒ» and Β«mΒ» are digits (from 0 to 9), and Β«#Β» is a sign Β«+Β» or Β«-Β».
Print the value of the given expression.
Input: 1-5 | Output: -4
Medium
2
128
121
40
0
1,913
B
1913B
B. Swap and Delete
1,000
strings
You are given a binary string \(s\) (a string consisting only of 0-s and 1-s).You can perform two types of operations on \(s\): delete one character from \(s\). This operation costs \(1\) coin; swap any pair of characters in \(s\). This operation is free (costs \(0\) coins). You can perform these operations any number ...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. Then \(t\) test cases follow.The only line of each test case contains a binary string \(s\) (\(1 \le |s| \le 2 \cdot 10^5\); \(s_i \in \{\)0, 1\(\}\)) β€” the initial string, consisting of characters 0 and/or 1.Additional co...
For each test case, print one integer β€” the minimum total cost to make string \(t\) good.
In the first test case, you have to delete a character from \(s\) to get the empty string \(t\). Only then \(t\) becomes good. One deletion costs \(1\) coin.In the second test case, you can, for example, delete the second character from \(s\) to get the string 01, and then swap the first and second characters to get th...
Input: 400110101110001111100 | Output: 1 1 0 4
Beginner
1
727
413
89
19
2,050
D
2050D
D. Digital string maximization
1,300
brute force; greedy; math; strings
You are given a string \(s\), consisting of digits from \(0\) to \(9\). In one operation, you can pick any digit in this string, except for \(0\) or the leftmost digit, decrease it by \(1\), and then swap it with the digit left to the picked.For example, in one operation from the string \(1023\), you can get \(1103\) o...
The first line of the input consists of an integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.Each test case consists of a single line consisting of a digital string \(s\) (\(1 \le |s| \le 2\cdot 10^5\)), where \(|s|\) denotes the length of \(s\). The string does not contain leading zeroes.It is guaranteed...
For each test case, print the answer on a separate line.
In the first example, the following sequence of operations is suitable: \(19 \rightarrow 81\).In the second example, the following sequence of operations is suitable: \(1709 \rightarrow 1780 \rightarrow 6180 \rightarrow 6710\).In the fourth example, the following sequence of operations is suitable: \(51476 \rightarrow ...
Input: 6191709115555147698765432105891917899 | Output: 81 6710 33311 55431 9876543210 7875567711
Easy
4
419
394
56
20
1,357
E1
1357E1
E1. Power of quantum Fourier transform
0
*special
Implement an operation that is equivalent to the operation QFT\(^P\), where QFT is the quantum Fourier transform.Your operation should take the following inputs: an integer \(P\) (\(2 \le P \le 2.1\cdot 10^6\)). a register of type LittleEndian - a wrapper type for an array of qubits that encodes an unsigned integer in ...
Beginner
1
1,212
0
0
13
2,039
E
2039E
E. Shohag Loves Inversions
2,200
combinatorics; dp; implementation; math
Shohag has an array \(a\) of integers. Initially \(a = [0, 1]\). He can repeatedly perform the following operation any number of times: Let \(k\) be the number of inversions\(^{\text{βˆ—}}\) in the current array \(a\). Insert \(k\) at any position in \(a\), including the beginning or the end. For example, if \(a = [4, 6,...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first and only line of each test case contains an integer \(n\) (\(2 \le n \le 10^6\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^6\).
For each test case, output an integer β€” the number of possible arrays modulo \(998\,244\,353\).
In the first test case, the following \(5\) arrays can be obtained (the inserted inversion count is shown in bold): \([0, 1] \rightarrow [0, \textbf{0}, 1] \rightarrow [0, 0, 1, \textbf{0}]\), \([0, 1] \rightarrow [0, \textbf{0}, 1] \rightarrow [0, 0, \textbf{0}, 1]\), \([0, 1] \rightarrow [0, 1, \textbf{0}] \rightarro...
Input: 442769 | Output: 5 1 682 325188814
Hard
4
903
272
95
20
358
B
358B
B. Dima and Text Messages
1,500
brute force; strings
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other.Dima and Inna are using a secret code in their text messages. Wh...
The first line contains integer n (1 ≀ n ≀ 105) β€” the number of words in Dima's message. Next n lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The number...
In a single line, print ""yes"" (without the quotes), if Dima decoded the text message correctly, and ""no"" (without the quotes) otherwise.
Please note that Dima got a good old kick in the pants for the second sample from the statement.
Input: 3iloveyou<3i<3love<23you<3 | Output: yes
Medium
2
1,088
461
140
3
1,743
A
1743A
A. Password
800
brute force; combinatorics; implementation; math
Monocarp has forgotten the password to his mobile phone. The password consists of \(4\) digits from \(0\) to \(9\) (note that it can start with the digit \(0\)).Monocarp remembers that his password had exactly two different digits, and each of these digits appeared exactly two times in the password. Monocarp also remem...
The first line contains a single integer \(t\) (\(1 \le t \le 200\)) β€” the number of testcases.The first line of each testcase contains a single integer \(n\) (\(1 \le n \le 8\)) β€” the number of digits for which Monocarp remembers that they were not used in the password.The second line contains \(n\) different integers...
For each testcase, print one integer β€” the number of different \(4\)-digit sequences that meet the constraints.
In the first example, all possible passwords are: ""3377"", ""3737"", ""3773"", ""7337"", ""7373"", ""7733"".
Input: 280 1 2 4 5 6 8 918 | Output: 6 216
Beginner
4
586
502
111
17
72
G
72G
G. Fibonacci army
1,000
*special; dp
King Cambyses loves Fibonacci numbers. He has several armies. Today he wants to make a new army for himself and he wants the number of men in this army to be the n-th Fibonacci number.Given n you should find n-th Fibonacci number. The set of Fibonacci numbers start with f0 = f1 = 1 and for each i β‰₯ 2, fi = fi - 1 + fi ...
Input contains a single integer n (1 ≀ n ≀ 20).
Write a single integer. The n-th Fibonacci number.
Input: 2 | Output: 2
Beginner
2
324
47
50
0
1,330
B
1330B
B. Dreamoon Likes Permutations
1,400
implementation; math
The sequence of \(m\) integers is called the permutation if it contains all integers from \(1\) to \(m\) exactly once. The number \(m\) is called the length of the permutation.Dreamoon has two permutations \(p_1\) and \(p_2\) of non-zero lengths \(l_1\) and \(l_2\).Now Dreamoon concatenates these two permutations into ...
The first line contains an integer \(t\) (\(1 \le t \le 10\,000\)) denoting the number of test cases in the input.Each test case contains two lines. The first line contains one integer \(n\) (\(2 \leq n \leq 200\,000\)): the length of \(a\). The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_...
For each test case, the first line of output should contain one integer \(k\): the number of ways to divide \(a\) into permutations \(p_1\) and \(p_2\).Each of the next \(k\) lines should contain two integers \(l_1\) and \(l_2\) (\(1 \leq l_1, l_2 \leq n, l_1 + l_2 = n\)), denoting, that it is possible to divide \(a\) ...
In the first example, two possible ways to divide \(a\) into permutations are \(\{1\} + \{4, 3, 2, 1\}\) and \(\{1,4,3,2\} + \{1\}\).In the second example, the only way to divide \(a\) into permutations is \(\{2,4,1,3\} + \{2,1\}\).In the third example, there are no possible ways.
Input: 6 5 1 4 3 2 1 6 2 4 1 3 2 1 4 2 1 1 3 4 1 3 3 1 12 2 1 3 4 5 6 7 8 9 1 10 2 3 1 1 1 | Output: 2 1 4 4 1 1 4 2 0 0 1 2 10 0
Easy
2
723
383
510
13
822
D
822D
D. My pretty girl Noora
1,800
brute force; dp; greedy; math; number theory
In Pavlopolis University where Noora studies it was decided to hold beauty contest ""Miss Pavlopolis University"". Let's describe the process of choosing the most beautiful girl in the university in more detail.The contest is held in several stages. Suppose that exactly n girls participate in the competition initially....
The first and single line contains three integers t, l and r (1 ≀ t < 109 + 7, 2 ≀ l ≀ r ≀ 5Β·106).
In the first line print single integer β€” the value of the expression modulo 109 + 7.
Consider the sample.It is necessary to find the value of .f(2) = 1. From two girls you can form only one group of two people, in which there will be one comparison.f(3) = 3. From three girls you can form only one group of three people, in which there will be three comparisons.f(4) = 3. From four girls you can form two ...
Input: 2 2 4 | Output: 19
Medium
5
1,911
98
84
8
526
C
526C
C. Om Nom and Candies
2,000
brute force; greedy; math
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place? One day, when he came to his friend Evan, Om Nom didn't find him at home but he...
The single line contains five integers C, Hr, Hb, Wr, Wb (1 ≀ C, Hr, Hb, Wr, Wb ≀ 109).
Print a single integer β€” the maximum number of joy units that Om Nom can get.
In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.
Input: 10 3 5 2 3 | Output: 16
Hard
3
1,142
87
77
5
1,693
E
1693E
E. Outermost Maximums
3,400
data structures; greedy
Yeri has an array of \(n + 2\) non-negative integers : \(a_0, a_1, ..., a_n, a_{n + 1}\).We know that \(a_0 = a_{n + 1} = 0\).She wants to make all the elements of \(a\) equal to zero in the minimum number of operations.In one operation she can do one of the following: Choose the leftmost maximum element and change it ...
The first line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le n\)).
Print a single integer β€” the minimum number of operations needed to make all elements of \(a\) equal to zero.
In the first sample, you get \(\langle 1, \underline{1}, 2, 4, 0, 2 \rangle\) by performing the first operation and \(\langle 1, 4, 2, \underline{2}, 0, 2 \rangle\) by performing the second operation.One way to achieve our goal is shown below. (The underlines show the last change.) \(\langle 1, 4, 2, 4, 0, 2 \rangle \t...
Input: 6 1 4 2 4 0 2 | Output: 7
Master
2
557
166
109
16
650
A
650A
A. Watchmen
1,400
data structures; geometry; math
Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn them as soon as possible. There are n watchmen on a plane, the i-th watchman is located at point (xi, yi).They need to arrange a plan, but there are some difficulties on their way. As you know, Doctor Manhattan considers ...
The first line of the input contains the single integer n (1 ≀ n ≀ 200 000) β€” the number of watchmen.Each of the following n lines contains two integers xi and yi (|xi|, |yi| ≀ 109).Some positions may coincide.
Print the number of pairs of watchmen such that the distance between them calculated by Doctor Manhattan is equal to the distance calculated by Daniel.
In the first sample, the distance between watchman 1 and watchman 2 is equal to |1 - 7| + |1 - 5| = 10 for Doctor Manhattan and for Daniel. For pairs (1, 1), (1, 5) and (7, 5), (1, 5) Doctor Manhattan and Daniel will calculate the same distances.
Input: 31 17 51 5 | Output: 2
Easy
3
743
210
151
6
1,657
B
1657B
B. XY Sequence
800
greedy
You are given four integers \(n\), \(B\), \(x\) and \(y\). You should build a sequence \(a_0, a_1, a_2, \dots, a_n\) where \(a_0 = 0\) and for each \(i \ge 1\) you can choose: either \(a_i = a_{i - 1} + x\) or \(a_i = a_{i - 1} - y\). Your goal is to build such a sequence \(a\) that \(a_i \le B\) for all \(i\) and \(\s...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. Next \(t\) cases follow.The first and only line of each test case contains four integers \(n\), \(B\), \(x\) and \(y\) (\(1 \le n \le 2 \cdot 10^5\); \(1 \le B, x, y \le 10^9\)).It's guaranteed that the total sum of \(n\) ...
For each test case, print one integer β€” the maximum possible \(\sum\limits_{i=0}^{n}{a_i}\).
In the first test case, the optimal sequence \(a\) is \([0, 1, 2, 3, 4, 5]\).In the second test case, the optimal sequence \(a\) is \([0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]\).In the third test case, the optimal sequence \(a\) is \([0, -3, -6, 1, -2]\).
Input: 35 100 1 307 1000000000 1000000000 10000000004 1 7 3 | Output: 15 4000000000 -10
Beginner
1
367
352
92
16
1,781
D
1781D
D. Many Perfect Squares
1,800
brute force; math; number theory
You are given a set \(a_1, a_2, \ldots, a_n\) of distinct positive integers.We define the squareness of an integer \(x\) as the number of perfect squares among the numbers \(a_1 + x, a_2 + x, \ldots, a_n + x\).Find the maximum squareness among all integers \(x\) between \(0\) and \(10^{18}\), inclusive.Perfect squares ...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 50\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 50\)) β€” the size of the set.The second line contains \(n\) distinct integers \(a_...
For each test case, print a single integer β€” the largest possible number of perfect squares among \(a_1 + x, a_2 + x, \ldots, a_n + x\), for some \(0 \le x \le 10^{18}\).
In the first test case, for \(x = 0\) the set contains two perfect squares: \(1\) and \(4\). It is impossible to obtain more than two perfect squares.In the second test case, for \(x = 3\) the set looks like \(4, 9, 16, 25, 100\), that is, all its elements are perfect squares.
Input: 451 2 3 4 551 6 13 22 97110052 5 10 17 26 | Output: 2 5 1 2
Medium
3
453
507
170
17
1,340
A
1340A
A. Nastya and Strange Generator
1,500
brute force; data structures; greedy; implementation
Denis was very sad after Nastya rejected him. So he decided to walk through the gateways to have some fun. And luck smiled at him! When he entered the first courtyard, he met a strange man who was selling something. Denis bought a mysterious item and it was... Random permutation generator! Denis could not believed his ...
The first line contains a single integer \(t\) \((1 \leq t \leq 10^5)\) β€” the number of test cases. Then the descriptions of the test cases follow.The first line of the test case contains a single integer \(n\) \((1 \leq n \leq 10^5)\) β€” the size of the permutation.The second line of the test case contains \(n\) differ...
Print ""Yes"" if this permutation could be obtained as a result of the generator. Otherwise, print ""No"".All letters can be displayed in any case.
Let's simulate the operation of the generator in the first test.At the \(1\) step, \(r = [1, 2, 3, 4, 5], count = [1, 1, 1, 1, 1]\). The maximum value is reached in any free position, so the generator can choose a random position from \(1\) to \(5\). In our example, it chose \(5\).At the \(2\) step, \(r = [1, 2, 3, 4, ...
Input: 5 5 2 3 4 5 1 1 1 3 1 3 2 4 4 2 3 1 5 1 5 2 4 3 | Output: Yes Yes No Yes No
Medium
4
2,614
501
147
13
555
D
555D
D. Case of a Top Secret
2,500
binary search; implementation; math
Andrewid the Android is a galaxy-famous detective. Now he is busy with a top secret case, the details of which are not subject to disclosure.However, he needs help conducting one of the investigative experiment. There are n pegs put on a plane, they are numbered from 1 to n, the coordinates of the i-th of them are (xi,...
The first line contains integers n and m (1 ≀ n, m ≀ 2Β·105) β€” the number of pegs and queries.The next line contains n integers x1, x2, ..., xn ( - 109 ≀ xi ≀ 109) β€” the coordinates of the pegs. It is guaranteed that the coordinates of all the pegs are distinct integers.Next m lines contain the descriptions of the queri...
Print m lines, the i-th line should contain the number of the peg around which the weight will eventually rotate after the i-th push.
Picture to the first sample test: Picture to the second sample test:Note that in the last query weight starts to rotate around the peg 1 attached to a rope segment of length 0.
Input: 3 20 3 52 31 8 | Output: 32
Expert
3
1,572
474
133
5
8
B
8B
B. Obsession with Robots
1,400
constructive algorithms; graphs; implementation
The whole world got obsessed with robots,and to keep pace with the progress, great Berland's programmer Draude decided to build his own robot. He was working hard at the robot. He taught it to walk the shortest path from one point to another, to record all its movements, but like in many Draude's programs, there was a ...
The first line of the input file contains the recording of the robot's movements. This recording is a non-empty string, consisting of uppercase Latin letters L, R, U and D, standing for movements left, right, up and down respectively. The length of the string does not exceed 100.
In the first line output the only word OK (if the above described map exists), or BUG (if such a map does not exist).
Input: LLUUUR | Output: OK
Easy
3
1,573
280
117
0
1,657
F
1657F
F. Words on Tree
2,600
2-sat; dfs and similar; dsu; graphs; trees
You are given a tree consisting of \(n\) vertices, and \(q\) triples \((x_i, y_i, s_i)\), where \(x_i\) and \(y_i\) are integers from \(1\) to \(n\), and \(s_i\) is a string with length equal to the number of vertices on the simple path from \(x_i\) to \(y_i\).You want to write a lowercase Latin letter on each vertex i...
The first line contains two integers \(n\) and \(q\) (\(2 \le n \le 4 \cdot 10^5\); \(1 \le q \le 4 \cdot 10^5\)) β€” the number of vertices in the tree and the number of triples, respectively.Then \(n - 1\) lines follow; the \(i\)-th of them contains two integers \(u_i\) and \(v_i\) (\(1 \le u_i, v_i \le n\); \(u_i \ne ...
If there is no way to meet the conditions on all triples, print NO. Otherwise, print YES in the first line, and a string of \(n\) lowercase Latin letters in the second line; the \(i\)-th character of the string should be the letter you write on the \(i\)-th vertex. If there are multiple answers, print any of them.
Input: 3 2 2 3 2 1 2 1 ab 2 3 bc | Output: YES abc
Expert
5
844
728
315
16
1,866
J
1866J
J. Jackets and Packets
2,800
dp
Pak Chanek has \(N\) jackets that are stored in a wardrobe. Pak Chanek's wardrobe has enough room for two stacks of jackets, namely the left stack and the right stack. Initially, all \(N\) jackets are in the left stack, while the right stack is empty. Initially, the \(i\)-th jacket from the top of the left stack has co...
The first line contains three integers \(N\), \(X\), and \(Y\) (\(1 \leq N \leq 400\); \(1\leq X,Y\leq10^9\)) β€” the number of jackets, the time to do a packing operation, and the time to do a movement operation.The second line contains \(N\) integers \(C_1, C_2, C_3, \ldots, C_N\) (\(1 \leq C_i \leq N\)) β€” the colour o...
An integer representing the minimum time to pack all jackets.
Let's denote the contents of the two stacks using arrays that represent the colours of the jackets in the stack from top to bottom. Initially, the two stacks form \([4, 4, 2, 4, 1, 2, 2, 1]\) and \([]\).Pak Chanek can do the following sequence of operations: Movement from left to right. The two stacks become \([4, 2, 4...
Input: 8 7 2 4 4 2 4 1 2 2 1 | Output: 38
Master
1
1,066
334
61
18
1,536
F
1536F
F. Omkar and Akmar
2,600
chinese remainder theorem; combinatorics; constructive algorithms; fft; games; geometry; math; meet-in-the-middle; string suffix structures
Omkar and Akmar are playing a game on a circular board with \(n\) (\(2 \leq n \leq 10^6\)) cells. The cells are numbered from \(1\) to \(n\) so that for each \(i\) (\(1 \leq i \leq n-1\)) cell \(i\) is adjacent to cell \(i+1\) and cell \(1\) is adjacent to cell \(n\). Initially, each cell is empty.Omkar and Akmar take ...
The only line will contain an integer \(n\) (\(2 \leq n \leq 10^6\)) β€” the number of cells on the board.
Output a single integer β€” the number of possible distinct games where both players play optimally modulo \(10^9+7\).
For the first sample case, the first player has \(4\) possible moves. No matter what the first player plays, the second player only has \(1\) possible move, so there are \(4\) possible games.
Input: 2 | Output: 4
Expert
9
1,264
104
116
15
1,671
D
1671D
D. Insert a Progression
1,600
brute force; constructive algorithms; greedy
You are given a sequence of \(n\) integers \(a_1, a_2, \dots, a_n\). You are also given \(x\) integers \(1, 2, \dots, x\).You are asked to insert each of the extra integers into the sequence \(a\). Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the ...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of testcases.The first line of each testcase contains two integers \(n\) and \(x\) (\(1 \le n, x \le 2 \cdot 10^5\)) β€” the length of the sequence and the number of extra integers.The second line of each testcase contains \(n\) integers \...
For each testcase, print a single integer β€” the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it.
Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. \(\underline{1}, \underline{2}, \underline{3}, \underline{4}, \underline{5}, 10\) \(\underline{7}, 7, \underline{6}, \underline{4}, 2, \underli...
Input: 41 5103 87 2 1010 26 1 5 7 3 3 9 10 10 14 101 3 1 2 | Output: 9 15 31 13
Medium
3
565
445
166
16
1,285
A
1285A
A. Mezo Playing Zoma
800
math
Today, Mezo is playing a game. Zoma, a character in that game, is initially at position \(x = 0\). Mezo starts sending \(n\) commands to Zoma. There are two possible commands: 'L' (Left) sets the position \(x: =x - 1\); 'R' (Right) sets the position \(x: =x + 1\). Unfortunately, Mezo's controller malfunctions sometimes...
The first line contains \(n\) \((1 \le n \le 10^5)\) β€” the number of commands Mezo sends.The second line contains a string \(s\) of \(n\) commands, each either 'L' (Left) or 'R' (Right).
Print one integer β€” the number of different positions Zoma may end up at.
In the example, Zoma may end up anywhere between \(-2\) and \(2\).
Input: 4 LRLR | Output: 5
Beginner
1
1,080
186
73
12
327
B
327B
B. Hungry Sequence
1,200
math
Iahub and Iahubina went to a date at a luxury restaurant. Everything went fine until paying for the food. Instead of money, the waiter wants Iahub to write a Hungry sequence consisting of n integers. A sequence a1, a2, ..., an, consisting of n integers, is Hungry if and only if: Its elements are in increasing order. Th...
The input contains a single integer: n (1 ≀ n ≀ 105).
Output a line that contains n space-separated integers a1 a2, ..., an (1 ≀ ai ≀ 107), representing a possible Hungry sequence. Note, that each ai must not be greater than 10000000 (107) and less than 1.If there are multiple solutions you can output any one.
Input: 3 | Output: 2 9 15
Easy
1
542
53
257
3
1,312
A
1312A
A. Two Regular Polygons
800
geometry; greedy; math; number theory
You are given two integers \(n\) and \(m\) (\(m < n\)). Consider a convex regular polygon of \(n\) vertices. Recall that a regular polygon is a polygon that is equiangular (all angles are equal in measure) and equilateral (all sides have the same length). Examples of convex regular polygons Your task is to say if it is...
The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The next \(t\) lines describe test cases. Each test case is given as two space-separated integers \(n\) and \(m\) (\(3 \le m < n \le 100\)) β€” the number of vertices in the initial polygon and the number of vertices ...
For each test case, print the answer β€” ""YES"" (without quotes), if it is possible to build another convex regular polygon with \(m\) vertices such that its center coincides with the center of the initial polygon and each of its vertices is some vertex of the initial polygon and ""NO"" otherwise.
The first test case of the example It can be shown that the answer for the second test case of the example is ""NO"".
Input: 2 6 3 7 3 | Output: YES NO
Beginner
4
571
353
297
13
1,616
E
1616E
E. Lexicographically Small Enough
2,200
brute force; data structures; greedy; strings
You are given two strings \(s\) and \(t\) of equal length \(n\). In one move, you can swap any two adjacent characters of the string \(s\).You need to find the minimal number of operations you need to make string \(s\) lexicographically smaller than string \(t\).A string \(a\) is lexicographically smaller than a string...
The first line of input contains one integer \(q\) (\(1 \le q \le 10\,000\)): the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)).The second line of each test case contains the string \(s\) consisting of \(n\) lowercase English letters.The third line of each ...
For each test case, print in a separate line the minimal number of operations you need to make string \(s\) lexicographically smaller than string \(t\), or \(-1\), if it's impossible.
Input: 4 1 a a 3 rll rrr 3 caa aca 5 ababa aabba | Output: -1 0 2 2
Hard
4
573
494
183
16
1,808
C
1808C
C. Unlucky Numbers
1,900
brute force; dp; greedy; implementation
In this problem, unlike problem A, you need to look for unluckiest number, not the luckiest one.Note that the constraints of this problem differ from such in problem A.Olympus City recently launched the production of personal starships. Now everyone on Mars can buy one and fly to other planets inexpensively.Each starsh...
The first line contains an integer \(t\) (\(1 \le t \le 600\)) β€”the number of test cases.Each of the following \(t\) lines contains a description of the test case. The description consists of two integers \(l\), \(r\) (\(1 \le l \le r \le 10^{18}\)) β€” the largest and smallest numbers of the starships in the store.
Print \(t\) lines, one line for each test case, containing the unluckiest starship number in the store.If there are several ways to choose the unluckiest number, output any of them.
Let's look at two test examples: the luckiness of the number \(59\) is \(9 - 5 = 4\); the luckiness of \(60\) equals \(6 - 0 = 6\); the luckiness of \(61\) equals \(6 - 1 = 5\); the luckiness of \(62\) equals \(6 - 2 = 4\); the luckiness of \(63\) is \(6 - 3 = 3\). So the unluckiest number is \(63\).In the fifth test c...
Input: 559 6342 4948 5390 901 100 | Output: 63 44 53 90 1
Hard
4
1,180
315
181
18
1,209
C
1209C
C. Paint the Digits
1,500
constructive algorithms; greedy; implementation
You are given a sequence of \(n\) digits \(d_1d_2 \dots d_{n}\). You need to paint all the digits in two colors so that: each digit is painted either in the color \(1\) or in the color \(2\); if you write in a row from left to right all the digits painted in the color \(1\), and then after them all the digits painted i...
The first line contains a single integer \(t\) (\(1 \le t \le 10000\)) β€” the number of test cases in the input.The first line of each test case contains an integer \(n\) (\(1 \le n \le 2\cdot10^5\)) β€” the length of a given sequence of digits.The next line contains a sequence of \(n\) digits \(d_1d_2 \dots d_{n}\) (\(0 ...
Print \(t\) lines β€” the answers to each of the test cases in the input.If there is a solution for a test case, the corresponding output line should contain any of the valid colorings written as a string of \(n\) digits \(t_1t_2 \dots t_n\) (\(1 \le t_i \le 2\)), where \(t_i\) is the color the \(i\)-th digit is painted ...
In the first test case, \(d=040425524644\). The output \(t=121212211211\) is correct because \(0022444\) (painted in \(1\)) concatenated with \(44556\) (painted in \(2\)) is \(002244444556\) which is a sorted sequence of \(n\) given digits.
Input: 5 12 040425524644 1 0 9 123456789 2 98 3 987 | Output: 121212211211 1 222222222 21 -
Medium
3
999
558
498
12
1,873
E
1873E
E. Building an Aquarium
1,100
binary search; sortings
You love fish, that's why you have decided to build an aquarium. You have a piece of coral made of \(n\) columns, the \(i\)-th of which is \(a_i\) units tall. Afterwards, you will build a tank around the coral as follows: Pick an integer \(h \geq 1\) β€” the height of the tank. Build walls of height \(h\) on either side ...
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases.The first line of each test case contains two positive integers \(n\) and \(x\) (\(1 \leq n \leq 2 \cdot 10^5\); \(1 \leq x \leq 10^9\)) β€” the number of columns of the coral and the maximum amount of water you can use.The...
For each test case, output a single positive integer \(h\) (\(h \geq 1\)) β€” the maximum height the tank can have, so you need at most \(x\) units of water to fill up the tank.We have a proof that under these constraints, such a value of \(h\) always exists.
The first test case is pictured in the statement. With \(h=4\) we need \(8\) units of water, but if \(h\) is increased to \(5\) we need \(13\) units of water, which is more than \(x=9\). So \(h=4\) is optimal.In the second test case, we can pick \(h=4\) and add \(3\) units to each column, using a total of \(9\) units o...
Input: 57 93 1 2 4 6 2 53 101 1 14 11 4 3 46 19842 6 5 9 1 81 10000000001 | Output: 4 4 2 335 1000000001
Easy
2
789
522
257
18
1,840
F
1840F
F. Railguns
2,200
brute force; dfs and similar; dp; graphs
Tema is playing a very interesting computer game.During the next mission, Tema's character found himself on an unfamiliar planet. Unlike Earth, this planet is flat and can be represented as an \(n \times m\) rectangle.Tema's character is located at the point with coordinates \((0, 0)\). In order to successfully complet...
The first line of the input contains a single 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 test case contains two integers \(n\) and \(m\) (\(1 \le n \cdot m \le 10^4\)) β€” the size of the planet, its height and width.The second lin...
For each test case, output a single number β€” the minimum time for the character to reach the coordinate \((n, m)\), or \(-1\) if he is doomed to die.
In the first test case, the character can move as follows: \((0, 0) \rightarrow (0, 1) \rightarrow (0, 2) \rightarrow (0, 3) \rightarrow (0, 3) \rightarrow (1, 3)\).In the second test case, the character will not be able to leave the rectangle that will be completely penetrated by shots at the second \(2\).
Input: 51 341 2 02 2 13 2 24 1 13 362 1 02 1 12 1 22 2 02 2 12 2 22 137 1 22 1 17 2 12 259 1 23 2 05 1 24 2 27 1 04 676 1 212 1 34 1 017 2 31 2 616 2 63 2 4 | Output: 5 -1 3 6 10
Hard
4
1,532
929
149
18
89
D
89D
D. Space mines
2,500
geometry
Once upon a time in the galaxy of far, far away...Darth Wader found out the location of a rebels' base. Now he is going to destroy the base (and the whole planet that the base is located at), using the Death Star.When the rebels learnt that the Death Star was coming, they decided to use their new secret weapon β€” space ...
The first input data line contains 7 integers Ax, Ay, Az, vx, vy, vz, R. They are the Death Star's initial position, the direction of its movement, and its radius ( - 10 ≀ vx, vy, vz ≀ 10, |v| > 0, 0 < R ≀ 100).The second line contains an integer n, which is the number of mines (1 ≀ n ≀ 100). Then follow n data blocks,...
If the rebels will succeed in stopping the Death Star using space mines, print the time from the moment the Death Star was noticed to the blast.If the Death Star will not touch a mine, print ""-1"" (without quotes).For the answer the absolute or relative error of 10 - 6 is acceptable.
Input: 0 0 0 1 0 0 5210 8 0 2 20 -3 02 2 020 0 0 4 32 4 0-4 3 01 -5 0 | Output: 10.0000000000
Expert
1
1,896
1,080
285
0
1,921
B
1921B
B. Arranging Cats
800
greedy; implementation
In order to test the hypothesis about the cats, the scientists must arrange the cats in the boxes in a specific way. Of course, they would like to test the hypothesis and publish a sensational article as quickly as possible, because they are too engrossed in the next hypothesis about the phone's battery charge.Scientis...
Each test consists of several test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. This is followed by descriptions of the test cases.Each test case consists of three lines.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) β€” ...
For each test case, output a single integer on a separate line β€” the minimum number of operations required to obtain the desired position from the initial position. It can be shown that a solution always exists.
In the first test case, you can first move the cat from the first box to the fifth, and then remove the cat from the fourth box.In the second test case, there is nothing to do β€” the only cat is already sitting in the correct box.In the third test case of input data, it takes three days to place a cat in each box.
Input: 6510010000011113000111401011010310010181001100111111110 | Output: 2 0 3 2 1 4
Beginner
2
1,426
782
211
19
1,461
A
1461A
A. String Generation
800
constructive algorithms; greedy
One fall day Joe got bored because he couldn't find himself something interesting to do. Marty suggested Joe to generate a string of length \(n\) to entertain him somehow. It didn't seem particularly difficult, but Joe's generated string had to follow these rules: the string may only contain characters 'a', 'b', or 'c'...
Each test contains one or more test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10\)).The only line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 1\,000\)) β€” the required string length and the maximum length of a palindrome substring, respectively.
For each test case, print any string that satisfies the conditions from the problem statement. If there are multiple correct answers, you can print any one of them. It can be proven that the answer always exists under the given constraints.
In the first test case of the example, the palindrome substring with the maximum length is ""aa"". Its length does not exceed \(2\), so it fits.In the second test case all palindrome substrings have the length one.
Input: 2 3 2 4 1 | Output: aab acba
Beginner
2
1,153
312
240
14
690
D3
690D3
D3. The Wall (hard)
2,100
dp
So many wall designs to choose from! Even modulo 106 + 3, it's an enormous number. Given that recently Heidi acquired an unlimited supply of bricks, her choices are endless! She really needs to do something to narrow them down.Heidi is quick to come up with criteria for a useful wall: In a useful wall, at least one seg...
The first and the only line of the input contains three space-separated integers C, W and H (1 ≀ C ≀ 108, 1 ≀ W, H ≀ 100).
Output the number of different walls, modulo 106 + 3, which are useless according to Heidi's criteria.
If there is no brick in any of the columns, the structure is considered as a useless wall.
Input: 1 1 1 | Output: 2
Hard
1
998
122
102
6
1,473
E
1473E
E. Minimum Path
2,400
graphs; shortest paths
You are given a weighted undirected connected graph consisting of \(n\) vertices and \(m\) edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.Let's define the weight of the path consisting of \(k\) edges with indices \(e_1, e_2, \dots, e_k\) as \(\sum\limits_{i=1}^{k}{w_{e_i}} - \...
The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 2 \cdot 10^5\); \(1 \le m \le 2 \cdot 10^5\)) β€” the number of vertices and the number of edges in the graph.Following \(m\) lines contains three integers \(v_i, u_i, w_i\) (\(1 \le v_i, u_i \le n\); \(1 \le w_i \le 10^9\); \(v_i \neq u_i\)) β€” endpoints...
Print \(n-1\) integers β€” the minimum weight of the path from \(1\)-st vertex to the \(i\)-th vertex for each \(i\) (\(2 \le i \le n\)).
Input: 5 4 5 3 4 2 1 1 3 2 2 2 4 2 | Output: 1 2 2 4
Expert
2
578
370
135
14
2,021
E3
2021E3
E3. Digital Village (Extreme Version)
2,800
data structures; dfs and similar; dp; dsu; graphs; greedy; math; trees
This is the extreme version of the problem. In the three versions, the constraints on \(n\) and \(m\) are different. You can make hacks only if all the versions of the problem are solved.Pak Chanek is setting up internet connections for the village of Khuntien. The village can be represented as a connected simple graph...
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 3 integers \(n\), \(m\), \(p\) (\(2 \le n \le 2 \cdot 10^5\); \(n-1 \le m \le 2 \cdot 10^5\); \(1 \le p \le n\)) β€” th...
For each test case, output \(n\) integers: the minimum total latency that can be achieved for all the houses requiring internet for each \(k = 1,2,\ldots,n\).
In the first test case for \(k=3\), a possible optimal solution is to install servers at vertices \(2\), \(6\) and \(8\) and obtain the following latency: \(\text{latency}(2) = 0\) \(\text{latency}(5) = \max(3, 5) = 5\) \(\text{latency}(6) = 0\) \(\text{latency}(8) = 0\) \(\text{latency}(9) = \max(2, 4) = 4\) So the to...
Input: 29 8 52 5 6 8 91 2 11 3 23 4 104 5 34 6 51 7 107 8 47 9 23 3 23 11 2 12 3 31 3 2 | Output: 34 19 9 4 0 0 0 0 0 2 0 0
Master
8
965
1,012
158
20
698
E
698E
E. Cron
2,800
Sometime the classic solution are not powerful enough and we have to design our own. For the purpose of this problem you have to implement the part of the system of task scheduling.Each task should be executed at some particular moments of time. In our system you may set the exact value for the second, minute, hour, da...
The first line of the input contains six integers s, m, h, day, date and month (0 ≀ s, m ≀ 59, 0 ≀ h ≀ 23, 1 ≀ day ≀ 7, 1 ≀ date ≀ 31, 1 ≀ month ≀ 12). Each of the number can also be equal to - 1. It's guaranteed, that there are infinitely many moments of time when this task should be executed.Next line contains the on...
Print n lines, the i-th of them should contain the first moment of time strictly greater than ti, when the task should be executed.
The moment of time 1467372658 after the midnight of January 1st, 1970 is 11:30:58 July 1st, 2016.
Input: -1 59 23 -1 -1 -16146737265814674175401467417541146741759814674175991467417600 | Output: 146741754014674175411467417542146741759914675039401467503940
Master
0
1,922
485
131
6
444
A
444A
A. DZY Loves Physics
1,600
greedy; math
DZY loves Physics, and he enjoys calculating density.Almost everything has density, even a graph. We define the density of a non-directed graph (nodes and edges of the graph have some values) as follows: where v is the sum of the values of the nodes, e is the sum of the values of the edges.Once DZY got a graph G, now h...
The first line contains two space-separated integers n (1 ≀ n ≀ 500), . Integer n represents the number of nodes of the graph G, m represents the number of edges.The second line contains n space-separated integers xi (1 ≀ xi ≀ 106), where xi represents the value of the i-th node. Consider the graph nodes are numbered f...
Output a real number denoting the answer, with an absolute or relative error of at most 10 - 9.
In the first sample, you can only choose an empty subgraph, or the subgraph containing only node 1.In the second sample, choosing the whole graph is optimal.
Input: 1 01 | Output: 0.000000000000000
Medium
2
777
534
95
4
1,763
F
1763F
F. Edge Queries
3,000
data structures; dfs and similar; dp; dsu; graphs; trees
You are given an undirected, connected graph of \(n\) nodes and \(m\) edges. All nodes \(u\) of the graph satisfy the following: Let \(S_u\) be the set of vertices in the longest simple cycle starting and ending at \(u\). Let \(C_u\) be the union of the sets of vertices in any simple cycle starting and ending at \(u\)....
The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le m \le \min\)(\(2 \cdot 10^5\), \((n \cdot (n-1))/2\))) β€” the total number of nodes and edges in the graph, respectively.The next \(m\) lines contain two integers \(u\) and \(v\) (\(1 \le\) \(u\), \(v\) \(\le n\), \(u \neq v\)) β€”...
For each query, output a single integer β€” answer to the query.
The graph in the first sample is : The first query is \((1, 4)\). There are \(5\) total edges that belong to any simple path from \(1\) to \(4\). Edges \((3, 4), (4, 5), (5, 3)\) will be counted in the answer to the query.The fourth query is \((2, 8)\). There is only one simple path from \(2\) to \(8\), thus none of th...
Input: 10 11 1 2 2 3 3 4 4 5 5 3 2 7 7 9 9 10 10 6 6 7 1 8 5 1 4 5 10 3 5 2 8 7 10 | Output: 3 7 3 0 4
Master
6
610
765
62
17
1,260
F
1260F
F. Colored Tree
2,700
data structures; trees
You're given a tree with \(n\) vertices. The color of the \(i\)-th vertex is \(h_{i}\).The value of the tree is defined as \(\sum\limits_{h_{i} = h_{j}, 1 \le i < j \le n}{dis(i,j)}\), where \(dis(i,j)\) is the number of edges on the shortest path between \(i\) and \(j\). The color of each vertex is lost, you only reme...
The first line contains one integer \(n\) (\(2 \le n \le 10^5\)) β€” the number of vertices.Then \(n\) lines follow, each line contains two integers \(l_i\) and \(r_i\) (\(1 \le l_i \le r_i \le 10^5\)) denoting the range of possible colors of vertex \(i\).Then \(n - 1\) lines follow, each containing two integers \(u\) an...
Print one integer β€” the sum of values of all possible trees, taken modulo \(10^9 + 7\).
In the first example there are four different ways to color the tree (so, there are four different trees): a tree with vertices colored as follows: \(\lbrace 1,1,1,1 \rbrace\). The value of this tree is \(dis(1,2)+dis(1,3)+dis(1,4)+dis(2,3)+dis(2,4)+dis(3,4) = 10\); a tree with vertices colored as follows: \(\lbrace 1,...
Input: 4 1 1 1 2 1 1 1 2 1 2 1 3 3 4 | Output: 22
Master
2
627
440
87
12
1,560
D
1560D
D. Make a Power of Two
1,300
greedy; math; strings
You are given an integer \(n\). In \(1\) move, you can do one of the following actions: erase any digit of the number (it's acceptable that the number before the operation has exactly one digit and after the operation, it is ""empty""); add one digit to the right. The actions may be performed in any order any number of...
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. Then \(t\) test cases follow.Each test case consists of one line containing one integer \(n\) (\(1 \le n \le 10^9\)).
For each test case, output in a separate line one integer \(m\) β€” the minimum number of moves to transform the number into any power of \(2\).
The answer for the first test case was considered above.The answer for the second test case was considered above.In the third test case, it's enough to add to the right the digit \(4\) β€” the number \(6\) will turn into \(64\).In the fourth test case, let's add to the right the digit \(8\) and then erase \(7\) and \(5\)...
Input: 12 1052 8888 6 75 128 1 301 12048 1504 6656 1000000000 687194767 | Output: 2 3 1 3 0 0 2 1 3 4 9 2
Easy
3
1,161
210
142
15
106
C
106C
C. Buns
1,700
dp
Lavrenty, a baker, is going to make several buns with stuffings and sell them.Lavrenty has n grams of dough as well as m different stuffing types. The stuffing types are numerated from 1 to m. Lavrenty knows that he has ai grams left of the i-th stuffing. It takes exactly bi grams of stuffing i and ci grams of dough to...
The first line contains 4 integers n, m, c0 and d0 (1 ≀ n ≀ 1000, 1 ≀ m ≀ 10, 1 ≀ c0, d0 ≀ 100). Each of the following m lines contains 4 integers. The i-th line contains numbers ai, bi, ci and di (1 ≀ ai, bi, ci, di ≀ 100).
Print the only number β€” the maximum number of tugriks Lavrenty can earn.
To get the maximum number of tugriks in the first sample, you need to cook 2 buns with stuffing 1, 4 buns with stuffing 2 and a bun without any stuffing.In the second sample Lavrenty should cook 4 buns without stuffings.
Input: 10 2 2 17 3 2 10012 3 1 10 | Output: 241
Medium
1
751
224
72
1
41
A
41A
A. Translation
800
implementation; strings
The translation from the Berland language into the Birland language is not an easy task. Those languages are very similar: a Berlandish word differs from a Birlandish word with the same meaning a little: it is spelled (and pronounced) reversely. For example, a Berlandish word code corresponds to a Birlandish word edoc....
The first line contains word s, the second line contains word t. The words consist of lowercase Latin letters. The input data do not contain unnecessary spaces. The words are not empty and their lengths do not exceed 100 symbols.
If the word t is a word s, written reversely, print YES, otherwise print NO.
Input: codeedoc | Output: YES
Beginner
2
504
229
76
0
1,873
A
1873A
A. Short Sort
800
brute force; implementation
There are three cards with letters \(\texttt{a}\), \(\texttt{b}\), \(\texttt{c}\) placed in a row in some order. You can do the following operation at most once: Pick two cards, and swap them. Is it possible that the row becomes \(\texttt{abc}\) after the operation? Output ""YES"" if it is possible, and ""NO"" otherwis...
The first line contains a single integer \(t\) (\(1 \leq t \leq 6\)) β€” the number of test cases.The only line of each test case contains a single string consisting of each of the three characters \(\texttt{a}\), \(\texttt{b}\), and \(\texttt{c}\) exactly once, representing the cards.
For each test case, output ""YES"" if you can make the row \(\texttt{abc}\) with at most one operation, or ""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).
In the first test case, we don't need to do any operations, since the row is already \(\texttt{abc}\).In the second test case, we can swap \(\texttt{c}\) and \(\texttt{b}\): \(\texttt{acb} \to \texttt{abc}\).In the third test case, we can swap \(\texttt{b}\) and \(\texttt{a}\): \(\texttt{bac} \to \texttt{abc}\).In the ...
Input: 6abcacbbacbcacabcba | Output: YES YES YES NO NO YES
Beginner
2
322
284
267
18
578
C
578C
C. Weakness and Poorness
2,000
ternary search
You are given a sequence of n integers a1, a2, ..., an. Determine a real number x such that the weakness of the sequence a1 - x, a2 - x, ..., an - x is as small as possible.The weakness of a sequence is defined as the maximum value of the poorness over all segments (contiguous subsequences) of a sequence.The poorness o...
The first line contains one integer n (1 ≀ n ≀ 200 000), the length of a sequence.The second line contains n integers a1, a2, ..., an (|ai| ≀ 10 000).
Output a real number denoting the minimum possible weakness of a1 - x, a2 - x, ..., an - x. Your answer will be considered correct if its relative or absolute error doesn't exceed 10 - 6.
For the first case, the optimal value of x is 2 so the sequence becomes - 1, 0, 1 and the max poorness occurs at the segment ""-1"" or segment ""1"". The poorness value (answer) equals to 1 in this case. For the second sample the optimal value of x is 2.5 so the sequence becomes - 1.5, - 0.5, 0.5, 1.5 and the max poorn...
Input: 31 2 3 | Output: 1.000000000000000
Hard
1
399
150
187
5
1,098
E
1098E
E. Fedya the Potter
3,400
binary search; implementation; math; number theory
Fedya loves problems involving data structures. Especially ones about different queries on subsegments. Fedya had a nice array \(a_1, a_2, \ldots a_n\) and a beautiful data structure. This data structure, given \(l\) and \(r\), \(1 \le l \le r \le n\), could find the greatest integer \(d\), such that \(d\) divides each...
First line contains a single integer \(n\) β€” number of elements in array \(a\) (\(1 \le n \le 50\,000\)). Second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) β€” elements of the array (\(1 \le a_i \le 100\,000\)).
Print a single integer β€” the lower median of array \(c\).
In the first sample array \(b\) is equal to \({3, 3, 6}\), then array \(c\) is equal to \({3, 3, 6, 6, 9, 12}\), so the lower median is \(6\).In the second sample \(b\) is \({8, 8, 8}\), \(c\) is \({8, 8, 8, 16, 16, 24}\), so the lower median is \(8\).
Input: 2 6 3 | Output: 6
Master
4
1,278
221
57
10
887
F
887F
F. Row of Models
2,500
greedy; sortings
During the final part of fashion show all models come to the stage and stay in one row and fashion designer stays to right to model on the right. During the rehearsal, Izabella noticed, that row isn't nice, but she can't figure out how to fix it. Like many other creative people, Izabella has a specific sense of beauty....
In first line there are two integers n and k (1 ≀ n ≀ 5Β·105, 1 ≀ k ≀ n) β€” number of models and required distance.Second line contains n space-separated integers ai (1 ≀ ai ≀ 109) β€” height of each model. Pay attention that height of fashion designer is not given and can be less than 1.
Print Β«YESΒ» (without quotes) if it's possible to make row nice using at most one exchange, and Β«NOΒ» (without quotes) otherwise.
Input: 5 42 3 5 2 5 | Output: NO
Expert
2
1,143
285
127
8
1,927
D
1927D
D. Find the Different Ones!
1,300
binary search; brute force; data structures; dp; dsu; greedy; two pointers
You are given an array \(a\) of \(n\) integers, and \(q\) queries.Each query is represented by two integers \(l\) and \(r\) (\(1 \le l \le r \le n\)). Your task is to find, for each query, two indices \(i\) and \(j\) (or determine that they do not exist) such that: \(l \le i \le r\); \(l \le j \le r\); \(a_i \ne a_j\)....
The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. The descriptions of the test cases follow.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β€” the length of the array \(a\).The second line of each test case conta...
For each query, output two integers separated by space: \(i\) and \(j\) (\(l \le i, j \le r\)), for which \(a_i \ne a_j\). If such a pair does not exist, output \(i=-1\) and \(j=-1\).You may separate the outputs for the test cases with empty lines. This is not a mandatory requirement.
Input: 551 1 2 1 131 51 21 3630 20 20 10 10 2051 22 32 42 63 545 2 3 441 21 42 32 451 4 3 2 451 52 43 43 54 552 3 1 4 271 21 41 52 42 53 54 5 | Output: 2 3 -1 -1 1 3 2 1 -1 -1 4 2 4 6 5 3 1 2 1 2 2 3 3 2 1 3 2 4 3 4 5 3 5 4 1 2 4 2 1 3 2 3 3 2 5 4 5 4
Easy
7
477
889
285
19
1,471
A
1471A
A. Strange Partition
900
greedy; math; number theory
You are given an array \(a\) of length \(n\), and an integer \(x\). You can perform the following operation as many times as you would like (possibly zero): replace two adjacent elements of the array by their sum. For example, if the initial array was \([3, 6, 9]\), in a single operation one can replace the last two el...
The first input line contains a single integer \(t\) β€” the number of test cases (\(1 \le t \le 1000\)).The first line of each test case contains two integers \(n\) and \(x\) (\(1 \leq n \leq 10^5\), \(1 \leq x \leq 10^9\)).The next line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)), the e...
For each test case output two integers β€” the minimal and the maximal possible beauty.
In the first test case the beauty of the array does not change if we perform any operations.In the second example we can leave the array unchanged to attain the maximum beauty, and to get the minimum beauty one can replace two elements \(4\) and \(11\) with their sum, yielding an array \([6, 15]\), which has its beauty...
Input: 23 33 6 93 36 4 11 | Output: 6 6 7 8
Beginner
3
1,083
442
85
14
1,003
D
1003D
D. Coins and Queries
1,600
greedy
Polycarp has \(n\) coins, the value of the \(i\)-th coin is \(a_i\). It is guaranteed that all the values are integer powers of \(2\) (i.e. \(a_i = 2^d\) for some non-negative integer number \(d\)).Polycarp wants to know answers on \(q\) queries. The \(j\)-th query is described as integer number \(b_j\). The answer to ...
The first line of the input contains two integers \(n\) and \(q\) (\(1 \le n, q \le 2 \cdot 10^5\)) β€” the number of coins and the number of queries.The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) β€” values of coins (\(1 \le a_i \le 2 \cdot 10^9\)). It is guaranteed that all \(a_i\) are inte...
Print \(q\) integers \(ans_j\). The \(j\)-th integer must be equal to the answer on the \(j\)-th query. If Polycarp can't obtain the value \(b_j\) the answer to the \(j\)-th query is -1.
Input: 5 42 4 8 2 4851410 | Output: 1-132
Medium
1
641
557
186
10
682
B
682B
B. Alyona and Mex
1,200
sortings
Someone gave Alyona an array containing n positive integers a1, a2, ..., an. In one operation, Alyona can choose any element of the array and decrease it, i.e. replace with any positive integer that is smaller than the current one. Alyona can repeat this operation as many times as she wants. In particular, she may not ...
The first line of the input contains a single integer n (1 ≀ n ≀ 100 000) β€” the number of elements in the Alyona's array.The second line of the input contains n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the elements of the array.
Print one positive integer β€” the maximum possible value of mex of the array after Alyona applies some (possibly none) operations.
In the first sample case if one will decrease the second element value to 2 and the fifth element value to 4 then the mex value of resulting array 1 2 3 3 4 will be equal to 5.To reach the answer to the second sample case one must not decrease any of the array elements.
Input: 51 3 3 3 6 | Output: 5
Easy
1
807
229
129
6
1,598
A
1598A
A. Computer Game
800
brute force; dfs and similar; dp; implementation
Monocarp is playing a computer game. Now he wants to complete the first level of this game.A level is a rectangular grid of \(2\) rows and \(n\) columns. Monocarp controls a character, which starts in cell \((1, 1)\) β€” at the intersection of the \(1\)-st row and the \(1\)-st column.Monocarp's character can move from on...
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β€” the number of test cases. Then the test cases follow. Each test case consists of three lines.The first line contains a single integer \(n\) (\(3 \le n \le 100\)) β€” the number of columns.The next two lines describe the level. The \(i\)-th of these li...
For each test case, output YES if it is possible to complete the level, and NO otherwise.
Consider the example from the statement.In the first test case, one of the possible paths is \((1, 1) \rightarrow (2, 2) \rightarrow (2, 3)\).In the second test case, one of the possible paths is \((1, 1) \rightarrow (1, 2) \rightarrow (2, 3) \rightarrow (2, 4)\).In the fourth test case, one of the possible paths is \(...
Input: 4 3 000 000 4 0011 1100 4 0111 1110 6 010101 101010 | Output: YES YES NO YES
Beginner
4
894
584
89
15
1,557
A
1557A
A. Ezzat and Two Subsequences
800
brute force; math; sortings
Ezzat has an array of \(n\) integers (maybe negative). He wants to split it into two non-empty subsequences \(a\) and \(b\), such that every element from the array belongs to exactly one subsequence, and the value of \(f(a) + f(b)\) is the maximum possible value, where \(f(x)\) is the average of the subsequence \(x\). ...
The first line contains a single integer \(t\) (\(1 \le t \le 10^3\))β€” the number of test cases. Each test case consists of two lines.The first line contains a single integer \(n\) (\(2 \le n \le 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-10^9 \le a_i \le 10^9\)).It is guaranteed that...
For each test case, print a single value β€” the maximum value that Ezzat can achieve.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|)}} ...
In the first test case, the array is \([3, 1, 2]\). These are all the possible ways to split this array: \(a = [3]\), \(b = [1,2]\), so the value of \(f(a) + f(b) = 3 + 1.5 = 4.5\). \(a = [3,1]\), \(b = [2]\), so the value of \(f(a) + f(b) = 2 + 2 = 4\). \(a = [3,2]\), \(b = [1]\), so the value of \(f(a) + f(b) = 2.5 +...
Input: 4 3 3 1 2 3 -7 -6 -6 3 2 2 2 4 17 3 5 -3 | Output: 4.500000000 -12.500000000 4.000000000 18.666666667
Beginner
3
671
389
334
15
1,172
D
1172D
D. Nauuo and Portals
2,900
constructive algorithms
Nauuo is a girl who loves playing games related to portals.One day she was playing a game as follows.In an \(n\times n\) grid, the rows are numbered from \(1\) to \(n\) from top to bottom, the columns are numbered from \(1\) to \(n\) from left to right. We denote a cell on the intersection of the \(r\)-th row and \(c\)...
The first line contains a single integer \(n\) (\(1\le n\le 1000\)) β€” the side length of the grid.The second line contains \(n\) integers \(r_1,r_2,\ldots,r_n\) (\(1\le r_i\le n\)) β€” if you walk into \((i,1)\) facing right, you should exit the grid from \((r_i,n)\). It is guaranteed that \(r_{1..n}\) is a permutation o...
If it is impossible to satisfy the rule, print the only number \(-1\).Otherwise the first line should contain a single integer \(m\) (\(0\le m\le\frac{n^2}2\)) β€” the number of portals you set.In the following \(m\) lines, each line should contain four integers \(x_1,y_1,x_2,y_2\), represents that you set a portal consi...
Example 1The cells with the same letter are a portal. You can set portals in this way:It satisfies the rule, because:Example 2You can set portals in this way:
Input: 3 1 3 2 3 1 2 | Output: 2 1 1 1 3 2 2 3 1
Master
1
1,566
576
450
11
1,450
H2
1450H2
H2. Multithreading (Hard Version)
3,300
combinatorics; implementation; math
The only difference between the two versions of the problem is that there are no updates in the easy version.There are \(n\) spools of thread placed on the rim of a circular table. The spools come in two types of thread: the first thread is black and the second thread is white.For any two spools of the same color, you ...
The first line contains two integers \(n\), \(m\) (\(2\le n\le 2\cdot 10^5\), \(n\) is even, \(0\le m\le 2\cdot 10^5\)) β€” the number of spools and the number of updates, respectively.The second line contains a string \(s\) of length \(n\) β€” the unfinished coloring of the spools. The \(i\)-th character will be 'w', 'b',...
Print \(m+1\) lines: the expected value of \(f(c)\) initially and after each update. All values should be found by modulo \(998244353\).
The first test corresponds closely to the image. Coloring '?' as 'w' does not create a valid coloring because the number of black spools is odd. Then the only reachable valid coloring is 'bwbbbwww' and \(f(\text{bwbbbwww}) = 1\), so the expected value is \(1\).In the second test, the string after each update is: ????w?...
Input: 8 0 bwbb?www | Output: 1
Master
3
2,224
749
136
14
376
B
376B
B. I.O.U.
1,300
implementation
Imagine that there is a group of three friends: A, B and Π‘. A owes B 20 rubles and B owes C 20 rubles. The total sum of the debts is 40 rubles. You can see that the debts are not organized in a very optimal manner. Let's rearrange them like that: assume that A owes C 20 rubles and B doesn't owe anything to anybody. The...
The first line contains two integers n and m (1 ≀ n ≀ 100; 0 ≀ m ≀ 104). The next m lines contain the debts. The i-th line contains three integers ai, bi, ci (1 ≀ ai, bi ≀ n; ai β‰  bi; 1 ≀ ci ≀ 100), which mean that person ai owes person bi ci rubles.Assume that the people are numbered by integers from 1 to n.It is guar...
Print a single integer β€” the minimum sum of debts in the optimal rearrangement.
In the first sample, you can assume that person number 1 owes 8 rubles to person number 2, 1 ruble to person number 3 and 1 ruble to person number 4. He doesn't owe anybody else anything. In the end, the total debt equals 10.In the second sample, there are no debts.In the third sample, you can annul all the debts.
Input: 5 31 2 102 3 12 4 1 | Output: 10
Easy
1
893
479
79
3
13
D
13D
D. Triangles
2,600
dp; geometry
Little Petya likes to draw. He drew N red and M blue points on the plane in such a way that no three points lie on the same line. Now he wonders what is the number of distinct triangles with vertices in red points which do not contain any blue point inside.
The first line contains two non-negative integer numbers N and M (0 ≀ N ≀ 500, 0 ≀ M ≀ 500) β€” the number of red and blue points respectively. The following N lines contain two integer numbers each β€” coordinates of red points. The following M lines contain two integer numbers each β€” coordinates of blue points. All coord...
Output one integer β€” the number of distinct triangles with vertices in red points which do not contain any blue point inside.
Input: 4 10 010 010 105 42 1 | Output: 2
Expert
2
257
363
125
0
2,023
C
2023C
C. C+K+S
2,400
constructive algorithms; dfs and similar; graphs; greedy; hashing; implementation; strings
You are given two strongly connected\(^{\dagger}\) directed graphs, each with exactly \(n\) vertices, but possibly different numbers of edges. Upon closer inspection, you noticed an important feature β€” the length of any cycle in these graphs is divisible by \(k\).Each of the \(2n\) vertices belongs to exactly one of tw...
Each test consists of multiple test cases. The first line 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 \(k\) (\(2 \le k \le n \le 2 \cdot 10^5\)) β€” the number of vertices in e...
For each test case, output ""YES"" (without quotes) if it is possible to draw \(n\) new edges such that all conditions are met, and ""NO"" (without quotes) otherwise.You may output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as a positive answer).
In the first test case, it is possible to draw edges from the first graph to the second graph as \((1, 3)\) and \((4, 2)\) (the first number in the pair is the vertex number in the first graph, and the second number in the pair is the vertex number in the second graph), and from the second graph to the first graph as \...
Input: 34 21 0 0 141 22 33 44 11 0 0 141 33 22 44 13 30 0 031 22 33 11 1 031 22 33 14 21 1 1 141 22 33 44 10 0 0 061 22 11 33 11 44 1 | Output: YES NO YES
Expert
7
910
1,992
310
20
1,674
D
1674D
D. A-B-C Sort
1,200
constructive algorithms; implementation; sortings
You are given three arrays \(a\), \(b\) and \(c\). Initially, array \(a\) consists of \(n\) elements, arrays \(b\) and \(c\) are empty.You are performing the following algorithm that consists of two steps: Step \(1\): while \(a\) is not empty, you take the last element from \(a\) and move it in the middle of array \(b\...
The first line contains a single integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β€” the number of test cases. Next \(t\) cases follow.The first line of each test case contains the single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the length of array \(a\).The second line of each test case contains \(n\) integers \(a_1,...
For each test, print YES (case-insensitive), if you can make array \(c\) sorted in non-decreasing order. Otherwise, print NO (case-insensitive).
In the first test case, we can do the following for \(a = [3, 1, 5, 3]\):Step \(1\): \(a\)\([3, 1, 5, 3]\)\(\Rightarrow\)\([3, 1, 5]\)\(\Rightarrow\)\([3, 1]\)\(\Rightarrow\)\([3]\)\(\Rightarrow\)\([]\)\(b\)\([]\)\([\underline{3}]\)\([3, \underline{5}]\)\([3, \underline{1}, 5]\)\([3, \underline{3}, 1, 5]\)Step \(2\): \...
Input: 343 1 5 333 2 117331 | Output: YES NO YES
Easy
3
909
459
144
16
1,303
E
1303E
E. Erase Subsequences
2,200
dp; strings
You are given a string \(s\). You can build new string \(p\) from \(s\) using the following operation no more than two times: choose any subsequence \(s_{i_1}, s_{i_2}, \dots, s_{i_k}\) where \(1 \le i_1 < i_2 < \dots < i_k \le |s|\); erase the chosen subsequence from \(s\) (\(s\) can become empty); concatenate chosen ...
The first line contains the single integer \(T\) (\(1 \le T \le 100\)) β€” the number of test cases.Next \(2T\) lines contain test cases β€” two per test case. The first line contains string \(s\) consisting of lowercase Latin letters (\(1 \le |s| \le 400\)) β€” the initial string.The second line contains string \(t\) consis...
Print \(T\) answers β€” one per test case. Print YES (case insensitive) if it's possible to build \(t\) and NO (case insensitive) otherwise.
Input: 4 ababcd abcba a b defi fed xyz x | Output: YES NO NO YES
Hard
2
855
487
138
13
1,284
G
1284G
G. Seollal
3,300
graphs
It is only a few days until Seollal (Korean Lunar New Year), and Jaehyun has invited his family to his garden. There are kids among the guests. To make the gathering more fun for the kids, Jaehyun is going to run a game of hide-and-seek.The garden can be represented by a \(n \times m\) grid of unit cells. Some (possibl...
Your program will be judged in multiple test cases.The first line contains the number of test cases \(t\). (\(1 \le t \le 100\)). Afterward, \(t\) test cases with the described format will be given.The first line of a test contains two integers \(n, m\) (\(2 \le n, m \le 20\)), the size of the grid.In the next \(n\) li...
For each test case, print the following:If there are no possible mazes, print a single line NO.Otherwise, print a single line YES, followed by a grid of size \((2n-1) \times (2m-1)\) denoting the found maze. The rules for displaying the maze follows. All cells are indexed in 1-base. For all \(1 \le i \le n, 1 \le j \le...
Input: 4 2 2 OO OO 3 3 OOO XOO OOO 4 4 OOOX XOOX OOXO OOOO 5 6 OOOOOO OOOOOO OOOOOO OOOOOO OOOOOO | Output: YES OOO O OOO NO YES OOOOO X O O X O O X O OOO X O O O O O OOOOO YES OOOOOOOOOOO O O O OOO OOO OOO O O O OOO OOO OOO O O O OOO OOO OOO O O O OOO OOO OOO
Master
1
2,031
812
1,362
12
650
E
650E
E. Clockwork Bomb
3,200
data structures; dfs and similar; dsu; greedy; trees
My name is James diGriz, I'm the most clever robber and treasure hunter in the whole galaxy. There are books written about my adventures and songs about my operations, though you were able to catch me up in a pretty awkward moment.I was able to hide from cameras, outsmart all the guards and pass numerous traps, but whe...
The first line of the input contains n (2 ≀ n ≀ 500 000), the number of contacts.Each of the following n - 1 lines contains two of integers xi and yi (1 ≀ xi, yi ≀ n, xi β‰  yi) denoting the contacts currently connected by the i-th wire.The remaining n - 1 lines contain the description of the sought scheme in the same fo...
The first line should contain k (k β‰₯ 0) β€” the minimum number of moves of unplugging and plugging back some wire required to defuse the bomb.In each of the following k lines output four integers ai, bi, ci, di meaning that on the i-th step it is neccesary to unplug the wire connecting the contacts ai and bi and plug it ...
Picture with the clarification for the sample tests:
Input: 31 22 31 33 2 | Output: 11 2 1 3
Master
5
1,719
467
524
6
234
D
234D
D. Cinema
1,600
implementation
Overall there are m actors in Berland. Each actor has a personal identifier β€” an integer from 1 to m (distinct actors have distinct identifiers). Vasya likes to watch Berland movies with Berland actors, and he has k favorite actors. He watched the movie trailers for the next month and wrote the following information fo...
The first line of the input contains two integers m and k (1 ≀ m ≀ 100, 1 ≀ k ≀ m) β€” the number of actors in Berland and the number of Vasya's favourite actors. The second line contains k distinct integers ai (1 ≀ ai ≀ m) β€” the identifiers of Vasya's favourite actors.The third line contains a single integer n (1 ≀ n ≀ ...
Print n lines in the output. In the i-th line print: 0, if the i-th movie will surely be the favourite; 1, if the i-th movie won't surely be the favourite; 2, if the i-th movie can either be favourite, or not favourite.
Note to the second sample: Movie jumanji can theoretically have from 1 to 3 Vasya's favourite actors. Movie theeagle has all three favourite actors, as the actor Vasya failed to remember, can only have identifier 5. Movie matrix can have exactly one favourite actor. Movie sourcecode doesn't have any favourite actors. T...
Input: 5 31 2 36firstfilm30 0 0secondfilm40 0 4 5thirdfilm12fourthfilm15fifthfilm14sixthfilm21 0 | Output: 221112
Medium
1
1,051
1,122
219
2
2,124
B
2124B
B. Minimise Sum
1,000
greedy
This problem differs from problem G. In this problem, you must output the minimum sum of prefix minimums after at most one operation.You are given an array \(a\) of length \(n\), with elements satisfying \(\boldsymbol{0 \le a_i \le n}\). You can perform the following operation at most once: Choose two indices \(i\) and...
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 \leq n \leq 2\cdot 10^5\)) β€” the length of \(a\).The following line contains \(n\) space-separ...
For each test case, output an integer on a new line, the minimum possible value of \(\min(a_1) + \min(a_1,a_2) + \ldots + \min(a_1, a_2, \ldots, a_n)\).
In the second test case, it is optimal to perform the operation with \(i=2\) and \(j=3\).In the third test case, it is optimal to not perform any operations. The answer is \(3\).
Input: 321 231 2 343 0 2 3 | Output: 2 2 3
Beginner
1
520
500
152
21
1,997
A
1997A
A. Strong Password
800
brute force; implementation; strings
Monocarp's current password on Codeforces is a string \(s\), consisting of lowercase Latin letters. Monocarp thinks that his current password is too weak, so he wants to insert exactly one lowercase Latin letter into the password to make it stronger. Monocarp can choose any letter and insert it anywhere, even before th...
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases.Each test case consists of one line containing the string \(s\) (\(1 \le |s| \le 10\)), consisting of lowercase Latin letters.
For each test case, print one line containing the new password β€” a string which can be obtained from \(s\) by inserting one lowercase Latin letter. The string you print should have the maximum possible required time to type it. If there are multiple answers, print any of them.
Input: 4aaaaabbpassword | Output: wa aada abcb pastsword
Beginner
3
1,091
218
277
19
1,499
F
1499F
F. Diameter Cuts
2,400
combinatorics; dfs and similar; dp; trees
You are given an integer \(k\) and an undirected tree, consisting of \(n\) vertices.The length of a simple path (a path in which each vertex appears at most once) between some pair of vertices is the number of edges in this path. A diameter of a tree is the maximum length of a simple path between all pairs of vertices ...
The first line contains two integers \(n\) and \(k\) (\(2 \le n \le 5000\), \(0 \le k \le n - 1\)) β€” the number of vertices of the tree and the maximum allowed diameter, respectively.Each of the next \(n-1\) lines contains a description of an edge: two integers \(v\) and \(u\) (\(1 \le v, u \le n\), \(v \neq u\)).The g...
Print a single integer β€” the number of valid sets of edges modulo \(998\,244\,353\).
In the first example the diameter of the given tree is already less than or equal to \(k\). Thus, you can choose any set of edges to remove and the resulting trees will have diameter less than or equal to \(k\). There are \(2^3\) sets, including the empty one.In the second example you have to remove the only edge. Othe...
Input: 4 3 1 2 1 3 1 4 | Output: 8
Expert
4
716
343
84
14
71
C
71C
C. Round Table Knights
1,600
dp; math; number theory
There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in...
The first line contains number n, which is the number of knights at the round table (3 ≀ n ≀ 105). The second line contains space-separated moods of all the n knights in the order of passing them around the table. ""1"" means that the knight is in a good mood an ""0"" means that he is in a bad mood.
Print ""YES"" without the quotes if the following month will turn out to be lucky. Otherwise, print ""NO"".
Input: 31 1 1 | Output: YES
Medium
3
835
300
107
0