Problem ID
stringlengths
2
6
Problem Description
stringlengths
0
7.52k
Rating
float64
800
3.5k
math
bool
2 classes
greedy
bool
2 classes
implementation
bool
2 classes
dp
bool
2 classes
data structures
bool
2 classes
constructive algorithms
bool
2 classes
brute force
bool
2 classes
binary search
bool
2 classes
sortings
bool
2 classes
graphs
bool
2 classes
__index_level_0__
int64
3
9.98k
2031C
Penchick loves two things: square numbers and Hong Kong-style BBQ buns! For his birthday, Kohane wants to combine them with a gift: $$$n$$$ BBQ buns arranged from left to right. There are $$$10^6$$$ available fillings of BBQ buns, numbered from $$$1$$$ to $$$10^6$$$. To ensure that Penchick would love this gift, Kohane has a few goals: No filling is used exactly once; that is, each filling must either not appear at all or appear at least twice. For any two buns $$$i$$$ and $$$j$$$ that have the same filling, the distance between them, which is $$$i-j$$$, must be a perfect square$$$^{ ext{βˆ—}}$$$. Help Kohane find a valid way to choose the filling of the buns, or determine if it is impossible to satisfy her goals! If there are multiple solutions, print any of them. $$$^{ ext{βˆ—}}$$$A positive integer $$$x$$$ is a perfect square if there exists a positive integer $$$y$$$ such that $$$x = y^2$$$. For example, $$$49$$$ and $$$1$$$ are perfect squares because $$$49 = 7^2$$$ and $$$1 = 1^2$$$ respectively. On the other hand, $$$5$$$ is not a perfect square as no integer squared equals $$$5$$$ Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 2cdot 10^5$$$). The description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1le nle 2cdot 10^5$$$)xa0β€” the number of BBQ buns. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, if no valid choice of fillings exists, output $$$-1$$$. Otherwise, output $$$n$$$ integers, where the $$$i$$$-th integer represents the filling of the $$$i$$$-th BBQ bun. If there are multiple solutions, print any of them. Example Output -1 1 2 3 6 10 2 7 6 10 1 7 3 Note In the first test case, the choice of fillings "1 1 1" is not allowed because buns $$$1$$$ and $$$3$$$ have the same filling, but are distance $$$2$$$ apart, which is not a perfect square. The choice of fillings "1 1 2" is also not allowed as filling $$$2$$$ is only used once. In the second test case, the solution is valid because no filling is used exactly once, and any two buns with the same filling are spaced at a distance equal to a perfect square. For example, buns $$$1$$$ and $$$10$$$ both have filling $$$1$$$ and are spaced at a distance of $$$9=3^2$$$. Similarly, buns $$$5$$$ and $$$9$$$ both have filling $$$10$$$ and are spaced at a distance of $$$4=2^2$$$.
1,300
true
false
false
false
false
true
false
false
false
false
78
277C
Two players play the following game. Initially, the players have a knife and a rectangular sheet of paper, divided into equal square grid cells of unit size. The players make moves in turn, the player who can't make a move loses. In one move, a player can take the knife and cut the paper along any segment of the grid line (not necessarily from border to border). The part of the paper, that touches the knife at least once, is considered cut. There is one limit not to turn the game into an infinite cycle: each move has to cut the paper, that is the knife has to touch the part of the paper that is not cut before. Obviously, the game ends when the entire sheet is cut into 1u2009Γ—u20091 blocks. During the game, the pieces of the sheet are not allowed to move. It is also prohibited to cut along the border. The coordinates of the ends of each cut must be integers. You are given an _n_u2009Γ—u2009_m_ piece of paper, somebody has already made _k_ cuts there. Your task is to determine who will win if the players start to play on this sheet. You can consider that both players play optimally well. If the first player wins, you also need to find the winning first move. Input The first line contains three integers _n_, _m_, _k_ (1u2009≀u2009_n_,u2009_m_u2009≀u2009109,u20090u2009≀u2009_k_u2009≀u2009105) β€” the sizes of the piece of paper and the number of cuts. Then follow _k_ lines, each containing 4 integers _xb__i_,u2009_yb__i_,u2009_xe__i_,u2009_ye__i_ (0u2009≀u2009_xb__i_,u2009_xe__i_u2009≀u2009_n_,u20090u2009≀u2009_yb__i_,u2009_ye__i_u2009≀u2009_m_) β€” the coordinates of the ends of the existing cuts. It is guaranteed that each cut has a non-zero length, is either vertical or horizontal and doesn't go along the sheet border. The cuts may intersect, overlap and even be the same. That is, it is not guaranteed that the cuts were obtained during any correct game. Output If the second player wins, print "SECOND". Otherwise, in the first line print "FIRST", and in the second line print any winning move of the first player (the coordinates of the cut ends, follow input format to print them). Examples Input 2 2 4 0 1 2 1 0 1 2 1 1 2 1 0 1 1 1 2
2,400
false
false
true
false
false
false
false
false
false
false
8,725
1330B
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 another sequence $$$a$$$ of length $$$l_1 + l_2$$$. First $$$l_1$$$ elements of $$$a$$$ is the permutation $$$p_1$$$ and next $$$l_2$$$ elements of $$$a$$$ is the permutation $$$p_2$$$. You are given the sequence $$$a$$$, and you need to find two permutations $$$p_1$$$ and $$$p_2$$$. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.) Input 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_i leq n-1$$$). The total sum of $$$n$$$ is less than $$$200,000$$$. Output 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$$$ into two permutations of length $$$l_1$$$ and $$$l_2$$$ ($$$p_1$$$ is the first $$$l_1$$$ elements of $$$a$$$, and $$$p_2$$$ is the last $$$l_2$$$ elements of $$$a$$$). You can print solutions in any order. Example 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 Note 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.
1,400
true
false
true
false
false
false
false
false
false
false
4,059
1561A
You have a permutation: an array $$$a = [a_1, a_2, ldots, a_n]$$$ of distinct integers from $$$1$$$ to $$$n$$$. The length of the permutation $$$n$$$ is odd. Consider the following algorithm of sorting the permutation in increasing order. A helper procedure of the algorithm, $$$f(i)$$$, takes a single argument $$$i$$$ ($$$1 le i le n-1$$$) and does the following. If $$$a_i > a_{i+1}$$$, the values of $$$a_i$$$ and $$$a_{i+1}$$$ are exchanged. Otherwise, the permutation doesn't change. The algorithm consists of iterations, numbered with consecutive integers starting with $$$1$$$. On the $$$i$$$-th iteration, the algorithm does the following: if $$$i$$$ is odd, call $$$f(1), f(3), ldots, f(n - 2)$$$; if $$$i$$$ is even, call $$$f(2), f(4), ldots, f(n - 1)$$$. It can be proven that after a finite number of iterations the permutation will be sorted in increasing order. After how many iterations will this happen for the first time? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 le n le 999$$$; $$$n$$$ is odd)xa0β€” the length of the permutation. The second line contains $$$n$$$ distinct integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$)xa0β€” the permutation itself. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$999$$$. Output For each test case print the number of iterations after which the permutation will become sorted in increasing order for the first time. If the given permutation is already sorted, print $$$0$$$. Example Input 3 3 3 2 1 7 4 5 7 1 3 2 6 5 1 2 3 4 5 Note In the first test case, the permutation will be changing as follows: after the $$$1$$$-st iteration: $$$[2, 3, 1]$$$; after the $$$2$$$-nd iteration: $$$[2, 1, 3]$$$; after the $$$3$$$-rd iteration: $$$[1, 2, 3]$$$. In the second test case, the permutation will be changing as follows: after the $$$1$$$-st iteration: $$$[4, 5, 1, 7, 2, 3, 6]$$$; after the $$$2$$$-nd iteration: $$$[4, 1, 5, 2, 7, 3, 6]$$$; after the $$$3$$$-rd iteration: $$$[1, 4, 2, 5, 3, 7, 6]$$$; after the $$$4$$$-th iteration: $$$[1, 2, 4, 3, 5, 6, 7]$$$; after the $$$5$$$-th iteration: $$$[1, 2, 3, 4, 5, 6, 7]$$$. In the third test case, the permutation is already sorted and the answer is $$$0$$$.
800
false
false
true
false
false
false
true
false
true
false
2,823
1461D
Mike received an array $$$a$$$ of length $$$n$$$ as a birthday present and decided to test how pretty it is. An array would pass the $$$i$$$-th prettiness test if there is a way to get an array with a sum of elements totaling $$$s_i$$$, using some number (possibly zero) of slicing operations. An array slicing operation is conducted in the following way: assume $$$mid = lfloorfrac{max(array) + min(array)}{2} floor$$$, where $$$max$$$ and $$$min$$$xa0β€” are functions that find the maximum and the minimum array elements. In other words, $$$mid$$$ is the sum of the maximum and the minimum element of $$$array$$$ divided by $$$2$$$ rounded down. Then the array is split into two parts $$$mathit{left}$$$ and $$$right$$$. The $$$mathit{left}$$$ array contains all elements which are less than or equal $$$mid$$$, and the $$$right$$$ array contains all elements which are greater than $$$mid$$$. Elements in $$$mathit{left}$$$ and $$$right$$$ keep their relative order from $$$array$$$. During the third step we choose which of the $$$mathit{left}$$$ and $$$right$$$ arrays we want to keep. The chosen array replaces the current one and the other is permanently discarded. You need to help Mike find out the results of $$$q$$$ prettiness tests. Note that you test the prettiness of the array $$$a$$$, so you start each prettiness test with the primordial (initial) array $$$a$$$. Thus, the first slice (if required) is always performed on the array $$$a$$$. Input Each test contains one or more test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). The first line of each test case contains two integers $$$n$$$ and $$$q$$$ $$$(1 le n, q le 10^5)$$$xa0β€” the length of the array $$$a$$$ and the total number of prettiness tests. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$ $$$(1 le a_i le 10^6)$$$xa0β€” the contents of the array $$$a$$$. Next $$$q$$$ lines of each test case contain a single integer $$$s_i$$$ $$$(1 le s_i le 10^9)$$$xa0β€” the sum of elements which Mike wants to get in the $$$i$$$-th test. It is guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ does not exceed $$$10^5$$$ ($$$sum n, sum q le 10^5$$$). Output Print $$$q$$$ lines, each containing either a "Yes" if the corresponding prettiness test is passed and "No" in the opposite case. Example Input 2 5 5 1 2 3 4 5 1 8 9 12 6 5 5 3 1 3 1 3 1 2 3 9 11 Output Yes No Yes No Yes No Yes No Yes Yes Note Explanation of the first test case: 1. We can get an array with the sum $$$s_1 = 1$$$ in the following way:1.1 $$$a = [1, 2, 3, 4, 5]$$$, $$$mid = frac{1+5}{2} = 3$$$, $$$mathit{left} = [1, 2, 3]$$$, $$$right = [4, 5]$$$. We choose to keep the $$$mathit{left}$$$ array. 1.2 $$$a = [1, 2, 3]$$$, $$$mid = frac{1+3}{2} = 2$$$, $$$mathit{left} = [1, 2]$$$, $$$right = [3]$$$. We choose to keep the $$$mathit{left}$$$ array. 1.3 $$$a = [1, 2]$$$, $$$mid = frac{1+2}{2} = 1$$$, $$$mathit{left} = [1]$$$, $$$right = [2]$$$. We choose to keep the $$$mathit{left}$$$ array with the sum equalling $$$1$$$. 2. It can be demonstrated that an array with the sum $$$s_2 = 8$$$ is impossible to generate. 3. An array with the sum $$$s_3 = 9$$$ can be generated in the following way:3.1 $$$a = [1, 2, 3, 4, 5]$$$, $$$mid = frac{1+5}{2} = 3$$$, $$$mathit{left} = [1, 2, 3]$$$, $$$right = [4, 5]$$$. We choose to keep the $$$right$$$ array with the sum equalling $$$9$$$. 4. It can be demonstrated that an array with the sum $$$s_4 = 12$$$ is impossible to generate. 5. We can get an array with the sum $$$s_5 = 6$$$ in the following way:5.1 $$$a = [1, 2, 3, 4, 5]$$$, $$$mid = frac{1+5}{2} = 3$$$, $$$mathit{left} = [1, 2, 3]$$$, $$$right = [4, 5]$$$. We choose to keep the $$$mathit{left}$$$ with the sum equalling $$$6$$$. Explanation of the second test case: 1. It can be demonstrated that an array with the sum $$$s_1 = 1$$$ is imposssible to generate. 2. We can get an array with the sum $$$s_2 = 2$$$ in the following way:2.1 $$$a = [3, 1, 3, 1, 3]$$$, $$$mid = frac{1+3}{2} = 2$$$, $$$mathit{left} = [1, 1]$$$, $$$right = [3, 3, 3]$$$. We choose to keep the $$$mathit{left}$$$ array with the sum equalling $$$2$$$. 3. It can be demonstrated that an array with the sum $$$s_3 = 3$$$ is imposssible to generate. 4. We can get an array with the sum $$$s_4 = 9$$$ in the following way:4.1 $$$a = [3, 1, 3, 1, 3]$$$, $$$mid = frac{1+3}{2} = 2$$$, $$$mathit{left} = [1, 1]$$$, $$$right = [3, 3, 3]$$$. We choose to keep the $$$right$$$ array with the sum equalling $$$9$$$. 5. We can get an array with the sum $$$s_5 = 11$$$ with zero slicing operations, because array sum is equal to $$$11$$$.
1,600
false
false
true
false
true
false
true
true
true
false
3,383
1681C
You are given two arrays $$$a$$$ and $$$b$$$, both consisting of $$$n$$$ integers. In one move, you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 le i, j le n$$$; $$$i eq j$$$) and swap $$$a_i$$$ with $$$a_j$$$ and $$$b_i$$$ with $$$b_j$$$. You have to perform the swap in both arrays. You are allowed to perform at most $$$10^4$$$ moves (possibly, zero). Can you make both arrays sorted in a non-decreasing order at the end? If you can, print any sequence of moves that makes both arrays sorted. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0β€” the number of testcases. The first line of each testcase contains a single integer $$$n$$$ ($$$2 le n le 100$$$)xa0β€” the number of elements in both arrays. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$)xa0β€” the first array. The third line contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$1 le b_i le n$$$)xa0β€” the second array. Output For each testcase, print the answer. If it's impossible to make both arrays sorted in a non-decreasing order in at most $$$10^4$$$ moves, print -1. Otherwise, first, print the number of moves $$$k$$$ $$$(0 le k le 10^4)$$$. Then print $$$i$$$ and $$$j$$$ for each move $$$(1 le i, j le n$$$; $$$i eq j)$$$. If there are multiple answers, then print any of them. You don't have to minimize the number of moves. Example Input 3 2 1 2 1 2 2 2 1 1 2 4 2 3 1 2 2 3 2 3
1,200
false
false
true
false
false
false
false
false
true
false
2,183
1971H
Bob has a grid with $$$3$$$ rows and $$$n$$$ columns, each of which contains either $$$a_i$$$ or $$$-a_i$$$ for some integer $$$1 leq i leq n$$$. For example, one possible grid for $$$n=4$$$ is shown below: $$$$$$begin{bmatrix} a_1 & -a_2 & -a_3 & -a_2 -a_4 & a_4 & -a_1 & -a_3 a_1 & a_2 & -a_2 & a_4 end{bmatrix}$$$$$$ Alice and Bob play a game as follows: Bob shows Alice his grid. Alice gives Bob an array $$$a_1, a_2, dots, a_n$$$ of her choosing, whose elements are all $$$mathbf{-1}$$$ or $$$mathbf{1}$$$. Bob substitutes these values into his grid to make a grid of $$$-1$$$s and $$$1$$$s. Bob sorts the elements of each column in non-decreasing order. Alice wins if all the elements in the middle row are $$$1$$$; otherwise, Bob wins. For example, suppose Alice gives Bob the array $$$[1, -1, -1, 1]$$$ for the grid above. Then the following will happen (colors are added for clarity): $$$$$$begin{bmatrix} color{red}{a_1} & color{green}{-a_2} & color{blue}{-a_3} & color{green}{-a_2} -a_4 & a_4 & color{red}{-a_1} & color{blue}{-a_3} color{red}{a_1} & color{green}{a_2} & color{green}{-a_2} & a_4 end{bmatrix} xrightarrow{[color{red}{1},color{green}{-1},color{blue}{-1},1]} begin{bmatrix} color{red}{1} & color{green}{1} & color{blue}{1} & color{green}{1} -1 & 1 & color{red}{-1} & color{blue}{1} color{red}{1} & color{green}{-1} & color{green}{1} & 1 end{bmatrix} xrightarrow{ ext{sort each column}} begin{bmatrix} -1 & -1 & -1 & 1 mathbf{1} & mathbf{1} & mathbf{1} & mathbf{1} 1 & 1 & 1 & 1 end{bmatrix},. $$$$$$ Since the middle row is all $$$1$$$, Alice wins. Given Bob's grid, determine whether or not Alice can choose the array $$$a$$$ to win the game. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0β€” the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 leq n leq 500$$$)xa0β€” the number of columns of Bob's grid. The next three lines each contain $$$n$$$ integers, the $$$i$$$-th of which contains $$$g_{i,1}, g_{i,2}, dots, g_{i,n}$$$ ($$$-n leq g_{i,j} leq n$$$, $$$g_{i,j} eq 0$$$), representing Bob's grid. If cell $$$x > 0$$$ is in the input, that cell in Bob's grid should contain $$$a_x$$$; if $$$x < 0$$$ is in the input, that cell in Bob's grid should contain $$$-a_{-x}$$$. See the sample input and notes for a better understanding. Output For each test case, output "YES" (without quotes) if Alice can win, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", and "Yes" will be recognized as a positive response). Example Input 4 4 1 -2 -3 -2 -4 4 -1 -3 1 2 -2 4 2 1 2 -1 -2 2 -2 5 1 2 3 4 5 -2 3 -4 -5 -1 3 -5 1 2 2 6 1 3 -6 2 5 2 1 3 -2 -3 -6 -5 -2 -1 -3 2 3 1 Note The first test case is described in the statement. In the second test case, Bob's grid is as follows: $$$$$$begin{bmatrix} a_1 & a_2 -a_1 & -a_2 a_2 & -a_2 end{bmatrix}$$$$$$ For the last column to have $$$1$$$ in the middle row when sorted, Alice must pick $$$a_2 = -1$$$. However, it is then impossible to choose $$$a_1$$$ such that the first column has $$$1$$$ in the middle when sorted. Thus, Alice cannot win. In the third test case, Alice can pick $$$a = [1,1,1,1,1]$$$.
2,100
false
false
false
false
false
false
false
false
false
true
458
169B
Problem - 169B - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags greedy *1100 No tag edit access β†’ Contest materials of sequence _s_ means that you can choose an arbitrary position _i_ (1u2009≀u2009_i_u2009≀u2009_n_) in _a_ and replace the digit in the chosen position _i_ with _s__j_. Each element in the sequence _s_ can participate in no more than one replacing operation. Your task is to perform such sequence of replacements, that the given number _a_ gets maximum value. You are allowed to use not all elements from _s_. Input The first line contains positive integer _a_. Its length _n_ is positive and doesn't exceed 105. The second line contains sequence of digits _s_. Its length _m_ is positive and doesn't exceed 105. The digits in the sequence _s_ are written consecutively without any separators. The given number _a_ doesn't contain leading zeroes. Output Print the maximum value that can be obtained from _a_ after a series of replacements. You are allowed to use not all elements from _s_. The printed number shouldn't contain any leading zeroes. Examples Input 1024 010 Output 1124 Input 987 1234567 Output 987
1,100
false
true
false
false
false
false
false
false
false
false
9,187
1686B
For an array $$$[b_1, b_2, ldots, b_m]$$$ define its number of inversions as the number of pairs $$$(i, j)$$$ of integers such that $$$1 le i < j le m$$$ and $$$b_i>b_j$$$. Let's call array $$$b$$$ odd if its number of inversions is odd. For example, array $$$[4, 2, 7]$$$ is odd, as its number of inversions is $$$1$$$, while array $$$[2, 1, 4, 3]$$$ isn't, as its number of inversions is $$$2$$$. You are given a permutation $$$[p_1, p_2, ldots, p_n]$$$ of integers from $$$1$$$ to $$$n$$$ (each of them appears exactly once in the permutation). You want to split it into several consecutive subarrays (maybe just one), so that the number of the odd subarrays among them is as large as possible. What largest number of these subarrays may be odd? Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$) xa0β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$) xa0β€” the size of the permutation. The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le n$$$, all $$$p_i$$$ are distinct) xa0β€” the elements of the permutation. The sum of $$$n$$$ over all test cases doesn't exceed $$$2cdot 10^5$$$. Output For each test case output a single integer xa0β€” the largest possible number of odd subarrays that you can get after splitting the permutation into several consecutive subarrays. Example Input 5 3 1 2 3 4 4 3 2 1 2 1 2 2 2 1 6 4 5 6 1 2 3 Note In the first and third test cases, no matter how we split our permutation, there won't be any odd subarrays. In the second test case, we can split our permutation into subarrays $$$[4, 3], [2, 1]$$$, both of which are odd since their numbers of inversions are $$$1$$$. In the fourth test case, we can split our permutation into a single subarray $$$[2, 1]$$$, which is odd. In the fifth test case, we can split our permutation into subarrays $$$[4, 5], [6, 1, 2, 3]$$$. The first subarray has $$$0$$$ inversions, and the second has $$$3$$$, so it is odd.
800
false
true
false
true
false
false
false
false
false
false
2,158
1585E
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 the root. Each question is defined by three integers $$$v$$$, $$$l$$$, and $$$k$$$. To get the answer to the question, you need to perform the following steps: First, write down the sequence of all integers written on the shortest path from the vertex $$$v$$$ to the root (including those written in the $$$v$$$ and the root). Count the number of times each integer occurs. Remove all integers with less than $$$l$$$ occurrences. Replace the sequence, removing all duplicates and ordering the elements by the number of occurrences in the original list in increasing order. In case of a tie, you can choose the order of these elements arbitrary. The answer to the question is the $$$k$$$-th number in the remaining sequence. Note that the answer is not always uniquely determined, because there could be several orderings. Also, it is possible that the length of the sequence on this step is less than $$$k$$$, in this case the answer is $$$-1$$$. For example, if the sequence of integers on the path from $$$v$$$ to the root is $$$[2, 2, 1, 7, 1, 1, 4, 4, 4, 4]$$$, $$$l = 2$$$ and $$$k = 2$$$, then the answer is $$$1$$$. Please answer all questions about the tree. Input 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. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq n$$$), where $$$a_i$$$ is the number written on the $$$i$$$-th vertex. The third line contains $$$n-1$$$ integers $$$p_2, p_3, ldots, p_n$$$ ($$$1 leq p_i leq n$$$), where $$$p_i$$$ is the parent of node $$$i$$$. It's guaranteed that the values $$$p$$$ define a correct tree. Each of the next $$$q$$$ lines contains three integers $$$v$$$, $$$l$$$, $$$k$$$ ($$$1 leq v, l, k leq n$$$) β€” descriptions of questions. It is guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ over all test cases do not exceed $$$10^6$$$. Output For each question of each test case print the answer to the question. In case of multiple answers, print any. Example 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
2,400
false
false
false
false
true
false
false
false
false
false
2,698
441E
Problem - 441E - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags bitmasks dp math probabilities *2400 No tag edit access β†’ Contest materials ") ") { rnd =
2,400
true
false
false
true
false
false
false
false
false
false
8,071
1815A
To thank Ian, Mary gifted an array $$$a$$$ of length $$$n$$$ to Ian. To make himself look smart, he wants to make the array in non-decreasing order by doing the following finitely many times: he chooses two adjacent elements $$$a_i$$$ and $$$a_{i+1}$$$ ($$$1le ile n-1$$$), and increases both of them by $$$1$$$ or decreases both of them by $$$1$$$. Note that, the elements of the array can become negative. As a smart person, you notice that, there are some arrays such that Ian cannot make it become non-decreasing order! Therefore, you decide to write a program to determine if it is possible to make the array in non-decreasing order. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$) β€” the number of test cases. The description of test cases follows. The first line of each test case consists of a single integer $$$n$$$ ($$$2le nle 3cdot10^5$$$) β€” the number of elements in the array. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$1le a_ile 10^9$$$) β€” the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3cdot10^5$$$. Output For each test case, output "YES" if there exists a sequence of operations which make the array non-decreasing, else output "NO". You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as positive answer). Example Input 5 3 1 3 2 2 2 1 4 1 3 5 7 4 2 1 4 3 5 5 4 3 2 1 Note For the first test case, we can increase $$$a_2$$$ and $$$a_3$$$ both by $$$1$$$. The array is now $$$[1, 4, 3]$$$. Then we can decrease $$$a_1$$$ and $$$a_2$$$ both by $$$1$$$. The array is now $$$[0, 3, 3]$$$, which is sorted in non-decreasing order. So the answer is "YES". For the second test case, no matter how Ian perform the operations, $$$a_1$$$ will always be larger than $$$a_2$$$. So the answer is "NO" and Ian cannot pretend to be smart. For the third test case, the array is already in non-decreasing order, so Ian does not need to do anything.
1,300
true
true
false
false
false
false
false
false
false
false
1,381
842D
Problem - 842D - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags binary search data structures *2000 No tag edit access β†’ Contest materials ") ") u2009=u20092 and _mex_(
2,000
false
false
false
false
true
false
false
true
false
false
6,393
451D
Problem - 451D - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags math *2000 No tag edit access β†’ Contest materials . Each character of the string will be either 'a' or 'b'. Output Print two space-separated integers: the number of good substrings of even length and the number of good substrings of odd length. Examples Input bb Output 1 2 Input baab Output 2 4 Input babb Output 2 5 Input babaa Output 2 7 Note In example 1, there are three good substrings ("b", "b", and "bb"). One of them has even length and two of them have odd length. In example 2, there are six good substrings (i.e. "b", "a", "a", "b", "aa", "baab"). Two of them have even length and four of them have odd length. In example 3, there are seven good substrings (i.e. "b", "a", "b", "b", "bb", "bab", "babb"). Two of them have even length and five of them have odd length. Definitions A substring _s_
2,000
true
false
false
false
false
false
false
false
false
false
8,034
1404E
A brick is defined as a rectangle with integer side lengths with either width $$$1$$$ or height $$$1$$$ (or both). There is an $$$n imes m$$$ grid, and each cell is colored either black or white. A tiling is a way to place bricks onto the grid such that each black cell is covered by exactly one brick, and each white cell is not covered by any brick. In other words, bricks are placed on black cells only, cover all black cells, and no two bricks overlap. An example tiling of the first test case using $$$5$$$ bricks. It is possible to do better, using only $$$4$$$ bricks. What is the minimum number of bricks required to make a valid tiling? Input The first line contains two integers $$$n$$$, $$$m$$$ ($$$1le n, mle 200$$$) β€” the number of rows and columns, respectively. The next $$$n$$$ lines describe the grid. The $$$i$$$-th of these lines contains a string of length $$$m$$$, where the $$$j$$$-th character denotes the color of the cell in row $$$i$$$, column $$$j$$$. A black cell is given by "#", and a white cell is given by ".". It is guaranteed that there is at least one black cell. Output Output a single integer, the minimum number of bricks required. Examples Input 6 6 ###### ##.... ###### ##...# ##...# ###### Input 10 8 ####..## #..#.##. #..#.### ####.#.# ....#### .###.### ###.#..# ######## ###..### .##.###. Note The first test case can be tiled with $$$4$$$ bricks placed vertically. The third test case can be tiled with $$$18$$$ bricks like this:
2,800
false
false
false
false
false
false
false
false
false
true
3,649
1814D
You've got a job in a game studio that developed an online shooter, and your first big task is to help to balance weapons. The game has $$$n$$$ weapons: the $$$i$$$-th gun has an integer fire rate $$$f_i$$$ and an integer damage per bullet $$$d_i$$$. The $$$i$$$-th gun's total firepower is equal to $$$p_i = f_i cdot d_i$$$. You have to modify the values $$$d_i$$$ of some guns in such a way that the new values $$$d_i$$$ will still be integers, and the firepower of all guns will become balanced. Given an integer $$$k$$$, the guns are said to be balanced if $$$maxlimits_{1 le i le n}{p_i} - minlimits_{1 le i le n}{p_i} le k$$$. Since gamers that play your game don't like big changes, you need to change the values $$$d_i$$$ for the minimum possible number of guns. What is the minimum number of guns for which you have to change these values to make the guns balanced? Note that the new values $$$d_i$$$ must be integers greater than $$$0$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β€” the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 3000$$$; $$$0 le k le 1500$$$)xa0β€” the number of guns to balance, and the maximum allowed gap between the most and the least powerful weapons. The second line contains $$$n$$$ integers $$$f_1, f_2, dots, f_n$$$ ($$$1 le f_i le 2000$$$), where $$$f_i$$$ is the fire rate of the $$$i$$$-th gun. The third line contains $$$n$$$ integers $$$d_1, d_2, dots, d_n$$$ ($$$1 le d_i le 10^9$$$), where $$$d_i$$$ is the damage per bullet of the $$$i$$$-th gun. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$3000$$$. Output For each test case, print the minimum number of guns which damage $$$d_i$$$ you have to change in order to make the guns balanced. Note that the new values $$$d_i$$$ must be integers greater than $$$0$$$. Example Input 5 4 2 6 3 13 7 1 2 1 2 3 2 100 101 102 100 99 98 5 0 1 12 4 4 3 12 1 3 3 4 2 50 1000 10 1000000000 1 3 5 1 19 11 49 4 72 Note In the first test case, you can set $$$d_1 = 2$$$ and $$$d_2 = 4$$$. You'll get an array $$$d = [2, 4, 1, 2]$$$, and the values of firepower will be $$$p = [12, 12, 13, 14]$$$. The guns are balanced, since $$$14 - 12 le 2$$$. In the second test case, you have to change the value $$$d_i$$$ for all three guns. For example, you can set $$$d = [5151, 5100, 5050]$$$. In the third test case, all guns are already balanced, so you don't have to change anything.
2,500
true
false
false
false
true
false
true
true
false
false
1,384
2001E2
This is the hard version of the problem. The difference between the two versions is the definition of deterministic max-heap, time limit, and constraints on $$$n$$$ and $$$t$$$. You can make hacks only if both versions of the problem are solved. Consider a perfect binary tree with size $$$2^n - 1$$$, with nodes numbered from $$$1$$$ to $$$2^n-1$$$ and rooted at $$$1$$$. For each vertex $$$v$$$ ($$$1 le v le 2^{n - 1} - 1$$$), vertex $$$2v$$$ is its left child and vertex $$$2v + 1$$$ is its right child. Each node $$$v$$$ also has a value $$$a_v$$$ assigned to it. Define the operation $$$mathrm{pop}$$$ as follows: 1. initialize variable $$$v$$$ as $$$1$$$; 2. repeat the following process until vertex $$$v$$$ is a leaf (i.e. until $$$2^{n - 1} le v le 2^n - 1$$$); 1. among the children of $$$v$$$, choose the one with the larger value on it and denote such vertex as $$$x$$$; if the values on them are equal (i.e. $$$a_{2v} = a_{2v + 1}$$$), you can choose any of them; 2. assign $$$a_x$$$ to $$$a_v$$$ (i.e. $$$a_v := a_x$$$); 3. assign $$$x$$$ to $$$v$$$ (i.e. $$$v := x$$$); 3. assign $$$-1$$$ to $$$a_v$$$ (i.e. $$$a_v := -1$$$). Then we say the $$$mathrm{pop}$$$ operation is deterministic if there is a unique way to do such operation. In other words, $$$a_{2v} eq a_{2v + 1}$$$ would hold whenever choosing between them. A binary tree is called a max-heap if for every vertex $$$v$$$ ($$$1 le v le 2^{n - 1} - 1$$$), both $$$a_v ge a_{2v}$$$ and $$$a_v ge a_{2v + 1}$$$ hold. A max-heap is deterministic if the $$$mathrm{pop}$$$ operation is deterministic to the heap when we do it for the first and the second time. Initially, $$$a_v := 0$$$ for every vertex $$$v$$$ ($$$1 le v le 2^n - 1$$$), and your goal is to count the number of different deterministic max-heaps produced by applying the following operation $$$mathrm{add}$$$ exactly $$$k$$$ times: Choose an integer $$$v$$$ ($$$1 le v le 2^n - 1$$$) and, for every vertex $$$x$$$ on the path between $$$1$$$ and $$$v$$$, add $$$1$$$ to $$$a_x$$$. Two heaps are considered different if there is a node which has different values in the heaps. Since the answer might be large, print it modulo $$$p$$$. Input 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 three integers $$$n, k, p$$$ ($$$2 le n le 100$$$, $$$1 le k le 500$$$, $$$10^8 le p le 10^9$$$, $$$p$$$ is a prime). It is guaranteed that the sum of $$$n$$$ does not exceed $$$100$$$ and the sum of $$$k$$$ over all test cases does not exceed $$$500$$$. Output For each test case, output a single line containing an integer: the number of different deterministic max-heaps produced by applying the aforementioned operation $$$mathrm{add}$$$ exactly $$$k$$$ times, modulo $$$p$$$. Examples Input 6 2 1 998244353 3 2 998244853 3 3 998244353 3 4 100000037 4 2 100000039 4 3 100000037 Input 2 87 63 100000037 13 437 100000039 Note For the first testcase, if we choose $$$v = 1$$$ and do the operation, we would have $$$a = [1, 0, 0]$$$, and since $$$a_2 = a_3$$$, we can choose either of them when doing the first $$$mathrm{pop}$$$ operation, so such heap is not a deterministic max-heap. And if we choose $$$v = 2$$$, we would have $$$a = [1, 1, 0]$$$, during the first $$$mathrm{pop}$$$, the following would happen: initialize $$$v$$$ as $$$1$$$ since $$$a_{2v} > a_{2v + 1}$$$, choose $$$2v$$$ as $$$x$$$, then $$$x = 2$$$ assign $$$a_x$$$ to $$$a_v$$$, then $$$a = [1, 1, 0]$$$ assign $$$x$$$ to $$$v$$$, then $$$v = 2$$$ since $$$v$$$ is a leaf, assign $$$-1$$$ to $$$a_v$$$, then $$$a = [1, -1, 0]$$$ And during the second $$$mathrm{pop}$$$, the following would happen: initialize $$$v$$$ as $$$1$$$ since $$$a_{2v} < a_{2v + 1}$$$, choose $$$2v + 1$$$ as $$$x$$$, then $$$x = 3$$$ assign $$$a_x$$$ to $$$a_v$$$, then $$$a = [0, -1, 0]$$$ assign $$$x$$$ to $$$v$$$, then $$$v = 3$$$ since $$$v$$$ is a leaf, assign $$$-1$$$ to $$$a_v$$$, then $$$a = [0, -1, -1]$$$ Since both the first and the second $$$mathrm{pop}$$$ operation are deterministic, this is a deterministic max-heap. Also, if we choose $$$v = 3$$$, $$$a$$$ would be a deterministic max-heap, so the answer is $$$2$$$.
2,900
false
false
false
true
false
false
false
false
false
false
250
204A
Problem - 204A - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags binary search combinatorics dp *1500 No tag edit access β†’ Contest materials . The Little Elephant has to find the number of such integers _x_ (_l_u2009≀u2009_x_u2009≀u2009_r_), that the first digit of integer _x_ equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not. Help him and count the number of described numbers _x_ for a given pair _l_ and _r_. Input The single line contains a pair of integers _l_ and _r_ (1u2009≀u2009_l_u2009≀u2009_r_u2009≀u20091018) β€” the boundaries of the interval. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier. Output On a single line print a single integer β€” the answer to the problem. Examples Input 2 47 Output 12 Input 47 1024 Output 98 Note In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
1,500
false
false
false
true
false
false
false
true
false
false
9,027
1575G
Problem - 1575G - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags math number theory *2200 No tag edit access β†’ Contest materials ") cdot gcd(i, j)}}$$$$$$ where $$$gcd(x, y)$$$ denotes the greatest common divisor (GCD) of integers $$$x$$$ and $$$y$$$. In other words, the prettiness value of an array $$$a$$$ is the total sum of $$$gcd(a_i, a_j) cdot gcd(i, j)$$$ for all pairs $$$(i, j)$$$. Help Mr. Chanek find the prettiness value of $$$a$$$, and output the result modulo $$$10^9 + 7$$$! Input The first line contains an integer $$$n$$$ ($$$2 leq n leq 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^5$$$). Output Output an integer denoting the prettiness value of $$$a$$$ modulo $$$10^9 + 7$$$. Example Input 5 3 6 2 1 4 Output 77
2,200
true
false
false
false
false
false
false
false
false
false
2,762
1148F
You are given $$$n$$$ objects. Each object has two integer properties: $$$val_i$$$xa0β€” its pricexa0β€” and $$$mask_i$$$. It is guaranteed that the sum of all prices is initially non-zero. You want to select a positive integer $$$s$$$. All objects will be modified after that. The $$$i$$$-th object will be modified using the following procedure: Consider $$$mask_i$$$ and $$$s$$$ in binary notation, Compute the , If ($$$s ,&, mask_i$$$) contains an odd number of ones, replace the $$$val_i$$$ with $$$-val_i$$$. Otherwise do nothing with the $$$i$$$-th object. You need to find such an integer $$$s$$$ that when the modification above is done the sum of all prices changes sign (if it was negative, it should become positive, and vice-versa; it is not allowed for it to become zero). The absolute value of the sum can be arbitrary. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 3 cdot 10^5$$$)xa0β€” the number of objects. The $$$i$$$-th of next $$$n$$$ lines contains integers $$$val_i$$$ and $$$mask_i$$$ ($$$-10^9 leq val_i leq 10^9$$$, $$$1 le mask_i le 2^{62} - 1$$$)xa0β€” the price of the object and its mask. It is guaranteed that the sum of $$$val_i$$$ is initially non-zero. Output Print an integer $$$s$$$ ($$$1 le s le 2^{62} - 1$$$), such that if we modify the objects as described above, the sign of the sum of $$$val_i$$$ changes its sign. If there are multiple such $$$s$$$, print any of them. One can show that there is always at least one valid $$$s$$$. Examples Input 5 17 206 -6 117 -2 151 9 93 6 117 Note In the first test sample all objects will change their prices except for the object with mask $$$151$$$. So their total sum will change its sign: initially $$$24$$$, after modificationsxa0β€” $$$-28$$$. In the second test sample the only object will change its price. So the total sum will change its sign.
2,700
false
false
false
false
false
true
false
false
false
false
4,972
1580C
Kawasiro Nitori is excellent in engineering. Thus she has been appointed to help maintain trains. There are $$$n$$$ models of trains, and Nitori's department will only have at most one train of each model at any moment. In the beginning, there are no trains, at each of the following $$$m$$$ days, one train will be added, or one train will be removed. When a train of model $$$i$$$ is added at day $$$t$$$, it works for $$$x_i$$$ days (day $$$t$$$ inclusive), then it is in maintenance for $$$y_i$$$ days, then in work for $$$x_i$$$ days again, and so on until it is removed. In order to make management easier, Nitori wants you to help her calculate how many trains are in maintenance in each day. On a day a train is removed, it is not counted as in maintenance. Input The first line contains two integers $$$n$$$, $$$m$$$ ($$$1 le n,m le 2 cdot 10^5$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i,y_i$$$ ($$$1 le x_i,y_i le 10^9$$$). Each of the next $$$m$$$ lines contains two integers $$$op$$$, $$$k$$$ ($$$1 le k le n$$$, $$$op = 1$$$ or $$$op = 2$$$). If $$$op=1$$$, it means this day's a train of model $$$k$$$ is added, otherwise the train of model $$$k$$$ is removed. It is guaranteed that when a train of model $$$x$$$ is added, there is no train of the same model in the department, and when a train of model $$$x$$$ is removed, there is such a train in the department. Output Print $$$m$$$ lines, The $$$i$$$-th of these lines contains one integers, denoting the number of trains in maintenance in the $$$i$$$-th day. Examples Input 3 4 10 15 12 10 1 1 1 3 1 1 2 1 2 3 Input 5 4 1 1 10000000 100000000 998244353 1 2 1 1 2 1 5 2 5 1 5 1 1 Note Consider the first example: The first day: Nitori adds a train of model $$$3$$$. Only a train of model $$$3$$$ is running and no train is in maintenance. The second day: Nitori adds a train of model $$$1$$$. A train of model $$$1$$$ is running and a train of model $$$3$$$ is in maintenance. The third day: Nitori removes a train of model $$$1$$$. The situation is the same as the first day. The fourth day: Nitori removes a train of model $$$3$$$. There are no trains at all.
2,200
false
false
true
false
true
false
true
false
false
false
2,731
1437A
You got a job as a marketer in a pet shop, and your current task is to boost sales of cat food. One of the strategies is to sell cans of food in packs with discounts. Suppose you decided to sell packs with $$$a$$$ cans in a pack with a discount and some customer wants to buy $$$x$$$ cans of cat food. Then he follows a greedy strategy: he buys $$$leftlfloor frac{x}{a} ight floor$$$ packs with a discount; then he wants to buy the remaining $$$(x bmod a)$$$ cans one by one. $$$leftlfloor frac{x}{a} ight floor$$$ is $$$x$$$ divided by $$$a$$$ rounded down, $$$x bmod a$$$ is the remainer of $$$x$$$ divided by $$$a$$$. But customers are greedy in general, so if the customer wants to buy $$$(x bmod a)$$$ cans one by one and it happens that $$$(x bmod a) ge frac{a}{2}$$$ he decides to buy the whole pack of $$$a$$$ cans (instead of buying $$$(x bmod a)$$$ cans). It makes you, as a marketer, happy since the customer bought more than he wanted initially. You know that each of the customers that come to your shop can buy any number of cans from $$$l$$$ to $$$r$$$ inclusive. Can you choose such size of pack $$$a$$$ that each customer buys more cans than they wanted initially? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β€” the number of test cases. The first and only line of each test case contains two integers $$$l$$$ and $$$r$$$ ($$$1 le l le r le 10^9$$$)xa0β€” the range of the number of cans customers can buy. Output For each test case, print YES if you can choose such size of pack $$$a$$$ that each customer buys more cans than they wanted initially. Otherwise, print NO. You can print each character in any case. Note In the first test case, you can take, for example, $$$a = 5$$$ as the size of the pack. Then if a customer wants to buy $$$3$$$ cans, he'll buy $$$5$$$ instead ($$$3 bmod 5 = 3$$$, $$$frac{5}{2} = 2.5$$$). The one who wants $$$4$$$ cans will also buy $$$5$$$ cans. In the second test case, there is no way to choose $$$a$$$. In the third test case, you can take, for example, $$$a = 80$$$.
800
true
true
false
false
false
true
true
false
false
false
3,485
1458F
Problem - 1458F - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags data structures trees *3500 No tag edit access β†’ Contest materials ") $$$ be the number of edges in the unique simple path connecting vertices $$$u$$$ and $$$v$$$. Let $$$mathrm{diam}(l, r) = max mathrm{dist}(u, v)$$$ over all pairs $$$u, v$$$ such that $$$l leq u, v leq r$$$. Compute $$$sum_{1 leq l leq r leq n} mathrm{diam}(l, r)$$$. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 10^5$$$)xa0β€” the number of vertices in the tree. The next $$$n - 1$$$ lines describe the tree edges. Each of these lines contains two integers $$$u, v$$$ ($$$1 leq u, v leq n$$$)xa0β€” endpoint indices of the respective tree edge. It is guaranteed that the edge list indeed describes a tree. Output Print a single integerxa0β€” $$$sum_{1 leq l leq r leq n} mathrm{diam}(l, r)$$$. Examples Input 4 1 2 2 4 3 2 Output 10 Input 10 1 8 2 9 5 6 4 8 4 2 7 9 3 6 10 4 3 9 Output 224
3,500
false
false
false
false
true
false
false
false
false
false
3,389
1292A
NEKO#ΦωΦ has just got a new maze game on her PC! The game's main puzzle is a maze, in the forms of a $$$2 imes n$$$ rectangle grid. NEKO's task is to lead a Nekomimi girl from cell $$$(1, 1)$$$ to the gate at $$$(2, n)$$$ and escape the maze. The girl can only move between cells sharing a common side. However, at some moments during the game, some cells may change their state: either from normal ground to lava (which forbids movement into that cell), or vice versa (which makes that cell passable again). Initially all cells are of the ground type. After hours of streaming, NEKO finally figured out there are only $$$q$$$ such moments: the $$$i$$$-th moment toggles the state of cell $$$(r_i, c_i)$$$ (either from ground to lava or vice versa). Knowing this, NEKO wonders, after each of the $$$q$$$ moments, whether it is still possible to move from cell $$$(1, 1)$$$ to cell $$$(2, n)$$$ without going through any lava cells. Although NEKO is a great streamer and gamer, she still can't get through quizzes and problems requiring large amount of Brain Power. Can you help her? Input The first line contains integers $$$n$$$, $$$q$$$ ($$$2 le n le 10^5$$$, $$$1 le q le 10^5$$$). The $$$i$$$-th of $$$q$$$ following lines contains two integers $$$r_i$$$, $$$c_i$$$ ($$$1 le r_i le 2$$$, $$$1 le c_i le n$$$), denoting the coordinates of the cell to be flipped at the $$$i$$$-th moment. It is guaranteed that cells $$$(1, 1)$$$ and $$$(2, n)$$$ never appear in the query list. Output For each moment, if it is possible to travel from cell $$$(1, 1)$$$ to cell $$$(2, n)$$$, print "Yes", otherwise print "No". There should be exactly $$$q$$$ answers, one after every update. You can print the words in any case (either lowercase, uppercase or mixed).
1,400
false
false
true
false
true
false
false
false
false
false
4,237
1541B
You are given an array $$$a_1, a_2, dots, a_n$$$ consisting of $$$n$$$ distinct integers. Count the number of pairs of indices $$$(i, j)$$$ such that $$$i < j$$$ and $$$a_i cdot a_j = i + j$$$. Input The first line contains one integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0β€” the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 leq n leq 10^5$$$)xa0β€” the length of array $$$a$$$. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 2 cdot n$$$)xa0β€” the array $$$a$$$. It is guaranteed that all elements are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output the number of pairs of indices $$$(i, j)$$$ such that $$$i < j$$$ and $$$a_i cdot a_j = i + j$$$. Example Input 3 2 3 1 3 6 1 5 5 3 1 5 9 2 Note For the first test case, the only pair that satisfies the constraints is $$$(1, 2)$$$, as $$$a_1 cdot a_2 = 1 + 2 = 3$$$ For the second test case, the only pair that satisfies the constraints is $$$(2, 3)$$$. For the third test case, the pairs that satisfy the constraints are $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 3)$$$.
1,200
true
false
true
false
false
false
true
false
false
false
2,935
1097C
One day, Yuhao came across a problem about checking if some bracket sequences are correct bracket sequences. A bracket sequence is any non-empty sequence of opening and closing parentheses. A bracket sequence is called a correct bracket sequence if it's possible to obtain a correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, the sequences "(())()", "()" and "(()(()))" are correct, while the bracket sequences ")(", "(()" and "(()))(" are not correct. Yuhao found this problem too simple for him so he decided to make the problem harder. You are given many (not necessarily correct) bracket sequences. The task is to connect some of them into ordered pairs so that each bracket sequence occurs in at most one pair and the concatenation of the bracket sequences in each pair is a correct bracket sequence. The goal is to create as many pairs as possible. This problem unfortunately turned out to be too difficult for Yuhao. Can you help him and solve it? Input The first line contains one integer $$$n$$$ ($$$1 leq n leq 10^5$$$) β€” the number of bracket sequences. Each of the following $$$n$$$ lines contains one bracket sequence β€” a non-empty string which consists only of characters "(" and ")". The sum of lengths of all bracket sequences in the input is at most $$$5 cdot 10^5$$$. Note that a bracket sequence may appear in the input multiple times. In this case, you can use each copy of the sequence separately. Also note that the order in which strings appear in the input doesn't matter. Output Print a single integer β€” the maximum number of pairs which can be made, adhering to the conditions in the statement. Examples Input 7 )()) ) (( (( ( ) ) Note In the first example, it's optimal to construct two pairs: "(( xa0xa0xa0 )())" and "( xa0xa0xa0 )".
1,400
false
true
true
false
false
false
false
false
false
false
5,222
1225G
There are $$$n$$$ positive integers written on the blackboard. Also, a positive number $$$k geq 2$$$ is chosen, and none of the numbers on the blackboard are divisible by $$$k$$$. In one operation, you can choose any two integers $$$x$$$ and $$$y$$$, erase them and write one extra number $$$f(x + y)$$$, where $$$f(x)$$$ is equal to $$$x$$$ if $$$x$$$ is not divisible by $$$k$$$, otherwise $$$f(x) = f(x / k)$$$. In the end, there will be a single number of the blackboard. Is it possible to make the final number equal to $$$1$$$? If so, restore any sequence of operations to do so. Input The first line contains two integers $$$n$$$ and $$$k$$$xa0β€” the initial number of integers on the blackboard, and the chosen number ($$$2 leq n leq 16$$$, $$$2 leq k leq 2000$$$). The second line contains $$$n$$$ positive integers $$$a_1, ldots, a_n$$$ initially written on the blackboard. It is guaranteed that none of the numbers $$$a_i$$$ is divisible by $$$k$$$, and the sum of all $$$a_i$$$ does not exceed $$$2000$$$. Output If it is impossible to obtain $$$1$$$ as the final number, print "NO" in the only line. Otherwise, print "YES" on the first line, followed by $$$n - 1$$$ lines describing operations. The $$$i$$$-th of these lines has to contain two integers $$$x_i$$$ and $$$y_i$$$ to be erased and replaced with $$$f(x_i + y_i)$$$ on the $$$i$$$-th operation. If there are several suitable ways, output any of them. Note In the second sample case: $$$f(8 + 7) = f(15) = f(5) = 5$$$; $$$f(23 + 13) = f(36) = f(12) = f(4) = 4$$$; $$$f(5 + 4) = f(9) = f(3) = f(1) = 1$$$.
3,100
false
true
false
true
false
true
false
false
false
false
4,545
295A
Greg has an array _a_u2009=u2009_a_1,u2009_a_2,u2009...,u2009_a__n_ and _m_ operations. Each operation looks as: _l__i_, _r__i_, _d__i_, (1u2009≀u2009_l__i_u2009≀u2009_r__i_u2009≀u2009_n_). To apply operation _i_ to the array means to increase all array elements with numbers _l__i_,u2009_l__i_u2009+u20091,u2009...,u2009_r__i_ by value _d__i_. Greg wrote down _k_ queries on a piece of paper. Each query has the following form: _x__i_, _y__i_, (1u2009≀u2009_x__i_u2009≀u2009_y__i_u2009≀u2009_m_). That means that one should apply operations with numbers _x__i_,u2009_x__i_u2009+u20091,u2009...,u2009_y__i_ to the array. Now Greg is wondering, what the array _a_ will be after all the queries are executed. Help Greg. Input The first line contains integers _n_, _m_, _k_ (1u2009≀u2009_n_,u2009_m_,u2009_k_u2009≀u2009105). The second line contains _n_ integers: _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≀u2009_a__i_u2009≀u2009105) β€” the initial array. Next _m_ lines contain operations, the operation number _i_ is written as three integers: _l__i_, _r__i_, _d__i_, (1u2009≀u2009_l__i_u2009≀u2009_r__i_u2009≀u2009_n_), (0u2009≀u2009_d__i_u2009≀u2009105). Next _k_ lines contain the queries, the query number _i_ is written as two integers: _x__i_, _y__i_, (1u2009≀u2009_x__i_u2009≀u2009_y__i_u2009≀u2009_m_). The numbers in the lines are separated by single spaces. Output On a single line print _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ β€” the array after executing all the queries. Separate the printed numbers by spaces. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier. Examples Input 3 3 3 1 2 3 1 2 1 1 3 2 2 3 4 1 2 1 3 2 3 Input 4 3 6 1 2 3 4 1 2 1 2 3 2 3 4 4 1 2 1 3 2 3 1 2 1 3 2 3
1,400
false
false
true
false
true
false
false
false
false
false
8,651
1676C
You are given $$$n$$$ words of equal length $$$m$$$, consisting of lowercase Latin alphabet letters. The $$$i$$$-th word is denoted $$$s_i$$$. In one move you can choose any position in any single word and change the letter at that position to the previous or next letter in alphabetical order. For example: you can change 'e' to 'd' or to 'f'; 'a' can only be changed to 'b'; 'z' can only be changed to 'y'. The difference between two words is the minimum number of moves required to make them equal. For example, the difference between "best" and "cost" is $$$1 + 10 + 0 + 0 = 11$$$. Find the minimum difference of $$$s_i$$$ and $$$s_j$$$ such that $$$(i < j)$$$. In other words, find the minimum difference over all possible pairs of the $$$n$$$ words. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 100$$$) β€” the number of test cases. The description of test cases follows. The first line of each test case contains $$$2$$$ integers $$$n$$$ and $$$m$$$ ($$$2 leq n leq 50$$$, $$$1 leq m leq 8$$$) β€” the number of strings and their length respectively. Then follows $$$n$$$ lines, the $$$i$$$-th of which containing a single string $$$s_i$$$ of length $$$m$$$, consisting of lowercase Latin letters. Output For each test case, print a single integer β€” the minimum difference over all possible pairs of the given strings. Example Input 6 2 4 best cost 6 3 abb zba bef cdu ooo zzz 2 7 aaabbbc bbaezfe 3 2 ab ab ab 2 8 aaaaaaaa zzzzzzzz 3 1 a u y Note For the second test case, one can show that the best pair is ("abb","bef"), which has difference equal to $$$8$$$, which can be obtained in the following way: change the first character of the first string to 'b' in one move, change the second character of the second string to 'b' in $$$3$$$ moves and change the third character of the second string to 'b' in $$$4$$$ moves, thus making in total $$$1 + 3 + 4 = 8$$$ moves. For the third test case, there is only one possible pair and it can be shown that the minimum amount of moves necessary to make the strings equal is $$$35$$$. For the fourth test case, there is a pair of strings which is already equal, so the answer is $$$0$$$.
800
true
true
true
false
false
false
true
false
false
false
2,213
1557C
Moamen and Ezzat are playing a game. They create an array $$$a$$$ of $$$n$$$ non-negative integers where every element is less than $$$2^k$$$. Moamen wins if $$$a_1 ,&, a_2 ,&, a_3 ,&, ldots ,&, a_n ge a_1 oplus a_2 oplus a_3 oplus ldots oplus a_n$$$. Here $$$&$$$ denotes the . Input The first line contains a single integer $$$t$$$ ($$$1 le t le 5$$$)β€” the number of test cases. Each test case consists of one line containing two integers $$$n$$$ and $$$k$$$ ($$$1 le nle 2cdot 10^5$$$, $$$0 le k le 2cdot 10^5$$$). Output For each test case, print a single value β€” the number of different arrays that Moamen wins with. Print the result modulo $$$1,000,000,007$$$ ($$$10^9 + 7$$$). Note In the first example, $$$n = 3$$$, $$$k = 1$$$. As a result, all the possible arrays are $$$[0,0,0]$$$, $$$[0,0,1]$$$, $$$[0,1,0]$$$, $$$[1,0,0]$$$, $$$[1,1,0]$$$, $$$[0,1,1]$$$, $$$[1,0,1]$$$, and $$$[1,1,1]$$$. Moamen wins in only $$$5$$$ of them: $$$[0,0,0]$$$, $$$[1,1,0]$$$, $$$[0,1,1]$$$, $$$[1,0,1]$$$, and $$$[1,1,1]$$$.
1,700
true
false
false
true
false
false
false
false
false
false
2,845
463A
Caisa is going to have a party and he needs to buy the ingredients for a big chocolate cake. For that he is going to the biggest supermarket in town. Unfortunately, he has just _s_ dollars for sugar. But that's not a reason to be sad, because there are _n_ types of sugar in the supermarket, maybe he able to buy one. But that's not all. The supermarket has very unusual exchange politics: instead of cents the sellers give sweets to a buyer as a change. Of course, the number of given sweets always doesn't exceed 99, because each seller maximizes the number of dollars in the change (100 cents can be replaced with a dollar). Caisa wants to buy only one type of sugar, also he wants to maximize the number of sweets in the change. What is the maximum number of sweets he can get? Note, that Caisa doesn't want to minimize the cost of the sugar, he only wants to get maximum number of sweets as change. Input The first line contains two space-separated integers _n_,u2009_s_ (1u2009≀u2009_n_,u2009_s_u2009≀u2009100). The _i_-th of the next _n_ lines contains two integers _x__i_, _y__i_ (1u2009≀u2009_x__i_u2009≀u2009100;xa00u2009≀u2009_y__i_u2009<u2009100), where _x__i_ represents the number of dollars and _y__i_ the number of cents needed in order to buy the _i_-th type of sugar. Output Print a single integer representing the maximum number of sweets he can buy, or -1 if he can't buy any type of sugar. Examples Input 5 10 3 90 12 0 9 70 5 50 7 0 Input 5 5 10 10 20 20 30 30 40 40 50 50 Note In the first test sample Caisa can buy the fourth type of sugar, in such a case he will take 50 sweets as a change.
1,200
false
false
true
false
false
false
true
false
false
false
7,989
1543A
Welcome to Rockport City! It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet $$$a$$$ dollars and Ronnie has bet $$$b$$$ dollars. But the fans seem to be disappointed. The excitement of the fans is given by $$$gcd(a,b)$$$, where $$$gcd(x, y)$$$ denotes the number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it. Note that $$$gcd(x,0)=x$$$ for any $$$x ge 0$$$. Input The first line of input contains a single integer $$$t$$$ ($$$1leq tleq 5cdot 10^3$$$) β€” the number of test cases. The first and the only line of each test case contains two integers $$$a$$$ and $$$b$$$ ($$$0leq a, bleq 10^{18}$$$). Output For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0. Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement. Note For the first test case, you can apply the first operation $$$1$$$ time to get $$$a=9$$$ and $$$b=6$$$. It can be shown that $$$3$$$ is the maximum excitement possible. For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to $$$1$$$. Since the initial excitement is also $$$1$$$, you don't need to apply any operation. For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times. For the fourth test case, you can apply the second operation $$$3$$$ times to get $$$a=0$$$ and $$$b=6$$$. Since, $$$gcd(0,6)=6$$$, the fans will get an excitement of $$$6$$$.
900
true
true
false
false
false
false
false
false
false
false
2,928
1132F
Problem - 1132F - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags dp *2000 No tag edit access β†’ Contest materials β€” the length of string $$$s$$$. The second line contains the string $$$s$$$ ($$$s = n$$$) consisting of lowercase Latin letters. Output Output a single integer β€” the minimal number of operation to delete string $$$s$$$. Examples Input 5 abaca Output 3 Input 8 abcddcba Output 4
2,000
false
false
false
true
false
false
false
false
false
false
5,056
960E
Given a tree with $$$n$$$ nodes numbered from $$$1$$$ to $$$n$$$. Each node $$$i$$$ has an associated value $$$V_i$$$. If the simple path from $$$u_1$$$ to $$$u_m$$$ consists of $$$m$$$ nodes namely $$$u_1 ightarrow u_2 ightarrow u_3 ightarrow dots u_{m-1} ightarrow u_{m}$$$, then its alternating function $$$A(u_{1},u_{m})$$$ is defined as $$$A(u_{1},u_{m}) = sumlimits_{i=1}^{m} (-1)^{i+1} cdot V_{u_{i}}$$$. A path can also have $$$0$$$ edges, i.e. $$$u_{1}=u_{m}$$$. Compute the sum of alternating functions of all unique simple paths. Note that the paths are directed: two paths are considered different if the starting vertices differ or the ending vertices differ. The answer may be large so compute it modulo $$$10^{9}+7$$$. Input The first line contains an integer $$$n$$$ $$$(2 leq n leq 2cdot10^{5} )$$$ β€” the number of vertices in the tree. The second line contains $$$n$$$ space-separated integers $$$V_1, V_2, ldots, V_n$$$ ($$$-10^9leq V_i leq 10^9$$$) β€” values of the nodes. The next $$$n-1$$$ lines each contain two space-separated integers $$$u$$$ and $$$v$$$ $$$(1leq u, vlequ2009n, u eq v)$$$ denoting an edge between vertices $$$u$$$ and $$$v$$$. It is guaranteed that the given graph is a tree. Note Consider the first example. A simple path from node $$$1$$$ to node $$$2$$$: $$$1 ightarrow 2$$$ has alternating function equal to $$$A(1,2) = 1 cdot (-4)+(-1) cdot 1 = -5$$$. A simple path from node $$$1$$$ to node $$$3$$$: $$$1 ightarrow 3$$$ has alternating function equal to $$$A(1,3) = 1 cdot (-4)+(-1) cdot 5 = -9$$$. A simple path from node $$$2$$$ to node $$$4$$$: $$$2 ightarrow 1 ightarrow 4$$$ has alternating function $$$A(2,4) = 1 cdot (1)+(-1) cdot (-4)+1 cdot (-2) = 3$$$. A simple path from node $$$1$$$ to node $$$1$$$ has a single node $$$1$$$, so $$$A(1,1) = 1 cdot (-4) = -4$$$. Similarly, $$$A(2, 1) = 5$$$, $$$A(3, 1) = 9$$$, $$$A(4, 2) = 3$$$, $$$A(1, 4) = -2$$$, $$$A(4, 1) = 2$$$, $$$A(2, 2) = 1$$$, $$$A(3, 3) = 5$$$, $$$A(4, 4) = -2$$$, $$$A(3, 4) = 7$$$, $$$A(4, 3) = 7$$$, $$$A(2, 3) = 10$$$, $$$A(3, 2) = 10$$$. So the answer is $$$(-5) + (-9) + 3 + (-4) + 5 + 9 + 3 + (-2) + 2 + 1 + 5 + (-2) + 7 + 7 + 10 + 10 = 40$$$. Similarly $$$A(1,4)=-2, A(2,2)=1, A(2,1)=5, A(2,3)=10, A(3,3)=5, A(3,1)=9, A(3,2)=10, A(3,4)=7, A(4,4)=-2, A(4,1)=2, A(4,2)=3 , A(4,3)=7$$$ which sums upto 40.
2,300
false
false
false
true
false
false
false
false
false
false
5,867
1799F
You have an array of positive integers $$$a_1, a_2, ldots, a_n$$$, of length $$$n$$$. You are also given a positive integer $$$b$$$. You are allowed to perform the following operations (possibly several) times in any order: 1. Choose some $$$1 le i le n$$$, and replace $$$a_i$$$ with $$$lceil frac{a_i}{2} ceil$$$. Here, $$$lceil x ceil$$$ denotes the smallest integer not less than $$$x$$$. 2. Choose some $$$1 le i le n$$$, and replace $$$a_i$$$ with $$$max(a_i - b, 0)$$$. However, you must also follow these rules: You can perform at most $$$k_1$$$ operations of type 1 in total. You can perform at most $$$k_2$$$ operations of type 2 in total. For all $$$1 le i le n$$$, you can perform at most $$$1$$$ operation of type 1 on element $$$a_i$$$. For all $$$1 le i le n$$$, you can perform at most $$$1$$$ operation of type 2 on element $$$a_i$$$. The cost of an array is the sum of its elements. Find the minimum cost of $$$a$$$ you can achieve by performing these operations. Input Input consists of multiple test cases. The first line contains a single integer $$$t$$$, the number of test cases ($$$1 le t le 5000$$$). The first line of each test case contains $$$n$$$, $$$b$$$, $$$k_1$$$, and $$$k_2$$$ ($$$1 le n le 5000$$$, $$$1 le b le 10^9$$$, $$$0 le k_1, k_2 le n$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ describing the array $$$a$$$ ($$$1 le a_i le 10^9$$$). It is guaranteed the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$. Output For each test case, print the minimum cost of $$$a$$$ you can achieve by performing the operations. Example Input 7 3 2 1 1 9 3 5 2 1 2 0 1000000000 1 5 3 1 1 2 8 3 19 3 6 9 4 2 1 2 3 4 5 6 3 10 3 3 1 2 3 5 1 0 0 999999999 999999999 999999999 999999999 999999999 5 5 4 3 5 9 10 7 4 Output 11 500000001 23 6 0 4999999995 6 Note In the first test case, you can do the following: Perform operation 2 on element $$$a_3$$$. It changes from $$$5$$$ to $$$3$$$. Perform operation 1 on element $$$a_1$$$. It changes from $$$9$$$ to $$$5$$$. After these operations, the array is $$$a = [5, 3, 3]$$$ has a cost $$$5 + 3 + 3 = 11$$$. We can show that this is the minimum achievable cost. In the second test case, note that we are not allowed to perform operation 1 more than once on $$$a_1$$$. So it is optimal to apply operation 1 once to each $$$a_1$$$ and $$$a_2$$$. Alternatively we could apply operation 1 only once to $$$a_1$$$, since it has no effect on $$$a_2$$$. In the third test case, here is one way to achieve a cost of $$$23$$$: Apply operation 1 to $$$a_4$$$. It changes from $$$19$$$ to $$$10$$$. Apply operation 2 to $$$a_4$$$. It changes from $$$10$$$ to $$$7$$$. After these operations, $$$a = [2, 8, 3, 7, 3]$$$. The cost of $$$a$$$ is $$$2 + 8 + 3 + 7 + 3 = 23$$$. We can show that this is the minimum achievable cost.
2,700
false
true
false
true
false
false
true
true
true
false
1,478
1514D
Baby Ehab has a piece of Cut and Stick with an array $$$a$$$ of length $$$n$$$ written on it. He plans to grab a pair of scissors and do the following to it: pick a range $$$(l, r)$$$ and cut out every element $$$a_l$$$, $$$a_{l + 1}$$$, ..., $$$a_r$$$ in this range; stick some of the elements together in the same order they were in the array; end up with multiple pieces, where every piece contains some of the elements and every element belongs to some piece. More formally, he partitions the sequence $$$a_l$$$, $$$a_{l + 1}$$$, ..., $$$a_r$$$ into subsequences. He thinks a partitioning is beautiful if for every piece (subsequence) it holds that, if it has length $$$x$$$, then no value occurs strictly more than $$$lceil frac{x}{2} ceil$$$ times in it. He didn't pick a range yet, so he's wondering: for $$$q$$$ ranges $$$(l, r)$$$, what is the minimum number of pieces he needs to partition the elements $$$a_l$$$, $$$a_{l + 1}$$$, ..., $$$a_r$$$ into so that the partitioning is beautiful. A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly zero) elements. Note that it does not have to be contiguous. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n,q le 3 cdot 10^5$$$)xa0β€” the length of the array $$$a$$$ and the number of queries. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_{n}$$$ ($$$1 le a_i le n$$$)xa0β€” the elements of the array $$$a$$$. Each of the next $$$q$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 le l le r le n$$$)xa0β€” the range of this query. Output For each query, print the minimum number of subsequences you need to partition this range into so that the partitioning is beautiful. We can prove such partitioning always exists. Example Input 6 2 1 3 2 3 3 2 1 6 2 5 Note In the first query, you can just put the whole array in one subsequence, since its length is $$$6$$$, and no value occurs more than $$$3$$$ times in it. In the second query, the elements of the query range are $$$[3,2,3,3]$$$. You can't put them all in one subsequence, since its length is $$$4$$$, and $$$3$$$ occurs more than $$$2$$$ times. However, you can partition it into two subsequences: $$$[3]$$$ and $$$[2,3,3]$$$.
2,000
false
true
true
false
true
false
false
true
true
false
3,093
1373D
# Maximum Sum on Even Positions Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 256 megabytes You are given an array a consisting of n integers. Indices of the array start from zero (i. e. the first element is a0, the second one is a1, and so on). You can reverse at most one subarray (continuous subsegment) of this array. Recall that the subarray of a with borders l and r is a[l; r] = al, a l+1 , . . . , a r.Your task is to reverse such a subarray that the sum of elements on even positions of the resulting array is maximized (i. e. the sum of elements a0, a 2, . . . , a 2k for integer k = b nβˆ’12 c should be maximum possible). You have to answer t independent test cases. # Input The first line of the input contains one integer t (1 ≀ t ≀ 2 Β· 10 4) x16 the number of test cases. Then t test cases follow. The first line of the test case contains one integer n (1 ≀ n ≀ 2 Β· 10 5) x16 the length of a. The second line of the test case contains n integers a0, a 1, . . . , a nβˆ’1 (1 ≀ ai ≀ 10 9), where ai is the i-th element of a.It is guaranteed that the sum of n does not exceed 2 Β· 10 5 (βˆ‘ n ≀ 2 Β· 10 5). # Output For each test case, print the answer on the separate line x16 the maximum possible sum of elements on even positions after reversing at most one subarray (continuous subsegment) of a. # Example standard input standard output 481 7 3 4 7 6 2 9 51 2 1 2 1 10 7 8 4 5 7 6 8 9 7 3 43 1 2 1 26 537 5 Page 1 of 1
1,600
false
true
true
true
false
false
false
false
false
false
3,798
267C
Berland traffic is very different from traffic in other countries. The capital of Berland consists of _n_ junctions and _m_ roads. Each road connects a pair of junctions. There can be multiple roads between a pair of junctions. For each road we know its capacity: value _c__i_ is the maximum number of cars that can drive along a road in any direction per a unit of time. For each road, the cars can drive along it in one of two direction. That it, the cars can't simultaneously move in both directions. A road's traffic is the number of cars that goes along it per a unit of time. For road (_a__i_,u2009_b__i_) this value is negative, if the traffic moves from _b__i_ to _a__i_. A road's traffic can be a non-integer number. The capital has two special junctions β€” the entrance to the city (junction 1) and the exit from the city (junction _n_). For all other junctions it is true that the traffic is not lost there. That is, for all junctions except for 1 and _n_ the incoming traffic sum equals the outgoing traffic sum. Traffic has an unusual peculiarity in the capital of Berland β€” for any pair of junctions (_x_,u2009_y_) the sum of traffics along any path from _x_ to _y_ doesn't change depending on the choice of the path. Such sum includes traffic along all roads on the path (possible with the "minus" sign, if the traffic along the road is directed against the direction of the road on the path from _x_ to _y_). Your task is to find the largest traffic that can pass trough the city per one unit of time as well as the corresponding traffic for each road. Input The first line contains a positive integer _n_ β€” the number of junctions (2u2009≀u2009_n_u2009≀u2009100). The second line contains integer _m_ (1u2009≀u2009_m_u2009≀u20095000) β€” the number of roads. Next _m_ lines contain the roads' descriptions. Each road contains a group of three numbers _a__i_, _b__i_, _c__i_, where _a__i_,u2009_b__i_ are the numbers of junctions, connected by the given road, and _c__i_ (1u2009≀u2009_a__i_,u2009_b__i_u2009≀u2009_n_; _a__i_u2009β‰ u2009_b__i_; 0u2009≀u2009_c__i_u2009≀u200910000) is the largest permissible traffic along this road. Output In the first line print the required largest traffic across the city. Then print _m_ lines, on each line print the speed, at which the traffic moves along the corresponding road. If the direction doesn't match the order of the junctions, given in the input, then print the traffic with the minus sign. Print the numbers with accuracy of at least five digits after the decimal point. If there are many optimal solutions, print any of them. Examples Input 2 3 1 2 2 1 2 4 2 1 1000 Output 6.00000 2.00000 2.00000 -2.00000 Input 7 11 1 2 7 1 2 7 1 3 7 1 4 7 2 3 7 2 5 7 3 6 7 4 7 7 5 4 7 5 6 7 6 7 7 Output 13.00000 2.00000 2.00000 3.00000 6.00000 1.00000 3.00000 4.00000 7.00000 1.00000 2.00000 6.00000
2,700
true
false
false
false
false
false
false
false
false
false
8,764
67A
Problem - 67A - Codeforces =============== xa0 . The second line gives (_n_u2009-u20091) characters consisting of "L", "R" and "=". For each pair of adjacent students "L" means that the left student has higher marks, "R" means that the right student has higher marks and "=" means that both have equal marks. Output Output consists of _n_ integers separated by a space representing the number of toffees each student receives in the queue starting from the first one to the last one. Examples Input 5 LRLR Output 2 1 2 1 2 Input 5 =RRR Output 1 1 2 3 4
1,800
false
true
true
true
false
false
false
false
false
true
9,636
527E
The project of a data center of a Big Software Company consists of _n_ computers connected by _m_ cables. Simply speaking, each computer can be considered as a box with multiple cables going out of the box. Very Important Information is transmitted along each cable in one of the two directions. As the data center plan is not yet approved, it wasn't determined yet in which direction information will go along each cable. The cables are put so that each computer is connected with each one, perhaps through some other computers. The person in charge of the cleaning the data center will be Claudia Ivanova, the janitor. She loves to tie cables into bundles using cable ties. For some reasons, she groups the cables sticking out of a computer into groups of two, and if it isn't possible, then she gets furious and attacks the computer with the water from the bucket. It should also be noted that due to the specific physical characteristics of the Very Important Information, it is strictly forbidden to connect in one bundle two cables where information flows in different directions. The management of the data center wants to determine how to send information along each cable so that Claudia Ivanova is able to group all the cables coming out of each computer into groups of two, observing the condition above. Since it may not be possible with the existing connections plan, you are allowed to add the minimum possible number of cables to the scheme, and then you need to determine the direction of the information flow for each cable (yes, sometimes data centers are designed based on the janitors' convenience...) Input The first line contains two numbers, _n_ and _m_ (1u2009≀u2009_n_u2009≀u2009100u2009000, 1u2009≀u2009_m_u2009≀u2009200u2009000) β€” the number of computers and the number of the already present cables, respectively. Each of the next lines contains two numbers _a__i_,u2009_b__i_ (1u2009≀u2009_a__i_,u2009_b__i_u2009≀u2009_n_) β€” the indices of the computers connected by the _i_-th cable. The data centers often have a very complex structure, so a pair of computers may have more than one pair of cables between them and some cables may connect a computer with itself. Output In the first line print a single number _p_ (_p_u2009β‰₯u2009_m_) β€” the minimum number of cables in the final scheme. In each of the next _p_ lines print a pair of numbers _c__i_,u2009_d__i_ (1u2009≀u2009_c__i_,u2009_d__i_u2009≀u2009_n_), describing another cable. Such entry means that information will go along a certain cable in direction from _c__i_ to _d__i_. Among the cables you printed there should be all the cables presented in the original plan in some of two possible directions. It is guaranteed that there is a solution where _p_ doesn't exceed 500u2009000. If there are several posible solutions with minimum possible value of _p_, print any of them. Examples Input 4 6 1 2 2 3 3 4 4 1 1 3 1 3 Output 6 1 2 3 4 1 4 3 2 1 3 1 3 Output 6 2 1 2 3 1 1 3 3 3 1 1 1 Note Picture for the first sample test. The tied pairs of cables are shown going out from the same point. Picture for the second test from the statement. The added cables are drawin in bold. Alternative answer for the second sample test:
2,600
false
false
false
false
false
false
false
false
false
true
7,720
39B
Petya works as a PR manager for a successful Berland company BerSoft. He needs to prepare a presentation on the company income growth since 2001 (the year of its founding) till now. Petya knows that in 2001 the company income amounted to _a_1 billion bourles, in 2002 β€” to _a_2 billion, ..., and in the current (2000u2009+u2009_n_)-th year β€” _a__n_ billion bourles. On the base of the information Petya decided to show in his presentation the linear progress history which is in his opinion perfect. According to a graph Petya has already made, in the first year BerSoft company income must amount to 1 billion bourles, in the second year β€” 2 billion bourles etc., each following year the income increases by 1 billion bourles. Unfortunately, the real numbers are different from the perfect ones. Among the numbers _a__i_ can even occur negative ones that are a sign of the company’s losses in some years. That is why Petya wants to ignore some data, in other words, cross some numbers _a__i_ from the sequence and leave only some subsequence that has perfect growth. Thus Petya has to choose a sequence of years _y_1, _y_2, ..., _y__k_,so that in the year _y_1 the company income amounted to 1 billion bourles, in the year _y_2 β€” 2 billion bourles etc., in accordance with the perfect growth dynamics. Help him to choose the longest such sequence. Input The first line contains an integer _n_ (1u2009≀u2009_n_u2009≀u2009100). The next line contains _n_ integers _a__i_ (u2009-u2009100u2009≀u2009_a__i_u2009≀u2009100). The number _a__i_ determines the income of BerSoft company in the (2000u2009+u2009_i_)-th year. The numbers in the line are separated by spaces. Output Output _k_ β€” the maximum possible length of a perfect sequence. In the next line output the sequence of years _y_1, _y_2, ..., _y__k_. Separate the numbers by spaces. If the answer is not unique, output any. If no solution exist, output one number 0. Examples Input 10 -2 1 1 3 2 3 4 -10 -2 5 Output 5 2002 2005 2006 2007 2010
1,300
false
true
false
false
false
false
false
false
false
false
9,799
444D
DZY loves strings, and he enjoys collecting them. In China, many people like to use strings containing their names' initials, for example: xyz, jcvb, dzy, dyh. Once DZY found a lucky string _s_. A lot of pairs of good friends came to DZY when they heard about the news. The first member of the _i_-th pair has name _a__i_, the second one has name _b__i_. Each pair wondered if there is a substring of the lucky string containing both of their names. If so, they want to find the one with minimum length, which can give them good luck and make their friendship last forever. Please help DZY for each pair find the minimum length of the substring of _s_ that contains both _a__i_ and _b__i_, or point out that such substring doesn't exist. A substring of _s_ is a string _s__l__s__l_u2009+u20091... _s__r_ for some integers _l_,u2009_r_ (1u2009≀u2009_l_u2009≀u2009_r_u2009≀u2009_s_). The length of such the substring is (_r_u2009-u2009_l_u2009+u20091). A string _p_ contains some another string _q_ if there is a substring of _p_ equal to _q_. Input The first line contains a string _s_ (1u2009≀u2009_s_u2009≀u200950000). The second line contains a non-negative integer _q_ (0u2009≀u2009_q_u2009≀u2009100000) β€” the number of pairs. Each of the next _q_ lines describes a pair, the line contains two space-separated strings _a__i_ and _b__i_ (1u2009≀u2009_a__i_,u2009_b__i_u2009≀u20094). It is guaranteed that all the strings only consist of lowercase English letters. Output For each pair, print a line containing a single integer β€” the minimum length of the required substring. If there is no such substring, output -1. Examples Input xudyhduxyz 3 xyz xyz dyh xyz dzy xyz Input abcabd 3 a c ab abc ab d Input baabcabaaa 2 abca baa aa aba Note The shortest substrings in the first sample are: xyz, dyhduxyz. The shortest substrings in the second sample are: ca, abc and abd. The shortest substrings in the third sample are: baabca and abaa.
2,500
false
false
false
false
false
false
false
true
false
false
8,060
1060F
Consider a tree $$$T$$$ (that is, a connected graph without cycles) with $$$n$$$ vertices labelled $$$1$$$ through $$$n$$$. We start the following process with $$$T$$$: while $$$T$$$ has more than one vertex, do the following: choose a random edge of $$$T$$$ equiprobably; shrink the chosen edge: if the edge was connecting vertices $$$v$$$ and $$$u$$$, erase both $$$v$$$ and $$$u$$$ and create a new vertex adjacent to all vertices previously adjacent to either $$$v$$$ or $$$u$$$. The new vertex is labelled either $$$v$$$ or $$$u$$$ equiprobably. At the end of the process, $$$T$$$ consists of a single vertex labelled with one of the numbers $$$1, ldots, n$$$. For each of the numbers, what is the probability of this number becoming the label of the final vertex? Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 50$$$). The following $$$n - 1$$$ lines describe the tree edges. Each of these lines contains two integers $$$u_i, v_i$$$xa0β€” labels of vertices connected by the respective edge ($$$1 leq u_i, v_i leq n$$$, $$$u_i eq v_i$$$). It is guaranteed that the given graph is a tree. Output Print $$$n$$$ floating numbersxa0β€” the desired probabilities for labels $$$1, ldots, n$$$ respectively. All numbers should be correct up to $$$10^{-6}$$$ relative or absolute precision. Examples Output 0.1250000000 0.2916666667 0.2916666667 0.2916666667 Output 0.0850694444 0.0664062500 0.0664062500 0.1955295139 0.1955295139 0.1955295139 0.1955295139 Note In the first sample, the resulting vertex has label 1 if and only if for all three edges the label 1 survives, hence the probability is $$$1/2^3 = 1/8$$$. All other labels have equal probability due to symmetry, hence each of them has probability $$$(1 - 1/8) / 3 = 7/24$$$.
2,900
false
false
false
true
false
false
false
false
false
false
5,411
847C
Recall that the bracket sequence is considered regular if it is possible to insert symbols '+' and '1' into it so that the result is a correct arithmetic expression. For example, a sequence "(()())" is regular, because we can get correct arithmetic expression insering symbols '+' and '1': "((1+1)+(1+1))". Also the following sequences are regular: "()()()", "(())" and "()". The following sequences are not regular bracket sequences: ")(", "(()" and "())(()". In this problem you are given two integers _n_ and _k_. Your task is to construct a regular bracket sequence consisting of round brackets with length 2Β·_n_ with total sum of nesting of all opening brackets equals to exactly _k_. The nesting of a single opening bracket equals to the number of pairs of brackets in which current opening bracket is embedded. For example, in the sequence "()(())" the nesting of first opening bracket equals to 0, the nesting of the second opening bracket equals to 0 and the nesting of the third opening bracket equal to 1. So the total sum of nestings equals to 1. Input The first line contains two integers _n_ and _k_ (1u2009≀u2009_n_u2009≀u20093Β·105, 0u2009≀u2009_k_u2009≀u20091018)xa0β€” the number of opening brackets and needed total nesting. Output Print the required regular bracket sequence consisting of round brackets. If there is no solution print "Impossible" (without quotes). Note The first example is examined in the statement. In the second example the answer is "(((())))". The nesting of the first opening bracket is 0, the nesting of the second is 1, the nesting of the third is 2, the nesting of fourth is 3. So the total sum of nestings equals to 0u2009+u20091u2009+u20092u2009+u20093u2009=u20096. In the third it is impossible to construct a regular bracket sequence, because the maximum possible total sum of nestings for two opening brackets equals to 1. This total sum of nestings is obtained for the sequence "(())".
1,800
false
false
false
false
false
true
false
false
false
false
6,369
1820B
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 866 (Div. 2) Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags math strings two pointers *1100 No tag edit access β†’ Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST B. JoJo's Incredible Adventures time limit per test1 second memory limit per test256 megabytes Did you think there was going to be a JoJo legend here? But no, that was me, Dio! Given a binary string $$$s$$$ of length $$$n$$$, consisting of characters 0 and 1. Let's build a square table of size $$$n imes n$$$, consisting of 0 and 1 characters as follows. In the first row of the table write the original string $$$s$$$. In the second row of the table write cyclic shift of the string $$$s$$$ by one to the right. In the third row of the table, write the cyclic shift of line $$$s$$$ by two to the right. And so on. Thus, the row with number $$$k$$$ will contain a cyclic shift of string $$$s$$$ by $$$k$$$ to the right. The rows are numbered from $$$0$$$ to $$$n - 1$$$ top-to-bottom. In the resulting table we need to find the rectangle consisting only of ones that has the largest area. We call a rectangle the set of all cells $$$(i, j)$$$ in the table, such that $$$x_1 le i le x_2$$$ and $$$y_1 le j le y_2$$$ for some integers $$$0 le x_1 le x_2 < n$$$ and $$$0 le y_1 le y_2 < n$$$. Recall that the cyclic shift of string $$$s$$$ by $$$k$$$ to the right is the string $$$s_{n-k+1} ldots s_n s_1 s_2 ldots s_{n-k}$$$. For example, the cyclic shift of the string "01011" by $$$0$$$ to the right is the string itself "01011", its cyclic shift by $$$3$$$ to the right is the string "01101". Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$)xa0β€” the number of test cases. The description of test cases follows. The first and the only line of each test case contains a single binary string $$$s$$$ ($$$1 le lvert s vert le 2 cdot 10^5$$$), consisting of characters 0 and 1. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integerxa0β€” the maximum area of a rectangle consisting only of ones. If there is no such rectangle, output $$$0$$$. Example input 5 0 1 101 011110 101010 output 0 1 2 6 1 Note In the first test case, there is a table $$$1 imes 1$$$ consisting of a single character 0, so there are no rectangles consisting of ones, and the answer is $$$0$$$. In the second test case, there is a table $$$1 imes 1$$$, consisting of a single character 1, so the answer is $$$1$$$. In the third test case, there is a table: 1 0 1 1 1 0 0 1 1 In the fourth test case, there is a table: 0 1 1 1 1 0 0 0 1 1 1 1 1 0 0 1 1 1 1 1 0 0 1 1 1 1 1 0 0 1 1 1 1 1 0 0 In the fifth test case, there is a table: 1 0 1 0 1 0 0 1 0 1 0 1 1 0 1 0 1 0 0 1 0 1 0 1 1 0 1 0 1 0 0 1 0 1 0 1 Rectangles with maximum area are shown in bold. Codeforces (c)
1,100
true
false
false
false
false
false
false
false
false
false
1,358
120G
Let's consider the famous game called Boom (aka Hat) with simplified rules. There are _n_ teams playing the game. Each team has two players. The purpose of the game is to explain the words to the teammate without using any words that contain the same root or that sound similarly. Player _j_ from team _i_ (1u2009≀u2009_i_u2009≀u2009_n_,u20091u2009≀u2009_j_u2009≀u20092) is characterized by two numbers: _a__ij_ and _b__ij_. The numbers correspondingly represent the skill of explaining and the skill of understanding this particular player has. Besides, _m_ cards are used for the game. Each card has a word written on it. The card number _k_ (1u2009≀u2009_k_u2009≀u2009_m_) is characterized by number _c__k_ β€” the complexity of the word it contains. Before the game starts the cards are put in a deck and shuffled. Then the teams play in turns like that: the 1-st player of the 1-st team, the 1-st player of the 2-nd team, ... , the 1-st player of the _n_-th team, the 2-nd player of the 1-st team, ... , the 2-nd player of the _n_-th team, the 1-st player of the 1-st team and so on. Each turn continues for _t_ seconds. It goes like that: Initially the time for each turn is _t_. While the time left to a player is more than 0, a player takes a card from the top of the deck and starts explaining the word it has to his teammate. The time needed for the _j_-th player of the _i_-th team to explain the word from the card _k_ to his teammate (the _q_-th player of the _i_-th team) equals _max_(1,u2009_c__k_u2009-u2009(_a__ij_u2009+u2009_b__iq_)u2009-u2009_d__ik_) (if _j_u2009=u20091,u2009 then _q_u2009=u20092,u2009 else _q_u2009=u20091). The value _d__ik_ is the number of seconds the _i_-th team has already spent explaining the word _k_ during the previous turns. Initially, all _d__ik_ equal 0. If a team manages to guess the word before the end of the turn, then the time given above is substracted from the duration of the turn, the card containing the guessed word leaves the game, the team wins one point and the game continues. If the team doesn't manage to guess the word, then the card is put at the bottom of the deck, _d__ik_ increases on the amount of time of the turn, spent on explaining the word. Thus, when this team gets the very same word, they start explaining it not from the beginning, but from the point where they stopped. The game ends when words from all _m_ cards are guessed correctly. You are given _n_ teams and a deck of _m_ cards. You should determine for each team, how many points it will have by the end of the game and which words the team will have guessed. Input The first line contains two integers _n_,u2009_t_ (1u2009≀u2009_n_,u2009_t_u2009≀u2009100), which correspondingly denote the number of teams and a turn's duration. Next _n_ lines of the input file contain four integers each: _a__i_1,u2009_b__i_1,u2009_a__i_2,u2009_b__i_2 (1u2009≀u2009_a__ij_,u2009_b__ij_u2009≀u2009100) β€” the skills of the first and the second player of the _i_-th team. The teams are given in the order in which they play. The next line of the input file contains integer _m_ (1u2009≀u2009_m_u2009≀u2009100) the number of cards. Next 2_m_ lines contain the cards' descriptions. Two lines describe each card. The first line of the description contains the word that consists of no more than 20 characters. The words only contain small Latin letters. The second line of the description contains an integer _c__k_ (1u2009≀u2009_c__k_u2009≀u2009100) β€” the complexity of the word written on the _k_-th card. The cards are listed in the order in which the lie in the deck from top to bottom. The words on all cards are different.
1,800
false
false
true
false
false
false
false
false
false
false
9,400
1906D
In The ICPC Galaxy, there exists a zone filled with asteroids that is unsafe to enter. The map of the galaxy is represented in a 2D Cartesian coordinate system. The zone is in the shape of an $$$N$$$-sided convex polygon. Each corner is numbered from $$$1$$$ to $$$N$$$; corner $$$i$$$ is located at $$$(X_i, Y_i)$$$. At any moment, you should not be inside this polygon; however, it is safe to touch the side of the polygon. There are $$$Q$$$ scenarios (numbered from $$$1$$$ to $$$Q$$$). In scenario $$$j$$$, you want to go from a starting point at $$$(A_j, B_j)$$$ to an ending point at $$$(C_j, D_j)$$$. You will be riding on a special spaceship that can only travel in a straight line. First, you set the direction of the spaceship, then the spaceship will start traveling in that direction. During the travel, you are only allowed to change direction at most once. Changing direction means you stop the spaceship, set a new direction, and then start traveling again in the new direction. For each scenario, determine the minimum distance required to travel without being inside of the zone at any moment, or report if it is impossible to reach the ending point. Input The first line consists of an integer $$$N$$$ ($$$3 leq N leq 100,000$$$). Each of the next $$$N$$$ lines consists of two integers $$$X_i$$$ $$$Y_i$$$ ($$$-10^9 leq X_i, Y_i leq 10^9$$$). The points form a convex polygon in counterclockwise order. There are no three points which are collinear. The following line consists of an integer $$$Q$$$ ($$$1 leq Q leq 100,000$$$). Each of the next $$$Q$$$ lines consists of four integers $$$A_j$$$ $$$B_j$$$ $$$C_j$$$ $$$D_j$$$ ($$$-10^9 leq A_j, B_j, C_j, D_j leq 10^9$$$). There are no starting points and ending points inside the zone. However, it is possible for the starting point and the ending point to be at the side of the zone. All the coordinates in the input are integers. Output For each scenario, output the answer in a single line. If it is possible to reach the ending point without being inside the zone at any moment, then output the minimum distance required to travel. Otherwise, output -1. Your answer is considered correct if its absolute error or relative error does not exceed $$$10^{-6}$$$. Namely, if your answer is $$$a$$$ and the jury's answer is $$$b$$$, then your answer is accepted if $$$frac{a - b}{max(1, b)} leq 10^{-6}$$$. Examples Input 5 0 2 2 0 4 0 4 4 2 4 5 6 1 6 3 2 5 0 0 3 5 3 -1 1 4 5 4 3 4 3 0 Output 2 5.6055512755 8.48528137422 4 -1 Input 4 -10 -9 10 -9 10 9 -10 9 2 0 10 0 -10 -10 -10 -10 -10 Input 8 -20 -10 10 -20 25 -15 35 -5 30 10 15 20 -25 15 -30 5 6 -15 -15 -15 20 -30 -5 30 15 25 20 -5 -20 -5 25 20 -20 -30 10 30 -10 -30 -50 50 0 Output 59.0857761929 103.2455532034 94.7213595500 101.5640991922 164.8528137424 94.3398113206 Note Explanation for the sample input/output #1 This sample is depicted in the following illustration. During scenario $$$1$$$ and $$$4$$$, you can directly go to the ending point without changing the direction. During scenario $$$2$$$, you can go to $$$(0, 2)$$$, then change direction to the ending point. During scenario $$$3$$$, you can go to $$$(6, 2)$$$, then change direction to the ending point. During scenario $$$5$$$, it can be shown that it is impossible to reach the ending point.
2,800
false
false
false
false
false
false
false
true
false
false
874
717C
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe is sulky as usual. Due to his angst fueled by disappointment in his own life, he has given them a lot of homework in Potions class. Each of the _n_ students has been assigned a single task. Some students do certain tasks faster than others. Thus, they want to redistribute the tasks so that each student still does exactly one task, and that all tasks are finished. Each student has their own laziness level, and each task has its own difficulty level. Professor Snipe is trying hard to improve their work ethics, so each student’s laziness level is equal to their task’s difficulty level. Both sets of values are given by the sequence _a_, where _a__i_ represents both the laziness level of the _i_-th student and the difficulty of his task. The time a student needs to finish a task is equal to the product of their laziness level and the task’s difficulty. They are wondering, what is the minimum possible total time they must spend to finish all tasks if they distribute them in the optimal way. Each person should receive one task and each task should be given to one person. Print the answer modulo 10u2009007. Note In the first sample, if the students switch their tasks, they will be able to finish them in 3u2009+u20093u2009=u20096 time units.
1,200
false
false
true
false
false
false
false
false
true
false
6,950
1773F
Scientists are researching an impact of football match results on the mood of football fans. They have a hypothesis that there is a correlation between the number of draws and fans' desire to watch football matches in the future. In football, two teams play a match. The teams score goals throughout a match. A score "$$$x$$$:$$$y$$$" means that the team we observe scored $$$x$$$ goals and conceded $$$y$$$ goals. If $$$x = y$$$, then the match ends in a draw. If $$$x > y$$$, then the observed team wins, and if $$$x < y$$$, then it loses. To find out if there is a correlation, the scientists gathered information about the results of teams in lower leagues. The information they found is the number of matches played by the team ($$$n$$$), the number of goals scored in these matches ($$$a$$$), and the number of goals conceded in these matches ($$$b$$$). You are given this information for a single team. You are asked to calculate the minimum number of draws that could have happened during the team's matches and provide a list of match scores with the minimum number of draws. Input The first line contains an integer $$$n$$$xa0β€” the number of matches played by the team ($$$1 le n le 100$$$). The second line contains an integer $$$a$$$xa0β€” the total number of goals scored by the team in all $$$n$$$ matches ($$$0 le a le 1000$$$). The third line contains an integer $$$b$$$xa0β€” the total number of goals conceded by the team in all $$$n$$$ matches ($$$0 le b le 1000$$$). Output In the first line, print a single integer $$$d$$$xa0β€” the minimum number of draws. In the following $$$n$$$ lines, print a list of match scores, each line in the format "$$$x$$$:$$$y$$$", where $$$x$$$ is the number of goals scored in the match, and $$$y$$$xa0– the number of goals conceded, so that exactly $$$d$$$ of these matches have ended in a draw. In case multiple such lists of match scores exist, print any of them. Examples Output 2 0:0 1:0 0:0 0:1 1:0 1:0
800
false
false
false
false
false
true
false
false
false
false
1,651
105C
Each item in the game has a level. The higher the level is, the higher basic parameters the item has. We shall consider only the following basic parameters: attack (atk), defense (def) and resistance to different types of impact (res). Each item belongs to one class. In this problem we will only consider three of such classes: weapon, armor, orb. Besides, there's a whole new world hidden inside each item. We can increase an item's level travelling to its world. We can also capture the so-called residents in the Item World Residents are the creatures that live inside items. Each resident gives some bonus to the item in which it is currently located. We will only consider residents of types: gladiator (who improves the item's atk), sentry (who improves def) and physician (who improves res). Each item has the size parameter. The parameter limits the maximum number of residents that can live inside an item. We can move residents between items. Within one moment of time we can take some resident from an item and move it to some other item if it has a free place for a new resident. We cannot remove a resident from the items and leave outside β€” any of them should be inside of some item at any moment of time. Laharl has a certain number of items. He wants to move the residents between items so as to equip himself with weapon, armor and a defensive orb. The weapon's atk should be largest possible in the end. Among all equipping patterns containing weapon's maximum atk parameter we should choose the ones where the armor’s def parameter is the largest possible. Among all such equipment patterns we should choose the one where the defensive orb would have the largest possible res parameter. Values of the parameters def and res of weapon, atk and res of armor and atk and def of orb are indifferent for Laharl. Find the optimal equipment pattern Laharl can get. Input The first line contains number _n_ (3u2009≀u2009_n_u2009≀u2009100) β€” representing how many items Laharl has. Then follow _n_ lines. Each line contains description of an item. The description has the following form: "_name_ _class_ _atk_ _def_ _res_ _size_" β€” the item's name, class, basic attack, defense and resistance parameters and its size correspondingly. _name_ and _class_ are strings and _atk_, _def_, _res_ and _size_ are integers. _name_ consists of lowercase Latin letters and its length can range from 1 to 10, inclusive. _class_ can be "weapon", "armor" or "orb". 0u2009≀u2009_atk_,u2009_def_,u2009_res_u2009≀u20091000. 1u2009≀u2009_size_u2009≀u200910. It is guaranteed that Laharl has at least one item of each class. The next line contains an integer _k_ (1u2009≀u2009_k_u2009≀u20091000) β€” the number of residents. Then _k_ lines follow. Each of them describes a resident. A resident description looks like: "_name_ _type_ _bonus_ _home_" β€” the resident's name, his type, the number of points the resident adds to the item's corresponding parameter and the name of the item which currently contains the resident. _name_, _type_ and _home_ are strings and _bonus_ is an integer. _name_ consists of lowercase Latin letters and its length can range from 1 to 10, inclusive. _type_ may be "gladiator", "sentry" or "physician". 1u2009≀u2009_bonus_u2009≀u2009100. It is guaranteed that the number of residents in each item does not exceed the item's size. The names of all items and residents are pairwise different. All words and numbers in the input are separated by single spaces. Output Print on the first line the name of the weapon in the optimal equipping pattern; then print the number of residents the weapon contains; then print the residents' names. Print on the second and third lines in the same form the names of the armor and defensive orb as well as the residents they contain. Use single spaces for separation. If there are several possible solutions, print any of them. Examples Input 4 sword weapon 10 2 3 2 pagstarmor armor 0 15 3 1 iceorb orb 3 2 13 2 longbow weapon 9 1 2 1 5 mike gladiator 5 longbow bobby sentry 6 pagstarmor petr gladiator 7 iceorb teddy physician 6 sword blackjack sentry 8 sword Output sword 2 petr mike pagstarmor 1 blackjack iceorb 2 teddy bobby Input 4 sword weapon 10 2 3 2 pagstarmor armor 0 15 3 1 iceorb orb 3 2 13 2 longbow weapon 9 1 2 1 6 mike gladiator 5 longbow bobby sentry 6 pagstarmor petr gladiator 7 iceorb teddy physician 6 sword blackjack sentry 8 sword joe physician 6 iceorb Output longbow 1 mike pagstarmor 1 bobby iceorb 2 petr joe Note In the second sample we have no free space inside the items, therefore we cannot move the residents between them.
2,200
false
false
true
false
false
false
true
false
true
false
9,464
1458B
There are $$$n$$$ glasses on the table numbered $$$1, ldots, n$$$. The glass $$$i$$$ can hold up to $$$a_i$$$ units of water, and currently contains $$$b_i$$$ units of water. You would like to choose $$$k$$$ glasses and collect as much water in them as possible. To that effect you can pour water from one glass to another as many times as you like. However, because of the glasses' awkward shape (and totally unrelated to your natural clumsiness), each time you try to transfer any amount of water, half of the amount is spilled on the floor. Formally, suppose a glass $$$i$$$ currently contains $$$c_i$$$ units of water, and a glass $$$j$$$ contains $$$c_j$$$ units of water. Suppose you try to transfer $$$x$$$ units from glass $$$i$$$ to glass $$$j$$$ (naturally, $$$x$$$ can not exceed $$$c_i$$$). Then, $$$x / 2$$$ units is spilled on the floor. After the transfer is done, the glass $$$i$$$ will contain $$$c_i - x$$$ units, and the glass $$$j$$$ will contain $$$min(a_j, c_j + x / 2)$$$ units (excess water that doesn't fit in the glass is also spilled). Each time you transfer water, you can arbitrarlly choose from which glass $$$i$$$ to which glass $$$j$$$ to pour, and also the amount $$$x$$$ transferred can be any positive real number. For each $$$k = 1, ldots, n$$$, determine the largest possible total amount of water that can be collected in arbitrarily chosen $$$k$$$ glasses after transferring water between glasses zero or more times. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 100$$$)xa0β€” the number of glasses. The following $$$n$$$ lines describe the glasses. The $$$i$$$-th of these lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$0 leq b_i leq a_i leq 100$$$, $$$a_i > 0$$$)xa0β€” capacity, and water amount currently contained for the glass $$$i$$$, respectively. Output Print $$$n$$$ real numbersxa0β€” the largest amount of water that can be collected in $$$1, ldots, n$$$ glasses respectively. Your answer will be accepted if each number is within $$$10^{-9}$$$ absolute or relative tolerance of the precise answer.
2,000
false
false
false
true
false
false
false
false
false
false
3,393
1157C1
The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2). You are given a sequence $$$a$$$ consisting of $$$n$$$ integers. All these integers are distinct, each value from $$$1$$$ to $$$n$$$ appears in the sequence exactly once. You are making a sequence of moves. During each move you must take either the leftmost element of the sequence or the rightmost element of the sequence, write it down and remove it from the sequence. Your task is to write down a strictly increasing sequence, and among all such sequences you should take the longest (the length of the sequence is the number of elements in it). For example, for the sequence $$$[2, 1, 5, 4, 3]$$$ the answer is $$$4$$$ (you take $$$2$$$ and the sequence becomes $$$[1, 5, 4, 3]$$$, then you take the rightmost element $$$3$$$ and the sequence becomes $$$[1, 5, 4]$$$, then you take $$$4$$$ and the sequence becomes $$$[1, 5]$$$ and then you take $$$5$$$ and the sequence becomes $$$[1]$$$, the obtained increasing sequence is $$$[2, 3, 4, 5]$$$). Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) β€” the number of elements in $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. All these integers are pairwise distinct. Output In the first line of the output print $$$k$$$ β€” the maximum number of elements in a strictly increasing sequence you can obtain. In the second line print a string $$$s$$$ of length $$$k$$$, where the $$$j$$$-th character of this string $$$s_j$$$ should be 'L' if you take the leftmost element during the $$$j$$$-th move and 'R' otherwise. If there are multiple answers, you can print any.
1,300
false
true
false
false
false
false
false
false
false
false
4,921
1102D
You are given a string $$$s$$$ consisting of exactly $$$n$$$ characters, and each character is either '0', '1' or '2'. Such strings are called ternary strings. Your task is to replace minimum number of characters in this string with other characters to obtain a balanced ternary string (balanced ternary string is a ternary string such that the number of characters '0' in this string is equal to the number of characters '1', and the number of characters '1' (and '0' obviously) is equal to the number of characters '2'). Among all possible balanced ternary strings you have to obtain the lexicographically (alphabetically) smallest. Note that you can neither remove characters from the string nor add characters to the string. Also note that you can replace the given characters only with characters '0', '1' and '2'. It is guaranteed that the answer exists. Input The first line of the input contains one integer $$$n$$$ ($$$3 le n le 3 cdot 10^5$$$, $$$n$$$ is divisible by $$$3$$$) β€” the number of characters in $$$s$$$. The second line contains the string $$$s$$$ consisting of exactly $$$n$$$ characters '0', '1' and '2'. Output Print one string β€” the lexicographically (alphabetically) smallest balanced ternary string which can be obtained from the given one with minimum number of replacements. Because $$$n$$$ is divisible by $$$3$$$ it is obvious that the answer exists. And it is obvious that there is only one possible answer.
1,500
false
true
false
false
false
false
false
false
false
false
5,190
65A
A long time ago (probably even in the first book), Nicholas Flamel, a great alchemist and the creator of the Philosopher's Stone, taught Harry Potter three useful spells. The first one allows you to convert _a_ grams of sand into _b_ grams of lead, the second one allows you to convert _c_ grams of lead into _d_ grams of gold and third one allows you to convert _e_ grams of gold into _f_ grams of sand. When Harry told his friends about these spells, Ron Weasley was amazed. After all, if they succeed in turning sand into lead, lead into gold, and then turning part of the gold into sand again and so on, then it will be possible to start with a small amount of sand and get huge amounts of gold! Even an infinite amount of gold! Hermione Granger, by contrast, was skeptical about that idea. She argues that according to the law of conservation of matter getting an infinite amount of matter, even using magic, is impossible. On the contrary, the amount of matter may even decrease during transformation, being converted to magical energy. Though Hermione's theory seems convincing, Ron won't believe her. As far as Ron is concerned, Hermione made up her law of conservation of matter to stop Harry and Ron wasting their time with this nonsense, and to make them go and do homework instead. That's why Ron has already collected a certain amount of sand for the experiments. A quarrel between the friends seems unavoidable... Help Harry to determine which one of his friends is right, and avoid the quarrel after all. To do this you have to figure out whether it is possible to get the amount of gold greater than any preassigned number from some finite amount of sand. Input The first line contains 6 integers _a_, _b_, _c_, _d_, _e_, _f_ (0u2009≀u2009_a_,u2009_b_,u2009_c_,u2009_d_,u2009_e_,u2009_f_u2009≀u20091000). Output Print "Ron", if it is possible to get an infinitely large amount of gold having a certain finite amount of sand (and not having any gold and lead at all), i.e., Ron is right. Otherwise, print "Hermione". Examples Input 100 200 250 150 200 250 Input 100 50 50 200 200 100 Input 100 10 200 20 300 30 Note Consider the first sample. Let's start with the 500 grams of sand. Apply the first spell 5 times and turn the sand into 1000 grams of lead. Then apply the second spell 4 times to get 600 grams of gold. Let’s take 400 grams from the resulting amount of gold turn them back into sand. We get 500 grams of sand and 200 grams of gold. If we apply the same operations to 500 grams of sand again, we can get extra 200 grams of gold every time. Thus, you can get 200, 400, 600 etc. grams of gold, i.e., starting with a finite amount of sand (500 grams), you can get the amount of gold which is greater than any preassigned number. In the forth sample it is impossible to get sand, or lead, or gold, applying the spells. In the fifth sample an infinitely large amount of gold can be obtained by using only the second spell, which allows you to receive 1 gram of gold out of nothing. Note that if such a second spell is available, then the first and the third one do not affect the answer at all. The seventh sample is more interesting. We can also start with a zero amount of sand there. With the aid of the third spell you can get sand out of nothing. We get 10000 grams of sand in this manner. Let's get 100 grams of lead using the first spell 100 times. Then make 1 gram of gold from them. We managed to receive 1 gram of gold, starting with a zero amount of sand! Clearly, in this manner you can get an infinitely large amount of gold.
1,800
true
false
true
false
false
false
false
false
false
false
9,646
508A
Pasha loves his phone and also putting his hair up... But the hair is now irrelevant. Pasha has installed a new game to his phone. The goal of the game is following. There is a rectangular field consisting of _n_ row with _m_ pixels in each row. Initially, all the pixels are colored white. In one move, Pasha can choose any pixel and color it black. In particular, he can choose the pixel that is already black, then after the boy's move the pixel does not change, that is, it remains black. Pasha loses the game when a 2u2009Γ—u20092 square consisting of black pixels is formed. Pasha has made a plan of _k_ moves, according to which he will paint pixels. Each turn in his plan is represented as a pair of numbers _i_ and _j_, denoting respectively the row and the column of the pixel to be colored on the current move. Determine whether Pasha loses if he acts in accordance with his plan, and if he does, on what move the 2u2009Γ—u20092 square consisting of black pixels is formed. Input The first line of the input contains three integers _n_,u2009_m_,u2009_k_ (1u2009≀u2009_n_,u2009_m_u2009≀u20091000, 1u2009≀u2009_k_u2009≀u2009105)xa0β€” the number of rows, the number of columns and the number of moves that Pasha is going to perform. The next _k_ lines contain Pasha's moves in the order he makes them. Each line contains two integers _i_ and _j_ (1u2009≀u2009_i_u2009≀u2009_n_, 1u2009≀u2009_j_u2009≀u2009_m_), representing the row number and column number of the pixel that was painted during a move. Output If Pasha loses, print the number of the move when the 2u2009Γ—u20092 square consisting of black pixels is formed. If Pasha doesn't lose, that is, no 2u2009Γ—u20092 square consisting of black pixels is formed during the given _k_ moves, print 0. Examples Input 2 3 6 2 3 2 2 1 3 2 2 1 2 1 1 Input 5 3 7 2 3 1 2 1 1 4 1 3 1 5 3 3 2
1,100
false
false
false
false
false
false
true
false
false
false
7,811
1095E
Problem - 1095E - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags implementation *1900 No tag edit access β†’ Contest materials ") Editorial") ' brackets. A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not. You can change the type of some bracket $$$s_i$$$. It means that if $$$s_i = $$$ ')' then you can change it to '(' and vice versa. Your task is to calculate the number of positions $$$i$$$ such that if you change the type of the $$$i$$$-th bracket, then the resulting bracket sequence becomes regular. Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 10^6$$$) β€” the length of the bracket sequence. The second line of the input contains the string $$$s$$$ consisting of $$$n$$$ opening '(' and closing ')' brackets. Output Print one integer β€” the number of positions $$$i$$$ such that if you change the type of the $$$i$$$-th bracket, then the resulting bracket sequence becomes regular. Examples Input 6 (((()) Output 3 Input 6 ()()() Output 0 Input 1 ) Output 0 Input 8 )))((((( Output 0
1,900
false
false
true
false
false
false
false
false
false
false
5,233
1370F1
# The Hidden Pair (Easy Version) Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 256 megabytes Note that the only difference between the easy and hard version is the constraint on the number of queries. You can make hacks only if all versions of the problem are solved. This is an interactive problem. You are given a tree consisting of n nodes numbered with integers from 1 to n. Ayush and Ashish chose two secret distinct nodes in the tree. You need to find out both the nodes. You can make the following query: β€’ Provide a list of nodes and you will receive a node from that list whose sum of distances to both the hidden nodes is minimal (if there are multiple such nodes in the list, you will receive any one of them). You will also get the sum of distances of that node to the hidden nodes. Recall that a tree is a connected graph without cycles. The distance between two nodes is defined as the number of edges in the simple path between them. More formally, let’s define two hidden nodes as s and f . In one query you can provide the set of nodes {a1, a 2, . . . , a c} of the tree. As a result, you will get two numbers ai and dist (ai, s ) + dist (ai, f ). The node ai is any node from the provided set, for which the number dist (ai, s ) + dist (ai, f ) is minimal. You can ask no more than 14 queries. # Input The first line contains a single integer t (1 ≀ t ≀ 10) x16 the number of test cases. Please note, how the interaction process is organized. The first line of each test case consists of a single integer n (2 ≀ n ≀ 1000) x16 the number of nodes in the tree. The next n βˆ’ 1 lines consist of two integers u, v (1 ≀ u, v ≀ n, u 6 = v) x16 the edges of the tree. # Interaction Protocol To ask a query print a single line: β€’ In the beginning print β€œ ? c β€œ (without quotes) where c (1 ≀ c ≀ n) denotes the number of nodes being queried, followed by c distinct integers in the range [1 , n ] x16 the indices of nodes from the list. For each query, you will receive two integers x, d x16 the node (among the queried nodes) with the minimum sum of distances to the hidden nodes and the sum of distances from that node to the hidden nodes. If the subset of nodes queried is invalid or you exceeded the number of queries then you will get x = d = βˆ’1.In this case, you should terminate the program immediately. When you have guessed the hidden nodes, print a single line β€œ ! β€œ (without quotes), followed by two integers in the range [1 , n ] x16 the hidden nodes. You can output the hidden nodes in any order. After this, you should read a string. If you guess the nodes correctly, you will receive the string β€œCorrect β€œ. In this case, you should continue solving the remaining test cases or terminate the program, if all test cases were solved. Otherwise, you will receive the string β€œ Incorrect β€œ. In this case, you should terminate the program immediately. Guessing the hidden nodes does not count towards the number of queries asked. Page 1 of 3 The interactor is not adaptive. The hidden nodes do not change with queries. Do not forget to read the string β€œ Correct β€œ / β€œ Incorrect β€œ after guessing the hidden nodes. You need to solve each test case before receiving the input for the next test case. The limit of 14 queries applies to each test case and not to the entire input. After printing a query do not forget to output the end of the line and flush the output. Otherwise, you will get Idleness limit exceeded . To do this, use: β€’ fflush(stdout) or cout.flush() in C++; β€’ System.out.flush() in Java; β€’ flush(output) in Pascal; β€’ stdout.flush() in Python; β€’ see the documentation for other languages. Hacks To hack the solution, use the following test format: The first line should contain a single integer t (1 ≀ t ≀ 10) x16 the number of test cases. The description of the test cases follows. The first line of each test case should contain a single integer n (2 ≀ n ≀ 1000) x16 the number of nodes in the tree. The second line should contain two distinct integers in the range [1 , n ] x16 the hidden nodes. The next n βˆ’ 1 lines should contain two integers u, v (1 ≀ u, v ≀ n, u 6 = v) x16 the edges of the tree. # Example standard input standard output 131 2 1 3 1 1 2 3 3 1 3 1 Correct ? 1 1 ? 1 2 ? 1 3 ? 2 2 3 ! 1 3 # Note The tree from the first test is shown below, and the hidden nodes are 1 and 3. Page 2 of 3 Page 3 of 3
2,400
false
false
false
false
false
false
false
true
false
true
3,816
802H
Thanks to your help, Heidi is confident that no one can fool her. She has now decided to post some fake news on the HC2 Facebook page. However, she wants to be able to communicate to the HC2 committee that the post is fake, using some secret phrase hidden in the post as a subsequence. To make this method foolproof, she wants the phrase to appear _n_ times in the post. She is asking you to design a post (string) _s_ and a hidden phrase _p_ such that _p_ appears in _s_ as a subsequence exactly _n_ times. Input The first and only line of input contains a single integer _n_ (1u2009≀u2009_n_u2009≀u20091u2009000u2009000). Output The output should contain two nonempty strings _s_ and _p_ separated by a single space. Each string should be composed of letters (a-z and A-Z: both lowercase and uppercase are allowed) and have length at most 200. The number of occurrences of _p_ in _s_ as a subsequence should be exactly _n_. If there are many possible solutions, output any of them. It is guaranteed that at least one solution exists. Note An occurrence of _p_ as a subsequence in _s_ should be thought of as a set of positions in _s_ such that the letters at these positions, in order, form _p_. The number of occurences is thus the number of such sets. For example, ab appears 6 times as a subsequence in aaabb, for the following sets of positions: {1,u20094},u2009{1,u20095},u2009{2,u20094},u2009{2,u20095},u2009{3,u20094},u2009{3,u20095} (that is, we should choose one of the a's and one of the b's).
2,200
false
false
false
false
false
true
false
false
false
false
6,560
596E
Wilbur the pig now wants to play with strings. He has found an _n_ by _m_ table consisting only of the digits from 0 to 9 where the rows are numbered 1 to _n_ and the columns are numbered 1 to _m_. Wilbur starts at some square and makes certain moves. If he is at square (_x_, _y_) and the digit _d_ (0u2009≀u2009_d_u2009≀u20099) is written at position (_x_, _y_), then he must move to the square (_x_u2009+u2009_a__d_, _y_u2009+u2009_b__d_), if that square lies within the table, and he stays in the square (_x_, _y_) otherwise. Before Wilbur makes a move, he can choose whether or not to write the digit written in this square on the white board. All digits written on the whiteboard form some string. Every time a new digit is written, it goes to the end of the current string. Wilbur has _q_ strings that he is worried about. For each string _s__i_, Wilbur wants to know whether there exists a starting position (_x_, _y_) so that by making finitely many moves, Wilbur can end up with the string _s__i_ written on the white board. Input The first line of the input consists of three integers _n_, _m_, and _q_ (1u2009≀u2009_n_,u2009_m_,u2009_q_u2009≀u2009200)xa0β€” the dimensions of the table and the number of strings to process, respectively. Each of the next _n_ lines contains _m_ digits from 0 and 9 giving the table itself. Then follow 10 lines. The _i_-th of them contains the values _a__i_u2009-u20091 and _b__i_u2009-u20091 (u2009-u2009200u2009≀u2009_a__i_,u2009_b__i_u2009≀u2009200), i.e. the vector that Wilbur uses to make a move from the square with a digit _i_u2009-u20091 in it. There are _q_ lines that follow. The _i_-th of them will contain a string _s__i_ consisting only of digits from 0 to 9. It is guaranteed that the total length of these _q_ strings won't exceed 1u2009000u2009000. Output For each of the _q_ strings, print "YES" if Wilbur can choose _x_ and _y_ in order to finish with this string after some finite number of moves. If it's impossible, than print "NO" for the corresponding string. Examples Input 1 1 2 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0000000000000 2413423432432 Input 4 2 5 01 23 45 67 0 1 0 -1 0 1 0 -1 0 1 0 -1 0 1 0 -1 0 1 0 -1 0000000000 010101011101 32232232322 44343222342444324 6767 Note In the first sample, there is a 1 by 1 table consisting of the only digit 0. The only move that can be made is staying on the square. The first string can be written on the white board by writing 0 repeatedly. The second string cannot be written as there is no 2 on the table.
2,500
false
false
false
true
false
false
false
false
false
true
7,461
567F
King of Berland Berl IV has recently died. Hail Berl V! As a sign of the highest achievements of the deceased king the new king decided to build a mausoleum with Berl IV's body on the main square of the capital. The mausoleum will be constructed from 2_n_ blocks, each of them has the shape of a cuboid. Each block has the bottom base of a 1u2009Γ—u20091 meter square. Among the blocks, exactly two of them have the height of one meter, exactly two have the height of two meters, ..., exactly two have the height of _n_ meters. The blocks are arranged in a row without spacing one after the other. Of course, not every arrangement of blocks has the form of a mausoleum. In order to make the given arrangement in the form of the mausoleum, it is necessary that when you pass along the mausoleum, from one end to the other, the heights of the blocks first were non-decreasing (i.e., increasing or remained the same), and then β€” non-increasing (decrease or remained unchanged). It is possible that any of these two areas will be omitted. For example, the following sequences of block height meet this requirement: [1,u20092,u20092,u20093,u20094,u20094,u20093,u20091]; [1,u20091]; [2,u20092,u20091,u20091]; [1,u20092,u20093,u20093,u20092,u20091]. Suddenly, _k_ more requirements appeared. Each of the requirements has the form: "_h_[_x__i_] sign_i_ _h_[_y__i_]", where _h_[_t_] is the height of the _t_-th block, and a sign_i_ is one of the five possible signs: '=' (equals), '<' (less than), '>' (more than), '<=' (less than or equals), '>=' (more than or equals). Thus, each of the _k_ additional requirements is given by a pair of indexes _x__i_, _y__i_ (1u2009≀u2009_x__i_,u2009_y__i_u2009≀u20092_n_) and sign sign_i_. Find the number of possible ways to rearrange the blocks so that both the requirement about the shape of the mausoleum (see paragraph 3) and the _k_ additional requirements were met. Input The first line of the input contains integers _n_ and _k_ (1u2009≀u2009_n_u2009≀u200935, 0u2009≀u2009_k_u2009≀u2009100) β€” the number of pairs of blocks and the number of additional requirements. Next _k_ lines contain listed additional requirements, one per line in the format "_x__i_ sign_i_ _y__i_" (1u2009≀u2009_x__i_,u2009_y__i_u2009≀u20092_n_), and the sign is on of the list of the five possible signs.
2,400
false
false
false
true
false
false
false
false
false
false
7,579
1709B
You are beta testing the new secret Terraria update. This update will add quests to the game! Simply, the world map can be represented as an array of length $$$n$$$, where the $$$i$$$-th column of the world has height $$$a_i$$$. There are $$$m$$$ quests you have to test. The $$$j$$$-th of them is represented by two integers $$$s_j$$$ and $$$t_j$$$. In this quest, you have to go from the column $$$s_j$$$ to the column $$$t_j$$$. At the start of the quest, you are appearing at the column $$$s_j$$$. In one move, you can go from the column $$$x$$$ to the column $$$x-1$$$ or to the column $$$x+1$$$. In this version, you have Spectre Boots, which allow you to fly. Since it is a beta version, they are bugged, so they only allow you to fly when you are going up and have infinite fly duration. When you are moving from the column with the height $$$p$$$ to the column with the height $$$q$$$, then you get some amount of fall damage. If the height $$$p$$$ is greater than the height $$$q$$$, you get $$$p - q$$$ fall damage, otherwise you fly up and get $$$0$$$ damage. For each of the given quests, determine the minimum amount of fall damage you can get during this quest. Input The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 10^5; 1 le m le 10^5$$$)xa0β€” the number of columns in the world and the number of quests you have to test, respectively. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$), where $$$a_i$$$ is the height of the $$$i$$$-th column of the world. The next $$$m$$$ lines describe quests. The $$$j$$$-th of them contains two integers $$$s_j$$$ and $$$t_j$$$ ($$$1 le s_j, t_j le n; s_j e t_j$$$), which means you have to move from the column $$$s_j$$$ to the column $$$t_j$$$ during the $$$j$$$-th quest. Note that $$$s_j$$$ can be greater than $$$t_j$$$.
900
false
false
true
true
true
false
false
false
false
false
2,025
1956C
The magical girl Nene has an $$$n imes n$$$ matrix $$$a$$$ filled with zeroes. The $$$j$$$-th element of the $$$i$$$-th row of matrix $$$a$$$ is denoted as $$$a_{i, j}$$$. She can perform operations of the following two types with this matrix: Type $$$1$$$ operation: choose an integer $$$i$$$ between $$$1$$$ and $$$n$$$ and a permutation $$$p_1, p_2, ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$. Assign $$$a_{i, j}:=p_j$$$ for all $$$1 le j le n$$$ simultaneously. Type $$$2$$$ operation: choose an integer $$$i$$$ between $$$1$$$ and $$$n$$$ and a permutation $$$p_1, p_2, ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$. Assign $$$a_{j, i}:=p_j$$$ for all $$$1 le j le n$$$ simultaneously. Nene wants to maximize the sum of all the numbers in the matrix $$$sumlimits_{i=1}^{n}sumlimits_{j=1}^{n}a_{i,j}$$$. She asks you to find the way to perform the operations so that this sum is maximized. As she doesn't want to make too many operations, you should provide a solution with no more than $$$2n$$$ operations. A permutation of length $$$n$$$ 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). Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 500$$$). The description of test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 le n le 500$$$)xa0β€” the size of the matrix $$$a$$$. It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$5 cdot 10^5$$$. Output For each test case, in the first line output two integers $$$s$$$ and $$$m$$$ ($$$0leq mleq 2n$$$)xa0β€” the maximum sum of the numbers in the matrix and the number of operations in your solution. In the $$$k$$$-th of the next $$$m$$$ lines output the description of the $$$k$$$-th operation: an integer $$$c$$$ ($$$c in {1, 2}$$$)xa0β€” the type of the $$$k$$$-th operation; an integer $$$i$$$ ($$$1 le i le n$$$)xa0β€” the row or the column the $$$k$$$-th operation is applied to; a permutation $$$p_1, p_2, ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$xa0β€” the permutation used in the $$$k$$$-th operation. Note that you don't need to minimize the number of operations used, you only should use no more than $$$2n$$$ operations. It can be shown that the maximum possible sum can always be obtained in no more than $$$2n$$$ operations. Example Output 1 1 1 1 1 7 3 1 1 1 2 1 2 1 2 2 1 1 2 Note In the first test case, the maximum sum $$$s=1$$$ can be obtained in $$$1$$$ operation by setting $$$a_{1, 1}:=1$$$. In the second test case, the maximum sum $$$s=7$$$ can be obtained in $$$3$$$ operations as follows: It can be shown that it is impossible to make the sum of the numbers in the matrix larger than $$$7$$$.
1,600
true
true
false
false
false
true
false
false
false
false
538
1238F
Assume that you have $$$k$$$ one-dimensional segments $$$s_1, s_2, dots s_k$$$ (each segment is denoted by two integers β€” its endpoints). Then you can build the following graph on these segments. The graph consists of $$$k$$$ vertexes, and there is an edge between the $$$i$$$-th and the $$$j$$$-th vertexes ($$$i eq j$$$) if and only if the segments $$$s_i$$$ and $$$s_j$$$ intersect (there exists at least one point that belongs to both of them). For example, if $$$s_1 = [1, 6], s_2 = [8, 20], s_3 = [4, 10], s_4 = [2, 13], s_5 = [17, 18]$$$, then the resulting graph is the following: A tree of size $$$m$$$ is good if it is possible to choose $$$m$$$ one-dimensional segments so that the graph built on these segments coincides with this tree. You are given a tree, you have to find its good subtree with maximum possible size. Recall that a subtree is a connected subgraph of a tree. Note that you have to answer $$$q$$$ independent queries. Input The first line contains one integer $$$q$$$ ($$$1 le q le 15 cdot 10^4$$$) β€” the number of the queries. The first line of each query contains one integer $$$n$$$ ($$$2 le n le 3 cdot 10^5$$$) β€” the number of vertices in the tree. Each of the next $$$n - 1$$$ lines contains two integers $$$x$$$ and $$$y$$$ ($$$1 le x, y le n$$$) denoting an edge between vertices $$$x$$$ and $$$y$$$. It is guaranteed that the given graph is a tree. It is guaranteed that the sum of all $$$n$$$ does not exceed $$$3 cdot 10^5$$$. Note In the first query there is a good subtree of size $$$8$$$. The vertices belonging to this subtree are $$${9, 4, 10, 2, 5, 1, 6, 3}$$$.
2,200
false
false
false
true
false
false
false
false
false
true
4,498
599A
Problem - 599A - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags implementation *800 No tag edit access β†’ Contest materials ") xa0β€” the lengths of the paths. _d_1 is the length of the path connecting Patrick's house and the first shop; _d_2 is the length of the path connecting Patrick's house and the second shop; _d_3 is the length of the path connecting both shops. Output Print the minimum distance that Patrick will have to walk in order to visit both shops and return to his house. Examples Input 10 20 30 Output 60 Input 1 1 5 Output 4 Note The first sample is shown on the picture in the problem statement. One of the optimal routes is: house first shop second shop house. In the second sample one of the optimal routes is: house first shop house second shop house.
800
false
false
true
false
false
false
false
false
false
false
7,451
1811G2
This is hard version of the problem, it differs from the easy one only by constraints on $$$n$$$ and $$$k$$$. Vlad found a row of $$$n$$$ tiles and the integer $$$k$$$. The tiles are indexed from left to right and the $$$i$$$-th tile has the color $$$c_i$$$. After a little thought, he decided what to do with it. You can start from any tile and jump to any number of tiles right, forming the path $$$p$$$. Let's call the path $$$p$$$ of length $$$m$$$ nice if: $$$p$$$ can be divided into blocks of length exactly $$$k$$$, that is, $$$m$$$ is divisible by $$$k$$$; $$$c_{p_1} = c_{p_2} = ldots = c_{p_k}$$$; $$$c_{p_{k+1}} = c_{p_{k+2}} = ldots = c_{p_{2k}}$$$; $$$ldots$$$ $$$c_{p_{m-k+1}} = c_{p_{m-k+2}} = ldots = c_{p_{m}}$$$; Your task is to find the number of nice paths of maximum length. Since this number may be too large, print it modulo $$$10^9 + 7$$$. Input The first line of each test contains the integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases in the test. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 5000$$$) β€” the number of tiles in a row and the length of the block. The second line of each test case contains $$$n$$$ integers $$$c_1, c_2, c_3, dots, c_n$$$ ($$$1 le c_i le n$$$) β€” tile colors. It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$25 cdot 10^6$$$. Output Print $$$t$$$ numbers, each of which is the answer to the corresponding test casexa0β€” the number of nice paths of maximum length modulo $$$10^9 + 7$$$. Example Input 5 5 2 1 2 3 4 5 7 2 1 3 1 3 3 1 3 11 4 1 1 1 1 1 1 1 1 1 1 1 5 2 1 1 2 2 2 5 1 1 2 3 4 5 Note In the first sample, it is impossible to make a nice path with a length greater than $$$0$$$. In the second sample, we are interested in the following paths: $$$1 ightarrow 3 ightarrow 4 ightarrow 5$$$ $$$2 ightarrow 4 ightarrow 5 ightarrow 7$$$ $$$1 ightarrow 3 ightarrow 5 ightarrow 7$$$ $$$1 ightarrow 3 ightarrow 4 ightarrow 7$$$ In the third example, any path of length $$$8$$$ is nice.
2,200
true
false
false
true
true
false
false
true
false
false
1,398
894C
In a dream Marco met an elderly man with a pair of black glasses. The man told him the key to immortality and then disappeared with the wind of time. When he woke up, he only remembered that the key was a sequence of positive integers of some length _n_, but forgot the exact sequence. Let the elements of the sequence be _a_1,u2009_a_2,u2009...,u2009_a__n_. He remembered that he calculated _gcd_(_a__i_,u2009_a__i_u2009+u20091,u2009...,u2009_a__j_) for every 1u2009≀u2009_i_u2009≀u2009_j_u2009≀u2009_n_ and put it into a set _S_. _gcd_ here means the xa0β€” the size of the set _S_. The second line contains _m_ integers _s_1,u2009_s_2,u2009...,u2009_s__m_ (1u2009≀u2009_s__i_u2009≀u2009106)xa0β€” the elements of the set _S_. It's guaranteed that the elements of the set are given in strictly increasing order, that means _s_1u2009<u2009_s_2u2009<u2009...u2009<u2009_s__m_. Output If there is no solution, print a single line containing -1. Otherwise, in the first line print a single integer _n_ denoting the length of the sequence, _n_ should not exceed 4000. In the second line print _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≀u2009_a__i_u2009≀u2009106)xa0β€” the sequence. We can show that if a solution exists, then there is a solution with _n_ not exceeding 4000 and _a__i_ not exceeding 106. If there are multiple solutions, print any of them. Note In the first example 2u2009=u2009_gcd_(4,u20096), the other elements from the set appear in the sequence, and we can show that there are no values different from 2, 4, 6 and 12 among _gcd_(_a__i_,u2009_a__i_u2009+u20091,u2009...,u2009_a__j_) for every 1u2009≀u2009_i_u2009≀u2009_j_u2009≀u2009_n_.
1,900
true
false
false
false
false
true
false
false
false
false
6,173
1883F
You are given an array of integers $$$a_1, a_2, ldots, a_n$$$. Calculate the number of subarrays of this array $$$1 leq l leq r leq n$$$, such that: The array $$$b = [a_l, a_{l+1}, ldots, a_r]$$$ occurs in the array $$$a$$$ as a subsequence exactly once. In other words, there is exactly one way to select a set of indices $$$1 leq i_1 < i_2 < ldots < i_{r - l + 1} leq n$$$, such that $$$b_j = a_{i_j}$$$ for all $$$1 leq j leq r - l + 1$$$. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$) β€” the number of test cases. This is followed by their description. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 10^5$$$) β€” the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output the number of suitable subarrays. Example Input 6 1 1 2 1 1 3 1 2 1 4 2 3 2 1 5 4 5 4 5 4 10 1 7 7 2 3 4 3 2 1 100 Note In the first test case, there is exactly one subarray $$$(1, 1)$$$ that suits us. In the second test case, there is exactly one subarray $$$(1, 2)$$$ that suits us. Subarrays $$$(1, 1)$$$ and $$$(2, 2)$$$ do not suit us, as the subsequence $$$[1]$$$ occurs twice in the array. In the third test case, all subarrays except $$$(1, 1)$$$ and $$$(3, 3)$$$ are suitable.
1,400
false
false
false
false
true
false
false
false
false
false
986
1400B
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course. You decided to rob a town's blacksmith and you take a follower with you. You can carry at most $$$p$$$ units and your followerxa0β€” at most $$$f$$$ units. In the blacksmith shop, you found $$$cnt_s$$$ swords and $$$cnt_w$$$ war axes. Each sword weights $$$s$$$ units and each war axexa0β€” $$$w$$$ units. You don't care what to take, since each of them will melt into one steel ingot. What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. The first line of each test case contains two integers $$$p$$$ and $$$f$$$ ($$$1 le p, f le 10^9$$$)xa0β€” yours and your follower's capacities. The second line of each test case contains two integers $$$cnt_s$$$ and $$$cnt_w$$$ ($$$1 le cnt_s, cnt_w le 2 cdot 10^5$$$)xa0β€” the number of swords and war axes in the shop. The third line of each test case contains two integers $$$s$$$ and $$$w$$$ ($$$1 le s, w le 10^9$$$)xa0β€” the weights of each sword and each war axe. It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed $$$2 cdot 10^5$$$. Output For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry. Example Input 3 33 27 6 10 5 6 100 200 10 10 5 5 1 19 1 3 19 5 Note In the first test case: you should take $$$3$$$ swords and $$$3$$$ war axes: $$$3 cdot 5 + 3 cdot 6 = 33 le 33$$$ and your followerxa0β€” $$$3$$$ swords and $$$2$$$ war axes: $$$3 cdot 5 + 2 cdot 6 = 27 le 27$$$. $$$3 + 3 + 3 + 2 = 11$$$ weapons in total. In the second test case, you can take all available weapons even without your follower's help, since $$$5 cdot 10 + 5 cdot 10 le 100$$$. In the third test case, you can't take anything, but your follower can take $$$3$$$ war axes: $$$3 cdot 5 le 19$$$.
1,700
true
true
false
false
false
false
true
false
false
false
3,671
1352F
For some binary string $$$s$$$ (i.e. each character $$$s_i$$$ is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length $$$2$$$ were written. For each pair (substring of length $$$2$$$), the number of '1' (ones) in it was calculated. You are given three numbers: $$$n_0$$$ β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$0$$$; $$$n_1$$$ β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$1$$$; $$$n_2$$$ β€” the number of such pairs of consecutive characters (substrings) where the number of ones equals $$$2$$$. For example, for the string $$$s=$$$"1110011110", the following substrings would be written: "11", "11", "10", "00", "01", "11", "11", "11", "10". Thus, $$$n_0=1$$$, $$$n_1=3$$$, $$$n_2=5$$$. Your task is to restore any suitable binary string $$$s$$$ from the given values $$$n_0, n_1, n_2$$$. It is guaranteed that at least one of the numbers $$$n_0, n_1, n_2$$$ is greater than $$$0$$$. Also, it is guaranteed that a solution exists. Input The first line contains an integer $$$t$$$ ($$$1 le t le 1000$$$) β€” the number of test cases in the input. Then test cases follow. Each test case consists of one line which contains three integers $$$n_0, n_1, n_2$$$ ($$$0 le n_0, n_1, n_2 le 100$$$; $$$n_0 + n_1 + n_2 > 0$$$). It is guaranteed that the answer for given $$$n_0, n_1, n_2$$$ exists.
1,500
true
false
false
false
false
true
false
false
false
false
3,947
1221A
You are playing a variation of game 2048. Initially you have a multiset $$$s$$$ of $$$n$$$ integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset. During each operation you choose two equal integers from $$$s$$$, remove them from $$$s$$$ and insert the number equal to their sum into $$$s$$$. For example, if $$$s = {1, 2, 1, 1, 4, 2, 2}$$$ and you choose integers $$$2$$$ and $$$2$$$, then the multiset becomes $$${1, 1, 1, 4, 4, 2}$$$. You win if the number $$$2048$$$ belongs to your multiset. For example, if $$$s = {1024, 512, 512, 4}$$$ you can win as follows: choose $$$512$$$ and $$$512$$$, your multiset turns into $$${1024, 1024, 4}$$$. Then choose $$$1024$$$ and $$$1024$$$, your multiset turns into $$${2048, 4}$$$ and you win. You have to determine if you can win this game. You have to answer $$$q$$$ independent queries. Input The first line contains one integer $$$q$$$ ($$$1 le q le 100$$$) – the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 le n le 100$$$) β€” the number of elements in multiset. The second line of each query contains $$$n$$$ integers $$$s_1, s_2, dots, s_n$$$ ($$$1 le s_i le 2^{29}$$$) β€” the description of the multiset. It is guaranteed that all elements of the multiset are powers of two. Output For each query print YES if it is possible to obtain the number $$$2048$$$ in your multiset, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). Note In the first query you can win as follows: choose $$$512$$$ and $$$512$$$, and $$$s$$$ turns into $$${1024, 64, 1024}$$$. Then choose $$$1024$$$ and $$$1024$$$, and $$$s$$$ turns into $$${2048, 64}$$$ and you win. In the second query $$$s$$$ contains $$$2048$$$ initially.
1,000
true
true
false
false
false
false
true
false
false
false
4,566
1466H
Getting so far in this contest is not an easy feat. By solving all the previous problems, you have impressed the gods greatly. Thus, they decided to spare you the story for this problem and grant a formal statement instead. Consider $$$n$$$ agents. Each one of them initially has exactly one item, $$$i$$$-th agent has the item number $$$i$$$. We are interested in reassignments of these items among the agents. An assignment is valid iff each item is assigned to exactly one agent, and each agent is assigned exactly one item. Each agent has a preference over the items, which can be described by a permutation $$$p$$$ of items sorted from the most to the least desirable. In other words, the agent prefers item $$$i$$$ to item $$$j$$$ iff $$$i$$$ appears earlier in the permutation $$$p$$$. A preference profile is a list of $$$n$$$ permutations of length $$$n$$$ each, such that $$$i$$$-th permutation describes preferences of the $$$i$$$-th agent. It is possible that some of the agents are not happy with the assignment of items. A set of dissatisfied agents may choose not to cooperate with other agents. In such a case, they would exchange the items they possess initially ($$$i$$$-th item belongs to $$$i$$$-th agent) only between themselves. Agents from this group don't care about the satisfaction of agents outside of it. However, they need to exchange their items in such a way that will make at least one of them happier, and none of them less happy (in comparison to the given assignment). Formally, consider a valid assignment of itemsxa0β€” $$$A$$$. Let $$$A(i)$$$ denote the item assigned to $$$i$$$-th agent. Also, consider a subset of agents. Let $$$S$$$ be the set of their indices. We will say this subset of agents is dissatisfied iff there exists a valid assignment $$$B(i)$$$ such that: For each $$$i in S$$$, $$$B(i) in S$$$. No agent $$$i in S$$$ prefers $$$A(i)$$$ to $$$B(i)$$$ (no agent from the $$$S$$$ is less happy). At least one agent $$$i in S$$$ prefers $$$B(i)$$$ to $$$A(i)$$$ (at least one agent from the $$$S$$$ is happier). An assignment is optimal if no subset of the agents is dissatisfied. Note that the empty subset cannot be dissatisfied. It can be proven that for each preference profile, there is precisely one optimal assignment. Example: Consider $$$3$$$ agents with the following preference profile: 1. $$$[2, 1, 3]$$$ 2. $$$[1, 2, 3]$$$ 3. $$$[1, 3, 2]$$$ And such an assignment: First agent gets item $$$2$$$ Second agent gets item $$$3$$$. Third agent gets item $$$1$$$. See that the set of agents $$${1, 2}$$$ is dissatisfied, because they can reassign their (initial) items in the following way: First agent gets item $$$2$$$. Second agent gets item $$$1$$$. Third agent gets item $$$3$$$. This reassignment will make the second agent happier and make no difference to the first agent. As a result, the third agent got an item that is worse for him, but this does not prevent the set $$${1,2}$$$ from being dissatisfied (he is not in this set). The following assignment would be optimal: First agent gets item $$$2$$$. Second agent gets item $$$1$$$. Third agent gets item $$$3$$$. Given an assignment $$$A$$$, calculate the number of distinct preference profiles for which assignment $$$A$$$ is optimal. As the answer can be huge, output it modulo $$$10^9+7$$$. Two preference profiles are different iff they assign different preference permutations to any agent.
3,300
true
true
false
true
false
false
false
false
false
true
3,359
1659A
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of $$$n$$$ matches. In the end, it turned out Team Red won $$$r$$$ times and Team Blue won $$$b$$$ times. Team Blue was less skilled than Team Red, so $$$b$$$ was strictly less than $$$r$$$. You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length $$$n$$$ where the $$$i$$$-th character denotes who won the $$$i$$$-th match xa0β€” it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won $$$3$$$ times in a row, which is the maximum. You must find a string satisfying the above conditions. If there are multiple answers, print any. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$) xa0β€” the number of test cases. Each test case has a single line containing three integers $$$n$$$, $$$r$$$, and $$$b$$$ ($$$3 leq n leq 100$$$; $$$1 leq b < r leq n$$$, $$$r+b=n$$$). Output For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any. Examples Output RBRBRBR RRRBRR RRBRRBRRBRRBRRBRRBR Input 6 3 2 1 10 6 4 11 6 5 10 9 1 10 8 2 11 9 2 Output RBR RRBRBRBRBR RBRBRBRBRBR RRRRRBRRRR RRRBRRRBRR RRRBRRRBRRR Note The first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is $$$1$$$. We cannot minimize it any further. The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is $$$2$$$, given by RR at the beginning. We cannot minimize the answer any further.
1,000
true
true
true
false
false
true
false
false
false
false
2,332
313B
Problem - 313B - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags dp implementation *1100 No tag edit access β†’ Contest materials ") , consisting only of characters "." and "#" and _m_ queries. Each query is described by a pair of integers _l__i_,u2009_r__i_ (1u2009≀u2009_l__i_u2009<u2009_r__i_u2009≀u2009_n_). The answer to the query _l__i_,u2009_r__i_ is the number of such integers _i_ (_l__i_u2009≀u2009_i_u2009<u2009_r__i_), that _s__i_u2009=u2009_s__i_u2009+u20091. Ilya the Lion wants to help his friends but is there anyone to help him? Help Ilya, solve the problem. Input The first line contains string _s_ of length _n_ (2u2009≀u2009_n_u2009≀u2009105). It is guaranteed that the given string only consists of characters "." and "#". The next line contains integer _m_ (1u2009≀u2009_m_u2009≀u2009105) β€” the number of queries. Each of the next _m_ lines contains the description of the corresponding query. The _i_-th line contains integers _l__i_,u2009_r__i_ (1u2009≀u2009_l__i_u2009<u2009_r__i_u2009≀u2009_n_). Output Print _m_ integers β€” the answers to the queries in the order in which they are given in the input. Examples Input ...... 4 3 4 2 3 1 6 2 6 Output 1 1 5 4 Input #..### 5 1 3 5 6 1 5 3 6 3 4 Output 1 1 2 2 0
1,100
false
false
true
true
false
false
false
false
false
false
8,594
1910G
Alice and Bob are swimming in the pool under the guidance of their instructor Monocarp. The pool can be represented as a segment on the OX-axis from $$$0$$$ to $$$50$$$. Both Alice and Bob started at moment $$$0$$$ at point $$$0$$$ with positive real speeds $$$v_A$$$ and $$$v_B$$$ correspondingly. Both Alice and Bob swim from $$$0$$$ to point $$$50$$$, then instantly turn back and swim from $$$50$$$ to $$$0$$$. At $$$0$$$ they turn back again and swim to $$$50$$$ and so on. Monocarp logged all valuable info about Alice and Bob, including moments of time when they met at the same point (Alice and Bob swim on parallel lanes, so they don't bother each other). Let's name that moments of time as sequence $$$t_1, t_2, dots, t_n$$$. Due to some incident, Monocarp lost almost all data he recorded, and all he has now is array $$$t$$$. Monocarp remembers that Alice and Bob had distinct positive real swimming speeds $$$v_A$$$ and $$$v_B$$$ ($$$v_A > 0$$$; $$$v_B > 0$$$; $$$v_A eq v_B$$$) and that sequence $$$t$$$ contains the first $$$n$$$ meeting moments. But looking at sequence $$$t$$$ he noticed that all $$$t_i$$$ are integer values, and now he doubts is sequence $$$t$$$ valid or there are some errors in it. Help Monocarp to check array $$$t$$$! Input The first line contains a single integer $$$c$$$ ($$$1 le c le 10^4$$$)xa0β€” the number of test cases. Then $$$c$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0β€” the size of array $$$t$$$. The second line of each test case contains $$$n$$$ integers $$$t_1, t_2, dots, t_n$$$ ($$$1 le t_1 < t_2 < dots < t_n le 10^9$$$)xa0β€” the meeting moments in the increasing order. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print VALID if the array $$$t$$$ is a valid array, or (in other words) exist such real values $$$v_A$$$ and $$$v_B$$$ ($$$v_A, v_B > 0$$$; $$$v_A eq v_B$$$) that array $$$t$$$ contains the first $$$n$$$ meeting moments of Alice and Bob in the pool. Otherwise, print INVALID. You can output the answer in any case (upper or lower). For example, the strings "vALid", "valid", "Valid", and "VALID" will be recognized as positive responses. Example Input 7 1 42 4 3 4 6 8 5 1 2 3 4 5 3 999999998 999999999 1000000000 5 4 6 8 12 16 4 6 11 12 14 2 10 30 Output VALID VALID VALID INVALID VALID INVALID INVALID Note In the first test case, imagine a situation: $$$v_A = 1$$$ and $$$v_B = frac{29}{21}$$$. Then, at moment $$$42$$$ Alice and Bob will be at point $$$42$$$. In the second test case, imagine a situation: $$$v_A = frac{175}{6}$$$ and $$$v_B = frac{25}{6}$$$. Then, at moment $$$3$$$ Alice and Bob will meet at point $$$12.5$$$, at $$$4$$$ they'll meet at point $$$frac{50}{3}$$$, at moment $$$6$$$ they'll meet at $$$25$$$ and at moment $$$8$$$xa0β€” at point $$$frac{100}{3}$$$. Since it's exactly the first $$$4$$$ meeting moments. Array $$$t$$$ may be valid. In the third test case, one of the possible situations is $$$v_A = 25$$$ and $$$v_B = 75$$$.
2,700
false
true
false
false
false
false
false
false
false
false
841
1186F
Vus the Cossack has a simple graph with $$$n$$$ vertices and $$$m$$$ edges. Let $$$d_i$$$ be a degree of the $$$i$$$-th vertex. Recall that a degree of the $$$i$$$-th vertex is the number of conected edges to the $$$i$$$-th vertex. He needs to remain not more than $$$lceil frac{n+m}{2} ceil$$$ edges. Let $$$f_i$$$ be the degree of the $$$i$$$-th vertex after removing. He needs to delete them in such way so that $$$lceil frac{d_i}{2} ceil leq f_i$$$ for each $$$i$$$. In other words, the degree of each vertex should not be reduced more than twice. Help Vus to remain the needed edges! Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n leq 10^6$$$, $$$0 leq m leq 10^6$$$)xa0β€” the number of vertices and edges respectively. Each of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 leq u_i, v_i leq n$$$)xa0β€” vertices between which there is an edge. It is guaranteed that the graph does not have loops and multiple edges. It is possible to show that the answer always exists. Output In the first line, print one integer $$$k$$$ ($$$0 leq k leq lceil frac{n+m}{2} ceil$$$)xa0β€” the number of edges which you need to remain. In each of the next $$$k$$$ lines, print two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 leq u_i, v_i leq n$$$)xa0β€” the vertices, the edge between which, you need to remain. You can not print the same edge more than once. Examples Input 6 6 1 2 2 3 3 4 4 5 5 3 6 5 Output 5 2 1 3 2 5 3 5 4 6 5 Input 10 20 4 3 6 5 4 5 10 8 4 8 5 8 10 4 9 5 5 1 3 8 1 2 4 7 1 4 10 7 1 7 6 1 9 6 3 9 7 9 6 2 Output 12 2 1 4 1 5 4 6 5 7 1 7 4 8 3 8 5 9 3 9 6 10 4 10 7
2,400
false
true
true
false
false
false
false
false
false
true
4,767
773E
It's well-known that blog posts are an important part of Codeforces platform. Every blog post has a global characteristic changing over timexa0β€” its community rating. A newly created blog post's community rating is 0. Codeforces users may visit the blog post page and rate it, changing its community rating by +1 or -1. Consider the following model of Codeforces users' behavior. The _i_-th user has his own estimated blog post rating denoted by an integer _a__i_. When a user visits a blog post page, he compares his estimated blog post rating to its community rating. If his estimated rating is higher, he rates the blog post with +1 (thus, the blog post's community rating increases by 1). If his estimated rating is lower, he rates the blog post with -1 (decreasing its community rating by 1). If the estimated rating and the community rating are equal, user doesn't rate the blog post at all (in this case we'll say that user rates the blog post for 0). In any case, after this procedure user closes the blog post page and never opens it again. Consider a newly created blog post with the initial community rating of 0. For each of _n_ Codeforces users, numbered from 1 to _n_, his estimated blog post rating _a__i_ is known. For each _k_ from 1 to _n_, inclusive, the following question is asked. Let users with indices from 1 to _k_, in some order, visit the blog post page, rate the blog post and close the page. Each user opens the blog post only after the previous user closes it. What could be the maximum possible community rating of the blog post after these _k_ visits? Input The first line contains a single integer _n_ (1u2009≀u2009_n_u2009≀u20095Β·105)xa0β€” the number of Codeforces users. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (u2009-u20095Β·105u2009≀u2009_a__i_u2009≀u20095Β·105)xa0β€” estimated blog post ratings for users in order from 1 to _n_. Output For each _k_ from 1 to _n_, output a single integer equal to the maximum possible community rating of the blog post after users with indices from 1 to _k_, in some order, visit the blog post page, rate the blog post, and close the page.
3,000
false
false
false
false
true
false
false
false
true
false
6,683
786E
ALT is a planet in a galaxy called "Encore". Humans rule this planet but for some reason there's no dog in their planet, so the people there are sad and depressed. Rick and Morty are universal philanthropists and they want to make people in ALT happy. ALT has _n_ cities numbered from 1 to _n_ and _n_u2009-u20091 bidirectional roads numbered from 1 to _n_u2009-u20091. One can go from any city to any other city using these roads. There are two types of people in ALT: 1. Guardians. A guardian lives in a house alongside a road and guards the road. 2. Citizens. A citizen lives in a house inside a city and works in an office in another city. Every person on ALT is either a guardian or a citizen and there's exactly one guardian alongside each road. Rick and Morty talked to all the people in ALT, and here's what they got: There are _m_ citizens living in ALT. Citizen number _i_ lives in city number _x__i_ and works in city number _y__i_. Every day each citizen will go through all roads along the shortest path from his home to his work. A citizen will be happy if and only if either he himself has a puppy himself or all of guardians along his path to his work has a puppy (he sees the guardian's puppy in each road and will be happy). A guardian is always happy. You need to tell Rick and Morty the minimum number of puppies they need in order to make all people in ALT happy, and also provide an optimal way to distribute these puppies. Input The first line of input contains two integers _n_ and _m_ (2u2009≀u2009_n_u2009≀u20092u2009Γ—u2009104, 1u2009≀u2009_m_u2009≀u2009104) β€” number of cities and number of citizens respectively. The next _n_u2009-u20091 lines contain the roads, _i_-th line contains endpoint of _i_-th edge, _v_ and _u_ (1u2009≀u2009_v_,u2009_u_u2009≀u2009_n_, _v_u2009β‰ u2009_u_). The next _m_ lines contain the information about citizens. _i_-th line contains two integers _x__i_ and _y__i_ (1u2009≀u2009_x__i_,u2009_y__i_u2009≀u2009_n_, _x__i_u2009β‰ u2009_y__i_). Output In the first line of input print a single integer _k_, the total number of puppies they need (1u2009≀u2009_k_u2009≀u2009_n_). In the second line print an integer _q_, the number of puppies to give to citizens, followed by _q_ distinct integers _a_1,u2009_a_2,u2009...,u2009_a__q_, index of citizens to give puppy to (0u2009≀u2009_q_u2009≀u2009_min_(_m_,u2009_k_), 1u2009≀u2009_a__i_u2009≀u2009_m_). In the third line print an integer _e_, the number of puppies to give to guardians, followed by _e_ distinct integers _b_1,u2009_b_2,u2009...,u2009_b__e_, index of road of guardians to give puppy to (0u2009≀u2009_e_u2009≀u2009_min_(_n_u2009-u20091,u2009_k_), 1u2009≀u2009_b__i_u2009≀u2009_n_u2009-u20091). Sum of _q_ and _e_ should be equal to _k_. Examples Input 4 5 2 4 3 4 1 4 2 4 2 1 2 4 1 2 2 3 Input 4 7 3 4 1 4 2 1 4 2 4 2 2 4 1 4 2 1 3 1 4 2 Note Map of ALT in the first sample testcase (numbers written on a road is its index): Map of ALT in the second sample testcase (numbers written on a road is its index):
3,200
false
false
false
false
true
false
false
false
false
true
6,624
1878B
After the Serbian Informatics Olympiad, Aleksa was very sad, because he didn't win a medal (he didn't know stack), so Vasilije came to give him an easy problem, just to make his day better. Vasilije gave Aleksa a positive integer $$$n$$$ ($$$n ge 3$$$) and asked him to construct a strictly increasing array of size $$$n$$$ of positive integers, such that $$$3cdot a_{i+2}$$$ is not divisible by $$$a_i+a_{i+1}$$$ for each $$$i$$$ ($$$1le i le n-2$$$). Note that a strictly increasing array $$$a$$$ of size $$$n$$$ is an array where $$$a_i < a_{i+1}$$$ for each $$$i$$$ ($$$1 le i le n-1$$$). Since Aleksa thinks he is a bad programmer now, he asked you to help him find such an array. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 le n le 2 cdot 10^5$$$)xa0β€” the number of elements in array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output $$$n$$$ integers $$$a_1, a_2, a_3, dots, a_n$$$ ($$$1 le a_i le 10^9$$$). It can be proved that the solution exists for any $$$n$$$. If there are multiple solutions, output any of them. Example Output 6 8 12 7 11 14 20 22 100 9 15 18 27 36 90 120 Note In the first test case, $$$a_1=6$$$, $$$a_2=8$$$, $$$a_3=12$$$, so $$$a_1+a_2=14$$$ and $$$3 cdot a_3=36$$$, so $$$3 cdot a_3$$$ is not divisible by $$$a_1+a_2$$$.
800
true
false
false
false
false
true
false
false
false
false
1,016
1903D2
This is the hard version of the problem. The only difference between the two versions is the constraint on $$$n$$$ and $$$q$$$, the memory and time limits. You can make hacks only if all versions of the problem are solved. Theofanis really likes to play with the bits of numbers. He has an array $$$a$$$ of size $$$n$$$ and an integer $$$k$$$. He can make at most $$$k$$$ operations in the array. In each operation, he picks a single element and increases it by $$$1$$$. He found the maximum xa0β€” the size of the array and the number of queries. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^6$$$)xa0β€” the elements of the array. Next $$$q$$$ lines describe the queries. The $$$i$$$-th line contains one integer $$$k_i$$$ ($$$0 le k_i le 10^{18}$$$)xa0β€” the number of operations that can be done in the $$$i$$$-th query. Output For each query, print one integerxa0β€” the maximum bitwise AND that array $$$a$$$ can have after at most $$$k_i$$$ operations. Note In the first test case, in the first query, we add $$$1$$$ in the first and last elements of the array. Thus, the array becomes $$$[2,3,7,6]$$$ with bitwise AND equal to $$$2$$$. In the second test case, in the first query, we add $$$1$$$ in the first element, $$$5$$$ in the second, and $$$3$$$ in the third and now all the elements are equal to $$$5$$$.
2,500
false
true
false
true
false
false
false
false
false
false
893
588B
Problem - 588B - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags math *1300 No tag edit access . As a birthday present, Malek wants to give her a lovely number from his store. He wants this number to be as big as possible. Malek always had issues in math, so he asked for your help. Please tell him what is the biggest lovely number in his store. Input The first and only line of input contains one integer, _n_ (1u2009≀u2009_n_u2009≀u20091012). Output Print the answer in one line. Examples Input 10 Output 10 Input 12 Output 6 Note In first sample case, there are numbers 1, 2, 5 and 10 in the shop. 10 isn't divisible by any perfect square, so 10 is lovely. In second sample case, there are numbers 1, 2, 3, 4, 6 and 12 in the shop. 12 is divisible by 4u2009=u200922, so 12 is not lovely, while 6 is indeed lovely.
1,300
true
false
false
false
false
false
false
false
false
false
7,490
1060A
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have $$$n$$$ cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer $$$n$$$xa0β€” the number of cards with digits that you have ($$$1 leq n leq 100$$$). The second line contains a string of $$$n$$$ digits (characters "0", "1", ..., "9") $$$s_1, s_2, ldots, s_n$$$. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 22 0011223344556677889988 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
800
false
false
false
false
false
false
true
false
false
false
5,416
653B
Limak is a little polar bear. Polar bears hate long strings and thus they like to compress them. You should also know that Limak is so young that he knows only first six letters of the English alphabet: 'a', 'b', 'c', 'd', 'e' and 'f'. You are given a set of _q_ possible operations. Limak can perform them in any order, any operation may be applied any number of times. The _i_-th operation is described by a string _a__i_ of length two and a string _b__i_ of length one. No two of _q_ possible operations have the same string _a__i_. When Limak has a string _s_ he can perform the _i_-th operation on _s_ if the first two letters of _s_ match a two-letter string _a__i_. Performing the _i_-th operation removes first two letters of _s_ and inserts there a string _b__i_. See the notes section for further clarification. You may note that performing an operation decreases the length of a string _s_ exactly by 1. Also, for some sets of operations there may be a string that cannot be compressed any further, because the first two letters don't match any _a__i_. Limak wants to start with a string of length _n_ and perform _n_u2009-u20091 operations to finally get a one-letter string "a". In how many ways can he choose the starting string to be able to get "a"? Remember that Limak can use only letters he knows. Input The first line contains two integers _n_ and _q_ (2u2009≀u2009_n_u2009≀u20096, 1u2009≀u2009_q_u2009≀u200936)xa0β€” the length of the initial string and the number of available operations. The next _q_ lines describe the possible operations. The _i_-th of them contains two strings _a__i_ and _b__i_ (_a__i_u2009=u20092,u2009_b__i_u2009=u20091). It's guaranteed that _a__i_u2009β‰ u2009_a__j_ for _i_u2009β‰ u2009_j_ and that all _a__i_ and _b__i_ consist of only first six lowercase English letters. Output Print the number of strings of length _n_ that Limak will be able to transform to string "a" by applying only operations given in the input. Examples Input 3 5 ab a cc c ca a ee c ff d Input 2 8 af e dc d cc f bc b da b eb a bb b ff c Note In the first sample, we count initial strings of length 3 from which Limak can get a required string "a". There are 4 such strings: "abb", "cab", "cca", "eea". The first one Limak can compress using operation 1 two times (changing "ab" to a single "a"). The first operation would change "abb" to "ab" and the second operation would change "ab" to "a". Other three strings may be compressed as follows: In the second sample, the only correct initial string is "eb" because it can be immediately compressed to "a".
1,300
false
false
false
true
false
false
true
false
false
false
7,203
1214F
Two large companies "Cecsi" and "Poca Pola" are fighting against each other for a long time. In order to overcome their competitor, "Poca Pola" started a super secret project, for which it has total $$$n$$$ vacancies in all of their offices. After many tests and interviews $$$n$$$ candidates were selected and the only thing left was their employment. Because all candidates have the same skills, it doesn't matter where each of them will work. That is why the company decided to distribute candidates between workplaces so that the total distance between home and workplace over all candidates is minimal. It is well known that Earth is round, so it can be described as a circle, and all $$$m$$$ cities on Earth can be described as points on this circle. All cities are enumerated from $$$1$$$ to $$$m$$$ so that for each $$$i$$$ ($$$1 le i le m - 1$$$) cities with indexes $$$i$$$ and $$$i + 1$$$ are neighbors and cities with indexes $$$1$$$ and $$$m$$$ are neighbors as well. People can move only along the circle. The distance between any two cities equals to minimal number of transitions between neighboring cities you have to perform to get from one city to another. In particular, the distance between the city and itself equals $$$0$$$. The "Poca Pola" vacancies are located at offices in cities $$$a_1, a_2, ldots, a_n$$$. The candidates live in cities $$$b_1, b_2, ldots, b_n$$$. It is possible that some vacancies are located in the same cities and some candidates live in the same cities. The "Poca Pola" managers are too busy with super secret project, so you were asked to help "Poca Pola" to distribute candidates between workplaces, so that the sum of the distance between home and workplace over all candidates is minimum possible. Input The first line contains two integers $$$m$$$ and $$$n$$$ ($$$1 le m le 10^9$$$, $$$1 le n le 200,000$$$)xa0β€” the number of cities on Earth and the number of vacancies. The second line contains $$$n$$$ integers $$$a_1, a_2, a_3, ldots, a_n$$$ ($$$1 le a_i le m$$$)xa0β€” the cities where vacancies are located. The third line contains $$$n$$$ integers $$$b_1, b_2, b_3, ldots, b_n$$$ ($$$1 le b_i le m$$$)xa0β€” the cities where the candidates live. Output The first line should contain the minimum total distance between home and workplace over all candidates. The second line should contain $$$n$$$ different integers from $$$1$$$ to $$$n$$$. The $$$i$$$-th of them should be the index of candidate that should work at $$$i$$$-th workplace. Note In the first example, the distance between each candidate and his workplace equals to $$$1$$$ (from $$$1$$$ to $$$10$$$, from $$$4$$$ to $$$5$$$ and from $$$6$$$ to $$$5$$$). In the second example: The second candidate works at first workplace, the distance between cities $$$3$$$ and $$$1$$$ equals to $$$2$$$. The third candidate works at second workplace, the distance between cities $$$6$$$ and $$$4$$$ equals to $$$2$$$. The first candidate works at third workplace, the distance between cities $$$8$$$ and $$$8$$$ equals to $$$0$$$.
2,700
false
true
false
false
false
false
false
false
true
false
4,606
2014D
Impress thy brother, yet fret not thy mother. Robin's brother and mother are visiting, and Robin gets to choose the start day for each visitor. All days are numbered from $$$1$$$ to $$$n$$$. Visitors stay for $$$d$$$ continuous days, all of those $$$d$$$ days must be between day $$$1$$$ and $$$n$$$ inclusive. Robin has a total of $$$k$$$ risky 'jobs' planned. The $$$i$$$-th job takes place between days $$$l_i$$$ and $$$r_i$$$ inclusive, for $$$1 le i le k$$$. If a job takes place on any of the $$$d$$$ days, the visit overlaps with this job (the length of overlap is unimportant). Robin wants his brother's visit to overlap with the maximum number of distinct jobs, and his mother's the minimum. Find suitable start days for the visits of Robin's brother and mother. If there are multiple suitable days, choose the earliest one. Input The first line of the input contains a single integer $$$t$$$ ($$$1leq t leq 10^4$$$)xa0β€” the number of test cases. The first line of each test case consists of three integers $$$n$$$, $$$d$$$, $$$k$$$ ($$$1 le n le 10^5, 1 le d, k le n$$$)xa0β€” the number of total days, duration of the visits, and the number of jobs. Then follow $$$k$$$ lines of each test case, each with two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le n$$$)xa0β€” the start and end day of each job. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output two integers, the best starting days of Robin's brother and mother respectively. Both visits must fit between day $$$1$$$ and $$$n$$$ inclusive. Example Input 6 2 1 1 1 2 4 1 2 1 2 2 4 7 2 3 1 2 1 3 6 7 5 1 2 1 2 3 5 9 2 1 2 8 9 2 4 7 9 4 8 1 3 2 3 Output 1 1 2 1 1 4 1 1 1 1 3 4 Note In the first test case, the only job fills all $$$2$$$ days, both should visit on day $$$1$$$. In the second test case, day $$$2$$$ overlaps with $$$2$$$ jobs and day $$$1$$$ overlaps with only $$$1$$$. In the third test case, Robert visits for days $$$[1,2]$$$, Mrs. Hood visits for days $$$[4,5]$$$.
1,400
false
true
false
false
true
false
true
false
true
false
168
906D
Priests of the Quetzalcoatl cult want to build a tower to represent a power of their god. Tower is usually made of power-charged rocks. It is built with the help of rare magic by levitating the current top of tower and adding rocks at its bottom. If top, which is built from _k_u2009-u20091 rocks, possesses power _p_ and we want to add the rock charged with power _w__k_ then value of power of a new tower will be {_w__k_}_p_. Rocks are added from the last to the first. That is for sequence _w_1,u2009...,u2009_w__m_ value of power will be After tower is built, its power may be extremely large. But still priests want to get some information about it, namely they want to know a number called cumulative power which is the true value of power taken modulo _m_. Priests have _n_ rocks numbered from 1 to _n_. They ask you to calculate which value of cumulative power will the tower possess if they will build it from rocks numbered _l_,u2009_l_u2009+u20091,u2009...,u2009_r_. Input First line of input contains two integers _n_ (1u2009≀u2009_n_u2009≀u2009105) and _m_ (1u2009≀u2009_m_u2009≀u2009109). Second line of input contains _n_ integers _w__k_ (1u2009≀u2009_w__k_u2009≀u2009109) which is the power of rocks that priests have. Third line of input contains single integer _q_ (1u2009≀u2009_q_u2009≀u2009105) which is amount of queries from priests to you. _k__th_ of next _q_ lines contains two integers _l__k_ and _r__k_ (1u2009≀u2009_l__k_u2009≀u2009_r__k_u2009≀u2009_n_). Output Output _q_ integers. _k_-th of them must be the amount of cumulative power the tower will have if is built from rocks _l__k_,u2009_l__k_u2009+u20091,u2009...,u2009_r__k_. Example Input 6 1000000000 1 2 2 3 3 3 8 1 1 1 6 2 2 2 3 2 4 4 4 4 5 4 6 Output 1 1 2 4 256 3 27 597484987
2,700
true
false
false
false
false
false
false
false
false
false
6,124
1163C1
This problem is same as the next one, but has smaller constraints. It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic. At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates $$$(x_i, y_i)$$$. Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire. Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem? Input The first line contains a single integer $$$n$$$ ($$$2 le n le 50$$$)xa0β€” the number of electric poles. Each of the following $$$n$$$ lines contains two integers $$$x_i$$$, $$$y_i$$$ ($$$-10^4 le x_i, y_i le 10^4$$$)xa0β€” the coordinates of the poles. It is guaranteed that all of these $$$n$$$ points are distinct. Note In the first example: In the second example: Note that the three poles $$$(0, 0)$$$, $$$(0, 2)$$$ and $$$(0, 4)$$$ are connected by a single wire. In the third example:
1,900
false
false
false
false
false
false
true
false
false
false
4,901
1285A
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. Some commands are sent successfully and some are ignored. If the command is ignored then the position $$$x$$$ doesn't change and Mezo simply proceeds to the next command. For example, if Mezo sends commands "LRLR", then here are some possible outcomes (underlined commands are sent successfully): "LRLR" β€” Zoma moves to the left, to the right, to the left again and to the right for the final time, ending up at position $$$0$$$; "LRLR" β€” Zoma recieves no commands, doesn't move at all and ends up at position $$$0$$$ as well; "LRLR" β€” Zoma moves to the left, then to the left again and ends up in position $$$-2$$$. Mezo doesn't know which commands will be sent successfully beforehand. Thus, he wants to know how many different positions may Zoma end up at. Input 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). Output Print one integer β€” the number of different positions Zoma may end up at. Note In the example, Zoma may end up anywhere between $$$-2$$$ and $$$2$$$.
800
true
false
false
false
false
false
false
false
false
false
4,267
1586I
Omkar is creating a mosaic using colored square tiles, which he places in an $$$n imes n$$$ grid. When the mosaic is complete, each cell in the grid will have either a glaucous or sinoper tile. However, currently he has only placed tiles in some cells. A completed mosaic will be a mastapeece if and only if each tile is adjacent to exactly $$$2$$$ tiles of the same color ($$$2$$$ tiles are adjacent if they share a side.) Omkar wants to fill the rest of the tiles so that the mosaic becomes a mastapeece. Now he is wondering, is the way to do this unique, and if it is, what is it? Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 2000$$$). Then follow $$$n$$$ lines with $$$n$$$ characters in each line. The $$$i$$$-th character in the $$$j$$$-th line corresponds to the cell in row $$$i$$$ and column $$$j$$$ of the grid, and will be $$$S$$$ if Omkar has placed a sinoper tile in this cell, $$$G$$$ if Omkar has placed a glaucous tile, $$$.$$$ if it's empty. Output On the first line, print UNIQUE if there is a unique way to get a mastapeece, NONE if Omkar cannot create any, and MULTIPLE if there is more than one way to do so. All letters must be uppercase. If you print UNIQUE, then print $$$n$$$ additional lines with $$$n$$$ characters in each line, such that the $$$i$$$-th character in the $$$j^{ ext{th}}$$$ line is $$$S$$$ if the tile in row $$$i$$$ and column $$$j$$$ of the mastapeece is sinoper, and $$$G$$$ if it is glaucous. Examples Input 4 S... ..G. .... ...S Input 6 S..... ....G. ..S... .....S ....G. G..... Input 10 .S....S... .......... ...SSS.... .......... .......... ...GS..... ....G...G. .......... ......G... .......... Output UNIQUE SSSSSSSSSS SGGGGGGGGS SGSSSSSSGS SGSGGGGSGS SGSGSSGSGS SGSGSSGSGS SGSGGGGSGS SGSSSSSSGS SGGGGGGGGS SSSSSSSSSS Note For the first test case, Omkar can make the mastapeeces SSSS SGGS SGGS SSSS and SSGG SSGG GGSS GGSS. For the second test case, it can be proven that it is impossible for Omkar to add tiles to create a mastapeece. For the third case, it can be proven that the given mastapeece is the only mastapeece Omkar can create by adding tiles. For the fourth test case, it's clearly impossible for the only tile in any mosaic Omkar creates to be adjacent to two tiles of the same color, as it will be adjacent to $$$0$$$ tiles total.
3,500
true
false
false
false
false
true
false
false
false
false
2,695
568A
Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and unpredictable. A palindromic number is another matter. It is aesthetically pleasing, and it has a number of remarkable properties. Help Rikhail to convince the scientific community in this! Let us remind you that a number is called prime if it is integer larger than one, and is not divisible by any positive integer other than itself and one. Rikhail calls a number a palindromic if it is integer, positive, and its decimal representation without leading zeros is a palindrome, i.e. reads the same from left to right and right to left. One problem with prime numbers is that there are too many of them. Let's introduce the following notation: Ο€(_n_)xa0β€” the number of primes no larger than _n_, _rub_(_n_)xa0β€” the number of palindromic numbers no larger than _n_. Rikhail wants to prove that there are a lot more primes than palindromic ones. He asked you to solve the following problem: for a given value of the coefficient _A_ find the maximum _n_, such that Ο€(_n_)u2009≀u2009_A_Β·_rub_(_n_). Input The input consists of two positive integers _p_, _q_, the numerator and denominator of the fraction that is the value of _A_xa0(,xa0). Output If such maximum number exists, then print it. Otherwise, print "Palindromic tree is better than splay tree" (without the quotes).
1,600
true
false
true
false
false
false
true
false
false
false
7,578
1458A
Problem - 1458A - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags math number theory *1600 No tag edit access β†’ Contest materials ") . The second line contains $$$n$$$ integers $$$a_1, ldots, a_n$$$ ($$$1 leq a_i leq 10^{18})$$$. The third line contains $$$m$$$ integers $$$b_1, ldots, b_m$$$ ($$$1 leq b_j leq 10^{18})$$$. Output Print $$$m$$$ integers. The $$$j$$$-th of them should be equal to GCD$$$(a_1 + b_j, ldots, a_n + b_j)$$$. Example Input 4 4 1 25 121 169 1 2 7 23 Output 2 3 8 24
1,600
true
false
false
false
false
false
false
false
false
false
3,394
1811E
Problem - 1811E - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags binary search dp math number theory *1500 No tag edit access β†’ Contest materials ") , you need to answer $$$a_k = 5$$$. Input The first line of input data contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. In the only line of each test case, there is a single integer $$$k$$$ ($$$1 le k le 10^{12}$$$)xa0β€” the position Bob is interested in. Output For each test case, print on a separate line the number $$$a_k$$$ in indexing with $$$1$$$. Example Input 7 3 5 22 10 100 12345 827264634912 Output 3 6 25 11 121 18937 2932285320890
1,500
true
false
false
true
false
false
false
true
false
false
1,401
141C
In the Main Berland Bank _n_ people stand in a queue at the cashier, everyone knows his/her height _h__i_, and the heights of the other people in the queue. Each of them keeps in mind number _a__i_ β€” how many people who are taller than him/her and stand in queue in front of him. After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order. When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number _a__i_. Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers _h__i_ β€” the heights of people in the queue, so that the numbers _a__i_ are correct. Input The first input line contains integer _n_ β€” the number of people in the queue (1u2009≀u2009_n_u2009≀u20093000). Then _n_ lines contain descriptions of the people as "_name__i_ _a__i_" (one description on one line), where _name__i_ is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the _i_-th person's name), _a__i_ is an integer (0u2009≀u2009_a__i_u2009≀u2009_n_u2009-u20091), that represents the number of people who are higher and stand in the queue in front of person _i_. It is guaranteed that all names are different. Output If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in _n_ lines the people as "_name__i_ _h__i_", where _h__i_ is the integer from 1 to 109 (inclusive), the possible height of a man whose name is _name__i_. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers _h__i_ are not necessarily unique. Examples Output a 150 c 170 d 180 b 160 Input 4 vasya 0 petya 1 manya 3 dunay 3
1,800
false
true
false
false
false
true
false
false
true
false
9,310
1009C
Alice got an array of length $$$n$$$ as a birthday present once again! This is the third year in a row! And what is more disappointing, it is overwhelmengly boring, filled entirely with zeros. Bob decided to apply some changes to the array to cheer up Alice. Bob has chosen $$$m$$$ changes of the following form. For some integer numbers $$$x$$$ and $$$d$$$, he chooses an arbitrary position $$$i$$$ ($$$1 le i le n$$$) and for every $$$j in [1, n]$$$ adds $$$x + d cdot dist(i, j)$$$ to the value of the $$$j$$$-th cell. $$$dist(i, j)$$$ is the distance between positions $$$i$$$ and $$$j$$$ (i.e. $$$dist(i, j) = i - j$$$, where $$$x$$$ is an absolute value of $$$x$$$). For example, if Alice currently has an array $$$[2, 1, 2, 2]$$$ and Bob chooses position $$$3$$$ for $$$x = -1$$$ and $$$d = 2$$$ then the array will become $$$[2 - 1 + 2 cdot 2,~1 - 1 + 2 cdot 1,~2 - 1 + 2 cdot 0,~2 - 1 + 2 cdot 1]$$$ = $$$[5, 2, 1, 3]$$$. Note that Bob can't choose position $$$i$$$ outside of the array (that is, smaller than $$$1$$$ or greater than $$$n$$$). Alice will be the happiest when the elements of the array are as big as possible. Bob claimed that the arithmetic mean value of the elements will work fine as a metric. What is the maximum arithmetic mean value Bob can achieve? Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 10^5$$$) β€” the number of elements of the array and the number of changes. Each of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$d_i$$$ ($$$-10^3 le x_i, d_i le 10^3$$$) β€” the parameters for the $$$i$$$-th change.
1,700
true
true
false
false
false
false
false
false
false
false
5,640
98B
This is the modification of the problem used during the official round. Unfortunately, author's solution of the original problem appeared wrong, so the problem was changed specially for the archive. Once upon a time in a far away kingdom lived the King. The King had a beautiful daughter, Victoria. They lived happily, but not happily ever after: one day a vicious dragon attacked the kingdom and stole Victoria. The King was full of grief, yet he gathered his noble knights and promised half of his kingdom and Victoria's hand in marriage to the one who will save the girl from the infernal beast. Having travelled for some time, the knights found the dragon's lair and all of them rushed there to save Victoria. Each knight spat on the dragon once and, as the dragon had quite a fragile and frail heart, his heart broke and poor beast died. As for the noble knights, they got Victoria right to the King and started brawling as each one wanted the girl's hand in marriage. The problem was that all the noble knights were equally noble and equally handsome, and Victoria didn't want to marry any of them anyway. Then the King (and he was a very wise man and didn't want to hurt anybody's feelings) decided to find out who will get his daughter randomly, i.e. tossing a coin. However, there turned out to be _n_ noble knights and the coin only has two sides. The good thing is that when a coin is tossed, the coin falls on each side with equal probability. The King got interested how to pick one noble knight using this coin so that all knights had equal probability of being chosen (the probability in that case should always be equal to 1u2009/u2009_n_). First the King wants to know the expected number of times he will need to toss a coin to determine the winner. Besides, while tossing the coin, the King should follow the optimal tossing strategy (i.e. the strategy that minimizes the expected number of tosses). Help the King in this challenging task.
2,200
false
false
true
false
false
false
false
false
false
false
9,495
232D
John Doe has a crooked fence, consisting of _n_ rectangular planks, lined up from the left to the right: the plank that goes _i_-th (1u2009≀u2009_i_u2009≀u2009_n_) (from left to right) has width 1 and height _h__i_. We will assume that the plank that goes _i_-th (1u2009≀u2009_i_u2009≀u2009_n_) (from left to right) has index _i_. A piece of the fence from _l_ to _r_ (1u2009≀u2009_l_u2009≀u2009_r_u2009≀u2009_n_) is a sequence of planks of wood with indices from _l_ to _r_ inclusive, that is, planks with indices _l_,u2009_l_u2009+u20091,u2009...,u2009_r_. The width of the piece of the fence from _l_ to _r_ is value _r_u2009-u2009_l_u2009+u20091. Two pieces of the fence from _l_1 to _r_1 and from _l_2 to _r_2 are called matching, if the following conditions hold: the pieces do not intersect, that is, there isn't a single plank, such that it occurs in both pieces of the fence; the pieces are of the same width; for all _i_ (0u2009≀u2009_i_u2009≀u2009_r_1u2009-u2009_l_1) the following condition holds: _h__l_1u2009+u2009_i_u2009+u2009_h__l_2u2009+u2009_i_u2009=u2009_h__l_1u2009+u2009_h__l_2. John chose a few pieces of the fence and now wants to know how many distinct matching pieces are for each of them. Two pieces of the fence are distinct if there is a plank, which belongs to one of them and does not belong to the other one. Input The first line contains integer _n_ (1u2009≀u2009_n_u2009≀u2009105) β€” the number of wood planks in the fence. The second line contains _n_ space-separated integers _h_1,u2009_h_2,u2009...,u2009_h__n_ (1u2009≀u2009_h__i_u2009≀u2009109) β€” the heights of fence planks. The third line contains integer _q_ (1u2009≀u2009_q_u2009≀u2009105) β€” the number of queries. Next _q_ lines contain two space-separated integers _l__i_ and _r__i_ (1u2009≀u2009_l__i_u2009≀u2009_r__i_u2009≀u2009_n_) β€” the boundaries of the _i_-th piece of the fence. Output For each query on a single line print a single integer β€” the number of pieces of the fence that match the given one. Print the answers to the queries in the order, in which the queries are given in the input. Examples Input 10 1 2 2 1 100 99 99 100 100 100 6 1 4 1 2 3 4 1 5 9 10 10 10
2,900
false
false
false
false
true
false
false
true
false
false
8,908
379B
The New Year is coming! That's why many people today are busy preparing New Year presents. Vasily the Programmer is no exception. Vasily knows that the best present is (no, it's not a contest) money. He's put _n_ empty wallets from left to right in a row and decided how much money to put in what wallet. Vasily decided to put _a__i_ coins to the _i_-th wallet from the left. Vasily is a very busy man, so the money are sorted into the bags by his robot. Initially, the robot stands by the leftmost wallet in the row. The robot can follow instructions of three types: go to the wallet that is to the left of the current one (if such wallet exists), go to the wallet that is to the right of the current one (if such wallet exists), put a coin to the current wallet. Due to some technical malfunctions the robot cannot follow two "put a coin" instructions in a row. Vasily doesn't want to wait for long, so he wants to write a program for the robot that contains at most 106 operations (not necessarily minimum in length) the robot can use to put coins into the wallets. Help him. Input The first line contains integer _n_ (2u2009≀u2009_n_u2009≀u2009300) β€” the number of wallets. The next line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≀u2009_a__i_u2009≀u2009300). It is guaranteed that at least one _a__i_ is positive. Output Print the sequence that consists of _k_ (1u2009≀u2009_k_u2009≀u2009106) characters, each of them equals: "L", "R" or "P". Each character of the sequence is an instruction to the robot. Character "L" orders to move to the left, character "R" orders to move to the right, character "P" orders the robot to put a coin in the wallet. The robot is not allowed to go beyond the wallet line. In other words, you cannot give instructions "L" if the robot is at wallet 1, or "R" at wallet _n_. As a result of the performed operations, the _i_-th wallet from the left must contain exactly _a__i_ coins. If there are multiple answers, you can print any of them.
1,200
false
false
true
false
false
true
false
false
false
false
8,316
962G
Petya has a polygon consisting of $$$n$$$ vertices. All sides of the Petya's polygon are parallel to the coordinate axes, and each two adjacent sides of the Petya's polygon are perpendicular. It is guaranteed that the polygon is simple, that is, it doesn't have self-intersections and self-touches. All internal area of the polygon (borders are not included) was painted in black color by Petya. Also, Petya has a rectangular window, defined by its coordinates, through which he looks at the polygon. A rectangular window can not be moved. The sides of the rectangular window are parallel to the coordinate axes. Blue color represents the border of a polygon, red color is the Petya's window. The answer in this case is 2. Determine the number of black connected areas of Petya's polygon, which can be seen through the rectangular window. Input The first line contain four integers $$$x_1, y_1, x_2, y_2$$$ ($$$x_1 < x_2$$$, $$$y_2 < y_1$$$) β€” the coordinates of top-left and bottom-right corners of the rectangular window. The second line contains a single integer $$$n$$$ ($$$4 le n le 15,000$$$) β€” the number of vertices in Petya's polygon. Each of the following $$$n$$$ lines contains two integers β€” the coordinates of vertices of the Petya's polygon in counterclockwise order. Guaranteed, that the given polygon satisfies the conditions described in the statement. All coordinates of the rectangular window and all coordinates of the vertices of the polygon are non-negative and do not exceed $$$15,000$$$. Output Print the number of black connected areas of Petya's polygon, which can be seen through the rectangular window. Example Input 5 7 16 3 16 0 0 18 0 18 6 16 6 16 1 10 1 10 4 7 4 7 2 2 2 2 6 12 6 12 12 10 12 10 8 0 8 Note The example corresponds to the picture above.
2,800
false
false
false
false
true
false
false
false
false
false
5,850
1485E
You are given $$$n - 1$$$ integers $$$a_2, dots, a_n$$$ and a tree with $$$n$$$ vertices rooted at vertex $$$1$$$. The leaves are all at the same distance $$$d$$$ from the root. Recall that a tree is a connected undirected graph without cycles. The distance between two vertices is the number of edges on the simple path between them. All non-root vertices with degree $$$1$$$ are leaves. If vertices $$$s$$$ and $$$f$$$ are connected by an edge and the distance of $$$f$$$ from the root is greater than the distance of $$$s$$$ from the root, then $$$f$$$ is called a child of $$$s$$$. Initially, there are a red coin and a blue coin on the vertex $$$1$$$. Let $$$r$$$ be the vertex where the red coin is and let $$$b$$$ be the vertex where the blue coin is. You should make $$$d$$$ moves. A move consists of three steps: Move the red coin to any child of $$$r$$$. Move the blue coin to any vertex $$$b'$$$ such that $$$dist(1, b') = dist(1, b) + 1$$$. Here $$$dist(x, y)$$$ indicates the length of the simple path between $$$x$$$ and $$$y$$$. Note that $$$b$$$ and $$$b'$$$ are not necessarily connected by an edge. You can optionally swap the two coins (or skip this step). Note that $$$r$$$ and $$$b$$$ can be equal at any time, and there is no number written on the root. After each move, you gain $$$a_r - a_b$$$ points. What's the maximum number of points you can gain after $$$d$$$ moves? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) β€” the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 leq n leq 2 cdot 10^5$$$) β€” the number of vertices in the tree. The second line of each test case contains $$$n-1$$$ integers $$$v_2, v_3, dots, v_n$$$ ($$$1 leq v_i leq n$$$, $$$v_i eq i$$$) xa0β€” the $$$i$$$-th of them indicates that there is an edge between vertices $$$i$$$ and $$$v_i$$$. It is guaranteed, that these edges form a tree. The third line of each test case contains $$$n-1$$$ integers $$$a_2, dots, a_n$$$ ($$$1 leq a_i leq 10^9$$$) β€” the numbers written on the vertices. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print a single integer: the maximum number of points you can gain after $$$d$$$ moves. Example Input 4 14 1 1 1 2 3 4 4 5 5 6 7 8 8 2 3 7 7 6 9 5 9 7 3 6 6 5 6 1 2 2 3 4 32 78 69 5 41 15 1 15 1 10 4 9 11 2 4 1 8 6 10 11 62 13 12 43 39 65 42 86 25 38 19 19 43 62 15 11 2 7 6 9 8 10 1 1 1 5 3 15 2 50 19 30 35 9 45 13 24 8 44 16 26 10 40 Note In the first test case, an optimal solution is to: move $$$1$$$: $$$r = 4$$$, $$$b = 2$$$; no swap; move $$$2$$$: $$$r = 7$$$, $$$b = 6$$$; swap (after it $$$r = 6$$$, $$$b = 7$$$); move $$$3$$$: $$$r = 11$$$, $$$b = 9$$$; no swap. The total number of points is $$$7 - 2 + 6 - 9 + 3 - 9 = 14$$$. In the second test case, an optimal solution is to: move $$$1$$$: $$$r = 2$$$, $$$b = 2$$$; no swap; move $$$2$$$: $$$r = 3$$$, $$$b = 4$$$; no swap; move $$$3$$$: $$$r = 5$$$, $$$b = 6$$$; no swap. The total number of points is $$$32 - 32 + 78 - 69 + 5 - 41 = 45$$$.
2,500
false
true
false
true
false
false
false
false
false
false
3,255
1355E
Problem - 1355E - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags binary search greedy math sortings ternary search *2100 No tag edit access β†’ Contest materials ") Editorial") xa0β€” the number of pillars and the costs of operations. The second line contains $$$N$$$ integers $$$h_{i}$$$ ($$$0 le h_{i} le 10^{9}$$$)xa0β€” initial heights of pillars. Output Print one integerxa0β€” the minimal cost of restoration. Examples Input 3 1 100 100 1 3 8 Output 12 Input 3 100 1 100 1 3 8 Output 9 Input 3 100 100 1 1 3 8 Output 4 Input 5 1 2 4 5 5 3 6 5 Output 4 Input 5 1 2 2 5 5 3 6 5 Output 3
2,100
true
true
false
false
false
false
false
true
true
false
3,927
331A1
β€” Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me? β€” Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night? At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming walk. He needed to cut down several trees. Let's consider the woodland belt as a sequence of trees. Each tree _i_ is described by the esthetic appeal _a__i_ β€” some trees are very esthetically pleasing, others are 'so-so', and some trees are positively ugly! The Smart Beaver calculated that he needed the following effects to win the Beaverette's heart: The first objective is to please the Beaverette: the sum of esthetic appeal of the remaining trees must be maximum possible; the second objective is to surprise the Beaverette: the esthetic appeal of the first and the last trees in the resulting belt must be the same; and of course, the walk should be successful: there must be at least two trees in the woodland belt left. Now help the Smart Beaver! Which trees does he need to cut down to win the Beaverette's heart? Input The first line contains a single integer _n_ β€” the initial number of trees in the woodland belt, 2u2009≀u2009_n_. The second line contains space-separated integers _a__i_ β€” the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value. to get 30 points, you need to solve the problem with constraints: _n_u2009≀u2009100 (subproblem A1); to get 100 points, you need to solve the problem with constraints: _n_u2009≀u20093Β·105 (subproblems A1+A2). Output In the first line print two integers β€” the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees _k_. In the next line print _k_ integers β€” the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to _n_ from left to right. If there are multiple solutions, print any of them. It is guaranteed that at least two trees have equal esthetic appeal.
1,400
false
false
true
false
false
false
true
false
false
false
8,522