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
818E
Vova again tries to play some computer card game. The rules of deck creation in this game are simple. Vova is given an existing deck of _n_ cards and a magic number _k_. The order of the cards in the deck is fixed. Each card has a number written on it; number _a__i_ is written on the _i_-th card in the deck. After receiving the deck and the magic number, Vova removes _x_ (possibly _x_u2009=u20090) cards from the top of the deck, _y_ (possibly _y_u2009=u20090) cards from the bottom of the deck, and the rest of the deck is his new deck (Vova has to leave at least one card in the deck after removing cards). So Vova's new deck actually contains cards _x_u2009+u20091, _x_u2009+u20092, ... _n_u2009-u2009_y_u2009-u20091, _n_u2009-u2009_y_ from the original deck. Vova's new deck is considered valid iff the product of all numbers written on the cards in his new deck is divisible by _k_. So Vova received a deck (possibly not a valid one) and a number _k_, and now he wonders, how many ways are there to choose _x_ and _y_ so the deck he will get after removing _x_ cards from the top and _y_ cards from the bottom is valid? Input The first line contains two integers _n_ and _k_ (1u2009≀u2009_n_u2009≀u2009100u2009000, 1u2009≀u2009_k_u2009≀u2009109). The second line contains _n_ integers _a_1, _a_2, ..., _a__n_ (1u2009≀u2009_a__i_u2009≀u2009109) β€” the numbers written on the cards. Output Print the number of ways to choose _x_ and _y_ so the resulting deck is valid. Note In the first example the possible values of _x_ and _y_ are: 1. _x_u2009=u20090,u2009_y_u2009=u20090; 2. _x_u2009=u20091,u2009_y_u2009=u20090; 3. _x_u2009=u20092,u2009_y_u2009=u20090; 4. _x_u2009=u20090,u2009_y_u2009=u20091.
1,900
false
false
false
false
true
false
false
true
false
false
6,483
1738C
Alice and Bob are playing a game on a sequence $$$a_1, a_2, dots, a_n$$$ of length $$$n$$$. They move in turns and Alice moves first. In the turn of each player, he or she should select an integer and remove it from the sequence. The game ends when there is no integer left in the sequence. Alice wins if the sum of her selected integers is even; otherwise, Bob wins. Your task is to determine who will win the game, if both players play optimally. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 100$$$) β€” the number of test cases. The following lines contain the description of each test case. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 100$$$), indicating the length of the sequence. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$-10^9 leq a_i leq 10^9$$$), indicating the elements of the sequence. Output For each test case, output "Alice" (without quotes) if Alice wins and "Bob" (without quotes) otherwise. Example Input 4 3 1 3 5 4 1 3 5 7 4 1 2 3 4 4 10 20 30 40 Output Alice Alice Bob Alice Note In the first and second test cases, Alice always selects two odd numbers, so the sum of her selected numbers is always even. Therefore, Alice always wins. In the third test case, Bob has a winning strategy that he always selects a number with the same parity as Alice selects in her last turn. Therefore, Bob always wins. In the fourth test case, Alice always selects two even numbers, so the sum of her selected numbers is always even. Therefore, Alice always wins.
1,500
true
true
false
true
false
false
false
false
false
false
1,859
1958I
You are given two rooted trees, consisting of $$$n$$$ vertices each. The vertices in the trees are numbered from $$$1$$$ to $$$n$$$, and the root is the vertex $$$1$$$. You can perform the following operation: choose the tree and the vertex $$$v$$$ (except the root of the tree) in it; connect the child nodes of $$$v$$$ to the parent of $$$v$$$ and remove $$$v$$$ from the tree. Let's say that two trees are equal if both of the following conditions hold: the sets of remaining vertices in both trees are the same; for every vertex $$$v$$$ which is not deleted, its parent in the first tree is the same as its parent in the second tree. Your task is to calculate the minimum number of aforementioned operation in order to make the trees equal. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 40$$$)xa0β€” the number of vertices in the trees. The second line contains $$$n-1$$$ integers $$$a_2, a_3, dots, a_n$$$ ($$$1 le a_i le n$$$), where $$$a_i$$$ the parent of the $$$i$$$-th vertex in the first tree. Vertex $$$1$$$ is the root. The third line contains $$$n-1$$$ integers $$$b_2, b_3, dots, b_n$$$ ($$$1 le b_i le n$$$), where $$$b_i$$$ the parent of the $$$i$$$-th vertex in the second tree. Vertex $$$1$$$ is the root. Output Print a single integerxa0β€” the minimum number of aforementioned operation in order to make the trees equal. Examples Input 10 4 7 10 6 2 9 7 1 1 1 2 10 3 4 6 6 5 5
3,100
false
false
false
false
false
false
false
false
false
true
518
241A
There are _n_ cities in the country where the Old Peykan lives. These cities are located on a straight line, we'll denote them from left to right as _c_1,u2009_c_2,u2009...,u2009_c__n_. The Old Peykan wants to travel from city _c_1 to _c__n_ using roads. There are (_n_u2009-u20091) one way roads, the _i_-th road goes from city _c__i_ to city _c__i_u2009+u20091 and is _d__i_ kilometers long. The Old Peykan travels 1 kilometer in 1 hour and consumes 1 liter of fuel during this time. Each city _c__i_ (except for the last city _c__n_) has a supply of _s__i_ liters of fuel which immediately transfers to the Old Peykan if it passes the city or stays in it. This supply refreshes instantly _k_ hours after it transfers. The Old Peykan can stay in a city for a while and fill its fuel tank many times. Initially (at time zero) the Old Peykan is at city _c_1 and _s_1 liters of fuel is transferred to it's empty tank from _c_1's supply. The Old Peykan's fuel tank capacity is unlimited. Old Peykan can not continue its travel if its tank is emptied strictly between two cities. Find the minimum time the Old Peykan needs to reach city _c__n_. Input The first line of the input contains two space-separated integers _m_ and _k_ (1u2009≀u2009_m_,u2009_k_u2009≀u20091000). The value _m_ specifies the number of roads between cities which is equal to _n_u2009-u20091. The next line contains _m_ space-separated integers _d_1,u2009_d_2,u2009...,u2009_d__m_ (1u2009≀u2009_d__i_u2009≀u20091000) and the following line contains _m_ space-separated integers _s_1,u2009_s_2,u2009...,u2009_s__m_ (1u2009≀u2009_s__i_u2009≀u20091000). Output In the only line of the output print a single integer β€” the minimum time required for The Old Peykan to reach city _c__n_ from city _c_1. Note In the second sample above, the Old Peykan stays in _c_1 for 3 hours.
1,300
false
true
false
false
false
false
false
false
false
false
8,875
2039H1
This is the easy version of the problem. The only difference is the maximum number of operations you can perform. You can only make hacks if both versions are solved. You are given an array $$$a$$$ of size $$$n$$$. A cool swap walk is the following process: In an $$$n imes n$$$ grid, we note the cells in row $$$i$$$ and column $$$j$$$ as $$$(i, j)$$$. You need to walk from $$$(1,1)$$$ to $$$(n,n)$$$, taking only steps to the right or down. Formally, if you are in $$$(x,y)$$$ currently, you can step to either $$$(x+1,y)$$$ or $$$(x,y+1)$$$, but you can not step beyond the boundaries of the grid. When you step in $$$(i,j)$$$, you must swap $$$a_i$$$ and $$$a_j$$$ when $$$i eq j$$$. You can perform at most $$$2n+4$$$ cool swap walks. Sort the array $$$a_1, a_2, ldots, a_n$$$ in non-decreasing order. We can show that it's always possible to do so. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$2 leq n leq 500$$$)xa0β€” the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,ldots ,a_n$$$ ($$$1 le a_i le n$$$)xa0β€” the elements of the array. It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$2.5 cdot 10^5$$$. Output For each test case, your output should consist of several lines: The first line contains an integer $$$k$$$ ($$$0 leq k leq 2n+4$$$), representing the number of cool swap walks you perform. Each of the next $$$k$$$ lines contains a string $$$s$$$ of length $$$2n-2$$$ consisting only of R and D, representing the path (letters are case sensitive). For all $$$1 le i le 2n-2$$$, if $$$s_i=$$$ R, you walk right in the $$$i$$$-th step, otherwise you walk down in the $$$i$$$-th step. Example Output 0 2 RRDD DRDR 3 RRDRDD DRDDRR DDRRRD Note In the first test case, the array $$$a$$$ is already non-decreasing, so you don't need to perform any walk. In the second test case, $$$a=[2,1,3]$$$ initially. In the first walk: In the $$$1$$$-st step, you step right to $$$(1,2)$$$. Then, $$$a=[1,2,3]$$$. Note that although the array $$$a$$$ is already non-decreasing, you can not stop until you reach $$$(n,n)$$$. In the $$$2$$$-nd step, you step right to $$$(1,3)$$$. Then, $$$a=[3,2,1]$$$. In the $$$3$$$-rd step, you step down to $$$(2,3)$$$. Then, $$$a=[3,1,2]$$$. In the $$$4$$$-th step, you step down to $$$(3,3)$$$. Then, $$$a=[3,1,2]$$$. In the second walk: In the $$$1$$$-st step, you step down to $$$(2,1)$$$. Then, $$$a=[1,3,2]$$$. In the $$$2$$$-nd step, you step right to $$$(2,2)$$$. Then, $$$a=[1,3,2]$$$. In the $$$3$$$-rd step, you step down to $$$(3,2)$$$. Then, $$$a=[1,2,3]$$$. In the $$$4$$$-th step, you step down to $$$(3,3)$$$. Then, $$$a=[1,2,3]$$$. After the two cool swap walks above, we get $$$a=[1,2,3]$$$, which is non-decreasing.
3,500
false
false
true
false
false
true
false
false
true
false
15
1895D
Problem - 1895D - 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 constructive algorithms data structures math string suffix structures trees *1900 No tag edit access β†’ Contest materials . Input The first line contains one integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$). The second line contains $$$n-1$$$ integers $$$a_1, a_2, dots, a_{n-1}$$$ ($$$0 le a_i le 2n$$$). Additional constraint on the input: it's always possible to construct at least one valid array $$$b$$$ from the given sequence $$$a$$$. Output Print $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$. If there are multiple such arrays, you may print any of them. Examples Input 4 2 1 2 Output 0 2 3 1 Input 6 1 6 1 4 1 Output 2 3 5 4 0 1
1,900
true
false
false
false
true
true
false
false
false
false
941
1420E
A lighthouse keeper Peter commands an army of $$$n$$$ battle lemmings. He ordered his army to stand in a line and numbered the lemmings from $$$1$$$ to $$$n$$$ from left to right. Some of the lemmings hold shields. Each lemming cannot hold more than one shield. The more protected Peter's army is, the better. To calculate the protection of the army, he finds the number of protected pairs of lemmings, that is such pairs that both lemmings in the pair don't hold a shield, but there is a lemming with a shield between them. Now it's time to prepare for defence and increase the protection of the army. To do this, Peter can give orders. He chooses a lemming with a shield and gives him one of the two orders: give the shield to the left neighbor if it exists and doesn't have a shield; give the shield to the right neighbor if it exists and doesn't have a shield. In one second Peter can give exactly one order. It's not clear how much time Peter has before the defence. So he decided to determine the maximal value of army protection for each $$$k$$$ from $$$0$$$ to $$$frac{n(n-1)}2$$$, if he gives no more that $$$k$$$ orders. Help Peter to calculate it! Input First line contains a single integer $$$n$$$ ($$$1 le n le 80$$$), the number of lemmings in Peter's army. Second line contains $$$n$$$ integers $$$a_i$$$ ($$$0 le a_i le 1$$$). If $$$a_i = 1$$$, then the $$$i$$$-th lemming has a shield, otherwise $$$a_i = 0$$$. Output Print $$$frac{n(n-1)}2 + 1$$$ numbers, the greatest possible protection after no more than $$$0, 1, dots, frac{n(n-1)}2$$$ orders. Examples Output 0 2 3 3 3 3 3 3 3 3 3 Input 12 0 0 0 0 1 1 1 1 0 1 1 0 Output 9 12 13 14 14 14 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 Note Consider the first example. The protection is initially equal to zero, because for each pair of lemmings without shields there is no lemmings with shield. In one second Peter can order the first lemming give his shield to the right neighbor. In this case, the protection is two, as there are two protected pairs of lemmings, $$$(1, 3)$$$ and $$$(1, 4)$$$. In two seconds Peter can act in the following way. First, he orders the fifth lemming to give a shield to the left neighbor. Then, he orders the first lemming to give a shield to the right neighbor. In this case Peter has three protected pairs of lemmingsxa0β€” $$$(1, 3)$$$, $$$(1, 5)$$$ and $$$(3, 5)$$$. You can make sure that it's impossible to give orders in such a way that the protection becomes greater than three.
2,500
false
true
false
true
false
false
false
false
false
false
3,575
1883G2
This is the hard version of the problem. The only difference is that in this version $$$m leq 10^9$$$. You are given two arrays of integers $$$a_1, a_2, ldots, a_n$$$ and $$$b_1, b_2, ldots, b_n$$$. Before applying any operations, you can reorder the elements of each array as you wish. Then, in one operation, you will perform both of the following actions, if the arrays are not empty: Choose any element from array $$$a$$$ and remove it (all remaining elements are shifted to a new array $$$a$$$), Choose any element from array $$$b$$$ and remove it (all remaining elements are shifted to a new array $$$b$$$). Let $$$k$$$ be the final size of both arrays. You need to find the minimum number of operations required to satisfy $$$a_i < b_i$$$ for all $$$1 leq i leq k$$$. This problem was too easy, so the problem author decided to make it more challenging. You are also given a positive integer $$$m$$$. Now, you need to find the sum of answers to the problem for $$$m$$$ pairs of arrays $$$(c[i], b)$$$, where $$$1 leq i leq m$$$. Array $$$c[i]$$$ is obtained from $$$a$$$ as follows: $$$c[i]_1 = i$$$, $$$c[i]_j = a_j$$$, for $$$2 leq j leq n$$$. 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 sets of input data. This is followed by their description. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 leq n leq 10^5$$$, $$$1 leq m leq 10^9$$$) - the size of arrays $$$a$$$ and $$$b$$$ and the constraints on the value of element $$$a_1$$$. The second line of each test case contains $$$n - 1$$$ integers $$$a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$). The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 leq b_i leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output the total number of minimum operations for all pairs of arrays $$$(c_i, b)$$$. Example Input 4 2 4 1 3 2 4 7 5 1 5 3 8 3 3 8 4 4 3 3 2 2 1 1 1 1 1 1 3 3 3 3 9 1 9 2 8 3 7 4 6 5 1 2 3 2 1 4 5 6 5 Note In the first test case: For the pair of arrays $$$([1, 1], [3, 2])$$$, the answer is $$$0$$$. No operations or reordering of elements are needed. For the pair of arrays $$$([2, 1], [3, 2])$$$, the answer is $$$0$$$. The elements of the first array can be rearranged to obtain $$$[1, 2)$$$. No operations are needed. For the pair of arrays $$$([3, 1], [3, 2])$$$, the answer is $$$1$$$. The element $$$3$$$ can be removed from the first array and the element $$$2$$$ can be removed from the second array. For the pair of arrays $$$([4, 1], [3, 2])$$$, the answer is $$$1$$$. The element $$$4$$$ can be removed from the first array and the element $$$3$$$ can be removed from the second array.
1,900
false
true
false
false
false
false
false
true
true
false
984
1372D
# Omkar and Circle Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 256 megabytes Danny, the local Math Maniac, is fascinated by circles, Omkar’s most recent creation. Help him solve this circle problem! You are given n nonnegative integers a1, a 2, . . . , a n arranged in a circle, where n must be odd (ie. n βˆ’ 1 is divisible by 2). Formally, for all i such that 2 ≀ i ≀ n, the elements aiβˆ’1 and ai are considered to be adjacent, and an and a1 are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value. Help Danny find the maximum possible circular value after some sequences of operations. # Input The first line contains one odd integer n (1 ≀ n < 2 Β· 10 5, n is odd) x16 the initial size of the circle. The second line contains n integers a1, a 2, . . . , a n (0 ≀ ai ≀ 10 9) x16 the initial numbers in the circle. # Output Output the maximum possible circular value after applying some sequence of operations to the given circle. # Examples standard input standard output 37 10 2 17 144 # Note For the first test case, here’s how a circular value of 17 is obtained: Pick the number at index 3. The sum of adjacent elements equals 17 . Delete 7 and 10 from the circle and replace 2 with 17 .Note that the answer may not fit in a 32 -bit integer. Page 1 of 1
2,100
false
true
false
true
false
false
true
false
false
false
3,804
1918F
The caterpillar decided to visit every node of the tree. Initially, it is sitting at the root. The tree is represented as a rooted tree with the root at the node $$$1$$$. Each crawl to a neighboring node takes $$$1$$$ minute for the caterpillar. And there is a trampoline under the tree. If the caterpillar detaches from the tree and falls onto the trampoline, it will end up at the root of the tree in $$$0$$$ seconds. But the trampoline is old and can withstand no more than $$$k$$$ caterpillar's falls. What is the minimum time the caterpillar can take to visit all the nodes of the tree? More formally, we need to find the minimum time required to visit all the nodes of the tree, if the caterpillar starts at the root (node $$$1$$$) and moves using two methods. 1. Crawl along an edge to one of the neighboring nodes: takes $$$1$$$ minute. 2. Teleport to the root: takes no time, no new nodes become visited. The second method (teleportation) can be used at most $$$k$$$ times. The caterpillar can finish the journey at any node. Input The first line of the input contains two integers: $$$n$$$ ($$$2 le n le 2cdot 10^5$$$)xa0β€” the number of nodes in the tree, and $$$k$$$ ($$$0 le k le 10^9$$$)xa0β€” the maximum number of teleports to the root. The second line contains $$$n-1$$$ integers $$$p_2$$$, $$$p_3$$$, ..., $$$p_n$$$ ($$$1 le p_i le n$$$)xa0β€” the ancestors in the tree for nodes $$$2, 3, ldots, n$$$; node $$$1$$$ is the root. Output Print a single number in a single linexa0β€” the minimum number of minutes required to visit all the nodes of the tree. Note The figure shows one of the ways to traverse the tree from the first test in 9 minutes.
2,500
false
true
true
false
false
false
false
false
true
true
783
1535C
Problem - 1535C - 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 greedy implementation strings two pointers *1400 No tag edit access β†’ Contest materials . Let's call a string beautiful if it consists of the characters 0, 1, and ?, and you can replace the characters ? to 0 or 1 (for each character, the choice is independent), so that the string becomes unstable. For example, the strings 0??10, 0, and ??? are beautiful, and the strings 00 and ?1??1 are not. Calculate the number of beautiful contiguous substrings of the string $$$s$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” number of test cases. The first and only line of each test case contains the string $$$s$$$ ($$$1 le s le 2 cdot 10^5$$$) consisting of characters 0, 1, and ?. It is guaranteed that the sum of the string lengths over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integerxa0β€” the number of beautiful substrings of the string $$$s$$$. Example Input 3 0?10 ??? ?10??1100 Output 8 6 25
1,400
false
true
true
true
false
false
false
true
false
false
2,972
13D
Problem - 13D - Codeforces =============== xa0 β€” the number of red and blue points respectively. The following _N_ lines contain two integer numbers each β€” coordinates of red points. The following _M_ lines contain two integer numbers each β€” coordinates of blue points. All coordinates do not exceed 109 by absolute value. Output Output one integer β€” the number of distinct triangles with vertices in red points which do not contain any blue point inside. Examples Input 4 1 0 0 10 0 10 10 5 4 2 1 Output 2 Input 5 5 5 10 6 1 8 6 -6 -7 7 -1 5 -1 10 -4 -10 -8 -10 5 -2 -8 Output 7
2,600
false
false
false
true
false
false
false
false
false
false
9,927
484D
Problem - 484D - 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 dp greedy *2400 No tag edit access β†’ Contest materials . The teacher wants to divide the children into some number of groups in such way that the total sociability of the groups is maximum. Help him find this value. Input The first line contains integer _n_xa0β€” the number of children in the line (1u2009≀u2009_n_u2009≀u2009106). The second line contains _n_ integers _a__i_xa0β€” the charisma of the _i_-th child (u2009-u2009109u2009≀u2009_a__i_u2009≀u2009109). Output Print the maximum possible total sociability of all groups. Examples Input 5 1 2 3 1 2 Output 3 Input 3 3 3 3 Output 0 Note In the first test sample one of the possible variants of an division is following: the first three children form a group with sociability 2, and the two remaining children form a group with sociability 1. In the second test sample any division leads to the same result, the sociability will be equal to 0 in each group.
2,400
false
true
false
true
true
false
false
false
false
false
7,899
79B
Fox Ciel saw a large field while she was on a bus. The field was a _n_u2009Γ—u2009_m_ rectangle divided into 1u2009Γ—u20091 cells. Some cells were wasteland, and other each cell contained crop plants: either carrots or kiwis or grapes. After seeing the field carefully, Ciel found that the crop plants of each cell were planted in following procedure: Assume that the rows are numbered 1 to _n_ from top to bottom and the columns are numbered 1 to _m_ from left to right, and a cell in row _i_ and column _j_ is represented as (_i_,u2009_j_). First, each field is either cultivated or waste. Crop plants will be planted in the cultivated cells in the order of (1,u20091)u2009β†’u2009...u2009β†’u2009(1,u2009_m_)u2009β†’u2009(2,u20091)u2009β†’u2009...u2009β†’u2009(2,u2009_m_)u2009β†’u2009...u2009β†’u2009(_n_,u20091)u2009β†’u2009...u2009β†’u2009(_n_,u2009_m_). Waste cells will be ignored. Crop plants (either carrots or kiwis or grapes) will be planted in each cell one after another cyclically. Carrots will be planted in the first cell, then kiwis in the second one, grapes in the third one, carrots in the forth one, kiwis in the fifth one, and so on. The following figure will show you the example of this procedure. Here, a white square represents a cultivated cell, and a black square represents a waste cell. Now she is wondering how to determine the crop plants in some certain cells. Input In the first line there are four positive integers _n_,u2009_m_,u2009_k_,u2009_t_ (1u2009≀u2009_n_u2009≀u20094Β·104,u20091u2009≀u2009_m_u2009≀u20094Β·104,u20091u2009≀u2009_k_u2009≀u2009103,u20091u2009≀u2009_t_u2009≀u2009103), each of which represents the height of the field, the width of the field, the number of waste cells and the number of queries that ask the kind of crop plants in a certain cell. Following each _k_ lines contains two integers _a_,u2009_b_ (1u2009≀u2009_a_u2009≀u2009_n_,u20091u2009≀u2009_b_u2009≀u2009_m_), which denotes a cell (_a_,u2009_b_) is waste. It is guaranteed that the same cell will not appear twice in this section. Following each _t_ lines contains two integers _i_,u2009_j_ (1u2009≀u2009_i_u2009≀u2009_n_,u20091u2009≀u2009_j_u2009≀u2009_m_), which is a query that asks you the kind of crop plants of a cell (_i_,u2009_j_). Output For each query, if the cell is waste, print Waste. Otherwise, print the name of crop plants in the cell: either Carrots or Kiwis or Grapes. Examples Input 4 5 5 6 4 3 1 3 3 3 2 5 3 2 1 3 1 4 2 3 2 4 1 1 1 1 Output Waste Grapes Carrots Kiwis Carrots Carrots Note The sample corresponds to the figure in the statement.
1,400
false
false
true
false
false
false
false
false
true
false
9,569
489A
In this problem your goal is to sort an array consisting of _n_ integers in at most _n_ swaps. For the given array find the sequence of swaps that makes the array sorted in the non-descending order. Swaps are performed consecutively, one after another. Note that in this problem you do not have to minimize the number of swaps β€” your task is to find any sequence that is no longer than _n_. Input The first line of the input contains integer _n_ (1u2009≀u2009_n_u2009≀u20093000) β€” the number of array elements. The second line contains elements of array: _a_0,u2009_a_1,u2009...,u2009_a__n_u2009-u20091 (u2009-u2009109u2009≀u2009_a__i_u2009≀u2009109), where _a__i_ is the _i_-th element of the array. The elements are numerated from 0 to _n_u2009-u20091 from left to right. Some integers may appear in the array more than once. Output In the first line print _k_ (0u2009≀u2009_k_u2009≀u2009_n_) β€” the number of swaps. Next _k_ lines must contain the descriptions of the _k_ swaps, one per line. Each swap should be printed as a pair of integers _i_, _j_ (0u2009≀u2009_i_,u2009_j_u2009≀u2009_n_u2009-u20091), representing the swap of elements _a__i_ and _a__j_. You can print indices in the pairs in any order. The swaps are performed in the order they appear in the output, from the first to the last. It is allowed to print _i_u2009=u2009_j_ and swap the same pair of elements multiple times. If there are multiple answers, print any of them. It is guaranteed that at least one answer exists.
1,200
false
true
true
false
false
false
false
false
true
false
7,883
1780E
Josuke received a huge undirected weighted complete$$$^dagger$$$ graph $$$G$$$ as a gift from his grandfather. The graph contains $$$10^{18}$$$ vertices. The peculiarity of the gift is that the weight of the edge between the different vertices $$$u$$$ and $$$v$$$ is equal to $$$gcd(u, v)^ddagger$$$. Josuke decided to experiment and make a new graph $$$G'$$$. To do this, he chooses two integers $$$l le r$$$ and deletes all vertices except such vertices $$$v$$$ that $$$l le v le r$$$, and also deletes all the edges except between the remaining vertices. Now Josuke is wondering how many different weights are there in $$$G'$$$. Since their count turned out to be huge, he asks for your help. $$$^dagger$$$ A complete graph is a simple undirected graph in which every pair of distinct vertices is adjacent. $$$^ddagger$$$ $$$gcd(x, y)$$$ denotes the β€” the number of test cases. The first line of each test case contains two numbers $$$l$$$, $$$r$$$ ($$$1 le l le r le 10^{18}$$$, $$$l le 10^9$$$). Output For each test case print a single number β€” the number of different weights among the remaining edges. Example Input 7 2 4 16 24 2 6 1 10 3 3 2562 2568 125 100090 Note The graph $$$G'$$$ for the first test case. The picture above shows that the graph has $$$2$$$ different weights. In the fifth test case, there is only one vertex from which no edges originate, so the answer is $$$0$$$.
2,400
true
false
false
false
true
false
true
true
false
false
1,591
626A
Problem - 626A - Codeforces =============== xa0 ]( "23515") xa0β€” the number of commands. The next line contains _n_ characters, each either 'U', 'R', 'D', or 'L'xa0β€” Calvin's source code. Output Print a single integerxa0β€” the number of contiguous substrings that Calvin can execute and return to his starting square. Examples Input 6 URLLDR Output 2 Input 4 DLUU Output 0 Input 7 RLRLRLR Output 12 Note In the first case, the entire source code works, as well as the "RL" substring in the second and third characters. Note that, in the third case, the substring "LR" appears three times, and is therefore counted three times to the total result.
1,000
false
false
true
false
false
false
true
false
false
false
7,328
1776N
You are given a string $$$s$$$ with length $$$n-1$$$ whose characters are either $$$ exttt{<}$$$ or $$$ exttt{>}$$$. Count the permutations $$$p_1, , p_2, , dots, , p_n$$$ of $$$1, , 2, , dots, , n$$$ such that, for all $$$i = 1, , 2, , dots, , n - 1$$$, if $$$s_i$$$ is $$$ exttt{<}$$$ then $$$p_i < p_{i+1}$$$ and if $$$s_i$$$ is $$$ exttt{>}$$$ then $$$p_i > p_{i+1}$$$. Since this number can be very large, compute its logarithm in base $$$2$$$. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 100,000$$$). The second line contains a string $$$s$$$ of length $$$n-1$$$; each character of $$$s$$$ is either $$$ exttt{<}$$$ or $$$ exttt{>}$$$. Output Print the logarithm in base $$$2$$$ of the number of permutations satisfying the constraints described in the statement. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$x$$$ and let the correct answer be $$$y$$$. Your answer is accepted if and only if $$$frac{x - y}{max{(1, y)}} le 10^{-6}$$$. Note In the first sample, there is only one valid permutation, that is $$$[2, 1]$$$. Since $$$log_2(1)=0$$$, the correct output is $$$0$$$. In the second sample, there are $$$2$$$ valid permutations, that are $$$[3, 1, 2]$$$ and $$$[2, 1, 3]$$$. Since $$$log_2(2)=1$$$, the correct output is $$$1$$$. In the third sample, there are $$$4$$$ valid permutations, that are $$$[1, 5, 4, 3, 2]$$$, $$$[2, 5, 4, 3, 1]$$$, $$$[3, 5, 4, 2, 1]$$$, $$$[4, 5, 3, 2, 1]$$$. Since $$$log_2(4)=2$$$, the correct output is $$$2$$$. In the fourth sample, there are $$$909$$$ valid permutations. Notice that $$$log_2(909)=9.828136484194ldots$$$
3,500
true
false
false
false
false
false
false
false
false
false
1,615
746D
Problem - 746D - 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 constructive algorithms greedy math *1500 No tag edit access β†’ Contest materials ") Editorial") more than _k_ times in a row. Your task is to determine the order of brewing tea bags so that Innokentiy will be able to drink _n_ cups of tea, without drinking the same tea more than _k_ times in a row, or to inform that it is impossible. Each tea bag has to be used exactly once. Input The first line contains four integers _n_, _k_, _a_ and _b_ (1u2009≀u2009_k_u2009≀u2009_n_u2009≀u2009105, 0u2009≀u2009_a_,u2009_b_u2009≀u2009_n_)xa0β€” the number of cups of tea Innokentiy wants to drink, the maximum number of cups of same tea he can drink in a row, the number of tea bags of green and black tea. It is guaranteed that _a_u2009+u2009_b_u2009=u2009_n_. Output If it is impossible to drink _n_ cups of tea, print "NO" (without quotes). Otherwise, print the string of the length _n_, which consists of characters 'G' and 'B'. If some character equals 'G', then the corresponding cup of tea should be green. If some character equals 'B', then the corresponding cup of tea should be black. If there are multiple answers, print any of them. Examples Input 5 1 3 2 Output GBGBG Input 7 2 2 5 Output BBGBGBB Input 4 3 4 0 Output NO
1,500
true
true
false
false
false
true
false
false
false
false
6,814
908B
Bob programmed a robot to navigate through a 2d maze. The maze has some obstacles. Empty cells are denoted by the character '.', where obstacles are denoted by '#'. There is a single robot in the maze. Its start position is denoted with the character 'S'. This position has no obstacle in it. There is also a single exit in the maze. Its position is denoted with the character 'E'. This position has no obstacle in it. The robot can only move up, left, right, or down. When Bob programmed the robot, he wrote down a string of digits consisting of the digits 0 to 3, inclusive. He intended for each digit to correspond to a distinct direction, and the robot would follow the directions in order to reach the exit. Unfortunately, he forgot to actually assign the directions to digits. The robot will choose some random mapping of digits to distinct directions. The robot will map distinct digits to distinct directions. The robot will then follow the instructions according to the given string in order and chosen mapping. If an instruction would lead the robot to go off the edge of the maze or hit an obstacle, the robot will crash and break down. If the robot reaches the exit at any point, then the robot will stop following any further instructions. Bob is having trouble debugging his robot, so he would like to determine the number of mappings of digits to directions that would lead the robot to the exit. Input The first line of input will contain two integers _n_ and _m_ (2u2009≀u2009_n_,u2009_m_u2009≀u200950), denoting the dimensions of the maze. The next _n_ lines will contain exactly _m_ characters each, denoting the maze. Each character of the maze will be '.', '#', 'S', or 'E'. There will be exactly one 'S' and exactly one 'E' in the maze. The last line will contain a single string _s_ (1u2009≀u2009_s_u2009≀u2009100)xa0β€” the instructions given to the robot. Each character of _s_ is a digit from 0 to 3. Note For the first sample, the only valid mapping is , where _D_ is down, _L_ is left, _U_ is up, _R_ is right.
1,200
false
false
true
false
false
false
true
false
false
false
6,119
725F
Alice and Bonnie are sisters, but they don't like each other very much. So when some old family photos were found in the attic, they started to argue about who should receive which photos. In the end, they decided that they would take turns picking photos. Alice goes first. There are _n_ stacks of photos. Each stack contains exactly two photos. In each turn, a player may take only a photo from the top of one of the stacks. Each photo is described by two non-negative integers _a_ and _b_, indicating that it is worth _a_ units of happiness to Alice and _b_ units of happiness to Bonnie. Values of _a_ and _b_ might differ for different photos. It's allowed to pass instead of taking a photo. The game ends when all photos are taken or both players pass consecutively. The players don't act to maximize their own happiness. Instead, each player acts to maximize the amount by which her happiness exceeds her sister's. Assuming both players play optimal, find the difference between Alice's and Bonnie's happiness. That is, if there's a perfectly-played game such that Alice has _x_ happiness and Bonnie has _y_ happiness at the end, you should print _x_u2009-u2009_y_. Input The first line of input contains a single integer _n_ (1u2009≀u2009_n_u2009≀u2009100u2009000)xa0β€” the number of two-photo stacks. Then follow _n_ lines, each describing one of the stacks. A stack is described by four space-separated non-negative integers _a_1, _b_1, _a_2 and _b_2, each not exceeding 109. _a_1 and _b_1 describe the top photo in the stack, while _a_2 and _b_2 describe the bottom photo in the stack. Output Output a single integer: the difference between Alice's and Bonnie's happiness if both play optimally.
2,900
false
true
false
false
false
false
false
false
false
false
6,902
1547A
There are three cells on an infinite 2-dimensional grid, labeled $$$A$$$, $$$B$$$, and $$$F$$$. Find the length of the shortest path from $$$A$$$ to $$$B$$$ if: in one move you can go to any of the four adjacent cells sharing a side; visiting the cell $$$F$$$ is forbidden (it is an obstacle). Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases in the input. Then $$$t$$$ test cases follow. Before each test case, there is an empty line. Each test case contains three lines. The first one contains two integers $$$x_A, y_A$$$ ($$$1 le x_A, y_A le 1000$$$)xa0β€” coordinates of the start cell $$$A$$$. The second one contains two integers $$$x_B, y_B$$$ ($$$1 le x_B, y_B le 1000$$$)xa0β€” coordinates of the finish cell $$$B$$$. The third one contains two integers $$$x_F, y_F$$$ ($$$1 le x_F, y_F le 1000$$$)xa0β€” coordinates of the forbidden cell $$$F$$$. All cells are distinct. Coordinate $$$x$$$ corresponds to the column number and coordinate $$$y$$$ corresponds to the row number (see the pictures below). Output Output $$$t$$$ lines. The $$$i$$$-th line should contain the answer for the $$$i$$$-th test case: the length of the shortest path from the cell $$$A$$$ to the cell $$$B$$$ if the cell $$$F$$$ is not allowed to be visited. Example Input 7 1 1 3 3 2 2 2 5 2 1 2 3 1000 42 1000 1 1000 1000 1 10 3 10 2 10 3 8 7 8 3 7 2 1 4 1 1 1 1 344 1 10 1 1 Note An example of a possible shortest path for the first test case. An example of a possible shortest path for the second test case.
800
true
false
true
false
false
false
false
false
false
false
2,913
1144G
Two integer sequences existed initially, one of them was strictly increasing, and another one β€” strictly decreasing. Strictly increasing sequence is a sequence of integers $$$[x_1 < x_2 < dots < x_k]$$$. And strictly decreasing sequence is a sequence of integers $$$[y_1 > y_2 > dots > y_l]$$$. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing. Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences $$$[1, 3, 4]$$$ and $$$[10, 4, 2]$$$ can produce the following resulting sequences: $$$[10, extbf{1}, extbf{3}, 4, 2, extbf{4}]$$$, $$$[ extbf{1}, extbf{3}, extbf{4}, 10, 4, 2]$$$. The following sequence cannot be the result of these insertions: $$$[ extbf{1}, 10, extbf{4}, 4, extbf{3}, 2]$$$ because the order of elements in the increasing sequence was changed. Let the obtained sequence be $$$a$$$. This sequence $$$a$$$ is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one β€” strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing. If there is a contradiction in the input and it is impossible to split the given sequence $$$a$$$ into one increasing sequence and one decreasing sequence, print "NO". 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$$$ ($$$0 le a_i le 2 cdot 10^5$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. Output If there is a contradiction in the input and it is impossible to split the given sequence $$$a$$$ into one increasing sequence and one decreasing sequence, print "NO" in the first line. Otherwise print "YES" in the first line. In the second line, print a sequence of $$$n$$$ integers $$$res_1, res_2, dots, res_n$$$, where $$$res_i$$$ should be either $$$0$$$ or $$$1$$$ for each $$$i$$$ from $$$1$$$ to $$$n$$$. The $$$i$$$-th element of this sequence should be $$$0$$$ if the $$$i$$$-th element of $$$a$$$ belongs to the increasing sequence, and $$$1$$$ otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
2,400
false
true
false
true
false
false
false
false
false
false
4,999
1121A
Everybody knows that the $$$m$$$-coder Tournament will happen soon. $$$m$$$ schools participate in the tournament, and only one student from each school participates. There are a total of $$$n$$$ students in those schools. Before the tournament, all students put their names and the names of their schools into the Technogoblet of Fire. After that, Technogoblet selects the strongest student from each school to participate. Arkady is a hacker who wants to have $$$k$$$ Chosen Ones selected by the Technogoblet. Unfortunately, not all of them are the strongest in their schools, but Arkady can make up some new school names and replace some names from Technogoblet with those. You can't use each made-up name more than once. In that case, Technogoblet would select the strongest student in those made-up schools too. You know the power of each student and schools they study in. Calculate the minimal number of schools Arkady has to make up so that $$$k$$$ Chosen Ones would be selected by the Technogoblet. Input The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 le n le 100$$$, $$$1 le m, k le n$$$)xa0β€” the total number of students, the number of schools and the number of the Chosen Ones. The second line contains $$$n$$$ different integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le n$$$), where $$$p_i$$$ denotes the power of $$$i$$$-th student. The bigger the power, the stronger the student. The third line contains $$$n$$$ integers $$$s_1, s_2, ldots, s_n$$$ ($$$1 le s_i le m$$$), where $$$s_i$$$ denotes the school the $$$i$$$-th student goes to. At least one student studies in each of the schools. The fourth line contains $$$k$$$ different integers $$$c_1, c_2, ldots, c_k$$$ ($$$1 le c_i le n$$$) xa0β€” the id's of the Chosen Ones. Output Output a single integer xa0β€” the minimal number of schools to be made up by Arkady so that $$$k$$$ Chosen Ones would be selected by the Technogoblet. Examples Input 7 3 1 1 5 3 4 6 7 2 1 3 1 2 1 2 3 3 Input 8 4 4 1 2 3 4 5 6 7 8 4 3 2 1 4 3 2 1 3 4 5 6 Note In the first example there's just a single Chosen One with id $$$3$$$. His power is equal to $$$3$$$, but in the same school $$$1$$$, there's a student with id $$$5$$$ and power $$$6$$$, and that means inaction would not lead to the latter being chosen. If we, however, make up a new school (let its id be $$$4$$$) for the Chosen One, Technogoblet would select students with ids $$$2$$$ (strongest in $$$3$$$), $$$5$$$ (strongest in $$$1$$$), $$$6$$$ (strongest in $$$2$$$) and $$$3$$$ (strongest in $$$4$$$). In the second example, you can change the school of student $$$3$$$ to the made-up $$$5$$$ and the school of student $$$4$$$ to the made-up $$$6$$$. It will cause the Technogoblet to choose students $$$8$$$, $$$7$$$, $$$6$$$, $$$5$$$, $$$3$$$ and $$$4$$$.
1,100
false
false
true
false
false
false
false
false
true
false
5,080
1990F
You are given an array $$$a$$$ of size $$$n$$$. A segment $$$$$$ is called a polygonal segment only if the following conditions hold: $$$(r-l+1) geq 3$$$; Considering $$$a_l, a_{l+1}, ldots, a_r$$$ as side lengths, these sides can form a polygon with $$$(r-l+1)$$$ sides. Process $$$q$$$ queries of two types: "1 l r": find the length of the longest segment among all polygonal segments $$$[l_0,r_0]$$$ satisfying $$$l le l_0 le r_0 le r$$$. If there is no such polygonal segment, output $$$-1$$$ instead; "2 i x": assign $$$a_i := x$$$. Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0β€” the number of test cases. For each test case: The first line of each testcase contains two integers $$$n$$$, $$$q$$$ ($$$4 le n le 2cdot 10^5$$$, $$$1 le q le 10^5$$$); The second line of each testcase contains $$$n$$$ integers $$$a_1,a_2,ldots, a_n$$$ ($$$1 le a_i le 10^{12}$$$); The following $$$q$$$ lines contain the description of queries. Each line is of one of two types: "1 l r" ($$$1 le l < r le n$$$, $$$r-l+1ge 3$$$); "2 i x" ($$$1 le i le n$$$, $$$1 le x le 10^{12}$$$). It is guaranteed that the sum of $$$n$$$ over all test cases will not exceed $$$2 cdot 10^5$$$, and the sum of $$$q$$$ over all test cases will not exceed $$$10^5$$$. Output For each query, if there is no suitable segment, output $$$-1$$$ in a new line. Otherwise, output the length of the longest segment satisfying the condition above in a new line. Example Input 2 5 6 3 1 2 2 8 1 1 3 1 1 4 1 1 5 2 1 5 1 1 4 1 1 5 4 10 500000000000 500000000000 1000000000000 500000000000 1 1 3 1 2 4 1 1 4 2 1 499999999999 2 3 999999999999 1 1 3 1 2 4 1 1 4 2 3 1000000000000 1 1 3 Output -1 4 4 3 5 -1 -1 4 -1 3 4 -1 Note In the first query of the first test case, there is no polygonal segment under the given condition. For example, considering segment $$$[1,3]$$$, you can not form a triangle with side lengths of $$$a_1=3$$$, $$$a_2=1$$$, and $$$a_3=2$$$. In the second query of the first test case, the longest polygonal segment is $$$[1,4]$$$. You can form a quadrilateral with side lengths of $$$a_1=3$$$, $$$a_2=1$$$, $$$a_3=2$$$, and $$$a_4=2$$$. An example of a quadrilateral with side lengths of $$$3$$$, $$$1$$$, $$$2$$$, and $$$2$$$.
2,800
false
true
false
true
true
false
true
false
false
false
329
556A
Problem - 556A - 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 *900 No tag edit access β†’ Contest materials ? Help him to calculate this number. Input First line of the input contains a single integer _n_ (1u2009≀u2009_n_u2009≀u20092Β·105), the length of the string that Andreid has. The second line contains the string of length _n_ consisting only from zeros and ones. Output Output the minimum length of the string that may remain after applying the described operations several times. Examples Input 4 1100 Output 0 Input 5 01010 Output 1 Input 8 11101111 Output 6 Note In the first sample test it is possible to change the string like the following: . In the second sample test it is possible to change the string like the following: . In the third sample test it is possible to change the string like the following: .
900
false
true
false
false
false
false
false
false
false
false
7,610
261E
Problem - 261E - 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 brute force dp two pointers *2800 No tag edit access β†’ Contest materials are there, such that we can write the number _x_ to the first cell of the calculator, having performed at most _p_ moves. Input The first line contains three integers: _l_, _r_, _p_ (2u2009≀u2009_l_u2009≀u2009_r_u2009≀u2009109,u20091u2009≀u2009_p_u2009≀u2009100). The numbers in the line are separated by single spaces. Output In a single line print a single integer β€” the answer to the problem. Examples Input 2 10 3 Output 1 Input 2 111 100 Output 106 Input 2 111 11 Output 47
2,800
false
false
false
true
false
false
true
false
false
false
8,786
46F
Today you are to solve the problem even the famous Hercule Poirot can't cope with! That's why this crime has not yet been solved and this story was never included in Agatha Christie's detective story books. You are not informed on what crime was committed, when and where the corpse was found and other details. We only know that the crime was committed in a house that has _n_ rooms and _m_ doors between the pairs of rooms. The house residents are very suspicious, that's why all the doors can be locked with keys and all the keys are different. According to the provided evidence on Thursday night all the doors in the house were locked, and it is known in what rooms were the residents, and what kind of keys had any one of them. The same is known for the Friday night, when all the doors were also locked. On Friday it was raining heavily, that's why nobody left the house and nobody entered it. During the day the house residents could open and close doors to the neighboring rooms using the keys at their disposal (every door can be opened and closed from each side); move freely from a room to a room if a corresponding door is open; give keys to one another, being in one room. "Little grey matter" of Hercule Poirot are not capable of coping with such amount of information. Find out if the positions of people and keys on the Thursday night could result in the positions on Friday night, otherwise somebody among the witnesses is surely lying. Input The first line contains three preset integers _n_, _m_ ΠΈ _k_ (1u2009≀u2009_n_,u2009_m_,u2009_k_u2009≀u20091000) β€” the number of rooms, the number of doors and the number of house residents respectively. The next _m_ lines contain pairs of room numbers which join the doors. The rooms are numbered with integers from 1 to _n_. There cannot be more that one door between the pair of rooms. No door connects a room with itself. The next _k_ lines describe the residents' position on the first night. Every line contains a resident's name (a non-empty line consisting of no more than 10 Latin letters), then after a space follows the room number, then, after a space β€” the number of keys the resident has. Then follow written space-separated numbers of the doors that can be unlocked by these keys. The doors are numbered with integers from 1 to _m_ in the order in which they are described in the input data. All the residents have different names, uppercase and lowercase letters considered to be different. Every _m_ keys occurs exactly once in the description. Multiple people may be present in one room, some rooms may be empty. The next _k_ lines describe the position of the residents on the second night in the very same format. It is guaranteed that in the second night's description the residents' names remain the same and every _m_ keys occurs exactly once. Output Print "YES" (without quotes) if the second arrangement can result from the first one, otherwise, print "NO". Examples Input 2 1 2 1 2 Dmitry 1 1 1 Natalia 2 0 Natalia 1 1 1 Dmitry 2 0 Input 4 4 3 1 3 1 2 2 3 3 4 Artem 1 1 4 Dmitry 1 1 2 Edvard 4 2 1 3 Artem 2 0 Dmitry 1 0 Edvard 4 4 1 2 3 4
2,300
false
false
false
false
false
false
false
false
false
true
9,744
1422B
A matrix of size $$$n imes m$$$ is called nice, if all rows and columns of the matrix are palindromes. A sequence of integers $$$(a_1, a_2, dots , a_k)$$$ is a palindrome, if for any integer $$$i$$$ ($$$1 le i le k$$$) the equality $$$a_i = a_{k - i + 1}$$$ holds. Sasha owns a matrix $$$a$$$ of size $$$n imes m$$$. In one operation he can increase or decrease any number in the matrix by one. Sasha wants to make the matrix nice. He is interested what is the minimum number of operations he needs. Help him! Input The first line contains a single integer $$$t$$$xa0β€” the number of test cases ($$$1 le t le 10$$$). The $$$t$$$ tests follow. The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 100$$$)xa0β€” the size of the matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, j}$$$ ($$$0 le a_{i, j} le 10^9$$$)xa0β€” the elements of the matrix. Output For each test output the smallest number of operations required to make the matrix nice. Example Input 2 4 2 4 2 2 4 4 2 2 4 3 4 1 2 3 4 5 6 7 8 9 10 11 18 Note In the first test case we can, for example, obtain the following nice matrix in $$$8$$$ operations: 2 2 4 4 4 4 2 2 In the second test case we can, for example, obtain the following nice matrix in $$$42$$$ operations: 5 6 6 5 6 6 6 6 5 6 6 5
1,300
true
true
true
false
false
false
false
false
false
false
3,568
2026E
Problem - 2026E - 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 dfs and similar flows graph matchings graphs *2500 No tag edit access β†’ Contest materials , and the value of the array is $$$4-2=2$$$. Your task is to calculate the maximum possible value of some subsequence of the given array. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0β€” the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 100$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i < 2^{60}$$$). Output For each test case, print the maximum possible value of some subsequence of the given array. Example Input 4 3 0 0 0 4 1 0 1 2 1 5 8 7 1 48 14 13 8 7 6 Output 3 2 0 3
2,500
false
false
false
false
false
false
false
false
false
true
112
1738E
Given an integer sequence $$$a_1, a_2, dots, a_n$$$ of length $$$n$$$, your task is to compute the number, modulo $$$998244353$$$, of ways to partition it into several non-empty continuous subsequences such that the sums of elements in the subsequences form a balanced sequence. A sequence $$$s_1, s_2, dots, s_k$$$ of length $$$k$$$ is said to be balanced, if $$$s_{i} = s_{k-i+1}$$$ for every $$$1 leq i leq k$$$. For example, $$$[1, 2, 3, 2, 1]$$$ and $$$[1,3,3,1]$$$ are balanced, but $$$[1,5,15]$$$ is not. Formally, every partition can be described by a sequence of indexes $$$i_1, i_2, dots, i_k$$$ of length $$$k$$$ with $$$1 = i_1 < i_2 < dots < i_k leq n$$$ such that 1. $$$k$$$ is the number of non-empty continuous subsequences in the partition; 2. For every $$$1 leq j leq k$$$, the $$$j$$$-th continuous subsequence starts with $$$a_{i_j}$$$, and ends exactly before $$$a_{i_{j+1}}$$$, where $$$i_{k+1} = n + 1$$$. That is, the $$$j$$$-th subsequence is $$$a_{i_j}, a_{i_j+1}, dots, a_{i_{j+1}-1}$$$. There are $$$2^{n-1}$$$ different partitions in total. Let $$$s_1, s_2, dots, s_k$$$ denote the sums of elements in the subsequences with respect to the partition $$$i_1, i_2, dots, i_k$$$. Formally, for every $$$1 leq j leq k$$$, $$$$$$ s_j = sum_{i=i_{j}}^{i_{j+1}-1} a_i = a_{i_j} + a_{i_j+1} + dots + a_{i_{j+1}-1}. $$$$$$ For example, the partition $$$[1,,2,3,,4,5,6]$$$ of sequence $$$[1,2,3,4,5,6]$$$ is described by the sequence $$$[1,2,4]$$$ of indexes, and the sums of elements in the subsequences with respect to the partition is $$$[1,5,15]$$$. Two partitions $$$i_1, i_2, dots, i_k$$$ and $$$i'_1, i'_2, dots, i'_{k'}$$$ (described by sequences of indexes) are considered to be different, if at least one of the following holds. $$$k eq k'$$$, $$$i_j eq i'_j$$$ for some $$$1 leq j leq minleft{ k, k' ight}$$$. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^5$$$) β€” the number of test cases. The following lines contain the description of each test case. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 10^5$$$), indicating the length of the sequence $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 leq a_i leq 10^9$$$), indicating the elements of the sequence $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Note For the first test case, there is only one way to partition a sequence of length $$$1$$$, which is itself and is, of course, balanced. For the second test case, there are $$$2$$$ ways to partition it: The sequence $$$[1, 1]$$$ itself, then $$$s = [2]$$$ is balanced; Partition into two subsequences $$$[1,,1]$$$, then $$$s = [1, 1]$$$ is balanced. For the third test case, there are $$$3$$$ ways to partition it: The sequence $$$[0, 0, 1, 0]$$$ itself, then $$$s = [1]$$$ is balanced; $$$[0 ,, 0, 1 ,, 0]$$$, then $$$s = [0, 1, 0]$$$ is balanced; $$$[0, 0 ,, 1 ,, 0]$$$, then $$$s = [0, 1, 0]$$$ is balanced. For the fourth test case, there are $$$4$$$ ways to partition it: The sequence $$$[1, 2, 3, 2, 1]$$$ itself, then $$$s = [9]$$$ is balanced; $$$[1, 2 ,, 3 ,, 2, 1]$$$, then $$$s = [3, 3, 3]$$$ is balanced; $$$[1 ,, 2, 3, 2 ,, 1]$$$, then $$$s = [1, 7, 1]$$$ is balanced; $$$[1 ,, 2 ,, 3 ,, 2 ,, 1]$$$, then $$$s = [1, 2, 3, 2, 1]$$$ is balanced. For the fifth test case, there are $$$2$$$ ways to partition it: The sequence $$$[1, 3, 5, 7, 9]$$$ itself, then $$$s = [25]$$$ is balanced; $$$[1, 3, 5 ,, 7 ,, 9]$$$, then $$$s = [9, 7, 9]$$$ is balanced. For the sixth test case, every possible partition should be counted. So the answer is $$$2^{32-1} equiv 150994942 pmod {998244353}$$$.
2,300
true
false
false
true
false
false
false
false
false
false
1,857
555B
Andrewid the Android is a galaxy-famous detective. He is now chasing a criminal hiding on the planet Oxa-5, the planet almost fully covered with water. The only dry land there is an archipelago of _n_ narrow islands located in a row. For more comfort let's represent them as non-intersecting segments on a straight line: island _i_ has coordinates [_l__i_,u2009_r__i_], besides, _r__i_u2009<u2009_l__i_u2009+u20091 for 1u2009≀u2009_i_u2009≀u2009_n_u2009-u20091. To reach the goal, Andrewid needs to place a bridge between each pair of adjacent islands. A bridge of length _a_ can be placed between the _i_-th and the (_i_u2009+u20091)-th islads, if there are such coordinates of _x_ and _y_, that _l__i_u2009≀u2009_x_u2009≀u2009_r__i_, _l__i_u2009+u20091u2009≀u2009_y_u2009≀u2009_r__i_u2009+u20091 and _y_u2009-u2009_x_u2009=u2009_a_. The detective was supplied with _m_ bridges, each bridge can be used at most once. Help him determine whether the bridges he got are enough to connect each pair of adjacent islands. Input The first line contains integers _n_ (2u2009≀u2009_n_u2009≀u20092Β·105) and _m_ (1u2009≀u2009_m_u2009≀u20092Β·105) β€” the number of islands and bridges. Next _n_ lines each contain two integers _l__i_ and _r__i_ (1u2009≀u2009_l__i_u2009≀u2009_r__i_u2009≀u20091018) β€” the coordinates of the island endpoints. The last line contains _m_ integer numbers _a_1,u2009_a_2,u2009...,u2009_a__m_ (1u2009≀u2009_a__i_u2009≀u20091018) β€” the lengths of the bridges that Andrewid got. Output If it is impossible to place a bridge between each pair of adjacent islands in the required manner, print on a single line "No" (without the quotes), otherwise print in the first line "Yes" (without the quotes), and in the second line print _n_u2009-u20091 numbers _b_1,u2009_b_2,u2009...,u2009_b__n_u2009-u20091, which mean that between islands _i_ and _i_u2009+u20091 there must be used a bridge number _b__i_. If there are multiple correct answers, print any of them. Note that in this problem it is necessary to print "Yes" and "No" in correct case. Examples Input 4 4 1 4 7 8 9 10 12 14 4 5 3 8 Input 2 1 1 1 1000000000000000000 1000000000000000000 999999999999999999 Note In the first sample test you can, for example, place the second bridge between points 3 and 8, place the third bridge between points 7 and 10 and place the first bridge between points 10 and 14. In the second sample test the first bridge is too short and the second bridge is too long, so the solution doesn't exist.
2,000
false
true
false
false
true
false
false
false
true
false
7,614
1806B
You are given an array $$$a$$$ of length $$$n$$$. The score of $$$a$$$ is the MEX$$$^{dagger}$$$ of $$$[a_1+a_2,a_2+a_3,ldots,a_{n-1}+a_n]$$$. Find the minimum score of $$$a$$$ if you are allowed to rearrange elements of $$$a$$$ in any order. Note that you are not required to construct the array $$$a$$$ that achieves the minimum score. $$$^{dagger}$$$ The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance: The MEX of $$$[2,2,1]$$$ is $$$0$$$, because $$$0$$$ does not belong to the array. The MEX of $$$[3,1,0,1]$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ belong to the array, but $$$2$$$ does not. The MEX of $$$[0,3,1,2]$$$ is $$$4$$$ because $$$0$$$, $$$1$$$, $$$2$$$, and $$$3$$$ belong to the array, but $$$4$$$ does not. Input The first line contains a single integer $$$t$$$ ($$$1le tle 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$$$ ($$$2le nle 2cdot10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 2cdot 10^5$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, output the minimum score of $$$a$$$ after rearranging the elements of $$$a$$$ in any order. Example Input 3 2 0 0 3 0 0 1 8 1 0 0 0 2 0 3 0 Note In the first test case, it is optimal to rearrange $$$a$$$ as $$$[0,0]$$$, the score of this array is the MEX of $$$[0+0]=[0]$$$, which is $$$1$$$. In the second test case, it is optimal to rearrange $$$a$$$ as $$$[0,1,0]$$$, the score of this array is the MEX of $$$[0+1,1+0]=[1,1]$$$, which is $$$0$$$.
900
false
true
false
false
false
true
false
false
false
false
1,441
1470B
Let us call two integers $$$x$$$ and $$$y$$$ adjacent if $$$frac{lcm(x, y)}{gcd(x, y)}$$$ is a perfect square. For example, $$$3$$$ and $$$12$$$ are adjacent, but $$$6$$$ and $$$9$$$ are not. Here $$$gcd(x, y)$$$ denotes the $$$ denotes the , that are adjacent to the current value. Let $$$d_i$$$ be the number of adjacent elements to $$$a_i$$$ (including $$$a_i$$$ itself). The beauty of the array is defined as $$$max_{1 le i le n} d_i$$$. You are given $$$q$$$ queries: each query is described by an integer $$$w$$$, and you have to output the beauty of the array after $$$w$$$ seconds. Input The first input line contains a single integer $$$t$$$ ($$$1 le t le 10^5)$$$xa0β€” the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$)xa0β€” the length of the array. The following line contains $$$n$$$ integers $$$a_1, ldots, a_n$$$ ($$$1 le a_i le 10^6$$$)xa0β€” array elements. The next line contain a single integer $$$q$$$ ($$$1 le q le 3 cdot 10^5$$$)xa0β€” the number of queries. The following $$$q$$$ lines contain a single integer $$$w$$$ each ($$$0 le w le 10^{18}$$$)xa0β€” the queries themselves. It is guaranteed that the sum of values $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$, and the sum of values $$$q$$$ over all test cases does not exceed $$$3 cdot 10^5$$$ Output For each query output a single integerxa0β€” the beauty of the array at the corresponding moment. Example Input 2 4 6 8 4 2 1 0 6 12 3 20 5 80 1 1 1 Note In the first test case, the initial array contains elements $$$[6, 8, 4, 2]$$$. Element $$$a_4=2$$$ in this array is adjacent to $$$a_4=2$$$ (since $$$frac{lcm(2, 2)}{gcd(2, 2)}=frac{2}{2}=1=1^2$$$) and $$$a_2=8$$$ (since $$$frac{lcm(8,2)}{gcd(8, 2)}=frac{8}{2}=4=2^2$$$). Hence, $$$d_4=2$$$, and this is the maximal possible value $$$d_i$$$ in this array. In the second test case, the initial array contains elements $$$[12, 3, 20, 5, 80, 1]$$$. The elements adjacent to $$$12$$$ are $$${12, 3}$$$, the elements adjacent to $$$3$$$ are $$${12, 3}$$$, the elements adjacent to $$$20$$$ are $$${20, 5, 80}$$$, the elements adjacent to $$$5$$$ are $$${20, 5, 80}$$$, the elements adjacent to $$$80$$$ are $$${20, 5, 80}$$$, the elements adjacent to $$$1$$$ are $$${1}$$$. After one second, the array is transformed into $$$[36, 36, 8000, 8000, 8000, 1]$$$.
1,900
true
false
false
false
false
false
false
false
false
true
3,331
1283F
Polycarp has decided to decorate his room because the New Year is soon. One of the main decorations that Polycarp will install is the garland he is going to solder himself. Simple garlands consisting of several lamps connected by one wire are too boring for Polycarp. He is going to solder a garland consisting of $$$n$$$ lamps and $$$n - 1$$$ wires. Exactly one lamp will be connected to power grid, and power will be transmitted from it to other lamps by the wires. Each wire connectes exactly two lamps; one lamp is called the main lamp for this wire (the one that gets power from some other wire and transmits it to this wire), the other one is called the auxiliary lamp (the one that gets power from this wire). Obviously, each lamp has at most one wire that brings power to it (and this lamp is the auxiliary lamp for this wire, and the main lamp for all other wires connected directly to it). Each lamp has a brightness value associated with it, the $$$i$$$-th lamp has brightness $$$2^i$$$. We define the importance of the wire as the sum of brightness values over all lamps that become disconnected from the grid if the wire is cut (and all other wires are still working). Polycarp has drawn the scheme of the garland he wants to make (the scheme depicts all $$$n$$$ lamp and $$$n - 1$$$ wires, and the lamp that will be connected directly to the grid is marked; the wires are placed in such a way that the power can be transmitted to each lamp). After that, Polycarp calculated the importance of each wire, enumerated them from $$$1$$$ to $$$n - 1$$$ in descending order of their importance, and then wrote the index of the main lamp for each wire (in the order from the first wire to the last one). The following day Polycarp bought all required components of the garland and decided to solder it β€” but he could not find the scheme. Fortunately, Polycarp found the list of indices of main lamps for all wires. Can you help him restore the original scheme? Input The first line contains one integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$) β€” the number of lamps. The second line contains $$$n - 1$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_{n - 1}$$$ ($$$1 le a_i le n$$$), where $$$a_i$$$ is the index of the main lamp for the $$$i$$$-th wire (wires are numbered in descending order of importance). Output If it is impossible to restore the original scheme, print one integer $$$-1$$$. Otherwise print the scheme as follows. In the first line, print one integer $$$k$$$ ($$$1 le k le n$$$) β€” the index of the lamp that is connected to the power grid. Then print $$$n - 1$$$ lines, each containing two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le n$$$, $$$x_i e y_i$$$) β€” the indices of the lamps connected by some wire. The descriptions of the wires (and the lamps connected by a wire) can be printed in any order. The printed description must correspond to a scheme of a garland such that Polycarp could have written the list $$$a_1$$$, $$$a_2$$$, ..., $$$a_{n - 1}$$$ from it. If there are multiple such schemes, output any of them.
2,200
false
true
false
false
false
true
false
false
false
false
4,275
282B
The Bitlandians are quite weird people. They have very peculiar customs. As is customary, Uncle J. wants to have _n_ eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work. The kids are excited because just as is customary, they're going to be paid for the job! Overall uncle J. has got _n_ eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000. Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500. Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible. Input The first line contains integer _n_ (1u2009≀u2009_n_u2009≀u2009106) β€” the number of eggs. Next _n_ lines contain two integers _a__i_ and _g__i_ each (0u2009≀u2009_a__i_,u2009_g__i_u2009≀u20091000;xa0_a__i_u2009+u2009_g__i_u2009=u20091000): _a__i_ is the price said by A. for the _i_-th egg and _g__i_ is the price said by G. for the _i_-th egg. Output If it is impossible to assign the painting, print "-1" (without quotes). Otherwise print a string, consisting of _n_ letters "G" and "A". The _i_-th letter of this string should represent the child who will get the _i_-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as _S__a_, and the money Uncle J. must pay G. for the painting as _S__g_, then this inequality must hold: _S__a_u2009u2009-u2009u2009_S__g_u2009u2009≀u2009u2009500. If there are several solutions, you are allowed to print any of them. Examples Input 3 400 600 400 600 400 600
1,500
true
true
false
false
false
false
false
false
false
false
8,707
909C
In Python, code blocks don't have explicit begin/end or curly braces to mark beginning and end of the block. Instead, code blocks are defined by indentation. We will consider an extremely simplified subset of Python with only two types of statements. Simple statements are written in a single line, one per line. An example of a simple statement is assignment. For statements are compound statements: they contain one or several other statements. For statement consists of a header written in a separate line which starts with "for" prefix, and loop body. Loop body is a block of statements indented one level further than the header of the loop. Loop body can contain both types of statements. Loop body can't be empty. You are given a sequence of statements without indentation. Find the number of ways in which the statements can be indented to form a valid Python program. Input The first line contains a single integer _N_ (1u2009≀u2009_N_u2009≀u20095000)xa0β€” the number of commands in the program. _N_ lines of the program follow, each line describing a single command. Each command is either "f" (denoting "for statement") or "s" ("simple statement"). It is guaranteed that the last line is a simple statement. Output Output one line containing an integer - the number of ways the given sequence of statements can be indented modulo 109u2009+u20097. Note In the first test case, there is only one way to indent the program: the second for statement must be part of the body of the first one. simple statement for statement for statement simple statement In the second test case, there are two ways to indent the program: the second for statement can either be part of the first one's body or a separate statement following the first one. for statement simple statement for statement simple statement or for statement simple statement for statement simple statement
1,800
false
false
false
true
false
false
false
false
false
false
6,110
930C
Young Teodor enjoys drawing. His favourite hobby is drawing segments with integer borders inside his huge [1;_m_] segment. One day Teodor noticed that picture he just drawn has one interesting feature: there doesn't exist an integer point, that belongs each of segments in the picture. Having discovered this fact, Teodor decided to share it with Sasha. Sasha knows that Teodor likes to show off so he never trusts him. Teodor wants to prove that he can be trusted sometimes, so he decided to convince Sasha that there is no such integer point in his picture, which belongs to each segment. However Teodor is lazy person and neither wills to tell Sasha all coordinates of segments' ends nor wills to tell him their amount, so he suggested Sasha to ask him series of questions 'Given the integer point _x__i_, how many segments in Fedya's picture contain that point?', promising to tell correct answers for this questions. Both boys are very busy studying and don't have much time, so they ask you to find out how many questions can Sasha ask Teodor, that having only answers on his questions, Sasha can't be sure that Teodor isn't lying to him. Note that Sasha doesn't know amount of segments in Teodor's picture. Sure, Sasha is smart person and never asks about same point twice. Input First line of input contains two integer numbers: _n_ and _m_ (1u2009≀u2009_n_,u2009_m_u2009≀u2009100u2009000)xa0β€” amount of segments of Teodor's picture and maximal coordinate of point that Sasha can ask about. _i_th of next _n_ lines contains two integer numbers _l__i_ and _r__i_ (1u2009≀u2009_l__i_u2009≀u2009_r__i_u2009≀u2009_m_)xa0β€” left and right ends of _i_th segment in the picture. Note that that left and right ends of segment can be the same point. It is guaranteed that there is no integer point, that belongs to all segments. Output Single line of output should contain one integer number _k_ – size of largest set (_x__i_,u2009_cnt_(_x__i_)) where all _x__i_ are different, 1u2009≀u2009_x__i_u2009≀u2009_m_, and _cnt_(_x__i_) is amount of segments, containing point with coordinate _x__i_, such that one can't be sure that there doesn't exist point, belonging to all of segments in initial picture, if he knows only this set(and doesn't know _n_). Note First example shows situation where Sasha can never be sure that Teodor isn't lying to him, because even if one knows _cnt_(_x__i_) for each point in segment [1;4], he can't distinguish this case from situation Teodor has drawn whole [1;4] segment. In second example Sasha can ask about 5 points e.g. 1,u20092,u20093,u20095,u20096, still not being sure if Teodor haven't lied to him. But once he knows information about all points in [1;6] segment, Sasha can be sure that Teodor haven't lied to him.
1,900
false
false
false
true
true
false
false
false
false
false
5,984
1288A
Adilbek was assigned to a special project. For Adilbek it means that he has $$$n$$$ days to run a special program and provide its results. But there is a problem: the program needs to run for $$$d$$$ days to calculate the results. Fortunately, Adilbek can optimize the program. If he spends $$$x$$$ ($$$x$$$ is a non-negative integer) days optimizing the program, he will make the program run in $$$leftlceil frac{d}{x + 1} ight ceil$$$ days ($$$leftlceil a ight ceil$$$ is the ceiling function: $$$leftlceil 2.4 ight ceil = 3$$$, $$$leftlceil 2 ight ceil = 2$$$). The program cannot be run and optimized simultaneously, so the total number of days he will spend is equal to $$$x + leftlceil frac{d}{x + 1} ight ceil$$$. Will Adilbek be able to provide the generated results in no more than $$$n$$$ days? Input The first line contains a single integer $$$T$$$ ($$$1 le T le 50$$$) β€” the number of test cases. The next $$$T$$$ lines contain test cases – one per line. Each line contains two integers $$$n$$$ and $$$d$$$ ($$$1 le n le 10^9$$$, $$$1 le d le 10^9$$$) β€” the number of days before the deadline and the number of days the program runs. Output Print $$$T$$$ answers β€” one per test case. For each test case print YES (case insensitive) if Adilbek can fit in $$$n$$$ days or NO (case insensitive) otherwise. Note In the first test case, Adilbek decides not to optimize the program at all, since $$$d le n$$$. In the second test case, Adilbek can spend $$$1$$$ day optimizing the program and it will run $$$leftlceil frac{5}{2} ight ceil = 3$$$ days. In total, he will spend $$$4$$$ days and will fit in the limit. In the third test case, it's impossible to fit in the limit. For example, if Adilbek will optimize the program $$$2$$$ days, it'll still work $$$leftlceil frac{11}{2+1} ight ceil = 4$$$ days.
1,100
true
false
false
false
false
false
true
true
false
false
4,252
1968A
Problem - 1968A - 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 brute force math number theory *800 No tag edit access β†’ Contest materials ") $$$ such that $$$gcd(x,y)+y$$$ is maximum possible. Note that if there is more than one $$$y$$$ which satisfies the statement, you are allowed to find any. $$$gcd(a,b)$$$ is the Greatest Common Divisor of $$$a$$$ and $$$b$$$. For example, $$$gcd(6,4)=2$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β€” the number of test cases. Each of the following $$$t$$$ lines contains a single integer $$$x$$$ ($$$2 le x le 1000$$$). Output For each test case, output any $$$y$$$ ($$$1 le y < x$$$), which satisfies the statement. Example Input 7 10 7 21 100 2 1000 6 Output 5 6 18 98 1 750 3
800
true
false
false
false
false
false
true
false
false
false
500
1173B
Nauuo is a girl who loves playing chess. One day she invented a game by herself which needs $$$n$$$ chess pieces to play on a $$$m imes m$$$ chessboard. The rows and columns are numbered from $$$1$$$ to $$$m$$$. We denote a cell on the intersection of the $$$r$$$-th row and $$$c$$$-th column as $$$(r,c)$$$. The game's goal is to place $$$n$$$ chess pieces numbered from $$$1$$$ to $$$n$$$ on the chessboard, the $$$i$$$-th piece lies on $$$(r_i,,c_i)$$$, while the following rule is satisfied: for all pairs of pieces $$$i$$$ and $$$j$$$, $$$r_i-r_j+c_i-c_jgei-j$$$. Here $$$x$$$ means the absolute value of $$$x$$$. However, Nauuo discovered that sometimes she couldn't find a solution because the chessboard was too small. She wants to find the smallest chessboard on which she can put $$$n$$$ pieces according to the rules. She also wonders how to place the pieces on such a chessboard. Can you help her? Input The only line contains a single integer $$$n$$$ ($$$1le nle 1000$$$) β€” the number of chess pieces for the game. Output The first line contains a single integer β€” the minimum value of $$$m$$$, where $$$m$$$ is the length of sides of the suitable chessboard. The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$r_i$$$ and $$$c_i$$$ ($$$1le r_i,c_ile m$$$) β€” the coordinates of the $$$i$$$-th chess piece. If there are multiple answers, print any. Note In the first example, you can't place the two pieces on a $$$1 imes1$$$ chessboard without breaking the rule. But you can place two pieces on a $$$2 imes2$$$ chessboard like this: In the second example, you can't place four pieces on a $$$2 imes2$$$ chessboard without breaking the rule. For example, if you place the pieces like this: then $$$r_1-r_3+c_1-c_3=1-2+1-1=1$$$, $$$1-3=2$$$, $$$1<2$$$; and $$$r_1-r_4+c_1-c_4=1-2+1-2=2$$$, $$$1-4=3$$$, $$$2<3$$$. It doesn't satisfy the rule. However, on a $$$3 imes3$$$ chessboard, you can place four pieces like this:
1,100
false
true
false
false
false
true
false
false
false
false
4,852
1700D
Recently in Divanovo, a huge river locks system was built. There are now $$$n$$$ locks, the $$$i$$$-th of them has the volume of $$$v_i$$$ liters, so that it can contain any amount of water between $$$0$$$ and $$$v_i$$$ liters. Each lock has a pipe attached to it. When the pipe is open, $$$1$$$ liter of water enters the lock every second. The locks system is built in a way to immediately transfer all water exceeding the volume of the lock $$$i$$$ to the lock $$$i + 1$$$. If the lock $$$i + 1$$$ is also full, water will be transferred further. Water exceeding the volume of the last lock pours out to the river. The picture illustrates $$$5$$$ locks with two open pipes at locks $$$1$$$ and $$$3$$$. Because locks $$$1$$$, $$$3$$$, and $$$4$$$ are already filled, effectively the water goes to locks $$$2$$$ and $$$5$$$. Note that the volume of the $$$i$$$-th lock may be greater than the volume of the $$$i + 1$$$-th lock. To make all locks work, you need to completely fill each one of them. The mayor of Divanovo is interested in $$$q$$$ independent queries. For each query, suppose that initially all locks are empty and all pipes are closed. Then, some pipes are opened simultaneously. For the $$$j$$$-th query the mayor asks you to calculate the minimum number of pipes to open so that all locks are filled no later than after $$$t_j$$$ seconds. Please help the mayor to solve this tricky problem and answer his queries. Input The first lines contains one integer $$$n$$$ ($$$1 le n le 200,000$$$)xa0β€” the number of locks. The second lines contains $$$n$$$ integers $$$v_1, v_2, dots, v_n$$$ ($$$1 le v_i le 10^9$$$))xa0β€” volumes of the locks. The third line contains one integer $$$q$$$ ($$$1 le q le 200,000$$$)xa0β€” the number of queries. Each of the next $$$q$$$ lines contains one integer $$$t_j$$$ ($$$1 le t_j le 10^9$$$)xa0β€” the number of seconds you have to fill all the locks in the query $$$j$$$. Output Print $$$q$$$ integers. The $$$j$$$-th of them should be equal to the minimum number of pipes to turn on so that after $$$t_j$$$ seconds all of the locks are filled. If it is impossible to fill all of the locks in given time, print $$$-1$$$. Examples Input 5 4 1 5 4 1 6 1 6 2 3 4 5 Input 5 4 4 4 4 4 6 1 3 6 5 2 4 Note There are $$$6$$$ queries in the first example test. In the queries $$$1, 3, 4$$$ the answer is $$$-1$$$. We need to wait $$$4$$$ seconds to fill the first lock even if we open all the pipes. In the sixth query we can open pipes in locks $$$1$$$, $$$3$$$, and $$$4$$$. After $$$4$$$ seconds the locks $$$1$$$ and $$$4$$$ are full. In the following $$$1$$$ second $$$1$$$ liter of water is transferred to the locks $$$2$$$ and $$$5$$$. The lock $$$3$$$ is filled by its own pipe. Similarly, in the second query one can open pipes in locks $$$1$$$, $$$3$$$, and $$$4$$$. In the fifth query one can open pipes $$$1, 2, 3, 4$$$.
1,900
true
true
false
true
false
false
false
true
false
false
2,079
1987F1
This is the easy version of the problem. The only difference between the two versions is the constraint on $$$n$$$. You can make hacks only if both versions of the problem are solved. You are given an array of integers $$$a$$$ of length $$$n$$$. In one operation, you will perform the following two-step process: 1. Choose an index $$$i$$$ such that $$$1 le i < a$$$ and $$$a_i = i$$$. 2. Remove $$$a_i$$$ and $$$a_{i+1}$$$ from the array and concatenate the remaining parts. Find the maximum number of times that you can perform the operation above. Input Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 le t le 100$$$)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 100$$$)xa0β€” the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$)xa0β€” the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$. Output For each test case, output a single integerxa0β€” the maximum number of times that you can perform the operation. Example Input 6 5 1 5 3 2 4 8 2 1 3 4 5 6 7 8 3 1 2 3 4 1 2 4 4 5 4 4 1 3 5 1 1 Note In the first test case, one possible optimal sequence of operations is $$$[ 1, 5, color{red}{3}, color{red}{2}, 4 ] ightarrow [color{red}{1}, color{red}{5}, 4] ightarrow [4]$$$. In the third test case, one possible optimal sequence of operations is $$$[1, color{red}{2}, color{red}{3}] ightarrow [1]$$$.
2,500
false
false
false
true
false
false
false
false
false
false
352
1542D
You are given a sequence $$$A$$$, where its elements are either in the form + x or -, where $$$x$$$ is an integer. For such a sequence $$$S$$$ where its elements are either in the form + x or -, define $$$f(S)$$$ as follows: iterate through $$$S$$$'s elements from the first one to the last one, and maintain a multiset $$$T$$$ as you iterate through it. for each element, if it's in the form + x, add $$$x$$$ to $$$T$$$; otherwise, erase the smallest element from $$$T$$$ (if $$$T$$$ is empty, do nothing). after iterating through all $$$S$$$'s elements, compute the sum of all elements in $$$T$$$. $$$f(S)$$$ is defined as the sum. The sequence $$$b$$$ is a subsequence of the sequence $$$a$$$ if $$$b$$$ can be derived from $$$a$$$ by removing zero or more elements without changing the order of the remaining elements. For all $$$A$$$'s subsequences $$$B$$$, compute the sum of $$$f(B)$$$, modulo $$$998,244,353$$$. Input The first line contains an integer $$$n$$$ ($$$1leq nleq 500$$$) β€” the length of $$$A$$$. Each of the next $$$n$$$ lines begins with an operator + or -. If the operator is +, then it's followed by an integer $$$x$$$ ($$$1le x<998,244,353$$$). The $$$i$$$-th line of those $$$n$$$ lines describes the $$$i$$$-th element in $$$A$$$. Output Print one integer, which is the answer to the problem, modulo $$$998,244,353$$$. Examples Input 15 + 2432543 - + 4567886 + 65638788 - + 578943 - - + 62356680 - + 711111 - + 998244352 - - Note In the first example, the following are all possible pairs of $$$B$$$ and $$$f(B)$$$: $$$B=$$$ {}, $$$f(B)=0$$$. $$$B=$$$ {-}, $$$f(B)=0$$$. $$$B=$$$ {+ 1, -}, $$$f(B)=0$$$. $$$B=$$$ {-, + 1, -}, $$$f(B)=0$$$. $$$B=$$$ {+ 2, -}, $$$f(B)=0$$$. $$$B=$$$ {-, + 2, -}, $$$f(B)=0$$$. $$$B=$$$ {-}, $$$f(B)=0$$$. $$$B=$$$ {-, -}, $$$f(B)=0$$$. $$$B=$$$ {+ 1, + 2}, $$$f(B)=3$$$. $$$B=$$$ {+ 1, + 2, -}, $$$f(B)=2$$$. $$$B=$$$ {-, + 1, + 2}, $$$f(B)=3$$$. $$$B=$$$ {-, + 1, + 2, -}, $$$f(B)=2$$$. $$$B=$$$ {-, + 1}, $$$f(B)=1$$$. $$$B=$$$ {+ 1}, $$$f(B)=1$$$. $$$B=$$$ {-, + 2}, $$$f(B)=2$$$. $$$B=$$$ {+ 2}, $$$f(B)=2$$$. The sum of these values is $$$16$$$.
2,200
true
false
true
true
false
false
false
false
false
false
2,931
158D
The Berland University is preparing to celebrate the 256-th anniversary of its founding! A specially appointed Vice Rector for the celebration prepares to decorate the campus. In the center of the campus _n_ ice sculptures were erected. The sculptures are arranged in a circle at equal distances from each other, so they form a regular _n_-gon. They are numbered in clockwise order with numbers from 1 to _n_. The site of the University has already conducted a voting that estimated each sculpture's characteristic of _t__i_ β€” the degree of the sculpture's attractiveness. The values of _t__i_ can be positive, negative or zero. When the university rector came to evaluate the work, he said that this might be not the perfect arrangement. He suggested to melt some of the sculptures so that: the remaining sculptures form a regular polygon (the number of vertices should be between 3 and _n_), the sum of the _t__i_ values of the remaining sculptures is maximized. Help the Vice Rector to analyze the criticism β€” find the maximum value of _t__i_ sum which can be obtained in this way. It is allowed not to melt any sculptures at all. The sculptures can not be moved. Input The first input line contains an integer _n_ (3u2009≀u2009_n_u2009≀u200920000) β€” the initial number of sculptures. The second line contains a sequence of integers _t_1,u2009_t_2,u2009...,u2009_t__n_, _t__i_ β€” the degree of the _i_-th sculpture's attractiveness (u2009-u20091000u2009≀u2009_t__i_u2009≀u20091000). The numbers on the line are separated by spaces. Output Print the required maximum sum of the sculptures' attractiveness. Note In the first sample it is best to leave every second sculpture, that is, leave sculptures with attractivenesses: 2, 4, 5 ΠΈ 3.
1,300
false
false
false
false
false
false
true
false
false
false
9,242
549F
Yura has a team of _k_ developers and a list of _n_ tasks numbered from 1 to _n_. Yura is going to choose some tasks to be done this week. Due to strange Looksery habits the numbers of chosen tasks should be a segment of consecutive integers containing no less than 2 numbers, i. e. a sequence of form _l_,u2009_l_u2009+u20091,u2009...,u2009_r_ for some 1u2009≀u2009_l_u2009<u2009_r_u2009≀u2009_n_. Every task _i_ has an integer number _a__i_ associated with it denoting how many man-hours are required to complete the _i_-th task. Developers are not self-confident at all, and they are actually afraid of difficult tasks. Knowing that, Yura decided to pick up a hardest task (the one that takes the biggest number of man-hours to be completed, among several hardest tasks with same difficulty level he chooses arbitrary one) and complete it on his own. So, if tasks with numbers [_l_,u2009_r_] are chosen then the developers are left with _r_u2009-u2009_l_ tasks to be done by themselves. Every developer can spend any integer amount of hours over any task, but when they are done with the whole assignment there should be exactly _a__i_ man-hours spent over the _i_-th task. The last, but not the least problem with developers is that one gets angry if he works more than another developer. A set of tasks [_l_,u2009_r_] is considered good if it is possible to find such a distribution of work that allows to complete all the tasks and to have every developer working for the same amount of time (amount of work performed by Yura doesn't matter for other workers as well as for him). For example, let's suppose that Yura have chosen tasks with following difficulties: _a_u2009=u2009[1,u20092,u20093,u20094], and he has three developers in his disposal. He takes the hardest fourth task to finish by himself, and the developers are left with tasks with difficulties [1,u20092,u20093]. If the first one spends an hour on the first task and an hour on the third one, the second developer spends two hours on the second task and the third developer spends two hours on the third task, then they are done, since every developer worked exactly for two hours and every task has been worked over for the required amount of time. As another example, if the first task required two hours instead of one to be completed then it would be impossible to assign the tasks in a way described above. Besides work, Yura is fond of problem solving. He wonders how many pairs (_l_,u2009_r_) (1u2009≀u2009_l_u2009<u2009_r_u2009≀u2009_n_) exists such that a segment [_l_,u2009_r_] is good? Yura has already solved this problem, but he has no time to write the code. Please, help Yura and implement the solution for this problem.
2,800
false
false
false
false
true
false
false
false
false
false
7,640
601E
There's a famous museum in the city where KleofΓ‘Ε‘ lives. In the museum, _n_ exhibits (numbered 1 through _n_) had been displayed for a long time; the _i_-th of those exhibits has value _v__i_ and mass _w__i_. Then, the museum was bought by a large financial group and started to vary the exhibits. At about the same time, KleofΓ‘Ε‘... gained interest in the museum, so to say. You should process _q_ events of three types: type 1 β€” the museum displays an exhibit with value _v_ and mass _w_; the exhibit displayed in the _i_-th event of this type is numbered _n_u2009+u2009_i_ (see sample explanation for more details) type 2 β€” the museum removes the exhibit with number _x_ and stores it safely in its vault type 3 β€” KleofΓ‘Ε‘ visits the museum and wonders (for no important reason at all, of course): if there was a robbery and exhibits with total mass at most _m_ were stolen, what would their maximum possible total value be? For each event of type 3, let _s_(_m_) be the maximum possible total value of stolen exhibits with total mass u2009≀u2009_m_. Formally, let _D_ be the set of numbers of all exhibits that are currently displayed (so initially _D_ = {1, ..., n}). Let _P_(_D_) be the set of all subsets of _D_ and let Then, _s_(_m_) is defined as Compute _s_(_m_) for each . Note that the output follows a special format. Input The first line of the input contains two space-separated integers _n_ and _k_ (1u2009≀u2009_n_u2009≀u20095000, 1u2009≀u2009_k_u2009≀u20091000) β€” the initial number of exhibits in the museum and the maximum interesting mass of stolen exhibits. Then, _n_ lines follow. The _i_-th of them contains two space-separated positive integers _v__i_ and _w__i_ (1u2009≀u2009_v__i_u2009≀u20091u2009000u2009000, 1u2009≀u2009_w__i_u2009≀u20091000)xa0β€” the value and mass of the _i_-th exhibit. The next line contains a single integer _q_ (1u2009≀u2009_q_u2009≀u200930u2009000)xa0β€” the number of events. Each of the next _q_ lines contains the description of one event in the following format: 1 _v_ _w_ β€” an event of type 1, a new exhibit with value _v_ and mass _w_ has been added (1u2009≀u2009_v_u2009≀u20091u2009000u2009000, 1u2009≀u2009_w_u2009≀u20091000) 2 _x_ β€” an event of type 2, the exhibit with number _x_ has been removed; it's guaranteed that the removed exhibit had been displayed at that time 3 β€” an event of type 3, KleofΓ‘Ε‘ visits the museum and asks his question There will be at most 10u2009000 events of type 1 and at least one event of type 3. Output As the number of values _s_(_m_) can get large, output the answers to events of type 3 in a special format. For each event of type 3, consider the values _s_(_m_) computed for the question that KleofΓ‘Ε‘ asked in this event; print one line containing a single number where _p_u2009=u2009107u2009+u200919 and _q_u2009=u2009109u2009+u20097. Print the answers to events of type 3 in the order in which they appear in the input. Examples Input 3 10 30 4 60 6 5 1 9 3 1 42 5 1 20 3 3 2 2 2 4 3 1 40 6 3 Output 556674384 168191145 947033915 181541912 Input 3 1000 100 42 100 47 400 15 4 2 2 2 1 2 3 3 Note In the first sample, the numbers of displayed exhibits and values _s_(1),u2009...,u2009_s_(10) for individual events of type 3 are, in order: The values of individual exhibits are _v_1u2009=u200930,u2009_v_2u2009=u200960,u2009_v_3u2009=u20095,u2009_v_4u2009=u200942,u2009_v_5u2009=u200920,u2009_v_6u2009=u200940 and their masses are _w_1u2009=u20094,u2009_w_2u2009=u20096,u2009_w_3u2009=u20091,u2009_w_4u2009=u20095,u2009_w_5u2009=u20093,u2009_w_6u2009=u20096. In the second sample, the only question is asked after removing all exhibits, so _s_(_m_)u2009=u20090 for any _m_.
2,800
false
false
false
true
true
false
false
false
false
false
7,436
1154F
There are $$$n$$$ shovels in the nearby shop. The $$$i$$$-th shovel costs $$$a_i$$$ bourles. Misha has to buy exactly $$$k$$$ shovels. Each shovel can be bought no more than once. Misha can buy shovels by several purchases. During one purchase he can choose any subset of remaining (non-bought) shovels and buy this subset. There are also $$$m$$$ special offers in the shop. The $$$j$$$-th of them is given as a pair $$$(x_j, y_j)$$$, and it means that if Misha buys exactly $$$x_j$$$ shovels during one purchase then $$$y_j$$$ most cheapest of them are for free (i.e. he will not pay for $$$y_j$$$ most cheapest shovels during the current purchase). Misha can use any offer any (possibly, zero) number of times, but he cannot use more than one offer during one purchase (but he can buy shovels without using any offers). Your task is to calculate the minimum cost of buying $$$k$$$ shovels, if Misha buys them optimally. Input The first line of the input contains three integers $$$n, m$$$ and $$$k$$$ ($$$1 le n, m le 2 cdot 10^5, 1 le k le min(n, 2000)$$$) β€” the number of shovels in the shop, the number of special offers and the number of shovels Misha has to buy, correspondingly. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 2 cdot 10^5$$$), where $$$a_i$$$ is the cost of the $$$i$$$-th shovel. The next $$$m$$$ lines contain special offers. The $$$j$$$-th of them is given as a pair of integers $$$(x_i, y_i)$$$ ($$$1 le y_i le x_i le n$$$) and means that if Misha buys exactly $$$x_i$$$ shovels during some purchase, then he can take $$$y_i$$$ most cheapest of them for free. Output Print one integer β€” the minimum cost of buying $$$k$$$ shovels if Misha buys them optimally. Examples Input 7 4 5 2 5 4 2 6 3 1 2 1 6 5 2 1 3 1 Input 9 4 8 6 8 5 1 8 1 1 2 1 9 2 8 4 5 3 9 7 Note In the first example Misha can buy shovels on positions $$$1$$$ and $$$4$$$ (both with costs $$$2$$$) during the first purchase and get one of them for free using the first or the third special offer. And then he can buy shovels on positions $$$3$$$ and $$$6$$$ (with costs $$$4$$$ and $$$3$$$) during the second purchase and get the second one for free using the first or the third special offer. Then he can buy the shovel on a position $$$7$$$ with cost $$$1$$$. So the total cost is $$$4 + 2 + 1 = 7$$$. In the second example Misha can buy shovels on positions $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$ and $$$8$$$ (costs are $$$6$$$, $$$8$$$, $$$5$$$, $$$1$$$ and $$$2$$$) and get three cheapest (with costs $$$5$$$, $$$1$$$ and $$$2$$$) for free. And then he can buy shovels on positions $$$6$$$, $$$7$$$ and $$$9$$$ (all with costs $$$1$$$) without using any special offers. So the total cost is $$$6 + 8 + 1 + 1 + 1 = 17$$$. In the third example Misha can buy four cheapest shovels without using any special offers and get the total cost $$$17$$$.
2,100
false
true
false
true
false
false
false
false
true
false
4,938
1497D
Please note the non-standard memory limit. There are $$$n$$$ problems numbered with integers from $$$1$$$ to $$$n$$$. $$$i$$$-th problem has the complexity $$$c_i = 2^i$$$, tag $$$tag_i$$$ and score $$$s_i$$$. After solving the problem $$$i$$$ it's allowed to solve problem $$$j$$$ if and only if $$$ ext{IQ} < c_i - c_j$$$ and $$$tag_i eq tag_j$$$. After solving it your $$$ ext{IQ}$$$ changes and becomes $$$ ext{IQ} = c_i - c_j$$$ and you gain $$$s_i - s_j$$$ points. Any problem can be the first. You can solve problems in any order and as many times as you want. Initially your $$$ ext{IQ} = 0$$$. Find the maximum number of points that can be earned. Input The first line contains a single integer $$$t$$$ $$$(1 le t le 100)$$$ xa0β€” the number of test cases. The first line of each test case contains an integer $$$n$$$ $$$(1 le n le 5000)$$$ xa0β€” the number of problems. The second line of each test case contains $$$n$$$ integers $$$tag_1, tag_2, ldots, tag_n$$$ $$$(1 le tag_i le n)$$$ xa0β€” tags of the problems. The third line of each test case contains $$$n$$$ integers $$$s_1, s_2, ldots, s_n$$$ $$$(1 le s_i le 10^9)$$$ xa0β€” scores of the problems. It's guaranteed that sum of $$$n$$$ over all test cases does not exceed $$$5000$$$. Output For each test case print a single integer xa0β€” the maximum number of points that can be earned. Example Input 5 4 1 2 3 4 5 10 15 20 4 1 2 1 2 5 10 15 20 4 2 2 4 1 2 8 19 1 2 1 1 6 9 1 1 666 Note In the first test case optimal sequence of solving problems is as follows: 1. $$$1 ightarrow 2$$$, after that total score is $$$5$$$ and $$$ ext{IQ} = 2$$$ 2. $$$2 ightarrow 3$$$, after that total score is $$$10$$$ and $$$ ext{IQ} = 4$$$ 3. $$$3 ightarrow 1$$$, after that total score is $$$20$$$ and $$$ ext{IQ} = 6$$$ 4. $$$1 ightarrow 4$$$, after that total score is $$$35$$$ and $$$ ext{IQ} = 14$$$ In the second test case optimal sequence of solving problems is as follows: 1. $$$1 ightarrow 2$$$, after that total score is $$$5$$$ and $$$ ext{IQ} = 2$$$ 2. $$$2 ightarrow 3$$$, after that total score is $$$10$$$ and $$$ ext{IQ} = 4$$$ 3. $$$3 ightarrow 4$$$, after that total score is $$$15$$$ and $$$ ext{IQ} = 8$$$ 4. $$$4 ightarrow 1$$$, after that total score is $$$35$$$ and $$$ ext{IQ} = 14$$$ In the third test case optimal sequence of solving problems is as follows: 1. $$$1 ightarrow 3$$$, after that total score is $$$17$$$ and $$$ ext{IQ} = 6$$$ 2. $$$3 ightarrow 4$$$, after that total score is $$$35$$$ and $$$ ext{IQ} = 8$$$ 3. $$$4 ightarrow 2$$$, after that total score is $$$42$$$ and $$$ ext{IQ} = 12$$$
2,500
false
false
false
true
false
false
false
false
false
true
3,184
1148H
You are given an array which is initially empty. You need to perform $$$n$$$ operations of the given format: "$$$a$$$ $$$l$$$ $$$r$$$ $$$k$$$": append $$$a$$$ to the end of the array. After that count the number of integer pairs $$$x, y$$$ such that $$$l leq x leq y leq r$$$ and $$$operatorname{mex}(a_{x}, a_{x+1}, ldots, a_{y}) = k$$$. The elements of the array are numerated from $$$1$$$ in the order they are added to the array. To make this problem more tricky we don't say your real parameters of the queries. Instead your are given $$$a'$$$, $$$l'$$$, $$$r'$$$, $$$k'$$$. To get $$$a$$$, $$$l$$$, $$$r$$$, $$$k$$$ on the $$$i$$$-th operation you need to perform the following: $$$a := (a' + lans) bmod(n + 1)$$$, $$$l := (l' + lans) bmod{i} + 1$$$, $$$r := (r' + lans) bmod{i} + 1$$$, if $$$l > r$$$ swap $$$l$$$ and $$$r$$$, $$$k := (k' + lans) bmod(n + 1)$$$, where $$$lans$$$ is the answer to the previous operation, initially $$$lans$$$ is equal to zero. $$$i$$$ is the id of the operation, operations are numbered from $$$1$$$. The $$$operatorname{mex}(S)$$$, where $$$S$$$ is a multiset of non-negative integers, is the smallest non-negative integer which does not appear in the set. For example, $$$operatorname{mex}({2, 2, 3}) = 0$$$ and $$$operatorname{mex} ({0, 1, 4, 1, 6}) = 2$$$. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$)xa0β€” the length of the array. The next $$$n$$$ lines contain the description of queries. Each of them $$$n$$$ lines contains four non-negative integers $$$a'$$$, $$$l'$$$, $$$r'$$$, $$$k'$$$ ($$$0, leq a', l', r', k' leq 10^9$$$), describing one operation. Note For the first example the decoded values of $$$a$$$, $$$l$$$, $$$r$$$, $$$k$$$ are the following: $$$a_1=0,l_1=1,r_1=1,k_1=1$$$ $$$a_2=1,l_2=1,r_2=2,k_2=0$$$ $$$a_3=0,l_3=1,r_3=3,k_3=1$$$ $$$a_4=1,l_4=1,r_4=4,k_4=2$$$ $$$a_5=2,l_5=1,r_5=5,k_5=3$$$ For the second example the decoded values of $$$a$$$, $$$l$$$, $$$r$$$, $$$k$$$ are the following: $$$a_1=2,l_1=1,r_1=1,k_1=2$$$ $$$a_2=2,l_2=1,r_2=2,k_2=1$$$ $$$a_3=0,l_3=1,r_3=3,k_3=0$$$ $$$a_4=0,l_4=2,r_4=2,k_4=3$$$ $$$a_5=0,l_5=3,r_5=4,k_5=0$$$
3,500
false
false
false
false
true
false
false
false
false
false
4,970
897B
Problem - 897B - 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 brute force *1300 No tag edit access β†’ Contest materials . Output Output single integerxa0β€” answer to the problem. Examples Input 2 100 Output 33 Input 5 30 Output 15 Note In the first example, the smallest zcy number is 11, and the second smallest zcy number is 22. In the second example, .
1,300
false
false
false
false
false
false
true
false
false
false
6,159
1954F
Let's say that two strings $$$a$$$ and $$$b$$$ are equal if you can get the string $$$b$$$ by cyclically shifting string $$$a$$$. For example, the strings 0100110 and 1100100 are equal, while 1010 and 1100 are not. You are given a binary string $$$s$$$ of length $$$n$$$. Its first $$$c$$$ characters are 1-s, and its last $$$n - c$$$ characters are 0-s. In one operation, you can replace one 0 with 1. Calculate the number of unique strings you can get using no more than $$$k$$$ operations. Since the answer may be too large, print it modulo $$$10^9 + 7$$$. Input The first and only line contains three integers $$$n$$$, $$$c$$$ and $$$k$$$ ($$$1 le n le 3000$$$; $$$1 le c le n$$$; $$$0 le k le n - c$$$)xa0β€” the length of string $$$s$$$, the length of prefix of 1-s and the maximum number of operations. Output Print the single integerxa0β€” the number of unique strings you can achieve performing no more than $$$k$$$ operations, modulo $$$10^9 + 7$$$. Note In the first test case, the only possible string is 1. In the second test case, the possible strings are: 100, 110, and 111. String 101 is equal to 110, so we don't count it. In the third test case, the possible strings are: 10000, 11000, 10100. String 10010 is equal to 10100, and 10001 is equal to 11000.
3,100
true
false
false
true
false
false
false
false
false
false
549
1206A
You are given an array $$$A$$$, consisting of $$$n$$$ positive integers $$$a_1, a_2, dots, a_n$$$, and an array $$$B$$$, consisting of $$$m$$$ positive integers $$$b_1, b_2, dots, b_m$$$. Choose some element $$$a$$$ of $$$A$$$ and some element $$$b$$$ of $$$B$$$ such that $$$a+b$$$ doesn't belong to $$$A$$$ and doesn't belong to $$$B$$$. For example, if $$$A = [2, 1, 7]$$$ and $$$B = [1, 3, 4]$$$, we can choose $$$1$$$ from $$$A$$$ and $$$4$$$ from $$$B$$$, as number $$$5 = 1 + 4$$$ doesn't belong to $$$A$$$ and doesn't belong to $$$B$$$. However, we can't choose $$$2$$$ from $$$A$$$ and $$$1$$$ from $$$B$$$, as $$$3 = 2 + 1$$$ belongs to $$$B$$$. It can be shown that such a pair exists. If there are multiple answers, print any. Choose and print any such two numbers. Input The first line contains one integer $$$n$$$ ($$$1le n le 100$$$)xa0β€” the number of elements of $$$A$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 200$$$)xa0β€” the elements of $$$A$$$. The third line contains one integer $$$m$$$ ($$$1le m le 100$$$)xa0β€” the number of elements of $$$B$$$. The fourth line contains $$$m$$$ different integers $$$b_1, b_2, dots, b_m$$$ ($$$1 le b_i le 200$$$)xa0β€” the elements of $$$B$$$. It can be shown that the answer always exists. Output Output two numbers $$$a$$$ and $$$b$$$ such that $$$a$$$ belongs to $$$A$$$, $$$b$$$ belongs to $$$B$$$, but $$$a+b$$$ doesn't belong to nor $$$A$$$ neither $$$B$$$. If there are multiple answers, print any. Note In the first example, we can choose $$$20$$$ from array $$$[20]$$$ and $$$20$$$ from array $$$[10, 20]$$$. Number $$$40 = 20 + 20$$$ doesn't belong to any of those arrays. However, it is possible to choose $$$10$$$ from the second array too. In the second example, we can choose $$$3$$$ from array $$$[3, 2, 2]$$$ and $$$1$$$ from array $$$[1, 5, 7, 7, 9]$$$. Number $$$4 = 3 + 1$$$ doesn't belong to any of those arrays. In the third example, we can choose $$$1$$$ from array $$$[1, 3, 5, 7]$$$ and $$$1$$$ from array $$$[7, 5, 3, 1]$$$. Number $$$2 = 1 + 1$$$ doesn't belong to any of those arrays.
800
true
false
false
false
false
false
false
false
true
false
4,663
1395A
Boboniu gives you $$$r$$$ red balls, $$$g$$$ green balls, $$$b$$$ blue balls, $$$w$$$ white balls. He allows you to do the following operation as many times as you want: Pick a red ball, a green ball, and a blue ball and then change their color to white. You should answer if it's possible to arrange all the balls into a palindrome after several (possibly zero) number of described operations. Input The first line contains one integer $$$T$$$ ($$$1le Tle 100$$$) denoting the number of test cases. For each of the next $$$T$$$ cases, the first line contains four integers $$$r$$$, $$$g$$$, $$$b$$$ and $$$w$$$ ($$$0le r,g,b,wle 10^9$$$). Output For each test case, print "Yes" if it's possible to arrange all the balls into a palindrome after doing several (possibly zero) number of described operations. Otherwise, print "No". Example Input 4 0 1 1 1 8 1 9 3 0 0 0 0 1000000000 1000000000 1000000000 1000000000 Note In the first test case, you're not able to do any operation and you can never arrange three balls of distinct colors into a palindrome. In the second test case, after doing one operation, changing $$$(8,1,9,3)$$$ to $$$(7,0,8,6)$$$, one of those possible palindromes may be "rrrwwwbbbbrbbbbwwwrrr". A palindrome is a word, phrase, or sequence that reads the same backwards as forwards. For example, "rggbwbggr", "b", "gg" are palindromes while "rgbb", "gbbgr" are not. Notice that an empty word, phrase, or sequence is palindrome.
1,000
true
false
false
false
false
false
true
false
false
false
3,696
938C
Let's denote a _m_-free matrix as a binary (that is, consisting of only 1's and 0's) matrix such that every square submatrix of size _m_u2009Γ—u2009_m_ of this matrix contains at least one zero. Consider the following problem: You are given two integers _n_ and _m_. You have to construct an _m_-free square matrix of size _n_u2009Γ—u2009_n_ such that the number of 1's in this matrix is maximum possible. Print the maximum possible number of 1's in such matrix. You don't have to solve this problem. Instead, you have to construct a few tests for it. You will be given _t_ numbers _x_1, _x_2, ..., _x__t_. For every , find two integers _n__i_ and _m__i_ (_n__i_u2009β‰₯u2009_m__i_) such that the answer for the aforementioned problem is exactly _x__i_ if we set _n_u2009=u2009_n__i_ and _m_u2009=u2009_m__i_. Input The first line contains one integer _t_ (1u2009≀u2009_t_u2009≀u2009100) β€” the number of tests you have to construct. Then _t_ lines follow, _i_-th line containing one integer _x__i_ (0u2009≀u2009_x__i_u2009≀u2009109). Note that in hacks you have to set _t_u2009=u20091. Output For each test you have to construct, output two positive numbers _n__i_ and _m__i_ (1u2009≀u2009_m__i_u2009≀u2009_n__i_u2009≀u2009109) such that the maximum number of 1's in a _m__i_-free _n__i_u2009Γ—u2009_n__i_ matrix is exactly _x__i_. If there are multiple solutions, you may output any of them; and if this is impossible to construct a test, output a single integer u2009-u20091.
1,700
false
false
false
false
false
true
true
true
false
false
5,949
1375I
A cubic lattice $$$L$$$ in $$$3$$$-dimensional euclidean space is a set of points defined in the following way: $$$$$$L={u cdot vec r_1 + v cdot vec r_2 + w cdot vec r_3}_{u, v, w in mathbb Z}$$$$$$ Where $$$vec r_1, vec r_2, vec r_3 in mathbb{Z}^3$$$ are some integer vectors such that: $$$vec r_1$$$, $$$vec r_2$$$ and $$$vec r_3$$$ are pairwise orthogonal: $$$$$$vec r_1 cdot vec r_2 = vec r_1 cdot vec r_3 = vec r_2 cdot vec r_3 = 0$$$$$$ Where $$$vec a cdot vec b$$$ is a dot product of vectors $$$vec a$$$ and $$$vec b$$$. $$$vec r_1$$$, $$$vec r_2$$$ and $$$vec r_3$$$ all have the same length: $$$$$$vec r_1 = vec r_2 = vec r_3 = r$$$$$$ You're given a set $$$A={vec a_1, vec a_2, dots, vec a_n}$$$ of integer points, $$$i$$$-th point has coordinates $$$a_i=(x_i;y_i;z_i)$$$. Let $$$g_i=gcd(x_i,y_i,z_i)$$$. It is guaranteed that $$$gcd(g_1,g_2,dots,g_n)=1$$$. You have to find a cubic lattice $$$L$$$ such that $$$A subset L$$$ and $$$r$$$ is the maximum possible. Input First line contains single integer $$$n$$$ ($$$1 leq n leq 10^4$$$)xa0β€” the number of points in $$$A$$$. The $$$i$$$-th of the following $$$n$$$ lines contains integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$0 < x_i^2 + y_i^2 + z_i^2 leq 10^{16}$$$)xa0β€” coordinates of the $$$i$$$-th point. It is guaranteed that $$$gcd(g_1,g_2,dots,g_n)=1$$$ where $$$g_i=gcd(x_i,y_i,z_i)$$$. Output In first line output a single integer $$$r^2$$$, the square of maximum possible $$$r$$$. In following $$$3$$$ lines output coordinates of vectors $$$vec r_1$$$, $$$vec r_2$$$ and $$$vec r_3$$$ respectively. If there are multiple possible answers, output any. Examples Output 1 1 0 0 0 1 0 0 0 1 Output 9 2 -2 1 1 2 2 -2 -1 2 Output 9 -1 2 2 2 -1 2 2 2 -1
3,500
true
false
false
false
false
false
false
false
false
false
3,779
784F
Problem - 784F - Codeforces =============== xa0 ]( "51289") β€” the size of the array. The following _n_ numbers are the elements of the array (1u2009≀u2009_a__i_u2009≀u2009100). Output Output space-separated elements of the sorted array. Example Input 3 3 1 2 Output 1 2 3 Note Remember, this is a very important feature, and you have to make sure the customers appreciate it!
1,900
false
false
true
false
false
false
false
false
false
false
6,635
1534G
Annie has gotten bored of winning every coding contest and farming unlimited rating. Today, she is going to farm potatoes instead. Annie's garden is an infinite 2D plane. She has $$$n$$$ potatoes to plant, and the $$$i$$$-th potato must be planted at $$$(x_i,y_i)$$$. Starting at the point $$$(0, 0)$$$, Annie begins walking, in one step she can travel one unit right or up (increasing her $$$x$$$ or $$$y$$$ coordinate by $$$1$$$ respectively). At any point $$$(X,Y)$$$ during her walk she can plant some potatoes at arbitrary points using her potato gun, consuming $$$max(X-x,Y-y)$$$ units of energy in order to plant a potato at $$$(x,y)$$$. Find the minimum total energy required to plant every potato. Note that Annie may plant any number of potatoes from any point. Input The first line contains the integer $$$n$$$ ($$$1 le n le 800,000$$$). The next $$$n$$$ lines contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 le x_i,y_i le 10^9$$$), representing the location of the $$$i$$$-th potato. It is possible that some potatoes should be planted in the same location. Output Print the minimum total energy to plant all potatoes. Examples Input 10 5 1 4 0 9 6 0 2 10 1 9 10 3 10 0 10 8 9 1 5 Input 10 1 1 2 2 2 0 4 2 4 0 2 0 0 2 4 0 4 2 5 1 Note In example $$$1$$$, Annie can travel to each spot directly and plant a potato with no energy required. In example $$$2$$$, moving to $$$(1,0)$$$, Annie plants the second potato using $$$1$$$ energy. Next, she travels to $$$(1,1)$$$ and plants the first potato with $$$0$$$ energy.
3,300
false
false
false
true
true
false
false
false
true
false
2,976
1918C
You are given integers $$$a$$$, $$$b$$$, $$$r$$$. Find the smallest value of $$$({a oplus x}) - ({b oplus x})$$$ among all $$$0 leq x leq r$$$. $$$oplus$$$ is the operation of xa0β€” the number of test cases. Each test case contains integers $$$a$$$, $$$b$$$, $$$r$$$ ($$$0 le a, b, r le 10^{18}$$$). Output For each test case, output a single numberxa0β€” the smallest possible value. Example Input 10 4 6 0 0 3 2 9 6 10 92 256 23 165 839 201 1 14 5 2 7 2 96549 34359 13851 853686404475946 283666553522252166 127929199446003072 735268590557942972 916721749674600979 895150420120690183 Output 2 1 1 164 542 5 3 37102 27934920819538516 104449824168870225 Note In the first test, when $$$r = 0$$$, then $$$x$$$ is definitely equal to $$$0$$$, so the answer is $$${4 oplus 0} - {6 oplus 0} = 4 - 6 = 2$$$. In the second test: When $$$x = 0$$$, $$${0 oplus 0} - {3 oplus 0} = 0 - 3 = 3$$$. When $$$x = 1$$$, $$${0 oplus 1} - {3 oplus 1} = 1 - 2 = 1$$$. When $$$x = 2$$$, $$${0 oplus 2} - {3 oplus 2} = 2 - 1 = 1$$$. Therefore, the answer is $$$1$$$. In the third test, the minimum is achieved when $$$x = 1$$$.
1,400
true
true
true
false
false
false
false
false
false
false
786
1912H
A revolutionary new transport system is currently operating in Byteland. This system requires neither roads nor sophisticated mechanisms, only giant catapults. The system works as follows. There are $$$n$$$ cities in Byteland. In every city there is a catapult, right in the city center. People who want to travel are put in a special capsule, and a catapult throws this capsule to the center of some other city. Every catapult is powerful enough to throw the capsule to any other city, with any number of passengers inside the capsule. The only problem is that it takes a long time to charge the catapult, so it is only possible to use it once a day. The passenger may need to use the catapults multiple times. For example, if the passenger wants to travel from city A to city B, they can first use one catapult to move from A to C, and then transfer to another catapult to move from C to B. Today there are $$$m$$$ passengers. Passenger $$$i$$$ wants to travel from city $$$a_i$$$ to city $$$b_i$$$. Your task is to find the way to deliver all the passengers to their destinations in a single day, using the minimal possible number of catapults, or say that it is impossible. Input The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n leq 1000$$$, $$$0 leq m leq 10^5$$$)xa0β€” the number of cities and the number of passengers. The next $$$m$$$ lines contain pairs of numbers $$$a_i$$$ and $$$b_i$$$ ($$$1 leq a_i, b_i leq n$$$, $$$a_i eq b_i$$$). Output In the first line print the number $$$k$$$xa0β€” the minimal number of catapults you need to use. In the next $$$k$$$ lines, print descriptions of each catapult launch, in the order they need to be performed. Each description should consist of two integers $$$c_i$$$, $$$d_i$$$, the index of the city to launch from, and the index of destination city. Note that you don't need to print what passengers should be put into the capsule on each launch, but it should be possible for each passenger to reach their destination city using the plan you provide. If it is impossible to deliver all passengers, print the single number $$$-1$$$. Examples Input 5 6 1 3 1 2 2 3 4 2 1 5 5 1 Output 5 5 1 1 2 4 2 2 3 3 5 Input 3 6 1 2 1 3 2 1 2 3 3 1 3 2
2,400
false
false
false
false
false
false
false
false
false
true
830
963A
You are given two integers $$$a$$$ and $$$b$$$. Moreover, you are given a sequence $$$s_0, s_1, dots, s_{n}$$$. All values in $$$s$$$ are integers $$$1$$$ or $$$-1$$$. It's known that sequence is $$$k$$$-periodic and $$$k$$$ divides $$$n+1$$$. In other words, for each $$$k leq i leq n$$$ it's satisfied that $$$s_{i} = s_{i - k}$$$. Find out the non-negative remainder of division of $$$sum limits_{i=0}^{n} s_{i} a^{n - i} b^{i}$$$ by $$$10^{9} + 9$$$. Note that the modulo is unusual! Input The first line contains four integers $$$n, a, b$$$ and $$$k$$$ $$$(1 leq n leq 10^{9}, 1 leq a, b leq 10^{9}, 1 leq k leq 10^{5})$$$. The second line contains a sequence of length $$$k$$$ consisting of characters '+' and '-'. If the $$$i$$$-th character (0-indexed) is '+', then $$$s_{i} = 1$$$, otherwise $$$s_{i} = -1$$$. Note that only the first $$$k$$$ members of the sequence are given, the rest can be obtained using the periodicity property. Output Output a single integerxa0β€” value of given expression modulo $$$10^{9} + 9$$$. Note In the first example: $$$(sum limits_{i=0}^{n} s_{i} a^{n - i} b^{i})$$$ = $$$2^{2} 3^{0} - 2^{1} 3^{1} + 2^{0} 3^{2}$$$ = 7 In the second example: $$$(sum limits_{i=0}^{n} s_{i} a^{n - i} b^{i}) = -1^{4} 5^{0} - 1^{3} 5^{1} - 1^{2} 5^{2} - 1^{1} 5^{3} - 1^{0} 5^{4} = -781 equiv 999999228 pmod{10^{9} + 9}$$$.
1,800
true
false
false
false
false
false
false
false
false
false
5,849
260E
A country called Flatland is an infinite two-dimensional plane. Flatland has _n_ cities, each of them is a point on the plane. Flatland is ruled by king Circle IV. Circle IV has 9 sons. He wants to give each of his sons part of Flatland to rule. For that, he wants to draw four distinct straight lines, such that two of them are parallel to the _Ox_ axis, and two others are parallel to the _Oy_ axis. At that, no straight line can go through any city. Thus, Flatland will be divided into 9 parts, and each son will be given exactly one of these parts. Circle IV thought a little, evaluated his sons' obedience and decided that the _i_-th son should get the part of Flatland that has exactly _a__i_ cities. Help Circle find such four straight lines that if we divide Flatland into 9 parts by these lines, the resulting parts can be given to the sons so that son number _i_ got the part of Flatland which contains _a__i_ cities. Input The first line contains integer _n_ (9u2009≀u2009_n_u2009≀u2009105) β€” the number of cities in Flatland. Next _n_ lines each contain two space-separated integers: _x__i_,u2009_y__i_ (u2009-u2009109u2009≀u2009_x__i_,u2009_y__i_u2009≀u2009109) β€” the coordinates of the _i_-th city. No two cities are located at the same point. The last line contains nine space-separated integers: . Output If there is no solution, print a single integer -1. Otherwise, print in the first line two distinct real space-separated numbers: _x_1,u2009_x_2 β€” the abscissas of the straight lines that are parallel to the _Oy_ axis. And in the second line print two distinct real space-separated numbers: _y_1,u2009_y_2 β€” the ordinates of the straight lines, parallel to the _Ox_. If there are multiple solutions, print any of them. When the answer is being checked, a city is considered to lie on a straight line, if the distance between the city and the line doesn't exceed 10u2009-u20096. Two straight lines are considered the same if the distance between them doesn't exceed 10u2009-u20096. Examples Input 9 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 1 1 1 1 1 1 1 1 1 Output 1.5000000000 2.5000000000 1.5000000000 2.5000000000 Input 15 4 4 -1 -3 1 5 3 -4 -4 4 -1 1 3 -3 -4 -5 -3 3 3 2 4 1 -4 2 -2 -5 -3 4 -1 4 2 1 2 1 2 1 3 2 1 Output -3.5000000000 2.0000000000 3.5000000000 -1.0000000000 Input 10 -2 10 6 0 -16 -6 -4 13 -4 -2 -17 -10 9 15 18 16 -5 2 10 -5 2 1 1 1 1 1 1 1 1 Note The solution for the first sample test is shown below: The solution for the second sample test is shown below: There is no solution for the third sample test.
2,500
false
false
false
false
true
false
true
true
false
false
8,791
914A
Problem - 914A - 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 brute force implementation math *900 No tag edit access β†’ Contest materials ") xa0β€” the number of elements in the array. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (u2009-u2009106u2009≀u2009_a__i_u2009≀u2009106)xa0β€” the elements of the array. It is guaranteed that at least one element of the array is not a perfect square. Output Print the largest number in the array which is not a perfect square. It is guaranteed that an answer always exists. Examples Input 2 4 2 Output 2 Input 8 1 2 4 8 16 32 64 576 Output 32 Note In the first sample case, 4 is a perfect square, so the largest number in the array that is not a perfect square is 2.
900
true
false
true
false
false
false
true
false
false
false
6,083
603B
Problem - 603B - 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 combinatorics dfs and similar dsu math number theory *1800 No tag edit access β†’ Contest materials under Farmer Ivan. During his Mathematics of Olympiads (MoO) class, Kevin was confronted with a weird functional equation and needs your help. For two fixed integers _k_ and _p_, where _p_ is an odd prime number, the functional equation states that for some function . (This equation should hold for any integer _x_ in the range 0 to _p_u2009-u20091, inclusive.) It turns out that _f_ can actually be many different functions. Instead of finding a solution, Kevin wants you to count the number of distinct functions _f_ that satisfy this equation. Since the answer may be very large, you should print your result modulo 109u2009+u20097. Input The input consists of two space-separated integers _p_ and _k_ (3u2009≀u2009_p_u2009≀u20091u2009000u2009000, 0u2009≀u2009_k_u2009≀u2009_p_u2009-u20091) on a single line. It is guaranteed that _p_ is an odd prime number. Output Print a single integer, the number of distinct functions _f_ modulo 109u2009+u20097. Examples Input 3 2 Output 3 Input 5 4 Output 25 Note In the first sample, _p_u2009=u20093 and _k_u2009=u20092. The following functions work: 1. _f_(0)u2009=u20090, _f_(1)u2009=u20091, _f_(2)u2009=u20092. 2. _f_(0)u2009=u20090, _f_(1)u2009=u20092, _f_(2)u2009=u20091. 3. _f_(0)u2009=u2009_f_(1)u2009=u2009_f_(2)u2009=u20090.
1,800
true
false
false
false
false
false
false
false
false
false
7,432
1991G
You are given a grid consisting of $$$n$$$ rows and $$$m$$$ columns, where each cell is initially white. Additionally, you are given an integer $$$k$$$, where $$$1 le k le min(n, m)$$$. You will process $$$q$$$ operations of two types: $$$mathtt{H}$$$ (horizontal operation)xa0β€” You choose a $$$1 imes k$$$ rectangle completely within the grid, where all cells in this rectangle are white. Then, all cells in this rectangle are changed to black. $$$mathtt{V}$$$ (vertical operation)xa0β€” You choose a $$$k imes 1$$$ rectangle completely within the grid, where all cells in this rectangle are white. Then, all cells in this rectangle are changed to black. After each operation, if any rows or columns become completely black, all cells in these rows and columns are simultaneously reset to white. Specifically, if all cells in the row and column a cell is contained in become black, all cells in both the row and column will be reset to white. Choose the rectangles in a way that you can perform all given operations, or determine that it is impossible. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β€” the number of test cases. The description of test cases follows. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$k$$$, and $$$q$$$ ($$$1 le n, m le 100$$$, $$$1 le k le min(n, m)$$$, $$$1 le q le 1000$$$)xa0β€” the number of rows and columns in the grid, the size of the operation rectangle, and the number of operations, respectively. The second line of each test case contains a string $$$s$$$ of length $$$q$$$, consisting only of characters $$$mathtt{H}$$$ and $$$mathtt{V}$$$xa0β€” the sequence of operation types. It is guaranteed that the sum of $$$q$$$ over all test cases does not exceed $$$1000$$$. Output For each test case, output a single integer $$$-1$$$ if it is impossible to perform all the operations. Otherwise, output $$$q$$$ lines. Each line contains two integers $$$i$$$, $$$j$$$ ($$$1 le i le n$$$, $$$1 le j le m$$$)xa0β€” the coordinates of the top-left cell of the operation rectangle. If there are multiple solutions, output any of them. Note Illustration of example. The first operation is horizontal. The operation rectangle starts at $$$(1,1)$$$ and is a $$$1 imes 3$$$ rectangle. After the operation, cells $$$(1,1)$$$, $$$(1,2)$$$, and $$$(1,3)$$$ become black. The second operation is vertical. The operation rectangle starts at $$$(2,1)$$$ and is a $$$3 imes 1$$$ rectangle. After the operation, cells $$$(2,1)$$$, $$$(3,1)$$$, and $$$(4,1)$$$ become black. At this point, the first column becomes completely black, so all cells in the first column are reset to white. The third operation is vertical. The operation rectangle starts at $$$(1,1)$$$ and is a $$$3 imes 1$$$ rectangle. After the operation, cells $$$(1,1)$$$, $$$(2,1)$$$, and $$$(3,1)$$$ become black. The fourth operation is horizontal. The operation rectangle starts at $$$(2,3)$$$ and is a $$$1 imes 3$$$ rectangle. After the operation, cells $$$(2,3)$$$, $$$(2,4)$$$, and $$$(2,5)$$$ become black. The fifth operation is horizontal. The operation rectangle starts at $$$(3,3)$$$ and is a $$$1 imes 3$$$ rectangle. After the operation, cells $$$(3,3)$$$, $$$(3,4)$$$, and $$$(3,5)$$$ become black. The sixth operation is vertical. The operation rectangle starts at $$$(2,2)$$$ and is a $$$3 imes 1$$$ rectangle. After the operation, cells $$$(2,2)$$$, $$$(3,2)$$$, and $$$(4,2)$$$ become black. At this point, two rows and one column become completely black, so all cells in these rows and the column are reset to white.
2,700
false
true
true
false
false
true
false
false
false
false
322
1335E1
The only difference between easy and hard versions is constraints. You are given a sequence $$$a$$$ consisting of $$$n$$$ positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $$$a$$$ and $$$b$$$, $$$a$$$ can be equal $$$b$$$) and is as follows: $$$[underbrace{a, a, dots, a}_{x}, underbrace{b, b, dots, b}_{y}, underbrace{a, a, dots, a}_{x}]$$$. There $$$x, y$$$ are integers greater than or equal to $$$0$$$. For example, sequences $$$[]$$$, $$$[2]$$$, $$$[1, 1]$$$, $$$[1, 2, 1]$$$, $$$[1, 2, 2, 1]$$$ and $$$[1, 1, 2, 1, 1]$$$ are three block palindromes but $$$[1, 2, 3, 2, 1]$$$, $$$[1, 2, 1, 2, 1]$$$ and $$$[1, 2]$$$ are not. Your task is to choose the maximum by length subsequence of $$$a$$$ that is a three blocks palindrome. You have to answer $$$t$$$ independent test cases. Recall that the sequence $$$t$$$ is a a subsequence of the sequence $$$s$$$ if $$$t$$$ can be derived from $$$s$$$ by removing zero or more elements without changing the order of the remaining elements. For example, if $$$s=[1, 2, 1, 3, 1, 2, 1]$$$, then possible subsequences are: $$$[1, 1, 1, 1]$$$, $$$[3]$$$ and $$$[1, 2, 1, 3, 1, 2, 1]$$$, but not $$$[3, 2, 3]$$$ and $$$[1, 1, 1, 1, 2]$$$. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 2000$$$) β€” the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 le n le 2000$$$) β€” the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 26$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. Note that the maximum value of $$$a_i$$$ can be up to $$$26$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$ ($$$sum n le 2000$$$).
1,700
false
false
false
true
true
false
true
true
false
false
4,026
1303G
We define the sum of prefix sums of an array $$$[s_1, s_2, dots, s_k]$$$ as $$$s_1 + (s_1 + s_2) + (s_1 + s_2 + s_3) + dots + (s_1 + s_2 + dots + s_k)$$$. You are given a tree consisting of $$$n$$$ vertices. Each vertex $$$i$$$ has an integer $$$a_i$$$ written on it. We define the value of the simple path from vertex $$$u$$$ to vertex $$$v$$$ as follows: consider all vertices appearing on the path from $$$u$$$ to $$$v$$$, write down all the numbers written on these vertices in the order they appear on the path, and compute the sum of prefix sums of the resulting sequence. Your task is to calculate the maximum value over all paths in the tree. Input The first line contains one integer $$$n$$$ ($$$2 le n le 150000$$$) β€” the number of vertices in the tree. Then $$$n - 1$$$ lines follow, representing the edges of the tree. Each line contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i, v_i le n$$$, $$$u_i e v_i$$$), denoting an edge between vertices $$$u_i$$$ and $$$v_i$$$. It is guaranteed that these edges form a tree. The last line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 le a_i le 10^6$$$). Output Print one integer β€” the maximum value over all paths in the tree. Example Input 4 4 2 3 2 4 1 1 3 3 7 Note The best path in the first example is from vertex $$$3$$$ to vertex $$$1$$$. It gives the sequence $$$[3, 3, 7, 1]$$$, and the sum of prefix sums is $$$36$$$.
2,700
false
false
false
false
true
false
false
false
false
false
4,172
1821A
An integer template is a string consisting of digits and/or question marks. A positive (strictly greater than $$$0$$$) integer matches the integer template if it is possible to replace every question mark in the template with a digit in such a way that we get the decimal representation of that integer without any leading zeroes. For example: $$$42$$$ matches 4?; $$$1337$$$ matches ????; $$$1337$$$ matches 1?3?; $$$1337$$$ matches 1337; $$$3$$$ does not match ??; $$$8$$$ does not match ???8; $$$1337$$$ does not match 1?7. You are given an integer template consisting of at most $$$5$$$ characters. Calculate the number of positive (strictly greater than $$$0$$$) integers that match it. Input The first line contains one integer $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$) β€” the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 le s le 5$$$) consisting of digits and/or question marks β€” the integer template for the corresponding test case. Output For each test case, print one integer β€” the number of positive (strictly greater than $$$0$$$) integers that match the template. Example Input 8 ?? ? 0 9 03 1??7 ?5? 9??99 Output 90 9 0 1 0 100 90 100
800
true
false
false
false
false
false
false
false
false
false
1,357
1986B
You are given a matrix of size $$$n imes m$$$, where the rows are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns are numbered from $$$1$$$ to $$$m$$$ from left to right. The element at the intersection of the $$$i$$$-th row and the $$$j$$$-th column is denoted by $$$a_{ij}$$$. Consider the algorithm for stabilizing matrix $$$a$$$: 1. Find the cell $$$(i, j)$$$ such that its value is strictly greater than the values of all its neighboring cells. If there is no such cell, terminate the algorithm. If there are multiple such cells, choose the cell with the smallest value of $$$i$$$, and if there are still multiple cells, choose the one with the smallest value of $$$j$$$. 2. Set $$$a_{ij} = a_{ij} - 1$$$. 3. Go to step $$$1$$$. In this problem, cells $$$(a, b)$$$ and $$$(c, d)$$$ are considered neighbors if they share a common side, i.e., $$$a - c + b - d = 1$$$. Your task is to output the matrix $$$a$$$ after the stabilization algorithm has been executed. It can be shown that this algorithm cannot run for an infinite number of iterations. Input Each test consists of multiple sets of input data. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0β€” the number of sets of input data. This is followed by their description. The first line of each set of input data contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 100, n cdot m > 1$$$)xa0β€” the number of rows and columns of matrix $$$a$$$. The next $$$n$$$ lines describe the corresponding rows of the matrix. The $$$i$$$-th line contains $$$m$$$ integers $$$a_{i1}, a_{i2}, ldots, a_{im}$$$ ($$$1 leq a_{ij} leq 10^9$$$). It is guaranteed that the sum of $$$n cdot m$$$ over all sets of input data does not exceed $$$2 cdot 10^5$$$. Output For each set of input data, output $$$n$$$ lines with $$$m$$$ numbers in each linexa0β€” the values of the cells of matrix $$$a$$$ after the stabilization algorithm. Example Input 6 1 2 3 1 2 1 1 1 2 2 1 2 3 4 2 3 7 4 5 1 8 10 5 4 92 74 31 74 74 92 17 7 31 17 92 3 74 7 3 92 7 31 1 1 3 3 1000000000 1 1000000000 1 1000000000 1 1000000000 1 1000000000 Output 1 1 1 1 1 2 3 3 4 4 5 1 8 8 74 74 31 31 74 74 17 7 31 17 17 3 31 7 3 3 7 7 1 1 1 1 1 1 1 1 1 1 1 Note In the first set of input data, the algorithm will select the cell $$$(1, 1)$$$ twice in a row and then terminate. In the second set of input data, there is no cell whose value is strictly greater than the values of all neighboring cells. In the third set of input data, the algorithm will select the cell $$$(2, 2)$$$ and then terminate. In the fourth set of input data, the algorithm will select the cell $$$(1, 1)$$$ three times and then the cell $$$(2, 3)$$$ twice.
1,000
false
true
false
false
true
false
true
false
true
false
364
515B
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan. There are _n_ boys and _m_ girls among his friends. Let's number them from 0 to _n_u2009-u20091 and 0 to _m_u2009-u20091 separately. In _i_-th day, Drazil invites -th boy and -th girl to have dinner together (as Drazil is programmer, _i_ starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if he/she was happy originally), he stays happy forever. Drazil wants to know whether he can use this plan to make all his friends become happy at some moment. Input The first line contains two integer _n_ and _m_ (1u2009≀u2009_n_,u2009_m_u2009≀u2009100). The second line contains integer _b_ (0u2009≀u2009_b_u2009≀u2009_n_), denoting the number of happy boys among friends of Drazil, and then follow _b_ distinct integers _x_1,u2009_x_2,u2009...,u2009_x__b_ (0u2009≀u2009_x__i_u2009<u2009_n_), denoting the list of indices of happy boys. The third line conatins integer _g_ (0u2009≀u2009_g_u2009≀u2009_m_), denoting the number of happy girls among friends of Drazil, and then follow _g_ distinct integers _y_1,u2009_y_2,u2009... ,u2009_y__g_ (0u2009≀u2009_y__j_u2009<u2009_m_), denoting the list of indices of happy girls. It is guaranteed that there is at least one person that is unhappy among his friends. Output If Drazil can make all his friends become happy by this plan, print "Yes". Otherwise, print "No". Note By we define the remainder of integer division of _i_ by _k_. In first sample case: On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day. On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day. On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day. On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy. On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
1,300
false
false
false
false
false
false
true
false
false
false
7,774
335F
Problem - 335F - Codeforces =============== xa0 , the number of pies you wish to acquire. Following this is a line with _n_ integers, each indicating the cost of a pie. All costs are positive integers not exceeding 109. Output Print the minimum cost to acquire all the pies. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 6 3 4 5 3 4 5 Output 14 Input 5 5 5 5 5 5 Output 25 Input 4 309999 6000 2080 2080 Output 314159 Note In the first test case you can pay for a pie with cost 5 and get a pie with cost 4 for free, then pay for a pie with cost 5 and get a pie with cost 3 for free, then pay for a pie with cost 4 and get a pie with cost 3 for free. In the second test case you have to pay full price for every pie.
3,000
false
true
false
true
false
false
false
false
false
false
8,493
424B
The administration of the Tomsk Region firmly believes that it's time to become a megacity (that is, get population of one million). Instead of improving the demographic situation, they decided to achieve its goal by expanding the boundaries of the city. The city of Tomsk can be represented as point on the plane with coordinates (0; 0). The city is surrounded with _n_ other locations, the _i_-th one has coordinates (_x__i_, _y__i_) with the population of _k__i_ people. You can widen the city boundaries to a circle of radius _r_. In such case all locations inside the circle and on its border are included into the city. Your goal is to write a program that will determine the minimum radius _r_, to which is necessary to expand the boundaries of Tomsk, so that it becomes a megacity. Input The first line of the input contains two integers _n_ and _s_ (1u2009≀u2009_n_u2009≀u2009103; 1u2009≀u2009_s_u2009<u2009106) β€” the number of locatons around Tomsk city and the population of the city. Then _n_ lines follow. The _i_-th line contains three integers β€” the _x__i_ and _y__i_ coordinate values of the _i_-th location and the number _k__i_ of people in it (1u2009≀u2009_k__i_u2009<u2009106). Each coordinate is an integer and doesn't exceed 104 in its absolute value. It is guaranteed that no two locations are at the same point and no location is at point (0;xa00). Output In the output, print "-1" (without the quotes), if Tomsk won't be able to become a megacity. Otherwise, in the first line print a single real number β€” the minimum radius of the circle that the city needs to expand to in order to become a megacity. The answer is considered correct if the absolute or relative error don't exceed 10u2009-u20096. Examples Input 4 999998 1 1 1 2 2 1 3 3 1 2 -2 1 Input 4 999998 1 1 2 2 2 1 3 3 1 2 -2 1
1,200
false
true
true
false
false
false
false
true
true
false
8,142
578D
Problem - 578D - Codeforces =============== xa0 ]( "Codeforces Round #320 between _S_ and _T_ is _n_u2009-u20091. Recall that LCS of two strings _S_ and _T_ is the longest string _C_ such that _C_ both in _S_ and _T_ as a subsequence. Input The first line contains two numbers _n_ and _m_ denoting the length of string _S_ and number of first English lowercase characters forming the character set for strings (1u2009≀u2009_n_u2009≀u2009100u2009000, 2u2009≀u2009_m_u2009≀u200926). The second line contains string _S_. Output Print the only line containing the answer. Examples Input 3 3 aaa Output 6 Input 3 3 aab Output 11 Input 1 2 a Output 1 Input 10 9 abacadefgh Output 789 Note For the first sample, the 6 possible strings _T_ are: aab, aac, aba, aca, baa, caa. For the second sample, the 11 possible strings _T_ are: aaa, aac, aba, abb, abc, aca, acb, baa, bab, caa, cab. For the third sample, the only possible string _T_ is b.
2,700
false
true
false
true
false
false
false
false
false
false
7,533
100J
Problem - 100J - Codeforces =============== xa0 , number of intervals. The following _n_ lines contain a interval description each. Each interval is described by two numbers _s__i_,u2009_e__i_ which are the start and end points of it (u2009-u2009105u2009<u2009_s__i_,u2009_e__i_u2009<u2009105, _s__i_u2009≀u2009_e__i_). See samples for clarity. A square bracket stands for including of the corresponding endpoint, while a round bracket stands for excluding. Output Write a single integer _k_ β€” the minimum number of colors needed for a nice coloring. Examples Input 2
2,400
true
true
false
false
false
false
false
false
false
false
9,480
1732D2
This is the hard version of the problem. The only difference is that in this version there are remove queries. Initially you have a set containing one element β€” $$$0$$$. You need to handle $$$q$$$ queries of the following types: + $$$x$$$ β€” add the integer $$$x$$$ to the set. It is guaranteed that this integer is not contained in the set; - $$$x$$$ β€” remove the integer $$$x$$$ from the set. It is guaranteed that this integer is contained in the set; ? $$$k$$$ β€” find the $$$k ext{-mex}$$$ of the set. In our problem, we define the $$$k ext{-mex}$$$ of a set of integers as the smallest non-negative integer $$$x$$$ that is divisible by $$$k$$$ and which is not contained in the set. Input The first line contains an integer $$$q$$$ ($$$1 leq q leq 2 cdot 10^5$$$) β€” the number of queries. The following $$$q$$$ lines describe the queries. An addition query of integer $$$x$$$ is given in the format + $$$x$$$ ($$$1 leq x leq 10^{18}$$$). It is guaranteed that $$$x$$$ is not contained in the set. A remove query of integer $$$x$$$ is given in the format - $$$x$$$ ($$$1 leq x leq 10^{18}$$$). It is guaranteed that $$$x$$$ is contained in the set. A search query of $$$k ext{-mex}$$$ is given in the format ? $$$k$$$ ($$$1 leq k leq 10^{18}$$$). It is guaranteed that there is at least one query of type ?. Output For each query of type ? output a single integer β€” the $$$k ext{-mex}$$$ of the set. Examples Input 18 + 1 + 2 ? 1 + 4 ? 2 + 6 ? 3 + 7 + 8 ? 1 ? 2 + 5 ? 1 + 1000000000000000000 ? 1000000000000000000 - 4 ? 1 ? 2 Output 3 6 3 3 10 3 2000000000000000000 3 4 Input 10 + 100 ? 100 + 200 ? 100 - 100 ? 100 + 50 ? 50 - 50 ? 50 Note In the first example: After the first and second queries, the set will contain elements $$${0, 1, 2}$$$. The smallest non-negative number that is divisible by $$$1$$$ and is not in the set is $$$3$$$. After the fourth query, the set will contain the elements $$${0, 1, 2, 4}$$$. The smallest non-negative number that is divisible by $$$2$$$ and is not in the set is $$$6$$$. In the second example: Initially, the set contains only the element $$${0}$$$. After adding an integer $$$100$$$ the set contains elements $$${0, 100}$$$. $$$100 ext{-mex}$$$ of the set is $$$200$$$. After adding an integer $$$200$$$ the set contains elements $$${0, 100, 200}$$$. $$$100 ext{-mex}$$$ of the set $$$300$$$. After removing an integer $$$100$$$ the set contains elements $$${0, 200}$$$. $$$100 ext{-mex}$$$ of the set is $$$100$$$. After adding an integer $$$50$$$ the set contains elements $$${0, 50, 200}$$$. $$$50 ext{-mex}$$$ of the set is $$$100$$$. After removing an integer $$$50$$$ the set contains elements $$${0, 200}$$$. $$$100 ext{-mex}$$$ of the set is $$$50$$$.
2,400
false
false
false
false
true
false
true
false
false
false
1,894
1019C
Sergey just turned five years old! When he was one year old, his parents gave him a number; when he was two years old, his parents gave him an array of integers. On his third birthday he received a string. When he was four, his mother woke him up in a quiet voice, wished him to be a good boy and gave him a rooted tree. Today he celebrates his birthday again! He found a directed graph without loops as a present from his parents. Since Sergey is a very curious boy, he immediately came up with a thing to do. He decided to find a set $$$Q$$$ of vertices in this graph, such that no two vertices $$$x, y in Q$$$ are connected by an edge, and it is possible to reach any vertex $$$z otin Q$$$ from some vertex of $$$Q$$$ in no more than two moves. After a little thought, Sergey was able to solve this task. Can you solve it too? A vertex $$$y$$$ is reachable from a vertex $$$x$$$ in at most two moves if either there is a directed edge $$$(x,y)$$$, or there exist two directed edges $$$(x,z)$$$ and $$$(z, y)$$$ for some vertex $$$z$$$. Input The first line of input contains two positive integers $$$n$$$ and $$$m$$$ ($$$1 le n le 1,000,000$$$, $$$1 le m le 1,000,000$$$)xa0β€” the number of vertices and the number of edges in the directed graph. Each of the following $$$m$$$ lines describes a corresponding edge. Each one contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i, b_i le n$$$, $$$a_i e b_i$$$)xa0β€” the beginning and the end of the $$$i$$$-th edge. The graph may contain multiple edges between the same pair of vertices. Output First print the number $$$k$$$xa0β€” the number of selected vertices. Then print $$$k$$$ distinct integersxa0β€” the indices of the selected vertices. If multiple answers exist you can output any of them. In particular, you don't have to minimize the number of vertices in the set. It is guaranteed, that there is always at least one valid set. Note In the first sample, the vertices $$$1, 3, 4, 5$$$ are not connected. The vertex $$$2$$$ is reachable from vertex $$$1$$$ by one edge. In the second sample, it is possible to reach the vertex $$$1$$$ in one move and the vertex $$$2$$$ in two moves. The following pictures illustrate sample tests and their answers.
3,000
false
false
false
false
false
true
false
false
false
true
5,595
402D
Problem - 402D - 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 greedy math number theory *1800 No tag edit access β†’ Contest materials is determined as follows: _f_(1)u2009=u20090; Let's assume that _p_ is the minimum prime divisor of _s_. If _p_ is a good prime, then , otherwise . You are allowed to perform an arbitrary (probably zero) number of operations to improve array _a_. The operation of improvement is the following sequence of actions: Choose some number _r_ (1u2009≀u2009_r_u2009≀u2009_n_) and calculate the value _g_ = GCD(_a_
1,800
true
true
false
true
false
false
false
false
false
false
8,214
1354E
You are given an undirected graph without self-loops or multiple edges which consists of $$$n$$$ vertices and $$$m$$$ edges. Also you are given three integers $$$n_1$$$, $$$n_2$$$ and $$$n_3$$$. Can you label each vertex with one of three numbers 1, 2 or 3 in such way, that: 1. Each vertex should be labeled by exactly one number 1, 2 or 3; 2. The total number of vertices with label 1 should be equal to $$$n_1$$$; 3. The total number of vertices with label 2 should be equal to $$$n_2$$$; 4. The total number of vertices with label 3 should be equal to $$$n_3$$$; 5. $$$col_u - col_v = 1$$$ for each edge $$$(u, v)$$$, where $$$col_x$$$ is the label of vertexxa0$$$x$$$. If there are multiple valid labelings, print any of them. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 5000$$$; $$$0 le m le 10^5$$$)xa0β€” the number of vertices and edges in the graph. The second line contains three integers $$$n_1$$$, $$$n_2$$$ and $$$n_3$$$ ($$$0 le n_1, n_2, n_3 le n$$$)xa0β€” the number of labels 1, 2 and 3, respectively. It's guaranteed that $$$n_1 + n_2 + n_3 = n$$$. Next $$$m$$$ lines contan description of edges: the $$$i$$$-th line contains two integers $$$u_i$$$, $$$v_i$$$ ($$$1 le u_i, v_i le n$$$; $$$u_i eq v_i$$$) β€” the vertices the $$$i$$$-th edge connects. It's guaranteed that the graph doesn't contain self-loops or multiple edges. Output If valid labeling exists then print "YES" (without quotes) in the first line. In the second line print string of length $$$n$$$ consisting of 1, 2 and 3. The $$$i$$$-th letter should be equal to the label of the $$$i$$$-th vertex. If there is no valid labeling, print "NO" (without quotes). Examples Input 6 3 2 2 2 3 1 5 4 2 5 Input 5 9 0 2 3 1 2 1 3 1 5 2 3 2 4 2 5 3 4 3 5 4 5
2,100
false
false
false
true
false
false
false
false
false
true
3,934
1104A
Vasya has his favourite number $$$n$$$. He wants to split it to some non-zero digits. It means, that he wants to choose some digits $$$d_1, d_2, ldots, d_k$$$, such that $$$1 leq d_i leq 9$$$ for all $$$i$$$ and $$$d_1 + d_2 + ldots + d_k = n$$$. Vasya likes beauty in everything, so he wants to find any solution with the minimal possible number of different digits among $$$d_1, d_2, ldots, d_k$$$. Help him! Input The first line contains a single integer $$$n$$$xa0β€” the number that Vasya wants to split ($$$1 leq n leq 1000$$$). Output In the first line print one integer $$$k$$$xa0β€” the number of digits in the partition. Note that $$$k$$$ must satisfy the inequality $$$1 leq k leq n$$$. In the next line print $$$k$$$ digits $$$d_1, d_2, ldots, d_k$$$ separated by spaces. All digits must satisfy the inequalities $$$1 leq d_i leq 9$$$. You should find a partition of $$$n$$$ in which the number of different digits among $$$d_1, d_2, ldots, d_k$$$ will be minimal possible among all partitions of $$$n$$$ into non-zero digits. Among such partitions, it is allowed to find any. It is guaranteed that there exists at least one partition of the number $$$n$$$ into digits. Note In the first test, the number $$$1$$$ can be divided into $$$1$$$ digit equal to $$$1$$$. In the second test, there are $$$3$$$ partitions of the number $$$4$$$ into digits in which the number of different digits is $$$1$$$. This partitions are $$$[1, 1, 1, 1]$$$, $$$[2, 2]$$$ and $$$[4]$$$. Any of these partitions can be found. And, for example, dividing the number $$$4$$$ to the digits $$$[1, 1, 2]$$$ isn't an answer, because it has $$$2$$$ different digits, that isn't the minimum possible number.
800
true
false
true
false
false
true
false
false
false
false
5,182
549B
The Looksery company, consisting of _n_ staff members, is planning another big party. Every employee has his phone number and the phone numbers of his friends in the phone book. Everyone who comes to the party, sends messages to his contacts about how cool it is. At the same time everyone is trying to spend as much time on the fun as possible, so they send messages to everyone without special thinking, moreover, each person even sends a message to himself or herself. Igor and Max, Looksery developers, started a dispute on how many messages each person gets. Igor indicates _n_ numbers, the _i_-th of which indicates how many messages, in his view, the _i_-th employee is going to take. If Igor guesses correctly at least one of these numbers, he wins, otherwise Max wins. You support Max in this debate, so you need, given the contact lists of the employees, to determine whether there is a situation where Igor loses. Specifically, you need to determine which employees should come to the party, and which should not, so after all the visitors send messages to their contacts, each employee received a number of messages that is different from what Igor stated. Input The first line contains a single integer _n_ (1u2009≀u2009_n_u2009≀u2009100) β€” the number of employees of company Looksery. Next _n_ lines contain the description of the contact lists of the employees. The _i_-th of these lines contains a string of length _n_, consisting of digits zero and one, specifying the contact list of the _i_-th employee. If the _j_-th character of the _i_-th string equals 1, then the _j_-th employee is in the _i_-th employee's contact list, otherwise he isn't. It is guaranteed that the _i_-th character of the _i_-th line is always equal to 1. The last line contains _n_ space-separated integers: _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≀u2009_a__i_u2009≀u2009_n_), where _a__i_ represents the number of messages that the _i_-th employee should get according to Igor. Output In the first line print a single integer _m_ β€” the number of employees who should come to the party so that Igor loses the dispute. In the second line print _m_ space-separated integers β€” the numbers of these employees in an arbitrary order. If Igor wins the dispute in any case, print -1. If there are multiple possible solutions, print any of them. Examples Input 4 1111 0101 1110 0001 1 0 1 0 Note In the first sample Igor supposes that the first employee will receive 0 messages. Since he isn't contained in any other contact list he must come to the party in order to receive one message from himself. If he is the only who come to the party then he will receive 1 message, the second employee will receive 0 messages and the third will also receive 1 message. Thereby Igor won't guess any number. In the second sample if the single employee comes to the party he receives 1 message and Igor wins, so he shouldn't do it. In the third sample the first employee will receive 2 messages, the second β€” 3, the third β€” 2, the fourth β€” 3.
2,300
false
true
false
false
false
true
false
false
false
true
7,644
1685E
It turns out that this is exactly the $$$100$$$-th problem of mine that appears in some programming competition. So it has to be special! And what can be more special than another problem about LIS... You are given a permutation $$$p_1, p_2, ldots, p_{2n+1}$$$ of integers from $$$1$$$ to $$$2n+1$$$. You will have to process $$$q$$$ updates, where the $$$i$$$-th update consists in swapping $$$p_{u_i}, p_{v_i}$$$. After each update, find any cyclic shift of $$$p$$$ with $$$LIS le n$$$, or determine that there is no such shift. (Refer to the output section for details). Here $$$LIS(a)$$$ denotes the length of . The second line of the input contains $$$2n+1$$$ integers $$$p_1, p_2, ldots, p_{2n+1}$$$ ($$$1 le p_i le 2n+1$$$, all $$$p_i$$$ are distinct) xa0β€” the elements of $$$p$$$. The $$$i$$$-th of the next $$$q$$$ lines contains two integers $$$u_i, v_i$$$ ($$$1 le u_i, v_i le 2n+1$$$, $$$u_i eq v_i$$$) xa0β€” indicating that you have to swap elements $$$p_{u_i}, p_{v_i}$$$ in the $$$i$$$-th update. Output After each update, output any $$$k$$$ $$$(0 le k le 2n)$$$, such that the length of the longest increasing subsequence of $$$(p_{k+1}, p_{k+2}, ldots, p_{2n+1}, p_1, ldots, p_k)$$$ doesn't exceed $$$n$$$, or $$$-1$$$, if there is no such $$$k$$$. Note After the first update, our permutation becomes $$$(5, 2, 3, 4, 1)$$$. We can show that all its cyclic shifts have $$$LIS ge 3$$$. After the second update, our permutation becomes $$$(1, 2, 3, 4, 5)$$$. We can show that all its cyclic shifts have $$$LIS ge 3$$$. After the third update, our permutation becomes $$$(1, 2, 3, 5, 4)$$$. Its shift by $$$2$$$ is $$$(3, 5, 4, 1, 2)$$$, and its $$$LIS = 2$$$. After the fourth update, our permutation becomes $$$(1, 2, 3, 4, 5)$$$. We can show that all its cyclic shifts have $$$LIS ge 3$$$. After the fifth update, our permutation becomes $$$(4, 2, 3, 1, 5)$$$. Its shift by $$$4$$$ is $$$(5, 4, 2, 3, 1)$$$, and its $$$LIS = 2$$$. After the fifth update, our permutation becomes $$$(4, 5, 3, 1, 2)$$$. Its shift by $$$0$$$ is $$$(4, 5, 3, 1, 2)$$$, and its $$$LIS = 2$$$.
3,500
false
true
false
false
true
false
false
false
false
false
2,160
2031F
This is an interactive problem. Returning from a restful vacation on Australia's Gold Coast, Penchick forgot to bring home gifts for his pet duck Duong Canh! But perhaps a beautiful problem crafted through deep thought on the scenic beaches could be the perfect souvenir. There is a hidden permutation$$$^{ ext{βˆ—}}$$$ $$$p$$$ of length $$$n$$$, where $$$n$$$ is even. You are allowed to make the following query: Choose a subsequence$$$^{ ext{†}}$$$ of the permutation $$$p$$$ with even length $$$4le kle n$$$. The interactor will return the value of the two medians$$$^{ ext{‑}}$$$ in the chosen subsequence. Find the index of the two medians in permutation $$$p$$$ using at most $$$80$$$ queries. Note that the interactor is non-adaptive. This means that the permutation $$$p$$$ is fixed at the beginning and will not change based on your queries. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1000$$$). The description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$6 le n le 100$$$, $$$n$$$ is even)xa0β€” the length of the hidden permutation $$$p$$$. For each test case, after reading the integer $$$n$$$, you should begin the interaction and find the answer before reading $$$n$$$ for the next test case. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$. Interaction To make a query, print a single line in the following format: $$$mathtt{?};k;x_1;x_2 ldots x_{k-1};x_k$$$ ($$$4le kle n$$$, $$$k$$$ is even, $$$1 le x_i le n$$$, $$$x_i$$$ is pairwise distinct)xa0β€” the length of the chosen subsequence followed by the indices of the chosen subsequence. After each query, you should read a line containing two integers $$$m_1$$$ and $$$m_2$$$ ($$$1 le m_1 < m_2 le n$$$)xa0β€” the value of the two medians in array $$$[p_{x_1}, p_{x_2}, ldots, p_{x_{k-1}}, p_{x_k}]$$$. You can make at most $$$80$$$ such queries in each test case. To give the final answer, print a single line in the following format: $$$mathtt{!};i_1;i_2$$$ ($$$1le i_1, i_2 le n$$$)xa0β€” the index of the two medians. Note that the order in which $$$i_1$$$ and $$$i_2$$$ is printed does not matter. In other words, your solution is valid as long as $$$p_{i_1} = frac{n}{2}$$$ and $$$p_{i_2} = frac{n}{2} + 1$$$, or $$$p_{i_1} = frac{n}{2} + 1$$$ and $$$p_{i_2} = frac{n}{2}$$$. After printing each query do not forget to output the end of line and flush$$$^{ ext{βˆ—}}$$$ the output. Otherwise, you will get Idleness limit exceeded verdict. If, at any interaction step, you read $$$-1$$$ instead of valid data, your solution must exit immediately. This means that your solution will receive Wrong answer because of an invalid query or any other mistake. Failing to exit can result in an arbitrary verdict because your solution will continue to read from a closed stream. Hack format For hacks, use the following format. The first line should contain $$$t$$$xa0β€” the number of test cases. The first line of each test case should contain a single even integer $$$n$$$. The second line of each test case should contain a permutation $$$p_1, p_2, ldots, p_n$$$ of length $$$n$$$. As an example, the hack format for the example input is: 2 6 6 2 3 5 1 4 10 10 9 8 7 6 5 4 3 2 1 Example Input 2 6 3 4 3 4 2 3 10 3 4 6 7 Output ? 6 1 2 3 4 5 6 ? 4 3 6 1 5 ? 4 3 6 2 5 ! 3 6 ? 6 1 3 7 8 9 10 ? 8 1 2 3 4 5 6 7 8 ! 6 5 Note In the first test case, the hidden permutation is $$$p = [6, 2, 3, 5, 1, 4]$$$. 1. The entire permutation was chosen for the first query. The two medians of the entire permutation $$$p$$$ are $$$3$$$ and $$$4$$$. 2. The indices of the chosen subsequence in the second query are $$$3$$$, $$$6$$$, $$$1$$$, and $$$5$$$. The interactor returns the two medians of the subsequence $$$[p_3, p_6, p_1, p_5] = [3, 4, 6, 1]$$$, which are $$$3$$$ and $$$4$$$. 3. The indices of the chosen subsequence in the second query are $$$3$$$, $$$6$$$, $$$2$$$, and $$$5$$$. The interactor returns the two medians of the subsequence $$$[p_3, p_6, p_2, p_5] = [3, 4, 2, 1]$$$, which are $$$2$$$ and $$$3$$$. The answer "! 3 6" is valid as $$$p_3 = 3$$$ and $$$p_6 = 4$$$. In the second test case, the hidden permutation is $$$p = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]$$$. 1. The indices of the chosen subsequence in the second query are $$$1$$$, $$$3$$$, $$$7$$$, $$$8$$$, $$$9$$$, and $$$10$$$. The interactor returns the two medians of the subsequence $$$[p_1, p_3, p_7, p_8, p_9, p_{10}] = [10, 8, 4, 3, 2, 1]$$$, which are $$$3$$$ and $$$4$$$. 2. The indices of the chosen subsequence in the second query are $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$, and $$$8$$$. The interactor returns the two medians of the subsequence $$$[p_1, p_2, p_3, p_4, p_5, p_6, p_7, p_8] = [10, 9, 8, 7, 6, 5, 4, 3]$$$, which are $$$6$$$ and $$$7$$$. The answer "! 5 6" is valid as $$$p_5 = 6$$$ and $$$p_6 = 5$$$.
2,800
false
false
false
false
false
true
false
true
false
false
75
1174D
Problem - 1174D - 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 constructive algorithms *1900 No tag edit access β†’ Contest materials elements from the beginning and several (possibly, zero or all) elements from the end. Input The only line contains two integers $$$n$$$ and $$$x$$$ ($$$1 le n le 18$$$, $$$1 le x<2^{18}$$$). Output The first line should contain the length of the array $$$l$$$. If $$$l$$$ is positive, the second line should contain $$$l$$$ space-separated integers $$$a_1$$$, $$$a_2$$$, $$$dots$$$, $$$a_l$$$ ($$$1 le a_i < 2^n$$$)xa0β€” the elements of the array $$$a$$$. If there are multiple solutions, print any of them. Examples Input 3 5 Output 3 6 1 3 Input 2 4 Output 3 1 3 1 Input 1 1 Output 0 Note In the first example, the bitwise XOR of the subsegments are $$${6,7,4,1,2,3}$$$.
1,900
false
false
false
false
false
true
false
false
false
false
4,848
226A
An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!). The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the 2-nd one, the 2-nd one β€” to the 1-st and the 3-rd ones, the 3-rd one β€” only to the 2-nd one. The transitions are possible only between the adjacent sections. The spacecraft team consists of _n_ aliens. Each of them is given a rank β€” an integer from 1 to _n_. The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section _a_ to section _b_ only if it is senior in rank to all aliens who are in the segments _a_ and _b_ (besides, the segments _a_ and _b_ are of course required to be adjacent). Any alien requires exactly 1 minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship. Alien _A_ is senior in rank to alien _B_, if the number indicating rank _A_, is more than the corresponding number for _B_. At the moment the whole saucer team is in the 3-rd segment. They all need to move to the 1-st segment. One member of the crew, the alien with the identification number CFR-140, decided to calculate the minimum time (in minutes) they will need to perform this task. Help CFR-140, figure out the minimum time (in minutes) that all the astronauts will need to move from the 3-rd segment to the 1-st one. Since this number can be rather large, count it modulo _m_. Input The first line contains two space-separated integers: _n_ and _m_ (1u2009≀u2009_n_,u2009_m_u2009≀u2009109) β€” the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly. Output Print a single number β€” the answer to the problem modulo _m_. Note In the first sample the only crew member moves from segment 3 to segment 2, and then from segment 2 to segment 1 without any problems. Thus, the whole moving will take two minutes. To briefly describe the movements in the second sample we will use value , which would correspond to an alien with rank _i_ moving from the segment in which it is at the moment, to the segment number _j_. Using these values, we will describe the movements between the segments in the second sample: , , , , , , , , , , , , , , , , , , , , , , , , , ; In total: the aliens need 26 moves. The remainder after dividing 26 by 8 equals 2, so the answer to this test is 2.
1,400
true
false
false
false
false
false
false
false
false
false
8,935
437B
Problem - 437B - 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 greedy implementation sortings *1500 No tag edit access β†’ Contest materials equals 2_k_ where _k_ is the position of the first one in the binary representation of _x_. For example, _lowbit_(100102)u2009=u2009102,u2009_lowbit_(100012)u2009=u200912,u2009_lowbit_(100002)u2009=u2009100002 (binary representation). Can you help Picks and find any set _S_, that satisfies all the above conditions? Input The first line contains two integers: _sum_,u2009_limit_ (1u2009≀u2009_sum_,u2009_limit_u2009≀u2009105). Output In the first line print an integer _n_ (1u2009≀u2009_n_u2009≀u2009105), denoting the size of _S_. Then print the elements of set _S_ in any order. If there are multiple answers, print any of them. If it's impossible to find a suitable set, print -1. Examples Input 5 5 Output 2 4 5 Input 4 3 Output 3 2 3 1 Input 5 1 Output -1 Note In sample test 1: _lowbit_(4)u2009=u20094,u2009_lowbit_(5)u2009=u20091,u20094u2009+u20091u2009=u20095. In sample test 2: _lowbit_(1)u2009=u20091,u2009_lowbit_(2)u2009=u20092,u2009_lowbit_(3)u2009=u20091,u20091u2009+u20092u2009+u20091u2009=u20094.
1,500
false
true
true
false
false
false
false
false
true
false
8,090
253A
Problem - 253A - 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 and Codeforces Round #155 (Div. 2)") β€” tutorial") such that positions with indexes _i_ and _i_u2009+u20091 contain children of different genders (position _i_ has a girl and position _i_u2009+u20091 has a boy or vice versa) must be as large as possible. Help the children and tell them how to form the line. Input The single line of the input contains two integers _n_ and _m_ (1u2009≀u2009_n_,u2009_m_u2009≀u2009100), separated by a space. Output Print a line of _n_u2009+u2009_m_ characters. Print on the _i_-th position of the line character "B", if the _i_-th position of your arrangement should have a boy and "G", if it should have a girl. Of course, the number of characters "B" should equal _n_ and the number of characters "G" should equal _m_. If there are multiple optimal solutions, print any of them. Examples Input 3 3 Output GBGBGB Input 4 2 Output BGBGBB Note In the first sample another possible answer is BGBGBG. In the second sample answer BBGBGB is also optimal.
1,100
false
true
false
false
false
false
false
false
false
false
8,824
1758A
A palindrome is a string that reads the same backward as forward. For example, the strings $$$ exttt{z}$$$, $$$ exttt{aaa}$$$, $$$ exttt{aba}$$$, and $$$ exttt{abccba}$$$ are palindromes, but $$$ exttt{codeforces}$$$ and $$$ exttt{ab}$$$ are not. The double of a string $$$s$$$ is obtained by writing each character twice. For example, the double of $$$ exttt{seeing}$$$ is $$$ exttt{sseeeeiinngg}$$$. Given a string $$$s$$$, rearrange its double to form a palindrome. Output the rearranged string. It can be proven that such a rearrangement always exists. Input The first line of input contains $$$t$$$ ($$$1 leq t leq 1000$$$)xa0β€” the number of test cases. The only line of each test case contains a single string $$$s$$$ ($$$1 leq s leq 100$$$) consisting only of lowercase English letters. Note that the sum of $$$s$$$ over all test cases is not bounded. Output For each test case, output a palindromic string of length $$$2 cdot s$$$ that is a rearrangement of the double of $$$s$$$. Example Input 4 a sururu errorgorn anutforajaroftuna Output aa suurruurruus rgnororerrerorongr aannuuttffoorraajjaarrooffttuunnaa Note In the first test case, the double of $$$ exttt{a}$$$ is $$$ exttt{aa}$$$, which is already a palindrome. In the second test case, the double of $$$ exttt{sururu}$$$ is $$$ exttt{ssuurruurruu}$$$. If we move the first $$$ exttt{s}$$$ to the end, we get $$$ exttt{suurruurruus}$$$, which is a palindrome. In the third test case, the double of $$$ exttt{errorgorn}$$$ is $$$ exttt{eerrrroorrggoorrnn}$$$. We can rearrange the characters to form $$$ exttt{rgnororerrerorongr}$$$, which is a palindrome.
800
false
false
false
false
false
true
false
false
false
false
1,768
908C
Carol is currently curling. She has _n_ disks each with radius _r_ on the 2D plane. Initially she has all these disks above the line _y_u2009=u200910100. She then will slide the disks towards the line _y_u2009=u20090 one by one in order from 1 to _n_. When she slides the _i_-th disk, she will place its center at the point (_x__i_,u200910100). She will then push it so the disk’s _y_ coordinate continuously decreases, and _x_ coordinate stays constant. The disk stops once it touches the line _y_u2009=u20090 or it touches any previous disk. Note that once a disk stops moving, it will not move again, even if hit by another disk. Compute the _y_-coordinates of centers of all the disks after all disks have been pushed. Input The first line will contain two integers _n_ and _r_ (1u2009≀u2009_n_,u2009_r_u2009≀u20091u2009000), the number of disks, and the radius of the disks, respectively. The next line will contain _n_ integers _x_1,u2009_x_2,u2009...,u2009_x__n_ (1u2009≀u2009_x__i_u2009≀u20091u2009000)xa0β€” the _x_-coordinates of the disks. Output Print a single line with _n_ numbers. The _i_-th number denotes the _y_-coordinate of the center of the _i_-th disk. The output will be accepted if it has absolute or relative error at most 10u2009-u20096. Namely, let's assume that your answer for a particular value of a coordinate is _a_ and the answer of the jury is _b_. The checker program will consider your answer correct if for all coordinates. Example Output 2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613 Note The final positions of the disks will look as follows: In particular, note the position of the last disk.
1,500
true
false
true
false
false
false
true
false
false
false
6,118
991E
This night wasn't easy on Vasya. His favorite team lost, and he didn't find himself victorious eitherxa0β€” although he played perfectly, his teammates let him down every time. He had to win at least one more time, but the losestreak only grew longer and longer... It's no wonder he didn't get any sleep this night at all. In the morning, Vasya was waiting the bus to the university on the bus stop. Vasya's thoughts were hazy and so he couldn't remember the right bus' number quite right and got onto the bus with the number $$$n$$$. In the bus, Vasya thought that he could get the order of the digits in the number of the bus wrong. Futhermore, he could "see" some digits several times, but the digits he saw were definitely in the real number of the bus. For example, if Vasya saw the number 2028, it could mean that the real bus number could be 2028, 8022, 2820 or just 820. However, numbers 80, 22208, 52 definitely couldn't be the number of the bus. Also, real bus number couldn't start with the digit 0, this meaning that, for example, number 082 couldn't be the real bus number too. Given $$$n$$$, determine the total number of possible bus number variants. Note In the first sample, only variants $$$97$$$ and $$$79$$$ are possible. In the second sample, the variants (in the increasing order) are the following: $$$208$$$, $$$280$$$, $$$802$$$, $$$820$$$, $$$2028$$$, $$$2082$$$, $$$2208$$$, $$$2280$$$, $$$2802$$$, $$$2820$$$, $$$8022$$$, $$$8202$$$, $$$8220$$$.
1,800
true
false
false
false
false
false
true
false
false
false
5,741
1196F
You are given a connected undirected weighted graph consisting of $$$n$$$ vertices and $$$m$$$ edges. You need to print the $$$k$$$-th smallest shortest path in this graph (paths from the vertex to itself are not counted, paths from $$$i$$$ to $$$j$$$ and from $$$j$$$ to $$$i$$$ are counted as one). More formally, if $$$d$$$ is the matrix of shortest paths, where $$$d_{i, j}$$$ is the length of the shortest path between vertices $$$i$$$ and $$$j$$$ ($$$1 le i < j le n$$$), then you need to print the $$$k$$$-th element in the sorted array consisting of all $$$d_{i, j}$$$, where $$$1 le i < j le n$$$. Input The first line of the input contains three integers $$$n, m$$$ and $$$k$$$ ($$$2 le n le 2 cdot 10^5$$$, $$$n - 1 le m le minBig(frac{n(n-1)}{2}, 2 cdot 10^5Big)$$$, $$$1 le k le minBig(frac{n(n-1)}{2}, 400Big)$$$xa0β€” the number of vertices in the graph, the number of edges in the graph and the value of $$$k$$$, correspondingly. Then $$$m$$$ lines follow, each containing three integers $$$x$$$, $$$y$$$ and $$$w$$$ ($$$1 le x, y le n$$$, $$$1 le w le 10^9$$$, $$$x e y$$$) denoting an edge between vertices $$$x$$$ and $$$y$$$ of weight $$$w$$$. It is guaranteed that the given graph is connected (there is a path between any pair of vertices), there are no self-loops (edges connecting the vertex with itself) and multiple edges (for each pair of vertices $$$x$$$ and $$$y$$$, there is at most one edge between this pair of vertices in the graph). Output Print one integerxa0β€” the length of the $$$k$$$-th smallest shortest path in the given graph (paths from the vertex to itself are not counted, paths from $$$i$$$ to $$$j$$$ and from $$$j$$$ to $$$i$$$ are counted as one). Examples Input 6 10 5 2 5 1 5 3 9 6 2 2 1 3 1 5 1 8 6 5 10 1 6 5 6 4 6 3 6 2 3 4 5 Input 7 15 18 2 6 3 5 7 4 6 5 4 3 6 9 6 7 7 1 6 4 7 1 6 7 2 1 4 3 2 3 2 8 5 3 6 2 5 5 3 7 9 4 1 8 2 1 1
2,200
false
false
false
false
false
true
true
false
true
false
4,716
1790F
Timofey came to a famous summer school and found a tree on $$$n$$$ vertices. A tree is a connected undirected graph without cycles. Every vertex of this tree, except $$$c_0$$$, is colored white. The vertex $$$c_0$$$ is colored black. Timofey wants to color all the vertices of this tree in black. To do this, he performs $$$n - 1$$$ operations. During the $$$i$$$-th operation, he selects the vertex $$$c_i$$$, which is currently white, and paints it black. Let's call the positivity of tree the minimum distance between all pairs of different black vertices in it. The distance between the vertices $$$v$$$ and $$$u$$$ is the number of edges on the path from $$$v$$$ to $$$u$$$. After each operation, Timofey wants to know the positivity of the current tree. Input The first line contains the integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of testcases. The first line of each testcase contains the integers $$$n, c_0$$$ ($$$2 le n le 2 cdot 10^5$$$, $$$1 le c_0 le n$$$)xa0β€” the number of vertices in the tree and index of the initial black vertex. The second line of each testcase contains $$$n - 1$$$ unique integers $$$c_1, c_2, dots, c_{n-1}$$$ ($$$1 le c_i le n$$$, $$$c_i e c_0$$$), where $$$c_i$$$ is the vertex which is colored black during the $$$i$$$-th operation. Each of the next $$$n - 1$$$ row of each testcase contains the integers $$$v_i, u_i$$$ ($$$1 le v_i, u_i le n$$$)xa0β€” edges in the tree. It is guaranteed that the sum of $$$n$$$ for all testcases does not exceed $$$2 cdot 10^5$$$. Output For each testcase, print $$$n - 1$$$ integer on a separate line. The integer with index $$$i$$$ must be equal to positivity of the tree obtained by the first $$$i$$$ operations. Example Input 6 6 6 4 1 3 5 2 2 4 6 5 5 3 3 4 1 3 4 2 4 1 3 3 1 2 3 1 4 10 3 10 7 6 5 2 9 8 1 4 1 2 1 3 4 5 4 3 6 4 8 7 9 8 10 8 1 8 7 3 7 5 1 2 4 6 1 2 3 2 4 5 3 4 6 5 7 6 9 7 9 3 1 4 2 6 8 5 4 1 8 9 4 8 2 6 7 3 2 4 3 5 5 4 10 2 1 8 5 10 6 9 4 3 7 10 7 7 8 3 6 9 7 7 6 4 2 1 6 7 5 9 2 Output 3 2 1 1 1 3 1 1 3 2 2 2 2 2 1 1 1 4 2 2 1 1 1 5 1 1 1 1 1 1 1 4 3 2 2 1 1 1 1 1 Note In the first testcase, after the second operation, the tree looks like this: The distance between vertices $$$1$$$ and $$$6$$$ is $$$3$$$, the distance between vertices $$$4$$$ and $$$6$$$ is $$$3$$$, the distance between vertices $$$1$$$ and $$$4$$$ is $$$2$$$. The positivity of this tree is equal to the minimum of these distances. It equals $$$2$$$. In the third testcase, after the fourth operation, the tree looks like this: The positivity of this tree is $$$2$$$.
2,100
true
true
false
false
false
false
true
false
false
true
1,537
1023D
Initially there was an array $$$a$$$ consisting of $$$n$$$ integers. Positions in it are numbered from $$$1$$$ to $$$n$$$. Exactly $$$q$$$ queries were performed on the array. During the $$$i$$$-th query some segment $$$(l_i, r_i)$$$ $$$(1 le l_i le r_i le n)$$$ was selected and values of elements on positions from $$$l_i$$$ to $$$r_i$$$ inclusive got changed to $$$i$$$. The order of the queries couldn't be changed and all $$$q$$$ queries were applied. It is also known that every position from $$$1$$$ to $$$n$$$ got covered by at least one segment. We could have offered you the problem about checking if some given array (consisting of $$$n$$$ integers with values from $$$1$$$ to $$$q$$$) can be obtained by the aforementioned queries. However, we decided that it will come too easy for you. So the enhancement we introduced to it is the following. Some set of positions (possibly empty) in this array is selected and values of elements on these positions are set to $$$0$$$. Your task is to check if this array can be obtained by the aforementioned queries. Also if it can be obtained then restore this array. If there are multiple possible arrays then print any of them. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 2 cdot 10^5$$$) β€” the number of elements of the array and the number of queries perfomed on it. The second line contains $$$n$$$ integer numbers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le q$$$) β€” the resulting array. If element at some position $$$j$$$ is equal to $$$0$$$ then the value of element at this position can be any integer from $$$1$$$ to $$$q$$$. Output Print "YES" if the array $$$a$$$ can be obtained by performing $$$q$$$ queries. Segments $$$(l_i, r_i)$$$ $$$(1 le l_i le r_i le n)$$$ are chosen separately for each query. Every position from $$$1$$$ to $$$n$$$ should be covered by at least one segment. Otherwise print "NO". If some array can be obtained then print $$$n$$$ integers on the second line β€” the $$$i$$$-th number should be equal to the $$$i$$$-th element of the resulting array and should have value from $$$1$$$ to $$$q$$$. This array should be obtainable by performing exactly $$$q$$$ queries. If there are multiple possible arrays then print any of them. Note In the first example you can also replace $$$0$$$ with $$$1$$$ but not with $$$3$$$. In the second example it doesn't really matter what segments to choose until query $$$10$$$ when the segment is $$$(1, 3)$$$. The third example showcases the fact that the order of queries can't be changed, you can't firstly set $$$(1, 3)$$$ to $$$6$$$ and after that change $$$(2, 2)$$$ to $$$5$$$. The segment of $$$5$$$ should be applied before segment of $$$6$$$. There is a lot of correct resulting arrays for the fourth example.
1,700
false
false
false
false
true
true
false
false
false
false
5,587
1091C
There are $$$n$$$ people sitting in a circle, numbered from $$$1$$$ to $$$n$$$ in the order in which they are seated. That is, for all $$$i$$$ from $$$1$$$ to $$$n-1$$$, the people with id $$$i$$$ and $$$i+1$$$ are adjacent. People with id $$$n$$$ and $$$1$$$ are adjacent as well. The person with id $$$1$$$ initially has a ball. He picks a positive integer $$$k$$$ at most $$$n$$$, and passes the ball to his $$$k$$$-th neighbour in the direction of increasing ids, that person passes the ball to his $$$k$$$-th neighbour in the same direction, and so on until the person with the id $$$1$$$ gets the ball back. When he gets it back, people do not pass the ball any more. For instance, if $$$n = 6$$$ and $$$k = 4$$$, the ball is passed in order $$$[1, 5, 3, 1]$$$. Consider the set of all people that touched the ball. The fun value of the game is the sum of the ids of people that touched it. In the above example, the fun value would be $$$1 + 5 + 3 = 9$$$. Find and report the set of possible fun values for all choices of positive integer $$$k$$$. It can be shown that under the constraints of the problem, the ball always gets back to the $$$1$$$-st player after finitely many steps, and there are no more than $$$10^5$$$ possible fun values for given $$$n$$$. Output Suppose the set of all fun values is $$$f_1, f_2, dots, f_m$$$. Output a single line containing $$$m$$$ space separated integers $$$f_1$$$ through $$$f_m$$$ in increasing order. Note In the first sample, we've already shown that picking $$$k = 4$$$ yields fun value $$$9$$$, as does $$$k = 2$$$. Picking $$$k = 6$$$ results in fun value of $$$1$$$. For $$$k = 3$$$ we get fun value $$$5$$$ and with $$$k = 1$$$ or $$$k = 5$$$ we get $$$21$$$. In the second sample, the values $$$1$$$, $$$10$$$, $$$28$$$, $$$64$$$ and $$$136$$$ are achieved for instance for $$$k = 16$$$, $$$8$$$, $$$4$$$, $$$10$$$ and $$$11$$$, respectively.
1,400
true
false
false
false
false
false
false
false
false
false
5,257
1748A
You have $$$n$$$ rectangular wooden blocks, which are numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th block is $$$1$$$ unit high and $$$lceil frac{i}{2} ceil$$$ units long. Here, $$$lceil frac{x}{2} ceil$$$ denotes the result of division of $$$x$$$ by $$$2$$$, rounded up. For example, $$$lceil frac{4}{2} ceil = 2$$$ and $$$lceil frac{5}{2} ceil = lceil 2.5 ceil = 3$$$. For example, if $$$n=5$$$, then the blocks have the following sizes: $$$1 imes 1$$$, $$$1 imes 1$$$, $$$1 imes 2$$$, $$$1 imes 2$$$, $$$1 imes 3$$$. The available blocks for $$$n=5$$$ Find the maximum possible side length of a square you can create using these blocks, without rotating any of them. Note that you don't have to use all of the blocks. One of the ways to create $$$3 imes 3$$$ square using blocks $$$1$$$ through $$$5$$$ Input Each test contains multiple test cases. 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 a single integer $$$n$$$ ($$$1 le n le 10^9$$$)xa0β€” the number of blocks. Output For each test case, print one integerxa0β€” the maximum possible side length of a square you can create. Note In the first test case, you can create a $$$1 imes 1$$$ square using only one of the blocks. In the second test case, one of the possible ways to create a $$$3 imes 3$$$ square is shown in the statement. It is impossible to create a $$$4 imes 4$$$ or larger square, so the answer is $$$3$$$.
800
true
false
false
false
false
false
false
false
false
false
1,797
1515H
Problem - 1515H - Codeforces =============== xa0 ]( "Codeforces Global Round 14") xa0β€” the number of integers and the number of queries, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i < 2^{20}$$$)xa0β€” the integers that Phoenix starts with. The next $$$q$$$ lines contain the queries. For each query, the first integer of each line is $$$t$$$ ($$$1 le t le 4$$$)xa0β€” the type of query. If $$$t in {1, 2, 3}$$$, then three integers $$$l_i$$$, $$$r_i$$$, and $$$x_i$$$ will follow ($$$0 le l_i, r_i, x_i < 2^{20}$$$; $$$l_i le r_i$$$). Otherwise, if $$$t=4$$$, two integers $$$l_i$$$ and $$$r_i$$$ will follow ($$$0 le l_i le r_i < 2^{20}$$$). It is guaranteed that there is at least one query where $$$t=4$$$. Output Print the answer for each query where $$$t=4$$$. Examples Input 5 6 5 4 3 2 1 1 2 3 2 4 2 5 3 2 5 3 4 1 6 2 1 1 8 4 8 10 Output 3 2 1 Input 6 7 6 0 2 3 2 7 1 0 4 3 2 6 8 4 4 0 7 3 2 5 3 1 0 1 2 4 0 3 4 2 7 Output 5 1 2 Note In the first example: For the first query, $$$2$$$ is replaced by $$$2$$$ AND $$$2 = 2$$$ and $$$3$$$ is replaced with $$$3$$$ AND $$$2 = 2$$$. The set of numbers is $$${1, 2, 4, 5}$$$. For the second query, there are $$$3$$$ distinct numbers between $$$2$$$ and $$$5$$$: $$$2$$$, $$$4$$$, and $$$5$$$. For the third query, $$$2$$$ is replaced by $$$2$$$ XOR $$$3 = 1$$$, $$$4$$$ is replaced by $$$4$$$ XOR $$$3 = 7$$$, and $$$5$$$ is replaced by $$$5$$$ XOR $$$3 = 6$$$. The set of numbers is $$${1, 6, 7}$$$. For the fourth query, there are $$$2$$$ distinct numbers between $$$1$$$ and $$$6$$$: $$$1$$$ and $$$6$$$. For the fifth query, $$$1$$$ is replaced by $$$1$$$ OR $$$8 = 9$$$. The set of numbers is $$${6, 7, 9}$$$. For the sixth query, there is one distinct number between $$$8$$$ and $$$10$$$: $$$9$$$.
3,500
false
false
false
false
true
false
true
false
true
false
3,084
29C
One day Bob got a letter in an envelope. Bob knows that when Berland's post officers send a letter directly from city Β«AΒ» to city Β«BΒ», they stamp it with Β«A BΒ», or Β«B AΒ». Unfortunately, often it is impossible to send a letter directly from the city of the sender to the city of the receiver, that's why the letter is sent via some intermediate cities. Post officers never send a letter in such a way that the route of this letter contains some city more than once. Bob is sure that the post officers stamp the letters accurately. There are _n_ stamps on the envelope of Bob's letter. He understands that the possible routes of this letter are only two. But the stamps are numerous, and Bob can't determine himself none of these routes. That's why he asks you to help him. Find one of the possible routes of the letter. Input The first line contains integer _n_ (1u2009≀u2009_n_u2009≀u2009105) β€” amount of mail stamps on the envelope. Then there follow _n_ lines with two integers each β€” description of the stamps. Each stamp is described with indexes of the cities between which a letter is sent. The indexes of cities are integers from 1 to 109. Indexes of all the cities are different. Every time the letter is sent from one city to another, exactly one stamp is put on the envelope. It is guaranteed that the given stamps correspond to some valid route from some city to some other city. Output Output _n_u2009+u20091 numbers β€” indexes of cities in one of the two possible routes of the letter.
1,700
false
false
true
false
true
false
false
false
false
true
9,851
1454C
You are given a sequence $$$a$$$, initially consisting of $$$n$$$ integers. You want to transform this sequence so that all elements in it are equal (i.xa0e. it contains several occurrences of the same element). To achieve this, you choose some integer $$$x$$$ that occurs at least once in $$$a$$$, and then perform the following operation any number of times (possibly zero): choose some segment $$$[l, r]$$$ of the sequence and remove it. But there is one exception: you are not allowed to choose a segment that contains $$$x$$$. More formally, you choose some contiguous subsequence $$$[a_l, a_{l + 1}, dots, a_r]$$$ such that $$$a_i e x$$$ if $$$l le i le r$$$, and remove it. After removal, the numbering of elements to the right of the removed segment changes: the element that was the $$$(r+1)$$$-th is now $$$l$$$-th, the element that was $$$(r+2)$$$-th is now $$$(l+1)$$$-th, and so on (i.xa0e. the remaining sequence just collapses). Note that you can not change $$$x$$$ after you chose it. For example, suppose $$$n = 6$$$, $$$a = [1, 3, 2, 4, 1, 2]$$$. Then one of the ways to transform it in two operations is to choose $$$x = 1$$$, then: 1. choose $$$l = 2$$$, $$$r = 4$$$, so the resulting sequence is $$$a = [1, 1, 2]$$$; 2. choose $$$l = 3$$$, $$$r = 3$$$, so the resulting sequence is $$$a = [1, 1]$$$. Note that choosing $$$x$$$ is not an operation. Also, note that you can not remove any occurrence of $$$x$$$. Your task is to find the minimum number of operations required to transform the sequence in a way described above. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$) β€” the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) β€” the number of elements in $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 cdot 10^5$$$ ($$$sum n le 2 cdot 10^5$$$). Output For each test case, print the answer β€” the minimum number of operations required to transform the given sequence in a way described in the problem statement. It can be proven that it is always possible to perform a finite sequence of operations so the sequence is transformed in the required way.
1,200
false
true
true
false
false
false
false
false
false
false
3,406
1763C
You are given an array $$$a$$$ of $$$n$$$ integers. You are allowed to perform the following operation on it as many times as you want (0 or more times): Choose $$$2$$$ indices $$$i$$$,$$$j$$$ where $$$1 le i < j le n$$$ and replace $$$a_k$$$ for all $$$i leq k leq j$$$ with $$$a_i - a_j$$$ Print the maximum sum of all the elements of the final array that you can obtain in such a way. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$)xa0β€” the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0β€” the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0β€” the elements of array $$$a$$$. It's guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print the sum of the final array. Note In the first test case, it is not possible to achieve a sum $$$> 3$$$ by using these operations, therefore the maximum sum is $$$3$$$. In the second test case, it can be shown that the maximum sum achievable is $$$16$$$. By using operation $$$(1,2)$$$ we transform the array from $$$[9,1]$$$ into $$$[8,8]$$$, thus the sum of the final array is $$$16$$$. In the third test case, it can be shown that it is not possible to achieve a sum $$$> 18$$$ by using these operations, therefore the maximum sum is $$$18$$$.
2,000
false
true
false
false
false
true
true
false
false
false
1,731
754B
Ilya is an experienced player in tic-tac-toe on the 4u2009Γ—u20094 field. He always starts and plays with Xs. He played a lot of games today with his friend Arseny. The friends became tired and didn't finish the last game. It was Ilya's turn in the game when they left it. Determine whether Ilya could have won the game by making single turn or not. The rules of tic-tac-toe on the 4u2009Γ—u20094 field are as follows. Before the first turn all the field cells are empty. The two players take turns placing their signs into empty cells (the first player places Xs, the second player places Os). The player who places Xs goes first, the another one goes second. The winner is the player who first gets three of his signs in a row next to each other (horizontal, vertical or diagonal). Input The tic-tac-toe position is given in four lines. Each of these lines contains four characters. Each character is '.' (empty cell), 'x' (lowercase English letter x), or 'o' (lowercase English letter o). It is guaranteed that the position is reachable playing tic-tac-toe, and it is Ilya's turn now (in particular, it means that the game is not finished). It is possible that all the cells are empty, it means that the friends left without making single turn. Output Print single line: "YES" in case Ilya could have won by making single turn, and "NO" otherwise. Note In the first example Ilya had two winning moves: to the empty cell in the left column and to the leftmost empty cell in the first row. In the second example it wasn't possible to win by making single turn. In the third example Ilya could have won by placing X in the last row between two existing Xs. In the fourth example it wasn't possible to win by making single turn.
1,100
false
false
true
false
false
false
true
false
false
false
6,781
1716B
A permutation of length $$$n$$$ is a sequence of integers from $$$1$$$ to $$$n$$$ such that each integer appears in it exactly once. Let the fixedness of a permutation $$$p$$$ be the number of fixed points in itxa0β€” the number of positions $$$j$$$ such that $$$p_j = j$$$, where $$$p_j$$$ is the $$$j$$$-th element of the permutation $$$p$$$. You are asked to build a sequence of permutations $$$a_1, a_2, dots$$$, starting from the identity permutation (permutation $$$a_1 = [1, 2, dots, n]$$$). Let's call it a permutation chain. Thus, $$$a_i$$$ is the $$$i$$$-th permutation of length $$$n$$$. For every $$$i$$$ from $$$2$$$ onwards, the permutation $$$a_i$$$ should be obtained from the permutation $$$a_{i-1}$$$ by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation $$$a_i$$$ should be strictly lower than the fixedness of the permutation $$$a_{i-1}$$$. Consider some chains for $$$n = 3$$$: $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$xa0β€” that is a valid chain of length $$$2$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. $$$a_1 = [2, 1, 3]$$$, $$$a_2 = [3, 1, 2]$$$xa0β€” that is not a valid chain. The first permutation should always be $$$[1, 2, 3]$$$ for $$$n = 3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [1, 3, 2]$$$, $$$a_3 = [1, 2, 3]$$$xa0β€” that is not a valid chain. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped but the fixedness increase from $$$1$$$ to $$$3$$$. $$$a_1 = [1, 2, 3]$$$, $$$a_2 = [3, 2, 1]$$$, $$$a_3 = [3, 1, 2]$$$xa0β€” that is a valid chain of length $$$3$$$. From $$$a_1$$$ to $$$a_2$$$, the elements on positions $$$1$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$3$$$ to $$$1$$$. From $$$a_2$$$ to $$$a_3$$$, the elements on positions $$$2$$$ and $$$3$$$ get swapped, the fixedness decrease from $$$1$$$ to $$$0$$$. Find the longest permutation chain. If there are multiple longest answers, print any of them. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 99$$$)xa0β€” the number of testcases. The only line of each testcase contains a single integer $$$n$$$ ($$$2 le n le 100$$$)xa0β€” the required length of permutations in the chain. Output For each testcase, first, print the length of a permutation chain $$$k$$$. Then print $$$k$$$ permutations $$$a_1, a_2, dots, a_k$$$. $$$a_1$$$ should be an identity permutation of length $$$n$$$ ($$$[1, 2, dots, n]$$$). For each $$$i$$$ from $$$2$$$ to $$$k$$$, $$$a_i$$$ should be obtained by swapping two elements in $$$a_{i-1}$$$. It should also have a strictly lower fixedness than $$$a_{i-1}$$$.
800
true
false
false
false
false
true
false
false
false
false
1,986