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
1348F
Problem - 1348F - 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 dfs and similar graphs greedy *2600 No tag edit access → Contest materials ") Editorial") xa0— the number of friends. The $$$i$$$-th of the next $$$n$$$ lines contain two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i le b_i le n$$$) xa0— Phoenix's memory of the $$$i$$$-th position from the left. It is guaranteed that Phoenix's memory is valid so there is at least one valid ordering. Output If Phoenix can reorder his friends in a unique order, print YES followed by $$$n$$$ integers xa0— the $$$i$$$-th integer should be the label of the $$$i$$$-th friend from the left. Otherwise, print NO. Then, print any two distinct valid orderings on the following two lines. If are multiple solutions, print any. Examples Input 4 4 4 1 3 2 4 3 4 Output YES 4 1 2 3 Input 4 1 3 2 4 3 4 2 3 Output NO 1 3 4 2 1 2 4 3
2,600
false
true
false
false
true
false
false
false
false
true
3,965
245C
Two pirates Polycarpus and Vasily play a very interesting game. They have _n_ chests with coins, the chests are numbered with integers from 1 to _n_. Chest number _i_ has _a__i_ coins. Polycarpus and Vasily move in turns. Polycarpus moves first. During a move a player is allowed to choose a positive integer _x_ (2·_x_u2009+u20091u2009≤u2009_n_) and take a coin from each chest with numbers _x_, 2·_x_, 2·_x_u2009+u20091. It may turn out that some chest has no coins, in this case the player doesn't take a coin from this chest. The game finishes when all chests get emptied. Polycarpus isn't a greedy scrooge. Polycarpys is a lazy slob. So he wonders in what minimum number of moves the game can finish. Help Polycarpus, determine the minimum number of moves in which the game can finish. Note that Polycarpus counts not only his moves, he also counts Vasily's moves. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of chests with coins. The second line contains a sequence of space-separated integers: _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u20091000), where _a__i_ is the number of coins in the chest number _i_ at the beginning of the game. Output Print a single integer — the minimum number of moves needed to finish the game. If no sequence of turns leads to finishing the game, print -1. Note In the first test case there isn't a single move that can be made. That's why the players won't be able to empty the chests. In the second sample there is only one possible move _x_u2009=u20091. This move should be repeated at least 3 times to empty the third chest.
1,700
false
true
false
false
false
false
false
false
false
false
8,854
350D
Emperor Palpatine loves owls very much. The emperor has some blueprints with the new Death Star, the blueprints contain _n_ distinct segments and _m_ distinct circles. We will consider the segments indexed from 1 to _n_ in some way and the circles — indexed from 1 to _m_ in some way. Palpatine defines an owl as a set of a pair of distinct circles (_i_,u2009_j_) (_i_u2009<u2009_j_) and one segment _k_, such that: 1. circles _i_ and _j_ are symmetrical relatively to the straight line containing segment _k_; 2. circles _i_ and _j_ don't have any common points; 3. circles _i_ and _j_ have the same radius; 4. segment _k_ intersects the segment that connects the centers of circles _i_ and _j_. Help Palpatine, count the number of distinct owls on the picture. Input The first line contains two integers — _n_ and _m_ (1u2009≤u2009_n_u2009≤u20093·105, 2u2009≤u2009_m_u2009≤u20091500). The next _n_ lines contain four integers each, _x_1, _y_1, _x_2, _y_2 — the coordinates of the two endpoints of the segment. It's guaranteed that each segment has positive length. The next _m_ lines contain three integers each, _x__i_, _y__i_, _r__i_ — the coordinates of the center and the radius of the _i_-th circle. All coordinates are integers of at most 104 in their absolute value. The radius is a positive integer of at most 104. It is guaranteed that all segments and all circles are dictinct. Output Print a single number — the answer to the problem. Please, do not use the %lld specifier to output 64-bit integers is С++. It is preferred to use the cout stream or the %I64d specifier. Examples Input 1 2 3 2 3 -2 0 0 2 6 0 2 Input 3 2 0 0 0 1 0 -1 0 1 0 -1 0 0 2 0 1 -2 0 1 Input 1 2 -1 0 1 0 -100 0 1 100 0 1 Note Here's an owl from the first sample. The owl is sitting and waiting for you to count it.
2,400
false
false
false
false
true
false
false
true
true
false
8,432
1920E
Patrick calls a substring$$$^dagger$$$ of a binary string$$$^ddagger$$$ good if this substring contains exactly one 1. Help Patrick count the number of binary strings $$$s$$$ such that $$$s$$$ contains exactly $$$n$$$ good substrings and has no good substring of length strictly greater than $$$k$$$. Note that substrings are differentiated by their location in the string, so if $$$s =$$$ 1010 you should count both occurrences of 10. $$$^dagger$$$ A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. $$$^ddagger$$$ A binary string is a string that only contains the characters 0 and 1. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 2500$$$) — the number of test cases. The description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 leq n leq 2500$$$, $$$1 leq k leq n$$$) — the number of required good substrings and the maximum allowed length of a good substring. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2500$$$. Output For each test case, output a single integer — the number of binary strings $$$s$$$ such that $$$s$$$ contains exactly $$$n$$$ good substrings and has no good substring of length strictly greater than $$$k$$$. Since this integer can be too large, output it modulo $$$998,244,353$$$. Example Input 6 1 1 3 2 4 2 5 4 6 2 2450 2391 Output 1 3 5 12 9 259280854 Note In the first test case, the only suitable binary string is 1. String 01 is not suitable because it contains a substring 01 with length $$$2 > 1$$$. In the second test case, suitable binary strings are 011, 110 and 111. In the third test case, suitable binary strings are 101, 0110, 0111, 1110, and 1111.
2,100
true
false
false
true
false
false
false
false
false
false
768
1486F
You are given a tree consisting of $$$n$$$ vertices, and $$$m$$$ simple vertex paths. Your task is to find how many pairs of those paths intersect at exactly one vertex. More formally you have to find the number of pairs $$$(i, j)$$$ $$$(1 leq i < j leq m)$$$ such that $$$path_i$$$ and $$$path_j$$$ have exactly one vertex in common. Input First line contains a single integer $$$n$$$ $$$(1 leq n leq 3 cdot 10^5)$$$. Next $$$n - 1$$$ lines describe the tree. Each line contains two integers $$$u$$$ and $$$v$$$ $$$(1 leq u, v leq n)$$$ describing an edge between vertices $$$u$$$ and $$$v$$$. Next line contains a single integer $$$m$$$ $$$(1 leq m leq 3 cdot 10^5)$$$. Next $$$m$$$ lines describe paths. Each line describes a path by it's two endpoints $$$u$$$ and $$$v$$$ $$$(1 leq u, v leq n)$$$. The given path is all the vertices on the shortest path from $$$u$$$ to $$$v$$$ (including $$$u$$$ and $$$v$$$). Output Output a single integer — the number of pairs of paths that intersect at exactly one vertex. Examples Input 5 1 2 1 3 1 4 3 5 4 2 3 2 4 3 4 3 5 Input 5 1 2 1 3 1 4 3 5 6 2 3 2 4 3 4 3 5 1 1 1 2 Note The tree in the first example and paths look like this. Pairs $$$(1,4)$$$ and $$$(3,4)$$$ intersect at one vertex. In the second example all three paths contain the same single vertex, so all pairs $$$(1, 2)$$$, $$$(1, 3)$$$ and $$$(2, 3)$$$ intersect at one vertex. The third example is the same as the first example with two additional paths. Pairs $$$(1,4)$$$, $$$(1,5)$$$, $$$(2,5)$$$, $$$(3,4)$$$, $$$(3,5)$$$, $$$(3,6)$$$ and $$$(5,6)$$$ intersect at one vertex.
2,600
false
false
false
true
true
false
false
false
false
false
3,247
178F2
The Smart Beaver from ABBYY has a long history of cooperating with the "Institute of Cytology and Genetics". Recently, the Institute staff challenged the Beaver with a new problem. The problem is as follows. There is a collection of _n_ proteins (not necessarily distinct). Each protein is a string consisting of lowercase Latin letters. The problem that the scientists offered to the Beaver is to select a subcollection of size _k_ from the initial collection of proteins so that the representativity of the selected subset of proteins is maximum possible. The Smart Beaver from ABBYY did some research and came to the conclusion that the representativity of a collection of proteins can be evaluated by a single number, which is simply calculated. Let's suppose we have a collection {_a_1,u2009...,u2009_a__k_} consisting of _k_ strings describing proteins. The representativity of this collection is the following value: where _f_(_x_,u2009_y_) is the length of the longest common prefix of strings _x_ and _y_; for example, _f_("abc", "abd")u2009=u20092, and _f_("ab", "bcd")u2009=u20090. Thus, the representativity of collection of proteins {"abc", "abd", "abe"} equals 6, and the representativity of collection {"aaa", "ba", "ba"} equals 2. Having discovered that, the Smart Beaver from ABBYY asked the Cup contestants to write a program that selects, from the given collection of proteins, a subcollection of size _k_ which has the largest possible value of representativity. Help him to solve this problem! Input The first input line contains two integers _n_ and _k_ (1u2009≤u2009_k_u2009≤u2009_n_), separated by a single space. The following _n_ lines contain the descriptions of proteins, one per line. Each protein is a non-empty string of no more than 500 characters consisting of only lowercase Latin letters (a...z). Some of the strings may be equal. The input limitations for getting 20 points are: 1u2009≤u2009_n_u2009≤u200920 The input limitations for getting 50 points are: 1u2009≤u2009_n_u2009≤u2009100 The input limitations for getting 100 points are: 1u2009≤u2009_n_u2009≤u20092000
2,200
false
false
false
true
false
false
false
false
true
false
9,124
1013A
There is a beautiful garden of stones in Innopolis. Its most beautiful place is the $$$n$$$ piles with stones numbered from $$$1$$$ to $$$n$$$. EJOI participants have visited this place twice. When they first visited it, the number of stones in piles was $$$x_1, x_2, ldots, x_n$$$, correspondingly. One of the participants wrote down this sequence in a notebook. They visited it again the following day, and the number of stones in piles was equal to $$$y_1, y_2, ldots, y_n$$$. One of the participants also wrote it down in a notebook. It is well known that every member of the EJOI jury during the night either sits in the room $$$108$$$ or comes to the place with stones. Each jury member who comes there either takes one stone for himself or moves one stone from one pile to another. We can assume that there is an unlimited number of jury members. No one except the jury goes to the place with stones at night. Participants want to know whether their notes can be correct or they are sure to have made a mistake. Input The first line of the input file contains a single integer $$$n$$$, the number of piles with stones in the garden ($$$1 leq n leq 50$$$). The second line contains $$$n$$$ integers separated by spaces $$$x_1, x_2, ldots, x_n$$$, the number of stones in piles recorded in the notebook when the participants came to the place with stones for the first time ($$$0 leq x_i leq 1000$$$). The third line contains $$$n$$$ integers separated by spaces $$$y_1, y_2, ldots, y_n$$$, the number of stones in piles recorded in the notebook when the participants came to the place with stones for the second time ($$$0 leq y_i leq 1000$$$). Output If the records can be consistent output "Yes", otherwise output "No" (quotes for clarity). Note In the first example, the following could have happened during the night: one of the jury members moved one stone from the second pile to the first pile, and the other jury member moved one stone from the fourth pile to the third pile. In the second example, the jury took stones from the second and fourth piles. It can be proved that it is impossible for the jury members to move and took stones to convert the first array into the second array.
800
true
false
false
false
false
false
false
false
false
false
5,621
375B
Problem - 375B - 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 implementation sortings *1600 No tag edit access → Contest materials . Formally, a submatrix of matrix _a_ is a group of four integers _d_,u2009_u_,u2009_l_,u2009_r_ (1u2009≤u2009_d_u2009≤u2009_u_u2009≤u2009_n_;xa01u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_m_). We will assume that the submatrix contains cells (_i_,u2009_j_) (_d_u2009≤u2009_i_u2009≤u2009_u_;xa0_l_u2009≤u2009_j_u2009≤u2009_r_). The area of the submatrix is the number of cells it contains. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20095000). Next _n_ lines contain _m_ characters each — matrix _a_. Matrix _a_ only contains characters: "0" and "1". Note that the elements of the matrix follow without any spaces in the lines. Output Print a single integer — the area of the maximum obtained submatrix. If we cannot obtain a matrix of numbers one, print 0. Examples Input 1 1 1 Output 1 Input 2 2 10 11 Output 2 Input 4 3 100 011 000 101 Output 2
1,600
false
false
true
true
true
false
false
false
true
false
8,330
938B
You and your friend are participating in a TV show "Run For Your Prize". At the start of the show _n_ prizes are located on a straight line. _i_-th prize is located at position _a__i_. Positions of all prizes are distinct. You start at position 1, your friend — at position 106 (and there is no prize in any of these two positions). You have to work as a team and collect all prizes in minimum possible time, in any order. You know that it takes exactly 1 second to move from position _x_ to position _x_u2009+u20091 or _x_u2009-u20091, both for you and your friend. You also have trained enough to instantly pick up any prize, if its position is equal to your current position (and the same is true for your friend). Carrying prizes does not affect your speed (or your friend's speed) at all. Now you may discuss your strategy with your friend and decide who will pick up each prize. Remember that every prize must be picked up, either by you or by your friend. What is the minimum number of seconds it will take to pick up all the prizes? Input The first line contains one integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of prizes. The second line contains _n_ integers _a_1, _a_2, ..., _a__n_ (2u2009≤u2009_a__i_u2009≤u2009106u2009-u20091) — the positions of the prizes. No two prizes are located at the same position. Positions are given in ascending order. Output Print one integer — the minimum number of seconds it will take to collect all prizes. Note In the first example you take all the prizes: take the first at 1, the second at 2 and the third at 8. In the second example you take the first prize in 1 second and your friend takes the other in 5 seconds, you do this simultaneously, so the total time is 5.
1,100
false
true
false
false
false
false
true
false
false
false
5,950
2031E
With just a few hours left until Penchick and Chloe leave for Singapore, they could hardly wait to see the towering trees at the Singapore Botanic Gardens! Attempting to contain their excitement, Penchick crafted a rooted tree to keep Chloe and himself busy. Penchick has a rooted tree$$$^{ ext{∗}}$$$ consisting of $$$n$$$ vertices, numbered from $$$1$$$ to $$$n$$$, with vertex $$$1$$$ as the root, and Chloe can select a non-negative integer $$$d$$$ to create a perfect binary tree$$$^{ ext{†}}$$$ of depth $$$d$$$. Since Penchick and Chloe are good friends, Chloe wants her tree to be isomorphic$$$^{ ext{‡}}$$$ to Penchick's tree. To meet this condition, Chloe can perform the following operation on her own tree any number of times: Select an edge $$$(u,v)$$$, where $$$u$$$ is the parent of $$$v$$$. Remove vertex $$$v$$$ and all the edges connected to $$$v$$$, then connect all of $$$v$$$'s previous children directly to $$$u$$$. In particular, doing an operation on an edge $$$(u, v)$$$ where $$$v$$$ is a leaf will delete vertex $$$v$$$ without adding any new edges. Since constructing a perfect binary tree can be time-consuming, Chloe wants to choose the minimum $$$d$$$ such that a perfect binary tree of depth $$$d$$$ can be made isomorphic to Penchick's tree using the above operation. Note that she can't change the roots of the trees. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 10^6$$$)xa0— the number of vertices in Penchick's tree. The second line of each test case contains $$$n-1$$$ integers $$$p_2, p_3, ldots, p_n$$$ ($$$1 leq p_i leq i-1$$$)xa0— the parent of vertex $$$i$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$. Note For the first test case, create a perfect binary tree with depth $$$2$$$. Consider carrying out the operation on edge $$$AC$$$. Then the edges $$$AC$$$, $$$CF$$$, and $$$CG$$$ are removed, and edges $$$AF$$$ and $$$AG$$$ are added. The resulting tree is isomorphic to the tree given in the input. It can be proven that no sequence of operations carried out on a binary tree of depth less than $$$2$$$ can lead to a tree isomorphic to the tree given in the input. In the second test case, the tree is already isomorphic to a perfect binary tree of depth $$$3$$$.
2,100
true
true
true
true
true
false
false
false
true
false
76
804E
Problem - 804E - 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 *3100 No tag edit access → Contest materials , where _i_u2009<u2009_j_, exactly once. MoJaK doesn't like to upset Sajjad. Given the permutation, determine whether it is possible to swap all pairs of positions so that the permutation stays the same. If it is possible find how to do that. Input The first line contains single integer _n_ (1u2009≤u2009_n_u2009≤u20091000)xa0— the size of the permutation. As the permutation is not important, you can consider _a__i_u2009=u2009_i_, where the permutation is _a_1,u2009_a_2,u2009...,u2009_a__n_. Output If it is not possible to swap all pairs of positions so that the permutation stays the same, print "NO", Otherwise print "YES", then print lines: the _i_-th of these lines should contain two integers _a_ and _b_ (_a_u2009<u2009_b_)xa0— the positions where the _i_-th swap is performed. Examples Input 3 Output NO Input 1 Output YES
3,100
false
false
false
false
false
true
false
false
false
false
6,541
1184A3
Now Heidi is ready to crack Madame Kovarian's hashing function. Madame Kovarian has a very strict set of rules for name changes. Two names can be interchanged only if using the following hashing function on them results in a collision. However, the hashing function is parametrized, so one can always find a set of parameters that causes such a collision. Heidi decided to exploit this to her advantage. Given two strings $$$w_1$$$, $$$w_2$$$ of equal length $$$n$$$ consisting of lowercase English letters and an integer $$$m$$$. Consider the standard polynomial hashing function: $$$H_p(w) := left( sum_{i=0}^{w-1} w_i r^i ight) mbox{mod}(p)$$$ where $$$p$$$ is some prime, and $$$r$$$ is some number such that $$$2leq r leq p-2$$$. The goal is to find $$$r$$$ and a prime $$$p$$$ ($$$m leq p leq 10^9$$$) such that $$$H_p(w_1) = H_p(w_2)$$$. Strings $$$w_1$$$ and $$$w_2$$$ are sampled independently at random from all strings of length $$$n$$$ over lowercase English letters. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$10 le n le 10^5$$$, $$$2 le m le 10^5$$$). The second and the third line, respectively, contain the words $$$w_1$$$, $$$w_2$$$ that were sampled independently at random from all strings of length $$$n$$$ over lowercase English letters. Output Output integers $$$p, r$$$. $$$p$$$ should be a prime in the range $$$[m, 10^9]$$$ and $$$r$$$ should be an integer satisfying $$$rin [2,p-2]$$$. At least one solution is guaranteed to exist. In case multiple solutions exist, print any of them. Examples Input 10 5 bgcbaaaaaa cccaaaaaaa Input 10 100 melodypond riversongg Note In the first example, note that even though $$$p=3$$$ and $$$r=2$$$ also causes a colision of hashes, it is not a correct solution, since $$$m$$$ is $$$5$$$ and thus we want $$$pgeq 5$$$. In the second example, we are aware of the extra 'g' at the end. We just didn't realize that "River Song" and "Melody Pond" have different lengths...
3,100
true
false
false
false
false
false
false
false
false
false
4,792
846B
Polycarp takes part in a math show. He is given _n_ tasks, each consists of _k_ subtasks, numbered 1 through _k_. It takes him _t__j_ minutes to solve the _j_-th subtask of any task. Thus, time required to solve a subtask depends only on its index, but not on the task itself. Polycarp can solve subtasks in any order. By solving subtask of arbitrary problem he earns one point. Thus, the number of points for task is equal to the number of solved subtasks in it. Moreover, if Polycarp completely solves the task (solves all _k_ of its subtasks), he recieves one extra point. Thus, total number of points he recieves for the complete solution of the task is _k_u2009+u20091. Polycarp has _M_ minutes of time. What is the maximum number of points he can earn? Input The first line contains three integer numbers _n_, _k_ and _M_ (1u2009≤u2009_n_u2009≤u200945, 1u2009≤u2009_k_u2009≤u200945, 0u2009≤u2009_M_u2009≤u20092·109). The second line contains _k_ integer numbers, values _t__j_ (1u2009≤u2009_t__j_u2009≤u20091000000), where _t__j_ is the time in minutes required to solve _j_-th subtask of any task. Output Print the maximum amount of points Polycarp can earn in _M_ minutes. Note In the first example Polycarp can complete the first task and spend 1u2009+u20092u2009+u20093u2009+u20094u2009=u200910 minutes. He also has the time to solve one subtask of the second task in one minute. In the second example Polycarp can solve the first subtask of all five tasks and spend 5·1u2009=u20095 minutes. Also he can solve the second subtasks of two tasks and spend 2·2u2009=u20094 minutes. Thus, he earns 5u2009+u20092u2009=u20097 points in total.
1,800
false
true
false
false
false
false
true
false
false
false
6,376
1096A
Problem - 1096A - 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 implementation math *800 No tag edit access → Contest materials $$$ that $$$l le x, y le r$$$, $$$x e y$$$ and $$$x$$$ divides $$$y$$$. If there are multiple answers, print any of them. You are also asked to answer $$$T$$$ independent queries. Input The first line contains a single integer $$$T$$$ ($$$1 le T le 1000$$$) — the number of queries. Each of the next $$$T$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 le l le r le 998244353$$$) — inclusive borders of the range. It is guaranteed that testset only includes queries, which have at least one suitable pair. Output Print $$$T$$$ lines, each line should contain the answer — two integers $$$x$$$ and $$$y$$$ such that $$$l le x, y le r$$$, $$$x e y$$$ and $$$x$$$ divides $$$y$$$. The answer in the $$$i$$$-th line should correspond to the $$$i$$$-th query from the input. If there are multiple answers, print any of them. Example Input 3 1 10 3 14 1 10 Output 1 7 3 9 5 10
800
true
true
true
false
false
false
false
false
false
false
5,231
1251E2
The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. There are $$$n$$$ voters, and two ways to convince each of them to vote for you. The first way to convince the $$$i$$$-th voter is to pay him $$$p_i$$$ coins. The second way is to make $$$m_i$$$ other voters vote for you, and the $$$i$$$-th voter will vote for free. Moreover, the process of such voting takes place in several steps. For example, if there are five voters with $$$m_1 = 1$$$, $$$m_2 = 2$$$, $$$m_3 = 2$$$, $$$m_4 = 4$$$, $$$m_5 = 5$$$, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: $$${5} ightarrow {1, 5} ightarrow {1, 2, 3, 5} ightarrow {1, 2, 3, 4, 5}$$$. Calculate the minimum number of coins you have to spend so that everyone votes for you. Input The first line contains one integer $$$t$$$ ($$$1 le t le 2 cdot 10^5$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of voters. The next $$$n$$$ lines contains the description of voters. $$$i$$$-th line contains two integers $$$m_i$$$ and $$$p_i$$$ ($$$1 le p_i le 10^9, 0 le m_i < n$$$). It is guaranteed that the sum of all $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case print one integer — the minimum number of coins you have to spend so that everyone votes for you. Example Input 3 3 1 5 2 10 2 8 7 0 1 3 1 1 1 6 1 1 1 4 1 4 1 6 2 6 2 3 2 8 2 7 4 4 5 5 Note In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: $$${3} ightarrow {1, 3} ightarrow {1, 2, 3}$$$. In the second example you don't need to buy votes. The set of people voting for you will change as follows: $$${1} ightarrow {1, 3, 5} ightarrow {1, 2, 3, 5} ightarrow {1, 2, 3, 5, 6, 7} ightarrow {1, 2, 3, 4, 5, 6, 7}$$$. In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: $$${2, 5} ightarrow {1, 2, 3, 4, 5} ightarrow {1, 2, 3, 4, 5, 6}$$$.
2,400
false
true
false
false
true
false
false
true
false
false
4,436
1036B
Mikhail walks on a Cartesian plane. He starts at the point $$$(0, 0)$$$, and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point $$$(0, 0)$$$, he can go to any of the following points in one move: $$$(1, 0)$$$; $$$(1, 1)$$$; $$$(0, 1)$$$; $$$(-1, 1)$$$; $$$(-1, 0)$$$; $$$(-1, -1)$$$; $$$(0, -1)$$$; $$$(1, -1)$$$. If Mikhail goes from the point $$$(x1, y1)$$$ to the point $$$(x2, y2)$$$ in one move, and $$$x1 e x2$$$ and $$$y1 e y2$$$, then such a move is called a diagonal move. Mikhail has $$$q$$$ queries. For the $$$i$$$-th query Mikhail's target is to go to the point $$$(n_i, m_i)$$$ from the point $$$(0, 0)$$$ in exactly $$$k_i$$$ moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point $$$(0, 0)$$$ to the point $$$(n_i, m_i)$$$ in $$$k_i$$$ moves. Note that Mikhail can visit any point any number of times (even the destination point!). Input The first line of the input contains one integer $$$q$$$ ($$$1 le q le 10^4$$$) — the number of queries. Then $$$q$$$ lines follow. The $$$i$$$-th of these $$$q$$$ lines contains three integers $$$n_i$$$, $$$m_i$$$ and $$$k_i$$$ ($$$1 le n_i, m_i, k_i le 10^{18}$$$) — $$$x$$$-coordinate of the destination point of the query, $$$y$$$-coordinate of the destination point of the query and the number of moves in the query, correspondingly. Output Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to -1 if Mikhail cannot go from the point $$$(0, 0)$$$ to the point $$$(n_i, m_i)$$$ in exactly $$$k_i$$$ moves described above. Otherwise the $$$i$$$-th integer should be equal to the the maximum number of diagonal moves among all possible movements. Note One of the possible answers to the first test case: $$$(0, 0) o (1, 0) o (1, 1) o (2, 2)$$$. One of the possible answers to the second test case: $$$(0, 0) o (0, 1) o (1, 2) o (0, 3) o (1, 4) o (2, 3) o (3, 2) o (4, 3)$$$. In the third test case Mikhail cannot reach the point $$$(10, 1)$$$ in 9 moves.
1,600
true
false
false
false
false
false
false
false
false
false
5,522
1837C
You are given a string $$$s$$$ consisting of the characters 0, 1 and/or ?. Let's call it a pattern. Let's say that the binary string (a string where each character is either 0 or 1) matches the pattern if you can replace each character ? with 0 or 1 (for each character, the choice is independent) so that the strings become equal. For example, 0010 matches ?01?, but 010 doesn't match 1??, ??, or ????. Let's define the cost of the binary string as the minimum number of operations of the form "reverse an arbitrary contiguous substring of the string" required to sort the string in non-descending order. You have to find a binary string with the minimum possible cost among those that match the given pattern. If there are multiple answers, print any of them. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 3 cdot 10^4$$$)xa0— the number of test cases. The first and only line of each test case contains the string $$$s$$$ ($$$1 le s le 3 cdot 10^5$$$) consisting of characters 0, 1, and/or ?. The sum of the string lengths over all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case, print a binary string with the minimum possible cost among those that match the given pattern. If there are multiple answers, print any of them. Example Input 4 ??01? 10100 1??10? 0?1?10?10 Output 00011 10100 111101 011110010 Note In the first test case of the example, the cost of the resulting string is $$$0$$$. In the second test case, the cost of the resulting string is $$$2$$$: we can reverse the substring from the $$$1$$$-st character to the $$$5$$$-th character, and we obtain the string 00101. Then we reverse the substring from the $$$3$$$-rd to the $$$4$$$-th character, and we obtain the string 00011, which is sorted in non-descending order.
1,000
false
true
false
false
false
true
false
false
false
false
1,268
710E
Problem - 710E - Codeforces =============== xa0 — the number of letters 'a' in the input file and the parameters from the problem statement. Output Print the only integer _t_ — the minimum amount of time needed to generate the input file. Examples Input 8 1 1 Output 4 Input 8 1 10 Output 8
2,000
false
false
false
true
false
false
false
false
false
false
6,978
1575E
There is a city park represented as a tree with $$$n$$$ attractions as its vertices and $$$n - 1$$$ rails as its edges. The $$$i$$$-th attraction has happiness value $$$a_i$$$. Each rail has a color. It is either black if $$$t_i = 0$$$, or white if $$$t_i = 1$$$. Black trains only operate on a black rail track, and white trains only operate on a white rail track. If you are previously on a black train and want to ride a white train, or you are previously on a white train and want to ride a black train, you need to use $$$1$$$ ticket. The path of a tour must be a simple path — it must not visit an attraction more than once. You do not need a ticket the first time you board a train. You only have $$$k$$$ tickets, meaning you can only switch train types at most $$$k$$$ times. In particular, you do not need a ticket to go through a path consisting of one rail color. Define $$$f(u, v)$$$ as the sum of happiness values of the attractions in the tour $$$(u, v)$$$, which is a simple path that starts at the $$$u$$$-th attraction and ends at the $$$v$$$-th attraction. Find the sum of $$$f(u,v)$$$ for all valid tours $$$(u, v)$$$ ($$$1 leq u leq v leq n$$$) that does not need more than $$$k$$$ tickets, modulo $$$10^9 + 7$$$. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 leq n leq 2 cdot 10^5$$$, $$$0 leq k leq n-1$$$) — the number of attractions in the city park and the number of tickets you have. The second line contains $$$n$$$ integers $$$a_1, a_2,ldots, a_n$$$ ($$$0 leq a_i leq 10^9$$$) — the happiness value of each attraction. The $$$i$$$-th of the next $$$n - 1$$$ lines contains three integers $$$u_i$$$, $$$v_i$$$, and $$$t_i$$$ ($$$1 leq u_i, v_i leq n$$$, $$$0 leq t_i leq 1$$$) — an edge between vertices $$$u_i$$$ and $$$v_i$$$ with color $$$t_i$$$. The given edges form a tree. Output Output an integer denoting the total happiness value for all valid tours $$$(u, v)$$$ ($$$1 leq u leq v leq n$$$), modulo $$$10^9 + 7$$$.
2,600
false
false
false
false
true
false
false
false
false
false
2,764
1846G
A new virus called "CodeVid-23" has spread among programmers. Rudolf, being a programmer, was not able to avoid it. There are $$$n$$$ symptoms numbered from $$$1$$$ to $$$n$$$ that can appear when infected. Initially, Rudolf has some of them. He went to the pharmacy and bought $$$m$$$ medicines. For each medicine, the number of days it needs to be taken is known, and the set of symptoms it removes. Unfortunately, medicines often have side effects. Therefore, for each medicine, the set of symptoms that appear when taking it is also known. After reading the instructions, Rudolf realized that taking more than one medicine at a time is very unhealthy. Rudolph wants to be healed as soon as possible. Therefore, he asks you to calculate the minimum number of days to remove all symptoms, or to say that it is impossible. Input The first line contains a single integer $$$t$$$ $$$(1 le t le 100)$$$ — the number of test cases. Then follow the descriptions of the test cases. The first line of each test case contains two integers $$$n, m$$$ $$$(1 le n le 10, 1 le m le 10^3)$$$ — the number of symptoms and medicines, respectively. The second line of each test case contains a string of length $$$n$$$ consisting of the characters $$$0$$$ and $$$1$$$ — the description of Rudolf's symptoms. If the $$$i$$$-th character of the string is $$$1$$$, Rudolf has the $$$i$$$-th symptom, otherwise he does not. Then follow $$$3 cdot m$$$ lines — the description of the medicines. The first line of each medicine description contains an integer $$$d$$$ $$$(1 le d le 10^3)$$$ — the number of days the medicine needs to be taken. The next two lines of the medicine description contain two strings of length $$$n$$$, consisting of the characters $$$0$$$ and $$$1$$$ — the description of the symptoms it removes and the description of the side effects. In the first of the two lines, $$$1$$$ at position $$$i$$$ means that the medicine removes the $$$i$$$-th symptom, and $$$0$$$ otherwise. In the second of the two lines, $$$1$$$ at position $$$i$$$ means that the $$$i$$$-th symptom appears after taking the medicine, and $$$0$$$ otherwise. Different medicines can have the same sets of symptoms and side effects. If a medicine relieves a certain symptom, it will not be among the side effects. The sum of $$$m$$$ over all test cases does not exceed $$$10^3$$$. Output For each test case, output a single integer on a separate line — the minimum number of days it will take Rudolf to remove all symptoms. If this never happens, output $$$-1$$$. Example Input 4 5 4 10011 3 10000 00110 3 00101 00000 3 01010 00100 5 11010 00100 4 1 0000 10 1011 0100 2 2 11 2 10 01 3 01 10 2 3 11 3 01 10 3 10 00 4 10 01 Note In the first example, we can first apply medicine number $$$4$$$, after which the symptoms will look like "00101". After that, medicine number $$$2$$$, then all symptoms will disappear, and the number of days will be $$$5 + 3 = 8$$$. Another option is to apply the medicines in the order $$$1, 3, 2$$$. In this case, all symptoms will also disappear, but the number of days will be $$$3 + 3 + 3 = 9$$$. In the second example, there are no symptoms initially, so the treatment will take $$$0$$$ days. In the third example, there are no options to remove all symptoms.
1,900
false
true
false
true
false
false
false
false
false
true
1,201
1827C
A substring is a continuous and non-empty segment of letters from a given string, without any reorders. An even palindrome is a string that reads the same backward as forward and has an even length. For example, strings "zz", "abba", "abccba" are even palindromes, but strings "codeforces", "reality", "aba", "c" are not. A beautiful string is an even palindrome or a string that can be partitioned into some smaller even palindromes. You are given a string $$$s$$$, consisting of $$$n$$$ lowercase Latin letters. Count the number of beautiful substrings of $$$s$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 5cdot 10^5$$$). The second line of each test case contains a string $$$s$$$. String $$$s$$$ consists of only lowercase Latin letters and has a length of $$$n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5cdot 10^5$$$. Output For each test case print the number of beautiful substrings. Example Input 6 6 abaaba 1 a 2 aa 6 abcdef 12 accabccbacca 6 abbaaa Note In the first test case, the beautiful substrings are "abaaba", "baab", "aa". In the last test case, the beautiful substrings are "aa" (counted twice), "abba", "bb", "bbaa", "abbaaa".
2,600
false
false
false
true
true
false
true
true
false
false
1,320
613E
Oleg Petrov loves crossword puzzles and every Thursday he buys his favorite magazine with crosswords and other word puzzles. In the last magazine Oleg found a curious puzzle, and the magazine promised a valuable prize for it's solution. We give a formal description of the problem below. The puzzle field consists of two rows, each row contains _n_ cells. Each cell contains exactly one small English letter. You also are given a word _w_, which consists of _k_ small English letters. A solution of the puzzle is a sequence of field cells _c_1, ..., _c__k_, such that: For all _i_ from 1 to _k_ the letter written in the cell _c__i_ matches the letter _w__i_; All the cells in the sequence are pairwise distinct; For all _i_ from 1 to _k_u2009-u20091 cells _c__i_ and _c__i_u2009+u20091 have a common side. Oleg Petrov quickly found a solution for the puzzle. Now he wonders, how many distinct solutions are there for this puzzle. Oleg Petrov doesn't like too large numbers, so calculate the answer modulo 109u2009+u20097. Two solutions _c__i_ and _c_'_i_ are considered distinct if the sequences of cells do not match in at least one position, that is there is such _j_ in range from 1 to _k_, such that _c__j_u2009≠u2009_c_'_j_. Input The first two lines contain the state of the field for the puzzle. Each of these non-empty lines contains exactly _n_ small English letters. The next line is left empty. The next line is non-empty and contains word _w_, consisting of small English letters. The length of each line doesn't exceed 2u2009000. Output Print a single integer — the number of distinct solutions for the puzzle modulo 109u2009+u20097.
3,200
false
false
false
true
false
false
false
false
false
false
7,383
1369D
# TediousLee Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 256 megabytes Lee tried so hard to make a good div.2 D problem to balance his recent contest, but it still doesn’t feel good at all. Lee invented it so tediously slow that he managed to develop a phobia about div.2 D problem setting instead. And now he is hiding behind the bushes. . . Let’s define a Rooted Dead Bush (RDB) of level n as a rooted tree constructed as described below. A rooted dead bush of level 1 is a single vertex. To construct an RDB of level i we, at first, construct an RDB of level i − 1, then for each vertex u: • if u has no children then we will add a single child to it; • if u has one child then we will add two children to it; • if u has more than one child, then we will skip it. > Rooted Dead Bushes of level 1,2and 3. Let’s define a claw as a rooted tree with four vertices: one root vertex (called also as center) with three children. It looks like a claw: Page 1 of 3 The center of the claw is the vertex with label 1. Lee has a Rooted Dead Bush of level n. Initially, all vertices of his RDB are green. In one move, he can choose a claw in his RDB, if all vertices in the claw are green and all vertices of the claw are children of its center, then he colors the claw’s vertices in yellow. He’d like to know the maximum number of yellow vertices he can achieve. Since the answer might be very large, print it modulo 10 9 + 7 . # Input The first line contains one integer t (1 ≤ t ≤ 10 4) x16 the number of test cases. Next t lines contain test cases x16 one per line. The first line of each test case contains one integer n (1 ≤ n ≤ 2 · 10 6) x16 the level of Lee’s RDB. # Output For each test case, print a single integer x16 the maximum number of yellow vertices Lee can make modulo 10 9 + 7 . # Example standard input standard output 712345100 2000000 004412 990998587 804665184 # Note It’s easy to see that the answer for RDB of level 1 or 2 is 0.The answer for RDB of level 3 is 4 since there is only one claw we can choose: {1, 2, 3, 4}.The answer for RDB of level 4 is 4 since we can choose either single claw {1, 3, 2, 4} or single claw {2, 7, 5, 6}. There are no other claws in the RDB of level 4 (for example, we can’t choose {2, 1, 7, 6}, since 1 is not a child of center vertex 2). Page 2 of 3 Rooted Dead Bush of level 4. Page 3 of 3
1,900
true
true
false
true
false
false
false
false
false
true
3,824
1624D
You have a string $$$s$$$ consisting of lowercase Latin alphabet letters. You can color some letters in colors from $$$1$$$ to $$$k$$$. It is not necessary to paint all the letters. But for each color, there must be a letter painted in that color. Then you can swap any two symbols painted in the same color as many times as you want. After that, $$$k$$$ strings will be created, $$$i$$$-th of them will contain all the characters colored in the color $$$i$$$, written in the order of their sequence in the string $$$s$$$. Your task is to color the characters of the string so that all the resulting $$$k$$$ strings are palindromes, and the length of the shortest of these $$$k$$$ strings is as large as possible. Read the note for the first test case of the example if you need a clarification. Recall that a string is a palindrome if it reads the same way both from left to right and from right to left. For example, the strings abacaba, cccc, z and dxd are palindromes, but the strings abab and aaabaaxa0— are not. Input The first line of input data contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of input data sets in the test. The descriptions of the input data sets follow. The first line of the description of each input data set contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 2 cdot 10^5$$$)xa0— the length of the string and the number of colors in which its letters can be painted. The second line of the description of each input data set contains a string $$$s$$$ of length $$$n$$$ consisting of lowercase letters of the Latin alphabet. It is guaranteed that the sum of n over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each set of input data, output a single integer xa0— the maximum length of the shortest palindrome string that can be obtained. Example Input 10 8 2 bxyaxzay 6 3 aaaaaa 6 1 abcdef 6 6 abcdef 3 2 dxd 11 2 abcabcabcac 6 6 sipkic 7 2 eatoohd 3 1 llw 6 2 bfvfbv Output 3 2 1 1 1 5 1 1 3 3 Note In the first test case, $$$s$$$="bxyaxzay", $$$k=2$$$. We use indices in the string from $$$1$$$ to $$$8$$$. The following coloring will work: $$$mathtt{mathbf{color{red}{b}color{blue}{xy}color{red}{a}color{blue}{x}zcolor{red}{a}color{blue}{y}}}$$$ (the letter z remained uncolored). After painting: swap two red characters (with the indices $$$1$$$ and $$$4$$$), we get $$$mathtt{mathbf{color{red}{a}color{blue}{xy}color{red}{b}color{blue}{x}zcolor{red}{a}color{blue}{y}}}$$$; swap two blue characters (with the indices $$$5$$$ and $$$8$$$), we get $$$mathtt{mathbf{color{red}{a}color{blue}{xy}color{red}{b}color{blue}{y}zcolor{red}{a}color{blue}{x}}}$$$. Now, for each of the two colors we write out the corresponding characters from left to right, we get two strings $$$mathtt{mathbf{color{red}{aba}}}$$$ and $$$mathtt{mathbf{color{blue}{xyyx}}}$$$. Both of them are palindromes, the length of the shortest is $$$3$$$. It can be shown that the greatest length of the shortest palindrome cannot be achieved. In the second set of input data, the following coloring is suitable: $$$[1, 1, 2, 2, 3, 3]$$$. There is no need to swap characters. Both received strings are equal to aa, they are palindromes and their length is $$$2$$$. In the third set of input data, you can color any character and take it into a string. In the fourth set of input data, you can color the $$$i$$$th character in the color $$$i$$$. In the fifth set of input data can be colored in each of the colors of one character. In the sixth set of input data, the following coloring is suitable: $$$[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 0]$$$. Rearrange the characters so as to get the palindromes abcba and acbca.
1,400
false
true
false
false
false
false
false
true
true
false
2,497
424A
Problem - 424A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation *900 No tag edit access → Contest materials Editorial") came to work out. The hamsters lined up and each hamster either sat down or stood up. For another exercise, Pasha needs exactly hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster ether sit down or stand up. How many minutes will he need to get what he wants if he acts optimally well? Input The first line contains integer _n_ (2u2009≤u2009_n_u2009≤u2009200; _n_ is even). The next line contains _n_ characters without spaces. These characters describe the hamsters' position: the _i_-th character equals 'X', if the _i_-th hamster in the row is standing, and 'x', if he is sitting. Output In the first line, print a single integer — the minimum required number of minutes. In the second line, print a string that describes the hamsters' position after Pasha makes the required changes. If there are multiple optimal positions, print any of them. Examples Input 4 xxXx Output 1 XxXx Input 2 XX Output 1 xX Input 6 xXXxXx Output 0 xXXxXx
900
false
false
true
false
false
false
false
false
false
false
8,143
1422A
Yura is tasked to build a closed fence in shape of an arbitrary non-degenerate simple quadrilateral. He's already got three straight fence segments with known lengths $$$a$$$, $$$b$$$, and $$$c$$$. Now he needs to find out some possible integer length $$$d$$$ of the fourth straight fence segment so that he can build the fence using these four segments. In other words, the fence should have a quadrilateral shape with side lengths equal to $$$a$$$, $$$b$$$, $$$c$$$, and $$$d$$$. Help Yura, find any possible length of the fourth side. A non-degenerate simple quadrilateral is such a quadrilateral that no three of its corners lie on the same line, and it does not cross itself. Input The first line contains a single integer $$$t$$$xa0— the number of test cases ($$$1 le t le 1000$$$). The next $$$t$$$ lines describe the test cases. Each line contains three integers $$$a$$$, $$$b$$$, and $$$c$$$xa0— the lengths of the three fence segments ($$$1 le a, b, c le 10^9$$$). Output For each test case print a single integer $$$d$$$xa0— the length of the fourth fence segment that is suitable for building the fence. If there are multiple answers, print any. We can show that an answer always exists. Note We can build a quadrilateral with sides $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$. We can build a quadrilateral with sides $$$12$$$, $$$34$$$, $$$56$$$, $$$42$$$.
800
true
false
false
false
false
false
false
false
false
false
3,569
1037C
You are given two binary strings $$$a$$$ and $$$b$$$ of the same length. You can perform the following two operations on the string $$$a$$$: Swap any two bits at indices $$$i$$$ and $$$j$$$ respectively ($$$1 le i, j le n$$$), the cost of this operation is $$$i - j$$$, that is, the absolute difference between $$$i$$$ and $$$j$$$. Select any arbitrary index $$$i$$$ ($$$1 le i le n$$$) and flip (change $$$0$$$ to $$$1$$$ or $$$1$$$ to $$$0$$$) the bit at this index. The cost of this operation is $$$1$$$. Find the minimum cost to make the string $$$a$$$ equal to $$$b$$$. It is not allowed to modify string $$$b$$$. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10^6$$$)xa0— the length of the strings $$$a$$$ and $$$b$$$. The second and third lines contain strings $$$a$$$ and $$$b$$$ respectively. Both strings $$$a$$$ and $$$b$$$ have length $$$n$$$ and contain only '0' and '1'. Output Output the minimum cost to make the string $$$a$$$ equal to $$$b$$$. Note In the first example, one of the optimal solutions is to flip index $$$1$$$ and index $$$3$$$, the string $$$a$$$ changes in the following way: "100" $$$ o$$$ "000" $$$ o$$$ "001". The cost is $$$1 + 1 = 2$$$. The other optimal solution is to swap bits and indices $$$1$$$ and $$$3$$$, the string $$$a$$$ changes then "100" $$$ o$$$ "001", the cost is also $$$1 - 3 = 2$$$. In the second example, the optimal solution is to swap bits at indices $$$2$$$ and $$$3$$$, the string $$$a$$$ changes as "0101" $$$ o$$$ "0011". The cost is $$$2 - 3 = 1$$$.
1,300
false
true
false
true
false
false
false
false
false
false
5,514
659C
In Berland recently a new collection of toys went on sale. This collection consists of 109 types of toys, numbered with integers from 1 to 109. A toy from the new collection of the _i_-th type costs _i_ bourles. Tania has managed to collect _n_ different types of toys _a_1,u2009_a_2,u2009...,u2009_a__n_ from the new collection. Today is Tanya's birthday, and her mother decided to spend no more than _m_ bourles on the gift to the daughter. Tanya will choose several different types of toys from the new collection as a gift. Of course, she does not want to get a type of toy which she already has. Tanya wants to have as many distinct types of toys in her collection as possible as the result. The new collection is too diverse, and Tanya is too little, so she asks you to help her in this. Input The first line contains two integers _n_ (1u2009≤u2009_n_u2009≤u2009100u2009000) and _m_ (1u2009≤u2009_m_u2009≤u2009109)xa0— the number of types of toys that Tanya already has and the number of bourles that her mom is willing to spend on buying new toys. The next line contains _n_ distinct integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109)xa0— the types of toys that Tanya already has. Output In the first line print a single integer _k_xa0— the number of different types of toys that Tanya should choose so that the number of different types of toys in her collection is maximum possible. Of course, the total cost of the selected toys should not exceed _m_. In the second line print _k_ distinct space-separated integers _t_1,u2009_t_2,u2009...,u2009_t__k_ (1u2009≤u2009_t__i_u2009≤u2009109)xa0— the types of toys that Tanya should choose. If there are multiple answers, you may print any of them. Values of _t__i_ can be printed in any order. Note In the first sample mom should buy two toys: one toy of the 2-nd type and one toy of the 5-th type. At any other purchase for 7 bourles (assuming that the toys of types 1, 3 and 4 have already been bought), it is impossible to buy two and more toys.
1,200
false
true
true
false
false
false
false
false
false
false
7,187
1280C
Welcome! Everything is fine. You have arrived in The Medium Place, the place between The Good Place and The Bad Place. You are assigned a task that will either make people happier or torture them for eternity. You have a list of $$$k$$$ pairs of people who have arrived in a new inhabited neighborhood. You need to assign each of the $$$2k$$$ people into one of the $$$2k$$$ houses. Each person will be the resident of exactly one house, and each house will have exactly one resident. Of course, in the neighborhood, it is possible to visit friends. There are $$$2k - 1$$$ roads, each of which connects two houses. It takes some time to traverse a road. We will specify the amount of time it takes in the input. The neighborhood is designed in such a way that from anyone's house, there is exactly one sequence of distinct roads you can take to any other house. In other words, the graph with the houses as vertices and the roads as edges is a tree. The truth is, these $$$k$$$ pairs of people are actually soulmates. We index them from $$$1$$$ to $$$k$$$. We denote by $$$f(i)$$$ the amount of time it takes for the $$$i$$$-th pair of soulmates to go to each other's houses. As we have said before, you will need to assign each of the $$$2k$$$ people into one of the $$$2k$$$ houses. You have two missions, one from the entities in The Good Place and one from the entities of The Bad Place. Here they are: The first mission, from The Good Place, is to assign the people into the houses such that the sum of $$$f(i)$$$ over all pairs $$$i$$$ is minimized. Let's define this minimized sum as $$$G$$$. This makes sure that soulmates can easily and efficiently visit each other; The second mission, from The Bad Place, is to assign the people into the houses such that the sum of $$$f(i)$$$ over all pairs $$$i$$$ is maximized. Let's define this maximized sum as $$$B$$$. This makes sure that soulmates will have a difficult time to visit each other. What are the values of $$$G$$$ and $$$B$$$? Input The first line of input contains a single integer $$$t$$$ ($$$1 le t le 500$$$) denoting the number of test cases. The next lines contain descriptions of the test cases. The first line of each test case contains a single integer $$$k$$$ denoting the number of pairs of people ($$$1 le k le 10^5$$$). The next $$$2k - 1$$$ lines describe the roads; the $$$i$$$-th of them contains three space-separated integers $$$a_i, b_i, t_i$$$ which means that the $$$i$$$-th road connects the $$$a_i$$$-th and $$$b_i$$$-th houses with a road that takes $$$t_i$$$ units of time to traverse ($$$1 le a_i, b_i le 2k$$$, $$$a_i eq b_i$$$, $$$1 le t_i le 10^6$$$). It is guaranteed that the given roads define a tree structure. It is guaranteed that the sum of the $$$k$$$ in a single file is at most $$$3 cdot 10^5$$$. Note For the sample test case, we have a minimum sum equal to $$$G = 15$$$. One way this can be achieved is with the following assignment: The first pair of people get assigned to houses $$$5$$$ and $$$6$$$, giving us $$$f(1) = 5$$$; The second pair of people get assigned to houses $$$1$$$ and $$$4$$$, giving us $$$f(2) = 6$$$; The third pair of people get assigned to houses $$$3$$$ and $$$2$$$, giving us $$$f(3) = 4$$$. Note that the sum of the $$$f(i)$$$ is $$$5 + 6 + 4 = 15$$$. We also have a maximum sum equal to $$$B = 33$$$. One way this can be achieved is with the following assignment: The first pair of people get assigned to houses $$$1$$$ and $$$4$$$, giving us $$$f(1) = 6$$$; The second pair of people get assigned to houses $$$6$$$ and $$$2$$$, giving us $$$f(2) = 14$$$; The third pair of people get assigned to houses $$$3$$$ and $$$5$$$, giving us $$$f(3) = 13$$$. Note that the sum of the $$$f(i)$$$ is $$$6 + 14 + 13 = 33$$$.
2,000
false
true
false
false
false
false
false
false
false
true
4,292
1770B
Reve has two integers $$$n$$$ and $$$k$$$. Let $$$p$$$ be a permutation$$$^dagger$$$ of length $$$n$$$. Let $$$c$$$ be an array of length $$$n - k + 1$$$ such that $$$$$$c_i = max(p_i, dots, p_{i+k-1}) + min(p_i, dots, p_{i+k-1}).$$$$$$ Let the cost of the permutation $$$p$$$ be the maximum element of $$$c$$$. Koxia wants you to construct a permutation with the minimum possible cost. $$$^dagger$$$ A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 2000$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 leq k leq n leq 2 cdot 10^5$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output $$$n$$$ integers $$$p_1,p_2,dots,p_n$$$, which is a permutation with minimal cost. If there is more than one permutation with minimal cost, you may output any of them. Example Output 5 1 2 3 4 1 2 3 4 5 3 2 4 1 6 5 Note In the first test case, $$$c_1 = max(p_1,p_2,p_3) + min(p_1,p_2,p_3) = 5 + 1 = 6$$$. $$$c_2 = max(p_2,p_3,p_4) + min(p_2,p_3,p_4) = 3 + 1 = 4$$$. $$$c_3 = max(p_3,p_4,p_5) + min(p_3,p_4,p_5) = 4 + 2 = 6$$$. Therefore, the cost is $$$max(6,4,6)=6$$$. It can be proven that this is the minimal cost.
1,000
false
false
false
false
false
true
false
false
false
false
1,676
1304B
Returning back to problem solving, Gildong is now studying about palindromes. He learned that a palindrome is a string that is the same as its reverse. For example, strings "pop", "noon", "x", and "kkkkkk" are palindromes, while strings "moon", "tv", and "abab" are not. An empty string is also a palindrome. Gildong loves this concept so much, so he wants to play with it. He has $$$n$$$ distinct strings of equal length $$$m$$$. He wants to discard some of the strings (possibly none or all) and reorder the remaining strings so that the concatenation becomes a palindrome. He also wants the palindrome to be as long as possible. Please help him find one. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 100$$$, $$$1 le m le 50$$$) — the number of strings and the length of each string. Next $$$n$$$ lines contain a string of length $$$m$$$ each, consisting of lowercase Latin letters only. All strings are distinct. Output In the first line, print the length of the longest palindrome string you made. In the second line, print that palindrome. If there are multiple answers, print any one of them. If the palindrome is empty, print an empty line or don't print this line at all. Examples Input 3 5 hello codef orces Input 9 4 abab baba abcd bcde cdef defg wxyz zyxw ijji Output 20 ababwxyzijjizyxwbaba Note In the first example, "battab" is also a valid answer. In the second example, there can be 4 different valid answers including the sample output. We are not going to provide any hints for what the others are. In the third example, the empty string is the only valid palindrome string.
1,100
false
true
true
false
false
true
true
false
false
false
4,170
1345A
You are given a special jigsaw puzzle consisting of $$$ncdot m$$$ identical pieces. Every piece has three tabs and one blank, as pictured below. The jigsaw puzzle is considered solved if the following conditions hold: 1. The pieces are arranged into a grid with $$$n$$$ rows and $$$m$$$ columns. 2. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece. Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle. Input The test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1le tle 1000$$$)xa0— the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. Each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n,m le 10^5$$$). Output For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower). Example Input 3 1 3 100000 100000 2 2 Note For the first test case, this is an example solution: For the second test case, we can show that no solution exists. For the third test case, this is an example solution:
800
true
false
false
false
false
false
false
false
false
false
3,981
1202A
You are given two binary strings $$$x$$$ and $$$y$$$, which are binary representations of some two integers (let's denote these integers as $$$f(x)$$$ and $$$f(y)$$$). You can choose any integer $$$k ge 0$$$, calculate the expression $$$s_k = f(x) + f(y) cdot 2^k$$$ and write the binary representation of $$$s_k$$$ in reverse order (let's denote it as $$$rev_k$$$). For example, let $$$x = 1010$$$ and $$$y = 11$$$; you've chosen $$$k = 1$$$ and, since $$$2^1 = 10_2$$$, so $$$s_k = 1010_2 + 11_2 cdot 10_2 = 10000_2$$$ and $$$rev_k = 00001$$$. For given $$$x$$$ and $$$y$$$, you need to choose such $$$k$$$ that $$$rev_k$$$ is lexicographically minimal (read notes if you don't know what does "lexicographically" means). It's guaranteed that, with given constraints, $$$k$$$ exists and is finite. Input The first line contains a single integer $$$T$$$ ($$$1 le T le 100$$$) — the number of queries. Next $$$2T$$$ lines contain a description of queries: two lines per query. The first line contains one binary string $$$x$$$, consisting of no more than $$$10^5$$$ characters. Each character is either 0 or 1. The second line contains one binary string $$$y$$$, consisting of no more than $$$10^5$$$ characters. Each character is either 0 or 1. It's guaranteed, that $$$1 le f(y) le f(x)$$$ (where $$$f(x)$$$ is the integer represented by $$$x$$$, and $$$f(y)$$$ is the integer represented by $$$y$$$), both representations don't have any leading zeroes, the total length of $$$x$$$ over all queries doesn't exceed $$$10^5$$$, and the total length of $$$y$$$ over all queries doesn't exceed $$$10^5$$$. Output Print $$$T$$$ integers (one per query). For each query print such $$$k$$$ that $$$rev_k$$$ is lexicographically minimal. Example Input 4 1010 11 10001 110 1 1 1010101010101 11110000 Note The first query was described in the legend. In the second query, it's optimal to choose $$$k = 3$$$. The $$$2^3 = 1000_2$$$ so $$$s_3 = 10001_2 + 110_2 cdot 1000_2 = 10001 + 110000 = 1000001$$$ and $$$rev_3 = 1000001$$$. For example, if $$$k = 0$$$, then $$$s_0 = 10111$$$ and $$$rev_0 = 11101$$$, but $$$rev_3 = 1000001$$$ is lexicographically smaller than $$$rev_0 = 11101$$$. In the third query $$$s_0 = 10$$$ and $$$rev_0 = 01$$$. For example, $$$s_2 = 101$$$ and $$$rev_2 = 101$$$. And $$$01$$$ is lexicographically smaller than $$$101$$$. The quote from Wikipedia: "To determine which of two strings of characters comes when arranging in lexicographical order, their first letters are compared. If they differ, then the string whose first letter comes earlier in the alphabet comes before the other string. If the first letters are the same, then the second letters are compared, and so on. If a position is reached where one string has no more letters to compare while the other does, then the first (shorter) string is deemed to come first in alphabetical order.
1,100
false
true
false
false
false
false
false
false
false
false
4,689
831A
Problem - 831A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation *1000 No tag edit access → Contest materials and the last block (decreasing) may be absent. It is allowed that both of this blocks are absent. For example, the following three arrays are unimodal:
1,000
false
false
true
false
false
false
false
false
false
false
6,442
1807C
You are given a string $$$s$$$ consisting of lowercase Latin characters. In an operation, you can take a character and replace all occurrences of this character with $$$ exttt{0}$$$ or replace all occurrences of this character with $$$ exttt{1}$$$. Is it possible to perform some number of moves so that the resulting string is an alternating binary string$$$^{dagger}$$$? For example, consider the string $$$ exttt{abacaba}$$$. You can perform the following moves: Replace $$$ exttt{a}$$$ with $$$ exttt{0}$$$. Now the string is $$$color{red}{ exttt{0}} exttt{b}color{red}{ exttt{0}} exttt{c}color{red}{ exttt{0}} exttt{b}color{red}{ exttt{0}}$$$. Replace $$$ exttt{b}$$$ with $$$ exttt{1}$$$. Now the string is $$${ exttt{0}}color{red}{ exttt{1}}{ exttt{0}} exttt{c}{ exttt{0}}color{red}{ exttt{1}}{ exttt{0}}$$$. Replace $$$ exttt{c}$$$ with $$$ exttt{1}$$$. Now the string is $$${ exttt{0}}{ exttt{1}}{ exttt{0}}color{red}{ exttt{1}}{ exttt{0}}{ exttt{1}}{ exttt{0}}$$$. This is an alternating binary string. $$$^{dagger}$$$An alternating binary string is a string of $$$ exttt{0}$$$s and $$$ exttt{1}$$$s such that no two adjacent bits are equal. For example, $$$ exttt{01010101}$$$, $$$ exttt{101}$$$, $$$ exttt{1}$$$ are alternating binary strings, but $$$ exttt{0110}$$$, $$$ exttt{0a0a0}$$$, $$$ exttt{10100}$$$ are not. Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 2000$$$)xa0— the length of the string $$$s$$$. The second line of each test case contains a string consisting of $$$n$$$ lowercase Latin charactersxa0— the string $$$s$$$. Output For each test case, output "YES" (without quotes) if you can make the string into an alternating binary string, and "NO" (without quotes) otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer). Example Input 8 7 abacaba 2 aa 1 y 4 bkpt 6 ninfia 6 banana 10 codeforces 8 testcase Output YES NO YES YES NO YES NO NO Note The first test case is explained in the statement. In the second test case, the only possible binary strings you can make are $$$ exttt{00}$$$ and $$$ exttt{11}$$$, neither of which are alternating. In the third test case, you can make $$$ exttt{1}$$$, which is an alternating binary string.
800
false
true
true
false
false
false
false
false
false
false
1,433
4C
A new e-mail service "Berlandesk" is going to be opened in Berland in the near future. The site administration wants to launch their project as soon as possible, that's why they ask you to help. You're suggested to implement the prototype of site registration system. The system should work on the following principle. Each time a new user wants to register, he sends to the system a request with his name. If such a name does not exist in the system database, it is inserted into the database, and the user gets the response OK, confirming the successful registration. If the name already exists in the system database, the system makes up a new user name, sends it to the user as a prompt and also inserts the prompt into the database. The new name is formed by the following rule. Numbers, starting with 1, are appended one after another to name (name1, name2, ...), among these numbers the least _i_ is found so that name_i_ does not yet exist in the database. Input The first line contains number _n_ (1u2009≤u2009_n_u2009≤u2009105). The following _n_ lines contain the requests to the system. Each request is a non-empty line, and consists of not more than 32 characters, which are all lowercase Latin letters. Output Print _n_ lines, which are system responses to the requests: OK in case of successful registration, or a prompt with a new name, if the requested name is already taken. Examples Input 4 abacaba acaba abacaba acab Input 6 first first second second third third Output OK first1 OK second1 OK third1
1,300
false
false
true
false
true
false
false
false
false
false
9,972
161D
Problem - 161D - Codeforces =============== xa0 of the shortest path between these vertices. You are given a tree with _n_ vertices and a positive number _k_. Find the number of distinct pairs of the vertices which have a distance of exactly _k_ between them. Note that pairs (_v_, _u_) and (_u_, _v_) are considered to be the same pair. Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u200950000, 1u2009≤u2009_k_u2009≤u2009500) — the number of vertices and the required distance between the vertices. Next _n_u2009-u20091 lines describe the edges as "_a__i_ _b__i_" (without the quotes) (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, _a__i_u2009≠u2009_b__i_), where _a__i_ and _b__i_ are the vertices connected by the _i_-th edge. All given edges are different. Output Print a single integer — the number of distinct pairs of the tree's vertices which have a distance of exactly _k_ between them. 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 5 2 1 2 2 3 3 4 2 5 Output 4 Input 5 3 1 2 2 3 3 4 4 5 Output 2 Note In the first sample the pairs of vertexes at distance 2 from each other are (1, 3), (1, 5), (3, 5) and (2, 4).
1,800
false
false
false
true
false
false
false
false
false
false
9,227
1972A
Problem - 1972A - 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 greedy two pointers *800 No tag edit access → Contest materials ") . The description of the test cases follows. The first line of each test case contains only one positive integer $$$n$$$ ($$$1 leq n leq 100$$$), representing the number of problems. The second line of each test case contains an array $$$a$$$ of length $$$n$$$ ($$$1le a_1le a_2lecdotsle a_nle 10^9$$$). The third line of each test case contains an array $$$b$$$ of length $$$n$$$ ($$$1le b_1le b_2lecdotsle b_nle 10^9$$$). Output For each test case, print an integer as your answer in a new line. Example Input 2 6 1000 1400 2000 2000 2200 2700 800 1200 1500 1800 2200 3000 6 4 5 6 7 8 9 1 2 3 4 5 6 Output 2 3 Note In the first test case: Propose a problem with difficulty $$$w=800$$$ and $$$a$$$ becomes $$$
800
false
true
false
false
false
false
true
false
false
false
457
643A
Bear Limak has _n_ colored balls, arranged in one long row. Balls are numbered 1 through _n_, from left to right. There are _n_ possible colors, also numbered 1 through _n_. The _i_-th ball has color _t__i_. For a fixed interval (set of consecutive elements) of balls we can define a dominant color. It's a color occurring the biggest number of times in the interval. In case of a tie between some colors, the one with the smallest number (index) is chosen as dominant. There are non-empty intervals in total. For each color, your task is to count the number of intervals in which this color is dominant. Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20095000)xa0— the number of balls. The second line contains _n_ integers _t_1,u2009_t_2,u2009...,u2009_t__n_ (1u2009≤u2009_t__i_u2009≤u2009_n_) where _t__i_ is the color of the _i_-th ball. Output Print _n_ integers. The _i_-th of them should be equal to the number of intervals where _i_ is a dominant color. Note In the first sample, color 2 is dominant in three intervals: An interval [2,u20092] contains one ball. This ball's color is 2 so it's clearly a dominant color. An interval [4,u20094] contains one ball, with color 2 again. An interval [2,u20094] contains two balls of color 2 and one ball of color 1. There are 7 more intervals and color 1 is dominant in all of them.
1,500
false
false
true
false
false
false
false
false
false
false
7,244
1850G
A compass points directly toward the morning star. It can only point in one of eight directions: the four cardinal directions (N, S, E, W) or some combination (NW, NE, SW, SE). Otherwise, it will break. The directions the compass can point. There are $$$n$$$ distinct points with integer coordinates on a plane. How many ways can you put a compass at one point and the morning star at another so that the compass does not break? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 leq n leq 2 cdot 10^5$$$)xa0— the number of points. Then $$$n$$$ lines follow, each line containing two integers $$$x_i$$$, $$$y_i$$$ ($$$-10^9 leq x_i, y_i leq 10^9$$$)xa0— the coordinates of each point, all points have distinct coordinates. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integerxa0— the number of pairs of points that don't break the compass. Example Input 5 3 0 0 -1 -1 1 1 4 4 5 5 7 6 9 10 13 3 -1000000000 1000000000 0 0 1000000000 -1000000000 5 0 0 2 2 -1 5 -1 10 2 11 3 0 0 -1 2 1 -2 Note In the first test case, any pair of points won't break the compass: The compass is at $$$(0,0)$$$, the morning star is at $$$(-1,-1)$$$: the compass will point $$$ ext{SW}$$$. The compass is at $$$(0,0)$$$, the morning star is at $$$(1,1)$$$: the compass will point $$$ ext{NE}$$$. The compass is at $$$(-1,-1)$$$, the morning star is at $$$(0,0)$$$: the compass will point $$$ ext{NE}$$$. The compass is at $$$(-1,-1)$$$, the morning star is at $$$(1,1)$$$: the compass will point $$$ ext{NE}$$$. The compass is at $$$(1,1)$$$, the morning star is at $$$(0,0)$$$: the compass will point $$$ ext{SW}$$$. The compass is at $$$(1,1)$$$, the morning star is at $$$(-1,-1)$$$: the compass will point $$$ ext{SW}$$$. In the second test case, only two pairs of points won't break the compass: The compass is at $$$(6,9)$$$, the morning star is at $$$(10,13)$$$: the compass will point $$$ ext{NE}$$$. The compass is at $$$(10,13)$$$, the morning star is at $$$(6,9)$$$: the compass will point $$$ ext{SW}$$$.
1,500
true
false
true
false
true
false
false
false
true
false
1,176
1567B
Alice gave Bob two integers $$$a$$$ and $$$b$$$ ($$$a > 0$$$ and $$$b ge 0$$$). Being a curious boy, Bob wrote down an array of non-negative integers with $$$operatorname{MEX}$$$ value of all elements equal to $$$a$$$ and $$$operatorname{XOR}$$$ value of all elements equal to $$$b$$$. What is the shortest possible length of the array Bob wrote? Recall that the $$$operatorname{MEX}$$$ (xa0— the number of test cases. The description of the test cases follows. The only line of each test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 leq a leq 3 cdot 10^5$$$; $$$0 leq b leq 3 cdot 10^5$$$)xa0— the $$$operatorname{MEX}$$$ and $$$operatorname{XOR}$$$ of the array, respectively. Output For each test case, output one (positive) integerxa0— the length of the shortest array with $$$operatorname{MEX}$$$ $$$a$$$ and $$$operatorname{XOR}$$$ $$$b$$$. We can show that such an array always exists. Example Input 5 1 1 2 1 2 0 1 10000 2 10000 Note In the first test case, one of the shortest arrays with $$$operatorname{MEX}$$$ $$$1$$$ and $$$operatorname{XOR}$$$ $$$1$$$ is $$$[0, 2020, 2021]$$$. In the second test case, one of the shortest arrays with $$$operatorname{MEX}$$$ $$$2$$$ and $$$operatorname{XOR}$$$ $$$1$$$ is $$$[0, 1]$$$. It can be shown that these arrays are the shortest arrays possible.
1,000
false
true
false
false
false
false
false
false
false
false
2,803
545C
Little Susie listens to fairy tales before bed every day. Today's fairy tale was about wood cutters and the little girl immediately started imagining the choppers cutting wood. She imagined the situation that is described below. There are _n_ trees located along the road at points with coordinates _x_1,u2009_x_2,u2009...,u2009_x__n_. Each tree has its height _h__i_. Woodcutters can cut down a tree and fell it to the left or to the right. After that it occupies one of the segments [_x__i_u2009-u2009_h__i_,u2009_x__i_] or [_x__i_;_x__i_u2009+u2009_h__i_]. The tree that is not cut down occupies a single point with coordinate _x__i_. Woodcutters can fell a tree if the segment to be occupied by the fallen tree doesn't contain any occupied point. The woodcutters want to process as many trees as possible, so Susie wonders, what is the maximum number of trees to fell. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of trees. Next _n_ lines contain pairs of integers _x__i_,u2009_h__i_ (1u2009≤u2009_x__i_,u2009_h__i_u2009≤u2009109) — the coordinate and the height of the _і_-th tree. The pairs are given in the order of ascending _x__i_. No two trees are located at the point with the same coordinate. Output Print a single number — the maximum number of trees that you can cut down by the given rules. Note In the first sample you can fell the trees like that: fell the 1-st tree to the left — now it occupies segment [u2009-u20091;1] fell the 2-nd tree to the right — now it occupies segment [2;3] leave the 3-rd tree — it occupies point 5 leave the 4-th tree — it occupies point 10 fell the 5-th tree to the right — now it occupies segment [19;20] In the second sample you can also fell 4-th tree to the right, after that it will occupy segment [10;19].
1,500
false
true
false
true
false
false
false
false
false
false
7,660
1930C
Stack has an array $$$a$$$ of length $$$n$$$. He also has an empty set $$$S$$$. Note that $$$S$$$ is not a multiset. He will do the following three-step operation exactly $$$n$$$ times: 1. Select an index $$$i$$$ such that $$$1 leq i leq a$$$. 2. Insert$$$^dagger$$$ $$$a_i + i$$$ into $$$S$$$. 3. Delete $$$a_i$$$ from $$$a$$$. Note that the indices of all elements to the right of $$$a_i$$$ will decrease by $$$1$$$. Note that after $$$n$$$ operations, $$$a$$$ will be empty. Stack will now construct a new array $$$b$$$ which is $$$S$$$ sorted in decreasing order. Formally, $$$b$$$ is an array of size $$$S$$$ where $$$b_i$$$ is the $$$i$$$-th largest element of $$$S$$$ for all $$$1 leq i leq S$$$. Find the lexicographically largest$$$^ddagger$$$ $$$b$$$ that Stack can make. $$$^dagger$$$ A set can only contain unique elements. Inserting an element that is already present in a set will not change the elements of the set. $$$^ddagger$$$ An array $$$p$$$ is lexicographically larger than a sequence $$$q$$$ if and only if one of the following holds: $$$q$$$ is a prefix of $$$p$$$, but $$$p e q$$$; or in the first position where $$$p$$$ and $$$q$$$ differ, the array $$$p$$$ has a larger element than the corresponding element in $$$q$$$. Note that $$$[3,1,4,1,5]$$$ is lexicographically larger than $$$[3,1,3]$$$, $$$[,]$$$, and $$$[3,1,4,1]$$$ but not $$$[3,1,4,1,5,9]$$$, $$$[3,1,4,1,5]$$$, and $$$[4]$$$. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)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 leq n leq 3 cdot 10^5$$$)xa0— the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,ldots,a_{n}$$$ ($$$1 leq a_i leq 10^9$$$)xa0— the elements of array $$$a$$$. The sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Note In the first test case, select $$$i=1$$$ in the first operation, insert $$$a_1 + 1 = 3$$$ in $$$S$$$, and delete $$$a_1$$$ from $$$a$$$. After the first operation, $$$a$$$ becomes $$$a=[1]$$$. In the second operation, we select $$$i=1$$$ again and insert $$$a_1 + 1 = 2$$$ in $$$S$$$. Thus $$$S={2, 3}$$$, and $$$b = [3, 2]$$$. Note that if you select $$$i=2$$$ in the first operation, and $$$i=1$$$ in the second operation, $$$S={3}$$$ as $$$3$$$ will be inserted twice, resulting in $$$b=[3]$$$. As $$$[3,2]$$$ is lexicographically larger than $$$[3]$$$, we should select $$$i=1$$$ in the first operation. In the second test case, in each operation, select the last element.
1,700
false
true
false
false
true
true
false
true
true
false
709
1774C
Little09 and his friends are playing a game. There are $$$n$$$ players, and the temperature value of the player $$$i$$$ is $$$i$$$. The types of environment are expressed as $$$0$$$ or $$$1$$$. When two players fight in a specific environment, if its type is $$$0$$$, the player with a lower temperature value in this environment always wins; if it is $$$1$$$, the player with a higher temperature value in this environment always wins. The types of the $$$n-1$$$ environments form a binary string $$$s$$$ with a length of $$$n-1$$$. If there are $$$x$$$ players participating in the game, there will be a total of $$$x-1$$$ battles, and the types of the $$$x-1$$$ environments will be the first $$$x-1$$$ characters of $$$s$$$. While there is more than one player left in the tournament, choose any two remaining players to fight. The player who loses will be eliminated from the tournament. The type of the environment of battle $$$i$$$ is $$$s_i$$$. For each $$$x$$$ from $$$2$$$ to $$$n$$$, answer the following question: if all players whose temperature value does not exceed $$$x$$$ participate in the game, how many players have a chance to win? Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1le t le 10^3$$$) 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$$$ ($$$2leq nleq 2cdot 10^5$$$) xa0— the number of players. The second line of each test case contains a binary string $$$s$$$ with a length $$$n-1$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3cdot 10^5$$$. Output For each test case output $$$n-1$$$ integers xa0— for each $$$x$$$ from $$$2$$$ to $$$n$$$, output the number of players that have a chance to win. Note In the first test case, for $$$x=2$$$ and $$$x=3$$$, only the player whose temperature value is $$$1$$$ can be the winner. For $$$x=4$$$, the player whose temperature value is $$$2,3,4$$$ can be the winner.
1,300
false
true
false
true
false
true
false
false
false
false
1,642
382A
Ksenia has ordinary pan scales and several weights of an equal mass. Ksenia has already put some weights on the scales, while other weights are untouched. Ksenia is now wondering whether it is possible to put all the remaining weights on the scales so that the scales were in equilibrium. The scales is in equilibrium if the total sum of weights on the left pan is equal to the total sum of weights on the right pan. Input The first line has a non-empty sequence of characters describing the scales. In this sequence, an uppercase English letter indicates a weight, and the symbol "" indicates the delimiter (the character occurs in the sequence exactly once). All weights that are recorded in the sequence before the delimiter are initially on the left pan of the scale. All weights that are recorded in the sequence after the delimiter are initially on the right pan of the scale. The second line contains a non-empty sequence containing uppercase English letters. Each letter indicates a weight which is not used yet. It is guaranteed that all the English letters in the input data are different. It is guaranteed that the input does not contain any extra characters. Output If you cannot put all the weights on the scales so that the scales were in equilibrium, print string "Impossible". Otherwise, print the description of the resulting scales, copy the format of the input. If there are multiple answers, print any of them.
1,100
false
true
true
false
false
false
false
false
false
false
8,303
375D
You have a rooted tree consisting of _n_ vertices. Each vertex of the tree has some color. We will assume that the tree vertices are numbered by integers from 1 to _n_. Then we represent the color of vertex _v_ as _c__v_. The tree root is a vertex with number 1. In this problem you need to answer to _m_ queries. Each query is described by two integers _v__j_,u2009_k__j_. The answer to query _v__j_,u2009_k__j_ is the number of such colors of vertices _x_, that the subtree of vertex _v__j_ contains at least _k__j_ vertices of color _x_. You can find the definition of a rooted tree by the following link: first line contains two integers _n_ and _m_ (2u2009≤u2009_n_u2009≤u2009105;xa01u2009≤u2009_m_u2009≤u2009105). The next line contains a sequence of integers _c_1,u2009_c_2,u2009...,u2009_c__n_ (1u2009≤u2009_c__i_u2009≤u2009105). The next _n_u2009-u20091 lines contain the edges of the tree. The _i_-th line contains the numbers _a__i_,u2009_b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_;xa0_a__i_u2009≠u2009_b__i_) — the vertices connected by an edge of the tree. Next _m_ lines contain the queries. The _j_-th line contains two integers _v__j_,u2009_k__j_ (1u2009≤u2009_v__j_u2009≤u2009_n_;xa01u2009≤u2009_k__j_u2009≤u2009105). Output Print _m_ integers — the answers to the queries in the order the queries appear in the input. Examples Input 8 5 1 2 2 3 3 2 3 3 1 2 1 5 2 3 2 4 5 6 5 7 5 8 1 2 1 3 1 4 2 3 5 3 Input 4 1 1 2 3 4 1 2 2 3 3 4 1 1 Note A subtree of vertex _v_ in a rooted tree with root _r_ is a set of vertices {_u_xa0:u2009_dist_(_r_,u2009_v_)u2009+u2009_dist_(_v_,u2009_u_)u2009=u2009_dist_(_r_,u2009_u_)}. Where _dist_(_x_,u2009_y_) is the length (in edges) of the shortest path between vertices _x_ and _y_.
2,400
false
false
false
false
true
false
false
false
false
false
8,328
1523G
William owns a flat in central London. He decided to rent his flat out for the next $$$n$$$ days to earn some money. Since his flat is in the center of the city, he instantly got $$$m$$$ offers in the form $$$(l_i, r_i)$$$, which means that someone wants to book the flat from day $$$l_i$$$ until day $$$r_i$$$ inclusive. To avoid spending a lot of time figuring out whether it's profitable for him to accept an offer, William decided to develop an algorithm. The algorithm processes all offers as they arrive and will only accept offer $$$i$$$ if the following two conditions are satisfied: $$$r_i - l_i + 1 ge x$$$. None of the days between $$$l_i$$$ and $$$r_i$$$ are occupied by a previously accepted offer William isn't sure what value $$$x$$$ should have and he asks you for help. For all $$$x$$$ from $$$1$$$ to $$$n$$$ he wants you to calculate the total number of days for which the flat would be occupied if the corresponding value will be assigned to $$$x$$$. Input The first line contains two integers $$$n$$$ and $$$m$$$ $$$(1 le n le 5 cdot 10^4, 1 le m le 10^5)$$$, which are the number of days and the number of offers, respectively. Each of the next $$$m$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ $$$(1 le l_i le r_i le n)$$$, which describe the $$$i$$$-th renting offer. All offers are given in chronological order. Output Print $$$n$$$ integers. The number in $$$i$$$-th line must be equal to the number of days the flat would be occupied if the algorithm will use the value of $$$x$$$ equal to $$$i$$$. Example Input 6 5 2 3 3 5 1 1 1 5 1 6 Note The description of segments from the first sample test for each $$$x$$$: $$$x = 1$$$ xa0— algorithm will approve offers: $$$1$$$ (2..3), $$$3$$$ (1..1). The total number of days for which William's flat will be rented out is 3 $$$x = 2$$$ xa0— algorithm will approve offers: $$$1$$$ (2..3). The total number of days for which William's flat will be rented out is 2 $$$x = 3$$$ xa0— algorithm will approve offers: $$$2$$$ (3..5). The total number of days for which William's flat will be rented out is 3 $$$x = 4$$$ xa0— algorithm will approve offers: $$$4$$$ (1..5). The total number of days for which William's flat will be rented out is 5 $$$x = 5$$$ xa0— algorithm will approve offers: $$$4$$$ (1..5). The total number of days for which William's flat will be rented out is 5 $$$x = 6$$$ xa0— algorithm will approve offers: $$$5$$$ (1..6). The total number of days for which William's flat will be rented out is 6
3,200
false
false
false
false
true
false
false
false
false
false
3,044
1389F
Problem - 1389F - 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 graph matchings sortings *2600 No tag edit access → Contest materials xa0— number of segments. The next $$$n$$$ lines contains three integers $$$l_i, r_i, t_i$$$ ($$$1 le l_i le r_i le 10^9; t_i in {1, 2}$$$)xa0— description of the $$$i$$$-th segment. Output Print the maximum number of segments that can be selected, so that there is no bad pair among the selected segments. Examples Input 3 1 3 1 4 6 2 2 5 1 Output 2 Input 5 5 8 1 1 3 2 3 4 2 6 6 1 2 10 2 Output 4 Input 7 19 20 1 13 15 2 6 11 2 4 10 1 14 17 1 13 13 2 5 9 1 Output 5
2,600
false
false
false
true
true
false
false
false
true
false
3,723
501A
Problem - 501A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation *900 No tag edit access → Contest materials . It is guaranteed that numbers _a_ and _b_ are divisible by 250 (just like on any real Codeforces round). Output Output on a single line: "Misha" (without the quotes), if Misha got more points than Vasya. "Vasya" (without the quotes), if Vasya got more points than Misha. "Tie" (without the quotes), if both of them got the same number of points. Examples Input 500 1000 20 30 Output Vasya Input 1000 1000 1 1 Output Tie Input 1500 1000 176 177 Output Misha
900
false
false
true
false
false
false
false
false
false
false
7,830
1932G
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 927 (Div. 3) 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 graphs math number theory shortest paths *2300 No tag edit access → Contest materials Announcement Tutorial PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST G. Moving Platforms time limit per test3 seconds memory limit per test512 megabytes There is a game where you need to move through a labyrinth. The labyrinth consists of $$$n$$$ platforms, connected by $$$m$$$ passages. Each platform is at some level $$$l_i$$$, an integer number from $$$0$$$ to $$$H - 1$$$. In a single step, if you are currently on platform $$$i$$$, you can stay on it, or move to another platform $$$j$$$. To move to platform $$$j$$$ they have to be connected by the passage, and their levels have to be the same, namely $$$l_i = l_j$$$. After each step, the levels of all platforms change. The new level of platform $$$i$$$ is calculated as $$$l'_i = (l_i + s_i) bmod H$$$, for all $$$i$$$. You start on platform $$$1$$$. Find the minimum number of steps you need to get to platform $$$n$$$. Input The first line of input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$H$$$ ($$$2 le n le 10^5$$$, $$$1 le m le 10^5$$$, $$$1 le H le 10^9$$$). The second line contains $$$n$$$ integers $$$l_i$$$, the initial level of each platform ($$$0 le l_i le H-1$$$). The third line contains $$$n$$$ integers $$$s_i$$$, the change of level for each platform ($$$0 le s_i le H-1$$$). Next $$$m$$$ lines contain a description of the passages. Each passage is described as a pair of integersxa0— the platforms, connected by the passage. There is at most one passage connecting each pair of platforms, and there is no passage connecting a platform to itself. The sum of $$$n$$$ for all tests does not exceed $$$10^5$$$, the sum of $$$m$$$ for all tests does not exceed $$$10^5$$$. Output For each test case, print a single integer, the minimum number of steps needed to get from platform $$$1$$$ to platform $$$n$$$. If it is impossible to get to platform $$$n$$$, print $$$-1$$$. Example input 3 3 3 10 1 9 4 2 3 0 1 2 3 2 1 3 2 1 10 1 2 4 6 1 2 8 7 25 22 14 5 3 10 14 11 1 9 5 4 10 7 16 18 18 2 8 6 3 3 5 7 5 2 6 1 4 4 7 output 6 -1 52 Note This is how levels of the platforms change, and what actions we need to perform in the first example. Platform 1 Platform 2 Platform 3 Action Step 1 1 9 4 Stay on the platform 1 Step 2 3 2 4 Stay on the platform 1 Step 3 5 5 4 Move to the platform 2 Step 4 7 8 4 Stay on the platform 2 Step 5 9 1 4 Stay on the platform 2 Step 6 1 4 4 Move to the platform 3 Codeforces (c)
2,300
true
false
false
false
false
false
false
false
false
true
688
1108F
You are given an undirected weighted connected graph with $$$n$$$ vertices and $$$m$$$ edges without loops and multiple edges. The $$$i$$$-th edge is $$$e_i = (u_i, v_i, w_i)$$$; the distance between vertices $$$u_i$$$ and $$$v_i$$$ along the edge $$$e_i$$$ is $$$w_i$$$ ($$$1 le w_i$$$). The graph is connected, i.u2009e. for any pair of vertices, there is at least one path between them consisting only of edges of the given graph. A minimum spanning tree (MST) in case of positive weights is a subset of the edges of a connected weighted undirected graph that connects all the vertices together and has minimum total cost among all such subsets (total cost is the sum of costs of chosen edges). You can modify the given graph. The only operation you can perform is the following: increase the weight of some edge by $$$1$$$. You can increase the weight of each edge multiple (possibly, zero) times. Suppose that the initial MST cost is $$$k$$$. Your problem is to increase weights of some edges with minimum possible number of operations in such a way that the cost of MST in the obtained graph remains $$$k$$$, but MST is unique (it means that there is only one way to choose MST in the obtained graph). Your problem is to calculate the minimum number of operations required to do it. Input The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 2 cdot 10^5, n - 1 le m le 2 cdot 10^5$$$) — the number of vertices and the number of edges in the initial graph. The next $$$m$$$ lines contain three integers each. The $$$i$$$-th line contains the description of the $$$i$$$-th edge $$$e_i$$$. It is denoted by three integers $$$u_i, v_i$$$ and $$$w_i$$$ ($$$1 le u_i, v_i le n, u_i e v_i, 1 le w le 10^9$$$), where $$$u_i$$$ and $$$v_i$$$ are vertices connected by the $$$i$$$-th edge and $$$w_i$$$ is the weight of this edge. It is guaranteed that the given graph doesn't contain loops and multiple edges (i.e. for each $$$i$$$ from $$$1$$$ to $$$m$$$ $$$u_i e v_i$$$ and for each unordered pair of vertices $$$(u, v)$$$ there is at most one edge connecting this pair of vertices). It is also guaranteed that the given graph is connected. Output Print one integer — the minimum number of operations to unify MST of the initial graph without changing the cost of MST. Examples Input 8 10 1 2 1 2 3 2 2 4 5 1 4 2 6 3 3 6 1 3 3 5 2 3 7 1 4 8 1 6 2 4 Input 4 3 2 1 3 4 3 4 2 4 1 Input 3 3 1 2 1 2 3 2 1 3 3 Input 3 3 1 2 1 2 3 3 1 3 3 Input 5 6 1 2 2 2 3 1 4 5 3 2 4 2 1 4 2 1 5 3 Note The picture corresponding to the first example: You can, for example, increase weight of the edge $$$(1, 6)$$$ or $$$(6, 3)$$$ by $$$1$$$ to unify MST. The picture corresponding to the last example: You can, for example, increase weights of edges $$$(1, 5)$$$ and $$$(2, 4)$$$ by $$$1$$$ to unify MST.
2,100
false
true
false
false
false
false
false
true
false
true
5,156
1826B
You have an array $$$a$$$ of $$$n$$$ non-negative integers. Let's define $$$f(a, x) = [a_1 bmod x, a_2 bmod x, dots, a_n bmod x]$$$ for some positive integer $$$x$$$. Find the biggest $$$x$$$, such that $$$f(a, x)$$$ is a palindrome. Here, $$$a bmod x$$$ is the remainder of the integer division of $$$a$$$ by $$$x$$$. An array is a palindrome if it reads the same backward as forward. More formally, an array $$$a$$$ of length $$$n$$$ is a palindrome if for every $$$i$$$ ($$$1 leq i leq n$$$) $$$a_i = a_{n - i + 1}$$$. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^5$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$0 leq a_i leq 10^9$$$). It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^5$$$. Output For each test case output the biggest $$$x$$$, such that $$$f(a, x)$$$ is a palindrome. If $$$x$$$ can be infinitely large, output $$$0$$$ instead. Example Input 4 2 1 2 8 3 0 1 2 0 3 2 1 1 0 3 100 1 1000000000 Note In the first example, $$$f(a, x = 1) = [0, 0]$$$ which is a palindrome. In the second example, $$$f(a, x = 2) = [1, 0, 1, 0, 0, 1, 0, 1]$$$ which is a palindrome. It can be proven that in the first two examples, no larger $$$x$$$ satisfies the condition. In the third example, $$$f(a, x) = [0]$$$ for any $$$x$$$, so we can choose it infinitely large, so the answer is $$$0$$$.
1,100
true
false
false
false
false
false
false
false
false
false
1,328
604A
Kevin Sun has just finished competing in Codeforces Round #334! The round was 120 minutes long and featured five problems with maximum point values of 500, 1000, 1500, 2000, and 2500, respectively. Despite the challenging tasks, Kevin was uncowed and bulldozed through all of them, distinguishing himself from the herd as the best cowmputer scientist in all of Bovinia. Kevin knows his submission time for each problem, the number of wrong submissions that he made on each problem, and his total numbers of successful and unsuccessful hacks. Because Codeforces scoring is complicated, Kevin wants you to write a program to compute his final score. Codeforces scores are computed as follows: If the maximum point value of a problem is _x_, and Kevin submitted correctly at minute _m_ but made _w_ wrong submissions, then his score on that problem is . His total score is equal to the sum of his scores for each problem. In addition, Kevin's total score gets increased by 100 points for each successful hack, but gets decreased by 50 points for each unsuccessful hack. All arithmetic operations are performed with absolute precision and no rounding. It is guaranteed that Kevin's final score is an integer. Input The first line of the input contains five space-separated integers _m_1, _m_2, _m_3, _m_4, _m_5, where _m__i_ (0u2009≤u2009_m__i_u2009≤u2009119) is the time of Kevin's last submission for problem _i_. His last submission is always correct and gets accepted. The second line contains five space-separated integers _w_1, _w_2, _w_3, _w_4, _w_5, where _w__i_ (0u2009≤u2009_w__i_u2009≤u200910) is Kevin's number of wrong submissions on problem _i_. The last line contains two space-separated integers _h__s_ and _h__u_ (0u2009≤u2009_h__s_,u2009_h__u_u2009≤u200920), denoting the Kevin's numbers of successful and unsuccessful hacks, respectively. Output Print a single integer, the value of Kevin's final score. Examples Input 20 40 60 80 100 0 1 2 3 4 1 0 Input 119 119 119 119 119 0 0 0 0 0 10 0 Note In the second sample, Kevin takes 119 minutes on all of the problems. Therefore, he gets of the points on each problem. So his score from solving problems is . Adding in 10·100u2009=u20091000 points from hacks, his total score becomes 3930u2009+u20091000u2009=u20094930.
1,000
false
false
true
false
false
false
false
false
false
false
7,428
1967E2
This is the hard version of the problem. The differences between the two versions are the constraints on $$$n, m, b_0$$$ and the time limit. You can make hacks only if both versions are solved. Little R has counted many sets before, and now she decides to count arrays. Little R thinks an array $$$b_0, ldots, b_n$$$ consisting of non-negative integers is continuous if and only if, for each $$$i$$$ such that $$$1 leq i leq n$$$, $$$lvert b_i - b_{i-1} vert = 1$$$ is satisfied. She likes continuity, so she only wants to generate continuous arrays. If Little R is given $$$b_0$$$ and $$$a_1, ldots, a_n$$$, she will try to generate a non-negative continuous array $$$b$$$, which has no similarity with $$$a$$$. More formally, for all $$$1 leq i leq n$$$, $$$a_i eq b_i$$$ holds. However, Little R does not have any array $$$a$$$. Instead, she gives you $$$n$$$, $$$m$$$ and $$$b_0$$$. She wants to count the different integer arrays $$$a_1, ldots, a_n$$$ satisfying: $$$1 leq a_i leq m$$$; At least one non-negative continuous array $$$b_0, ldots, b_n$$$ can be generated. Note that $$$b_i geq 0$$$, but the $$$b_i$$$ can be arbitrarily large. Since the actual answer may be enormous, please just tell her the answer modulo $$$998,244,353$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t (1 leq t leq 10^4)$$$. The description of the test cases follows. The first and only line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$b_0$$$ ($$$1 leq n leq 2 cdot 10^6$$$, $$$1 leq m leq 2 cdot 10^6$$$, $$$0 leq b_0 leq 2cdot 10^6$$$)xa0— the length of the array $$$a_1, ldots, a_n$$$, the maximum possible element in $$$a_1, ldots, a_n$$$, and the initial element of the array $$$b_0, ldots, b_n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceeds $$$10^7$$$. Output For each test case, output a single line containing an integer: the number of different arrays $$$a_1, ldots, a_n$$$ satisfying the conditions, modulo $$$998,244,353$$$. Example Input 6 3 2 1 5 5 3 13 4 1 100 6 7 100 11 3 1000 424 132 Output 6 3120 59982228 943484039 644081522 501350342 Note In the first test case, for example, when $$$a = [1, 2, 1]$$$, we can set $$$b = [1, 0, 1, 0]$$$. When $$$a = [1, 1, 2]$$$, we can set $$$b = [1, 2, 3, 4]$$$. In total, there are $$$6$$$ valid choices of $$$a_1, ldots, a_n$$$: in fact, it can be proved that only $$$a = [2, 1, 1]$$$ and $$$a = [2, 1, 2]$$$ make it impossible to construct a non-negative continuous $$$b_0, ldots, b_n$$$, so the answer is $$$2^3 - 2 = 6$$$.
3,500
true
false
false
true
false
false
false
false
false
false
502
1769B2
Statement is not available on English language В этой версии задачи размеры копируемых файлов не превышают $$$10^{10}$$$ байт. Вы копируете с одного сервера на другой $$$n$$$ файлов размером $$$a_1, a_2, ldots, a_n$$$ байт. Файлы копируются последовательно в заданном порядке. При копировании вы видите два прогресс-бара: первый показывает процент скопированных данных в текущем файле, а второйxa0— общий процент скопированных данных по всем $$$n$$$ файлам. Оба процента отображаются округлёнными вниз до целого числа. Значения на прогресс-барах обновляются после копирования каждого байта. Формально, после копирования байта номер $$$x$$$ из файла номер $$$i$$$ первый прогресс-бар показывает $$$lfloor frac{100 cdot x}{a_i} floor$$$ процентов, а второйxa0— $$$lfloor frac{100 cdot (a_1 + a_2 + ldots + a_{i - 1} + x)}{a_1 + a_2 + ldots + a_n} floor$$$ процентов. В самом начале копирования оба прогресс-бара показывают $$$0$$$ процентов. Найдите все такие целые числа от $$$0$$$ до $$$100$$$ включительно, что существует момент времени, в который оба прогресс-бара одновременно показывают это число. Выведите эти числа в порядке возрастания. Входные данные В первой строке задано одно целое число $$$n$$$ ($$$1 le n le 100$$$)xa0— число копируемых файлов. Во второй строке заданы $$$n$$$ целых чисел $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^{10}$$$)xa0— размеры файлов в байтах в том порядке, в котором они будут копироваться. Выходные данные Выведите в возрастающем порядке все числа от $$$0$$$ до $$$100$$$ включительно такие, что существует момент времени, в который на обоих прогресс-барах одновременно показывается это число. Примеры Выходные данные 0 16 33 50 66 83 100 Выходные данные 0 95 96 97 98 99 100 Входные данные 4 10000000000 2 2 9999999998 Выходные данные 0 50 99 100 Входные данные 6 170 130 400 256 30 100 Выходные данные 0 17 43 44 84 90 99 100 Примечание В первом тесте копируется всего один файл, поэтому оба прогресс-бара всегда показывают одинаковые значения. Во втором тесте первый прогресс-бар сразу же уйдёт вперёд, потом сбросится в ноль и начнёт догонять второй прогресс-бар заново. В конце копирования прогресс-бары некоторое время будут показывать одно и то же число. Обратите внимание, что третий тест в этой версии задачи отличается от третьего теста в предыдущей версии задачи.
1,400
true
false
false
false
false
false
true
true
false
false
1,683
1136D
At the big break Nastya came to the school dining room. There are $$$n$$$ pupils in the school, numbered from $$$1$$$ to $$$n$$$. Unfortunately, Nastya came pretty late, so that all pupils had already stood in the queue, i.e. Nastya took the last place in the queue. Of course, it's a little bit sad for Nastya, but she is not going to despond because some pupils in the queue can agree to change places with some other pupils. Formally, there are some pairs $$$u$$$, $$$v$$$ such that if the pupil with number $$$u$$$ stands directly in front of the pupil with number $$$v$$$, Nastya can ask them and they will change places. Nastya asks you to find the maximal number of places in queue she can move forward. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n leq 3 cdot 10^{5}$$$, $$$0 leq m leq 5 cdot 10^{5}$$$)xa0— the number of pupils in the queue and number of pairs of pupils such that the first one agrees to change places with the second one if the first is directly in front of the second. The second line contains $$$n$$$ integers $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$xa0— the initial arrangement of pupils in the queue, from the queue start to its end ($$$1 leq p_i leq n$$$, $$$p$$$ is a permutation of integers from $$$1$$$ to $$$n$$$). In other words, $$$p_i$$$ is the number of the pupil who stands on the $$$i$$$-th position in the queue. The $$$i$$$-th of the following $$$m$$$ lines contains two integers $$$u_i$$$, $$$v_i$$$ ($$$1 leq u_i, v_i leq n, u_i eq v_i$$$), denoting that the pupil with number $$$u_i$$$ agrees to change places with the pupil with number $$$v_i$$$ if $$$u_i$$$ is directly in front of $$$v_i$$$. It is guaranteed that if $$$i eq j$$$, than $$$v_i eq v_j$$$ or $$$u_i eq u_j$$$. Note that it is possible that in some pairs both pupils agree to change places with each other. Nastya is the last person in the queue, i.e. the pupil with number $$$p_n$$$. Output Print a single integerxa0— the number of places in queue she can move forward. Note In the first example Nastya can just change places with the first pupil in the queue. Optimal sequence of changes in the second example is change places for pupils with numbers $$$1$$$ and $$$3$$$. change places for pupils with numbers $$$3$$$ and $$$2$$$. change places for pupils with numbers $$$1$$$ and $$$2$$$. The queue looks like $$$[3, 1, 2]$$$, then $$$[1, 3, 2]$$$, then $$$[1, 2, 3]$$$, and finally $$$[2, 1, 3]$$$ after these operations.
1,800
false
true
false
false
false
false
false
false
false
false
5,044
722A
You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59. You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format. For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39. Input The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively. The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes. Output The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them.
1,300
false
false
true
false
false
false
true
false
false
false
6,926
1788A
You are given a sequence $$$a_1, a_2, ldots, a_n$$$. Each element of $$$a$$$ is $$$1$$$ or $$$2$$$. Find out if an integer $$$k$$$ exists so that the following conditions are met. $$$1 leq k leq n-1$$$, and $$$a_1 cdot a_2 cdot ldots cdot a_k = a_{k+1} cdot a_{k+2} cdot ldots cdot a_n$$$. If there exist multiple $$$k$$$ that satisfy the given condition, print the smallest. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). Description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$2 leq n leq 1000$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 2$$$). Output For each test case, if there is no such $$$k$$$, print $$$-1$$$. Otherwise, print the smallest possible $$$k$$$. Example Input 3 6 2 2 1 2 1 2 3 1 2 1 4 1 1 1 1 Note For the first test case, $$$k=2$$$ satisfies the condition since $$$a_1 cdot a_2 = a_3 cdot a_4 cdot a_5 cdot a_6 = 4$$$. $$$k=3$$$ also satisfies the given condition, but the smallest should be printed. For the second test case, there is no $$$k$$$ that satisfies $$$a_1 cdot a_2 cdot ldots cdot a_k = a_{k+1} cdot a_{k+2} cdot ldots cdot a_n$$$ For the third test case, $$$k=1$$$, $$$2$$$, and $$$3$$$ satisfy the given condition, so the answer is $$$1$$$.
800
true
false
true
false
false
false
true
false
false
false
1,554
1487F
Problem - 1487F - 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 shortest paths *2900 No tag edit access → Contest materials integer $$$n$$$. You have to represent $$$n$$$ as the sum of integers (possibly negative) consisting only of ones (digits '1'). For example, $$$24 = 11 + 11 + 1 + 1$$$ and $$$102 = 111 - 11 + 1 + 1$$$. Among all possible representations, you have to find the one that uses the minimum number of ones in total. Input The single line contains one integer $$$n$$$ ($$$1 le n < 10^{50}$$$). Output Print one integer $$$x$$$ — the minimum number of ones, such that there exist a representation of $$$n$$$ as the sum of integers (possibly negative) that uses $$$x$$$ ones in total. Examples Input 24 Output 6 Input 102 Output 7
2,900
false
true
false
true
false
false
false
false
false
false
3,241
1059A
Vasya has recently got a job as a cashier at a local store. His day at work is $$$L$$$ minutes long. Vasya has already memorized $$$n$$$ regular customers, the $$$i$$$-th of which comes after $$$t_{i}$$$ minutes after the beginning of the day, and his service consumes $$$l_{i}$$$ minutes. It is guaranteed that no customer will arrive while Vasya is servicing another customer. Vasya is a bit lazy, so he likes taking smoke breaks for $$$a$$$ minutes each. Those breaks may go one after another, but Vasya must be present at work during all the time periods he must serve regular customers, otherwise one of them may alert his boss. What is the maximum number of breaks Vasya can take during the day? Input The first line contains three integers $$$n$$$, $$$L$$$ and $$$a$$$ ($$$0 le n le 10^{5}$$$, $$$1 le L le 10^{9}$$$, $$$1 le a le L$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$t_{i}$$$ and $$$l_{i}$$$ ($$$0 le t_{i} le L - 1$$$, $$$1 le l_{i} le L$$$). It is guaranteed that $$$t_{i} + l_{i} le t_{i + 1}$$$ and $$$t_{n} + l_{n} le L$$$. Output Output one integer xa0— the maximum number of breaks. Note In the first sample Vasya can take $$$3$$$ breaks starting after $$$2$$$, $$$5$$$ and $$$8$$$ minutes after the beginning of the day. In the second sample Vasya can take $$$2$$$ breaks starting after $$$0$$$ and $$$2$$$ minutes after the beginning of the day. In the third sample Vasya can't take any breaks.
1,000
false
false
true
false
false
false
false
false
false
false
5,421
359D
Problem - 359D - 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 brute force data structures math two pointers *2000 No tag edit access → Contest materials ") ") , such that the following conditions hold: 1. there is integer _j_ (_l_u2009≤u2009_j_u2009≤u2009_r_), such that all integers _a__l_,u2009_a__l_u2009+u20091,u2009...,u2009_a__r_ are divisible by _a__j_; 2. value _r_u2009-u2009_l_ takes the maximum value among all pairs for which condition 1 is true; Help Simon, find the required pair of numbers (_l_,u2009_r_). If there are multiple required pairs find all of them. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u20093·105). The second line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009106). Output Print two integers in the first line — the number of required pairs and the maximum value of _r_u2009-u2009_l_. On the following line print all _l_ values from optimal pairs in increasing order. Examples Input 5 4 6 9 3 6 Output 1 3 2 Input 5 1 3 5 7 9 Output 1 4 1 Input 5 2 3 5 7 11 Output 5 0 1 2 3 4 5 Note In the first sample the pair of numbers is right, as numbers 6,u20099,u20093 are divisible by 3. In the second sample all numbers are divisible by number 1. In the third sample all numbers are prime, so conditions 1 and 2 are true only for pairs of numbers (1,u20091), (2,u20092), (3,u20093), (4,u20094), (5,u20095).
2,000
true
false
false
false
true
false
true
true
false
false
8,396
1438A
Andre has very specific tastes. Recently he started falling in love with arrays. Andre calls an nonempty array $$$b$$$ good, if sum of its elements is divisible by the length of this array. For example, array $$$[2, 3, 1]$$$ is good, as sum of its elementsxa0— $$$6$$$xa0— is divisible by $$$3$$$, but array $$$[1, 1, 2, 3]$$$ isn't good, as $$$7$$$ isn't divisible by $$$4$$$. Andre calls an array $$$a$$$ of length $$$n$$$ perfect if the following conditions hold: Every nonempty subarray of this array is good. For every $$$i$$$ ($$$1 le i le n$$$), $$$1 leq a_i leq 100$$$. Given a positive integer $$$n$$$, output any perfect array of length $$$n$$$. We can show that for the given constraints such an array always exists. An array $$$c$$$ is a subarray of an array $$$d$$$ if $$$c$$$ can be obtained from $$$d$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). Description of the test cases follows. The first and only line of every test case contains a single integer $$$n$$$ ($$$1 le n le 100$$$). Output For every test, output any perfect array of length $$$n$$$ on a separate line. Example Output 24 19 33 7 37 79 49 Note Array $$$[19, 33]$$$ is perfect as all $$$3$$$ its subarrays: $$$[19]$$$, $$$[33]$$$, $$$[19, 33]$$$, have sums divisible by their lengths, and therefore are good.
800
false
false
true
false
false
true
false
false
false
false
3,478
1354D
Note that the memory limit is unusual. You are given a multiset consisting of $$$n$$$ integers. You have to process queries of two types: add integer $$$k$$$ into the multiset; find the $$$k$$$-th order statistics in the multiset and remove it. $$$k$$$-th order statistics in the multiset is the $$$k$$$-th element in the sorted list of all elements of the multiset. For example, if the multiset contains elements $$$1$$$, $$$4$$$, $$$2$$$, $$$1$$$, $$$4$$$, $$$5$$$, $$$7$$$, and $$$k = 3$$$, then you have to find the $$$3$$$-rd element in $$$[1, 1, 2, 4, 4, 5, 7]$$$, which is $$$2$$$. If you try to delete an element which occurs multiple times in the multiset, only one occurence is removed. After processing all queries, print any number belonging to the multiset, or say that it is empty. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 10^6$$$) — the number of elements in the initial multiset and the number of queries, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 le a_1 le a_2 le dots le a_n le n$$$) — the elements of the multiset. The third line contains $$$q$$$ integers $$$k_1$$$, $$$k_2$$$, ..., $$$k_q$$$, each representing a query: if $$$1 le k_i le n$$$, then the $$$i$$$-th query is "insert $$$k_i$$$ into the multiset"; if $$$k_i < 0$$$, then the $$$i$$$-th query is "remove the $$$k_i$$$-th order statistics from the multiset". For this query, it is guaranteed that $$$k_i$$$ is not greater than the size of the multiset. Output If the multiset is empty after all queries, print $$$0$$$. Otherwise, print any integer that belongs to the resulting multiset. Examples Input 5 5 1 2 3 4 5 -1 -1 -1 -1 -1 Input 5 4 1 2 3 4 5 -5 -1 -3 -1 Note In the first example, all elements of the multiset are deleted. In the second example, the elements $$$5$$$, $$$1$$$, $$$4$$$, $$$2$$$ are deleted (they are listed in chronological order of their removal). In the third example, $$$6$$$ is not the only answer.
1,900
false
false
false
false
true
false
false
true
false
false
3,935
633G
Yash loves playing with trees and gets especially excited when they have something to do with prime numbers. On his 20th birthday he was granted with a rooted tree of _n_ nodes to answer queries on. Hearing of prime numbers on trees, Yash gets too intoxicated with excitement and asks you to help out and answer queries on trees for him. Tree is rooted at node 1. Each node _i_ has some value _a__i_ associated with it. Also, integer _m_ is given. There are queries of two types: 1. for given node _v_ and integer value _x_, increase all _a__i_ in the subtree of node _v_ by value _x_ 2. for given node _v_, find the number of prime numbers _p_ less than _m_, for which there exists a node _u_ in the subtree of _v_ and a non-negative integer value _k_, such that _a__u_u2009=u2009_p_u2009+u2009_m_·_k_. Input The first of the input contains two integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009100u2009000,u20091u2009≤u2009_m_u2009≤u20091000)xa0— the number of nodes in the tree and value _m_ from the problem statement, respectively. The second line consists of _n_ integers _a__i_ (0u2009≤u2009_a__i_u2009≤u2009109)xa0— initial values of the nodes. Then follow _n_u2009-u20091 lines that describe the tree. Each of them contains two integers _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_)xa0— indices of nodes connected by the _i_-th edge. Next line contains a single integer _q_ (1u2009≤u2009_q_u2009≤u2009100u2009000)xa0— the number of queries to proceed. Each of the last _q_ lines is either 1 v x or 2 v (1u2009≤u2009_v_u2009≤u2009_n_,u20090u2009≤u2009_x_u2009≤u2009109), giving the query of the first or the second type, respectively. It's guaranteed that there will be at least one query of the second type. Output For each of the queries of the second type print the number of suitable prime numbers. Examples Input 8 20 3 7 9 8 4 11 7 3 1 2 1 3 3 4 4 5 4 6 4 7 5 8 4 2 1 1 1 1 2 5 2 4 Input 5 10 8 7 5 1 0 1 2 2 3 1 5 2 4 3 1 1 0 1 1 2 2 2
2,800
true
false
false
false
true
false
false
false
false
false
7,269
1250A
Polycarp recently signed up to a new social network Berstagram. He immediately published $$$n$$$ posts there. He assigned numbers from $$$1$$$ to $$$n$$$ to all posts and published them one by one. So, just after publishing Polycarp's news feed contained posts from $$$1$$$ to $$$n$$$ — the highest post had number $$$1$$$, the next one had number $$$2$$$, ..., the lowest post had number $$$n$$$. After that he wrote down all likes from his friends. Likes were coming consecutively from the $$$1$$$-st one till the $$$m$$$-th one. You are given a sequence $$$a_1, a_2, dots, a_m$$$ ($$$1 le a_j le n$$$), where $$$a_j$$$ is the post that received the $$$j$$$-th like. News feed in Berstagram works in the following manner. Let's assume the $$$j$$$-th like was given to post $$$a_j$$$. If this post is not the highest (first) one then it changes its position with the one above. If $$$a_j$$$ is the highest post nothing changes. For example, if $$$n=3$$$, $$$m=5$$$ and $$$a=[3,2,1,3,3]$$$, then Polycarp's news feed had the following states: before the first like: $$$[1, 2, 3]$$$; after the first like: $$$[1, 3, 2]$$$; after the second like: $$$[1, 2, 3]$$$; after the third like: $$$[1, 2, 3]$$$; after the fourth like: $$$[1, 3, 2]$$$; after the fifth like: $$$[3, 1, 2]$$$. Polycarp wants to know the highest (minimum) and the lowest (maximum) positions for each post. Polycarp considers all moments of time, including the moment "before all likes". Input The first line contains two integer numbers $$$n$$$ and $$$m$$$ ($$$1 le n le 10^5$$$, $$$1 le m le 4 cdot10^5$$$) — number of posts and number of likes. The second line contains integers $$$a_1, a_2, dots, a_m$$$ ($$$1 le a_j le n$$$), where $$$a_j$$$ is the post that received the $$$j$$$-th like. Output Print $$$n$$$ pairs of integer numbers. The $$$i$$$-th line should contain the highest (minimum) and the lowest (maximum) positions of the $$$i$$$-th post. You should take into account positions at all moments of time: before all likes, after each like and after all likes. Positions are numbered from $$$1$$$ (highest) to $$$n$$$ (lowest).
1,400
false
false
true
false
false
false
false
false
false
false
4,455
51A
Cheaterius is a famous in all the Berland astrologist, magician and wizard, and he also is a liar and a cheater. One of his latest inventions is Cheaterius' amulets! They bring luck and wealth, but are rather expensive. Cheaterius makes them himself. The technology of their making is kept secret. But we know that throughout long nights Cheaterius glues together domino pairs with super glue to get squares 2u2009×u20092 which are the Cheaterius' magic amulets! That's what one of Cheaterius's amulets looks like After a hard night Cheaterius made _n_ amulets. Everyone of them represents a square 2u2009×u20092, every quarter contains 1 to 6 dots. Now he wants sort them into piles, every pile must contain similar amulets. Two amulets are called similar if they can be rotated by 90, 180 or 270 degrees so that the following condition is met: the numbers of dots in the corresponding quarters should be the same. It is forbidden to turn over the amulets. Write a program that by the given amulets will find the number of piles on Cheaterius' desk. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u20091000), where _n_ is the number of amulets. Then the amulet's descriptions are contained. Every description occupies two lines and contains two numbers (from 1 to 6) in each line. Between every pair of amulets the line "**" is located. Output Print the required number of piles. Examples Input 4 31 23 * 31 23 * 13 32 * 32 13 Input 4 51 26 * 54 35 * 25 61 * 45 53
1,300
false
false
true
false
false
false
false
false
false
false
9,719
545E
Little girl Susie accidentally found her elder brother's notebook. She has many things to do, more important than solving problems, but she found this problem too interesting, so she wanted to know its solution and decided to ask you about it. So, the problem statement is as follows. Let's assume that we are given a connected weighted undirected graph _G_u2009=u2009(_V_,u2009_E_) (here _V_ is the set of vertices, _E_ is the set of edges). The shortest-path tree from vertex _u_ is such graph _G_1u2009=u2009(_V_,u2009_E_1) that is a tree with the set of edges _E_1 that is the subset of the set of edges of the initial graph _E_, and the lengths of the shortest paths from _u_ to any vertex to _G_ and to _G_1 are the same. You are given a connected weighted undirected graph _G_ and vertex _u_. Your task is to find the shortest-path tree of the given graph from vertex _u_, the total weight of whose edges is minimum possible. Input The first line contains two numbers, _n_ and _m_ (1u2009≤u2009_n_u2009≤u20093·105, 0u2009≤u2009_m_u2009≤u20093·105) — the number of vertices and edges of the graph, respectively. Next _m_ lines contain three integers each, representing an edge — _u__i_,u2009_v__i_,u2009_w__i_ — the numbers of vertices connected by an edge and the weight of the edge (_u__i_u2009≠u2009_v__i_,u20091u2009≤u2009_w__i_u2009≤u2009109). It is guaranteed that graph is connected and that there is no more than one edge between any pair of vertices. The last line of the input contains integer _u_ (1u2009≤u2009_u_u2009≤u2009_n_) — the number of the start vertex. Output In the first line print the minimum total weight of the edges of the tree. In the next line print the indices of the edges that are included in the tree, separated by spaces. The edges are numbered starting from 1 in the order they follow in the input. You may print the numbers of the edges in any order. If there are multiple answers, print any of them. Examples Input 4 4 1 2 1 2 3 1 3 4 1 4 1 2 4 Note In the first sample there are two possible shortest path trees: with edges 1u2009–u20093 and 2u2009–u20093 (the total weight is 3); with edges 1u2009–u20092 and 2u2009–u20093 (the total weight is 2); And, for example, a tree with edges 1u2009–u20092 and 1u2009–u20093 won't be a shortest path tree for vertex 3, because the distance from vertex 3 to vertex 2 in this tree equals 3, and in the original graph it is 1.
2,000
false
true
false
false
false
false
false
false
false
true
7,658
1070G
Polycarp is an introvert person. In fact he is so much of an introvert that he plays "Monsters and Potions" board game alone. The board of the game is a row of $$$n$$$ cells. The cells are numbered from $$$1$$$ to $$$n$$$ from left to right. There are three types of cells: a cell containing a single monster, a cell containing a single potion or a blank cell (it contains neither a monster nor a potion). Polycarp has $$$m$$$ tokens representing heroes fighting monsters, which are initially located in the blank cells $$$s_1, s_2, dots, s_m$$$. Polycarp's task is to choose a single cell (rally point) and one by one move all the heroes into this cell. A rally point can be a cell of any of three types. After Policarp selects a rally point, he picks a hero and orders him to move directly to the point. Once that hero reaches the point, Polycarp picks another hero and orders him also to go to the point. And so forth, until all the heroes reach the rally point cell. While going to the point, a hero can not deviate from the direct route or take a step back. A hero just moves cell by cell in the direction of the point until he reaches it. It is possible that multiple heroes are simultaneously in the same cell. Initially the $$$i$$$-th hero has $$$h_i$$$ hit points (HP). Monsters also have HP, different monsters might have different HP. And potions also have HP, different potions might have different HP. If a hero steps into a cell which is blank (i.e. doesn't contain a monster/potion), hero's HP does not change. If a hero steps into a cell containing a monster, then the hero and the monster fight. If monster's HP is strictly higher than hero's HP, then the monster wins and Polycarp loses the whole game. If hero's HP is greater or equal to monster's HP, then the hero wins and monster's HP is subtracted from hero's HP. I.e. the hero survives if his HP drops to zero, but dies (and Polycarp looses) if his HP becomes negative due to a fight. If a hero wins a fight with a monster, then the monster disappears, and the cell becomes blank. If a hero steps into a cell containing a potion, then the hero drinks the potion immediately. As a result, potion's HP is added to hero's HP, the potion disappears, and the cell becomes blank. Obviously, Polycarp wants to win the game. It means that he must choose such rally point and the order in which heroes move, that every hero reaches the rally point and survives. I.e. Polycarp loses if a hero reaches rally point but is killed by a monster at the same time. Polycarp can use any of $$$n$$$ cells as a rally point — initially it can contain a monster, a potion, or be a blank cell with or without a hero in it. Help Polycarp write a program to choose a rally point and the order in which heroes move. Input The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 100$$$; $$$1 le m le n$$$) — length of the game board and the number of heroes on it. The following $$$m$$$ lines describe heroes. Each line contains two integers $$$s_i$$$ and $$$h_i$$$ ($$$1 le s_i le n$$$; $$$1 le h_i le 10^6$$$), where $$$s_i$$$ is the initial position and $$$h_i$$$ is the initial HP of the $$$i$$$-th hero. It is guaranteed that each cell $$$s_i$$$ is blank. It is also guaranteed that all $$$s_i$$$ are different. The following line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$-10^6 le a_j le 10^6$$$), where $$$a_j$$$ describes the $$$i$$$-th cell of the game board: $$$a_j=0$$$ means that the $$$i$$$-th cell is blank, $$$a_j<0$$$ means that the $$$i$$$-th cell contains monster with positive HP of $$$-a_j$$$, $$$a_j>0$$$ means that the $$$i$$$-th cell contains potion with $$$a_j$$$ HP. Output On the first line of the output print the index of the rally point cell. On the second line print $$$m$$$ integers — the order in which heroes should move to the rally point. Heroes are numbered from $$$1$$$ to $$$m$$$ in the order they are given in the input. If there are multiple solutions, print any of them. If it is impossible to find a rally point which can be reached by all heroes, print a single integer -1 in the output.
2,300
false
true
true
true
false
false
true
false
false
false
5,361
1451A
Problem - 1451A - 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 math *800 No tag edit access → Contest materials ") xa0— the number of test cases. The only line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 10^9$$$). Output For each test case, output the minimum number of moves required to reduce $$$n$$$ to $$$1$$$. Example Input 6 1 2 3 4 6 9 Output 0 1 2 2 2 3 Note For the test cases in the example, $$$n$$$ may be reduced to $$$1$$$ using the following operations in sequence $$$1$$$ $$$2 xrightarrow{} 1$$$ $$$3 xrightarrow{} 2 xrightarrow{} 1$$$ $$$4 xrightarrow{} 2 xrightarrow{} 1$$$ $$$6 xrightarrow{} 2 xrightarrow{} 1$$$ $$$9 xrightarrow{} 3 xrightarrow{} 2xrightarrow{} 1$$$
800
true
true
false
false
false
false
false
false
false
false
3,428
1856C
You are given an array of integers $$$a$$$ of length $$$n$$$. In one operation you: Choose an index $$$i$$$ such that $$$1 le i le n - 1$$$ and $$$a_i le a_{i + 1}$$$. Increase $$$a_i$$$ by $$$1$$$. Find the maximum possible value of $$$max(a_1, a_2, ldots a_n)$$$ that you can get after performing this operation at most $$$k$$$ times. 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 two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 1000$$$, $$$1 le k le 10^{8}$$$)xa0— the length of the array $$$a$$$ and the maximum number of operations that can be performed. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$1 le a_i le 10^{8}$$$)xa0— the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$. Note In the first test case, one possible optimal sequence of operations is: $$$[color{red}{1}, 3, 3] ightarrow [2, color{red}{3}, 3] ightarrow [color{red}{2}, 4, 3] ightarrow [color{red}{3}, 4, 3] ightarrow [4, 4, 3]$$$. In the second test case, one possible optimal sequence of operations is: $$$[1, color{red}{3}, 4, 5, 1] ightarrow [1, color{red}{4}, 4, 5, 1] ightarrow [1, 5, color{red}{4}, 5, 1] ightarrow [1, 5, color{red}{5}, 5, 1] ightarrow [1, color{red}{5}, 6, 5, 1] ightarrow [1, color{red}{6}, 6, 5, 1] ightarrow [1, 7, 6, 5, 1]$$$.
1,600
false
false
false
true
true
false
true
true
false
false
1,148
1100C
NN is an experienced internet user and that means he spends a lot of time on the social media. Once he found the following image on the Net, which asked him to compare the sizes of inner circles: It turned out that the circles are equal. NN was very surprised by this fact, so he decided to create a similar picture himself. He managed to calculate the number of outer circles $$$n$$$ and the radius of the inner circle $$$r$$$. NN thinks that, using this information, you can exactly determine the radius of the outer circles $$$R$$$ so that the inner circle touches all of the outer ones externally and each pair of neighboring outer circles also touches each other. While NN tried very hard to guess the required radius, he didn't manage to do that. Help NN find the required radius for building the required picture. Input The first and the only line of the input file contains two numbers $$$n$$$ and $$$r$$$ ($$$3 leq n leq 100$$$, $$$1 leq r leq 100$$$)xa0— the number of the outer circles and the radius of the inner circle respectively. Output Output a single number $$$R$$$xa0— the radius of the outer circle required for building the required picture. Your answer will be accepted if its relative or absolute error does not exceed $$$10^{-6}$$$. Formally, if your answer is $$$a$$$ and the jury's answer is $$$b$$$. Your answer is accepted if and only when $$$frac{a-b}{max(1, b)} le 10^{-6}$$$.
1,200
true
false
false
false
false
false
false
true
false
false
5,204
512D
Fox Ciel is going to travel to New Foxland during this summer. New Foxland has _n_ attractions that are linked by _m_ undirected roads. Two attractions are called adjacent if they are linked by a road. Fox Ciel has _k_ days to visit this city and each day she will visit exactly one attraction. There is one important rule in New Foxland: you can't visit an attraction if it has more than one adjacent attraction that you haven't visited yet. At the beginning Fox Ciel haven't visited any attraction. During her travelling she may move aribtrarly between attraction. After visiting attraction _a_, she may travel to any attraction _b_ satisfying conditions above that hasn't been visited yet, even if it is not reachable from _a_ by using the roads (Ciel uses boat for travelling between attractions, so it is possible). She wants to know how many different travelling plans she can make. Calculate this number modulo 109u2009+u20099 for every _k_ from 0 to _n_ since she hasn't decided for how many days she is visiting New Foxland. Input First line contains two integers: _n_, _m_ (1u2009≤u2009_n_u2009≤u2009100, ), the number of attractions and number of undirected roads. Then next _m_ lines each contain two integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_ and _a__i_u2009≠u2009_b__i_), describing a road. There is no more than one road connecting each pair of attractions. Output Output _n_u2009+u20091 integer: the number of possible travelling plans modulo 109u2009+u20099 for all _k_ from 0 to _n_. Examples Input 12 11 2 3 4 7 4 5 5 6 4 6 6 12 5 12 5 8 8 9 10 8 11 9 Output 1 6 31 135 483 1380 3060 5040 5040 0 0 0 0 Output 1 13 156 1716 17160 154440 1235520 8648640 51891840 259459200 37836791 113510373 227020746 227020746 Note In the first sample test for _k_u2009=u20093 there are 4 travelling plans: {1,u20092,u20093},u2009{1,u20093,u20092},u2009{3,u20091,u20092},u2009{3,u20092,u20091}. In the second sample test Ciel can't visit any attraction in the first day, so for _k_u2009>u20090 the answer is 0. In the third sample test Foxlands look like this:
2,900
false
false
false
true
false
false
false
false
false
false
7,795
1569B
A chess tournament will be held soon, where $$$n$$$ chess players will take part. Every participant will play one game against every other participant. Each game ends in either a win for one player and a loss for another player, or a draw for both players. Each of the players has their own expectations about the tournament, they can be one of two types: 1. a player wants not to lose any game (i.u2009e. finish the tournament with zero losses); 2. a player wants to win at least one game. You have to determine if there exists an outcome for all the matches such that all the players meet their expectations. If there are several possible outcomes, print any of them. If there are none, report that it's impossible. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 200$$$)xa0— the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$2 le n le 50$$$)xa0— the number of chess players. The second line contains the string $$$s$$$ ($$$s = n$$$; $$$s_i in {1, 2}$$$). If $$$s_i = 1$$$, then the $$$i$$$-th player has expectations of the first type, otherwise of the second type. Output For each test case, print the answer in the following format: In the first line, print NO if it is impossible to meet the expectations of all players. Otherwise, print YES, and the matrix of size $$$n imes n$$$ in the next $$$n$$$ lines. The matrix element in the $$$i$$$-th row and $$$j$$$-th column should be equal to: +, if the $$$i$$$-th player won in a game against the $$$j$$$-th player; -, if the $$$i$$$-th player lost in a game against the $$$j$$$-th player; =, if the $$$i$$$-th and $$$j$$$-th players' game resulted in a draw; X, if $$$i = j$$$. Example Output YES X== =X= ==X NO YES X--+ +X++ +-X- --+X
1,000
false
false
false
false
false
true
false
false
false
false
2,797
1759A
You talked to Polycarp and asked him a question. You know that when he wants to answer "yes", he repeats Yes many times in a row. Because of the noise, you only heard part of the answerxa0— some substring of it. That is, if he answered YesYes, then you could hear esY, YesYes, sYes, e, but you couldn't Yess, YES or se. Determine if it is true that the given string $$$s$$$ is a substring of YesYesYes... (Yes repeated many times in a row). Input The first line of input data contains the singular $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases in the test. Each test case is described by a single string of Latin letters $$$s$$$ ($$$1 le s le 50$$$)xa0— the part of Polycarp's answer that you heard, where $$$s$$$ — is the length of the string $$$s$$$. Output Output $$$t$$$ lines, each of which is the answer to the corresponding test case. As an answer, output "YES" if the specified string $$$s$$$ is a substring of the string YesYesYes...Yes (the number of words Yes is arbitrary), and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer). Example Input 12 YES esYes codeforces es se YesY esYesYesYesYesYesYe seY Yess sY o Yes Output NO YES NO YES NO YES YES NO NO YES NO YES
800
false
false
true
false
false
false
false
false
false
false
1,762
869C
— This is not playing but duty as allies of justice, Nii-chan! — Not allies but justice itself, Onii-chan! With hands joined, go everywhere at a speed faster than our thoughts! This time, the Fire Sistersxa0— Karen and Tsukihixa0— is heading for somewhere they've never reachedxa0— water-surrounded islands! There are three clusters of islands, conveniently coloured red, blue and purple. The clusters consist of _a_, _b_ and _c_ distinct islands respectively. Bridges have been built between some (possibly all or none) of the islands. A bridge bidirectionally connects two different islands and has length 1. For any two islands of the same colour, either they shouldn't be reached from each other through bridges, or the shortest distance between them is at least 3, apparently in order to prevent oddities from spreading quickly inside a cluster. The Fire Sisters are ready for the unknown, but they'd also like to test your courage. And you're here to figure out the number of different ways to build all bridges under the constraints, and give the answer modulo 998u2009244u2009353. Two ways are considered different if a pair of islands exist, such that there's a bridge between them in one of them, but not in the other. Input The first and only line of input contains three space-separated integers _a_, _b_ and _c_ (1u2009≤u2009_a_,u2009_b_,u2009_c_u2009≤u20095u2009000)xa0— the number of islands in the red, blue and purple clusters, respectively. Note In the first example, there are 3 bridges that can possibly be built, and no setup of bridges violates the restrictions. Thus the answer is 23u2009=u20098. In the second example, the upper two structures in the figure below are instances of valid ones, while the lower two are invalid due to the blue and purple clusters, respectively.
1,800
true
false
false
true
false
false
false
false
false
false
6,264
1613C
Monocarp is playing yet another computer game. In this game, his character has to kill a dragon. The battle with the dragon lasts $$$100^{500}$$$ seconds, during which Monocarp attacks the dragon with a poisoned dagger. The $$$i$$$-th attack is performed at the beginning of the $$$a_i$$$-th second from the battle start. The dagger itself does not deal damage, but it applies a poison effect on the dragon, which deals $$$1$$$ damage during each of the next $$$k$$$ seconds (starting with the same second when the dragon was stabbed by the dagger). However, if the dragon has already been poisoned, then the dagger updates the poison effect (i.e. cancels the current poison effect and applies a new one). For example, suppose $$$k = 4$$$, and Monocarp stabs the dragon during the seconds $$$2$$$, $$$4$$$ and $$$10$$$. Then the poison effect is applied at the start of the $$$2$$$-nd second and deals $$$1$$$ damage during the $$$2$$$-nd and $$$3$$$-rd seconds; then, at the beginning of the $$$4$$$-th second, the poison effect is reapplied, so it deals exactly $$$1$$$ damage during the seconds $$$4$$$, $$$5$$$, $$$6$$$ and $$$7$$$; then, during the $$$10$$$-th second, the poison effect is applied again, and it deals $$$1$$$ damage during the seconds $$$10$$$, $$$11$$$, $$$12$$$ and $$$13$$$. In total, the dragon receives $$$10$$$ damage. Monocarp knows that the dragon has $$$h$$$ hit points, and if he deals at least $$$h$$$ damage to the dragon during the battle — he slays the dragon. Monocarp has not decided on the strength of the poison he will use during the battle, so he wants to find the minimum possible value of $$$k$$$ (the number of seconds the poison effect lasts) that is enough to deal at least $$$h$$$ damage to the dragon. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The first line of the test case contains two integers $$$n$$$ and $$$h$$$ ($$$1 le n le 100; 1 le h le 10^{18}$$$)xa0— the number of Monocarp's attacks and the amount of damage that needs to be dealt. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 le a_i le 10^9; a_i < a_{i + 1}$$$), where $$$a_i$$$ is the second when the $$$i$$$-th attack is performed. Output For each test case, print a single integerxa0— the minimum value of the parameter $$$k$$$, such that Monocarp will cause at least $$$h$$$ damage to the dragon. Example Input 4 2 5 1 5 3 10 2 4 10 5 3 1 2 4 5 7 4 1000 3 25 64 1337 Note In the first example, for $$$k=3$$$, damage is dealt in seconds $$$[1, 2, 3, 5, 6, 7]$$$. In the second example, for $$$k=4$$$, damage is dealt in seconds $$$[2, 3, 4, 5, 6, 7, 10, 11, 12, 13]$$$. In the third example, for $$$k=1$$$, damage is dealt in seconds $$$[1, 2, 4, 5, 7]$$$.
1,200
false
false
false
false
false
false
false
true
false
false
2,574
1039E
While some people enjoy spending their time solving programming contests, Dina prefers taking beautiful pictures. As soon as Byteland Botanical Garden announced Summer Oenothera Exhibition she decided to test her new camera there. The exhibition consists of $$$l = 10^{100}$$$ Oenothera species arranged in a row and consecutively numbered with integers from $$$0$$$ to $$$l - 1$$$. Camera lens allows to take a photo of $$$w$$$ species on it, i.e. Dina can take a photo containing flowers with indices from $$$x$$$ to $$$x + w - 1$$$ for some integer $$$x$$$ between $$$0$$$ and $$$l - w$$$. We will denote such photo with $$$[x, x + w - 1]$$$. She has taken $$$n$$$ photos, the $$$i$$$-th of which (in chronological order) is $$$[x_i, x_i + w - 1]$$$ in our notation. She decided to build a time-lapse video from these photos once she discovered that Oenothera blossoms open in the evening. Dina takes each photo and truncates it, leaving its segment containing exactly $$$k$$$ flowers, then she composes a video of these photos keeping their original order and voilà, a beautiful artwork has been created! A scene is a contiguous sequence of photos such that the set of flowers on them is the same. The change between two scenes is called a cut. For example, consider the first photo contains flowers $$$[1, 5]$$$, the second photo contains flowers $$$[3, 7]$$$ and the third photo contains flowers $$$[8, 12]$$$. If $$$k = 3$$$, then Dina can truncate the first and the second photo into $$$[3, 5]$$$, and the third photo into $$$[9, 11]$$$. First two photos form a scene, third photo also forms a scene and the transition between these two scenes which happens between the second and the third photos is a cut. If $$$k = 4$$$, then each of the transitions between photos has to be a cut. Dina wants the number of cuts to be as small as possible. Please help her! Calculate the minimum possible number of cuts for different values of $$$k$$$. Input The first line contains three positive integer $$$n$$$, $$$w$$$, $$$q$$$ ($$$1 leq n, q leq 100,000$$$, $$$1 leq w leq 10^9$$$)xa0— the number of taken photos, the number of flowers on a single photo and the number of queries. Next line contains $$$n$$$ non-negative integers $$$x_i$$$ ($$$0 le x_i le 10^9$$$)xa0— the indices of the leftmost flowers on each of the photos. Next line contains $$$q$$$ positive integers $$$k_i$$$ ($$$1 le k_i le w$$$)xa0— the values of $$$k$$$ for which you have to solve the problem. It's guaranteed that all $$$k_i$$$ are distinct.
3,400
false
false
false
false
true
false
false
false
false
false
5,498
191A
The ancient Berlanders believed that the longer the name, the more important its bearer is. Thus, Berland kings were famous for their long names. But long names are somewhat inconvenient, so the Berlanders started to abbreviate the names of their kings. They called every king by the first letters of its name. Thus, the king, whose name was Victorious Vasily Pupkin, was always called by the berlanders VVP. In Berland over its long history many dynasties of kings replaced each other, but they were all united by common traditions. Thus, according to one Berland traditions, to maintain stability in the country, the first name of the heir should be the same as the last name his predecessor (hence, the first letter of the abbreviated name of the heir coincides with the last letter of the abbreviated name of the predecessor). Berlanders appreciate stability, so this tradition has never been broken. Also Berlanders like perfection, so another tradition requires that the first name of the first king in the dynasty coincides with the last name of the last king in this dynasty (hence, the first letter of the abbreviated name of the first king coincides with the last letter of the abbreviated name of the last king). This tradition, of course, has also been always observed. The name of a dynasty is formed by very simple rules: we take all the short names of the kings in the order in which they ruled, and write them in one line. Thus, a dynasty of kings "ab" and "ba" is called "abba", and the dynasty, which had only the king "abca", is called "abca". Vasya, a historian, has recently found a list of abbreviated names of all Berland kings and their relatives. Help Vasya to find the maximally long name of the dynasty that could have existed in Berland. Note that in his list all the names are ordered by the time, that is, if name _A_ is earlier in the list than _B_, then if _A_ and _B_ were kings, then king _A_ ruled before king _B_. Note In the first sample two dynasties can exist: the one called "abcca" (with the first and second kings) and the one called "abccba" (with the first and third kings). In the second sample there aren't acceptable dynasties. The only dynasty in the third sample consists of one king, his name is "c".
1,500
false
false
false
true
false
false
false
false
false
false
9,077
837A
Problem - 837A - Codeforces =============== xa0 — length of the text. The second line contains text of single-space separated words _s_1,u2009_s_2,u2009...,u2009_s__i_, consisting only of small and capital Latin letters. Output Print one integer number — volume of text. Examples Input 7 NonZERO Output 5 Input 24 this is zero answer text Output 0 Input 24 Harbour Space University Output 1 Note In the first example there is only one word, there are 5 capital letters in it. In the second example all of the words contain 0 capital letters.
800
false
false
true
false
false
false
false
false
false
false
6,421
383B
Iahub got lost in a very big desert. The desert can be represented as a _n_u2009×u2009_n_ square matrix, where each cell is a zone of the desert. The cell (_i_,u2009_j_) represents the cell at row _i_ and column _j_ (1u2009≤u2009_i_,u2009_j_u2009≤u2009_n_). Iahub can go from one cell (_i_,u2009_j_) only down or right, that is to cells (_i_u2009+u20091,u2009_j_) or (_i_,u2009_j_u2009+u20091). Also, there are _m_ cells that are occupied by volcanoes, which Iahub cannot enter. Iahub is initially at cell (1,u20091) and he needs to travel to cell (_n_,u2009_n_). Knowing that Iahub needs 1 second to travel from one cell to another, find the minimum time in which he can arrive in cell (_n_,u2009_n_). Input The first line contains two integers _n_ (1u2009≤u2009_n_u2009≤u2009109) and _m_ (1u2009≤u2009_m_u2009≤u2009105). Each of the next _m_ lines contains a pair of integers, _x_ and _y_ (1u2009≤u2009_x_,u2009_y_u2009≤u2009_n_), representing the coordinates of the volcanoes. Consider matrix rows are numbered from 1 to _n_ from top to bottom, and matrix columns are numbered from 1 to _n_ from left to right. There is no volcano in cell (1,u20091). No two volcanoes occupy the same location. Output Print one integer, the minimum time in which Iahub can arrive at cell (_n_,u2009_n_). If no solution exists (there is no path to the final cell), print -1. Examples Input 7 8 1 6 2 6 3 5 3 6 4 3 5 1 5 2 5 3 Note Consider the first sample. A possible road is: (1,u20091) u2009→u2009 (1,u20092) u2009→u2009 (2,u20092) u2009→u2009 (2,u20093) u2009→u2009 (3,u20093) u2009→u2009 (3,u20094) u2009→u2009 (4,u20094).
2,500
false
false
true
false
false
false
false
true
true
false
8,297
1475G
Polycarp found on the street an array $$$a$$$ of $$$n$$$ elements. Polycarp invented his criterion for the beauty of an array. He calls an array $$$a$$$ beautiful if at least one of the following conditions must be met for each different pair of indices $$$i e j$$$: $$$a_i$$$ is divisible by $$$a_j$$$; or $$$a_j$$$ is divisible by $$$a_i$$$. For example, if: $$$n=5$$$ and $$$a=[7, 9, 3, 14, 63]$$$, then the $$$a$$$ array is not beautiful (for $$$i=4$$$ and $$$j=2$$$, none of the conditions above is met); $$$n=3$$$ and $$$a=[2, 14, 42]$$$, then the $$$a$$$ array is beautiful; $$$n=4$$$ and $$$a=[45, 9, 3, 18]$$$, then the $$$a$$$ array is not beautiful (for $$$i=1$$$ and $$$j=4$$$ none of the conditions above is met); Ugly arrays upset Polycarp, so he wants to remove some elements from the array $$$a$$$ so that it becomes beautiful. Help Polycarp determine the smallest number of elements to remove to make the array $$$a$$$ beautiful. Input The first line contains one integer $$$t$$$ ($$$1 leq t leq 10$$$)xa0— the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$)xa0— the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ numbers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 2 cdot 10^5$$$)xa0— elements of the array $$$a$$$. Output For each test case output one integerxa0— the minimum number of elements that must be removed to make the array $$$a$$$ beautiful. Example Input 4 5 7 9 3 14 63 3 2 14 42 4 45 9 3 18 3 2 2 8 Note In the first test case, removing $$$7$$$ and $$$14$$$ will make array $$$a$$$ beautiful. In the second test case, the array $$$a$$$ is already beautiful. In the third test case, removing one of the elements $$$45$$$ or $$$18$$$ will make the array $$$a$$$ beautiful. In the fourth test case, the array $$$a$$$ is beautiful.
1,900
true
false
false
true
false
false
false
false
true
false
3,298
1286B
Evlampiy was gifted a rooted tree. The vertices of the tree are numbered from $$$1$$$ to $$$n$$$. Each of its vertices also has an integer $$$a_i$$$ written on it. For each vertex $$$i$$$, Evlampiy calculated $$$c_i$$$xa0— the number of vertices $$$j$$$ in the subtree of vertex $$$i$$$, such that $$$a_j < a_i$$$. Illustration for the second example, the first integer is $$$a_i$$$ and the integer in parentheses is $$$c_i$$$ After the new year, Evlampiy could not remember what his gift was! He remembers the tree and the values of $$$c_i$$$, but he completely forgot which integers $$$a_i$$$ were written on the vertices. Help him to restore initial integers! Input The first line contains an integer $$$n$$$ $$$(1 leq n leq 2000)$$$ — the number of vertices in the tree. The next $$$n$$$ lines contain descriptions of vertices: the $$$i$$$-th line contains two integers $$$p_i$$$ and $$$c_i$$$ ($$$0 leq p_i leq n$$$; $$$0 leq c_i leq n-1$$$), where $$$p_i$$$ is the parent of vertex $$$i$$$ or $$$0$$$ if vertex $$$i$$$ is root, and $$$c_i$$$ is the number of vertices $$$j$$$ in the subtree of vertex $$$i$$$, such that $$$a_j < a_i$$$. It is guaranteed that the values of $$$p_i$$$ describe a rooted tree with $$$n$$$ vertices. Output If a solution exists, in the first line print "YES", and in the second line output $$$n$$$ integers $$$a_i$$$ $$$(1 leq a_i leq {10}^{9})$$$. If there are several solutions, output any of them. One can prove that if there is a solution, then there is also a solution in which all $$$a_i$$$ are between $$$1$$$ and $$$10^9$$$. If there are no solutions, print "NO". Examples Input 5 0 1 1 3 2 1 3 0 2 0
1,800
false
true
false
false
true
true
false
false
false
true
4,260
1827B1
The only difference between this problem and the hard version is the constraints on $$$t$$$ and $$$n$$$. You are given an array $$$a$$$, consisting of $$$n$$$ distinct integers $$$a_1, a_2, ldots, a_n$$$. Define the beauty of an array $$$p_1, p_2, ldots p_k$$$ as the minimum amount of time needed to sort this array using an arbitrary number of range-sort operations. In each range-sort operation, you will do the following: Choose two integers $$$l$$$ and $$$r$$$ ($$$1 le l < r le k$$$). Sort the subarray $$$p_l, p_{l + 1}, ldots, p_r$$$ in $$$r - l$$$ seconds. Please calculate the sum of beauty over all subarrays of array $$$a$$$. A subarray of an array is defined as a sequence of consecutive elements of the array. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 5 cdot 10^3$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 5 cdot 10^3$$$)xa0— the length of the array $$$a$$$. The second line of each test case consists of $$$n$$$ integers $$$a_1,a_2,ldots, a_n$$$ ($$$1le a_ile 10^9$$$). It is guaranteed that all elements of $$$a$$$ are pairwise distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 cdot 10^3$$$. Output For each test case, output the sum of beauty over all subarrays of array $$$a$$$. Example Input 5 2 6 4 3 3 10 6 4 4 8 7 2 5 9 8 2 4 6 12 2 6 13 3 15 5 10 8 16 9 11 18 Note In the first test case: The subarray $$$[6]$$$ is already sorted, so its beauty is $$$0$$$. The subarray $$$[4]$$$ is already sorted, so its beauty is $$$0$$$. You can sort the subarray $$$[6, 4]$$$ in one operation by choosing $$$l = 1$$$ and $$$r = 2$$$. Its beauty is equal to $$$1$$$. The sum of beauty over all subarrays of the given array is equal to $$$0 + 0 + 1 = 1$$$. In the second test case: The subarray $$$[3]$$$ is already sorted, so its beauty is $$$0$$$. The subarray $$$[10]$$$ is already sorted, so its beauty is $$$0$$$. The subarray $$$[6]$$$ is already sorted, so its beauty is $$$0$$$. The subarray $$$[3, 10]$$$ is already sorted, so its beauty is $$$0$$$. You can sort the subarray $$$[10, 6]$$$ in one operation by choosing $$$l = 1$$$ and $$$r = 2$$$. Its beauty is equal to $$$2 - 1 = 1$$$. You can sort the subarray $$$[3, 10, 6]$$$ in one operation by choosing $$$l = 2$$$ and $$$r = 3$$$. Its beauty is equal to $$$3 - 2 = 1$$$. The sum of beauty over all subarrays of the given array is equal to $$$0 + 0 + 0 + 0 + 1 + 1 = 2$$$.
2,000
false
true
false
true
false
false
false
true
false
false
1,322
1815B
This is an interactive problem. There is a hidden permutation $$$p_1, p_2, dots, p_n$$$. Consider an undirected graph with $$$n$$$ nodes only with no edges. You can make two types of queries: 1. Specify an integer $$$x$$$ satisfying $$$2 le x le 2n$$$. For all integers $$$i$$$ ($$$1 le i le n$$$) such that $$$1 le x-i le n$$$, an edge between node $$$i$$$ and node $$$x-i$$$ will be added. 2. Query the number of edges in the shortest path between node $$$p_i$$$ and node $$$p_j$$$. As the answer to this question you will get the number of edges in the shortest path if such a path exists, or $$$-1$$$ if there is no such path. Note that you can make both types of queries in any order. Within $$$2n$$$ queries (including type $$$1$$$ and type $$$2$$$), guess two possible permutations, at least one of which is $$$p_1, p_2, dots, p_n$$$. You get accepted if at least one of the permutations is correct. You are allowed to guess the same permutation twice. A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 10^3$$$) — the length of the permutation. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^3$$$. Interaction The interaction for each test case begins by reading the integer $$$n$$$. Then, make at most $$$2n$$$ queries: If you want to make a type $$$1$$$ query, output "+ x". $$$x$$$ must be an integer between $$$2$$$ and $$$2n$$$ inclusive. After doing that read $$$1$$$ or $$$-2$$$. If you read $$$1$$$ your query was valid, otherwise it was invalid or you exceed the limit of queries, and your program must terminate immediately to receive a Wrong Answer verdict. If you want to make a type $$$2$$$ query, output "? i j". $$$i$$$ and $$$j$$$ must be integers between $$$1$$$ and $$$n$$$ inclusive. After that, read in a single integer $$$r$$$ ($$$-1 le r le n$$$) — the answer to your query. If you receive the integer $$$−2$$$ instead of an answer, it means your program has made an invalid query, or has exceeded the limit of queries. Your program must terminate immediately to receive a Wrong Answer verdict. At any point of the interaction, if you want to guess two permutations, output "! $$$p_{1,1}$$$ $$$p_{1,2}$$$ $$$dots$$$ $$$p_{1,n}$$$ $$$p_{2,1}$$$ $$$p_{2,2}$$$ $$$dots$$$ $$$p_{2,n}$$$". Note that you should output the two permutations on the same line, and no exclamation mark is needed to separate the two permutations. After doing that read $$$1$$$ or $$$-2$$$. If you read $$$1$$$ your answer was correct, otherwise it was incorrect and your program must terminate immediately to receive a Wrong Answer verdict. After that, move on to the next test case, or terminate the program if there are none. Note that reporting the answer does not count as a query. Note that even if you output a correct permutation, the second permutation should be a permutation and not an arbitrary array. At any point, if you continue interaction after reading in the integer $$$-2$$$, you can get an arbitrary verdict because your solution will continue to read from a closed stream. After printing a query or the answer do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see the documentation for other languages. Interactor is non-adaptive. This means that all permutations are fixed before the interaction starts. Hacks To make a hack, use the following format. The first line should contain a single integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. The first line of each test case should contain a single integer $$$n$$$ ($$$2 le n le 10^3$$$) — the length of the permutation. The second line of each test case should contain $$$n$$$ distinct integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le n$$$) — the hidden permutation. The sum of $$$n$$$ over all test cases should not exceed $$$10^3$$$. Example Input 2 6 1 1 1 1 1 2 -1 1 2 1 Output + 12 + 2 + 3 ? 1 3 + 5 ? 1 5 ? 4 5 ! 1 4 2 5 3 6 1 2 3 4 5 6 ! 1 2 2 1 Note In the first test case, $$$n=6$$$ and the hidden permutation $$$p = [1,4,2,5,3,6]$$$. Firstly, make a type $$$1$$$ query on $$$x=12, 2, 3$$$ respectively. This adds four edges to the graph in total: An edge that connects node $$$6$$$ and node $$$6$$$. An edge that connects node $$$1$$$ and node $$$1$$$. An edge that connects node $$$1$$$ and node $$$2$$$. An edge that connects node $$$2$$$ and node $$$1$$$. Since all of these queries are valid, the interactor returns $$$1$$$ after each of them. Then, query the number of edges in the shortest path between node $$$p_1 = 1$$$ and $$$p_3 = 2$$$, which is equal to $$$1$$$. Then, make a type $$$1$$$ query on $$$x=5$$$. This adds four edges to the graph in total: An edge that connects node $$$1$$$ and node $$$4$$$. An edge that connects node $$$2$$$ and node $$$3$$$. An edge that connects node $$$3$$$ and node $$$2$$$. An edge that connects node $$$4$$$ and node $$$1$$$. Since this query is valid, the interactor returns $$$1$$$. Then, query the number of edges in the shortest path between node $$$p_1 = 1$$$ and $$$p_5 = 3$$$, which is equal to $$$2$$$. Then, query the number of edges in the shortest path between node $$$p_4 = 5$$$ and $$$p_5 = 3$$$. Such a path doesn't exist, therefore the interactor returns $$$-1$$$. Afterwards, due to some magic, two possible permutations that can be $$$p$$$ are determined: the first permutation is $$$[1,4,2,5,3,6]$$$ and the second permutation is $$$[1,2,3,4,5,6]$$$. Since the first permutation is equal to the hidden permutation, this test case is solved correctly. In total, $$$7$$$ queries are used, which is within the limit of $$$2 cdot 6 = 12$$$ queries. Since the answer is correct, the interactor returns $$$1$$$. In the second test case, $$$n=2$$$ and the hidden permutation is $$$p = [2,1]$$$. Since there are only $$$2! = 2$$$ possible permutations, no queries are needed. It is sufficient to just output the two permutations, $$$[1,2]$$$ and $$$[2,1]$$$. In total, $$$0$$$ queries are used, which is within the limit of $$$2 cdot 2 = 4$$$ queries. Since the answer is correct, the interactor returns $$$1$$$.
2,000
false
false
true
false
false
true
true
false
false
true
1,380
745A
Hongcow is learning to spell! One day, his teacher gives him a word that he needs to learn to spell. Being a dutiful student, he immediately learns how to spell the word. Hongcow has decided to try to make new words from this one. He starts by taking the word he just learned how to spell, and moves the last character of the word to the beginning of the word. He calls this a cyclic shift. He can apply cyclic shift many times. For example, consecutively applying cyclic shift operation to the word "abracadabra" Hongcow will get words "aabracadabr", "raabracadab" and so on. Hongcow is now wondering how many distinct words he can generate by doing the cyclic shift arbitrarily many times. The initial string is also counted. Input The first line of input will be a single string _s_ (1u2009≤u2009_s_u2009≤u200950), the word Hongcow initially learns how to spell. The string _s_ consists only of lowercase English letters ('a'–'z'). Output Output a single integer equal to the number of distinct strings that Hongcow can obtain by applying the cyclic shift arbitrarily many times to the given string. Note For the first sample, the strings Hongcow can generate are "abcd", "dabc", "cdab", and "bcda". For the second sample, no matter how many times Hongcow does the cyclic shift, Hongcow can only generate "bbb". For the third sample, the two strings Hongcow can generate are "yzyz" and "zyzy".
900
false
false
true
false
false
false
false
false
false
false
6,819
526A
In this problem you will meet the simplified model of game King of Thieves. In a new ZeptoLab game called "King of Thieves" your aim is to reach a chest with gold by controlling your character, avoiding traps and obstacles on your way. An interesting feature of the game is that you can design your own levels that will be available to other players. Let's consider the following simple design of a level. A dungeon consists of _n_ segments located at a same vertical level, each segment is either a platform that character can stand on, or a pit with a trap that makes player lose if he falls into it. All segments have the same length, platforms on the scheme of the level are represented as '*' and pits are represented as '.'. One of things that affects speedrun characteristics of the level is a possibility to perform a series of consecutive jumps of the same length. More formally, when the character is on the platform number _i_1, he can make a sequence of jumps through the platforms _i_1u2009<u2009_i_2u2009<u2009...u2009<u2009_i__k_, if _i_2u2009-u2009_i_1u2009=u2009_i_3u2009-u2009_i_2u2009=u2009...u2009=u2009_i__k_u2009-u2009_i__k_u2009-u20091. Of course, all segments _i_1,u2009_i_2,u2009... _i__k_ should be exactly the platforms, not pits. Let's call a level to be good if you can perform a sequence of four jumps of the same length or in the other words there must be a sequence _i_1,u2009_i_2,u2009...,u2009_i_5, consisting of five platforms so that the intervals between consecutive platforms are of the same length. Given the scheme of the level, check if it is good. Output If the level is good, print the word "yes" (without the quotes), otherwise print the word "no" (without the quotes). Note In the first sample test you may perform a sequence of jumps through platforms 2,u20095,u20098,u200911,u200914.
1,300
false
false
true
false
false
false
true
false
false
false
7,731
630E
Developing tools for creation of locations maps for turn-based fights in a new game, Petya faced the following problem. A field map consists of hexagonal cells. Since locations sizes are going to be big, a game designer wants to have a tool for quick filling of a field part with identical enemy units. This action will look like following: a game designer will select a rectangular area on the map, and each cell whose center belongs to the selected rectangle will be filled with the enemy unit. More formally, if a game designer selected cells having coordinates (_x_1,u2009_y_1) and (_x_2,u2009_y_2), where _x_1u2009≤u2009_x_2 and _y_1u2009≤u2009_y_2, then all cells having center coordinates (_x_,u2009_y_) such that _x_1u2009≤u2009_x_u2009≤u2009_x_2 and _y_1u2009≤u2009_y_u2009≤u2009_y_2 will be filled. Orthogonal coordinates system is set up so that one of cell sides is parallel to _OX_ axis, all hexagon centers have integer coordinates and for each integer _x_ there are cells having center with such _x_ coordinate and for each integer _y_ there are cells having center with such _y_ coordinate. It is guaranteed that difference _x_2u2009-u2009_x_1 is divisible by 2. Working on the problem Petya decided that before painting selected units he wants to output number of units that will be painted on the map. Help him implement counting of these units before painting. Input The only line of input contains four integers _x_1,u2009_y_1,u2009_x_2,u2009_y_2 (u2009-u2009109u2009≤u2009_x_1u2009≤u2009_x_2u2009≤u2009109,u2009u2009-u2009109u2009≤u2009_y_1u2009≤u2009_y_2u2009≤u2009109) — the coordinates of the centers of two cells. Output Output one integer — the number of cells to be filled.
1,900
true
false
false
false
false
false
false
false
false
false
7,300
935E
Ancient Egyptians are known to have understood difficult concepts in mathematics. The ancient Egyptian mathematician Ahmes liked to write a kind of arithmetic expressions on papyrus paper which he called as Ahmes arithmetic expression. An Ahmes arithmetic expression can be defined as: "_d_" is an Ahmes arithmetic expression, where _d_ is a one-digit positive integer; "(_E_1u2009_op_u2009_E_2)" is an Ahmes arithmetic expression, where _E_1 and _E_2 are valid Ahmes arithmetic expressions (without spaces) and _op_ is either plus (u2009+u2009) or minus (u2009-u2009). For example 5, (1-1) and ((1+(2-3))-5) are valid Ahmes arithmetic expressions. On his trip to Egypt, Fafa found a piece of papyrus paper having one of these Ahmes arithmetic expressions written on it. Being very ancient, the papyrus piece was very worn out. As a result, all the operators were erased, keeping only the numbers and the brackets. Since Fafa loves mathematics, he decided to challenge himself with the following task: Given the number of plus and minus operators in the original expression, find out the maximum possible value for the expression on the papyrus paper after putting the plus and minus operators in the place of the original erased operators. Input The first line contains a string _E_ (1u2009≤u2009_E_u2009≤u2009104) — a valid Ahmes arithmetic expression. All operators are erased and replaced with '?'. The second line contains two space-separated integers _P_ and _M_ (0u2009≤u2009_min_(_P_,u2009_M_)u2009≤u2009100) — the number of plus and minus operators, respectively. It is guaranteed that _P_u2009+u2009_M_u2009=u2009 the number of erased operators. Output Print one line containing the answer to the problem. Examples Input ((1?(5?7))?((6?2)?7)) 3 2 Input ((1?(5?7))?((6?2)?7)) 2 3 Note The first sample will be (1u2009+u20091)u2009u2009=u2009u20092. The second sample will be (2u2009+u2009(1u2009-u20092))u2009u2009=u2009u20091. The third sample will be ((1u2009-u2009(5u2009-u20097))u2009+u2009((6u2009+u20092)u2009+u20097))u2009u2009=u2009u200918. The fourth sample will be ((1u2009+u2009(5u2009+u20097))u2009-u2009((6u2009-u20092)u2009-u20097))u2009u2009=u2009u200916.
2,300
false
false
false
true
false
false
false
false
false
false
5,960
297D
Even polar bears feel cold when lying on the ice. Therefore, a polar bear Alice is going to make a carpet. The carpet can be viewed as a grid with height _h_ and width _w_. Then the grid is divided into _h_u2009×u2009_w_ squares. Alice is going to assign one of _k_ different colors to each square. The colors are numbered from 1 to _k_. She may choose not to use all of the colors. However, there are some restrictions. For every two adjacent squares (squares that shares an edge) _x_ and _y_, there is a color constraint in one of the forms: _color_(_x_)u2009=u2009_color_(_y_), or _color_(_x_)u2009≠u2009_color_(_y_). Example of the color constraints: Ideally, Alice wants to satisfy all color constraints. But again, life in the Arctic is hard. It is not always possible to satisfy all color constraints. Fortunately, she will still be happy if at least of the color constraints are satisfied. If she has 4 colors she can color the carpet in the following way: And she is happy because of the color constraints are satisfied, and . Your task is to help her color the carpet. Input The first line contains three integers _h_,u2009_w_,u2009_k_ (2u2009≤u2009_h_,u2009_w_u2009≤u20091000,u20091u2009≤u2009_k_u2009≤u2009_w_·_h_). The next 2_h_u2009-u20091 lines describe the color constraints from top to bottom, left to right. They contain _w_u2009-u20091,u2009_w_,u2009_w_u2009-u20091,u2009_w_,u2009...,u2009_w_u2009-u20091 characters respectively. Each color constraint is represented by a character "E" or "N", where "E" means "u2009=u2009" and "N" means "u2009≠u2009". The color constraints listed in the order they are depicted on the picture. Output If there is a coloring that satisfies at least of the color constraints, print "YES" (without quotes) in the first line. In each of the next _h_ lines, print _w_ integers describing the coloring. Otherwise, print "NO" (without quotes). Examples Input 3 4 4 ENE NNEE NEE ENEN ENN Output YES 1 1 2 2 3 4 1 1 3 3 2 4
2,500
false
false
false
false
false
true
false
false
false
false
8,641
932D
Problem - 932D - 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 trees *2200 No tag edit access → Contest materials . Support _Q_ queries of following two types: Add a new node (index _cnt_u2009+u20091) with weight _W_ and add edge between node _R_ and this node. Output the maximum length of sequence of nodes which 1. starts with _R_. 2. Every node in the sequence is an ancestor of its predecessor. 3. Sum of weight of nodes in sequence does not exceed _X_. 4. For some nodes _i_,u2009_j_ that are consecutive in the sequence if _i_ is an ancestor of _j_ then _w_
2,200
false
false
false
true
false
false
false
true
false
false
5,975
990B
You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them. You know that you have $$$n$$$ bacteria in the Petri dish and size of the $$$i$$$-th bacteria is $$$a_i$$$. Also you know intergalactic positive integer constant $$$K$$$. The $$$i$$$-th bacteria can swallow the $$$j$$$-th bacteria if and only if $$$a_i > a_j$$$ and $$$a_i le a_j + K$$$. The $$$j$$$-th bacteria disappear, but the $$$i$$$-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria $$$i$$$ can swallow any bacteria $$$j$$$ if $$$a_i > a_j$$$ and $$$a_i le a_j + K$$$. The swallow operations go one after another. For example, the sequence of bacteria sizes $$$a=[101, 53, 42, 102, 101, 55, 54]$$$ and $$$K=1$$$. The one of possible sequences of swallows is: $$$[101, 53, 42, 102, underline{101}, 55, 54]$$$ $$$ o$$$ $$$[101, underline{53}, 42, 102, 55, 54]$$$ $$$ o$$$ $$$[underline{101}, 42, 102, 55, 54]$$$ $$$ o$$$ $$$[42, 102, 55, underline{54}]$$$ $$$ o$$$ $$$[42, 102, 55]$$$. In total there are $$$3$$$ bacteria remained in the Petri dish. Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope. Input The first line contains two space separated positive integers $$$n$$$ and $$$K$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$1 le K le 10^6$$$) — number of bacteria and intergalactic constant $$$K$$$. The second line contains $$$n$$$ space separated integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^6$$$) — sizes of bacteria you have. Note The first example is clarified in the problem statement. In the second example an optimal possible sequence of swallows is: $$$[20, 15, 10, 15, underline{20}, 25]$$$ $$$ o$$$ $$$[20, 15, 10, underline{15}, 25]$$$ $$$ o$$$ $$$[20, 15, underline{10}, 25]$$$ $$$ o$$$ $$$[20, underline{15}, 25]$$$ $$$ o$$$ $$$[underline{20}, 25]$$$ $$$ o$$$ $$$[25]$$$. In the third example no bacteria can swallow any other bacteria.
1,200
false
true
false
false
false
false
false
false
true
false
5,751
1796D
You are given an array $$$a_1, a_2, dots, a_n$$$, consisting of $$$n$$$ integers. You are also given two integers $$$k$$$ and $$$x$$$. You have to perform the following operation exactly once: add $$$x$$$ to the elements on exactly $$$k$$$ distinct positions, and subtract $$$x$$$ from all the others. For example, if $$$a = [2, -1, 2, 3]$$$, $$$k = 1$$$, $$$x = 2$$$, and we have picked the first element, then after the operation the array $$$a = [4, -3, 0, 1]$$$. Let $$$f(a)$$$ be the maximum possible sum of a subarray of $$$a$$$. The subarray of $$$a$$$ is a contiguous part of the array $$$a$$$, i.u2009e. the array $$$a_i, a_{i + 1}, dots, a_j$$$ for some $$$1 le i le j le n$$$. An empty subarray should also be considered, it has sum $$$0$$$. Let the array $$$a'$$$ be the array $$$a$$$ after applying the aforementioned operation. Apply the operation in such a way that $$$f(a')$$$ is the maximum possible, and print the maximum possible value of $$$f(a')$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$k$$$ and $$$x$$$ ($$$1 le n le 2 cdot 10^5$$$; $$$0 le k le min(20, n)$$$; $$$-10^9 le x le 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$-10^9 le a_i le 10^9$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$.
2,000
false
true
false
true
true
false
false
false
false
false
1,499
1722D
There are $$$n$$$ people in a horizontal line, each looking either to the left or the right. Each person counts the number of people in the direction they are looking. The value of the line is the sum of each person's count. For example, in the arrangement LRRLL, where L stands for a person looking left and R stands for a person looking right, the counts for each person are $$$[0, 3, 2, 3, 4]$$$, and the value is $$$0+3+2+3+4=12$$$. You are given the initial arrangement of people in the line. For each $$$k$$$ from $$$1$$$ to $$$n$$$, determine the maximum value of the line if you can change the direction of at most $$$k$$$ people. Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 2cdot10^5$$$)xa0— the length of the line. The following line contains a string consisting of $$$n$$$ characters, each of which is either L or R, representing a person facing left or right, respectivelyxa0— the description of the line. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot10^5$$$. Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++). Output For each test case, output $$$n$$$ space-separated non-negative integersxa0— the maximum value of the line if you can change the direction of at most $$$k$$$ people for each $$$k$$$ from $$$1$$$ to $$$n$$$, inclusive. Example Input 6 3 LLR 5 LRRLL 1 L 12 LRRRLLLRLLRL 10 LLLLLRRRRR 9 LRLRLRLRL Output 3 5 5 16 16 16 16 16 0 86 95 98 101 102 102 102 102 102 102 102 102 29 38 45 52 57 62 65 68 69 70 44 50 54 56 56 56 56 56 56 Note In the first test case: $$$k=1$$$: change the direction of $$$1$$$ person to make the line RLR. The total value is $$$2+1+0=3$$$. $$$k=2$$$: change the direction of $$$2$$$ people to make the line RLL. The total value is $$$2+1+2=5$$$. $$$k=3$$$: change the direction of $$$2$$$ people to make the line RLL. The total value is $$$2+1+2=5$$$. Note that you have to change the direction of at most $$$k$$$ people. In the second test case, it is optimal to only change the direction of the first person for all $$$k$$$ from $$$1$$$ to $$$5$$$ (that is, make the line RRRLL).
1,100
false
true
false
false
false
false
false
false
true
false
1,950
1731E
You are given an initially empty undirected graph with $$$n$$$ nodes, numbered from $$$1$$$ to $$$n$$$ (i.xa0e. $$$n$$$ nodes and $$$0$$$ edges). You want to add $$$m$$$ edges to the graph, so the graph won't contain any self-loop or multiple edges. If an edge connecting two nodes $$$u$$$ and $$$v$$$ is added, its weight must be equal to the greatest common divisor of $$$u$$$ and $$$v$$$, i.xa0e. $$$gcd(u, v)$$$. In order to add edges to the graph, you can repeat the following process any number of times (possibly zero): choose an integer $$$k ge 1$$$; add exactly $$$k$$$ edges to the graph, each having a weight equal to $$$k + 1$$$. Adding these $$$k$$$ edges costs $$$k + 1$$$ in total. Note that you can't create self-loops or multiple edges. Also, if you can't add $$$k$$$ edges of weight $$$k + 1$$$, you can't choose such $$$k$$$. For example, if you can add $$$5$$$ more edges to the graph of weight $$$6$$$, you may add them, and it will cost $$$6$$$ for the whole pack of $$$5$$$ edges. But if you can only add $$$4$$$ edges of weight $$$6$$$ to the graph, you can't perform this operation for $$$k = 5$$$. Given two integers $$$n$$$ and $$$m$$$, find the minimum total cost to form a graph of $$$n$$$ vertices and exactly $$$m$$$ edges using the operation above. If such a graph can't be constructed, output $$$-1$$$. Note that the final graph may consist of several connected components. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 10^4$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 leq n leq 10^6$$$; $$$1 leq m leq frac{n(n-1)}{2}$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, print the minimum cost to build the graph, or $$$-1$$$ if you can't build such a graph. Note In the first test case, we can add an edge between the vertices $$$2$$$ and $$$4$$$ with $$$gcd = 2$$$. This is the only possible way to add $$$1$$$ edge that will cost $$$2$$$. In the second test case, there is no way to add $$$10$$$ edges, so the answer is $$$-1$$$. In the third test case, we can add the following edges: $$$k = 1$$$: edge of weight $$$2$$$ between vertices $$$2$$$ and $$$4$$$ ($$$gcd(2, 4) = 2$$$). Cost: $$$2$$$; $$$k = 1$$$: edge of weight $$$2$$$ between vertices $$$4$$$ and $$$6$$$ ($$$gcd(4, 6) = 2$$$). Cost: $$$2$$$; $$$k = 2$$$: edges of weight $$$3$$$: $$$(3, 6)$$$ and $$$(3, 9)$$$ ($$$gcd(3, 6) = gcd(3, 9) = 3$$$). Cost: $$$3$$$. As a result, we added $$$1 + 1 + 2 = 4$$$ edges with total cost $$$2 + 2 + 3 = 7$$$, which is the minimal possible cost.
2,000
true
true
false
true
false
false
false
false
false
false
1,901
1290F
You are given $$$n$$$ pairwise non-collinear two-dimensional vectors. You can make shapes in the two-dimensional plane with these vectors in the following fashion: 1. Start at the origin $$$(0, 0)$$$. 2. Choose a vector and add the segment of the vector to the current point. For example, if your current point is at $$$(x, y)$$$ and you choose the vector $$$(u, v)$$$, draw a segment from your current point to the point at $$$(x + u, y + v)$$$ and set your current point to $$$(x + u, y + v)$$$. 3. Repeat step 2 until you reach the origin again. You can reuse a vector as many times as you want. Count the number of different, non-degenerate (with an area greater than $$$0$$$) and convex shapes made from applying the steps, such that the shape can be contained within a $$$m imes m$$$ square, and the vectors building the shape are in counter-clockwise fashion. Since this number can be too large, you should calculate it by modulo $$$998244353$$$. Two shapes are considered the same if there exists some parallel translation of the first shape to another. A shape can be contained within a $$$m imes m$$$ square if there exists some parallel translation of this shape so that every point $$$(u, v)$$$ inside or on the border of the shape satisfies $$$0 leq u, v leq m$$$. Input The first line contains two integers $$$n$$$ and $$$m$$$ xa0— the number of vectors and the size of the square ($$$1 leq n leq 5$$$, $$$1 leq m leq 10^9$$$). Each of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ xa0— the $$$x$$$-coordinate and $$$y$$$-coordinate of the $$$i$$$-th vector ($$$x_i, y_i leq 4$$$, $$$(x_i, y_i) eq (0, 0)$$$). It is guaranteed, that no two vectors are parallel, so for any two indices $$$i$$$ and $$$j$$$ such that $$$1 leq i < j leq n$$$, there is no real value $$$k$$$ such that $$$x_i cdot k = x_j$$$ and $$$y_i cdot k = y_j$$$. Output Output a single integer xa0— the number of satisfiable shapes by modulo $$$998244353$$$. Examples Input 3 1776966 -1 0 3 3 0 -2 Input 4 15 -4 -4 -1 1 -1 -4 4 3 Input 5 10 3 -4 4 -3 1 -3 2 -3 -3 -4 Input 5 1000000000 -2 4 2 -3 0 -4 2 4 -1 -3 Note The shapes for the first sample are: The only shape for the second sample is: The only shape for the fourth sample is:
3,500
false
false
false
true
false
false
false
false
false
false
4,241
1784E
Alice and Bob are playing an infinite game consisting of sets. Each set consists of rounds. In each round, one of the players wins. The first player to win two rounds in a set wins this set. Thus, a set always ends with the score of $$$2:0$$$ or $$$2:1$$$ in favor of one of the players. Let's call a game scenario a finite string $$$s$$$ consisting of characters 'a' and 'b'. Consider an infinite string formed with repetitions of string $$$s$$$: $$$sss ldots$$$ Suppose that Alice and Bob play rounds according to this infinite string, left to right. If a character of the string $$$sss ldots$$$ is 'a', then Alice wins the round; if it's 'b', Bob wins the round. As soon as one of the players wins two rounds, the set ends in their favor, and a new set starts from the next round. Let's define $$$a_i$$$ as the number of sets won by Alice among the first $$$i$$$ sets while playing according to the given scenario. Let's also define $$$r$$$ as the limit of ratio $$$frac{a_i}{i}$$$ as $$$i ightarrow infty$$$. If $$$r > frac{1}{2}$$$, we'll say that scenario $$$s$$$ is winning for Alice. If $$$r = frac{1}{2}$$$, we'll say that scenario $$$s$$$ is tied. If $$$r < frac{1}{2}$$$, we'll say that scenario $$$s$$$ is winning for Bob. You are given a string $$$s$$$ consisting of characters 'a', 'b', and '?'. Consider all possible ways of replacing every '?' with 'a' or 'b' to obtain a string consisting only of characters 'a' and 'b'. Count how many of them result in a scenario winning for Alice, how many result in a tied scenario, and how many result in a scenario winning for Bob. Print these three numbers modulo $$$998,244,353$$$. Input The only line contains a single string $$$s$$$xa0($$$1 le s le 200$$$), consisting of characters 'a', 'b', and '?'. Output Print three integers: how many ways result in a scenario winning for Alice, how many result in a tied scenario, and how many result in a scenario winning for Bob, modulo $$$998,244,353$$$.
3,100
false
false
false
true
false
false
true
false
false
false
1,568
491A
Hiking club "Up the hill" just returned from a walk. Now they are trying to remember which hills they've just walked through. It is known that there were _N_ stops, all on different integer heights between 1 and _N_ kilometers (inclusive) above the sea level. On the first day they've traveled from the first stop to the second stop, on the second day they've traveled from the second to the third and so on, and on the last day they've traveled from the stop _N_u2009-u20091 to the stop _N_ and successfully finished their expedition. They are trying to find out which heights were their stops located at. They have an entry in a travel journal specifying how many days did they travel up the hill, and how many days did they walk down the hill. Help them by suggesting some possible stop heights satisfying numbers from the travel journal. Input In the first line there is an integer non-negative number _A_ denoting the number of days of climbing up the hill. Second line contains an integer non-negative number _B_xa0— the number of days of walking down the hill (_A_u2009+u2009_B_u2009+u20091u2009=u2009_N_, 1u2009≤u2009_N_u2009≤u2009100u2009000). Output Output _N_ space-separated distinct integers from 1 to _N_ inclusive, denoting possible heights of the stops in order of visiting.
1,000
false
false
true
false
false
true
false
false
false
false
7,871
2037G
You are exploring the stunning region of Natlan! This region consists of $$$n$$$ cities, and each city is rated with an attractiveness $$$a_i$$$. A directed edge exists from City $$$i$$$ to City $$$j$$$ if and only if $$$i < j$$$ and $$$gcd(a_i,a_j) eq 1$$$, where $$$gcd(x, y)$$$ denotes the — the number of cities. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$2 leq a_i leq 10^6$$$) — the attractiveness of each city. Output Output the total number of distinct paths you can take to reach City $$$n$$$, modulo $$$998,244,353$$$. Examples Input 5 4 196 2662 2197 121 Note In the first example, the five paths are the following: City $$$1 ightarrow$$$ City $$$5$$$ City $$$1 ightarrow$$$ City $$$2 ightarrow$$$ City $$$5$$$ City $$$1 ightarrow$$$ City $$$2 ightarrow$$$ City $$$3 ightarrow$$$ City $$$5$$$ City $$$1 ightarrow$$$ City $$$2 ightarrow$$$ City $$$4 ightarrow$$$ City $$$5$$$ City $$$1 ightarrow$$$ City $$$4 ightarrow$$$ City $$$5$$$ In the second example, the two paths are the following: City $$$1 ightarrow$$$ City $$$3 ightarrow$$$ City $$$5$$$ City $$$1 ightarrow$$$ City $$$2 ightarrow$$$ City $$$3 ightarrow$$$ City $$$5$$$
2,000
true
false
false
true
true
false
false
false
false
false
39
126C
After years of hard work scientists invented an absolutely new e-reader display. The new display has a larger resolution, consumes less energy and its production is cheaper. And besides, one can bend it. The only inconvenience is highly unusual management. For that very reason the developers decided to leave the e-readers' software to programmers. The display is represented by _n_u2009×u2009_n_ square of pixels, each of which can be either black or white. The display rows are numbered with integers from 1 to _n_ upside down, the columns are numbered with integers from 1 to _n_ from the left to the right. The display can perform commands like "_x_,u2009_y_". When a traditional display fulfills such command, it simply inverts a color of (_x_,u2009_y_), where _x_ is the row number and _y_ is the column number. But in our new display every pixel that belongs to at least one of the segments (_x_,u2009_x_)u2009-u2009(_x_,u2009_y_) and (_y_,u2009_y_)u2009-u2009(_x_,u2009_y_) (both ends of both segments are included) inverts a color. For example, if initially a display 5u2009×u20095 in size is absolutely white, then the sequence of commands (1,u20094), (3,u20095), (5,u20091), (3,u20093) leads to the following changes: You are an e-reader software programmer and you should calculate minimal number of commands needed to display the picture. You can regard all display pixels as initially white. Input The first line contains number _n_ (1u2009≤u2009_n_u2009≤u20092000). Next _n_ lines contain _n_ characters each: the description of the picture that needs to be shown. "0" represents the white color and "1" represents the black color.
2,000
false
true
false
false
false
true
false
false
false
false
9,375
708A
Problem - 708A - 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 implementation strings *1200 No tag edit access → Contest materials consisting of lowercase English letters. Output Print the lexicographically minimum string that can be obtained from _s_ by shifting letters of exactly one non-empty substring. Examples Input codeforces Output bncdenqbdr Input abacaba Output aaacaba Note String _s_ is lexicographically smaller than some other string _t_ of the same length if there exists some 1u2009≤u2009_i_u2009≤u2009_s_, such that _s_1u2009=u2009_t_1,u2009_s_2u2009=u2009_t_2,u2009...,u2009_s__i_u2009-u20091u2009=u2009_t__i_u2009-u20091, and _s__i_u2009<u2009_t__i_.
1,200
false
true
true
false
false
true
false
false
false
false
6,989